| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/renderer/webcrypto/webcrypto_impl.h" | 5 #include "content/renderer/webcrypto/webcrypto_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 std::vector<uint8> HexStringToBytes(const std::string& hex) { | 24 std::vector<uint8> HexStringToBytes(const std::string& hex) { |
| 25 std::vector<uint8> bytes; | 25 std::vector<uint8> bytes; |
| 26 base::HexStringToBytes(hex, &bytes); | 26 base::HexStringToBytes(hex, &bytes); |
| 27 return bytes; | 27 return bytes; |
| 28 } | 28 } |
| 29 | 29 |
| 30 void ExpectArrayBufferMatchesHex(const std::string& expected_hex, | 30 void ExpectArrayBufferMatchesHex(const std::string& expected_hex, |
| 31 const WebKit::WebArrayBuffer& array_buffer) { | 31 const blink::WebArrayBuffer& array_buffer) { |
| 32 EXPECT_STRCASEEQ( | 32 EXPECT_STRCASEEQ( |
| 33 expected_hex.c_str(), | 33 expected_hex.c_str(), |
| 34 base::HexEncode( | 34 base::HexEncode( |
| 35 array_buffer.data(), array_buffer.byteLength()).c_str()); | 35 array_buffer.data(), array_buffer.byteLength()).c_str()); |
| 36 } | 36 } |
| 37 | 37 |
| 38 WebKit::WebCryptoAlgorithm CreateAlgorithm(WebKit::WebCryptoAlgorithmId id) { | 38 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { |
| 39 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); | 39 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); |
| 40 } | 40 } |
| 41 | 41 |
| 42 WebKit::WebCryptoAlgorithm CreateHmacAlgorithm( | 42 blink::WebCryptoAlgorithm CreateHmacAlgorithm( |
| 43 WebKit::WebCryptoAlgorithmId hashId) { | 43 blink::WebCryptoAlgorithmId hashId) { |
| 44 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 44 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 45 WebKit::WebCryptoAlgorithmIdHmac, | 45 blink::WebCryptoAlgorithmIdHmac, |
| 46 new WebKit::WebCryptoHmacParams(CreateAlgorithm(hashId))); | 46 new blink::WebCryptoHmacParams(CreateAlgorithm(hashId))); |
| 47 } | 47 } |
| 48 | 48 |
| 49 WebKit::WebCryptoAlgorithm CreateHmacKeyAlgorithm( | 49 blink::WebCryptoAlgorithm CreateHmacKeyAlgorithm( |
| 50 WebKit::WebCryptoAlgorithmId hashId, | 50 blink::WebCryptoAlgorithmId hashId, |
| 51 unsigned hash_length) { | 51 unsigned hash_length) { |
| 52 // hash_length < 0 means unspecified | 52 // hash_length < 0 means unspecified |
| 53 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 53 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 54 WebKit::WebCryptoAlgorithmIdHmac, | 54 blink::WebCryptoAlgorithmIdHmac, |
| 55 new WebKit::WebCryptoHmacKeyParams(CreateAlgorithm(hashId), | 55 new blink::WebCryptoHmacKeyParams(CreateAlgorithm(hashId), |
| 56 (hash_length != 0), | 56 (hash_length != 0), |
| 57 hash_length)); | 57 hash_length)); |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Returns a pointer to the start of |data|, or NULL if it is empty. This is a | 60 // Returns a pointer to the start of |data|, or NULL if it is empty. This is a |
| 61 // convenience function for getting the pointer, and should not be used beyond | 61 // convenience function for getting the pointer, and should not be used beyond |
| 62 // the expected lifetime of |data|. | 62 // the expected lifetime of |data|. |
| 63 const uint8* Start(const std::vector<uint8>& data) { | 63 const uint8* Start(const std::vector<uint8>& data) { |
| 64 if (data.empty()) | 64 if (data.empty()) |
| 65 return NULL; | 65 return NULL; |
| 66 return &data[0]; | 66 return &data[0]; |
| 67 } | 67 } |
| 68 | 68 |
| 69 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm( | 69 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm( |
| 70 const std::vector<uint8>& iv) { | 70 const std::vector<uint8>& iv) { |
| 71 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 71 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 72 WebKit::WebCryptoAlgorithmIdAesCbc, | 72 blink::WebCryptoAlgorithmIdAesCbc, |
| 73 new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size())); | 73 new blink::WebCryptoAesCbcParams(Start(iv), iv.size())); |
| 74 } | 74 } |
| 75 | 75 |
| 76 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm( | 76 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm( |
| 77 unsigned short key_length_bits) { // NOLINT | 77 unsigned short key_length_bits) { // NOLINT |
| 78 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 78 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 79 WebKit::WebCryptoAlgorithmIdAesCbc, | 79 blink::WebCryptoAlgorithmIdAesCbc, |
| 80 new WebKit::WebCryptoAesKeyGenParams(key_length_bits)); | 80 new blink::WebCryptoAesKeyGenParams(key_length_bits)); |
| 81 } | 81 } |
| 82 | 82 |
| 83 WebKit::WebCryptoAlgorithm CreateRsaAlgorithm( | 83 blink::WebCryptoAlgorithm CreateRsaAlgorithm( |
| 84 WebKit::WebCryptoAlgorithmId algorithm_id, | 84 blink::WebCryptoAlgorithmId algorithm_id, |
| 85 unsigned modulus_length, | 85 unsigned modulus_length, |
| 86 const std::vector<uint8>& public_exponent) { | 86 const std::vector<uint8>& public_exponent) { |
| 87 DCHECK(algorithm_id == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || | 87 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || |
| 88 algorithm_id == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | 88 algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
| 89 algorithm_id == WebKit::WebCryptoAlgorithmIdRsaOaep); | 89 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); |
| 90 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 90 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 91 algorithm_id, | 91 algorithm_id, |
| 92 new WebKit::WebCryptoRsaKeyGenParams( | 92 new blink::WebCryptoRsaKeyGenParams( |
| 93 modulus_length, Start(public_exponent), public_exponent.size())); | 93 modulus_length, Start(public_exponent), public_exponent.size())); |
| 94 } | 94 } |
| 95 | 95 |
| 96 } // namespace | 96 } // namespace |
| 97 | 97 |
| 98 namespace content { | 98 namespace content { |
| 99 | 99 |
| 100 class WebCryptoImplTest : public testing::Test { | 100 class WebCryptoImplTest : public testing::Test { |
| 101 protected: | 101 protected: |
| 102 WebKit::WebCryptoKey ImportSecretKeyFromRawHexString( | 102 blink::WebCryptoKey ImportSecretKeyFromRawHexString( |
| 103 const std::string& key_hex, | 103 const std::string& key_hex, |
| 104 const WebKit::WebCryptoAlgorithm& algorithm, | 104 const blink::WebCryptoAlgorithm& algorithm, |
| 105 WebKit::WebCryptoKeyUsageMask usage) { | 105 blink::WebCryptoKeyUsageMask usage) { |
| 106 std::vector<uint8> key_raw = HexStringToBytes(key_hex); | 106 std::vector<uint8> key_raw = HexStringToBytes(key_hex); |
| 107 | 107 |
| 108 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 108 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 109 bool extractable = true; | 109 bool extractable = true; |
| 110 EXPECT_TRUE(crypto_.ImportKeyInternal(WebKit::WebCryptoKeyFormatRaw, | 110 EXPECT_TRUE(crypto_.ImportKeyInternal(blink::WebCryptoKeyFormatRaw, |
| 111 Start(key_raw), | 111 Start(key_raw), |
| 112 key_raw.size(), | 112 key_raw.size(), |
| 113 algorithm, | 113 algorithm, |
| 114 extractable, | 114 extractable, |
| 115 usage, | 115 usage, |
| 116 &key)); | 116 &key)); |
| 117 | 117 |
| 118 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, key.type()); | 118 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 119 EXPECT_FALSE(key.isNull()); | 119 EXPECT_FALSE(key.isNull()); |
| 120 EXPECT_TRUE(key.handle()); | 120 EXPECT_TRUE(key.handle()); |
| 121 return key; | 121 return key; |
| 122 } | 122 } |
| 123 | 123 |
| 124 // Forwarding methods to gain access to protected methods of | 124 // Forwarding methods to gain access to protected methods of |
| 125 // WebCryptoImpl. | 125 // WebCryptoImpl. |
| 126 | 126 |
| 127 bool DigestInternal( | 127 bool DigestInternal( |
| 128 const WebKit::WebCryptoAlgorithm& algorithm, | 128 const blink::WebCryptoAlgorithm& algorithm, |
| 129 const std::vector<uint8>& data, | 129 const std::vector<uint8>& data, |
| 130 WebKit::WebArrayBuffer* buffer) { | 130 blink::WebArrayBuffer* buffer) { |
| 131 return crypto_.DigestInternal(algorithm, Start(data), data.size(), buffer); | 131 return crypto_.DigestInternal(algorithm, Start(data), data.size(), buffer); |
| 132 } | 132 } |
| 133 | 133 |
| 134 bool GenerateKeyInternal( | 134 bool GenerateKeyInternal( |
| 135 const WebKit::WebCryptoAlgorithm& algorithm, | 135 const blink::WebCryptoAlgorithm& algorithm, |
| 136 WebKit::WebCryptoKey* key) { | 136 blink::WebCryptoKey* key) { |
| 137 bool extractable = true; | 137 bool extractable = true; |
| 138 WebKit::WebCryptoKeyUsageMask usage_mask = 0; | 138 blink::WebCryptoKeyUsageMask usage_mask = 0; |
| 139 return crypto_.GenerateKeyInternal(algorithm, extractable, usage_mask, key); | 139 return crypto_.GenerateKeyInternal(algorithm, extractable, usage_mask, key); |
| 140 } | 140 } |
| 141 | 141 |
| 142 bool GenerateKeyPairInternal( | 142 bool GenerateKeyPairInternal( |
| 143 const WebKit::WebCryptoAlgorithm& algorithm, | 143 const blink::WebCryptoAlgorithm& algorithm, |
| 144 bool extractable, | 144 bool extractable, |
| 145 WebKit::WebCryptoKeyUsageMask usage_mask, | 145 blink::WebCryptoKeyUsageMask usage_mask, |
| 146 WebKit::WebCryptoKey* public_key, | 146 blink::WebCryptoKey* public_key, |
| 147 WebKit::WebCryptoKey* private_key) { | 147 blink::WebCryptoKey* private_key) { |
| 148 return crypto_.GenerateKeyPairInternal( | 148 return crypto_.GenerateKeyPairInternal( |
| 149 algorithm, extractable, usage_mask, public_key, private_key); | 149 algorithm, extractable, usage_mask, public_key, private_key); |
| 150 } | 150 } |
| 151 | 151 |
| 152 bool ImportKeyInternal( | 152 bool ImportKeyInternal( |
| 153 WebKit::WebCryptoKeyFormat format, | 153 blink::WebCryptoKeyFormat format, |
| 154 const std::vector<uint8>& key_data, | 154 const std::vector<uint8>& key_data, |
| 155 const WebKit::WebCryptoAlgorithm& algorithm, | 155 const blink::WebCryptoAlgorithm& algorithm, |
| 156 WebKit::WebCryptoKeyUsageMask usage_mask, | 156 blink::WebCryptoKeyUsageMask usage_mask, |
| 157 WebKit::WebCryptoKey* key) { | 157 blink::WebCryptoKey* key) { |
| 158 bool extractable = true; | 158 bool extractable = true; |
| 159 return crypto_.ImportKeyInternal(format, | 159 return crypto_.ImportKeyInternal(format, |
| 160 Start(key_data), | 160 Start(key_data), |
| 161 key_data.size(), | 161 key_data.size(), |
| 162 algorithm, | 162 algorithm, |
| 163 extractable, | 163 extractable, |
| 164 usage_mask, | 164 usage_mask, |
| 165 key); | 165 key); |
| 166 } | 166 } |
| 167 | 167 |
| 168 bool SignInternal( | 168 bool SignInternal( |
| 169 const WebKit::WebCryptoAlgorithm& algorithm, | 169 const blink::WebCryptoAlgorithm& algorithm, |
| 170 const WebKit::WebCryptoKey& key, | 170 const blink::WebCryptoKey& key, |
| 171 const std::vector<uint8>& data, | 171 const std::vector<uint8>& data, |
| 172 WebKit::WebArrayBuffer* buffer) { | 172 blink::WebArrayBuffer* buffer) { |
| 173 return crypto_.SignInternal( | 173 return crypto_.SignInternal( |
| 174 algorithm, key, Start(data), data.size(), buffer); | 174 algorithm, key, Start(data), data.size(), buffer); |
| 175 } | 175 } |
| 176 | 176 |
| 177 bool VerifySignatureInternal( | 177 bool VerifySignatureInternal( |
| 178 const WebKit::WebCryptoAlgorithm& algorithm, | 178 const blink::WebCryptoAlgorithm& algorithm, |
| 179 const WebKit::WebCryptoKey& key, | 179 const blink::WebCryptoKey& key, |
| 180 const unsigned char* signature, | 180 const unsigned char* signature, |
| 181 unsigned signature_size, | 181 unsigned signature_size, |
| 182 const std::vector<uint8>& data, | 182 const std::vector<uint8>& data, |
| 183 bool* signature_match) { | 183 bool* signature_match) { |
| 184 return crypto_.VerifySignatureInternal(algorithm, | 184 return crypto_.VerifySignatureInternal(algorithm, |
| 185 key, | 185 key, |
| 186 signature, | 186 signature, |
| 187 signature_size, | 187 signature_size, |
| 188 Start(data), | 188 Start(data), |
| 189 data.size(), | 189 data.size(), |
| 190 signature_match); | 190 signature_match); |
| 191 } | 191 } |
| 192 | 192 |
| 193 bool EncryptInternal( | 193 bool EncryptInternal( |
| 194 const WebKit::WebCryptoAlgorithm& algorithm, | 194 const blink::WebCryptoAlgorithm& algorithm, |
| 195 const WebKit::WebCryptoKey& key, | 195 const blink::WebCryptoKey& key, |
| 196 const unsigned char* data, | 196 const unsigned char* data, |
| 197 unsigned data_size, | 197 unsigned data_size, |
| 198 WebKit::WebArrayBuffer* buffer) { | 198 blink::WebArrayBuffer* buffer) { |
| 199 return crypto_.EncryptInternal(algorithm, key, data, data_size, buffer); | 199 return crypto_.EncryptInternal(algorithm, key, data, data_size, buffer); |
| 200 } | 200 } |
| 201 | 201 |
| 202 bool EncryptInternal( | 202 bool EncryptInternal( |
| 203 const WebKit::WebCryptoAlgorithm& algorithm, | 203 const blink::WebCryptoAlgorithm& algorithm, |
| 204 const WebKit::WebCryptoKey& key, | 204 const blink::WebCryptoKey& key, |
| 205 const std::vector<uint8>& data, | 205 const std::vector<uint8>& data, |
| 206 WebKit::WebArrayBuffer* buffer) { | 206 blink::WebArrayBuffer* buffer) { |
| 207 return crypto_.EncryptInternal( | 207 return crypto_.EncryptInternal( |
| 208 algorithm, key, Start(data), data.size(), buffer); | 208 algorithm, key, Start(data), data.size(), buffer); |
| 209 } | 209 } |
| 210 | 210 |
| 211 bool DecryptInternal( | 211 bool DecryptInternal( |
| 212 const WebKit::WebCryptoAlgorithm& algorithm, | 212 const blink::WebCryptoAlgorithm& algorithm, |
| 213 const WebKit::WebCryptoKey& key, | 213 const blink::WebCryptoKey& key, |
| 214 const unsigned char* data, | 214 const unsigned char* data, |
| 215 unsigned data_size, | 215 unsigned data_size, |
| 216 WebKit::WebArrayBuffer* buffer) { | 216 blink::WebArrayBuffer* buffer) { |
| 217 return crypto_.DecryptInternal(algorithm, key, data, data_size, buffer); | 217 return crypto_.DecryptInternal(algorithm, key, data, data_size, buffer); |
| 218 } | 218 } |
| 219 | 219 |
| 220 bool DecryptInternal( | 220 bool DecryptInternal( |
| 221 const WebKit::WebCryptoAlgorithm& algorithm, | 221 const blink::WebCryptoAlgorithm& algorithm, |
| 222 const WebKit::WebCryptoKey& key, | 222 const blink::WebCryptoKey& key, |
| 223 const std::vector<uint8>& data, | 223 const std::vector<uint8>& data, |
| 224 WebKit::WebArrayBuffer* buffer) { | 224 blink::WebArrayBuffer* buffer) { |
| 225 return crypto_.DecryptInternal( | 225 return crypto_.DecryptInternal( |
| 226 algorithm, key, Start(data), data.size(), buffer); | 226 algorithm, key, Start(data), data.size(), buffer); |
| 227 } | 227 } |
| 228 | 228 |
| 229 private: | 229 private: |
| 230 WebCryptoImpl crypto_; | 230 WebCryptoImpl crypto_; |
| 231 }; | 231 }; |
| 232 | 232 |
| 233 TEST_F(WebCryptoImplTest, DigestSampleSets) { | 233 TEST_F(WebCryptoImplTest, DigestSampleSets) { |
| 234 // The results are stored here in hex format for readability. | 234 // The results are stored here in hex format for readability. |
| 235 // | 235 // |
| 236 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced | 236 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced |
| 237 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 | 237 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 |
| 238 // | 238 // |
| 239 // Results were generated using the command sha{1,224,256,384,512}sum. | 239 // Results were generated using the command sha{1,224,256,384,512}sum. |
| 240 struct TestCase { | 240 struct TestCase { |
| 241 WebKit::WebCryptoAlgorithmId algorithm; | 241 blink::WebCryptoAlgorithmId algorithm; |
| 242 const std::string hex_input; | 242 const std::string hex_input; |
| 243 const char* hex_result; | 243 const char* hex_result; |
| 244 }; | 244 }; |
| 245 | 245 |
| 246 const TestCase kTests[] = { | 246 const TestCase kTests[] = { |
| 247 { WebKit::WebCryptoAlgorithmIdSha1, "", | 247 { blink::WebCryptoAlgorithmIdSha1, "", |
| 248 "da39a3ee5e6b4b0d3255bfef95601890afd80709" | 248 "da39a3ee5e6b4b0d3255bfef95601890afd80709" |
| 249 }, | 249 }, |
| 250 { WebKit::WebCryptoAlgorithmIdSha224, "", | 250 { blink::WebCryptoAlgorithmIdSha224, "", |
| 251 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f" | 251 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f" |
| 252 }, | 252 }, |
| 253 { WebKit::WebCryptoAlgorithmIdSha256, "", | 253 { blink::WebCryptoAlgorithmIdSha256, "", |
| 254 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" | 254 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" |
| 255 }, | 255 }, |
| 256 { WebKit::WebCryptoAlgorithmIdSha384, "", | 256 { blink::WebCryptoAlgorithmIdSha384, "", |
| 257 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" | 257 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" |
| 258 "debfe76f65fbd51ad2f14898b95b" | 258 "debfe76f65fbd51ad2f14898b95b" |
| 259 }, | 259 }, |
| 260 { WebKit::WebCryptoAlgorithmIdSha512, "", | 260 { blink::WebCryptoAlgorithmIdSha512, "", |
| 261 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" | 261 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" |
| 262 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", | 262 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", |
| 263 }, | 263 }, |
| 264 { WebKit::WebCryptoAlgorithmIdSha1, "00", | 264 { blink::WebCryptoAlgorithmIdSha1, "00", |
| 265 "5ba93c9db0cff93f52b521d7420e43f6eda2784f", | 265 "5ba93c9db0cff93f52b521d7420e43f6eda2784f", |
| 266 }, | 266 }, |
| 267 { WebKit::WebCryptoAlgorithmIdSha224, "00", | 267 { blink::WebCryptoAlgorithmIdSha224, "00", |
| 268 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", | 268 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", |
| 269 }, | 269 }, |
| 270 { WebKit::WebCryptoAlgorithmIdSha256, "00", | 270 { blink::WebCryptoAlgorithmIdSha256, "00", |
| 271 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", | 271 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", |
| 272 }, | 272 }, |
| 273 { WebKit::WebCryptoAlgorithmIdSha384, "00", | 273 { blink::WebCryptoAlgorithmIdSha384, "00", |
| 274 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" | 274 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" |
| 275 "ec2b413465966817a9c208a11717", | 275 "ec2b413465966817a9c208a11717", |
| 276 }, | 276 }, |
| 277 { WebKit::WebCryptoAlgorithmIdSha512, "00", | 277 { blink::WebCryptoAlgorithmIdSha512, "00", |
| 278 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" | 278 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" |
| 279 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", | 279 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", |
| 280 }, | 280 }, |
| 281 { WebKit::WebCryptoAlgorithmIdSha1, "000102030405", | 281 { blink::WebCryptoAlgorithmIdSha1, "000102030405", |
| 282 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", | 282 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", |
| 283 }, | 283 }, |
| 284 { WebKit::WebCryptoAlgorithmIdSha224, "000102030405", | 284 { blink::WebCryptoAlgorithmIdSha224, "000102030405", |
| 285 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", | 285 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", |
| 286 }, | 286 }, |
| 287 { WebKit::WebCryptoAlgorithmIdSha256, "000102030405", | 287 { blink::WebCryptoAlgorithmIdSha256, "000102030405", |
| 288 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", | 288 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", |
| 289 }, | 289 }, |
| 290 { WebKit::WebCryptoAlgorithmIdSha384, "000102030405", | 290 { blink::WebCryptoAlgorithmIdSha384, "000102030405", |
| 291 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | 291 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" |
| 292 "22780d7e1b95bfeaa86a678e4552", | 292 "22780d7e1b95bfeaa86a678e4552", |
| 293 }, | 293 }, |
| 294 { WebKit::WebCryptoAlgorithmIdSha512, "000102030405", | 294 { blink::WebCryptoAlgorithmIdSha512, "000102030405", |
| 295 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | 295 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" |
| 296 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | 296 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", |
| 297 }, | 297 }, |
| 298 }; | 298 }; |
| 299 | 299 |
| 300 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); | 300 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); |
| 301 ++test_index) { | 301 ++test_index) { |
| 302 SCOPED_TRACE(test_index); | 302 SCOPED_TRACE(test_index); |
| 303 const TestCase& test = kTests[test_index]; | 303 const TestCase& test = kTests[test_index]; |
| 304 | 304 |
| 305 WebKit::WebCryptoAlgorithm algorithm = CreateAlgorithm(test.algorithm); | 305 blink::WebCryptoAlgorithm algorithm = CreateAlgorithm(test.algorithm); |
| 306 std::vector<uint8> input = HexStringToBytes(test.hex_input); | 306 std::vector<uint8> input = HexStringToBytes(test.hex_input); |
| 307 | 307 |
| 308 WebKit::WebArrayBuffer output; | 308 blink::WebArrayBuffer output; |
| 309 ASSERT_TRUE(DigestInternal(algorithm, input, &output)); | 309 ASSERT_TRUE(DigestInternal(algorithm, input, &output)); |
| 310 ExpectArrayBufferMatchesHex(test.hex_result, output); | 310 ExpectArrayBufferMatchesHex(test.hex_result, output); |
| 311 } | 311 } |
| 312 } | 312 } |
| 313 | 313 |
| 314 TEST_F(WebCryptoImplTest, HMACSampleSets) { | 314 TEST_F(WebCryptoImplTest, HMACSampleSets) { |
| 315 struct TestCase { | 315 struct TestCase { |
| 316 WebKit::WebCryptoAlgorithmId algorithm; | 316 blink::WebCryptoAlgorithmId algorithm; |
| 317 const char* key; | 317 const char* key; |
| 318 const char* message; | 318 const char* message; |
| 319 const char* mac; | 319 const char* mac; |
| 320 }; | 320 }; |
| 321 | 321 |
| 322 const TestCase kTests[] = { | 322 const TestCase kTests[] = { |
| 323 // Empty sets. Result generated via OpenSSL commandline tool. These | 323 // Empty sets. Result generated via OpenSSL commandline tool. These |
| 324 // particular results are also posted on the Wikipedia page examples: | 324 // particular results are also posted on the Wikipedia page examples: |
| 325 // http://en.wikipedia.org/wiki/Hash-based_message_authentication_code | 325 // http://en.wikipedia.org/wiki/Hash-based_message_authentication_code |
| 326 { | 326 { |
| 327 WebKit::WebCryptoAlgorithmIdSha1, | 327 blink::WebCryptoAlgorithmIdSha1, |
| 328 "", | 328 "", |
| 329 "", | 329 "", |
| 330 // openssl dgst -sha1 -hmac "" < /dev/null | 330 // openssl dgst -sha1 -hmac "" < /dev/null |
| 331 "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", | 331 "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", |
| 332 }, | 332 }, |
| 333 { | 333 { |
| 334 WebKit::WebCryptoAlgorithmIdSha256, | 334 blink::WebCryptoAlgorithmIdSha256, |
| 335 "", | 335 "", |
| 336 "", | 336 "", |
| 337 // openssl dgst -sha256 -hmac "" < /dev/null | 337 // openssl dgst -sha256 -hmac "" < /dev/null |
| 338 "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", | 338 "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", |
| 339 }, | 339 }, |
| 340 // For this data, see http://csrc.nist.gov/groups/STM/cavp/index.html#07 | 340 // For this data, see http://csrc.nist.gov/groups/STM/cavp/index.html#07 |
| 341 // Download: | 341 // Download: |
| 342 // http://csrc.nist.gov/groups/STM/cavp/documents/mac/hmactestvectors.zip | 342 // http://csrc.nist.gov/groups/STM/cavp/documents/mac/hmactestvectors.zip |
| 343 // L=20 set 45 | 343 // L=20 set 45 |
| 344 { | 344 { |
| 345 WebKit::WebCryptoAlgorithmIdSha1, | 345 blink::WebCryptoAlgorithmIdSha1, |
| 346 // key | 346 // key |
| 347 "59785928d72516e31272", | 347 "59785928d72516e31272", |
| 348 // message | 348 // message |
| 349 "a3ce8899df1022e8d2d539b47bf0e309c66f84095e21438ec355bf119ce5fdcb4e73a6" | 349 "a3ce8899df1022e8d2d539b47bf0e309c66f84095e21438ec355bf119ce5fdcb4e73a6" |
| 350 "19cdf36f25b369d8c38ff419997f0c59830108223606e31223483fd39edeaa4d3f0d21" | 350 "19cdf36f25b369d8c38ff419997f0c59830108223606e31223483fd39edeaa4d3f0d21" |
| 351 "198862d239c9fd26074130ff6c86493f5227ab895c8f244bd42c7afce5d147a20a5907" | 351 "198862d239c9fd26074130ff6c86493f5227ab895c8f244bd42c7afce5d147a20a5907" |
| 352 "98c68e708e964902d124dadecdbda9dbd0051ed710e9bf", | 352 "98c68e708e964902d124dadecdbda9dbd0051ed710e9bf", |
| 353 // mac | 353 // mac |
| 354 "3c8162589aafaee024fc9a5ca50dd2336fe3eb28", | 354 "3c8162589aafaee024fc9a5ca50dd2336fe3eb28", |
| 355 }, | 355 }, |
| 356 // L=20 set 299 | 356 // L=20 set 299 |
| 357 { | 357 { |
| 358 WebKit::WebCryptoAlgorithmIdSha1, | 358 blink::WebCryptoAlgorithmIdSha1, |
| 359 // key | 359 // key |
| 360 "ceb9aedf8d6efcf0ae52bea0fa99a9e26ae81bacea0cff4d5eecf201e3bca3c3577480" | 360 "ceb9aedf8d6efcf0ae52bea0fa99a9e26ae81bacea0cff4d5eecf201e3bca3c3577480" |
| 361 "621b818fd717ba99d6ff958ea3d59b2527b019c343bb199e648090225867d994607962" | 361 "621b818fd717ba99d6ff958ea3d59b2527b019c343bb199e648090225867d994607962" |
| 362 "f5866aa62930d75b58f6", | 362 "f5866aa62930d75b58f6", |
| 363 // message | 363 // message |
| 364 "99958aa459604657c7bf6e4cdfcc8785f0abf06ffe636b5b64ecd931bd8a4563055924" | 364 "99958aa459604657c7bf6e4cdfcc8785f0abf06ffe636b5b64ecd931bd8a4563055924" |
| 365 "21fc28dbcccb8a82acea2be8e54161d7a78e0399a6067ebaca3f2510274dc9f92f2c8a" | 365 "21fc28dbcccb8a82acea2be8e54161d7a78e0399a6067ebaca3f2510274dc9f92f2c8a" |
| 366 "e4265eec13d7d42e9f8612d7bc258f913ecb5a3a5c610339b49fb90e9037b02d684fc6" | 366 "e4265eec13d7d42e9f8612d7bc258f913ecb5a3a5c610339b49fb90e9037b02d684fc6" |
| 367 "0da835657cb24eab352750c8b463b1a8494660d36c3ab2", | 367 "0da835657cb24eab352750c8b463b1a8494660d36c3ab2", |
| 368 // mac | 368 // mac |
| 369 "4ac41ab89f625c60125ed65ffa958c6b490ea670", | 369 "4ac41ab89f625c60125ed65ffa958c6b490ea670", |
| 370 }, | 370 }, |
| 371 // L=32, set 30 | 371 // L=32, set 30 |
| 372 { | 372 { |
| 373 WebKit::WebCryptoAlgorithmIdSha256, | 373 blink::WebCryptoAlgorithmIdSha256, |
| 374 // key | 374 // key |
| 375 "9779d9120642797f1747025d5b22b7ac607cab08e1758f2f3a46c8be1e25c53b8c6a8f" | 375 "9779d9120642797f1747025d5b22b7ac607cab08e1758f2f3a46c8be1e25c53b8c6a8f" |
| 376 "58ffefa176", | 376 "58ffefa176", |
| 377 // message | 377 // message |
| 378 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" | 378 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" |
| 379 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" | 379 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" |
| 380 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" | 380 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" |
| 381 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e", | 381 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e", |
| 382 // mac | 382 // mac |
| 383 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b", | 383 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b", |
| 384 }, | 384 }, |
| 385 // L=32, set 224 | 385 // L=32, set 224 |
| 386 { | 386 { |
| 387 WebKit::WebCryptoAlgorithmIdSha256, | 387 blink::WebCryptoAlgorithmIdSha256, |
| 388 // key | 388 // key |
| 389 "4b7ab133efe99e02fc89a28409ee187d579e774f4cba6fc223e13504e3511bef8d4f63" | 389 "4b7ab133efe99e02fc89a28409ee187d579e774f4cba6fc223e13504e3511bef8d4f63" |
| 390 "8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b08" | 390 "8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b08" |
| 391 "3371289b", | 391 "3371289b", |
| 392 // message | 392 // message |
| 393 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69" | 393 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69" |
| 394 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e" | 394 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e" |
| 395 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c" | 395 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c" |
| 396 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99", | 396 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99", |
| 397 // mac | 397 // mac |
| 398 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", | 398 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", |
| 399 }, | 399 }, |
| 400 }; | 400 }; |
| 401 | 401 |
| 402 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); | 402 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); |
| 403 ++test_index) { | 403 ++test_index) { |
| 404 SCOPED_TRACE(test_index); | 404 SCOPED_TRACE(test_index); |
| 405 const TestCase& test = kTests[test_index]; | 405 const TestCase& test = kTests[test_index]; |
| 406 | 406 |
| 407 WebKit::WebCryptoAlgorithm algorithm = CreateHmacAlgorithm(test.algorithm); | 407 blink::WebCryptoAlgorithm algorithm = CreateHmacAlgorithm(test.algorithm); |
| 408 | 408 |
| 409 WebKit::WebCryptoKey key = ImportSecretKeyFromRawHexString( | 409 blink::WebCryptoKey key = ImportSecretKeyFromRawHexString( |
| 410 test.key, algorithm, WebKit::WebCryptoKeyUsageSign); | 410 test.key, algorithm, blink::WebCryptoKeyUsageSign); |
| 411 | 411 |
| 412 std::vector<uint8> message_raw = HexStringToBytes(test.message); | 412 std::vector<uint8> message_raw = HexStringToBytes(test.message); |
| 413 | 413 |
| 414 WebKit::WebArrayBuffer output; | 414 blink::WebArrayBuffer output; |
| 415 | 415 |
| 416 ASSERT_TRUE(SignInternal(algorithm, key, message_raw, &output)); | 416 ASSERT_TRUE(SignInternal(algorithm, key, message_raw, &output)); |
| 417 | 417 |
| 418 ExpectArrayBufferMatchesHex(test.mac, output); | 418 ExpectArrayBufferMatchesHex(test.mac, output); |
| 419 | 419 |
| 420 bool signature_match = false; | 420 bool signature_match = false; |
| 421 EXPECT_TRUE(VerifySignatureInternal( | 421 EXPECT_TRUE(VerifySignatureInternal( |
| 422 algorithm, | 422 algorithm, |
| 423 key, | 423 key, |
| 424 static_cast<const unsigned char*>(output.data()), | 424 static_cast<const unsigned char*>(output.data()), |
| (...skipping 19 matching lines...) Expand all Loading... |
| 444 key, | 444 key, |
| 445 kLongSignature, | 445 kLongSignature, |
| 446 sizeof(kLongSignature), | 446 sizeof(kLongSignature), |
| 447 message_raw, | 447 message_raw, |
| 448 &signature_match)); | 448 &signature_match)); |
| 449 EXPECT_FALSE(signature_match); | 449 EXPECT_FALSE(signature_match); |
| 450 } | 450 } |
| 451 } | 451 } |
| 452 | 452 |
| 453 TEST_F(WebCryptoImplTest, AesCbcFailures) { | 453 TEST_F(WebCryptoImplTest, AesCbcFailures) { |
| 454 WebKit::WebCryptoKey key = ImportSecretKeyFromRawHexString( | 454 blink::WebCryptoKey key = ImportSecretKeyFromRawHexString( |
| 455 "2b7e151628aed2a6abf7158809cf4f3c", | 455 "2b7e151628aed2a6abf7158809cf4f3c", |
| 456 CreateAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc), | 456 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 457 WebKit::WebCryptoKeyUsageEncrypt | WebKit::WebCryptoKeyUsageDecrypt); | 457 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); |
| 458 | 458 |
| 459 WebKit::WebArrayBuffer output; | 459 blink::WebArrayBuffer output; |
| 460 | 460 |
| 461 // Use an invalid |iv| (fewer than 16 bytes) | 461 // Use an invalid |iv| (fewer than 16 bytes) |
| 462 { | 462 { |
| 463 std::vector<uint8> input(32); | 463 std::vector<uint8> input(32); |
| 464 std::vector<uint8> iv; | 464 std::vector<uint8> iv; |
| 465 EXPECT_FALSE( | 465 EXPECT_FALSE( |
| 466 EncryptInternal(CreateAesCbcAlgorithm(iv), key, input, &output)); | 466 EncryptInternal(CreateAesCbcAlgorithm(iv), key, input, &output)); |
| 467 EXPECT_FALSE( | 467 EXPECT_FALSE( |
| 468 DecryptInternal(CreateAesCbcAlgorithm(iv), key, input, &output)); | 468 DecryptInternal(CreateAesCbcAlgorithm(iv), key, input, &output)); |
| 469 } | 469 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 493 CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); | 493 CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); |
| 494 EXPECT_FALSE(DecryptInternal( | 494 EXPECT_FALSE(DecryptInternal( |
| 495 CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); | 495 CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); |
| 496 } | 496 } |
| 497 | 497 |
| 498 // Fail importing the key (too few bytes specified) | 498 // Fail importing the key (too few bytes specified) |
| 499 { | 499 { |
| 500 std::vector<uint8> key_raw(1); | 500 std::vector<uint8> key_raw(1); |
| 501 std::vector<uint8> iv(16); | 501 std::vector<uint8> iv(16); |
| 502 | 502 |
| 503 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 503 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 504 EXPECT_FALSE(ImportKeyInternal(WebKit::WebCryptoKeyFormatRaw, | 504 EXPECT_FALSE(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, |
| 505 key_raw, | 505 key_raw, |
| 506 CreateAesCbcAlgorithm(iv), | 506 CreateAesCbcAlgorithm(iv), |
| 507 WebKit::WebCryptoKeyUsageDecrypt, | 507 blink::WebCryptoKeyUsageDecrypt, |
| 508 &key)); | 508 &key)); |
| 509 } | 509 } |
| 510 } | 510 } |
| 511 | 511 |
| 512 TEST_F(WebCryptoImplTest, AesCbcSampleSets) { | 512 TEST_F(WebCryptoImplTest, AesCbcSampleSets) { |
| 513 struct TestCase { | 513 struct TestCase { |
| 514 const char* key; | 514 const char* key; |
| 515 const char* iv; | 515 const char* iv; |
| 516 const char* plain_text; | 516 const char* plain_text; |
| 517 const char* cipher_text; | 517 const char* cipher_text; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 584 | 584 |
| 585 // cipher_text | 585 // cipher_text |
| 586 "8518b8878d34e7185e300d0fcc426396" | 586 "8518b8878d34e7185e300d0fcc426396" |
| 587 }, | 587 }, |
| 588 }; | 588 }; |
| 589 | 589 |
| 590 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(kTests); index++) { | 590 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(kTests); index++) { |
| 591 SCOPED_TRACE(index); | 591 SCOPED_TRACE(index); |
| 592 const TestCase& test = kTests[index]; | 592 const TestCase& test = kTests[index]; |
| 593 | 593 |
| 594 WebKit::WebCryptoKey key = ImportSecretKeyFromRawHexString( | 594 blink::WebCryptoKey key = ImportSecretKeyFromRawHexString( |
| 595 test.key, | 595 test.key, |
| 596 CreateAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc), | 596 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), |
| 597 WebKit::WebCryptoKeyUsageEncrypt | WebKit::WebCryptoKeyUsageDecrypt); | 597 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); |
| 598 | 598 |
| 599 std::vector<uint8> plain_text = HexStringToBytes(test.plain_text); | 599 std::vector<uint8> plain_text = HexStringToBytes(test.plain_text); |
| 600 std::vector<uint8> iv = HexStringToBytes(test.iv); | 600 std::vector<uint8> iv = HexStringToBytes(test.iv); |
| 601 | 601 |
| 602 WebKit::WebArrayBuffer output; | 602 blink::WebArrayBuffer output; |
| 603 | 603 |
| 604 // Test encryption. | 604 // Test encryption. |
| 605 EXPECT_TRUE(EncryptInternal(CreateAesCbcAlgorithm(iv), | 605 EXPECT_TRUE(EncryptInternal(CreateAesCbcAlgorithm(iv), |
| 606 key, | 606 key, |
| 607 plain_text, | 607 plain_text, |
| 608 &output)); | 608 &output)); |
| 609 ExpectArrayBufferMatchesHex(test.cipher_text, output); | 609 ExpectArrayBufferMatchesHex(test.cipher_text, output); |
| 610 | 610 |
| 611 // Test decryption. | 611 // Test decryption. |
| 612 std::vector<uint8> cipher_text = HexStringToBytes(test.cipher_text); | 612 std::vector<uint8> cipher_text = HexStringToBytes(test.cipher_text); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 636 &cipher_text[0], | 636 &cipher_text[0], |
| 637 cipher_text.size() - 3, | 637 cipher_text.size() - 3, |
| 638 &output)); | 638 &output)); |
| 639 } | 639 } |
| 640 } | 640 } |
| 641 } | 641 } |
| 642 | 642 |
| 643 // TODO(padolph): Add test to verify generated symmetric keys appear random. | 643 // TODO(padolph): Add test to verify generated symmetric keys appear random. |
| 644 | 644 |
| 645 TEST_F(WebCryptoImplTest, GenerateKeyAes) { | 645 TEST_F(WebCryptoImplTest, GenerateKeyAes) { |
| 646 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 646 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 647 ASSERT_TRUE(GenerateKeyInternal(CreateAesCbcAlgorithm(128), &key)); | 647 ASSERT_TRUE(GenerateKeyInternal(CreateAesCbcAlgorithm(128), &key)); |
| 648 EXPECT_TRUE(key.handle()); | 648 EXPECT_TRUE(key.handle()); |
| 649 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, key.type()); | 649 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 650 } | 650 } |
| 651 | 651 |
| 652 TEST_F(WebCryptoImplTest, GenerateKeyAesBadLength) { | 652 TEST_F(WebCryptoImplTest, GenerateKeyAesBadLength) { |
| 653 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 653 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 654 EXPECT_FALSE(GenerateKeyInternal(CreateAesCbcAlgorithm(0), &key)); | 654 EXPECT_FALSE(GenerateKeyInternal(CreateAesCbcAlgorithm(0), &key)); |
| 655 EXPECT_FALSE(GenerateKeyInternal(CreateAesCbcAlgorithm(129), &key)); | 655 EXPECT_FALSE(GenerateKeyInternal(CreateAesCbcAlgorithm(129), &key)); |
| 656 } | 656 } |
| 657 | 657 |
| 658 TEST_F(WebCryptoImplTest, GenerateKeyHmac) { | 658 TEST_F(WebCryptoImplTest, GenerateKeyHmac) { |
| 659 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 659 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 660 WebKit::WebCryptoAlgorithm algorithm = | 660 blink::WebCryptoAlgorithm algorithm = |
| 661 CreateHmacKeyAlgorithm(WebKit::WebCryptoAlgorithmIdSha1, 128); | 661 CreateHmacKeyAlgorithm(blink::WebCryptoAlgorithmIdSha1, 128); |
| 662 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); | 662 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); |
| 663 EXPECT_FALSE(key.isNull()); | 663 EXPECT_FALSE(key.isNull()); |
| 664 EXPECT_TRUE(key.handle()); | 664 EXPECT_TRUE(key.handle()); |
| 665 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, key.type()); | 665 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 666 } | 666 } |
| 667 | 667 |
| 668 TEST_F(WebCryptoImplTest, GenerateKeyHmacNoLength) { | 668 TEST_F(WebCryptoImplTest, GenerateKeyHmacNoLength) { |
| 669 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 669 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 670 WebKit::WebCryptoAlgorithm algorithm = | 670 blink::WebCryptoAlgorithm algorithm = |
| 671 CreateHmacKeyAlgorithm(WebKit::WebCryptoAlgorithmIdSha1, 0); | 671 CreateHmacKeyAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); |
| 672 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); | 672 ASSERT_TRUE(GenerateKeyInternal(algorithm, &key)); |
| 673 EXPECT_TRUE(key.handle()); | 673 EXPECT_TRUE(key.handle()); |
| 674 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, key.type()); | 674 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 675 } | 675 } |
| 676 | 676 |
| 677 TEST_F(WebCryptoImplTest, ImportSecretKeyNoAlgorithm) { | 677 TEST_F(WebCryptoImplTest, ImportSecretKeyNoAlgorithm) { |
| 678 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 678 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
| 679 | 679 |
| 680 // This fails because the algorithm is null. | 680 // This fails because the algorithm is null. |
| 681 EXPECT_FALSE(ImportKeyInternal( | 681 EXPECT_FALSE(ImportKeyInternal( |
| 682 WebKit::WebCryptoKeyFormatRaw, | 682 blink::WebCryptoKeyFormatRaw, |
| 683 HexStringToBytes("00000000000000000000"), | 683 HexStringToBytes("00000000000000000000"), |
| 684 WebKit::WebCryptoAlgorithm::createNull(), | 684 blink::WebCryptoAlgorithm::createNull(), |
| 685 WebKit::WebCryptoKeyUsageSign, | 685 blink::WebCryptoKeyUsageSign, |
| 686 &key)); | 686 &key)); |
| 687 } | 687 } |
| 688 | 688 |
| 689 #if !defined(USE_OPENSSL) | 689 #if !defined(USE_OPENSSL) |
| 690 | 690 |
| 691 TEST_F(WebCryptoImplTest, GenerateKeyPairRsa) { | 691 TEST_F(WebCryptoImplTest, GenerateKeyPairRsa) { |
| 692 // Note: using unrealistic short key lengths here to avoid bogging down tests. | 692 // Note: using unrealistic short key lengths here to avoid bogging down tests. |
| 693 | 693 |
| 694 // Successful WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key generation. | 694 // Successful WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key generation. |
| 695 const unsigned modulus_length = 256; | 695 const unsigned modulus_length = 256; |
| 696 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); | 696 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); |
| 697 WebKit::WebCryptoAlgorithm algorithm = | 697 blink::WebCryptoAlgorithm algorithm = |
| 698 CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, | 698 CreateRsaAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, |
| 699 modulus_length, | 699 modulus_length, |
| 700 public_exponent); | 700 public_exponent); |
| 701 bool extractable = false; | 701 bool extractable = false; |
| 702 const WebKit::WebCryptoKeyUsageMask usage_mask = 0; | 702 const blink::WebCryptoKeyUsageMask usage_mask = 0; |
| 703 WebKit::WebCryptoKey public_key = WebKit::WebCryptoKey::createNull(); | 703 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
| 704 WebKit::WebCryptoKey private_key = WebKit::WebCryptoKey::createNull(); | 704 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); |
| 705 EXPECT_TRUE(GenerateKeyPairInternal( | 705 EXPECT_TRUE(GenerateKeyPairInternal( |
| 706 algorithm, extractable, usage_mask, &public_key, &private_key)); | 706 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 707 EXPECT_FALSE(public_key.isNull()); | 707 EXPECT_FALSE(public_key.isNull()); |
| 708 EXPECT_FALSE(private_key.isNull()); | 708 EXPECT_FALSE(private_key.isNull()); |
| 709 EXPECT_EQ(WebKit::WebCryptoKeyTypePublic, public_key.type()); | 709 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
| 710 EXPECT_EQ(WebKit::WebCryptoKeyTypePrivate, private_key.type()); | 710 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
| 711 EXPECT_EQ(extractable, public_key.extractable()); | 711 EXPECT_EQ(extractable, public_key.extractable()); |
| 712 EXPECT_EQ(extractable, private_key.extractable()); | 712 EXPECT_EQ(extractable, private_key.extractable()); |
| 713 EXPECT_EQ(usage_mask, public_key.usages()); | 713 EXPECT_EQ(usage_mask, public_key.usages()); |
| 714 EXPECT_EQ(usage_mask, private_key.usages()); | 714 EXPECT_EQ(usage_mask, private_key.usages()); |
| 715 | 715 |
| 716 // Fail with bad modulus. | 716 // Fail with bad modulus. |
| 717 algorithm = CreateRsaAlgorithm( | 717 algorithm = CreateRsaAlgorithm( |
| 718 WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 0, public_exponent); | 718 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 0, public_exponent); |
| 719 EXPECT_FALSE(GenerateKeyPairInternal( | 719 EXPECT_FALSE(GenerateKeyPairInternal( |
| 720 algorithm, extractable, usage_mask, &public_key, &private_key)); | 720 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 721 | 721 |
| 722 // Fail with bad exponent: larger than unsigned long. | 722 // Fail with bad exponent: larger than unsigned long. |
| 723 unsigned exponent_length = sizeof(unsigned long) + 1; // NOLINT | 723 unsigned exponent_length = sizeof(unsigned long) + 1; // NOLINT |
| 724 const std::vector<uint8> long_exponent(exponent_length, 0x01); | 724 const std::vector<uint8> long_exponent(exponent_length, 0x01); |
| 725 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, | 725 algorithm = CreateRsaAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, |
| 726 modulus_length, | 726 modulus_length, |
| 727 long_exponent); | 727 long_exponent); |
| 728 EXPECT_FALSE(GenerateKeyPairInternal( | 728 EXPECT_FALSE(GenerateKeyPairInternal( |
| 729 algorithm, extractable, usage_mask, &public_key, &private_key)); | 729 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 730 | 730 |
| 731 // Fail with bad exponent: empty. | 731 // Fail with bad exponent: empty. |
| 732 const std::vector<uint8> empty_exponent; | 732 const std::vector<uint8> empty_exponent; |
| 733 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, | 733 algorithm = CreateRsaAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, |
| 734 modulus_length, | 734 modulus_length, |
| 735 empty_exponent); | 735 empty_exponent); |
| 736 EXPECT_FALSE(GenerateKeyPairInternal( | 736 EXPECT_FALSE(GenerateKeyPairInternal( |
| 737 algorithm, extractable, usage_mask, &public_key, &private_key)); | 737 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 738 | 738 |
| 739 // Fail with bad exponent: all zeros. | 739 // Fail with bad exponent: all zeros. |
| 740 std::vector<uint8> exponent_with_leading_zeros(15, 0x00); | 740 std::vector<uint8> exponent_with_leading_zeros(15, 0x00); |
| 741 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, | 741 algorithm = CreateRsaAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, |
| 742 modulus_length, | 742 modulus_length, |
| 743 exponent_with_leading_zeros); | 743 exponent_with_leading_zeros); |
| 744 EXPECT_FALSE(GenerateKeyPairInternal( | 744 EXPECT_FALSE(GenerateKeyPairInternal( |
| 745 algorithm, extractable, usage_mask, &public_key, &private_key)); | 745 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 746 | 746 |
| 747 // Key generation success using exponent with leading zeros. | 747 // Key generation success using exponent with leading zeros. |
| 748 exponent_with_leading_zeros.insert(exponent_with_leading_zeros.end(), | 748 exponent_with_leading_zeros.insert(exponent_with_leading_zeros.end(), |
| 749 public_exponent.begin(), | 749 public_exponent.begin(), |
| 750 public_exponent.end()); | 750 public_exponent.end()); |
| 751 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, | 751 algorithm = CreateRsaAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, |
| 752 modulus_length, | 752 modulus_length, |
| 753 exponent_with_leading_zeros); | 753 exponent_with_leading_zeros); |
| 754 EXPECT_TRUE(GenerateKeyPairInternal( | 754 EXPECT_TRUE(GenerateKeyPairInternal( |
| 755 algorithm, extractable, usage_mask, &public_key, &private_key)); | 755 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 756 EXPECT_FALSE(public_key.isNull()); | 756 EXPECT_FALSE(public_key.isNull()); |
| 757 EXPECT_FALSE(private_key.isNull()); | 757 EXPECT_FALSE(private_key.isNull()); |
| 758 EXPECT_EQ(WebKit::WebCryptoKeyTypePublic, public_key.type()); | 758 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
| 759 EXPECT_EQ(WebKit::WebCryptoKeyTypePrivate, private_key.type()); | 759 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
| 760 EXPECT_EQ(extractable, public_key.extractable()); | 760 EXPECT_EQ(extractable, public_key.extractable()); |
| 761 EXPECT_EQ(extractable, private_key.extractable()); | 761 EXPECT_EQ(extractable, private_key.extractable()); |
| 762 EXPECT_EQ(usage_mask, public_key.usages()); | 762 EXPECT_EQ(usage_mask, public_key.usages()); |
| 763 EXPECT_EQ(usage_mask, private_key.usages()); | 763 EXPECT_EQ(usage_mask, private_key.usages()); |
| 764 | 764 |
| 765 // Successful WebCryptoAlgorithmIdRsaOaep key generation. | 765 // Successful WebCryptoAlgorithmIdRsaOaep key generation. |
| 766 algorithm = CreateRsaAlgorithm( | 766 algorithm = CreateRsaAlgorithm( |
| 767 WebKit::WebCryptoAlgorithmIdRsaOaep, modulus_length, public_exponent); | 767 blink::WebCryptoAlgorithmIdRsaOaep, modulus_length, public_exponent); |
| 768 EXPECT_TRUE(GenerateKeyPairInternal( | 768 EXPECT_TRUE(GenerateKeyPairInternal( |
| 769 algorithm, extractable, usage_mask, &public_key, &private_key)); | 769 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 770 EXPECT_FALSE(public_key.isNull()); | 770 EXPECT_FALSE(public_key.isNull()); |
| 771 EXPECT_FALSE(private_key.isNull()); | 771 EXPECT_FALSE(private_key.isNull()); |
| 772 EXPECT_EQ(WebKit::WebCryptoKeyTypePublic, public_key.type()); | 772 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
| 773 EXPECT_EQ(WebKit::WebCryptoKeyTypePrivate, private_key.type()); | 773 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
| 774 EXPECT_EQ(extractable, public_key.extractable()); | 774 EXPECT_EQ(extractable, public_key.extractable()); |
| 775 EXPECT_EQ(extractable, private_key.extractable()); | 775 EXPECT_EQ(extractable, private_key.extractable()); |
| 776 EXPECT_EQ(usage_mask, public_key.usages()); | 776 EXPECT_EQ(usage_mask, public_key.usages()); |
| 777 EXPECT_EQ(usage_mask, private_key.usages()); | 777 EXPECT_EQ(usage_mask, private_key.usages()); |
| 778 | 778 |
| 779 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation. | 779 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation. |
| 780 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 780 algorithm = CreateRsaAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, |
| 781 modulus_length, | 781 modulus_length, |
| 782 public_exponent); | 782 public_exponent); |
| 783 EXPECT_TRUE(GenerateKeyPairInternal( | 783 EXPECT_TRUE(GenerateKeyPairInternal( |
| 784 algorithm, extractable, usage_mask, &public_key, &private_key)); | 784 algorithm, extractable, usage_mask, &public_key, &private_key)); |
| 785 EXPECT_FALSE(public_key.isNull()); | 785 EXPECT_FALSE(public_key.isNull()); |
| 786 EXPECT_FALSE(private_key.isNull()); | 786 EXPECT_FALSE(private_key.isNull()); |
| 787 EXPECT_EQ(WebKit::WebCryptoKeyTypePublic, public_key.type()); | 787 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); |
| 788 EXPECT_EQ(WebKit::WebCryptoKeyTypePrivate, private_key.type()); | 788 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); |
| 789 EXPECT_EQ(extractable, public_key.extractable()); | 789 EXPECT_EQ(extractable, public_key.extractable()); |
| 790 EXPECT_EQ(extractable, private_key.extractable()); | 790 EXPECT_EQ(extractable, private_key.extractable()); |
| 791 EXPECT_EQ(usage_mask, public_key.usages()); | 791 EXPECT_EQ(usage_mask, public_key.usages()); |
| 792 EXPECT_EQ(usage_mask, private_key.usages()); | 792 EXPECT_EQ(usage_mask, private_key.usages()); |
| 793 } | 793 } |
| 794 | 794 |
| 795 #endif // #if !defined(USE_OPENSSL) | 795 #endif // #if !defined(USE_OPENSSL) |
| 796 | 796 |
| 797 } // namespace content | 797 } // namespace content |
| OLD | NEW |