Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/mac/hmac/hmac_done.c
5972 views
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
#include "tomcrypt.h"
10
11
/**
12
@file hmac_done.c
13
HMAC support, terminate stream, Tom St Denis/Dobes Vandermeer
14
*/
15
16
#ifdef LTC_HMAC
17
18
#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
19
20
/**
21
Terminate an HMAC session
22
@param hmac The HMAC state
23
@param out [out] The destination of the HMAC authentication tag
24
@param outlen [in/out] The max size and resulting size of the HMAC authentication tag
25
@return CRYPT_OK if successful
26
*/
27
int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
28
{
29
unsigned char *buf, *isha;
30
unsigned long hashsize, i;
31
int hash, err;
32
33
LTC_ARGCHK(hmac != NULL);
34
LTC_ARGCHK(out != NULL);
35
36
/* test hash */
37
hash = hmac->hash;
38
if((err = hash_is_valid(hash)) != CRYPT_OK) {
39
return err;
40
}
41
42
/* get the hash message digest size */
43
hashsize = hash_descriptor[hash].hashsize;
44
45
/* allocate buffers */
46
buf = XMALLOC(LTC_HMAC_BLOCKSIZE);
47
isha = XMALLOC(hashsize);
48
if (buf == NULL || isha == NULL) {
49
if (buf != NULL) {
50
XFREE(buf);
51
}
52
if (isha != NULL) {
53
XFREE(isha);
54
}
55
return CRYPT_MEM;
56
}
57
58
/* Get the hash of the first HMAC vector plus the data */
59
if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) {
60
goto LBL_ERR;
61
}
62
63
/* Create the second HMAC vector vector for step (3) */
64
for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) {
65
buf[i] = hmac->key[i] ^ 0x5C;
66
}
67
68
/* Now calculate the "outer" hash for step (5), (6), and (7) */
69
if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) {
70
goto LBL_ERR;
71
}
72
if ((err = hash_descriptor[hash].process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) {
73
goto LBL_ERR;
74
}
75
if ((err = hash_descriptor[hash].process(&hmac->md, isha, hashsize)) != CRYPT_OK) {
76
goto LBL_ERR;
77
}
78
if ((err = hash_descriptor[hash].done(&hmac->md, buf)) != CRYPT_OK) {
79
goto LBL_ERR;
80
}
81
82
/* copy to output */
83
for (i = 0; i < hashsize && i < *outlen; i++) {
84
out[i] = buf[i];
85
}
86
*outlen = i;
87
88
err = CRYPT_OK;
89
LBL_ERR:
90
XFREE(hmac->key);
91
#ifdef LTC_CLEAN_STACK
92
zeromem(isha, hashsize);
93
zeromem(buf, hashsize);
94
zeromem(hmac, sizeof(*hmac));
95
#endif
96
97
XFREE(isha);
98
XFREE(buf);
99
100
return err;
101
}
102
103
#endif
104
105