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
3 // found in the LICENSE file.
5 #include "content/child/webcrypto/nss/aes_key_nss.h"
7 #include "base/logging.h"
8 #include "content/child/webcrypto/crypto_data.h"
9 #include "content/child/webcrypto/jwk.h"
10 #include "content/child/webcrypto/nss/key_nss.h"
11 #include "content/child/webcrypto/nss/sym_key_nss.h"
12 #include "content/child/webcrypto/status.h"
13 #include "content/child/webcrypto/webcrypto_util.h"
14 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
20 AesAlgorithm::AesAlgorithm(CK_MECHANISM_TYPE import_mechanism
,
21 CK_FLAGS import_flags
,
22 blink::WebCryptoKeyUsageMask all_key_usages
,
23 const std::string
& jwk_suffix
)
24 : import_mechanism_(import_mechanism
),
25 import_flags_(import_flags
),
26 all_key_usages_(all_key_usages
),
27 jwk_suffix_(jwk_suffix
) {
30 AesAlgorithm::AesAlgorithm(CK_MECHANISM_TYPE import_mechanism
,
31 const std::string
& jwk_suffix
)
32 : import_mechanism_(import_mechanism
),
33 import_flags_(CKF_ENCRYPT
| CKF_DECRYPT
),
34 all_key_usages_(blink::WebCryptoKeyUsageEncrypt
|
35 blink::WebCryptoKeyUsageDecrypt
|
36 blink::WebCryptoKeyUsageWrapKey
|
37 blink::WebCryptoKeyUsageUnwrapKey
),
38 jwk_suffix_(jwk_suffix
) {
41 Status
AesAlgorithm::VerifyKeyUsagesBeforeGenerateKey(
42 blink::WebCryptoKeyUsageMask usage_mask
) const {
43 return CheckKeyCreationUsages(all_key_usages_
, usage_mask
);
46 Status
AesAlgorithm::GenerateSecretKey(
47 const blink::WebCryptoAlgorithm
& algorithm
,
49 blink::WebCryptoKeyUsageMask usage_mask
,
50 blink::WebCryptoKey
* key
) const {
51 unsigned int keylen_bits
;
53 GetAesKeyGenLengthInBits(algorithm
.aesKeyGenParams(), &keylen_bits
);
57 return GenerateSecretKeyNss(
58 blink::WebCryptoKeyAlgorithm::createAes(algorithm
.id(), keylen_bits
),
66 Status
AesAlgorithm::VerifyKeyUsagesBeforeImportKey(
67 blink::WebCryptoKeyFormat format
,
68 blink::WebCryptoKeyUsageMask usage_mask
) const {
70 case blink::WebCryptoKeyFormatRaw
:
71 case blink::WebCryptoKeyFormatJwk
:
72 return CheckKeyCreationUsages(all_key_usages_
, usage_mask
);
74 return Status::ErrorUnsupportedImportKeyFormat();
77 Status
AesAlgorithm::ImportKeyRaw(const CryptoData
& key_data
,
78 const blink::WebCryptoAlgorithm
& algorithm
,
80 blink::WebCryptoKeyUsageMask usage_mask
,
81 blink::WebCryptoKey
* key
) const {
82 const unsigned int keylen_bytes
= key_data
.byte_length();
83 Status status
= VerifyAesKeyLengthForImport(keylen_bytes
);
87 // No possibility of overflow.
88 unsigned int keylen_bits
= keylen_bytes
* 8;
90 return ImportKeyRawNss(
92 blink::WebCryptoKeyAlgorithm::createAes(algorithm
.id(), keylen_bits
),
100 Status
AesAlgorithm::ImportKeyJwk(const CryptoData
& key_data
,
101 const blink::WebCryptoAlgorithm
& algorithm
,
103 blink::WebCryptoKeyUsageMask usage_mask
,
104 blink::WebCryptoKey
* key
) const {
105 std::vector
<uint8_t> raw_data
;
106 Status status
= ReadAesSecretKeyJwk(
107 key_data
, jwk_suffix_
, extractable
, usage_mask
, &raw_data
);
108 if (status
.IsError())
112 CryptoData(raw_data
), algorithm
, extractable
, usage_mask
, key
);
115 Status
AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey
& key
,
116 std::vector
<uint8_t>* buffer
) const {
117 *buffer
= SymKeyNss::Cast(key
)->raw_key_data();
118 return Status::Success();
121 Status
AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey
& key
,
122 std::vector
<uint8_t>* buffer
) const {
123 SymKeyNss
* sym_key
= SymKeyNss::Cast(key
);
124 const std::vector
<uint8_t>& raw_data
= sym_key
->raw_key_data();
126 WriteSecretKeyJwk(CryptoData(raw_data
),
127 MakeJwkAesAlgorithmName(jwk_suffix_
, raw_data
.size()),
132 return Status::Success();
135 } // namespace webcrypto
137 } // namespace content