Index: content/child/webcrypto/test/sha_unittest.cc |
diff --git a/content/child/webcrypto/openssl/aes_gcm_openssl.cc b/content/child/webcrypto/test/sha_unittest.cc |
similarity index 17% |
copy from content/child/webcrypto/openssl/aes_gcm_openssl.cc |
copy to content/child/webcrypto/test/sha_unittest.cc |
index a3e1cd31a5591646b06a696aece119664f7a6e97..6019c5bb77b60847e8f3f1e583a87a0c256dc86b 100644 |
--- a/content/child/webcrypto/openssl/aes_gcm_openssl.cc |
+++ b/content/child/webcrypto/test/sha_unittest.cc |
@@ -2,20 +2,17 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include <vector> |
-#include <openssl/evp.h> |
- |
#include "base/logging.h" |
#include "base/stl_util.h" |
+#include "content/child/webcrypto/algorithm_dispatch.h" |
#include "content/child/webcrypto/crypto_data.h" |
-#include "content/child/webcrypto/openssl/aes_key_openssl.h" |
-#include "content/child/webcrypto/openssl/key_openssl.h" |
-#include "content/child/webcrypto/openssl/util_openssl.h" |
#include "content/child/webcrypto/status.h" |
+#include "content/child/webcrypto/test/test_helpers.h" |
#include "content/child/webcrypto/webcrypto_util.h" |
-#include "crypto/openssl_util.h" |
-#include "crypto/scoped_openssl_types.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
#include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
+#include "third_party/WebKit/public/platform/WebCryptoKey.h" |
+#include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
namespace content { |
@@ -23,66 +20,67 @@ namespace webcrypto { |
namespace { |
-const EVP_AEAD* GetAesGcmAlgorithmFromKeySize(unsigned int key_size_bytes) { |
- switch (key_size_bytes) { |
- case 16: |
- return EVP_aead_aes_128_gcm(); |
- case 32: |
- return EVP_aead_aes_256_gcm(); |
- default: |
- return NULL; |
+TEST(WebCryptoShaTest, DigestSampleSets) { |
+ scoped_ptr<base::ListValue> tests; |
+ // TODO(eroman): rename to sha.json |
+ ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests)); |
+ |
+ for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
+ SCOPED_TRACE(test_index); |
+ base::DictionaryValue* test; |
+ ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
+ |
+ blink::WebCryptoAlgorithm test_algorithm = |
+ GetDigestAlgorithm(test, "algorithm"); |
+ std::vector<uint8_t> test_input = GetBytesFromHexString(test, "input"); |
+ std::vector<uint8_t> test_output = GetBytesFromHexString(test, "output"); |
+ |
+ std::vector<uint8_t> output; |
+ ASSERT_EQ(Status::Success(), |
+ Digest(test_algorithm, CryptoData(test_input), &output)); |
+ EXPECT_BYTES_EQ(test_output, output); |
} |
} |
-Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode, |
- const blink::WebCryptoAlgorithm& algorithm, |
- const blink::WebCryptoKey& key, |
- const CryptoData& data, |
- std::vector<uint8_t>* buffer) { |
- const std::vector<uint8_t>& raw_key = |
- SymKeyOpenSsl::Cast(key)->raw_key_data(); |
- const blink::WebCryptoAesGcmParams* params = algorithm.aesGcmParams(); |
- |
- unsigned int tag_length_bits; |
- Status status = GetAesGcmTagLengthInBits(params, &tag_length_bits); |
- if (status.IsError()) |
- return status; |
- |
- return AeadEncryptDecrypt(mode, |
- raw_key, |
- data, |
- tag_length_bits / 8, |
- CryptoData(params->iv()), |
- CryptoData(params->optionalAdditionalData()), |
- GetAesGcmAlgorithmFromKeySize(raw_key.size()), |
- buffer); |
-} |
- |
-class AesGcmImplementation : public AesAlgorithm { |
- public: |
- AesGcmImplementation() : AesAlgorithm("GCM") {} |
- |
- virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, |
- const blink::WebCryptoKey& key, |
- const CryptoData& data, |
- std::vector<uint8_t>* buffer) const OVERRIDE { |
- return AesGcmEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer); |
+TEST(WebCryptoShaTest, DigestSampleSetsInChunks) { |
+ scoped_ptr<base::ListValue> tests; |
+ ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests)); |
+ |
+ for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { |
+ SCOPED_TRACE(test_index); |
+ base::DictionaryValue* test; |
+ ASSERT_TRUE(tests->GetDictionary(test_index, &test)); |
+ |
+ blink::WebCryptoAlgorithm test_algorithm = |
+ GetDigestAlgorithm(test, "algorithm"); |
+ std::vector<uint8_t> test_input = GetBytesFromHexString(test, "input"); |
+ std::vector<uint8_t> test_output = GetBytesFromHexString(test, "output"); |
+ |
+ // Test the chunk version of the digest functions. Test with 129 byte chunks |
+ // because the SHA-512 chunk size is 128 bytes. |
+ unsigned char* output; |
+ unsigned int output_length; |
+ static const size_t kChunkSizeBytes = 129; |
+ size_t length = test_input.size(); |
+ scoped_ptr<blink::WebCryptoDigestor> digestor( |
+ CreateDigestor(test_algorithm.id())); |
+ std::vector<uint8_t>::iterator begin = test_input.begin(); |
+ size_t chunk_index = 0; |
+ while (begin != test_input.end()) { |
+ size_t chunk_length = std::min(kChunkSizeBytes, length - chunk_index); |
+ std::vector<uint8_t> chunk(begin, begin + chunk_length); |
+ ASSERT_TRUE(chunk.size() > 0); |
+ EXPECT_TRUE(digestor->consume(&chunk.front(), chunk.size())); |
+ chunk_index = chunk_index + chunk_length; |
+ begin = begin + chunk_length; |
+ } |
+ EXPECT_TRUE(digestor->finish(output, output_length)); |
+ EXPECT_BYTES_EQ(test_output, CryptoData(output, output_length)); |
} |
- |
- virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, |
- const blink::WebCryptoKey& key, |
- const CryptoData& data, |
- std::vector<uint8_t>* buffer) const OVERRIDE { |
- return AesGcmEncryptDecrypt(DECRYPT, algorithm, key, data, buffer); |
- } |
-}; |
+} |
} // namespace |
-AlgorithmImplementation* CreatePlatformAesGcmImplementation() { |
- return new AesGcmImplementation; |
-} |
- |
} // namespace webcrypto |
} // namespace content |