Issue: WCSWREL-733 i.MX Linux: MM6X18540.p7: nanotls related files been wrong added which may cause license confilct

Removed the default config got SECURE_HOST which leads to
addition for nanotls dependency..

Signed-off-by: AMIT CHOUDHARY <amit.kumar_13@nxp.com>
This commit is contained in:
AMIT CHOUDHARY 2025-08-25 16:50:10 +05:30 • committed by Sherry Sun
parent a250162547
commit 6818dd8ca9
49 changed files with 11 additions and 7547 deletions

View file

@ -590,32 +590,6 @@ endif
$(info $(PWD))
$(info $(src))
ccflags-y += -I$(PWD)/nanotls/include
ccflags-y += -I$(PWD)/nanotls/lib/include
ccflags-y += -I$(PWD)/nanotls/lib/aes/include
ccflags-y += -I$(src)/include
ccflags-y += -I$(src)/lib/include
ccflags-y += -I$(src)/lib/aes/include
ccflags-y += -I$(KERNELDIR)/include/linux
ccflags-y += -std=gnu99
ccflags-y += -Wno-format-security -Wno-unused-but-set-variable -Wno-declaration-after-statement
MLANOBJS += mlan/mlan_shc.o
MOALOBJS += nanotls/lib/src/blockwise.o \
nanotls/lib/src/chash.o \
nanotls/lib/src/hmac.o \
nanotls/lib/src/p256-m.o \
nanotls/lib/src/sha256.o \
nanotls/lib/aes/src/aes.o \
nanotls/lib/aes/src/gcm.o \
nanotls/src/nanotls-common.o \
nanotls/src/nanotls-hooks.o \
nanotls/src/nanotls-host.o \
nanotls/src/nanotls-device.o \
nanotls/src/nanotls-data.o \
mlinux/moal_shc.o
obj-m := mlan.o

View file

