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 #ifndef __HMAC_WAPI_H__
20 #define __HMAC_WAPI_H__
21 /* ****************************************************************************
22 1 头文件包含
23 **************************************************************************** */
24 #include "oal_net.h"
25 #include "mac_resource.h"
26 #include "hmac_vap.h"
27 #include "hmac_user.h"
28 #include "hmac_wapi_wpi.h"
29
30 #ifdef __cplusplus
31 #if __cplusplus
32 extern "C" {
33 #endif
34 #endif
35
36 /* ****************************************************************************
37 2 宏定义
38 **************************************************************************** */
39 #define WAPI_UCAST_INC 2 /* 发送或者接收单播帧,pn的步进值 */
40 #define WAPI_BCAST_INC 1 /* 发送或者接收组播帧,pn的步进值 */
41 #define WAPI_WAI_TYPE (hi_u16)0x88B4 /* wapi的以太类型 */
42
43 #define WAPI_BCAST_KEY_TYPE 1
44 #define WAPI_UCAST_KEY_TYPE 0
45
46 #define SMS4_MIC_LEN (hi_u8)16 /* SMS4封包MIC的长度 */
47
48 #define SMS4_PN_LEN 16 /* wapi pn的长度 */
49 #define SMS4_KEY_IDX 1 /* WAPI头中 keyidx占1个字节 */
50 #define SMS4_WAPI_HDR_RESERVE 1 /* WAPI头中保留字段 */
51 #define HMAC_WAPI_HDR_LEN (hi_u8)(SMS4_PN_LEN + SMS4_KEY_IDX + SMS4_WAPI_HDR_RESERVE)
52 #define WAPI_PDU_LEN 2 /* wapi头中,wapi pdu len字段所占字节数 */
53 #define SMS4_PADDING_LEN 16 /* mic data按照16字节对齐 */
54
55 #define SMS4_MIC_PART1_QOS_LEN 48 /* 按照协议,如果有qos字段,mic第一部分16字节对齐后的长度 */
56 #define SMS4_MIC_PART1_NO_QOS_LEN 32 /* 按照协议,如果没有qos字段,mic第一部分16字节对齐后的长度 */
57
58 #define WAPI_IE_VERSION 1 /* wapi的version */
59 #define WAPI_IE_VER_SIZE 2 /* wapi ver-ie 所占字节数 */
60 #define WAPI_IE_SUIT_TYPE_SIZE 1 /* suit type size */
61 #define WAPI_IE_WAPICAP_SIZE 2 /* wapi cap字段所占字节数 */
62 #define WAPI_IE_BKIDCNT_SIZE 2 /* wapi bkid数字段所占字节数 */
63 #define WAPI_IE_BKID_SIZE 16 /* 一个bkid所占字节数 */
64 #define WAPI_IE_OUI_SIZE 3 /* wapi oui字节数 */
65 #define WAPI_IE_SMS4 1 /* wapi加密类型为sms4 */
66 #define WAPI_IE_SUITCNT_SIZE 2 /* wapi suit count所占字节数 */
67 /* wapi key len */
68 #define WAPI_MIC_SEQ_CONTROL_LEN 2
69
70 #define wapi_is_port_valid(wapi) ((wapi)->port_valid)
71
72 #define wapi_is_work(pst_hmac_vap) ((pst_hmac_vap)->wapi)
73 #define padding(x, size) (((x) + (size) - 1) & (~ ((size) - 1)))
74
75 #ifdef _PRE_WAPI_DEBUG
76 #define wapi_tx_drop_inc(pst_wapi) pst_wapi->debug.ultx_ucast_drop++
77 #define wapi_tx_wai_inc(pst_wapi) pst_wapi->debug.ultx_wai++
78 #define wapi_tx_port_valid(pst_wapi) pst_wapi->debug.ultx_port_valid++
79 #define wapi_rx_port_valid(wapi) wapi->debug.ulrx_port_valid++
80 #define wapi_rx_idx_err(wapi) wapi->debug.ulrx_idx_err++
81 #define wapi_rx_netbuf_len_err(wapi) wapi->debug.ulrx_netbuff_len_err++
82 #define wapi_rx_idx_update_err(wapi) wapi->debug.ulrx_idx_update_err++
83 #define wapi_rx_key_en_err(pst_wapi) pst_wapi->debug.ulrx_key_en_err++
84 #define wapi_rx_memalloc_err(wapi) wapi->debug.ulrx_memalloc_err++
85 #define wapi_rx_mic_err(wapi) wapi->debug.ulrx_mic_calc_fail++
86 #define wapi_rx_decrypt_ok(wapi) wapi->debug.ulrx_decrypt_ok++
87 #define wapi_tx_memalloc_err(wapi) wapi->debug.ultx_memalloc_err++
88 #define wapi_tx_mic_err(wapi) wapi->debug.ultx_mic_calc_fail++
89 #define wapi_tx_encrypt_ok(wapi) wapi->debug.ultx_encrypt_ok++
90 #else
91 #define wapi_tx_drop_inc(pst_wapi)
92 #define wapi_tx_wai_inc(pst_wapi)
93 #define wapi_tx_port_valid(pst_wapi)
94 #define wapi_rx_port_valid(pst_wapi)
95 #define wapi_rx_idx_err(pst_wapi)
96 #define wapi_rx_netbuf_len_err(pst_wapi)
97 #define wapi_rx_idx_update_err(pst_wapi)
98 #define wapi_rx_key_en_err(pst_wapi)
99 #define wapi_rx_memalloc_err(pst_wapi)
100 #define wapi_rx_mic_err(pst_wapi)
101 #define wapi_rx_decrypt_ok(pst_wapi)
102 #define wapi_tx_memalloc_err(pst_wapi)
103 #define wapi_tx_mic_err(pst_wapi)
104 #define wapi_tx_wai_drop_inc(pst_wapi)
105 #define wapi_tx_encrypt_ok(pst_wapi)
106 #endif /* #ifdef WAPI_DEBUG_MODE */
107
108 /* ****************************************************************************
109 3 STRUCT定义
110 **************************************************************************** */
111 typedef struct {
112 hi_u8 auc_framectl[2]; /* 帧控制 2: 元素个数 */
113 hi_u8 auc_adress1[OAL_MAC_ADDR_LEN]; /* 地址1 */
114 hi_u8 auc_adress2[OAL_MAC_ADDR_LEN]; /* 地址2 */
115 hi_u8 auc_seqctl[2]; /* 序列控制 2: 元素个数 */
116 hi_u8 auc_adress3[OAL_MAC_ADDR_LEN]; /* 地址3 */
117 hi_u8 auc_adress4[OAL_MAC_ADDR_LEN]; /* 地址4 */
118 } wapi_mic_hdr_stru;
119
120 typedef struct {
121 hi_u16 us_mic_len;
122 hi_u16 pdu_len;
123 hi_u8 auc_calc_mic[SMS4_MIC_LEN];
124 hi_u8 auc_pn_swap[SMS4_PN_LEN]; /* 保存变换后的pn,用来计算mic和加密 */
125 hmac_wapi_crypt_stru wpi_key_ck;
126 hmac_wapi_crypt_stru wpi_key_ek;
127 hi_u8 key_index;
128 hi_u8 mac_hdr_len;
129 } hmac_wapi_encrypt_stru;
130
131 typedef struct {
132 hi_u8 *puc_mic;
133 hi_u16 us_mic_len;
134 hi_u8 rsv[2]; /* 2 字节补齐 */
135 } mic_date_stru;
136
137 /* ****************************************************************************
138 4 函数声明
139 **************************************************************************** */
140 hi_u32 hmac_wapi_deinit(hmac_wapi_stru *wapi);
141 hi_u32 hmac_wapi_init(hmac_wapi_stru *wapi, hi_u8 pairwise);
142 #ifdef _PRE_WAPI_DEBUG
143 hi_u32 hmac_wapi_display_info(mac_vap_stru *mac_vap, hi_u16 us_usr_idx);
144 #endif /* #ifdef _PRE_DEBUG_MODE */
145 hi_u32 hmac_wapi_add_key(hmac_wapi_stru *wapi, hi_u8 key_index, const hi_u8 *puc_key);
146
147 /* ****************************************************************************
148 功能描述 : 将port重置
149 修改历史 :
150 1.日 期 : 2015年5月26日
151 作 者 : HiSilicon
152 修改内容 : 新生成函数
153 **************************************************************************** */
hmac_wapi_reset_port(hmac_wapi_stru * wapi)154 static inline hi_void hmac_wapi_reset_port(hmac_wapi_stru *wapi)
155 {
156 wapi->port_valid = HI_FALSE;
157 }
158
159 #ifdef __cplusplus
160 #if __cplusplus
161 }
162 #endif
163 #endif
164
165 #endif /* __HMAC_WAPI_H__ */
166