1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/proximity_auth/cryptauth/fake_secure_message_delegate.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace proximity_auth
{
14 const char kTestPublicKey
[] = "the private key is in another castle";
15 const char kPayload
[] = "500 tons of uranium";
16 const char kSymmetricKey
[] = "hunter2";
17 const char kPublicMetadata
[] = "brought to you by our sponsors";
18 const char kAssociatedData
[] = "save 20% bytes on your nonce insurance";
19 const char kVerificationKeyId
[] = "the one with the red stripes";
20 const char kDecryptionKeyId
[] = "it's in your pocket somewhere";
22 // Callback for saving the result of GenerateKeys().
23 void SaveKeyPair(std::string
* private_key_out
,
24 std::string
* public_key_out
,
25 const std::string
& private_key
,
26 const std::string
& public_key
) {
27 *private_key_out
= private_key
;
28 *public_key_out
= public_key
;
31 // Callback for saving the result of DeriveKey() and CreateSecureMessage().
32 void SaveString(std::string
* out
, const std::string
& value
) {
36 // Callback for saving the result of UnwrapSecureMessage().
37 void SaveUnwrapResults(std::string
* payload_out
,
38 securemessage::Header
* header_out
,
40 const std::string
& payload
,
41 const securemessage::Header
& header
) {
42 ASSERT_TRUE(verified
);
43 *payload_out
= payload
;
47 // Returns the CreateOptions struct to create the test message.
48 SecureMessageDelegate::CreateOptions
GetCreateOptions(
49 securemessage::EncScheme encryption_scheme
,
50 securemessage::SigScheme signature_scheme
) {
51 SecureMessageDelegate::CreateOptions create_options
;
52 create_options
.encryption_scheme
= encryption_scheme
;
53 create_options
.signature_scheme
= signature_scheme
;
54 create_options
.public_metadata
= kPublicMetadata
;
55 create_options
.associated_data
= kAssociatedData
;
56 create_options
.verification_key_id
= kVerificationKeyId
;
57 create_options
.decryption_key_id
= kDecryptionKeyId
;
58 return create_options
;
61 // Returns the UnwrapOptions struct to unwrap the test message.
62 SecureMessageDelegate::UnwrapOptions
GetUnwrapOptions(
63 securemessage::EncScheme encryption_scheme
,
64 securemessage::SigScheme signature_scheme
) {
65 SecureMessageDelegate::UnwrapOptions unwrap_options
;
66 unwrap_options
.encryption_scheme
= encryption_scheme
;
67 unwrap_options
.signature_scheme
= signature_scheme
;
68 unwrap_options
.associated_data
= kAssociatedData
;
69 return unwrap_options
;
72 void CheckSerializedSecureMessage(
73 const std::string
& serialized_message
,
74 const SecureMessageDelegate::CreateOptions
& create_options
) {
75 securemessage::SecureMessage secure_message
;
76 ASSERT_TRUE(secure_message
.ParseFromString(serialized_message
));
77 securemessage::HeaderAndBody header_and_body
;
79 header_and_body
.ParseFromString(secure_message
.header_and_body()));
81 const securemessage::Header
& header
= header_and_body
.header();
82 EXPECT_EQ(create_options
.signature_scheme
, header
.signature_scheme());
83 EXPECT_EQ(create_options
.encryption_scheme
, header
.encryption_scheme());
84 EXPECT_EQ(create_options
.verification_key_id
, header
.verification_key_id());
85 EXPECT_EQ(create_options
.decryption_key_id
, header
.decryption_key_id());
86 EXPECT_EQ(create_options
.public_metadata
, header
.public_metadata());
91 class ProximityAuthFakeSecureMessageDelegateTest
: public testing::Test
{
93 ProximityAuthFakeSecureMessageDelegateTest() {}
95 FakeSecureMessageDelegate delegate_
;
97 DISALLOW_COPY_AND_ASSIGN(ProximityAuthFakeSecureMessageDelegateTest
);
100 TEST_F(ProximityAuthFakeSecureMessageDelegateTest
, GenerateKeyPair
) {
101 std::string public_key1
, private_key1
;
102 delegate_
.GenerateKeyPair(
103 base::Bind(&SaveKeyPair
, &public_key1
, &private_key1
));
104 EXPECT_NE(private_key1
, public_key1
);
106 std::string public_key2
, private_key2
;
107 delegate_
.GenerateKeyPair(
108 base::Bind(&SaveKeyPair
, &public_key2
, &private_key2
));
109 EXPECT_NE(private_key2
, public_key2
);
111 EXPECT_NE(public_key1
, public_key2
);
112 EXPECT_NE(private_key1
, private_key2
);
114 delegate_
.set_next_public_key(kTestPublicKey
);
115 std::string public_key3
, private_key3
;
116 delegate_
.GenerateKeyPair(
117 base::Bind(&SaveKeyPair
, &public_key3
, &private_key3
));
118 EXPECT_EQ(kTestPublicKey
, public_key3
);
119 EXPECT_NE(private_key3
, public_key3
);
121 EXPECT_NE(public_key1
, public_key3
);
122 EXPECT_NE(private_key1
, private_key3
);
125 TEST_F(ProximityAuthFakeSecureMessageDelegateTest
, DeriveKey
) {
126 delegate_
.set_next_public_key("key_pair_1");
127 std::string public_key1
, private_key1
;
128 delegate_
.GenerateKeyPair(
129 base::Bind(&SaveKeyPair
, &public_key1
, &private_key1
));
131 delegate_
.set_next_public_key("key_pair_2");
132 std::string public_key2
, private_key2
;
133 delegate_
.GenerateKeyPair(
134 base::Bind(&SaveKeyPair
, &public_key2
, &private_key2
));
136 std::string symmetric_key1
, symmetric_key2
;
137 delegate_
.DeriveKey(private_key1
, public_key2
,
138 base::Bind(&SaveString
, &symmetric_key1
));
139 delegate_
.DeriveKey(private_key2
, public_key1
,
140 base::Bind(&SaveString
, &symmetric_key2
));
142 EXPECT_EQ(symmetric_key1
, symmetric_key2
);
145 TEST_F(ProximityAuthFakeSecureMessageDelegateTest
,
146 CreateAndUnwrapWithSymmetricKey
) {
147 // Create SecureMessage using symmetric key.
148 SecureMessageDelegate::CreateOptions create_options
=
149 GetCreateOptions(securemessage::AES_256_CBC
, securemessage::HMAC_SHA256
);
150 std::string serialized_message
;
151 delegate_
.CreateSecureMessage(kPayload
, kSymmetricKey
, create_options
,
152 base::Bind(&SaveString
, &serialized_message
));
154 CheckSerializedSecureMessage(serialized_message
, create_options
);
156 // Unwrap SecureMessage using symmetric key.
157 SecureMessageDelegate::UnwrapOptions unwrap_options
=
158 GetUnwrapOptions(securemessage::AES_256_CBC
, securemessage::HMAC_SHA256
);
160 securemessage::Header header
;
161 delegate_
.UnwrapSecureMessage(
162 serialized_message
, kSymmetricKey
, unwrap_options
,
163 base::Bind(&SaveUnwrapResults
, &payload
, &header
));
165 EXPECT_EQ(kPayload
, payload
);
168 TEST_F(ProximityAuthFakeSecureMessageDelegateTest
,
169 CreateAndUnwrapWithAsymmetricKey
) {
170 delegate_
.set_next_public_key(kTestPublicKey
);
171 std::string public_key
, private_key
;
172 delegate_
.GenerateKeyPair(
173 base::Bind(&SaveKeyPair
, &public_key
, &private_key
));
175 // Create SecureMessage using asymmetric key.
176 SecureMessageDelegate::CreateOptions create_options
=
177 GetCreateOptions(securemessage::NONE
, securemessage::ECDSA_P256_SHA256
);
178 std::string serialized_message
;
179 delegate_
.CreateSecureMessage(kPayload
, private_key
, create_options
,
180 base::Bind(&SaveString
, &serialized_message
));
182 CheckSerializedSecureMessage(serialized_message
, create_options
);
184 // Unwrap SecureMessage using symmetric key.
185 SecureMessageDelegate::UnwrapOptions unwrap_options
=
186 GetUnwrapOptions(securemessage::NONE
, securemessage::ECDSA_P256_SHA256
);
188 securemessage::Header header
;
189 delegate_
.UnwrapSecureMessage(
190 serialized_message
, public_key
, unwrap_options
,
191 base::Bind(&SaveUnwrapResults
, &payload
, &header
));
193 EXPECT_EQ(kPayload
, payload
);
196 TEST_F(ProximityAuthFakeSecureMessageDelegateTest
, GetPrivateKeyForPublicKey
) {
197 delegate_
.set_next_public_key(kTestPublicKey
);
198 std::string public_key
, private_key
;
199 delegate_
.GenerateKeyPair(
200 base::Bind(&SaveKeyPair
, &public_key
, &private_key
));
201 EXPECT_EQ(kTestPublicKey
, public_key
);
202 EXPECT_EQ(private_key
, delegate_
.GetPrivateKeyForPublicKey(kTestPublicKey
));