OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/webcrypto/shared_crypto.h" | 5 #include "content/child/webcrypto/shared_crypto.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 2746 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2757 // Import the key to be wrapped. | 2757 // Import the key to be wrapped. |
2758 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | 2758 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
2759 test_key, | 2759 test_key, |
2760 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), | 2760 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
2761 blink::WebCryptoKeyUsageEncrypt); | 2761 blink::WebCryptoKeyUsageEncrypt); |
2762 | 2762 |
2763 // Wrap the key and verify the ciphertext result against the known answer. | 2763 // Wrap the key and verify the ciphertext result against the known answer. |
2764 std::vector<uint8> wrapped_key; | 2764 std::vector<uint8> wrapped_key; |
2765 ASSERT_EQ(Status::Success(), | 2765 ASSERT_EQ(Status::Success(), |
2766 WrapKey(blink::WebCryptoKeyFormatRaw, | 2766 WrapKey(blink::WebCryptoKeyFormatRaw, |
| 2767 key, |
2767 wrapping_key, | 2768 wrapping_key, |
2768 key, | |
2769 wrapping_algorithm, | 2769 wrapping_algorithm, |
2770 &wrapped_key)); | 2770 &wrapped_key)); |
2771 EXPECT_BYTES_EQ(test_ciphertext, wrapped_key); | 2771 EXPECT_BYTES_EQ(test_ciphertext, wrapped_key); |
2772 | 2772 |
2773 // Unwrap the known ciphertext to get a new test_key. | 2773 // Unwrap the known ciphertext to get a new test_key. |
2774 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); | 2774 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); |
2775 ASSERT_EQ( | 2775 ASSERT_EQ( |
2776 Status::Success(), | 2776 Status::Success(), |
2777 UnwrapKey(blink::WebCryptoKeyFormatRaw, | 2777 UnwrapKey(blink::WebCryptoKeyFormatRaw, |
2778 CryptoData(test_ciphertext), | 2778 CryptoData(test_ciphertext), |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3100 CryptoData(cleartext), | 3100 CryptoData(cleartext), |
3101 key_algorithm, | 3101 key_algorithm, |
3102 true, | 3102 true, |
3103 blink::WebCryptoKeyUsageSign, | 3103 blink::WebCryptoKeyUsageSign, |
3104 &key)); | 3104 &key)); |
3105 | 3105 |
3106 // Wrap the symmetric key with raw format. | 3106 // Wrap the symmetric key with raw format. |
3107 std::vector<uint8> wrapped_key; | 3107 std::vector<uint8> wrapped_key; |
3108 ASSERT_EQ(Status::Success(), | 3108 ASSERT_EQ(Status::Success(), |
3109 WrapKey(blink::WebCryptoKeyFormatRaw, | 3109 WrapKey(blink::WebCryptoKeyFormatRaw, |
| 3110 key, |
3110 public_key, | 3111 public_key, |
3111 key, | |
3112 algorithm, | 3112 algorithm, |
3113 &wrapped_key)); | 3113 &wrapped_key)); |
3114 | 3114 |
3115 // Unwrap the wrapped key. | 3115 // Unwrap the wrapped key. |
3116 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); | 3116 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); |
3117 ASSERT_EQ(Status::Success(), | 3117 ASSERT_EQ(Status::Success(), |
3118 UnwrapKey(blink::WebCryptoKeyFormatRaw, | 3118 UnwrapKey(blink::WebCryptoKeyFormatRaw, |
3119 CryptoData(wrapped_key), | 3119 CryptoData(wrapped_key), |
3120 private_key, | 3120 private_key, |
3121 algorithm, | 3121 algorithm, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3177 CryptoData(data), | 3177 CryptoData(data), |
3178 key_algorithm, | 3178 key_algorithm, |
3179 true, | 3179 true, |
3180 blink::WebCryptoKeyUsageSign, | 3180 blink::WebCryptoKeyUsageSign, |
3181 &key)); | 3181 &key)); |
3182 | 3182 |
3183 // Wrapping with a private key should fail. | 3183 // Wrapping with a private key should fail. |
3184 std::vector<uint8> wrapped_key; | 3184 std::vector<uint8> wrapped_key; |
3185 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), | 3185 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), |
3186 WrapKey(blink::WebCryptoKeyFormatRaw, | 3186 WrapKey(blink::WebCryptoKeyFormatRaw, |
| 3187 key, |
3187 private_key, | 3188 private_key, |
3188 key, | |
3189 wrapping_algorithm, | 3189 wrapping_algorithm, |
3190 &wrapped_key)); | 3190 &wrapped_key)); |
3191 | 3191 |
3192 // Wrapping a key whose raw keying material is too large for the wrapping key | 3192 // Wrapping a key whose raw keying material is too large for the wrapping key |
3193 // should fail. | 3193 // should fail. |
3194 // RSAES can encrypt data up to length of k - 11 bytes, where k is the octet | 3194 // RSAES can encrypt data up to length of k - 11 bytes, where k is the octet |
3195 // length of the RSA modulus, and can decrypt data up to length k. Fabricate a | 3195 // length of the RSA modulus, and can decrypt data up to length k. Fabricate a |
3196 // big piece of data here that fails both of these criteria, so it can be used | 3196 // big piece of data here that fails both of these criteria, so it can be used |
3197 // for both wrap and unwrap negative tests below. | 3197 // for both wrap and unwrap negative tests below. |
3198 const std::vector<uint8> big_data(kModulusLengthBits / 8 + 1, 0); | 3198 const std::vector<uint8> big_data(kModulusLengthBits / 8 + 1, 0); |
3199 blink::WebCryptoKey big_key = blink::WebCryptoKey::createNull(); | 3199 blink::WebCryptoKey big_key = blink::WebCryptoKey::createNull(); |
3200 ASSERT_EQ(Status::Success(), | 3200 ASSERT_EQ(Status::Success(), |
3201 ImportKey(blink::WebCryptoKeyFormatRaw, | 3201 ImportKey(blink::WebCryptoKeyFormatRaw, |
3202 CryptoData(big_data), | 3202 CryptoData(big_data), |
3203 key_algorithm, | 3203 key_algorithm, |
3204 true, | 3204 true, |
3205 blink::WebCryptoKeyUsageSign, | 3205 blink::WebCryptoKeyUsageSign, |
3206 &big_key)); | 3206 &big_key)); |
3207 EXPECT_EQ(Status::ErrorDataTooLarge(), | 3207 EXPECT_EQ(Status::ErrorDataTooLarge(), |
3208 WrapKey(blink::WebCryptoKeyFormatRaw, | 3208 WrapKey(blink::WebCryptoKeyFormatRaw, |
| 3209 big_key, |
3209 public_key, | 3210 public_key, |
3210 big_key, | |
3211 wrapping_algorithm, | 3211 wrapping_algorithm, |
3212 &wrapped_key)); | 3212 &wrapped_key)); |
3213 | 3213 |
3214 // Unwrapping with a public key should fail. | 3214 // Unwrapping with a public key should fail. |
3215 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); | 3215 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); |
3216 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), | 3216 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), |
3217 UnwrapKey(blink::WebCryptoKeyFormatRaw, | 3217 UnwrapKey(blink::WebCryptoKeyFormatRaw, |
3218 CryptoData(data), | 3218 CryptoData(data), |
3219 public_key, | 3219 public_key, |
3220 wrapping_algorithm, | 3220 wrapping_algorithm, |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3320 wrapping_algorithm, | 3320 wrapping_algorithm, |
3321 false, | 3321 false, |
3322 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, | 3322 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, |
3323 &public_wrapping_key, | 3323 &public_wrapping_key, |
3324 &private_wrapping_key); | 3324 &private_wrapping_key); |
3325 | 3325 |
3326 // Wrap the symkey in JWK format, using the public wrapping key. | 3326 // Wrap the symkey in JWK format, using the public wrapping key. |
3327 std::vector<uint8> wrapped_data; | 3327 std::vector<uint8> wrapped_data; |
3328 ASSERT_EQ(Status::Success(), | 3328 ASSERT_EQ(Status::Success(), |
3329 WrapKey(blink::WebCryptoKeyFormatJwk, | 3329 WrapKey(blink::WebCryptoKeyFormatJwk, |
| 3330 key_to_wrap, |
3330 public_wrapping_key, | 3331 public_wrapping_key, |
3331 key_to_wrap, | |
3332 wrapping_algorithm, | 3332 wrapping_algorithm, |
3333 &wrapped_data)); | 3333 &wrapped_data)); |
3334 | 3334 |
3335 // Unwrap the key using the private wrapping key. | 3335 // Unwrap the key using the private wrapping key. |
3336 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); | 3336 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); |
3337 ASSERT_EQ(Status::Success(), | 3337 ASSERT_EQ(Status::Success(), |
3338 UnwrapKey(blink::WebCryptoKeyFormatJwk, | 3338 UnwrapKey(blink::WebCryptoKeyFormatJwk, |
3339 CryptoData(wrapped_data), | 3339 CryptoData(wrapped_data), |
3340 private_wrapping_key, | 3340 private_wrapping_key, |
3341 wrapping_algorithm, | 3341 wrapping_algorithm, |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3419 algorithm, | 3419 algorithm, |
3420 CreateAesCbcAlgorithm(std::vector<uint8>(0, 16)), | 3420 CreateAesCbcAlgorithm(std::vector<uint8>(0, 16)), |
3421 true, | 3421 true, |
3422 blink::WebCryptoKeyUsageEncrypt, | 3422 blink::WebCryptoKeyUsageEncrypt, |
3423 &unwrapped_key)); | 3423 &unwrapped_key)); |
3424 } | 3424 } |
3425 | 3425 |
3426 } // namespace webcrypto | 3426 } // namespace webcrypto |
3427 | 3427 |
3428 } // namespace content | 3428 } // namespace content |
OLD | NEW |