1 /*
2 * Copyright 2018 Google LLC
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 "fcp/secagg/shared/ecdh_key_agreement.h"
18
19 #include <string>
20 #include <vector>
21
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "fcp/secagg/shared/aes_key.h"
25 #include "fcp/secagg/shared/ecdh_keys.h"
26 #include "fcp/secagg/testing/ecdh_pregenerated_test_keys.h"
27
28 namespace fcp {
29 namespace secagg {
30 namespace {
31
32 using ::testing::Contains;
33 using ::testing::Eq;
34 using ::testing::Not;
35
TEST(EcdhKeyAgreementTest,CanRecoverFromPrivateKey)36 TEST(EcdhKeyAgreementTest, CanRecoverFromPrivateKey) {
37 EcdhPregeneratedTestKeys keys;
38 auto ecdh1 = EcdhKeyAgreement::CreateFromKeypair(keys.GetPrivateKey(0),
39 keys.GetPublicKey(0))
40 .value();
41 auto ecdh2 =
42 EcdhKeyAgreement::CreateFromPrivateKey(ecdh1->PrivateKey()).value();
43 EXPECT_THAT(ecdh2->PrivateKey(), Eq(ecdh1->PrivateKey()));
44 EXPECT_THAT(ecdh2->PublicKey().size(), Eq(0));
45 }
46
TEST(EcdhKeyAgreementTest,CanRecoverKeypairFromPrivateAndPublicKeys)47 TEST(EcdhKeyAgreementTest, CanRecoverKeypairFromPrivateAndPublicKeys) {
48 EcdhPregeneratedTestKeys keys;
49 auto ecdh1 = EcdhKeyAgreement::CreateFromKeypair(keys.GetPrivateKey(0),
50 keys.GetPublicKey(0))
51 .value();
52 auto ecdh2 = EcdhKeyAgreement::CreateFromKeypair(ecdh1->PrivateKey(),
53 ecdh1->PublicKey())
54 .value();
55 EXPECT_THAT(ecdh2->PrivateKey(), Eq(ecdh1->PrivateKey()));
56 EXPECT_THAT(ecdh2->PublicKey(), Eq(ecdh1->PublicKey()));
57 }
58
TEST(EcdhKeyAgreementTest,PrivateKeyIsExpectedLength)59 TEST(EcdhKeyAgreementTest, PrivateKeyIsExpectedLength) {
60 EcdhPregeneratedTestKeys keys;
61 auto ecdh = EcdhKeyAgreement::CreateFromKeypair(keys.GetPrivateKey(0),
62 keys.GetPublicKey(0))
63 .value();
64 EXPECT_THAT(ecdh->PrivateKey().size(), Eq(EcdhPrivateKey::kSize));
65 }
66
TEST(EcdhKeyAgreementTest,PublicKeyIsExpectedLength)67 TEST(EcdhKeyAgreementTest, PublicKeyIsExpectedLength) {
68 EcdhPregeneratedTestKeys keys;
69 auto ecdh = EcdhKeyAgreement::CreateFromKeypair(keys.GetPrivateKey(0),
70 keys.GetPublicKey(0))
71 .value();
72 EXPECT_THAT(ecdh->PublicKey().size(), Eq(EcdhPublicKey::kSize));
73 }
74
TEST(EcdhKeyAgreementTest,RandomKeypairIsntTheSameEveryTime)75 TEST(EcdhKeyAgreementTest, RandomKeypairIsntTheSameEveryTime) {
76 std::vector<Key> private_keys;
77 std::vector<Key> public_keys;
78 private_keys.reserve(16);
79 public_keys.reserve(16);
80 for (int i = 0; i < 16; ++i) {
81 auto ecdh = EcdhKeyAgreement::CreateFromRandomKeys().value();
82 EXPECT_THAT(private_keys, Not(Contains(ecdh->PrivateKey())));
83 EXPECT_THAT(public_keys, Not(Contains(ecdh->PublicKey())));
84 private_keys.push_back(ecdh->PrivateKey());
85 public_keys.push_back(ecdh->PublicKey());
86 }
87 }
88
TEST(EcdhKeyAgreementTest,SharedSecretsHaveCorrectLength)89 TEST(EcdhKeyAgreementTest, SharedSecretsHaveCorrectLength) {
90 EcdhPregeneratedTestKeys keys;
91 auto ecdh = EcdhKeyAgreement::CreateFromKeypair(keys.GetPrivateKey(0),
92 keys.GetPublicKey(0))
93 .value();
94 auto secret = ecdh->ComputeSharedSecret(keys.GetPublicKey(1));
95 ASSERT_TRUE(secret.ok());
96 EXPECT_THAT(secret.value().size(), Eq(AesKey::kSize));
97 }
98
TEST(EcdhKeyAgreementTest,SharedSecretsAreDeterministic)99 TEST(EcdhKeyAgreementTest, SharedSecretsAreDeterministic) {
100 EcdhPregeneratedTestKeys keys;
101 auto ecdh = EcdhKeyAgreement::CreateFromKeypair(keys.GetPrivateKey(0),
102 keys.GetPublicKey(0))
103 .value();
104 auto secret1 = ecdh->ComputeSharedSecret(keys.GetPublicKey(1));
105 auto secret2 = ecdh->ComputeSharedSecret(keys.GetPublicKey(1));
106 ASSERT_TRUE(secret1.ok());
107 ASSERT_TRUE(secret2.ok());
108 EXPECT_THAT(secret1.value(), Eq(secret2.value()));
109 }
110
TEST(EcdhKeyAgreementTest,SharedSecretsAreConsistent)111 TEST(EcdhKeyAgreementTest, SharedSecretsAreConsistent) {
112 EcdhPregeneratedTestKeys keys;
113 auto ecdh1 = EcdhKeyAgreement::CreateFromKeypair(keys.GetPrivateKey(0),
114 keys.GetPublicKey(0))
115 .value();
116 auto ecdh2 = EcdhKeyAgreement::CreateFromKeypair(keys.GetPrivateKey(1),
117 keys.GetPublicKey(1))
118 .value();
119 auto secret1 = ecdh1->ComputeSharedSecret(ecdh2->PublicKey());
120 auto secret2 = ecdh2->ComputeSharedSecret(ecdh1->PublicKey());
121 ASSERT_TRUE(secret1.ok());
122 ASSERT_TRUE(secret2.ok());
123 EXPECT_THAT(secret1.value(), Eq(secret2.value()));
124 }
125
TEST(EcdhKeyAgreementTest,SharedSecretsAreConsistentWithoutPublicKey)126 TEST(EcdhKeyAgreementTest, SharedSecretsAreConsistentWithoutPublicKey) {
127 EcdhPregeneratedTestKeys keys;
128 auto ecdh1 =
129 EcdhKeyAgreement::CreateFromPrivateKey(keys.GetPrivateKey(0)).value();
130 auto ecdh2 =
131 EcdhKeyAgreement::CreateFromPrivateKey(keys.GetPrivateKey(1)).value();
132 auto secret1 = ecdh1->ComputeSharedSecret(keys.GetPublicKey(1));
133 auto secret2 = ecdh2->ComputeSharedSecret(keys.GetPublicKey(0));
134 ASSERT_TRUE(secret1.ok());
135 ASSERT_TRUE(secret2.ok());
136 EXPECT_THAT(secret1.value(), Eq(secret2.value()));
137 }
138
TEST(EcdhKeyAgreementTest,CreateFromKeypairErrorsOnInconsistentKeys)139 TEST(EcdhKeyAgreementTest, CreateFromKeypairErrorsOnInconsistentKeys) {
140 EcdhPregeneratedTestKeys keys;
141 auto ecdh = EcdhKeyAgreement::CreateFromKeypair(keys.GetPrivateKey(0),
142 keys.GetPublicKey(1));
143 EXPECT_THAT(ecdh.ok(), Eq(false));
144 }
145
TEST(EcdhKeyAgreementTest,ComputeSharedSecretErrorsOnGarbagePublicKey)146 TEST(EcdhKeyAgreementTest, ComputeSharedSecretErrorsOnGarbagePublicKey) {
147 EcdhPregeneratedTestKeys keys;
148 auto ecdh = EcdhKeyAgreement::CreateFromPrivateKey(keys.GetPrivateKey(0));
149 ASSERT_TRUE(ecdh.ok());
150
151 // first byte valid at least
152 const char bad_key[] =
153 "\x2"
154 "23456789012345678901234567890123";
155
156 auto secret = ecdh.value()->ComputeSharedSecret(
157 EcdhPublicKey(reinterpret_cast<const uint8_t*>(bad_key)));
158 EXPECT_THAT(secret.ok(), Eq(false));
159 }
160
TEST(EcdhKeyAgreementTest,SharedSecretsWorkWithUncompressedPublicKeys)161 TEST(EcdhKeyAgreementTest, SharedSecretsWorkWithUncompressedPublicKeys) {
162 EcdhPregeneratedTestKeys keys;
163 auto ecdh = EcdhKeyAgreement::CreateFromKeypair(keys.GetPrivateKey(0),
164 keys.GetPublicKey(0))
165 .value();
166 auto secret = ecdh->ComputeSharedSecret(keys.GetUncompressedPublicKey(0));
167 ASSERT_TRUE(secret.ok());
168 EXPECT_THAT(secret.value().size(), Eq(AesKey::kSize));
169 }
170 } // namespace
171 } // namespace secagg
172 } // namespace fcp
173