// SPDX-License-Identifier: GPL-2.0 /****************************************************************************** * * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved. * ******************************************************************************/ #include <linux/crc32.h> #include <drv_types.h> #include <rtw_debug.h> #include <crypto/aes.h> static const char * const _security_type_str[] = …; const char *security_type_str(u8 value) { … } /* WEP related ===== */ /* Need to consider the fragment situation */ void rtw_wep_encrypt(struct adapter *padapter, u8 *pxmitframe) { … } void rtw_wep_decrypt(struct adapter *padapter, u8 *precvframe) { … } /* 3 =====TKIP related ===== */ static u32 secmicgetuint32(u8 *p) /* Convert from Byte[] to Us3232 in a portable way */ { … } static void secmicputuint32(u8 *p, u32 val) /* Convert from Us3232 to Byte[] in a portable way */ { … } static void secmicclear(struct mic_data *pmicdata) { … } void rtw_secmicsetkey(struct mic_data *pmicdata, u8 *key) { … } void rtw_secmicappendbyte(struct mic_data *pmicdata, u8 b) { … } void rtw_secmicappend(struct mic_data *pmicdata, u8 *src, u32 nbytes) { … } void rtw_secgetmic(struct mic_data *pmicdata, u8 *dst) { … } void rtw_seccalctkipmic(u8 *key, u8 *header, u8 *data, u32 data_len, u8 *mic_code, u8 pri) { … } /* macros for extraction/creation of unsigned char/unsigned short values */ #define RotR1(v16) … #define Lo8(v16) … #define Hi8(v16) … #define Lo16(v32) … #define Hi16(v32) … #define Mk16(hi, lo) … /* select the Nth 16-bit word of the temporal key unsigned char array TK[] */ #define TK16(N) … /* S-box lookup: 16 bits --> 16 bits */ #define _S_(v16) … /* fixed algorithm "parameters" */ #define PHASE1_LOOP_CNT … /* 2-unsigned char by 2-unsigned char subset of the full AES S-box table */ static const unsigned short Sbox1[2][256] = …; /* ********************************************************************** * Routine: Phase 1 -- generate P1K, given TA, TK, IV32 * * Inputs: * tk[] = temporal key [128 bits] * ta[] = transmitter's MAC address [ 48 bits] * iv32 = upper 32 bits of IV [ 32 bits] * Output: * p1k[] = Phase 1 key [ 80 bits] * * Note: * This function only needs to be called every 2**16 packets, * although in theory it could be called every packet. * ********************************************************************** */ static void phase1(u16 *p1k, const u8 *tk, const u8 *ta, u32 iv32) { … } /* ********************************************************************** * Routine: Phase 2 -- generate RC4KEY, given TK, P1K, IV16 * * Inputs: * tk[] = Temporal key [128 bits] * p1k[] = Phase 1 output key [ 80 bits] * iv16 = low 16 bits of IV counter [ 16 bits] * Output: * rc4key[] = the key used to encrypt the packet [128 bits] * * Note: * The value {TA, IV32, IV16} for Phase1/Phase2 must be unique * across all packets using the same key TK value. Then, for a * given value of TK[], this TKIP48 construction guarantees that * the final RC4KEY value is unique across all packets. * * Suggested implementation optimization: if PPK[] is "overlaid" * appropriately on RC4KEY[], there is no need for the final * for loop below that copies the PPK[] result into RC4KEY[]. * ********************************************************************** */ static void phase2(u8 *rc4key, const u8 *tk, const u16 *p1k, u16 iv16) { … } /* The hlen isn't include the IV */ u32 rtw_tkip_encrypt(struct adapter *padapter, u8 *pxmitframe) { … } /* The hlen isn't include the IV */ u32 rtw_tkip_decrypt(struct adapter *padapter, u8 *precvframe) { … } /* 3 =====AES related ===== */ #define MAX_MSG_SIZE … /*****************************/ /**** Function Prototypes ****/ /*****************************/ static void bitwise_xor(u8 *ina, u8 *inb, u8 *out); static void construct_mic_iv(u8 *mic_header1, signed int qc_exists, signed int a4_exists, u8 *mpdu, uint payload_length, u8 *pn_vector, uint frtype); /* add for CONFIG_IEEE80211W, none 11w also can use */ static void construct_mic_header1(u8 *mic_header1, signed int header_length, u8 *mpdu, uint frtype); /* for CONFIG_IEEE80211W, none 11w also can use */ static void construct_mic_header2(u8 *mic_header2, u8 *mpdu, signed int a4_exists, signed int qc_exists); static void construct_ctr_preload(u8 *ctr_preload, signed int a4_exists, signed int qc_exists, u8 *mpdu, u8 *pn_vector, signed int c, uint frtype); /* for CONFIG_IEEE80211W, none 11w also can use */ static void aes128k128d(u8 *key, u8 *data, u8 *ciphertext); /****************************************/ /* aes128k128d() */ /* Performs a 128 bit AES encrypt with */ /* 128 bit data. */ /****************************************/ static void aes128k128d(u8 *key, u8 *data, u8 *ciphertext) { … } /************************************************/ /* construct_mic_iv() */ /* Builds the MIC IV from header fields and PN */ /* Baron think the function is construct CCM */ /* nonce */ /************************************************/ static void construct_mic_iv(u8 *mic_iv, signed int qc_exists, signed int a4_exists, u8 *mpdu, uint payload_length, u8 *pn_vector, uint frtype) /* add for CONFIG_IEEE80211W, none 11w also can use */ { … } /************************************************/ /* construct_mic_header1() */ /* Builds the first MIC header block from */ /* header fields. */ /* Build AAD SC, A1, A2 */ /************************************************/ static void construct_mic_header1(u8 *mic_header1, signed int header_length, u8 *mpdu, uint frtype) /* for CONFIG_IEEE80211W, none 11w also can use */ { … } /************************************************/ /* construct_mic_header2() */ /* Builds the last MIC header block from */ /* header fields. */ /************************************************/ static void construct_mic_header2(u8 *mic_header2, u8 *mpdu, signed int a4_exists, signed int qc_exists) { … } /************************************************/ /* construct_mic_header2() */ /* Builds the last MIC header block from */ /* header fields. */ /* Baron think the function is construct CCM */ /* nonce */ /************************************************/ static void construct_ctr_preload(u8 *ctr_preload, signed int a4_exists, signed int qc_exists, u8 *mpdu, u8 *pn_vector, signed int c, uint frtype) /* for CONFIG_IEEE80211W, none 11w also can use */ { … } /************************************/ /* bitwise_xor() */ /* A 128 bit, bitwise exclusive or */ /************************************/ static void bitwise_xor(u8 *ina, u8 *inb, u8 *out) { … } static signed int aes_cipher(u8 *key, uint hdrlen, u8 *pframe, uint plen) { … } u32 rtw_aes_encrypt(struct adapter *padapter, u8 *pxmitframe) { … } static signed int aes_decipher(u8 *key, uint hdrlen, u8 *pframe, uint plen) { … } u32 rtw_aes_decrypt(struct adapter *padapter, u8 *precvframe) { … } u32 rtw_BIP_verify(struct adapter *padapter, u8 *precvframe) { … } static void gf_mulx(u8 *pad) { … } /** * omac1_aes_128_vector - One-Key CBC MAC (OMAC1) hash with AES-128 * @key: 128-bit key for the hash operation * @num_elem: Number of elements in the data vector * @addr: Pointers to the data areas * @len: Lengths of the data blocks * @mac: Buffer for MAC (128 bits, i.e., 16 bytes) * Returns: 0 on success, -1 on failure * * This is a mode for using block cipher (AES in this case) for authentication. * OMAC1 was standardized with the name CMAC by NIST in a Special Publication * (SP) 800-38B. */ static int omac1_aes_128_vector(u8 *key, size_t num_elem, u8 *addr[], size_t *len, u8 *mac) { … } /** * omac1_aes_128 - One-Key CBC MAC (OMAC1) hash with AES-128 (aka AES-CMAC) * @key: 128-bit key for the hash operation * @data: Data buffer for which a MAC is determined * @data_len: Length of data buffer in bytes * @mac: Buffer for MAC (128 bits, i.e., 16 bytes) * Returns: 0 on success, -1 on failure * * This is a mode for using block cipher (AES in this case) for authentication. * OMAC1 was standardized with the name CMAC by NIST in a Special Publication * (SP) 800-38B. * modify for CONFIG_IEEE80211W */ int omac1_aes_128(u8 *key, u8 *data, size_t data_len, u8 *mac) { … } /* Restore HW wep key setting according to key_mask */ void rtw_sec_restore_wep_key(struct adapter *adapter) { … } u8 rtw_handle_tkip_countermeasure(struct adapter *adapter, const char *caller) { … }