masqmail
view src/md5/hmac_md5.c @ 336:2d4aa516ba0e
updated ChangeLog and NEWS
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Sat, 27 Aug 2011 18:38:55 +0200 |
parents | 10da50168dab |
children | 41958685480d |
line source
1 /*
2 hmac_md5 -- implements RFC 2104
4 Copyright 2010, markus schnalke <meillo@marmaro.de>
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 My motivation to write this code was the lack of a nicely licensed
20 hmac_md5 function in C. I programmed it following the RFC's text.
21 Obviously this code is highly similar to the sample code of the RFC.
22 The code is tested against the test vectors of the RFC. Wikipedia's
23 HMAC page helped me to understand the algorithm better.
25 This hmac_md5 function requires an OpenSSL-compatible MD5
26 implementation. There are Public Domain MD5 implementations by Colin
27 Plumb and by Solar Designer. You probably want to use one of these.
28 */
30 #include <string.h>
31 #include "md5.h"
34 const int blocksize = 64;
35 const int hashsize = 16;
38 /*
39 The computed HMAC will be written to `digest'.
40 Ensure digest points to hashsize bytes of allocated memory.
41 */
42 void
43 hmac_md5(unsigned char* text, int textlen, unsigned char* key, int keylen, unsigned char* digest)
44 {
45 int i;
46 MD5_CTX context;
47 unsigned char ipad[blocksize];
48 unsigned char opad[blocksize];
50 /* too long keys are replaced by their hash value */
51 if (keylen > blocksize) {
52 MD5_Init(&context);
53 MD5_Update(&context, key, keylen);
54 MD5_Final(digest, &context);
55 key = digest;
56 keylen = hashsize;
57 }
59 /* copy the key into the pads */
60 memset(ipad, 0, sizeof(ipad));
61 memcpy(ipad, key, keylen);
63 memset(opad, 0, sizeof(opad));
64 memcpy(opad, key, keylen);
66 /* xor the pads with their ``basic'' value */
67 for (i=0; i<blocksize; i++) {
68 ipad[i] ^= 0x36;
69 opad[i] ^= 0x5c;
70 }
72 /* inner pass (ipad ++ message) */
73 MD5_Init(&context);
74 MD5_Update(&context, ipad, sizeof(ipad));
75 MD5_Update(&context, text, textlen);
76 MD5_Final(digest, &context);
78 /* outer pass (opad ++ result of inner pass) */
79 MD5_Init(&context);
80 MD5_Update(&context, opad, sizeof(opad));
81 MD5_Update(&context, digest, hashsize);
82 MD5_Final(digest, &context);
83 }