@ -46,8 +46,6 @@ Change Log:
#endif /* PCIE */
#include "mlan_init.h"
#include "mlan_shc.h"
/********************************************************
Local Variables
********************************************************/
@ -1547,11 +1545,7 @@ static mlan_status wlan_dnld_cmd_to_fw(mlan_private *pmpriv,
(t_u8 *)pcmd + S_DS_GEN)),
cmd_size, wlan_le16_to_cpu(pcmd->seq_num), timeout);
if (!pmadapter->shc_secure_host)
DBG_HEXDUMP(MCMD_D, "DNLD_CMD", (t_u8 *)pcmd, cmd_size);
if (pmadapter->shc_secure_host)
DBG_HEXDUMP(MSHC_D, "DNLD_CMD", (t_u8 *)pcmd, cmd_size);
DBG_HEXDUMP(MCMD_D, "DNLD_CMD", (t_u8 *)pcmd, cmd_size);
#if defined(SDIO) || defined(PCIE)
if (!IS_USB(pmadapter->card_type)) {
@ -1561,21 +1555,6 @@ static mlan_status wlan_dnld_cmd_to_fw(mlan_private *pmpriv,
}
#endif
if (pmadapter->shc_secure_host &&
wlan_is_secure_host_cmd(pcmd->command) == MTRUE) {
if (wlan_shc_secure_hostcmd_process(pmadapter, pcmd) !=
MLAN_STATUS_FAILURE) {
pcmd_node->cmdbuf->data_len += 16;
PRINTM(MCMND, "%s is encrypted",
wlan_hostcmd_get_name(cmd_code));
DBG_HEXDUMP(MCMD_D, "Encrypted DNLD_CMD", (t_u8 *)pcmd,
pcmd->size);
} else {
ret = MLAN_STATUS_FAILURE;
goto done;
}
}
/* Send the command to lower layer */
ret = pmadapter->ops.host_to_card(pmpriv, MLAN_TYPE_CMD,
pcmd_node->cmdbuf, MNULL);
@ -2479,13 +2458,6 @@ static void wlan_handle_cmd_error_in_pre_aleep(mlan_adapter *pmadapter,
MLAN_STATUS_SUCCESS);
pcmd_node->respbuf = MNULL;
}
if (pmadapter->shc_secure_host) {
memcpy_ext(pmadapter,
pcmd_node->cmdbuf->pbuf +
pcmd_node->cmdbuf->data_offset,
rsp, rsp->size, rsp->size);
pcmd_node->cmdbuf->data_len = rsp->size;
}
wlan_insert_cmd_to_pending_q(pmadapter, pcmd_node, MFALSE);
}
@ -2550,13 +2522,6 @@ mlan_status wlan_process_cmdresp(mlan_adapter *pmadapter)
resp = (HostCmd_DS_COMMAND *)(pmadapter->curr_cmd->respbuf->pbuf +
pmadapter->curr_cmd->respbuf->data_offset);
if (pmadapter->shc_secure_host) {
if (wlan_shc_secure_hostresp_process(pmadapter, resp) !=
MLAN_STATUS_SUCCESS) {
goto done;
}
}
orig_cmdresp_no = wlan_le16_to_cpu(resp->command);
cmdresp_no = (orig_cmdresp_no & HostCmd_CMD_ID_MASK);
if (pmadapter->curr_cmd->cmd_no != cmdresp_no) {
@ -2688,8 +2653,7 @@ mlan_status wlan_process_cmdresp(mlan_adapter *pmadapter)
/* Check init command response */
if (pmadapter->hw_status == WlanHardwareStatusInitializing ||
pmadapter->hw_status == WlanHardwareStatusGetHwSpec ||
pmadapter->hw_status == WlanHardwareStatusSecHandshake) {
pmadapter->hw_status == WlanHardwareStatusGetHwSpec) {
if (ret == MLAN_STATUS_FAILURE) {
#if 0
//ignore command error for WARM RESET
@ -2708,11 +2672,6 @@ mlan_status wlan_process_cmdresp(mlan_adapter *pmadapter)
PRINTM(MERROR,
"cmd 0x%02x failed during initialization\n",
cmdresp_no);
if (pmadapter->hw_status ==
WlanHardwareStatusSecHandshake) {
PRINTM(MERROR,
"secure host handshake incomplete\n");
}
wlan_init_fw_complete(pmadapter);
goto done;
}
@ -2789,23 +2748,6 @@ mlan_status wlan_process_cmdresp(mlan_adapter *pmadapter)
pmadapter->hw_status = WlanHardwareStatusGetHwSpecdone;
}
#if defined(PCIEAW693)
else if (pmadapter->shc_secure_host &&
(pmadapter->hw_status == WlanHardwareStatusGetHwSpec) &&
(HostCmd_CMD_FUNC_INIT == cmdresp_no)) {
if (!pmadapter->second_mac) {
pmadapter->hw_status = WlanHardwareStatusSecHandshake;
PRINTM(MMSG, "secure host handshake start\n");
ret = mlan_shc_handshake(pmadapter, TLS_HOST_HELLO,
MNULL);
if (ret != MLAN_STATUS_SUCCESS) {
pmadapter->hw_status =
WlanHardwareStatusNotReady;
wlan_init_fw_complete(pmadapter);
}
} else {
wlan_adapter_get_hw_spec(pmadapter);
}
}
#endif
done:
@ -5473,46 +5415,6 @@ mlan_status wlan_cmd_func_init(pmlan_private pmpriv, HostCmd_DS_COMMAND *cmd)
return MLAN_STATUS_SUCCESS;
}
mlan_status wlan_adapter_func_init(pmlan_adapter pmadapter)
{
mlan_status ret;
pmlan_private priv = wlan_get_priv(pmadapter, MLAN_BSS_ROLE_ANY);
ENTER();
#if defined(SDIO)
/*
* This should be issued in the very first to config
* SDIO_GPIO interrupt mode.
*/
if (IS_SD(pmadapter->card_type) &&
(wlan_set_sdio_gpio_int(priv) != MLAN_STATUS_SUCCESS)) {
ret = MLAN_STATUS_FAILURE;
goto done;
}
#endif
#ifdef PCIE
if (IS_PCIE(pmadapter->card_type) &&
(MLAN_STATUS_SUCCESS != wlan_set_pcie_buf_config(priv))) {
ret = MLAN_STATUS_FAILURE;
goto done;
}
#endif
ret = wlan_prepare_cmd(priv, HostCmd_CMD_FUNC_INIT, HostCmd_ACT_GEN_SET,
0, MNULL, MNULL);
if (ret) {
ret = MLAN_STATUS_FAILURE;
goto done;
}
ret = MLAN_STATUS_PENDING;
done:
LEAVE();
return ret;
}
/**
* @brief This function issues adapter specific commands
* to initialize firmware
@ -5544,20 +5446,18 @@ mlan_status wlan_adapter_get_hw_spec(pmlan_adapter pmadapter)
#endif
#ifdef PCIE
if (IS_PCIE(pmadapter->card_type) && !pmadapter->shc_secure_host &&
if (IS_PCIE(pmadapter->card_type) &&
(MLAN_STATUS_SUCCESS != wlan_set_pcie_buf_config(priv))) {
ret = MLAN_STATUS_FAILURE;
goto done;
}
#endif
if (!pmadapter->shc_secure_host) {
ret = wlan_prepare_cmd(priv, HostCmd_CMD_FUNC_INIT,
HostCmd_ACT_GEN_SET, 0, MNULL, MNULL);
if (ret) {
ret = MLAN_STATUS_FAILURE;
goto done;
}
ret = wlan_prepare_cmd(priv, HostCmd_CMD_FUNC_INIT, HostCmd_ACT_GEN_SET,
0, MNULL, MNULL);
if (ret) {
ret = MLAN_STATUS_FAILURE;
goto done;
}
/** DPD data dnld cmd prepare */
@ -12038,12 +11938,6 @@ mlan_status wlan_ret_get_sensor_temp(pmlan_private pmpriv,
return MLAN_STATUS_SUCCESS;
}
mlan_status wlan_process_secure_host_event(pmlan_private pmpriv, t_u8 *data,
t_u32 len)
{
return wlan_shc_process_secure_host_event(pmpriv, data, len);
}
/**
* @brief This function prepares command to configure edmac
*

View file

@ -687,7 +687,6 @@ typedef enum {
#define MFWDP_D MBIT(21)
#endif /*FWDUMP_VIA_PRINT*/
#define MSCH_D MBIT(22)
#define MSHC_D MBIT(23)
#define MENTRY MBIT(28)
#define MWARN MBIT(29)
#define MINFO MBIT(30)
@ -2761,25 +2760,6 @@ typedef struct _mlan_callbacks {
t_s32 copy_delay);
mlan_status (*moal_calc_short_ssid)(t_u8 *pssid, t_u32 ssid_len,
t_u32 *pshort_ssid);
t_u8 (*moal_secure_host_get_msg_id)(t_void *msg);
mlan_status (*moal_secure_host_init)(t_void *pmoal,
const t_u8 esdca_pub[64]);
void (*moal_secure_host_cleanup)(t_void *pmoal);
mlan_status (*moal_secure_host_do_hello)(t_void *pmoal, t_void **msg);
mlan_status (*moal_secure_host_device_hello_rcvd)(t_void *pmoal,
t_void *msg);
mlan_status (*moal_secure_host_do_finished)(t_void *pmoal,
t_void **msg);
mlan_status (*moal_secure_host_derive_traffic_keys)(t_void *pmoal);
mlan_status (*moal_secure_host_data_ctx_init)(t_void *pmoal);
mlan_status (*moal_secure_host_data_encrypt)(t_void *pmoal,
t_void **enc_data,
t_void **payload,
t_u32 len);
mlan_status (*moal_secure_host_data_decrypt)(t_void *pmoal,
t_void **dec_data,
t_void **payload,
t_u32 len);
/* Unaligned Access */
struct unaligned_access {
t_u16 (*moal_read_u16)(const void *src);
@ -2966,7 +2946,6 @@ typedef struct _mlan_device {
t_u8 disable_11h_tpc;
t_u8 tpe_ie_ignore;
t_u32 amsdu_disable;
t_u32 secure_host;
} mlan_device, *pmlan_device;
/** MLAN API function prototype */

View file

@ -238,7 +238,5 @@ ENUM_ELEMENT(EVENT_DUMMY_HOST_WAKEUP_SIGNAL, 0x0001),
/** Event ID: WiFi uAP Channel Avoidance */
ENUM_ELEMENT(EVENT_WIFI_CHANNEL_AVOID_LIST, 0x00AF),
ENUM_ELEMENT(EVENT_SECURE_HOST_COMM, 0x00AE),
/* Always keep this last */
ENUM_ELEMENT_LAST(__HostEvent_Last)

View file

@ -1202,9 +1202,6 @@ typedef enum _ENH_PS_MODES {
EN_AUTO_PS = 0xff,
} ENH_PS_MODES;
/** Secure Host command, payload is encrypted*/
#define HostCmd_Encrypted_BIT 0x4000
/** Command RET code, MSB is set to 1 */
#define HostCmd_RET_BIT 0x8000
@ -2391,20 +2388,6 @@ typedef MLAN_PACK_START struct _HostCmd_DS_GTK_REKEY_PARAMS {
t_u32 replay_ctr_high;
} MLAN_PACK_END HostCmd_DS_GTK_REKEY_PARAMS;
typedef MLAN_PACK_START struct _SECURE_HOST_MSG_HEADER {
t_u32 magic;
t_u16 len;
t_u8 id;
t_u8 version;
} MLAN_PACK_END SECURE_HOST_MSG_HEADER;
typedef MLAN_PACK_START struct _HostCmd_DS_SECURE_HOST {
/** Action */
t_u16 action;
/** TLS handshake message data */
t_u8 tls_data[1024];
} MLAN_PACK_END HostCmd_DS_SECURE_HOST;
/** Data structure of WMM QoS information */
typedef MLAN_PACK_START struct _WmmQosInfo_t {
#ifdef BIG_ENDIAN_SUPPORT
@ -7942,8 +7925,6 @@ typedef struct MLAN_PACK_START _HostCmd_DS_COMMAND {
HostCmd_CMD_802_11_STA_TX_RATE sta_rx_rate;
HostCmd_MCLIENT_SCHEDULE_CFG mclient_cfg;
HostCmd_DS_SECURE_HOST shc;
/** WMM HOST ADDTS */
HostCmd_DS_WMM_HOST_ADDTS_REQ host_add_ts;
/** WMM HOST DELTS */
@ -8090,19 +8071,4 @@ typedef MLAN_PACK_START struct _Event_DPD_CAL_t {
t_u8 sub_band;
} MLAN_PACK_END Event_DPD_CAL_t;
typedef MLAN_PACK_START struct _SECURE_HOST_EVENT_HEADER {
/** Type ID */
t_u16 type_id;
t_u16 bbs;
/** Action */
t_u16 tls_action;
} MLAN_PACK_END SECURE_HOST_EVENT_HEADER;
typedef MLAN_PACK_START struct SECURE_HOST_EVENT {
/** TLS handshake message header */
SECURE_HOST_EVENT_HEADER tls_header;
/** TLS handshake message data */
t_u8 tls_data[];
} MLAN_PACK_END SECURE_HOST_EVENT;
#endif /* !_MLAN_FW_H_ */

View file

@ -489,8 +489,6 @@ ENUM_ELEMENT(HostCmd_CMD_GET_HW_SPEC, 0x0003),
ENUM_ELEMENT(HostCmd_CMD_NAV_MITIGATION_HW_CFG, 0x0294),
ENUM_ELEMENT(HostCmd_CMD_SECURE_HOST, 0x0295),
/** Host Command ID : To enable/disable/force-enable PPB feature */
ENUM_ELEMENT(HostCmd_CMD_PREAMBLE_PWR_BOOST, 0x0296),
/* Always keep this last */

View file

@ -1578,11 +1578,7 @@ mlan_status wlan_init_fw(pmlan_adapter pmadapter)
#ifdef MFG_CMD_SUPPORT
if (pmadapter->mfg_mode != MTRUE) {
#endif
if (pmadapter->shc_secure_host) {
wlan_adapter_func_init(pmadapter);
} else {
wlan_adapter_get_hw_spec(pmadapter);
}
wlan_adapter_get_hw_spec(pmadapter);
#ifdef MFG_CMD_SUPPORT
}
#ifdef PCIE
@ -1593,8 +1589,7 @@ mlan_status wlan_init_fw(pmlan_adapter pmadapter)
}
}
if (((pmadapter->card_type) & 0xff) == CARD_TYPE_AW693 &&
(!pmadapter->shc_secure_host)) {
if (((pmadapter->card_type) & 0xff) == CARD_TYPE_AW693) {
ret = wlan_prepare_cmd(priv, HostCmd_CMD_FUNC_INIT,
HostCmd_ACT_GEN_SET, 0, MNULL, MNULL);
if (ret) {

View file

@ -30,8 +30,6 @@ Change log:
#ifndef _MLAN_MAIN_H_
#define _MLAN_MAIN_H_
#include "nanotls-host.h"
#ifdef DEBUG_LEVEL1
extern t_void (*print_callback)(t_pvoid pmoal_handle, t_u32 level,
char *pformat, IN...);
@ -709,7 +707,6 @@ typedef enum _WLAN_HARDWARE_STATUS {
WlanHardwareStatusReady,
WlanHardwareStatusGetHwSpec,
WlanHardwareStatusGetHwSpecdone,
WlanHardwareStatusSecHandshake,
WlanHardwareStatusInitializing,
WlanHardwareStatusInitdone,
WlanHardwareStatusReset,
@ -3140,8 +3137,6 @@ struct _mlan_adapter {
/** agiled channel switch info */
agcs_stats agcs_info;
#endif /* UAP_SUPPORT */
t_u32 shc_secure_host;
};
/** IPv4 ARP request header */
@ -5220,10 +5215,4 @@ void wlan_delete_iPhone_entry(mlan_private *priv, t_u8 *mac);
extern void print_chan_switch_block_event(t_u16 reason_code);
mlan_status wlan_adapter_func_init(pmlan_adapter pmadapter);
mlan_status wlan_cmd_secure_host(pmlan_private pmpriv, HostCmd_DS_COMMAND *cmd,
t_pvoid pdata_buf);
mlan_status wlan_process_secure_host_event(pmlan_private pmpriv, t_u8 *data,
t_u32 len);
#endif /* !_MLAN_MAIN_H_ */

View file

@ -547,16 +547,6 @@ mlan_status mlan_register(pmlan_device pmdevice, t_void **ppmlan_adapter)
(t_u8)pmdevice->bss_attr[0].bss_num;
}
pmadapter->shc_secure_host = pmdevice->secure_host;
if (pmadapter->second_mac && pmadapter->shc_secure_host) {
if (pcb->moal_secure_host_derive_traffic_keys(
pmadapter->pmoal_handle) ||
pcb->moal_secure_host_data_ctx_init(
pmadapter->pmoal_handle)) {
goto error;
}
}
/* init function table */
for (j = 0; mlan_ops[j]; j++) {
if (mlan_ops[j]->bss_role == GET_BSS_ROLE(pmadapter->priv[0])) {

View file

@ -3900,23 +3900,6 @@ static mlan_status wlan_cmd_auth_assoc_timeout_cfg(pmlan_private pmpriv,
return MLAN_STATUS_SUCCESS;
}
mlan_status wlan_cmd_secure_host(pmlan_private pmpriv, HostCmd_DS_COMMAND *cmd,
t_void *pdata_buf)
{
HostCmd_DS_SECURE_HOST *shc = &cmd->params.shc;
SECURE_HOST_MSG_HEADER *tls_hdr = (SECURE_HOST_MSG_HEADER *)pdata_buf;
t_u16 tls_len = tls_hdr->len;
cmd->command = wlan_cpu_to_le16(HostCmd_CMD_SECURE_HOST);
cmd->size = wlan_cpu_to_le16(S_DS_GEN + sizeof(shc->action) + tls_len);
shc->action = 0x0;
memcpy_ext(pmpriv->adapter, (void *)&shc->tls_data, (void *)pdata_buf,
tls_len, tls_len);
LEAVE();
return MLAN_STATUS_SUCCESS;
}
/**
* @brief This function prepare the command before sending to firmware.
*
@ -4539,9 +4522,6 @@ mlan_status wlan_ops_sta_prepare_cmd(t_void *priv, t_u16 cmd_no,
cmd_action, pdata_buf);
break;
case HostCmd_CMD_SECURE_HOST:
ret = wlan_cmd_secure_host(pmpriv, cmd_ptr, pdata_buf);
break;
default:
PRINTM(MERROR, "PREP_CMD: unknown command- %#x\n", cmd_no);
ret = MLAN_STATUS_FAILURE;

View file

@ -37,7 +37,6 @@
#include "mlan_11ax.h"
#include "mlan_11h.h"
#include "mlan_meas.h"
#include "mlan_shc.h"
/********************************************************
* Local Variables
@ -3475,8 +3474,6 @@ mlan_status wlan_ops_sta_process_cmdresp(t_void *priv, t_u16 cmdresp_no,
case HostCmd_CMD_FUNC_INIT:
case HostCmd_CMD_FUNC_SHUTDOWN:
break;
case HostCmd_CMD_SECURE_HOST:
break;
case HostCmd_CMD_802_11_KEY_MATERIAL:
ret = wlan_ret_802_11_key_material(pmpriv, resp, pioctl_buf);
break;

View file

@ -1661,11 +1661,6 @@ mlan_status wlan_ops_sta_process_event(t_void *priv)
(t_u8 *)pevent->event_buf, pevent->event_len);
wlan_recv_event(pmpriv, pevent->event_id, pevent);
break;
case EVENT_SECURE_HOST_COMM:
ret = wlan_process_secure_host_event(
pmpriv, pmbuf->pbuf + pmbuf->data_offset,
pmbuf->data_len);
break;
default:
PRINTM(MEVENT, "EVENT: unknown event id: %#x\n", eventcause);
wlan_recv_event(pmpriv, MLAN_EVENT_ID_FW_UNKNOWN, MNULL);

View file

@ -687,7 +687,6 @@ typedef enum {
#define MFWDP_D MBIT(21)
#endif /*FWDUMP_VIA_PRINT*/
#define MSCH_D MBIT(22)
#define MSHC_D MBIT(23)
#define MENTRY MBIT(28)
#define MWARN MBIT(29)
#define MINFO MBIT(30)
@ -2761,25 +2760,6 @@ typedef struct _mlan_callbacks {
t_s32 copy_delay);
mlan_status (*moal_calc_short_ssid)(t_u8 *pssid, t_u32 ssid_len,
t_u32 *pshort_ssid);
t_u8 (*moal_secure_host_get_msg_id)(t_void *msg);
mlan_status (*moal_secure_host_init)(t_void *pmoal,
const t_u8 esdca_pub[64]);
void (*moal_secure_host_cleanup)(t_void *pmoal);
mlan_status (*moal_secure_host_do_hello)(t_void *pmoal, t_void **msg);
mlan_status (*moal_secure_host_device_hello_rcvd)(t_void *pmoal,
t_void *msg);
mlan_status (*moal_secure_host_do_finished)(t_void *pmoal,
t_void **msg);
mlan_status (*moal_secure_host_derive_traffic_keys)(t_void *pmoal);
mlan_status (*moal_secure_host_data_ctx_init)(t_void *pmoal);
mlan_status (*moal_secure_host_data_encrypt)(t_void *pmoal,
t_void **enc_data,
t_void **payload,
t_u32 len);
mlan_status (*moal_secure_host_data_decrypt)(t_void *pmoal,
t_void **dec_data,
t_void **payload,
t_u32 len);
/* Unaligned Access */
struct unaligned_access {
t_u16 (*moal_read_u16)(const void *src);
@ -2966,7 +2946,6 @@ typedef struct _mlan_device {
t_u8 disable_11h_tpc;
t_u8 tpe_ie_ignore;
t_u32 amsdu_disable;
t_u32 secure_host;
} mlan_device, *pmlan_device;
/** MLAN API function prototype */

View file

@ -5156,8 +5156,6 @@ static int woal_priv_set_get_drvdbg(moal_private *priv, t_u8 *respbuf,
#endif
printk(KERN_ALERT "MMPA_D (%08x) %s\n", MMPA_D,
(drvdbg & MMPA_D) ? "X" : "");
printk(KERN_ALERT "MSHC_D (%08x) %s\n", MSHC_D,
(drvdbg & MSHC_D) ? "X" : "");
printk(KERN_ALERT "MIF_D (%08x) %s\n", MIF_D,
(drvdbg & MIF_D) ? "X" : "");
printk(KERN_ALERT "MFW_D (%08x) %s\n", MFW_D,
@ -7014,11 +7012,6 @@ static int woal_priv_warmreset(moal_private *priv, t_u8 *respbuf,
moal_private *ref_priv;
ENTER();
if (handle->params.secure_host) {
PRINTM(MERROR,
"Warm reset not supported with secure host enabled\n");
goto done;
}
ret = woal_pre_warmreset(priv);
if (ret)
goto done;

View file

@ -448,9 +448,6 @@ static int make_before_break = 0;
static int auto_11ax = 1;
static int reject_addba_req = 0;
/** secure host mode support */
int secure_host = 0;
/**
* @brief This function read a line in module parameter file
*
@ -1756,12 +1753,6 @@ static mlan_status parse_cfg_read_block(t_u8 *data, t_u32 size,
params->make_before_break = out_data;
PRINTM(MMSG, "make_before_break=%x\n",
params->make_before_break);
} else if (strncmp(line, "secure_host",
strlen("secure_host")) == 0) {
if (parse_line_read_int(line, &out_data) !=
MLAN_STATUS_SUCCESS)
goto err;
params->secure_host = out_data;
}
}
@ -1775,13 +1766,6 @@ static mlan_status parse_cfg_read_block(t_u8 *data, t_u32 size,
params->mclient_scheduling = 0;
#endif
#ifdef PCIE
if (!IS_PCIEAW693(handle->card_type))
params->secure_host = 0;
#else
params->secure_host = 0;
#endif
if (end)
return ret;
err:
@ -2240,16 +2224,6 @@ static void woal_setup_module_param(moal_handle *handle, moal_mod_para *params)
if (params)
handle->params.tpe_ie_ignore = params->tpe_ie_ignore;
handle->params.make_before_break = make_before_break;
handle->params.secure_host = secure_host;
if (params)
handle->params.secure_host = params->secure_host;
#ifdef PCIE
if (!IS_PCIEAW693(handle->card_type))
handle->params.secure_host = 0;
#else
handle->params.secure_host = 0;
#endif
}
/**
@ -2861,14 +2835,6 @@ void woal_init_from_dev_tree(void)
make_before_break = data;
}
}
else if (!strncmp(prop->name, "secure_host",
strlen("secure_host"))) {
if (!of_property_read_u32(dt_node, prop->name, &data)) {
PRINTM(MIOCTL, "secure_host=0x%x\n", data);
secure_host = data;
}
}
}
LEAVE();
return;
@ -3530,8 +3496,3 @@ module_param(make_before_break, int, 0);
MODULE_PARM_DESC(
make_before_break,
"1: make_before_break during roam; 0: no make_before_break during roam");
module_param(secure_host, int, 0660);
MODULE_PARM_DESC(
secure_host,
"0: Disable secure host mode(default); 1: Enable secure host mode");

View file

@ -84,7 +84,6 @@ Change log:
#include <linux/rtc.h>
#include <linux/utsname.h>
#endif
#include "moal_shc.h"
/********************************************************
Global Variables
@ -942,8 +941,7 @@ static struct _card_info card_info_SD8987 = {
/** Driver version */
char driver_version[MLAN_MAX_VER_STR_LEN] =
INTF_CARDTYPE KERN_VERSION "--" MLAN_RELEASE_VERSION "-GPL"
"-("
INTF_CARDTYPE KERN_VERSION "--" MLAN_RELEASE_VERSION "-("
"FP" FPNUM ")"
#ifdef DEBUG_LEVEL2
"-dbg"
@ -1013,18 +1011,6 @@ static mlan_callbacks woal_callbacks = {
.moal_tp_accounting_rx_param = moal_tp_accounting_rx_param,
.moal_amsdu_tp_accounting = moal_amsdu_tp_accounting,
.moal_calc_short_ssid = moal_calc_short_ssid,
.moal_secure_host_get_msg_id = moal_secure_host_get_msg_id,
.moal_secure_host_init = moal_secure_host_init,
.moal_secure_host_cleanup = moal_secure_host_cleanup,
.moal_secure_host_do_hello = moal_secure_host_do_hello,
.moal_secure_host_device_hello_rcvd =
moal_secure_host_device_hello_rcvd,
.moal_secure_host_do_finished = moal_secure_host_do_finished,
.moal_secure_host_derive_traffic_keys =
moal_secure_host_derive_traffic_keys,
.moal_secure_host_data_ctx_init = moal_secure_host_data_ctx_init,
.moal_secure_host_data_encrypt = moal_secure_host_data_encrypt,
.moal_secure_host_data_decrypt = moal_secure_host_data_decrypt,
.moal_unaligned_access.moal_read_u16 = moal_read_unaligned_u16,
.moal_unaligned_access.moal_read_u32 = moal_read_unaligned_u32,
.moal_unaligned_access.moal_write_u16 = moal_write_unaligned_u16,
@ -2574,8 +2560,6 @@ mlan_status woal_init_sw(moal_handle *handle)
t_void *pmlan;
int cfg80211_wext = handle->params.cfg80211_wext;
moal_handle *ref_handle = NULL;
ENTER();
/* Initialize moal_handle structure */
@ -2936,22 +2920,6 @@ mlan_status woal_init_sw(moal_handle *handle)
/* Clean up the mode_psd_string for 6E Indoor/Outdoor */
memset(handle->mode_psd_string, 0, sizeof(handle->mode_psd_string));
device.secure_host = (t_u32)handle->params.secure_host;
if (handle->second_mac && handle->params.secure_host &&
handle->pref_mac) {
ref_handle = (moal_handle *)handle->pref_mac;
handle->secure = ref_handle->secure;
}
if (handle->second_mac && handle->params.secure_host &&
!handle->secure) {
PRINTM(MERROR, "secure host handshake incomplete\n");
ret = MLAN_STATUS_FAILURE;
LEAVE();
return ret;
}
moal_memcpy_ext(handle, &device.callbacks, &woal_callbacks,
sizeof(mlan_callbacks), sizeof(mlan_callbacks));
if (!handle->params.amsdu_deaggr)
@ -3039,14 +3007,6 @@ void woal_free_moal_handle(moal_handle *handle)
fwdump_fname = NULL;
}
if (handle->params.secure_host) {
moal_secure_host_cleanup(handle);
handle->secure = NULL;
if (handle->pref_mac) {
ref_handle = (moal_handle *)handle->pref_mac;
ref_handle->secure = NULL;
}
}
/* Free module params */
woal_free_module_param(handle);
/** clear pref_mac to avoid later crash */

View file

@ -2902,7 +2902,6 @@ typedef struct _moal_mod_para {
int tpe_ie_ignore;
/* make_before_break during roam */
int make_before_break;
int secure_host;
} moal_mod_para;
void woal_tp_acnt_timer_func(void *context);
@ -3510,8 +3509,6 @@ struct _moal_handle {
mlan_hw_info hw_info;
#endif /* UAP_SUPPORT */
void *secure;
#ifdef DUMP_TO_PROC
#if defined(PCIE)
/** ssu dump buffer total len */

View file

@ -2604,8 +2604,6 @@ static int woal_drv_dbg(moal_private *priv, struct iwreq *wrq)
#endif
printk(KERN_ALERT "MMPA_D (%08x) %s\n", MMPA_D,
(drvdbg & MMPA_D) ? "X" : "");
printk(KERN_ALERT "MSHC_D (%08x) %s\n", MSHC_D,
(drvdbg & MSHC_D) ? "X" : "");
printk(KERN_ALERT "MIF_D (%08x) %s\n", MIF_D,
(drvdbg & MIF_D) ? "X" : "");
printk(KERN_ALERT "MFW_D (%08x) %s\n", MFW_D,

View file

@ -1,315 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef NANOTLS_COMMON_H
#define NANOTLS_COMMON_H
#include <nanotls-config.h>
#include <nanotls-hooks.h>
#include <nanotls-proto.h>
#include <nanotls-types.h>
#include <uassert.h>
// TODO: cleanup
#undef memset
#define ABS(x) (((x) < 0) ? -(x) : (x))
#define offsetdiff(s, field_a, field_b) \
ABS(offsetof(s, field_b) - offsetof(s, field_a))
enum ecode nanotls_mem_eq(const void *a, const void *b, size_t size);
void nanotls_mem_erase(void *buf, size_t size);
static inline void nanotls_hdr_init(struct nanotls_message_hdr *hdr, size_t len,
enum nanotls_message_id id)
{
hdr->magic = NANOTLS_MAGIC;
hdr->len = (uint16_t)len;
hdr->message_id = (uint8_t)id;
hdr->protocol_version = NANOTLS_VERSION;
}
static inline enum ecode
nanotls_hdr_check(const struct nanotls_message_hdr *hdr, size_t len,
enum nanotls_message_id id)
{
if (hdr->magic != NANOTLS_MAGIC)
return E_INVALID;
if (hdr->len != (uint16_t)len)
return E_INVALID;
if (hdr->message_id != (uint8_t)id)
return E_INVALID;
if (hdr->protocol_version != NANOTLS_VERSION)
return E_INVALID;
return E_OK;
}
static inline enum ecode nanotls_data_add_init(struct nanotls_data_add *add,
size_t data_size)
{
if (data_size > NANOTLS_MAX_DATA_LEN)
return E_INVALID;
memset(add, 0, sizeof(*add));
add->version = NANOTLS_VERSION;
add->len = data_size;
return E_OK;
}
void nanotls_hkdf_sha256_extract(const void *salt, size_t salt_len,
const void *ikm, size_t ikm_len,
uint8_t prk_result[NANOTLS_SHA256_HASH_SIZE]);
/*
* TLS HKDF-Expand-Label(Secret, Label, Context, Hash.length) =
* HKDF-Expand(Secret, Length || Label || Context, Length) =
* HMAC-Hash(Secret, Length || Label || Context || 0x01, Length)
*
* Here Length is fixed to 32.
*/
void nanotls_hkdf_expand_label(const uint8_t secret[NANOTLS_SHA256_HASH_SIZE],
const void *label, size_t label_size,
const uint8_t *context, size_t context_size,
void *output, size_t output_length);
/* TLS Derive-Secret with fixed context size = hash size */
static inline void
nanotls_hkdf_derive_secret(const uint8_t secret[NANOTLS_SHA256_HASH_SIZE],
const void *label, size_t label_size,
const uint8_t context[NANOTLS_SHA256_HASH_SIZE],
uint8_t output[NANOTLS_SECRET_SIZE])
{
nanotls_hkdf_expand_label(secret, label, label_size, context,
NANOTLS_SHA256_HASH_SIZE, output,
NANOTLS_SECRET_SIZE);
}
/*
* HKDF-Expand-Label(Secret, Label, Context = "", Length) =
* HKDF-Expand(Secret, Length || Label || Context, Length) =
* HKDF-Expand(Secret, Length || Label, Length)
*/
static inline void
nanotls_hkdf_expand_label_noctx(const uint8_t secret[NANOTLS_SHA256_HASH_SIZE],
const void *label, size_t label_size,
uint8_t *output, size_t output_length)
{
nanotls_hkdf_expand_label(secret, label, label_size, NULL, 0, output,
output_length);
}
static inline void nanotls_derive_master_secret(
uint8_t master_secret[NANOTLS_SECRET_SIZE],
const uint8_t handshake_secret[NANOTLS_SECRET_SIZE])
{
uint8_t zeroes[NANOTLS_HASH_SIZE] = {0};
uint8_t dhs[NANOTLS_SECRET_SIZE];
nanotls_hkdf_expand_label_noctx(handshake_secret, NANOTLS_DERIVED_LABEL,
dhs, sizeof(dhs));
nanotls_hkdf_sha256_extract(dhs, sizeof(dhs), zeroes, sizeof(zeroes),
master_secret);
nanotls_mem_erase(dhs, sizeof(dhs));
}
void nanotls_write_finished(const nanotls_sha_ctx *handshake_hash,
const uint8_t hs_traffic_secret[NANOTLS_SECRET_SIZE],
uint8_t verify_data[NANOTLS_SHA256_HASH_SIZE]);
void nanotls_derive_key_iv(uint8_t key[NANOTLS_KEY_SIZE],
uint8_t iv[NANOTLS_IV_SIZE],
const uint8_t secret[NANOTLS_SECRET_SIZE]);
static inline void
nanotls_handshake_encrypt(void *buf, size_t size, struct auth_tag *auth_tag,
const uint8_t traffic_secret[NANOTLS_SECRET_SIZE])
{
#ifdef NANOTLS_CONFIG_ENCRYPTED_HANDSHAKE
nanotls_aes_context aes_ctx;
const struct nanotls_data_add add = {.version = NANOTLS_VERSION,
.len = (uint16_t)size};
uint8_t key[NANOTLS_KEY_SIZE];
uint8_t iv[NANOTLS_KEY_SIZE];
uassert(size <= NANOTLS_MAX_DATA_LEN);
nanotls_derive_key_iv(key, iv, traffic_secret);
nanotls_aes_ctx_init(&aes_ctx, key);
nanotls_mem_erase(key, sizeof(key));
nanotls_aes_encrypt(&aes_ctx, buf, buf, size, iv, &add, sizeof(add),
auth_tag->tag);
nanotls_mem_erase(iv, sizeof(iv));
nanotls_aes_ctx_destroy(&aes_ctx);
#else
(void)buf;
(void)size;
(void)auth_tag;
(void)traffic_secret;
#endif
}
static inline enum ecode nanotls_handshake_decrypt_verify(
void *buf, size_t size, const struct auth_tag *auth_tag,
const uint8_t traffic_secret[NANOTLS_SECRET_SIZE])
{
#ifdef NANOTLS_CONFIG_ENCRYPTED_HANDSHAKE
nanotls_aes_context aes_ctx;
const struct nanotls_data_add add = {.version = NANOTLS_VERSION,
.len = (uint16_t)size};
uint8_t key[NANOTLS_KEY_SIZE];
uint8_t iv[NANOTLS_KEY_SIZE];
uint8_t tag_calc[NANOTLS_AUTH_TAG_SIZE];
uassert(size <= NANOTLS_MAX_DATA_LEN);
nanotls_derive_key_iv(key, iv, traffic_secret);
nanotls_aes_ctx_init(&aes_ctx, key);
nanotls_mem_erase(key, sizeof(key));
nanotls_aes_decrypt(&aes_ctx, buf, buf, size, iv, &add, sizeof(add),
tag_calc);
nanotls_mem_erase(iv, sizeof(iv));
nanotls_aes_ctx_destroy(&aes_ctx);
return nanotls_mem_eq(tag_calc, auth_tag->tag, sizeof(tag_calc));
#else
(void)buf;
(void)size;
(void)auth_tag;
(void)traffic_secret;
return E_OK;
#endif
}
static inline void
nanotls_handshake_sig_hash(const uint8_t transcript_hash[NANOTLS_HASH_SIZE],
const char *context, size_t context_len,
uint8_t output_hash[NANOTLS_SHA256_HASH_SIZE])
{
nanotls_sha_ctx sha_ctx;
const uint8_t zero = 0;
/*
* The digital signature is then computed over the concatenation of:
* - A string that consists of octet 32 (0x20) repeated 64 times
* - The context string
* - A single 0 byte which serves as the separator
* - The content to be signed
*/
memset(output_hash, 0x20, NANOTLS_SHA256_HASH_SIZE);
nanotls_sha256_init(&sha_ctx);
/* 2x hash size = block size of 0x20 */
nanotls_sha256_update(&sha_ctx, output_hash, NANOTLS_SHA256_HASH_SIZE);
nanotls_sha256_update(&sha_ctx, output_hash, NANOTLS_SHA256_HASH_SIZE);
nanotls_sha256_update(&sha_ctx, context, context_len);
nanotls_sha256_update(&sha_ctx, &zero, sizeof(zero));
nanotls_sha256_update(&sha_ctx, transcript_hash,
NANOTLS_SHA256_HASH_SIZE);
nanotls_sha256_digest_final(&sha_ctx, output_hash);
}
static inline void nanotls_derive_traffic_keys(
const uint8_t master_secret[NANOTLS_SECRET_SIZE],
const uint8_t transcript_hash[NANOTLS_SHA256_HASH_SIZE],
const uint8_t *instance_label, size_t instance_label_len,
struct nanotls_traffic_keys *keys)
{
/*
* Device application traffic secret:
* device_traffic_secret = Derive-Secret(master_secret, label,
* transcript_hash(HostHello || DeviceHello || HostFinished)) =
* HKDF-Expand(master_secret, 32 || label || transcript_hash, 32)
*/
nanotls_hkdf_derive_secret(master_secret, NANOTLS_DEVICE_AP_TS_LABEL,
transcript_hash, keys->d2h_secret);
if (instance_label) {
/*
* Device application traffic (instance) secret:
* device_traffic_secret_n =
* HKDF-Expand-Label(device_traffic_secret, instance_label, "")
*/
nanotls_hkdf_expand_label_noctx(
keys->d2h_secret, instance_label, instance_label_len,
keys->d2h_secret, NANOTLS_SECRET_SIZE);
}
nanotls_derive_key_iv(keys->d2h_key, keys->d2h_iv, keys->d2h_secret);
/*
* Host application traffic secret:
* host_traffic_secret = Derive-Secret(master_secret, label,
* transcript_hash(HostHello || DeviceHello || HostFinished)) =
* HKDF-Expand(master_secret, 32 || label || transcript_hash, 32)
*/
nanotls_hkdf_derive_secret(master_secret, NANOTLS_HOST_AP_TS_LABEL,
transcript_hash, keys->h2d_secret);
if (instance_label) {
/*
* Host application traffic (instance) secret:
* host_traffic_secret_n =
* HKDF-Expand-Label(host_traffic_secret, instance_label, "")
*/
nanotls_hkdf_expand_label_noctx(
keys->h2d_secret, instance_label, instance_label_len,
keys->h2d_secret, NANOTLS_SECRET_SIZE);
}
nanotls_derive_key_iv(keys->h2d_key, keys->h2d_iv, keys->h2d_secret);
}
#ifdef NANOTLS_CONFIG_KEY_UPDATE
static inline enum ecode
nanotls_update_traffic_secret(uint8_t secret[NANOTLS_SECRET_SIZE],
uint8_t key[NANOTLS_KEY_SIZE],
uint8_t iv[NANOTLS_IV_SIZE])
{
/*
* Update the D2H traffic secret:
* xxx_secret(N+1) = HKDF-Expand-Label(xxx_secret(N), label, "")
*/
nanotls_hkdf_expand_label_noctx(secret, NANOTLS_KEY_UPDATE_LABEL,
secret, NANOTLS_SECRET_SIZE);
nanotls_derive_key_iv(key, iv, secret);
return E_OK;
}
#endif
#endif /* NANOTLS_COMMON_H */

View file

@ -1,67 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef NANOTLS_CONFIG_H
#define NANOTLS_CONFIG_H
/* nanoTLS compile-time configuration options */
/*
* Harden implementation against glitching, SCA, etc.
* Will increase the code size.
*/
//#define NANOTLS_CONFIG_HARDENING
/*
* Enable support for key update
*/
#define NANOTLS_CONFIG_KEY_UPDATE
/*
* Maximum number of failures during handshake before transitioning
* into error state.
* If not defined, then there is no limit (unsafe).
*/
#ifndef NANOTLS_CONFIG_MAX_ERR_CNT
#define NANOTLS_CONFIG_MAX_ERR_CNT 4
#endif
/*
* Maximum number of consecutive data AES-GCM tag failures before going into
* error state and erasing the keys. If not defined, then there is no limit
* (unsafe).
*/
#ifndef NANOTLS_CONFIG_MAX_DATA_TAG_MISMATCH_CNT
#define NANOTLS_CONFIG_MAX_DATA_TAG_MISMATCH_CNT 256
#endif
/*
* Enable encryption during handshake.
* ECDSA signature and Finished messages are encrypted.
* AES-GCM is used and authentication tag is generated and verified.
* Introduces dependency on AES API during handshake.
*
* NB: TLS 1.2 does not encrypt these messages, encryption introduced in TLS 1.3
*/
#define NANOTLS_CONFIG_ENCRYPTED_HANDSHAKE
#endif /* NANOTLS_CONFIG_H */

View file

@ -1,100 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef NANOTLS_DATA_H
#define NANOTLS_DATA_H
#include <linux/types.h>
#include <asm/string.h>
#include <nanotls-common.h>
#include <nanotls-proto.h>
#include <nanotls-hooks.h>
/*
* NanoTLS data layer - used for message encryption/decryption
* once handshake is completed.
*/
struct nanotls_ctx_data {
enum nanotls_role role;
uint64_t enc_seq_no;
uint8_t enc_iv[NANOTLS_IV_SIZE];
nanotls_aes_context aes_enc;
size_t enc_len_remaining;
uint64_t dec_seq_no;
uint8_t dec_iv[NANOTLS_IV_SIZE];
nanotls_aes_context aes_dec;
size_t dec_len_remaining;
size_t dec_tag_mismatch_count;
#ifdef NANOTLS_CONFIG_KEY_UPDATE
uint8_t enc_secret[NANOTLS_SECRET_SIZE];
uint8_t dec_secret[NANOTLS_SECRET_SIZE];
#endif
};
enum ecode nanotls_data_ctx_init(struct nanotls_ctx_data *ctx,
enum nanotls_role role,
const struct nanotls_traffic_keys *keys);
enum ecode nanotls_data_ctx_destroy(struct nanotls_ctx_data *ctx);
enum ecode nanotls_data_write_start(struct nanotls_ctx_data *ctx,
size_t data_size);
/*
* All but the final invocation MUST be called with length mod 16 == 0.
* Only the final call can have a partial block length of < 128 bits.
*/
enum ecode nanotls_data_write_update(struct nanotls_ctx_data *ctx,
void *enc_out, const void *plain_in,
size_t data_size);
enum ecode nanotls_data_write_finish(struct nanotls_ctx_data *ctx,
uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE]);
enum ecode nanotls_data_write(struct nanotls_ctx_data *ctx, void *enc_out,
const void *plain_in, size_t data_size,
uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE]);
enum ecode nanotls_data_read_start(struct nanotls_ctx_data *ctx,
size_t data_size);
/*
* All but the final invocation MUST be called with length mod 16 == 0.
* Only the final call can have a partial block length of < 128 bits.
*/
enum ecode nanotls_data_read_update(struct nanotls_ctx_data *ctx,
void *plain_out, const void *enc_in,
size_t data_size);
enum ecode
nanotls_data_read_finish(struct nanotls_ctx_data *ctx,
const uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE]);
enum ecode nanotls_data_read(struct nanotls_ctx_data *ctx, void *plain_out,
const void *enc_in, size_t data_size,
const uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE]);
#ifdef NANOTLS_CONFIG_KEY_UPDATE
enum ecode nanotls_data_write_key_update(struct nanotls_ctx_data *ctx);
enum ecode nanotls_data_read_key_update(struct nanotls_ctx_data *ctx);
#endif
#endif /* NANOTLS_DATA_H */

View file

@ -1,101 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef NANOTLS_DEVICE_H
#define NANOTLS_DEVICE_H
#include <nanotls-config.h>
#include <nanotls-hooks.h>
#include <nanotls-proto.h>
enum nanotls_device_state {
NANOTLS_DEV_UNINIT = 0,
#ifdef NANOTLS_CONFIG_HARDENING
NANOTLS_DEV_INIT = 0x32eaf5,
NANOTLS_DEV_HELLO_RCVD = 0x7a52ae,
NANOTLS_DEV_HELLO_SENT = 0x5e3a0a,
NANOTLS_DEV_DONE = 0xb09835,
NANOTLS_DEV_ERR = 0xed2312,
#else
NANOTLS_DEV_INIT,
NANOTLS_DEV_HELLO_RCVD,
NANOTLS_DEV_HELLO_SENT,
NANOTLS_DEV_DONE,
NANOTLS_DEV_ERR = -1,
#endif
};
struct nanotls_device_ctx {
enum nanotls_device_state state;
size_t error_count;
struct nanotls_device_info device_info;
/* ECDSA - Authentication */
uint8_t ecdsa_priv[NANOTLS_ECDSA_PRIVATE_KEY_SIZE];
/* ECDH - Key negotiation */
uint8_t ecdh_priv[NANOTLS_ECDH_PRIVATE_KEY_SIZE];
struct nanotls_key_share host_key_share;
/* Running transcript hash context */
nanotls_sha_ctx handshake_hash;
uint8_t handshake_h2_hash[NANOTLS_HASH_SIZE];
uint8_t handshake_secret[NANOTLS_SECRET_SIZE];
uint8_t master_secret[NANOTLS_SECRET_SIZE];
};
/**
* Initialize the device handshake context.
*
* @param ctx device handshake context
* @param ecdsa_priv device ECDSA private key (matching the host public key)
* @param device_info device information to be forwarded to the host as part of
* DeviceHello. Could optionally be NULL, then device_info will be filled with
* zeroes.
* @return E_OK in case of success, error code otherwise
*/
enum ecode
nanotls_device_init(struct nanotls_device_ctx *ctx,
const uint8_t ecdsa_priv[NANOTLS_ECDSA_PRIVATE_KEY_SIZE],
const struct nanotls_device_info *device_info);
enum ecode nanotls_device_host_hello_rcvd(struct nanotls_device_ctx *ctx,
const struct nanotls_host_hello *msg);
enum ecode nanotls_device_do_hello(struct nanotls_device_ctx *ctx,
struct nanotls_device_hello *msg);
enum ecode nanotls_device_host_finished_rcvd(struct nanotls_device_ctx *ctx,
struct nanotls_host_finished *msg);
enum ecode nanotls_device_derive_traffic_keys_instance(
const struct nanotls_device_ctx *ctx, struct nanotls_traffic_keys *keys,
size_t instance_idx);
enum ecode
nanotls_device_derive_traffic_keys(const struct nanotls_device_ctx *ctx,
struct nanotls_traffic_keys *keys);
enum ecode nanotls_device_cleanup(struct nanotls_device_ctx *ctx);
#endif /* NANOTLS_DEVICE_H */

View file

@ -1,121 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef NANOTLS_HOOKS_H
#define NANOTLS_HOOKS_H
#include <linux/types.h>
#include <asm/string.h>
#include <nanotls-types.h>
#include <sha2.h>
#include <hmac.h>
#include <gcm.h>
#define NANOTLS_SHA256_HASH_SIZE 32
#define NANOTLS_SHA256_BLOCK_SIZE 64
typedef cf_sha256_context nanotls_sha_ctx;
typedef cf_hmac_ctx nanotls_hmac_ctx;
// TODO: cleanup
#undef MIN
#undef MAX
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define ROUND_UP(x, align) ((((x) + (align)-1) / (align)) * (align))
#define ROUND_DN(x, align) (((x) / (align)) * (align))
// TODO: cleanup
//#define DIV_ROUND_UP(x, y) (((x) + (y)-1) / (y))
enum ecode nanotls_secure_random(void *buf, size_t size);
enum ecode nanotls_p256_gen_keypair(uint8_t priv[32], uint8_t pub[64]);
enum ecode nanotls_p256_ecdh_shared_secret(uint8_t secret[32],
const uint8_t priv[32],
const uint8_t pub[64]);
enum ecode nanotls_p256_ecdsa_sign(uint8_t sig[64], const uint8_t priv[32],
const uint8_t *hash, size_t hlen);
enum ecode nanotls_p256_ecdsa_verify(const uint8_t sig[64],
const uint8_t pub[64], const uint8_t *hash,
size_t hlen);
enum ecode nanotls_sha256_init(nanotls_sha_ctx *ctx);
enum ecode nanotls_sha256_update(nanotls_sha_ctx *ctx, const void *buf,
size_t size);
enum ecode nanotls_sha256_digest(const nanotls_sha_ctx *ctx,
uint8_t hash[NANOTLS_SHA256_HASH_SIZE]);
enum ecode nanotls_sha256_digest_final(nanotls_sha_ctx *ctx,
uint8_t hash[NANOTLS_SHA256_HASH_SIZE]);
enum ecode nanotls_hmac_sha256_init(nanotls_hmac_ctx *ctx, const void *key,
size_t key_size);
enum ecode nanotls_hmac_sha256_update(nanotls_hmac_ctx *ctx, const void *data,
size_t data_size);
enum ecode nanotls_hmac_sha256_finish(nanotls_hmac_ctx *ctx,
uint8_t result[NANOTLS_SHA256_HASH_SIZE]);
enum ecode nanotls_hmac_sha256(const void *key, size_t key_size,
const void *data, size_t data_size,
uint8_t result[NANOTLS_SHA256_HASH_SIZE]);
/*
* AES API - needed post-handshake.
* Only AES-GCM-128 is supported with fixed 12 byte IV size.
*/
#define NANOTLS_AES_KEY_SIZE 16
#define NANOTLS_AES_IV_SIZE 12
#define NANOTLS_AES_GCM_TAG_SIZE 16
typedef gcm_context nanotls_aes_context;
enum ecode nanotls_aes_ctx_init(nanotls_aes_context *ctx,
const uint8_t key[NANOTLS_AES_KEY_SIZE]);
enum ecode nanotls_aes_start_enc(nanotls_aes_context *ctx,
const uint8_t iv[NANOTLS_AES_IV_SIZE],
const void *add, size_t add_size);
enum ecode nanotls_aes_start_dec(nanotls_aes_context *ctx,
const uint8_t iv[NANOTLS_AES_IV_SIZE],
const void *add, size_t add_size);
/*
* All but the final invocation MUST be called with length mod 16 == 0.
* Only the final call can have a partial block length of < 128 bits.
*/
enum ecode nanotls_aes_update(nanotls_aes_context *ctx, void *out,
const void *in, size_t data_size);
enum ecode nanotls_aes_finish(nanotls_aes_context *ctx,
uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE]);
enum ecode nanotls_aes_ctx_destroy(nanotls_aes_context *ctx);
enum ecode nanotls_aes_encrypt(nanotls_aes_context *ctx, void *out_data,
const void *in_data, size_t data_size,
const uint8_t iv[NANOTLS_AES_IV_SIZE],
const void *add, size_t add_size,
uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE]);
enum ecode nanotls_aes_decrypt(nanotls_aes_context *ctx, void *out_data,
const void *in_data, size_t data_size,
const uint8_t iv[NANOTLS_AES_IV_SIZE],
const void *add, size_t add_size,
uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE]);
#endif /* NANOTLS_HOOKS_H */

View file

@ -1,145 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef NANOTLS_HOST_H
#define NANOTLS_HOST_H
#include <nanotls-config.h>
#include <nanotls-hooks.h>
#include <nanotls-proto.h>
enum nanotls_host_state {
NANOTLS_HOST_UNINIT = 0,
#ifdef NANOTLS_CONFIG_HARDENING
NANOTLS_HOST_INIT = 0x32eaf5,
NANOTLS_HOST_HELLO_SENT = 0x7a52ae,
NANOTLS_HOST_HELLO_RCVD = 0x5e3a0a,
NANOTLS_HOST_DONE = 0xb09835,
NANOTLS_HOST_ERR = 0xed2312,
#else
NANOTLS_HOST_INIT,
NANOTLS_HOST_HELLO_SENT,
NANOTLS_HOST_HELLO_RCVD,
NANOTLS_HOST_DONE,
NANOTLS_HOST_ERR = -1,
#endif
};
struct nanotls_host_ctx;
/**
* Public key request callback.
* A callback called from nanotls_host_device_hello_rcvd() called to
* request a device public key if it has not been provided during context init
* time.
* @param ctx [in] host handshake context (for reference)
* @param device_info [in] device information data provided by the device in
* DeviceHello. Could be used by the driver to narrow down the FW image search.
* @param ecdsa_pub [out] destination buffer callback function should write
* resulting ecdsa public key to
* @return E_OK in case of public key found and successfully written to
* ecdsa_pub buffer, error code otherwise
*/
typedef enum ecode (*nanotls_host_pub_key_req_cb)(
const struct nanotls_host_ctx *ctx,
const struct nanotls_device_info *device_info,
uint8_t ecdsa_pub[NANOTLS_ECDSA_PUBLIC_KEY_SIZE]);
struct nanotls_host_ctx {
enum nanotls_host_state state;
size_t error_count;
struct nanotls_device_info device_info;
/* ECDSA - Authentication */
uint8_t ecdsa_pub[NANOTLS_ECDSA_PUBLIC_KEY_SIZE];
nanotls_host_pub_key_req_cb pub_key_req_cb; /* callback to request the
public key */
/* ECDH - Key negotiation */
uint8_t ecdh_priv[NANOTLS_ECDH_PRIVATE_KEY_SIZE];
struct nanotls_key_share device_key_share;
/* Running transcript hash context */
nanotls_sha_ctx handshake_hash;
uint8_t handshake_h2_hash[NANOTLS_HASH_SIZE];
uint8_t handshake_secret[NANOTLS_SECRET_SIZE];
uint8_t master_secret[NANOTLS_SECRET_SIZE];
};
/**
* Initialize the host handshake context with the known public key.
*
* This init function flavor assumes the public key is known at the init time.
*
* @param ctx host handshake context
* @param ecdsa_pub host ECDSA public key (matching the device private key)
* @return E_OK in case of success, error code otherwise
*/
enum ecode
nanotls_host_init(struct nanotls_host_ctx *ctx,
const uint8_t ecdsa_pub[NANOTLS_ECDSA_PUBLIC_KEY_SIZE]);
/**
* Initialize the host handshake context with deferred public key lookup.
*
* This init function flavor assumes the public key is not known at the init
* time, so the pub_key_req_cb callback is called from
* nanotls_host_device_hello_rcvd() to look up and provide the public key using
* device_info provided in DeviceHello.
*
* @param ctx host handshake context
* @param pub_key_req_cb a callback to be called from
* nanotls_host_device_hello_rcvd() to provide lookup for the public key using
* device_info provided in DeviceHello.
* @return E_OK in case of success, error code otherwise
*/
enum ecode
nanotls_host_init_pubkey_cb(struct nanotls_host_ctx *ctx,
nanotls_host_pub_key_req_cb pub_key_req_cb);
enum ecode nanotls_host_do_hello(struct nanotls_host_ctx *ctx,
struct nanotls_host_hello *msg);
enum ecode nanotls_host_device_hello_rcvd(struct nanotls_host_ctx *ctx,
struct nanotls_device_hello *msg);
/* Get device info after DeviceHello has been processed */
enum ecode
nanotls_host_get_device_info(const struct nanotls_host_ctx *ctx,
struct nanotls_device_info *device_info);
enum ecode nanotls_host_do_finished(struct nanotls_host_ctx *ctx,
struct nanotls_host_finished *msg);
enum ecode
nanotls_host_derive_traffic_keys_instance(const struct nanotls_host_ctx *ctx,
struct nanotls_traffic_keys *keys,
size_t instance_idx);
enum ecode nanotls_host_derive_traffic_keys(const struct nanotls_host_ctx *ctx,
struct nanotls_traffic_keys *keys);
enum ecode nanotls_host_cleanup(struct nanotls_host_ctx *ctx);
#endif /* NANOTLS_HOST_H */

View file

@ -1,188 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef NANOTLS_PROTO_H
#define NANOTLS_PROTO_H
#include <linux/types.h>
#include <asm/string.h>
#include <nanotls-config.h>
#define NANOTLS_MAGIC 0x43b826f3
#define NANOTLS_VERSION 1
#define NANOTLS_HASH_SIZE NANOTLS_SHA256_HASH_SIZE
#define NANOTLS_SECRET_SIZE NANOTLS_HASH_SIZE
#define NANOTLS_KEY_SIZE (128 / 8)
#define NANOTLS_IV_SIZE 12
#define NANOTLS_AUTH_TAG_SIZE 16
#define NANOTLS_ECDSA_PUBLIC_KEY_SIZE 64
#define NANOTLS_ECDSA_PRIVATE_KEY_SIZE 32
#define NANOTLS_ECDH_PUBLIC_KEY_SIZE 64
#define NANOTLS_ECDH_PRIVATE_KEY_SIZE 32
#define NANOTLS_UUID_LEN 16
#define NANOTLS_MAX_DATA_LEN 0xFFFF
#define NANOTLS_LABEL(string) string, strlen(string)
#define NANOTLS_DEVICE_HS_TS_LABEL NANOTLS_LABEL("D HS TS")
#define NANOTLS_DEVICE_AP_TS_LABEL NANOTLS_LABEL("D AP TS")
#define NANOTLS_HOST_HS_TS_LABEL NANOTLS_LABEL("H HS TS")
#define NANOTLS_HOST_AP_TS_LABEL NANOTLS_LABEL("H AP TS")
#define NANOTLS_FINISHED_LABEL NANOTLS_LABEL("finished")
#define NANOTLS_DERIVED_LABEL NANOTLS_LABEL("derived")
#define NANOTLS_KEY_UPDATE_LABEL NANOTLS_LABEL("key update")
#define NANOTLS_KEY_INSTANCE_LABEL NANOTLS_LABEL("key instance")
#define NANOTLS_DEVICE_HS_SIG_LABEL NANOTLS_LABEL("D HS SIG")
#define NANOTLS_HOST_HS_SIG_LABEL NANOTLS_LABEL("H HS SIG")
#define NANOTLS_KEYING_IV_LABEL NANOTLS_LABEL("iv")
#define NANOTLS_KEYING_KEY_LABEL NANOTLS_LABEL("key")
enum nanotls_message_id {
NANOTLS_HOST_HELLO = 1,
NANOTLS_DEVICE_HELLO = 2,
NANOTLS_HOST_FINISHED = 3,
};
enum nanotls_signature_algorithm {
NANOTLS_ECDSA_SECP256R1_SHA256 = 0x0403,
};
enum nanotls_key_exchange_type {
NANOTLS_ECDHE_SECP256R1 = 0x0017,
};
enum nanotls_cipher_suite {
NANOTLS_AES_128_GCM_SHA256 = 0x1301,
};
struct nanotls_key_share {
uint8_t pubkey[NANOTLS_ECDH_PUBLIC_KEY_SIZE]; /* P-256 ECDH public key,
two points */
};
struct nanotls_message_hdr {
uint32_t magic;
uint16_t len;
uint8_t message_id;
uint8_t protocol_version;
};
struct nanotls_signature {
uint8_t sig[64]; /* P-256 ECDSA signature, two points */
};
struct nanotls_finished {
uint8_t verify_data[32];
};
struct auth_tag {
uint8_t tag[NANOTLS_AUTH_TAG_SIZE];
};
struct nanotls_host_hello {
struct nanotls_message_hdr hdr;
uint16_t sig_alg;
uint16_t key_exchange_type;
uint16_t cipher_suite;
uint16_t reserved;
uint8_t random[32];
struct nanotls_key_share host_key_share; /* ECDHE */
};
/* DeviceHello's submessage */
struct nanotls_device_info {
uint16_t chip_id;
uint16_t device_flags;
uint8_t reserved[12];
uint8_t uuid[NANOTLS_UUID_LEN];
};
struct nanotls_device_hello {
struct nanotls_message_hdr hdr;
uint32_t reserved;
uint8_t random[32];
struct nanotls_key_share device_key_share; /* ECDHE */
/* Encrypted portion */
struct {
struct nanotls_device_info device_info; /* aka Certificate */
struct nanotls_signature device_handshake_sig; /* aka TLS
CertificateVerify
*/
struct nanotls_finished device_finished;
} enc;
struct auth_tag auth_tag; /* Auth tag for the encrypted portion */
};
#define NANOTLS_DEVICE_HELLO_SIG_CUTOFF_POS \
offsetof(struct nanotls_device_hello, enc)
#define NANOTLS_DEVICE_HELLO_FINISHED_ENC_CUTOFF_POS \
(offsetof(struct nanotls_device_hello, enc.device_finished) - \
NANOTLS_DEVICE_HELLO_SIG_CUTOFF_POS)
struct nanotls_host_finished {
struct nanotls_message_hdr hdr;
uint32_t reserved;
/* Encrypted portion */
struct {
struct nanotls_signature reserved2;
struct nanotls_finished host_finished;
} enc;
struct auth_tag auth_tag; /* Auth tag for the encrypted portion */
};
#define NANOTLS_HOST_HELLO_FINISHED_CUTOFF_POS \
offsetof(struct nanotls_host_finished, enc.host_finished)
struct nanotls_data_add {
uint8_t version; /* NANOTLS_VERSION */
uint8_t reserved[5]; /* zeroes */
uint16_t len;
};
struct nanotls_traffic_keys {
/*
* Data secrets are saved for future possible key update.
* !! Should not be used directly for encryption/decryption.
* !! Keys should be derived from it using HDKF-Expand().
*/
uint8_t h2d_secret[NANOTLS_SECRET_SIZE];
uint8_t d2h_secret[NANOTLS_SECRET_SIZE];
/* These keys below should be used for message encryption/decryption */
uint8_t h2d_iv[NANOTLS_IV_SIZE];
uint8_t h2d_key[NANOTLS_KEY_SIZE];
uint8_t d2h_iv[NANOTLS_IV_SIZE];
uint8_t d2h_key[NANOTLS_KEY_SIZE];
};
#endif /* NANOTLS_PROTO_H */

View file

@ -1,52 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef NANOTLS_TYPES_H
#define NANOTLS_TYPES_H
#include <nanotls-config.h>
enum nanotls_role {
NANOTLS_ROLE_UNKNOWN = 0,
#ifdef NANOTLS_CONFIG_HARDENING
NANOTLS_ROLE_HOST = 0x1ea71e,
NANOTLS_ROLE_DEVICE = 0xdb25cc,
#else
NANOTLS_ROLE_HOST = 1,
NANOTLS_ROLE_DEVICE = 2,
#endif
};
enum ecode {
#ifdef NANOTLS_CONFIG_HARDENING
/* Error codes with big Hamming distance for better hardening */
/* Not trivial enough, but still could be generated with single RISC-V
instruction */
E_OK = -1446, /* 0xFFFFFA5A */
E_INVALID = 0x153E95A5, /* Invalid value or state */
#else
E_OK = 0,
E_INVALID = -1,
#endif
};
#endif /* NANOTLS_TYPES_H */

View file

@ -1,63 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef AES_HEADER
#define AES_HEADER
#include <linux/types.h>
#include <asm/string.h>
#define AES_CONFIG_128_BIT_ONLY
enum aes_op_mode {
AES_MODE_NONE = 0,
AES_MODE_ENCRYPT = 0x231d14,
AES_MODE_DECRYPT = 0x1676a1,
};
/******************************************************************************
* AES_CONTEXT : cipher context / holds inter-call data
******************************************************************************/
typedef struct {
int rounds; // keysize-based rounds count
uint32_t *rk; // pointer to current round key
uint32_t buf[68]; // key expansion buffer
} aes_context;
/******************************************************************************
* AES_SETKEY : called to expand the key for encryption or decryption
******************************************************************************/
int aes_setkey(aes_context *ctx, // pointer to context
const uint8_t *key, // AES input key
size_t keysize); // size in bytes (must be 16, 24, 32 for
// 128, 192 or 256-bit keys respectively)
// returns 0 for success
/******************************************************************************
* AES_CIPHER : called to encrypt or decrypt ONE 128-bit block of data
******************************************************************************/
int aes_cipher(aes_context *ctx, // pointer to context
const uint8_t input[16], // 128-bit block to en/decipher
uint8_t output[16]); // 128-bit output result block
// returns 0 for success
#endif /* AES_HEADER */

View file

@ -1,246 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef AES_TABLES_H
#define AES_TABLES_H
#include <linux/types.h>
#include <asm/string.h>
/* Forward substitution box (FSb) */
static const uint8_t FSb[256] = {
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26,
0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed,
0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec,
0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11,
0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
0xb0, 0x54, 0xbb, 0x16};
/* Forward key schedule assembly tables */
static const uint32_t FT0[256] = {
0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6, 0x0df2f2ff, 0xbd6b6bd6,
0xb16f6fde, 0x54c5c591, 0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56,
0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec, 0x45caca8f, 0x9d82821f,
0x40c9c989, 0x877d7dfa, 0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb,
0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45, 0xbf9c9c23, 0xf7a4a453,
0x967272e4, 0x5bc0c09b, 0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c,
0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83, 0x5c343468, 0xf4a5a551,
0x34e5e5d1, 0x08f1f1f9, 0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a,
0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d, 0x28181830, 0xa1969637,
0x0f05050a, 0xb59a9a2f, 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df,
0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea, 0x1b090912, 0x9e83831d,
0x742c2c58, 0x2e1a1a34, 0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b,
0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d, 0x7b292952, 0x3ee3e3dd,
0x712f2f5e, 0x97848413, 0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1,
0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6, 0xbe6a6ad4, 0x46cbcb8d,
0xd9bebe67, 0x4b393972, 0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85,
0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed, 0xc5434386, 0xd74d4d9a,
0x55333366, 0x94858511, 0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe,
0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b, 0xf35151a2, 0xfea3a35d,
0xc0404080, 0x8a8f8f05, 0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1,
0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142, 0x30101020, 0x1affffe5,
0x0ef3f3fd, 0x6dd2d2bf, 0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3,
0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e, 0x57c4c493, 0xf2a7a755,
0x827e7efc, 0x473d3d7a, 0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6,
0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3, 0x66222244, 0x7e2a2a54,
0xab90903b, 0x8388880b, 0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428,
0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad, 0x3be0e0db, 0x56323264,
0x4e3a3a74, 0x1e0a0a14, 0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8,
0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4, 0xa8919139, 0xa4959531,
0x37e4e4d3, 0x8b7979f2, 0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda,
0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949, 0xb46c6cd8, 0xfa5656ac,
0x07f4f4f3, 0x25eaeacf, 0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810,
0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c, 0x241c1c38, 0xf1a6a657,
0xc7b4b473, 0x51c6c697, 0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e,
0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f, 0x907070e0, 0x423e3e7c,
0xc4b5b571, 0xaa6666cc, 0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c,
0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969, 0x91868617, 0x58c1c199,
0x271d1d3a, 0xb99e9e27, 0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122,
0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433, 0xb69b9b2d, 0x221e1e3c,
0x92878715, 0x20e9e9c9, 0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5,
0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a, 0xdabfbf65, 0x31e6e6d7,
0xc6424284, 0xb86868d0, 0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e,
0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c,
};
static const uint32_t FT1[256] = {
0x6363c6a5, 0x7c7cf884, 0x7777ee99, 0x7b7bf68d, 0xf2f2ff0d, 0x6b6bd6bd,
0x6f6fdeb1, 0xc5c59154, 0x30306050, 0x01010203, 0x6767cea9, 0x2b2b567d,
0xfefee719, 0xd7d7b562, 0xabab4de6, 0x7676ec9a, 0xcaca8f45, 0x82821f9d,
0xc9c98940, 0x7d7dfa87, 0xfafaef15, 0x5959b2eb, 0x47478ec9, 0xf0f0fb0b,
0xadad41ec, 0xd4d4b367, 0xa2a25ffd, 0xafaf45ea, 0x9c9c23bf, 0xa4a453f7,
0x7272e496, 0xc0c09b5b, 0xb7b775c2, 0xfdfde11c, 0x93933dae, 0x26264c6a,
0x36366c5a, 0x3f3f7e41, 0xf7f7f502, 0xcccc834f, 0x3434685c, 0xa5a551f4,
0xe5e5d134, 0xf1f1f908, 0x7171e293, 0xd8d8ab73, 0x31316253, 0x15152a3f,
0x0404080c, 0xc7c79552, 0x23234665, 0xc3c39d5e, 0x18183028, 0x969637a1,
0x05050a0f, 0x9a9a2fb5, 0x07070e09, 0x12122436, 0x80801b9b, 0xe2e2df3d,
0xebebcd26, 0x27274e69, 0xb2b27fcd, 0x7575ea9f, 0x0909121b, 0x83831d9e,
0x2c2c5874, 0x1a1a342e, 0x1b1b362d, 0x6e6edcb2, 0x5a5ab4ee, 0xa0a05bfb,
0x5252a4f6, 0x3b3b764d, 0xd6d6b761, 0xb3b37dce, 0x2929527b, 0xe3e3dd3e,
0x2f2f5e71, 0x84841397, 0x5353a6f5, 0xd1d1b968, 0x00000000, 0xededc12c,
0x20204060, 0xfcfce31f, 0xb1b179c8, 0x5b5bb6ed, 0x6a6ad4be, 0xcbcb8d46,
0xbebe67d9, 0x3939724b, 0x4a4a94de, 0x4c4c98d4, 0x5858b0e8, 0xcfcf854a,
0xd0d0bb6b, 0xefefc52a, 0xaaaa4fe5, 0xfbfbed16, 0x434386c5, 0x4d4d9ad7,
0x33336655, 0x85851194, 0x45458acf, 0xf9f9e910, 0x02020406, 0x7f7ffe81,
0x5050a0f0, 0x3c3c7844, 0x9f9f25ba, 0xa8a84be3, 0x5151a2f3, 0xa3a35dfe,
0x404080c0, 0x8f8f058a, 0x92923fad, 0x9d9d21bc, 0x38387048, 0xf5f5f104,
0xbcbc63df, 0xb6b677c1, 0xdadaaf75, 0x21214263, 0x10102030, 0xffffe51a,
0xf3f3fd0e, 0xd2d2bf6d, 0xcdcd814c, 0x0c0c1814, 0x13132635, 0xececc32f,
0x5f5fbee1, 0x979735a2, 0x444488cc, 0x17172e39, 0xc4c49357, 0xa7a755f2,
0x7e7efc82, 0x3d3d7a47, 0x6464c8ac, 0x5d5dbae7, 0x1919322b, 0x7373e695,
0x6060c0a0, 0x81811998, 0x4f4f9ed1, 0xdcdca37f, 0x22224466, 0x2a2a547e,
0x90903bab, 0x88880b83, 0x46468cca, 0xeeeec729, 0xb8b86bd3, 0x1414283c,
0xdedea779, 0x5e5ebce2, 0x0b0b161d, 0xdbdbad76, 0xe0e0db3b, 0x32326456,
0x3a3a744e, 0x0a0a141e, 0x494992db, 0x06060c0a, 0x2424486c, 0x5c5cb8e4,
0xc2c29f5d, 0xd3d3bd6e, 0xacac43ef, 0x6262c4a6, 0x919139a8, 0x959531a4,
0xe4e4d337, 0x7979f28b, 0xe7e7d532, 0xc8c88b43, 0x37376e59, 0x6d6ddab7,
0x8d8d018c, 0xd5d5b164, 0x4e4e9cd2, 0xa9a949e0, 0x6c6cd8b4, 0x5656acfa,
0xf4f4f307, 0xeaeacf25, 0x6565caaf, 0x7a7af48e, 0xaeae47e9, 0x08081018,
0xbaba6fd5, 0x7878f088, 0x25254a6f, 0x2e2e5c72, 0x1c1c3824, 0xa6a657f1,
0xb4b473c7, 0xc6c69751, 0xe8e8cb23, 0xdddda17c, 0x7474e89c, 0x1f1f3e21,
0x4b4b96dd, 0xbdbd61dc, 0x8b8b0d86, 0x8a8a0f85, 0x7070e090, 0x3e3e7c42,
0xb5b571c4, 0x6666ccaa, 0x484890d8, 0x03030605, 0xf6f6f701, 0x0e0e1c12,
0x6161c2a3, 0x35356a5f, 0x5757aef9, 0xb9b969d0, 0x86861791, 0xc1c19958,
0x1d1d3a27, 0x9e9e27b9, 0xe1e1d938, 0xf8f8eb13, 0x98982bb3, 0x11112233,
0x6969d2bb, 0xd9d9a970, 0x8e8e0789, 0x949433a7, 0x9b9b2db6, 0x1e1e3c22,
0x87871592, 0xe9e9c920, 0xcece8749, 0x5555aaff, 0x28285078, 0xdfdfa57a,
0x8c8c038f, 0xa1a159f8, 0x89890980, 0x0d0d1a17, 0xbfbf65da, 0xe6e6d731,
0x424284c6, 0x6868d0b8, 0x414182c3, 0x999929b0, 0x2d2d5a77, 0x0f0f1e11,
0xb0b07bcb, 0x5454a8fc, 0xbbbb6dd6, 0x16162c3a,
};
static const uint32_t FT2[256] = {
0x63c6a563, 0x7cf8847c, 0x77ee9977, 0x7bf68d7b, 0xf2ff0df2, 0x6bd6bd6b,
0x6fdeb16f, 0xc59154c5, 0x30605030, 0x01020301, 0x67cea967, 0x2b567d2b,
0xfee719fe, 0xd7b562d7, 0xab4de6ab, 0x76ec9a76, 0xca8f45ca, 0x821f9d82,
0xc98940c9, 0x7dfa877d, 0xfaef15fa, 0x59b2eb59, 0x478ec947, 0xf0fb0bf0,
0xad41ecad, 0xd4b367d4, 0xa25ffda2, 0xaf45eaaf, 0x9c23bf9c, 0xa453f7a4,
0x72e49672, 0xc09b5bc0, 0xb775c2b7, 0xfde11cfd, 0x933dae93, 0x264c6a26,
0x366c5a36, 0x3f7e413f, 0xf7f502f7, 0xcc834fcc, 0x34685c34, 0xa551f4a5,
0xe5d134e5, 0xf1f908f1, 0x71e29371, 0xd8ab73d8, 0x31625331, 0x152a3f15,
0x04080c04, 0xc79552c7, 0x23466523, 0xc39d5ec3, 0x18302818, 0x9637a196,
0x050a0f05, 0x9a2fb59a, 0x070e0907, 0x12243612, 0x801b9b80, 0xe2df3de2,
0xebcd26eb, 0x274e6927, 0xb27fcdb2, 0x75ea9f75, 0x09121b09, 0x831d9e83,
0x2c58742c, 0x1a342e1a, 0x1b362d1b, 0x6edcb26e, 0x5ab4ee5a, 0xa05bfba0,
0x52a4f652, 0x3b764d3b, 0xd6b761d6, 0xb37dceb3, 0x29527b29, 0xe3dd3ee3,
0x2f5e712f, 0x84139784, 0x53a6f553, 0xd1b968d1, 0x00000000, 0xedc12ced,
0x20406020, 0xfce31ffc, 0xb179c8b1, 0x5bb6ed5b, 0x6ad4be6a, 0xcb8d46cb,
0xbe67d9be, 0x39724b39, 0x4a94de4a, 0x4c98d44c, 0x58b0e858, 0xcf854acf,
0xd0bb6bd0, 0xefc52aef, 0xaa4fe5aa, 0xfbed16fb, 0x4386c543, 0x4d9ad74d,
0x33665533, 0x85119485, 0x458acf45, 0xf9e910f9, 0x02040602, 0x7ffe817f,
0x50a0f050, 0x3c78443c, 0x9f25ba9f, 0xa84be3a8, 0x51a2f351, 0xa35dfea3,
0x4080c040, 0x8f058a8f, 0x923fad92, 0x9d21bc9d, 0x38704838, 0xf5f104f5,
0xbc63dfbc, 0xb677c1b6, 0xdaaf75da, 0x21426321, 0x10203010, 0xffe51aff,
0xf3fd0ef3, 0xd2bf6dd2, 0xcd814ccd, 0x0c18140c, 0x13263513, 0xecc32fec,
0x5fbee15f, 0x9735a297, 0x4488cc44, 0x172e3917, 0xc49357c4, 0xa755f2a7,
0x7efc827e, 0x3d7a473d, 0x64c8ac64, 0x5dbae75d, 0x19322b19, 0x73e69573,
0x60c0a060, 0x81199881, 0x4f9ed14f, 0xdca37fdc, 0x22446622, 0x2a547e2a,
0x903bab90, 0x880b8388, 0x468cca46, 0xeec729ee, 0xb86bd3b8, 0x14283c14,
0xdea779de, 0x5ebce25e, 0x0b161d0b, 0xdbad76db, 0xe0db3be0, 0x32645632,
0x3a744e3a, 0x0a141e0a, 0x4992db49, 0x060c0a06, 0x24486c24, 0x5cb8e45c,
0xc29f5dc2, 0xd3bd6ed3, 0xac43efac, 0x62c4a662, 0x9139a891, 0x9531a495,
0xe4d337e4, 0x79f28b79, 0xe7d532e7, 0xc88b43c8, 0x376e5937, 0x6ddab76d,
0x8d018c8d, 0xd5b164d5, 0x4e9cd24e, 0xa949e0a9, 0x6cd8b46c, 0x56acfa56,
0xf4f307f4, 0xeacf25ea, 0x65caaf65, 0x7af48e7a, 0xae47e9ae, 0x08101808,
0xba6fd5ba, 0x78f08878, 0x254a6f25, 0x2e5c722e, 0x1c38241c, 0xa657f1a6,
0xb473c7b4, 0xc69751c6, 0xe8cb23e8, 0xdda17cdd, 0x74e89c74, 0x1f3e211f,
0x4b96dd4b, 0xbd61dcbd, 0x8b0d868b, 0x8a0f858a, 0x70e09070, 0x3e7c423e,
0xb571c4b5, 0x66ccaa66, 0x4890d848, 0x03060503, 0xf6f701f6, 0x0e1c120e,
0x61c2a361, 0x356a5f35, 0x57aef957, 0xb969d0b9, 0x86179186, 0xc19958c1,
0x1d3a271d, 0x9e27b99e, 0xe1d938e1, 0xf8eb13f8, 0x982bb398, 0x11223311,
0x69d2bb69, 0xd9a970d9, 0x8e07898e, 0x9433a794, 0x9b2db69b, 0x1e3c221e,
0x87159287, 0xe9c920e9, 0xce8749ce, 0x55aaff55, 0x28507828, 0xdfa57adf,
0x8c038f8c, 0xa159f8a1, 0x89098089, 0x0d1a170d, 0xbf65dabf, 0xe6d731e6,
0x4284c642, 0x68d0b868, 0x4182c341, 0x9929b099, 0x2d5a772d, 0x0f1e110f,
0xb07bcbb0, 0x54a8fc54, 0xbb6dd6bb, 0x162c3a16,
};
static const uint32_t FT3[256] = {
0xc6a56363, 0xf8847c7c, 0xee997777, 0xf68d7b7b, 0xff0df2f2, 0xd6bd6b6b,
0xdeb16f6f, 0x9154c5c5, 0x60503030, 0x02030101, 0xcea96767, 0x567d2b2b,
0xe719fefe, 0xb562d7d7, 0x4de6abab, 0xec9a7676, 0x8f45caca, 0x1f9d8282,
0x8940c9c9, 0xfa877d7d, 0xef15fafa, 0xb2eb5959, 0x8ec94747, 0xfb0bf0f0,
0x41ecadad, 0xb367d4d4, 0x5ffda2a2, 0x45eaafaf, 0x23bf9c9c, 0x53f7a4a4,
0xe4967272, 0x9b5bc0c0, 0x75c2b7b7, 0xe11cfdfd, 0x3dae9393, 0x4c6a2626,
0x6c5a3636, 0x7e413f3f, 0xf502f7f7, 0x834fcccc, 0x685c3434, 0x51f4a5a5,
0xd134e5e5, 0xf908f1f1, 0xe2937171, 0xab73d8d8, 0x62533131, 0x2a3f1515,
0x080c0404, 0x9552c7c7, 0x46652323, 0x9d5ec3c3, 0x30281818, 0x37a19696,
0x0a0f0505, 0x2fb59a9a, 0x0e090707, 0x24361212, 0x1b9b8080, 0xdf3de2e2,
0xcd26ebeb, 0x4e692727, 0x7fcdb2b2, 0xea9f7575, 0x121b0909, 0x1d9e8383,
0x58742c2c, 0x342e1a1a, 0x362d1b1b, 0xdcb26e6e, 0xb4ee5a5a, 0x5bfba0a0,
0xa4f65252, 0x764d3b3b, 0xb761d6d6, 0x7dceb3b3, 0x527b2929, 0xdd3ee3e3,
0x5e712f2f, 0x13978484, 0xa6f55353, 0xb968d1d1, 0x00000000, 0xc12ceded,
0x40602020, 0xe31ffcfc, 0x79c8b1b1, 0xb6ed5b5b, 0xd4be6a6a, 0x8d46cbcb,
0x67d9bebe, 0x724b3939, 0x94de4a4a, 0x98d44c4c, 0xb0e85858, 0x854acfcf,
0xbb6bd0d0, 0xc52aefef, 0x4fe5aaaa, 0xed16fbfb, 0x86c54343, 0x9ad74d4d,
0x66553333, 0x11948585, 0x8acf4545, 0xe910f9f9, 0x04060202, 0xfe817f7f,
0xa0f05050, 0x78443c3c, 0x25ba9f9f, 0x4be3a8a8, 0xa2f35151, 0x5dfea3a3,
0x80c04040, 0x058a8f8f, 0x3fad9292, 0x21bc9d9d, 0x70483838, 0xf104f5f5,
0x63dfbcbc, 0x77c1b6b6, 0xaf75dada, 0x42632121, 0x20301010, 0xe51affff,
0xfd0ef3f3, 0xbf6dd2d2, 0x814ccdcd, 0x18140c0c, 0x26351313, 0xc32fecec,
0xbee15f5f, 0x35a29797, 0x88cc4444, 0x2e391717, 0x9357c4c4, 0x55f2a7a7,
0xfc827e7e, 0x7a473d3d, 0xc8ac6464, 0xbae75d5d, 0x322b1919, 0xe6957373,
0xc0a06060, 0x19988181, 0x9ed14f4f, 0xa37fdcdc, 0x44662222, 0x547e2a2a,
0x3bab9090, 0x0b838888, 0x8cca4646, 0xc729eeee, 0x6bd3b8b8, 0x283c1414,
0xa779dede, 0xbce25e5e, 0x161d0b0b, 0xad76dbdb, 0xdb3be0e0, 0x64563232,
0x744e3a3a, 0x141e0a0a, 0x92db4949, 0x0c0a0606, 0x486c2424, 0xb8e45c5c,
0x9f5dc2c2, 0xbd6ed3d3, 0x43efacac, 0xc4a66262, 0x39a89191, 0x31a49595,
0xd337e4e4, 0xf28b7979, 0xd532e7e7, 0x8b43c8c8, 0x6e593737, 0xdab76d6d,
0x018c8d8d, 0xb164d5d5, 0x9cd24e4e, 0x49e0a9a9, 0xd8b46c6c, 0xacfa5656,
0xf307f4f4, 0xcf25eaea, 0xcaaf6565, 0xf48e7a7a, 0x47e9aeae, 0x10180808,
0x6fd5baba, 0xf0887878, 0x4a6f2525, 0x5c722e2e, 0x38241c1c, 0x57f1a6a6,
0x73c7b4b4, 0x9751c6c6, 0xcb23e8e8, 0xa17cdddd, 0xe89c7474, 0x3e211f1f,
0x96dd4b4b, 0x61dcbdbd, 0x0d868b8b, 0x0f858a8a, 0xe0907070, 0x7c423e3e,
0x71c4b5b5, 0xccaa6666, 0x90d84848, 0x06050303, 0xf701f6f6, 0x1c120e0e,
0xc2a36161, 0x6a5f3535, 0xaef95757, 0x69d0b9b9, 0x17918686, 0x9958c1c1,
0x3a271d1d, 0x27b99e9e, 0xd938e1e1, 0xeb13f8f8, 0x2bb39898, 0x22331111,
0xd2bb6969, 0xa970d9d9, 0x07898e8e, 0x33a79494, 0x2db69b9b, 0x3c221e1e,
0x15928787, 0xc920e9e9, 0x8749cece, 0xaaff5555, 0x50782828, 0xa57adfdf,
0x038f8c8c, 0x59f8a1a1, 0x09808989, 0x1a170d0d, 0x65dabfbf, 0xd731e6e6,
0x84c64242, 0xd0b86868, 0x82c34141, 0x29b09999, 0x5a772d2d, 0x1e110f0f,
0x7bcbb0b0, 0xa8fc5454, 0x6dd6bbbb, 0x2c3a1616,
};
/* AES round constants */
static const uint8_t RCON[10] = {0x01, 0x02, 0x04, 0x08, 0x10,
0x20, 0x40, 0x80, 0x1b, 0x36};
#endif /* AES_TABLES_H */

View file

@ -1,172 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef GCM_HEADER
#define GCM_HEADER
#define GCM_AUTH_FAILURE 0x55555555 // authentication failure
#include "aes.h" // gcm_context includes aes_context
#include <linux/types.h>
#include <asm/string.h>
#define AES_GCM_FIXED_12_BYTE_IV
/******************************************************************************
* GCM_CONTEXT : GCM context / holds keytables, instance data, and AES ctx
******************************************************************************/
typedef struct {
enum aes_op_mode mode; // cipher direction: encrypt/decrypt
uint64_t len; // cipher data length processed so far
uint64_t add_len; // total add data length
uint64_t HL[16]; // precalculated lo-half HTable
uint64_t HH[16]; // precalculated hi-half HTable
uint8_t base_ectr[16]; // first counter-mode cipher output for tag
uint8_t y[16]; // the current cipher-input IV|Counter value
uint8_t buf[16]; // buf working value
aes_context aes_ctx; // cipher context used
} gcm_context;
/******************************************************************************
* GCM_SETKEY : sets the GCM (and AES) keying material for use
******************************************************************************/
int gcm_setkey(gcm_context *ctx, // caller-provided context ptr
const uint8_t *key, // pointer to cipher key
const size_t keysize // size in bytes (must be 16, 24, 32 for
// 128, 192 or 256-bit keys respectively)
); // returns 0 for success
/******************************************************************************
*
* GCM_CRYPT_AND_TAG
*
* This either encrypts or decrypts the user-provided data and, either
* way, generates an authentication tag of the requested length. It must be
* called with a GCM context whose key has already been set with GCM_SETKEY.
*
* The user would typically call this explicitly to ENCRYPT a buffer of data
* and optional associated data, and produce its an authentication tag.
*
* To reverse the process the user would typically call the companion
* GCM_AUTH_DECRYPT function to decrypt data and verify a user-provided
* authentication tag. The GCM_AUTH_DECRYPT function calls this function
* to perform its decryption and tag generation, which it then compares.
*
******************************************************************************/
int gcm_crypt_and_tag(gcm_context *ctx, // gcm context with key already setup
enum aes_op_mode mode, // cipher direction: ENCRYPT (1) or
// DECRYPT (0)
const uint8_t *iv, // pointer to the 12-byte
// initialization vector
size_t iv_len, // byte length if the IV. should always be
// 12
const uint8_t *add, // pointer to the non-ciphered
// additional data
size_t add_len, // byte length of the additional AEAD data
const uint8_t *input, // pointer to the cipher data source
uint8_t *output, // pointer to the cipher data destination
size_t length, // byte length of the cipher data
uint8_t *tag, // pointer to the tag to be generated
size_t tag_len); // byte length of the tag to be generated
/******************************************************************************
*
* GCM_AUTH_DECRYPT
*
* This DECRYPTS a user-provided data buffer with optional associated data.
* It then verifies a user-supplied authentication tag against the tag just
* re-created during decryption to verify that the data has not been altered.
*
* This function calls GCM_CRYPT_AND_TAG (above) to perform the decryption
* and authentication tag generation.
*
******************************************************************************/
int gcm_auth_decrypt(gcm_context *ctx, // gcm context with key already setup
const uint8_t *iv, // pointer to the 12-byte initialization
// vector
size_t iv_len, // byte length if the IV. should always be
// 12
const uint8_t *add, // pointer to the non-ciphered
// additional data
size_t add_len, // byte length of the additional AEAD data
const uint8_t *input, // pointer to the cipher data source
uint8_t *output, // pointer to the cipher data destination
size_t length, // byte length of the cipher data
const uint8_t *tag, // pointer to the tag to be
// authenticated
size_t tag_len); // byte length of the tag <= 16
/******************************************************************************
*
* GCM_START
*
* Given a user-provided GCM context, this initializes it, sets the encryption
* mode, and preprocesses the initialization vector and additional AEAD data.
*
******************************************************************************/
int gcm_start(gcm_context *ctx, // pointer to user-provided GCM context
enum aes_op_mode mode, // ENCRYPT (1) or DECRYPT (0)
const uint8_t *iv, // pointer to initialization vector
size_t iv_len, // IV length in bytes (should == 12)
const uint8_t *add, // pointer to additional AEAD data (NULL if
// none)
size_t add_len); // length of additional AEAD data (bytes)
/******************************************************************************
*
* GCM_UPDATE
*
* This is called once or more to process bulk plaintext or ciphertext data.
* We give this some number of bytes of input and it returns the same number
* of output bytes. If called multiple times (which is fine) all but the final
* invocation MUST be called with length mod 16 == 0. (Only the final call can
* have a partial block length of < 128 bits.)
*
******************************************************************************/
int gcm_update(gcm_context *ctx, // pointer to user-provided GCM context
size_t length, // length, in bytes, of data to process
const uint8_t *input, // pointer to source data
uint8_t *output); // pointer to destination data
/******************************************************************************
*
* GCM_FINISH
*
* This is called once after all calls to GCM_UPDATE to finalize the GCM.
* It performs the final GHASH to produce the resulting authentication TAG.
*
******************************************************************************/
int gcm_finish(gcm_context *ctx, // pointer to user-provided GCM context
uint8_t *tag, // ptr to tag buffer - NULL if tag_len = 0
size_t tag_len); // length, in bytes, of the tag-receiving buf
/******************************************************************************
*
* GCM_ZERO_CTX
*
* The GCM context contains both the GCM context and the AES context.
* This includes keying and key-related material which is security-
* sensitive, so it MUST be zeroed after use. This function does that.
*
******************************************************************************/
void gcm_zero_ctx(gcm_context *ctx);
#endif /* GCM_HEADER */

View file

@ -1,294 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#include "aes.h"
/*
* The following static local tables must be filled-in before the first use of
* the GCM or AES ciphers. They are used for the AES key expansion/scheduling
* and once built are read-only and thread safe.
*
* NOTE: GCM Encryption/Decryption does NOT REQUIRE AES decryption. Since
* GCM uses AES in counter-mode, where the AES cipher output is XORed with
* the GCM input, we ONLY NEED AES encryption.
*/
#include <aes_tables.h>
enum aes_rounds {
AES_128_ROUNDS = 10,
AES_192_ROUNDS = 12,
AES_256_ROUNDS = 14,
};
/*
* Platform Endianness Neutralizing Load and Store Macro definitions
* AES wants platform-neutral Little Endian (LE) byte ordering
*/
#define GET_UINT32_LE(n, b, i) \
{ \
(n) = ((uint32_t)(b)[(i)]) | ((uint32_t)(b)[(i) + 1] << 8) | \
((uint32_t)(b)[(i) + 2] << 16) | \
((uint32_t)(b)[(i) + 3] << 24); \
}
#define PUT_UINT32_LE(n, b, i) \
{ \
(b)[(i)] = (uint8_t)((n)); \
(b)[(i) + 1] = (uint8_t)((n) >> 8); \
(b)[(i) + 2] = (uint8_t)((n) >> 16); \
(b)[(i) + 3] = (uint8_t)((n) >> 24); \
}
/*
* AES forward and reverse encryption round processing macros
*/
#define AES_FROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3) \
{ \
X0 = *RK++ ^ FT0[(Y0)&0xFF] ^ FT1[(Y1 >> 8) & 0xFF] ^ \
FT2[(Y2 >> 16) & 0xFF] ^ FT3[(Y3 >> 24) & 0xFF]; \
\
X1 = *RK++ ^ FT0[(Y1)&0xFF] ^ FT1[(Y2 >> 8) & 0xFF] ^ \
FT2[(Y3 >> 16) & 0xFF] ^ FT3[(Y0 >> 24) & 0xFF]; \
\
X2 = *RK++ ^ FT0[(Y2)&0xFF] ^ FT1[(Y3 >> 8) & 0xFF] ^ \
FT2[(Y0 >> 16) & 0xFF] ^ FT3[(Y1 >> 24) & 0xFF]; \
\
X3 = *RK++ ^ FT0[(Y3)&0xFF] ^ FT1[(Y0 >> 8) & 0xFF] ^ \
FT2[(Y1 >> 16) & 0xFF] ^ FT3[(Y2 >> 24) & 0xFF]; \
}
#define AES_RROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3) \
{ \
X0 = *RK++ ^ RT0[(Y0)&0xFF] ^ RT1[(Y3 >> 8) & 0xFF] ^ \
RT2[(Y2 >> 16) & 0xFF] ^ RT3[(Y1 >> 24) & 0xFF]; \
\
X1 = *RK++ ^ RT0[(Y1)&0xFF] ^ RT1[(Y0 >> 8) & 0xFF] ^ \
RT2[(Y3 >> 16) & 0xFF] ^ RT3[(Y2 >> 24) & 0xFF]; \
\
X2 = *RK++ ^ RT0[(Y2)&0xFF] ^ RT1[(Y1 >> 8) & 0xFF] ^ \
RT2[(Y0 >> 16) & 0xFF] ^ RT3[(Y3 >> 24) & 0xFF]; \
\
X3 = *RK++ ^ RT0[(Y3)&0xFF] ^ RT1[(Y2 >> 8) & 0xFF] ^ \
RT2[(Y1 >> 16) & 0xFF] ^ RT3[(Y0 >> 24) & 0xFF]; \
}
/*
* These macros improve the readability of the key
* generation initialization code by collapsing
* repetitive common operations into logical pieces.
*/
#define ROTL8(x) ((x << 8) & 0xFFFFFFFF) | (x >> 24)
#define XTIME(x) ((x << 1) ^ ((x & 0x80) ? 0x1B : 0x00))
#define MUL(x, y) ((x && y) ? pow[(log[x] + log[y]) % 255] : 0)
#define MIX(x, y) \
{ \
y = ((y << 1) | (y >> 7)) & 0xFF; \
x ^= y; \
}
#define CPY128 \
{ \
*RK++ = *SK++; \
*RK++ = *SK++; \
*RK++ = *SK++; \
*RK++ = *SK++; \
}
/******************************************************************************
*
* AES_SET_ENCRYPTION_KEY
*
* This is called by 'aes_setkey' when we're establishing a key for
* subsequent encryption. We give it a pointer to the encryption
* context, a pointer to the key, and the key's length in bytes.
* Valid lengths are: 16, 24 or 32 bytes (128, 192, 256 bits).
*
******************************************************************************/
int aes_set_encryption_key(aes_context *ctx, const uint8_t *key, size_t keysize)
{
size_t i; // general purpose iteration local
uint32_t *RK = ctx->rk; // initialize our RoundKey buffer pointer
for (i = 0; i < (keysize >> 2); i++) {
GET_UINT32_LE(RK[i], key, i << 2);
}
switch (ctx->rounds) {
case 10:
for (i = 0; i < 10; i++, RK += 4) {
RK[4] = RK[0] ^ (uint32_t)RCON[i] ^
((uint32_t)FSb[(RK[3] >> 8) & 0xFF]) ^
((uint32_t)FSb[(RK[3] >> 16) & 0xFF] << 8) ^
((uint32_t)FSb[(RK[3] >> 24) & 0xFF] << 16) ^
((uint32_t)FSb[(RK[3]) & 0xFF] << 24);
RK[5] = RK[1] ^ RK[4];
RK[6] = RK[2] ^ RK[5];
RK[7] = RK[3] ^ RK[6];
}
break;
#ifndef AES_CONFIG_128_BIT_ONLY
case 12:
for (i = 0; i < 8; i++, RK += 6) {
RK[6] = RK[0] ^ (uint32_t)RCON[i] ^
((uint32_t)FSb[(RK[5] >> 8) & 0xFF]) ^
((uint32_t)FSb[(RK[5] >> 16) & 0xFF] << 8) ^
((uint32_t)FSb[(RK[5] >> 24) & 0xFF] << 16) ^
((uint32_t)FSb[(RK[5]) & 0xFF] << 24);
RK[7] = RK[1] ^ RK[6];
RK[8] = RK[2] ^ RK[7];
RK[9] = RK[3] ^ RK[8];
RK[10] = RK[4] ^ RK[9];
RK[11] = RK[5] ^ RK[10];
}
break;
case 14:
for (i = 0; i < 7; i++, RK += 8) {
RK[8] = RK[0] ^ (uint32_t)RCON[i] ^
((uint32_t)FSb[(RK[7] >> 8) & 0xFF]) ^
((uint32_t)FSb[(RK[7] >> 16) & 0xFF] << 8) ^
((uint32_t)FSb[(RK[7] >> 24) & 0xFF] << 16) ^
((uint32_t)FSb[(RK[7]) & 0xFF] << 24);
RK[9] = RK[1] ^ RK[8];
RK[10] = RK[2] ^ RK[9];
RK[11] = RK[3] ^ RK[10];
RK[12] = RK[4] ^ ((uint32_t)FSb[(RK[11]) & 0xFF]) ^
((uint32_t)FSb[(RK[11] >> 8) & 0xFF] << 8) ^
((uint32_t)FSb[(RK[11] >> 16) & 0xFF] << 16) ^
((uint32_t)FSb[(RK[11] >> 24) & 0xFF] << 24);
RK[13] = RK[5] ^ RK[12];
RK[14] = RK[6] ^ RK[13];
RK[15] = RK[7] ^ RK[14];
}
break;
#endif
default:
return -1;
}
return (0);
}
/******************************************************************************
*
* AES_SETKEY
*
* Invoked to establish the key schedule for subsequent encryption/decryption
*
******************************************************************************/
int aes_setkey(aes_context *ctx, // AES context provided by our caller
const uint8_t *key, // pointer to the key
size_t keysize) // key length in bytes
{
ctx->rk = ctx->buf; // initialize our round key pointer
switch (keysize) // set the rounds count based upon the keysize
{
case 16:
ctx->rounds = AES_128_ROUNDS;
break; // 16-byte, 128-bit key
#ifndef AES_CONFIG_128_BIT_ONLY
case 24:
ctx->rounds = AES_192_ROUNDS;
break; // 24-byte, 192-bit key
case 32:
ctx->rounds = AES_256_ROUNDS;
break; // 32-byte, 256-bit key
#endif
default:
return (-1);
}
return (aes_set_encryption_key(ctx, key, keysize));
}
/******************************************************************************
*
* AES_CIPHER
*
* Perform AES encryption and decryption.
* The AES context will have been setup with the encryption mode
* and all keying information appropriate for the task.
*
******************************************************************************/
int aes_cipher(aes_context *ctx, const uint8_t input[16], uint8_t output[16])
{
uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; // general purpose locals
size_t rounds_loop = (ctx->rounds >> 1) - 1;
int i;
#ifdef AES_CONFIG_128_BIT_ONLY
rounds_loop = (AES_128_ROUNDS >> 1) - 1;
#endif
RK = ctx->rk;
GET_UINT32_LE(X0, input, 0);
X0 ^= *RK++; // load our 128-bit
GET_UINT32_LE(X1, input, 4);
X1 ^= *RK++; // input buffer in a storage
GET_UINT32_LE(X2, input, 8);
X2 ^= *RK++; // memory endian-neutral way
GET_UINT32_LE(X3, input, 12);
X3 ^= *RK++;
for (i = (ctx->rounds >> 1) - 1; i > 0; i--) {
AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
AES_FROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3);
}
AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
X0 = *RK++ ^ ((uint32_t)FSb[(Y0)&0xFF]) ^
((uint32_t)FSb[(Y1 >> 8) & 0xFF] << 8) ^
((uint32_t)FSb[(Y2 >> 16) & 0xFF] << 16) ^
((uint32_t)FSb[(Y3 >> 24) & 0xFF] << 24);
X1 = *RK++ ^ ((uint32_t)FSb[(Y1)&0xFF]) ^
((uint32_t)FSb[(Y2 >> 8) & 0xFF] << 8) ^
((uint32_t)FSb[(Y3 >> 16) & 0xFF] << 16) ^
((uint32_t)FSb[(Y0 >> 24) & 0xFF] << 24);
X2 = *RK++ ^ ((uint32_t)FSb[(Y2)&0xFF]) ^
((uint32_t)FSb[(Y3 >> 8) & 0xFF] << 8) ^
((uint32_t)FSb[(Y0 >> 16) & 0xFF] << 16) ^
((uint32_t)FSb[(Y1 >> 24) & 0xFF] << 24);
X3 = *RK++ ^ ((uint32_t)FSb[(Y3)&0xFF]) ^
((uint32_t)FSb[(Y0 >> 8) & 0xFF] << 8) ^
((uint32_t)FSb[(Y1 >> 16) & 0xFF] << 16) ^
((uint32_t)FSb[(Y2 >> 24) & 0xFF] << 24);
PUT_UINT32_LE(X0, output, 0);
PUT_UINT32_LE(X1, output, 4);
PUT_UINT32_LE(X2, output, 8);
PUT_UINT32_LE(X3, output, 12);
return (0);
}
/* end of aes.c */

View file

@ -1,522 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#include "gcm.h"
#include "aes.h"
#include <uassert.h>
/******************************************************************************
* ==== IMPLEMENTATION WARNING ====
*
* This code was developed for use within SQRL's fixed environmnent. Thus, it
* is somewhat less "general purpose" than it would be if it were designed as
* a general purpose AES-GCM library. Specifically, it bothers with almost NO
* error checking on parameter limits, buffer bounds, etc. It assumes that it
* is being invoked by its author or by someone who understands the values it
* expects to receive. Its behavior will be undefined otherwise.
*
* All functions that might fail are defined to return 'ints' to indicate a
* problem. Most do not do so now. But this allows for error propagation out
* of internal functions if robust error checking should ever be desired.
*
******************************************************************************/
/* Calculating the "GHASH"
*
* There are many ways of calculating the so-called GHASH in software, each with
* a traditional size vs performance tradeoff. The GHASH (Galois field hash) is
* an intriguing construction which takes two 128-bit strings (also the cipher's
* block size and the fundamental operation size for the system) and hashes them
* into a third 128-bit result.
*
* Many implementation solutions have been worked out that use large precomputed
* table lookups in place of more time consuming bit fiddling, and this approach
* can be scaled easily upward or downward as needed to change the time/space
* tradeoff. It's been studied extensively and there's a solid body of theory
* and practice. For example, without using any lookup tables an implementation
* might obtain 119 cycles per byte throughput, whereas using a simple, though
* large, key-specific 64 kbyte 8-bit lookup table the performance jumps to 13
* cycles per byte.
*
* And Intel's processors have, since 2010, included an instruction which does
* the entire 128x128->128 bit job in just several 64x64->128 bit pieces.
*
* Since SQRL is interactive, and only processing a few 128-bit blocks, I've
* settled upon a relatively slower but appealing small-table compromise which
* folds a bunch of not only time consuming but also bit twiddling into a simple
* 16-entry table which is attributed to Victor Shoup's 1996 work while at
* Bellcore: "On Fast and Provably Secure MessageAuthentication Based on
* Universal Hashing." See: http://www.shoup.net/papers/macs.pdf
* See, also section 4.1 of the "gcm-revised-spec" cited above.
*/
/*
* This 16-entry table of pre-computed constants is used by the
* GHASH multiplier to improve over a strictly table-free but
* significantly slower 128x128 bit multiple within GF(2^128).
*/
static const uint64_t last4[16] = {0x0000, 0x1c20, 0x3840, 0x2460,
0x7080, 0x6ca0, 0x48c0, 0x54e0,
0xe100, 0xfd20, 0xd940, 0xc560,
0x9180, 0x8da0, 0xa9c0, 0xb5e0};
/*
* Platform Endianness Neutralizing Load and Store Macro definitions
* GCM wants platform-neutral Big Endian (BE) byte ordering
*/
#define GET_UINT32_BE(n, b, i) \
{ \
(n) = ((uint32_t)(b)[(i)] << 24) | \
((uint32_t)(b)[(i) + 1] << 16) | \
((uint32_t)(b)[(i) + 2] << 8) | \
((uint32_t)(b)[(i) + 3]); \
}
#define PUT_UINT32_BE(n, b, i) \
{ \
(b)[(i)] = (uint8_t)((n) >> 24); \
(b)[(i) + 1] = (uint8_t)((n) >> 16); \
(b)[(i) + 2] = (uint8_t)((n) >> 8); \
(b)[(i) + 3] = (uint8_t)((n)); \
}
/******************************************************************************
*
* GCM_MULT
*
* Performs a GHASH operation on the 128-bit input vector 'x', setting
* the 128-bit output vector to 'x' times H using our precomputed tables.
* 'x' and 'output' are seen as elements of GCM's GF(2^128) Galois field.
*
******************************************************************************/
static void gcm_mult(gcm_context *ctx, // pointer to established context
const uint8_t x[16], // pointer to 128-bit input vector
uint8_t output[16]) // pointer to 128-bit output vector
{
int i;
uint8_t lo, hi, rem;
uint64_t zh, zl;
lo = (uint8_t)(x[15] & 0x0f);
hi = (uint8_t)(x[15] >> 4);
zh = ctx->HH[lo];
zl = ctx->HL[lo];
for (i = 15; i >= 0; i--) {
lo = (uint8_t)(x[i] & 0x0f);
hi = (uint8_t)(x[i] >> 4);
if (i != 15) {
rem = (uint8_t)(zl & 0x0f);
zl = (zh << 60) | (zl >> 4);
zh = (zh >> 4);
zh ^= (uint64_t)last4[rem] << 48;
zh ^= ctx->HH[lo];
zl ^= ctx->HL[lo];
}
rem = (uint8_t)(zl & 0x0f);
zl = (zh << 60) | (zl >> 4);
zh = (zh >> 4);
zh ^= (uint64_t)last4[rem] << 48;
zh ^= ctx->HH[hi];
zl ^= ctx->HL[hi];
}
PUT_UINT32_BE(zh >> 32, output, 0);
PUT_UINT32_BE(zh, output, 4);
PUT_UINT32_BE(zl >> 32, output, 8);
PUT_UINT32_BE(zl, output, 12);
}
/******************************************************************************
*
* GCM_SETKEY
*
* This is called to set the AES-GCM key. It initializes the AES key
* and populates the gcm context's pre-calculated HTables.
*
******************************************************************************/
int gcm_setkey(gcm_context *ctx, // pointer to caller-provided gcm context
const uint8_t *key, // pointer to the AES encryption key
const size_t keysize) // size in bytes (must be 16, 24, 32 for
// 128, 192 or 256-bit keys respectively)
{
int ret, i, j;
uint64_t hi, lo;
uint64_t vl, vh;
unsigned char h[16];
memset(ctx, 0, sizeof(gcm_context)); // zero caller-provided GCM context
memset(h, 0, 16); // initialize the block to encrypt
ctx->mode = AES_MODE_NONE;
// encrypt the null 128-bit block to generate a key-based value
// which is then used to initialize our GHASH lookup tables
if ((ret = aes_setkey(&ctx->aes_ctx, key, keysize)) != 0)
return (ret);
if ((ret = aes_cipher(&ctx->aes_ctx, h, h)) != 0)
return (ret);
GET_UINT32_BE(hi, h, 0); // pack h as two 64-bit ints, big-endian
GET_UINT32_BE(lo, h, 4);
vh = (uint64_t)hi << 32 | lo;
GET_UINT32_BE(hi, h, 8);
GET_UINT32_BE(lo, h, 12);
vl = (uint64_t)hi << 32 | lo;
ctx->HL[8] = vl; // 8 = 1000 corresponds to 1 in GF(2^128)
ctx->HH[8] = vh;
ctx->HH[0] = 0; // 0 corresponds to 0 in GF(2^128)
ctx->HL[0] = 0;
for (i = 4; i > 0; i >>= 1) {
uint32_t T = (uint32_t)(vl & 1) * 0xe1000000U;
vl = (vh << 63) | (vl >> 1);
vh = (vh >> 1) ^ ((uint64_t)T << 32);
ctx->HL[i] = vl;
ctx->HH[i] = vh;
}
for (i = 2; i < 16; i <<= 1) {
uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i;
vh = *HiH;
vl = *HiL;
for (j = 1; j < i; j++) {
HiH[j] = vh ^ ctx->HH[j];
HiL[j] = vl ^ ctx->HL[j];
}
}
return (0);
}
/******************************************************************************
*
* GCM processing occurs four phases: SETKEY, START, UPDATE and FINISH.
*
* SETKEY:
*
* START: Sets the Encryption/Decryption mode.
* Accepts the initialization vector and additional data.
*
* UPDATE: Encrypts or decrypts the plaintext or ciphertext.
*
* FINISH: Performs a final GHASH to generate the authentication tag.
*
******************************************************************************
*
* GCM_START
*
* Given a user-provided GCM context, this initializes it, sets the encryption
* mode, and preprocesses the initialization vector and additional AEAD data.
*
******************************************************************************/
int gcm_start(gcm_context *ctx, // pointer to user-provided GCM context
enum aes_op_mode mode, // GCM_ENCRYPT or GCM_DECRYPT
const uint8_t *iv, // pointer to initialization vector
size_t iv_len, // IV length in bytes (should == 12)
const uint8_t *add, // ptr to additional AEAD data (NULL if none)
size_t add_len) // length of additional AEAD data (bytes)
{
int ret; // our error return if the AES encrypt fails
// TODO: cleanup
#ifndef AES_GCM_FIXED_12_BYTE_IV
uint8_t work_buf[16]; // XOR source built from provided IV if len != 16
#endif
const uint8_t *p; // general purpose array pointer
size_t use_len; // byte count to process, up to 16 bytes
size_t i; // local loop iterator
// since the context might be reused under the same key
// we zero the working buffers for this next new process
memset(ctx->y, 0x00, sizeof(ctx->y));
memset(ctx->buf, 0x00, sizeof(ctx->buf));
ctx->len = 0;
ctx->add_len = 0;
ctx->mode = mode; // set the GCM encryption/decryption mode
if (iv_len == 12) { // GCM natively uses a 12-byte, 96-bit IV
memcpy(ctx->y, iv, iv_len); // copy the IV to the top of the 'y'
// buff
ctx->y[15] = 1; // start "counting" from 1 (not 0)
}
#ifdef AES_GCM_FIXED_12_BYTE_IV
uassert(iv_len == 12);
#else
else // if we don't have a 12-byte IV, we GHASH whatever we've been
// given
{
memset(work_buf, 0x00, 16); // clear the working buffer
PUT_UINT32_BE(iv_len * 8, work_buf, 12); // place the IV into
// buffer
p = iv;
while (iv_len > 0) {
use_len = (iv_len < 16) ? iv_len : 16;
for (i = 0; i < use_len; i++)
ctx->y[i] ^= p[i];
gcm_mult(ctx, ctx->y, ctx->y);
iv_len -= use_len;
p += use_len;
}
for (i = 0; i < 16; i++)
ctx->y[i] ^= work_buf[i];
gcm_mult(ctx, ctx->y, ctx->y);
}
#endif
if ((ret = aes_cipher(&ctx->aes_ctx, ctx->y, ctx->base_ectr)) != 0)
return (ret);
ctx->add_len = add_len;
p = add;
while (add_len > 0) {
use_len = (add_len < 16) ? add_len : 16;
for (i = 0; i < use_len; i++)
ctx->buf[i] ^= p[i];
gcm_mult(ctx, ctx->buf, ctx->buf);
add_len -= use_len;
p += use_len;
}
return (0);
}
/******************************************************************************
*
* GCM_UPDATE
*
* This is called once or more to process bulk plaintext or ciphertext data.
* We give this some number of bytes of input and it returns the same number
* of output bytes. If called multiple times (which is fine) all but the final
* invocation MUST be called with length mod 16 == 0. (Only the final call can
* have a partial block length of < 128 bits.)
*
******************************************************************************/
int gcm_update(gcm_context *ctx, // pointer to user-provided GCM context
size_t length, // length, in bytes, of data to process
const uint8_t *input, // pointer to source data
uint8_t *output) // pointer to destination data
{
int ret; // our error return if the AES encrypt fails
uint8_t ectr[16]; // counter-mode cipher output for XORing
size_t use_len; // byte count to process, up to 16 bytes
size_t i; // local loop iterator
uassert(ctx->mode == AES_MODE_ENCRYPT || ctx->mode == AES_MODE_DECRYPT);
ctx->len += length; // bump the GCM context's running length count
while (length > 0) {
// clamp the length to process at 16 bytes
use_len = (length < 16) ? length : 16;
// increment the context's 128-bit IV||Counter 'y' vector
for (i = 16; i > 12; i--)
if (++ctx->y[i - 1] != 0)
break;
// encrypt the context's 'y' vector under the established key
if ((ret = aes_cipher(&ctx->aes_ctx, ctx->y, ectr)) != 0)
return (ret);
// encrypt or decrypt the input to the output
if (ctx->mode == AES_MODE_ENCRYPT) {
for (i = 0; i < use_len; i++) {
// XOR the cipher's ouptut vector (ectr) with
// our input
output[i] = (uint8_t)(ectr[i] ^ input[i]);
// now we mix in our data into the
// authentication hash. if we're ENcrypting we
// XOR in the post-XOR (output) results, but if
// we're DEcrypting we XOR in the input data
ctx->buf[i] ^= output[i];
}
} else if (ctx->mode == AES_MODE_DECRYPT) {
for (i = 0; i < use_len; i++) {
// but if we're DEcrypting we XOR in the input
// data first, i.e. before saving to ouput data,
// otherwise if the input and output buffer are
// the same (inplace decryption) we would not
// get the correct auth tag
ctx->buf[i] ^= input[i];
// XOR the cipher's ouptut vector (ectr) with
// our input
output[i] = (uint8_t)(ectr[i] ^ input[i]);
}
}
gcm_mult(ctx, ctx->buf, ctx->buf); // perform a GHASH operation
length -= use_len; // drop the remaining byte count to process
input += use_len; // bump our input pointer forward
output += use_len; // bump our output pointer forward
}
return (0);
}
/******************************************************************************
*
* GCM_FINISH
*
* This is called once after all calls to GCM_UPDATE to finalize the GCM.
* It performs the final GHASH to produce the resulting authentication TAG.
*
******************************************************************************/
int gcm_finish(gcm_context *ctx, // pointer to user-provided GCM context
uint8_t *tag, // pointer to buffer which receives the tag
size_t tag_len) // length, in bytes, of the tag-receiving buf
{
uint8_t work_buf[16];
uint64_t orig_len = ctx->len * 8;
uint64_t orig_add_len = ctx->add_len * 8;
size_t i;
uassert(ctx->mode == AES_MODE_ENCRYPT || ctx->mode == AES_MODE_DECRYPT);
if (tag_len != 0)
memcpy(tag, ctx->base_ectr, tag_len);
if (orig_len || orig_add_len) {
memset(work_buf, 0x00, 16);
PUT_UINT32_BE((orig_add_len >> 32), work_buf, 0);
PUT_UINT32_BE((orig_add_len), work_buf, 4);
PUT_UINT32_BE((orig_len >> 32), work_buf, 8);
PUT_UINT32_BE((orig_len), work_buf, 12);
for (i = 0; i < 16; i++)
ctx->buf[i] ^= work_buf[i];
gcm_mult(ctx, ctx->buf, ctx->buf);
for (i = 0; i < tag_len; i++)
tag[i] ^= ctx->buf[i];
}
return (0);
}
/******************************************************************************
*
* GCM_CRYPT_AND_TAG
*
* This either encrypts or decrypts the user-provided data and, either
* way, generates an authentication tag of the requested length. It must be
* called with a GCM context whose key has already been set with GCM_SETKEY.
*
* The user would typically call this explicitly to ENCRYPT a buffer of data
* and optional associated data, and produce its an authentication tag.
*
* To reverse the process the user would typically call the companion
* GCM_AUTH_DECRYPT function to decrypt data and verify a user-provided
* authentication tag. The GCM_AUTH_DECRYPT function calls this function
* to perform its decryption and tag generation, which it then compares.
*
******************************************************************************/
int gcm_crypt_and_tag(gcm_context *ctx, // gcm context with key already setup
enum aes_op_mode mode, // cipher direction: GCM_ENCRYPT or
// GCM_DECRYPT
const uint8_t *iv, // pointer to the 12-byte
// initialization vector
size_t iv_len, // byte length if the IV. should always be
// 12
const uint8_t *add, // pointer to the non-ciphered
// additional data
size_t add_len, // byte length of the additional AEAD data
const uint8_t *input, // pointer to the cipher data source
uint8_t *output, // pointer to the cipher data destination
size_t length, // byte length of the cipher data
uint8_t *tag, // pointer to the tag to be generated
size_t tag_len) // byte length of the tag to be generated
{ /*
assuming that the caller has already invoked gcm_setkey to
prepare the gcm context with the keying material, we simply
invoke each of the three GCM sub-functions in turn...
*/
gcm_start(ctx, mode, iv, iv_len, add, add_len);
gcm_update(ctx, length, input, output);
gcm_finish(ctx, tag, tag_len);
return (0);
}
/******************************************************************************
*
* GCM_AUTH_DECRYPT
*
* This DECRYPTS a user-provided data buffer with optional associated data.
* It then verifies a user-supplied authentication tag against the tag just
* re-created during decryption to verify that the data has not been altered.
*
* This function calls GCM_CRYPT_AND_TAG (above) to perform the decryption
* and authentication tag generation.
*
******************************************************************************/
int gcm_auth_decrypt(gcm_context *ctx, // gcm context with key already setup
const uint8_t *iv, // pointer to the 12-byte initialization
// vector
size_t iv_len, // byte length if the IV. should always be
// 12
const uint8_t *add, // pointer to the non-ciphered
// additional data
size_t add_len, // byte length of the additional AEAD data
const uint8_t *input, // pointer to the cipher data source
uint8_t *output, // pointer to the cipher data destination
size_t length, // byte length of the cipher data
const uint8_t *tag, // pointer to the tag to be
// authenticated
size_t tag_len) // byte length of the tag <= 16
{
uint8_t check_tag[16]; // the tag generated and returned by decryption
int diff; // an ORed flag to detect authentication errors
size_t i; // our local iterator
/*
we use GCM_DECRYPT_AND_TAG (above) to perform our decryption
(which is an identical XORing to reverse the previous one)
and also to re-generate the matching authentication tag
*/
gcm_crypt_and_tag(ctx, AES_MODE_DECRYPT, iv, iv_len, add, add_len,
input, output, length, check_tag, tag_len);
// now we verify the authentication tag in 'constant time'
for (diff = 0, i = 0; i < tag_len; i++)
diff |= tag[i] ^ check_tag[i];
if (diff != 0) { // see whether any bits differed?
memset(output, 0, length); // if so... wipe the output data
return (GCM_AUTH_FAILURE); // return GCM_AUTH_FAILURE
}
return (0);
}
/******************************************************************************
*
* GCM_ZERO_CTX
*
* The GCM context contains both the GCM context and the AES context.
* This includes keying and key-related material which is security-
* sensitive, so it MUST be zeroed after use. This function does that.
*
******************************************************************************/
void gcm_zero_ctx(gcm_context *ctx)
{
// zero the context originally provided to us
memset(ctx, 0, sizeof(gcm_context));
ctx->mode = AES_MODE_NONE;
}

View file

@ -1,281 +0,0 @@
/*
* cifra - embedded cryptography library
* Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#ifndef BITOPS_H
#define BITOPS_H
#include <linux/types.h>
/* Assorted bitwise and common operations used in ciphers. */
/** Circularly rotate right x by n bits.
* 0 > n > 32. */
static inline uint32_t rotr32(uint32_t x, unsigned n)
{
return (x >> n) | (x << (32 - n));
}
/** Circularly rotate left x by n bits.
* 0 > n > 32. */
static inline uint32_t rotl32(uint32_t x, unsigned n)
{
return (x << n) | (x >> (32 - n));
}
/** Circularly rotate right x by n bits.
* 0 > n > 64. */
static inline uint64_t rotr64(uint64_t x, unsigned n)
{
return (x >> n) | (x << (64 - n));
}
/** Circularly rotate left x by n bits.
* 0 > n > 64. */
static inline uint64_t rotl64(uint64_t x, unsigned n)
{
return (x << n) | (x >> (64 - n));
}
/** Read 4 bytes from buf, as a 32-bit big endian quantity. */
static inline uint32_t read32_be(const uint8_t buf[4])
{
return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | (buf[3]);
}
/** Read 4 bytes from buf, as a 32-bit little endian quantity. */
static inline uint32_t read32_le(const uint8_t buf[4])
{
return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | (buf[0]);
}
/** Read 8 bytes from buf, as a 64-bit big endian quantity. */
static inline uint64_t read64_be(const uint8_t buf[8])
{
uint32_t hi = read32_be(buf), lo = read32_be(buf + 4);
return ((uint64_t)hi) << 32 | lo;
}
/** Read 8 bytes from buf, as a 64-bit little endian quantity. */
static inline uint64_t read64_le(const uint8_t buf[8])
{
uint32_t hi = read32_le(buf + 4), lo = read32_le(buf);
return ((uint64_t)hi) << 32 | lo;
}
/** Encode v as a 32-bit big endian quantity into buf. */
static inline void write32_be(uint32_t v, uint8_t buf[4])
{
*buf++ = (v >> 24) & 0xff;
*buf++ = (v >> 16) & 0xff;
*buf++ = (v >> 8) & 0xff;
*buf = v & 0xff;
}
/** Encode v as a 32-bit little endian quantity into buf. */
static inline void write32_le(uint32_t v, uint8_t buf[4])
{
*buf++ = v & 0xff;
*buf++ = (v >> 8) & 0xff;
*buf++ = (v >> 16) & 0xff;
*buf = (v >> 24) & 0xff;
}
/** Encode v as a 64-bit big endian quantity into buf. */
static inline void write64_be(uint64_t v, uint8_t buf[8])
{
*buf++ = (v >> 56) & 0xff;
*buf++ = (v >> 48) & 0xff;
*buf++ = (v >> 40) & 0xff;
*buf++ = (v >> 32) & 0xff;
*buf++ = (v >> 24) & 0xff;
*buf++ = (v >> 16) & 0xff;
*buf++ = (v >> 8) & 0xff;
*buf = v & 0xff;
}
/** Encode v as a 64-bit little endian quantity into buf. */
static inline void write64_le(uint64_t v, uint8_t buf[8])
{
*buf++ = v & 0xff;
*buf++ = (v >> 8) & 0xff;
*buf++ = (v >> 16) & 0xff;
*buf++ = (v >> 24) & 0xff;
*buf++ = (v >> 32) & 0xff;
*buf++ = (v >> 40) & 0xff;
*buf++ = (v >> 48) & 0xff;
*buf = (v >> 56) & 0xff;
}
/** out = in ^ b8.
* out and in may alias. */
static inline void xor_b8(uint8_t *out, const uint8_t *in, uint8_t b8,
size_t len)
{
for (size_t i = 0; i < len; i++)
out[i] = in[i] ^ b8;
}
/** out = x ^ y.
* out, x and y may alias. */
static inline void xor_bb(uint8_t *out, const uint8_t *x, const uint8_t *y,
size_t len)
{
for (size_t i = 0; i < len; i++)
out[i] = x[i] ^ y[i];
}
/* out ^= x
* out and x may alias. */
static inline void xor_words(uint32_t *out, const uint32_t *x, size_t nwords)
{
for (size_t i = 0; i < nwords; i++)
out[i] ^= x[i];
}
/** Produce 0xffffffff if x == y, zero otherwise, without branching. */
static inline uint32_t mask_u32(uint32_t x, uint32_t y)
{
uint32_t diff = x ^ y;
uint32_t diff_is_zero = ~diff & (diff - 1);
return -(diff_is_zero >> 31);
}
/** Product 0xff if x == y, zero otherwise, without branching. */
static inline uint8_t mask_u8(uint32_t x, uint32_t y)
{
uint32_t diff = x ^ y;
uint8_t diff_is_zero = ~diff & (diff - 1);
return -(diff_is_zero >> 7);
}
/** Select the ith entry from the given table of n values, in a side
* channel-silent way. */
static inline uint32_t select_u32(uint32_t i, volatile const uint32_t *tab,
uint32_t n)
{
uint32_t r = 0;
for (uint32_t ii = 0; ii < n; ii++) {
uint32_t mask = mask_u32(i, ii);
r = (r & ~mask) | (tab[ii] & mask);
}
return r;
}
/** Select the ith entry from the given table of n values, in a side
* channel-silent way. */
static inline uint8_t select_u8(uint32_t i, volatile const uint8_t *tab,
uint32_t n)
{
uint8_t r = 0;
for (uint32_t ii = 0; ii < n; ii++) {
uint8_t mask = mask_u8(i, ii);
r = (r & ~mask) | (tab[ii] & mask);
}
return r;
}
/** Select the ath, bth, cth and dth entries from the given table of n values,
* placing the results into a, b, c and d. */
static inline void select_u8x4(uint8_t *a, uint8_t *b, uint8_t *c, uint8_t *d,
volatile const uint8_t *tab, uint32_t n)
{
uint8_t ra = 0, rb = 0, rc = 0, rd = 0;
uint8_t mask;
for (uint32_t i = 0; i < n; i++) {
uint8_t item = tab[i];
mask = mask_u8(*a, i);
ra = (ra & ~mask) | (item & mask);
mask = mask_u8(*b, i);
rb = (rb & ~mask) | (item & mask);
mask = mask_u8(*c, i);
rc = (rc & ~mask) | (item & mask);
mask = mask_u8(*d, i);
rd = (rd & ~mask) | (item & mask);
}
*a = ra;
*b = rb;
*c = rc;
*d = rd;
}
/** out ^= if0 or if1, depending on the value of bit. */
static inline void select_xor128(uint32_t out[4], const uint32_t if0[4],
const uint32_t if1[4], uint8_t bit)
{
uint32_t mask1 = mask_u32(bit, 1);
uint32_t mask0 = ~mask1;
out[0] ^= (if0[0] & mask0) | (if1[0] & mask1);
out[1] ^= (if0[1] & mask0) | (if1[1] & mask1);
out[2] ^= (if0[2] & mask0) | (if1[2] & mask1);
out[3] ^= (if0[3] & mask0) | (if1[3] & mask1);
}
/** Increments the integer stored at v (of non-zero length len)
* with the least significant byte first. */
static inline void incr_le(uint8_t *v, size_t len)
{
size_t i = 0;
while (1) {
if (++v[i] != 0)
return;
i++;
if (i == len)
return;
}
}
/** Increments the integer stored at v (of non-zero length len)
* with the most significant byte last. */
static inline void incr_be(uint8_t *v, size_t len)
{
len--;
while (1) {
if (++v[len] != 0)
return;
if (len == 0)
return;
len--;
}
}
/** Copies len bytes from in to out, with in shifted left by offset bits
* to the right. */
static inline void copy_bytes_unaligned(uint8_t *out, const uint8_t *in,
size_t len, uint8_t offset)
{
uint8_t byte_off = offset / 8;
uint8_t bit_off = offset & 7;
uint8_t rmask = (1 << bit_off) - 1;
uint8_t lmask = ~rmask;
for (size_t i = 0; i < len; i++) {
out[i] = (in[i + byte_off] << bit_off) & lmask;
out[i] |= (in[i + byte_off + 1] >> (8 - bit_off)) & rmask;
}
}
static inline uint32_t count_trailing_zeroes(uint32_t x)
{
return (uint32_t)__builtin_ctzl(x);
}
#endif

View file

@ -1,135 +0,0 @@
/*
* cifra - embedded cryptography library
* Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#ifndef BLOCKWISE_H
#define BLOCKWISE_H
#include <linux/types.h>
/* Processing function for cf_blockwise_accumulate. */
typedef void (*cf_blockwise_in_fn)(void *ctx, const uint8_t *data);
/* Processing function for cf_blockwise_xor. */
typedef void (*cf_blockwise_out_fn)(void *ctx, uint8_t *data);
/* This function manages the common abstraction of accumulating input in
* a buffer, and processing it when a full block is available.
*
* partial is the buffer (maintained by the caller)
* on entry, npartial is the currently valid count of used bytes on
* the front of partial.
* on exit, npartial is updated to reflect the status of partial.
* nblock is the blocksize to accumulate -- partial must be at least
* this long!
* input is the new data to process, of length nbytes.
* process is the processing function, passed ctx and a pointer
* to the data to process (always exactly nblock bytes long!)
* which may not neccessarily be the same as partial.
*/
void cf_blockwise_accumulate(uint8_t *partial, size_t *npartial, size_t nblock,
const void *input, size_t nbytes,
cf_blockwise_in_fn process, void *ctx);
/* This function manages the common abstraction of accumulating input in
* a buffer, and processing it when a full block is available.
* This version supports calling a different processing function for
* the last block.
*
* partial is the buffer (maintained by the caller)
* on entry, npartial is the currently valid count of used bytes on
* the front of partial.
* on exit, npartial is updated to reflect the status of partial.
* nblock is the blocksize to accumulate -- partial must be at least
* this long!
* input is the new data to process, of length nbytes.
* process is the processing function, passed ctx and a pointer
* to the data to process (always exactly nblock bytes long!)
* which may not neccessarily be the same as partial.
* process_final is called last (but may not be called at all if
* all input is buffered).
*/
void cf_blockwise_accumulate_final(uint8_t *partial, size_t *npartial,
size_t nblock, const void *input,
size_t nbytes, cf_blockwise_in_fn process,
cf_blockwise_in_fn process_final, void *ctx);
/* This function manages XORing an input stream with a keystream
* to produce an output stream. The keystream is produced in blocks
* (ala a block cipher in counter mode).
*
* partial is the keystream buffer (maintained by the caller)
* on entry, *npartial is the currently valid count of bytes in partial:
* unused bytes are at the *end*. So *npartial = 4 means the last four
* bytes of partial are usable as keystream.
* on exit, npartial is updated to reflect the new state of partial.
* nblock is the blocksize to accumulate -- partial must be at least
* this long!
* input is the new data to process, of length nbytes.
* output is where to write input xored with the keystream -- also length
* nbytes.
* process is the processing function, passed ctx and partial which it
* should fill with fresh key stream.
*/
void cf_blockwise_xor(uint8_t *partial, size_t *npartial, size_t nblock,
const void *input, void *output, size_t nbytes,
cf_blockwise_out_fn newblock, void *ctx);
/* This function processes a single byte a number of times. It's useful
* for padding, and more efficient than calling cf_blockwise_accumulate
* a bunch of times.
*
* partial is the buffer (maintained by the caller)
* on entry, npartial is the currently valid count of used bytes on
* the front of partial.
* on exit, npartial is updated to reflect the status of partial.
* nblock is the blocksize to accumulate -- partial must be at least
* this long!
* process is the processing function, passed ctx and a pointer
* to the data to process (always exactly nblock bytes long!)
* which may not neccessarily be the same as partial.
* byte is the byte to process, nbytes times.
*/
void cf_blockwise_acc_byte(uint8_t *partial, size_t *npartial, size_t nblock,
uint8_t byte, size_t nbytes,
cf_blockwise_in_fn process, void *ctx);
/* This function attempts to process patterns of bytes common in
* block cipher padding.
*
* This takes three bytes:
* - a first byte, fbyte,
* - a middle byte, mbyte,
* - a last byte, lbyte.
*
* If nbytes is zero, nothing happens.
* If nbytes is one, the byte fbyte ^ lbyte is processed.
* If nbytes is two, the fbyte then lbyte are processed.
* If nbytes is three or more, fbyte, then one or more mbytes, then fbyte
* is processed.
*
* partial is the buffer (maintained by the caller)
* on entry, npartial is the currently valid count of used bytes on
* the front of partial.
* on exit, npartial is updated to reflect the status of partial.
* nblock is the blocksize to accumulate -- partial must be at least
* this long!
* process is the processing function, passed ctx and a pointer
* to the data to process (always exactly nblock bytes long!)
* which may not neccessarily be the same as partial.
*/
void cf_blockwise_acc_pad(uint8_t *partial, size_t *npartial, size_t nblock,
uint8_t fbyte, uint8_t mbyte, uint8_t lbyte,
size_t nbytes, cf_blockwise_in_fn process, void *ctx);
#endif

View file

@ -1,134 +0,0 @@
/*
* cifra - embedded cryptography library
* Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#ifndef CHASH_H
#define CHASH_H
#include <linux/types.h>
/**
* General hash function description
* =================================
* This allows us to make use of hash functions without depending
* on a specific one. This is useful in implementing, for example,
* :doc:`HMAC <hmac>`.
*/
/* .. c:type:: cf_chash_init
* Hashing initialisation function type.
*
* Functions of this type should initialise the context in preparation
* for hashing a message with `cf_chash_update` functions.
*
* :rtype: void
* :param ctx: hash function-specific context structure.
*/
typedef void (*cf_chash_init)(void *ctx);
/* .. c:type:: cf_chash_update
* Hashing data processing function type.
*
* Functions of this type hash `count` bytes of data at `data`,
* updating the contents of `ctx`.
*
* :rtype: void
* :param ctx: hash function-specific context structure.
* :param data: input data to hash.
* :param count: number of bytes to hash.
*/
typedef void (*cf_chash_update)(void *ctx, const void *data, size_t count);
/* .. c:type:: cf_chash_digest
* Hashing completion function type.
*
* Functions of this type complete a hashing operation,
* writing :c:member:`cf_chash.hashsz` bytes to `hash`.
*
* This function does not change `ctx` -- any padding which needs doing
* must be done seperately (in a copy of `ctx`, say).
*
* This means you can interlave `_update` and `_digest` calls to
* learn `H(A)` and `H(A || B)` without hashing `A` twice.
*
* :rtype: void
* :param ctx: hash function-specific context structure.
* :param hash: location to write hash result.
*/
typedef void (*cf_chash_digest)(const void *ctx, uint8_t *hash);
/* .. c:type:: cf_chash
* This type describes an incremental hash function in an abstract way.
*
* .. c:member:: cf_chash.hashsz
* The hash function's output, in bytes.
*
* .. c:member:: cf_chash.blocksz
* The hash function's internal block size, in bytes.
*
* .. c:member:: cf_chash.init
* Context initialisation function.
*
* .. c:member:: cf_chash:update
* Data processing function.
*
* .. c:member:: cf_chash:digest
* Completion function.
*
*/
typedef struct {
size_t hashsz;
size_t blocksz;
cf_chash_init init;
cf_chash_update update;
cf_chash_digest digest;
} cf_chash;
/* .. c:macro:: CF_CHASH_MAXCTX
* The maximum size of a :c:type:`cf_chash_ctx`. This allows
* use to put a structure in automatic storage that can
* store working data for any supported hash function. */
#define CF_CHASH_MAXCTX 390
/* .. c:macro:: CF_CHASH_MAXBLK
* Maximum hash function block size (in bytes). */
#define CF_CHASH_MAXBLK 128
/* .. c:macro:: CF_MAXHASH
* Maximum hash function output (in bytes). */
#define CF_MAXHASH 64
/* .. c:type:: cf_chash_ctx
* A type usable with any `cf_chash` as a context. */
typedef union {
uint8_t ctx[CF_CHASH_MAXCTX];
uint16_t u16;
uint32_t u32;
uint64_t u64;
} cf_chash_ctx;
/* .. c:function:: $DECL
* One shot hashing: `out = h(m)`.
*
* Using the hash function `h`, `nm` bytes at `m` are hashed and `h->hashsz`
* bytes of result is written to the buffer `out`.
*
* :param h: hash function description.
* :param m: message buffer.
* :param nm: message length.
* :param out: hash result buffer (written).
*/
void cf_hash(const cf_chash *h, const void *m, size_t nm, uint8_t *out);
#endif

View file

@ -1,119 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef HANDY_H
#define HANDY_H
#include <asm/string.h>
#include <uassert.h>
/*
* Handy CPP defines and C inline functions.
*/
/* Evaluates to the number of items in array-type variable arr. */
#define ARRAYCOUNT(arr) (sizeof arr / sizeof arr[0])
/* Normal MIN/MAX macros. Evaluate argument expressions only once. */
#ifndef MIN
#define MIN(x, y) \
({ \
typeof(x) __x = (x); \
typeof(y) __y = (y); \
__x < __y ? __x : __y; \
})
#endif
#ifndef MAX
#define MAX(x, y) \
({ \
typeof(x) __x = (x); \
typeof(y) __y = (y); \
__x > __y ? __x : __y; \
})
#endif
/* Swap two values. Uses GCC type inference magic. */
#ifndef SWAP
#define SWAP(x, y) \
do { \
typeof(x) __tmp = (x); \
(x) = (y); \
(y) = __tmp; \
} while (0)
#endif
/** Stringify its argument. */
#define STRINGIFY(x) STRINGIFY_(x)
#define STRINGIFY_(x) #x
/* Error handling macros.
*
* These expect a zero = success, non-zero = error convention.
*/
/** Error: return.
*
* If the expression fails, return the error from this function. */
#define ER(expr) \
do { \
typeof(expr) err_ = (expr); \
if (err_) \
return err_; \
} while (0)
/** Error: goto.
*
* If the expression fails, goto x_err. Assumes defn of label
* x_err and 'error_type err'. */
#define EG(expr) \
do { \
err = (expr); \
if (err) \
goto x_err; \
} while (0)
/** Like memset(ptr, 0, len), but not allowed to be removed by
* compilers. */
static inline void mem_clean(volatile void *v, size_t len)
{
if (len) {
memset((void *)v, 0, len);
(void)*((volatile uint8_t *)v);
}
}
/** Returns 1 if len bytes at va equal len bytes at vb, 0 if they do not.
* Does not leak length of common prefix through timing. */
static inline unsigned mem_eq(const void *va, const void *vb, size_t len)
{
const volatile uint8_t *a = va;
const volatile uint8_t *b = vb;
uint8_t diff = 0;
while (len--) {
diff |= *a++ ^ *b++;
}
return !diff;
}
#endif

View file

@ -1,72 +0,0 @@
/*
* cifra - embedded cryptography library
* Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#ifndef HMAC_H
#define HMAC_H
#include <linux/types.h>
#include "chash.h"
/**
* HMAC
* ====
* This is a one-shot and incremental interface to computing
* HMAC with any hash function.
*
* (Note: HMAC with SHA3 is possible, but is probably not a
* sensible thing to want.)
*/
/* .. c:type:: cf_hmac_ctx
* HMAC incremental interface context.
*
* .. c:member:: cf_hmac_ctx.hash
* Hash function description.
*
* .. c:member:: cf_hmac_ctx.inner
* Inner hash computation.
*
* .. c:member:: cf_hmac_ctx.outer
* Outer hash computation.
*/
typedef struct {
const cf_chash *hash;
cf_chash_ctx inner;
cf_chash_ctx outer;
} cf_hmac_ctx;
/* .. c:function:: $DECL
* Set up ctx for computing a HMAC using the given hash and key. */
void cf_hmac_init(cf_hmac_ctx *ctx, const cf_chash *hash, const uint8_t *key,
size_t nkey);
/* .. c:function:: $DECL
* Input data. */
void cf_hmac_update(cf_hmac_ctx *ctx, const void *data, size_t ndata);
/* .. c:function:: $DECL
* Finish and compute HMAC.
* `ctx->hash->hashsz` bytes are written to `out`. */
void cf_hmac_finish(cf_hmac_ctx *ctx, uint8_t *out);
/* .. c:function:: $DECL
* One shot interface: compute `HMAC_hash(key, msg)`, writing the
* answer (which is `hash->hashsz` long) to `out`.
*
* This function does not fail. */
void cf_hmac(const uint8_t *key, size_t nkey, const uint8_t *msg, size_t nmsg,
uint8_t *out, const cf_chash *hash);
#endif

View file

@ -1,144 +0,0 @@
/*
* Interface of curve P-256 (ECDH and ECDSA)
*
* Copyright The Mbed TLS Contributors
* Author: Manuel Pégourié-Gonnard.
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
*/
#ifndef P256_M_H
#define P256_M_H
#include <linux/types.h>
#include <linux/random.h>
/* Status codes */
#define P256_SUCCESS 0
#define P256_RANDOM_FAILED -1
#define P256_INVALID_PUBKEY -2
#define P256_INVALID_PRIVKEY -3
#define P256_INVALID_SIGNATURE -4
#ifdef __cplusplus
extern "C" {
#endif
/*
* RNG function - must be provided externally and be cryptographically secure.
*
* in: output - must point to a writable buffer of at least output_size bytes.
* output_size - the number of random bytes to write to output.
* out: output is filled with output_size random bytes.
* return 0 on success, non-zero on errors.
*/
extern int p256_generate_random(uint8_t *output, unsigned output_size);
/*
* Zeroize function - function to zerioze memory.
*
* in: buf - buffer to be zeroize.
* bytes - number of bytes to zeroize.
* out: buffer is filled with zeroes.
*/
extern void p256_mem_zeroize(void *buf, size_t bytes);
/*
* ECDH/ECDSA generate key pair
*
* [in] draws from p256_generate_random()
* [out] priv: on success, holds the private key, as a big-endian integer
* [out] pub: on success, holds the public key, as two big-endian integers
*
* return: P256_SUCCESS on success
* P256_RANDOM_FAILED on failure
*/
int p256_gen_keypair(uint8_t priv[32], uint8_t pub[64]);
/*
* ECDH compute shared secret
*
* [out] secret: on success, holds the shared secret, as a big-endian integer
* [in] priv: our private key as a big-endian integer
* [in] pub: the peer's public key, as two big-endian integers
*
* return: P256_SUCCESS on success
* P256_INVALID_PRIVKEY if priv is invalid
* P256_INVALID_PUBKEY if pub is invalid
*/
int p256_ecdh_shared_secret(uint8_t secret[32], const uint8_t priv[32],
const uint8_t pub[64]);
/*
* ECDSA sign
*
* [in] draws from p256_generate_random()
* [out] sig: on success, holds the signature, as two big-endian integers
* [in] priv: our private key as a big-endian integer
* [in] hash: the hash of the message to be signed
* [in] hlen: the size of hash in bytes
*
* return: P256_SUCCESS on success
* P256_RANDOM_FAILED on failure
* P256_INVALID_PRIVKEY if priv is invalid
*/
int p256_ecdsa_sign(uint8_t sig[64], const uint8_t priv[32],
const uint8_t *hash, size_t hlen);
/*
* ECDSA verify
*
* [in] sig: the signature to be verified, as two big-endian integers
* [in] pub: the associated public key, as two big-endian integers
* [in] hash: the hash of the message that was signed
* [in] hlen: the size of hash in bytes
*
* return: P256_SUCCESS on success - the signature was verified as valid
* P256_INVALID_PUBKEY if pub is invalid
* P256_INVALID_SIGNATURE if the signature was found to be invalid
*/
int p256_ecdsa_verify(const uint8_t sig[64], const uint8_t pub[64],
const uint8_t *hash, size_t hlen);
/*
* Public key validation
*
* Note: you never need to call this function, as all other functions always
* validate their input; however it's availabe if you want to validate the key
* without performing an operation.
*
* [in] pub: the public key, as two big-endian integers
*
* return: P256_SUCCESS if the key is valid
* P256_INVALID_PUBKEY if pub is invalid
*/
int p256_validate_pubkey(const uint8_t pub[64]);
/*
* Private key validation
*
* Note: you never need to call this function, as all other functions always
* validate their input; however it's availabe if you want to validate the key
* without performing an operation.
*
* [in] priv: the private key, as a big-endian integer
*
* return: P256_SUCCESS if the key is valid
* P256_INVALID_PRIVKEY if priv is invalid
*/
int p256_validate_privkey(const uint8_t priv[32]);
/*
* Compute public key from private key
*
* [out] pub: the associated public key, as two big-endian integers
* [in] priv: the private key, as a big-endian integer
*
* return: P256_SUCCESS on success
* P256_INVALID_PRIVKEY if priv is invalid
*/
int p256_public_from_private(uint8_t pub[64], const uint8_t priv[32]);
#ifdef __cplusplus
}
#endif
#endif /* P256_M_H */

View file

@ -1,244 +0,0 @@
/*
* cifra - embedded cryptography library
* Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#ifndef SHA2_H
#define SHA2_H
#include <linux/types.h>
#include "chash.h"
/**
* SHA224/SHA256
* =============
*/
/* .. c:macro:: CF_SHA224_HASHSZ
* The output size of SHA224: 28 bytes. */
#define CF_SHA224_HASHSZ 28
/* .. c:macro:: CF_SHA224_BLOCKSZ
* The block size of SHA224: 64 bytes. */
#define CF_SHA224_BLOCKSZ 64
/* .. c:macro:: CF_SHA256_HASHSZ
* The output size of SHA256: 32 bytes. */
#define CF_SHA256_HASHSZ 32
/* .. c:macro:: CF_SHA256_BLOCKSZ
* The block size of SHA256: 64 bytes. */
#define CF_SHA256_BLOCKSZ 64
/* .. c:type:: cf_sha256_context
* Incremental SHA256 hashing context.
*
* .. c:member:: cf_sha256_context.H
* Intermediate values.
*
* .. c:member:: cf_sha256_context.partial
* Unprocessed input.
*
* .. c:member:: cf_sha256_context.npartial
* Number of bytes of unprocessed input.
*
* .. c:member:: cf_sha256_context.blocks
* Number of full blocks processed.
*/
typedef struct {
uint32_t H[8]; /* State. */
uint8_t partial[CF_SHA256_BLOCKSZ]; /* Partial block of input. */
uint32_t blocks; /* Number of full blocks processed into H. */
size_t npartial; /* Number of bytes in prefix of partial. */
} cf_sha256_context;
/* .. c:function:: $DECL
* Sets up `ctx` ready to hash a new message.
*/
extern void cf_sha256_init(cf_sha256_context *ctx);
/* .. c:function:: $DECL
* Hashes `nbytes` at `data`. Copies the data if there isn't enough to make
* a full block.
*/
extern void cf_sha256_update(cf_sha256_context *ctx, const void *data,
size_t nbytes);
/* .. c:function:: $DECL
* Finishes the hash operation, writing `CF_SHA256_HASHSZ` bytes to `hash`.
*
* This leaves `ctx` unchanged.
*/
extern void cf_sha256_digest(const cf_sha256_context *ctx,
uint8_t hash[CF_SHA256_HASHSZ]);
/* .. c:function:: $DECL
* Finishes the hash operation, writing `CF_SHA256_HASHSZ` bytes to `hash`.
*
* This destroys `ctx`, but uses less stack than :c:func:`cf_sha256_digest`.
*/
extern void cf_sha256_digest_final(cf_sha256_context *ctx,
uint8_t hash[CF_SHA256_HASHSZ]);
/* .. c:function:: $DECL
* Sets up `ctx` ready to hash a new message.
*
* nb. SHA224 uses SHA256's underlying types.
*/
extern void cf_sha224_init(cf_sha256_context *ctx);
/* .. c:function:: $DECL
* Hashes `nbytes` at `data`. Copies the data if there isn't enough to make
* a full block.
*/
extern void cf_sha224_update(cf_sha256_context *ctx, const void *data,
size_t nbytes);
/* .. c:function:: $DECL
* Finishes the hash operation, writing `CF_SHA224_HASHSZ` bytes to `hash`.
*
* This leaves `ctx` unchanged.
*/
extern void cf_sha224_digest(const cf_sha256_context *ctx,
uint8_t hash[CF_SHA224_HASHSZ]);
/* .. c:function:: $DECL
* Finishes the hash operation, writing `CF_SHA224_HASHSZ` bytes to `hash`.
*
* This destroys `ctx`, but uses less stack than :c:func:`cf_sha224_digest`.
*/
extern void cf_sha224_digest_final(cf_sha256_context *ctx,
uint8_t hash[CF_SHA224_HASHSZ]);
/* .. c:var:: cf_sha224
* Abstract interface to SHA224. See :c:type:`cf_chash` for more information.
*/
extern const cf_chash cf_sha224;
/* .. c:var:: cf_sha256
* Abstract interface to SHA256. See :c:type:`cf_chash` for more information.
*/
extern const cf_chash cf_sha256;
/**
* SHA384/SHA512
* =============
*/
/* .. c:macro:: CF_SHA384_HASHSZ
* The output size of SHA384: 48 bytes. */
#define CF_SHA384_HASHSZ 48
/* .. c:macro:: CF_SHA384_BLOCKSZ
* The block size of SHA384: 128 bytes. */
#define CF_SHA384_BLOCKSZ 128
/* .. c:macro:: CF_SHA512_HASHSZ
* The output size of SHA512: 64 bytes. */
#define CF_SHA512_HASHSZ 64
/* .. c:macro:: CF_SHA512_BLOCKSZ
* The block size of SHA512: 128 bytes. */
#define CF_SHA512_BLOCKSZ 128
/* .. c:type:: cf_sha512_context
* Incremental SHA512 hashing context.
*
* .. c:member:: cf_sha512_context.H
* Intermediate values.
*
* .. c:member:: cf_sha512_context.partial
* Unprocessed input.
*
* .. c:member:: cf_sha512_context.npartial
* Number of bytes of unprocessed input.
*
* .. c:member:: cf_sha512_context.blocks
* Number of full blocks processed.
*/
typedef struct {
uint64_t H[8];
uint8_t partial[CF_SHA512_BLOCKSZ];
uint32_t blocks;
size_t npartial;
} cf_sha512_context;
/* .. c:function:: $DECL
* Sets up `ctx` ready to hash a new message.
*/
extern void cf_sha512_init(cf_sha512_context *ctx);
/* .. c:function:: $DECL
* Hashes `nbytes` at `data`. Copies the data if there isn't enough to make
* a full block.
*/
extern void cf_sha512_update(cf_sha512_context *ctx, const void *data,
size_t nbytes);
/* .. c:function:: $DECL
* Finishes the hash operation, writing `CF_SHA512_HASHSZ` bytes to `hash`.
*
* This leaves `ctx` unchanged.
*/
extern void cf_sha512_digest(const cf_sha512_context *ctx,
uint8_t hash[CF_SHA512_HASHSZ]);
/* .. c:function:: $DECL
* Finishes the hash operation, writing `CF_SHA512_HASHSZ` bytes to `hash`.
*
* This destroys `ctx`, but uses less stack than :c:func:`cf_sha512_digest`.
*/
extern void cf_sha512_digest_final(cf_sha512_context *ctx,
uint8_t hash[CF_SHA512_HASHSZ]);
/* .. c:function:: $DECL
* Sets up `ctx` ready to hash a new message.
*
* nb. SHA384 uses SHA512's underlying types.
*/
extern void cf_sha384_init(cf_sha512_context *ctx);
/* .. c:function:: $DECL
* Hashes `nbytes` at `data`. Copies the data if there isn't enough to make
* a full block.
*/
extern void cf_sha384_update(cf_sha512_context *ctx, const void *data,
size_t nbytes);
/* .. c:function:: $DECL
* Finishes the hash operation, writing `CF_SHA384_HASHSZ` bytes to `hash`.
*
* This leaves `ctx` unchanged.
*/
extern void cf_sha384_digest(const cf_sha512_context *ctx,
uint8_t hash[CF_SHA384_HASHSZ]);
/* .. c:function:: $DECL
* Finishes the hash operation, writing `CF_SHA384_HASHSZ` bytes to `hash`.
*
* This destroys `ctx`, but uses less stack than :c:func:`cf_sha384_digest`.
*/
extern void cf_sha384_digest_final(cf_sha512_context *ctx,
uint8_t hash[CF_SHA384_HASHSZ]);
/* .. c:var:: cf_sha384
* Abstract interface to SHA384. See :c:type:`cf_chash` for more information.
*/
extern const cf_chash cf_sha384;
/* .. c:var:: cf_sha512
* Abstract interface to SHA512. See :c:type:`cf_chash` for more information.
*/
extern const cf_chash cf_sha512;
#endif

View file

@ -1,36 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#ifndef NANOTLS_UASSERT_H
#define NANOTLS_UASSERT_H
#include <linux/types.h>
static inline void uassert(__attribute__((unused)) bool cond)
{
#ifdef DEBUG
#include <assert.h>
assert(cond);
#endif
}
#endif /* NANOTLS_UASSERT_H */

View file

@ -1,186 +0,0 @@
/*
* cifra - embedded cryptography library
* Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include "blockwise.h"
#include "bitops.h"
#include "handy.h"
#include <asm/string.h>
void cf_blockwise_accumulate(uint8_t *partial, size_t *npartial, size_t nblock,
const void *inp, size_t nbytes,
cf_blockwise_in_fn process, void *ctx)
{
cf_blockwise_accumulate_final(partial, npartial, nblock, inp, nbytes,
process, process, ctx);
}
void cf_blockwise_accumulate_final(uint8_t *partial, size_t *npartial,
size_t nblock, const void *inp,
size_t nbytes, cf_blockwise_in_fn process,
cf_blockwise_in_fn process_final, void *ctx)
{
const uint8_t *bufin = inp;
uassert(partial && *npartial < nblock);
uassert(inp || !nbytes);
uassert(process && ctx);
/* If we have partial data, copy in to buffer. */
if (*npartial && nbytes) {
size_t space = nblock - *npartial;
size_t taken = MIN(space, nbytes);
memcpy(partial + *npartial, bufin, taken);
bufin += taken;
nbytes -= taken;
*npartial += taken;
/* If that gives us a full block, process it. */
if (*npartial == nblock) {
if (nbytes == 0)
process_final(ctx, partial);
else
process(ctx, partial);
*npartial = 0;
}
}
/* now nbytes < nblock or *npartial == 0. */
/* If we have a full block of data, process it directly. */
while (nbytes >= nblock) {
/* Partial buffer must be empty, or we're ignoring extant data
*/
uassert(*npartial == 0);
if (nbytes == nblock)
process_final(ctx, bufin);
else
process(ctx, bufin);
bufin += nblock;
nbytes -= nblock;
}
/* Finally, if we have remaining data, buffer it. */
while (nbytes) {
size_t space = nblock - *npartial;
size_t taken = MIN(space, nbytes);
memcpy(partial + *npartial, bufin, taken);
bufin += taken;
nbytes -= taken;
*npartial += taken;
/* If we started with *npartial, we must have copied it
* in first. */
uassert(*npartial < nblock);
}
}
void cf_blockwise_xor(uint8_t *partial, size_t *npartial, size_t nblock,
const void *inp, void *outp, size_t nbytes,
cf_blockwise_out_fn process, void *ctx)
{
const uint8_t *inb = inp;
uint8_t *outb = outp;
uassert(partial && *npartial < nblock);
uassert(inp || !nbytes);
uassert(process && ctx);
while (nbytes) {
/* If we're out of material, and need more, produce a block. */
if (*npartial == 0) {
process(ctx, partial);
*npartial = nblock;
}
size_t offset = nblock - *npartial;
size_t taken = MIN(*npartial, nbytes);
xor_bb(outb, inb, partial + offset, taken);
*npartial -= taken;
nbytes -= taken;
outb += taken;
inb += taken;
}
}
void cf_blockwise_acc_byte(uint8_t *partial, size_t *npartial, size_t nblock,
uint8_t byte, size_t nbytes,
cf_blockwise_in_fn process, void *ctx)
{
/* only memset the whole of the block once */
int filled = 0;
while (nbytes) {
size_t start = *npartial;
size_t count = MIN(nbytes, nblock - start);
if (!filled)
memset(partial + start, byte, count);
if (start == 0 && count == nblock)
filled = 1;
if (start + count == nblock) {
process(ctx, partial);
*npartial = 0;
} else {
*npartial += count;
}
nbytes -= count;
}
}
void cf_blockwise_acc_pad(uint8_t *partial, size_t *npartial, size_t nblock,
uint8_t fbyte, uint8_t mbyte, uint8_t lbyte,
size_t nbytes, cf_blockwise_in_fn process, void *ctx)
{
switch (nbytes) {
case 0:
break;
case 1:
fbyte ^= lbyte;
cf_blockwise_accumulate(partial, npartial, nblock, &fbyte, 1,
process, ctx);
break;
case 2:
cf_blockwise_accumulate(partial, npartial, nblock, &fbyte, 1,
process, ctx);
cf_blockwise_accumulate(partial, npartial, nblock, &lbyte, 1,
process, ctx);
break;
default:
cf_blockwise_accumulate(partial, npartial, nblock, &fbyte, 1,
process, ctx);
/* If the middle and last bytes differ, then process the last
* byte separately. Otherwise, just extend the middle block
* size. */
if (lbyte != mbyte) {
cf_blockwise_acc_byte(partial, npartial, nblock, mbyte,
nbytes - 2, process, ctx);
cf_blockwise_accumulate(partial, npartial, nblock,
&lbyte, 1, process, ctx);
} else {
cf_blockwise_acc_byte(partial, npartial, nblock, mbyte,
nbytes - 1, process, ctx);
}
break;
}
}

View file

@ -1,26 +0,0 @@
/*
* cifra - embedded cryptography library
* Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include "chash.h"
#include "handy.h"
void cf_hash(const cf_chash *h, const void *m, size_t nm, uint8_t *out)
{
cf_chash_ctx ctx;
uassert(h);
h->init(&ctx);
h->update(&ctx, m, nm);
h->digest(&ctx, out);
mem_clean(&ctx, sizeof ctx);
}

View file

@ -1,101 +0,0 @@
/*
* cifra - embedded cryptography library
* Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include "hmac.h"
#include "chash.h"
#include "bitops.h"
#include "handy.h"
#include <asm/string.h>
void cf_hmac_init(cf_hmac_ctx *ctx, const cf_chash *hash, const uint8_t *key,
size_t nkey)
{
uassert(ctx);
uassert(hash);
mem_clean(ctx, sizeof *ctx);
ctx->hash = hash;
/* Prepare key: */
uint8_t k[CF_CHASH_MAXBLK];
/* Shorten long keys. */
if (nkey > hash->blocksz) {
/* Standard doesn't cover case where blocksz < hashsz.
* FIPS186-1 seems to want to append a negative number of zero
* bytes. In any case, we only have a k buffer of
* CF_CHASH_MAXBLK! */
uassert(hash->hashsz <= hash->blocksz);
cf_hash(hash, key, nkey, k);
key = k;
nkey = hash->hashsz;
}
/* Right zero-pad short keys. */
if (k != key)
memcpy(k, key, nkey);
if (hash->blocksz > nkey)
memset(k + nkey, 0, hash->blocksz - nkey);
/* Start inner hash computation */
uint8_t blk[CF_CHASH_MAXBLK];
xor_b8(blk, k, 0x36, hash->blocksz);
hash->init(&ctx->inner);
hash->update(&ctx->inner, blk, hash->blocksz);
/* And outer. */
xor_b8(blk, k, 0x5c, hash->blocksz);
hash->init(&ctx->outer);
hash->update(&ctx->outer, blk, hash->blocksz);
mem_clean(blk, sizeof blk);
mem_clean(k, sizeof k);
}
void cf_hmac_update(cf_hmac_ctx *ctx, const void *data, size_t ndata)
{
uassert(ctx && ctx->hash);
ctx->hash->update(&ctx->inner, data, ndata);
}
void cf_hmac_finish(cf_hmac_ctx *ctx, uint8_t *out)
{
uassert(ctx && ctx->hash);
uassert(out);
uint8_t innerh[CF_MAXHASH];
ctx->hash->digest(&ctx->inner, innerh);
ctx->hash->update(&ctx->outer, innerh, ctx->hash->hashsz);
ctx->hash->digest(&ctx->outer, out);
mem_clean(ctx, sizeof *ctx);
}
void cf_hmac(const uint8_t *key, size_t nkey, const uint8_t *msg, size_t nmsg,
uint8_t *out, const cf_chash *hash)
{
cf_hmac_ctx ctx;
uassert(out);
uassert(hash);
cf_hmac_init(&ctx, hash, key, nkey);
cf_hmac_update(&ctx, msg, nmsg);
cf_hmac_finish(&ctx, out);
}

File diff suppressed because it is too large Load diff

View file

@ -1,216 +0,0 @@
/*
* cifra - embedded cryptography library
* Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <linux/types.h>
#include <asm/posix_types.h>
#include <asm/string.h>
#include "sha2.h"
#include "blockwise.h"
#include "bitops.h"
#include "handy.h"
static const uint32_t K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#define BSIG0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))
#define BSIG1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))
#define SSIG0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))
#define SSIG1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))
void cf_sha256_init(cf_sha256_context *ctx)
{
memset(ctx, 0, sizeof *ctx);
ctx->H[0] = 0x6a09e667;
ctx->H[1] = 0xbb67ae85;
ctx->H[2] = 0x3c6ef372;
ctx->H[3] = 0xa54ff53a;
ctx->H[4] = 0x510e527f;
ctx->H[5] = 0x9b05688c;
ctx->H[6] = 0x1f83d9ab;
ctx->H[7] = 0x5be0cd19;
}
void cf_sha224_init(cf_sha256_context *ctx)
{
memset(ctx, 0, sizeof *ctx);
ctx->H[0] = 0xc1059ed8;
ctx->H[1] = 0x367cd507;
ctx->H[2] = 0x3070dd17;
ctx->H[3] = 0xf70e5939;
ctx->H[4] = 0xffc00b31;
ctx->H[5] = 0x68581511;
ctx->H[6] = 0x64f98fa7;
ctx->H[7] = 0xbefa4fa4;
}
static void sha256_update_block(void *vctx, const uint8_t *inp)
{
cf_sha256_context *ctx = vctx;
/* This is a 16-word window into the whole W array. */
uint32_t W[16];
uint32_t a = ctx->H[0], b = ctx->H[1], c = ctx->H[2], d = ctx->H[3],
e = ctx->H[4], f = ctx->H[5], g = ctx->H[6], h = ctx->H[7], Wt;
for (size_t t = 0; t < 64; t++) {
/* For W[0..16] we process the input into W.
* For W[16..64] we compute the next W value:
*
* W[t] = SSIG1(W[t - 2]) + W[t - 7] + SSIG0(W[t - 15]) + W[t -
* 16];
*
* But all W indices are reduced mod 16 into our window.
*/
if (t < 16) {
W[t] = Wt = read32_be(inp);
inp += 4;
} else {
Wt = SSIG1(W[(t - 2) % 16]) + W[(t - 7) % 16] +
SSIG0(W[(t - 15) % 16]) + W[(t - 16) % 16];
W[t % 16] = Wt;
}
uint32_t T1 = h + BSIG1(e) + CH(e, f, g) + K[t] + Wt;
uint32_t T2 = BSIG0(a) + MAJ(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
}
ctx->H[0] += a;
ctx->H[1] += b;
ctx->H[2] += c;
ctx->H[3] += d;
ctx->H[4] += e;
ctx->H[5] += f;
ctx->H[6] += g;
ctx->H[7] += h;
ctx->blocks++;
}
void cf_sha256_update(cf_sha256_context *ctx, const void *data, size_t nbytes)
{
cf_blockwise_accumulate(ctx->partial, &ctx->npartial,
sizeof ctx->partial, data, nbytes,
sha256_update_block, ctx);
}
void cf_sha224_update(cf_sha256_context *ctx, const void *data, size_t nbytes)
{
cf_sha256_update(ctx, data, nbytes);
}
void cf_sha256_digest(const cf_sha256_context *ctx,
uint8_t hash[CF_SHA256_HASHSZ])
{
/* We copy the context, so the finalisation doesn't effect the caller's
* context. This means the caller can do:
*
* x = init()
* x.update('hello')
* h1 = x.digest()
* x.update(' world')
* h2 = x.digest()
*
* to get h1 = H('hello') and h2 = H('hello world')
*
* This wouldn't work if we applied MD-padding to *ctx.
*/
cf_sha256_context ours = *ctx;
cf_sha256_digest_final(&ours, hash);
}
void cf_sha256_digest_final(cf_sha256_context *ctx,
uint8_t hash[CF_SHA256_HASHSZ])
{
uint64_t digested_bytes = ctx->blocks;
digested_bytes = digested_bytes * CF_SHA256_BLOCKSZ + ctx->npartial;
uint64_t digested_bits = digested_bytes * 8;
size_t padbytes =
CF_SHA256_BLOCKSZ - ((digested_bytes + 8) % CF_SHA256_BLOCKSZ);
/* Hash 0x80 00 ... block first. */
cf_blockwise_acc_pad(ctx->partial, &ctx->npartial, sizeof ctx->partial,
0x80, 0x00, 0x00, padbytes, sha256_update_block,
ctx);
/* Now hash length. */
uint8_t buf[8];
write64_be(digested_bits, buf);
cf_sha256_update(ctx, buf, 8);
/* We ought to have got our padding calculation right! */
uassert(ctx->npartial == 0);
write32_be(ctx->H[0], hash + 0);
write32_be(ctx->H[1], hash + 4);
write32_be(ctx->H[2], hash + 8);
write32_be(ctx->H[3], hash + 12);
write32_be(ctx->H[4], hash + 16);
write32_be(ctx->H[5], hash + 20);
write32_be(ctx->H[6], hash + 24);
write32_be(ctx->H[7], hash + 28);
memset(ctx, 0, sizeof *ctx);
}
void cf_sha224_digest(const cf_sha256_context *ctx,
uint8_t hash[CF_SHA224_HASHSZ])
{
uint8_t full[CF_SHA256_HASHSZ];
cf_sha256_digest(ctx, full);
memcpy(hash, full, CF_SHA224_HASHSZ);
}
void cf_sha224_digest_final(cf_sha256_context *ctx,
uint8_t hash[CF_SHA224_HASHSZ])
{
uint8_t full[CF_SHA256_HASHSZ];
cf_sha256_digest_final(ctx, full);
memcpy(hash, full, CF_SHA224_HASHSZ);
}
const cf_chash cf_sha224 = {.hashsz = CF_SHA224_HASHSZ,
.blocksz = CF_SHA256_BLOCKSZ,
.init = (cf_chash_init)cf_sha224_init,
.update = (cf_chash_update)cf_sha224_update,
.digest = (cf_chash_digest)cf_sha224_digest};
const cf_chash cf_sha256 = {.hashsz = CF_SHA256_HASHSZ,
.blocksz = CF_SHA256_BLOCKSZ,
.init = (cf_chash_init)cf_sha256_init,
.update = (cf_chash_update)cf_sha256_update,
.digest = (cf_chash_digest)cf_sha256_digest};

View file

@ -1,130 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#include <nanotls-common.h>
#include <asm/string.h>
#include "uassert.h"
enum ecode nanotls_mem_eq(const void *a, const void *b, size_t size)
{
const volatile uint8_t *va = a;
const volatile uint8_t *vb = b;
uint8_t diff = 0;
while (size--) {
diff |= *va++ ^ *vb++;
}
return (diff == 0) ? E_OK : E_INVALID;
}
void nanotls_mem_erase(void *buf, size_t size)
{
memset(buf, 0, size);
}
void nanotls_hkdf_sha256_extract(const void *salt, size_t salt_len,
const void *ikm, size_t ikm_len,
uint8_t prk_result[NANOTLS_SHA256_HASH_SIZE])
{
if (salt_len > 0) {
nanotls_hmac_sha256(salt, salt_len, ikm, ikm_len, prk_result);
} else {
uint8_t zeroes[NANOTLS_SHA256_HASH_SIZE] = {0};
nanotls_hmac_sha256(zeroes, NANOTLS_SHA256_HASH_SIZE, ikm,
ikm_len, prk_result);
}
}
void nanotls_hkdf_expand_label(const uint8_t secret[NANOTLS_SHA256_HASH_SIZE],
const void *label, size_t label_size,
const uint8_t *context, size_t context_size,
void *output, size_t output_length)
{
uint8_t hmac_out[NANOTLS_SHA256_HASH_SIZE];
nanotls_hmac_ctx hmac_ctx;
const uint16_t length = output_length;
const uint8_t one = 0x01;
uassert(length <= NANOTLS_SHA256_HASH_SIZE);
nanotls_hmac_sha256_init(&hmac_ctx, secret, NANOTLS_SHA256_HASH_SIZE);
nanotls_hmac_sha256_update(&hmac_ctx, &length, sizeof(length));
nanotls_hmac_sha256_update(&hmac_ctx, label, label_size);
if (context && context_size > 0)
nanotls_hmac_sha256_update(&hmac_ctx, context, context_size);
nanotls_hmac_sha256_update(&hmac_ctx, &one, sizeof(one));
nanotls_hmac_sha256_finish(&hmac_ctx, hmac_out);
memcpy(output, hmac_out, output_length);
nanotls_mem_erase(hmac_out, sizeof(hmac_out));
}
void nanotls_write_finished(const nanotls_sha_ctx *handshake_hash,
const uint8_t hs_traffic_secret[NANOTLS_SECRET_SIZE],
uint8_t verify_data[NANOTLS_SHA256_HASH_SIZE])
{
uint8_t transcript_hash[NANOTLS_SHA256_HASH_SIZE];
uint8_t finished_key[NANOTLS_SHA256_HASH_SIZE]; /* HMAC key (not AES),
hence it's bigger */
/* Calculate transcript hash (transcript_hash) */
nanotls_sha256_digest(handshake_hash, transcript_hash);
/*
* Derive "Finished" key
* finished_key = HKDF-Expand-Label(hs_traffic_secret, "finished", "",
* Hash.length)
*/
nanotls_hkdf_expand_label_noctx(hs_traffic_secret,
NANOTLS_FINISHED_LABEL, finished_key,
sizeof(finished_key));
/*
* Create Finished.verify_data
* verify_data = HMAC(finished_key, transcript_hash)
*/
nanotls_hmac_sha256(finished_key, sizeof(finished_key), transcript_hash,
sizeof(transcript_hash), verify_data);
nanotls_mem_erase(finished_key, sizeof(finished_key));
}
void nanotls_derive_key_iv(uint8_t key[NANOTLS_KEY_SIZE],
uint8_t iv[NANOTLS_IV_SIZE],
const uint8_t secret[NANOTLS_SECRET_SIZE])
{
/* key = HKDF-Expand-Label(secret, "key", "") */
nanotls_hkdf_expand_label_noctx(secret, NANOTLS_KEYING_KEY_LABEL, key,
NANOTLS_KEY_SIZE);
/* iv = HKDF-Expand-Label(secret, "iv", "") */
nanotls_hkdf_expand_label_noctx(secret, NANOTLS_KEYING_IV_LABEL, iv,
NANOTLS_IV_SIZE);
}

View file

@ -1,380 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#include <nanotls-data.h>
#include <nanotls-hooks.h>
#include <asm/string.h>
#include <uassert.h>
static enum ecode nanotls_data_check_ctx(const struct nanotls_ctx_data *ctx)
{
if (ctx->role != NANOTLS_ROLE_HOST && ctx->role != NANOTLS_ROLE_DEVICE)
return E_INVALID;
return E_OK;
}
/* NONCE = IV ^ SEQ_NO */
static inline void nanotls_data_get_nonce(uint8_t nonce[NANOTLS_AES_IV_SIZE],
const uint8_t iv[NANOTLS_AES_IV_SIZE],
uint64_t seq_no)
{
size_t offset = sizeof(seq_no);
uint64_t tmp;
memcpy(&tmp, iv, sizeof(tmp));
tmp ^= seq_no;
memcpy(nonce, &tmp, sizeof(tmp));
memcpy(nonce + offset, iv + offset, NANOTLS_AES_IV_SIZE - offset);
}
enum ecode nanotls_data_ctx_init(struct nanotls_ctx_data *ctx,
enum nanotls_role role,
const struct nanotls_traffic_keys *keys)
{
const uint8_t *enc_key = NULL;
const uint8_t *dec_key = NULL;
enum ecode ret;
memset(ctx, 0, sizeof(*ctx));
ctx->role = role;
switch (role) {
case NANOTLS_ROLE_HOST:
memcpy(ctx->enc_iv, keys->h2d_iv, sizeof(ctx->enc_iv));
memcpy(ctx->dec_iv, keys->d2h_iv, sizeof(ctx->dec_iv));
#ifdef NANOTLS_CONFIG_KEY_UPDATE
memcpy(ctx->enc_secret, keys->h2d_secret,
sizeof(ctx->enc_secret));
memcpy(ctx->dec_secret, keys->d2h_secret,
sizeof(ctx->dec_secret));
#endif
enc_key = keys->h2d_key;
dec_key = keys->d2h_key;
break;
case NANOTLS_ROLE_DEVICE:
memcpy(ctx->enc_iv, keys->d2h_iv, sizeof(ctx->enc_iv));
memcpy(ctx->dec_iv, keys->h2d_iv, sizeof(ctx->dec_iv));
#ifdef NANOTLS_CONFIG_KEY_UPDATE
memcpy(ctx->enc_secret, keys->d2h_secret,
sizeof(ctx->enc_secret));
memcpy(ctx->dec_secret, keys->h2d_secret,
sizeof(ctx->dec_secret));
#endif
enc_key = keys->d2h_key;
dec_key = keys->h2d_key;
break;
default:
return E_INVALID;
}
ret = nanotls_aes_ctx_init(&ctx->aes_enc, enc_key);
if (ret != E_OK)
return ret;
ret = nanotls_aes_ctx_init(&ctx->aes_dec, dec_key);
if (ret != E_OK)
return ret;
ctx->dec_tag_mismatch_count = 0;
return E_OK;
}
enum ecode nanotls_data_ctx_destroy(struct nanotls_ctx_data *ctx)
{
enum ecode ret;
ret = nanotls_aes_ctx_destroy(&ctx->aes_enc);
if (ret != E_OK)
return ret;
ret = nanotls_aes_ctx_destroy(&ctx->aes_dec);
if (ret != E_OK)
return ret;
memset(ctx, 0, sizeof(*ctx));
ctx->role = NANOTLS_ROLE_UNKNOWN;
return E_OK;
}
enum ecode nanotls_data_write_start(struct nanotls_ctx_data *ctx,
size_t data_size)
{
struct nanotls_data_add add;
uint8_t nonce[NANOTLS_AES_IV_SIZE];
enum ecode ret;
ret = nanotls_data_check_ctx(ctx);
if (ret != E_OK)
return ret;
ret = nanotls_data_add_init(&add, data_size);
if (ret != E_OK)
return ret;
nanotls_data_get_nonce(nonce, ctx->enc_iv, ctx->enc_seq_no);
ret = nanotls_aes_start_enc(&ctx->aes_enc, nonce, &add, sizeof(add));
if (ret != E_OK)
return ret;
ctx->enc_len_remaining = data_size;
return E_OK;
}
enum ecode nanotls_data_write_update(struct nanotls_ctx_data *ctx,
void *enc_out, const void *plain_in,
size_t data_size)
{
enum ecode ret;
ret = nanotls_data_check_ctx(ctx);
if (ret != E_OK)
return ret;
if (data_size > ctx->enc_len_remaining)
return E_INVALID;
ret = nanotls_aes_update(&ctx->aes_enc, enc_out, plain_in, data_size);
if (ret != E_OK)
return ret;
ctx->enc_len_remaining -= data_size;
return E_OK;
}
enum ecode nanotls_data_write_finish(struct nanotls_ctx_data *ctx,
uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE])
{
enum ecode ret;
ret = nanotls_data_check_ctx(ctx);
if (ret != E_OK)
return ret;
if (ctx->enc_len_remaining > 0)
return E_INVALID;
ret = nanotls_aes_finish(&ctx->aes_enc, tag);
if (ret != E_OK)
return ret;
ctx->enc_seq_no++;
uassert(ctx->enc_seq_no != 0); /* should never happen */
return E_OK;
}
enum ecode nanotls_data_write(struct nanotls_ctx_data *ctx, void *enc_out,
const void *plain_in, size_t data_size,
uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE])
{
enum ecode ret;
ret = nanotls_data_write_start(ctx, data_size);
if (ret != E_OK)
return ret;
ret = nanotls_data_write_update(ctx, enc_out, plain_in, data_size);
if (ret != E_OK)
return ret;
return nanotls_data_write_finish(ctx, tag);
}
enum ecode nanotls_data_read_start(struct nanotls_ctx_data *ctx,
size_t data_size)
{
struct nanotls_data_add add;
uint8_t nonce[NANOTLS_AES_IV_SIZE];
enum ecode ret;
ret = nanotls_data_check_ctx(ctx);
if (ret != E_OK)
return ret;
ret = nanotls_data_add_init(&add, data_size);
if (ret != E_OK)
return ret;
nanotls_data_get_nonce(nonce, ctx->dec_iv, ctx->dec_seq_no);
ret = nanotls_aes_start_dec(&ctx->aes_dec, nonce, &add, sizeof(add));
if (ret != E_OK)
return ret;
ctx->dec_len_remaining = data_size;
return E_OK;
}
enum ecode nanotls_data_read_update(struct nanotls_ctx_data *ctx,
void *plain_out, const void *enc_in,
size_t data_size)
{
enum ecode ret;
ret = nanotls_data_check_ctx(ctx);
if (ret != E_OK)
return ret;
if (data_size > ctx->dec_len_remaining)
return E_INVALID;
ret = nanotls_aes_update(&ctx->aes_dec, plain_out, enc_in, data_size);
if (ret != E_OK)
return ret;
ctx->dec_len_remaining -= data_size;
return E_OK;
}
enum ecode nanotls_data_read_finish(struct nanotls_ctx_data *ctx,
const uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE])
{
uint8_t computed_tag[NANOTLS_AES_GCM_TAG_SIZE];
enum ecode ret;
ret = nanotls_data_check_ctx(ctx);
if (ret != E_OK)
return ret;
if (ctx->dec_len_remaining > 0)
return E_INVALID;
ret = nanotls_aes_finish(&ctx->aes_dec, computed_tag);
if (ret != E_OK)
return ret;
ret = nanotls_mem_eq(computed_tag, tag, NANOTLS_AES_GCM_TAG_SIZE);
if (ret != E_OK) {
ctx->dec_tag_mismatch_count++;
#ifdef NANOTLS_CONFIG_MAX_DATA_TAG_MISMATCH_CNT
if (ctx->dec_tag_mismatch_count >
NANOTLS_CONFIG_MAX_DATA_TAG_MISMATCH_CNT)
nanotls_data_ctx_destroy(ctx);
#endif
return ret;
}
ctx->dec_seq_no++;
uassert(ctx->dec_seq_no != 0); /* should never happen */
ctx->dec_tag_mismatch_count = 0;
return E_OK;
}
enum ecode nanotls_data_read(struct nanotls_ctx_data *ctx, void *plain_out,
const void *enc_in, size_t data_size,
const uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE])
{
enum ecode ret;
ret = nanotls_data_read_start(ctx, data_size);
if (ret != E_OK)
return ret;
ret = nanotls_data_read_update(ctx, plain_out, enc_in, data_size);
if (ret != E_OK)
return ret;
return nanotls_data_read_finish(ctx, tag);
}
#ifdef NANOTLS_CONFIG_KEY_UPDATE
static enum ecode nanotls_data_key_update(nanotls_aes_context *aes_ctx,
uint8_t iv[NANOTLS_IV_SIZE],
uint8_t secret[NANOTLS_SECRET_SIZE])
{
uint8_t key[NANOTLS_KEY_SIZE];
enum ecode ret;
ret = nanotls_update_traffic_secret(secret, key, iv);
if (ret != E_OK)
return ret;
ret = nanotls_aes_ctx_destroy(aes_ctx);
if (ret != E_OK)
return ret;
ret = nanotls_aes_ctx_init(aes_ctx, key);
if (ret != E_OK)
return ret;
nanotls_mem_erase(key, sizeof(key));
return E_OK;
}
enum ecode nanotls_data_write_key_update(struct nanotls_ctx_data *ctx)
{
uint8_t old_iv[NANOTLS_IV_SIZE];
enum ecode ret;
memcpy(old_iv, ctx->enc_iv, sizeof(old_iv));
ret = nanotls_data_key_update(&ctx->aes_enc, ctx->enc_iv,
ctx->enc_secret);
if (ret != E_OK)
return ret;
/* old IV and new IV should not match */
uassert(nanotls_mem_eq(old_iv, ctx->enc_iv, sizeof(old_iv)) != E_OK);
ctx->enc_seq_no = 0;
return E_OK;
}
enum ecode nanotls_data_read_key_update(struct nanotls_ctx_data *ctx)
{
uint8_t old_iv[NANOTLS_IV_SIZE];
enum ecode ret;
memcpy(old_iv, ctx->dec_iv, sizeof(old_iv));
ret = nanotls_data_key_update(&ctx->aes_dec, ctx->dec_iv,
ctx->dec_secret);
if (ret != E_OK)
return ret;
/* old IV and new IV should not match */
uassert(nanotls_mem_eq(old_iv, ctx->dec_iv, sizeof(old_iv)) != E_OK);
ctx->dec_seq_no = 0;
return E_OK;
}
#endif

