37 #if defined(HMAC_ALG_NAME)
39 #define HMAC_CONCAT_INNER(name,suffix) name##suffix
40 #define HMAC_CONCAT(name,suffix) HMAC_CONCAT_INNER(name,suffix)
43 #define HMAC_IPAD 0x36
46 #define HMAC_OPAD 0x5C
56 static void HMAC_CONCAT(HMAC_ALG_NAME,_xor_pad)
57 (
unsigned char *out,
const unsigned char *in,
58 size_t size,
unsigned char pad)
74 static void HMAC_CONCAT(HMAC_ALG_NAME,_absorb_key)
75 (HMAC_STATE *state,
const unsigned char *key,
size_t keylen,
78 unsigned char temp[HMAC_HASH_SIZE];
84 if (keylen <= HMAC_BLOCK_SIZE) {
85 HMAC_HASH_INIT(state);
89 if (len > HMAC_HASH_SIZE)
91 HMAC_CONCAT(HMAC_ALG_NAME,_xor_pad)(temp, key, len, pad);
92 HMAC_HASH_UPDATE(state, temp, len);
99 HMAC_HASH_INIT(state);
100 HMAC_HASH_UPDATE(state, key, keylen);
101 HMAC_HASH_FINALIZE(state, temp);
102 HMAC_CONCAT(HMAC_ALG_NAME,_xor_pad)(temp, temp, HMAC_HASH_SIZE, pad);
103 HMAC_HASH_INIT(state);
104 HMAC_HASH_UPDATE(state, temp, HMAC_HASH_SIZE);
105 posn = HMAC_HASH_SIZE;
109 memset(temp, pad,
sizeof(temp));
110 while (posn < HMAC_BLOCK_SIZE) {
111 len = HMAC_BLOCK_SIZE - posn;
112 if (len > HMAC_HASH_SIZE)
113 len = HMAC_HASH_SIZE;
114 HMAC_HASH_UPDATE(state, temp, len);
121 const unsigned char *key,
size_t keylen,
122 const unsigned char *in,
size_t inlen)
125 HMAC_CONCAT(HMAC_ALG_NAME,_absorb_key)(&state, key, keylen, HMAC_IPAD);
126 HMAC_HASH_UPDATE(&state, in, inlen);
127 HMAC_CONCAT(HMAC_ALG_NAME,_finalize)(&state, key, keylen, out);
128 aead_clean(&state,
sizeof(state));
131 void HMAC_CONCAT(HMAC_ALG_NAME,_init)
132 (HMAC_STATE *state,
const unsigned char *key,
size_t keylen)
134 HMAC_CONCAT(HMAC_ALG_NAME,_absorb_key)(state, key, keylen, HMAC_IPAD);
137 void HMAC_CONCAT(HMAC_ALG_NAME,_update)
138 (HMAC_STATE *state,
const unsigned char *in,
size_t inlen)
140 HMAC_HASH_UPDATE(state, in, inlen);
143 void HMAC_CONCAT(HMAC_ALG_NAME,_finalize)
144 (HMAC_STATE *state,
const unsigned char *key,
size_t keylen,
147 unsigned char temp[HMAC_HASH_SIZE];
148 HMAC_HASH_FINALIZE(state, temp);
149 HMAC_CONCAT(HMAC_ALG_NAME,_absorb_key)(state, key, keylen, HMAC_OPAD);
150 HMAC_HASH_UPDATE(state, temp, HMAC_HASH_SIZE);
151 HMAC_HASH_FINALIZE(state, out);
152 aead_clean(temp,
sizeof(temp));
160 #undef HMAC_HASH_SIZE
161 #undef HMAC_BLOCK_SIZE
163 #undef HMAC_HASH_INIT
164 #undef HMAC_HASH_UPDATE
165 #undef HMAC_HASH_FINALIZE
166 #undef HMAC_CONCAT_INNER