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 GenerateKeyResult* result) const override { |
| 77 Status status = CheckKeyCreationUsages(kAllKeyUsages, usage_mask); |
| 78 if (status.IsError()) |
| 79 return status; |
| 80 |
77 const blink::WebCryptoHmacKeyGenParams* params = | 81 const blink::WebCryptoHmacKeyGenParams* params = |
78 algorithm.hmacKeyGenParams(); | 82 algorithm.hmacKeyGenParams(); |
79 | 83 |
80 unsigned int keylen_bits = 0; | 84 unsigned int keylen_bits = 0; |
81 Status status = GetHmacKeyGenLengthInBits(params, &keylen_bits); | 85 status = GetHmacKeyGenLengthInBits(params, &keylen_bits); |
82 if (status.IsError()) | 86 if (status.IsError()) |
83 return status; | 87 return status; |
84 | 88 |
85 return GenerateSecretKeyOpenSsl(blink::WebCryptoKeyAlgorithm::createHmac( | 89 return GenerateSecretKeyOpenSsl(blink::WebCryptoKeyAlgorithm::createHmac( |
86 params->hash().id(), keylen_bits), | 90 params->hash().id(), keylen_bits), |
87 extractable, | 91 extractable, |
88 usage_mask, | 92 usage_mask, |
89 keylen_bits / 8, | 93 keylen_bits / 8, |
90 key); | 94 result); |
91 } | 95 } |
92 | 96 |
93 virtual Status VerifyKeyUsagesBeforeImportKey( | 97 virtual Status VerifyKeyUsagesBeforeImportKey( |
94 blink::WebCryptoKeyFormat format, | 98 blink::WebCryptoKeyFormat format, |
95 blink::WebCryptoKeyUsageMask usage_mask) const override { | 99 blink::WebCryptoKeyUsageMask usage_mask) const override { |
96 switch (format) { | 100 switch (format) { |
97 case blink::WebCryptoKeyFormatRaw: | 101 case blink::WebCryptoKeyFormatRaw: |
98 case blink::WebCryptoKeyFormatJwk: | 102 case blink::WebCryptoKeyFormatJwk: |
99 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); | 103 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); |
100 default: | 104 default: |
101 return Status::ErrorUnsupportedImportKeyFormat(); | 105 return Status::ErrorUnsupportedImportKeyFormat(); |
102 } | 106 } |
103 } | 107 } |
104 | 108 |
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, | 109 virtual Status ImportKeyRaw(const CryptoData& key_data, |
111 const blink::WebCryptoAlgorithm& algorithm, | 110 const blink::WebCryptoAlgorithm& algorithm, |
112 bool extractable, | 111 bool extractable, |
113 blink::WebCryptoKeyUsageMask usage_mask, | 112 blink::WebCryptoKeyUsageMask usage_mask, |
114 blink::WebCryptoKey* key) const override { | 113 blink::WebCryptoKey* key) const override { |
115 const blink::WebCryptoAlgorithm& hash = | 114 const blink::WebCryptoAlgorithm& hash = |
116 algorithm.hmacImportParams()->hash(); | 115 algorithm.hmacImportParams()->hash(); |
117 | 116 |
118 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); | 117 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); |
119 keylen_bits *= 8; | 118 keylen_bits *= 8; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 207 |
209 } // namespace | 208 } // namespace |
210 | 209 |
211 AlgorithmImplementation* CreatePlatformHmacImplementation() { | 210 AlgorithmImplementation* CreatePlatformHmacImplementation() { |
212 return new HmacImplementation; | 211 return new HmacImplementation; |
213 } | 212 } |
214 | 213 |
215 } // namespace webcrypto | 214 } // namespace webcrypto |
216 | 215 |
217 } // namespace content | 216 } // namespace content |
OLD | NEW |