• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <gtest/gtest.h>
18 
19 #include <memory>
20 
21 #include <adb/pairing/aes_128_gcm.h>
22 #include <openssl/rand.h>
23 
24 namespace adb {
25 namespace pairing {
26 
TEST(Aes128GcmTest,init_null_material)27 TEST(Aes128GcmTest, init_null_material) {
28     std::unique_ptr<Aes128Gcm> cipher;
29     ASSERT_DEATH({ cipher.reset(new Aes128Gcm(nullptr, 42)); }, "");
30 }
31 
TEST(Aes128GcmTest,init_empty_material)32 TEST(Aes128GcmTest, init_empty_material) {
33     uint8_t material[64];
34     std::unique_ptr<Aes128Gcm> cipher;
35     ASSERT_DEATH({ cipher.reset(new Aes128Gcm(material, 0)); }, "");
36 }
37 
TEST(Aes128GcmTest,encrypt_decrypt)38 TEST(Aes128GcmTest, encrypt_decrypt) {
39     const uint8_t msg[] = "alice and bob, sitting in a binary tree";
40     uint8_t material[256];
41     uint8_t encrypted[1024];
42     uint8_t out_buf[1024] = {};
43 
44     RAND_bytes(material, sizeof(material));
45     Aes128Gcm alice(material, sizeof(material));
46     Aes128Gcm bob(material, sizeof(material));
47     ;
48 
49     ASSERT_GE(alice.EncryptedSize(sizeof(msg)), sizeof(msg));
50     auto encrypted_size = alice.Encrypt(msg, sizeof(msg), encrypted, sizeof(encrypted));
51     ASSERT_TRUE(encrypted_size.has_value());
52     ASSERT_GT(*encrypted_size, 0);
53     size_t out_size = sizeof(out_buf);
54     ASSERT_GE(bob.DecryptedSize(*encrypted_size), sizeof(msg));
55     auto decrypted_size = bob.Decrypt(encrypted, *encrypted_size, out_buf, out_size);
56     ASSERT_TRUE(decrypted_size.has_value());
57     ASSERT_EQ(sizeof(msg), *decrypted_size);
58     ASSERT_STREQ(reinterpret_cast<const char*>(msg), reinterpret_cast<const char*>(out_buf));
59 }
60 
61 }  // namespace pairing
62 }  // namespace adb
63