blob: f74865f8d003cf42c2e22343fa4ae436be52a7b5 [file] [log] [blame]
Ethan Yonkere9afc3d2018-08-30 15:16:27 -05001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_TWRP_KEYSTORAGE_H
18#define ANDROID_TWRP_KEYSTORAGE_H
19
Peter Cai90edd2e2019-05-23 16:32:22 +080020#include "Keymaster4.h"
Ethan Yonkere9afc3d2018-08-30 15:16:27 -050021#include "KeyBuffer.h"
Peter Cai90edd2e2019-05-23 16:32:22 +080022#include <ext4_utils/ext4_crypt.h>
Ethan Yonkere9afc3d2018-08-30 15:16:27 -050023
24#include <string>
25
26namespace android {
27namespace vold {
28
Peter Cai90edd2e2019-05-23 16:32:22 +080029namespace km = ::android::hardware::keymaster::V4_0;
30
Ethan Yonkere9afc3d2018-08-30 15:16:27 -050031// Represents the information needed to decrypt a disk encryption key.
32// If "token" is nonempty, it is passed in as a required Gatekeeper auth token.
33// If "token" and "secret" are nonempty, "secret" is appended to the application-specific
34// binary needed to unlock.
35// If only "secret" is nonempty, it is used to decrypt in a non-Keymaster process.
36class KeyAuthentication {
37 public:
38 KeyAuthentication(std::string t, std::string s) : token{t}, secret{s} {};
39
40 bool usesKeymaster() const { return !token.empty() || secret.empty(); };
41
42 const std::string token;
43 const std::string secret;
44};
45
Peter Cai90edd2e2019-05-23 16:32:22 +080046enum class KeyType {
47 DE_SYS,
48 DE_USER,
49 CE_USER
50};
51
Ethan Yonkere9afc3d2018-08-30 15:16:27 -050052extern const KeyAuthentication kEmptyAuthentication;
53
54// Checks if path "path" exists.
55bool pathExists(const std::string& path);
56
57bool createSecdiscardable(const std::string& path, std::string* hash);
58bool readSecdiscardable(const std::string& path, std::string* hash);
59
60// Create a directory at the named path, and store "key" in it,
61// in such a way that it can only be retrieved via Keymaster and
62// can be securely deleted.
63// It's safe to move/rename the directory after creation.
64bool storeKey(const std::string& dir, const KeyAuthentication& auth, const KeyBuffer& key);
65
66// Create a directory at the named path, and store "key" in it as storeKey
67// This version creates the key in "tmp_path" then atomically renames "tmp_path"
68// to "key_path" thereby ensuring that the key is either stored entirely or
69// not at all.
70bool storeKeyAtomically(const std::string& key_path, const std::string& tmp_path,
71 const KeyAuthentication& auth, const KeyBuffer& key);
72
73// Retrieve the key from the named directory.
74bool retrieveKey(const std::string& dir, const KeyAuthentication& auth, KeyBuffer* key);
75
76// Securely destroy the key stored in the named directory and delete the directory.
77bool destroyKey(const std::string& dir);
78
79bool runSecdiscardSingle(const std::string& file);
Peter Cai90edd2e2019-05-23 16:32:22 +080080
81bool generateWrappedKey(userid_t user_id, KeyType key_type, KeyBuffer* key);
82bool getEphemeralWrappedKey(km::KeyFormat format, KeyBuffer& kmKey, KeyBuffer* key);
Ethan Yonkere9afc3d2018-08-30 15:16:27 -050083} // namespace vold
84} // namespace android
85
86#endif