1 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
4 License to copy and use this software is granted provided that it
5 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
6 Algorithm" in all material mentioning or referencing this software
9 License is also granted to make and use derivative works provided
10 that such works are identified as "derived from the RSA Data
11 Security, Inc. MD5 Message-Digest Algorithm" in all material
12 mentioning or referencing the derived work.
14 RSA Data Security, Inc. makes no representations concerning either
15 the merchantability of this software or the suitability of this
16 software for any particular purpose. It is provided "as is"
17 without express or implied warranty of any kind.
19 These notices must be retained in any copies of any part of this
20 documentation and/or software.
24 #include <sys/types.h>
25 #include <libubox/md5.h>
30 ** Function: hmac_md5 from RFC-2104
32 void hmac_md5(text
, text_len
, key
, key_len
, digest
)
33 unsigned char* text
; /* pointer to data stream */
34 int text_len
; /* length of data stream */
35 unsigned char* key
; /* pointer to authentication key */
36 int key_len
; /* length of authentication key */
37 caddr_t digest
; /* caller digest to be filled in */
40 unsigned char k_ipad
[65]; /* inner padding -
43 unsigned char k_opad
[65]; /* outer padding -
48 /* if key is longer than 64 bytes reset it to key=MD5(key) */
54 md5_hash(key
, key_len
, &tctx
);
62 * the HMAC_MD5 transform looks like:
64 * MD5(K XOR opad, MD5(K XOR ipad, text))
66 * where K is an n byte key
67 * ipad is the byte 0x36 repeated 64 times
68 * opad is the byte 0x5c repeated 64 times
69 * and text is the data being protected
72 /* start out by storing key in pads */
73 bzero(k_ipad
, sizeof k_ipad
);
74 bzero(k_opad
, sizeof k_opad
);
75 bcopy(key
, k_ipad
, key_len
);
76 bcopy( key
, k_opad
, key_len
);
78 /* XOR key with ipad and opad values */
79 for(i
= 0; i
< 64; ++i
)
87 md5_begin(&context
); /* init context for 1st
89 md5_hash(k_ipad
, 64, &context
); /* start with inner pad */
90 md5_hash(text
, text_len
, &context
); /* then text of datagram */
91 md5_end(digest
, &context
); /* finish up 1st pass */
95 md5_begin(&context
); /* init context for 2nd
97 md5_hash(k_opad
, 64, &context
); /* start with outer pad */
98 md5_hash(digest
, 16, &context
); /* then results of 1st
100 md5_end(digest
, &context
); /* finish up 2nd pass */