OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <vector> | |
6 #include <openssl/evp.h> | |
7 | |
8 #include "base/logging.h" | |
9 #include "base/numerics/safe_math.h" | |
10 #include "base/stl_util.h" | |
11 #include "components/webcrypto/crypto_data.h" | |
12 #include "components/webcrypto/openssl/aes_algorithm_openssl.h" | |
13 #include "components/webcrypto/openssl/key_openssl.h" | |
14 #include "components/webcrypto/openssl/util_openssl.h" | |
15 #include "components/webcrypto/status.h" | |
16 #include "crypto/openssl_util.h" | |
17 #include "crypto/scoped_openssl_types.h" | |
18 | |
19 namespace webcrypto { | |
20 | |
21 namespace { | |
22 | |
23 const EVP_AEAD* GetAesKwAlgorithmFromKeySize(size_t key_size_bytes) { | |
24 switch (key_size_bytes) { | |
25 case 16: | |
26 return EVP_aead_aes_128_key_wrap(); | |
27 case 32: | |
28 return EVP_aead_aes_256_key_wrap(); | |
29 default: | |
30 return NULL; | |
31 } | |
32 } | |
33 | |
34 Status AesKwEncryptDecrypt(EncryptOrDecrypt mode, | |
35 const blink::WebCryptoAlgorithm& algorithm, | |
36 const blink::WebCryptoKey& key, | |
37 const CryptoData& data, | |
38 std::vector<uint8_t>* buffer) { | |
39 // These length checks are done in order to give a more specific error. These | |
40 // are not required for correctness. | |
41 if ((mode == ENCRYPT && data.byte_length() < 16) || | |
42 (mode == DECRYPT && data.byte_length() < 24)) { | |
43 return Status::ErrorDataTooSmall(); | |
44 } | |
45 if (data.byte_length() % 8) | |
46 return Status::ErrorInvalidAesKwDataLength(); | |
47 | |
48 const std::vector<uint8_t>& raw_key = | |
49 SymKeyOpenSsl::Cast(key)->raw_key_data(); | |
50 | |
51 return AeadEncryptDecrypt(mode, raw_key, data, | |
52 8, // tag_length_bytes | |
53 CryptoData(), // iv | |
54 CryptoData(), // additional_data | |
55 GetAesKwAlgorithmFromKeySize(raw_key.size()), | |
56 buffer); | |
57 } | |
58 | |
59 class AesKwImplementation : public AesAlgorithm { | |
60 public: | |
61 AesKwImplementation() | |
62 : AesAlgorithm( | |
63 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, | |
64 "KW") {} | |
65 | |
66 Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | |
67 const blink::WebCryptoKey& key, | |
68 const CryptoData& data, | |
69 std::vector<uint8_t>* buffer) const override { | |
70 return AesKwEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer); | |
71 } | |
72 | |
73 Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, | |
74 const blink::WebCryptoKey& key, | |
75 const CryptoData& data, | |
76 std::vector<uint8_t>* buffer) const override { | |
77 return AesKwEncryptDecrypt(DECRYPT, algorithm, key, data, buffer); | |
78 } | |
79 }; | |
80 | |
81 } // namespace | |
82 | |
83 AlgorithmImplementation* CreatePlatformAesKwImplementation() { | |
84 return new AesKwImplementation; | |
85 } | |
86 | |
87 } // namespace webcrypto | |
OLD | NEW |