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 DoEncrypt(key.data(), iv, src, dst, nbytes);
37   }
38   virtual bool DoEncrypt(const uint8_t *key, const uint8_t *iv,
39                          const uint8_t *src, uint8_t *dst,
40                          int nbytes) const = 0;
41   virtual int keysize() const = 0;
42   virtual int ivsize() const = 0;
43 };
44 
45 // aes_256_xts.cpp
46 
47 constexpr int kAesBlockSize = 16;
48 constexpr int kAes256KeySize = 32;
49 constexpr int kAes256XtsKeySize = 2 * kAes256KeySize;
50 
51 class Aes256XtsCipher : public Cipher {
52  public:
53   bool DoEncrypt(const uint8_t *key, const uint8_t *iv, const uint8_t *src,
54                  uint8_t *dst, int nbytes) const;
keysize()55   int keysize() const { return kAes256XtsKeySize; }
ivsize()56   int ivsize() const { return kAesBlockSize; }
57 };
58 
59 // adiantum.cpp
60 
61 constexpr int kAdiantumKeySize = 32;
62 
63 // It's variable-length in general, but the Linux kernel always uses 32.
64 constexpr int kAdiantumIVSize = 32;
65 
66 class AdiantumCipher : public Cipher {
67  public:
68   bool DoEncrypt(const uint8_t *key, const uint8_t *iv, const uint8_t *src,
69                  uint8_t *dst, int nbytes) const;
keysize()70   int keysize() const { return kAdiantumKeySize; }
ivsize()71   int ivsize() const { return kAdiantumIVSize; }
72 };
73 
74 // utils.cpp
75 
76 std::string Errno();
77 
78 void DeleteRecursively(const std::string &path);
79 
80 void RandomBytesForTesting(std::vector<uint8_t> &bytes);
81 
82 std::vector<uint8_t> GenerateTestKey(size_t size);
83 
84 std::string BytesToHex(const std::vector<uint8_t> &bytes);
85 
86 template <size_t N>
BytesToHex(const uint8_t (& array)[N])87 static inline std::string BytesToHex(const uint8_t (&array)[N]) {
88   return BytesToHex(std::vector<uint8_t>(&array[0], &array[N]));
89 }
90 
91 bool GetFirstApiLevel(int *first_api_level);
92 
93 constexpr int kFilesystemUuidSize = 16;
94 
95 struct FilesystemUuid {
96   uint8_t bytes[kFilesystemUuidSize];
97 };
98 
99 struct FilesystemInfo {
100   std::string fs_blk_device;
101   std::string type;
102   FilesystemUuid uuid;
103   std::string raw_blk_device;
104 };
105 
106 bool GetFilesystemInfo(const std::string &mountpoint, FilesystemInfo *info);
107 
108 bool VerifyDataRandomness(const std::vector<uint8_t> &bytes);
109 
110 bool CreateHwWrappedKey(std::vector<uint8_t> *master_key,
111                         std::vector<uint8_t> *exported_key);
112 
113 bool DeriveHwWrappedEncryptionKey(const std::vector<uint8_t> &master_key,
114                                   std::vector<uint8_t> *enc_key);
115 
116 bool DeriveHwWrappedRawSecret(const std::vector<uint8_t> &master_key,
117                               std::vector<uint8_t> *secret);
118 }  // namespace kernel
119 }  // namespace android
120