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 <secerr.h> | 5 #include <secerr.h> |
6 | 6 |
7 #include "base/numerics/safe_math.h" | 7 #include "base/numerics/safe_math.h" |
8 #include "content/child/webcrypto/crypto_data.h" | 8 #include "content/child/webcrypto/crypto_data.h" |
9 #include "content/child/webcrypto/nss/aes_key_nss.h" | 9 #include "content/child/webcrypto/nss/aes_key_nss.h" |
10 #include "content/child/webcrypto/nss/key_nss.h" | 10 #include "content/child/webcrypto/nss/key_nss.h" |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 AesKwCryptoAlgorithmNss() | 131 AesKwCryptoAlgorithmNss() |
132 : AesAlgorithm( | 132 : AesAlgorithm( |
133 CKM_NSS_AES_KEY_WRAP, | 133 CKM_NSS_AES_KEY_WRAP, |
134 CKF_WRAP | CKF_WRAP, | 134 CKF_WRAP | CKF_WRAP, |
135 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, | 135 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, |
136 "KW") {} | 136 "KW") {} |
137 | 137 |
138 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | 138 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, |
139 const blink::WebCryptoKey& wrapping_key, | 139 const blink::WebCryptoKey& wrapping_key, |
140 const CryptoData& data, | 140 const CryptoData& data, |
141 std::vector<uint8_t>* buffer) const OVERRIDE { | 141 std::vector<uint8_t>* buffer) const override { |
142 if (data.byte_length() < 16) | 142 if (data.byte_length() < 16) |
143 return Status::ErrorDataTooSmall(); | 143 return Status::ErrorDataTooSmall(); |
144 if (data.byte_length() % 8) | 144 if (data.byte_length() % 8) |
145 return Status::ErrorInvalidAesKwDataLength(); | 145 return Status::ErrorInvalidAesKwDataLength(); |
146 | 146 |
147 // Due to limitations in the NSS API for the AES-KW algorithm, |data| must | 147 // Due to limitations in the NSS API for the AES-KW algorithm, |data| must |
148 // be temporarily viewed as a symmetric key to be wrapped (encrypted). | 148 // be temporarily viewed as a symmetric key to be wrapped (encrypted). |
149 SECItem data_item = MakeSECItemForBuffer(data); | 149 SECItem data_item = MakeSECItemForBuffer(data); |
150 crypto::ScopedPK11Slot slot(PK11_GetInternalSlot()); | 150 crypto::ScopedPK11Slot slot(PK11_GetInternalSlot()); |
151 crypto::ScopedPK11SymKey data_as_sym_key( | 151 crypto::ScopedPK11SymKey data_as_sym_key( |
152 PK11_ImportSymKey(slot.get(), | 152 PK11_ImportSymKey(slot.get(), |
153 CKK_GENERIC_SECRET, | 153 CKK_GENERIC_SECRET, |
154 PK11_OriginUnwrap, | 154 PK11_OriginUnwrap, |
155 CKA_SIGN, | 155 CKA_SIGN, |
156 &data_item, | 156 &data_item, |
157 NULL)); | 157 NULL)); |
158 if (!data_as_sym_key) | 158 if (!data_as_sym_key) |
159 return Status::OperationError(); | 159 return Status::OperationError(); |
160 | 160 |
161 return WrapSymKeyAesKw( | 161 return WrapSymKeyAesKw( |
162 data_as_sym_key.get(), SymKeyNss::Cast(wrapping_key)->key(), buffer); | 162 data_as_sym_key.get(), SymKeyNss::Cast(wrapping_key)->key(), buffer); |
163 } | 163 } |
164 | 164 |
165 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, | 165 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, |
166 const blink::WebCryptoKey& wrapping_key, | 166 const blink::WebCryptoKey& wrapping_key, |
167 const CryptoData& data, | 167 const CryptoData& data, |
168 std::vector<uint8_t>* buffer) const OVERRIDE { | 168 std::vector<uint8_t>* buffer) const override { |
169 if (data.byte_length() < 24) | 169 if (data.byte_length() < 24) |
170 return Status::ErrorDataTooSmall(); | 170 return Status::ErrorDataTooSmall(); |
171 if (data.byte_length() % 8) | 171 if (data.byte_length() % 8) |
172 return Status::ErrorInvalidAesKwDataLength(); | 172 return Status::ErrorInvalidAesKwDataLength(); |
173 | 173 |
174 // Due to limitations in the NSS API for the AES-KW algorithm, |data| must | 174 // Due to limitations in the NSS API for the AES-KW algorithm, |data| must |
175 // be temporarily viewed as a symmetric key to be unwrapped (decrypted). | 175 // be temporarily viewed as a symmetric key to be unwrapped (decrypted). |
176 crypto::ScopedPK11SymKey decrypted; | 176 crypto::ScopedPK11SymKey decrypted; |
177 Status status = DoUnwrapSymKeyAesKw(data, | 177 Status status = DoUnwrapSymKeyAesKw(data, |
178 SymKeyNss::Cast(wrapping_key)->key(), | 178 SymKeyNss::Cast(wrapping_key)->key(), |
(...skipping 18 matching lines...) Expand all Loading... |
197 | 197 |
198 } // namespace | 198 } // namespace |
199 | 199 |
200 AlgorithmImplementation* CreatePlatformAesKwImplementation() { | 200 AlgorithmImplementation* CreatePlatformAesKwImplementation() { |
201 return new AesKwCryptoAlgorithmNss; | 201 return new AesKwCryptoAlgorithmNss; |
202 } | 202 } |
203 | 203 |
204 } // namespace webcrypto | 204 } // namespace webcrypto |
205 | 205 |
206 } // namespace content | 206 } // namespace content |
OLD | NEW |