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" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 return true; | 48 return true; |
49 default: | 49 default: |
50 return false; | 50 return false; |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 class HmacImplementation : public AlgorithmImplementation { | 54 class HmacImplementation : public AlgorithmImplementation { |
55 public: | 55 public: |
56 HmacImplementation() {} | 56 HmacImplementation() {} |
57 | 57 |
58 virtual Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, | 58 virtual Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, |
59 bool extractable, | 59 bool extractable, |
60 blink::WebCryptoKeyUsageMask usage_mask, | 60 blink::WebCryptoKeyUsageMask usage_mask, |
61 blink::WebCryptoKey* key) const OVERRIDE { | 61 blink::WebCryptoKey*, |
| 62 blink::WebCryptoKey* key) const OVERRIDE { |
| 63 Status status = CheckKeyCreationUsages(kAllKeyUsages, usage_mask); |
| 64 if (status.IsError()) |
| 65 return status; |
| 66 |
62 const blink::WebCryptoHmacKeyGenParams* params = | 67 const blink::WebCryptoHmacKeyGenParams* params = |
63 algorithm.hmacKeyGenParams(); | 68 algorithm.hmacKeyGenParams(); |
64 | 69 |
65 const blink::WebCryptoAlgorithm& hash = params->hash(); | 70 const blink::WebCryptoAlgorithm& hash = params->hash(); |
66 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; | 71 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; |
67 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) | 72 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) |
68 return Status::ErrorUnsupported(); | 73 return Status::ErrorUnsupported(); |
69 | 74 |
70 unsigned int keylen_bits = 0; | 75 unsigned int keylen_bits = 0; |
71 Status status = GetHmacKeyGenLengthInBits(params, &keylen_bits); | 76 status = GetHmacKeyGenLengthInBits(params, &keylen_bits); |
72 if (status.IsError()) | 77 if (status.IsError()) |
73 return status; | 78 return status; |
74 | 79 |
75 return GenerateSecretKeyNss( | 80 return GenerateSecretKeyNss( |
76 blink::WebCryptoKeyAlgorithm::createHmac(hash.id(), keylen_bits), | 81 blink::WebCryptoKeyAlgorithm::createHmac(hash.id(), keylen_bits), |
77 extractable, | 82 extractable, |
78 usage_mask, | 83 usage_mask, |
79 keylen_bits / 8, | 84 keylen_bits / 8, |
80 mechanism, | 85 mechanism, |
81 key); | 86 key); |
82 } | 87 } |
83 | 88 |
84 virtual Status VerifyKeyUsagesBeforeImportKey( | 89 virtual Status VerifyKeyUsagesBeforeImportKey( |
85 blink::WebCryptoKeyFormat format, | 90 blink::WebCryptoKeyFormat format, |
86 blink::WebCryptoKeyUsageMask usage_mask) const OVERRIDE { | 91 blink::WebCryptoKeyUsageMask usage_mask) const OVERRIDE { |
87 switch (format) { | 92 switch (format) { |
88 case blink::WebCryptoKeyFormatRaw: | 93 case blink::WebCryptoKeyFormatRaw: |
89 case blink::WebCryptoKeyFormatJwk: | 94 case blink::WebCryptoKeyFormatJwk: |
90 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); | 95 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); |
91 default: | 96 default: |
92 return Status::ErrorUnsupportedImportKeyFormat(); | 97 return Status::ErrorUnsupportedImportKeyFormat(); |
93 } | 98 } |
94 } | 99 } |
95 | 100 |
96 virtual Status VerifyKeyUsagesBeforeGenerateKey( | |
97 blink::WebCryptoKeyUsageMask usage_mask) const OVERRIDE { | |
98 return CheckKeyCreationUsages(kAllKeyUsages, usage_mask); | |
99 } | |
100 | |
101 virtual Status ImportKeyRaw(const CryptoData& key_data, | 101 virtual Status ImportKeyRaw(const CryptoData& key_data, |
102 const blink::WebCryptoAlgorithm& algorithm, | 102 const blink::WebCryptoAlgorithm& algorithm, |
103 bool extractable, | 103 bool extractable, |
104 blink::WebCryptoKeyUsageMask usage_mask, | 104 blink::WebCryptoKeyUsageMask usage_mask, |
105 blink::WebCryptoKey* key) const OVERRIDE { | 105 blink::WebCryptoKey* key) const OVERRIDE { |
106 const blink::WebCryptoAlgorithm& hash = | 106 const blink::WebCryptoAlgorithm& hash = |
107 algorithm.hmacImportParams()->hash(); | 107 algorithm.hmacImportParams()->hash(); |
108 | 108 |
109 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; | 109 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; |
110 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) | 110 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 232 |
233 } // namespace | 233 } // namespace |
234 | 234 |
235 AlgorithmImplementation* CreatePlatformHmacImplementation() { | 235 AlgorithmImplementation* CreatePlatformHmacImplementation() { |
236 return new HmacImplementation; | 236 return new HmacImplementation; |
237 } | 237 } |
238 | 238 |
239 } // namespace webcrypto | 239 } // namespace webcrypto |
240 | 240 |
241 } // namespace content | 241 } // namespace content |
OLD | NEW |