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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 iv.size(), | 99 iv.size(), |
100 true, | 100 true, |
101 Uint8VectorStart(additional_data), | 101 Uint8VectorStart(additional_data), |
102 additional_data.size(), | 102 additional_data.size(), |
103 true, | 103 true, |
104 tag_length_bits)); | 104 tag_length_bits)); |
105 } | 105 } |
106 | 106 |
107 // Creates an HMAC algorithm whose parameters struct is compatible with key | 107 // Creates an HMAC algorithm whose parameters struct is compatible with key |
108 // generation. It is an error to call this with a hash_id that is not a SHA*. | 108 // generation. It is an error to call this with a hash_id that is not a SHA*. |
109 // The key_length_bytes parameter is optional, with zero meaning unspecified. | 109 // The key_length_bits parameter is optional, with zero meaning unspecified. |
110 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( | 110 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( |
111 blink::WebCryptoAlgorithmId hash_id, | 111 blink::WebCryptoAlgorithmId hash_id, |
112 unsigned int key_length_bytes) { | 112 unsigned int key_length_bits) { |
| 113 #if !defined(WEBCRYPTO_HMAC_BITS) |
| 114 // TODO(eroman): Delete |
| 115 key_length_bits /= 8; |
| 116 #endif |
113 DCHECK(IsHashAlgorithm(hash_id)); | 117 DCHECK(IsHashAlgorithm(hash_id)); |
114 // key_length_bytes == 0 means unspecified | 118 // key_length_bytes == 0 means unspecified |
115 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 119 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
116 blink::WebCryptoAlgorithmIdHmac, | 120 blink::WebCryptoAlgorithmIdHmac, |
117 new blink::WebCryptoHmacKeyGenParams( | 121 new blink::WebCryptoHmacKeyGenParams( |
118 CreateAlgorithm(hash_id), (key_length_bytes != 0), key_length_bytes)); | 122 CreateAlgorithm(hash_id), (key_length_bits != 0), key_length_bits)); |
119 } | 123 } |
120 | 124 |
121 // Returns a slightly modified version of the input vector. | 125 // Returns a slightly modified version of the input vector. |
122 // | 126 // |
123 // - For non-empty inputs a single bit is inverted. | 127 // - For non-empty inputs a single bit is inverted. |
124 // - For empty inputs, a byte is added. | 128 // - For empty inputs, a byte is added. |
125 std::vector<uint8> Corrupted(const std::vector<uint8>& input) { | 129 std::vector<uint8> Corrupted(const std::vector<uint8>& input) { |
126 std::vector<uint8> corrupted_data(input); | 130 std::vector<uint8> corrupted_data(input); |
127 if (corrupted_data.empty()) | 131 if (corrupted_data.empty()) |
128 corrupted_data.push_back(0); | 132 corrupted_data.push_back(0); |
(...skipping 729 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
858 } | 862 } |
859 } | 863 } |
860 | 864 |
861 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmac)) { | 865 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmac)) { |
862 // Generate a small sample of HMAC keys. | 866 // Generate a small sample of HMAC keys. |
863 std::vector<blink::WebArrayBuffer> keys; | 867 std::vector<blink::WebArrayBuffer> keys; |
864 for (int i = 0; i < 16; ++i) { | 868 for (int i = 0; i < 16; ++i) { |
865 blink::WebArrayBuffer key_bytes; | 869 blink::WebArrayBuffer key_bytes; |
866 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 870 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
867 blink::WebCryptoAlgorithm algorithm = | 871 blink::WebCryptoAlgorithm algorithm = |
868 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 64); | 872 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 512); |
869 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); | 873 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key)); |
870 EXPECT_FALSE(key.isNull()); | 874 EXPECT_FALSE(key.isNull()); |
871 EXPECT_TRUE(key.handle()); | 875 EXPECT_TRUE(key.handle()); |
872 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 876 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
873 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 877 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
874 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 878 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
875 key.algorithm().hmacParams()->hash().id()); | 879 key.algorithm().hmacParams()->hash().id()); |
876 | 880 |
877 blink::WebArrayBuffer raw_key; | 881 blink::WebArrayBuffer raw_key; |
878 ASSERT_STATUS_SUCCESS( | 882 ASSERT_STATUS_SUCCESS( |
(...skipping 1425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2304 test_cipher_text, | 2308 test_cipher_text, |
2305 test_authentication_tag, | 2309 test_authentication_tag, |
2306 &plain_text)); | 2310 &plain_text)); |
2307 } | 2311 } |
2308 } | 2312 } |
2309 } | 2313 } |
2310 | 2314 |
2311 } // namespace webcrypto | 2315 } // namespace webcrypto |
2312 | 2316 |
2313 } // namespace content | 2317 } // namespace content |
OLD | NEW |