• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 <stddef.h>
8 
9 #include <algorithm>
10 #include <string>
11 
12 #include "base/logging.h"
13 #include "base/stl_util.h"
14 #include "crypto/openssl_util.h"
15 #include "crypto/secure_util.h"
16 #include "crypto/symmetric_key.h"
17 #include "third_party/boringssl/src/include/openssl/hmac.h"
18 
19 namespace crypto {
20 
HMAC(HashAlgorithm hash_alg)21 HMAC::HMAC(HashAlgorithm hash_alg) : hash_alg_(hash_alg), initialized_(false) {
22   // Only SHA-1 and SHA-256 hash algorithms are supported now.
23   DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256);
24 }
25 
~HMAC()26 HMAC::~HMAC() {
27   // Zero out key copy.
28   key_.assign(key_.size(), 0);
29   base::STLClearObject(&key_);
30 }
31 
DigestLength() const32 size_t HMAC::DigestLength() const {
33   switch (hash_alg_) {
34     case SHA1:
35       return 20;
36     case SHA256:
37       return 32;
38     default:
39       NOTREACHED();
40       return 0;
41   }
42 }
43 
Init(const unsigned char * key,size_t key_length)44 bool HMAC::Init(const unsigned char* key, size_t key_length) {
45   // Init must not be called more than once on the same HMAC object.
46   DCHECK(!initialized_);
47   initialized_ = true;
48   key_.assign(key, key + key_length);
49   return true;
50 }
51 
Init(SymmetricKey * key)52 bool HMAC::Init(SymmetricKey* key) {
53   std::string raw_key;
54   bool result = key->GetRawKey(&raw_key) && Init(raw_key);
55   // Zero out key copy.  This might get optimized away, but one can hope.
56   // Using std::string to store key info at all is a larger problem.
57   std::fill(raw_key.begin(), raw_key.end(), 0);
58   return result;
59 }
60 
Sign(const base::StringPiece & data,unsigned char * digest,size_t digest_length) const61 bool HMAC::Sign(const base::StringPiece& data,
62                 unsigned char* digest,
63                 size_t digest_length) const {
64   DCHECK(initialized_);
65 
66   ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length);
67   return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), key_.data(),
68                   key_.size(),
69                   reinterpret_cast<const unsigned char*>(data.data()),
70                   data.size(), result.safe_buffer(), nullptr);
71 }
72 
Verify(const base::StringPiece & data,const base::StringPiece & digest) const73 bool HMAC::Verify(const base::StringPiece& data,
74                   const base::StringPiece& digest) const {
75   if (digest.size() != DigestLength())
76     return false;
77   return VerifyTruncated(data, digest);
78 }
79 
VerifyTruncated(const base::StringPiece & data,const base::StringPiece & digest) const80 bool HMAC::VerifyTruncated(const base::StringPiece& data,
81                            const base::StringPiece& digest) const {
82   if (digest.empty())
83     return false;
84   size_t digest_length = DigestLength();
85   std::unique_ptr<unsigned char[]> computed_digest(
86       new unsigned char[digest_length]);
87   if (!Sign(data, computed_digest.get(), digest_length))
88     return false;
89 
90   return SecureMemEqual(digest.data(), computed_digest.get(),
91                         std::min(digest.size(), digest_length));
92 }
93 
94 }  // namespace crypto
95