Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: content/child/webcrypto/shared_crypto_unittest.cc

Issue 410743002: [refactor] Replace custom utility function with base's "vector_as_array()" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase onto master Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <algorithm> 5 #include <algorithm>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/json/json_reader.h" 11 #include "base/json/json_reader.h"
12 #include "base/json/json_writer.h" 12 #include "base/json/json_writer.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/path_service.h" 15 #include "base/path_service.h"
16 #include "base/stl_util.h"
16 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
19 #include "content/child/webcrypto/algorithm_dispatch.h" 20 #include "content/child/webcrypto/algorithm_dispatch.h"
20 #include "content/child/webcrypto/crypto_data.h" 21 #include "content/child/webcrypto/crypto_data.h"
21 #include "content/child/webcrypto/status.h" 22 #include "content/child/webcrypto/status.h"
22 #include "content/child/webcrypto/webcrypto_util.h" 23 #include "content/child/webcrypto/webcrypto_util.h"
23 #include "content/public/common/content_paths.h" 24 #include "content/public/common/content_paths.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" 26 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 unsigned int modulus_length, 153 unsigned int modulus_length,
153 const std::vector<uint8_t>& public_exponent) { 154 const std::vector<uint8_t>& public_exponent) {
154 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || 155 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
155 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); 156 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep);
156 DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id)); 157 DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id));
157 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 158 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
158 algorithm_id, 159 algorithm_id,
159 new blink::WebCryptoRsaHashedKeyGenParams( 160 new blink::WebCryptoRsaHashedKeyGenParams(
160 CreateAlgorithm(hash_id), 161 CreateAlgorithm(hash_id),
161 modulus_length, 162 modulus_length,
162 webcrypto::Uint8VectorStart(public_exponent), 163 vector_as_array(&public_exponent),
163 public_exponent.size())); 164 public_exponent.size()));
164 } 165 }
165 166
166 // Creates an RSA-OAEP algorithm 167 // Creates an RSA-OAEP algorithm
167 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm( 168 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm(
168 const std::vector<uint8_t>& label) { 169 const std::vector<uint8_t>& label) {
169 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 170 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
170 blink::WebCryptoAlgorithmIdRsaOaep, 171 blink::WebCryptoAlgorithmIdRsaOaep,
171 new blink::WebCryptoRsaOaepParams( 172 new blink::WebCryptoRsaOaepParams(
172 !label.empty(), Uint8VectorStart(label), label.size())); 173 !label.empty(), vector_as_array(&label), label.size()));
173 } 174 }
174 175
175 // Creates an AES-CBC algorithm. 176 // Creates an AES-CBC algorithm.
176 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm( 177 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(
177 const std::vector<uint8_t>& iv) { 178 const std::vector<uint8_t>& iv) {
178 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 179 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
179 blink::WebCryptoAlgorithmIdAesCbc, 180 blink::WebCryptoAlgorithmIdAesCbc,
180 new blink::WebCryptoAesCbcParams(Uint8VectorStart(iv), iv.size())); 181 new blink::WebCryptoAesCbcParams(vector_as_array(&iv), iv.size()));
181 } 182 }
182 183
183 // Creates an AES-GCM algorithm. 184 // Creates an AES-GCM algorithm.
184 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm( 185 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm(
185 const std::vector<uint8_t>& iv, 186 const std::vector<uint8_t>& iv,
186 const std::vector<uint8_t>& additional_data, 187 const std::vector<uint8_t>& additional_data,
187 unsigned int tag_length_bits) { 188 unsigned int tag_length_bits) {
188 EXPECT_TRUE(SupportsAesGcm()); 189 EXPECT_TRUE(SupportsAesGcm());
189 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 190 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
190 blink::WebCryptoAlgorithmIdAesGcm, 191 blink::WebCryptoAlgorithmIdAesGcm,
191 new blink::WebCryptoAesGcmParams(Uint8VectorStart(iv), 192 new blink::WebCryptoAesGcmParams(vector_as_array(&iv),
192 iv.size(), 193 iv.size(),
193 true, 194 true,
194 Uint8VectorStart(additional_data), 195 vector_as_array(&additional_data),
195 additional_data.size(), 196 additional_data.size(),
196 true, 197 true,
197 tag_length_bits)); 198 tag_length_bits));
198 } 199 }
199 200
200 // Creates an HMAC algorithm whose parameters struct is compatible with key 201 // Creates an HMAC algorithm whose parameters struct is compatible with key
201 // generation. It is an error to call this with a hash_id that is not a SHA*. 202 // generation. It is an error to call this with a hash_id that is not a SHA*.
202 // The key_length_bits parameter is optional, with zero meaning unspecified. 203 // The key_length_bits parameter is optional, with zero meaning unspecified.
203 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( 204 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm(
204 blink::WebCryptoAlgorithmId hash_id, 205 blink::WebCryptoAlgorithmId hash_id,
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 algorithm, 595 algorithm,
595 extractable, 596 extractable,
596 usage_mask, 597 usage_mask,
597 key); 598 key);
598 } 599 }
599 600
600 // Parses a vector of JSON into a dictionary. 601 // Parses a vector of JSON into a dictionary.
601 scoped_ptr<base::DictionaryValue> GetJwkDictionary( 602 scoped_ptr<base::DictionaryValue> GetJwkDictionary(
602 const std::vector<uint8_t>& json) { 603 const std::vector<uint8_t>& json) {
603 base::StringPiece json_string( 604 base::StringPiece json_string(
604 reinterpret_cast<const char*>(Uint8VectorStart(json)), json.size()); 605 reinterpret_cast<const char*>(vector_as_array(&json)), json.size());
605 base::Value* value = base::JSONReader::Read(json_string); 606 base::Value* value = base::JSONReader::Read(json_string);
606 EXPECT_TRUE(value); 607 EXPECT_TRUE(value);
607 base::DictionaryValue* dict_value = NULL; 608 base::DictionaryValue* dict_value = NULL;
608 value->GetAsDictionary(&dict_value); 609 value->GetAsDictionary(&dict_value);
609 return scoped_ptr<base::DictionaryValue>(dict_value); 610 return scoped_ptr<base::DictionaryValue>(dict_value);
610 } 611 }
611 612
612 // Verifies the input dictionary contains the expected values. Exact matches are 613 // Verifies the input dictionary contains the expected values. Exact matches are
613 // required on the fields examined. 614 // required on the fields examined.
614 ::testing::AssertionResult VerifyJwk( 615 ::testing::AssertionResult VerifyJwk(
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
896 key, 897 key,
897 CryptoData(output), 898 CryptoData(output),
898 CryptoData(test_message), 899 CryptoData(test_message),
899 &signature_match)); 900 &signature_match));
900 EXPECT_TRUE(signature_match); 901 EXPECT_TRUE(signature_match);
901 902
902 // Ensure truncated signature does not verify by passing one less byte. 903 // Ensure truncated signature does not verify by passing one less byte.
903 EXPECT_EQ(Status::Success(), 904 EXPECT_EQ(Status::Success(),
904 Verify(algorithm, 905 Verify(algorithm,
905 key, 906 key,
906 CryptoData(Uint8VectorStart(output), output.size() - 1), 907 CryptoData(vector_as_array(&output), output.size() - 1),
907 CryptoData(test_message), 908 CryptoData(test_message),
908 &signature_match)); 909 &signature_match));
909 EXPECT_FALSE(signature_match); 910 EXPECT_FALSE(signature_match);
910 911
911 // Ensure truncated signature does not verify by passing no bytes. 912 // Ensure truncated signature does not verify by passing no bytes.
912 EXPECT_EQ(Status::Success(), 913 EXPECT_EQ(Status::Success(),
913 Verify(algorithm, 914 Verify(algorithm,
914 key, 915 key,
915 CryptoData(), 916 CryptoData(),
916 CryptoData(test_message), 917 CryptoData(test_message),
(...skipping 1515 matching lines...) Expand 10 before | Expand all | Expand 10 after
2432 // Technically it is OK to fail since JWA says that consumer are not required 2433 // Technically it is OK to fail since JWA says that consumer are not required
2433 // to support lack of the optional parameters. 2434 // to support lack of the optional parameters.
2434 ASSERT_EQ(Status::OperationError(), 2435 ASSERT_EQ(Status::OperationError(),
2435 ImportKeyJwkFromDict(dict, 2436 ImportKeyJwkFromDict(dict,
2436 CreateRsaHashedImportAlgorithm( 2437 CreateRsaHashedImportAlgorithm(
2437 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2438 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2438 blink::WebCryptoAlgorithmIdSha1), 2439 blink::WebCryptoAlgorithmIdSha1),
2439 true, 2440 true,
2440 blink::WebCryptoKeyUsageSign, 2441 blink::WebCryptoKeyUsageSign,
2441 &key)); 2442 &key));
2442
2443 } 2443 }
2444 2444
2445 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyPairRsa)) { 2445 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyPairRsa)) {
2446 // Note: using unrealistic short key lengths here to avoid bogging down tests. 2446 // Note: using unrealistic short key lengths here to avoid bogging down tests.
2447 2447
2448 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation (sha256) 2448 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation (sha256)
2449 const unsigned int modulus_length = 256; 2449 const unsigned int modulus_length = 256;
2450 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001"); 2450 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001");
2451 blink::WebCryptoAlgorithm algorithm = 2451 blink::WebCryptoAlgorithm algorithm =
2452 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2452 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
2676 blink::WebCryptoAlgorithm algorithm = CreateRsaHashedKeyGenAlgorithm( 2676 blink::WebCryptoAlgorithm algorithm = CreateRsaHashedKeyGenAlgorithm(
2677 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2677 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2678 blink::WebCryptoAlgorithmIdSha256, 2678 blink::WebCryptoAlgorithmIdSha256,
2679 modulus_length, 2679 modulus_length,
2680 HexStringToBytes(kPublicExponents[i])); 2680 HexStringToBytes(kPublicExponents[i]));
2681 2681
2682 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 2682 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
2683 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 2683 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
2684 2684
2685 EXPECT_EQ(Status::ErrorGenerateKeyPublicExponent(), 2685 EXPECT_EQ(Status::ErrorGenerateKeyPublicExponent(),
2686 GenerateKeyPair( 2686 GenerateKeyPair(algorithm, true, 0, &public_key, &private_key));
2687 algorithm, true, 0, &public_key, &private_key));
2688 } 2687 }
2689 } 2688 }
2690 2689
2691 TEST_F(SharedCryptoTest, RsaSsaSignVerifyFailures) { 2690 TEST_F(SharedCryptoTest, RsaSsaSignVerifyFailures) {
2692 if (!SupportsRsaKeyImport()) 2691 if (!SupportsRsaKeyImport())
2693 return; 2692 return;
2694 2693
2695 // Import a key pair. 2694 // Import a key pair.
2696 blink::WebCryptoAlgorithm import_algorithm = 2695 blink::WebCryptoAlgorithm import_algorithm =
2697 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2696 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
(...skipping 19 matching lines...) Expand all
2717 // Compute a signature. 2716 // Compute a signature.
2718 const std::vector<uint8_t> data = HexStringToBytes("010203040506070809"); 2717 const std::vector<uint8_t> data = HexStringToBytes("010203040506070809");
2719 ASSERT_EQ(Status::Success(), 2718 ASSERT_EQ(Status::Success(),
2720 Sign(algorithm, private_key, CryptoData(data), &signature)); 2719 Sign(algorithm, private_key, CryptoData(data), &signature));
2721 2720
2722 // Ensure truncated signature does not verify by passing one less byte. 2721 // Ensure truncated signature does not verify by passing one less byte.
2723 EXPECT_EQ( 2722 EXPECT_EQ(
2724 Status::Success(), 2723 Status::Success(),
2725 Verify(algorithm, 2724 Verify(algorithm,
2726 public_key, 2725 public_key,
2727 CryptoData(Uint8VectorStart(signature), signature.size() - 1), 2726 CryptoData(vector_as_array(&signature), signature.size() - 1),
2728 CryptoData(data), 2727 CryptoData(data),
2729 &signature_match)); 2728 &signature_match));
2730 EXPECT_FALSE(signature_match); 2729 EXPECT_FALSE(signature_match);
2731 2730
2732 // Ensure truncated signature does not verify by passing no bytes. 2731 // Ensure truncated signature does not verify by passing no bytes.
2733 EXPECT_EQ(Status::Success(), 2732 EXPECT_EQ(Status::Success(),
2734 Verify(algorithm, 2733 Verify(algorithm,
2735 public_key, 2734 public_key,
2736 CryptoData(), 2735 CryptoData(),
2737 CryptoData(data), 2736 CryptoData(data),
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after
3423 std::vector<uint8_t> wrapping_key_data(16, 0); 3422 std::vector<uint8_t> wrapping_key_data(16, 0);
3424 std::vector<uint8_t> wrapped_key = HexStringToBytes( 3423 std::vector<uint8_t> wrapped_key = HexStringToBytes(
3425 "1A07ACAB6C906E50883173C29441DB1DE91D34F45C435B5F99C822867FB3956F"); 3424 "1A07ACAB6C906E50883173C29441DB1DE91D34F45C435B5F99C822867FB3956F");
3426 3425
3427 blink::WebCryptoKey wrapping_key = 3426 blink::WebCryptoKey wrapping_key =
3428 ImportSecretKeyFromRaw(wrapping_key_data, 3427 ImportSecretKeyFromRaw(wrapping_key_data,
3429 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), 3428 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw),
3430 blink::WebCryptoKeyUsageUnwrapKey); 3429 blink::WebCryptoKeyUsageUnwrapKey);
3431 3430
3432 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); 3431 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
3433 ASSERT_EQ(Status::ErrorAes192BitUnsupported(), 3432 ASSERT_EQ(Status::ErrorAes192BitUnsupported(),
3434 UnwrapKey(blink::WebCryptoKeyFormatRaw, 3433 UnwrapKey(blink::WebCryptoKeyFormatRaw,
3435 CryptoData(wrapped_key), 3434 CryptoData(wrapped_key),
3436 wrapping_key, 3435 wrapping_key,
3437 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), 3436 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw),
3438 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 3437 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
3439 true, 3438 true,
3440 blink::WebCryptoKeyUsageEncrypt, 3439 blink::WebCryptoKeyUsageEncrypt,
3441 &unwrapped_key)); 3440 &unwrapped_key));
3442 } 3441 }
3443 3442
3444 class SharedCryptoRsaOaepTest : public ::testing::Test { 3443 class SharedCryptoRsaOaepTest : public ::testing::Test {
3445 public: 3444 public:
3446 scoped_ptr<base::DictionaryValue> CreatePublicKeyJwkDict() { 3445 scoped_ptr<base::DictionaryValue> CreatePublicKeyJwkDict() {
3447 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue()); 3446 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue());
3448 jwk->SetString("kty", "RSA"); 3447 jwk->SetString("kty", "RSA");
3449 jwk->SetString("n", 3448 jwk->SetString("n",
3450 Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyModulusHex))); 3449 Base64EncodeUrlSafe(HexStringToBytes(kPublicKeyModulusHex)));
3451 jwk->SetString( 3450 jwk->SetString(
(...skipping 958 matching lines...) Expand 10 before | Expand all | Expand 10 after
4410 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); 4409 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki);
4411 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); 4410 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8);
4412 4411
4413 EXPECT_NE(public_key_spki, wrapped_public_key); 4412 EXPECT_NE(public_key_spki, wrapped_public_key);
4414 EXPECT_NE(private_key_pkcs8, wrapped_private_key); 4413 EXPECT_NE(private_key_pkcs8, wrapped_private_key);
4415 } 4414 }
4416 4415
4417 } // namespace webcrypto 4416 } // namespace webcrypto
4418 4417
4419 } // namespace content 4418 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webcrypto/openssl/sha_openssl.cc ('k') | content/child/webcrypto/webcrypto_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698