| 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 <openssl/evp.h> | 5 #include <openssl/digest.h> |
| 6 #include <openssl/sha.h> | |
| 7 #include <stdint.h> | 6 #include <stdint.h> |
| 8 | 7 |
| 9 #include <vector> | 8 #include <vector> |
| 10 | 9 |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 13 #include "components/webcrypto/algorithm_implementation.h" | 12 #include "components/webcrypto/algorithm_implementation.h" |
| 14 #include "components/webcrypto/algorithms/util.h" | 13 #include "components/webcrypto/algorithms/util.h" |
| 15 #include "components/webcrypto/crypto_data.h" | 14 #include "components/webcrypto/crypto_data.h" |
| 16 #include "components/webcrypto/status.h" | 15 #include "components/webcrypto/status.h" |
| 17 #include "crypto/openssl_util.h" | 16 #include "crypto/openssl_util.h" |
| 18 #include "crypto/scoped_openssl_types.h" | |
| 19 | 17 |
| 20 namespace webcrypto { | 18 namespace webcrypto { |
| 21 | 19 |
| 22 namespace { | 20 namespace { |
| 23 | 21 |
| 24 // Implementation of blink::WebCryptoDigester, an internal Blink detail not | 22 // Implementation of blink::WebCryptoDigester, an internal Blink detail not |
| 25 // part of WebCrypto, that allows chunks of data to be streamed in before | 23 // part of WebCrypto, that allows chunks of data to be streamed in before |
| 26 // computing a SHA-* digest (as opposed to ShaImplementation, which computes | 24 // computing a SHA-* digest (as opposed to ShaImplementation, which computes |
| 27 // digests over complete messages) | 25 // digests over complete messages) |
| 28 class DigestorImpl : public blink::WebCryptoDigestor { | 26 class DigestorImpl : public blink::WebCryptoDigestor { |
| 29 public: | 27 public: |
| 30 explicit DigestorImpl(blink::WebCryptoAlgorithmId algorithm_id) | 28 explicit DigestorImpl(blink::WebCryptoAlgorithmId algorithm_id) |
| 31 : initialized_(false), | 29 : initialized_(false), |
| 32 digest_context_(EVP_MD_CTX_create()), | |
| 33 algorithm_id_(algorithm_id) {} | 30 algorithm_id_(algorithm_id) {} |
| 34 | 31 |
| 35 bool consume(const unsigned char* data, unsigned int size) override { | 32 bool consume(const unsigned char* data, unsigned int size) override { |
| 36 return ConsumeWithStatus(data, size).IsSuccess(); | 33 return ConsumeWithStatus(data, size).IsSuccess(); |
| 37 } | 34 } |
| 38 | 35 |
| 39 Status ConsumeWithStatus(const unsigned char* data, unsigned int size) { | 36 Status ConsumeWithStatus(const unsigned char* data, unsigned int size) { |
| 40 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 37 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 41 Status error = Init(); | 38 Status error = Init(); |
| 42 if (!error.IsSuccess()) | 39 if (!error.IsSuccess()) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 66 | 63 |
| 67 private: | 64 private: |
| 68 Status Init() { | 65 Status Init() { |
| 69 if (initialized_) | 66 if (initialized_) |
| 70 return Status::Success(); | 67 return Status::Success(); |
| 71 | 68 |
| 72 const EVP_MD* digest_algorithm = GetDigest(algorithm_id_); | 69 const EVP_MD* digest_algorithm = GetDigest(algorithm_id_); |
| 73 if (!digest_algorithm) | 70 if (!digest_algorithm) |
| 74 return Status::ErrorUnsupported(); | 71 return Status::ErrorUnsupported(); |
| 75 | 72 |
| 76 if (!digest_context_.get()) | |
| 77 return Status::OperationError(); | |
| 78 | |
| 79 if (!EVP_DigestInit_ex(digest_context_.get(), digest_algorithm, NULL)) | 73 if (!EVP_DigestInit_ex(digest_context_.get(), digest_algorithm, NULL)) |
| 80 return Status::OperationError(); | 74 return Status::OperationError(); |
| 81 | 75 |
| 82 initialized_ = true; | 76 initialized_ = true; |
| 83 return Status::Success(); | 77 return Status::Success(); |
| 84 } | 78 } |
| 85 | 79 |
| 86 Status FinishInternal(unsigned char* result, unsigned int* result_size) { | 80 Status FinishInternal(unsigned char* result, unsigned int* result_size) { |
| 87 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 81 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 88 Status error = Init(); | 82 Status error = Init(); |
| 89 if (!error.IsSuccess()) | 83 if (!error.IsSuccess()) |
| 90 return error; | 84 return error; |
| 91 | 85 |
| 92 const size_t hash_expected_size = EVP_MD_CTX_size(digest_context_.get()); | 86 const size_t hash_expected_size = EVP_MD_CTX_size(digest_context_.get()); |
| 93 if (hash_expected_size == 0) | 87 if (hash_expected_size == 0) |
| 94 return Status::ErrorUnexpected(); | 88 return Status::ErrorUnexpected(); |
| 95 DCHECK_LE(hash_expected_size, static_cast<unsigned>(EVP_MAX_MD_SIZE)); | 89 DCHECK_LE(hash_expected_size, static_cast<unsigned>(EVP_MAX_MD_SIZE)); |
| 96 | 90 |
| 97 if (!EVP_DigestFinal_ex(digest_context_.get(), result, result_size) || | 91 if (!EVP_DigestFinal_ex(digest_context_.get(), result, result_size) || |
| 98 *result_size != hash_expected_size) | 92 *result_size != hash_expected_size) |
| 99 return Status::OperationError(); | 93 return Status::OperationError(); |
| 100 | 94 |
| 101 return Status::Success(); | 95 return Status::Success(); |
| 102 } | 96 } |
| 103 | 97 |
| 104 bool initialized_; | 98 bool initialized_; |
| 105 crypto::ScopedEVP_MD_CTX digest_context_; | 99 bssl::ScopedEVP_MD_CTX digest_context_; |
| 106 blink::WebCryptoAlgorithmId algorithm_id_; | 100 blink::WebCryptoAlgorithmId algorithm_id_; |
| 107 unsigned char result_[EVP_MAX_MD_SIZE]; | 101 unsigned char result_[EVP_MAX_MD_SIZE]; |
| 108 }; | 102 }; |
| 109 | 103 |
| 110 class ShaImplementation : public AlgorithmImplementation { | 104 class ShaImplementation : public AlgorithmImplementation { |
| 111 public: | 105 public: |
| 112 Status Digest(const blink::WebCryptoAlgorithm& algorithm, | 106 Status Digest(const blink::WebCryptoAlgorithm& algorithm, |
| 113 const CryptoData& data, | 107 const CryptoData& data, |
| 114 std::vector<uint8_t>* buffer) const override { | 108 std::vector<uint8_t>* buffer) const override { |
| 115 DigestorImpl digestor(algorithm.id()); | 109 DigestorImpl digestor(algorithm.id()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 127 std::unique_ptr<AlgorithmImplementation> CreateShaImplementation() { | 121 std::unique_ptr<AlgorithmImplementation> CreateShaImplementation() { |
| 128 return base::MakeUnique<ShaImplementation>(); | 122 return base::MakeUnique<ShaImplementation>(); |
| 129 } | 123 } |
| 130 | 124 |
| 131 std::unique_ptr<blink::WebCryptoDigestor> CreateDigestorImplementation( | 125 std::unique_ptr<blink::WebCryptoDigestor> CreateDigestorImplementation( |
| 132 blink::WebCryptoAlgorithmId algorithm) { | 126 blink::WebCryptoAlgorithmId algorithm) { |
| 133 return std::unique_ptr<blink::WebCryptoDigestor>(new DigestorImpl(algorithm)); | 127 return std::unique_ptr<blink::WebCryptoDigestor>(new DigestorImpl(algorithm)); |
| 134 } | 128 } |
| 135 | 129 |
| 136 } // namespace webcrypto | 130 } // namespace webcrypto |
| OLD | NEW |