| 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 <vector> | |
| 6 #include <openssl/evp.h> | |
| 7 | |
| 8 #include "base/logging.h" | 5 #include "base/logging.h" |
| 9 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
| 7 #include "content/child/webcrypto/algorithm_dispatch.h" |
| 10 #include "content/child/webcrypto/crypto_data.h" | 8 #include "content/child/webcrypto/crypto_data.h" |
| 11 #include "content/child/webcrypto/openssl/aes_key_openssl.h" | |
| 12 #include "content/child/webcrypto/openssl/key_openssl.h" | |
| 13 #include "content/child/webcrypto/openssl/util_openssl.h" | |
| 14 #include "content/child/webcrypto/status.h" | 9 #include "content/child/webcrypto/status.h" |
| 10 #include "content/child/webcrypto/test/test_helpers.h" |
| 15 #include "content/child/webcrypto/webcrypto_util.h" | 11 #include "content/child/webcrypto/webcrypto_util.h" |
| 16 #include "crypto/openssl_util.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "crypto/scoped_openssl_types.h" | |
| 18 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 13 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 14 #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
| 15 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 19 | 16 |
| 20 namespace content { | 17 namespace content { |
| 21 | 18 |
| 22 namespace webcrypto { | 19 namespace webcrypto { |
| 23 | 20 |
| 24 namespace { | 21 namespace { |
| 25 | 22 |
| 26 const EVP_AEAD* GetAesGcmAlgorithmFromKeySize(unsigned int key_size_bytes) { | 23 TEST(WebCryptoShaTest, DigestSampleSets) { |
| 27 switch (key_size_bytes) { | 24 scoped_ptr<base::ListValue> tests; |
| 28 case 16: | 25 // TODO(eroman): rename to sha.json |
| 29 return EVP_aead_aes_128_gcm(); | 26 ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests)); |
| 30 case 32: | 27 |
| 31 return EVP_aead_aes_256_gcm(); | 28 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
| 32 default: | 29 SCOPED_TRACE(test_index); |
| 33 return NULL; | 30 base::DictionaryValue* test; |
| 31 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
| 32 |
| 33 blink::WebCryptoAlgorithm test_algorithm = |
| 34 GetDigestAlgorithm(test, "algorithm"); |
| 35 std::vector<uint8_t> test_input = GetBytesFromHexString(test, "input"); |
| 36 std::vector<uint8_t> test_output = GetBytesFromHexString(test, "output"); |
| 37 |
| 38 std::vector<uint8_t> output; |
| 39 ASSERT_EQ(Status::Success(), |
| 40 Digest(test_algorithm, CryptoData(test_input), &output)); |
| 41 EXPECT_BYTES_EQ(test_output, output); |
| 34 } | 42 } |
| 35 } | 43 } |
| 36 | 44 |
| 37 Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode, | 45 TEST(WebCryptoShaTest, DigestSampleSetsInChunks) { |
| 38 const blink::WebCryptoAlgorithm& algorithm, | 46 scoped_ptr<base::ListValue> tests; |
| 39 const blink::WebCryptoKey& key, | 47 ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests)); |
| 40 const CryptoData& data, | |
| 41 std::vector<uint8_t>* buffer) { | |
| 42 const std::vector<uint8_t>& raw_key = | |
| 43 SymKeyOpenSsl::Cast(key)->raw_key_data(); | |
| 44 const blink::WebCryptoAesGcmParams* params = algorithm.aesGcmParams(); | |
| 45 | 48 |
| 46 unsigned int tag_length_bits; | 49 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
| 47 Status status = GetAesGcmTagLengthInBits(params, &tag_length_bits); | 50 SCOPED_TRACE(test_index); |
| 48 if (status.IsError()) | 51 base::DictionaryValue* test; |
| 49 return status; | 52 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
| 50 | 53 |
| 51 return AeadEncryptDecrypt(mode, | 54 blink::WebCryptoAlgorithm test_algorithm = |
| 52 raw_key, | 55 GetDigestAlgorithm(test, "algorithm"); |
| 53 data, | 56 std::vector<uint8_t> test_input = GetBytesFromHexString(test, "input"); |
| 54 tag_length_bits / 8, | 57 std::vector<uint8_t> test_output = GetBytesFromHexString(test, "output"); |
| 55 CryptoData(params->iv()), | 58 |
| 56 CryptoData(params->optionalAdditionalData()), | 59 // Test the chunk version of the digest functions. Test with 129 byte chunks |
| 57 GetAesGcmAlgorithmFromKeySize(raw_key.size()), | 60 // because the SHA-512 chunk size is 128 bytes. |
| 58 buffer); | 61 unsigned char* output; |
| 62 unsigned int output_length; |
| 63 static const size_t kChunkSizeBytes = 129; |
| 64 size_t length = test_input.size(); |
| 65 scoped_ptr<blink::WebCryptoDigestor> digestor( |
| 66 CreateDigestor(test_algorithm.id())); |
| 67 std::vector<uint8_t>::iterator begin = test_input.begin(); |
| 68 size_t chunk_index = 0; |
| 69 while (begin != test_input.end()) { |
| 70 size_t chunk_length = std::min(kChunkSizeBytes, length - chunk_index); |
| 71 std::vector<uint8_t> chunk(begin, begin + chunk_length); |
| 72 ASSERT_TRUE(chunk.size() > 0); |
| 73 EXPECT_TRUE(digestor->consume(&chunk.front(), chunk.size())); |
| 74 chunk_index = chunk_index + chunk_length; |
| 75 begin = begin + chunk_length; |
| 76 } |
| 77 EXPECT_TRUE(digestor->finish(output, output_length)); |
| 78 EXPECT_BYTES_EQ(test_output, CryptoData(output, output_length)); |
| 79 } |
| 59 } | 80 } |
| 60 | 81 |
| 61 class AesGcmImplementation : public AesAlgorithm { | |
| 62 public: | |
| 63 AesGcmImplementation() : AesAlgorithm("GCM") {} | |
| 64 | |
| 65 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | |
| 66 const blink::WebCryptoKey& key, | |
| 67 const CryptoData& data, | |
| 68 std::vector<uint8_t>* buffer) const OVERRIDE { | |
| 69 return AesGcmEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer); | |
| 70 } | |
| 71 | |
| 72 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, | |
| 73 const blink::WebCryptoKey& key, | |
| 74 const CryptoData& data, | |
| 75 std::vector<uint8_t>* buffer) const OVERRIDE { | |
| 76 return AesGcmEncryptDecrypt(DECRYPT, algorithm, key, data, buffer); | |
| 77 } | |
| 78 }; | |
| 79 | |
| 80 } // namespace | 82 } // namespace |
| 81 | 83 |
| 82 AlgorithmImplementation* CreatePlatformAesGcmImplementation() { | |
| 83 return new AesGcmImplementation; | |
| 84 } | |
| 85 | |
| 86 } // namespace webcrypto | 84 } // namespace webcrypto |
| 87 | 85 |
| 88 } // namespace content | 86 } // namespace content |
| OLD | NEW |