xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/mvl88w8977/mlan/esa/common/hmac_md5.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /** @file hmac_md5.c
2  *
3  *  @brief This file defines algorithm for hmac md5
4  *
5  * Copyright (C) 2014-2017, Marvell International Ltd.
6  *
7  * This software file (the "File") is distributed by Marvell International
8  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
9  * (the "License").  You may use, redistribute and/or modify this File in
10  * accordance with the terms and conditions of the License, a copy of which
11  * is available by writing to the Free Software Foundation, Inc.,
12  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
13  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
14  *
15  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
17  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
18  * this warranty disclaimer.
19  */
20 
21 /******************************************************
22 Change log:
23     03/07/2014: Initial version
24 ******************************************************/
25 
26 #include "wltypes.h"
27 #include "md5.h"
28 
29 #include "hostsa_ext_def.h"
30 #include "authenticator.h"
31 
32 void
Mrvl_hmac_md5(void * priv,UINT8 * text_data,int text_len,UINT8 * key,int key_len,void * digest)33 Mrvl_hmac_md5(void *priv, UINT8 *text_data, int text_len, UINT8 *key,
34 	      int key_len, void *digest)
35 {
36 	phostsa_private psapriv = (phostsa_private)priv;
37 	hostsa_util_fns *util_fns = &psapriv->util_fns;
38 	//BufferDesc_t * pDesc = NULL;
39 	UINT8 buf[400] = { 0 };
40 	Mrvl_MD5_CTX *context;
41 	unsigned char *k_pad;	/* padding - key XORd with i/opad */
42 	int i;
43 #if 0
44 	/* Wait forever ensures a buffer */
45 	pDesc = (BufferDesc_t *) bml_AllocBuffer(ramHook_encrPoolConfig,
46 						 400, BML_WAIT_FOREVER);
47 #endif
48 	/* WLAN buffers are aligned, so k_pad start at a UINT32 boundary */
49 	//k_pad = (unsigned char *)BML_DATA_PTR(pDesc);
50 	k_pad = (unsigned char *)buf;
51 	context = (Mrvl_MD5_CTX *)(k_pad + 64);
52 
53 	/* if key is longer than 64 bytes reset it to key=MD5(key) */
54 	if (key_len > 64) {
55 		Mrvl_MD5_CTX tctx;
56 
57 		wpa_MD5Init(&tctx);
58 		wpa_MD5Update((void *)priv, &tctx, key, key_len);
59 		wpa_MD5Final((void *)priv, context->buffer, &tctx);
60 
61 		key = context->buffer;
62 		key_len = 16;
63 	}
64 
65 	/* the HMAC_MD5 transform looks like: */
66 	/* */
67 	/*  MD5(K XOR opad, MD5(K XOR ipad, text)) */
68 	/* */
69 	/* where K is an n byte key */
70 	/* ipad is the byte 0x36 repeated 64 times */
71 	/* opad is the byte 0x5c repeated 64 times */
72 	/* and text is the data being protected */
73 
74 	/* start out by storing key in pads */
75 	memset(util_fns, k_pad, 0, 64);
76 	memcpy(util_fns, k_pad, key, key_len);
77 
78 	/* XOR key with ipad and opad values */
79 	for (i = 0; i < 16; i++) {
80 		((UINT32 *)k_pad)[i] ^= 0x36363636;
81 	}
82 
83 	/* perform inner MD5 */
84 	wpa_MD5Init(context);	/* init context for 1st pass */
85 	wpa_MD5Update((void *)priv, context, k_pad, 64);	/* start with inner pad */
86 	wpa_MD5Update((void *)priv, context, text_data, text_len);	/* then text of datagram */
87 	wpa_MD5Final((void *)priv, digest, context);	/* finish up 1st pass */
88 
89 	/* start out by storing key in pads */
90 	memset(util_fns, k_pad, 0, 64);
91 	memcpy(util_fns, k_pad, key, key_len);
92 
93 	for (i = 0; i < 16; i++) {
94 		((UINT32 *)k_pad)[i] ^= 0x5c5c5c5c;
95 	}
96 
97 	/* perform outer MD5 */
98 	wpa_MD5Init(context);	/* init context for 2nd pass */
99 	wpa_MD5Update((void *)priv, context, k_pad, 64);
100 	/* start with outer pad */
101 	wpa_MD5Update((void *)priv, context, digest, 16);
102 	/* then results of 1st hash */
103 	wpa_MD5Final((void *)priv, digest, context);	/* finish up 2nd pass */
104 
105 //    bml_FreeBuffer((UINT32)pDesc);
106 }
107