summaryrefslogtreecommitdiff
path: root/lib/libcrypto/include/hmac_generic.h
blob: a749228e36b123667b23a40ad7a501aef64ccc77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef _HMAC_GENERIC_H
#define _HMAC_GENERIC_H
/*
 * HMAC macro helpers
 *
 * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 */

#ifndef DIVUP
#define DIVUP(x,y) ((x + y -1) / y) /* divide, rounding upwards */
#endif
#ifndef HMAC_IPAD
#define		HMAC_IPAD	0x36
#define		HMAC_OPAD	0x5C
#endif
#define HMAC_SET_KEY_IMPL(func_name, hctx_t, blocksize, func_init, func_update) \
void func_name(hctx_t *hctx, const u_int8_t * key, int keylen) { \
	int i;\
	u_int8_t kb[blocksize];		\
	for (i = 0; i < DIVUP(keylen*8, 8); i++) {	\
		kb[i] = key[i] ^ HMAC_IPAD;	\
	}					\
	for (; i < blocksize; i++) {		\
		kb[i] = HMAC_IPAD;		\
	}					\
	func_init(&hctx->ictx);			\
	func_update(&hctx->ictx, kb, blocksize);	\
	for (i = 0; i < blocksize; i++) {	\
		kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);	\
	}					\
	func_init(&hctx->octx);			\
	func_update(&hctx->octx, kb, blocksize);	\
}
#define HMAC_HASH_IMPL(func_name, hctx_t, ctx_t, ahlen, func_update, func_result ) \
void func_name(hctx_t *hctx, const u_int8_t * dat, int len, u_int8_t * hash, int hashlen) {	\
	ctx_t ctx;	\
	ctx=hctx->ictx;	\
	if (dat) func_update(&ctx, dat, len);	\
	if (hash) {				\
		u_int8_t hash_buf[ahlen];			\
		func_result(&ctx, hash_buf, ahlen);	\
		ctx=hctx->octx;				\
		func_update(&ctx, hash_buf, ahlen);	\
		func_result(&ctx, hash, hashlen);	\
		memset(&ctx, 0, sizeof (ctx));		\
		memset(&hash_buf, 0, sizeof (hash_buf));\
	}					\
}
#endif /* _HMAC_GENERIC_H */