-
Notifications
You must be signed in to change notification settings - Fork 16
/
hmac.c
executable file
·59 lines (45 loc) · 1.53 KB
/
hmac.c
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
/* HMAC-SHA1 */
#define CRYPTO_INTERNAL
#include "crypto.h"
unsigned char ipad[HMAC_BLOCK_SIZE], opad[HMAC_BLOCK_SIZE];
void hmac_init() {
memset(ipad, 0x36, HMAC_BLOCK_SIZE);
memset(opad, 0x5C, HMAC_BLOCK_SIZE);
}
hmac_ctx *hmac_ctx_init(unsigned char *key, int len) {
unsigned char *K = key;
hmac_ctx *ctx = CRYPTO_MALLOC(sizeof(*ctx));
check_len:
if (len <= HMAC_BLOCK_SIZE) {
memcpy(ctx->key, K, len);
memset(ctx->key + len, 0x0, HMAC_BLOCK_SIZE - len);
} else {
K = sha1(key, len);
len = 20;
goto check_len;
}
return ctx;
}
void hmac_free(hmac_ctx *ctx) {
CRYPTO_FREE(ctx);
}
unsigned char *hmac(hmac_ctx *ctx, const unsigned char *data, unsigned int dlen) {
unsigned char temp[HMAC_BLOCK_SIZE], *stream, *hash;
int i;
for (i = 0; i < HMAC_BLOCK_SIZE; i += sizeof(unsigned int))
*(unsigned int *)(temp + i) = *(unsigned int *)(ctx->key + i) ^ *(unsigned int *)(ipad + i);
stream = CRYPTO_MALLOC(HMAC_BLOCK_SIZE + dlen);
memcpy(stream, temp, HMAC_BLOCK_SIZE);
memcpy(stream + HMAC_BLOCK_SIZE, data, dlen);
hash = sha1(stream, HMAC_BLOCK_SIZE + dlen);
CRYPTO_FREE(stream);
for (i = 0; i < HMAC_BLOCK_SIZE; i += sizeof(unsigned int))
*(unsigned int *)(temp + i) = *(unsigned int *)(ctx->key + i) ^ *(unsigned int *)(opad + i);
stream = CRYPTO_MALLOC(20 /* sha1 o/p */ + HMAC_BLOCK_SIZE);
memcpy(stream, temp, HMAC_BLOCK_SIZE);
memcpy(stream + HMAC_BLOCK_SIZE, hash, 20);
CRYPTO_FREE(hash);
hash = sha1(stream, 20 + HMAC_BLOCK_SIZE);
CRYPTO_FREE(stream);
return hash;
}