comparison libtomcrypt/src/mac/hmac/hmac_init.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children 1ff2a1034c52
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 #include "tomcrypt.h" 9 #include "tomcrypt.h"
12 10
13 /** 11 /**
14 @file hmac_init.c 12 @file hmac_init.c
15 LTC_HMAC support, initialize state, Tom St Denis/Dobes Vandermeer 13 HMAC support, initialize state, Tom St Denis/Dobes Vandermeer
16 */ 14 */
17 15
18 #ifdef LTC_HMAC 16 #ifdef LTC_HMAC
19 17
20 #define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize 18 #define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
21 19
22 /** 20 /**
23 Initialize an LTC_HMAC context. 21 Initialize an HMAC context.
24 @param hmac The LTC_HMAC state 22 @param hmac The HMAC state
25 @param hash The index of the hash you want to use 23 @param hash The index of the hash you want to use
26 @param key The secret key 24 @param key The secret key
27 @param keylen The length of the secret key (octets) 25 @param keylen The length of the secret key (octets)
28 @return CRYPT_OK if successful 26 @return CRYPT_OK if successful
29 */ 27 */
59 if(keylen > LTC_HMAC_BLOCKSIZE) { 57 if(keylen > LTC_HMAC_BLOCKSIZE) {
60 z = LTC_HMAC_BLOCKSIZE; 58 z = LTC_HMAC_BLOCKSIZE;
61 if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) { 59 if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) {
62 goto LBL_ERR; 60 goto LBL_ERR;
63 } 61 }
64 if(hashsize < LTC_HMAC_BLOCKSIZE) {
65 zeromem((hmac->key) + hashsize, (size_t)(LTC_HMAC_BLOCKSIZE - hashsize));
66 }
67 keylen = hashsize; 62 keylen = hashsize;
68 } else { 63 } else {
69 XMEMCPY(hmac->key, key, (size_t)keylen); 64 XMEMCPY(hmac->key, key, (size_t)keylen);
65 }
66
70 if(keylen < LTC_HMAC_BLOCKSIZE) { 67 if(keylen < LTC_HMAC_BLOCKSIZE) {
71 zeromem((hmac->key) + keylen, (size_t)(LTC_HMAC_BLOCKSIZE - keylen)); 68 zeromem((hmac->key) + keylen, (size_t)(LTC_HMAC_BLOCKSIZE - keylen));
72 } 69 }
73 }
74 70
75 /* Create the initial vector for step (3) */ 71 /* Create the initialization vector for step (3) */
76 for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) { 72 for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) {
77 buf[i] = hmac->key[i] ^ 0x36; 73 buf[i] = hmac->key[i] ^ 0x36;
78 } 74 }
79 75
80 /* Pre-pend that to the hash data */ 76 /* Pre-pend that to the hash data */
97 return err; 93 return err;
98 } 94 }
99 95
100 #endif 96 #endif
101 97
102 /* $Source$ */ 98 /* ref: $Format:%D$ */
103 /* $Revision$ */ 99 /* git commit: $Format:%H$ */
104 /* $Date$ */ 100 /* commit time: $Format:%ai$ */