View file

@ -1,374 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#include <nanotls-device.h>
#include <nanotls-common.h>
#include <nanotls-host.h>
#include <p256-m.h>
static enum ecode nanotls_device_handle_error(struct nanotls_device_ctx *ctx,
enum ecode ret)
{
ctx->error_count++;
#ifdef NANOTLS_CONFIG_MAX_ERR_CNT
if (ctx->error_count >= NANOTLS_CONFIG_MAX_ERR_CNT) {
nanotls_device_cleanup(ctx);
ctx->state = NANOTLS_DEV_ERR;
}
#endif
return ret;
}
enum ecode
nanotls_device_init(struct nanotls_device_ctx *ctx,
const uint8_t ecdsa_priv[NANOTLS_ECDSA_PRIVATE_KEY_SIZE],
const struct nanotls_device_info *device_info)
{
enum ecode ret;
ret = nanotls_secure_random(ctx, sizeof(*ctx));
if (ret != E_OK)
return ret;
nanotls_sha256_init(&ctx->handshake_hash);
memcpy(ctx->ecdsa_priv, ecdsa_priv, sizeof(ctx->ecdsa_priv));
if (device_info) {
memcpy(&ctx->device_info, device_info,
sizeof(ctx->device_info));
}
ctx->error_count = 0;
ctx->state = NANOTLS_DEV_INIT;
return E_OK;
}
enum ecode nanotls_device_host_hello_rcvd(struct nanotls_device_ctx *ctx,
const struct nanotls_host_hello *msg)
{
enum ecode ret = E_INVALID;
if (ctx->state != NANOTLS_DEV_INIT)
return nanotls_device_handle_error(ctx, E_INVALID);
ret = nanotls_hdr_check(&msg->hdr, sizeof(*msg), NANOTLS_HOST_HELLO);
if (ret != E_OK)
return nanotls_device_handle_error(ctx, ret);
/* Only support a single crypto algorithm combination for now */
if (msg->sig_alg != NANOTLS_ECDSA_SECP256R1_SHA256)
return nanotls_device_handle_error(ctx, E_INVALID);
if (msg->key_exchange_type != NANOTLS_ECDHE_SECP256R1)
return nanotls_device_handle_error(ctx, E_INVALID);
if (msg->cipher_suite != NANOTLS_AES_128_GCM_SHA256)
return nanotls_device_handle_error(ctx, E_INVALID);
/* Feed the entire HostHello to the transcript hash context */
nanotls_sha256_update(&ctx->handshake_hash, msg, sizeof(*msg));
/* Save Host's ECDHE key share (ephemeral public key) */
ctx->host_key_share = msg->host_key_share;
ctx->state = NANOTLS_DEV_HELLO_RCVD;
return E_OK;
}
static enum ecode
nanotls_device_hello_sign(const struct nanotls_device_ctx *ctx,
struct nanotls_device_hello *msg)
{
uint8_t hash_sig[NANOTLS_SHA256_HASH_SIZE];
/* Produce the hash value to sign, as defined in TLS */
nanotls_handshake_sig_hash(ctx->handshake_h2_hash,
NANOTLS_DEVICE_HS_SIG_LABEL, hash_sig);
return nanotls_p256_ecdsa_sign(msg->enc.device_handshake_sig.sig,
ctx->ecdsa_priv, hash_sig,
sizeof(hash_sig));
}
static enum ecode
nanotls_device_derive_hs_secrets(struct nanotls_device_ctx *ctx,
uint8_t hs_traffic_secret[NANOTLS_SECRET_SIZE])
{
uint8_t shared_secret[NANOTLS_SECRET_SIZE];
enum ecode ret = E_INVALID;
/* Compute ECDHE shared secret */
ret = nanotls_p256_ecdh_shared_secret(shared_secret, ctx->ecdh_priv,
ctx->host_key_share.pubkey);
if (ret != E_OK)
return ret;
/* Erase ECDHE private key */
nanotls_mem_erase(ctx->ecdh_priv, sizeof(ctx->ecdh_priv));
nanotls_mem_erase(ctx->host_key_share.pubkey,
sizeof(ctx->host_key_share.pubkey));
/*
* handshake_secret = HKDF-Extract(salt = "", ikm = ecdh_shared_secret)
*
* A deviation from TLS is that it takes a different fixed value of salt
* since we don't support PSK, we just use empty hash as fixed value for
* simplification (one fixed value vs another one).
*/
nanotls_hkdf_sha256_extract(NULL, 0, shared_secret,
sizeof(shared_secret),
ctx->handshake_secret);
/* Erase hared secret as it's no longer needed */
nanotls_mem_erase(shared_secret, sizeof(shared_secret));
/*
* Derive handshake traffic secret with Derive-Secret (HKDF-Expand)
* hs_traffic_secret = Derive-Secret(handshake_secret, label, hash) =
* = HKDF-Expand(handshake_secret, 32 | label |
* transcript_hash, 32)
*/
nanotls_hkdf_derive_secret(ctx->handshake_secret,
NANOTLS_DEVICE_HS_TS_LABEL,
ctx->handshake_h2_hash, hs_traffic_secret);
return E_OK;
}
enum ecode nanotls_device_do_hello(struct nanotls_device_ctx *ctx,
struct nanotls_device_hello *msg)
{
const size_t sig_cutoff_pos = NANOTLS_DEVICE_HELLO_SIG_CUTOFF_POS;
uint8_t dev_hs_traffic_secret[NANOTLS_SECRET_SIZE];
enum ecode ret = E_INVALID;
if (ctx->state != NANOTLS_DEV_HELLO_RCVD)
return nanotls_device_handle_error(ctx, E_INVALID);
memset(msg, 0, sizeof(*msg));
nanotls_hdr_init(&msg->hdr, sizeof(*msg), NANOTLS_DEVICE_HELLO);
/* Fill in the Device Info */
memcpy(&msg->enc.device_info, &ctx->device_info,
sizeof(msg->enc.device_info));
/*
* Device's nonce - used for optional host authentication by device,
* replay protection, and MiTM attack protection.
*/
ret = nanotls_secure_random(msg->random, sizeof(msg->random));
if (ret != E_OK)
return nanotls_device_handle_error(ctx, ret);
/* Device's ECDHE key share (ephemeral public key) */
ret = nanotls_p256_gen_keypair(ctx->ecdh_priv,
msg->device_key_share.pubkey);
if (ret != E_OK)
return nanotls_device_handle_error(ctx, ret);
/*
* [H2]
* Feed the DeviceHello content up until the handshake_sig to transcript
* hash context, handshake_sig (aka CertificateVerify) is yet to
* be generated using this transcript hash.
*/
nanotls_sha256_update(&ctx->handshake_hash, msg, sig_cutoff_pos);
nanotls_sha256_digest(&ctx->handshake_hash,
ctx->handshake_h2_hash); /* [H2] */
/*
* All ECDHE steps have been completed, now calculate ECDHE shared
* secret and derive Handshake Secrets from it.
*/
ret = nanotls_device_derive_hs_secrets(ctx, dev_hs_traffic_secret);
if (ret != E_OK)
return nanotls_device_handle_error(ctx, ret);
/*
* Generate handshake_sig (aka CertificateVerify)
* using Device's private key and handshake context (H2).
*/
ret = nanotls_device_hello_sign(ctx, msg);
if (ret != E_OK)
return nanotls_device_handle_error(ctx, ret);
/* Erase the ECDSA private key as it's not needed anymore */
nanotls_mem_erase(ctx->ecdsa_priv, sizeof(ctx->ecdsa_priv));
/* [H7] Update handshake transcript with the part before Finished */
nanotls_sha256_update(&ctx->handshake_hash, &msg->enc,
NANOTLS_DEVICE_HELLO_FINISHED_ENC_CUTOFF_POS);
/* Produce a device_finished message (MAC over transcript hash). */
nanotls_write_finished(&ctx->handshake_hash, dev_hs_traffic_secret,
msg->enc.device_finished.verify_data);
/* [H3] Update handshake transcript with just created device_finished */
nanotls_sha256_update(&ctx->handshake_hash, &msg->enc.device_finished,
sizeof(msg->enc.device_finished));
/* Encrypt device signature and Finished */
nanotls_handshake_encrypt(&msg->enc, sizeof(msg->enc), &msg->auth_tag,
dev_hs_traffic_secret);
nanotls_mem_erase(dev_hs_traffic_secret, sizeof(dev_hs_traffic_secret));
ctx->state = NANOTLS_DEV_HELLO_SENT;
return E_OK;
}
static enum ecode nanotls_device_verify_host_finished(
const struct nanotls_device_ctx *ctx,
const struct nanotls_host_finished *msg,
const uint8_t host_hs_traffic_secret[NANOTLS_SECRET_SIZE])
{
uint8_t verify_data[NANOTLS_SHA256_HASH_SIZE];
nanotls_write_finished(&ctx->handshake_hash, host_hs_traffic_secret,
verify_data);
return nanotls_mem_eq(verify_data, msg->enc.host_finished.verify_data,
sizeof(verify_data));
}
enum ecode nanotls_device_host_finished_rcvd(struct nanotls_device_ctx *ctx,
struct nanotls_host_finished *msg)
{
const size_t finished_cutoff_pos =
NANOTLS_HOST_HELLO_FINISHED_CUTOFF_POS;
uint8_t host_hs_traffic_secret[NANOTLS_SECRET_SIZE];
enum ecode ret = E_INVALID;
if (ctx->state != NANOTLS_DEV_HELLO_SENT)
return nanotls_device_handle_error(ctx, E_INVALID);
ret = nanotls_hdr_check(&msg->hdr, sizeof(*msg), NANOTLS_HOST_FINISHED);
if (ret != E_OK)
return nanotls_device_handle_error(ctx, ret);
/*
* Derive handshake traffic secret with Derive-Secret (HKDF-Expand)
* hs_traffic_secret = Derive-Secret(handshake_secret, label, hash) =
* = HKDF-Expand(handshake_secret, 32 | label |
* transcript_hash, 32)
*/
nanotls_hkdf_derive_secret(ctx->handshake_secret,
NANOTLS_HOST_HS_TS_LABEL,
ctx->handshake_h2_hash,
host_hs_traffic_secret);
ret = nanotls_handshake_decrypt_verify(&msg->enc, sizeof(msg->enc),
&msg->auth_tag,
host_hs_traffic_secret);
if (ret != E_OK)
return nanotls_device_handle_error(ctx, ret);
/* [H9] Update handshake transcript with the starting part of
* HostFinished */
nanotls_sha256_update(&ctx->handshake_hash, msg, finished_cutoff_pos);
/*
* Verify host_finished message.
* It's a MAC over transcript hash computed using handshake secret
* computed by the Host.
*/
ret = nanotls_device_verify_host_finished(ctx, msg,
host_hs_traffic_secret);
if (ret != E_OK)
return nanotls_device_handle_error(ctx, ret);
/*
* [H4] - Full transcript hash
* Update handshake transcript with just processed host_finished
* H4 = SHA256(HostHello | DeviceHello | HostFinished)
*/
nanotls_sha256_update(&ctx->handshake_hash, &msg->enc.host_finished,
sizeof(msg->enc.host_finished));
/*
* At this point, handshake is completed from the Device perspective.
* The Device has everything to derive the final Master Secret.
*
* Now call nanotls_device_derive_traffic_keys() to get the encryption
* keys.
*/
nanotls_derive_master_secret(ctx->master_secret, ctx->handshake_secret);
nanotls_mem_erase(host_hs_traffic_secret,
sizeof(host_hs_traffic_secret));
ctx->state = NANOTLS_DEV_DONE;
return E_OK;
}
enum ecode nanotls_device_derive_traffic_keys_instance(
const struct nanotls_device_ctx *ctx, struct nanotls_traffic_keys *keys,
size_t instance_idx)
{
uint8_t transcript_hash[NANOTLS_SHA256_HASH_SIZE];
uint8_t instance_label_hash[NANOTLS_SHA256_HASH_SIZE];
if (ctx->state != NANOTLS_DEV_DONE)
return E_INVALID;
nanotls_hkdf_sha256_extract(&instance_idx, sizeof(instance_idx),
NANOTLS_KEY_INSTANCE_LABEL,
instance_label_hash);
nanotls_sha256_digest(&ctx->handshake_hash, transcript_hash); /* [H4] */
nanotls_derive_traffic_keys(ctx->master_secret, transcript_hash,
instance_label_hash,
sizeof(instance_label_hash), keys);
return E_OK;
}
enum ecode
nanotls_device_derive_traffic_keys(const struct nanotls_device_ctx *ctx,
struct nanotls_traffic_keys *keys)
{
uint8_t hash[NANOTLS_SHA256_HASH_SIZE];
if (ctx->state != NANOTLS_DEV_DONE)
return E_INVALID;
nanotls_sha256_digest(&ctx->handshake_hash, hash); /* [H4] */
nanotls_derive_traffic_keys(ctx->master_secret, hash, NULL, 0, keys);
return E_OK;
}
enum ecode nanotls_device_cleanup(struct nanotls_device_ctx *ctx)
{
nanotls_mem_erase(ctx, sizeof(*ctx));
ctx->state = NANOTLS_DEV_UNINIT;
return E_OK;
}

