OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <vector> | 5 #include <vector> |
6 #include <openssl/evp.h> | 6 #include <openssl/evp.h> |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/stl_util.h" |
9 #include "content/child/webcrypto/crypto_data.h" | 10 #include "content/child/webcrypto/crypto_data.h" |
10 #include "content/child/webcrypto/openssl/aes_key_openssl.h" | 11 #include "content/child/webcrypto/openssl/aes_key_openssl.h" |
11 #include "content/child/webcrypto/openssl/key_openssl.h" | 12 #include "content/child/webcrypto/openssl/key_openssl.h" |
12 #include "content/child/webcrypto/openssl/util_openssl.h" | 13 #include "content/child/webcrypto/openssl/util_openssl.h" |
13 #include "content/child/webcrypto/status.h" | 14 #include "content/child/webcrypto/status.h" |
14 #include "content/child/webcrypto/webcrypto_util.h" | 15 #include "content/child/webcrypto/webcrypto_util.h" |
15 #include "crypto/openssl_util.h" | 16 #include "crypto/openssl_util.h" |
16 #include "crypto/scoped_openssl_types.h" | 17 #include "crypto/scoped_openssl_types.h" |
17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 18 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
18 | 19 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 | 56 |
56 EVP_AEAD_CTX ctx; | 57 EVP_AEAD_CTX ctx; |
57 | 58 |
58 const EVP_AEAD* const aead_alg = | 59 const EVP_AEAD* const aead_alg = |
59 GetAesGcmAlgorithmFromKeySize(raw_key.size()); | 60 GetAesGcmAlgorithmFromKeySize(raw_key.size()); |
60 if (!aead_alg) | 61 if (!aead_alg) |
61 return Status::ErrorUnexpected(); | 62 return Status::ErrorUnexpected(); |
62 | 63 |
63 if (!EVP_AEAD_CTX_init(&ctx, | 64 if (!EVP_AEAD_CTX_init(&ctx, |
64 aead_alg, | 65 aead_alg, |
65 Uint8VectorStart(raw_key), | 66 vector_as_array(&raw_key), |
66 raw_key.size(), | 67 raw_key.size(), |
67 tag_length_bytes, | 68 tag_length_bytes, |
68 NULL)) { | 69 NULL)) { |
69 return Status::OperationError(); | 70 return Status::OperationError(); |
70 } | 71 } |
71 | 72 |
72 crypto::ScopedOpenSSL<EVP_AEAD_CTX, EVP_AEAD_CTX_cleanup>::Type ctx_cleanup( | 73 crypto::ScopedOpenSSL<EVP_AEAD_CTX, EVP_AEAD_CTX_cleanup>::Type ctx_cleanup( |
73 &ctx); | 74 &ctx); |
74 | 75 |
75 size_t len; | 76 size_t len; |
76 int ok; | 77 int ok; |
77 | 78 |
78 if (mode == DECRYPT) { | 79 if (mode == DECRYPT) { |
79 if (data.byte_length() < tag_length_bytes) | 80 if (data.byte_length() < tag_length_bytes) |
80 return Status::ErrorDataTooSmall(); | 81 return Status::ErrorDataTooSmall(); |
81 | 82 |
82 buffer->resize(data.byte_length() - tag_length_bytes); | 83 buffer->resize(data.byte_length() - tag_length_bytes); |
83 | 84 |
84 ok = EVP_AEAD_CTX_open(&ctx, | 85 ok = EVP_AEAD_CTX_open(&ctx, |
85 Uint8VectorStart(buffer), | 86 vector_as_array(buffer), |
86 &len, | 87 &len, |
87 buffer->size(), | 88 buffer->size(), |
88 iv.bytes(), | 89 iv.bytes(), |
89 iv.byte_length(), | 90 iv.byte_length(), |
90 data.bytes(), | 91 data.bytes(), |
91 data.byte_length(), | 92 data.byte_length(), |
92 additional_data.bytes(), | 93 additional_data.bytes(), |
93 additional_data.byte_length()); | 94 additional_data.byte_length()); |
94 } else { | 95 } else { |
95 // No need to check for unsigned integer overflow here (seal fails if | 96 // No need to check for unsigned integer overflow here (seal fails if |
96 // the output buffer is too small). | 97 // the output buffer is too small). |
97 buffer->resize(data.byte_length() + tag_length_bytes); | 98 buffer->resize(data.byte_length() + tag_length_bytes); |
98 | 99 |
99 ok = EVP_AEAD_CTX_seal(&ctx, | 100 ok = EVP_AEAD_CTX_seal(&ctx, |
100 Uint8VectorStart(buffer), | 101 vector_as_array(buffer), |
101 &len, | 102 &len, |
102 buffer->size(), | 103 buffer->size(), |
103 iv.bytes(), | 104 iv.bytes(), |
104 iv.byte_length(), | 105 iv.byte_length(), |
105 data.bytes(), | 106 data.bytes(), |
106 data.byte_length(), | 107 data.byte_length(), |
107 additional_data.bytes(), | 108 additional_data.bytes(), |
108 additional_data.byte_length()); | 109 additional_data.byte_length()); |
109 } | 110 } |
110 | 111 |
(...skipping 24 matching lines...) Expand all Loading... |
135 | 136 |
136 } // namespace | 137 } // namespace |
137 | 138 |
138 AlgorithmImplementation* CreatePlatformAesGcmImplementation() { | 139 AlgorithmImplementation* CreatePlatformAesGcmImplementation() { |
139 return new AesGcmImplementation; | 140 return new AesGcmImplementation; |
140 } | 141 } |
141 | 142 |
142 } // namespace webcrypto | 143 } // namespace webcrypto |
143 | 144 |
144 } // namespace content | 145 } // namespace content |
OLD | NEW |