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