LCOV - code coverage report
Current view: top level - util/crypto/nss - nss_hmac_sha1.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 26 28 92.9 %
Date: 2016-06-29 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :     Authors:
       3             :         Jan Cholasta <jcholast@redhat.com>
       4             : 
       5             :     Copyright (C) 2012 Red Hat
       6             : 
       7             :     This program is free software; you can redistribute it and/or modify
       8             :     it under the terms of the GNU General Public License as published by
       9             :     the Free Software Foundation; either version 3 of the License, or
      10             :     (at your option) any later version.
      11             : 
      12             :     This program is distributed in the hope that it will be useful,
      13             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :     GNU General Public License for more details.
      16             : 
      17             :     You should have received a copy of the GNU General Public License
      18             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : /*
      21             :     NSS does not provide public API for HMAC, so we implement it ourselves.
      22             : 
      23             :     See RFC 2104 for details on the algorithm.
      24             : */
      25             : 
      26             : #include "util/util.h"
      27             : #include "util/crypto/sss_crypto.h"
      28             : #include "util/crypto/nss/nss_util.h"
      29             : 
      30             : #include <sechash.h>
      31             : 
      32             : #define HMAC_SHA1_BLOCKSIZE 64
      33             : 
      34           3 : int sss_hmac_sha1(const unsigned char *key,
      35             :                   size_t key_len,
      36             :                   const unsigned char *in,
      37             :                   size_t in_len,
      38             :                   unsigned char *out)
      39             : {
      40             :     int ret;
      41             :     unsigned char ikey[HMAC_SHA1_BLOCKSIZE], okey[HMAC_SHA1_BLOCKSIZE];
      42             :     size_t i;
      43             :     HASHContext *sha1;
      44             :     unsigned char hash[SSS_SHA1_LENGTH];
      45             :     unsigned int res_len;
      46             : 
      47           3 :     ret = nspr_nss_init();
      48           3 :     if (ret != EOK) {
      49           0 :         return ret;
      50             :     }
      51             : 
      52           3 :     sha1 = HASH_Create(HASH_AlgSHA1);
      53           3 :     if (!sha1) {
      54           0 :         return ENOMEM;
      55             :     }
      56             : 
      57           3 :     if (key_len > HMAC_SHA1_BLOCKSIZE) {
      58             :         /* keys longer than blocksize are shortened */
      59           1 :         HASH_Begin(sha1);
      60           1 :         HASH_Update(sha1, key, key_len);
      61           1 :         HASH_End(sha1, ikey, &res_len, SSS_SHA1_LENGTH);
      62           1 :         memset(ikey + SSS_SHA1_LENGTH, 0, HMAC_SHA1_BLOCKSIZE - SSS_SHA1_LENGTH);
      63             :     } else {
      64             :         /* keys shorter than blocksize are zero-padded */
      65           2 :         memcpy(ikey, key, key_len);
      66           2 :         if (key_len != HMAC_SHA1_BLOCKSIZE) {
      67           1 :             memset(ikey + key_len, 0, HMAC_SHA1_BLOCKSIZE - key_len);
      68             :         }
      69             :     }
      70             : 
      71             :     /* HMAC(key, msg) = HASH(key XOR opad, HASH(key XOR ipad, msg)) */
      72         195 :     for (i = 0; i < HMAC_SHA1_BLOCKSIZE; i++) {
      73         192 :         okey[i] = ikey[i] ^ 0x5c;
      74         192 :         ikey[i] ^= 0x36;
      75             :     }
      76             : 
      77           3 :     HASH_Begin(sha1);
      78           3 :     HASH_Update(sha1, ikey, HMAC_SHA1_BLOCKSIZE);
      79           3 :     HASH_Update(sha1, in, in_len);
      80           3 :     HASH_End(sha1, hash, &res_len, SSS_SHA1_LENGTH);
      81             : 
      82           3 :     HASH_Begin(sha1);
      83           3 :     HASH_Update(sha1, okey, HMAC_SHA1_BLOCKSIZE);
      84           3 :     HASH_Update(sha1, hash, SSS_SHA1_LENGTH);
      85           3 :     HASH_End(sha1, out, &res_len, SSS_SHA1_LENGTH);
      86             : 
      87           3 :     HASH_Destroy(sha1);
      88             : 
      89           3 :     return EOK;
      90             : }

Generated by: LCOV version 1.10