Roll src/third_party/skia d32087a:1052f51
[chromium-blink-merge.git] / components / webcrypto / algorithms / aes_gcm.cc
blobb4e7c3d55ad1343bac3cd1d5fe22b7e36d3e34ca
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 <openssl/evp.h>
6 #include <vector>
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "components/webcrypto/algorithms/aes.h"
11 #include "components/webcrypto/algorithms/util_openssl.h"
12 #include "components/webcrypto/crypto_data.h"
13 #include "components/webcrypto/key.h"
14 #include "components/webcrypto/status.h"
15 #include "components/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"
20 namespace webcrypto {
22 namespace {
24 const EVP_AEAD* GetAesGcmAlgorithmFromKeySize(size_t key_size_bytes) {
25 switch (key_size_bytes) {
26 case 16:
27 return EVP_aead_aes_128_gcm();
28 case 32:
29 return EVP_aead_aes_256_gcm();
30 default:
31 return NULL;
35 Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode,
36 const blink::WebCryptoAlgorithm& algorithm,
37 const blink::WebCryptoKey& key,
38 const CryptoData& data,
39 std::vector<uint8_t>* buffer) {
40 const std::vector<uint8_t>& raw_key = GetSymmetricKeyData(key);
41 const blink::WebCryptoAesGcmParams* params = algorithm.aesGcmParams();
43 unsigned int tag_length_bits;
44 Status status = GetAesGcmTagLengthInBits(params, &tag_length_bits);
45 if (status.IsError())
46 return status;
48 return AeadEncryptDecrypt(
49 mode, raw_key, data, tag_length_bits / 8, CryptoData(params->iv()),
50 CryptoData(params->optionalAdditionalData()),
51 GetAesGcmAlgorithmFromKeySize(raw_key.size()), buffer);
54 class AesGcmImplementation : public AesAlgorithm {
55 public:
56 AesGcmImplementation() : AesAlgorithm("GCM") {}
58 Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
59 const blink::WebCryptoKey& key,
60 const CryptoData& data,
61 std::vector<uint8_t>* buffer) const override {
62 return AesGcmEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer);
65 Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
66 const blink::WebCryptoKey& key,
67 const CryptoData& data,
68 std::vector<uint8_t>* buffer) const override {
69 return AesGcmEncryptDecrypt(DECRYPT, algorithm, key, data, buffer);
73 } // namespace
75 scoped_ptr<AlgorithmImplementation> CreateAesGcmImplementation() {
76 return make_scoped_ptr(new AesGcmImplementation);
79 } // namespace webcrypto