12 | 12 |
*/
|
13 | 13 |
|
14 | 14 |
#include "sha1.h"
|
|
15 |
|
15 | 16 |
#include <string.h>
|
16 | |
|
17 | 17 |
|
18 | 18 |
|
19 | 19 |
/* ===== start - public domain SHA1 implementation ===== */
|
|
261 | 261 |
/* ===== end - public domain SHA1 implementation ===== */
|
262 | 262 |
|
263 | 263 |
|
|
264 |
void HMACInit(HMAC_CTX* context, const uint8_t *key, size_t key_len)
|
|
265 |
{
|
|
266 |
unsigned char ipad[64]; /* padding - key XORd with ipad */
|
|
267 |
unsigned char tk[20];
|
|
268 |
size_t i;
|
|
269 |
|
|
270 |
/* if key is longer than 64 bytes reset it to key = SHA1(key) */
|
|
271 |
if (key_len > 64) {
|
|
272 |
sha1_vector(1, &key, &key_len, tk);
|
|
273 |
key = tk;
|
|
274 |
key_len = 20;
|
|
275 |
}
|
|
276 |
|
|
277 |
/* start out by storing key in ipad */
|
|
278 |
memset(ipad, 0, sizeof(ipad));
|
|
279 |
memcpy(ipad, key, key_len);
|
|
280 |
|
|
281 |
/* XOR key with ipad values */
|
|
282 |
for (i = 0; i < 64; i++)
|
|
283 |
ipad[i] ^= 0x36;
|
|
284 |
|
|
285 |
/* Store the key in our context */
|
|
286 |
memcpy(context->key, key, key_len);
|
|
287 |
context->key_len = key_len;
|
|
288 |
|
|
289 |
SHA1Init (&context->context);
|
|
290 |
SHA1Update (&context->context, ipad, sizeof(ipad));
|
|
291 |
}
|
|
292 |
|
|
293 |
void HMACUpdate(HMAC_CTX *context, const void *data, uint32_t len)
|
|
294 |
{
|
|
295 |
SHA1Update (&context->context, data, len);
|
|
296 |
}
|
|
297 |
|
|
298 |
void HMACFinal(unsigned char digest[20], HMAC_CTX *context)
|
|
299 |
{
|
|
300 |
|
|
301 |
unsigned char opad[64]; /* padding - key XORd with opad */
|
|
302 |
unsigned char sha1_digest[SHA1_MAC_LEN];
|
|
303 |
const uint8_t *_addr[2];
|
|
304 |
size_t _len[2];
|
|
305 |
size_t i;
|
|
306 |
|
|
307 |
SHA1Final (sha1_digest, &context->context);
|
|
308 |
|
|
309 |
memset(opad, 0, sizeof(opad));
|
|
310 |
memcpy(opad, context->key, context->key_len);
|
|
311 |
|
|
312 |
/* XOR key with opad values */
|
|
313 |
for (i = 0; i < 64; i++)
|
|
314 |
opad[i] ^= 0x5c;
|
|
315 |
|
|
316 |
/* perform outer SHA1 */
|
|
317 |
_addr[0] = opad;
|
|
318 |
_len[0] = 64;
|
|
319 |
_addr[1] = sha1_digest;
|
|
320 |
_len[1] = SHA1_MAC_LEN;
|
|
321 |
sha1_vector(2, _addr, _len, digest);
|
|
322 |
}
|
|
323 |
|
264 | 324 |
|
265 | 325 |
/**
|
266 | 326 |
* hmac_sha1_vector:
|