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

Side by Side Diff: components/webcrypto/algorithms/hmac.cc

Issue 1355923002: [refactor] Misc post-NSS WebCrypto cleanups. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@util_split
Patch Set: add an explicit size_t --> unsigned cast Created 5 years, 2 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
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 <openssl/hmac.h> 5 #include <openssl/hmac.h>
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h" 8 #include "base/numerics/safe_math.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "components/webcrypto/algorithm_implementation.h" 10 #include "components/webcrypto/algorithm_implementation.h"
11 #include "components/webcrypto/algorithms/secret_key_util.h" 11 #include "components/webcrypto/algorithms/secret_key_util.h"
12 #include "components/webcrypto/algorithms/util_openssl.h" 12 #include "components/webcrypto/algorithms/util_openssl.h"
13 #include "components/webcrypto/crypto_data.h" 13 #include "components/webcrypto/crypto_data.h"
14 #include "components/webcrypto/jwk.h" 14 #include "components/webcrypto/jwk.h"
15 #include "components/webcrypto/key.h" 15 #include "components/webcrypto/key.h"
16 #include "components/webcrypto/status.h" 16 #include "components/webcrypto/status.h"
17 #include "components/webcrypto/webcrypto_util.h" 17 #include "components/webcrypto/webcrypto_util.h"
18 #include "crypto/openssl_util.h" 18 #include "crypto/openssl_util.h"
19 #include "crypto/secure_util.h" 19 #include "crypto/secure_util.h"
20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" 20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
21 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" 21 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
22 22
23 namespace webcrypto { 23 namespace webcrypto {
24 24
25 namespace { 25 namespace {
26 26
27 // TODO(eroman): Use EVP_MD_block_size() instead. 27 Status GetDigestBlockSizeBits(const blink::WebCryptoAlgorithm& algorithm,
28 Status GetShaBlockSizeBits(const blink::WebCryptoAlgorithm& algorithm, 28 unsigned int* block_size_bits) {
29 unsigned int* block_size_bits) { 29 const EVP_MD* md = GetDigest(algorithm.id());
30 switch (algorithm.id()) { 30 if (!md)
31 case blink::WebCryptoAlgorithmIdSha1: 31 return Status::ErrorUnsupported();
32 case blink::WebCryptoAlgorithmIdSha256: 32 *block_size_bits = static_cast<unsigned int>(8 * EVP_MD_block_size(md));
33 *block_size_bits = 512; 33 return Status::Success();
34 return Status::Success();
35 case blink::WebCryptoAlgorithmIdSha384:
36 case blink::WebCryptoAlgorithmIdSha512:
37 *block_size_bits = 1024;
38 return Status::Success();
39 default:
40 return Status::ErrorUnsupported();
41 }
42 } 34 }
43 35
44 // Gets the requested key length in bits for an HMAC import operation. 36 // Gets the requested key length in bits for an HMAC import operation.
45 Status GetHmacImportKeyLengthBits( 37 Status GetHmacImportKeyLengthBits(
46 const blink::WebCryptoHmacImportParams* params, 38 const blink::WebCryptoHmacImportParams* params,
47 unsigned int key_data_byte_length, 39 unsigned int key_data_byte_length,
48 unsigned int* keylen_bits) { 40 unsigned int* keylen_bits) {
49 if (key_data_byte_length == 0) 41 if (key_data_byte_length == 0)
50 return Status::ErrorHmacImportEmptyKey(); 42 return Status::ErrorHmacImportEmptyKey();
51 43
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 const blink::WebCryptoHmacKeyGenParams* params = 123 const blink::WebCryptoHmacKeyGenParams* params =
132 algorithm.hmacKeyGenParams(); 124 algorithm.hmacKeyGenParams();
133 125
134 unsigned int keylen_bits = 0; 126 unsigned int keylen_bits = 0;
135 if (params->hasLengthBits()) { 127 if (params->hasLengthBits()) {
136 keylen_bits = params->optionalLengthBits(); 128 keylen_bits = params->optionalLengthBits();
137 // Zero-length HMAC keys are disallowed by the spec. 129 // Zero-length HMAC keys are disallowed by the spec.
138 if (keylen_bits == 0) 130 if (keylen_bits == 0)
139 return Status::ErrorGenerateHmacKeyLengthZero(); 131 return Status::ErrorGenerateHmacKeyLengthZero();
140 } else { 132 } else {
141 status = GetShaBlockSizeBits(params->hash(), &keylen_bits); 133 status = GetDigestBlockSizeBits(params->hash(), &keylen_bits);
142 if (status.IsError()) 134 if (status.IsError())
143 return status; 135 return status;
144 } 136 }
145 137
146 return GenerateWebCryptoSecretKey(blink::WebCryptoKeyAlgorithm::createHmac( 138 return GenerateWebCryptoSecretKey(blink::WebCryptoKeyAlgorithm::createHmac(
147 params->hash().id(), keylen_bits), 139 params->hash().id(), keylen_bits),
148 extractable, usages, keylen_bits, result); 140 extractable, usages, keylen_bits, result);
149 } 141 }
150 142
151 Status VerifyKeyUsagesBeforeImportKey( 143 Status VerifyKeyUsagesBeforeImportKey(
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 key_length_algorithm.hmacImportParams(); 276 key_length_algorithm.hmacImportParams();
285 277
286 *has_length_bits = true; 278 *has_length_bits = true;
287 if (params->hasLengthBits()) { 279 if (params->hasLengthBits()) {
288 *length_bits = params->optionalLengthBits(); 280 *length_bits = params->optionalLengthBits();
289 if (*length_bits == 0) 281 if (*length_bits == 0)
290 return Status::ErrorGetHmacKeyLengthZero(); 282 return Status::ErrorGetHmacKeyLengthZero();
291 return Status::Success(); 283 return Status::Success();
292 } 284 }
293 285
294 return GetShaBlockSizeBits(params->hash(), length_bits); 286 return GetDigestBlockSizeBits(params->hash(), length_bits);
295 } 287 }
296 }; 288 };
297 289
298 } // namespace 290 } // namespace
299 291
300 scoped_ptr<AlgorithmImplementation> CreateHmacImplementation() { 292 scoped_ptr<AlgorithmImplementation> CreateHmacImplementation() {
301 return make_scoped_ptr(new HmacImplementation); 293 return make_scoped_ptr(new HmacImplementation);
302 } 294 }
303 295
304 } // namespace webcrypto 296 } // namespace webcrypto
OLDNEW
« no previous file with comments | « components/webcrypto/algorithms/ecdsa_unittest.cc ('k') | components/webcrypto/algorithms/rsa_pss_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698