]>
jfr.im git - irc/quakenet/newserv.git/blob - lib/hmac.c
ee11d76e04569c45e6c1d3e3adf51c11b26af434
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 void hmacmd5_init(hmacmd5
*c
, unsigned char *key
, int keylen
) {
81 unsigned char realkey
[64], outerkey
[64], innerkey
[64];
85 memset(realkey
, 0, sizeof(realkey
));
88 MD5Update(&keyc
, key
, keylen
);
89 MD5Final(realkey
, &keyc
);
92 memcpy(realkey
, key
, keylen
);
95 /* abusing the cache here, if we do sha1 in between that'll erase it */
98 innerkey
[i
] = r
^ 0x36;
99 outerkey
[i
] = r
^ 0x5c;
104 MD5Update(&c
->outer
, outerkey
, 64);
105 MD5Update(&c
->inner
, innerkey
, 64);
108 void hmacmd5_update(hmacmd5
*c
, unsigned char *message
, int messagelen
) {
109 MD5Update(&c
->inner
, message
, messagelen
);
112 void hmacmd5_final(hmacmd5
*c
, unsigned char *digest
) {
113 MD5Final(digest
, &c
->inner
);
114 MD5Update(&c
->outer
, digest
, 16);
115 MD5Final(digest
, &c
->outer
);
118 static const char *hexdigits
= "0123456789abcdef";
120 char *hmac_printhex(unsigned char *data
, char *out
, size_t len
) {
122 unsigned char *o
= (unsigned char *)out
;
125 *o
++ = hexdigits
[(*data
& 0xf0) >> 4];
126 *o
++ = hexdigits
[*data
& 0x0f];
134 int hmac_strcmp(char *a
, char *b
) {
140 if(strlen(a
) != strlen(b
))
144 result
&=(tolower(*a
++) == tolower(*b
++));