blob: 16aaf99454afd919acd01bf1d1043638b57ea00c [file] [log] [blame]
Paul Crowleyf71ace32016-06-02 11:01:19 -07001/*
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_VOLD_KEYUTIL_H
18#define ANDROID_VOLD_KEYUTIL_H
19
Paul Crowley4eac2642020-02-12 11:04:05 -080020#include "CryptoType.h"
Pavel Grafove2e2d302017-08-01 17:15:53 +010021#include "KeyBuffer.h"
Paul Crowley26a53882017-10-26 11:16:39 -070022#include "KeyStorage.h"
Pavel Grafove2e2d302017-08-01 17:15:53 +010023
Paul Crowley77df7f22020-01-23 15:29:30 -080024#include <fscrypt/fscrypt.h>
25
Pavel Grafove2e2d302017-08-01 17:15:53 +010026#include <memory>
Paul Crowley14c8c072018-09-18 13:30:21 -070027#include <string>
Paul Crowleyf71ace32016-06-02 11:01:19 -070028
29namespace android {
30namespace vold {
31
Paul Crowley77df7f22020-01-23 15:29:30 -080032using namespace android::fscrypt;
33
Paul Crowley4eac2642020-02-12 11:04:05 -080034// Description of how to generate a key when needed.
35struct KeyGeneration {
36 size_t keysize;
37 bool allow_gen;
38 bool use_hw_wrapped_key;
39};
Eric Biggersf3dc4202019-09-30 13:05:58 -070040
Paul Crowley4eac2642020-02-12 11:04:05 -080041// Generate a key as specified in KeyGeneration
42bool generateStorageKey(const KeyGeneration& gen, KeyBuffer* key);
43
44// Returns KeyGeneration suitable for key as described in EncryptionOptions
45const KeyGeneration makeGen(const EncryptionOptions& options);
46
47// Returns KeyGeneration suitable for key as described in CryptoType
48const KeyGeneration makeGen(const CryptoType& crypto);
49
50// Returns a key with allow_gen false so generateStorageKey returns false;
51// this is used to indicate to retrieveOrGenerateKey that a key should not
52// be generated.
53const KeyGeneration neverGen();
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080054
Eric Biggersf3dc4202019-09-30 13:05:58 -070055bool isFsKeyringSupported(void);
56
Paul Crowley77df7f22020-01-23 15:29:30 -080057// Install a file-based encryption key to the kernel, for use by encrypted files
58// on the specified filesystem using the specified encryption policy version.
59//
60// For v1 policies, we use FS_IOC_ADD_ENCRYPTION_KEY if the kernel supports it.
61// Otherwise we add the key to the legacy global session keyring.
62//
63// For v2 policies, we always use FS_IOC_ADD_ENCRYPTION_KEY; it's the only way
64// the kernel supports.
65//
66// Returns %true on success, %false on failure. On success also sets *policy
67// to the EncryptionPolicy used to refer to this key.
68bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
69 const KeyBuffer& key, EncryptionPolicy* policy);
70
71// Evict a file-based encryption key from the kernel.
72//
73// We use FS_IOC_REMOVE_ENCRYPTION_KEY if the kernel supports it. Otherwise we
74// remove the key from the legacy global session keyring.
75//
76// In the latter case, the caller is responsible for dropping caches.
77bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy);
78
Paul Crowley4eac2642020-02-12 11:04:05 -080079bool retrieveOrGenerateKey(const std::string& key_path, const std::string& tmp_path,
80 const KeyAuthentication& key_authentication, const KeyGeneration& gen,
81 KeyBuffer* key, bool keepOld = true);
Paul Crowleyf71ace32016-06-02 11:01:19 -070082
83} // namespace vold
84} // namespace android
85
86#endif