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 #pragma once
17
18 #include <gtest/gtest.h>
19 #include <stdint.h>
20
21 #include <string>
22 #include <vector>
23
24 namespace android {
25 namespace kernel {
26
27 class Cipher {
28 public:
~Cipher()29 virtual ~Cipher() {}
Encrypt(const std::vector<uint8_t> & key,const uint8_t * iv,const uint8_t * src,uint8_t * dst,int nbytes)30 bool Encrypt(const std::vector<uint8_t> &key, const uint8_t *iv,
31 const uint8_t *src, uint8_t *dst, int nbytes) const {
32 if (key.size() != keysize()) {
33 ADD_FAILURE() << "Bad key size";
34 return false;
35 }
36 return DoCrypt(key.data(), iv, src, dst, nbytes, true);
37 }
Decrypt(const std::vector<uint8_t> & key,const uint8_t * iv,const uint8_t * src,uint8_t * dst,int nbytes)38 bool Decrypt(const std::vector<uint8_t> &key, const uint8_t *iv,
39 const uint8_t *src, uint8_t *dst, int nbytes) const {
40 if (key.size() != keysize()) {
41 ADD_FAILURE() << "Bad key size";
42 return false;
43 }
44 return DoCrypt(key.data(), iv, src, dst, nbytes, false);
45 }
46 virtual int keysize() const = 0;
47 virtual int ivsize() const = 0;
48
49 protected:
50 virtual bool DoCrypt(const uint8_t *key, const uint8_t *iv,
51 const uint8_t *src, uint8_t *dst, int nbytes,
52 bool encrypt) const = 0;
53 };
54
55 // aes_256_xts.cpp
56
57 constexpr int kAesBlockSize = 16;
58 constexpr int kAes256KeySize = 32;
59 constexpr int kAes256XtsKeySize = 2 * kAes256KeySize;
60
61 class Aes256XtsCipher : public Cipher {
62 public:
keysize()63 int keysize() const { return kAes256XtsKeySize; }
ivsize()64 int ivsize() const { return kAesBlockSize; }
65
66 private:
67 bool DoCrypt(const uint8_t *key, const uint8_t *iv, const uint8_t *src,
68 uint8_t *dst, int nbytes, bool encrypt) const;
69 };
70
71 // adiantum.cpp
72
73 constexpr int kAdiantumKeySize = 32;
74
75 // It's variable-length in general, but the Linux kernel always uses 32.
76 constexpr int kAdiantumIVSize = 32;
77
78 class AdiantumCipher : public Cipher {
79 public:
keysize()80 int keysize() const { return kAdiantumKeySize; }
ivsize()81 int ivsize() const { return kAdiantumIVSize; }
82
83 private:
84 bool DoCrypt(const uint8_t *key, const uint8_t *iv, const uint8_t *src,
85 uint8_t *dst, int nbytes, bool encrypt) const;
86 };
87
88 // utils.cpp
89
90 std::string Errno();
91
92 void DeleteRecursively(const std::string &path);
93
94 void RandomBytesForTesting(std::vector<uint8_t> &bytes);
95
96 std::vector<uint8_t> GenerateTestKey(size_t size);
97
98 std::string BytesToHex(const std::vector<uint8_t> &bytes);
99
100 template <size_t N>
BytesToHex(const uint8_t (& array)[N])101 static inline std::string BytesToHex(const uint8_t (&array)[N]) {
102 return BytesToHex(std::vector<uint8_t>(&array[0], &array[N]));
103 }
104
105 bool GetFirstApiLevel(int *first_api_level);
106
107 constexpr int kFilesystemUuidSize = 16;
108
109 struct FilesystemUuid {
110 uint8_t bytes[kFilesystemUuidSize];
111 };
112
113 struct FilesystemInfo {
114 std::string fs_blk_device;
115 std::string type;
116 FilesystemUuid uuid;
117 std::string raw_blk_device;
118 };
119
120 bool GetFilesystemInfo(const std::string &mountpoint, FilesystemInfo *info);
121
122 bool VerifyDataRandomness(const std::vector<uint8_t> &bytes);
123
124 bool CreateHwWrappedKey(std::vector<uint8_t> *master_key,
125 std::vector<uint8_t> *exported_key);
126
127 bool DeriveHwWrappedEncryptionKey(const std::vector<uint8_t> &master_key,
128 std::vector<uint8_t> *enc_key);
129
130 bool DeriveHwWrappedRawSecret(const std::vector<uint8_t> &master_key,
131 std::vector<uint8_t> *secret);
132 } // namespace kernel
133 } // namespace android
134