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 <CommonCrypto/CommonHMAC.h> | 7 #include <CommonCrypto/CommonHMAC.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... |
35 HMAC::~HMAC() { | 35 HMAC::~HMAC() { |
36 // Zero out key copy. | 36 // Zero out key copy. |
37 plat_->key_.assign(plat_->key_.length(), std::string::value_type()); | 37 plat_->key_.assign(plat_->key_.length(), std::string::value_type()); |
38 plat_->key_.clear(); | 38 plat_->key_.clear(); |
39 plat_->key_.reserve(0); | 39 plat_->key_.reserve(0); |
40 } | 40 } |
41 | 41 |
42 bool HMAC::Sign(const base::StringPiece& data, | 42 bool HMAC::Sign(const base::StringPiece& data, |
43 unsigned char* digest, | 43 unsigned char* digest, |
44 int digest_length) const { | 44 int digest_length) const { |
| 45 if (plat_->key_.empty()) { |
| 46 // Init has not been called or has failed. |
| 47 NOTREACHED(); |
| 48 return false; |
| 49 } |
| 50 |
45 CCHmacAlgorithm algorithm; | 51 CCHmacAlgorithm algorithm; |
46 int algorithm_digest_length; | 52 int algorithm_digest_length; |
47 switch (hash_alg_) { | 53 switch (hash_alg_) { |
48 case SHA1: | 54 case SHA1: |
49 algorithm = kCCHmacAlgSHA1; | 55 algorithm = kCCHmacAlgSHA1; |
50 algorithm_digest_length = CC_SHA1_DIGEST_LENGTH; | 56 algorithm_digest_length = CC_SHA1_DIGEST_LENGTH; |
51 break; | 57 break; |
52 case SHA256: | 58 case SHA256: |
53 algorithm = kCCHmacAlgSHA256; | 59 algorithm = kCCHmacAlgSHA256; |
54 algorithm_digest_length = CC_SHA256_DIGEST_LENGTH; | 60 algorithm_digest_length = CC_SHA256_DIGEST_LENGTH; |
55 break; | 61 break; |
56 default: | 62 default: |
57 NOTREACHED(); | 63 NOTREACHED(); |
58 return false; | 64 return false; |
59 } | 65 } |
60 | 66 |
61 if (digest_length < algorithm_digest_length) { | 67 if (digest_length < algorithm_digest_length) { |
62 NOTREACHED(); | 68 NOTREACHED(); |
63 return false; | 69 return false; |
64 } | 70 } |
65 | 71 |
66 CCHmac(algorithm, | 72 CCHmac(algorithm, |
67 plat_->key_.data(), plat_->key_.length(), data.data(), data.length(), | 73 plat_->key_.data(), plat_->key_.length(), data.data(), data.length(), |
68 digest); | 74 digest); |
69 | 75 |
70 return true; | 76 return true; |
71 } | 77 } |
72 | 78 |
73 } // namespace crypto | 79 } // namespace crypto |
OLD | NEW |