1 /*
2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 /* ****************************************************************************
20 1 头文件包含
21 **************************************************************************** */
22 #include "oal_ext_if.h"
23 #include "oal_err_wifi.h"
24 #include "hmac_wapi_sms4.h"
25 #include "hmac_wapi_wpi.h"
26 #include "hmac_wapi.h"
27
28 #ifdef __cplusplus
29 #if __cplusplus
30 extern "C" {
31 #endif
32 #endif
33
34 /* ****************************************************************************
35 3 函数实现
36 **************************************************************************** */
37 /* ****************************************************************************
38 功能描述 : ofb encrypt
39 输入参数 : hi_u8 *puc_iv 为IV存储区起始地址
40 hi_u8 *puc_bufin 为明文存储区起始地址
41 hi_u32 ul_buflen 为明文(以Byte为单位)长度
42 hi_u8 *puc_key 为会话密钥存储区起始地址
43 hi_u8* puc_bufout 为密文存储区起始地址,
44 密文存储区空间与明文存储区空间大小相同
45 修改历史 :
46 1.日 期 : 2012年5月2日
47 作 者 : HiSilicon
48 修改内容 : 新生成函数
49
50 2.日 期 : 2015年5月28日
51 作 者 : HiSilicon
52 修改内容 : 移植
53 **************************************************************************** */
hmac_wpi_encrypt(hmac_wapi_crypt_stru wpi_key,hi_u8 * puc_bufin,hi_u32 buflen,hi_u8 * puc_bufout)54 hi_u32 hmac_wpi_encrypt(hmac_wapi_crypt_stru wpi_key, hi_u8 *puc_bufin, hi_u32 buflen, hi_u8 *puc_bufout)
55 {
56 hi_u32 aul_iv_out[4] = { 0 }; /* 元素个数为4 */
57 hi_u32 *pul_in = HI_NULL;
58 hi_u32 *pul_out = HI_NULL;
59 hi_u8 *puc_out = HI_NULL;
60 hi_u8 *puc_in = HI_NULL;
61 hi_u32 counter;
62 hi_u32 comp;
63 hi_u32 loop;
64 hi_u32 aul_pr_keyin[WPI_PR_KEYIN_LEN] = { 0 };
65 hi_u8 *puc_iv = wpi_key.puc_iv;
66 hi_u8 iv_len = wpi_key.iv_len;
67 hi_u8 *puc_key = wpi_key.puc_key;
68 hi_u8 key_len = wpi_key.key_len;
69
70 if (buflen < 1) {
71 #ifdef WAPI_DEBUG_MODE
72 g_stMacDriverStats.ulsms4ofbinparminvalid++;
73 #endif
74 return HI_FAIL;
75 }
76
77 hmac_sms4_keyext(puc_key, key_len, aul_pr_keyin, WPI_PR_KEYIN_LEN);
78
79 counter = buflen / 16; /* 16 用于计算 */
80 comp = buflen % 16; /* 16 用于计算 */
81
82 /* get the iv */
83 hmac_sms4_crypt(puc_iv, iv_len, (hi_u8 *)aul_iv_out, aul_pr_keyin, WPI_PR_KEYIN_LEN);
84 pul_in = (hi_u32 *)puc_bufin;
85 pul_out = (hi_u32 *)puc_bufout;
86
87 for (loop = 0; loop < counter; loop++) {
88 pul_out[0] = pul_in[0] ^ aul_iv_out[0];
89 pul_out[1] = pul_in[1] ^ aul_iv_out[1];
90 pul_out[2] = pul_in[2] ^ aul_iv_out[2]; /* 2 元素索引 */
91 pul_out[3] = pul_in[3] ^ aul_iv_out[3]; /* 3 元素索引 */
92
93 hmac_sms4_crypt((hi_u8 *)aul_iv_out, 4, (hi_u8 *)aul_iv_out, aul_pr_keyin, WPI_PR_KEYIN_LEN); /* iv_out len 4 */
94 pul_in += 4; /* 自增4 */
95 pul_out += 4; /* 自增4 */
96 }
97
98 puc_in = (hi_u8 *)pul_in;
99 puc_out = (hi_u8 *)pul_out;
100 puc_iv = (hi_u8 *)aul_iv_out;
101
102 for (loop = 0; loop < comp; loop++) {
103 puc_out[loop] = puc_in[loop] ^ puc_iv[loop];
104 }
105
106 return HI_SUCCESS;
107 }
108
109 /* ****************************************************************************
110 功能描述 : ofb decrypt
111 输入参数 : hi_u8* puc_iv 为IV存储区起始地址
112 hi_u8* puc_bufin 为密文存储区起始地址
113 hi_u32 ul_buflen 为密文(以Byte为单位)长度
114 hi_u8* puc_key 为会话密钥存储区起始地址
115 hi_u8* puc_bufout 为明文存储区起始地址
116 修改历史 :
117 1.日 期 : 2012年5月2日
118 作 者 : HiSilicon
119 修改内容 : 新生成函数
120
121 2.日 期 : 2015年5月28日
122 作 者 : HiSilicon
123 修改内容 : 移植
124 **************************************************************************** */
hmac_wpi_decrypt(hmac_wapi_crypt_stru wpi_key,hi_u8 * puc_bufin,hi_u32 buflen,hi_u8 * puc_bufout)125 hi_u32 hmac_wpi_decrypt(hmac_wapi_crypt_stru wpi_key, hi_u8 *puc_bufin, hi_u32 buflen, hi_u8 *puc_bufout)
126 {
127 return hmac_wpi_encrypt(wpi_key, puc_bufin, buflen, puc_bufout);
128 }
129
130 /* ****************************************************************************
131 功能描述 : 计算mic
132 输入参数 : hi_u8* puc_iv 为IV存储区起始地址
133 hi_u8* pucBuf 为text存储区起始地址
134 hi_u32 ulPamclen 为text长度(以Byte为单位,且应为16Byte的整数倍)除以16的倍数
135 hi_u8* pucKey 为用于计算MIC的密钥KEY存储区起始地址
136 hi_u8* pucMic 为MIC存储区起始地址
137 修改历史 :
138 1.日 期 : 2012年5月2日
139 作 者 : HiSilicon
140 修改内容 : 新生成函数
141 **************************************************************************** */
hmac_wpi_pmac(hmac_wapi_crypt_stru wpi_key,hi_u8 * puc_buf,hi_u32 pamclen,hi_u8 * puc_mic,hi_u8 mic_len)142 hi_u32 hmac_wpi_pmac(hmac_wapi_crypt_stru wpi_key, hi_u8 *puc_buf, hi_u32 pamclen, hi_u8 *puc_mic, hi_u8 mic_len)
143 {
144 hi_u32 aul_mic_tmp[4] = { 0 }; /* 元素个数为4 */
145 hi_u32 loop;
146 hi_u32 *pul_in = HI_NULL;
147 hi_u32 aul_pr_mac_keyin[WPI_PR_KEYIN_LEN] = { 0 };
148 hi_u8 *puc_iv = wpi_key.puc_iv;
149 hi_u8 iv_len = wpi_key.iv_len;
150 hi_u8 *puc_key = wpi_key.puc_key;
151 hi_u8 key_len = wpi_key.key_len;
152
153 if (mic_len < SMS4_MIC_LEN) {
154 return HI_FAIL;
155 }
156 if ((pamclen < 1) || (pamclen > 4096)) { /* 4096 边界 */
157 return HI_FAIL;
158 }
159
160 hmac_sms4_keyext(puc_key, key_len, aul_pr_mac_keyin, WPI_PR_KEYIN_LEN);
161 pul_in = (hi_u32 *)puc_buf;
162 hmac_sms4_crypt(puc_iv, iv_len, (hi_u8 *)aul_mic_tmp, aul_pr_mac_keyin, WPI_PR_KEYIN_LEN);
163
164 for (loop = 0; loop < pamclen; loop++) {
165 aul_mic_tmp[0] ^= pul_in[0];
166 aul_mic_tmp[1] ^= pul_in[1];
167 aul_mic_tmp[2] ^= pul_in[2]; /* 2 元素索引 */
168 aul_mic_tmp[3] ^= pul_in[3]; /* 3 元素索引 */
169 pul_in += 4; /* 自增4 */
170 hmac_sms4_crypt((hi_u8 *)aul_mic_tmp, 4, (hi_u8 *)aul_mic_tmp, aul_pr_mac_keyin, WPI_PR_KEYIN_LEN); /* len 4 */
171 }
172
173 pul_in = (hi_u32 *)puc_mic;
174 pul_in[0] = aul_mic_tmp[0];
175 pul_in[1] = aul_mic_tmp[1];
176 pul_in[2] = aul_mic_tmp[2]; /* 2 元素索引 */
177 pul_in[3] = aul_mic_tmp[3]; /* 3 元素索引 */
178
179 return HI_SUCCESS;
180 }
181
182 #ifdef __cplusplus
183 #if __cplusplus
184 }
185 #endif
186 #endif
187