]>
jfr.im git - irc/quakenet/newserv.git/blob - lib/hmac.c
4 void hmacsha256_init(hmacsha256
*c
, unsigned char *key
, int keylen
) {
5 unsigned char realkey
[64], outerkey
[64], innerkey
[64];
9 memset(realkey
, 0, sizeof(realkey
));
12 SHA256_Update(&keyc
, key
, keylen
);
13 SHA256_Final(realkey
, &keyc
);
16 memcpy(realkey
, key
, keylen
);
19 /* abusing the cache here, if we do sha256 in between that'll erase it */
22 innerkey
[i
] = r
^ 0x36;
23 outerkey
[i
] = r
^ 0x5c;
26 SHA256_Init(&c
->outer
);
27 SHA256_Init(&c
->inner
);
28 SHA256_Update(&c
->outer
, outerkey
, 64);
29 SHA256_Update(&c
->inner
, innerkey
, 64);
32 void hmacsha256_update(hmacsha256
*c
, unsigned char *message
, int messagelen
) {
33 SHA256_Update(&c
->inner
, message
, messagelen
);
36 void hmacsha256_final(hmacsha256
*c
, unsigned char *digest
) {
37 SHA256_Final(digest
, &c
->inner
);
38 SHA256_Update(&c
->outer
, digest
, 32);
39 SHA256_Final(digest
, &c
->outer
);
42 void hmacsha1_init(hmacsha1
*c
, unsigned char *key
, int keylen
) {
43 unsigned char realkey
[64], outerkey
[64], innerkey
[64];
47 memset(realkey
, 0, sizeof(realkey
));
50 SHA1Update(&keyc
, key
, keylen
);
51 SHA1Final(realkey
, &keyc
);
54 memcpy(realkey
, key
, keylen
);
57 /* abusing the cache here, if we do sha1 in between that'll erase it */
60 innerkey
[i
] = r
^ 0x36;
61 outerkey
[i
] = r
^ 0x5c;
66 SHA1Update(&c
->outer
, outerkey
, 64);
67 SHA1Update(&c
->inner
, innerkey
, 64);
70 void hmacsha1_update(hmacsha1
*c
, unsigned char *message
, int messagelen
) {
71 SHA1Update(&c
->inner
, message
, messagelen
);
74 void hmacsha1_final(hmacsha1
*c
, unsigned char *digest
) {
75 SHA1Final(digest
, &c
->inner
);
76 SHA1Update(&c
->outer
, digest
, 20);
77 SHA1Final(digest
, &c
->outer
);
80 static const char *hexdigits
= "0123456789abcdef";
82 char *hmac_printhex(unsigned char *data
, char *out
, size_t len
) {
87 *o
++ = hexdigits
[(*data
& 0xf0) >> 4];
88 *o
++ = hexdigits
[*data
& 0x0f];