Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "crypto/hmac.h" | 5 #include "crypto/hmac.h" |
| 6 | 6 |
| 7 #include <openssl/hmac.h> | 7 #include <openssl/hmac.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { | 24 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) { |
| 25 // Only SHA-1 and SHA-256 hash algorithms are supported now. | 25 // Only SHA-1 and SHA-256 hash algorithms are supported now. |
| 26 DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256); | 26 DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256); |
| 27 } | 27 } |
| 28 | 28 |
| 29 bool HMAC::Init(const unsigned char* key, size_t key_length) { | 29 bool HMAC::Init(const unsigned char* key, size_t key_length) { |
| 30 // Init must not be called more than once on the same HMAC object. | 30 // Init must not be called more than once on the same HMAC object. |
| 31 DCHECK(plat_->key.empty()); | 31 DCHECK(plat_->key.empty()); |
| 32 | 32 |
| 33 plat_->key.assign(key, key + key_length); | 33 plat_->key.assign(key, key + key_length); |
| 34 if (key_length == 0) { | 34 if (key_length == 0) { |
|
davidben
2015/03/04 19:40:59
Hrm. This seems to conflict with the documentation
| |
| 35 // Special-case: if the key is empty, use a key with one zero | 35 // HMAC::Sign() assumes that plat_->key is not empty. |
| 36 // byte. OpenSSL's HMAC function breaks when passed a NULL key. (It calls | 36 // HMAC pads keys with zeros, so this key is equivalent. |
|
davidben
2015/03/04 19:40:59
Hrm. This seems to only be relevant for the DCHECK
| |
| 37 // HMAC_Init_ex which treats a NULL key as having already been initialized | |
| 38 // with a key previously.) HMAC pads keys with zeros, so this key is | |
| 39 // equivalent. | |
| 40 plat_->key.push_back(0); | 37 plat_->key.push_back(0); |
| 41 } | 38 } |
| 42 return true; | 39 return true; |
| 43 } | 40 } |
| 44 | 41 |
| 45 HMAC::~HMAC() { | 42 HMAC::~HMAC() { |
| 46 // Zero out key copy. | 43 // Zero out key copy. |
| 47 plat_->key.assign(plat_->key.size(), 0); | 44 plat_->key.assign(plat_->key.size(), 0); |
| 48 STLClearObject(&plat_->key); | 45 STLClearObject(&plat_->key); |
| 49 } | 46 } |
| 50 | 47 |
| 51 bool HMAC::Sign(const base::StringPiece& data, | 48 bool HMAC::Sign(const base::StringPiece& data, |
| 52 unsigned char* digest, | 49 unsigned char* digest, |
| 53 size_t digest_length) const { | 50 size_t digest_length) const { |
| 54 DCHECK(!plat_->key.empty()); // Init must be called before Sign. | 51 DCHECK(!plat_->key.empty()); // Init must be called before Sign. |
| 55 | 52 |
| 56 ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length); | 53 ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length); |
| 57 return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), | 54 return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), |
| 58 &plat_->key[0], plat_->key.size(), | 55 &plat_->key[0], plat_->key.size(), |
| 59 reinterpret_cast<const unsigned char*>(data.data()), | 56 reinterpret_cast<const unsigned char*>(data.data()), |
| 60 data.size(), | 57 data.size(), |
| 61 result.safe_buffer(), NULL); | 58 result.safe_buffer(), NULL); |
| 62 } | 59 } |
| 63 | 60 |
| 64 } // namespace crypto | 61 } // namespace crypto |
| OLD | NEW |