OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/webcrypto_digest.h" |
| 6 |
| 7 #include <sechash.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "crypto/nss_util.h" |
| 11 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| 12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 WebCryptoDigest::WebCryptoDigest( |
| 17 const WebKit::WebCryptoOperationResult& result) |
| 18 : result_(result) { |
| 19 } |
| 20 |
| 21 WebCryptoDigest::~WebCryptoDigest() { |
| 22 } |
| 23 |
| 24 bool WebCryptoDigest::Initialize( |
| 25 const WebKit::WebCryptoAlgorithm& algorithm) { |
| 26 crypto::EnsureNSSInit(); |
| 27 |
| 28 HASH_HashType hash_type = HASH_AlgNULL; |
| 29 |
| 30 switch (algorithm.id()) { |
| 31 case WebKit::WebCryptoAlgorithmIdSha1: |
| 32 hash_type = HASH_AlgSHA1; |
| 33 break; |
| 34 case WebKit::WebCryptoAlgorithmIdSha224: |
| 35 hash_type = HASH_AlgSHA224; |
| 36 break; |
| 37 case WebKit::WebCryptoAlgorithmIdSha256: |
| 38 hash_type = HASH_AlgSHA256; |
| 39 break; |
| 40 case WebKit::WebCryptoAlgorithmIdSha384: |
| 41 hash_type = HASH_AlgSHA384; |
| 42 break; |
| 43 case WebKit::WebCryptoAlgorithmIdSha512: |
| 44 hash_type = HASH_AlgSHA512; |
| 45 break; |
| 46 default: |
| 47 NOTREACHED(); |
| 48 return false; |
| 49 } |
| 50 |
| 51 context_.reset(HASH_Create(hash_type)); |
| 52 if (!context_) { |
| 53 LOG(ERROR) << "Could not create digest context for hash algorithm: " |
| 54 << hash_type; |
| 55 return false; |
| 56 } |
| 57 |
| 58 HASH_Begin(context_.get()); |
| 59 |
| 60 return true; |
| 61 } |
| 62 |
| 63 void WebCryptoDigest::process(const unsigned char* bytes, size_t size) { |
| 64 DCHECK(context_); |
| 65 HASH_Update(context_.get(), bytes, size); |
| 66 } |
| 67 |
| 68 void WebCryptoDigest::abort() { |
| 69 delete this; |
| 70 } |
| 71 |
| 72 void WebCryptoDigest::finish() { |
| 73 DCHECK(context_); |
| 74 |
| 75 unsigned int hash_result_length = HASH_ResultLenContext(context_.get()); |
| 76 WebKit::WebArrayBuffer buffer( |
| 77 WebKit::WebArrayBuffer::create(hash_result_length, 1)); |
| 78 |
| 79 unsigned char* digest = reinterpret_cast<unsigned char*>(buffer.data()); |
| 80 DCHECK(digest); |
| 81 |
| 82 unsigned int result_length = 0; |
| 83 HASH_End(context_.get(), digest, &result_length, hash_result_length); |
| 84 if (result_length != hash_result_length) { |
| 85 LOG(ERROR) << "Result length invalid; expected " << hash_result_length |
| 86 << ", got " << result_length; |
| 87 result_.completeWithError(); |
| 88 delete this; |
| 89 return; |
| 90 } |
| 91 |
| 92 result_.completeWithArrayBuffer(buffer); |
| 93 |
| 94 delete this; |
| 95 } |
| 96 |
| 97 void WebCryptoDigest::HASHDeleter::operator()(void* ptr) const { |
| 98 HASH_Destroy(static_cast<HASHContext*>(ptr)); |
| 99 } |
| 100 |
| 101 } // namespace content |
OLD | NEW |