comparison hmac_memory.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
10 */ 10 */
11 /* Submited by Dobes Vandermeer ([email protected]) */ 11 /* Submited by Dobes Vandermeer ([email protected]) */
12 12
13 #include "mycrypt.h" 13 #include "mycrypt.h"
14 14
15 /*
16 (1) append zeros to the end of K to create a B byte string
17 (e.g., if K is of length 20 bytes and B=64, then K will be
18 appended with 44 zero bytes 0x00)
19 (2) XOR (bitwise exclusive-OR) the B byte string computed in step
20 (1) with ipad (ipad = the byte 0x36 repeated B times)
21 (3) append the stream of data 'text' to the B byte string resulting
22 from step (2)
23 (4) apply H to the stream generated in step (3)
24 (5) XOR (bitwise exclusive-OR) the B byte string computed in
25 step (1) with opad (opad = the byte 0x5C repeated B times.)
26 (6) append the H result from step (4) to the B byte string
27 resulting from step (5)
28 (7) apply H to the stream generated in step (6) and output
29 the result
30 */
31
32 #ifdef HMAC 15 #ifdef HMAC
33
34 #define HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
35 16
36 int hmac_memory(int hash, const unsigned char *key, unsigned long keylen, 17 int hmac_memory(int hash, const unsigned char *key, unsigned long keylen,
37 const unsigned char *data, unsigned long len, 18 const unsigned char *data, unsigned long len,
38 unsigned char *dst, unsigned long *dstlen) 19 unsigned char *dst, unsigned long *dstlen)
39 { 20 {
40 hmac_state hmac; 21 hmac_state *hmac;
41 int err; 22 int err;
42 23
43 _ARGCHK(key != NULL); 24 _ARGCHK(key != NULL);
44 _ARGCHK(data != NULL); 25 _ARGCHK(data != NULL);
45 _ARGCHK(dst != NULL); 26 _ARGCHK(dst != NULL);
46 _ARGCHK(dstlen != NULL); 27 _ARGCHK(dstlen != NULL);
47 28
48 if((err = hash_is_valid(hash)) != CRYPT_OK) { 29 /* allocate ram for hmac state */
49 return err; 30 hmac = XMALLOC(sizeof(hmac_state));
31 if (hmac == NULL) {
32 return CRYPT_MEM;
50 } 33 }
51 34
52 if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) { 35 if ((err = hmac_init(hmac, hash, key, keylen)) != CRYPT_OK) {
53 return err; 36 goto __ERR;
54 } 37 }
55 38
56 if ((err = hmac_process(&hmac, data, len)) != CRYPT_OK) { 39 if ((err = hmac_process(hmac, data, len)) != CRYPT_OK) {
57 return err; 40 goto __ERR;
58 } 41 }
59 42
60 if ((err = hmac_done(&hmac, dst, dstlen)) != CRYPT_OK) { 43 if ((err = hmac_done(hmac, dst, dstlen)) != CRYPT_OK) {
61 return err; 44 goto __ERR;
62 } 45 }
63 return CRYPT_OK; 46
47 err = CRYPT_OK;
48 __ERR:
49 #ifdef CLEAN_STACK
50 zeromem(hmac, sizeof(hmac_state));
51 #endif
52
53 XFREE(hmac);
54 return err;
64 } 55 }
65 56
66 #endif 57 #endif
67 58