View file

@ -1,269 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#include <nanotls-hooks.h>
#include <asm/string.h>
#include <linux/random.h>
#include <p256-m.h>
#include "nanotls-common.h"
enum ecode nanotls_secure_random(void *buf, size_t size)
{
get_random_bytes(buf, size);
return E_OK;
}
enum ecode nanotls_p256_gen_keypair(uint8_t priv[32], uint8_t pub[64])
{
return (p256_gen_keypair(priv, pub) == P256_SUCCESS) ? E_OK : E_INVALID;
}
enum ecode nanotls_p256_ecdh_shared_secret(uint8_t secret[32],
const uint8_t priv[32],
const uint8_t pub[64])
{
return (p256_ecdh_shared_secret(secret, priv, pub) == P256_SUCCESS) ?
E_OK :
E_INVALID;
}
enum ecode nanotls_p256_ecdsa_sign(uint8_t sig[64], const uint8_t priv[32],
const uint8_t *hash, size_t hlen)
{
return (p256_ecdsa_sign(sig, priv, hash, hlen) == P256_SUCCESS) ?
E_OK :
E_INVALID;
}
enum ecode nanotls_p256_ecdsa_verify(const uint8_t sig[64],
const uint8_t pub[64], const uint8_t *hash,
size_t hlen)
{
return (p256_ecdsa_verify(sig, pub, hash, hlen) == P256_SUCCESS) ?
E_OK :
E_INVALID;
}
enum ecode nanotls_sha256_init(nanotls_sha_ctx *ctx)
{
cf_sha256_init(ctx);
return E_OK;
}
enum ecode nanotls_sha256_update(nanotls_sha_ctx *ctx, const void *buf,
size_t size)
{
cf_sha256_update(ctx, buf, size);
return E_OK;
}
enum ecode nanotls_sha256_digest(const nanotls_sha_ctx *ctx,
uint8_t hash[NANOTLS_SHA256_HASH_SIZE])
{
cf_sha256_digest(ctx, hash);
return E_OK;
}
enum ecode nanotls_sha256_digest_final(nanotls_sha_ctx *ctx,
uint8_t hash[NANOTLS_SHA256_HASH_SIZE])
{
cf_sha256_digest_final(ctx, hash);
return E_OK;
}
enum ecode nanotls_hmac_sha256_init(nanotls_hmac_ctx *ctx, const void *key,
size_t key_size)
{
cf_hmac_init(ctx, &cf_sha256, key, key_size);
return E_OK;
}
enum ecode nanotls_hmac_sha256_update(nanotls_hmac_ctx *ctx, const void *data,
size_t data_size)
{
cf_hmac_update(ctx, data, data_size);
return E_OK;
}
enum ecode nanotls_hmac_sha256_finish(nanotls_hmac_ctx *ctx,
uint8_t result[NANOTLS_SHA256_HASH_SIZE])
{
cf_hmac_finish(ctx, result);
return E_OK;
}
enum ecode nanotls_hmac_sha256(const void *key, size_t key_size,
const void *data, size_t data_size,
uint8_t result[NANOTLS_SHA256_HASH_SIZE])
{
nanotls_hmac_ctx ctx;
enum ecode ret = E_INVALID;
ret = nanotls_hmac_sha256_init(&ctx, key, key_size);
if (ret != E_OK)
return ret;
ret = nanotls_hmac_sha256_update(&ctx, data, data_size);
if (ret != E_OK)
return ret;
ret = nanotls_hmac_sha256_finish(&ctx, result);
if (ret != E_OK)
return ret;
return E_OK;
}
/*
* AES API - needed post-handshake.
* Only AES-GCM-128 is supported with fixed 12 byte IV size.
*/
enum ecode nanotls_aes_ctx_init(nanotls_aes_context *ctx,
const uint8_t key[NANOTLS_AES_KEY_SIZE])
{
int ret;
ret = gcm_setkey(ctx, key, NANOTLS_AES_KEY_SIZE);
return (ret == 0) ? E_OK : E_INVALID;
}
enum ecode nanotls_aes_start_enc(nanotls_aes_context *ctx,
const uint8_t iv[NANOTLS_AES_IV_SIZE],
const void *add, size_t add_size)
{
int ret;
ret = gcm_start(ctx, AES_MODE_ENCRYPT, iv, NANOTLS_AES_IV_SIZE, add,
add_size);
return (ret == 0) ? E_OK : E_INVALID;
}
enum ecode nanotls_aes_start_dec(nanotls_aes_context *ctx,
const uint8_t iv[NANOTLS_AES_IV_SIZE],
const void *add, size_t add_size)
{
int ret;
ret = gcm_start(ctx, AES_MODE_DECRYPT, iv, NANOTLS_AES_IV_SIZE, add,
add_size);
return (ret == 0) ? E_OK : E_INVALID;
}
enum ecode nanotls_aes_update(nanotls_aes_context *ctx, void *out,
const void *in, size_t data_size)
{
int ret;
ret = gcm_update(ctx, data_size, in, out);
return (ret == 0) ? E_OK : E_INVALID;
}
enum ecode nanotls_aes_finish(nanotls_aes_context *ctx,
uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE])
{
int ret;
ret = gcm_finish(ctx, tag, NANOTLS_AES_GCM_TAG_SIZE);
return (ret == 0) ? E_OK : E_INVALID;
}
enum ecode nanotls_aes_ctx_destroy(nanotls_aes_context *ctx)
{
nanotls_mem_erase(ctx, sizeof(*ctx));
ctx->mode = AES_MODE_NONE;
/* Not needed as we're erasing it already */
/* gcm_zero_ctx(ctx); */
return E_OK;
}
enum ecode nanotls_aes_encrypt(nanotls_aes_context *ctx, void *out_data,
const void *in_data, size_t data_size,
const uint8_t iv[NANOTLS_AES_IV_SIZE],
const void *add, size_t add_size,
uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE])
{
enum ecode ret;
ret = nanotls_aes_start_enc(ctx, iv, add, add_size);
if (ret != E_OK)
return ret;
ret = nanotls_aes_update(ctx, out_data, in_data, data_size);
if (ret != E_OK)
return ret;
ret = nanotls_aes_finish(ctx, tag);
if (ret != E_OK)
return ret;
return E_OK;
}
enum ecode nanotls_aes_decrypt(nanotls_aes_context *ctx, void *out_data,
const void *in_data, size_t data_size,
const uint8_t iv[NANOTLS_AES_IV_SIZE],
const void *add, size_t add_size,
uint8_t tag[NANOTLS_AES_GCM_TAG_SIZE])
{
enum ecode ret;
ret = nanotls_aes_start_dec(ctx, iv, add, add_size);
if (ret != E_OK)
return ret;
ret = nanotls_aes_update(ctx, out_data, in_data, data_size);
if (ret != E_OK)
return ret;
ret = nanotls_aes_finish(ctx, tag);
if (ret != E_OK)
return ret;
return E_OK;
}
/* Provide random API to P256 library (for ECDSA & ECDH) */
int p256_generate_random(uint8_t *output, unsigned output_size)
{
return (nanotls_secure_random(output, output_size) == E_OK) ? 0 : -1;
}
void p256_mem_zeroize(void *buf, size_t bytes)
{
nanotls_mem_erase(buf, bytes);
}

