OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / libcrypto / include / hmac_generic.h
1 #ifndef _HMAC_GENERIC_H
2 #define _HMAC_GENERIC_H
3 /*
4  * HMAC macro helpers
5  *
6  * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
12  * 
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16  * for more details.
17  *
18  */
19
20 #ifndef DIVUP
21 #define DIVUP(x,y) ((x + y -1) / y) /* divide, rounding upwards */
22 #endif
23 #ifndef HMAC_IPAD
24 #define         HMAC_IPAD       0x36
25 #define         HMAC_OPAD       0x5C
26 #endif
27 #define HMAC_SET_KEY_IMPL(func_name, hctx_t, blocksize, func_init, func_update) \
28 void func_name(hctx_t *hctx, const u_int8_t * key, int keylen) { \
29         int i;\
30         u_int8_t kb[blocksize];         \
31         for (i = 0; i < DIVUP(keylen*8, 8); i++) {      \
32                 kb[i] = key[i] ^ HMAC_IPAD;     \
33         }                                       \
34         for (; i < blocksize; i++) {            \
35                 kb[i] = HMAC_IPAD;              \
36         }                                       \
37         func_init(&hctx->ictx);                 \
38         func_update(&hctx->ictx, kb, blocksize);        \
39         for (i = 0; i < blocksize; i++) {       \
40                 kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);       \
41         }                                       \
42         func_init(&hctx->octx);                 \
43         func_update(&hctx->octx, kb, blocksize);        \
44 }
45 #define HMAC_HASH_IMPL(func_name, hctx_t, ctx_t, ahlen, func_update, func_result ) \
46 void func_name(hctx_t *hctx, const u_int8_t * dat, int len, u_int8_t * hash, int hashlen) {     \
47         ctx_t ctx;      \
48         ctx=hctx->ictx; \
49         if (dat) func_update(&ctx, dat, len);   \
50         if (hash) {                             \
51                 u_int8_t hash_buf[ahlen];                       \
52                 func_result(&ctx, hash_buf, ahlen);     \
53                 ctx=hctx->octx;                         \
54                 func_update(&ctx, hash_buf, ahlen);     \
55                 func_result(&ctx, hash, hashlen);       \
56                 memset(&ctx, 0, sizeof (ctx));          \
57                 memset(&hash_buf, 0, sizeof (hash_buf));\
58         }                                       \
59 }
60 #endif /* _HMAC_GENERIC_H */