blob: 878b4ab2a6f83896c715d181b98327a75c1491be [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
Pavel Grafove2e2d302017-08-01 17:15:53 +010020#include "KeyBuffer.h"
Paul Crowley26a53882017-10-26 11:16:39 -070021#include "KeyStorage.h"
Pavel Grafove2e2d302017-08-01 17:15:53 +010022
Paul Crowley77df7f22020-01-23 15:29:30 -080023#include <fscrypt/fscrypt.h>
24
Pavel Grafove2e2d302017-08-01 17:15:53 +010025#include <memory>
Paul Crowley14c8c072018-09-18 13:30:21 -070026#include <string>
Paul Crowleyf71ace32016-06-02 11:01:19 -070027
28namespace android {
29namespace vold {
30
Paul Crowley77df7f22020-01-23 15:29:30 -080031using namespace android::fscrypt;
32
Pavel Grafove2e2d302017-08-01 17:15:53 +010033bool randomKey(KeyBuffer* key);
Eric Biggersf3dc4202019-09-30 13:05:58 -070034
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080035bool generateStorageKey(const EncryptionOptions& options, KeyBuffer* key);
36
Eric Biggersf3dc4202019-09-30 13:05:58 -070037bool isFsKeyringSupported(void);
38
Paul Crowley77df7f22020-01-23 15:29:30 -080039// Install a file-based encryption key to the kernel, for use by encrypted files
40// on the specified filesystem using the specified encryption policy version.
41//
42// For v1 policies, we use FS_IOC_ADD_ENCRYPTION_KEY if the kernel supports it.
43// Otherwise we add the key to the legacy global session keyring.
44//
45// For v2 policies, we always use FS_IOC_ADD_ENCRYPTION_KEY; it's the only way
46// the kernel supports.
47//
48// Returns %true on success, %false on failure. On success also sets *policy
49// to the EncryptionPolicy used to refer to this key.
50bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
51 const KeyBuffer& key, EncryptionPolicy* policy);
52
53// Evict a file-based encryption key from the kernel.
54//
55// We use FS_IOC_REMOVE_ENCRYPTION_KEY if the kernel supports it. Otherwise we
56// remove the key from the legacy global session keyring.
57//
58// In the latter case, the caller is responsible for dropping caches.
59bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy);
60
61bool retrieveKey(bool create_if_absent, const KeyAuthentication& key_authentication,
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080062 const std::string& key_path, const std::string& tmp_path,
63 const EncryptionOptions& options, KeyBuffer* key, bool keepOld = true);
Paul Crowleyf71ace32016-06-02 11:01:19 -070064
65} // namespace vold
66} // namespace android
67
68#endif