-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
369 lines (296 loc) · 12.4 KB
/
main.cpp
File metadata and controls
369 lines (296 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "gtest/gtest.h"
#include "log.h"
#include "crypto_helper.h"
#include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
#include <android/hardware/keymaster/3.0/types.h>
#include <keymaster/keymaster_configuration.h>
#include "authorization_set.h"
#include "key_param_output.h"
#define CERT_PATH "/data/local/tmp/cert.der"
#define SIGNATURE_PATH "/data/local/tmp/signature.dat"
using ::android::sp;
namespace android {
namespace hardware {
namespace keymaster {
namespace V3_0 {
class HidlBuf : public hidl_vec<uint8_t> {
typedef hidl_vec<uint8_t> super;
public:
HidlBuf() {}
HidlBuf(const super& other) : super(other) {}
HidlBuf(super&& other) : super(std::move(other)) {}
explicit HidlBuf(const std::string& other) : HidlBuf() { *this = other; }
HidlBuf& operator=(const super& other) {
super::operator=(other);
return *this;
}
HidlBuf& operator=(super&& other) {
super::operator=(std::move(other));
return *this;
}
HidlBuf& operator=(const std::string& other) {
resize(other.size());
for (size_t i = 0; i < other.size(); ++i) {
(*this)[i] = static_cast<uint8_t>(other[i]);
}
return *this;
}
std::string to_string() const { return std::string(reinterpret_cast<const char*>(data()), size()); }
};
class Base : public ::testing::Test {
public:
Base() {}
virtual ~Base() {}
void SetUp() {}
void TearDown() {}
int GenerateRandomBytes(char *buf, size_t size) {
return RAND_bytes((unsigned char*)buf, (int)size);
}
};
class Derived : public Base {
public:
sp<IKeymasterDevice> keymaster_;
bool isSecure_, supportsEc_, supportsSymmetric_, supportsAttestation_, supportsAllDigests_;
hidl_string name_;
hidl_string author_;
HidlBuf key_blob_;
KeyCharacteristics key_characteristics_;
Derived() {
if(!keymaster_)
keymaster_ = IKeymasterDevice::getService();
if(!keymaster_) {
LOGE("Failed to get keymaster device");
return;
}
auto err = keymaster_->getHardwareFeatures([&](bool isSecure, bool supportsEc, bool supportsSymmetric,
bool supportsAttestation, bool supportsAllDigests,
const hidl_string& name, const hidl_string& author) {
isSecure_ = isSecure;
name_ = name;
author_ = author;
supportsEc_ = supportsEc;
supportsSymmetric_ = supportsSymmetric;
supportsAttestation_ = supportsAttestation;
supportsAllDigests_ = supportsAllDigests;
});
if(!err.isOk()) {
LOGI("Security level = %d", (int)isSecure_);
LOGI("name = %s", name_.c_str());
LOGI("author_ = %s", name_.c_str());
}
else
LOGW("Failed to get hardware features. Error %s", err.description().c_str());
}
~Derived() {
if(keymaster_)
keymaster_.clear();
}
ErrorCode GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob,
KeyCharacteristics* key_characteristics) {
EXPECT_NE(key_blob, nullptr);
EXPECT_NE(key_characteristics, nullptr);
EXPECT_EQ(0U, key_blob->size());
ErrorCode error;
EXPECT_TRUE(keymaster_
->generateKey(key_desc.hidl_data(),
[&](ErrorCode hidl_error, const HidlBuf& hidl_key_blob,
const KeyCharacteristics& hidl_key_characteristics) {
error = hidl_error;
*key_blob = hidl_key_blob;
*key_characteristics = hidl_key_characteristics;
})
.isOk());
// On error, blob & characteristics should be empty.
if (error != ErrorCode::OK) {
EXPECT_EQ(0U, key_blob->size());
EXPECT_EQ(0U, (key_characteristics->softwareEnforced.size() +
key_characteristics->teeEnforced.size()));
}
return error;
}
ErrorCode GenerateKey(const AuthorizationSet& key_desc) {
return GenerateKey(key_desc, &key_blob_, &key_characteristics_);
}
};
TEST_F(Base, AES256GCMEncryptDecryptSuccess) {
char key[AES_GCM_256_KEY_LEN] = { 0, };
char iv[AES_GCM_256_IV_LEN] = { 0, };
char tag[AES_GCM_256_TAG_LEN] = { 0, };
char plaintext[] = "What is the key to the life on Earth?";
char ciphertext[256] = { 0, };
char decrypted_text[256] = { 0, };
size_t ciphertext_len, decryptedtext_len;
ASSERT_EQ(1, GenerateRandomBytes(key, sizeof(key)));
ASSERT_EQ(1, GenerateRandomBytes(iv, sizeof(iv)));
ASSERT_EQ(0, AES256EncryptDecrypt(MODE_ENCRYPT, plaintext,
strlen(plaintext), key, iv, tag, ciphertext, &ciphertext_len))
<< "Failed to encrypt message";
ASSERT_TRUE(ciphertext_len == strlen(plaintext));
ASSERT_EQ(0, AES256EncryptDecrypt(MODE_DECRYPT, ciphertext,
ciphertext_len, key, iv, tag, decrypted_text, &decryptedtext_len))
<< "Failed to decrypt message";
ASSERT_EQ(strlen(plaintext), decryptedtext_len)
<< "Decrypted text len != plaintext len";
ASSERT_EQ(0, strcmp(decrypted_text, plaintext))
<< "Decrypted text != plaintext";
LOGI("Plaintext: %s\nEncrypted Text: %s\nDecrypted text: %s\n",
plaintext, ciphertext, decrypted_text);
}
TEST_F(Base, AES256GCMBrokenEncryptedText) {
char key[AES_GCM_256_KEY_LEN] = { 0, };
char iv[AES_GCM_256_IV_LEN] = { 0, };
char tag[AES_GCM_256_TAG_LEN] = { 0, };
char plaintext[] = "What is the key to the life on Earth?";
char ciphertext[256] = { 0, };
char decrypted_text[256] = { 0, };
size_t ciphertext_len, decryptedtext_len;
ASSERT_EQ(1, GenerateRandomBytes(key, sizeof(key)));
ASSERT_EQ(1, GenerateRandomBytes(iv, sizeof(iv)));
ASSERT_EQ(0, AES256EncryptDecrypt(MODE_ENCRYPT, plaintext,
strlen(plaintext), key, iv, tag, ciphertext, &ciphertext_len))
<< "Failed to encrypt message";
ciphertext[0] ^= 1;
ASSERT_EQ(-1, AES256EncryptDecrypt(MODE_DECRYPT, ciphertext,
ciphertext_len, key, iv, tag, decrypted_text, &decryptedtext_len))
<< "Message decryption should have failed";
}
TEST_F(Base, AES256GCMBrokenKey) {
char key[AES_GCM_256_KEY_LEN] = { 0, };
char iv[AES_GCM_256_IV_LEN] = { 0, };
char tag[AES_GCM_256_TAG_LEN] = { 0, };
char plaintext[] = "What is the key to the life on Earth?";
char ciphertext[256] = { 0, };
char decrypted_text[256] = { 0, };
size_t ciphertext_len, decryptedtext_len;
ASSERT_EQ(1, GenerateRandomBytes(key, sizeof(key)));
ASSERT_EQ(1, GenerateRandomBytes(iv, sizeof(iv)));
ASSERT_EQ(0, AES256EncryptDecrypt(MODE_ENCRYPT, plaintext,
strlen(plaintext), key, iv, tag, ciphertext, &ciphertext_len))
<< "Failed to encrypt message";
key[0] ^= 1;
ASSERT_EQ(-1, AES256EncryptDecrypt(MODE_DECRYPT, ciphertext,
ciphertext_len, key, iv, tag, decrypted_text, &decryptedtext_len))
<< "Message decryption should have failed";
}
TEST_F(Base, AES256GCMBrokenTag) {
char key[AES_GCM_256_KEY_LEN] = { 0, };
char iv[AES_GCM_256_IV_LEN] = { 0, };
char tag[AES_GCM_256_TAG_LEN] = { 0, };
char plaintext[] = "What is the key to the life on Earth?";
char ciphertext[256] = { 0, };
char decrypted_text[256] = { 0, };
size_t ciphertext_len, decryptedtext_len;
ASSERT_EQ(1, GenerateRandomBytes(key, sizeof(key)));
ASSERT_EQ(1, GenerateRandomBytes(iv, sizeof(iv)));
ASSERT_EQ(0, AES256EncryptDecrypt(MODE_ENCRYPT, plaintext,
strlen(plaintext), key, iv, tag, ciphertext, &ciphertext_len))
<< "Failed to encrypt message";
tag[0] ^= 1;
ASSERT_EQ(-1, AES256EncryptDecrypt(MODE_DECRYPT, ciphertext,
ciphertext_len, key, iv, tag, decrypted_text, &decryptedtext_len))
<< "Message decryption should have failed";
}
TEST_F(Base, RSASignVerifySuccess) {
char plaintext[] = "What is the key to the life on Earth?";
char *signature = NULL;
size_t siglen;
EVP_PKEY *pkey = GenerateRSAKey(2048);
ASSERT_NE((evp_pkey_st *)NULL, pkey) << "Failed to generate rsa key";
ASSERT_EQ(0, RSASign(plaintext, strlen(plaintext), pkey,
&signature, &siglen)) << "Failed to sign data";
ASSERT_EQ(0, RSAVerify(plaintext, strlen(plaintext),
pkey, signature, siglen)) << "Failed to verify signature";
EVP_PKEY_free(pkey);
free(signature);
}
TEST_F(Base, RSASignVerifyFakeKeyFails) {
char plaintext[] = "What is the key to the life on Earth?";
char *signature = NULL;
size_t siglen;
EVP_PKEY *pkey = GenerateRSAKey(2048);
EVP_PKEY *fake_pkey = GenerateRSAKey(2048);
ASSERT_NE((evp_pkey_st *)NULL, pkey) << "Failed to generate rsa key";
ASSERT_EQ(0, RSASign(plaintext, strlen(plaintext), pkey,
&signature, &siglen)) << "Failed to sign data";
ASSERT_NE(0, RSAVerify(plaintext, strlen(plaintext),
fake_pkey, signature, siglen)) << "Should have failed to verify signature";
EVP_PKEY_free(pkey);
EVP_PKEY_free(fake_pkey);
free(signature);
}
TEST_F(Base, RSASignVerifyChangedDataFails) {
char plaintext[] = "What is the key to the life on Earth?";
char *signature = NULL;
size_t siglen;
EVP_PKEY *pkey = GenerateRSAKey(2048);
ASSERT_NE((evp_pkey_st *)NULL, pkey) << "Failed to generate rsa key";
ASSERT_EQ(0, RSASign(plaintext, strlen(plaintext), pkey,
&signature, &siglen)) << "Failed to sign data";
plaintext[0] ^= 1;
ASSERT_NE(0, RSAVerify(plaintext, strlen(plaintext),
pkey, signature, siglen)) << "should have failed to verify signature";
EVP_PKEY_free(pkey);
free(signature);
}
TEST_F(Base, RSASignVerifyChangedSignatureFails) {
char plaintext[] = "What is the key to the life on Earth?";
char *signature = NULL;
size_t siglen;
EVP_PKEY *pkey = GenerateRSAKey(2048);
ASSERT_NE((evp_pkey_st *)NULL, pkey) << "Failed to generate rsa key";
ASSERT_EQ(0, RSASign(plaintext, strlen(plaintext), pkey,
&signature, &siglen)) << "Failed to sign data";
signature[0] ^= 1;
ASSERT_NE(0, RSAVerify(plaintext, strlen(plaintext),
pkey, signature, siglen)) << "should have failed to verify signature";
EVP_PKEY_free(pkey);
free(signature);
}
TEST_F(Base, SignAndMakeCert) {
char plaintext[] = "What is the key to the life on Earth?";
int len;
char *signature = NULL;
size_t siglen;
unsigned char *cert_data = NULL;
X509 *cert = NULL;
FILE *pFile;
EVP_PKEY *pkey = GenerateRSAKey(2048);
ASSERT_NE((evp_pkey_st *)NULL, pkey) << "Failed to generate rsa key";
ASSERT_EQ(0, MakeCertificate(&cert, pkey, 1, 365));
len = i2d_X509(cert, NULL);
ASSERT_FALSE(0 > len) << "Failed to get cert length";
len = i2d_X509(cert, &cert_data);
ASSERT_FALSE(0 > len) << "Failed to serialize certificate";
pFile = fopen(CERT_PATH, "wb");
ASSERT_NE((FILE*)NULL, pFile) << "Failed to open a file";
ASSERT_EQ(fwrite(cert_data, 1, (size_t)len, pFile), (size_t)len)
<< "Dailed to write certificate to a file";
fclose(pFile);
free(cert_data);
X509_free(cert);
ASSERT_EQ(0, RSASign(plaintext, strlen(plaintext), pkey,
&signature, &siglen)) << "Failed to sign data";
pFile = fopen(SIGNATURE_PATH, "wb");
ASSERT_NE((FILE*)NULL, pFile) << "Failed to open a file";
ASSERT_EQ(fwrite(signature, 1, siglen, pFile), siglen)
<< "Failed to write signature to a file";
fclose(pFile);
EVP_PKEY_free(pkey);
free(signature);
}
TEST_F(Derived, KeymasterTest) {
for (auto key_size : {1024, 2048, 3072, 4096}) {
HidlBuf key_blob;
KeyCharacteristics key_characteristics;
ASSERT_EQ(ErrorCode::OK, GenerateKey(AuthorizationSetBuilder()
.RsaSigningKey(key_size, 3)
.Digest(Digest::NONE)
.Padding(PaddingMode::NONE),
&key_blob, &key_characteristics));
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
}}}}