OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/renderer/webcrypto_impl.h" | 5 #include "content/renderer/webcrypto_impl.h" |
6 | 6 |
7 #include <openssl/evp.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "crypto/openssl_util.h" | |
11 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | |
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | |
13 | |
7 namespace content { | 14 namespace content { |
8 | 15 |
9 void WebCryptoImpl::Init() { | 16 void WebCryptoImpl::Init() { crypto::EnsureOpenSSLInit(); } |
10 } | |
11 | 17 |
12 bool WebCryptoImpl::DigestInternal( | 18 bool WebCryptoImpl::DigestInternal(const WebKit::WebCryptoAlgorithm& algorithm, |
13 const WebKit::WebCryptoAlgorithm& algorithm, | 19 const unsigned char* data, |
14 const unsigned char* data, | 20 unsigned data_size, |
15 unsigned data_size, | 21 WebKit::WebArrayBuffer* buffer) { |
16 WebKit::WebArrayBuffer* buffer) { | 22 |
17 // TODO(bryaneyler): Placeholder for OpenSSL implementation. | 23 crypto::OpenSSLErrStackTracer(FROM_HERE); |
18 // Issue http://crbug.com/267888. | 24 |
19 return false; | 25 const EVP_MD* digest_algorithm; |
26 switch (algorithm.id()) { | |
27 case WebKit::WebCryptoAlgorithmIdSha1: | |
28 digest_algorithm = EVP_sha1(); | |
29 break; | |
30 case WebKit::WebCryptoAlgorithmIdSha224: | |
31 digest_algorithm = EVP_sha224(); | |
32 break; | |
33 case WebKit::WebCryptoAlgorithmIdSha256: | |
34 digest_algorithm = EVP_sha256(); | |
35 break; | |
36 case WebKit::WebCryptoAlgorithmIdSha384: | |
37 digest_algorithm = EVP_sha384(); | |
38 break; | |
39 case WebKit::WebCryptoAlgorithmIdSha512: | |
40 digest_algorithm = EVP_sha512(); | |
41 break; | |
42 default: | |
43 // Not a digest algorithm. | |
44 return false; | |
45 } | |
46 | |
47 crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context( | |
48 EVP_MD_CTX_create()); | |
49 if (!digest_context.get()) return false; | |
Bryan Eyler
2013/09/23 21:45:51
For consistency (with EVP_Digest* ops and hash_exp
padolph
2013/09/23 22:08:31
Done.
| |
50 | |
51 if (!EVP_DigestInit_ex(digest_context.get(), digest_algorithm, NULL) || | |
52 !EVP_DigestUpdate(digest_context.get(), data, data_size)) { | |
53 return false; | |
54 } | |
55 | |
56 const int hash_expected_size = EVP_MD_CTX_size(digest_context.get()); | |
57 if (hash_expected_size <= 0) { | |
58 return false; | |
59 } | |
60 DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE); | |
61 | |
62 *buffer = WebKit::WebArrayBuffer::create(hash_expected_size, 1); | |
63 unsigned char* const hash_buffer = | |
64 reinterpret_cast<unsigned char* const>(buffer->data()); | |
65 | |
66 unsigned int hash_size = 0; | |
Bryan Eyler
2013/09/23 21:45:51
I believe Chromium style is to just use "unsigned"
padolph
2013/09/23 22:08:31
Done.
| |
67 if (!EVP_DigestFinal_ex(digest_context.get(), hash_buffer, &hash_size) || | |
68 static_cast<int>(hash_size) != hash_expected_size) { | |
69 buffer->reset(); | |
70 return false; | |
71 } | |
72 | |
73 return true; | |
20 } | 74 } |
21 | 75 |
22 } // namespace content | 76 } // namespace content |
OLD | NEW |