| 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 <cryptohi.h> | 5 #include <cryptohi.h> |
| 6 #include <pk11pub.h> | 6 #include <pk11pub.h> |
| 7 #include <secerr.h> | 7 #include <secerr.h> |
| 8 #include <sechash.h> | 8 #include <sechash.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/numerics/safe_math.h" |
| 11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 12 #include "content/child/webcrypto/algorithm_implementation.h" | 13 #include "content/child/webcrypto/algorithm_implementation.h" |
| 13 #include "content/child/webcrypto/crypto_data.h" | 14 #include "content/child/webcrypto/crypto_data.h" |
| 14 #include "content/child/webcrypto/jwk.h" | 15 #include "content/child/webcrypto/jwk.h" |
| 15 #include "content/child/webcrypto/nss/key_nss.h" | 16 #include "content/child/webcrypto/nss/key_nss.h" |
| 16 #include "content/child/webcrypto/nss/sym_key_nss.h" | 17 #include "content/child/webcrypto/nss/sym_key_nss.h" |
| 17 #include "content/child/webcrypto/nss/util_nss.h" | 18 #include "content/child/webcrypto/nss/util_nss.h" |
| 18 #include "content/child/webcrypto/status.h" | 19 #include "content/child/webcrypto/status.h" |
| 19 #include "content/child/webcrypto/webcrypto_util.h" | 20 #include "content/child/webcrypto/webcrypto_util.h" |
| 20 #include "crypto/secure_util.h" | 21 #include "crypto/secure_util.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 bool extractable, | 103 bool extractable, |
| 103 blink::WebCryptoKeyUsageMask usage_mask, | 104 blink::WebCryptoKeyUsageMask usage_mask, |
| 104 blink::WebCryptoKey* key) const OVERRIDE { | 105 blink::WebCryptoKey* key) const OVERRIDE { |
| 105 const blink::WebCryptoAlgorithm& hash = | 106 const blink::WebCryptoAlgorithm& hash = |
| 106 algorithm.hmacImportParams()->hash(); | 107 algorithm.hmacImportParams()->hash(); |
| 107 | 108 |
| 108 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; | 109 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; |
| 109 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) | 110 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) |
| 110 return Status::ErrorUnsupported(); | 111 return Status::ErrorUnsupported(); |
| 111 | 112 |
| 112 // TODO(eroman): check for overflow. | 113 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); |
| 113 unsigned int keylen_bits = key_data.byte_length() * 8; | 114 keylen_bits *= 8; |
| 114 return ImportKeyRawNss( | 115 |
| 115 key_data, | 116 if (!keylen_bits.IsValid()) |
| 116 blink::WebCryptoKeyAlgorithm::createHmac(hash.id(), keylen_bits), | 117 return Status::ErrorDataTooLarge(); |
| 117 extractable, | 118 |
| 118 usage_mask, | 119 return ImportKeyRawNss(key_data, |
| 119 mechanism, | 120 blink::WebCryptoKeyAlgorithm::createHmac( |
| 120 CKF_SIGN | CKF_VERIFY, | 121 hash.id(), keylen_bits.ValueOrDie()), |
| 121 key); | 122 extractable, |
| 123 usage_mask, |
| 124 mechanism, |
| 125 CKF_SIGN | CKF_VERIFY, |
| 126 key); |
| 122 } | 127 } |
| 123 | 128 |
| 124 virtual Status ImportKeyJwk(const CryptoData& key_data, | 129 virtual Status ImportKeyJwk(const CryptoData& key_data, |
| 125 const blink::WebCryptoAlgorithm& algorithm, | 130 const blink::WebCryptoAlgorithm& algorithm, |
| 126 bool extractable, | 131 bool extractable, |
| 127 blink::WebCryptoKeyUsageMask usage_mask, | 132 blink::WebCryptoKeyUsageMask usage_mask, |
| 128 blink::WebCryptoKey* key) const OVERRIDE { | 133 blink::WebCryptoKey* key) const OVERRIDE { |
| 129 const char* algorithm_name = | 134 const char* algorithm_name = |
| 130 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); | 135 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); |
| 131 if (!algorithm_name) | 136 if (!algorithm_name) |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 | 232 |
| 228 } // namespace | 233 } // namespace |
| 229 | 234 |
| 230 AlgorithmImplementation* CreatePlatformHmacImplementation() { | 235 AlgorithmImplementation* CreatePlatformHmacImplementation() { |
| 231 return new HmacImplementation; | 236 return new HmacImplementation; |
| 232 } | 237 } |
| 233 | 238 |
| 234 } // namespace webcrypto | 239 } // namespace webcrypto |
| 235 | 240 |
| 236 } // namespace content | 241 } // namespace content |
| OLD | NEW |