View file

@ -1,416 +0,0 @@
/*
* Copyright 2025 NXP
*
* NXP CONFIDENTIAL
* The source code contained or described herein and all documents related to
* the source code ("Materials") are owned by NXP, its
* suppliers and/or its licensors. Title to the Materials remains with NXP,
* its suppliers and/or its licensors. The Materials contain
* trade secrets and proprietary and confidential information of NXP, its
* suppliers and/or its licensors. The Materials are protected by worldwide
* copyright and trade secret laws and treaty provisions. No part of the
* Materials may be used, copied, reproduced, modified, published, uploaded,
* posted, transmitted, distributed, or disclosed in any way without NXP's prior
* express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* express and approved by NXP in writing.
*/
#include <nanotls-host.h>
#include <nanotls-common.h>
#include <asm/string.h>
#include <p256-m.h>
static enum ecode nanotls_host_handle_error(struct nanotls_host_ctx *ctx,
enum ecode ret)
{
ctx->error_count++;
#ifdef NANOTLS_CONFIG_MAX_ERR_CNT
if (ctx->error_count >= NANOTLS_CONFIG_MAX_ERR_CNT) {
nanotls_host_cleanup(ctx);
ctx->state = NANOTLS_HOST_ERR;
}
#endif
return ret;
}
enum ecode
nanotls_host_init(struct nanotls_host_ctx *ctx,
const uint8_t ecdsa_pub[NANOTLS_ECDSA_PUBLIC_KEY_SIZE])
{
enum ecode ret;
ret = nanotls_secure_random(ctx, sizeof(*ctx));
if (ret != E_OK)
return ret;
nanotls_sha256_init(&ctx->handshake_hash);
if (ecdsa_pub) {
memcpy(ctx->ecdsa_pub, ecdsa_pub, sizeof(ctx->ecdsa_pub));
}
ctx->error_count = 0;
ctx->state = NANOTLS_HOST_INIT;
ctx->pub_key_req_cb = NULL;
return E_OK;
}
enum ecode
nanotls_host_init_pubkey_cb(struct nanotls_host_ctx *ctx,
nanotls_host_pub_key_req_cb pub_key_req_cb)
{
enum ecode ret;
ret = nanotls_host_init(ctx, NULL);
if (ret != E_OK)
return ret;
ctx->pub_key_req_cb = pub_key_req_cb;
return E_OK;
}
enum ecode nanotls_host_do_hello(struct nanotls_host_ctx *ctx,
struct nanotls_host_hello *msg)
{
enum ecode ret = E_INVALID;
if (ctx->state != NANOTLS_HOST_INIT)
return nanotls_host_handle_error(ctx, E_INVALID);
memset(msg, 0, sizeof(*msg));
nanotls_hdr_init(&msg->hdr, sizeof(*msg), NANOTLS_HOST_HELLO);
msg->sig_alg = NANOTLS_ECDSA_SECP256R1_SHA256;
msg->key_exchange_type = NANOTLS_ECDHE_SECP256R1;
msg->cipher_suite = NANOTLS_AES_128_GCM_SHA256;
/*
* Host's nonce - used for device authentication by host,
* replay protection, and MiTM attack protection.
*/
ret = nanotls_secure_random(msg->random, sizeof(msg->random));
if (ret != E_OK)
return nanotls_host_handle_error(ctx, ret);
/* Host's ECDHE key share (ephemeral public key) */
ret = nanotls_p256_gen_keypair(ctx->ecdh_priv,
msg->host_key_share.pubkey);
if (ret != E_OK)
return nanotls_host_handle_error(ctx, ret);
/*
* Feed the entire HostHello to the transcript hash context.
* Important: our copy, hashed before sending (i.e. trusted).
*/
nanotls_sha256_update(&ctx->handshake_hash, msg, sizeof(*msg));
ctx->state = NANOTLS_HOST_HELLO_SENT;
return E_OK;
}
static enum ecode
nanotls_host_device_hello_sig_verify(const struct nanotls_host_ctx *ctx,
const struct nanotls_device_hello *msg)
{
uint8_t hash_sig[NANOTLS_HASH_SIZE];
/* Produce the hash value to sign, as defined in TLS */
nanotls_handshake_sig_hash(ctx->handshake_h2_hash,
NANOTLS_DEVICE_HS_SIG_LABEL, hash_sig);
return nanotls_p256_ecdsa_verify(msg->enc.device_handshake_sig.sig,
ctx->ecdsa_pub, hash_sig,
sizeof(hash_sig));
}
static enum ecode
nanotls_host_derive_hs_secrets(struct nanotls_host_ctx *ctx,
uint8_t hs_traffic_secret[NANOTLS_SECRET_SIZE])
{
uint8_t shared_secret[NANOTLS_SECRET_SIZE];
enum ecode ret = E_INVALID;
/* Compute ECDHE shared secret */
ret = nanotls_p256_ecdh_shared_secret(shared_secret, ctx->ecdh_priv,
ctx->device_key_share.pubkey);
if (ret != E_OK)
return ret;
/* Erase ECDHE keys */
nanotls_mem_erase(ctx->ecdh_priv, sizeof(ctx->ecdh_priv));
nanotls_mem_erase(ctx->device_key_share.pubkey,
sizeof(ctx->device_key_share.pubkey));
/*
* handshake_secret = HKDF-Extract(salt = "", ikm = ecdh_shared_secret)
*
* TLS deviation: it takes a different fixed value of salt
* since we don't support PSK, we just use empty hash as fixed value for
* simplification (one fixed value vs another one).
*/
nanotls_hkdf_sha256_extract(NULL, 0, shared_secret,
sizeof(shared_secret),
ctx->handshake_secret);
/* Erase hared secret as it's no longer needed */
nanotls_mem_erase(shared_secret, sizeof(shared_secret));
/*
* Derive handshake traffic secret with Derive-Secret (HKDF-Expand)
* hs_traffic_secret = Derive-Secret(handshake_secret, label, hash) =
* = HKDF-Expand(handshake_secret, 32 | label |
* transcript_hash, 32)
*/
nanotls_hkdf_derive_secret(ctx->handshake_secret,
NANOTLS_DEVICE_HS_TS_LABEL,
ctx->handshake_h2_hash, hs_traffic_secret);
return E_OK;
}
static enum ecode nanotls_host_verify_device_finished(
const struct nanotls_host_ctx *ctx,
const struct nanotls_device_hello *msg,
const uint8_t hs_traffic_secret[NANOTLS_SECRET_SIZE])
{
uint8_t verify_data[NANOTLS_SHA256_HASH_SIZE];
nanotls_write_finished(&ctx->handshake_hash, hs_traffic_secret,
verify_data);
return nanotls_mem_eq(verify_data, msg->enc.device_finished.verify_data,
sizeof(verify_data));
}
enum ecode nanotls_host_device_hello_rcvd(struct nanotls_host_ctx *ctx,
struct nanotls_device_hello *msg)
{
const size_t sig_cutoff_pos = NANOTLS_DEVICE_HELLO_SIG_CUTOFF_POS;
uint8_t dev_hs_traffic_secret[NANOTLS_SECRET_SIZE];
enum ecode ret = E_INVALID;
if (ctx->state != NANOTLS_HOST_HELLO_SENT)
return nanotls_host_handle_error(ctx, E_INVALID);
ret = nanotls_hdr_check(&msg->hdr, sizeof(*msg), NANOTLS_DEVICE_HELLO);
if (ret != E_OK)
return nanotls_host_handle_error(ctx, ret);
/*
* [H2]
* Feed the DeviceHello content up until the handshake_sig to transcript
* hash context.
*/
nanotls_sha256_update(&ctx->handshake_hash, msg, sig_cutoff_pos);
nanotls_sha256_digest(&ctx->handshake_hash,
ctx->handshake_h2_hash); /* [H2] */
/* Save Device's ECDHE key share (ephemeral public key) */
ctx->device_key_share = msg->device_key_share;
/*
* All ECDHE steps have been completed, now calculate ECDHE shared
* secret and derive Handshake Secret from it.
*/
ret = nanotls_host_derive_hs_secrets(ctx, dev_hs_traffic_secret);
if (ret != E_OK)
return nanotls_host_handle_error(ctx, ret);
ret = nanotls_handshake_decrypt_verify(&msg->enc, sizeof(msg->enc),
&msg->auth_tag,
dev_hs_traffic_secret);
if (ret != E_OK)
return nanotls_host_handle_error(ctx, ret);
memcpy(&ctx->device_info, &msg->enc.device_info,
sizeof(ctx->device_info));
/*
* Verify ECDSA signature handshake_sig using Device's public key.
*
* This is the key point where Device authentication happens:
* - Host generates a random (HostHello.random)
* - Device signs the entire handshake (incl. Host's random) with its
* private key (DeviceHello.device_handshake_sig)
* - Host now verifies ECDSA signature generated by device using
* Device's public key
*
* Only the device that possesses the proper private key could sign the
* Host's random.
* For this reason Host's random should be of a good quality and never
* repeat.
*
* If the device is an impostor and does not pose a valid private key,
* the handshake will fail at this point.
*/
if (ctx->pub_key_req_cb) {
/* device public key is not valid, and we need to first fetch it
* using callback */
ret = ctx->pub_key_req_cb(ctx, &msg->enc.device_info,
ctx->ecdsa_pub);
if (ret != E_OK)
return nanotls_host_handle_error(ctx, ret);
}
ret = nanotls_host_device_hello_sig_verify(ctx, msg);
if (ret != E_OK)
return nanotls_host_handle_error(ctx, ret);
/* [H7] Update handshake transcript with the part before Finished */
nanotls_sha256_update(&ctx->handshake_hash, &msg->enc,
NANOTLS_DEVICE_HELLO_FINISHED_ENC_CUTOFF_POS);
/*
* Verify device_finished message.
* It's a MAC over transcript hash computed using handshake secret
* computed by the Device.
*/
ret = nanotls_host_verify_device_finished(ctx, msg,
dev_hs_traffic_secret);
if (ret != E_OK)
return nanotls_host_handle_error(ctx, ret);
/* [H3] Update handshake transcript with just processed device_finished
*/
nanotls_sha256_update(&ctx->handshake_hash, &msg->enc.device_finished,
sizeof(msg->enc.device_finished));
nanotls_mem_erase(dev_hs_traffic_secret, sizeof(dev_hs_traffic_secret));
ctx->state = NANOTLS_HOST_HELLO_RCVD;
return E_OK;
}
enum ecode nanotls_host_get_device_info(const struct nanotls_host_ctx *ctx,
struct nanotls_device_info *device_info)
{
switch (ctx->state) {
case NANOTLS_HOST_HELLO_RCVD:
case NANOTLS_HOST_DONE:
break;
default:
return E_INVALID;
}
memcpy(device_info, &ctx->device_info, sizeof(ctx->device_info));
return E_OK;
}
enum ecode nanotls_host_do_finished(struct nanotls_host_ctx *ctx,
struct nanotls_host_finished *msg)
{
const size_t finished_cutoff_pos =
NANOTLS_HOST_HELLO_FINISHED_CUTOFF_POS;
uint8_t host_hs_traffic_secret[NANOTLS_SECRET_SIZE];
if (ctx->state != NANOTLS_HOST_HELLO_RCVD)
return nanotls_host_handle_error(ctx, E_INVALID);
memset(msg, 0, sizeof(*msg));
nanotls_hdr_init(&msg->hdr, sizeof(*msg), NANOTLS_HOST_FINISHED);
/* [H9] Update handshake transcript with the starting part of
* HostFinished */
nanotls_sha256_update(&ctx->handshake_hash, msg, finished_cutoff_pos);
/*
* Derive handshake traffic secret with Derive-Secret (HKDF-Expand)
* hs_traffic_secret = Derive-Secret(handshake_secret, label, hash) =
* = HKDF-Expand(handshake_secret, 32 | label |
* transcript_hash, 32)
*/
nanotls_hkdf_derive_secret(ctx->handshake_secret,
NANOTLS_HOST_HS_TS_LABEL,
ctx->handshake_h2_hash,
host_hs_traffic_secret);
/* Produce host_finished message (MAC over transcript hash [H9]). */
nanotls_write_finished(&ctx->handshake_hash, host_hs_traffic_secret,
msg->enc.host_finished.verify_data);
/*
* [H4] - Full transcript hash
* Update handshake transcript with just created host_finished.
* H4 = SHA256(HostHello | DeviceHello | HostFinished)
*/
nanotls_sha256_update(&ctx->handshake_hash, &msg->enc.host_finished,
sizeof(msg->enc.host_finished));
nanotls_handshake_encrypt(&msg->enc, sizeof(msg->enc), &msg->auth_tag,
host_hs_traffic_secret);
/*
* At this point, handshake is completed from the Host's perspective.
* Host Authenticated the Device and has everything to derive the final
* Master Secret.
*
* Now call nanotls_host_derive_traffic_keys() to get the encryption
* keys.
*/
nanotls_derive_master_secret(ctx->master_secret, ctx->handshake_secret);
nanotls_mem_erase(host_hs_traffic_secret,
sizeof(host_hs_traffic_secret));
ctx->state = NANOTLS_HOST_DONE;
return E_OK;
}
enum ecode
nanotls_host_derive_traffic_keys_instance(const struct nanotls_host_ctx *ctx,
struct nanotls_traffic_keys *keys,
size_t instance_idx)
{
uint8_t transcript_hash[NANOTLS_SHA256_HASH_SIZE];
uint8_t instance_label_hash[NANOTLS_SHA256_HASH_SIZE];
if (ctx->state != NANOTLS_HOST_DONE)
return E_INVALID;
nanotls_hkdf_sha256_extract(&instance_idx, sizeof(instance_idx),
NANOTLS_KEY_INSTANCE_LABEL,
instance_label_hash);
nanotls_sha256_digest(&ctx->handshake_hash, transcript_hash); /* [H4] */
nanotls_derive_traffic_keys(ctx->master_secret, transcript_hash,
instance_label_hash,
sizeof(instance_label_hash), keys);
return E_OK;
}
enum ecode nanotls_host_derive_traffic_keys(const struct nanotls_host_ctx *ctx,
struct nanotls_traffic_keys *keys)
{
uint8_t hash[NANOTLS_SHA256_HASH_SIZE];
if (ctx->state != NANOTLS_HOST_DONE)
return E_INVALID;
nanotls_sha256_digest(&ctx->handshake_hash, hash); /* [H4] */
nanotls_derive_traffic_keys(ctx->master_secret, hash, NULL, 0, keys);
return E_OK;
}
enum ecode nanotls_host_cleanup(struct nanotls_host_ctx *ctx)
{
nanotls_mem_erase(ctx, sizeof(*ctx));
ctx->state = NANOTLS_HOST_UNINIT;
return E_OK;
}