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 DiskMapEntry {
114   std::string fs_blk_device;
115   std::string raw_blk_device;
116   int64_t start_blkaddr;
117   int64_t end_blkaddr;
118 };
119 
120 struct FilesystemInfo {
121   std::string type;
122   FilesystemUuid uuid;
123 
124   // The filesystem's block devices in sorted order of filesystem block address.
125   // The covered addresses are guaranteed to be contiguous and non-overlapping.
126   // The first device, starting at address 0, is the filesystem's "main" block
127   // device.
128   // Note, the disk_map's end_blkaddr is inclusive like below:
129   // [disk number]   [start_blkaddr]   [end_blkaddr]
130   // 0               0                 X - 1
131   // 1               X                 Y - 1
132   // 2               Y                 Z
133   std::vector<DiskMapEntry> disk_map;
134 };
135 
136 bool GetFilesystemInfo(const std::string &mountpoint, FilesystemInfo *info);
137 
138 bool VerifyDataRandomness(const std::vector<uint8_t> &bytes);
139 
140 bool CreateHwWrappedKey(std::vector<uint8_t> *master_key,
141                         std::vector<uint8_t> *exported_key);
142 
143 bool DeriveHwWrappedEncryptionKey(const std::vector<uint8_t> &master_key,
144                                   std::vector<uint8_t> *enc_key);
145 
146 bool DeriveHwWrappedRawSecret(const std::vector<uint8_t> &master_key,
147                               std::vector<uint8_t> *secret);
148 }  // namespace kernel
149 }  // namespace android
150