Mercurial > dropbear
comparison hmac_init.c @ 143:5d99163f7e32 libtomcrypt-orig
import of libtomcrypt 0.99
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 19 Dec 2004 11:34:45 +0000 |
parents | 7faae8f46238 |
children |
comparison
equal
deleted
inserted
replaced
15:6362d3854bb4 | 143:5d99163f7e32 |
---|---|
33 | 33 |
34 #define HMAC_BLOCKSIZE hash_descriptor[hash].blocksize | 34 #define HMAC_BLOCKSIZE hash_descriptor[hash].blocksize |
35 | 35 |
36 int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen) | 36 int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen) |
37 { | 37 { |
38 unsigned char buf[MAXBLOCKSIZE]; | 38 unsigned char *buf; |
39 unsigned long hashsize; | 39 unsigned long hashsize; |
40 unsigned long i, z; | 40 unsigned long i, z; |
41 int err; | 41 int err; |
42 | 42 |
43 _ARGCHK(hmac != NULL); | 43 _ARGCHK(hmac != NULL); |
44 _ARGCHK(key != NULL); | 44 _ARGCHK(key != NULL); |
45 | 45 |
46 /* valid hash? */ | |
46 if ((err = hash_is_valid(hash)) != CRYPT_OK) { | 47 if ((err = hash_is_valid(hash)) != CRYPT_OK) { |
47 return err; | 48 return err; |
48 } | 49 } |
50 hmac->hash = hash; | |
51 hashsize = hash_descriptor[hash].hashsize; | |
49 | 52 |
50 /* valid key length? */ | 53 /* valid key length? */ |
51 if (keylen == 0) { | 54 if (keylen == 0) { |
52 return CRYPT_INVALID_KEYSIZE; | 55 return CRYPT_INVALID_KEYSIZE; |
53 } | 56 } |
54 | 57 |
55 hmac->hash = hash; | 58 /* allocate ram for buf */ |
59 buf = XMALLOC(HMAC_BLOCKSIZE); | |
60 if (buf == NULL) { | |
61 return CRYPT_MEM; | |
62 } | |
56 | 63 |
57 // (1) make sure we have a large enough key | 64 /* allocate memory for key */ |
58 hashsize = hash_descriptor[hash].hashsize; | 65 hmac->key = XMALLOC(HMAC_BLOCKSIZE); |
66 if (hmac->key == NULL) { | |
67 XFREE(buf); | |
68 return CRYPT_MEM; | |
69 } | |
70 | |
71 /* (1) make sure we have a large enough key */ | |
59 if(keylen > HMAC_BLOCKSIZE) { | 72 if(keylen > HMAC_BLOCKSIZE) { |
60 z = (unsigned long)sizeof(hmac->key); | 73 z = HMAC_BLOCKSIZE; |
61 if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) { | 74 if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) { |
62 return err; | 75 goto __ERR; |
63 } | 76 } |
64 if(hashsize < HMAC_BLOCKSIZE) { | 77 if(hashsize < HMAC_BLOCKSIZE) { |
65 zeromem((hmac->key) + hashsize, (size_t)(HMAC_BLOCKSIZE - hashsize)); | 78 zeromem((hmac->key) + hashsize, (size_t)(HMAC_BLOCKSIZE - hashsize)); |
66 } | 79 } |
67 keylen = hashsize; | 80 keylen = hashsize; |
68 } else { | 81 } else { |
69 memcpy(hmac->key, key, (size_t)keylen); | 82 XMEMCPY(hmac->key, key, (size_t)keylen); |
70 if(keylen < HMAC_BLOCKSIZE) { | 83 if(keylen < HMAC_BLOCKSIZE) { |
71 zeromem((hmac->key) + keylen, (size_t)(HMAC_BLOCKSIZE - keylen)); | 84 zeromem((hmac->key) + keylen, (size_t)(HMAC_BLOCKSIZE - keylen)); |
72 } | 85 } |
73 } | 86 } |
74 | 87 |
75 // Create the initial vector for step (3) | 88 /* Create the initial vector for step (3) */ |
76 for(i=0; i < HMAC_BLOCKSIZE; i++) { | 89 for(i=0; i < HMAC_BLOCKSIZE; i++) { |
77 buf[i] = hmac->key[i] ^ 0x36; | 90 buf[i] = hmac->key[i] ^ 0x36; |
78 } | 91 } |
79 | 92 |
80 // Pre-pend that to the hash data | 93 /* Pre-pend that to the hash data */ |
81 hash_descriptor[hash].init(&hmac->md); | 94 if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) { |
82 hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE); | 95 goto __ERR; |
96 } | |
83 | 97 |
84 return CRYPT_OK; | 98 if ((err = hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE)) != CRYPT_OK) { |
99 goto __ERR; | |
100 } | |
101 goto done; | |
102 __ERR: | |
103 /* free the key since we failed */ | |
104 XFREE(hmac->key); | |
105 done: | |
106 #ifdef CLEAN_STACK | |
107 zeromem(buf, HMAC_BLOCKSIZE); | |
108 #endif | |
109 | |
110 XFREE(buf); | |
111 return err; | |
85 } | 112 } |
86 | 113 |
87 #endif | 114 #endif |