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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <stdint.h>
#include "base/stl_util.h"
#include "components/webcrypto/algorithm_dispatch.h"
#include "components/webcrypto/algorithms/ec.h"
#include "components/webcrypto/algorithms/test_helpers.h"
#include "components/webcrypto/crypto_data.h"
#include "components/webcrypto/jwk.h"
#include "components/webcrypto/status.h"
#include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
#include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
namespace webcrypto {
namespace {
// TODO(eroman): Test passing an RSA public key instead of ECDH key.
// TODO(eroman): Test passing an ECDSA public key
blink::WebCryptoAlgorithm CreateEcdhImportAlgorithm(
blink::WebCryptoNamedCurve named_curve) {
return CreateEcImportAlgorithm(blink::WebCryptoAlgorithmIdEcdh, named_curve);
}
blink::WebCryptoAlgorithm CreateEcdhDeriveParams(
const blink::WebCryptoKey& public_key) {
return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
blink::WebCryptoAlgorithmIdEcdh,
new blink::WebCryptoEcdhKeyDeriveParams(public_key));
}
blink::WebCryptoAlgorithm CreateAesGcmDerivedKeyParams(
unsigned short length_bits) {
return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
blink::WebCryptoAlgorithmIdAesGcm,
new blink::WebCryptoAesDerivedKeyParams(length_bits));
}
// Helper that loads a "public_key" and "private_key" from the test data.
bool ImportKeysFromTest(const base::DictionaryValue* test,
blink::WebCryptoKey* public_key,
blink::WebCryptoKey* private_key) {
// Import the public key.
const base::DictionaryValue* public_key_json = NULL;
EXPECT_TRUE(test->GetDictionary("public_key", &public_key_json));
blink::WebCryptoNamedCurve curve =
GetCurveNameFromDictionary(public_key_json);
EXPECT_EQ(Status::Success(),
ImportKey(blink::WebCryptoKeyFormatJwk,
CryptoData(MakeJsonVector(*public_key_json)),
CreateEcdhImportAlgorithm(curve), true, 0, public_key));
// If the test didn't specify an error for private key import, that implies
// it expects success.
std::string expected_private_key_error = "Success";
test->GetString("private_key_error", &expected_private_key_error);
// Import the private key.
const base::DictionaryValue* private_key_json = NULL;
EXPECT_TRUE(test->GetDictionary("private_key", &private_key_json));
curve = GetCurveNameFromDictionary(private_key_json);
Status status = ImportKey(
blink::WebCryptoKeyFormatJwk,
CryptoData(MakeJsonVector(*private_key_json)),
CreateEcdhImportAlgorithm(curve), true,
blink::WebCryptoKeyUsageDeriveBits | blink::WebCryptoKeyUsageDeriveKey,
private_key);
EXPECT_EQ(expected_private_key_error, StatusToString(status));
return status.IsSuccess();
}
class WebCryptoEcdhTest : public WebCryptoTestBase {};
TEST_F(WebCryptoEcdhTest, DeriveBitsKnownAnswer) {
std::unique_ptr<base::ListValue> tests;
ASSERT_TRUE(ReadJsonTestFileToList("ecdh.json", &tests));
for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
SCOPED_TRACE(test_index);
const base::DictionaryValue* test;
ASSERT_TRUE(tests->GetDictionary(test_index, &test));
// Import the keys.
blink::WebCryptoKey public_key;
blink::WebCryptoKey private_key;
if (!ImportKeysFromTest(test, &public_key, &private_key))
continue;
// Now try to derive bytes.
std::vector<uint8_t> derived_bytes;
int length_bits = 0;
ASSERT_TRUE(test->GetInteger("length_bits", &length_bits));
// If the test didn't specify an error, that implies it expects success.
std::string expected_error = "Success";
test->GetString("error", &expected_error);
Status status = DeriveBits(CreateEcdhDeriveParams(public_key), private_key,
length_bits, &derived_bytes);
ASSERT_EQ(expected_error, StatusToString(status));
if (status.IsError())
continue;
std::vector<uint8_t> expected_bytes =
GetBytesFromHexString(test, "derived_bytes");
EXPECT_EQ(CryptoData(expected_bytes), CryptoData(derived_bytes));
}
}
// Loads up a test ECDH public and private key for P-521. The keys
// come from different key pairs, and can be used for key derivation of up to
// 528 bits.
::testing::AssertionResult LoadTestKeys(blink::WebCryptoKey* public_key,
blink::WebCryptoKey* private_key) {
std::unique_ptr<base::ListValue> tests;
if (!ReadJsonTestFileToList("ecdh.json", &tests))
return ::testing::AssertionFailure() << "Failed loading ecdh.json";
const base::DictionaryValue* test = NULL;
bool valid_p521_keys = false;
for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
SCOPED_TRACE(test_index);
EXPECT_TRUE(tests->GetDictionary(test_index, &test));
test->GetBoolean("valid_p521_keys", &valid_p521_keys);
if (valid_p521_keys)
break;
}
if (!valid_p521_keys) {
return ::testing::AssertionFailure()
<< "The P-521 test are missing in ecdh.json";
}
ImportKeysFromTest(test, public_key, private_key);
EXPECT_EQ(blink::WebCryptoNamedCurveP521,
public_key->algorithm().ecParams()->namedCurve());
return ::testing::AssertionSuccess();
}
// Try deriving an AES key of length 129 bits.
TEST_F(WebCryptoEcdhTest, DeriveKeyBadAesLength) {
blink::WebCryptoKey public_key;
blink::WebCryptoKey base_key;
ASSERT_TRUE(LoadTestKeys(&public_key, &base_key));
blink::WebCryptoKey derived_key;
ASSERT_EQ(Status::ErrorGetAesKeyLength(),
DeriveKey(CreateEcdhDeriveParams(public_key), base_key,
CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
CreateAesGcmDerivedKeyParams(129), true,
blink::WebCryptoKeyUsageEncrypt, &derived_key));
}
// Try deriving an AES key of length 192 bits.
TEST_F(WebCryptoEcdhTest, DeriveKeyUnsupportedAesLength) {
blink::WebCryptoKey public_key;
blink::WebCryptoKey base_key;
ASSERT_TRUE(LoadTestKeys(&public_key, &base_key));
blink::WebCryptoKey derived_key;
ASSERT_EQ(Status::ErrorAes192BitUnsupported(),
DeriveKey(CreateEcdhDeriveParams(public_key), base_key,
CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
CreateAesGcmDerivedKeyParams(192), true,
blink::WebCryptoKeyUsageEncrypt, &derived_key));
}
// Try deriving an HMAC key of length 0 bits.
TEST_F(WebCryptoEcdhTest, DeriveKeyZeroLengthHmac) {
blink::WebCryptoKey public_key;
blink::WebCryptoKey base_key;
ASSERT_TRUE(LoadTestKeys(&public_key, &base_key));
blink::WebCryptoKey derived_key;
const blink::WebCryptoAlgorithm import_algorithm =
CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0);
ASSERT_EQ(Status::ErrorGetHmacKeyLengthZero(),
DeriveKey(CreateEcdhDeriveParams(public_key), base_key,
import_algorithm, import_algorithm, true,
blink::WebCryptoKeyUsageSign, &derived_key));
}
// Derive an HMAC key of length 19 bits.
TEST_F(WebCryptoEcdhTest, DeriveKeyHmac19Bits) {
blink::WebCryptoKey public_key;
blink::WebCryptoKey base_key;
ASSERT_TRUE(LoadTestKeys(&public_key, &base_key));
blink::WebCryptoKey derived_key;
const blink::WebCryptoAlgorithm import_algorithm =
CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1, 19);
ASSERT_EQ(Status::Success(),
DeriveKey(CreateEcdhDeriveParams(public_key), base_key,
import_algorithm, import_algorithm, true,
blink::WebCryptoKeyUsageSign, &derived_key));
ASSERT_EQ(blink::WebCryptoAlgorithmIdHmac, derived_key.algorithm().id());
ASSERT_EQ(blink::WebCryptoAlgorithmIdSha1,
derived_key.algorithm().hmacParams()->hash().id());
ASSERT_EQ(19u, derived_key.algorithm().hmacParams()->lengthBits());
// Export the key and verify its contents.
std::vector<uint8_t> raw_key;
EXPECT_EQ(Status::Success(),
ExportKey(blink::WebCryptoKeyFormatRaw, derived_key, &raw_key));
EXPECT_EQ(3u, raw_key.size());
// The last 7 bits of the key should be zero.
EXPECT_EQ(0, raw_key[raw_key.size() - 1] & 0x1f);
}
// Derive an HMAC key with no specified length (just the hash of SHA-256).
TEST_F(WebCryptoEcdhTest, DeriveKeyHmacSha256NoLength) {
blink::WebCryptoKey public_key;
blink::WebCryptoKey base_key;
ASSERT_TRUE(LoadTestKeys(&public_key, &base_key));
blink::WebCryptoKey derived_key;
const blink::WebCryptoAlgorithm import_algorithm =
CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha256);
ASSERT_EQ(Status::Success(),
DeriveKey(CreateEcdhDeriveParams(public_key), base_key,
import_algorithm, import_algorithm, true,
blink::WebCryptoKeyUsageSign, &derived_key));
ASSERT_EQ(blink::WebCryptoAlgorithmIdHmac, derived_key.algorithm().id());
ASSERT_EQ(blink::WebCryptoAlgorithmIdSha256,
derived_key.algorithm().hmacParams()->hash().id());
ASSERT_EQ(512u, derived_key.algorithm().hmacParams()->lengthBits());
// Export the key and verify its contents.
std::vector<uint8_t> raw_key;
EXPECT_EQ(Status::Success(),
ExportKey(blink::WebCryptoKeyFormatRaw, derived_key, &raw_key));
EXPECT_EQ(64u, raw_key.size());
}
// Derive an HMAC key with no specified length (just the hash of SHA-512).
//
// This fails, because ECDH using P-521 can only generate 528 bits, however HMAC
// SHA-512 requires 1024 bits.
//
// In practice, authors won't be directly generating keys from key agreement
// schemes, as that is frequently insecure, and instead be using KDFs to expand
// and generate keys. For simplicity of testing, however, test using an HMAC
// key.
TEST_F(WebCryptoEcdhTest, DeriveKeyHmacSha512NoLength) {
blink::WebCryptoKey public_key;
blink::WebCryptoKey base_key;
ASSERT_TRUE(LoadTestKeys(&public_key, &base_key));
blink::WebCryptoKey derived_key;
const blink::WebCryptoAlgorithm import_algorithm =
CreateHmacImportAlgorithmNoLength(blink::WebCryptoAlgorithmIdSha512);
ASSERT_EQ(Status::ErrorEcdhLengthTooBig(528),
DeriveKey(CreateEcdhDeriveParams(public_key), base_key,
import_algorithm, import_algorithm, true,
blink::WebCryptoKeyUsageSign, &derived_key));
}
// Try deriving an AES key of length 128 bits.
TEST_F(WebCryptoEcdhTest, DeriveKeyAes128) {
blink::WebCryptoKey public_key;
blink::WebCryptoKey base_key;
ASSERT_TRUE(LoadTestKeys(&public_key, &base_key));
blink::WebCryptoKey derived_key;
ASSERT_EQ(Status::Success(),
DeriveKey(CreateEcdhDeriveParams(public_key), base_key,
CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
CreateAesGcmDerivedKeyParams(128), true,
blink::WebCryptoKeyUsageEncrypt, &derived_key));
ASSERT_EQ(blink::WebCryptoAlgorithmIdAesGcm, derived_key.algorithm().id());
ASSERT_EQ(128, derived_key.algorithm().aesParams()->lengthBits());
// Export the key and verify its contents.
std::vector<uint8_t> raw_key;
EXPECT_EQ(Status::Success(),
ExportKey(blink::WebCryptoKeyFormatRaw, derived_key, &raw_key));
EXPECT_EQ(16u, raw_key.size());
}
TEST_F(WebCryptoEcdhTest, ImportKeyEmptyUsage) {
blink::WebCryptoKey key;
std::unique_ptr<base::ListValue> tests;
ASSERT_TRUE(ReadJsonTestFileToList("ecdh.json", &tests));
const base::DictionaryValue* test;
ASSERT_TRUE(tests->GetDictionary(0, &test));
// Import the public key.
const base::DictionaryValue* public_key_json = NULL;
EXPECT_TRUE(test->GetDictionary("public_key", &public_key_json));
blink::WebCryptoNamedCurve curve =
GetCurveNameFromDictionary(public_key_json);
ASSERT_EQ(Status::Success(),
ImportKey(blink::WebCryptoKeyFormatJwk,
CryptoData(MakeJsonVector(*public_key_json)),
CreateEcdhImportAlgorithm(curve), true, 0, &key));
EXPECT_EQ(0, key.usages());
// Import the private key.
const base::DictionaryValue* private_key_json = NULL;
EXPECT_TRUE(test->GetDictionary("private_key", &private_key_json));
curve = GetCurveNameFromDictionary(private_key_json);
ASSERT_EQ(Status::ErrorCreateKeyEmptyUsages(),
ImportKey(blink::WebCryptoKeyFormatJwk,
CryptoData(MakeJsonVector(*private_key_json)),
CreateEcdhImportAlgorithm(curve), true, 0, &key));
}
} // namespace
} // namespace webcrypto
|