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

Side by Side Diff: content/child/webcrypto/platform_crypto_openssl.cc

Issue 200763005: Support for the new WebCrypto digest by chunks API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated return param of FinishWithWebArrayAndStatus Created 6 years, 9 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
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 "content/child/webcrypto/platform_crypto.h" 5 #include "content/child/webcrypto/platform_crypto.h"
6 6
7 #include <vector> 7 #include <vector>
8 #include <openssl/aes.h> 8 #include <openssl/aes.h>
9 #include <openssl/evp.h> 9 #include <openssl/evp.h>
10 #include <openssl/hmac.h> 10 #include <openssl/hmac.h>
11 #include <openssl/rand.h> 11 #include <openssl/rand.h>
12 #include <openssl/sha.h> 12 #include <openssl/sha.h>
13 13
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
15 #include "content/child/webcrypto/crypto_data.h" 16 #include "content/child/webcrypto/crypto_data.h"
16 #include "content/child/webcrypto/status.h" 17 #include "content/child/webcrypto/status.h"
17 #include "content/child/webcrypto/webcrypto_util.h" 18 #include "content/child/webcrypto/webcrypto_util.h"
18 #include "crypto/openssl_util.h" 19 #include "crypto/openssl_util.h"
19 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" 20 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" 21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" 22 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
22 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" 23 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
23 24
24 namespace content { 25 namespace content {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 static_cast<unsigned int>(final_output_chunk_len); 145 static_cast<unsigned int>(final_output_chunk_len);
145 DCHECK_LE(final_output_len, output_max_len); 146 DCHECK_LE(final_output_len, output_max_len);
146 147
147 ShrinkBuffer(buffer, final_output_len); 148 ShrinkBuffer(buffer, final_output_len);
148 149
149 return Status::Success(); 150 return Status::Success();
150 } 151 }
151 152
152 } // namespace 153 } // namespace
153 154
155 class DigestorOpenSSL : public blink::WebCryptoDigestor {
156 public:
157 explicit DigestorOpenSSL(blink::WebCryptoAlgorithmId algorithm_id)
158 : initialized_(false),
159 digest_context_(EVP_MD_CTX_create()),
160 algorithm_id_(algorithm_id) {}
161
162 virtual bool consume(const unsigned char* data, unsigned int size) {
163 return ConsumeWithStatus(data, size).IsSuccess();
164 }
165
166 Status ConsumeWithStatus(const unsigned char* data, unsigned int size) {
167 crypto::OpenSSLErrStackTracer(FROM_HERE);
168 Status error = Init();
169 if (!error.IsSuccess())
170 return error;
171
172 if (!EVP_DigestUpdate(digest_context_.get(), data, size))
173 return Status::Error();
174
175 return Status::Success();
176 }
177
178 virtual bool finish(unsigned char*& result_data,
179 unsigned int& result_data_size) {
180 Status error = FinishInternal(result_, &result_data_size);
181 if (!error.IsSuccess())
182 return false;
183 result_data = result_;
184 return true;
185 }
186
187 Status FinishWithWebArrayAndStatus(blink::WebArrayBuffer* result) {
188 const int hash_expected_size = EVP_MD_CTX_size(digest_context_.get());
189 *result = blink::WebArrayBuffer::create(hash_expected_size, 1);
190 unsigned char* const hash_buffer =
191 static_cast<unsigned char* const>(result->data());
192 unsigned int hash_buffer_size; // ignored
193 Status error = FinishInternal(hash_buffer, &hash_buffer_size);
194 if (!error.IsSuccess())
195 result->reset();
196 return error;
197 }
198
199 private:
200 bool initialized_;
eroman 2014/03/26 01:43:54 same comment: the member variables should be at th
jww 2014/03/26 18:49:40 Done.
201 crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context_;
202 blink::WebCryptoAlgorithmId algorithm_id_;
203 unsigned char result_[EVP_MAX_MD_SIZE];
204
205 Status Init() {
206 if (initialized_)
207 return Status::Success();
208
209 const EVP_MD* digest_algorithm = GetDigest(algorithm_id_);
210 if (!digest_algorithm)
211 return Status::ErrorUnexpected();
212
213 if (!digest_context_.get())
214 return Status::Error();
215
216 if (!EVP_DigestInit_ex(digest_context_.get(), digest_algorithm, NULL))
217 return Status::Error();
218
219 initialized_ = true;
220 return Status::Success();
221 }
222
223 Status FinishInternal(unsigned char* result, unsigned int* result_size) {
224 crypto::OpenSSLErrStackTracer(FROM_HERE);
225 Status error = Init();
226 if (!error.IsSuccess())
227 return error;
228
229 const int hash_expected_size = EVP_MD_CTX_size(digest_context_.get());
230 if (hash_expected_size <= 0)
231 return Status::ErrorUnexpected();
232 DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE);
233
234 if (!EVP_DigestFinal_ex(digest_context_.get(), result, result_size) ||
235 static_cast<int>(*result_size) != hash_expected_size)
236 return Status::Error();
237
238 return Status::Success();
239 }
240 };
241
154 Status ExportKeyRaw(SymKey* key, blink::WebArrayBuffer* buffer) { 242 Status ExportKeyRaw(SymKey* key, blink::WebArrayBuffer* buffer) {
155 *buffer = CreateArrayBuffer(Uint8VectorStart(key->key()), key->key().size()); 243 *buffer = CreateArrayBuffer(Uint8VectorStart(key->key()), key->key().size());
156 return Status::Success(); 244 return Status::Success();
157 } 245 }
158 246
159 void Init() { crypto::EnsureOpenSSLInit(); } 247 void Init() { crypto::EnsureOpenSSLInit(); }
160 248
161 Status EncryptDecryptAesCbc(EncryptOrDecrypt mode, 249 Status EncryptDecryptAesCbc(EncryptOrDecrypt mode,
162 SymKey* key, 250 SymKey* key,
163 const CryptoData& data, 251 const CryptoData& data,
164 const CryptoData& iv, 252 const CryptoData& iv,
165 blink::WebArrayBuffer* buffer) { 253 blink::WebArrayBuffer* buffer) {
166 // TODO(eroman): inline the function here. 254 // TODO(eroman): inline the function here.
167 return AesCbcEncryptDecrypt(mode, key, iv, data, buffer); 255 return AesCbcEncryptDecrypt(mode, key, iv, data, buffer);
168 } 256 }
169 257
170 Status DigestSha(blink::WebCryptoAlgorithmId algorithm, 258 Status DigestSha(blink::WebCryptoAlgorithmId algorithm,
171 const CryptoData& data, 259 const CryptoData& data,
172 blink::WebArrayBuffer* buffer) { 260 blink::WebArrayBuffer* buffer) {
173 crypto::OpenSSLErrStackTracer(FROM_HERE); 261 DigestorOpenSSL digestor(algorithm);
262 Status error = digestor.ConsumeWithStatus(data.bytes(), data.byte_length());
263 if (!error.IsSuccess())
264 return error;
265 return digestor.FinishWithWebArrayAndStatus(buffer);
266 }
174 267
175 const EVP_MD* digest_algorithm = GetDigest(algorithm); 268 scoped_ptr<blink::WebCryptoDigestor> CreateDigestor(
176 if (!digest_algorithm) 269 blink::WebCryptoAlgorithmId algorithm_id) {
177 return Status::ErrorUnexpected(); 270 return scoped_ptr<blink::WebCryptoDigestor>(
178 271 new DigestorOpenSSL(algorithm_id));
179 crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context(
180 EVP_MD_CTX_create());
181 if (!digest_context.get())
182 return Status::Error();
183
184 if (!EVP_DigestInit_ex(digest_context.get(), digest_algorithm, NULL) ||
185 !EVP_DigestUpdate(
186 digest_context.get(), data.bytes(), data.byte_length())) {
187 return Status::Error();
188 }
189
190 const int hash_expected_size = EVP_MD_CTX_size(digest_context.get());
191 if (hash_expected_size <= 0)
192 return Status::ErrorUnexpected();
193 DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE);
194
195 *buffer = blink::WebArrayBuffer::create(hash_expected_size, 1);
196 unsigned char* const hash_buffer =
197 reinterpret_cast<unsigned char* const>(buffer->data());
198
199 unsigned int hash_size = 0;
200 if (!EVP_DigestFinal_ex(digest_context.get(), hash_buffer, &hash_size) ||
201 static_cast<int>(hash_size) != hash_expected_size) {
202 buffer->reset();
203 return Status::Error();
204 }
205
206 return Status::Success();
207 } 272 }
208 273
209 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, 274 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,
210 bool extractable, 275 bool extractable,
211 blink::WebCryptoKeyUsageMask usage_mask, 276 blink::WebCryptoKeyUsageMask usage_mask,
212 unsigned keylen_bytes, 277 unsigned keylen_bytes,
213 blink::WebCryptoKey* key) { 278 blink::WebCryptoKey* key) {
214 // TODO(eroman): Is this right? 279 // TODO(eroman): Is this right?
215 if (keylen_bytes == 0) 280 if (keylen_bytes == 0)
216 return Status::ErrorGenerateKeyLength(); 281 return Status::ErrorGenerateKeyLength();
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 blink::WebCryptoKey* key) { 498 blink::WebCryptoKey* key) {
434 // TODO(eroman): http://crbug.com/267888 499 // TODO(eroman): http://crbug.com/267888
435 return Status::ErrorUnsupported(); 500 return Status::ErrorUnsupported();
436 } 501 }
437 502
438 } // namespace platform 503 } // namespace platform
439 504
440 } // namespace webcrypto 505 } // namespace webcrypto
441 506
442 } // namespace content 507 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698