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/openssl/util_openssl.h"
7 #include <openssl/evp.h>
9 #include "base/stl_util.h"
10 #include "content/child/webcrypto/crypto_data.h"
11 #include "content/child/webcrypto/openssl/key_openssl.h"
12 #include "content/child/webcrypto/platform_crypto.h"
13 #include "content/child/webcrypto/status.h"
14 #include "crypto/openssl_util.h"
21 crypto::EnsureOpenSSLInit();
24 const EVP_MD
* GetDigest(blink::WebCryptoAlgorithmId id
) {
26 case blink::WebCryptoAlgorithmIdSha1
:
28 case blink::WebCryptoAlgorithmIdSha256
:
30 case blink::WebCryptoAlgorithmIdSha384
:
32 case blink::WebCryptoAlgorithmIdSha512
:
39 Status
AeadEncryptDecrypt(EncryptOrDecrypt mode
,
40 const std::vector
<uint8_t>& raw_key
,
41 const CryptoData
& data
,
42 unsigned int tag_length_bytes
,
44 const CryptoData
& additional_data
,
45 const EVP_AEAD
* aead_alg
,
46 std::vector
<uint8_t>* buffer
) {
47 crypto::OpenSSLErrStackTracer
err_tracer(FROM_HERE
);
51 return Status::ErrorUnexpected();
53 if (!EVP_AEAD_CTX_init(&ctx
,
55 vector_as_array(&raw_key
),
59 return Status::OperationError();
62 crypto::ScopedOpenSSL
<EVP_AEAD_CTX
, EVP_AEAD_CTX_cleanup
>::Type
ctx_cleanup(
68 if (mode
== DECRYPT
) {
69 if (data
.byte_length() < tag_length_bytes
)
70 return Status::ErrorDataTooSmall();
72 buffer
->resize(data
.byte_length() - tag_length_bytes
);
74 ok
= EVP_AEAD_CTX_open(&ctx
,
75 vector_as_array(buffer
),
82 additional_data
.bytes(),
83 additional_data
.byte_length());
85 // No need to check for unsigned integer overflow here (seal fails if
86 // the output buffer is too small).
87 buffer
->resize(data
.byte_length() + EVP_AEAD_max_overhead(aead_alg
));
89 ok
= EVP_AEAD_CTX_seal(&ctx
,
90 vector_as_array(buffer
),
97 additional_data
.bytes(),
98 additional_data
.byte_length());
102 return Status::OperationError();
104 return Status::Success();
107 } // namespace webcrypto
109 } // namespace content