ipc/chromium/src/base/hmac_mac.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // Copyright (c) 2008 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.
     5 #include "base/hmac.h"
     7 #include <CommonCrypto/CommonHMAC.h>
     9 #include "base/logging.h"
    11 namespace base {
    13 struct HMACPlatformData {
    14   std::string key_;
    15 };
    17 HMAC::HMAC(HashAlgorithm hash_alg)
    18     : hash_alg_(hash_alg), plat_(new HMACPlatformData()) {
    19   // Only SHA-1 digest is supported now.
    20   DCHECK(hash_alg_ == SHA1);
    21 }
    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   }
    30   plat_->key_.assign(reinterpret_cast<const char*>(key), key_length);
    32   return true;
    33 }
    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 }
    42 bool HMAC::Sign(const std::string& data,
    43                 unsigned char* digest,
    44                 int digest_length) {
    45   CCHmacAlgorithm algorithm;
    46   int algorithm_digest_length;
    47   switch (hash_alg_) {
    48     case SHA1:
    49       algorithm = kCCHmacAlgSHA1;
    50       algorithm_digest_length = CC_SHA1_DIGEST_LENGTH;
    51       break;
    52     default:
    53       NOTREACHED();
    54       return false;
    55   }
    57   if (digest_length < algorithm_digest_length) {
    58     NOTREACHED();
    59     return false;
    60   }
    62   CCHmac(algorithm,
    63          plat_->key_.data(), plat_->key_.length(), data.data(), data.length(),
    64          digest);
    66   return true;
    67 }
    69 }  // namespace base

mercurial