1 /*
2 * Copyright 2022 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 "compos_key.h"
18
19 #include <vector>
20
21 #include "gtest/gtest.h"
22
23 using namespace compos_key;
24
25 constexpr Seed seed = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
26 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32};
27 constexpr Seed other_seed = {3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,
28 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2};
29 const std::vector<uint8_t> data = {42, 180, 65, 0};
30
31 struct ComposKeyTest : public testing::Test {
32 Ed25519KeyPair key_pair;
33
SetUpComposKeyTest34 void SetUp() override {
35 auto key_pair = keyFromSeed(seed);
36 ASSERT_TRUE(key_pair.ok()) << key_pair.error();
37 this->key_pair = *key_pair;
38 }
39 };
40
TEST_F(ComposKeyTest,SameSeedSameKey)41 TEST_F(ComposKeyTest, SameSeedSameKey) {
42 auto other_key_pair = keyFromSeed(seed);
43 ASSERT_TRUE(other_key_pair.ok()) << other_key_pair.error();
44
45 ASSERT_EQ(key_pair.private_key, other_key_pair->private_key);
46 ASSERT_EQ(key_pair.public_key, other_key_pair->public_key);
47 }
48
TEST_F(ComposKeyTest,DifferentSeedDifferentKey)49 TEST_F(ComposKeyTest, DifferentSeedDifferentKey) {
50 auto other_key_pair = keyFromSeed(other_seed);
51 ASSERT_TRUE(other_key_pair.ok()) << other_key_pair.error();
52
53 ASSERT_NE(key_pair.private_key, other_key_pair->private_key);
54 ASSERT_NE(key_pair.public_key, other_key_pair->public_key);
55 }
56
TEST_F(ComposKeyTest,CanVerifyValidSignature)57 TEST_F(ComposKeyTest, CanVerifyValidSignature) {
58 auto signature = sign(key_pair.private_key, data.data(), data.size());
59 ASSERT_TRUE(signature.ok()) << signature.error();
60
61 bool verified = verify(key_pair.public_key, *signature, data.data(), data.size());
62 ASSERT_TRUE(verified);
63 }
64
TEST_F(ComposKeyTest,WrongSignatureDoesNotVerify)65 TEST_F(ComposKeyTest, WrongSignatureDoesNotVerify) {
66 auto signature = sign(key_pair.private_key, data.data(), data.size());
67 ASSERT_TRUE(signature.ok()) << signature.error();
68
69 (*signature)[0] ^= 1;
70
71 bool verified = verify(key_pair.public_key, *signature, data.data(), data.size());
72 ASSERT_FALSE(verified);
73 }
74
TEST_F(ComposKeyTest,WrongDataDoesNotVerify)75 TEST_F(ComposKeyTest, WrongDataDoesNotVerify) {
76 auto signature = sign(key_pair.private_key, data.data(), data.size());
77 ASSERT_TRUE(signature.ok()) << signature.error();
78
79 auto other_data = data;
80 other_data[0] ^= 1;
81
82 bool verified = verify(key_pair.public_key, *signature, other_data.data(), other_data.size());
83 ASSERT_FALSE(verified);
84 }
85
TEST_F(ComposKeyTest,WrongKeyDoesNotVerify)86 TEST_F(ComposKeyTest, WrongKeyDoesNotVerify) {
87 auto signature = sign(key_pair.private_key, data.data(), data.size());
88
89 auto other_key_pair = keyFromSeed(other_seed);
90 ASSERT_TRUE(other_key_pair.ok()) << other_key_pair.error();
91
92 bool verified = verify(other_key_pair->public_key, *signature, data.data(), data.size());
93 ASSERT_FALSE(verified);
94 }
95