You've already forked wifi-rtl8852bs
mirror of
https://github.com/armbian/wifi-rtl8852bs.git
synced 2026-01-06 11:09:01 -08:00
Update for v1.0alpha2
This commit is contained in:
5
Kconfig
Normal file
5
Kconfig
Normal file
@@ -0,0 +1,5 @@
|
||||
config RTL8852BS
|
||||
tristate "Realtek 8852B SDIO Wireless driver"
|
||||
depends on WLAN && MMC && CFG80211
|
||||
help
|
||||
This option enables support for RTL8852BS SDIO drivers
|
||||
5
clean
Executable file
5
clean
Executable file
@@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
rmmod 8192cu
|
||||
rmmod 8192ce
|
||||
rmmod 8192du
|
||||
rmmod 8192de
|
||||
151
common.mk
Normal file
151
common.mk
Normal file
@@ -0,0 +1,151 @@
|
||||
########### OS_DEP PATH #################################
|
||||
_OS_INTFS_FILES := os_dep/osdep_service.o \
|
||||
os_dep/osdep_service_linux.o \
|
||||
os_dep/linux/rtw_cfg.o \
|
||||
os_dep/linux/os_intfs.o \
|
||||
os_dep/linux/ioctl_linux.o \
|
||||
os_dep/linux/xmit_linux.o \
|
||||
os_dep/linux/mlme_linux.o \
|
||||
os_dep/linux/recv_linux.o \
|
||||
os_dep/linux/ioctl_cfg80211.o \
|
||||
os_dep/linux/rtw_cfgvendor.o \
|
||||
os_dep/linux/os_ch_utils.o \
|
||||
os_dep/linux/wifi_regd.o \
|
||||
os_dep/linux/rtw_android.o \
|
||||
os_dep/linux/rtw_proc.o \
|
||||
os_dep/linux/nlrtw.o \
|
||||
os_dep/linux/rtw_rhashtable.o
|
||||
|
||||
ifeq ($(CONFIG_HWSIM), y)
|
||||
_OS_INTFS_FILES += os_dep/linux/hwsim/medium/local.o
|
||||
_OS_INTFS_FILES += os_dep/linux/hwsim/medium/sock_udp.o
|
||||
_OS_INTFS_FILES += os_dep/linux/hwsim/medium/loopback.o
|
||||
_OS_INTFS_FILES += os_dep/linux/hwsim/core.o
|
||||
_OS_INTFS_FILES += os_dep/linux/hwsim/txrx.o
|
||||
_OS_INTFS_FILES += os_dep/linux/hwsim/netdev.o
|
||||
_OS_INTFS_FILES += os_dep/linux/hwsim/cfg80211.o
|
||||
_OS_INTFS_FILES += os_dep/linux/hwsim/platform_dev.o
|
||||
|
||||
_OS_INTFS_FILES += os_dep/linux/$(HCI_NAME)_ops_linux.o
|
||||
else
|
||||
_OS_INTFS_FILES += os_dep/linux/$(HCI_NAME)_intf.o
|
||||
_OS_INTFS_FILES += os_dep/linux/$(HCI_NAME)_ops_linux.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_MP_INCLUDED), y)
|
||||
_OS_INTFS_FILES += os_dep/linux/ioctl_mp.o \
|
||||
os_dep/linux/ioctl_efuse.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_SDIO_HCI), y)
|
||||
_OS_INTFS_FILES += os_dep/linux/custom_gpio_linux.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_GSPI_HCI), y)
|
||||
_OS_INTFS_FILES += os_dep/linux/custom_gpio_linux.o
|
||||
endif
|
||||
|
||||
########### CORE PATH #################################
|
||||
_CORE_FILES := core/rtw_cmd.o \
|
||||
core/rtw_security.o \
|
||||
core/rtw_debug.o \
|
||||
core/rtw_io.o \
|
||||
core/rtw_ioctl_query.o \
|
||||
core/rtw_ioctl_set.o \
|
||||
core/rtw_ieee80211.o \
|
||||
core/rtw_mlme.o \
|
||||
core/rtw_mlme_ext.o \
|
||||
core/rtw_sec_cam.o \
|
||||
core/rtw_mi.o \
|
||||
core/rtw_wlan_util.o \
|
||||
core/rtw_vht.o \
|
||||
core/rtw_he.o \
|
||||
core/rtw_eht.o \
|
||||
core/rtw_pwrctrl.o \
|
||||
core/rtw_rf.o \
|
||||
core/rtw_chplan.o \
|
||||
core/rtw_regdb_$(CONFIG_RTW_REGDB).o \
|
||||
core/rtw_chset.o \
|
||||
core/rtw_dfs.o \
|
||||
core/rtw_txpwr.o \
|
||||
core/monitor/rtw_radiotap.o \
|
||||
core/rtw_recv.o \
|
||||
core/rtw_recv_shortcut.o \
|
||||
core/rtw_sta_mgt.o \
|
||||
core/rtw_ap.o \
|
||||
core/rtw_csa.o \
|
||||
core/wds/rtw_wds.o \
|
||||
core/mesh/rtw_mesh.o \
|
||||
core/mesh/rtw_mesh_pathtbl.o \
|
||||
core/mesh/rtw_mesh_hwmp.o \
|
||||
core/rtw_xmit.o \
|
||||
core/rtw_xmit_shortcut.o \
|
||||
core/rtw_p2p.o \
|
||||
core/rtw_tdls.o \
|
||||
core/rtw_br_ext.o \
|
||||
core/rtw_sreset.o \
|
||||
core/rtw_rm.o \
|
||||
core/rtw_rm_fsm.o \
|
||||
core/rtw_rm_util.o \
|
||||
core/rtw_trx.o \
|
||||
core/rtw_beamforming.o \
|
||||
core/rtw_scan.o
|
||||
#core/efuse/rtw_efuse.o
|
||||
|
||||
_CORE_FILES += core/rtw_phl.o \
|
||||
core/rtw_phl_cmd.o
|
||||
|
||||
EXTRA_CFLAGS += -I$(src)/core/crypto
|
||||
_CORE_FILES += core/crypto/aes-internal.o \
|
||||
core/crypto/aes-internal-enc.o \
|
||||
core/crypto/aes-gcm.o \
|
||||
core/crypto/aes-ccm.o \
|
||||
core/crypto/aes-omac1.o \
|
||||
core/crypto/ccmp.o \
|
||||
core/crypto/gcmp.o \
|
||||
core/crypto/aes-siv.o \
|
||||
core/crypto/aes-ctr.o \
|
||||
core/crypto/sha256-internal.o \
|
||||
core/crypto/sha256.o \
|
||||
core/crypto/sha256-prf.o \
|
||||
core/crypto/rtw_crypto_wrap.o \
|
||||
core/rtw_swcrypto.o
|
||||
|
||||
ifeq ($(CONFIG_WOWLAN), y)
|
||||
_CORE_FILES += core/rtw_wow.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_PCI_HCI), y)
|
||||
_CORE_FILES += core/rtw_trx_pci.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_USB_HCI), y)
|
||||
_CORE_FILES += core/rtw_trx_usb.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_SDIO_HCI), y)
|
||||
_CORE_FILES += core/rtw_sdio.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_FPGA_INCLUDED), y)
|
||||
_CORE_FILES += core/rtw_fpga.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_MP_INCLUDED), y)
|
||||
_CORE_FILES += core/rtw_mp.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_WAPI_SUPPORT), y)
|
||||
_CORE_FILES += core/rtw_wapi.o \
|
||||
core/rtw_wapi_sms4.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_BTC), y)
|
||||
_CORE_FILES += core/rtw_btc.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_RTW_MBO), y)
|
||||
_CORE_FILES += core/rtw_mbo.o core/rtw_ft.o core/rtw_wnm.o
|
||||
endif
|
||||
|
||||
OBJS += $(_OS_INTFS_FILES) $(_CORE_FILES)
|
||||
211
core/crypto/aes-ccm.c
Normal file
211
core/crypto/aes-ccm.c
Normal file
@@ -0,0 +1,211 @@
|
||||
/*
|
||||
* Counter with CBC-MAC (CCM) with AES
|
||||
*
|
||||
* Copyright (c) 2010-2012, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#include "rtw_crypto_wrap.h"
|
||||
|
||||
#include "aes.h"
|
||||
#include "aes_wrap.h"
|
||||
|
||||
|
||||
static void xor_aes_block(u8 *dst, const u8 *src)
|
||||
{
|
||||
u32 *d = (u32 *) dst;
|
||||
u32 *s = (u32 *) src;
|
||||
*d++ ^= *s++;
|
||||
*d++ ^= *s++;
|
||||
*d++ ^= *s++;
|
||||
*d++ ^= *s++;
|
||||
}
|
||||
|
||||
|
||||
static void aes_ccm_auth_start(void *aes, size_t M, size_t L, const u8 *nonce,
|
||||
const u8 *aad, size_t aad_len, size_t plain_len,
|
||||
u8 *x)
|
||||
{
|
||||
u8 aad_buf[2 * AES_BLOCK_SIZE];
|
||||
u8 b[AES_BLOCK_SIZE];
|
||||
|
||||
/* Authentication */
|
||||
/* B_0: Flags | Nonce N | l(m) */
|
||||
b[0] = aad_len ? 0x40 : 0 /* Adata */;
|
||||
b[0] |= (((M - 2) / 2) /* M' */ << 3);
|
||||
b[0] |= (L - 1) /* L' */;
|
||||
os_memcpy(&b[1], nonce, 15 - L);
|
||||
WPA_PUT_BE16(&b[AES_BLOCK_SIZE - L], plain_len);
|
||||
|
||||
wpa_hexdump_key(_MSG_EXCESSIVE_, "CCM B_0", b, AES_BLOCK_SIZE);
|
||||
aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
|
||||
|
||||
if (!aad_len)
|
||||
return;
|
||||
|
||||
WPA_PUT_BE16(aad_buf, aad_len);
|
||||
os_memcpy(aad_buf + 2, aad, aad_len);
|
||||
os_memset(aad_buf + 2 + aad_len, 0, sizeof(aad_buf) - 2 - aad_len);
|
||||
|
||||
xor_aes_block(aad_buf, x);
|
||||
aes_encrypt(aes, aad_buf, x); /* X_2 = E(K, X_1 XOR B_1) */
|
||||
|
||||
if (aad_len > AES_BLOCK_SIZE - 2) {
|
||||
xor_aes_block(&aad_buf[AES_BLOCK_SIZE], x);
|
||||
/* X_3 = E(K, X_2 XOR B_2) */
|
||||
aes_encrypt(aes, &aad_buf[AES_BLOCK_SIZE], x);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void aes_ccm_auth(void *aes, const u8 *data, size_t len, u8 *x)
|
||||
{
|
||||
size_t last = len % AES_BLOCK_SIZE;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < len / AES_BLOCK_SIZE; i++) {
|
||||
/* X_i+1 = E(K, X_i XOR B_i) */
|
||||
xor_aes_block(x, data);
|
||||
data += AES_BLOCK_SIZE;
|
||||
aes_encrypt(aes, x, x);
|
||||
}
|
||||
if (last) {
|
||||
/* XOR zero-padded last block */
|
||||
for (i = 0; i < last; i++)
|
||||
x[i] ^= *data++;
|
||||
aes_encrypt(aes, x, x);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void aes_ccm_encr_start(size_t L, const u8 *nonce, u8 *a)
|
||||
{
|
||||
/* A_i = Flags | Nonce N | Counter i */
|
||||
a[0] = L - 1; /* Flags = L' */
|
||||
os_memcpy(&a[1], nonce, 15 - L);
|
||||
}
|
||||
|
||||
|
||||
static void aes_ccm_encr(void *aes, size_t L, const u8 *in, size_t len, u8 *out,
|
||||
u8 *a)
|
||||
{
|
||||
size_t last = len % AES_BLOCK_SIZE;
|
||||
size_t i;
|
||||
|
||||
/* crypt = msg XOR (S_1 | S_2 | ... | S_n) */
|
||||
for (i = 1; i <= len / AES_BLOCK_SIZE; i++) {
|
||||
WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], i);
|
||||
/* S_i = E(K, A_i) */
|
||||
aes_encrypt(aes, a, out);
|
||||
xor_aes_block(out, in);
|
||||
out += AES_BLOCK_SIZE;
|
||||
in += AES_BLOCK_SIZE;
|
||||
}
|
||||
if (last) {
|
||||
WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], i);
|
||||
aes_encrypt(aes, a, out);
|
||||
/* XOR zero-padded last block */
|
||||
for (i = 0; i < last; i++)
|
||||
*out++ ^= *in++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void aes_ccm_encr_auth(void *aes, size_t M, u8 *x, u8 *a, u8 *auth)
|
||||
{
|
||||
size_t i;
|
||||
u8 tmp[AES_BLOCK_SIZE];
|
||||
|
||||
wpa_hexdump_key(_MSG_EXCESSIVE_, "CCM T", x, M);
|
||||
/* U = T XOR S_0; S_0 = E(K, A_0) */
|
||||
WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], 0);
|
||||
aes_encrypt(aes, a, tmp);
|
||||
for (i = 0; i < M; i++)
|
||||
auth[i] = x[i] ^ tmp[i];
|
||||
wpa_hexdump_key(_MSG_EXCESSIVE_, "CCM U", auth, M);
|
||||
}
|
||||
|
||||
|
||||
static void aes_ccm_decr_auth(void *aes, size_t M, u8 *a, const u8 *auth, u8 *t)
|
||||
{
|
||||
size_t i;
|
||||
u8 tmp[AES_BLOCK_SIZE];
|
||||
|
||||
wpa_hexdump_key(_MSG_EXCESSIVE_, "CCM U", auth, M);
|
||||
/* U = T XOR S_0; S_0 = E(K, A_0) */
|
||||
WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], 0);
|
||||
aes_encrypt(aes, a, tmp);
|
||||
for (i = 0; i < M; i++)
|
||||
t[i] = auth[i] ^ tmp[i];
|
||||
wpa_hexdump_key(_MSG_EXCESSIVE_, "CCM T", t, M);
|
||||
}
|
||||
|
||||
|
||||
/* AES-CCM with fixed L=2 and aad_len <= 30 assumption */
|
||||
int aes_ccm_ae(const u8 *key, size_t key_len, const u8 *nonce,
|
||||
size_t M, const u8 *plain, size_t plain_len,
|
||||
const u8 *aad, size_t aad_len, u8 *crypt, u8 *auth)
|
||||
{
|
||||
const size_t L = 2;
|
||||
void *aes;
|
||||
u8 x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
|
||||
|
||||
if (aad_len > 30 || M > AES_BLOCK_SIZE)
|
||||
return -1;
|
||||
|
||||
aes = aes_encrypt_init(key, key_len);
|
||||
if (aes == NULL)
|
||||
return -1;
|
||||
|
||||
aes_ccm_auth_start(aes, M, L, nonce, aad, aad_len, plain_len, x);
|
||||
aes_ccm_auth(aes, plain, plain_len, x);
|
||||
|
||||
/* Encryption */
|
||||
aes_ccm_encr_start(L, nonce, a);
|
||||
aes_ccm_encr(aes, L, plain, plain_len, crypt, a);
|
||||
aes_ccm_encr_auth(aes, M, x, a, auth);
|
||||
|
||||
aes_encrypt_deinit(aes);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* AES-CCM with fixed L=2 and aad_len <= 30 assumption */
|
||||
int aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce,
|
||||
size_t M, const u8 *crypt, size_t crypt_len,
|
||||
const u8 *aad, size_t aad_len, const u8 *auth, u8 *plain)
|
||||
{
|
||||
const size_t L = 2;
|
||||
void *aes;
|
||||
u8 x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
|
||||
u8 t[AES_BLOCK_SIZE];
|
||||
|
||||
if (aad_len > 30 || M > AES_BLOCK_SIZE)
|
||||
return -1;
|
||||
|
||||
aes = aes_encrypt_init(key, key_len);
|
||||
if (aes == NULL)
|
||||
return -1;
|
||||
|
||||
/* Decryption */
|
||||
aes_ccm_encr_start(L, nonce, a);
|
||||
aes_ccm_decr_auth(aes, M, a, auth, t);
|
||||
|
||||
/* plaintext = msg XOR (S_1 | S_2 | ... | S_n) */
|
||||
aes_ccm_encr(aes, L, crypt, crypt_len, plain, a);
|
||||
|
||||
aes_ccm_auth_start(aes, M, L, nonce, aad, aad_len, crypt_len, x);
|
||||
aes_ccm_auth(aes, plain, crypt_len, x);
|
||||
|
||||
aes_encrypt_deinit(aes);
|
||||
|
||||
if (os_memcmp_const(x, t, M) != 0) {
|
||||
wpa_printf(_MSG_EXCESSIVE_, "CCM: Auth mismatch");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
70
core/crypto/aes-ctr.c
Normal file
70
core/crypto/aes-ctr.c
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* AES-128/192/256 CTR
|
||||
*
|
||||
* Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#include "rtw_crypto_wrap.h"
|
||||
|
||||
#include "aes.h"
|
||||
#include "aes_wrap.h"
|
||||
|
||||
/**
|
||||
* aes_ctr_encrypt - AES-128/192/256 CTR mode encryption
|
||||
* @key: Key for encryption (key_len bytes)
|
||||
* @key_len: Length of the key (16, 24, or 32 bytes)
|
||||
* @nonce: Nonce for counter mode (16 bytes)
|
||||
* @data: Data to encrypt in-place
|
||||
* @data_len: Length of data in bytes
|
||||
* Returns: 0 on success, -1 on failure
|
||||
*/
|
||||
int aes_ctr_encrypt(const u8 *key, size_t key_len, const u8 *nonce,
|
||||
u8 *data, size_t data_len)
|
||||
{
|
||||
void *ctx;
|
||||
size_t j, len, left = data_len;
|
||||
int i;
|
||||
u8 *pos = data;
|
||||
u8 counter[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
|
||||
|
||||
ctx = aes_encrypt_init(key, key_len);
|
||||
if (ctx == NULL)
|
||||
return -1;
|
||||
os_memcpy(counter, nonce, AES_BLOCK_SIZE);
|
||||
|
||||
while (left > 0) {
|
||||
aes_encrypt(ctx, counter, buf);
|
||||
|
||||
len = (left < AES_BLOCK_SIZE) ? left : AES_BLOCK_SIZE;
|
||||
for (j = 0; j < len; j++)
|
||||
pos[j] ^= buf[j];
|
||||
pos += len;
|
||||
left -= len;
|
||||
|
||||
for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
|
||||
counter[i]++;
|
||||
if (counter[i])
|
||||
break;
|
||||
}
|
||||
}
|
||||
aes_encrypt_deinit(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* aes_128_ctr_encrypt - AES-128 CTR mode encryption
|
||||
* @key: Key for encryption (key_len bytes)
|
||||
* @nonce: Nonce for counter mode (16 bytes)
|
||||
* @data: Data to encrypt in-place
|
||||
* @data_len: Length of data in bytes
|
||||
* Returns: 0 on success, -1 on failure
|
||||
*/
|
||||
int aes_128_ctr_encrypt(const u8 *key, const u8 *nonce,
|
||||
u8 *data, size_t data_len)
|
||||
{
|
||||
return aes_ctr_encrypt(key, 16, nonce, data, data_len);
|
||||
}
|
||||
326
core/crypto/aes-gcm.c
Normal file
326
core/crypto/aes-gcm.c
Normal file
@@ -0,0 +1,326 @@
|
||||
/*
|
||||
* Galois/Counter Mode (GCM) and GMAC with AES
|
||||
*
|
||||
* Copyright (c) 2012, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#include "rtw_crypto_wrap.h"
|
||||
|
||||
#include "aes.h"
|
||||
#include "aes_wrap.h"
|
||||
|
||||
static void inc32(u8 *block)
|
||||
{
|
||||
u32 val;
|
||||
val = WPA_GET_BE32(block + AES_BLOCK_SIZE - 4);
|
||||
val++;
|
||||
WPA_PUT_BE32(block + AES_BLOCK_SIZE - 4, val);
|
||||
}
|
||||
|
||||
|
||||
static void xor_block(u8 *dst, const u8 *src)
|
||||
{
|
||||
u32 *d = (u32 *) dst;
|
||||
u32 *s = (u32 *) src;
|
||||
*d++ ^= *s++;
|
||||
*d++ ^= *s++;
|
||||
*d++ ^= *s++;
|
||||
*d++ ^= *s++;
|
||||
}
|
||||
|
||||
|
||||
static void shift_right_block(u8 *v)
|
||||
{
|
||||
u32 val;
|
||||
|
||||
val = WPA_GET_BE32(v + 12);
|
||||
val >>= 1;
|
||||
if (v[11] & 0x01)
|
||||
val |= 0x80000000;
|
||||
WPA_PUT_BE32(v + 12, val);
|
||||
|
||||
val = WPA_GET_BE32(v + 8);
|
||||
val >>= 1;
|
||||
if (v[7] & 0x01)
|
||||
val |= 0x80000000;
|
||||
WPA_PUT_BE32(v + 8, val);
|
||||
|
||||
val = WPA_GET_BE32(v + 4);
|
||||
val >>= 1;
|
||||
if (v[3] & 0x01)
|
||||
val |= 0x80000000;
|
||||
WPA_PUT_BE32(v + 4, val);
|
||||
|
||||
val = WPA_GET_BE32(v);
|
||||
val >>= 1;
|
||||
WPA_PUT_BE32(v, val);
|
||||
}
|
||||
|
||||
|
||||
/* Multiplication in GF(2^128) */
|
||||
static void gf_mult(const u8 *x, const u8 *y, u8 *z)
|
||||
{
|
||||
u8 v[16];
|
||||
int i, j;
|
||||
|
||||
os_memset(z, 0, 16); /* Z_0 = 0^128 */
|
||||
os_memcpy(v, y, 16); /* V_0 = Y */
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
for (j = 0; j < 8; j++) {
|
||||
if (x[i] & BIT(7 - j)) {
|
||||
/* Z_(i + 1) = Z_i XOR V_i */
|
||||
xor_block(z, v);
|
||||
} else {
|
||||
/* Z_(i + 1) = Z_i */
|
||||
}
|
||||
|
||||
if (v[15] & 0x01) {
|
||||
/* V_(i + 1) = (V_i >> 1) XOR R */
|
||||
shift_right_block(v);
|
||||
/* R = 11100001 || 0^120 */
|
||||
v[0] ^= 0xe1;
|
||||
} else {
|
||||
/* V_(i + 1) = V_i >> 1 */
|
||||
shift_right_block(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void ghash_start(u8 *y)
|
||||
{
|
||||
/* Y_0 = 0^128 */
|
||||
os_memset(y, 0, 16);
|
||||
}
|
||||
|
||||
|
||||
static void ghash(const u8 *h, const u8 *x, size_t xlen, u8 *y)
|
||||
{
|
||||
size_t m, i;
|
||||
const u8 *xpos = x;
|
||||
u8 tmp[16];
|
||||
|
||||
m = xlen / 16;
|
||||
|
||||
for (i = 0; i < m; i++) {
|
||||
/* Y_i = (Y^(i-1) XOR X_i) dot H */
|
||||
xor_block(y, xpos);
|
||||
xpos += 16;
|
||||
|
||||
/* dot operation:
|
||||
* multiplication operation for binary Galois (finite) field of
|
||||
* 2^128 elements */
|
||||
gf_mult(y, h, tmp);
|
||||
os_memcpy(y, tmp, 16);
|
||||
}
|
||||
|
||||
if (x + xlen > xpos) {
|
||||
/* Add zero padded last block */
|
||||
size_t last = x + xlen - xpos;
|
||||
os_memcpy(tmp, xpos, last);
|
||||
os_memset(tmp + last, 0, sizeof(tmp) - last);
|
||||
|
||||
/* Y_i = (Y^(i-1) XOR X_i) dot H */
|
||||
xor_block(y, tmp);
|
||||
|
||||
/* dot operation:
|
||||
* multiplication operation for binary Galois (finite) field of
|
||||
* 2^128 elements */
|
||||
gf_mult(y, h, tmp);
|
||||
os_memcpy(y, tmp, 16);
|
||||
}
|
||||
|
||||
/* Return Y_m */
|
||||
}
|
||||
|
||||
|
||||
static void aes_gctr(void *aes, const u8 *icb, const u8 *x, size_t xlen, u8 *y)
|
||||
{
|
||||
size_t i, n, last;
|
||||
u8 cb[AES_BLOCK_SIZE], tmp[AES_BLOCK_SIZE];
|
||||
const u8 *xpos = x;
|
||||
u8 *ypos = y;
|
||||
|
||||
if (xlen == 0)
|
||||
return;
|
||||
|
||||
n = xlen / 16;
|
||||
|
||||
os_memcpy(cb, icb, AES_BLOCK_SIZE);
|
||||
/* Full blocks */
|
||||
for (i = 0; i < n; i++) {
|
||||
aes_encrypt(aes, cb, ypos);
|
||||
xor_block(ypos, xpos);
|
||||
xpos += AES_BLOCK_SIZE;
|
||||
ypos += AES_BLOCK_SIZE;
|
||||
inc32(cb);
|
||||
}
|
||||
|
||||
last = x + xlen - xpos;
|
||||
if (last) {
|
||||
/* Last, partial block */
|
||||
aes_encrypt(aes, cb, tmp);
|
||||
for (i = 0; i < last; i++)
|
||||
*ypos++ = *xpos++ ^ tmp[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void * aes_gcm_init_hash_subkey(const u8 *key, size_t key_len, u8 *H)
|
||||
{
|
||||
void *aes;
|
||||
|
||||
aes = aes_encrypt_init(key, key_len);
|
||||
if (aes == NULL)
|
||||
return NULL;
|
||||
|
||||
/* Generate hash subkey H = AES_K(0^128) */
|
||||
os_memset(H, 0, AES_BLOCK_SIZE);
|
||||
aes_encrypt(aes, H, H);
|
||||
wpa_hexdump_key(_MSG_EXCESSIVE_, "Hash subkey H for GHASH",
|
||||
H, AES_BLOCK_SIZE);
|
||||
return aes;
|
||||
}
|
||||
|
||||
|
||||
static void aes_gcm_prepare_j0(const u8 *iv, size_t iv_len, const u8 *H, u8 *J0)
|
||||
{
|
||||
u8 len_buf[16];
|
||||
|
||||
if (iv_len == 12) {
|
||||
/* Prepare block J_0 = IV || 0^31 || 1 [len(IV) = 96] */
|
||||
os_memcpy(J0, iv, iv_len);
|
||||
os_memset(J0 + iv_len, 0, AES_BLOCK_SIZE - iv_len);
|
||||
J0[AES_BLOCK_SIZE - 1] = 0x01;
|
||||
} else {
|
||||
/*
|
||||
* s = 128 * ceil(len(IV)/128) - len(IV)
|
||||
* J_0 = GHASH_H(IV || 0^(s+64) || [len(IV)]_64)
|
||||
*/
|
||||
ghash_start(J0);
|
||||
ghash(H, iv, iv_len, J0);
|
||||
WPA_PUT_BE64(len_buf, 0);
|
||||
WPA_PUT_BE64(len_buf + 8, iv_len * 8);
|
||||
ghash(H, len_buf, sizeof(len_buf), J0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void aes_gcm_gctr(void *aes, const u8 *J0, const u8 *in, size_t len,
|
||||
u8 *out)
|
||||
{
|
||||
u8 J0inc[AES_BLOCK_SIZE];
|
||||
|
||||
if (len == 0)
|
||||
return;
|
||||
|
||||
os_memcpy(J0inc, J0, AES_BLOCK_SIZE);
|
||||
inc32(J0inc);
|
||||
aes_gctr(aes, J0inc, in, len, out);
|
||||
}
|
||||
|
||||
|
||||
static void aes_gcm_ghash(const u8 *H, const u8 *aad, size_t aad_len,
|
||||
const u8 *crypt, size_t crypt_len, u8 *S)
|
||||
{
|
||||
u8 len_buf[16];
|
||||
|
||||
/*
|
||||
* u = 128 * ceil[len(C)/128] - len(C)
|
||||
* v = 128 * ceil[len(A)/128] - len(A)
|
||||
* S = GHASH_H(A || 0^v || C || 0^u || [len(A)]64 || [len(C)]64)
|
||||
* (i.e., zero padded to block size A || C and lengths of each in bits)
|
||||
*/
|
||||
ghash_start(S);
|
||||
ghash(H, aad, aad_len, S);
|
||||
ghash(H, crypt, crypt_len, S);
|
||||
WPA_PUT_BE64(len_buf, aad_len * 8);
|
||||
WPA_PUT_BE64(len_buf + 8, crypt_len * 8);
|
||||
ghash(H, len_buf, sizeof(len_buf), S);
|
||||
|
||||
wpa_hexdump_key(_MSG_EXCESSIVE_, "S = GHASH_H(...)", S, 16);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* aes_gcm_ae - GCM-AE_K(IV, P, A)
|
||||
*/
|
||||
int aes_gcm_ae(const u8 *key, size_t key_len, const u8 *iv, size_t iv_len,
|
||||
const u8 *plain, size_t plain_len,
|
||||
const u8 *aad, size_t aad_len, u8 *crypt, u8 *tag)
|
||||
{
|
||||
u8 H[AES_BLOCK_SIZE];
|
||||
u8 J0[AES_BLOCK_SIZE];
|
||||
u8 S[16];
|
||||
void *aes;
|
||||
|
||||
aes = aes_gcm_init_hash_subkey(key, key_len, H);
|
||||
if (aes == NULL)
|
||||
return -1;
|
||||
|
||||
aes_gcm_prepare_j0(iv, iv_len, H, J0);
|
||||
|
||||
/* C = GCTR_K(inc_32(J_0), P) */
|
||||
aes_gcm_gctr(aes, J0, plain, plain_len, crypt);
|
||||
|
||||
aes_gcm_ghash(H, aad, aad_len, crypt, plain_len, S);
|
||||
|
||||
/* T = MSB_t(GCTR_K(J_0, S)) */
|
||||
aes_gctr(aes, J0, S, sizeof(S), tag);
|
||||
|
||||
/* Return (C, T) */
|
||||
|
||||
aes_encrypt_deinit(aes);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* aes_gcm_ad - GCM-AD_K(IV, C, A, T)
|
||||
*/
|
||||
int aes_gcm_ad(const u8 *key, size_t key_len, const u8 *iv, size_t iv_len,
|
||||
const u8 *crypt, size_t crypt_len,
|
||||
const u8 *aad, size_t aad_len, const u8 *tag, u8 *plain)
|
||||
{
|
||||
u8 H[AES_BLOCK_SIZE];
|
||||
u8 J0[AES_BLOCK_SIZE];
|
||||
u8 S[16], T[16];
|
||||
void *aes;
|
||||
|
||||
aes = aes_gcm_init_hash_subkey(key, key_len, H);
|
||||
if (aes == NULL)
|
||||
return -1;
|
||||
|
||||
aes_gcm_prepare_j0(iv, iv_len, H, J0);
|
||||
|
||||
/* P = GCTR_K(inc_32(J_0), C) */
|
||||
aes_gcm_gctr(aes, J0, crypt, crypt_len, plain);
|
||||
|
||||
aes_gcm_ghash(H, aad, aad_len, crypt, crypt_len, S);
|
||||
|
||||
/* T' = MSB_t(GCTR_K(J_0, S)) */
|
||||
aes_gctr(aes, J0, S, sizeof(S), T);
|
||||
|
||||
aes_encrypt_deinit(aes);
|
||||
|
||||
if (os_memcmp_const(tag, T, 16) != 0) {
|
||||
wpa_printf(_MSG_EXCESSIVE_, "GCM: Tag mismatch");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int aes_gmac(const u8 *key, size_t key_len, const u8 *iv, size_t iv_len,
|
||||
const u8 *aad, size_t aad_len, u8 *tag)
|
||||
{
|
||||
return aes_gcm_ae(key, key_len, iv, iv_len, NULL, 0, aad, aad_len, NULL,
|
||||
tag);
|
||||
}
|
||||
129
core/crypto/aes-internal-enc.c
Normal file
129
core/crypto/aes-internal-enc.c
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* AES (Rijndael) cipher - encrypt
|
||||
*
|
||||
* Modifications to public domain implementation:
|
||||
* - cleanup
|
||||
* - use C pre-processor to make it easier to change S table access
|
||||
* - added option (AES_SMALL_TABLES) for reducing code size by about 8 kB at
|
||||
* cost of reduced throughput (quite small difference on Pentium 4,
|
||||
* 10-25% when using -O1 or -O2 optimization)
|
||||
*
|
||||
* Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#include "rtw_crypto_wrap.h"
|
||||
|
||||
#include "aes_i.h"
|
||||
|
||||
static void rijndaelEncrypt(const u32 rk[], int Nr, const u8 pt[16], u8 ct[16])
|
||||
{
|
||||
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
||||
#ifndef FULL_UNROLL
|
||||
int r;
|
||||
#endif /* ?FULL_UNROLL */
|
||||
|
||||
/*
|
||||
* map byte array block to cipher state
|
||||
* and add initial round key:
|
||||
*/
|
||||
s0 = GETU32(pt ) ^ rk[0];
|
||||
s1 = GETU32(pt + 4) ^ rk[1];
|
||||
s2 = GETU32(pt + 8) ^ rk[2];
|
||||
s3 = GETU32(pt + 12) ^ rk[3];
|
||||
|
||||
#define ROUND(i,d,s) \
|
||||
d##0 = TE0(s##0) ^ TE1(s##1) ^ TE2(s##2) ^ TE3(s##3) ^ rk[4 * i]; \
|
||||
d##1 = TE0(s##1) ^ TE1(s##2) ^ TE2(s##3) ^ TE3(s##0) ^ rk[4 * i + 1]; \
|
||||
d##2 = TE0(s##2) ^ TE1(s##3) ^ TE2(s##0) ^ TE3(s##1) ^ rk[4 * i + 2]; \
|
||||
d##3 = TE0(s##3) ^ TE1(s##0) ^ TE2(s##1) ^ TE3(s##2) ^ rk[4 * i + 3]
|
||||
|
||||
#ifdef FULL_UNROLL
|
||||
|
||||
ROUND(1,t,s);
|
||||
ROUND(2,s,t);
|
||||
ROUND(3,t,s);
|
||||
ROUND(4,s,t);
|
||||
ROUND(5,t,s);
|
||||
ROUND(6,s,t);
|
||||
ROUND(7,t,s);
|
||||
ROUND(8,s,t);
|
||||
ROUND(9,t,s);
|
||||
if (Nr > 10) {
|
||||
ROUND(10,s,t);
|
||||
ROUND(11,t,s);
|
||||
if (Nr > 12) {
|
||||
ROUND(12,s,t);
|
||||
ROUND(13,t,s);
|
||||
}
|
||||
}
|
||||
|
||||
rk += Nr << 2;
|
||||
|
||||
#else /* !FULL_UNROLL */
|
||||
|
||||
/* Nr - 1 full rounds: */
|
||||
r = Nr >> 1;
|
||||
for (;;) {
|
||||
ROUND(1,t,s);
|
||||
rk += 8;
|
||||
if (--r == 0)
|
||||
break;
|
||||
ROUND(0,s,t);
|
||||
}
|
||||
|
||||
#endif /* ?FULL_UNROLL */
|
||||
|
||||
#undef ROUND
|
||||
|
||||
/*
|
||||
* apply last round and
|
||||
* map cipher state to byte array block:
|
||||
*/
|
||||
s0 = TE41(t0) ^ TE42(t1) ^ TE43(t2) ^ TE44(t3) ^ rk[0];
|
||||
PUTU32(ct , s0);
|
||||
s1 = TE41(t1) ^ TE42(t2) ^ TE43(t3) ^ TE44(t0) ^ rk[1];
|
||||
PUTU32(ct + 4, s1);
|
||||
s2 = TE41(t2) ^ TE42(t3) ^ TE43(t0) ^ TE44(t1) ^ rk[2];
|
||||
PUTU32(ct + 8, s2);
|
||||
s3 = TE41(t3) ^ TE42(t0) ^ TE43(t1) ^ TE44(t2) ^ rk[3];
|
||||
PUTU32(ct + 12, s3);
|
||||
}
|
||||
|
||||
|
||||
void * aes_encrypt_init(const u8 *key, size_t len)
|
||||
{
|
||||
u32 *rk;
|
||||
int res;
|
||||
|
||||
if (TEST_FAIL())
|
||||
return NULL;
|
||||
|
||||
rk = os_malloc(AES_PRIV_SIZE);
|
||||
if (rk == NULL)
|
||||
return NULL;
|
||||
res = rijndaelKeySetupEnc(rk, key, len * 8);
|
||||
if (res < 0) {
|
||||
rtw_mfree(rk, AES_PRIV_SIZE);
|
||||
return NULL;
|
||||
}
|
||||
rk[AES_PRIV_NR_POS] = res;
|
||||
return rk;
|
||||
}
|
||||
|
||||
|
||||
int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
|
||||
{
|
||||
u32 *rk = ctx;
|
||||
rijndaelEncrypt(ctx, rk[AES_PRIV_NR_POS], plain, crypt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void aes_encrypt_deinit(void *ctx)
|
||||
{
|
||||
os_memset(ctx, 0, AES_PRIV_SIZE);
|
||||
rtw_mfree(ctx, AES_PRIV_SIZE);
|
||||
}
|
||||
843
core/crypto/aes-internal.c
Normal file
843
core/crypto/aes-internal.c
Normal file
File diff suppressed because it is too large
Load Diff
172
core/crypto/aes-omac1.c
Normal file
172
core/crypto/aes-omac1.c
Normal file
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* One-key CBC MAC (OMAC1) hash with AES
|
||||
*
|
||||
* Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#include "rtw_crypto_wrap.h"
|
||||
|
||||
#include "aes.h"
|
||||
#include "aes_wrap.h"
|
||||
|
||||
static void gf_mulx(u8 *pad)
|
||||
{
|
||||
int i, carry;
|
||||
|
||||
carry = pad[0] & 0x80;
|
||||
for (i = 0; i < AES_BLOCK_SIZE - 1; i++)
|
||||
pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
|
||||
pad[AES_BLOCK_SIZE - 1] <<= 1;
|
||||
if (carry)
|
||||
pad[AES_BLOCK_SIZE - 1] ^= 0x87;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* omac1_aes_vector - One-Key CBC MAC (OMAC1) hash with AES
|
||||
* @key: Key for the hash operation
|
||||
* @key_len: Key length in octets
|
||||
* @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.
|
||||
*/
|
||||
int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
|
||||
const u8 *addr[], const size_t *len, u8 *mac)
|
||||
{
|
||||
void *ctx;
|
||||
u8 cbc[AES_BLOCK_SIZE], pad[AES_BLOCK_SIZE];
|
||||
const u8 *pos, *end;
|
||||
size_t i, e, left, total_len;
|
||||
|
||||
if (TEST_FAIL())
|
||||
return -1;
|
||||
|
||||
ctx = aes_encrypt_init(key, key_len);
|
||||
if (ctx == NULL)
|
||||
return -1;
|
||||
os_memset(cbc, 0, AES_BLOCK_SIZE);
|
||||
|
||||
total_len = 0;
|
||||
for (e = 0; e < num_elem; e++)
|
||||
total_len += len[e];
|
||||
left = total_len;
|
||||
|
||||
e = 0;
|
||||
pos = addr[0];
|
||||
end = pos + len[0];
|
||||
|
||||
while (left >= AES_BLOCK_SIZE) {
|
||||
for (i = 0; i < AES_BLOCK_SIZE; i++) {
|
||||
cbc[i] ^= *pos++;
|
||||
if (pos >= end) {
|
||||
/*
|
||||
* Stop if there are no more bytes to process
|
||||
* since there are no more entries in the array.
|
||||
*/
|
||||
if (i + 1 == AES_BLOCK_SIZE &&
|
||||
left == AES_BLOCK_SIZE)
|
||||
break;
|
||||
e++;
|
||||
pos = addr[e];
|
||||
end = pos + len[e];
|
||||
}
|
||||
}
|
||||
if (left > AES_BLOCK_SIZE)
|
||||
aes_encrypt(ctx, cbc, cbc);
|
||||
left -= AES_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
os_memset(pad, 0, AES_BLOCK_SIZE);
|
||||
aes_encrypt(ctx, pad, pad);
|
||||
gf_mulx(pad);
|
||||
|
||||
if (left || total_len == 0) {
|
||||
for (i = 0; i < left; i++) {
|
||||
cbc[i] ^= *pos++;
|
||||
if (pos >= end) {
|
||||
/*
|
||||
* Stop if there are no more bytes to process
|
||||
* since there are no more entries in the array.
|
||||
*/
|
||||
if (i + 1 == left)
|
||||
break;
|
||||
e++;
|
||||
pos = addr[e];
|
||||
end = pos + len[e];
|
||||
}
|
||||
}
|
||||
cbc[left] ^= 0x80;
|
||||
gf_mulx(pad);
|
||||
}
|
||||
|
||||
for (i = 0; i < AES_BLOCK_SIZE; i++)
|
||||
pad[i] ^= cbc[i];
|
||||
aes_encrypt(ctx, pad, mac);
|
||||
aes_encrypt_deinit(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
int omac1_aes_128_vector(const u8 *key, size_t num_elem,
|
||||
const u8 *addr[], const size_t *len, u8 *mac)
|
||||
{
|
||||
return omac1_aes_vector(key, 16, num_elem, addr, len, 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.
|
||||
*/
|
||||
int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
|
||||
{
|
||||
return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* omac1_aes_256 - One-Key CBC MAC (OMAC1) hash with AES-256 (aka AES-CMAC)
|
||||
* @key: 256-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.
|
||||
*/
|
||||
int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
|
||||
{
|
||||
return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
|
||||
}
|
||||
207
core/crypto/aes-siv.c
Normal file
207
core/crypto/aes-siv.c
Normal file
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
* AES SIV (RFC 5297)
|
||||
* Copyright (c) 2013 Cozybit, Inc.
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#include "rtw_crypto_wrap.h"
|
||||
|
||||
#include "aes.h"
|
||||
#include "aes_wrap.h"
|
||||
#include "aes_siv.h"
|
||||
|
||||
|
||||
static const u8 zero[AES_BLOCK_SIZE];
|
||||
|
||||
|
||||
static void dbl(u8 *pad)
|
||||
{
|
||||
int i, carry;
|
||||
|
||||
carry = pad[0] & 0x80;
|
||||
for (i = 0; i < AES_BLOCK_SIZE - 1; i++)
|
||||
pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
|
||||
pad[AES_BLOCK_SIZE - 1] <<= 1;
|
||||
if (carry)
|
||||
pad[AES_BLOCK_SIZE - 1] ^= 0x87;
|
||||
}
|
||||
|
||||
|
||||
static void xor(u8 *a, const u8 *b)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < AES_BLOCK_SIZE; i++)
|
||||
*a++ ^= *b++;
|
||||
}
|
||||
|
||||
|
||||
static void xorend(u8 *a, int alen, const u8 *b, int blen)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (alen < blen)
|
||||
return;
|
||||
|
||||
for (i = 0; i < blen; i++)
|
||||
a[alen - blen + i] ^= b[i];
|
||||
}
|
||||
|
||||
|
||||
static void pad_block(u8 *pad, const u8 *addr, size_t len)
|
||||
{
|
||||
os_memset(pad, 0, AES_BLOCK_SIZE);
|
||||
os_memcpy(pad, addr, len);
|
||||
|
||||
if (len < AES_BLOCK_SIZE)
|
||||
pad[len] = 0x80;
|
||||
}
|
||||
|
||||
|
||||
static int aes_s2v(const u8 *key, size_t key_len,
|
||||
size_t num_elem, const u8 *addr[], size_t *len, u8 *mac)
|
||||
{
|
||||
u8 tmp[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
|
||||
u8 *buf = NULL;
|
||||
int ret;
|
||||
size_t i;
|
||||
const u8 *data[1];
|
||||
size_t data_len[1];
|
||||
|
||||
if (!num_elem) {
|
||||
os_memcpy(tmp, zero, sizeof(zero));
|
||||
tmp[AES_BLOCK_SIZE - 1] = 1;
|
||||
data[0] = tmp;
|
||||
data_len[0] = sizeof(tmp);
|
||||
return omac1_aes_vector(key, key_len, 1, data, data_len, mac);
|
||||
}
|
||||
|
||||
data[0] = zero;
|
||||
data_len[0] = sizeof(zero);
|
||||
ret = omac1_aes_vector(key, key_len, 1, data, data_len, tmp);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
for (i = 0; i < num_elem - 1; i++) {
|
||||
ret = omac1_aes_vector(key, key_len, 1, &addr[i], &len[i],
|
||||
tmp2);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
dbl(tmp);
|
||||
xor(tmp, tmp2);
|
||||
}
|
||||
if (len[i] >= AES_BLOCK_SIZE) {
|
||||
buf = os_memdup(addr[i], len[i]);
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
xorend(buf, len[i], tmp, AES_BLOCK_SIZE);
|
||||
data[0] = buf;
|
||||
ret = omac1_aes_vector(key, key_len, 1, data, &len[i], mac);
|
||||
bin_clear_free(buf, len[i]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
dbl(tmp);
|
||||
pad_block(tmp2, addr[i], len[i]);
|
||||
xor(tmp, tmp2);
|
||||
|
||||
data[0] = tmp;
|
||||
data_len[0] = sizeof(tmp);
|
||||
return omac1_aes_vector(key, key_len, 1, data, data_len, mac);
|
||||
}
|
||||
|
||||
|
||||
int aes_siv_encrypt(const u8 *key, size_t key_len,
|
||||
const u8 *pw, size_t pwlen,
|
||||
size_t num_elem, const u8 *addr[], const size_t *len,
|
||||
u8 *out)
|
||||
{
|
||||
const u8 *_addr[6];
|
||||
size_t _len[6];
|
||||
const u8 *k1, *k2;
|
||||
u8 v[AES_BLOCK_SIZE];
|
||||
size_t i;
|
||||
u8 *iv, *crypt_pw;
|
||||
|
||||
if (num_elem > ARRAY_SIZE(_addr) - 1 ||
|
||||
(key_len != 32 && key_len != 48 && key_len != 64))
|
||||
return -1;
|
||||
|
||||
key_len /= 2;
|
||||
k1 = key;
|
||||
k2 = key + key_len;
|
||||
|
||||
for (i = 0; i < num_elem; i++) {
|
||||
_addr[i] = addr[i];
|
||||
_len[i] = len[i];
|
||||
}
|
||||
_addr[num_elem] = pw;
|
||||
_len[num_elem] = pwlen;
|
||||
|
||||
if (aes_s2v(k1, key_len, num_elem + 1, _addr, _len, v))
|
||||
return -1;
|
||||
|
||||
iv = out;
|
||||
crypt_pw = out + AES_BLOCK_SIZE;
|
||||
|
||||
os_memcpy(iv, v, AES_BLOCK_SIZE);
|
||||
os_memcpy(crypt_pw, pw, pwlen);
|
||||
|
||||
/* zero out 63rd and 31st bits of ctr (from right) */
|
||||
v[8] &= 0x7f;
|
||||
v[12] &= 0x7f;
|
||||
return aes_ctr_encrypt(k2, key_len, v, crypt_pw, pwlen);
|
||||
}
|
||||
|
||||
|
||||
int aes_siv_decrypt(const u8 *key, size_t key_len,
|
||||
const u8 *iv_crypt, size_t iv_c_len,
|
||||
size_t num_elem, const u8 *addr[], const size_t *len,
|
||||
u8 *out)
|
||||
{
|
||||
const u8 *_addr[6];
|
||||
size_t _len[6];
|
||||
const u8 *k1, *k2;
|
||||
size_t crypt_len;
|
||||
size_t i;
|
||||
int ret;
|
||||
u8 iv[AES_BLOCK_SIZE];
|
||||
u8 check[AES_BLOCK_SIZE];
|
||||
|
||||
if (iv_c_len < AES_BLOCK_SIZE || num_elem > ARRAY_SIZE(_addr) - 1 ||
|
||||
(key_len != 32 && key_len != 48 && key_len != 64))
|
||||
return -1;
|
||||
crypt_len = iv_c_len - AES_BLOCK_SIZE;
|
||||
key_len /= 2;
|
||||
k1 = key;
|
||||
k2 = key + key_len;
|
||||
|
||||
for (i = 0; i < num_elem; i++) {
|
||||
_addr[i] = addr[i];
|
||||
_len[i] = len[i];
|
||||
}
|
||||
_addr[num_elem] = out;
|
||||
_len[num_elem] = crypt_len;
|
||||
|
||||
os_memcpy(iv, iv_crypt, AES_BLOCK_SIZE);
|
||||
os_memcpy(out, iv_crypt + AES_BLOCK_SIZE, crypt_len);
|
||||
|
||||
iv[8] &= 0x7f;
|
||||
iv[12] &= 0x7f;
|
||||
|
||||
ret = aes_ctr_encrypt(k2, key_len, iv, out, crypt_len);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
ret = aes_s2v(k1, key_len, num_elem + 1, _addr, _len, check);
|
||||
if (ret)
|
||||
return ret;
|
||||
if (os_memcmp(check, iv_crypt, AES_BLOCK_SIZE) == 0)
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
21
core/crypto/aes.h
Normal file
21
core/crypto/aes.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* AES functions
|
||||
* Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#ifndef AES_H
|
||||
#define AES_H
|
||||
|
||||
#define AES_BLOCK_SIZE 16
|
||||
|
||||
void * aes_encrypt_init(const u8 *key, size_t len);
|
||||
int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt);
|
||||
void aes_encrypt_deinit(void *ctx);
|
||||
void * aes_decrypt_init(const u8 *key, size_t len);
|
||||
int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain);
|
||||
void aes_decrypt_deinit(void *ctx);
|
||||
|
||||
#endif /* AES_H */
|
||||
125
core/crypto/aes_i.h
Normal file
125
core/crypto/aes_i.h
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* AES (Rijndael) cipher
|
||||
* Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#ifndef AES_I_H
|
||||
#define AES_I_H
|
||||
|
||||
#include "aes.h"
|
||||
|
||||
/* #define FULL_UNROLL */
|
||||
#define AES_SMALL_TABLES
|
||||
|
||||
extern const u32 Te0[256];
|
||||
extern const u32 Te1[256];
|
||||
extern const u32 Te2[256];
|
||||
extern const u32 Te3[256];
|
||||
extern const u32 Te4[256];
|
||||
extern const u32 Td0[256];
|
||||
extern const u32 Td1[256];
|
||||
extern const u32 Td2[256];
|
||||
extern const u32 Td3[256];
|
||||
extern const u32 Td4[256];
|
||||
extern const u32 rcon[10];
|
||||
extern const u8 Td4s[256];
|
||||
extern const u8 rcons[10];
|
||||
|
||||
#ifndef AES_SMALL_TABLES
|
||||
|
||||
#define RCON(i) rcon[(i)]
|
||||
|
||||
#define TE0(i) Te0[((i) >> 24) & 0xff]
|
||||
#define TE1(i) Te1[((i) >> 16) & 0xff]
|
||||
#define TE2(i) Te2[((i) >> 8) & 0xff]
|
||||
#define TE3(i) Te3[(i) & 0xff]
|
||||
#define TE41(i) (Te4[((i) >> 24) & 0xff] & 0xff000000)
|
||||
#define TE42(i) (Te4[((i) >> 16) & 0xff] & 0x00ff0000)
|
||||
#define TE43(i) (Te4[((i) >> 8) & 0xff] & 0x0000ff00)
|
||||
#define TE44(i) (Te4[(i) & 0xff] & 0x000000ff)
|
||||
#define TE421(i) (Te4[((i) >> 16) & 0xff] & 0xff000000)
|
||||
#define TE432(i) (Te4[((i) >> 8) & 0xff] & 0x00ff0000)
|
||||
#define TE443(i) (Te4[(i) & 0xff] & 0x0000ff00)
|
||||
#define TE414(i) (Te4[((i) >> 24) & 0xff] & 0x000000ff)
|
||||
#define TE411(i) (Te4[((i) >> 24) & 0xff] & 0xff000000)
|
||||
#define TE422(i) (Te4[((i) >> 16) & 0xff] & 0x00ff0000)
|
||||
#define TE433(i) (Te4[((i) >> 8) & 0xff] & 0x0000ff00)
|
||||
#define TE444(i) (Te4[(i) & 0xff] & 0x000000ff)
|
||||
#define TE4(i) (Te4[(i)] & 0x000000ff)
|
||||
|
||||
#define TD0(i) Td0[((i) >> 24) & 0xff]
|
||||
#define TD1(i) Td1[((i) >> 16) & 0xff]
|
||||
#define TD2(i) Td2[((i) >> 8) & 0xff]
|
||||
#define TD3(i) Td3[(i) & 0xff]
|
||||
#define TD41(i) (Td4[((i) >> 24) & 0xff] & 0xff000000)
|
||||
#define TD42(i) (Td4[((i) >> 16) & 0xff] & 0x00ff0000)
|
||||
#define TD43(i) (Td4[((i) >> 8) & 0xff] & 0x0000ff00)
|
||||
#define TD44(i) (Td4[(i) & 0xff] & 0x000000ff)
|
||||
#define TD0_(i) Td0[(i) & 0xff]
|
||||
#define TD1_(i) Td1[(i) & 0xff]
|
||||
#define TD2_(i) Td2[(i) & 0xff]
|
||||
#define TD3_(i) Td3[(i) & 0xff]
|
||||
|
||||
#else /* AES_SMALL_TABLES */
|
||||
|
||||
#define RCON(i) (rcons[(i)] << 24)
|
||||
|
||||
static inline u32 rotr(u32 val, int bits)
|
||||
{
|
||||
return (val >> bits) | (val << (32 - bits));
|
||||
}
|
||||
|
||||
#define TE0(i) Te0[((i) >> 24) & 0xff]
|
||||
#define TE1(i) rotr(Te0[((i) >> 16) & 0xff], 8)
|
||||
#define TE2(i) rotr(Te0[((i) >> 8) & 0xff], 16)
|
||||
#define TE3(i) rotr(Te0[(i) & 0xff], 24)
|
||||
#define TE41(i) ((Te0[((i) >> 24) & 0xff] << 8) & 0xff000000)
|
||||
#define TE42(i) (Te0[((i) >> 16) & 0xff] & 0x00ff0000)
|
||||
#define TE43(i) (Te0[((i) >> 8) & 0xff] & 0x0000ff00)
|
||||
#define TE44(i) ((Te0[(i) & 0xff] >> 8) & 0x000000ff)
|
||||
#define TE421(i) ((Te0[((i) >> 16) & 0xff] << 8) & 0xff000000)
|
||||
#define TE432(i) (Te0[((i) >> 8) & 0xff] & 0x00ff0000)
|
||||
#define TE443(i) (Te0[(i) & 0xff] & 0x0000ff00)
|
||||
#define TE414(i) ((Te0[((i) >> 24) & 0xff] >> 8) & 0x000000ff)
|
||||
#define TE411(i) ((Te0[((i) >> 24) & 0xff] << 8) & 0xff000000)
|
||||
#define TE422(i) (Te0[((i) >> 16) & 0xff] & 0x00ff0000)
|
||||
#define TE433(i) (Te0[((i) >> 8) & 0xff] & 0x0000ff00)
|
||||
#define TE444(i) ((Te0[(i) & 0xff] >> 8) & 0x000000ff)
|
||||
#define TE4(i) ((Te0[(i)] >> 8) & 0x000000ff)
|
||||
|
||||
#define TD0(i) Td0[((i) >> 24) & 0xff]
|
||||
#define TD1(i) rotr(Td0[((i) >> 16) & 0xff], 8)
|
||||
#define TD2(i) rotr(Td0[((i) >> 8) & 0xff], 16)
|
||||
#define TD3(i) rotr(Td0[(i) & 0xff], 24)
|
||||
#define TD41(i) (Td4s[((i) >> 24) & 0xff] << 24)
|
||||
#define TD42(i) (Td4s[((i) >> 16) & 0xff] << 16)
|
||||
#define TD43(i) (Td4s[((i) >> 8) & 0xff] << 8)
|
||||
#define TD44(i) (Td4s[(i) & 0xff])
|
||||
#define TD0_(i) Td0[(i) & 0xff]
|
||||
#define TD1_(i) rotr(Td0[(i) & 0xff], 8)
|
||||
#define TD2_(i) rotr(Td0[(i) & 0xff], 16)
|
||||
#define TD3_(i) rotr(Td0[(i) & 0xff], 24)
|
||||
|
||||
#endif /* AES_SMALL_TABLES */
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00)
|
||||
#define GETU32(p) SWAP(*((u32 *)(p)))
|
||||
#define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
|
||||
#else
|
||||
#define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ \
|
||||
((u32)(pt)[2] << 8) ^ ((u32)(pt)[3]))
|
||||
#define PUTU32(ct, st) { \
|
||||
(ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); \
|
||||
(ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); }
|
||||
#endif
|
||||
|
||||
#define AES_PRIV_SIZE (4 * 4 * 15 + 4)
|
||||
#define AES_PRIV_NR_POS (4 * 15)
|
||||
|
||||
int rijndaelKeySetupEnc(u32 rk[], const u8 cipherKey[], int keyBits);
|
||||
|
||||
#endif /* AES_I_H */
|
||||
21
core/crypto/aes_siv.h
Normal file
21
core/crypto/aes_siv.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* AES SIV (RFC 5297)
|
||||
* Copyright (c) 2013 Cozybit, Inc.
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#ifndef AES_SIV_H
|
||||
#define AES_SIV_H
|
||||
|
||||
int aes_siv_encrypt(const u8 *key, size_t key_len,
|
||||
const u8 *pw, size_t pwlen,
|
||||
size_t num_elem, const u8 *addr[], const size_t *len,
|
||||
u8 *out);
|
||||
int aes_siv_decrypt(const u8 *key, size_t key_len,
|
||||
const u8 *iv_crypt, size_t iv_c_len,
|
||||
size_t num_elem, const u8 *addr[], const size_t *len,
|
||||
u8 *out);
|
||||
|
||||
#endif /* AES_SIV_H */
|
||||
73
core/crypto/aes_wrap.h
Normal file
73
core/crypto/aes_wrap.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* AES-based functions
|
||||
*
|
||||
* - AES Key Wrap Algorithm (RFC3394)
|
||||
* - One-Key CBC MAC (OMAC1) hash with AES-128 and AES-256
|
||||
* - AES-128/192/256 CTR mode encryption
|
||||
* - AES-128 EAX mode encryption/decryption
|
||||
* - AES-128 CBC
|
||||
* - AES-GCM
|
||||
* - AES-CCM
|
||||
*
|
||||
* Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#ifndef AES_WRAP_H
|
||||
#define AES_WRAP_H
|
||||
|
||||
int __must_check aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain,
|
||||
u8 *cipher);
|
||||
int __must_check aes_unwrap(const u8 *kek, size_t kek_len, int n,
|
||||
const u8 *cipher, u8 *plain);
|
||||
int __must_check omac1_aes_vector(const u8 *key, size_t key_len,
|
||||
size_t num_elem, const u8 *addr[],
|
||||
const size_t *len, u8 *mac);
|
||||
int __must_check omac1_aes_128_vector(const u8 *key, size_t num_elem,
|
||||
const u8 *addr[], const size_t *len,
|
||||
u8 *mac);
|
||||
int __must_check omac1_aes_128(const u8 *key, const u8 *data, size_t data_len,
|
||||
u8 *mac);
|
||||
int __must_check omac1_aes_256(const u8 *key, const u8 *data, size_t data_len,
|
||||
u8 *mac);
|
||||
int __must_check aes_128_encrypt_block(const u8 *key, const u8 *in, u8 *out);
|
||||
int __must_check aes_ctr_encrypt(const u8 *key, size_t key_len, const u8 *nonce,
|
||||
u8 *data, size_t data_len);
|
||||
int __must_check aes_128_ctr_encrypt(const u8 *key, const u8 *nonce,
|
||||
u8 *data, size_t data_len);
|
||||
int __must_check aes_128_eax_encrypt(const u8 *key,
|
||||
const u8 *nonce, size_t nonce_len,
|
||||
const u8 *hdr, size_t hdr_len,
|
||||
u8 *data, size_t data_len, u8 *tag);
|
||||
int __must_check aes_128_eax_decrypt(const u8 *key,
|
||||
const u8 *nonce, size_t nonce_len,
|
||||
const u8 *hdr, size_t hdr_len,
|
||||
u8 *data, size_t data_len, const u8 *tag);
|
||||
int __must_check aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data,
|
||||
size_t data_len);
|
||||
int __must_check aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data,
|
||||
size_t data_len);
|
||||
int __must_check aes_gcm_ae(const u8 *key, size_t key_len,
|
||||
const u8 *iv, size_t iv_len,
|
||||
const u8 *plain, size_t plain_len,
|
||||
const u8 *aad, size_t aad_len,
|
||||
u8 *crypt, u8 *tag);
|
||||
int __must_check aes_gcm_ad(const u8 *key, size_t key_len,
|
||||
const u8 *iv, size_t iv_len,
|
||||
const u8 *crypt, size_t crypt_len,
|
||||
const u8 *aad, size_t aad_len, const u8 *tag,
|
||||
u8 *plain);
|
||||
int __must_check aes_gmac(const u8 *key, size_t key_len,
|
||||
const u8 *iv, size_t iv_len,
|
||||
const u8 *aad, size_t aad_len, u8 *tag);
|
||||
int __must_check aes_ccm_ae(const u8 *key, size_t key_len, const u8 *nonce,
|
||||
size_t M, const u8 *plain, size_t plain_len,
|
||||
const u8 *aad, size_t aad_len, u8 *crypt, u8 *auth);
|
||||
int __must_check aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce,
|
||||
size_t M, const u8 *crypt, size_t crypt_len,
|
||||
const u8 *aad, size_t aad_len, const u8 *auth,
|
||||
u8 *plain);
|
||||
|
||||
#endif /* AES_WRAP_H */
|
||||
510
core/crypto/ccmp.c
Normal file
510
core/crypto/ccmp.c
Normal file
File diff suppressed because it is too large
Load Diff
194
core/crypto/gcmp.c
Normal file
194
core/crypto/gcmp.c
Normal file
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
* GCM with GMAC Protocol (GCMP)
|
||||
* Copyright (c) 2012, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#include "rtw_crypto_wrap.h"
|
||||
|
||||
#include "aes.h"
|
||||
#include "aes_wrap.h"
|
||||
#include "wlancrypto_wrap.h"
|
||||
|
||||
|
||||
static void gcmp_aad_nonce(_adapter * padapter, const struct ieee80211_hdr *hdr, const u8 *data,
|
||||
u8 *aad, size_t *aad_len, u8 *nonce)
|
||||
{
|
||||
u16 fc, stype, seq;
|
||||
int qos = 0, addr4 = 0;
|
||||
u8 *pos;
|
||||
|
||||
fc = le_to_host16(hdr->frame_control);
|
||||
stype = WLAN_FC_GET_STYPE(fc);
|
||||
if ((fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
|
||||
(WLAN_FC_TODS | WLAN_FC_FROMDS))
|
||||
addr4 = 1;
|
||||
|
||||
if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA) {
|
||||
fc &= ~0x0070; /* Mask subtype bits */
|
||||
if (stype & WLAN_FC_STYPE_QOS_DATA) {
|
||||
const u8 *qc;
|
||||
qos = 1;
|
||||
fc &= ~WLAN_FC_ORDER;
|
||||
qc = (const u8 *)hdr + 24;
|
||||
if (addr4)
|
||||
qc += ETH_ALEN;
|
||||
}
|
||||
}
|
||||
|
||||
fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA);
|
||||
WPA_PUT_LE16(aad, fc);
|
||||
pos = aad + 2;
|
||||
os_memcpy(pos, hdr->addr1, 3 * ETH_ALEN);
|
||||
pos += 3 * ETH_ALEN;
|
||||
seq = le_to_host16(hdr->seq_ctrl);
|
||||
seq &= ~0xfff0; /* Mask Seq#; do not modify Frag# */
|
||||
WPA_PUT_LE16(pos, seq);
|
||||
pos += 2;
|
||||
|
||||
wpa_printf(_MSG_INFO_, "pos - aad = %u, qos(%d)\n", (pos - aad), qos);
|
||||
|
||||
os_memcpy(pos, (u8 *)hdr + 24, addr4 * ETH_ALEN + qos * 2);
|
||||
pos += addr4 * ETH_ALEN;
|
||||
if (qos) {
|
||||
pos[0] &= ~0x70;
|
||||
/* only spp mode need to refer QoS bit7 */
|
||||
if (padapter->registrypriv.amsdu_mode != RTW_AMSDU_MODE_SPP)
|
||||
pos[0] &= ~0x80;
|
||||
pos++;
|
||||
*pos++ = 0x00;
|
||||
}
|
||||
|
||||
wpa_printf(_MSG_INFO_, "pos - aad = %u\n", (pos - aad));
|
||||
*aad_len = pos - aad;
|
||||
|
||||
os_memcpy(nonce, hdr->addr2, ETH_ALEN);
|
||||
nonce[6] = data[7]; /* PN5 */
|
||||
nonce[7] = data[6]; /* PN4 */
|
||||
nonce[8] = data[5]; /* PN3 */
|
||||
nonce[9] = data[4]; /* PN2 */
|
||||
nonce[10] = data[1]; /* PN1 */
|
||||
nonce[11] = data[0]; /* PN0 */
|
||||
}
|
||||
|
||||
/**
|
||||
* gcmp_decrypt -
|
||||
* @tk: the temporal key
|
||||
* @tk_len: length of @tk
|
||||
* @hdr: the mac header
|
||||
* @data: payload after mac header (PN + enc_data + MIC)
|
||||
* @data_len: length of @data (PN + enc_data + MIC)
|
||||
* @decrypted_len: length of the data decrypted
|
||||
*/
|
||||
u8 * gcmp_decrypt(_adapter *padapter, const u8 *tk, size_t tk_len, const struct ieee80211_hdr *hdr,
|
||||
const u8 *data, size_t data_len, size_t *decrypted_len)
|
||||
{
|
||||
u8 aad[30], nonce[12], *plain;
|
||||
size_t aad_len, mlen;
|
||||
const u8 *m;
|
||||
|
||||
if (data_len < 8 + 16)
|
||||
return NULL;
|
||||
|
||||
plain = os_malloc(data_len + AES_BLOCK_SIZE);
|
||||
if (plain == NULL)
|
||||
return NULL;
|
||||
|
||||
m = data + 8;
|
||||
mlen = data_len - 8 - 16;
|
||||
|
||||
os_memset(aad, 0, sizeof(aad));
|
||||
gcmp_aad_nonce(padapter, hdr, data, aad, &aad_len, nonce);
|
||||
wpa_hexdump(_MSG_EXCESSIVE_, "GCMP AAD", aad, aad_len);
|
||||
wpa_hexdump(_MSG_EXCESSIVE_, "GCMP nonce", nonce, sizeof(nonce));
|
||||
|
||||
if (aes_gcm_ad(tk, tk_len, nonce, sizeof(nonce), m, mlen, aad, aad_len,
|
||||
m + mlen, plain) < 0) {
|
||||
u16 seq_ctrl = le_to_host16(hdr->seq_ctrl);
|
||||
wpa_printf(_MSG_INFO_, "Invalid GCMP frame: A1=" MACSTR
|
||||
" A2=" MACSTR " A3=" MACSTR " seq=%u frag=%u",
|
||||
MAC2STR(hdr->addr1), MAC2STR(hdr->addr2),
|
||||
MAC2STR(hdr->addr3),
|
||||
WLAN_GET_SEQ_SEQ(seq_ctrl),
|
||||
WLAN_GET_SEQ_FRAG(seq_ctrl));
|
||||
rtw_mfree(plain, data_len + AES_BLOCK_SIZE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*decrypted_len = mlen;
|
||||
return plain;
|
||||
}
|
||||
|
||||
/**
|
||||
* gcmp_encrypt -
|
||||
* @tk: the temporal key
|
||||
* @tk_len: length of @tk
|
||||
* @frame: the point to mac header, the frame including mac header and payload,
|
||||
* if @pn is NULL, then the frame including pn
|
||||
* @len: length of @frame
|
||||
* length = mac header + payload
|
||||
* @hdrlen: length of the mac header
|
||||
* @qos: pointer to the QOS field of the frame
|
||||
* @pn: packet number
|
||||
* @keyid: key id
|
||||
* @encrypted_len: length of the encrypted frame
|
||||
* including mac header, pn, payload and MIC
|
||||
*/
|
||||
u8 * gcmp_encrypt(_adapter *padapter, const u8 *tk, size_t tk_len, const u8 *frame, size_t len,
|
||||
size_t hdrlen, const u8 *qos,
|
||||
const u8 *pn, int keyid, size_t *encrypted_len)
|
||||
{
|
||||
u8 aad[30], nonce[12], *crypt, *pos;
|
||||
const u8 *pdata;
|
||||
size_t aad_len, plen;
|
||||
struct ieee80211_hdr *hdr;
|
||||
|
||||
if (len < hdrlen || hdrlen < 24)
|
||||
return NULL;
|
||||
plen = len - hdrlen;
|
||||
|
||||
crypt = os_malloc(hdrlen + 8 + plen + 16 + AES_BLOCK_SIZE);
|
||||
if (crypt == NULL)
|
||||
return NULL;
|
||||
|
||||
if (pn == NULL) {
|
||||
os_memcpy(crypt, frame, hdrlen + 8);
|
||||
hdr = (struct ieee80211_hdr *)crypt;
|
||||
pos = crypt + hdrlen + 8;
|
||||
pdata = frame + hdrlen + 8;
|
||||
} else {
|
||||
os_memcpy(crypt, frame, hdrlen);
|
||||
hdr = (struct ieee80211_hdr *)crypt;
|
||||
pos = crypt + hdrlen;
|
||||
|
||||
*pos++ = pn[5]; /* PN0 */
|
||||
*pos++ = pn[4]; /* PN1 */
|
||||
*pos++ = 0x00; /* Rsvd */
|
||||
*pos++ = 0x20 | (keyid << 6);
|
||||
*pos++ = pn[3]; /* PN2 */
|
||||
*pos++ = pn[2]; /* PN3 */
|
||||
*pos++ = pn[1]; /* PN4 */
|
||||
*pos++ = pn[0]; /* PN5 */
|
||||
pdata = frame + hdrlen;
|
||||
}
|
||||
|
||||
os_memset(aad, 0, sizeof(aad));
|
||||
gcmp_aad_nonce(padapter, hdr, crypt + hdrlen, aad, &aad_len, nonce);
|
||||
wpa_hexdump(_MSG_EXCESSIVE_, "GCMP AAD", aad, aad_len);
|
||||
wpa_hexdump(_MSG_EXCESSIVE_, "GCMP nonce", nonce, sizeof(nonce));
|
||||
|
||||
if (aes_gcm_ae(tk, tk_len, nonce, sizeof(nonce), pdata, plen,
|
||||
aad, aad_len, pos, pos + plen) < 0) {
|
||||
rtw_mfree(crypt, hdrlen + 8 + plen + 16 + AES_BLOCK_SIZE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wpa_hexdump(_MSG_EXCESSIVE_, "GCMP MIC", pos + plen, 16);
|
||||
wpa_hexdump(_MSG_EXCESSIVE_, "GCMP encrypted", pos, plen);
|
||||
|
||||
*encrypted_len = hdrlen + 8 + plen + 16;
|
||||
|
||||
return crypt;
|
||||
}
|
||||
85
core/crypto/rtw_crypto_wrap.c
Normal file
85
core/crypto/rtw_crypto_wrap.c
Normal file
@@ -0,0 +1,85 @@
|
||||
#include "rtw_crypto_wrap.h"
|
||||
|
||||
#ifndef DEBUG_CRYPTO
|
||||
#define DEBUG_CRYPTO 0
|
||||
#endif /* DEBUG_CRYTO */
|
||||
|
||||
int os_memcmp(const void *s1, const void *s2, size_t n)
|
||||
{
|
||||
return _rtw_memcmp2(s1, s2, n);
|
||||
}
|
||||
|
||||
int os_memcmp_const(const void *a, const void *b, size_t len)
|
||||
{
|
||||
const u8 *aa = a;
|
||||
const u8 *bb = b;
|
||||
size_t i;
|
||||
u8 res;
|
||||
|
||||
for (res = 0, i = 0; i < len; i++)
|
||||
res |= aa[i] ^ bb[i];
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void* os_memdup(const void *src, u32 sz)
|
||||
{
|
||||
void *r = rtw_malloc(sz);
|
||||
|
||||
if (r && src)
|
||||
_rtw_memcpy(r, src, sz);
|
||||
return r;
|
||||
}
|
||||
|
||||
size_t os_strlen(const char *s)
|
||||
{
|
||||
const char *p = s;
|
||||
while (*p)
|
||||
p++;
|
||||
return p - s;
|
||||
}
|
||||
|
||||
|
||||
void forced_memzero(void *ptr, size_t len)
|
||||
{
|
||||
_rtw_memset(ptr, 0, len);
|
||||
}
|
||||
|
||||
void bin_clear_free(void *bin, size_t len)
|
||||
{
|
||||
if (bin) {
|
||||
forced_memzero(bin, len);
|
||||
rtw_mfree(bin, len);
|
||||
}
|
||||
}
|
||||
|
||||
void wpa_printf(int level, const char *fmt, ...)
|
||||
{
|
||||
#if DEBUG_CRYPTO
|
||||
#define MSG_LEN 100
|
||||
va_list args;
|
||||
u8 buf[MSG_LEN] = { 0 };
|
||||
int err;
|
||||
|
||||
va_start(args, fmt);
|
||||
err = vsnprintf(buf, MSG_LEN, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
RTW_INFO("%s", buf);
|
||||
#undef MSG_LEN
|
||||
#endif /* DEBUG_CRYPTO */
|
||||
}
|
||||
|
||||
void wpa_hexdump(int level, const char *title, const void *buf, size_t len)
|
||||
{
|
||||
#if DEBUG_CRYPTO
|
||||
RTW_INFO_DUMP((u8 *)title, buf, len);
|
||||
#endif /* DEBUG_CRYPTO */
|
||||
}
|
||||
|
||||
void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len)
|
||||
{
|
||||
#if DEBUG_CRYPTO
|
||||
RTW_INFO_DUMP((u8 *)title, buf, len);
|
||||
#endif /* DEBUG_CRYPTO */
|
||||
}
|
||||
64
core/crypto/rtw_crypto_wrap.h
Normal file
64
core/crypto/rtw_crypto_wrap.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifndef RTW_CRYTO_WRAP_H
|
||||
#define RTW_CRYTO_WRAP_H
|
||||
|
||||
#include <drv_types.h>
|
||||
|
||||
#define TEST_FAIL() 0
|
||||
|
||||
#define os_memset _rtw_memset
|
||||
#define os_memcpy _rtw_memcpy
|
||||
#define os_malloc rtw_malloc
|
||||
|
||||
#define le_to_host16 le16_to_cpu
|
||||
#define host_to_le16 cpu_to_le16
|
||||
|
||||
#define WPA_PUT_LE16 RTW_PUT_LE16
|
||||
#define WPA_GET_LE16 RTW_GET_LE16
|
||||
#define WPA_PUT_LE32 RTW_PUT_LE32
|
||||
#define WPA_GET_LE32 RTW_GET_LE32
|
||||
#define WPA_PUT_LE64 RTW_PUT_LE64
|
||||
#define WPA_GET_LE64 RTW_GET_LE64
|
||||
#define WPA_PUT_BE16 RTW_PUT_BE16
|
||||
#define WPA_GET_BE16 RTW_GET_BE16
|
||||
#define WPA_PUT_BE32 RTW_PUT_BE32
|
||||
#define WPA_GET_BE32 RTW_GET_BE32
|
||||
#define WPA_PUT_BE64 RTW_PUT_BE64
|
||||
#define WPA_GET_BE64 RTW_GET_BE64
|
||||
|
||||
#ifndef MAC2STR
|
||||
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
|
||||
#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
|
||||
#endif
|
||||
|
||||
#define WLAN_FC_PVER 0x0003
|
||||
#define WLAN_FC_TODS 0x0100
|
||||
#define WLAN_FC_FROMDS 0x0200
|
||||
#define WLAN_FC_MOREFRAG 0x0400
|
||||
#define WLAN_FC_RETRY 0x0800
|
||||
#define WLAN_FC_PWRMGT 0x1000
|
||||
#define WLAN_FC_MOREDATA 0x2000
|
||||
#define WLAN_FC_ISWEP 0x4000
|
||||
#define WLAN_FC_ORDER 0x8000
|
||||
|
||||
#define WLAN_FC_TYPE_DATA RTW_IEEE80211_FTYPE_DATA
|
||||
#define WLAN_FC_TYPE_MGMT RTW_IEEE80211_FTYPE_MGMT
|
||||
|
||||
#define WLAN_FC_STYPE_QOS_DATA RTW_IEEE80211_STYPE_QOS_DATA
|
||||
|
||||
enum {
|
||||
_MSG_EXCESSIVE_, _MSG_MSGDUMP_, _MSG_DEBUG_, _MSG_INFO_, _MSG_WARNING_, _MSG_ERROR_
|
||||
};
|
||||
|
||||
int os_memcmp(const void *s1, const void *s2, size_t n);
|
||||
int os_memcmp_const(const void *a, const void *b, size_t len);
|
||||
void* os_memdup(const void *src, u32 sz);
|
||||
size_t os_strlen(const char *s);
|
||||
|
||||
void forced_memzero(void *ptr, size_t len);
|
||||
void bin_clear_free(void *bin, size_t len);
|
||||
|
||||
void wpa_printf(int level, const char *fmt, ...);
|
||||
void wpa_hexdump(int level, const char *title, const void *buf, size_t len);
|
||||
void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len);
|
||||
|
||||
#endif /* RTW_CRYTO_WRAP_H */
|
||||
230
core/crypto/sha256-internal.c
Normal file
230
core/crypto/sha256-internal.c
Normal file
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
* SHA-256 hash implementation and interface functions
|
||||
* Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#include "rtw_crypto_wrap.h"
|
||||
|
||||
//#include "common.h"
|
||||
#include "sha256.h"
|
||||
#include "sha256_i.h"
|
||||
//#include "crypto.h"
|
||||
#include "wlancrypto_wrap.h"
|
||||
|
||||
|
||||
/**
|
||||
* sha256_vector - SHA256 hash for data vector
|
||||
* @num_elem: Number of elements in the data vector
|
||||
* @addr: Pointers to the data areas
|
||||
* @len: Lengths of the data blocks
|
||||
* @mac: Buffer for the hash
|
||||
* Returns: 0 on success, -1 of failure
|
||||
*/
|
||||
int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
|
||||
u8 *mac)
|
||||
{
|
||||
struct _sha256_state ctx;
|
||||
size_t i;
|
||||
|
||||
if (TEST_FAIL())
|
||||
return -1;
|
||||
|
||||
_sha256_init(&ctx);
|
||||
for (i = 0; i < num_elem; i++)
|
||||
if (sha256_process(&ctx, addr[i], len[i]))
|
||||
return -1;
|
||||
if (sha256_done(&ctx, mac))
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* ===== start - public domain SHA256 implementation ===== */
|
||||
|
||||
/* This is based on SHA256 implementation in LibTomCrypt that was released into
|
||||
* public domain by Tom St Denis. */
|
||||
|
||||
/* the K array */
|
||||
static const unsigned long K[64] = {
|
||||
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
|
||||
0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
|
||||
0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
|
||||
0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
|
||||
0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
|
||||
0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
|
||||
0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
|
||||
0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
|
||||
0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
|
||||
0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
|
||||
0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
|
||||
0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
|
||||
0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
|
||||
};
|
||||
|
||||
|
||||
/* Various logical functions */
|
||||
#define RORc(x, y) \
|
||||
( ((((unsigned long) (x) & 0xFFFFFFFFUL) >> (unsigned long) ((y) & 31)) | \
|
||||
((unsigned long) (x) << (unsigned long) (32 - ((y) & 31)))) & 0xFFFFFFFFUL)
|
||||
#define Ch(x,y,z) (z ^ (x & (y ^ z)))
|
||||
#define Maj(x,y,z) (((x | y) & z) | (x & y))
|
||||
#define S(x, n) RORc((x), (n))
|
||||
#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
|
||||
#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
|
||||
#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
|
||||
#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
|
||||
#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
|
||||
#ifndef MIN
|
||||
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
#endif
|
||||
|
||||
/* compress 512-bits */
|
||||
static int sha256_compress(struct _sha256_state *md, unsigned char *buf)
|
||||
{
|
||||
u32 S[8], W[64], t0, t1;
|
||||
u32 t;
|
||||
int i;
|
||||
|
||||
/* copy state into S */
|
||||
for (i = 0; i < 8; i++) {
|
||||
S[i] = md->state[i];
|
||||
}
|
||||
|
||||
/* copy the state into 512-bits into W[0..15] */
|
||||
for (i = 0; i < 16; i++)
|
||||
W[i] = WPA_GET_BE32(buf + (4 * i));
|
||||
|
||||
/* fill W[16..63] */
|
||||
for (i = 16; i < 64; i++) {
|
||||
W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) +
|
||||
W[i - 16];
|
||||
}
|
||||
|
||||
/* Compress */
|
||||
#define RND(a,b,c,d,e,f,g,h,i) \
|
||||
t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
|
||||
t1 = Sigma0(a) + Maj(a, b, c); \
|
||||
d += t0; \
|
||||
h = t0 + t1;
|
||||
|
||||
for (i = 0; i < 64; ++i) {
|
||||
RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i);
|
||||
t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
|
||||
S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
|
||||
}
|
||||
|
||||
/* feedback */
|
||||
for (i = 0; i < 8; i++) {
|
||||
md->state[i] = md->state[i] + S[i];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* Initialize the hash state */
|
||||
void _sha256_init(struct _sha256_state *md)
|
||||
{
|
||||
md->curlen = 0;
|
||||
md->length = 0;
|
||||
md->state[0] = 0x6A09E667UL;
|
||||
md->state[1] = 0xBB67AE85UL;
|
||||
md->state[2] = 0x3C6EF372UL;
|
||||
md->state[3] = 0xA54FF53AUL;
|
||||
md->state[4] = 0x510E527FUL;
|
||||
md->state[5] = 0x9B05688CUL;
|
||||
md->state[6] = 0x1F83D9ABUL;
|
||||
md->state[7] = 0x5BE0CD19UL;
|
||||
}
|
||||
|
||||
/**
|
||||
Process a block of memory though the hash
|
||||
@param md The hash state
|
||||
@param in The data to hash
|
||||
@param inlen The length of the data (octets)
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sha256_process(struct _sha256_state *md, const unsigned char *in,
|
||||
unsigned long inlen)
|
||||
{
|
||||
unsigned long n;
|
||||
|
||||
if (md->curlen >= sizeof(md->buf))
|
||||
return -1;
|
||||
|
||||
while (inlen > 0) {
|
||||
if (md->curlen == 0 && inlen >= SHA256_BLOCK_SIZE) {
|
||||
if (sha256_compress(md, (unsigned char *) in) < 0)
|
||||
return -1;
|
||||
md->length += SHA256_BLOCK_SIZE * 8;
|
||||
in += SHA256_BLOCK_SIZE;
|
||||
inlen -= SHA256_BLOCK_SIZE;
|
||||
} else {
|
||||
n = MIN(inlen, (SHA256_BLOCK_SIZE - md->curlen));
|
||||
os_memcpy(md->buf + md->curlen, in, n);
|
||||
md->curlen += n;
|
||||
in += n;
|
||||
inlen -= n;
|
||||
if (md->curlen == SHA256_BLOCK_SIZE) {
|
||||
if (sha256_compress(md, md->buf) < 0)
|
||||
return -1;
|
||||
md->length += 8 * SHA256_BLOCK_SIZE;
|
||||
md->curlen = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Terminate the hash to get the digest
|
||||
@param md The hash state
|
||||
@param out [out] The destination of the hash (32 bytes)
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sha256_done(struct _sha256_state *md, unsigned char *out)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (md->curlen >= sizeof(md->buf))
|
||||
return -1;
|
||||
|
||||
/* increase the length of the message */
|
||||
md->length += md->curlen * 8;
|
||||
|
||||
/* append the '1' bit */
|
||||
md->buf[md->curlen++] = (unsigned char) 0x80;
|
||||
|
||||
/* if the length is currently above 56 bytes we append zeros
|
||||
* then compress. Then we can fall back to padding zeros and length
|
||||
* encoding like normal.
|
||||
*/
|
||||
if (md->curlen > 56) {
|
||||
while (md->curlen < SHA256_BLOCK_SIZE) {
|
||||
md->buf[md->curlen++] = (unsigned char) 0;
|
||||
}
|
||||
sha256_compress(md, md->buf);
|
||||
md->curlen = 0;
|
||||
}
|
||||
|
||||
/* pad up to 56 bytes of zeroes */
|
||||
while (md->curlen < 56) {
|
||||
md->buf[md->curlen++] = (unsigned char) 0;
|
||||
}
|
||||
|
||||
/* store length */
|
||||
WPA_PUT_BE64(md->buf + 56, md->length);
|
||||
sha256_compress(md, md->buf);
|
||||
|
||||
/* copy output */
|
||||
for (i = 0; i < 8; i++)
|
||||
WPA_PUT_BE32(out + (4 * i), md->state[i]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ===== end - public domain SHA256 implementation ===== */
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user