Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #if defined(USE_NSS) | |
| 8 #include <sechash.h> | |
| 9 #endif | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
|
eroman
2013/08/22 02:25:05
unused; remove
Bryan Eyler
2013/08/22 18:51:03
Done.
| |
| 13 #include "crypto/nss_util.h" | |
| 14 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | |
| 7 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 15 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 8 | 16 |
| 9 namespace content { | 17 namespace content { |
| 10 | 18 |
| 11 WebKit::WebCryptoOperation* WebCryptoImpl::digest( | 19 void WebCryptoImpl::digest( |
| 12 const WebKit::WebCryptoAlgorithm& algorithm) { | 20 const WebKit::WebCryptoAlgorithm& algorithm, |
| 21 const unsigned char* data, | |
| 22 size_t data_size, | |
| 23 WebKit::WebCryptoResult result) { | |
| 24 // TODO(bryaneyler): Also include these in OpenSSL build. Issue 267888. | |
| 25 #if defined(USE_NSS) | |
|
eroman
2013/08/22 02:25:05
Because this is quickly going to turn into a lot o
Bryan Eyler
2013/08/22 18:51:03
Done.
| |
| 26 HASH_HashType hash_type = HASH_AlgNULL; | |
| 27 | |
| 13 switch (algorithm.id()) { | 28 switch (algorithm.id()) { |
| 14 case WebKit::WebCryptoAlgorithmIdSha1: | 29 case WebKit::WebCryptoAlgorithmIdSha1: |
| 30 hash_type = HASH_AlgSHA1; | |
| 31 break; | |
| 15 case WebKit::WebCryptoAlgorithmIdSha224: | 32 case WebKit::WebCryptoAlgorithmIdSha224: |
| 33 hash_type = HASH_AlgSHA224; | |
| 34 break; | |
| 16 case WebKit::WebCryptoAlgorithmIdSha256: | 35 case WebKit::WebCryptoAlgorithmIdSha256: |
| 36 hash_type = HASH_AlgSHA256; | |
| 37 break; | |
| 17 case WebKit::WebCryptoAlgorithmIdSha384: | 38 case WebKit::WebCryptoAlgorithmIdSha384: |
| 39 hash_type = HASH_AlgSHA384; | |
| 40 break; | |
| 18 case WebKit::WebCryptoAlgorithmIdSha512: | 41 case WebKit::WebCryptoAlgorithmIdSha512: |
| 19 // TODO(eroman): Implement. | 42 hash_type = HASH_AlgSHA512; |
| 20 return NULL; | 43 break; |
| 21 default: | 44 default: |
| 22 // Not a digest algorithm. | 45 // Not a digest algorithm. |
| 23 return NULL; | 46 result.completeWithError(); |
| 47 return; | |
| 24 } | 48 } |
| 49 | |
| 50 crypto::EnsureNSSInit(); | |
| 51 | |
| 52 HASHContext* context = HASH_Create(hash_type); | |
| 53 if (!context) { | |
| 54 LOG(ERROR) << "Could not create digest context for hash algorithm: " | |
|
eroman
2013/08/22 02:25:05
I don't think there is value to having these LOG(E
Bryan Eyler
2013/08/22 18:51:03
Done.
| |
| 55 << hash_type; | |
| 56 result.completeWithError(); | |
| 57 return; | |
| 58 } | |
| 59 | |
| 60 HASH_Begin(context); | |
| 61 | |
| 62 HASH_Update(context, data, data_size); | |
| 63 | |
| 64 unsigned int hash_result_length = HASH_ResultLenContext(context); | |
|
jamesr
2013/08/22 02:31:01
i think size_t is a better type, but you don't wan
Bryan Eyler
2013/08/22 18:51:03
Changed to size_t. FWIW, I was trying to match th
| |
| 65 WebKit::WebArrayBuffer buffer( | |
| 66 WebKit::WebArrayBuffer::create(hash_result_length, 1)); | |
| 67 | |
| 68 unsigned char* digest = reinterpret_cast<unsigned char*>(buffer.data()); | |
| 69 DCHECK(digest); | |
|
eroman
2013/08/22 02:25:05
I think the only situation when buffer.data() can
jamesr
2013/08/22 02:31:01
I'm not sure this DCHECK makes sense. WebArrayBuff
Bryan Eyler
2013/08/22 18:51:03
DCHECK changed to sanity check hash_result_length.
| |
| 70 | |
| 71 unsigned int result_length = 0; | |
|
jamesr
2013/08/22 02:31:01
please use a better type
Bryan Eyler
2013/08/22 18:51:03
I've made this uint32 for lack of a better type.
| |
| 72 HASH_End(context, digest, &result_length, hash_result_length); | |
| 73 if (result_length != hash_result_length) { | |
| 74 LOG(ERROR) << "Result length invalid; expected " << hash_result_length | |
|
eroman
2013/08/22 02:25:05
LOG(ERROR) have very little usefulness in my exper
Bryan Eyler
2013/08/22 18:51:03
Done.
| |
| 75 << ", got " << result_length; | |
| 76 result.completeWithError(); | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 result.completeWithBuffer(buffer); | |
| 81 | |
| 82 #else | |
| 83 // No way to process. | |
| 84 result.completeWithError(); | |
| 85 #endif | |
| 25 } | 86 } |
| 26 | 87 |
| 27 } // namespace content | 88 } // namespace content |
| OLD | NEW |