blob: 5940b8a8f9aeeaa3d69fc99c5fb0cde5d91f8f9d [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 Crowley4eac2642020-02-12 11:04:05 -080031// Description of how to generate a key when needed.
32struct KeyGeneration {
33 size_t keysize;
34 bool allow_gen;
35 bool use_hw_wrapped_key;
36};
Eric Biggersf3dc4202019-09-30 13:05:58 -070037
Paul Crowley4eac2642020-02-12 11:04:05 -080038// Generate a key as specified in KeyGeneration
39bool generateStorageKey(const KeyGeneration& gen, KeyBuffer* key);
40
Paul Crowley4eac2642020-02-12 11:04:05 -080041// Returns a key with allow_gen false so generateStorageKey returns false;
42// this is used to indicate to retrieveOrGenerateKey that a key should not
43// be generated.
44const KeyGeneration neverGen();
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080045
Eric Biggersf3dc4202019-09-30 13:05:58 -070046bool isFsKeyringSupported(void);
47
Paul Crowley77df7f22020-01-23 15:29:30 -080048// Install a file-based encryption key to the kernel, for use by encrypted files
49// on the specified filesystem using the specified encryption policy version.
50//
51// For v1 policies, we use FS_IOC_ADD_ENCRYPTION_KEY if the kernel supports it.
Nikita Ioffe1c6731c2020-02-28 19:50:31 +000052// Otherwise we add the key to the global session keyring as a "logon" key.
Paul Crowley77df7f22020-01-23 15:29:30 -080053//
54// For v2 policies, we always use FS_IOC_ADD_ENCRYPTION_KEY; it's the only way
55// the kernel supports.
56//
Nikita Ioffe1c6731c2020-02-28 19:50:31 +000057// If kernel supports FS_IOC_ADD_ENCRYPTION_KEY, also installs key of
58// fscrypt-provisioning type to the global session keyring. This makes it
59// possible to unmount and then remount mountpoint without losing the file-based
60// key.
61//
Paul Crowley77df7f22020-01-23 15:29:30 -080062// Returns %true on success, %false on failure. On success also sets *policy
63// to the EncryptionPolicy used to refer to this key.
Paul Crowleyf4430382020-04-05 19:34:31 -070064bool installKey(const std::string& mountpoint, const android::fscrypt::EncryptionOptions& options,
65 const KeyBuffer& key, android::fscrypt::EncryptionPolicy* policy);
Paul Crowley77df7f22020-01-23 15:29:30 -080066
67// Evict a file-based encryption key from the kernel.
68//
Nikita Ioffe1c6731c2020-02-28 19:50:31 +000069// This undoes the effect of installKey().
Paul Crowley77df7f22020-01-23 15:29:30 -080070//
Nikita Ioffe1c6731c2020-02-28 19:50:31 +000071// If the kernel doesn't support the filesystem-level keyring, the caller is
72// responsible for dropping caches.
Paul Crowleyf4430382020-04-05 19:34:31 -070073bool evictKey(const std::string& mountpoint, const android::fscrypt::EncryptionPolicy& policy);
Paul Crowley77df7f22020-01-23 15:29:30 -080074
Eric Biggersc4939032020-10-29 12:59:28 -070075// Retrieves the key from the named directory, or generates it if it doesn't
Eric Biggersf74373b2020-11-05 19:58:26 -080076// exist.
Paul Crowley4eac2642020-02-12 11:04:05 -080077bool retrieveOrGenerateKey(const std::string& key_path, const std::string& tmp_path,
78 const KeyAuthentication& key_authentication, const KeyGeneration& gen,
Eric Biggersf74373b2020-11-05 19:58:26 -080079 KeyBuffer* key);
Paul Crowleyf71ace32016-06-02 11:01:19 -070080
Nikita Ioffe1c6731c2020-02-28 19:50:31 +000081// Re-installs a file-based encryption key of fscrypt-provisioning type from the
82// global session keyring back into fs keyring of the mountpoint.
Paul Crowleyf4430382020-04-05 19:34:31 -070083bool reloadKeyFromSessionKeyring(const std::string& mountpoint,
84 const android::fscrypt::EncryptionPolicy& policy);
Nikita Ioffe1c6731c2020-02-28 19:50:31 +000085
Paul Crowleyf71ace32016-06-02 11:01:19 -070086} // namespace vold
87} // namespace android
88
89#endif