Chromium Code Reviews| 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/evp.h> |
| 6 #include <openssl/sha.h> | 6 #include <openssl/sha.h> |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 bool finish(unsigned char*& result_data, | 49 bool finish(unsigned char*& result_data, |
| 50 unsigned int& result_data_size) override { | 50 unsigned int& result_data_size) override { |
| 51 Status error = FinishInternal(result_, &result_data_size); | 51 Status error = FinishInternal(result_, &result_data_size); |
| 52 if (!error.IsSuccess()) | 52 if (!error.IsSuccess()) |
| 53 return false; | 53 return false; |
| 54 result_data = result_; | 54 result_data = result_; |
| 55 return true; | 55 return true; |
| 56 } | 56 } |
| 57 | 57 |
| 58 Status FinishWithVectorAndStatus(std::vector<uint8_t>* result) { | 58 Status FinishWithVectorAndStatus(std::vector<uint8_t>* result) { |
| 59 const int hash_expected_size = EVP_MD_CTX_size(digest_context_.get()); | 59 const size_t hash_expected_size = EVP_MD_CTX_size(digest_context_.get()); |
| 60 result->resize(hash_expected_size); | 60 result->resize(hash_expected_size); |
| 61 unsigned int hash_buffer_size; // ignored | 61 unsigned int hash_buffer_size; // ignored |
| 62 return FinishInternal(result->data(), &hash_buffer_size); | 62 return FinishInternal(result->data(), &hash_buffer_size); |
| 63 } | 63 } |
| 64 | 64 |
| 65 private: | 65 private: |
| 66 Status Init() { | 66 Status Init() { |
| 67 if (initialized_) | 67 if (initialized_) |
| 68 return Status::Success(); | 68 return Status::Success(); |
| 69 | 69 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 80 initialized_ = true; | 80 initialized_ = true; |
| 81 return Status::Success(); | 81 return Status::Success(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 Status FinishInternal(unsigned char* result, unsigned int* result_size) { | 84 Status FinishInternal(unsigned char* result, unsigned int* result_size) { |
| 85 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 85 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 86 Status error = Init(); | 86 Status error = Init(); |
| 87 if (!error.IsSuccess()) | 87 if (!error.IsSuccess()) |
| 88 return error; | 88 return error; |
| 89 | 89 |
| 90 const int hash_expected_size = EVP_MD_CTX_size(digest_context_.get()); | 90 const size_t hash_expected_size = EVP_MD_CTX_size(digest_context_.get()); |
| 91 if (hash_expected_size <= 0) | 91 if (hash_expected_size == 0) |
| 92 return Status::ErrorUnexpected(); | 92 return Status::ErrorUnexpected(); |
| 93 DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE); | 93 DCHECK_LE(hash_expected_size, static_cast<unsigned>(EVP_MAX_MD_SIZE)); |
|
davidben
2016/04/21 00:17:09
This is kind of depressing. :-/ EVP_MAX_MD_SIZE is
| |
| 94 | 94 |
| 95 if (!EVP_DigestFinal_ex(digest_context_.get(), result, result_size) || | 95 if (!EVP_DigestFinal_ex(digest_context_.get(), result, result_size) || |
| 96 static_cast<int>(*result_size) != hash_expected_size) | 96 *result_size != hash_expected_size) |
| 97 return Status::OperationError(); | 97 return Status::OperationError(); |
| 98 | 98 |
| 99 return Status::Success(); | 99 return Status::Success(); |
| 100 } | 100 } |
| 101 | 101 |
| 102 bool initialized_; | 102 bool initialized_; |
| 103 crypto::ScopedEVP_MD_CTX digest_context_; | 103 crypto::ScopedEVP_MD_CTX digest_context_; |
| 104 blink::WebCryptoAlgorithmId algorithm_id_; | 104 blink::WebCryptoAlgorithmId algorithm_id_; |
| 105 unsigned char result_[EVP_MAX_MD_SIZE]; | 105 unsigned char result_[EVP_MAX_MD_SIZE]; |
| 106 }; | 106 }; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 125 scoped_ptr<AlgorithmImplementation> CreateShaImplementation() { | 125 scoped_ptr<AlgorithmImplementation> CreateShaImplementation() { |
| 126 return make_scoped_ptr(new ShaImplementation()); | 126 return make_scoped_ptr(new ShaImplementation()); |
| 127 } | 127 } |
| 128 | 128 |
| 129 scoped_ptr<blink::WebCryptoDigestor> CreateDigestorImplementation( | 129 scoped_ptr<blink::WebCryptoDigestor> CreateDigestorImplementation( |
| 130 blink::WebCryptoAlgorithmId algorithm) { | 130 blink::WebCryptoAlgorithmId algorithm) { |
| 131 return scoped_ptr<blink::WebCryptoDigestor>(new DigestorImpl(algorithm)); | 131 return scoped_ptr<blink::WebCryptoDigestor>(new DigestorImpl(algorithm)); |
| 132 } | 132 } |
| 133 | 133 |
| 134 } // namespace webcrypto | 134 } // namespace webcrypto |
| OLD | NEW |