comparison hmac_done.c @ 0:d7da3b1e1540 libtomcrypt

put back the 0.95 makefile which was inadvertently merged over
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:21:40 +0000
parents
children 5d99163f7e32
comparison
equal deleted inserted replaced
-1:000000000000 0:d7da3b1e1540
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11 /* Submited by Dobes Vandermeer ([email protected]) */
12
13 #include "mycrypt.h"
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
33
34 #define HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
35
36 int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen)
37 {
38 unsigned char buf[MAXBLOCKSIZE];
39 unsigned char isha[MAXBLOCKSIZE];
40 unsigned long hashsize, i;
41 int hash, err;
42
43 _ARGCHK(hmac != NULL);
44 _ARGCHK(hashOut != NULL);
45
46 hash = hmac->hash;
47 if((err = hash_is_valid(hash)) != CRYPT_OK) {
48 return err;
49 }
50
51 /* get the hash message digest size */
52 hashsize = hash_descriptor[hash].hashsize;
53
54 // Get the hash of the first HMAC vector plus the data
55 if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) {
56 return err;
57 }
58
59 // Create the second HMAC vector vector for step (3)
60 for(i=0; i < HMAC_BLOCKSIZE; i++) {
61 buf[i] = hmac->key[i] ^ 0x5C;
62 }
63
64 // Now calculate the "outer" hash for step (5), (6), and (7)
65 hash_descriptor[hash].init(&hmac->md);
66 hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE);
67 hash_descriptor[hash].process(&hmac->md, isha, hashsize);
68 hash_descriptor[hash].done(&hmac->md, buf);
69
70 // copy to output
71 for (i = 0; i < hashsize && i < *outlen; i++) {
72 hashOut[i] = buf[i];
73 }
74 *outlen = i;
75
76 #ifdef CLEAN_STACK
77 zeromem(isha, sizeof(buf));
78 zeromem(buf, sizeof(isha));
79 zeromem(hmac, sizeof(*hmac));
80 #endif
81 return CRYPT_OK;
82 }
83
84 #endif