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/stl_util.h" | |
10 #include "content/child/webcrypto/crypto_data.h" | |
11 #include "content/child/webcrypto/openssl/aes_algorithm_openssl.h" | |
12 #include "content/child/webcrypto/openssl/key_openssl.h" | |
13 #include "content/child/webcrypto/openssl/util_openssl.h" | |
14 #include "content/child/webcrypto/status.h" | |
15 #include "content/child/webcrypto/webcrypto_util.h" | |
16 #include "crypto/openssl_util.h" | |
17 #include "crypto/scoped_openssl_types.h" | |
18 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
19 | |
20 namespace content { | |
21 | |
22 namespace webcrypto { | |
23 | |
24 namespace { | |
25 | |
26 const EVP_AEAD* GetAesGcmAlgorithmFromKeySize(unsigned int key_size_bytes) { | |
27 switch (key_size_bytes) { | |
28 case 16: | |
29 return EVP_aead_aes_128_gcm(); | |
30 case 32: | |
31 return EVP_aead_aes_256_gcm(); | |
32 default: | |
33 return NULL; | |
34 } | |
35 } | |
36 | |
37 Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode, | |
38 const blink::WebCryptoAlgorithm& algorithm, | |
39 const blink::WebCryptoKey& key, | |
40 const CryptoData& data, | |
41 std::vector<uint8_t>* buffer) { | |
42 const std::vector<uint8_t>& raw_key = | |
43 SymKeyOpenSsl::Cast(key)->raw_key_data(); | |
44 const blink::WebCryptoAesGcmParams* params = algorithm.aesGcmParams(); | |
45 | |
46 unsigned int tag_length_bits; | |
47 Status status = GetAesGcmTagLengthInBits(params, &tag_length_bits); | |
48 if (status.IsError()) | |
49 return status; | |
50 | |
51 return AeadEncryptDecrypt( | |
52 mode, raw_key, data, tag_length_bits / 8, CryptoData(params->iv()), | |
53 CryptoData(params->optionalAdditionalData()), | |
54 GetAesGcmAlgorithmFromKeySize(raw_key.size()), buffer); | |
55 } | |
56 | |
57 class AesGcmImplementation : public AesAlgorithm { | |
58 public: | |
59 AesGcmImplementation() : AesAlgorithm("GCM") {} | |
60 | |
61 Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | |
62 const blink::WebCryptoKey& key, | |
63 const CryptoData& data, | |
64 std::vector<uint8_t>* buffer) const override { | |
65 return AesGcmEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer); | |
66 } | |
67 | |
68 Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, | |
69 const blink::WebCryptoKey& key, | |
70 const CryptoData& data, | |
71 std::vector<uint8_t>* buffer) const override { | |
72 return AesGcmEncryptDecrypt(DECRYPT, algorithm, key, data, buffer); | |
73 } | |
74 }; | |
75 | |
76 } // namespace | |
77 | |
78 AlgorithmImplementation* CreatePlatformAesGcmImplementation() { | |
79 return new AesGcmImplementation; | |
80 } | |
81 | |
82 } // namespace webcrypto | |
83 | |
84 } // namespace content | |
OLD | NEW |