Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: content/child/webcrypto/openssl/aes_gcm_openssl.cc

Issue 379383002: Refactor WebCrypto code (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase onto master (no longer has BoringSSL) Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "content/child/webcrypto/crypto_data.h"
10 #include "content/child/webcrypto/openssl/aes_key_openssl.h"
11 #include "content/child/webcrypto/openssl/key_openssl.h"
12 #include "content/child/webcrypto/openssl/util_openssl.h"
13 #include "content/child/webcrypto/status.h"
14 #include "content/child/webcrypto/webcrypto_util.h"
15 #include "crypto/openssl_util.h"
16 #include "crypto/scoped_openssl_types.h"
17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
18
19 namespace content {
20
21 namespace webcrypto {
22
23 namespace {
24
25 const EVP_AEAD* GetAesGcmAlgorithmFromKeySize(unsigned int key_size_bytes) {
26 switch (key_size_bytes) {
27 case 16:
28 return EVP_aead_aes_128_gcm();
29 // TODO(eroman): Hook up 256-bit support when it is available.
30 default:
31 return NULL;
32 }
33 }
34
35 Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode,
36 const blink::WebCryptoAlgorithm& algorithm,
37 const blink::WebCryptoKey& key,
38 const CryptoData& data,
39 std::vector<uint8>* buffer) {
40 const std::vector<uint8>& raw_key = SymKeyOpenSsl::Cast(key)->raw_key_data();
41 const blink::WebCryptoAesGcmParams* params = algorithm.aesGcmParams();
42
43 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
44
45 unsigned int tag_length_bits;
46 Status status = GetAesGcmTagLengthInBits(params, &tag_length_bits);
47 if (status.IsError())
48 return status;
49 unsigned int tag_length_bytes = tag_length_bits / 8;
50
51 CryptoData iv(params->iv());
52 CryptoData additional_data(params->optionalAdditionalData());
53
54 EVP_AEAD_CTX ctx;
55
56 const EVP_AEAD* const aead_alg =
57 GetAesGcmAlgorithmFromKeySize(raw_key.size());
58 if (!aead_alg)
59 return Status::ErrorUnexpected();
60
61 if (!EVP_AEAD_CTX_init(&ctx,
62 aead_alg,
63 Uint8VectorStart(raw_key),
64 raw_key.size(),
65 tag_length_bytes,
66 NULL)) {
67 return Status::OperationError();
68 }
69
70 crypto::ScopedOpenSSL<EVP_AEAD_CTX, EVP_AEAD_CTX_cleanup>::Type ctx_cleanup(
71 &ctx);
72
73 ssize_t len;
74
75 if (mode == DECRYPT) {
76 if (data.byte_length() < tag_length_bytes)
77 return Status::ErrorDataTooSmall();
78
79 buffer->resize(data.byte_length() - tag_length_bytes);
80
81 len = EVP_AEAD_CTX_open(&ctx,
82 Uint8VectorStart(buffer),
83 buffer->size(),
84 iv.bytes(),
85 iv.byte_length(),
86 data.bytes(),
87 data.byte_length(),
88 additional_data.bytes(),
89 additional_data.byte_length());
90 } else {
91 // No need to check for unsigned integer overflow here (seal fails if
92 // the output buffer is too small).
93 buffer->resize(data.byte_length() + tag_length_bytes);
94
95 len = EVP_AEAD_CTX_seal(&ctx,
96 Uint8VectorStart(buffer),
97 buffer->size(),
98 iv.bytes(),
99 iv.byte_length(),
100 data.bytes(),
101 data.byte_length(),
102 additional_data.bytes(),
103 additional_data.byte_length());
104 }
105
106 if (len < 0)
107 return Status::OperationError();
108 buffer->resize(len);
109 return Status::Success();
110 }
111
112 class AesGcmImplementation : public AesAlgorithm {
113 public:
114 AesGcmImplementation() : AesAlgorithm("GCM") {}
115
116 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
117 const blink::WebCryptoKey& key,
118 const CryptoData& data,
119 std::vector<uint8>* buffer) const OVERRIDE {
120 return AesGcmEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer);
121 }
122
123 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
124 const blink::WebCryptoKey& key,
125 const CryptoData& data,
126 std::vector<uint8>* buffer) const OVERRIDE {
127 return AesGcmEncryptDecrypt(DECRYPT, algorithm, key, data, buffer);
128 }
129 };
130
131 } // namespace
132
133 AlgorithmImplementation* CreatePlatformAesGcmImplementation() {
134 return new AesGcmImplementation;
135 }
136
137 } // namespace webcrypto
138
139 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webcrypto/openssl/aes_cbc_openssl.cc ('k') | content/child/webcrypto/openssl/aes_key_openssl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698