Mercurial > masqmail
annotate src/md5/hmac_md5.c @ 216:84bf7a6b6ccd
added misc/list-versions
This script helps to check if the versions numbers in the project
are the same as the one for the release. This script is motivated
by the 0.2.27 release in which masqmail introduces itself as being
version 0.2.26.
author | meillo@marmaro.de |
---|---|
date | Mon, 19 Jul 2010 14:01:13 +0200 |
parents | 0f36c0a46f82 |
children | 41958685480d |
rev | line source |
---|---|
0 | 1 /* |
211
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
2 hmac_md5 -- implements RFC 2104 |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
3 |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
4 Copyright 2010, markus schnalke <meillo@marmaro.de> |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
5 |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
6 Permission to use, copy, modify, and/or distribute this software for any |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
7 purpose with or without fee is hereby granted, provided that the above |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
8 copyright notice and this permission notice appear in all copies. |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
9 |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
17 |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
18 |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
19 My motivation to write this code was the lack of a nicely licensed |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
20 hmac_md5 function in C. I programmed it following the RFC's text. |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
21 Obviously this code is highly similar to the sample code of the RFC. |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
22 The code is tested against the test vectors of the RFC. Wikipedia's |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
23 HMAC page helped me to understand the algorithm better. |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
24 |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
25 This hmac_md5 function requires an OpenSSL-compatible MD5 |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
26 implementation. There are Public Domain MD5 implementations by Colin |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
27 Plumb and by Solar Designer. You probably want to use one of these. |
0 | 28 */ |
29 | |
30 #include <string.h> | |
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 | 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 /* |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
39 The computed HMAC will be written to `digest'. |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
40 Ensure digest points to hashsize bytes of allocated memory. |
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 |
211
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
43 hmac_md5(unsigned char* text, int textlen, unsigned char* key, int keylen, unsigned char* digest) |
0 | 44 { |
211
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
45 int i; |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
46 MD5_CTX context; |
211
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
47 unsigned char ipad[blocksize]; |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
48 unsigned char opad[blocksize]; |
0 | 49 |
211
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
50 /* 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
|
51 if (keylen > blocksize) { |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
52 MD5_Init(&context); |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
53 MD5_Update(&context, key, keylen); |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
54 MD5_Final(digest, &context); |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
55 key = digest; |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
56 keylen = hashsize; |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
57 } |
0 | 58 |
211
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
59 /* 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
|
60 memset(ipad, 0, sizeof(ipad)); |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
61 memcpy(ipad, key, keylen); |
0 | 62 |
211
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
63 memset(opad, 0, sizeof(opad)); |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
64 memcpy(opad, key, keylen); |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
65 |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
66 /* 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
|
67 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
|
68 ipad[i] ^= 0x36; |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
69 opad[i] ^= 0x5c; |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
70 } |
0 | 71 |
211
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
72 /* inner pass (ipad ++ message) */ |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
73 MD5_Init(&context); |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
74 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
|
75 MD5_Update(&context, text, textlen); |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
76 MD5_Final(digest, &context); |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
77 |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
78 /* 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
|
79 MD5_Init(&context); |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
80 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
|
81 MD5_Update(&context, digest, hashsize); |
0f36c0a46f82
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
209
diff
changeset
|
82 MD5_Final(digest, &context); |
0 | 83 } |