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

Side by Side Diff: crypto/hmac_mac.cc

Issue 10543146: Use NSS for symmetric key crypto operations on Windows and Mac. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: removed DEPS; rebased Created 8 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « crypto/encryptor_win.cc ('k') | crypto/symmetric_key.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "crypto/hmac.h"
6
7 #include <CommonCrypto/CommonHMAC.h>
8
9 #include "base/logging.h"
10
11 namespace crypto {
12
13 struct HMACPlatformData {
14 std::string key_;
15 };
16
17 HMAC::HMAC(HashAlgorithm hash_alg)
18 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) {
19 // Only SHA-1 and SHA-256 hash algorithms are supported now.
20 DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256);
21 }
22
23 bool HMAC::Init(const unsigned char *key, int key_length) {
24 if (!plat_->key_.empty()) {
25 // Init must not be called more than once on the same HMAC object.
26 NOTREACHED();
27 return false;
28 }
29
30 plat_->key_.assign(reinterpret_cast<const char*>(key), key_length);
31
32 return true;
33 }
34
35 HMAC::~HMAC() {
36 // Zero out key copy.
37 plat_->key_.assign(plat_->key_.length(), std::string::value_type());
38 plat_->key_.clear();
39 plat_->key_.reserve(0);
40 }
41
42 bool HMAC::Sign(const base::StringPiece& data,
43 unsigned char* digest,
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
51 CCHmacAlgorithm algorithm;
52 int algorithm_digest_length;
53 switch (hash_alg_) {
54 case SHA1:
55 algorithm = kCCHmacAlgSHA1;
56 algorithm_digest_length = CC_SHA1_DIGEST_LENGTH;
57 break;
58 case SHA256:
59 algorithm = kCCHmacAlgSHA256;
60 algorithm_digest_length = CC_SHA256_DIGEST_LENGTH;
61 break;
62 default:
63 NOTREACHED();
64 return false;
65 }
66
67 if (digest_length < algorithm_digest_length) {
68 NOTREACHED();
69 return false;
70 }
71
72 CCHmac(algorithm,
73 plat_->key_.data(), plat_->key_.length(), data.data(), data.length(),
74 digest);
75
76 return true;
77 }
78
79 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/encryptor_win.cc ('k') | crypto/symmetric_key.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698