netif_utils: correctly close fd on read error
[project/ustp.git] / hmac_md5.c
1 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
2 rights reserved.
3
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
7 or this function.
8
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.
13
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.
18
19 These notices must be retained in any copies of any part of this
20 documentation and/or software.
21 */
22
23 #include <string.h>
24 #include <sys/types.h>
25 #include <libubox/md5.h>
26
27 #include "mstp.h"
28
29 /*
30 ** Function: hmac_md5 from RFC-2104
31 */
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 */
38 {
39 md5_ctx_t context;
40 unsigned char k_ipad[65]; /* inner padding -
41 * key XORd with ipad
42 */
43 unsigned char k_opad[65]; /* outer padding -
44 * key XORd with opad
45 */
46 unsigned char tk[16];
47 int i;
48 /* if key is longer than 64 bytes reset it to key=MD5(key) */
49 if(key_len > 64)
50 {
51 md5_ctx_t tctx;
52
53 md5_begin(&tctx);
54 md5_hash(key, key_len, &tctx);
55 md5_end(tk, &tctx);
56
57 key = tk;
58 key_len = 16;
59 }
60
61 /*
62 * the HMAC_MD5 transform looks like:
63 *
64 * MD5(K XOR opad, MD5(K XOR ipad, text))
65 *
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
70 */
71
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);
77
78 /* XOR key with ipad and opad values */
79 for(i = 0; i < 64; ++i)
80 {
81 k_ipad[i] ^= 0x36;
82 k_opad[i] ^= 0x5c;
83 }
84 /*
85 * perform inner MD5
86 */
87 md5_begin(&context); /* init context for 1st
88 * pass */
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 */
92 /*
93 * perform outer MD5
94 */
95 md5_begin(&context); /* init context for 2nd
96 * pass */
97 md5_hash(k_opad, 64, &context); /* start with outer pad */
98 md5_hash(digest, 16, &context); /* then results of 1st
99 * hash */
100 md5_end(digest, &context); /* finish up 2nd pass */
101 }