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 "content/child/webcrypto/crypto_data.h" | 9 #include "content/child/webcrypto/crypto_data.h" |
10 #include "content/child/webcrypto/openssl/aes_key_openssl.h" | 10 #include "content/child/webcrypto/openssl/aes_key_openssl.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 Uint8VectorStart(raw_key), | 64 Uint8VectorStart(raw_key), |
65 raw_key.size(), | 65 raw_key.size(), |
66 tag_length_bytes, | 66 tag_length_bytes, |
67 NULL)) { | 67 NULL)) { |
68 return Status::OperationError(); | 68 return Status::OperationError(); |
69 } | 69 } |
70 | 70 |
71 crypto::ScopedOpenSSL<EVP_AEAD_CTX, EVP_AEAD_CTX_cleanup>::Type ctx_cleanup( | 71 crypto::ScopedOpenSSL<EVP_AEAD_CTX, EVP_AEAD_CTX_cleanup>::Type ctx_cleanup( |
72 &ctx); | 72 &ctx); |
73 | 73 |
74 ssize_t len; | 74 size_t len; |
| 75 int ok; |
75 | 76 |
76 if (mode == DECRYPT) { | 77 if (mode == DECRYPT) { |
77 if (data.byte_length() < tag_length_bytes) | 78 if (data.byte_length() < tag_length_bytes) |
78 return Status::ErrorDataTooSmall(); | 79 return Status::ErrorDataTooSmall(); |
79 | 80 |
80 buffer->resize(data.byte_length() - tag_length_bytes); | 81 buffer->resize(data.byte_length() - tag_length_bytes); |
81 | 82 |
82 len = EVP_AEAD_CTX_open(&ctx, | 83 ok = EVP_AEAD_CTX_open(&ctx, |
83 Uint8VectorStart(buffer), | 84 Uint8VectorStart(buffer), |
84 buffer->size(), | 85 &len, |
85 iv.bytes(), | 86 buffer->size(), |
86 iv.byte_length(), | 87 iv.bytes(), |
87 data.bytes(), | 88 iv.byte_length(), |
88 data.byte_length(), | 89 data.bytes(), |
89 additional_data.bytes(), | 90 data.byte_length(), |
90 additional_data.byte_length()); | 91 additional_data.bytes(), |
| 92 additional_data.byte_length()); |
91 } else { | 93 } else { |
92 // No need to check for unsigned integer overflow here (seal fails if | 94 // No need to check for unsigned integer overflow here (seal fails if |
93 // the output buffer is too small). | 95 // the output buffer is too small). |
94 buffer->resize(data.byte_length() + tag_length_bytes); | 96 buffer->resize(data.byte_length() + tag_length_bytes); |
95 | 97 |
96 len = EVP_AEAD_CTX_seal(&ctx, | 98 ok = EVP_AEAD_CTX_seal(&ctx, |
97 Uint8VectorStart(buffer), | 99 Uint8VectorStart(buffer), |
98 buffer->size(), | 100 &len, |
99 iv.bytes(), | 101 buffer->size(), |
100 iv.byte_length(), | 102 iv.bytes(), |
101 data.bytes(), | 103 iv.byte_length(), |
102 data.byte_length(), | 104 data.bytes(), |
103 additional_data.bytes(), | 105 data.byte_length(), |
104 additional_data.byte_length()); | 106 additional_data.bytes(), |
| 107 additional_data.byte_length()); |
105 } | 108 } |
106 | 109 |
107 if (len < 0) | 110 if (!ok) |
108 return Status::OperationError(); | 111 return Status::OperationError(); |
109 buffer->resize(len); | 112 buffer->resize(len); |
110 return Status::Success(); | 113 return Status::Success(); |
111 } | 114 } |
112 | 115 |
113 class AesGcmImplementation : public AesAlgorithm { | 116 class AesGcmImplementation : public AesAlgorithm { |
114 public: | 117 public: |
115 AesGcmImplementation() : AesAlgorithm("GCM") {} | 118 AesGcmImplementation() : AesAlgorithm("GCM") {} |
116 | 119 |
117 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | 120 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, |
(...skipping 13 matching lines...) Expand all Loading... |
131 | 134 |
132 } // namespace | 135 } // namespace |
133 | 136 |
134 AlgorithmImplementation* CreatePlatformAesGcmImplementation() { | 137 AlgorithmImplementation* CreatePlatformAesGcmImplementation() { |
135 return new AesGcmImplementation; | 138 return new AesGcmImplementation; |
136 } | 139 } |
137 | 140 |
138 } // namespace webcrypto | 141 } // namespace webcrypto |
139 | 142 |
140 } // namespace content | 143 } // namespace content |
OLD | NEW |