1 /*
2 * Copyright (C) 2020 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 #pragma once
18
19 #include <stdlib.h>
20
21 namespace android {
22 namespace vold {
23
24 // Struct representing an encryption algorithm supported by vold.
25 // "config_name" represents the name we give the algorithm in
26 // read-only properties and fstab files
27 // "kernel_name" is the name we present to the Linux kernel
28 // "keysize" is the size of the key in bytes.
29 struct CryptoType {
30 // We should only be constructing CryptoTypes as part of
31 // supported_crypto_types[]. We do it via this pseudo-builder pattern,
32 // which isn't pure or fully protected as a concession to being able to
33 // do it all at compile time. Add new CryptoTypes in
34 // supported_crypto_types[] below.
CryptoTypeCryptoType35 constexpr CryptoType() : CryptoType(nullptr, nullptr, 0xFFFFFFFF) {}
set_keysizeCryptoType36 constexpr CryptoType set_keysize(size_t size) const {
37 return CryptoType(this->config_name, this->kernel_name, size);
38 }
set_config_nameCryptoType39 constexpr CryptoType set_config_name(const char* property) const {
40 return CryptoType(property, this->kernel_name, this->keysize);
41 }
set_kernel_nameCryptoType42 constexpr CryptoType set_kernel_name(const char* crypto) const {
43 return CryptoType(this->config_name, crypto, this->keysize);
44 }
45
get_config_nameCryptoType46 constexpr const char* get_config_name() const { return config_name; }
get_kernel_nameCryptoType47 constexpr const char* get_kernel_name() const { return kernel_name; }
get_keysizeCryptoType48 constexpr size_t get_keysize() const { return keysize; }
49
50 private:
51 const char* config_name;
52 const char* kernel_name;
53 size_t keysize;
54
CryptoTypeCryptoType55 constexpr CryptoType(const char* property, const char* crypto, size_t ksize)
56 : config_name(property), kernel_name(crypto), keysize(ksize) {}
57 };
58
59 // Use the named android property to look up a type from the table
60 // If the property is not set or matches no table entry, return the default.
61 const CryptoType& lookup_crypto_algorithm(const CryptoType table[], int table_len,
62 const CryptoType& default_alg, const char* property);
63
64 // Some useful types
65
66 constexpr CryptoType invalid_crypto_type = CryptoType();
67
68 constexpr CryptoType aes_256_xts = CryptoType()
69 .set_config_name("aes-256-xts")
70 .set_kernel_name("aes-xts-plain64")
71 .set_keysize(64);
72
73 constexpr CryptoType adiantum = CryptoType()
74 .set_config_name("adiantum")
75 .set_kernel_name("xchacha12,aes-adiantum-plain64")
76 .set_keysize(32);
77
78 // Support compile-time validation of a crypto type table
79
80 template <typename T, size_t N>
array_length(T (&)[N])81 constexpr size_t array_length(T (&)[N]) {
82 return N;
83 }
84
isValidCryptoType(size_t max_keylen,const CryptoType & crypto_type)85 constexpr bool isValidCryptoType(size_t max_keylen, const CryptoType& crypto_type) {
86 return ((crypto_type.get_config_name() != nullptr) &&
87 (crypto_type.get_kernel_name() != nullptr) &&
88 (crypto_type.get_keysize() <= max_keylen));
89 }
90
91 // Confirms that all supported_crypto_types have a small enough keysize and
92 // had both set_config_name() and set_kernel_name() called.
93 // Note in C++11 that constexpr functions can only have a single line.
94 // So our code is a bit convoluted (using recursion instead of a loop),
95 // but it's asserting at compile time that all of our key lengths are valid.
validateSupportedCryptoTypes(size_t max_keylen,const CryptoType types[],size_t len)96 constexpr bool validateSupportedCryptoTypes(size_t max_keylen, const CryptoType types[],
97 size_t len) {
98 return len == 0 || (isValidCryptoType(max_keylen, types[len - 1]) &&
99 validateSupportedCryptoTypes(max_keylen, types, len - 1));
100 }
101
102 } // namespace vold
103 } // namespace android
104