Mercurial > masqmail-0.2
annotate src/md5/hmac_md5.c @ 170:0f0e4e7cd762
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 | b8c358b2e242 |
children |
rev | line source |
---|---|
0 | 1 /* |
164
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
2 hmac_md5 -- implements RFC 2104 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
3 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
4 Copyright 2010, markus schnalke <meillo@marmaro.de> |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
5 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
6 Permission to use, copy, modify, and/or distribute this software for any |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
7 purpose with or without fee is hereby granted, provided that the above |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
8 copyright notice and this permission notice appear in all copies. |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
9 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
17 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
18 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
19 My motivation to write this code was the lack of a nicely licensed |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
20 hmac_md5 function in C. I programmed it following the RFC's text. |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
21 Obviously this code is highly similar to the sample code of the RFC. |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
22 The code is tested against the test vectors of the RFC. Wikipedia's |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
23 HMAC page helped me to understand the algorithm better. |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
24 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
25 This hmac_md5 function requires an OpenSSL-compatible MD5 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
26 implementation. There are Public Domain MD5 implementations by Colin |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
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" | |
164
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
32 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
33 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
34 const int blocksize = 64; |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
35 const int hashsize = 16; |
0 | 36 |
164
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
37 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
38 /* |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
39 The computed HMAC will be written to `digest'. |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
40 Ensure digest points to hashsize bytes of allocated memory. |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
41 */ |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
42 void |
164
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
43 hmac_md5(unsigned char* text, int textlen, unsigned char* key, int keylen, unsigned char* digest) |
0 | 44 { |
164
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
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; |
164
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
47 unsigned char ipad[blocksize]; |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
48 unsigned char opad[blocksize]; |
0 | 49 |
164
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
50 /* too long keys are replaced by their hash value */ |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
51 if (keylen > blocksize) { |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
52 MD5_Init(&context); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
53 MD5_Update(&context, key, keylen); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
54 MD5_Final(digest, &context); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
55 key = digest; |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
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 |
164
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
59 /* copy the key into the pads */ |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
60 memset(ipad, 0, sizeof(ipad)); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
61 memcpy(ipad, key, keylen); |
0 | 62 |
164
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
63 memset(opad, 0, sizeof(opad)); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
64 memcpy(opad, key, keylen); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
65 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
66 /* xor the pads with their ``basic'' value */ |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
67 for (i=0; i<blocksize; i++) { |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
68 ipad[i] ^= 0x36; |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
69 opad[i] ^= 0x5c; |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
70 } |
0 | 71 |
164
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
72 /* inner pass (ipad ++ message) */ |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
73 MD5_Init(&context); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
74 MD5_Update(&context, ipad, sizeof(ipad)); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
75 MD5_Update(&context, text, textlen); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
76 MD5_Final(digest, &context); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
77 |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
78 /* outer pass (opad ++ result of inner pass) */ |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
79 MD5_Init(&context); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
80 MD5_Update(&context, opad, sizeof(opad)); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
81 MD5_Update(&context, digest, hashsize); |
b8c358b2e242
replaced hmac_md5.c with an own implementation of RFC 2104
meillo@marmaro.de
parents:
162
diff
changeset
|
82 MD5_Final(digest, &context); |
0 | 83 } |