Resync with broadcom drivers 5.100.138.20 and utilities.
[tomato.git] / release / src-rt / bcmcrypto / hmac.c
blobfb5eb691d14676acb61c86c737350e918553bbbc
1 /*
2 * Function: hmac_md5
3 * From rfc2104.txt
5 * $Id: hmac.c,v 1.16 2009-04-13 16:25:16 Exp $
6 */
8 /*
9 * Copyright (C) The Internet Society (2001). All Rights Reserved.
11 * This document and translations of it may be copied and furnished to
12 * others, and derivative works that comment on or otherwise explain it
13 * or assist in its implementation may be prepared, copied, published
14 * and distributed, in whole or in part, without restriction of any
15 * kind, provided that the above copyright notice and this paragraph are
16 * included on all such copies and derivative works. However, this
17 * document itself may not be modified in any way, such as by removing
18 * the copyright notice or references to the Internet Society or other
19 * Internet organizations, except as needed for the purpose of
20 * developing Internet standards in which case the procedures for
21 * copyrights defined in the Internet Standards process must be
22 * followed, or as required to translate it into languages other than
23 * English.
25 * The limited permissions granted above are perpetual and will not be
26 * revoked by the Internet Society or its successors or assigns.
28 * This document and the information contained herein is provided on an
29 * "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
30 * TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
31 * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
32 * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
33 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
35 * Copyright (C) 2010, Broadcom Corporation
36 * All Rights Reserved.
38 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
39 * the contents of this file may not be disclosed to third parties, copied
40 * or duplicated in any form, in whole or in part, without the prior
41 * written permission of Broadcom Corporation.
43 * $Id: hmac.c,v 1.16 2009-04-13 16:25:16 Exp $
47 #include <bcmcrypto/md5.h>
49 #ifdef BCMDRIVER
50 #if !defined(BCMSUP_PSK)
51 #error "BCMSUP_PSK or BCMCCX must be defined to compile hmac.c for driver!"
52 #endif
53 #include <osl.h>
54 #else
55 #if defined(__GNUC__)
56 extern void bcopy(const void *src, void *dst, int len);
57 extern void bzero(void *b, int len);
58 #else
59 #define bcopy(src, dst, len) memcpy((dst), (src), (len))
60 #define bzero(b, len) memset((b), 0, (len))
61 #endif
62 #endif /* BCMDRIVER */
65 extern void BCMROMFN(hmac_md5)(unsigned char *text, int text_len, unsigned char *key,
66 int key_len, unsigned char *digest);
68 /* text pointer to data stream */
69 /* text_len length of data stream */
70 /* key pointer to authentication key */
71 /* key_len length of authentication key */
72 /* digest caller digest to be filled in */
73 void
74 BCMROMFN(hmac_md5)(unsigned char *text, int text_len, unsigned char *key,
75 int key_len, unsigned char *digest)
77 MD5_CTX context;
78 unsigned char k_ipad[65]; /* inner padding -
79 * key XORd with ipad
81 unsigned char k_opad[65]; /* outer padding -
82 * key XORd with opad
84 unsigned char tk[16];
85 int i;
86 /* if key is longer than 64 bytes reset it to key=MD5(key) */
87 if (key_len > 64) {
89 MD5_CTX tctx;
91 MD5Init(&tctx);
92 MD5Update(&tctx, key, key_len);
93 MD5Final(tk, &tctx);
95 key = tk;
96 key_len = 16;
100 * the HMAC_MD5 transform looks like:
102 * MD5(K XOR opad, MD5(K XOR ipad, text))
104 * where K is an n byte key
105 * ipad is the byte 0x36 repeated 64 times
107 * opad is the byte 0x5c repeated 64 times
108 * and text is the data being protected
111 /* start out by storing key in pads */
112 bzero(k_ipad, sizeof(k_ipad));
113 bzero(k_opad, sizeof(k_opad));
114 bcopy(key, k_ipad, key_len);
115 bcopy(key, k_opad, key_len);
117 /* XOR key with ipad and opad values */
118 for (i = 0; i < 64; i++) {
119 k_ipad[i] ^= 0x36;
120 k_opad[i] ^= 0x5c;
123 * perform inner MD5
125 MD5Init(&context); /* init context for 1st pass */
126 MD5Update(&context, k_ipad, 64); /* start with inner pad */
127 MD5Update(&context, text, text_len); /* then text of datagram */
128 MD5Final(digest, &context); /* finish up 1st pass */
130 * perform outer MD5
132 MD5Init(&context); /* init context for 2nd pass */
133 MD5Update(&context, k_opad, 64); /* start with outer pad */
134 MD5Update(&context, digest, 16); /* then results of 1st hash */
135 MD5Final(digest, &context); /* finish up 2nd pass */