Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(463)

Side by Side Diff: components/webcrypto/algorithms/hmac.cc

Issue 2202843002: Pass a minimally sized buffer to BoringSSL's HMAC(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address david's feedback Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/hmac.h> 5 #include <openssl/hmac.h>
6 #include <stddef.h> 6 #include <stddef.h>
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 const CryptoData& data, 89 const CryptoData& data,
90 std::vector<uint8_t>* buffer) { 90 std::vector<uint8_t>* buffer) {
91 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 91 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
92 92
93 const EVP_MD* digest_algorithm = GetDigest(hash); 93 const EVP_MD* digest_algorithm = GetDigest(hash);
94 if (!digest_algorithm) 94 if (!digest_algorithm)
95 return Status::ErrorUnsupported(); 95 return Status::ErrorUnsupported();
96 size_t hmac_expected_length = EVP_MD_size(digest_algorithm); 96 size_t hmac_expected_length = EVP_MD_size(digest_algorithm);
97 97
98 buffer->resize(hmac_expected_length); 98 buffer->resize(hmac_expected_length);
99 crypto::ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> hmac_result(
100 buffer->data(), hmac_expected_length);
101 99
102 unsigned int hmac_actual_length; 100 unsigned int hmac_actual_length;
103 unsigned char* const success = 101 if (!HMAC(digest_algorithm, raw_key.data(), raw_key.size(), data.bytes(),
104 HMAC(digest_algorithm, raw_key.data(), raw_key.size(), data.bytes(), 102 data.byte_length(), buffer->data(), &hmac_actual_length)) {
105 data.byte_length(), hmac_result.safe_buffer(), &hmac_actual_length);
106 if (!success || hmac_actual_length != hmac_expected_length)
107 return Status::OperationError(); 103 return Status::OperationError();
104 }
105
106 // HMAC() promises to use at most EVP_MD_CTX_size(). If this was not the
107 // case then memory corruption may have just occurred.
108 CHECK_EQ(hmac_expected_length, hmac_actual_length);
108 109
109 return Status::Success(); 110 return Status::Success();
110 } 111 }
111 112
112 class HmacImplementation : public AlgorithmImplementation { 113 class HmacImplementation : public AlgorithmImplementation {
113 public: 114 public:
114 HmacImplementation() {} 115 HmacImplementation() {}
115 116
116 Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, 117 Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm,
117 bool extractable, 118 bool extractable,
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 } 288 }
288 }; 289 };
289 290
290 } // namespace 291 } // namespace
291 292
292 std::unique_ptr<AlgorithmImplementation> CreateHmacImplementation() { 293 std::unique_ptr<AlgorithmImplementation> CreateHmacImplementation() {
293 return base::WrapUnique(new HmacImplementation); 294 return base::WrapUnique(new HmacImplementation);
294 } 295 }
295 296
296 } // namespace webcrypto 297 } // namespace webcrypto
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698