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 <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 "content/child/webcrypto/algorithm_implementation.h" | 10 #include "content/child/webcrypto/algorithm_implementation.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 if (!success || hmac_actual_length != hmac_expected_length) | 63 if (!success || hmac_actual_length != hmac_expected_length) |
64 return Status::OperationError(); | 64 return Status::OperationError(); |
65 | 65 |
66 return Status::Success(); | 66 return Status::Success(); |
67 } | 67 } |
68 | 68 |
69 class HmacImplementation : public AlgorithmImplementation { | 69 class HmacImplementation : public AlgorithmImplementation { |
70 public: | 70 public: |
71 HmacImplementation() {} | 71 HmacImplementation() {} |
72 | 72 |
73 virtual Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, | 73 virtual Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, |
74 bool extractable, | 74 bool extractable, |
75 blink::WebCryptoKeyUsageMask usage_mask, | 75 blink::WebCryptoKeyUsageMask usage_mask, |
76 blink::WebCryptoKey* key) const OVERRIDE { | 76 blink::WebCryptoKey*, |
| 77 blink::WebCryptoKey* key) const OVERRIDE { |
| 78 Status status = CheckKeyCreationUsages(kAllKeyUsages, usage_mask); |
| 79 if (status.IsError()) |
| 80 return status; |
| 81 |
77 const blink::WebCryptoHmacKeyGenParams* params = | 82 const blink::WebCryptoHmacKeyGenParams* params = |
78 algorithm.hmacKeyGenParams(); | 83 algorithm.hmacKeyGenParams(); |
79 | 84 |
80 unsigned int keylen_bits = 0; | 85 unsigned int keylen_bits = 0; |
81 Status status = GetHmacKeyGenLengthInBits(params, &keylen_bits); | 86 status = GetHmacKeyGenLengthInBits(params, &keylen_bits); |
82 if (status.IsError()) | 87 if (status.IsError()) |
83 return status; | 88 return status; |
84 | 89 |
85 return GenerateSecretKeyOpenSsl(blink::WebCryptoKeyAlgorithm::createHmac( | 90 return GenerateSecretKeyOpenSsl(blink::WebCryptoKeyAlgorithm::createHmac( |
86 params->hash().id(), keylen_bits), | 91 params->hash().id(), keylen_bits), |
87 extractable, | 92 extractable, |
88 usage_mask, | 93 usage_mask, |
89 keylen_bits / 8, | 94 keylen_bits / 8, |
90 key); | 95 key); |
91 } | 96 } |
92 | 97 |
93 virtual Status VerifyKeyUsagesBeforeImportKey( | 98 virtual Status VerifyKeyUsagesBeforeImportKey( |
94 blink::WebCryptoKeyFormat format, | 99 blink::WebCryptoKeyFormat format, |
95 blink::WebCryptoKeyUsageMask usage_mask) const OVERRIDE { | 100 blink::WebCryptoKeyUsageMask usage_mask) const OVERRIDE { |
96 switch (format) { | 101 switch (format) { |
97 case blink::WebCryptoKeyFormatRaw: | 102 case blink::WebCryptoKeyFormatRaw: |
98 case blink::WebCryptoKeyFormatJwk: | 103 case blink::WebCryptoKeyFormatJwk: |
99 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); | 104 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); |
100 default: | 105 default: |
101 return Status::ErrorUnsupportedImportKeyFormat(); | 106 return Status::ErrorUnsupportedImportKeyFormat(); |
102 } | 107 } |
103 } | 108 } |
104 | 109 |
105 virtual Status VerifyKeyUsagesBeforeGenerateKey( | |
106 blink::WebCryptoKeyUsageMask usage_mask) const OVERRIDE { | |
107 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); | |
108 } | |
109 | |
110 virtual Status ImportKeyRaw(const CryptoData& key_data, | 110 virtual Status ImportKeyRaw(const CryptoData& key_data, |
111 const blink::WebCryptoAlgorithm& algorithm, | 111 const blink::WebCryptoAlgorithm& algorithm, |
112 bool extractable, | 112 bool extractable, |
113 blink::WebCryptoKeyUsageMask usage_mask, | 113 blink::WebCryptoKeyUsageMask usage_mask, |
114 blink::WebCryptoKey* key) const OVERRIDE { | 114 blink::WebCryptoKey* key) const OVERRIDE { |
115 const blink::WebCryptoAlgorithm& hash = | 115 const blink::WebCryptoAlgorithm& hash = |
116 algorithm.hmacImportParams()->hash(); | 116 algorithm.hmacImportParams()->hash(); |
117 | 117 |
118 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); | 118 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); |
119 keylen_bits *= 8; | 119 keylen_bits *= 8; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 208 |
209 } // namespace | 209 } // namespace |
210 | 210 |
211 AlgorithmImplementation* CreatePlatformHmacImplementation() { | 211 AlgorithmImplementation* CreatePlatformHmacImplementation() { |
212 return new HmacImplementation; | 212 return new HmacImplementation; |
213 } | 213 } |
214 | 214 |
215 } // namespace webcrypto | 215 } // namespace webcrypto |
216 | 216 |
217 } // namespace content | 217 } // namespace content |
OLD | NEW |