xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/mvl88w8977/mlan/esa/common/mrvl_sha256_crypto.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /** @file Mrvl_sha256_crypto.c
2  *
3  *  @brief This file  defines sha256 crypto
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 #include <linux/string.h>
26 
27 #include "wltypes.h"
28 #include "wl_macros.h"
29 
30 #include "sha_256.h"
31 #include "mrvl_sha256_crypto.h"
32 
33 #include "hostsa_ext_def.h"
34 #include "authenticator.h"
35 
36 /* Helper function to allocate scratch memory and call sha256_vector() */
37 void
mrvl_sha256_crypto_vector(void * priv,size_t num_elem,UINT8 * addr[],size_t * len,UINT8 * mac)38 mrvl_sha256_crypto_vector(void *priv, size_t num_elem,
39 			  UINT8 *addr[], size_t * len, UINT8 *mac)
40 {
41 
42 	//BufferDesc_t* pBufDesc = NULL;
43 	UINT8 buf[SHA256_MIN_SCRATCH_BUF] = { 0 };
44 
45 	sha256_vector((void *)priv, num_elem, addr, len, mac, (UINT8 *)buf);
46 
47 //    bml_FreeBuffer((UINT32)pBufDesc);
48 }
49 
50 void
mrvl_sha256_crypto_kdf(void * priv,UINT8 * pKey,UINT8 key_len,char * label,UINT8 label_len,UINT8 * pContext,UINT16 context_len,UINT8 * pOutput,UINT16 output_len)51 mrvl_sha256_crypto_kdf(void *priv, UINT8 *pKey,
52 		       UINT8 key_len,
53 		       char *label,
54 		       UINT8 label_len,
55 		       UINT8 *pContext,
56 		       UINT16 context_len, UINT8 *pOutput, UINT16 output_len)
57 {
58 	phostsa_private psapriv = (phostsa_private)priv;
59 	hostsa_util_fns *util_fns = &psapriv->util_fns;
60 	UINT8 *vectors[4 + 1];
61 	size_t vectLen[NELEMENTS(vectors)];
62 	UINT8 *pResult;
63 	UINT8 *pLoopResult;
64 	UINT8 iterations;
65 	UINT16 i;
66 	//BufferDesc_t* pBufDesc = NULL;
67 	UINT8 buf[HMAC_SHA256_MIN_SCRATCH_BUF] = { 0 };
68 
69 	pResult = pContext + context_len;
70 
71 	/*
72 	 ** Working memory layout:
73 	 **            | KDF-Len output --- >
74 	 **            |
75 	 ** [KDF Input][HMAC output#1][HMAC output#2][...]
76 	 **
77 	 */
78 
79 	/* Number of SHA256 digests needed to meet the bit length output_len */
80 	iterations = (output_len + 255) / 256;
81 
82 	pLoopResult = pResult;
83 
84 	for (i = 1; i <= iterations; i++) {
85 		/* Skip vectors[0]; Used internally in hmac_sha256_vector function */
86 		vectors[1] = (UINT8 *)&i;
87 		vectLen[1] = sizeof(i);
88 
89 		vectors[2] = (UINT8 *)label;
90 		vectLen[2] = label_len;
91 
92 		vectors[3] = pContext;
93 		vectLen[3] = context_len;
94 
95 		vectors[4] = (UINT8 *)&output_len;
96 		vectLen[4] = sizeof(output_len);
97 
98 		/*
99 		 **
100 		 **  KDF input = (K, i || label || Context || Length)
101 		 **
102 		 */
103 		hmac_sha256_vector(priv, pKey, key_len,
104 				   NELEMENTS(vectors), vectors, vectLen,
105 				   pLoopResult, (UINT8 *)buf);
106 
107 		/* Move the hmac output pointer so another digest can be appended
108 		 **  if more loop iterations are needed to get the output_len key
109 		 **  bit total
110 		 */
111 		pLoopResult += SHA256_MAC_LEN;
112 	}
113 
114 //    bml_FreeBuffer((UINT32)pBufDesc);
115 
116 	memcpy(util_fns, pOutput, pResult, output_len / 8);
117 
118 }
119