annotate src/md5/hmac_md5.c @ 434:f2a7271746d1 default tip

Removes Freshmeat.net from the docs The site, which was later renamed to freecode.com, is no longer maintained (contains only a static copy).
author markus schnalke <meillo@marmaro.de>
date Sat, 07 Feb 2015 11:45:07 +0100
parents b27f66555ba8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
1 /*
367
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
2 ** hmac_md5 -- implements RFC 2104
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
3 **
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
4 ** Copyright 2010, markus schnalke <meillo@marmaro.de>
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
5 **
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
6 ** Permission to use, copy, modify, and/or distribute this software for any
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
7 ** purpose with or without fee is hereby granted, provided that the above
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
8 ** copyright notice and this permission notice appear in all copies.
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
9 **
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
10 ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
11 ** WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
12 ** MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
13 ** ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
14 ** WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
15 ** ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
16 ** OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
17 **
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
18 **
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
19 ** My motivation to write this code was the lack of a nicely licensed
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
20 ** hmac_md5 function in C. I programmed it following the RFC's text.
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
21 ** Obviously this code is highly similar to the sample code of the RFC.
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
22 ** The code is tested against the test vectors of the RFC. Wikipedia's
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
23 ** HMAC page helped me to understand the algorithm better.
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
24 **
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
25 ** This hmac_md5 function requires an OpenSSL-compatible MD5
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
26 ** implementation. There are Public Domain MD5 implementations by Colin
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
27 ** Plumb and by Solar Designer. You probably want to use one of these.
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
28 */
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
29
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
30 #include <string.h>
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
31 #include "md5.h"
211
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
32
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
33
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
34 const int blocksize = 64;
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
35 const int hashsize = 16;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
36
211
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
37
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
38 /*
367
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
39 ** The computed HMAC will be written to `digest'.
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
40 ** Ensure digest points to hashsize bytes of allocated memory.
211
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
41 */
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
42 void
367
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
43 hmac_md5(unsigned char *text, int textlen, unsigned char *key, int keylen,
b27f66555ba8 Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents: 366
diff changeset
44 unsigned char *digest)
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
45 {
211
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
46 int i;
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
47 MD5_CTX context;
211
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
48 unsigned char ipad[blocksize];
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
49 unsigned char opad[blocksize];
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
50
211
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
51 /* too long keys are replaced by their hash value */
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
52 if (keylen > blocksize) {
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
53 MD5_Init(&context);
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
54 MD5_Update(&context, key, keylen);
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
55 MD5_Final(digest, &context);
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
56 key = digest;
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
57 keylen = hashsize;
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
58 }
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
59
211
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
60 /* copy the key into the pads */
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
61 memset(ipad, 0, sizeof(ipad));
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
62 memcpy(ipad, key, keylen);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
63
211
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
64 memset(opad, 0, sizeof(opad));
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
65 memcpy(opad, key, keylen);
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
66
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
67 /* xor the pads with their ``basic'' value */
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
68 for (i=0; i<blocksize; i++) {
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
69 ipad[i] ^= 0x36;
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
70 opad[i] ^= 0x5c;
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
71 }
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
72
211
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
73 /* inner pass (ipad ++ message) */
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
74 MD5_Init(&context);
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
75 MD5_Update(&context, ipad, sizeof(ipad));
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
76 MD5_Update(&context, text, textlen);
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
77 MD5_Final(digest, &context);
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
78
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
79 /* outer pass (opad ++ result of inner pass) */
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
80 MD5_Init(&context);
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
81 MD5_Update(&context, opad, sizeof(opad));
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
82 MD5_Update(&context, digest, hashsize);
0f36c0a46f82 replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents: 209
diff changeset
83 MD5_Final(digest, &context);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
84 }