OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/logging.h" |
| 6 #include "base/stl_util.h" |
| 7 #include "content/child/webcrypto/algorithm_dispatch.h" |
| 8 #include "content/child/webcrypto/crypto_data.h" |
| 9 #include "content/child/webcrypto/status.h" |
| 10 #include "content/child/webcrypto/test/test_helpers.h" |
| 11 #include "content/child/webcrypto/webcrypto_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 14 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 namespace webcrypto { |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Creates an HMAC algorithm whose parameters struct is compatible with key |
| 23 // generation. It is an error to call this with a hash_id that is not a SHA*. |
| 24 // The key_length_bits parameter is optional, with zero meaning unspecified. |
| 25 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( |
| 26 blink::WebCryptoAlgorithmId hash_id, |
| 27 unsigned int key_length_bits) { |
| 28 DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id)); |
| 29 // key_length_bytes == 0 means unspecified |
| 30 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 31 blink::WebCryptoAlgorithmIdHmac, |
| 32 new blink::WebCryptoHmacKeyGenParams( |
| 33 CreateAlgorithm(hash_id), (key_length_bits != 0), key_length_bits)); |
| 34 } |
| 35 |
| 36 TEST(WebCryptoHmacTest, HMACSampleSets) { |
| 37 scoped_ptr<base::ListValue> tests; |
| 38 ASSERT_TRUE(ReadJsonTestFileToList("hmac.json", &tests)); |
| 39 // TODO(padolph): Missing known answer tests for HMAC SHA384, and SHA512. |
| 40 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
| 41 SCOPED_TRACE(test_index); |
| 42 base::DictionaryValue* test; |
| 43 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
| 44 |
| 45 blink::WebCryptoAlgorithm test_hash = GetDigestAlgorithm(test, "hash"); |
| 46 const std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key"); |
| 47 const std::vector<uint8_t> test_message = |
| 48 GetBytesFromHexString(test, "message"); |
| 49 const std::vector<uint8_t> test_mac = GetBytesFromHexString(test, "mac"); |
| 50 |
| 51 blink::WebCryptoAlgorithm algorithm = |
| 52 CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac); |
| 53 |
| 54 blink::WebCryptoAlgorithm import_algorithm = |
| 55 CreateHmacImportAlgorithm(test_hash.id()); |
| 56 |
| 57 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
| 58 test_key, |
| 59 import_algorithm, |
| 60 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify); |
| 61 |
| 62 EXPECT_EQ(test_hash.id(), key.algorithm().hmacParams()->hash().id()); |
| 63 EXPECT_EQ(test_key.size() * 8, key.algorithm().hmacParams()->lengthBits()); |
| 64 |
| 65 // Verify exported raw key is identical to the imported data |
| 66 std::vector<uint8_t> raw_key; |
| 67 EXPECT_EQ(Status::Success(), |
| 68 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 69 EXPECT_BYTES_EQ(test_key, raw_key); |
| 70 |
| 71 std::vector<uint8_t> output; |
| 72 |
| 73 ASSERT_EQ(Status::Success(), |
| 74 Sign(algorithm, key, CryptoData(test_message), &output)); |
| 75 |
| 76 EXPECT_BYTES_EQ(test_mac, output); |
| 77 |
| 78 bool signature_match = false; |
| 79 EXPECT_EQ(Status::Success(), |
| 80 Verify(algorithm, |
| 81 key, |
| 82 CryptoData(output), |
| 83 CryptoData(test_message), |
| 84 &signature_match)); |
| 85 EXPECT_TRUE(signature_match); |
| 86 |
| 87 // Ensure truncated signature does not verify by passing one less byte. |
| 88 EXPECT_EQ(Status::Success(), |
| 89 Verify(algorithm, |
| 90 key, |
| 91 CryptoData(vector_as_array(&output), output.size() - 1), |
| 92 CryptoData(test_message), |
| 93 &signature_match)); |
| 94 EXPECT_FALSE(signature_match); |
| 95 |
| 96 // Ensure truncated signature does not verify by passing no bytes. |
| 97 EXPECT_EQ(Status::Success(), |
| 98 Verify(algorithm, |
| 99 key, |
| 100 CryptoData(), |
| 101 CryptoData(test_message), |
| 102 &signature_match)); |
| 103 EXPECT_FALSE(signature_match); |
| 104 |
| 105 // Ensure extra long signature does not cause issues and fails. |
| 106 const unsigned char kLongSignature[1024] = {0}; |
| 107 EXPECT_EQ(Status::Success(), |
| 108 Verify(algorithm, |
| 109 key, |
| 110 CryptoData(kLongSignature, sizeof(kLongSignature)), |
| 111 CryptoData(test_message), |
| 112 &signature_match)); |
| 113 EXPECT_FALSE(signature_match); |
| 114 } |
| 115 } |
| 116 |
| 117 TEST(WebCryptoHmacTest, GenerateKeyIsRandom) { |
| 118 // Generate a small sample of HMAC keys. |
| 119 std::vector<std::vector<uint8_t> > keys; |
| 120 for (int i = 0; i < 16; ++i) { |
| 121 std::vector<uint8_t> key_bytes; |
| 122 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 123 blink::WebCryptoAlgorithm algorithm = |
| 124 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 512); |
| 125 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); |
| 126 EXPECT_FALSE(key.isNull()); |
| 127 EXPECT_TRUE(key.handle()); |
| 128 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 129 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
| 130 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
| 131 key.algorithm().hmacParams()->hash().id()); |
| 132 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); |
| 133 |
| 134 std::vector<uint8_t> raw_key; |
| 135 ASSERT_EQ(Status::Success(), |
| 136 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 137 EXPECT_EQ(64U, raw_key.size()); |
| 138 keys.push_back(raw_key); |
| 139 } |
| 140 // Ensure all entries in the key sample set are unique. This is a simplistic |
| 141 // estimate of whether the generated keys appear random. |
| 142 EXPECT_FALSE(CopiesExist(keys)); |
| 143 } |
| 144 |
| 145 // If the key length is not provided, then the block size is used. |
| 146 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha1) { |
| 147 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 148 blink::WebCryptoAlgorithm algorithm = |
| 149 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); |
| 150 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); |
| 151 EXPECT_TRUE(key.handle()); |
| 152 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 153 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
| 154 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
| 155 key.algorithm().hmacParams()->hash().id()); |
| 156 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); |
| 157 std::vector<uint8_t> raw_key; |
| 158 ASSERT_EQ(Status::Success(), |
| 159 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 160 EXPECT_EQ(64U, raw_key.size()); |
| 161 } |
| 162 |
| 163 // If the key length is not provided, then the block size is used. |
| 164 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha512) { |
| 165 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 166 blink::WebCryptoAlgorithm algorithm = |
| 167 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); |
| 168 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); |
| 169 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
| 170 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, |
| 171 key.algorithm().hmacParams()->hash().id()); |
| 172 EXPECT_EQ(1024u, key.algorithm().hmacParams()->lengthBits()); |
| 173 std::vector<uint8_t> raw_key; |
| 174 ASSERT_EQ(Status::Success(), |
| 175 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 176 EXPECT_EQ(128U, raw_key.size()); |
| 177 } |
| 178 |
| 179 TEST(WebCryptoHmacTest, ImportKeyJwkKeyOpsSignVerify) { |
| 180 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 181 base::DictionaryValue dict; |
| 182 dict.SetString("kty", "oct"); |
| 183 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); |
| 184 base::ListValue* key_ops = new base::ListValue; |
| 185 dict.Set("key_ops", key_ops); // Takes ownership. |
| 186 |
| 187 key_ops->AppendString("sign"); |
| 188 |
| 189 EXPECT_EQ(Status::Success(), |
| 190 ImportKeyJwkFromDict( |
| 191 dict, |
| 192 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256), |
| 193 false, |
| 194 blink::WebCryptoKeyUsageSign, |
| 195 &key)); |
| 196 |
| 197 EXPECT_EQ(blink::WebCryptoKeyUsageSign, key.usages()); |
| 198 |
| 199 key_ops->AppendString("verify"); |
| 200 |
| 201 EXPECT_EQ(Status::Success(), |
| 202 ImportKeyJwkFromDict( |
| 203 dict, |
| 204 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256), |
| 205 false, |
| 206 blink::WebCryptoKeyUsageVerify, |
| 207 &key)); |
| 208 |
| 209 EXPECT_EQ(blink::WebCryptoKeyUsageVerify, key.usages()); |
| 210 } |
| 211 |
| 212 // Test 'use' inconsistent with 'key_ops'. |
| 213 TEST(WebCryptoHmacTest, ImportKeyJwkUseInconsisteWithKeyOps) { |
| 214 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 215 base::DictionaryValue dict; |
| 216 dict.SetString("kty", "oct"); |
| 217 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); |
| 218 base::ListValue* key_ops = new base::ListValue; |
| 219 dict.Set("key_ops", key_ops); // Takes ownership. |
| 220 |
| 221 dict.SetString("alg", "HS256"); |
| 222 dict.SetString("use", "sig"); |
| 223 key_ops->AppendString("sign"); |
| 224 key_ops->AppendString("verify"); |
| 225 key_ops->AppendString("encrypt"); |
| 226 EXPECT_EQ(Status::ErrorJwkUseAndKeyopsInconsistent(), |
| 227 ImportKeyJwkFromDict( |
| 228 dict, |
| 229 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256), |
| 230 false, |
| 231 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, |
| 232 &key)); |
| 233 } |
| 234 |
| 235 // Test JWK composite 'sig' use |
| 236 TEST(WebCryptoHmacTest, ImportKeyJwkUseSig) { |
| 237 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 238 base::DictionaryValue dict; |
| 239 dict.SetString("kty", "oct"); |
| 240 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); |
| 241 |
| 242 dict.SetString("use", "sig"); |
| 243 EXPECT_EQ(Status::Success(), |
| 244 ImportKeyJwkFromDict( |
| 245 dict, |
| 246 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256), |
| 247 false, |
| 248 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, |
| 249 &key)); |
| 250 |
| 251 EXPECT_EQ(blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, |
| 252 key.usages()); |
| 253 } |
| 254 |
| 255 TEST(WebCryptoHmacTest, ImportJwkInputConsistency) { |
| 256 // The Web Crypto spec says that if a JWK value is present, but is |
| 257 // inconsistent with the input value, the operation must fail. |
| 258 |
| 259 // Consistency rules when JWK value is not present: Inputs should be used. |
| 260 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 261 bool extractable = false; |
| 262 blink::WebCryptoAlgorithm algorithm = |
| 263 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256); |
| 264 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageVerify; |
| 265 base::DictionaryValue dict; |
| 266 dict.SetString("kty", "oct"); |
| 267 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); |
| 268 std::vector<uint8_t> json_vec = MakeJsonVector(dict); |
| 269 EXPECT_EQ(Status::Success(), |
| 270 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 271 CryptoData(json_vec), |
| 272 algorithm, |
| 273 extractable, |
| 274 usage_mask, |
| 275 &key)); |
| 276 EXPECT_TRUE(key.handle()); |
| 277 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 278 EXPECT_EQ(extractable, key.extractable()); |
| 279 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
| 280 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, |
| 281 key.algorithm().hmacParams()->hash().id()); |
| 282 EXPECT_EQ(320u, key.algorithm().hmacParams()->lengthBits()); |
| 283 EXPECT_EQ(blink::WebCryptoKeyUsageVerify, key.usages()); |
| 284 key = blink::WebCryptoKey::createNull(); |
| 285 |
| 286 // Consistency rules when JWK value exists: Fail if inconsistency is found. |
| 287 |
| 288 // Pass: All input values are consistent with the JWK values. |
| 289 dict.Clear(); |
| 290 dict.SetString("kty", "oct"); |
| 291 dict.SetString("alg", "HS256"); |
| 292 dict.SetString("use", "sig"); |
| 293 dict.SetBoolean("ext", false); |
| 294 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); |
| 295 json_vec = MakeJsonVector(dict); |
| 296 EXPECT_EQ(Status::Success(), |
| 297 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 298 CryptoData(json_vec), |
| 299 algorithm, |
| 300 extractable, |
| 301 usage_mask, |
| 302 &key)); |
| 303 |
| 304 // Extractable cases: |
| 305 // 1. input=T, JWK=F ==> fail (inconsistent) |
| 306 // 4. input=F, JWK=F ==> pass, result extractable is F |
| 307 // 2. input=T, JWK=T ==> pass, result extractable is T |
| 308 // 3. input=F, JWK=T ==> pass, result extractable is F |
| 309 EXPECT_EQ(Status::ErrorJwkExtInconsistent(), |
| 310 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 311 CryptoData(json_vec), |
| 312 algorithm, |
| 313 true, |
| 314 usage_mask, |
| 315 &key)); |
| 316 EXPECT_EQ(Status::Success(), |
| 317 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 318 CryptoData(json_vec), |
| 319 algorithm, |
| 320 false, |
| 321 usage_mask, |
| 322 &key)); |
| 323 EXPECT_FALSE(key.extractable()); |
| 324 dict.SetBoolean("ext", true); |
| 325 EXPECT_EQ(Status::Success(), |
| 326 ImportKeyJwkFromDict(dict, algorithm, true, usage_mask, &key)); |
| 327 EXPECT_TRUE(key.extractable()); |
| 328 EXPECT_EQ(Status::Success(), |
| 329 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); |
| 330 EXPECT_FALSE(key.extractable()); |
| 331 dict.SetBoolean("ext", true); // restore previous value |
| 332 |
| 333 // Fail: Input algorithm (AES-CBC) is inconsistent with JWK value |
| 334 // (HMAC SHA256). |
| 335 dict.Clear(); |
| 336 dict.SetString("kty", "oct"); |
| 337 dict.SetString("alg", "HS256"); |
| 338 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); |
| 339 EXPECT_EQ( |
| 340 Status::ErrorJwkAlgorithmInconsistent(), |
| 341 ImportKeyJwkFromDict(dict, |
| 342 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 343 extractable, |
| 344 blink::WebCryptoKeyUsageEncrypt, |
| 345 &key)); |
| 346 // Fail: Input usage (encrypt) is inconsistent with JWK value (use=sig). |
| 347 EXPECT_EQ(Status::ErrorJwkUseInconsistent(), |
| 348 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 349 CryptoData(json_vec), |
| 350 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 351 extractable, |
| 352 blink::WebCryptoKeyUsageEncrypt, |
| 353 &key)); |
| 354 |
| 355 // Fail: Input algorithm (HMAC SHA1) is inconsistent with JWK value |
| 356 // (HMAC SHA256). |
| 357 EXPECT_EQ( |
| 358 Status::ErrorJwkAlgorithmInconsistent(), |
| 359 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 360 CryptoData(json_vec), |
| 361 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1), |
| 362 extractable, |
| 363 usage_mask, |
| 364 &key)); |
| 365 |
| 366 // Pass: JWK alg missing but input algorithm specified: use input value |
| 367 dict.Remove("alg", NULL); |
| 368 EXPECT_EQ(Status::Success(), |
| 369 ImportKeyJwkFromDict( |
| 370 dict, |
| 371 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256), |
| 372 extractable, |
| 373 usage_mask, |
| 374 &key)); |
| 375 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, algorithm.id()); |
| 376 dict.SetString("alg", "HS256"); |
| 377 |
| 378 // Fail: Input usage_mask (encrypt) is not a subset of the JWK value |
| 379 // (sign|verify). Moreover "encrypt" is not a valid usage for HMAC. |
| 380 EXPECT_EQ(Status::ErrorCreateKeyBadUsages(), |
| 381 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 382 CryptoData(json_vec), |
| 383 algorithm, |
| 384 extractable, |
| 385 blink::WebCryptoKeyUsageEncrypt, |
| 386 &key)); |
| 387 |
| 388 // Fail: Input usage_mask (encrypt|sign|verify) is not a subset of the JWK |
| 389 // value (sign|verify). Moreover "encrypt" is not a valid usage for HMAC. |
| 390 usage_mask = blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageSign | |
| 391 blink::WebCryptoKeyUsageVerify; |
| 392 EXPECT_EQ(Status::ErrorCreateKeyBadUsages(), |
| 393 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 394 CryptoData(json_vec), |
| 395 algorithm, |
| 396 extractable, |
| 397 usage_mask, |
| 398 &key)); |
| 399 |
| 400 // TODO(padolph): kty vs alg consistency tests: Depending on the kty value, |
| 401 // only certain alg values are permitted. For example, when kty = "RSA" alg |
| 402 // must be of the RSA family, or when kty = "oct" alg must be symmetric |
| 403 // algorithm. |
| 404 |
| 405 // TODO(padolph): key_ops consistency tests |
| 406 } |
| 407 |
| 408 TEST(WebCryptoHmacTest, ImportJwkHappy) { |
| 409 // This test verifies the happy path of JWK import, including the application |
| 410 // of the imported key material. |
| 411 |
| 412 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 413 bool extractable = false; |
| 414 blink::WebCryptoAlgorithm algorithm = |
| 415 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256); |
| 416 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageSign; |
| 417 |
| 418 // Import a symmetric key JWK and HMAC-SHA256 sign() |
| 419 // Uses the first SHA256 test vector from the HMAC sample set above. |
| 420 |
| 421 base::DictionaryValue dict; |
| 422 dict.SetString("kty", "oct"); |
| 423 dict.SetString("alg", "HS256"); |
| 424 dict.SetString("use", "sig"); |
| 425 dict.SetBoolean("ext", false); |
| 426 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); |
| 427 |
| 428 ASSERT_EQ( |
| 429 Status::Success(), |
| 430 ImportKeyJwkFromDict(dict, algorithm, extractable, usage_mask, &key)); |
| 431 |
| 432 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, |
| 433 key.algorithm().hmacParams()->hash().id()); |
| 434 |
| 435 const std::vector<uint8_t> message_raw = HexStringToBytes( |
| 436 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" |
| 437 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" |
| 438 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" |
| 439 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e"); |
| 440 |
| 441 std::vector<uint8_t> output; |
| 442 |
| 443 ASSERT_EQ(Status::Success(), |
| 444 Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), |
| 445 key, |
| 446 CryptoData(message_raw), |
| 447 &output)); |
| 448 |
| 449 const std::string mac_raw = |
| 450 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b"; |
| 451 |
| 452 EXPECT_BYTES_EQ_HEX(mac_raw, output); |
| 453 |
| 454 // TODO(padolph): Import an RSA public key JWK and use it |
| 455 } |
| 456 |
| 457 TEST(WebCryptoHmacTest, ImportExportJwk) { |
| 458 // HMAC SHA-1 |
| 459 ImportExportJwkSymmetricKey( |
| 460 256, |
| 461 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1), |
| 462 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, |
| 463 "HS1"); |
| 464 |
| 465 // HMAC SHA-384 |
| 466 ImportExportJwkSymmetricKey( |
| 467 384, |
| 468 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha384), |
| 469 blink::WebCryptoKeyUsageSign, |
| 470 "HS384"); |
| 471 |
| 472 // HMAC SHA-512 |
| 473 ImportExportJwkSymmetricKey( |
| 474 512, |
| 475 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha512), |
| 476 blink::WebCryptoKeyUsageVerify, |
| 477 "HS512"); |
| 478 |
| 479 // Zero usage value |
| 480 ImportExportJwkSymmetricKey( |
| 481 512, |
| 482 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha512), |
| 483 0, |
| 484 "HS512"); |
| 485 } |
| 486 |
| 487 TEST(WebCryptoHmacTest, ExportJwkEmptyKey) { |
| 488 const blink::WebCryptoAlgorithm import_algorithm = |
| 489 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1); |
| 490 |
| 491 blink::WebCryptoKeyUsageMask usages = blink::WebCryptoKeyUsageSign; |
| 492 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 493 |
| 494 // Import a zero-byte HMAC key. |
| 495 const char key_data_hex[] = ""; |
| 496 key = ImportSecretKeyFromRaw( |
| 497 HexStringToBytes(key_data_hex), import_algorithm, usages); |
| 498 EXPECT_EQ(0u, key.algorithm().hmacParams()->lengthBits()); |
| 499 |
| 500 // Export the key in JWK format and validate. |
| 501 std::vector<uint8_t> json; |
| 502 ASSERT_EQ(Status::Success(), |
| 503 ExportKey(blink::WebCryptoKeyFormatJwk, key, &json)); |
| 504 EXPECT_TRUE(VerifySecretJwk(json, "HS1", key_data_hex, usages)); |
| 505 |
| 506 // Now try re-importing the JWK key. |
| 507 key = blink::WebCryptoKey::createNull(); |
| 508 EXPECT_EQ(Status::Success(), |
| 509 ImportKey(blink::WebCryptoKeyFormatJwk, |
| 510 CryptoData(json), |
| 511 import_algorithm, |
| 512 true, |
| 513 usages, |
| 514 &key)); |
| 515 |
| 516 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 517 EXPECT_EQ(0u, key.algorithm().hmacParams()->lengthBits()); |
| 518 |
| 519 std::vector<uint8_t> exported_key_data; |
| 520 EXPECT_EQ(Status::Success(), |
| 521 ExportKey(blink::WebCryptoKeyFormatRaw, key, &exported_key_data)); |
| 522 |
| 523 EXPECT_EQ(0u, exported_key_data.size()); |
| 524 } |
| 525 |
| 526 } // namespace |
| 527 |
| 528 } // namespace webcrypto |
| 529 |
| 530 } // namespace content |
OLD | NEW |