mirror of
https://github.com/openharmony/device_soc_winnermicro.git
synced 2026-06-30 22:17:56 -04:00
@@ -12,3 +12,7 @@
|
||||
# limitations under the License.
|
||||
|
||||
rsource "*/Kconfig.liteos_m.defconfig.series"
|
||||
|
||||
config HALS_COMMUCATION_WIFI_LITE
|
||||
bool "WIFI LITE"
|
||||
default y
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Copyright (c) 2020, HiHope Community.
|
||||
# Copyright (c) 2020 HiHope Community.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
import("//kernel/liteos_m/liteos.gni")
|
||||
|
||||
module_switch = defined(LOSCFG_HALS_COMMUCATION_BT_LITE)
|
||||
module_switch = defined(LOSCFG_HALS_COMMUCATION_WIFI_LITE)
|
||||
|
||||
module_name = get_path_info(rebase_path("."), "name")
|
||||
|
||||
@@ -35,18 +35,18 @@ kernel_module(module_name) {
|
||||
sources = [
|
||||
"source/nble.c",
|
||||
"source/nble_server.c",
|
||||
"source/ble_util.c",
|
||||
"source/ble_util.c"
|
||||
]
|
||||
deps = [ "//foundation/communication/bt_lite:bt" ]
|
||||
deps = [ "//foundation/communication/wifi_lite:wifi" ]
|
||||
visibility += [ "../../.." ]
|
||||
include_dirs = [
|
||||
"//utils/native/lite/include",
|
||||
"//device/soc/winnermicro/w800/board/include/",
|
||||
"//foundation/communication/bt_lite/interfaces/ble",
|
||||
"//device/soc/winnermicro/w800/board/src/bt/blehost/nimble",
|
||||
"//device/soc/winnermicro/w800/board/src/bt/blehost/nimble/include",
|
||||
"//device/soc/winnermicro/w800/board/src/bt/blehost/nimble/host/include",
|
||||
"//device/soc/winnermicro/w800/board/src/bt/blehost/porting/w800/include",
|
||||
"//device/soc/winnermicro/w800/board/src/bt/blehost/nimble/host/util/include",
|
||||
"//device/soc/winnermicro/wm800/board/include/",
|
||||
"//foundation/communication/bluetooth/interfaces/innerkits/native_c/include",
|
||||
"//device/soc/winnermicro/wm800/board/src/bt/blehost/nimble",
|
||||
"//device/soc/winnermicro/wm800/board/src/bt/blehost/nimble/include",
|
||||
"//device/soc/winnermicro/wm800/board/src/bt/blehost/nimble/host/include",
|
||||
"//device/soc/winnermicro/wm800/board/src/bt/blehost/porting/w800/include",
|
||||
"//device/soc/winnermicro/wm800/board/src/bt/blehost/nimble/host/util/include",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,159 +1,77 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 2021 WinnerMicro Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include "host/ble_hs.h"
|
||||
|
||||
#include "ble_util.h"
|
||||
#include "bt_def.h"
|
||||
#include "wm_bt_def.h"
|
||||
#include "wm_type_def.h"
|
||||
#include "stdbool.h"
|
||||
|
||||
// typedef enum {
|
||||
// false,
|
||||
// true
|
||||
// } bool;
|
||||
#ifndef CASE_RETURN_STR
|
||||
#define CASE_RETURN_STR(const) case const: return #const;
|
||||
#endif
|
||||
|
||||
static unsigned char BASE_UUID[16] =
|
||||
|
||||
const char *tls_bt_gap_evt_2_str(uint32_t event)
|
||||
{
|
||||
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
|
||||
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
static int uuidType(unsigned char *p_uuid)
|
||||
{
|
||||
int i = 0;
|
||||
int match = 0;
|
||||
int all_zero = 1;
|
||||
|
||||
for(i = 0; i != 16; ++i)
|
||||
{
|
||||
if(i == 12 || i == 13)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(p_uuid[i] == BASE_UUID[i])
|
||||
{
|
||||
++match;
|
||||
}
|
||||
|
||||
if(p_uuid[i] != 0)
|
||||
{
|
||||
all_zero = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if(all_zero)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(match == 12)
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
|
||||
if(match == 14)
|
||||
{
|
||||
return 16;
|
||||
}
|
||||
|
||||
return 128;
|
||||
}
|
||||
|
||||
void app_to_btif_uuid(tls_bt_uuid_t *p_dest, BtUuid *p_src)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
if(p_src->uuidLen == 16 || p_src->uuidLen == 32)
|
||||
{
|
||||
for(i = 0; i != 16; ++i)
|
||||
{
|
||||
p_dest->uu[i] = BASE_UUID[i];
|
||||
}
|
||||
}
|
||||
|
||||
switch(p_src->uuidLen)
|
||||
{
|
||||
case 0:
|
||||
break;
|
||||
|
||||
case 16:
|
||||
p_dest->uu[12] = p_src->uuid[0]& 0xff; //endian???
|
||||
p_dest->uu[13] = p_src->uuid[1]& 0xff;
|
||||
break;
|
||||
|
||||
case 32:
|
||||
p_dest->uu[12] = p_src->uuid[0]& 0xff;
|
||||
p_dest->uu[13] = p_src->uuid[1]& 0xff;
|
||||
p_dest->uu[14] = p_src->uuid[2]& 0xff;
|
||||
p_dest->uu[15] = p_src->uuid[3]& 0xff;
|
||||
break;
|
||||
|
||||
case 128:
|
||||
for(i = 0; i != 16; ++i)
|
||||
{
|
||||
p_dest->uu[i] = p_src->uuid[i];
|
||||
}
|
||||
|
||||
break;
|
||||
switch(event) {
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_CONNECT)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_DISCONNECT)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_CONN_UPDATE)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_CONN_UPDATE_REQ)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_L2CAP_UPDATE_REQ)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_TERM_FAILURE)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_DISC)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_DISC_COMPLETE)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_ADV_COMPLETE)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_ENC_CHANGE)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_PASSKEY_ACTION)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_NOTIFY_RX)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_NOTIFY_TX)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_SUBSCRIBE)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_MTU)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_IDENTITY_RESOLVED)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_REPEAT_PAIRING)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_PHY_UPDATE_COMPLETE)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_EXT_DISC)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_PERIODIC_SYNC)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_PERIODIC_REPORT)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_PERIODIC_SYNC_LOST)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_SCAN_REQ_RCVD)
|
||||
CASE_RETURN_STR(BLE_GAP_EVENT_PERIODIC_TRANSFER)
|
||||
|
||||
default:
|
||||
BLE_IF_DEBUG("Unknown UUID length %d!", p_src->uuidLen);
|
||||
break;
|
||||
return "unkown bt host evt";
|
||||
}
|
||||
}
|
||||
|
||||
void btif_to_app_uuid(BtUuid *p_dest, tls_bt_uuid_t *p_src)
|
||||
void
|
||||
tls_bt_dump_hexstring(const char *info, uint8_t *p, int length)
|
||||
{
|
||||
char *p_byte = (char *)p_src;
|
||||
int i = 0;
|
||||
p_dest->uuidLen = uuidType(p_src->uu);
|
||||
|
||||
switch(p_dest->uuidLen)
|
||||
{
|
||||
case 16:
|
||||
p_dest->uuid[0] = p_src->uu[12];
|
||||
p_dest->uuid[1] = p_src->uu[13];
|
||||
break;
|
||||
|
||||
case 32:
|
||||
p_dest->uuid[0] = p_src->uu[12];
|
||||
p_dest->uuid[1] = p_src->uu[13];
|
||||
p_dest->uuid[2] = p_src->uu[14];
|
||||
p_dest->uuid[3] = p_src->uu[15];
|
||||
break;
|
||||
|
||||
case 128:
|
||||
for(i = 0; i != 16; ++i)
|
||||
{
|
||||
p_dest->uuid[i] = p_byte[i];
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
BLE_IF_DEBUG("Unknown UUID length %d!", p_dest->uuidLen);
|
||||
break;
|
||||
}
|
||||
int i=0,j=0;
|
||||
printf("%s\r\n",info);
|
||||
for (i=0;i<length;i++)
|
||||
{
|
||||
j++;
|
||||
printf("%02x ", p[i]);
|
||||
if((j%16)==0)
|
||||
{
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
|
||||
|
||||
static tls_bt_uuid_t app_base_uuid =
|
||||
{
|
||||
{
|
||||
0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
|
||||
0x00, 0x10, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00
|
||||
}
|
||||
};
|
||||
|
||||
uint16_t btif_uuid128_to_uuid16(tls_bt_uuid_t *uuid)
|
||||
{
|
||||
uint16_t id = 0;
|
||||
memcpy(&id, uuid->uu+12, 2);
|
||||
return id;
|
||||
}
|
||||
tls_bt_uuid_t * btif_uuid16_to_uuid128(uint16_t uuid16)
|
||||
{
|
||||
memcpy(app_base_uuid.uu+12, &uuid16, 2);
|
||||
return &app_base_uuid;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
#ifndef __BLE_UTIL_H__
|
||||
#define __BLE_UTIL_H__
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
// #include "harmony_bt_def.h"
|
||||
#include "bt_def.h"
|
||||
#include "wm_bt_def.h"
|
||||
|
||||
#define BLE_IF_DBG 1
|
||||
|
||||
@@ -34,12 +29,8 @@
|
||||
#ifndef MIN
|
||||
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
extern const char *tls_bt_gap_evt_2_str(uint32_t event);
|
||||
extern void tls_bt_dump_hexstring(const char *info, uint8_t *p, int length);
|
||||
|
||||
extern void app_to_btif_uuid(tls_bt_uuid_t *p_dest, BtUuid *p_src);
|
||||
|
||||
extern void btif_to_app_uuid(BtUuid *p_dest, tls_bt_uuid_t *p_src);
|
||||
|
||||
extern uint16_t btif_uuid128_to_uuid16(tls_bt_uuid_t *uuid);
|
||||
tls_bt_uuid_t * btif_uuid16_to_uuid128(uint16_t uuid16);
|
||||
|
||||
#endif
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
#ifndef __BLE_MY_STDBOOL_H__
|
||||
#define __BLE_MY_STDBOOL_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef bool
|
||||
#define bool unsigned char
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,47 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2020, HiHope Community.
|
||||
* Copyright (c) 2021 WinnerMicro Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "my_stdbool.h"
|
||||
#include "ohos_bt_gatt_server.h"
|
||||
|
||||
#include "wm_mem.h"
|
||||
#include "wm_ble.h"
|
||||
#include "wm_bt_def.h"
|
||||
#include "bt_gatt.h"
|
||||
#include "wm_mem.h"
|
||||
#include "list.h"
|
||||
#include "ble_util.h"
|
||||
|
||||
#include "host/ble_hs.h"
|
||||
#include "host/util/util.h"
|
||||
// #include "harmony_bt_def.h"
|
||||
// #include "harmony_bt_gatt.h"
|
||||
|
||||
/*
|
||||
* STRUCTURE DEFINITIONS
|
||||
****************************************************************************************
|
||||
*/
|
||||
|
||||
typedef struct{
|
||||
struct dl_list list;
|
||||
@@ -66,15 +57,23 @@ typedef struct{
|
||||
struct ble_gatt_svc_def *svc;
|
||||
} nim_service_t;
|
||||
|
||||
/*
|
||||
* GLOBAL VARIABLE DEFINITIONS
|
||||
****************************************************************************************
|
||||
*/
|
||||
|
||||
static uint16_t g_server_id;
|
||||
static server_elem_t server_list;
|
||||
|
||||
static nim_service_t nim_service_list;
|
||||
|
||||
/*
|
||||
* LOCAL FUNCTION DEFINITIONS
|
||||
****************************************************************************************
|
||||
*/
|
||||
|
||||
void ble_server_gap_event(struct ble_gap_event *event, void *arg)
|
||||
{
|
||||
int rc;
|
||||
|
||||
BdAddr bdaddr;
|
||||
struct ble_gap_conn_desc desc;
|
||||
server_elem_t *svr_item = NULL;
|
||||
@@ -85,7 +84,7 @@ void ble_server_gap_event(struct ble_gap_event *event, void *arg)
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
if (event->connect.status == 0)
|
||||
{
|
||||
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
||||
int rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
||||
assert(rc == 0);
|
||||
memcpy(bdaddr.addr, desc.peer_id_addr.val, 6);
|
||||
|
||||
@@ -178,25 +177,6 @@ void ble_server_update_svc_handle(ble_uuid_t *uuid, uint16_t attr_handle)
|
||||
}
|
||||
}
|
||||
}
|
||||
#if BLE_IF_DBG
|
||||
|
||||
static void
|
||||
ble_server_gatt_dump_hexstring(const char *info, uint8_t *p, int length)
|
||||
{
|
||||
int i = 0, j = 0;
|
||||
printf("%s\r\n", info);
|
||||
for(i = 0; i<length; i++)
|
||||
{
|
||||
j++;
|
||||
printf("%02x ", p[i]);
|
||||
if((j % 16) == 0)
|
||||
{
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
void ble_server_func_by_attr_handle(uint16_t attr_handle ,uint8_t op, uint8_t *data, int *len)
|
||||
{
|
||||
@@ -216,7 +196,7 @@ void ble_server_func_by_attr_handle(uint16_t attr_handle ,uint8_t op, uint8_t *d
|
||||
if(svc_item->func.write)
|
||||
{
|
||||
#if BLE_IF_DBG
|
||||
//ble_server_gatt_dump_hexstring("TO HILINK:", data, *len);
|
||||
//tls_bt_dump_hexstring("To Local:", data, *len);
|
||||
#endif
|
||||
svc_item->func.write(data, (int)*len);
|
||||
}
|
||||
@@ -226,7 +206,7 @@ void ble_server_func_by_attr_handle(uint16_t attr_handle ,uint8_t op, uint8_t *d
|
||||
{
|
||||
svc_item->func.read(data, len);
|
||||
#if BLE_IF_DBG
|
||||
ble_server_gatt_dump_hexstring("FROM HILINK:", data, *len);
|
||||
tls_bt_dump_hexstring("To Remote:", data, *len);
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
@@ -246,7 +226,7 @@ static int
|
||||
ble_server_gatt_svc_access_func(uint16_t conn_handle, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
int length = 0;
|
||||
int offset = 0;
|
||||
struct os_mbuf *om = ctxt->om;
|
||||
@@ -272,7 +252,7 @@ ble_server_gatt_svc_access_func(uint16_t conn_handle, uint16_t attr_handle,
|
||||
ble_server_func_by_attr_handle(attr_handle, ctxt->op, cache_buffer, &length);
|
||||
if(length>0)
|
||||
{
|
||||
rc = os_mbuf_append(ctxt->om, &cache_buffer[0],length);
|
||||
int rc = os_mbuf_append(ctxt->om, &cache_buffer[0],length);
|
||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
return 0;
|
||||
@@ -303,11 +283,15 @@ ble_server_uuid_init_from_buf(ble_uuid_any_t *uuid, const void *buf, size_t len)
|
||||
return BLE_HS_EINVAL;
|
||||
}
|
||||
|
||||
/*
|
||||
* EXPORTED FUNCTION DEFINITIONS
|
||||
****************************************************************************************
|
||||
*/
|
||||
|
||||
int ble_server_alloc(BleGattService *srvcinfo)
|
||||
{
|
||||
int i = 0, rc = 0;
|
||||
ble_uuid_t uuid;
|
||||
int i = 0;
|
||||
|
||||
uint8_t srvc_counter = 0;
|
||||
uint8_t char_counter = 0;
|
||||
uint8_t desc_counter = 0;
|
||||
@@ -453,7 +437,7 @@ int ble_server_alloc(BleGattService *srvcinfo)
|
||||
|
||||
int ble_server_free(int server_id)
|
||||
{
|
||||
int c = 0, d = 0;
|
||||
int d = 0;
|
||||
nim_service_t *nim_service_item = NULL;
|
||||
server_elem_t *svr_item = NULL;
|
||||
server_elem_t *svr_item_next = NULL;
|
||||
@@ -489,7 +473,7 @@ int ble_server_free(int server_id)
|
||||
{
|
||||
if(svc_array->characteristics != NULL)
|
||||
{
|
||||
for (c = 0; svc_array->characteristics[c].uuid != NULL; c++)
|
||||
for (int c = 0; svc_array->characteristics[c].uuid != NULL; c++)
|
||||
{
|
||||
chr_array = svc_array->characteristics + c;
|
||||
|
||||
@@ -514,11 +498,12 @@ int ble_server_free(int server_id)
|
||||
|
||||
void ble_server_start_service()
|
||||
{
|
||||
int rc;
|
||||
|
||||
nim_service_t *svc_item = NULL;
|
||||
|
||||
if(!dl_list_empty(&nim_service_list.list))
|
||||
{
|
||||
int rc;
|
||||
dl_list_for_each(svc_item, &nim_service_list.list, nim_service_t, list)
|
||||
{
|
||||
if(svc_item == NULL)
|
||||
@@ -539,14 +524,11 @@ void ble_server_start_service()
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ble_server_init()
|
||||
{
|
||||
memset(&server_list, 0, sizeof(server_elem_t));
|
||||
dl_list_init(&server_list.list);
|
||||
dl_list_init(&nim_service_list.list);
|
||||
|
||||
dl_list_init(&nim_service_list.list);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,9 +4,10 @@
|
||||
/*Initialize the list for ble server*/
|
||||
extern void ble_server_init();
|
||||
|
||||
/*Enable the all service stored in the servcie list one by one*/
|
||||
extern void ble_server_start_service();
|
||||
|
||||
/*Free servcie*/
|
||||
/*Free servcie by server id*/
|
||||
extern int ble_server_free(int server_id);
|
||||
|
||||
/*Register one service, and return server if*/
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Copyright (c) 2020, HiHope Community.
|
||||
# Copyright (c) 2020 HiHope Community.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2020, HiHope Community.
|
||||
* Copyright (c) 2020 HiHope Community.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
@@ -33,7 +33,7 @@
|
||||
#include "wm_params.h"
|
||||
#include "wm_mem.h"
|
||||
#include "wm_efuse.h"
|
||||
#include "wm_netif.h"
|
||||
#include "kv_store.h"
|
||||
|
||||
/* Why we doing this?
|
||||
* Symbol @WIFI_DISCONNECTED conflict with OHOS wifiservice,
|
||||
@@ -67,12 +67,20 @@ u8 g_hasConnected = 0;
|
||||
/* Store Scanned AP count
|
||||
* TODO: Use lock to protect Scan result.
|
||||
*/
|
||||
|
||||
#define KV_FILE_NAME "/data"
|
||||
#define WIFI_CFG_INFO "wifi_cfg_info"
|
||||
|
||||
static int gScannedAPCount;
|
||||
static u8* gScannedBuffer;
|
||||
|
||||
#define MAX_WIFI_KV_NAME_LEN (32)
|
||||
|
||||
#define MAX_WIFI_KV_STRING_LEN (160)
|
||||
static u8 kvstring[MAX_WIFI_KV_STRING_LEN];
|
||||
static u8 keystring[MAX_WIFI_KV_NAME_LEN];
|
||||
static u8 keynew = 0;
|
||||
static u8 keyold = 0;
|
||||
|
||||
//#ifndef DEBUG
|
||||
#define DEBUG (1)
|
||||
//#endif
|
||||
@@ -100,7 +108,7 @@ WifiErrorCode EnableWifi(void)
|
||||
}
|
||||
return ERROR_WIFI_BUSY;
|
||||
}
|
||||
|
||||
tls_wifi_init();
|
||||
gWifiStaStatus = WIFI_STA_ACTIVE;
|
||||
|
||||
if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
|
||||
@@ -218,9 +226,7 @@ static void WifiScanHandler(void)
|
||||
if (gScannedBuffer == NULL) {
|
||||
printf("[wifi_device]: scan buffer is NULL!\n");
|
||||
|
||||
#if defined(OHOS_XTS_WITH_W800)
|
||||
gWifiScanDone = TRUE;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -230,9 +236,7 @@ static void WifiScanHandler(void)
|
||||
tls_mem_free(gScannedBuffer);
|
||||
gScannedBuffer = NULL;
|
||||
|
||||
#if defined(OHOS_XTS_WITH_W800)
|
||||
gWifiScanDone = TRUE;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -330,9 +334,6 @@ static void WifiEventCallback(u8 status)
|
||||
debug_wifi("WifiEventCallback status = WIFI_DISCONNECTED\n");
|
||||
DispatchConnectEvent(WIFI_STATE_NOT_AVALIABLE, NULL);
|
||||
break;
|
||||
case NETIF_IP_NET_UP:
|
||||
debug_wifi("WifiEventCallback status = NETIF_IP_NET_UP\n");
|
||||
break;
|
||||
case NETIF_WIFI_SOFTAP_SUCCESS: /* ap */
|
||||
debug_wifi("WifiEventCallback status = WIFI_SOFTAP_SUCCESS\n");
|
||||
DispatchHotspotStateChangedEvent(WIFI_HOTSPOT_ACTIVE);
|
||||
@@ -345,12 +346,6 @@ static void WifiEventCallback(u8 status)
|
||||
debug_wifi("WifiEventCallback status = WIFI_SOFTAP_CLOSED\n");
|
||||
DispatchHotspotStateChangedEvent(WIFI_HOTSPOT_NOT_ACTIVE);
|
||||
break;
|
||||
case NETIF_IP_NET2_UP:
|
||||
debug_wifi("WifiEventCallback status = NETIF_IP_NET2_UP\n");
|
||||
break;
|
||||
case NETIF_IPV6_NET_UP:
|
||||
debug_wifi("WifiEventCallback status = NETIF_IPV6_NET_UP\n");
|
||||
break;
|
||||
default:
|
||||
debug_wifi("WifiEventCallback invalid status: %d\n", status);
|
||||
break;
|
||||
@@ -404,7 +399,7 @@ WifiErrorCode RegisterWifiEvent(WifiEvent* event)
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
}
|
||||
|
||||
err_t err = tls_netif_add_status_event(WifiEventCallback);
|
||||
err_t err = tls_wifi_netif_add_status_event(WifiEventCallback);
|
||||
if (err != 0) {
|
||||
printf("[wifi_device]: tls_netif_add_status_event failed.\n");
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
@@ -436,7 +431,7 @@ WifiErrorCode UnRegisterWifiEvent(const WifiEvent* event)
|
||||
}
|
||||
UnlockWifiEventLock();
|
||||
|
||||
err_t err = tls_netif_remove_status_event(WifiEventCallback);
|
||||
err_t err = tls_wifi_netif_remove_status_event(WifiEventCallback);
|
||||
if (err != 0) {
|
||||
printf("[wifi_device]: tls_netif_add_status_event failed.\n");
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
@@ -474,7 +469,6 @@ WifiErrorCode AdvanceScan(WifiScanParams *params)
|
||||
|
||||
if (params->scanType < 0 || params->scanType > WIFI_BAND_SCAN) {
|
||||
printf("[wifi_service] scanType invalid!\n");
|
||||
return WIFI_SUCCESS; // TODO: test case code logic error, but we follow it, need update when test code update.
|
||||
}
|
||||
|
||||
if (LockWifiGlobalLock() != WIFI_SUCCESS) {
|
||||
@@ -510,7 +504,6 @@ WifiErrorCode AdvanceScan(WifiScanParams *params)
|
||||
|
||||
WifiErrorCode GetScanInfoList(WifiScanInfo* result, unsigned int* size)
|
||||
{
|
||||
int ret;
|
||||
struct tls_scan_bss_t *scanRes = NULL;
|
||||
struct tls_bss_info_t *bssInfo;
|
||||
u32 scanCount, i;
|
||||
@@ -544,9 +537,9 @@ WifiErrorCode GetScanInfoList(WifiScanInfo* result, unsigned int* size)
|
||||
scanCount = WIFI_SCAN_HOTSPOT_LIMIT;
|
||||
}
|
||||
|
||||
int cpyErr;
|
||||
|
||||
for (i = 0; i < scanCount; ++i) {
|
||||
cpyErr = memcpy_s(result[i].ssid, WIFI_MAX_SSID_LEN, bssInfo->ssid, bssInfo->ssid_len);
|
||||
int cpyErr = memcpy_s(result[i].ssid, WIFI_MAX_SSID_LEN, bssInfo->ssid, bssInfo->ssid_len);
|
||||
if (cpyErr != EOK) {
|
||||
printf("[wifi_device]: copy ssid of scan result failed\n");
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
@@ -604,6 +597,7 @@ WifiErrorCode AddDeviceConfig(const WifiDeviceConfig* config, int* result)
|
||||
{
|
||||
int netId = WIFI_CONFIG_INVALID;
|
||||
int i;
|
||||
int ret = 0;
|
||||
|
||||
if (config == NULL || result == NULL) {
|
||||
printf("[wifi_device]:add device config invalid argument.\n");
|
||||
@@ -614,16 +608,7 @@ WifiErrorCode AddDeviceConfig(const WifiDeviceConfig* config, int* result)
|
||||
printf("[wifi_device]:Lock wifi global lock failed.\n");
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
}
|
||||
|
||||
int ret = WM_KvWrite(WIFI_CFG_INFO, config, sizeof(WifiDeviceConfig));
|
||||
if (ret < 0 ) {
|
||||
printf("\r\n save wifi cfg info fail\r\n");
|
||||
} else {
|
||||
printf("\r\n save wifi cfg info ok\r\n");
|
||||
}
|
||||
|
||||
|
||||
for (i = 0; i < WIFI_MAX_CONFIG_SIZE; ++i) {
|
||||
for (i = 0; i < WIFI_MAX_CONFIG_SIZE; i++) {
|
||||
if (gWifiConfigs[i].netId != i) {
|
||||
netId = i;
|
||||
break;
|
||||
@@ -633,12 +618,23 @@ WifiErrorCode AddDeviceConfig(const WifiDeviceConfig* config, int* result)
|
||||
if (netId == WIFI_CONFIG_INVALID) {
|
||||
printf("[wifi_service]:AddDeviceConfig wifi config is full, delete one first\n");
|
||||
if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
|
||||
printf("[wifi_device] Unlock wifi global lock failed.\n");
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
}
|
||||
return ERROR_WIFI_BUSY;
|
||||
}
|
||||
|
||||
UtilsSetEnv(KV_FILE_NAME);
|
||||
memset(kvstring, 0, MAX_WIFI_KV_STRING_LEN);
|
||||
memset(keystring, 0, MAX_WIFI_KV_NAME_LEN);
|
||||
memcpy(kvstring, config, sizeof(WifiDeviceConfig));
|
||||
kvstring[sizeof(WifiDeviceConfig)] = '\0';
|
||||
|
||||
sprintf(keystring, WIFI_CFG_INFO"_%d", netId);
|
||||
ret = UtilsSetValue(keystring, kvstring);
|
||||
if (ret < 0 ) {
|
||||
return ERROR_WIFI_BUSY;
|
||||
}
|
||||
|
||||
int cpyErr = memcpy_s(&gWifiConfigs[netId], sizeof(WifiDeviceConfig), config, sizeof(WifiDeviceConfig));
|
||||
if (cpyErr != EOK) {
|
||||
printf("[wifi_service]:AddDeviceConfig memcpy failed, err = %d\n", cpyErr);
|
||||
@@ -663,6 +659,8 @@ WifiErrorCode GetDeviceConfigs(WifiDeviceConfig* result, unsigned int* size)
|
||||
unsigned int retIndex = 0;
|
||||
int i = 0;
|
||||
int cpyErr;
|
||||
int validflag = -1;
|
||||
|
||||
|
||||
if (result == NULL || size == NULL || *size == 0) {
|
||||
return ERROR_WIFI_INVALID_ARGS;
|
||||
@@ -674,8 +672,19 @@ WifiErrorCode GetDeviceConfigs(WifiDeviceConfig* result, unsigned int* size)
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
}
|
||||
|
||||
int ret = WM_KvRead(WIFI_CFG_INFO, &gWifiConfigs[0], sizeof(WifiDeviceConfig));
|
||||
if (ret <= 0 ) {
|
||||
UtilsSetEnv(KV_FILE_NAME);
|
||||
for (i = 0; i < WIFI_MAX_CONFIG_SIZE; i++)
|
||||
{
|
||||
memset(keystring, 0, MAX_WIFI_KV_NAME_LEN);
|
||||
sprintf(keystring, WIFI_CFG_INFO"_%d", i);
|
||||
int ret = UtilsGetValue(keystring, &gWifiConfigs[i], sizeof(WifiDeviceConfig));
|
||||
if (ret == 0)
|
||||
{
|
||||
validflag = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (validflag < 0 ) {
|
||||
printf("\r\n read wifi cfg info fail");
|
||||
if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
|
||||
printf("[wifi_device] Unlock wifi global lock failed in get device config.\n");
|
||||
@@ -723,12 +732,12 @@ WifiErrorCode GetDeviceConfigs(WifiDeviceConfig* result, unsigned int* size)
|
||||
WifiErrorCode Disconnect(void)
|
||||
{
|
||||
printf("\r\nDisconnect: g_connectStatus=%d", g_connectStatus);
|
||||
if (g_connectStatus != NETIF_WIFI_JOIN_SUCCESS) {
|
||||
#if defined(OHOS_XTS_WITH_W800)
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
#endif
|
||||
}
|
||||
|
||||
tls_wifi_disconnect();
|
||||
|
||||
if (g_connectStatus != NETIF_WIFI_JOIN_SUCCESS) {
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
}
|
||||
g_connectStatus = NETIF_WIFI_DISCONNECTED;
|
||||
return WIFI_SUCCESS;
|
||||
}
|
||||
@@ -754,11 +763,13 @@ WifiErrorCode RemoveDevice(int networkId)
|
||||
g_hasConnected = 0;
|
||||
|
||||
#if 1
|
||||
int ret = WM_KvDelete(WIFI_CFG_INFO);
|
||||
UtilsSetEnv(KV_FILE_NAME);
|
||||
memset(keystring, 0, MAX_WIFI_KV_NAME_LEN);
|
||||
sprintf(keystring, WIFI_CFG_INFO"_%d", networkId);
|
||||
int ret = UtilsDeleteValue(keystring);
|
||||
if (ret < 0 ) {
|
||||
printf("\r\n clear wifi cfg info fail");
|
||||
} else {
|
||||
printf("\r\n clear wifi cfg info ok");
|
||||
}
|
||||
#else
|
||||
extern void HalFlashFileDeInit(void);
|
||||
@@ -806,14 +817,6 @@ WifiErrorCode GetLinkedInfo(WifiLinkedInfo* result)
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
}
|
||||
|
||||
struct tls_ethif *netif = tls_netif_get_ethif();
|
||||
cpyErr = memcpy_s(&result->ipAddress, sizeof(ip_addr_t), &netif->ip_addr, sizeof(ip_addr_t));
|
||||
if (cpyErr != EOK) {
|
||||
printf("[wifi_device]: GetLinkedInfo copy ipaddr failed. err = %d.\n", cpyErr);
|
||||
tls_mem_free(bss);
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
}
|
||||
|
||||
switch(wifi_states) {
|
||||
case WM_WIFI_DISCONNECTED:
|
||||
result->connState = WIFI_DISCONNECTED;
|
||||
@@ -853,7 +856,6 @@ static void InitWifiConfig(void)
|
||||
tls_param_set(TLS_PARAM_ID_WPROTOCOL, (void *) &wireless_protocol, TRUE);//FALSE
|
||||
}
|
||||
|
||||
tls_wifi_set_oneshot_flag(0);
|
||||
ip_param = tls_mem_alloc(sizeof(struct tls_param_ip));
|
||||
if (ip_param != NULL) {
|
||||
tls_param_get(TLS_PARAM_ID_IP, ip_param, FALSE);
|
||||
@@ -917,7 +919,7 @@ WifiErrorCode ConnectTo(int networkId)
|
||||
}
|
||||
|
||||
g_connectStatus = 0;
|
||||
err_t err = tls_netif_add_status_event(WifiStatusHandler);
|
||||
err_t err = tls_wifi_netif_add_status_event(WifiStatusHandler);
|
||||
if (err != 0) {
|
||||
printf("[wifi_device]: tls_netif_add_status_event for ConnectTo failed.\n");
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
@@ -934,7 +936,7 @@ WifiErrorCode ConnectTo(int networkId)
|
||||
return WIFI_SUCCESS;
|
||||
}
|
||||
|
||||
err = tls_netif_remove_status_event(WifiStatusHandler);
|
||||
err = tls_wifi_netif_remove_status_event(WifiStatusHandler);
|
||||
if (err != 0) {
|
||||
printf("[wifi_device]: tls_netif_remove_status_event for ConnectTo failed.\n");
|
||||
return ERROR_WIFI_UNKNOWN;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2020, HiHope Community.
|
||||
* Copyright (c) 2020 HiHope Community.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2020, HiHope Community.
|
||||
* Copyright (c) 2020 HiHope Community.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2020, HiHope Community.
|
||||
* Copyright (c) 2020 HiHope Community.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
|
||||
Executable
+38
@@ -0,0 +1,38 @@
|
||||
# Copyright (c) 2020 HiHope Community.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this
|
||||
# list of conditions and the following disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions and the following disclaimer in the documentation
|
||||
# and/or other materials provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import("//kernel/liteos_m/liteos.gni")
|
||||
|
||||
static_library("hal_file_static") {
|
||||
sources = [
|
||||
"src/hal_lfs.c",
|
||||
]
|
||||
|
||||
include_dirs = [
|
||||
"//utils/native/lite/hals/file",
|
||||
]
|
||||
}
|
||||
Executable
+261
@@ -0,0 +1,261 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include "hal_file.h"
|
||||
#include "utils_file.h"
|
||||
#include "wm_type_def.h"
|
||||
|
||||
|
||||
#define FLASH_FILE_NAME_LEN 32
|
||||
#define FLASH_FILE_MAX_NUM 64
|
||||
|
||||
#define MAX_NUM_OF_OPENED_FILES 32
|
||||
static int g_openFileNum = 0;
|
||||
|
||||
static int _mode_convert(int flags)
|
||||
{
|
||||
int mode, res = 0;
|
||||
|
||||
mode = flags & O_ACCMODE;
|
||||
if (mode == O_RDONLY_FS) {
|
||||
res |= O_RDONLY;
|
||||
} else if (mode == O_WRONLY_FS) {
|
||||
res |= O_WRONLY;
|
||||
} else if (mode == O_RDWR_FS) {
|
||||
res |= O_RDWR;
|
||||
}
|
||||
//printf("%s: 1 res 0x%x mode 0x%x, flags 0x%x\n", __func__, res, mode, flags);
|
||||
if (flags & O_CREAT_FS) {
|
||||
res |= O_CREAT;
|
||||
}
|
||||
if (flags & O_EXCL_FS) {
|
||||
res |= O_EXCL;
|
||||
}
|
||||
if (flags & O_TRUNC_FS) {
|
||||
res |= O_TRUNC;
|
||||
}
|
||||
if (flags & O_APPEND_FS) {
|
||||
res |= O_CREAT | O_APPEND;
|
||||
}
|
||||
//printf("%s: 2 res 0x%x, O_CREAT 0x%x, O_CREAT_FS 0x%x\n", __func__, res, O_CREAT, O_CREAT_FS);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Relative path convert */
|
||||
static char *_path_convert(const char *path)
|
||||
{
|
||||
int len;
|
||||
char *target_path;
|
||||
|
||||
len = strlen(path) + 8;
|
||||
target_path = (char *)malloc(len);
|
||||
if (target_path == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
memset_s(target_path, len, 0, len);
|
||||
int i, j = 0;
|
||||
//target_path[j++] = '/';
|
||||
memcpy(target_path, "/data/", 6);
|
||||
j += 6;
|
||||
for (i = 0; i < strlen(path); i++){
|
||||
if (path[i] != '/')
|
||||
{
|
||||
target_path[j++] = path[i];
|
||||
}
|
||||
}
|
||||
target_path[j] = '\0';
|
||||
|
||||
return target_path;
|
||||
}
|
||||
|
||||
|
||||
int HalFileOpen(const char* path, int oflag, int mode)
|
||||
{
|
||||
|
||||
//printf("%s: path %s enter\n", __func__, path);
|
||||
if ((path == NULL) || (strlen(path) >= FLASH_FILE_NAME_LEN)) {
|
||||
return -1;
|
||||
}
|
||||
if (g_openFileNum >= MAX_NUM_OF_OPENED_FILES) {
|
||||
printf("\r\n_file_open: the number of open files reached max (%d)", MAX_NUM_OF_OPENED_FILES);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *target_path = _path_convert(path);
|
||||
if (target_path == NULL) {
|
||||
printf("\r\n_file_open: target_path is null");
|
||||
return -1;
|
||||
}
|
||||
int newMode = _mode_convert(oflag);
|
||||
int fd = open(target_path, newMode);
|
||||
//printf("%s: target_path %s fd %d\n", __func__, target_path, fd);
|
||||
free(target_path);
|
||||
target_path = NULL;
|
||||
if (fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
g_openFileNum ++;
|
||||
return fd+1;
|
||||
}
|
||||
|
||||
int HalFileClose(int fd)
|
||||
{
|
||||
int ret = 0;
|
||||
//printf("%s: enter\n", __func__);
|
||||
if (fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
ret = close(fd-1);
|
||||
if(ret)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
g_openFileNum --;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int HalFileRead(int fd, char* buf, unsigned int len)
|
||||
{
|
||||
int ret = 0;
|
||||
//printf("%s: enter\n", __func__);
|
||||
if (fd < 0 || (buf == NULL) || (len == 0)) {
|
||||
return -1;
|
||||
}
|
||||
ret = read(fd-1, buf, len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int HalFileWrite(int fd, const char* buf, unsigned int len)
|
||||
{
|
||||
int ret = 0;
|
||||
//printf("%s: enter\n", __func__);
|
||||
if (fd < 0 || (buf == NULL) || (len == 0)) {
|
||||
return -1;
|
||||
}
|
||||
ret = write(fd-1, buf, len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int HalFileDelete(const char* path)
|
||||
{
|
||||
int ret = 0;
|
||||
//printf("%s: path %s enter\n", __func__, path);
|
||||
if ((path == NULL) || (strlen(path) >= FLASH_FILE_NAME_LEN)) {
|
||||
printf("\r\nHalFileDelete: input invalid parameter");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *target_path = _path_convert(path);
|
||||
if (target_path == NULL) {
|
||||
printf("\r\n_file_open: target_path is null");
|
||||
return -1;
|
||||
}
|
||||
ret = unlink(target_path);
|
||||
free(target_path);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int HalFileStat(const char* path, unsigned int* fileSize)
|
||||
{
|
||||
//printf("%s: path %s enter\n", __func__, path);
|
||||
if ((path == NULL) || (strlen(path) >= FLASH_FILE_NAME_LEN)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
char *target_path = _path_convert(path);
|
||||
if (target_path == NULL) {
|
||||
printf("\r\n_file_open: target_path is null");
|
||||
return -1;
|
||||
}
|
||||
struct stat info = {0};
|
||||
if (stat(target_path, &info) != F_OK) {
|
||||
free(target_path);
|
||||
return -1;
|
||||
}
|
||||
free(target_path);
|
||||
*fileSize = info.st_size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int HalFileSeek(int fd, int offset, unsigned int whence)
|
||||
{
|
||||
int ret = 0;
|
||||
//printf("%s: enter\n", __func__);
|
||||
if (fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
ret = lseek(fd-1, offset, whence);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
struct stat info = {0};
|
||||
if (fstat(fd-1, &info) != F_OK) {
|
||||
return -1;
|
||||
}
|
||||
if(ret > info.st_size)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return ret;
|
||||
|
||||
}
|
||||
#if 0
|
||||
int WM_KvRead(const char* path, char* buf, unsigned int len)
|
||||
{
|
||||
int fd = HalFileOpen(path, O_RDWR_FS | O_CREAT_FS, 0);
|
||||
if (fd < 0) {
|
||||
printf("\r\nWM_KvRead: open fail");
|
||||
return -1;
|
||||
}
|
||||
int ret = HalFileRead(fd, buf, len);
|
||||
if (ret < 0) {
|
||||
printf("\r\nWM_KvRead: read fail");
|
||||
}
|
||||
HalFileClose(fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int WM_KvWrite(const char* path, char* buf, unsigned int len)
|
||||
{
|
||||
int fd = HalFileOpen(path, O_RDWR_FS | O_CREAT_FS, 0);
|
||||
if (fd < 0) {
|
||||
printf("\r\nWM_KvWrite: open fail");
|
||||
return -1;
|
||||
}
|
||||
int ret = HalFileWrite(fd, buf, len);
|
||||
if (ret < 0) {
|
||||
printf("\r\nWM_KvWrite: write fail");
|
||||
}
|
||||
HalFileClose(fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int WM_KvGetLen(const char* path, unsigned int* fileSize)
|
||||
{
|
||||
return HalFileStat(path, fileSize);
|
||||
}
|
||||
|
||||
int WM_KvDelete(const char* path)
|
||||
{
|
||||
return HalFileDelete(path);
|
||||
}
|
||||
#endif
|
||||
@@ -1,79 +0,0 @@
|
||||
/**
|
||||
* @file wm_at_ri_init.h
|
||||
*
|
||||
* @brief AT_RI task and interface resource initial Module
|
||||
*
|
||||
* @author winnermicro
|
||||
*
|
||||
* Copyright (c) 2015 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
|
||||
#ifndef WM_AT_RI_H
|
||||
#define WM_AT_RI_H
|
||||
|
||||
/**
|
||||
* @defgroup APP_APIs APP APIs
|
||||
* @brief APP APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup APP_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup AT_RI_APIs AT_RI APIs
|
||||
* @brief AT_RI command APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup AT_RI_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief This function is used to initialize hostif task
|
||||
used by AT&RI Command
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note Usually the system will call this api at power on.
|
||||
*/
|
||||
int tls_hostif_init(void);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief This function is used to initialize high speed SPI
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note Users can decide to call this api or not according to his application.
|
||||
*/
|
||||
int tls_hspi_init(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to initialize UART
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note Usually the system will call this api at power on.
|
||||
*/
|
||||
void tls_uart_init(void);
|
||||
|
||||
#endif /* WM_AT_RI_H */
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
/**
|
||||
* @file wm_crypto.h
|
||||
*
|
||||
* @brief crypto driver module
|
||||
*
|
||||
* @author dave
|
||||
*
|
||||
* Copyright (c) 2014 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
#ifndef WM_CRYPTO_H
|
||||
#define WM_CRYPTO_H
|
||||
|
||||
/**
|
||||
* @brief Encrypt plain data by 128 AES crypto
|
||||
* @param[in] key the key for encryption
|
||||
* @param[in] iv the IV value for encryption
|
||||
* @param[in] data where the plain data stored
|
||||
* @param[in] data_len length of the plain data to be encrypted
|
||||
* @retval 0 finish Encryption successfully
|
||||
* @retval -1 Error
|
||||
* @note Encrypted data will be placed into the plain @data area
|
||||
*
|
||||
*/
|
||||
int aes_128_cbc_encrypt (const u8 *key, const u8 *iv, u8 *data, size_t data_len) ;
|
||||
|
||||
/**
|
||||
* @brief Decrypt data by 128 AES crypto
|
||||
* @param[in] key the key for encryption
|
||||
* @param[in] iv the IV value for encryption
|
||||
* @param[in] data where the plain data stored
|
||||
* @param[in] data_len length of the plain data to be decrypted
|
||||
* @retval 0 finish Decryption successfully
|
||||
* @retval -1 Error
|
||||
* @note plain data will be placed into the encrypted @data area
|
||||
*
|
||||
*/
|
||||
int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data,
|
||||
size_t data_len);
|
||||
|
||||
/**
|
||||
* @brief XOR RC4 stream to given data with skip-stream-start
|
||||
* @param[in] key RC4 key
|
||||
* @param[in] keylen RC4 key length
|
||||
* @param[in] data data to be XOR'ed with RC4 stream
|
||||
* @param[in] data_len length of the plain data to be encrypted
|
||||
* @retval 0 finish Encryption/Decryption successfully
|
||||
* @retval -1 Error
|
||||
* @note this function should be used for Encryption & Decryption both For the Encryption, the plain @data
|
||||
* will be replaced by the encrypted output, and vice versa;
|
||||
*/
|
||||
int rc4(const u8 *key, size_t keylen, u8 *data, size_t data_len);
|
||||
|
||||
|
||||
/**
|
||||
* @brief MD5 hash for data vector
|
||||
* @param[in] addr Pointers to the data area
|
||||
* @param[in] len Lengths of the data block
|
||||
* @param[in] mac Buffer for the hash (16 bytes)
|
||||
* @retval 0 finish caculation successfully
|
||||
* @retval -1 Error
|
||||
* @note
|
||||
*/
|
||||
int md5(const u8 *addr, int len, u8 *mac);
|
||||
|
||||
/**
|
||||
* @brief HMAC-MD5 over data buffer (RFC 2104)
|
||||
* @param[in] key Key for HMAC operations
|
||||
* @param[in] keylen Length of the key in bytes
|
||||
* @param[in] data data to be caculated
|
||||
* @param[in] data_len Lengths of the data block
|
||||
* @param[in] mac Buffer for the hash (16 bytes)
|
||||
* @retval 0 finish caculation successfully
|
||||
* @retval -1 Error
|
||||
* @note
|
||||
*/
|
||||
int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len, u8 *mac);
|
||||
|
||||
/**
|
||||
* @brief SHA1 hash for data vector
|
||||
* @param[in] addr Pointers to the data area
|
||||
* @param[in] len Lengths of the data block
|
||||
* @param[in] mac Buffer for the hash (16 bytes)
|
||||
* @retval 0 finish caculation successfully
|
||||
* @retval -1 Error
|
||||
* @note
|
||||
*/
|
||||
|
||||
int sha1(const u8 *addr, int len, u8 *mac);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
/**
|
||||
* @file wm_dhcp_server.h
|
||||
*
|
||||
* @brief DHCP SERVER
|
||||
*
|
||||
* @author winnermicro
|
||||
*
|
||||
* Copyright (c) 2015 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
|
||||
#ifndef WM_DHCP_SERVER_H
|
||||
#define WM_DHCP_SERVER_H
|
||||
|
||||
/**
|
||||
* @defgroup APP_APIs APP APIs
|
||||
* @brief APP APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup APP_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup DHCPS_APIs DHCPS APIs
|
||||
* @brief DHCP server APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup DHCPS_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief This function is used to start DHCP Server for a network
|
||||
interface
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @retval WM_SUCCESS success
|
||||
* @retval WM_FAILED failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
s8 tls_dhcps_start(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to stop DHCP server
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_dhcps_stop(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to get station's IP address by
|
||||
MAC address
|
||||
*
|
||||
* @param[in] *mac STA's MAC address
|
||||
*
|
||||
* @retval ip_addr STA's IP address
|
||||
* @retval NULL Not found match IP with MAC address
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
ip_addr_t *tls_dhcps_getip(const u8 *mac);
|
||||
|
||||
/**
|
||||
* @brief This function is used to set DHCP server's DNS address
|
||||
*
|
||||
* @param[in] numdns the index of the DNS server to set must be 0 or 1
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_dhcps_setdns(u8 numdns);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#endif /* WM_DHCP_SERVER_H */
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
/**
|
||||
* @file wm_dns_server.h
|
||||
*
|
||||
* @brief DNS SERVER
|
||||
*
|
||||
* @author winnermicro
|
||||
*
|
||||
* Copyright (c) 2015 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
|
||||
#ifndef WM_DNS_SERVER_H
|
||||
#define WM_DNS_SERVER_H
|
||||
|
||||
/**
|
||||
* @defgroup APP_APIs APP APIs
|
||||
* @brief APP APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup APP_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup DNSS_APIs DNSS APIs
|
||||
* @brief DNS server APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup DNSS_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief This function is used to start DNS service
|
||||
*
|
||||
* @param[in] *dnsname Specify the server's dns name
|
||||
*
|
||||
* @retval WM_SUCCESS success
|
||||
* @retval WM_FAILED failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
s8 tls_dnss_start(u8 *dnsname);
|
||||
|
||||
/**
|
||||
* @brief This function is used to stop DNS service
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_dnss_stop(void);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#endif /* WM_DNS_SERVER_H */
|
||||
|
||||
@@ -1,486 +0,0 @@
|
||||
/**
|
||||
* @file wm_http_client.h
|
||||
*
|
||||
* @brief Http client APIs
|
||||
*
|
||||
* @author wanghf
|
||||
*
|
||||
* Copyright (c) 2014 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
#ifndef WM_HTTP_CLIENT_H
|
||||
#define WM_HTTP_CLIENT_H
|
||||
|
||||
#include "wm_config.h"
|
||||
#include "wm_type_def.h"
|
||||
#ifdef BOOL
|
||||
#undef BOOL
|
||||
#endif
|
||||
#ifdef UCHAR
|
||||
#undef UCHAR
|
||||
#endif
|
||||
#ifdef CHAR
|
||||
#undef CHAR
|
||||
#endif
|
||||
#ifdef UINT16
|
||||
#undef UINT16
|
||||
#endif
|
||||
#ifdef INT16
|
||||
#undef INT16
|
||||
#endif
|
||||
#ifdef UINT32
|
||||
#undef UINT32
|
||||
#endif
|
||||
#ifdef INT32
|
||||
#undef INT32
|
||||
#endif
|
||||
#ifdef UINT64
|
||||
#undef UINT64
|
||||
#endif
|
||||
#ifdef INT64
|
||||
#undef INT64
|
||||
#endif
|
||||
#ifdef ULONG
|
||||
#undef ULONG
|
||||
#endif
|
||||
#ifdef LONG
|
||||
#undef LONG
|
||||
#endif
|
||||
#define VOID void
|
||||
typedef int BOOL;
|
||||
typedef unsigned char UCHAR;
|
||||
//typedef signed char CHAR;
|
||||
typedef char CHAR;
|
||||
typedef unsigned short UINT16;
|
||||
typedef signed short INT16;
|
||||
typedef unsigned int UINT32;
|
||||
typedef signed int INT32;
|
||||
typedef unsigned long long UINT64;
|
||||
typedef long long INT64;
|
||||
typedef unsigned long ULONG;
|
||||
typedef signed long LONG;
|
||||
|
||||
|
||||
/* HTTP Status, API Return codes */
|
||||
/** HTTP Success status */
|
||||
#define HTTP_CLIENT_SUCCESS 0
|
||||
/** Unknown error */
|
||||
#define HTTP_CLIENT_UNKNOWN_ERROR 1
|
||||
/** an Invalid handle or possible bad pointer was passed to a function */
|
||||
#define HTTP_CLIENT_ERROR_INVALID_HANDLE 2
|
||||
/** Buffer too small or a failure while in memory allocation */
|
||||
#define HTTP_CLIENT_ERROR_NO_MEMORY 3
|
||||
/** an attempt to use an invalid socket handle was made */
|
||||
#define HTTP_CLIENT_ERROR_SOCKET_INVALID 4
|
||||
/** Can't send socket parameters */
|
||||
#define HTTP_CLIENT_ERROR_SOCKET_CANT_SET 5
|
||||
/** Error while resolving host name */
|
||||
#define HTTP_CLIENT_ERROR_SOCKET_RESOLVE 6
|
||||
/** Error while connecting to the remote server */
|
||||
#define HTTP_CLIENT_ERROR_SOCKET_CONNECT 7
|
||||
/** socket time out error */
|
||||
#define HTTP_CLIENT_ERROR_SOCKET_TIME_OUT 8
|
||||
/** Error while receiving data */
|
||||
#define HTTP_CLIENT_ERROR_SOCKET_RECV 9
|
||||
/** Error while sending data */
|
||||
#define HTTP_CLIENT_ERROR_SOCKET_SEND 10
|
||||
/** Error while receiving the remote HTTP headers */
|
||||
#define HTTP_CLIENT_ERROR_HEADER_RECV 11
|
||||
/** Could not find element within header */
|
||||
#define HTTP_CLIENT_ERROR_HEADER_NOT_FOUND 12
|
||||
/** The headers search clue was too large for the internal API buffer */
|
||||
#define HTTP_CLIENT_ERROR_HEADER_BIG_CLUE 13
|
||||
/** No content length was specified for the outgoing data. the caller should
|
||||
specify chunking mode in the session creation */
|
||||
#define HTTP_CLIENT_ERROR_HEADER_NO_LENGTH 14
|
||||
/** The HTTP chunk token that was received from the server was too big and possibly wrong */
|
||||
#define HTTP_CLIENT_ERROR_CHUNK_TOO_BIG 15
|
||||
/** Could not authenticate with the remote host */
|
||||
#define HTTP_CLIENT_ERROR_AUTH_HOST 16
|
||||
/** Could not authenticate with the remote proxy */
|
||||
#define HTTP_CLIENT_ERROR_AUTH_PROXY 17
|
||||
/** Bad or not supported HTTP verb was passed to a function */
|
||||
#define HTTP_CLIENT_ERROR_BAD_VERB 18
|
||||
/** a function received a parameter that was too large */
|
||||
#define HTTP_CLIENT_ERROR_LONG_INPUT 19
|
||||
/** The session state prevents the current function from proceeding */
|
||||
#define HTTP_CLIENT_ERROR_BAD_STATE 20
|
||||
/** Could not parse the chunk length while in chunked transfer */
|
||||
#define HTTP_CLIENT_ERROR_CHUNK 21
|
||||
/** Could not parse curtail elements from the URL (such as the host name, HTTP prefix act') */
|
||||
#define HTTP_CLIENT_ERROR_BAD_URL 22
|
||||
/** Could not detect key elements in the received headers */
|
||||
#define HTTP_CLIENT_ERROR_BAD_HEADER 23
|
||||
/** Error while attempting to resize a buffer */
|
||||
#define HTTP_CLIENT_ERROR_BUFFER_RSIZE 24
|
||||
/** Authentication schema is not supported */
|
||||
#define HTTP_CLIENT_ERROR_BAD_AUTH 25
|
||||
/** The selected authentication schema does not match the server response */
|
||||
#define HTTP_CLIENT_ERROR_AUTH_MISMATCH 26
|
||||
/** an element was missing while parsing the digest authentication challenge */
|
||||
#define HTTP_CLIENT_ERROR_NO_DIGEST_TOKEN 27
|
||||
/** Digest algorithem could be MD5 or MD5-sess other types are not supported */
|
||||
#define HTTP_CLIENT_ERROR_NO_DIGEST_ALG 28
|
||||
/** Binding error */
|
||||
#define HTTP_CLIENT_ERROR_SOCKET_BIND 29
|
||||
/** Tls negotiation error */
|
||||
#define HTTP_CLIENT_ERROR_TLS_NEGO 30
|
||||
/** Feature is not (yet) implemented */
|
||||
#define HTTP_CLIENT_ERROR_NOT_IMPLEMENTED 64
|
||||
/** HTTP end of stream message */
|
||||
#define HTTP_CLIENT_EOS 1000
|
||||
|
||||
// HTTP Session flags (Public flags)
|
||||
#define HTTP_CLIENT_FLAG_KEEP_ALIVE 0x00000001 // Set the keep alive header
|
||||
#define HTTP_CLIENT_FLAG_SEND_CHUNKED 0x00000002 // The outgoing should chunked
|
||||
#define HTTP_CLIENT_FLAG_NO_CACHE 0x00000004 // Set the no cache header
|
||||
#define HTTP_CLIENT_FLAG_ASYNC 0x00000008 // Currently not implemented
|
||||
#define HTTP_CLIENT_FLAG_MULTIPART_FORM 0x00000010 // The outgoing should multipart/form-data
|
||||
|
||||
// HTTP Type Definitions
|
||||
typedef UINT32 HTTP_SESSION_HANDLE;
|
||||
typedef UINT32 HTTP_CLIENT_SESSION_FLAGS;
|
||||
/******************************************************************************
|
||||
*
|
||||
* Section : HTTP API structures
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
/* HTTP Type Definitions */
|
||||
/** http seesion handle */
|
||||
typedef u32 tls_http_session_handle_t;
|
||||
/** http seesion flags */
|
||||
typedef u32 tls_http_session_flags_t;
|
||||
|
||||
/** HTTP Supported authentication methods */
|
||||
typedef enum _HTTP_AUTH_SCHEMA
|
||||
{
|
||||
AuthSchemaNone = 0,
|
||||
AuthSchemaBasic,
|
||||
AuthSchemaDigest,
|
||||
AuthSchemaKerberos,
|
||||
AuthNotSupported
|
||||
|
||||
} HTTP_AUTH_SCHEMA;
|
||||
/** HTTP supported verbs */
|
||||
typedef enum _HTTP_VERB
|
||||
{
|
||||
VerbGet = 0,
|
||||
VerbHead,
|
||||
VerbPost,
|
||||
VerbPut,
|
||||
VerbFwup,
|
||||
VerbNotSupported
|
||||
// Note: others verb such as connect and put are currently not supported
|
||||
|
||||
} HTTP_VERB;
|
||||
/** Data structure that the caller can request at any time that will include
|
||||
some information regarding the session */
|
||||
typedef struct _HTTP_CLIENT
|
||||
{
|
||||
UINT32 HTTPStatusCode; // HTTP Status code (200 OK)
|
||||
UINT32 RequestBodyLengthSent; // Total bytes sent (body only)
|
||||
UINT32 ResponseBodyLengthReceived; // Total bytes received (body only)
|
||||
UINT32 TotalResponseBodyLength; // as extracted from the “content-length" header
|
||||
UINT32 HttpState;
|
||||
} HTTP_CLIENT;
|
||||
|
||||
/** HTTP parameters */
|
||||
typedef struct _HTTPParameters
|
||||
{
|
||||
CHAR* Uri;
|
||||
CHAR* ProxyHost;
|
||||
UINT32 UseProxy ;
|
||||
UINT32 ProxyPort;
|
||||
UINT32 Verbose;
|
||||
CHAR* UserName;
|
||||
CHAR* Password;
|
||||
HTTP_AUTH_SCHEMA AuthType;
|
||||
|
||||
} HTTPParameters;
|
||||
|
||||
#if TLS_CONFIG_HTTP_CLIENT_TASK
|
||||
/** the callback function of http clent for received */
|
||||
typedef void (*http_client_recv_callback_fn)(HTTP_SESSION_HANDLE pSession, CHAR * data, UINT32 totallen, UINT32 datalen);
|
||||
/** the callback function of http clent for err */
|
||||
typedef void (*http_client_err_callback_fn)(HTTP_SESSION_HANDLE pSession, int err);
|
||||
|
||||
/** message of the http client */
|
||||
typedef struct _http_client_msg
|
||||
{
|
||||
HTTP_SESSION_HANDLE pSession;
|
||||
HTTPParameters param;
|
||||
HTTP_VERB method;
|
||||
CHAR* sendData;
|
||||
UINT32 dataLen;
|
||||
http_client_recv_callback_fn recv_fn;
|
||||
http_client_err_callback_fn err_fn;
|
||||
} http_client_msg;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @defgroup APP_APIs APP APIs
|
||||
* @brief APP APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup APP_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup HTTPC_APIs HTTPC APIs
|
||||
* @brief HTTP client APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup HTTPC_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
*
|
||||
* Section : HTTP API public interface
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Allocate memory for a new HTTP Session
|
||||
*
|
||||
* @param[in] Flags HTTP Session internal API flags, 0 should be passed here
|
||||
*
|
||||
* @retval 0 failed
|
||||
* @retval other HTTP Session handle
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
HTTP_SESSION_HANDLE HTTPClientOpenRequest (HTTP_CLIENT_SESSION_FLAGS Flags);
|
||||
/**
|
||||
* @brief Closes the active connection and free the corresponding memory
|
||||
*
|
||||
* @param[in] *pSession HTTP Session handle
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientCloseRequest (HTTP_SESSION_HANDLE *pSession);
|
||||
/**
|
||||
* @brief Sets the HTTP authentication schema
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
* @param[in] AuthSchema HTTP Supported authentication methods
|
||||
* @param[in] *pReserved Reserved parameter
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientSetAuth (HTTP_SESSION_HANDLE pSession, HTTP_AUTH_SCHEMA AuthSchema, void *pReserved);
|
||||
/**
|
||||
* @brief Sets credentials for the target host
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
* @param[in] *pUserName User name
|
||||
* @param[in] *pPassword Password
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientSetCredentials (HTTP_SESSION_HANDLE pSession, CHAR *pUserName, CHAR *pPassword);
|
||||
/**
|
||||
* @brief Sets all the proxy related parameters
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
* @param[in] *pProxyName The host name
|
||||
* @param[in] nPort The proxy port number
|
||||
* @param[in] *pUserName User name for proxy authentication (can be null)
|
||||
* @param[in] *pPassword User password for proxy authentication (can be null)
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientSetProxy (HTTP_SESSION_HANDLE pSession, CHAR *pProxyName, UINT16 nPort, CHAR *pUserName, CHAR *pPassword);
|
||||
/**
|
||||
* @brief Sets the HTTP verb for the outgoing request
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
* @param[in] HttpVerb HTTP supported verbs
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientSetVerb (HTTP_SESSION_HANDLE pSession, HTTP_VERB HttpVerb);
|
||||
/**
|
||||
* @brief Add headers into the outgoing request
|
||||
*
|
||||
* @param[in] pSession HTTP Session
|
||||
* @param[in] *pHeaderName The Header name
|
||||
* @param[in] *pHeaderData The header data
|
||||
* @param[in] nInsert Reserved, could be any
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientAddRequestHeaders (HTTP_SESSION_HANDLE pSession, CHAR *pHeaderName, CHAR *pHeaderData, BOOL nInsert);
|
||||
/**
|
||||
* @brief This function builds the request headers, performs a DNS resolution,
|
||||
* opens the connection (if it was not opened yet by a previous request
|
||||
* or if it has closed) and sends the request headers
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
* @param[in] *pUrl The requested URL
|
||||
* @param[in] *pData Data to post to the server
|
||||
* @param[in] nDataLength Length of posted data
|
||||
* @param[in] TotalLength Valid only when http method is post
|
||||
* TRUE: Post data to http server.
|
||||
* FALSE: In a post request without knowing the total
|
||||
* length in advance so return error or use chunking.
|
||||
* @param[in] nTimeout Operation timeout
|
||||
* @param[in] nClientPort Client side port 0 for none
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientSendRequest (HTTP_SESSION_HANDLE pSession, CHAR *pUrl, VOID *pData, UINT32 nDataLength, BOOL TotalLength, UINT32 nTimeout,UINT32 nClientPort);
|
||||
/**
|
||||
* @brief Write data to the remote server
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
* @param[in] *pBuffer Data to write to the server
|
||||
* @param[in] nBufferLength Length of wtitten data
|
||||
* @param[in] nTimeout Timeout for the operation
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientWriteData (HTTP_SESSION_HANDLE pSession, VOID *pBuffer, UINT32 nBufferLength, UINT32 nTimeout);
|
||||
/**
|
||||
* @brief Receives the response header on the connection and parses it.
|
||||
* Performs any required authentication.
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
* @param[in] nTimeout Timeout for the operation
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientRecvResponse (HTTP_SESSION_HANDLE pSession, UINT32 nTimeout);
|
||||
/**
|
||||
* @brief Read data from the server. Parse out the chunks data
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
* @param[out] *pBuffer A pointer to a buffer that will be filled with the servers response
|
||||
* @param[in] nBytesToRead The size of the buffer (numbers of bytes to read)
|
||||
* @param[in] nTimeout Operation timeout in seconds
|
||||
* @param[out] *nBytesRecived Count of the bytes that were received in this operation
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientReadData (HTTP_SESSION_HANDLE pSession, VOID *pBuffer, UINT32 nBytesToRead, UINT32 nTimeout, UINT32 *nBytesRecived);
|
||||
/**
|
||||
* @brief Fill the users structure with the session information
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
* @param[out] *HTTPClient The session information
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientGetInfo (HTTP_SESSION_HANDLE pSession, HTTP_CLIENT *HTTPClient);
|
||||
/**
|
||||
* @brief Initiate the headr searching functions and find the first header
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
* @param[in] *pSearchClue Search clue
|
||||
* @param[out] *pHeaderBuffer A pointer to a buffer that will be filled with the header name and value
|
||||
* @param[out] *nLength Count of the bytes that were received in this operation
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientFindFirstHeader (HTTP_SESSION_HANDLE pSession, CHAR *pSearchClue,CHAR *pHeaderBuffer, UINT32 *nLength);
|
||||
/**
|
||||
* @brief Find the next header.
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
* @param[out] *pHeaderBuffer A pointer to a buffer that will be filled with the header name and value
|
||||
* @param[out] *nLength Count of the bytes that were received in this operation
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientGetNextHeader (HTTP_SESSION_HANDLE pSession, CHAR *pHeaderBuffer, UINT32 *nLength);
|
||||
/**
|
||||
* @brief Terminate a headers search session
|
||||
*
|
||||
* @param[in] pSession HTTP Session handle
|
||||
*
|
||||
* @retval HTTP_CLIENT_SUCCESS success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
UINT32 HTTPClientFindCloseHeader (HTTP_SESSION_HANDLE pSession);
|
||||
|
||||
#if TLS_CONFIG_HTTP_CLIENT_TASK
|
||||
/**
|
||||
* @brief initialize task of the http client
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @retval WM_SUCCESS success
|
||||
* @retval WM_FAILED failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int http_client_task_init(void);
|
||||
|
||||
/**
|
||||
* @brief post message to the task of http client
|
||||
*
|
||||
* @param[in] msg pointer to the message
|
||||
*
|
||||
* @retval ERR_OK success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int http_client_post(http_client_msg * msg);
|
||||
#endif /* TLS_CONFIG_HTTP_CLIENT_TASK */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#endif /* WM_HTTP_CLIENT_H */
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
/**
|
||||
* @file wm_netif.h
|
||||
*
|
||||
* @brief ETHERNET INIT Interface
|
||||
*
|
||||
* @author winnermicro
|
||||
*
|
||||
* Copyright (c) 2015 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
#ifndef WM_NETIF_H
|
||||
#define WM_NETIF_H
|
||||
#include "wm_config.h"
|
||||
#include "wm_netif2.0.3.h"
|
||||
#endif /* WM_NETIF_H */
|
||||
|
||||
@@ -1,437 +0,0 @@
|
||||
/**
|
||||
* @file wm_netif2.0.3.h
|
||||
*
|
||||
* @brief netif203 module
|
||||
*
|
||||
* @author dave
|
||||
*
|
||||
* Copyright (c) 2014 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
|
||||
#ifndef WM_NETIF2_0_3_H
|
||||
#define WM_NETIF2_0_3_H
|
||||
|
||||
#include "wm_config.h"
|
||||
#include "wm_type_def.h"
|
||||
#include "wm_sockets.h"
|
||||
#include "wm_wifi.h"
|
||||
#include "wm_params.h"
|
||||
|
||||
/** MACRO for callback EVENT to join AP or create soft-AP successfully */
|
||||
#define NETIF_WIFI_JOIN_SUCCESS 0x1
|
||||
/** MACRO for callback EVENT to fail to join AP */
|
||||
#define NETIF_WIFI_JOIN_FAILED 0x2
|
||||
/** MACRO for callback EVENT to disconnect from AP or destroy soft-AP */
|
||||
#define NETIF_WIFI_DISCONNECTED 0x3
|
||||
/** MACRO for callbck EVENT to get IP address */
|
||||
#define NETIF_IP_NET_UP 0x4
|
||||
/** MACRO for callback EVNET to create AP successfully */
|
||||
#define NETIF_WIFI_SOFTAP_SUCCESS 0x5
|
||||
/** MACRO for callback EVNET to create soft-AP failed */
|
||||
#define NETIF_WIFI_SOFTAP_FAILED 0x6
|
||||
/** MACRO for callback EVNET to close soft-AP */
|
||||
#define NETIF_WIFI_SOFTAP_CLOSED 0x7
|
||||
/** MACRO for callback EVNET to inform soft ap's net */
|
||||
#define NETIF_IP_NET2_UP 0x8
|
||||
|
||||
#define NETIF_IPV6_NET_UP 0x9
|
||||
|
||||
/** These are the values for ip_addr_t.type */
|
||||
#define IPADDR_TYPE_V4 0U
|
||||
#define IPADDR_TYPE_V6 6U
|
||||
#define IPADDR_TYPE_ANY 46U
|
||||
|
||||
#define IPV6_ADDR_MAX_NUM 3
|
||||
|
||||
#if 0
|
||||
struct ip_addr {
|
||||
u32_t addr;
|
||||
};
|
||||
|
||||
typedef struct ip_addr ip_addr_t;
|
||||
#endif
|
||||
#if 0
|
||||
struct ip4_addr {
|
||||
u32_t addr;
|
||||
};
|
||||
typedef struct ip4_addr ip4_addr_t;
|
||||
|
||||
struct ip6_addr {
|
||||
u32_t addr[4];
|
||||
};
|
||||
typedef struct ip6_addr ip6_addr_t;
|
||||
|
||||
#if (TLS_CONFIG_IPV4 && TLS_CONFIG_IPV6)
|
||||
typedef struct _ip_addr {
|
||||
union {
|
||||
ip6_addr_t ip6;
|
||||
ip4_addr_t ip4;
|
||||
} u_addr;
|
||||
u8_t type;
|
||||
} ip_addr_t;
|
||||
#else
|
||||
#if TLS_CONFIG_IPV4
|
||||
typedef ip4_addr_t ip_addr_t;
|
||||
#else
|
||||
typedef ip6_addr_t ip_addr_t;
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
struct tls_ethif {
|
||||
ip_addr_t ip_addr;
|
||||
ip_addr_t netmask;
|
||||
ip_addr_t gw;
|
||||
#if TLS_CONFIG_IPV6
|
||||
ip_addr_t ip6_addr[IPV6_ADDR_MAX_NUM];
|
||||
#endif
|
||||
ip_addr_t dns1;
|
||||
ip_addr_t dns2;
|
||||
u8 status; //0:net down; 1:net up
|
||||
#if TLS_CONFIG_IPV6
|
||||
u8 ipv6_status[IPV6_ADDR_MAX_NUM]; //0:net down; 1:net up
|
||||
#endif
|
||||
};
|
||||
|
||||
//type defination of netif status changed callback.
|
||||
typedef void (*tls_netif_status_event_fn)(u8 status);
|
||||
|
||||
/**
|
||||
* @defgroup APP_APIs APP APIs
|
||||
* @brief APP APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup APP_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup NETIF_APIs NETIF APIs
|
||||
* @brief network interface APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup NETIF_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief This function is used to initialize TCP/IP Stack
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_ethernet_init(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to get IP information stored in
|
||||
tls_ethif struct
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval tls_ethif * Pointer to struct tls_ethif
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
struct tls_ethif * tls_netif_get_ethif(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to set tls_ethif status
|
||||
*
|
||||
* @param[in] status net status, 0-up, 1-down
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_netif_set_status(u8 status);
|
||||
|
||||
/**
|
||||
* @brief This function is used to start DHCP Client
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval Minus failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
err_t tls_dhcp_start(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to stop DHCP client
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval Minus failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
err_t tls_dhcp_stop(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to change IP information
|
||||
*
|
||||
* @param[in] *ipaddr IP address
|
||||
* @param[in] *netmask netmask
|
||||
* @param[in] *gw default gateway
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval Minus failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
err_t tls_netif_set_addr(ip_addr_t *ipaddr,
|
||||
ip_addr_t *netmask,
|
||||
ip_addr_t *gw);
|
||||
|
||||
/**
|
||||
* @brief This function is used to set dns servers
|
||||
*
|
||||
* @param[in] numdns index of the DNS server to set
|
||||
must be < DNS_MAX_SERVERS
|
||||
* @param[in] *dnsserver IP address of the DNS server to set
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_netif_dns_setserver(u8 numdns, ip_addr_t *dnsserver);
|
||||
|
||||
/**
|
||||
* @brief This function is used to bring up an interface,available
|
||||
for processing traffic
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval Minus failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
err_t tls_netif_set_up(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to bring down an interface,disabling
|
||||
any traffic processing
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval Minus failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
err_t tls_netif_set_down(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to add netif status changed callback
|
||||
to event list,if exists, do nothing
|
||||
*
|
||||
* @param[in] event_fn pointer to tls_netif_status_event_fn
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval Minus failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
err_t tls_netif_add_status_event(tls_netif_status_event_fn event_fn);
|
||||
|
||||
/**
|
||||
* @brief This function is used to remove netif status changed
|
||||
callback function from event list,if not exists, do nothing
|
||||
*
|
||||
* @param[in] event_fn pointer to tls_netif_status_event_fn
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval Minus failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
err_t tls_netif_remove_status_event(tls_netif_status_event_fn event_fn);
|
||||
|
||||
/**
|
||||
* @brief This function is used to get pointer of netif
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval pointer of netif
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
struct netif *tls_get_netif(void);
|
||||
|
||||
#if TLS_CONFIG_AP
|
||||
/**
|
||||
* @brief Start DHCP Server for a network interface
|
||||
* *
|
||||
* @retval DHCPS_ERR_SUCCESS - No error
|
||||
* @retval DHCPS_ERR_MEM - Out of memory
|
||||
* @retval DHCPS_ERR_LINKDOWN - The NI is inactive
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
INT8S tls_dhcps_start(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to stop DHCP Server
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_dhcps_stop(void);
|
||||
|
||||
/**
|
||||
* @brief Start the dns server's service
|
||||
* *
|
||||
* @retval DHCPS_ERR_SUCCESS - No error
|
||||
* @retval DHCPS_ERR_MEM - Out of memory
|
||||
* @retval DHCPS_ERR_LINKDOWN - The NI is inactive
|
||||
* @retval DNSS_ERR_PARAM - Input parameter error
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
INT8S tls_dnss_start(INT8U * DnsName);
|
||||
|
||||
/**
|
||||
* @brief Stop the dns server's service
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_dnss_stop(void);
|
||||
|
||||
/**
|
||||
* @brief Get station's ip address by mac address
|
||||
*
|
||||
* @param[in] mac station's mac address
|
||||
*
|
||||
* @retval ip_addr station's ip address
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
ip_addr_t *tls_dhcps_getip(const u8_t *mac);
|
||||
|
||||
/**
|
||||
* @brief Get station's mac address by ip address
|
||||
*
|
||||
* @param[in] ip station's ip address
|
||||
*
|
||||
* @retval u8* station's mac address
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
u8 *tls_dhcps_getmac(const ip_addr_t *ip);
|
||||
#endif //TLS_CONFIG_AP
|
||||
|
||||
#if TLS_CONFIG_RMMS
|
||||
/**
|
||||
* @brief Start remote manager server.
|
||||
* *
|
||||
* @retval DHCPS_ERR_SUCCESS - No error
|
||||
* @retval DHCPS_ERR_MEM - Out of memory
|
||||
* @retval DHCPS_ERR_LINKDOWN - The NIF is inactive
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
INT8S tls_rmms_start(void);
|
||||
|
||||
/**
|
||||
* @brief Disable remote manager server
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_rmms_stop(void);
|
||||
#endif
|
||||
|
||||
#if TLS_CONFIG_AP
|
||||
/**
|
||||
* @brief This is used to bring up an interface for APSTA,available
|
||||
for processing traffic
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval Minus failed
|
||||
*
|
||||
* @note Can only be used at APSTA mode
|
||||
*/
|
||||
err_t tls_netif2_set_up(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to bring down an interface for APSTA, disabling
|
||||
any traffic processing
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval Minus failed
|
||||
*
|
||||
* @note Can only be used at APSTA mode
|
||||
*/
|
||||
err_t tls_netif2_set_down(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to change IP information for
|
||||
a network interface for APSTA
|
||||
*
|
||||
* @param[in] *ipaddr IP address
|
||||
* @param[in] *netmask netmask
|
||||
* @param[in] *gw default gateway
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval Minus failed
|
||||
*
|
||||
* @note Can only be used at APSTA mode
|
||||
*/
|
||||
err_t tls_netif2_set_addr(ip_addr_t *ipaddr,
|
||||
ip_addr_t *netmask,
|
||||
ip_addr_t *gw);
|
||||
/***************************************************************************
|
||||
* Function: tls_dhcps_setdns
|
||||
* Description: Set dhcp server's dns address.
|
||||
*
|
||||
* Input: numdns: the index of the DNS server to set must be less than DNS_MAX_SERVERS
|
||||
*
|
||||
* Output: None
|
||||
*
|
||||
* Return: None
|
||||
*
|
||||
* Date : 2015-3-10
|
||||
****************************************************************************/
|
||||
/**
|
||||
* @brief Set dhcp server's dns address
|
||||
*
|
||||
* @param[in] numdns the index of the DNS server to set must be less than DNS_MAX_SERVERS
|
||||
*
|
||||
* @retval None
|
||||
*
|
||||
* @note Can only be used at APSTA mode
|
||||
*/
|
||||
void tls_dhcps_setdns(u8_t numdns);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#endif //WM_NETIF_H
|
||||
@@ -1,80 +0,0 @@
|
||||
/**
|
||||
* @file wm_ntp.h
|
||||
*
|
||||
* @brief ntp module
|
||||
*
|
||||
* @author dave
|
||||
*
|
||||
* Copyright (c) 2015 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
|
||||
#ifndef WM_NTP_H
|
||||
#define WM_NTP_H
|
||||
|
||||
#include "wm_type_def.h"
|
||||
|
||||
/**
|
||||
* @defgroup APP_APIs APP APIs
|
||||
* @brief APP APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup APP_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup NTP_APIs NTP APIs
|
||||
* @brief NTP APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup NTP_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief This function is used to get network time.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @retval time value
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
u32 tls_ntp_client(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to set ntp servers.
|
||||
*
|
||||
* @param[in] *ipaddr xxx.xxx.xxx.xxx
|
||||
* @param[in] server_no max num is three
|
||||
*
|
||||
* @retval WM_SUCCESS success
|
||||
* @retval WM_FAILED failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_ntp_set_server(char *ipaddr, int server_no);
|
||||
|
||||
/**
|
||||
* @brief This function is used to query params of the ntp servers
|
||||
*
|
||||
*
|
||||
* @retval WM_SUCCESS success
|
||||
* @retval WM_FAILED failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_ntp_query_sntpcfg(void);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#endif /* WM_NTP_H */
|
||||
|
||||
@@ -1,159 +0,0 @@
|
||||
#ifndef _SSL_SERVER_H_
|
||||
#define _SSL_SERVER_H_
|
||||
|
||||
#include "wm_config.h"
|
||||
#include "HTTPClientWrapper.h"
|
||||
|
||||
#if 1//for doxygen
|
||||
//#if TLS_CONFIG_SERVER_SIDE_SSL
|
||||
|
||||
#if TLS_CONFIG_USE_POLARSSL
|
||||
#include "polarssl/config.h"
|
||||
#include "polarssl/ssl.h"
|
||||
|
||||
#error "PolaSSL does not support ssl server now!"
|
||||
#elif TLS_CONFIG_USE_MBEDTLS
|
||||
typedef void tls_ssl_key_t;
|
||||
#endif
|
||||
//key type for tls_ssl_server_init
|
||||
#define KEY_RSA 1
|
||||
#define KEY_ECC 2
|
||||
#define KEY_DH 3
|
||||
|
||||
/**
|
||||
* @defgroup APP_APIs APP APIs
|
||||
* @brief APP APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup APP_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup SSL_SERVER_APIs SSL SERVER APIs
|
||||
* @brief SSL Server APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup SSL_SERVER_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief This function is used to initialize SSL Server
|
||||
*
|
||||
* @param[in] *arg proto version: 0 - sslv3
|
||||
* 1 - tls1.0
|
||||
* 2 - tls1.1
|
||||
* 3 - tls1.2
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_ssl_server_init(void * arg);
|
||||
|
||||
/**
|
||||
* @brief This function is used to set SSL keys
|
||||
*
|
||||
* @param[in] **keys SSL key pointer
|
||||
* @param[in] *certBuf SSL certificate
|
||||
* @param[in] certLen SSL certificate length
|
||||
* @param[in] *privBuf SSL private key
|
||||
* @param[in] privLen SSL private key length
|
||||
* @param[in] *CAbuf CA certificate
|
||||
* @param[in] CAlen CA certificate length
|
||||
* @param[in] keyType key type: KEY_RSA,KEY_ECC,KEY_DH
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_ssl_server_load_keys(tls_ssl_key_t **keys, unsigned char *certBuf,
|
||||
int32 certLen, unsigned char *privBuf, int32 privLen,
|
||||
unsigned char *CAbuf, int32 CAlen, int keyType);
|
||||
|
||||
/**
|
||||
* @brief This function is used to set SSL Server working
|
||||
*
|
||||
* @param[in] **ssl_p SSL hanlde
|
||||
* @param[in] fd socket number
|
||||
* @param[in] *keys SSL keys
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_ssl_server_handshake(tls_ssl_t **ssl_p, int fd, tls_ssl_key_t *keys);
|
||||
|
||||
/**
|
||||
* @brief This function is used to send data
|
||||
*
|
||||
* @param[in] *ssl SSL hanlde
|
||||
* @param[in] s socket number
|
||||
* @param[in] *sndbuf send buffer
|
||||
* @param[in] len send length
|
||||
* @param[in] flags some flags
|
||||
*
|
||||
* @retval > 0 success
|
||||
* @retval <=0 failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_ssl_server_send(tls_ssl_t *ssl, int s,char *sndbuf, int len,int flags);
|
||||
|
||||
/**
|
||||
* @brief This function is used to receive data
|
||||
*
|
||||
* @param[in] *ssl SSL hanlde
|
||||
* @param[in] s socket number
|
||||
* @param[in] *buf receive buffer
|
||||
* @param[in] len receive buffer length
|
||||
* @param[in] flags some flags
|
||||
*
|
||||
* @retval > 0 success
|
||||
* @retval <=0 failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_ssl_server_recv(tls_ssl_t *ssl,int s,char *buf, int len,int flags);
|
||||
|
||||
/**
|
||||
* @brief This function is used to close connection
|
||||
*
|
||||
* @param[in] *ssl SSL hanlde
|
||||
* @param[in] s socket number
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_ssl_server_close_conn(tls_ssl_t *ssl, int s);
|
||||
|
||||
/**
|
||||
* @brief This function is used to close SSL Server
|
||||
*
|
||||
* @param[in] *keys SSL keys
|
||||
*
|
||||
* @retval 0 success
|
||||
* @retval other failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_ssl_server_close(tls_ssl_key_t * keys);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#endif /*TLS_CONFIG_SERVER_SIDE_SSL*/
|
||||
#endif /*_SSL_SERVER_H_*/
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
/**
|
||||
* @file wm_webserver.h
|
||||
*
|
||||
* @brief WEB SERVER
|
||||
*
|
||||
* @author winnermicro
|
||||
*
|
||||
* Copyright (c) 2015 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
|
||||
#ifndef __WEBSERVER_H__
|
||||
#define __WEBSERVER_H__
|
||||
|
||||
/**
|
||||
* @defgroup APP_APIs APP APIs
|
||||
* @brief APP APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup APP_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup WEB_APIs WEB APIs
|
||||
* @brief WEB server APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup WEB_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief This function is used to start WEB SERVER service
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_webserver_init(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to deinit WEB SERVER service
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_webserver_deinit(void);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#endif /*__WEBSERVER_H__*/
|
||||
|
||||
@@ -1,291 +0,0 @@
|
||||
/**
|
||||
* @file wm_wifi_oneshot.h
|
||||
*
|
||||
* @brief Wi-Fi OneShot
|
||||
*
|
||||
* @author winnermicro
|
||||
*
|
||||
* Copyright (c) 2015 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
|
||||
#ifndef WM_WIFI_ONESHOT_H
|
||||
#define WM_WIFI_ONESHOT_H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <wm_type_def.h>
|
||||
#if (GCC_COMPILE==1)
|
||||
#include "wm_ieee80211_gcc.h"
|
||||
#else
|
||||
#include <wm_ieee80211.h>
|
||||
#endif
|
||||
#include "wm_config.h"
|
||||
#include "wm_bt_config.h"
|
||||
|
||||
/** DEBUG USE MAC FILTER START */
|
||||
#define CONFIG_ONESHOT_MAC_FILTER 0
|
||||
extern int tls_filter_module_srcmac(u8 *mac);
|
||||
|
||||
|
||||
|
||||
#define ONESHOT_ON 1
|
||||
#define ONESHOT_OFF 0
|
||||
|
||||
/* ONE SHOT */
|
||||
/** UDP MULTICAST ONE SHOT */
|
||||
#define TLS_CONFIG_UDP_ONE_SHOT ONESHOT_ON
|
||||
|
||||
/** WinnerMicro ONSHOT */
|
||||
#define TLS_CONFIG_UDP_LSD_SPECIAL (ONESHOT_ON&& TLS_CONFIG_UDP_ONE_SHOT)
|
||||
|
||||
/** AP ONESHOT */
|
||||
#define TLS_CONFIG_AP_MODE_ONESHOT (ONESHOT_ON && TLS_CONFIG_AP)
|
||||
#define TLS_CONFIG_WEB_SERVER_MODE (ONESHOT_ON && TLS_CONFIG_AP_MODE_ONESHOT)
|
||||
#define TLS_CONFIG_SOCKET_MODE (ONESHOT_ON && TLS_CONFIG_AP_MODE_ONESHOT)
|
||||
|
||||
|
||||
/** AIRKISS ONESHOT */
|
||||
#define TLS_CONFIG_AIRKISS_MODE_ONESHOT (ONESHOT_OFF && TLS_CONFIG_UDP_ONE_SHOT)
|
||||
#define AIRKISS_USE_SELF_WRITE 1
|
||||
|
||||
|
||||
/** BLE ONESHOT */
|
||||
#define TLS_CONFIG_BLE_WIFI_ONESHOT (ONESHOT_ON && (WM_BLE_INCLUDED == CFG_ON || WM_NIMBLE_INCLUDED == CFG_ON))
|
||||
|
||||
typedef enum{
|
||||
ONESHOT_SCAN_START,
|
||||
ONESHOT_SCAN_FINISHED,
|
||||
ONESHOT_SWITCH_CHANNEL,
|
||||
ONESHOT_STOP_TMP_CHAN_SWITCH,
|
||||
ONESHOT_STOP_CHAN_SWITCH,
|
||||
ONESHOT_HANDSHAKE_TIMEOUT,
|
||||
ONESHOT_RECV_TIMEOUT,
|
||||
ONESHOT_RECV_ERR,
|
||||
ONESHOT_STOP_DATA_CLEAR,
|
||||
ONESHOT_NET_UP,
|
||||
AP_SOCK_S_MSG_SOCKET_RECEIVE_DATA,
|
||||
AP_WEB_S_MSG_RECEIVE_DATA,
|
||||
AP_SOCK_S_MSG_SOCKET_CREATE,
|
||||
AP_SOCK_S_MSG_WJOIN_FAILD,
|
||||
}ONESHOT_MSG_ENUM;
|
||||
|
||||
/**
|
||||
* @defgroup APP_APIs APP APIs
|
||||
* @brief APP APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup APP_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup Oneshot_APIs Oneshot APIs
|
||||
* @brief Wi-Fi oneshot APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup Oneshot_APIs
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief This function is used to set oneshot flag.
|
||||
*
|
||||
* @param[in] flag, 0: one shot closed
|
||||
* 1: one shot open
|
||||
* 2: AP+socket
|
||||
* 3: AP+WEBSERVER
|
||||
* 4: bt
|
||||
*
|
||||
* @param[out] None
|
||||
*
|
||||
* @retval 0: success
|
||||
* -1: failed
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_wifi_set_oneshot_flag(u8 flag);
|
||||
|
||||
/**
|
||||
* @brief This function is used to get oneshot flag.
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @param[out] None
|
||||
*
|
||||
* @retval 0: one shot closed
|
||||
* 1: one shot open
|
||||
* 2: AP+socket
|
||||
* 3: AP+WEBSERVER
|
||||
* 4: bt
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_wifi_get_oneshot_flag(void);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Handle wild packets coming from the air.
|
||||
*
|
||||
* @param[in] *hdr point to ieee80211 data header
|
||||
* @param[in] data_len data len of ieee80211 data
|
||||
*
|
||||
* @retval no mean
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
u8 tls_wifi_dataframe_recv(struct ieee80211_hdr *hdr, u32 data_len);
|
||||
|
||||
#if TLS_CONFIG_AIRKISS_MODE_ONESHOT
|
||||
/**
|
||||
* @brief This function is used to acknowledge app when airkiss process is done.
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void oneshot_airkiss_send_reply(void);
|
||||
/**
|
||||
* @brief This function is used to deal with airkiss's
|
||||
wild packet
|
||||
*
|
||||
* @param[in] *data ieee80211 packet
|
||||
* @param[in] data_len packet length
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_airkiss_recv(u8 *data, u16 data_len);
|
||||
/**
|
||||
* @brief This function is used to start airkiss
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_airkiss_start(void);
|
||||
/**
|
||||
* @brief This function is used to stop airkiss
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_airkiss_stop(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to change channel for airkiss
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_oneshot_airkiss_change_channel(void);
|
||||
#endif /*TLS_CONFIG_AIRKISS_MODE_ONESHOT*/
|
||||
|
||||
/**
|
||||
* @brief This function is used to init oneshot task
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note Not in use now
|
||||
*/
|
||||
int wm_oneshot_task_init(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to stop oneshot timer
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_oneshot_switch_channel_tim_stop(struct ieee80211_hdr *hdr);
|
||||
|
||||
/**
|
||||
* @brief This function is used to stop oneshot temp timer
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_oneshot_switch_channel_tim_temp_stop(void);
|
||||
|
||||
/**
|
||||
* @brief handle if use bssid to connect wifi.
|
||||
*
|
||||
* @param[in] *ssid : ap name to connect
|
||||
* @param[in] *ssid_len: ap name's length to connect
|
||||
* @param[in] *bssid : ap bssid
|
||||
*
|
||||
* @retval no mean
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_oneshot_if_use_bssid(u8 *ssid, u8 *ssid_len, u8 *bssid);
|
||||
|
||||
/**
|
||||
* @brief Find channel according to ssid
|
||||
*
|
||||
* @param[in] *ssid ssid to be compared
|
||||
* @param[in] ssid_len ssid length
|
||||
* @param[out] chlist chlist to be add according to ssid info
|
||||
*
|
||||
* @retval None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_oneshot_find_chlist(u8 *ssid, u8 ssid_len, u16 *chlist);
|
||||
|
||||
/**
|
||||
* @brief This function is to deal with oneshot event according netif status.
|
||||
*
|
||||
* @param[in] status:net status
|
||||
*
|
||||
* @param[out] None
|
||||
*
|
||||
* @retval None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void wm_oneshot_netif_status_event(u8 status );
|
||||
|
||||
#if TLS_CONFIG_WEB_SERVER_MODE
|
||||
/**
|
||||
* @brief This function is used to send web config msg to oneshot task.
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @param[out] None
|
||||
*
|
||||
* @retval None
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_oneshot_send_web_connect_msg(void);
|
||||
#endif
|
||||
|
||||
#endif /*WM_WIFI_ONESHOT_H*/
|
||||
|
||||
@@ -921,9 +921,7 @@ __STATIC_INLINE uint32_t csi_vic_get_prio(int32_t IRQn)
|
||||
*/
|
||||
__STATIC_INLINE void csi_vic_set_vector(int32_t IRQn, uint32_t handler)
|
||||
{
|
||||
if (IRQn >= 0 && IRQn < 128) {
|
||||
irq_vectors[32 + IRQn] = handler;
|
||||
}
|
||||
OsSetVector(IRQn, handler);
|
||||
|
||||
csi_icache_invalid();
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
/**
|
||||
* @file wm_socket.h
|
||||
*
|
||||
* @brief socket Module
|
||||
*
|
||||
* @author dave
|
||||
*
|
||||
* Copyright (c) 2014 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
|
||||
#ifndef WM_SOCKET_H
|
||||
#define WM_SOCKET_H
|
||||
#include "wm_config.h"
|
||||
#include "wm_socket2.0.3.h"
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,422 +0,0 @@
|
||||
/**
|
||||
* @file wm_socket2.0.3.h
|
||||
*
|
||||
* @brief socket203 Module
|
||||
*
|
||||
* @author dave
|
||||
*
|
||||
* @copyright (c) 2014 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
#ifndef WM_SOCKET2_0_3_H
|
||||
#define WM_SOCKET2_0_3_H
|
||||
|
||||
#include "wm_type_def.h"
|
||||
#include "wm_netif.h"
|
||||
|
||||
//socket state defination
|
||||
#define NETCONN_STATE_NONE 0
|
||||
#define NETCONN_STATE_WAITING 1
|
||||
#define NETCONN_STATE_CONNECTED 2
|
||||
#define NETCONN_STATE_CLOSED 3
|
||||
|
||||
//socket event defination
|
||||
#define NET_EVENT_TCP_JOINED 0
|
||||
#define NET_EVENT_TCP_DISCONNECT 1
|
||||
#define NET_EVENT_TCP_CONNECTED 2
|
||||
#define NET_EVENT_TCP_CONNECT_FAILED 3
|
||||
#define NET_EVENT_UDP_START 4
|
||||
#define NET_EVENT_UDP_START_FAILED 5
|
||||
|
||||
#define TLS_MAX_SOCKET_NUM 4
|
||||
#define TLS_MAX_NETCONN_NUM 20
|
||||
|
||||
/** Main packet buffer struct */
|
||||
struct pbuf {
|
||||
/** next pbuf in singly linked pbuf chain */
|
||||
struct pbuf *next;
|
||||
|
||||
/** pointer to the actual data in the buffer */
|
||||
void *payload;
|
||||
|
||||
/**
|
||||
* total length of this buffer and all next buffers in chain
|
||||
* belonging to the same packet.
|
||||
*
|
||||
* For non-queue packet chains this is the invariant:
|
||||
* p->tot_len == p->len + (p->next? p->next->tot_len: 0)
|
||||
*/
|
||||
u16_t tot_len;
|
||||
|
||||
/** length of this buffer */
|
||||
u16_t len;
|
||||
|
||||
/** pbuf_type as u8_t instead of enum to save space */
|
||||
u8_t /*pbuf_type*/ type;
|
||||
|
||||
/** misc flags */
|
||||
u8_t flags;
|
||||
|
||||
/**
|
||||
* the reference count always equals the number of pointers
|
||||
* that refer to this pbuf. This can be pointers from an application,
|
||||
* the stack itself, or pbuf->next pointers from a chain.
|
||||
*/
|
||||
u16_t ref;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief This Function prototype for tcp error callback functions. Called when
|
||||
* receives a RST or is unexpectedly closed for any other reason.
|
||||
* The corresponding socket is already freed when this callback is called!
|
||||
*
|
||||
* @param[in] skt_num Is the socket number that returned by tls_socket_create function.
|
||||
*
|
||||
* @param[in] err Error code to indicate why the socket has been closed
|
||||
* ERR_ABRT: aborted through returning ERR_ABRT from within others
|
||||
* callback functions
|
||||
* ERR_RST: the connection was reset by the remote host
|
||||
*/
|
||||
typedef void (*socket_err_fn)(u8 skt_num, err_t err);
|
||||
|
||||
/**
|
||||
* @brief This Function prototype for socket receive callback functions. Called when data has
|
||||
* been received.
|
||||
*
|
||||
* @param[in] skt_num Is the socket number that returned by tls_socket_create function.
|
||||
*
|
||||
* @param[in] p The received data (or NULL when the connection has been closed!)
|
||||
*
|
||||
* @param[in] err An error code if there has been an error receiving, always be ERR_OK
|
||||
* when cs mode is udp.
|
||||
*
|
||||
* @retval The return value is only valid for tcp receive, for upd it means nothing.
|
||||
* ERR_OK: Return this value after handling the received data.
|
||||
* ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the
|
||||
* callback function!
|
||||
*/
|
||||
typedef err_t (*socket_recv_fn)(u8 skt_num, struct pbuf *p, err_t err);
|
||||
|
||||
/**
|
||||
* @brief This Function prototype for socket srce ip callback functions. Called when data has
|
||||
* been received.
|
||||
*
|
||||
* @param[in] skt_num Is the socket number that returned by tls_socket_create function.
|
||||
*
|
||||
* @param[in] datalen The received data length
|
||||
*
|
||||
* @param[in] ipsrc source ip addr
|
||||
*
|
||||
* @param[in] port source port
|
||||
*
|
||||
* @param[in] err An error code if there has been an error receiving, always be ERR_OK
|
||||
* when cs mode is udp.
|
||||
*
|
||||
* @retval The return value is only valid for UDP receive, for udp it means nothing.
|
||||
* ERR_OK: Return this value after handling the received data.
|
||||
* ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the
|
||||
* callback function!
|
||||
*/
|
||||
typedef err_t (*socket_recv_ip_rpt_fn)(u8 skt_num, u16 datalen, u8 *ipsrc, u16 port, err_t err);
|
||||
|
||||
/**
|
||||
* @brief This Function prototype for tcp connected callback functions. Called when
|
||||
* connected to the remote side.
|
||||
*
|
||||
* @param[in] skt_num Is the socket number that returned by tls_socket_create function.
|
||||
*
|
||||
* @param[in] err An unused error code, always ERR_OK currently.
|
||||
*
|
||||
* @retval ERR_OK: Return this value after handling your logic.
|
||||
* @retval ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the
|
||||
* callback function!
|
||||
*/
|
||||
typedef err_t (*socket_connected_fn)(u8 skt_num, err_t err);
|
||||
|
||||
/**
|
||||
* @brief This Function prototype for tcp poll callback functions. Called periodically.
|
||||
*
|
||||
* @param[in] skt_num Is the socket number that returned by tls_socket_create function.
|
||||
*
|
||||
* @retval ERR_OK: Try to do something periodically.
|
||||
* @retval ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the
|
||||
* callback function!
|
||||
*/
|
||||
typedef err_t (*socket_poll_fn)(u8 skt_num);
|
||||
|
||||
/**
|
||||
* @brief This Function prototype for tcp accept callback functions. Called when a new
|
||||
* connection can be accepted on a listening tcp.
|
||||
*
|
||||
* @param[in] skt_num Is the socket number that returned by tls_socket_create function.
|
||||
*
|
||||
* @param[in] err An error code if there has been an error accepting.
|
||||
*
|
||||
* @retval ERR_OK: Return this value after handling your logic.
|
||||
* @retval ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the
|
||||
* callback function!
|
||||
*/
|
||||
typedef err_t (*socket_accept_fn)(u8 skt_num, err_t err);
|
||||
|
||||
/**
|
||||
* @brief This Function prototype for socket state changed callback functions. Called when socket
|
||||
* the sockte's state changed.
|
||||
*
|
||||
* @param[in] skt_num Is the socket number that returned by tls_socket_create function.
|
||||
*
|
||||
* @param[in] event Is the event number, see socket event defination.
|
||||
*
|
||||
* @param[in] state Is the socket state, see socket state defination.
|
||||
*/
|
||||
typedef void(*socket_state_changed_fn)(u8 skt_num, u8 event, u8 state);
|
||||
|
||||
/** Definitions for error constants. */
|
||||
typedef enum {
|
||||
/** No error, everything OK. */
|
||||
ERR_OK = 0,
|
||||
/** Out of memory error. */
|
||||
ERR_MEM = -1,
|
||||
/** Buffer error. */
|
||||
ERR_BUF = -2,
|
||||
/** Timeout. */
|
||||
ERR_TIMEOUT = -3,
|
||||
/** Routing problem. */
|
||||
ERR_RTE = -4,
|
||||
/** Operation in progress */
|
||||
ERR_INPROGRESS = -5,
|
||||
/** Illegal value. */
|
||||
ERR_VAL = -6,
|
||||
/** Operation would block. */
|
||||
ERR_WOULDBLOCK = -7,
|
||||
/** Address in use. */
|
||||
ERR_USE = -8,
|
||||
/** Already connecting. */
|
||||
ERR_ALREADY = -9,
|
||||
/** Conn already established.*/
|
||||
ERR_ISCONN = -10,
|
||||
/** Not connected. */
|
||||
ERR_CONN = -11,
|
||||
/** Low-level netif error */
|
||||
ERR_IF = -12,
|
||||
|
||||
/** Connection aborted. */
|
||||
ERR_ABRT = -13,
|
||||
/** Connection reset. */
|
||||
ERR_RST = -14,
|
||||
/** Connection closed. */
|
||||
ERR_CLSD = -15,
|
||||
/** Illegal argument. */
|
||||
ERR_ARG = -16
|
||||
} err_enum_t;
|
||||
|
||||
enum tls_socket_protocol{
|
||||
SOCKET_PROTO_TCP, /* TCP Protocol */
|
||||
SOCKET_PROTO_UDP, /* UDP Protocol */
|
||||
};
|
||||
|
||||
enum tls_socket_cs_mode{
|
||||
SOCKET_CS_MODE_CLIENT, /* Client mode */
|
||||
SOCKET_CS_MODE_SERVER, /* Server mode */
|
||||
};
|
||||
|
||||
struct tls_socket_desc {
|
||||
enum tls_socket_cs_mode cs_mode; /* Server mode or Client mode, Only for tcp protocol is valid */
|
||||
enum tls_socket_protocol protocol; /* TCP Protocol or UDP Protocol */
|
||||
ip_addr_t ip_addr; /* Remote ip address, for tcp client mode is remote server's ip address; for tcp server mode can be any address. */
|
||||
/* for udp is remote server's ip address */
|
||||
u16 port; /* port, for tcp client mode is remote server's port; for tcp server mode is local listen port .
|
||||
for udp is remote server's port */
|
||||
u16 localport; /* local port, for udp and tcp client is local listen port, for tcp server means nothing, tcp server always listen at port */
|
||||
char host_name[32]; /* remote host name, not support for now */
|
||||
u8 host_len; /* the length of host name */
|
||||
u32 timeout; /* poll timeout, not implemented for now */
|
||||
socket_err_fn errf; /* a pointer to socket_err_fn */
|
||||
socket_recv_fn recvf; /* a pointer to socket_recv_fn */
|
||||
socket_connected_fn connf; /* a pointer to socket_connected_fn */
|
||||
socket_poll_fn pollf; /* a pointer to socket_poll_fn */
|
||||
socket_accept_fn acceptf; /* a pointer to socket_accept_fn */
|
||||
socket_state_changed_fn state_changed; /* a pointer to socket_state_changed_fn */
|
||||
socket_recv_ip_rpt_fn recvwithipf; /*recv skt info report*/
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief This function is called by your application code to create a socket.
|
||||
*
|
||||
* @param[in] skd Is a pointer to an tls_socket_desc.
|
||||
*
|
||||
* @retval ERR_OK If create socket successfully.
|
||||
* negative number If an error was detected.
|
||||
*/
|
||||
int tls_socket_create(struct tls_socket_desc * skd);
|
||||
|
||||
/**
|
||||
* @brief This function is called by your application code to send data by the socket.
|
||||
*
|
||||
* @param[in] skt_num Is the socket number that returned by tls_socket_create function.
|
||||
*
|
||||
* @param[in] pdata Is a pointer to the data which need to be send by the socket.
|
||||
*
|
||||
* @param[in] len The data's length.
|
||||
*
|
||||
* @retval ERR_OK If send data successfully.
|
||||
* negative number If an error was detected.
|
||||
*/
|
||||
int tls_socket_send(u8 skt_num, void *pdata, u16 len);
|
||||
|
||||
/**
|
||||
* @brief This function is called by your application code to close the socket, and the related resources would be released.
|
||||
*
|
||||
* @param[in] skt_num Is the socket number that returned by tls_socket_create function.
|
||||
*
|
||||
* @retval ERR_OK If close socket successfully.
|
||||
* negative number If an error was detected.
|
||||
*/
|
||||
int tls_socket_close(u8 skt_num);
|
||||
|
||||
struct tls_skt_status_ext_t {
|
||||
u8 socket;
|
||||
u8 status;
|
||||
enum tls_socket_protocol protocol;
|
||||
u8 host_ipaddr[4];
|
||||
u16 remote_port;
|
||||
u16 local_port;
|
||||
};
|
||||
|
||||
struct tls_skt_status_t {
|
||||
u32 socket_cnt;
|
||||
struct tls_skt_status_ext_t skts_ext[1];
|
||||
};
|
||||
/**
|
||||
* @brief This function is called by your application code to get the socket status of specified socket num.
|
||||
*
|
||||
* @param[in] skt_num Is the socket number that returned by tls_socket_create function.
|
||||
*
|
||||
* @param[in] buf Is a pointer to the data contains the socket status, if the socket is server, also contains it's client's status.
|
||||
*
|
||||
* @param[in] len The buf's length. At least, the len should be bigger than sizeof(struct tls_skt_status_t).
|
||||
*
|
||||
* @retval ERR_OK If send data successfully.
|
||||
* negative number If an error was detected.
|
||||
*/
|
||||
int tls_socket_get_status(u8 skt_num, u8 *buf, u32 bufsize);
|
||||
|
||||
/**
|
||||
* @brief This function is called by your application code to send data by udp socket.
|
||||
*
|
||||
* @param[in] localport This function will search all created sockets, if there is a socket whose localport equals this value and it's protocol is udp,
|
||||
* then send the data by this socket, otherwise, nothing to send.
|
||||
*
|
||||
* @param[in] ip_addr Is the remote ip address.
|
||||
*
|
||||
* @param[in] port Is the remote port which upd send to.
|
||||
*
|
||||
* @param[in] pdata Is a pointer to the data which need to be send by the socket.
|
||||
*
|
||||
* @param[in] len The data's length.
|
||||
* @retval ERR_OK If send data successfully.
|
||||
* negative number If an error was detected.
|
||||
*/
|
||||
int tls_socket_udp_sendto(u16 localport, u8 *ip_addr, u16 port, void *pdata, u16 len);
|
||||
|
||||
/**
|
||||
* @ingroup pbuf
|
||||
* Enumeration of pbuf layers
|
||||
*/
|
||||
typedef enum {
|
||||
/** Includes spare room for transport layer header, e.g. UDP header.
|
||||
* Use this if you intend to pass the pbuf to functions like udp_send().
|
||||
*/
|
||||
PBUF_TRANSPORT,
|
||||
/** Includes spare room for IP header.
|
||||
* Use this if you intend to pass the pbuf to functions like raw_send().
|
||||
*/
|
||||
PBUF_IP,
|
||||
/** Includes spare room for link layer header (ethernet header).
|
||||
* Use this if you intend to pass the pbuf to functions like ethernet_output().
|
||||
* @see PBUF_LINK_HLEN
|
||||
*/
|
||||
PBUF_LINK,
|
||||
/** Includes spare room for additional encapsulation header before ethernet
|
||||
* headers (e.g. 802.11).
|
||||
* Use this if you intend to pass the pbuf to functions like netif->linkoutput().
|
||||
* @see PBUF_LINK_ENCAPSULATION_HLEN
|
||||
*/
|
||||
PBUF_RAW_TX,
|
||||
/** Use this for input packets in a netif driver when calling netif->input()
|
||||
* in the most common case - ethernet-layer netif driver. */
|
||||
PBUF_RAW
|
||||
} pbuf_layer;
|
||||
|
||||
/**
|
||||
* @ingroup pbuf
|
||||
* Enumeration of pbuf types
|
||||
*/
|
||||
typedef enum {
|
||||
/** pbuf data is stored in RAM, used for TX mostly, struct pbuf and its payload
|
||||
are allocated in one piece of contiguous memory (so the first payload byte
|
||||
can be calculated from struct pbuf).
|
||||
pbuf_alloc() allocates PBUF_RAM pbufs as unchained pbufs (although that might
|
||||
change in future versions).
|
||||
This should be used for all OUTGOING packets (TX).*/
|
||||
PBUF_RAM,
|
||||
/** pbuf data is stored in ROM, i.e. struct pbuf and its payload are located in
|
||||
totally different memory areas. Since it points to ROM, payload does not
|
||||
have to be copied when queued for transmission. */
|
||||
PBUF_ROM,
|
||||
/** pbuf comes from the pbuf pool. Much like PBUF_ROM but payload might change
|
||||
so it has to be duplicated when queued before transmitting, depending on
|
||||
who has a 'ref' to it. */
|
||||
PBUF_REF,
|
||||
/** pbuf payload refers to RAM. This one comes from a pool and should be used
|
||||
for RX. Payload can be chained (scatter-gather RX) but like PBUF_RAM, struct
|
||||
pbuf and its payload are allocated in one piece of contiguous memory (so
|
||||
the first payload byte can be calculated from struct pbuf).
|
||||
Don't use this for TX, if the pool becomes empty e.g. because of TCP queuing,
|
||||
you are unable to receive TCP acks! */
|
||||
PBUF_POOL
|
||||
} pbuf_type;
|
||||
|
||||
/**
|
||||
* @brief This Function allocates a pbuf of the given type (possibly a chain for PBUF_POOL type).
|
||||
*
|
||||
* The actual memory allocated for the pbuf is determined by the
|
||||
* layer at which the pbuf is allocated and the requested size
|
||||
* (from the size parameter).
|
||||
*
|
||||
* @param[in] l layer flag to define header size
|
||||
* @param[in] length size of the pbuf's payload
|
||||
* @param[in] type this parameter decides how and where the pbuf
|
||||
*
|
||||
* @retval The allocated pbuf. If multiple pbufs where allocated, this
|
||||
* is the first pbuf of a pbuf chain.
|
||||
*/
|
||||
struct pbuf *pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type);
|
||||
|
||||
/**
|
||||
* @brief This Function for release the buffer that you receive within the socket_recv_fn callback function.
|
||||
* Attention please: If you return ERR_OK in the socket_recv_fn callback function, you must call this
|
||||
* function to release the buffer by yourself. Otherwise, the buffer do not need be
|
||||
* released by your code.
|
||||
*
|
||||
* @param[in] p The buffer you received in the socket_recv_fn callback function.
|
||||
*
|
||||
* @retval The number of de-allocated pbufs
|
||||
*/
|
||||
u8 pbuf_free(struct pbuf *p);
|
||||
|
||||
/**
|
||||
* @brief This Function for copy (part of) the contents of a packet buffer to an application supplied buffer.
|
||||
*
|
||||
* @param[in] p the pbuf from which to copy data.
|
||||
*
|
||||
* @param[in] dataptr the application supplied buffer
|
||||
*
|
||||
* @param[in] len length of data to copy (dataptr must be big enough). No more
|
||||
* than buf->tot_len will be copied, irrespective of len
|
||||
*
|
||||
* @param[in] offset offset into the packet buffer from where to begin copying len bytes
|
||||
*
|
||||
* @retval The number of bytes copied, or 0 on failure
|
||||
*/
|
||||
u16_t pbuf_copy_partial(const struct pbuf *p, void *dataptr, u16_t len, u16_t offset);
|
||||
#endif
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
/**
|
||||
* @file wm_sockets.h
|
||||
*
|
||||
* @brief socket apis
|
||||
*
|
||||
* @author winnermicro
|
||||
*
|
||||
* Copyright (c) 2014 Winner Microelectronics Co., Ltd.
|
||||
*/
|
||||
#ifndef WM_SOCKET_API_H
|
||||
#define WM_SOCKET_API_H
|
||||
#include "wm_config.h"
|
||||
#include "wm_sockets2.0.3.h"
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,13 +0,0 @@
|
||||
|
||||
#ifndef WM_CRYPTO_HARD_MBED_H
|
||||
#define WM_CRYPTO_HARD_MBED_H
|
||||
|
||||
#include "mbedtls/bignum.h"
|
||||
|
||||
#define MAX_HARD_EXPTMOD_BITLEN (2048)
|
||||
|
||||
int tls_crypto_mbedtls_exptmod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,24 @@
|
||||
*/
|
||||
#ifndef TLS_WIFI_FUNC_H
|
||||
#define TLS_WIFI_FUNC_H
|
||||
#include "list.h"
|
||||
|
||||
/** MACRO for callback EVENT to join AP or create soft-AP successfully */
|
||||
#define NETIF_WIFI_JOIN_SUCCESS 0x1
|
||||
/** MACRO for callback EVENT to fail to join AP */
|
||||
#define NETIF_WIFI_JOIN_FAILED 0x2
|
||||
/** MACRO for callback EVENT to disconnect from AP or destroy soft-AP */
|
||||
#define NETIF_WIFI_DISCONNECTED 0x3
|
||||
|
||||
/** MACRO for callback EVNET to create AP successfully */
|
||||
#define NETIF_WIFI_SOFTAP_SUCCESS 0x5
|
||||
/** MACRO for callback EVNET to create soft-AP failed */
|
||||
#define NETIF_WIFI_SOFTAP_FAILED 0x6
|
||||
/** MACRO for callback EVNET to close soft-AP */
|
||||
#define NETIF_WIFI_SOFTAP_CLOSED 0x7
|
||||
|
||||
|
||||
|
||||
|
||||
/* Return Error definition*/
|
||||
/** invalid SSID */
|
||||
@@ -323,6 +341,14 @@ typedef int (*net_rx_data_cb)(const u8 *bssid, u8 *buf, u32 buf_len);
|
||||
/** callback function of receive Wi-Fi data with some information of the physical layer */
|
||||
typedef void (*tls_wifi_data_ext_recv_callback)(u8* data, u32 data_len, struct tls_wifi_ext_t *ext);
|
||||
|
||||
/** wifi event status structure for user layer*/
|
||||
typedef void (*tls_wifi_netif_status_event_fn)(u8 status);
|
||||
struct tls_wifi_netif_status_event
|
||||
{
|
||||
struct dl_list list;
|
||||
tls_wifi_netif_status_event_fn status_callback;
|
||||
};
|
||||
|
||||
/**
|
||||
* @defgroup Wi-Fi_APIs Wi-Fi APIs
|
||||
* @brief Wi-Fi related APIs
|
||||
@@ -914,7 +940,7 @@ int tls_wps_start_pbc(void);
|
||||
*
|
||||
* @return None
|
||||
*
|
||||
* @note None
|
||||
* @note register ip rx callback when user porting lwip.
|
||||
*/
|
||||
void tls_ethernet_data_rx_callback(net_rx_data_cb callback);
|
||||
#if TLS_CONFIG_AP_OPT_FWD
|
||||
@@ -1158,5 +1184,72 @@ u8 tls_wifi_get_tempcomp_flag(void);
|
||||
|
||||
int tls_wl_get_isr_count(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to add wifi event function
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return 0-success
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_wifi_netif_add_status_event(tls_wifi_netif_status_event_fn event_fn);
|
||||
|
||||
/**
|
||||
* @brief This function is used to remove wifi event function
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return 0-success
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_wifi_netif_remove_status_event(tls_wifi_netif_status_event_fn event_fn);
|
||||
|
||||
/**
|
||||
* @brief This function is used to initialize wifi
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return 0-success
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
int tls_wifi_init(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to initialize wifi netif event list
|
||||
*
|
||||
* @param[in] None
|
||||
*
|
||||
* @return 0-success
|
||||
*
|
||||
* @note None
|
||||
*/
|
||||
void tls_wifi_netif_event_init(void);
|
||||
|
||||
/**
|
||||
* @brief This function is used to get wifi tx buffer when use tcp/ip tx
|
||||
*
|
||||
* @param[in] total_len:tx data len from tcp/ip output
|
||||
*
|
||||
* @return None-zero:available buffer, NULL:no buffer
|
||||
*
|
||||
* @note tls_wifi_buffer_acquire/tls_wifi_buffer_release must be used at pair
|
||||
*/
|
||||
u8* tls_wifi_buffer_acquire(int total_len);
|
||||
/**
|
||||
* @brief This function is used to tx buffer when use tcp/ip tx
|
||||
*
|
||||
* @param[in] is_apsta: always false if ap and sta use the same network interface
|
||||
* @param[in] buffer: tx data's buffer from tcp/ip
|
||||
* @return None
|
||||
*
|
||||
* @note tls_wifi_buffer_acquire/tls_wifi_buffer_release must be used at pair
|
||||
*/
|
||||
void tls_wifi_buffer_release(bool is_apsta, u8* buffer);
|
||||
|
||||
|
||||
|
||||
#endif /* TLS_WIFI_FUNC_H */
|
||||
|
||||
|
||||
@@ -70,14 +70,11 @@
|
||||
#include "wm_uart.h"
|
||||
#include "wm_gpio.h"
|
||||
#include "wm_hostspi.h"
|
||||
#include "wm_socket.h"
|
||||
#include "wm_sockets.h"
|
||||
#include "wm_wifi.h"
|
||||
#include "wm_hspi.h"
|
||||
#include "wm_pwm.h"
|
||||
#include "wm_params.h"
|
||||
#include "wm_osal.h"
|
||||
#include "wm_netif.h"
|
||||
#include "wm_efuse.h"
|
||||
#include "wm_mem.h"
|
||||
#include "wm_regs.h"
|
||||
|
||||
+209
-209
@@ -10,21 +10,21 @@
|
||||
#ifndef WM_REGS_H
|
||||
#define WM_REGS_H
|
||||
|
||||
typedef volatile unsigned char vu8;
|
||||
typedef volatile unsigned short vu16;
|
||||
typedef volatile unsigned long vu32;
|
||||
typedef volatile unsigned char vu8;
|
||||
typedef volatile unsigned short vu16;
|
||||
typedef volatile unsigned long vu32;
|
||||
|
||||
#define M8(adr) (*((vu8 *) (adr)))
|
||||
#define M16(adr) (*((vu16*) (adr)))
|
||||
#define M32(adr) (*((vu32*) (adr)))
|
||||
#define M8(adr) (*((vu8 *) (adr)))
|
||||
#define M16(adr) (*((vu16*) (adr)))
|
||||
#define M32(adr) (*((vu32*) (adr)))
|
||||
|
||||
#define ATTRIBUTE_ISR __attribute__((isr))
|
||||
#define ATTRIBUTE_ISR //__attribute__((isr))
|
||||
|
||||
typedef enum IRQn
|
||||
{
|
||||
/****** W800 specific Interrupt Numbers *********************************************************/
|
||||
SDIO_IRQn = 0, /*!< SDIO Receiver/Transmit/Command Receiver/SDIO Command Transmit Interrupt */
|
||||
MAC_IRQn = 1, /*!< MAC Interrupt */
|
||||
MAC_IRQn = 1, /*!< MAC Interrupt */
|
||||
RF_CFG_IRQn = 2, /*!< RF_CFG Interrupt */
|
||||
SEC_IRQn = 3, /*!< SEC RX/TX_MNGT/TX_DATA Interrupt */
|
||||
DMA_Channel0_IRQn = 4, /*!< DMA Channel 0 global Interrupt */
|
||||
@@ -33,20 +33,20 @@ typedef enum IRQn
|
||||
DMA_Channel3_IRQn = 7, /*!< DMA Channel 3 global Interrupt */
|
||||
DMA_Channel4_7_IRQn = 8, /*!< DMA Channel 4~7 global Interrupt */
|
||||
DMA_BRUST_IRQn = 9, /*!< DMA Burst global Interrupt */
|
||||
I2C_IRQn = 10, /*!< I2C Interrupts */
|
||||
ADC_IRQn = 11, /*!< ADC Convert Interrupts */
|
||||
I2C_IRQn = 10, /*!< I2C Interrupts */
|
||||
ADC_IRQn = 11, /*!< ADC Convert Interrupts */
|
||||
SPI_LS_IRQn = 12, /*!< Low Speed SPI Interrupt */
|
||||
SPI_HS_IRQn = 13, /*!< High Speed SPI Interrupt */
|
||||
GPIOA_IRQn = 14, /*!< GPIO A Interrupt */
|
||||
GPIOB_IRQn = 15, /*!< GPIO B Interrupt */
|
||||
UART0_IRQn = 16, /*!< UART0 Interrupts */
|
||||
UART1_IRQn = 17, /*!< UART1 Interrupt */
|
||||
UART1_IRQn = 17, /*!< UART1 Interrupt */
|
||||
TOUCH_IRQn = 18, /*!< Touch Sensor Iterrupt */
|
||||
UART24_IRQn = 19, /*!< UART2/3/4/5 Interrupt */
|
||||
BLE_IRQn = 20, /*!< BLE Interrupt */
|
||||
BT_IRQn = 21, /*!< BT Interrupt */
|
||||
PWM_IRQn = 22, /*!< PWM Interrupt */
|
||||
I2S_IRQn = 23, /*!< I2S Interrupts */
|
||||
PWM_IRQn = 22, /*!< PWM Interrupt */
|
||||
I2S_IRQn = 23, /*!< I2S Interrupts */
|
||||
SIDO_HOST_IRQn = 24, /*!< SDIO Host Interrupts */
|
||||
SYS_TICK_IRQn = 25, /*!< SYS Tick Interrupts */
|
||||
RSA_IRQn = 26, /*!< RSA Interrupts */
|
||||
@@ -54,7 +54,7 @@ typedef enum IRQn
|
||||
FLASH_IRQn = 28, /*!< Flash Interrupt */
|
||||
PMU_IRQn = 29, /*!< PMU Interrupt */
|
||||
TIMER_IRQn = 30, /*!< Timer0~5 Trigger */
|
||||
WDG_IRQn = 31, /*!< Watch Dog Interrupt */
|
||||
WDG_IRQn = 31, /*!< Watch Dog Interrupt */
|
||||
} IRQn_Type;
|
||||
|
||||
//#include "core_cm3.h"
|
||||
@@ -151,7 +151,7 @@ typedef volatile unsigned int TLS_REG; /* Hardware register definition */
|
||||
#define HR_DMA_BASE_ADDR (DEVICE_BASE_ADDR + 0x800)
|
||||
#define HR_DMA_INT_MASK (HR_DMA_BASE_ADDR + 0x0)
|
||||
#define HR_DMA_INT_SRC (HR_DMA_BASE_ADDR + 0x4)
|
||||
#define HR_DMA_CHNL_SEL (HR_DMA_BASE_ADDR + 0x08)
|
||||
#define HR_DMA_CHNL_SEL (HR_DMA_BASE_ADDR + 0x08)
|
||||
#define HR_DMA_CHNL0_SRC_ADDR (HR_DMA_BASE_ADDR + 0x10)
|
||||
#define HR_DMA_CHNL0_DEST_ADDR (HR_DMA_BASE_ADDR + 0x14)
|
||||
#define HR_DMA_CHNL0_SRC_WRAP_ADDR (HR_DMA_BASE_ADDR + 0x18)
|
||||
@@ -164,22 +164,22 @@ typedef volatile unsigned int TLS_REG; /* Hardware register definition */
|
||||
#define HR_DMA_CHNL0_LINK_DEST_ADDR (HR_DMA_BASE_ADDR + 0x34)
|
||||
#define HR_DMA_CHNL0_CURRENT_DEST_ADDR (HR_DMA_BASE_ADDR + 0x38)
|
||||
|
||||
#define DMA_STARTADDRESS HR_DMA_BASE_ADDR
|
||||
#define DMA_INTMASK_REG (*(volatile unsigned int*)(DMA_STARTADDRESS+0x000))
|
||||
#define DMA_INTSRC_REG (*(volatile unsigned int*)(DMA_STARTADDRESS+0x004))
|
||||
#define DMA_CHANNEL_SEL (*(volatile unsigned int*)(DMA_STARTADDRESS+0x008))
|
||||
#define DMA_CHNL_REG_BASE (DMA_STARTADDRESS+0x010)
|
||||
#define DMA_SRCADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x00))
|
||||
#define DMA_DESTADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x04))
|
||||
#define DMA_SRCWRAPADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x08))
|
||||
#define DMA_DESTWRAPADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x0C))
|
||||
#define DMA_WRAPSIZE_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x10))
|
||||
#define DMA_CHNLCTRL_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x14))
|
||||
#define DMA_MODE_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x18))
|
||||
#define DMA_CTRL_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x1C))
|
||||
#define DMA_STATUS_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x20))
|
||||
#define DMA_DESC_ADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x24))
|
||||
#define DMA_CURRDESTADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x28))
|
||||
#define DMA_STARTADDRESS HR_DMA_BASE_ADDR
|
||||
#define DMA_INTMASK_REG (*(volatile unsigned int*)(DMA_STARTADDRESS+0x000))
|
||||
#define DMA_INTSRC_REG (*(volatile unsigned int*)(DMA_STARTADDRESS+0x004))
|
||||
#define DMA_CHANNEL_SEL (*(volatile unsigned int*)(DMA_STARTADDRESS+0x008))
|
||||
#define DMA_CHNL_REG_BASE (DMA_STARTADDRESS+0x010)
|
||||
#define DMA_SRCADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x00))
|
||||
#define DMA_DESTADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x04))
|
||||
#define DMA_SRCWRAPADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x08))
|
||||
#define DMA_DESTWRAPADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x0C))
|
||||
#define DMA_WRAPSIZE_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x10))
|
||||
#define DMA_CHNLCTRL_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x14))
|
||||
#define DMA_MODE_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x18))
|
||||
#define DMA_CTRL_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x1C))
|
||||
#define DMA_STATUS_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x20))
|
||||
#define DMA_DESC_ADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x24))
|
||||
#define DMA_CURRDESTADDR_REG(ch) (*(volatile unsigned int*)(DMA_CHNL_REG_BASE + 0x30 * (ch /*- 1*/) +0x28))
|
||||
|
||||
#define DMA_CHNL_CTRL_CHNL_ON (1<<0)
|
||||
#define DMA_CHNL_CTRL_CHNL_OFF (1<<1)
|
||||
@@ -187,35 +187,35 @@ typedef volatile unsigned int TLS_REG; /* Hardware register definition */
|
||||
#define DMA_MODE_HARD_MODE (1<<0)
|
||||
#define DMA_MODE_CHAIN_MODE (1<<1)
|
||||
|
||||
#define DMA_MODE_SEL_MASK (0xF<<2)
|
||||
#define DMA_MODE_SEL_MASK (0xF<<2)
|
||||
#define DMA_MODE_SEL_UART_RX (0<<2)
|
||||
#define DMA_MODE_SEL_UART_TX (1<<2)
|
||||
#define DMA_MODE_SEL_PWM0 (2<<2)
|
||||
#define DMA_DODE_SEL_PWM1 (3<<2)
|
||||
#define DMA_MODE_SEL_PWM0 (2<<2)
|
||||
#define DMA_DODE_SEL_PWM1 (3<<2)
|
||||
#define DMA_MODE_SEL_LSSPI_RX (4<<2)
|
||||
#define DMA_MODE_SEL_LSSPI_TX (5<<2)
|
||||
#define DMA_MODE_SEL_SDADC_CH0 (6<<2)
|
||||
#define DMA_MODE_SEL_SDADC_CH1 (7<<2)
|
||||
#define DMA_MODE_SEL_SDADC_CH2 (8<<2)
|
||||
#define DMA_MODE_SEL_SDADC_CH3 (9<<2)
|
||||
#define DMA_MODE_SEL_I2S_RX (10<<2)
|
||||
#define DMA_MODE_SEL_I2S_TX (11<<2)
|
||||
#define DMA_MODE_SEL_SDIOHOST (12<<2)
|
||||
#define DMA_MODE_SEL_I2S_RX (10<<2)
|
||||
#define DMA_MODE_SEL_I2S_TX (11<<2)
|
||||
#define DMA_MODE_SEL_SDIOHOST (12<<2)
|
||||
|
||||
#define DMA_MODE_CHAIN_LINK_EN (1<<6)
|
||||
|
||||
#define DMA_CTRL_AUTO_RELOAD (1<<0)
|
||||
#define DMA_CTRL_SRC_ADDR_INC (1<<1)
|
||||
#define DMA_CTRL_SRC_ADDR_CIR (3<<1)
|
||||
#define DMA_CTRL_SRC_ADDR_CIR (3<<1)
|
||||
#define DMA_CTRL_DEST_ADDR_INC (1<<3)
|
||||
#define DMA_CTRL_DEST_ADDR_CIR (3<<3)
|
||||
#define DMA_CTRL_DEST_ADDR_CIR (3<<3)
|
||||
#define DMA_CTRL_DATA_SIZE_BYTE (0<<5)
|
||||
#define DMA_CTRL_DATA_SIZE_SHORT (1<<5)
|
||||
#define DMA_CTRL_DATA_SIZE_WORD (2<<5)
|
||||
#define DMA_CTRL_BURST_SIZE1 (0<<7)
|
||||
#define DMA_CTRL_BURST_SIZE4 (1<<7)
|
||||
|
||||
#define DMA_CTRL_TOTAL_MASK (0xFFF<<8)
|
||||
#define DMA_CTRL_TOTAL_MASK (0xFFF<<8)
|
||||
#define DMA_CTRL_TOTAL_SIZE(n) (n<<8)
|
||||
|
||||
/***************************************************************
|
||||
@@ -225,10 +225,10 @@ typedef volatile unsigned int TLS_REG; /* Hardware register definition */
|
||||
#define HR_PMU_PS_CR (HR_PMU_BASE_ADDR + 0x00)
|
||||
#define HR_PMU_TIMER0 (HR_PMU_BASE_ADDR + 0x04)
|
||||
#define HR_PMU_TIMER1 (HR_PMU_BASE_ADDR + 0x08)
|
||||
#define HR_PMU_RTC_CTRL1 (HR_PMU_BASE_ADDR + 0x0C)
|
||||
#define HR_PMU_RTC_CTRL1 (HR_PMU_BASE_ADDR + 0x0C)
|
||||
#define HR_PMU_RTC_CTRL2 (HR_PMU_BASE_ADDR + 0x10)
|
||||
#define HR_PMU_INTERRUPT_SRC (HR_PMU_BASE_ADDR + 0x14)
|
||||
#define HR_PMU_INTERRUPT_MASK (HR_PMU_BASE_ADDR + 0x18)
|
||||
#define HR_PMU_INTERRUPT_MASK (HR_PMU_BASE_ADDR + 0x18)
|
||||
#define HR_PMU_WLAN_STTS (HR_PMU_BASE_ADDR + 0x1C)
|
||||
#define HR_PMU_BK_REG (HR_PMU_BASE_ADDR + 0x20)
|
||||
|
||||
@@ -244,17 +244,17 @@ typedef volatile unsigned int TLS_REG; /* Hardware register definition */
|
||||
#define HR_CLK_GATE_EN HR_CLK_BASE_ADDR
|
||||
#define HR_CLK_SOFT_CLK_MASK (HR_CLK_BASE_ADDR + 0x04)
|
||||
#define HR_CLK_BBP_CLT_CTRL (HR_CLK_BASE_ADDR + 0x08)
|
||||
#define HR_CLK_RST_CTL (HR_CLK_BASE_ADDR + 0x0c)
|
||||
#define HR_CLK_DIV_CTL (HR_CLK_BASE_ADDR + 0x10)
|
||||
#define HR_CLK_SEL_CTL (HR_CLK_BASE_ADDR + 0x14)
|
||||
#define HR_OSC_32K_REG (HR_CLK_BASE_ADDR + 0x14)
|
||||
#define HR_CLK_I2S_CTL (HR_CLK_BASE_ADDR + 0x18)
|
||||
#define HR_CLK_RST_STA (HR_CLK_BASE_ADDR + 0x1C)
|
||||
#define HR_CLK_RST_CTL (HR_CLK_BASE_ADDR + 0x0c)
|
||||
#define HR_CLK_DIV_CTL (HR_CLK_BASE_ADDR + 0x10)
|
||||
#define HR_CLK_SEL_CTL (HR_CLK_BASE_ADDR + 0x14)
|
||||
#define HR_OSC_32K_REG (HR_CLK_BASE_ADDR + 0x14)
|
||||
#define HR_CLK_I2S_CTL (HR_CLK_BASE_ADDR + 0x18)
|
||||
#define HR_CLK_RST_STA (HR_CLK_BASE_ADDR + 0x1C)
|
||||
|
||||
/***************************************************************
|
||||
* ÄÚ´æÄ£¿é¼Ä´æÆ÷¶¨Òå
|
||||
***************************************************************/
|
||||
#define HR_MEM_BASE_ADDR (DEVICE_BASE_ADDR + 0xF00)
|
||||
#define HR_MEM_BASE_ADDR (DEVICE_BASE_ADDR + 0xF00)
|
||||
#define HR_MEM_TXBUF_BASE_ADDR (HR_MEM_BASE_ADDR + 0x00)
|
||||
#define HR_MEM_RXBUF_BASE_ADDR (HR_MEM_BASE_ADDR + 0x04)
|
||||
#define HR_MEM_BUF_NUM_CFG (HR_MEM_BASE_ADDR + 0x08)
|
||||
@@ -285,65 +285,65 @@ typedef volatile unsigned int TLS_REG; /* Hardware register definition */
|
||||
#define HR_I2C_CR (HR_I2C_BASE_ADDR + 0x18)
|
||||
|
||||
|
||||
#define I2C_CTRL_INT_DISABLE (0x1UL<<6)
|
||||
#define I2C_CTRL_ENABLE (0x1UL<<7)
|
||||
#define I2C_CTRL_INT_DISABLE (0x1UL<<6)
|
||||
#define I2C_CTRL_ENABLE (0x1UL<<7)
|
||||
|
||||
#define I2C_CR_STA (0x1UL<<7)
|
||||
#define I2C_CR_STO (0x1UL<<6)
|
||||
#define I2C_CR_RD (0x1UL<<5)
|
||||
#define I2C_CR_WR (0x1UL<<4)
|
||||
#define I2C_CR_NAK (0x1UL<<3)
|
||||
#define I2C_CR_IACK (0x1UL)
|
||||
#define I2C_CR_STA (0x1UL<<7)
|
||||
#define I2C_CR_STO (0x1UL<<6)
|
||||
#define I2C_CR_RD (0x1UL<<5)
|
||||
#define I2C_CR_WR (0x1UL<<4)
|
||||
#define I2C_CR_NAK (0x1UL<<3)
|
||||
#define I2C_CR_IACK (0x1UL)
|
||||
|
||||
#define I2C_SR_NAK (0x1UL<<7)
|
||||
#define I2C_SR_BUSY (0x1UL<<6)
|
||||
#define I2C_SR_TIP (0x1UL<<1)
|
||||
#define I2C_SR_IF (0x1UL)
|
||||
#define I2C_SR_NAK (0x1UL<<7)
|
||||
#define I2C_SR_BUSY (0x1UL<<6)
|
||||
#define I2C_SR_TIP (0x1UL<<1)
|
||||
#define I2C_SR_IF (0x1UL)
|
||||
|
||||
|
||||
/***************************************************************
|
||||
* SD ADC Reg
|
||||
***************************************************************/
|
||||
#define HR_SD_ADC_BASE_ADDR (HR_APB_BASE_ADDR + 0x200)
|
||||
#define HR_SD_ADC_RESULT_REG ((HR_SD_ADC_BASE_ADDR+0x000))
|
||||
#define HR_SD_ADC_ANA_CTRL ((HR_SD_ADC_BASE_ADDR+0x004))
|
||||
#define HR_SD_ADC_PGA_CTRL ((HR_SD_ADC_BASE_ADDR+0x008))
|
||||
#define HR_SD_ADC_TEMP_CTRL ((HR_SD_ADC_BASE_ADDR+0x00C))
|
||||
#define HR_SD_ADC_CTRL ((HR_SD_ADC_BASE_ADDR+0x010))
|
||||
#define HR_SD_ADC_INT_STATUS ((HR_SD_ADC_BASE_ADDR+0x014))
|
||||
#define HR_SD_ADC_CMP_VALUE ((HR_SD_ADC_BASE_ADDR+0x018))
|
||||
#define HR_SD_ADC_RESULT_REG ((HR_SD_ADC_BASE_ADDR+0x000))
|
||||
#define HR_SD_ADC_ANA_CTRL ((HR_SD_ADC_BASE_ADDR+0x004))
|
||||
#define HR_SD_ADC_PGA_CTRL ((HR_SD_ADC_BASE_ADDR+0x008))
|
||||
#define HR_SD_ADC_TEMP_CTRL ((HR_SD_ADC_BASE_ADDR+0x00C))
|
||||
#define HR_SD_ADC_CTRL ((HR_SD_ADC_BASE_ADDR+0x010))
|
||||
#define HR_SD_ADC_INT_STATUS ((HR_SD_ADC_BASE_ADDR+0x014))
|
||||
#define HR_SD_ADC_CMP_VALUE ((HR_SD_ADC_BASE_ADDR+0x018))
|
||||
|
||||
|
||||
/***************************************************************
|
||||
*Low Speed SPI Reg
|
||||
***************************************************************/
|
||||
#define HR_SPI_BASE_ADDR (HR_APB_BASE_ADDR + 0x400)
|
||||
#define HR_SPI_CHCFG_REG (HR_SPI_BASE_ADDR + 0x000)
|
||||
#define HR_SPI_SPICFG_REG (HR_SPI_BASE_ADDR + 0x004)
|
||||
#define HR_SPI_CLKCFG_REG (HR_SPI_BASE_ADDR + 0x008)
|
||||
#define HR_SPI_MODECFG_REG (HR_SPI_BASE_ADDR + 0x00C)
|
||||
#define HR_SPI_INT_MASK_REG (HR_SPI_BASE_ADDR + 0x010)
|
||||
#define HR_SPI_INT_STATUS_REG (HR_SPI_BASE_ADDR + 0x014)
|
||||
#define HR_SPI_STATUS_REG (HR_SPI_BASE_ADDR + 0x018)
|
||||
#define HR_SPI_TIMEOUT_REG (HR_SPI_BASE_ADDR + 0x01C)
|
||||
#define HR_SPI_TXDATA_REG (HR_SPI_BASE_ADDR + 0x020)
|
||||
#define HR_SPI_TRANSLEN_REG (HR_SPI_BASE_ADDR + 0x024)
|
||||
#define HR_SPI_SLV_XMIT_LEN_REG (HR_SPI_BASE_ADDR + 0x028)
|
||||
#define HR_SPI_RXDATA_REG (HR_SPI_BASE_ADDR + 0x030)
|
||||
#define HR_SPI_CHCFG_REG (HR_SPI_BASE_ADDR + 0x000)
|
||||
#define HR_SPI_SPICFG_REG (HR_SPI_BASE_ADDR + 0x004)
|
||||
#define HR_SPI_CLKCFG_REG (HR_SPI_BASE_ADDR + 0x008)
|
||||
#define HR_SPI_MODECFG_REG (HR_SPI_BASE_ADDR + 0x00C)
|
||||
#define HR_SPI_INT_MASK_REG (HR_SPI_BASE_ADDR + 0x010)
|
||||
#define HR_SPI_INT_STATUS_REG (HR_SPI_BASE_ADDR + 0x014)
|
||||
#define HR_SPI_STATUS_REG (HR_SPI_BASE_ADDR + 0x018)
|
||||
#define HR_SPI_TIMEOUT_REG (HR_SPI_BASE_ADDR + 0x01C)
|
||||
#define HR_SPI_TXDATA_REG (HR_SPI_BASE_ADDR + 0x020)
|
||||
#define HR_SPI_TRANSLEN_REG (HR_SPI_BASE_ADDR + 0x024)
|
||||
#define HR_SPI_SLV_XMIT_LEN_REG (HR_SPI_BASE_ADDR + 0x028)
|
||||
#define HR_SPI_RXDATA_REG (HR_SPI_BASE_ADDR + 0x030)
|
||||
|
||||
|
||||
#define SPIM_STARTADDRESS (HR_APB_BASE_ADDR + 0x400)
|
||||
#define SPIM_CHCFG_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x000))
|
||||
#define SPIM_SPICFG_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x004))
|
||||
#define SPIM_CLKCFG_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x008))
|
||||
#define SPIM_MODECFG_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x00C))
|
||||
#define SPIM_INTEN_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x010))
|
||||
#define SPIM_INTSRC_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x014))
|
||||
#define SPIM_SPISTATUS_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x018))
|
||||
#define SPIM_SPITIMEOUT_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x01C))
|
||||
#define SPIM_TXDATA_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x020))
|
||||
#define SPIM_TRANSLEN_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x024))
|
||||
#define SPIM_RXDATA_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x030))
|
||||
#define SPIM_STARTADDRESS (HR_APB_BASE_ADDR + 0x400)
|
||||
#define SPIM_CHCFG_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x000))
|
||||
#define SPIM_SPICFG_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x004))
|
||||
#define SPIM_CLKCFG_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x008))
|
||||
#define SPIM_MODECFG_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x00C))
|
||||
#define SPIM_INTEN_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x010))
|
||||
#define SPIM_INTSRC_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x014))
|
||||
#define SPIM_SPISTATUS_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x018))
|
||||
#define SPIM_SPITIMEOUT_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x01C))
|
||||
#define SPIM_TXDATA_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x020))
|
||||
#define SPIM_TRANSLEN_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x024))
|
||||
#define SPIM_RXDATA_REG (*(volatile unsigned int*)(SPIM_STARTADDRESS+0x030))
|
||||
|
||||
|
||||
/*RSA Reg*/
|
||||
@@ -455,9 +455,9 @@ typedef volatile unsigned int TLS_REG; /* Hardware register definition */
|
||||
***************************************************************/
|
||||
#define HR_UART0_BASE_ADDR (HR_APB_BASE_ADDR + 0x600)
|
||||
#define HR_UART1_BASE_ADDR (HR_APB_BASE_ADDR + 0x800)
|
||||
#define HR_UART2_BASE_ADDR (HR_APB_BASE_ADDR + 0xA00)
|
||||
#define HR_UART3_BASE_ADDR (HR_APB_BASE_ADDR + 0xC00)
|
||||
#define HR_UART4_BASE_ADDR (HR_APB_BASE_ADDR + 0xE00)
|
||||
#define HR_UART2_BASE_ADDR (HR_APB_BASE_ADDR + 0xA00)
|
||||
#define HR_UART3_BASE_ADDR (HR_APB_BASE_ADDR + 0xC00)
|
||||
#define HR_UART4_BASE_ADDR (HR_APB_BASE_ADDR + 0xE00)
|
||||
|
||||
#define HR_UART0_LINE_CTRL (HR_UART0_BASE_ADDR + 0x0)
|
||||
#define HR_UART0_FLOW_CTRL (HR_UART0_BASE_ADDR + 0x04)
|
||||
@@ -491,10 +491,10 @@ typedef volatile unsigned int TLS_REG; /* Hardware register definition */
|
||||
#define HR_UART2_FIFO_STATUS (HR_UART2_BASE_ADDR + 0x1C)
|
||||
#define HR_UART2_TX_WIN (HR_UART2_BASE_ADDR + 0x20)
|
||||
#define HR_UART2_RX_WIN (HR_UART2_BASE_ADDR + 0x30)
|
||||
#define HR_UART2_GUARD_TIME (HR_UART2_BASE_ADDR + 0x40)
|
||||
#define HR_UART2_WAIT_TIME (HR_UART2_BASE_ADDR + 0x44)
|
||||
#define HR_UART2_GT (HR_UART2_BASE_ADDR + 0x40)
|
||||
#define HR_UART2_WT (HR_UART2_BASE_ADDR + 0x44)
|
||||
#define HR_UART2_GUARD_TIME (HR_UART2_BASE_ADDR + 0x40)
|
||||
#define HR_UART2_WAIT_TIME (HR_UART2_BASE_ADDR + 0x44)
|
||||
#define HR_UART2_GT (HR_UART2_BASE_ADDR + 0x40)
|
||||
#define HR_UART2_WT (HR_UART2_BASE_ADDR + 0x44)
|
||||
|
||||
#define HR_UART3_LINE_CTRL (HR_UART3_BASE_ADDR + 0x0)
|
||||
#define HR_UART3_FLOW_CTRL (HR_UART3_BASE_ADDR + 0x04)
|
||||
@@ -615,72 +615,72 @@ typedef volatile unsigned int TLS_REG; /* Hardware register definition */
|
||||
#define HR_GPIOA_BASE_ADDR (HR_APB_BASE_ADDR + 0x1200)
|
||||
#define HR_GPIOA_DATA (HR_GPIOA_BASE_ADDR + 0x0)
|
||||
#define HR_GPIOA_DATA_EN (HR_GPIOA_BASE_ADDR + 0x04)
|
||||
#define HR_GPIOA_DATA_DIR (HR_GPIOA_BASE_ADDR + 0x08)
|
||||
#define HR_GPIOA_DATA_PULLEN (HR_GPIOA_BASE_ADDR + 0x0c)
|
||||
#define HR_GPIOA_AFSEL (HR_GPIOA_BASE_ADDR + 0x10)
|
||||
#define HR_GPIOA_AFS1 (HR_GPIOA_BASE_ADDR + 0x14)
|
||||
#define HR_GPIOA_AFS0 (HR_GPIOA_BASE_ADDR + 0x18)
|
||||
#define HR_GPIOA_IS (HR_GPIOA_BASE_ADDR + 0x20)
|
||||
#define HR_GPIOA_IBE (HR_GPIOA_BASE_ADDR + 0x24)
|
||||
#define HR_GPIOA_IEV (HR_GPIOA_BASE_ADDR + 0x28)
|
||||
#define HR_GPIOA_IE (HR_GPIOA_BASE_ADDR + 0x2C)
|
||||
#define HR_GPIOA_RIS (HR_GPIOA_BASE_ADDR + 0x30)
|
||||
#define HR_GPIOA_MIS (HR_GPIOA_BASE_ADDR + 0x34)
|
||||
#define HR_GPIOA_IC (HR_GPIOA_BASE_ADDR + 0x38)
|
||||
#define HR_GPIOA_DATA_DIR (HR_GPIOA_BASE_ADDR + 0x08)
|
||||
#define HR_GPIOA_DATA_PULLEN (HR_GPIOA_BASE_ADDR + 0x0c)
|
||||
#define HR_GPIOA_AFSEL (HR_GPIOA_BASE_ADDR + 0x10)
|
||||
#define HR_GPIOA_AFS1 (HR_GPIOA_BASE_ADDR + 0x14)
|
||||
#define HR_GPIOA_AFS0 (HR_GPIOA_BASE_ADDR + 0x18)
|
||||
#define HR_GPIOA_IS (HR_GPIOA_BASE_ADDR + 0x20)
|
||||
#define HR_GPIOA_IBE (HR_GPIOA_BASE_ADDR + 0x24)
|
||||
#define HR_GPIOA_IEV (HR_GPIOA_BASE_ADDR + 0x28)
|
||||
#define HR_GPIOA_IE (HR_GPIOA_BASE_ADDR + 0x2C)
|
||||
#define HR_GPIOA_RIS (HR_GPIOA_BASE_ADDR + 0x30)
|
||||
#define HR_GPIOA_MIS (HR_GPIOA_BASE_ADDR + 0x34)
|
||||
#define HR_GPIOA_IC (HR_GPIOA_BASE_ADDR + 0x38)
|
||||
|
||||
#define HR_GPIOB_BASE_ADDR (HR_APB_BASE_ADDR + 0x1400)
|
||||
#define HR_GPIOB_DATA (HR_GPIOB_BASE_ADDR + 0x0)
|
||||
#define HR_GPIOB_DATA_EN (HR_GPIOB_BASE_ADDR + 0x04)
|
||||
#define HR_GPIOB_DATA_DIR (HR_GPIOB_BASE_ADDR + 0x08)
|
||||
#define HR_GPIOB_DATA_PULLEN (HR_GPIOB_BASE_ADDR + 0x0c)
|
||||
#define HR_GPIOB_AFSEL (HR_GPIOB_BASE_ADDR + 0x10)
|
||||
#define HR_GPIOB_AFS1 (HR_GPIOB_BASE_ADDR + 0x14)
|
||||
#define HR_GPIOB_AFS0 (HR_GPIOB_BASE_ADDR + 0x18)
|
||||
#define HR_GPIOB_IS (HR_GPIOB_BASE_ADDR + 0x20)
|
||||
#define HR_GPIOB_IBE (HR_GPIOB_BASE_ADDR + 0x24)
|
||||
#define HR_GPIOB_IEV (HR_GPIOB_BASE_ADDR + 0x28)
|
||||
#define HR_GPIOB_IE (HR_GPIOB_BASE_ADDR + 0x2C)
|
||||
#define HR_GPIOB_RIS (HR_GPIOB_BASE_ADDR + 0x30)
|
||||
#define HR_GPIOB_MIS (HR_GPIOB_BASE_ADDR + 0x34)
|
||||
#define HR_GPIOB_IC (HR_GPIOB_BASE_ADDR + 0x38)
|
||||
#define HR_GPIOB_DATA_DIR (HR_GPIOB_BASE_ADDR + 0x08)
|
||||
#define HR_GPIOB_DATA_PULLEN (HR_GPIOB_BASE_ADDR + 0x0c)
|
||||
#define HR_GPIOB_AFSEL (HR_GPIOB_BASE_ADDR + 0x10)
|
||||
#define HR_GPIOB_AFS1 (HR_GPIOB_BASE_ADDR + 0x14)
|
||||
#define HR_GPIOB_AFS0 (HR_GPIOB_BASE_ADDR + 0x18)
|
||||
#define HR_GPIOB_IS (HR_GPIOB_BASE_ADDR + 0x20)
|
||||
#define HR_GPIOB_IBE (HR_GPIOB_BASE_ADDR + 0x24)
|
||||
#define HR_GPIOB_IEV (HR_GPIOB_BASE_ADDR + 0x28)
|
||||
#define HR_GPIOB_IE (HR_GPIOB_BASE_ADDR + 0x2C)
|
||||
#define HR_GPIOB_RIS (HR_GPIOB_BASE_ADDR + 0x30)
|
||||
#define HR_GPIOB_MIS (HR_GPIOB_BASE_ADDR + 0x34)
|
||||
#define HR_GPIOB_IC (HR_GPIOB_BASE_ADDR + 0x38)
|
||||
|
||||
|
||||
#define IO_PA_BASE_ADDR (HR_APB_BASE_ADDR + 0x1200)
|
||||
#define IO_PA_DATA (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x0))
|
||||
#define IO_PA_EN (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x4))
|
||||
#define IO_PA_DIR (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x8))
|
||||
#define IO_PA_REN (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0xC))
|
||||
#define IO_PA_AFSEL (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x10))
|
||||
#define IO_PA_AFS1 (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x14))
|
||||
#define IO_PA_AFS0 (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x18))
|
||||
#define IO_PA_BASE_ADDR (HR_APB_BASE_ADDR + 0x1200)
|
||||
#define IO_PA_DATA (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x0))
|
||||
#define IO_PA_EN (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x4))
|
||||
#define IO_PA_DIR (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x8))
|
||||
#define IO_PA_REN (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0xC))
|
||||
#define IO_PA_AFSEL (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x10))
|
||||
#define IO_PA_AFS1 (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x14))
|
||||
#define IO_PA_AFS0 (*(volatile unsigned int*)(IO_PA_BASE_ADDR + 0x18))
|
||||
|
||||
#define IO_PB_BASE_ADDR (HR_APB_BASE_ADDR + 0x1400)
|
||||
#define IO_PB_DATA (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x0))
|
||||
#define IO_PB_EN (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x4))
|
||||
#define IO_PB_DIR (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x8))
|
||||
#define IO_PB_REN (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0xC))
|
||||
#define IO_PB_AFSEL (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x10))
|
||||
#define IO_PB_AFS1 (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x14))
|
||||
#define IO_PB_AFS0 (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x18))
|
||||
#define IO_PB_BASE_ADDR (HR_APB_BASE_ADDR + 0x1400)
|
||||
#define IO_PB_DATA (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x0))
|
||||
#define IO_PB_EN (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x4))
|
||||
#define IO_PB_DIR (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x8))
|
||||
#define IO_PB_REN (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0xC))
|
||||
#define IO_PB_AFSEL (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x10))
|
||||
#define IO_PB_AFS1 (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x14))
|
||||
#define IO_PB_AFS0 (*(volatile unsigned int*)(IO_PB_BASE_ADDR + 0x18))
|
||||
|
||||
/***************************************************************
|
||||
* TIMER Reg
|
||||
***************************************************************/
|
||||
#define HR_TIMER_BASE_ADDR (HR_APB_BASE_ADDR + 0x1800)
|
||||
#define HR_TIMER_CFG (HR_TIMER_BASE_ADDR + 0x0)
|
||||
#define HR_TIMER0_5_CSR (HR_TIMER_BASE_ADDR + 0x04)
|
||||
#define HR_TIMER0_PRD (HR_TIMER_BASE_ADDR + 0x08)
|
||||
#define HR_TIMER1_PRD (HR_TIMER_BASE_ADDR + 0x0C)
|
||||
#define HR_TIMER2_PRD (HR_TIMER_BASE_ADDR + 0x10)
|
||||
#define HR_TIMER3_PRD (HR_TIMER_BASE_ADDR + 0x14)
|
||||
#define HR_TIMER4_PRD (HR_TIMER_BASE_ADDR + 0x18)
|
||||
#define HR_TIMER5_PRD (HR_TIMER_BASE_ADDR + 0x1C)
|
||||
#define HR_TIMER0_CNT (HR_TIMER_BASE_ADDR + 0x20)
|
||||
#define HR_TIMER1_CNT (HR_TIMER_BASE_ADDR + 0x24)
|
||||
#define HR_TIMER2_CNT (HR_TIMER_BASE_ADDR + 0x28)
|
||||
#define HR_TIMER3_CNT (HR_TIMER_BASE_ADDR + 0x2C)
|
||||
#define HR_TIMER4_CNT (HR_TIMER_BASE_ADDR + 0x30)
|
||||
#define HR_TIMER5_CNT (HR_TIMER_BASE_ADDR + 0x34)
|
||||
#define HR_TIMER0_5_CSR (HR_TIMER_BASE_ADDR + 0x04)
|
||||
#define HR_TIMER0_PRD (HR_TIMER_BASE_ADDR + 0x08)
|
||||
#define HR_TIMER1_PRD (HR_TIMER_BASE_ADDR + 0x0C)
|
||||
#define HR_TIMER2_PRD (HR_TIMER_BASE_ADDR + 0x10)
|
||||
#define HR_TIMER3_PRD (HR_TIMER_BASE_ADDR + 0x14)
|
||||
#define HR_TIMER4_PRD (HR_TIMER_BASE_ADDR + 0x18)
|
||||
#define HR_TIMER5_PRD (HR_TIMER_BASE_ADDR + 0x1C)
|
||||
#define HR_TIMER0_CNT (HR_TIMER_BASE_ADDR + 0x20)
|
||||
#define HR_TIMER1_CNT (HR_TIMER_BASE_ADDR + 0x24)
|
||||
#define HR_TIMER2_CNT (HR_TIMER_BASE_ADDR + 0x28)
|
||||
#define HR_TIMER3_CNT (HR_TIMER_BASE_ADDR + 0x2C)
|
||||
#define HR_TIMER4_CNT (HR_TIMER_BASE_ADDR + 0x30)
|
||||
#define HR_TIMER5_CNT (HR_TIMER_BASE_ADDR + 0x34)
|
||||
|
||||
/* timer csr bits defintion */
|
||||
#define TLS_TIMER_MS_UNIT(n) (1UL<<(0+5*n))
|
||||
@@ -696,77 +696,77 @@ typedef volatile unsigned int TLS_REG; /* Hardware register definition */
|
||||
#define HR_WDG_LOAD_VALUE (HR_WDG_BASE_ADDR + 0x00)
|
||||
#define HR_WDG_CUR_VALUE (HR_WDG_BASE_ADDR + 0x04)
|
||||
#define HR_WDG_CTRL (HR_WDG_BASE_ADDR + 0x08)
|
||||
#define HR_WDG_INT_CLR (HR_WDG_BASE_ADDR + 0x0C)
|
||||
#define HR_WDG_INT_CLR (HR_WDG_BASE_ADDR + 0x0C)
|
||||
#define HR_WDG_INT_SRC (HR_WDG_BASE_ADDR + 0x10)
|
||||
#define HR_WDG_INT_MIS (HR_WDG_BASE_ADDR + 0x14)
|
||||
#define HR_WDG_LOCK (HR_WDG_BASE_ADDR + 0x40)
|
||||
|
||||
/** bit field of the lcd gate control in CLK gating register */
|
||||
#define HR_CLK_LCD_GATE_Pos (14)
|
||||
#define HR_CLK_LCD_GATE_Pos (14)
|
||||
|
||||
/***********************************************************//**
|
||||
* LCD Reg
|
||||
***************************************************************/
|
||||
|
||||
#define HR_LCD_REG_BASE (HR_APB_BASE_ADDR + 0x1C00) //(0x4001 1C00)
|
||||
#define HR_LCD_CR (HR_LCD_REG_BASE+0x000)
|
||||
#define HR_LCD_FRAME_CNT (HR_LCD_REG_BASE+0x004)
|
||||
#define HR_LCD_COM0_SEG (HR_LCD_REG_BASE+0x008)
|
||||
#define HR_LCD_COM1_SEG (HR_LCD_REG_BASE+0x00C)
|
||||
#define HR_LCD_COM2_SEG (HR_LCD_REG_BASE+0x010)
|
||||
#define HR_LCD_COM3_SEG (HR_LCD_REG_BASE+0x014)
|
||||
#define HR_LCD_COM4_SEG (HR_LCD_REG_BASE+0x018)
|
||||
#define HR_LCD_COM5_SEG (HR_LCD_REG_BASE+0x01C)
|
||||
#define HR_LCD_COM6_SEG (HR_LCD_REG_BASE+0x020)
|
||||
#define HR_LCD_COM7_SEG (HR_LCD_REG_BASE+0x024)
|
||||
#define HR_LCD_COM_EN (HR_LCD_REG_BASE+0x028)
|
||||
#define HR_LCD_SEG_EN (HR_LCD_REG_BASE+0x02C)
|
||||
#define HR_LCD_REG_BASE (HR_APB_BASE_ADDR + 0x1C00) //(0x4001 1C00)
|
||||
#define HR_LCD_CR (HR_LCD_REG_BASE+0x000)
|
||||
#define HR_LCD_FRAME_CNT (HR_LCD_REG_BASE+0x004)
|
||||
#define HR_LCD_COM0_SEG (HR_LCD_REG_BASE+0x008)
|
||||
#define HR_LCD_COM1_SEG (HR_LCD_REG_BASE+0x00C)
|
||||
#define HR_LCD_COM2_SEG (HR_LCD_REG_BASE+0x010)
|
||||
#define HR_LCD_COM3_SEG (HR_LCD_REG_BASE+0x014)
|
||||
#define HR_LCD_COM4_SEG (HR_LCD_REG_BASE+0x018)
|
||||
#define HR_LCD_COM5_SEG (HR_LCD_REG_BASE+0x01C)
|
||||
#define HR_LCD_COM6_SEG (HR_LCD_REG_BASE+0x020)
|
||||
#define HR_LCD_COM7_SEG (HR_LCD_REG_BASE+0x024)
|
||||
#define HR_LCD_COM_EN (HR_LCD_REG_BASE+0x028)
|
||||
#define HR_LCD_SEG_EN (HR_LCD_REG_BASE+0x02C)
|
||||
|
||||
#define LCD_CR_EN_Pos (8)
|
||||
#define LCD_CR_PD_Pos (9)
|
||||
#define LCD_CR_EN_Pos (8)
|
||||
#define LCD_CR_PD_Pos (9)
|
||||
|
||||
#define LCD_VDD_ON (1UL<<9)
|
||||
#define LCD_VDD_OFF (0UL<<9)
|
||||
#define LCD_EN (1UL<<8)
|
||||
#define LCD_VDD_ON (1UL<<9)
|
||||
#define LCD_VDD_OFF (0UL<<9)
|
||||
#define LCD_EN (1UL<<8)
|
||||
|
||||
#define LCD_BIAS_MASK (3UL<<6)
|
||||
#define LCD_BIAS_MASK_Pos (6)
|
||||
#define LCD_BIAS_ONEFOURTH (0UL<<6)
|
||||
#define LCD_BIAS_ONEHALF (1UL<<6)
|
||||
#define LCD_BIAS_ONETHIRD (2UL<<6)
|
||||
#define LCD_BIAS_STATIC (3UL<<6)
|
||||
#define LCD_BIAS_MASK (3UL<<6)
|
||||
#define LCD_BIAS_MASK_Pos (6)
|
||||
#define LCD_BIAS_ONEFOURTH (0UL<<6)
|
||||
#define LCD_BIAS_ONEHALF (1UL<<6)
|
||||
#define LCD_BIAS_ONETHIRD (2UL<<6)
|
||||
#define LCD_BIAS_STATIC (3UL<<6)
|
||||
|
||||
#define LCD_VLCD_MASK (7UL<<3)
|
||||
#define LCD_VLCD_MASK_Pos (3)
|
||||
#define LCD_VLCD_27 (0UL<<3)
|
||||
#define LCD_VLCD_29 (1UL<<3)
|
||||
#define LCD_VLCD_31 (2UL<<3)
|
||||
#define LCD_VLCD_33 (3UL<<3)
|
||||
#define LCD_VLCD_MASK (7UL<<3)
|
||||
#define LCD_VLCD_MASK_Pos (3)
|
||||
#define LCD_VLCD_27 (0UL<<3)
|
||||
#define LCD_VLCD_29 (1UL<<3)
|
||||
#define LCD_VLCD_31 (2UL<<3)
|
||||
#define LCD_VLCD_33 (3UL<<3)
|
||||
|
||||
#define LCD_DUTY_MASK (7UL<<0)
|
||||
#define LCD_DUTY_MASK_Pos (0)
|
||||
#define LCD_DUTY_STATIC (0UL<<0)
|
||||
#define LCD_DUTY_ONEHALF (1UL<<0)
|
||||
#define LCD_DUTY_ONETHIRD (2UL<<0)
|
||||
#define LCD_DUTY_ONEFOURTH (3UL<<0)
|
||||
#define LCD_DUTY_ONEFIFTH (4UL<<0)
|
||||
#define LCD_DUTY_ONESIXTH (5UL<<0)
|
||||
#define LCD_DUTY_ONESEVENTH (6UL<<0)
|
||||
#define LCD_DUTY_ONEEIGHTH (7UL<<0)
|
||||
#define LCD_DUTY_MASK (7UL<<0)
|
||||
#define LCD_DUTY_MASK_Pos (0)
|
||||
#define LCD_DUTY_STATIC (0UL<<0)
|
||||
#define LCD_DUTY_ONEHALF (1UL<<0)
|
||||
#define LCD_DUTY_ONETHIRD (2UL<<0)
|
||||
#define LCD_DUTY_ONEFOURTH (3UL<<0)
|
||||
#define LCD_DUTY_ONEFIFTH (4UL<<0)
|
||||
#define LCD_DUTY_ONESIXTH (5UL<<0)
|
||||
#define LCD_DUTY_ONESEVENTH (6UL<<0)
|
||||
#define LCD_DUTY_ONEEIGHTH (7UL<<0)
|
||||
|
||||
/***********************************************************//**
|
||||
* I2S Reg
|
||||
***************************************************************/
|
||||
#define HR_CLK_I2S_GATE_Pos (10)
|
||||
#define HR_CLK_I2S_GATE_Pos (10)
|
||||
|
||||
|
||||
#define HR_I2S_REG_BASE (HR_APB_BASE_ADDR+0x2000)
|
||||
#define HR_I2S_CTRL (HR_I2S_REG_BASE+0x000)
|
||||
#define HR_I2S_INT_MASK (HR_I2S_REG_BASE+0x004)
|
||||
#define HR_I2S_INT_SRC (HR_I2S_REG_BASE+0x008)
|
||||
#define HR_I2S_STATUS (HR_I2S_REG_BASE+0x00C)
|
||||
#define HR_I2S_TX (HR_I2S_REG_BASE+0x010)
|
||||
#define HR_I2S_RX (HR_I2S_REG_BASE+0x014)
|
||||
#define HR_I2S_REG_BASE (HR_APB_BASE_ADDR+0x2000)
|
||||
#define HR_I2S_CTRL (HR_I2S_REG_BASE+0x000)
|
||||
#define HR_I2S_INT_MASK (HR_I2S_REG_BASE+0x004)
|
||||
#define HR_I2S_INT_SRC (HR_I2S_REG_BASE+0x008)
|
||||
#define HR_I2S_STATUS (HR_I2S_REG_BASE+0x00C)
|
||||
#define HR_I2S_TX (HR_I2S_REG_BASE+0x010)
|
||||
#define HR_I2S_RX (HR_I2S_REG_BASE+0x014)
|
||||
|
||||
/*****************************************************************/
|
||||
/* APB2SPI modem spi access register definition(BT) */
|
||||
@@ -861,7 +861,7 @@ typedef volatile unsigned int TLS_REG; /* Hardware register definition */
|
||||
#define HR_TC16_TRESHOLD_REG (HR_TC_REG_BASE+0x0040)
|
||||
#define HR_TC16_COUNT_REG (HR_TC_REG_BASE+0x0040)
|
||||
|
||||
#define HR_TC_INT_EN (HR_TC_REG_BASE+0x0044) /*bit 31:16*/
|
||||
#define HR_TC_INT_EN (HR_TC_REG_BASE+0x0044) /*bit 31:16*/
|
||||
#define HR_TC_INT_STATUS (HR_TC_REG_BASE+0x0044) /*bit 15:0*/
|
||||
|
||||
|
||||
@@ -931,11 +931,11 @@ static __inline unsigned int tls_reg_read32(unsigned int reg)
|
||||
*/
|
||||
static __inline void tls_bitband_write(volatile unsigned int addr, unsigned int bit, unsigned int val)
|
||||
{
|
||||
unsigned int temp;
|
||||
unsigned int temp;
|
||||
|
||||
temp = (M32(addr) & ~(1 << bit)) | (val << bit);
|
||||
temp = (M32(addr) & ~(1 << bit)) | (val << bit);
|
||||
|
||||
*((volatile unsigned int * )addr) = temp;
|
||||
*((volatile unsigned int * )addr) = temp;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -951,11 +951,11 @@ static __inline void tls_bitband_write(volatile unsigned int addr, unsigned int
|
||||
*/
|
||||
static __inline unsigned int tls_bitband_read(volatile unsigned int addr, unsigned int bit)
|
||||
{
|
||||
unsigned int temp;
|
||||
unsigned int temp;
|
||||
|
||||
temp = (M32(addr) >> bit) & 0x1;
|
||||
temp = (M32(addr) >> bit) & 0x1;
|
||||
|
||||
return *((volatile unsigned int *)temp);
|
||||
return *((volatile unsigned int *)temp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -32,11 +32,7 @@ kernel_module(module_name) {
|
||||
"xt804/libc/liteos/math/fmodl.c",
|
||||
"xt804/libc/liteos/math/scalbn.c",
|
||||
"xt804/libc/liteos/math/scalbnl.c",
|
||||
"xt804/gcc/los_context.c",
|
||||
"xt804/gcc/los_dispatch.S",
|
||||
"xt804/gcc/los_interrupt.c",
|
||||
"xt804/gcc/los_timer.c",
|
||||
]
|
||||
include_dirs = [ "xt804/gcc/" ]
|
||||
include_dirs = []
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
* @date 02. June 2017
|
||||
******************************************************************************/
|
||||
#include <csi_config.h>
|
||||
#include "wm_config.h"
|
||||
#include "wm_regs.h"
|
||||
|
||||
extern void systick_handler(void);
|
||||
@@ -45,7 +46,6 @@ extern void PMU_TIMER1_IRQHandler(void);
|
||||
extern void PMU_GPIO_WAKE_IRQHandler(void);
|
||||
extern void PMU_RTC_IRQHandler(void);
|
||||
|
||||
#define ATTRIBUTE_ISR __attribute__((isr))
|
||||
|
||||
#define readl(addr) \
|
||||
({ unsigned int __v = (*(volatile unsigned int *) (addr)); __v; })
|
||||
@@ -58,8 +58,24 @@ extern void PMU_RTC_IRQHandler(void);
|
||||
#define CSI_INTRPT_EXIT()
|
||||
#endif
|
||||
|
||||
int csi_kernel_intrpt_enter(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int csi_kernel_intrpt_exit(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
void HalPreInterruptHandler(uint32_t arg)
|
||||
{
|
||||
if( arg == 57)
|
||||
{
|
||||
readl(0xE000E010);// clear tick irq
|
||||
}
|
||||
}
|
||||
|
||||
//static int tick_test = 0;
|
||||
ATTRIBUTE_ISR void CORET_IRQHandler(void)
|
||||
ATTRIBUTE_ISR void CORET_IRQ_Handler(void)
|
||||
{
|
||||
#ifndef CONFIG_KERNEL_FREERTOS
|
||||
CSI_INTRPT_ENTER();
|
||||
@@ -82,7 +98,7 @@ ATTRIBUTE_ISR void CORET_IRQHandler(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
ATTRIBUTE_ISR void SDIO_IRQHandler(void)
|
||||
ATTRIBUTE_ISR void SDIO_IRQ_Handler(void)
|
||||
{
|
||||
CSI_INTRPT_ENTER();
|
||||
#ifndef WM_WIFI_SIMULATION_PROJECT
|
||||
@@ -91,14 +107,14 @@ ATTRIBUTE_ISR void SDIO_IRQHandler(void)
|
||||
CSI_INTRPT_EXIT();
|
||||
}
|
||||
|
||||
ATTRIBUTE_ISR void GPSEC_IRQHandler(void)
|
||||
ATTRIBUTE_ISR void GPSEC_IRQ_Handler(void)
|
||||
{
|
||||
CSI_INTRPT_ENTER();
|
||||
CRYPTION_IRQHandler();
|
||||
CSI_INTRPT_EXIT();
|
||||
}
|
||||
|
||||
ATTRIBUTE_ISR void RSA_IRQHandler(void)
|
||||
ATTRIBUTE_ISR void RSA_IRQ_Handler(void)
|
||||
{
|
||||
CSI_INTRPT_ENTER();
|
||||
RSA_F_IRQHandler();
|
||||
@@ -106,7 +122,7 @@ ATTRIBUTE_ISR void RSA_IRQHandler(void)
|
||||
}
|
||||
|
||||
|
||||
ATTRIBUTE_ISR void TIM0_5_IRQHandler(void)
|
||||
ATTRIBUTE_ISR void TIM0_5_IRQ_Handler(void)
|
||||
{
|
||||
CSI_INTRPT_ENTER();
|
||||
TIMER0_5_IRQHandler();
|
||||
@@ -114,7 +130,7 @@ ATTRIBUTE_ISR void TIM0_5_IRQHandler(void)
|
||||
}
|
||||
|
||||
|
||||
ATTRIBUTE_ISR void SPI_HS_IRQHandler(void)
|
||||
ATTRIBUTE_ISR void SPI_HS_IRQ_Handler(void)
|
||||
{
|
||||
CSI_INTRPT_ENTER();
|
||||
#ifndef WM_WIFI_SIMULATION_PROJECT
|
||||
@@ -123,7 +139,7 @@ ATTRIBUTE_ISR void SPI_HS_IRQHandler(void)
|
||||
CSI_INTRPT_EXIT();
|
||||
}
|
||||
|
||||
ATTRIBUTE_ISR void MAC_IRQHandler(void)
|
||||
ATTRIBUTE_ISR void MAC_IRQ_Handler(void)
|
||||
{
|
||||
CSI_INTRPT_ENTER();
|
||||
#ifndef CONFIG_NO_WIFI
|
||||
@@ -132,7 +148,7 @@ ATTRIBUTE_ISR void MAC_IRQHandler(void)
|
||||
CSI_INTRPT_EXIT();
|
||||
}
|
||||
|
||||
ATTRIBUTE_ISR void SEC_IRQHandler(void)
|
||||
ATTRIBUTE_ISR void SEC_IRQ_Handler(void)
|
||||
{
|
||||
CSI_INTRPT_ENTER();
|
||||
#ifndef CONFIG_NO_WIFI
|
||||
@@ -141,7 +157,7 @@ ATTRIBUTE_ISR void SEC_IRQHandler(void)
|
||||
CSI_INTRPT_EXIT();
|
||||
}
|
||||
|
||||
ATTRIBUTE_ISR void PMU_IRQHandler(void)
|
||||
ATTRIBUTE_ISR void PMU_IRQ_Handler(void)
|
||||
{
|
||||
CSI_INTRPT_ENTER();
|
||||
#ifndef CONFIG_NO_WIFI
|
||||
|
||||
@@ -76,8 +76,6 @@ void mdelay(uint32_t ms)
|
||||
*/
|
||||
void SystemInit(void)
|
||||
{
|
||||
__set_VBR((uint32_t) & (irq_vectors));
|
||||
|
||||
#if defined(CONFIG_SEPARATE_IRQ_SP) && !defined(CONFIG_KERNEL_NONE)
|
||||
/* 801 not supported */
|
||||
__set_Int_SP((uint32_t)&g_top_irqstack);
|
||||
@@ -97,6 +95,5 @@ void SystemInit(void)
|
||||
|
||||
//csi_coret_config(g_system_clock / CONFIG_SYSTICK_HZ, SYS_TICK_IRQn); //10ms
|
||||
//#ifndef CONFIG_KERNEL_NONE
|
||||
csi_vic_enable_irq(SYS_TICK_IRQn);
|
||||
//#endif
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ kernel_module(module_name) {
|
||||
"params/wm_param.c",
|
||||
"utils/utils.c",
|
||||
"crypto/wm_crypto_hard.c",
|
||||
#"crypto/wm_crypto_hard_mbed.c",
|
||||
]
|
||||
include_dirs = []
|
||||
}
|
||||
|
||||
@@ -1,619 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include "core_804.h"
|
||||
#include "wm_irq.h"
|
||||
#include "wm_regs.h"
|
||||
#include "wm_debug.h"
|
||||
#include "wm_pmu.h"
|
||||
#include "wm_crypto_hard.h"
|
||||
#include "wm_crypto_hard_mbed.h"
|
||||
|
||||
#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
|
||||
#define biL (ciL << 3) /* bits in limb */
|
||||
#define biH (ciL << 2) /* half limb size */
|
||||
|
||||
extern struct wm_crypto_ctx g_crypto_ctx;
|
||||
static void rsaMonMulSetLen(const u32 len)
|
||||
{
|
||||
RSAN = len;
|
||||
}
|
||||
static void rsaMonMulWriteMc(const u32 mc)
|
||||
{
|
||||
u32 val = 0;
|
||||
RSAMC = mc;
|
||||
val = RSAMC;
|
||||
if(val == mc)
|
||||
{
|
||||
val = 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
static void rsaMonMulWriteA(const u32 *const in)
|
||||
{
|
||||
memcpy((u32 *)&RSAXBUF, in, RSAN * sizeof(u32));
|
||||
}
|
||||
static void rsaMonMulWriteB(const u32 *const in)
|
||||
{
|
||||
memcpy((u32 *)&RSAYBUF, in, RSAN * sizeof(u32));
|
||||
}
|
||||
static void rsaMonMulWriteM(const u32 *const in)
|
||||
{
|
||||
memcpy((u32 *)&RSAMBUF, in, RSAN * sizeof(u32));
|
||||
}
|
||||
static void rsaMonMulReadA(u32 *const in)
|
||||
{
|
||||
memcpy(in, (u32 *)&RSAXBUF, RSAN * sizeof(u32));
|
||||
}
|
||||
static void rsaMonMulReadB(u32 *const in)
|
||||
{
|
||||
memcpy(in, (u32 *)&RSAYBUF, RSAN * sizeof(u32));
|
||||
}
|
||||
static void rsaMonMulReadD(u32 *const in)
|
||||
{
|
||||
memcpy(in, (u32 *)&RSADBUF, RSAN * sizeof(u32));
|
||||
}
|
||||
static int rsaMulModRead(unsigned char w, mbedtls_mpi *a)
|
||||
{
|
||||
u32 in[64];
|
||||
int err = 0;
|
||||
memset(in, 0, 64 * sizeof(u32));
|
||||
switch(w)
|
||||
{
|
||||
case 'A':
|
||||
rsaMonMulReadA(in);
|
||||
break;
|
||||
case 'B':
|
||||
rsaMonMulReadB(in);
|
||||
break;
|
||||
case 'D':
|
||||
rsaMonMulReadD(in);
|
||||
break;
|
||||
}
|
||||
mp_reverse((unsigned char *)in, RSAN * sizeof(u32));
|
||||
if ((err = mbedtls_mpi_read_binary(a, (unsigned char *)in, RSAN * sizeof(u32))) != 0)
|
||||
{
|
||||
mbedtls_mpi_free(a);
|
||||
return err;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static void rsaMulModDump(unsigned char w)
|
||||
{
|
||||
int addr = 0;
|
||||
switch(w)
|
||||
{
|
||||
case 'A':
|
||||
addr = 0;
|
||||
break;
|
||||
case 'B':
|
||||
addr = 0x100;
|
||||
break;
|
||||
case 'D':
|
||||
addr = 0x300;
|
||||
break;
|
||||
}
|
||||
printf("%c", w);
|
||||
dumpUint32(" Val:",((volatile u32*) (RSA_BASE_ADDRESS + addr )), RSAN);
|
||||
}
|
||||
|
||||
static void rsaMulModWrite(unsigned char w, mbedtls_mpi *a)
|
||||
{
|
||||
u32 in[64];
|
||||
memset(in, 0, 64 * sizeof(u32));
|
||||
mbedtls_mpi_write_binary_nr(a, (unsigned char *)in, a->n * ciL);
|
||||
//printf("rsaMulModWrite %c\n", w);
|
||||
//dumpUint32("a", a->p, a->n);
|
||||
//dumpUint32("in", in, a->n);
|
||||
switch(w)
|
||||
{
|
||||
case 'A':
|
||||
rsaMonMulWriteA(in);
|
||||
break;
|
||||
case 'B':
|
||||
rsaMonMulWriteB(in);
|
||||
break;
|
||||
case 'M':
|
||||
rsaMonMulWriteM(in);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void rsaMonMulAA(void)
|
||||
{
|
||||
u32 cpu_sr = tls_os_set_critical();
|
||||
g_crypto_ctx.rsa_complete = 0;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
RSACON = 0x2c;
|
||||
while (TRUE)
|
||||
{
|
||||
cpu_sr = tls_os_set_critical();
|
||||
if(g_crypto_ctx.rsa_complete)
|
||||
break;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
}
|
||||
g_crypto_ctx.rsa_complete = 0;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
}
|
||||
static void rsaMonMulDD(void)
|
||||
{
|
||||
u32 cpu_sr = tls_os_set_critical();
|
||||
g_crypto_ctx.rsa_complete = 0;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
RSACON = 0x20;
|
||||
while (TRUE)
|
||||
{
|
||||
cpu_sr = tls_os_set_critical();
|
||||
if(g_crypto_ctx.rsa_complete)
|
||||
break;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
}
|
||||
g_crypto_ctx.rsa_complete = 0;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
}
|
||||
static void rsaMonMulAB(void)
|
||||
{
|
||||
u32 cpu_sr = tls_os_set_critical();
|
||||
g_crypto_ctx.rsa_complete = 0;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
RSACON = 0x24;
|
||||
while (TRUE)
|
||||
{
|
||||
cpu_sr = tls_os_set_critical();
|
||||
if(g_crypto_ctx.rsa_complete)
|
||||
break;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
}
|
||||
g_crypto_ctx.rsa_complete = 0;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
}
|
||||
static void rsaMonMulBD(void)
|
||||
{
|
||||
u32 cpu_sr = tls_os_set_critical();
|
||||
g_crypto_ctx.rsa_complete = 0;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
RSACON = 0x28;
|
||||
while (TRUE)
|
||||
{
|
||||
cpu_sr = tls_os_set_critical();
|
||||
if(g_crypto_ctx.rsa_complete)
|
||||
break;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
}
|
||||
g_crypto_ctx.rsa_complete = 0;
|
||||
tls_os_release_critical(cpu_sr);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
compute mc, s.t. mc * in = 0xffffffff
|
||||
******************************************************************************/
|
||||
static void rsaCalMc(u32 *mc, const u32 in)
|
||||
{
|
||||
u32 y = 1;
|
||||
u32 i = 31;
|
||||
u32 left = 1;
|
||||
u32 right = 0;
|
||||
for(i = 31; i != 0; i--)
|
||||
{
|
||||
left <<= 1; /* 2^(i-1) */
|
||||
right = (in * y) & left; /* (n*y) mod 2^i */
|
||||
if( right )
|
||||
{
|
||||
y += left;
|
||||
}
|
||||
}
|
||||
*mc = ~y + 1;
|
||||
}
|
||||
|
||||
int tls_crypto_mbedtls_exptmod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N )
|
||||
{
|
||||
int i = 0;
|
||||
u32 k = 0, mc = 0, dp0;
|
||||
volatile u8 monmulFlag = 0;
|
||||
mbedtls_mpi R, X1, Y;
|
||||
mbedtls_mpi T;
|
||||
int ret = 0;
|
||||
size_t max_len;
|
||||
|
||||
tls_open_peripheral_clock(TLS_PERIPHERAL_TYPE_RSA);
|
||||
|
||||
#ifndef CONFIG_KERNEL_NONE
|
||||
tls_fls_sem_lock();
|
||||
#endif
|
||||
|
||||
max_len = (mbedtls_mpi_bitlen(N) + biL - 1) / biL;
|
||||
|
||||
mbedtls_mpi_init(&X1);
|
||||
mbedtls_mpi_init(&Y);
|
||||
mbedtls_mpi_init(&R);
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( N, max_len ) );
|
||||
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R, 1 ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &R, N->n * biL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R, &R, N ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &R, N->n ) );
|
||||
//dumpUint32("R", R.p, R.n);
|
||||
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X1, A, &R ) );//X = A * R
|
||||
//dumpUint32("X = A * R", X1.p, X1.n);
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &X1, &X1, N ) ); //X = A * R mod N
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &X1, N->n ) );
|
||||
//dumpUint32("X = A * R mod N", X1.p, X1.n);
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, &R ) );
|
||||
|
||||
dp0 = (u32)N->p[0];
|
||||
rsaCalMc(&mc, dp0);
|
||||
rsaMonMulSetLen(N->n);
|
||||
rsaMonMulWriteMc(mc);
|
||||
rsaMulModWrite('M', N);
|
||||
rsaMulModWrite('B', &X1);
|
||||
rsaMulModWrite('A', &Y);
|
||||
|
||||
k = mbedtls_mpi_bitlen(E);
|
||||
//printf("mbedtls e bit len %d\n", k);
|
||||
for(i = k - 1; i >= 0; i--)
|
||||
{
|
||||
//montMulMod(&Y, &Y, n, &Y);
|
||||
//if(pstm_get_bit(e, i))
|
||||
// montMulMod(&Y, &X, n, &Y);
|
||||
if(monmulFlag == 0)
|
||||
{
|
||||
rsaMonMulAA();
|
||||
monmulFlag = 1;
|
||||
//rsaMulModDump('D');
|
||||
}
|
||||
else
|
||||
{
|
||||
rsaMonMulDD();
|
||||
monmulFlag = 0;
|
||||
//rsaMulModDump('A');
|
||||
}
|
||||
|
||||
if(mbedtls_mpi_get_bit(E, i))
|
||||
{
|
||||
if(monmulFlag == 0)
|
||||
{
|
||||
rsaMonMulAB();
|
||||
monmulFlag = 1;
|
||||
//rsaMulModDump('D');
|
||||
}
|
||||
else
|
||||
{
|
||||
rsaMonMulBD();
|
||||
monmulFlag = 0;
|
||||
//rsaMulModDump('A');
|
||||
}
|
||||
}
|
||||
}
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R, 1 ) );
|
||||
rsaMulModWrite('B', &R);
|
||||
//montMulMod(&Y, &R, n, res);
|
||||
if(monmulFlag == 0)
|
||||
{
|
||||
rsaMonMulAB();
|
||||
rsaMulModRead('D', X);
|
||||
}
|
||||
else
|
||||
{
|
||||
rsaMonMulBD();
|
||||
rsaMulModRead('A', X);
|
||||
}
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( X, N->n ) );
|
||||
cleanup:
|
||||
mbedtls_mpi_free(&X1);
|
||||
mbedtls_mpi_free(&Y);
|
||||
mbedtls_mpi_free(&R);
|
||||
#ifndef CONFIG_KERNEL_NONE
|
||||
tls_fls_sem_unlock();
|
||||
#endif
|
||||
tls_close_peripheral_clock(TLS_PERIPHERAL_TYPE_RSA);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if 0
|
||||
#if 1
|
||||
typedef s32 psPool_t;
|
||||
#include "libtommath.h"
|
||||
#define pstm_set(a, b) mp_set((mp_int *)a, b)
|
||||
#define pstm_init(pool, a) wpa_mp_init((mp_int *)a)
|
||||
#define pstm_count_bits(a) mp_count_bits((mp_int *)a)
|
||||
#define pstm_init_for_read_unsigned_bin(pool, a, len) mp_init_for_read_unsigned_bin((mp_int *)a, len)
|
||||
#define pstm_read_unsigned_bin(a, b, c) mp_read_unsigned_bin((mp_int *)a, b, c)
|
||||
#define pstm_copy(a, b) mp_copy((mp_int *)a, (mp_int *)b)
|
||||
#define pstm_clear(a) mp_clear((mp_int *)a)
|
||||
#define pstm_clamp(a) mp_clamp((mp_int *)a)
|
||||
#define pstm_mulmod(pool, a, b, c, d) mp_mulmod((mp_int *)a, (mp_int *)b, (mp_int *)c, (mp_int *)d)
|
||||
#define pstm_exptmod(pool, G, X, P, Y) mp_exptmod((mp_int *)G, (mp_int *)X, (mp_int *)P, (mp_int *)Y)
|
||||
#define pstm_reverse mp_reverse
|
||||
#define pstm_cmp mp_cmp
|
||||
#define pstm_to_unsigned_bin_nr(pool, a, b) mp_to_unsigned_bin_nr((mp_int *)a, (unsigned char *)b)
|
||||
|
||||
#define pstm_2expt(a, b) mp_2expt((mp_int *)a, b)
|
||||
#define pstm_mod(pool, a, b, c) mp_mod((mp_int *)a, (mp_int *)b, (mp_int *)c)
|
||||
|
||||
#endif
|
||||
|
||||
uint8_t modulus[] = {
|
||||
0xdf, 0x83, 0xe4, 0x76, 0x2d, 0x00, 0x61, 0xf6, 0xd0, 0x8d, 0x4a, 0x04, 0x66, 0xb1, 0xd5, 0x55,
|
||||
0xef, 0x71, 0xb5, 0xa5, 0x4e, 0x69, 0x44, 0xd3, 0x4f, 0xb8, 0x3d, 0xec, 0xb1, 0x1d, 0x5f, 0x82,
|
||||
0x6a, 0x48, 0x21, 0x00, 0x7f, 0xd7, 0xd5, 0xf6, 0x82, 0x35, 0xc2, 0xa6, 0x67, 0xa3, 0x53, 0x2d,
|
||||
0x3a, 0x83, 0x9a, 0xba, 0x60, 0xc2, 0x11, 0x22, 0xc2, 0x35, 0x83, 0xe9, 0x10, 0xa1, 0xb4, 0xa6,
|
||||
0x74, 0x57, 0x99, 0xd3, 0xa8, 0x6a, 0x21, 0x83, 0x76, 0xc1, 0x67, 0xde, 0xd8, 0xec, 0xdf, 0xf7,
|
||||
0xc0, 0x1b, 0xf6, 0xfa, 0x14, 0xa4, 0x0a, 0xec, 0xd1, 0xee, 0xc0, 0x76, 0x4c, 0xcd, 0x4a, 0x0a,
|
||||
0x5c, 0x96, 0xf2, 0xc9, 0xa4, 0x67, 0x03, 0x97, 0x2e, 0x17, 0xcd, 0xa9, 0x27, 0x9d, 0xa6, 0x35,
|
||||
0x5f, 0x7d, 0xb1, 0x6b, 0x68, 0x0e, 0x99, 0xc7, 0xdd, 0x5d, 0x6f, 0x15, 0xce, 0x8e, 0x85, 0x33
|
||||
};
|
||||
static const uint8_t publicExponent[] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01
|
||||
};
|
||||
static const uint8_t privateExponent[] = {
|
||||
0xc6, 0x15, 0x3d, 0x02, 0xfe, 0x1e, 0xb8, 0xb2, 0xe3, 0x60, 0x53, 0x98, 0x52, 0xea, 0x87, 0x06,
|
||||
0x01, 0x8d, 0xe4, 0x4c, 0xfb, 0x90, 0x8f, 0x4e, 0x35, 0xf8, 0x31, 0xe8, 0xf1, 0x8d, 0xf6, 0x76,
|
||||
0xbd, 0x79, 0xee, 0xc5, 0x62, 0x87, 0x05, 0x37, 0xd1, 0x6d, 0x93, 0x73, 0xa5, 0xa5, 0x38, 0xb1,
|
||||
0x7c, 0x89, 0xe5, 0x36, 0x07, 0x49, 0xf5, 0xa5, 0xb8, 0x37, 0x75, 0x0f, 0xb7, 0x8d, 0x97, 0x69,
|
||||
0xc4, 0xd4, 0x8a, 0xb7, 0xfe, 0x74, 0x48, 0x45, 0x58, 0x47, 0x29, 0xa3, 0x0b, 0xa7, 0xdc, 0x55,
|
||||
0x98, 0x18, 0x8c, 0xd4, 0x52, 0xf5, 0xc9, 0xe8, 0x40, 0xce, 0x97, 0x46, 0x14, 0x1f, 0x62, 0x94,
|
||||
0xc3, 0x21, 0x1e, 0x5d, 0x49, 0x59, 0x31, 0xeb, 0xc4, 0x95, 0xf9, 0x33, 0x70, 0xa7, 0x90, 0xc3,
|
||||
0x9e, 0x98, 0x58, 0xa4, 0x00, 0xa4, 0x0f, 0xf3, 0x51, 0x80, 0xc6, 0x14, 0xfb, 0xd5, 0x5b, 0x01
|
||||
};
|
||||
|
||||
uint8_t Digest_signature_pkcs1_padding_out[] = {
|
||||
0x07, 0x2d, 0x25, 0xde, 0xa5, 0xfd, 0x7c, 0xb0, 0x92, 0xb4, 0xee, 0x57, 0xe8, 0xd3, 0x79, 0x74,
|
||||
0x59, 0x25, 0x34, 0xef, 0xfd, 0x2b, 0xda, 0x8b, 0xa4, 0x40, 0x4e, 0xd8, 0x92, 0x6e, 0xee, 0x84,
|
||||
0x52, 0xb0, 0xe1, 0x0e, 0xa8, 0xa9, 0x68, 0x62, 0x1b, 0x51, 0xed, 0x50, 0x84, 0x98, 0x6a, 0x97,
|
||||
0x98, 0xe8, 0xcf, 0x3f, 0x85, 0xd3, 0x28, 0x26, 0xf3, 0x7a, 0x52, 0x4b, 0x04, 0x95, 0xe6, 0xfd,
|
||||
0xfa, 0x41, 0xf3, 0xac, 0x8a, 0x6d, 0x74, 0x91, 0x8c, 0x87, 0x52, 0x38, 0x08, 0x49, 0xf4, 0x60,
|
||||
0xcd, 0x4b, 0x1a, 0x9e, 0x52, 0x60, 0xf2, 0x73, 0x60, 0x31, 0x78, 0x37, 0xd9, 0x42, 0xc4, 0x61,
|
||||
0x43, 0xcf, 0x6d, 0x55, 0xee, 0x05, 0x19, 0xb7, 0xc3, 0x37, 0xa7, 0xa8, 0xa4, 0xbd, 0xf1, 0xac,
|
||||
0x8e, 0x39, 0x20, 0x59, 0xcd, 0xfc, 0x50, 0x16, 0x81, 0x2d, 0xeb, 0xba, 0x95, 0xe9, 0x38, 0xa5,
|
||||
};
|
||||
|
||||
static const uint8_t Digest[] = {
|
||||
0x00, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
|
||||
0xe4, 0x2c, 0x9f, 0x12, 0xf7, 0xd2, 0x67, 0x3a, 0x23, 0xea, 0x85, 0x61, 0xeb, 0xb2, 0xc2, 0x19,
|
||||
0xdc, 0xd9, 0xf1, 0xaa
|
||||
};
|
||||
static const uint8_t base[] = {
|
||||
0x79, 0x91, 0x2F, 0x5D, 0x2C, 0x58, 0xED, 0xBF, 0xF8, 0x35, 0x75, 0x9B, 0x06, 0xF5, 0x08, 0x66,
|
||||
0xDD, 0xA4, 0xA8, 0x8D, 0x39, 0xDB, 0xB0, 0x20, 0xDB, 0xAE, 0xFC, 0x17, 0x16, 0xC2, 0x07, 0x77,
|
||||
0x01, 0x45, 0xA7, 0xC3, 0xFE, 0xEA, 0x98, 0x62, 0x50, 0x18, 0xB3, 0x1F, 0x6D, 0xF6, 0x39, 0xFA,
|
||||
0x1F, 0x2F, 0xB4, 0xBD, 0x72, 0x1D, 0x09, 0x51, 0x3D, 0xA0, 0x2B, 0xEC, 0x89, 0xD9, 0x78, 0xBD,
|
||||
0xE4, 0x8A, 0x3D, 0x48, 0x36, 0xD2, 0x25, 0xF2, 0x24, 0xC2, 0x60, 0xC6, 0x88, 0x50, 0x47, 0xB8,
|
||||
0xD4, 0x3E, 0x82, 0x8C, 0x94, 0x4B, 0x53, 0x4B, 0x7C, 0xE9, 0x52, 0x3D, 0x96, 0xEF, 0x08, 0x3E,
|
||||
0xCA, 0xA7, 0x4A, 0xD8, 0x18, 0xFB, 0x97, 0xCE, 0x5F, 0x9A, 0x75, 0x79, 0x22, 0x62, 0x47, 0x79,
|
||||
0xFA, 0x8D, 0xD5, 0x42, 0x61, 0xB4, 0xFF, 0x5D, 0xF4, 0x89, 0x0C, 0x69, 0x3D, 0x3A, 0x3A, 0x2D
|
||||
};
|
||||
|
||||
int initMpiParams(u32 len, mbedtls_mpi *pa, mbedtls_mpi *pb, mbedtls_mpi *pm, int isRand){
|
||||
|
||||
u32 * a = NULL;
|
||||
u32 * b = NULL;
|
||||
u32 * m = NULL;
|
||||
int err = -1;
|
||||
|
||||
a = tls_mem_alloc(64 * sizeof(u32));
|
||||
if(a == NULL)
|
||||
goto out;
|
||||
b = tls_mem_alloc(64 * sizeof(u32));
|
||||
if(b== NULL)
|
||||
goto out;
|
||||
m = tls_mem_alloc(64 * sizeof(u32));
|
||||
if(m == NULL)
|
||||
goto out;
|
||||
|
||||
memcpy(a, base, sizeof(base));
|
||||
memcpy(b, privateExponent, sizeof(privateExponent));
|
||||
memcpy(m, modulus, sizeof(modulus));
|
||||
|
||||
dumpBuffer("modulus", (unsigned char *)m, len * 4);
|
||||
dumpBuffer("exponent", (unsigned char *)b, len * 4);
|
||||
dumpBuffer("base", (unsigned char *)a, len * 4);
|
||||
mbedtls_mpi_init(pa);
|
||||
if ((err = mbedtls_mpi_read_binary(pa, (unsigned char *)a, len * sizeof(u32))) != PS_SUCCESS) {
|
||||
mbedtls_mpi_free(pa);
|
||||
goto out;
|
||||
}
|
||||
mbedtls_mpi_init(pb);
|
||||
if ((err = mbedtls_mpi_read_binary(pb, (unsigned char *)b, len * sizeof(u32))) != PS_SUCCESS) {
|
||||
mbedtls_mpi_free(pa);
|
||||
mbedtls_mpi_free(pb);
|
||||
goto out;
|
||||
}
|
||||
mbedtls_mpi_init(pm);
|
||||
if ((err = mbedtls_mpi_read_binary(pm, (unsigned char *)m, len * sizeof(u32))) != PS_SUCCESS) {
|
||||
mbedtls_mpi_free(pa);
|
||||
mbedtls_mpi_free(pb);
|
||||
mbedtls_mpi_free(pm);
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
if(a)
|
||||
tls_mem_free(a);
|
||||
if(b)
|
||||
tls_mem_free(b);
|
||||
if(m)
|
||||
tls_mem_free(m);
|
||||
return err;
|
||||
}
|
||||
|
||||
int initPstmParams(u32 len, hstm_int *pa, hstm_int *pb, hstm_int *pm, int isRand){
|
||||
|
||||
u32 * a = NULL;
|
||||
u32 * b = NULL;
|
||||
u32 * m = NULL;
|
||||
int err = -1;
|
||||
|
||||
a = tls_mem_alloc(64 * sizeof(u32));
|
||||
if(a == NULL)
|
||||
goto out;
|
||||
b = tls_mem_alloc(64 * sizeof(u32));
|
||||
if(b== NULL)
|
||||
goto out;
|
||||
m = tls_mem_alloc(64 * sizeof(u32));
|
||||
if(m == NULL)
|
||||
goto out;
|
||||
|
||||
memcpy(a, base, sizeof(base));
|
||||
memcpy(b, privateExponent, sizeof(privateExponent));
|
||||
memcpy(m, modulus, sizeof(modulus));
|
||||
|
||||
// pstm_reverse((unsigned char *)a, len * sizeof(u32));
|
||||
// pstm_reverse((unsigned char *)b, len * sizeof(u32));
|
||||
// pstm_reverse((unsigned char *)m, len * sizeof(u32));
|
||||
dumpBuffer("modulus", (unsigned char *)m, len * 4);
|
||||
dumpBuffer("exponent", (unsigned char *)b, len * 4);
|
||||
dumpBuffer("base", (unsigned char *)a, len * 4);
|
||||
if ((err = pstm_init_for_read_unsigned_bin(NULL, pa, len * sizeof(u32))) != PS_SUCCESS){
|
||||
goto out;
|
||||
}
|
||||
if ((err = pstm_read_unsigned_bin(pa, (unsigned char *)a, len * sizeof(u32))) != PS_SUCCESS) {
|
||||
pstm_clear(pa);
|
||||
goto out;
|
||||
}
|
||||
if ((err = pstm_init_for_read_unsigned_bin(NULL, pb, len * sizeof(u32))) != PS_SUCCESS){
|
||||
pstm_clear(pa);
|
||||
goto out;
|
||||
}
|
||||
if ((err = pstm_read_unsigned_bin(pb, (unsigned char *)b, len * sizeof(u32))) != PS_SUCCESS) {
|
||||
pstm_clear(pa);
|
||||
pstm_clear(pb);
|
||||
goto out;
|
||||
}
|
||||
if ((err = pstm_init_for_read_unsigned_bin(NULL, pm, len * sizeof(u32))) != PS_SUCCESS){
|
||||
pstm_clear(pa);
|
||||
pstm_clear(pb);
|
||||
goto out;
|
||||
}
|
||||
if ((err = pstm_read_unsigned_bin(pm, (unsigned char *)m, len * sizeof(u32))) != PS_SUCCESS) {
|
||||
pstm_clear(pa);
|
||||
pstm_clear(pb);
|
||||
pstm_clear(pm);
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
if(a)
|
||||
tls_mem_free(a);
|
||||
if(b)
|
||||
tls_mem_free(b);
|
||||
if(m)
|
||||
tls_mem_free(m);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
int exptModTest(u32 len){
|
||||
|
||||
hstm_int pa;
|
||||
hstm_int pb;
|
||||
hstm_int pm;
|
||||
hstm_int pres;
|
||||
hstm_int mres;
|
||||
|
||||
|
||||
mbedtls_mpi ppa;
|
||||
mbedtls_mpi ppb;
|
||||
mbedtls_mpi ppm;
|
||||
mbedtls_mpi ppres;
|
||||
mbedtls_mpi pmres;
|
||||
|
||||
int err = -1;
|
||||
|
||||
if((err = initMpiParams(len, &ppa, &ppb, &ppm, 1)))
|
||||
{
|
||||
return err;
|
||||
}
|
||||
if((err = initPstmParams(len, &pa, &pb, &pm, 1)))
|
||||
{
|
||||
return err;
|
||||
}
|
||||
dumpUint32("mbed ppa", ppa.p, ppa.n);
|
||||
dumpUint32("mbed ppb", ppb.p, ppb.n);
|
||||
dumpUint32("mbed ppm", ppm.p, ppm.n);
|
||||
pstm_init(NULL, &pres);
|
||||
pstm_init(NULL, &mres);
|
||||
mbedtls_mpi_init(&ppres);
|
||||
mbedtls_mpi_init(&pmres);
|
||||
|
||||
tls_crypto_mbedtls_exptmod(&ppres, &ppa, &ppb, &ppm);
|
||||
dumpUint32("mbed ppres", ppres.p, ppres.n);
|
||||
mbedtls_mpi_exp_mod(&pmres, &ppa, &ppb, &ppm, NULL);
|
||||
dumpUint32("mbed pmres", pmres.p, pmres.n);
|
||||
|
||||
tls_crypto_exptmod(&pa, &pb, &pm, &pres);
|
||||
printf("pres:\n");
|
||||
dumpUint32("pres", pres.dp, pres.used);
|
||||
//montExptMod(&pa, &pb, &pm, &pres);
|
||||
//rsaMontExptMod(&pa, &pb, &pm, &mres);
|
||||
pstm_exptmod(NULL, &pa, &pb, &pm, &mres);
|
||||
if(pstm_cmp(&mres, &pres) != 0)
|
||||
{
|
||||
#if 1
|
||||
int i = 0;
|
||||
printf("mres:\n");
|
||||
for(;i<mres.used;i++)
|
||||
{
|
||||
printf("%x ", mres.dp[i]);
|
||||
}
|
||||
printf("\n");
|
||||
printf("pres:\n");
|
||||
for(i=0;i<pres.used;i++)
|
||||
{
|
||||
printf("%x ", pres.dp[i]);
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
err = -1;
|
||||
goto out;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if 1
|
||||
int i = 0;
|
||||
printf("mres:\n");
|
||||
for(;i<mres.used;i++)
|
||||
{
|
||||
printf("%x ", mres.dp[i]);
|
||||
}
|
||||
printf("\n");
|
||||
printf("pres:\n");
|
||||
for(i=0;i<pres.used;i++)
|
||||
{
|
||||
printf("%x ", pres.dp[i]);
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
err = 0;
|
||||
out:
|
||||
pstm_clear(&pa);
|
||||
pstm_clear(&pb);
|
||||
pstm_clear(&pm);
|
||||
pstm_clear(&pres);
|
||||
pstm_clear(&mres);
|
||||
mbedtls_mpi_free(&ppa);
|
||||
mbedtls_mpi_free(&ppb);
|
||||
mbedtls_mpi_free(&ppm);
|
||||
mbedtls_mpi_free(&ppres);
|
||||
mbedtls_mpi_free(&pmres);
|
||||
printf("exptModTest err %d\n", err);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -39,7 +39,9 @@
|
||||
static struct tls_fwup *fwup = NULL;
|
||||
static tls_os_queue_t *fwup_msg_queue = NULL;
|
||||
|
||||
#if TLS_OS_FREERTOS
|
||||
static u32 *fwup_task_stk = NULL;
|
||||
#endif
|
||||
static u8 oneshotback = 0;
|
||||
|
||||
extern int tls_fls_fast_write_init(void);
|
||||
@@ -675,13 +677,19 @@ int tls_fwup_init(void)
|
||||
tls_mem_free(fwup);
|
||||
return TLS_FWUP_STATUS_EMEM;
|
||||
}
|
||||
#if TLS_OS_FREERTOS
|
||||
fwup_task_stk = (u32 *)tls_mem_alloc(FWUP_TASK_STK_SIZE * sizeof(u32));
|
||||
if (fwup_task_stk)
|
||||
{
|
||||
#endif
|
||||
err = tls_os_task_create(NULL, "fwup",
|
||||
fwup_scheduler,
|
||||
(void *)fwup,
|
||||
#if TLS_OS_FREERTOS
|
||||
(void *)fwup_task_stk,
|
||||
#else
|
||||
NULL,
|
||||
#endif
|
||||
FWUP_TASK_STK_SIZE * sizeof(u32),
|
||||
TLS_FWUP_TASK_PRIO,
|
||||
0);
|
||||
@@ -695,10 +703,13 @@ int tls_fwup_init(void)
|
||||
fwup->list_lock = NULL;
|
||||
tls_mem_free(fwup);
|
||||
fwup = NULL;
|
||||
#if TLS_OS_FREERTOS
|
||||
tls_mem_free(fwup_task_stk);
|
||||
fwup_task_stk = NULL;
|
||||
#endif
|
||||
return TLS_FWUP_STATUS_EMEM;
|
||||
}
|
||||
#if TLS_OS_FREERTOS
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -709,6 +720,7 @@ int tls_fwup_init(void)
|
||||
tls_mem_free(fwup);
|
||||
return TLS_FWUP_STATUS_EMEM;
|
||||
}
|
||||
#endif
|
||||
|
||||
return TLS_FWUP_STATUS_OK;
|
||||
}
|
||||
|
||||
@@ -497,81 +497,10 @@ int is_safe_addr_debug(void* p, u32 len, char* file, int line)
|
||||
}
|
||||
|
||||
#else /* WM_MEM_DEBUG */
|
||||
#if TLS_OS_FREERTOS
|
||||
u32 alloc_heap_mem_bytes = 0;
|
||||
u32 alloc_heap_mem_blk_cnt = 0;
|
||||
u32 alloc_heap_mem_max_size = 0;
|
||||
#define OS_MEM_FLAG (0x5AA5A55A)
|
||||
#define NON_OS_MEM_FLAG (0xA55A5A5A)
|
||||
#define MEM_HEAD_FLAG (0xBB55B55B)
|
||||
#endif
|
||||
|
||||
#define USING_ADD_HEADER 1
|
||||
extern u32 total_mem_size;
|
||||
void * mem_alloc_debug(u32 size)
|
||||
{
|
||||
// u32 cpu_sr = 0;
|
||||
u32 *buffer = NULL;
|
||||
u32 length = size;
|
||||
|
||||
|
||||
//printf("size:%d\n", size);
|
||||
if (!memory_manager_initialized) {
|
||||
tls_os_status_t os_status;
|
||||
memory_manager_initialized = true;
|
||||
//
|
||||
// NOTE: If two thread allocate the very first allocation simultaneously
|
||||
// it could cause double initialization of the memory manager. This is a
|
||||
// highly unlikely scenario and will occur in debug versions only.
|
||||
//
|
||||
os_status = tls_os_sem_create(&mem_sem, 1);
|
||||
if(os_status != TLS_OS_SUCCESS)
|
||||
printf("mem_alloc_debug: tls_os_sem_create mem_sem error\r\n");
|
||||
}
|
||||
|
||||
#if USING_ADD_HEADER && TLS_OS_FREERTOS
|
||||
length += 8;
|
||||
|
||||
if(tls_get_isr_count() > 0)
|
||||
{
|
||||
extern void *pvPortMalloc( size_t xWantedSize );
|
||||
buffer = pvPortMalloc(length);
|
||||
if(buffer)
|
||||
{
|
||||
*buffer = OS_MEM_FLAG;
|
||||
buffer++;
|
||||
*buffer = length;
|
||||
buffer++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tls_os_sem_acquire(mem_sem, 0);
|
||||
//cpu_sr = tls_os_set_critical();
|
||||
buffer = (u32*)malloc(length);
|
||||
if(buffer)
|
||||
{
|
||||
*buffer = NON_OS_MEM_FLAG;
|
||||
buffer++;
|
||||
*buffer = length;
|
||||
buffer++;
|
||||
total_mem_size -= length;
|
||||
}
|
||||
//if(tls_get_isr_count() == 0)
|
||||
// {
|
||||
// tls_os_release_critical(cpu_sr);
|
||||
tls_os_sem_release(mem_sem);
|
||||
// }
|
||||
}
|
||||
#elif TLS_OS_LITEOS
|
||||
buffer = LOS_MemAlloc(OS_SYS_MEM_ADDR, size);
|
||||
#else //UCOSII
|
||||
tls_os_sem_acquire(mem_sem, 0);
|
||||
cpu_sr = tls_os_set_critical();
|
||||
buffer = (u32*)malloc(length);
|
||||
tls_os_release_critical(cpu_sr);
|
||||
tls_os_sem_release(mem_sem);
|
||||
#endif
|
||||
if(buffer == NULL)
|
||||
printf("malloc error \n");
|
||||
|
||||
@@ -581,304 +510,43 @@ void * mem_alloc_debug(u32 size)
|
||||
|
||||
void mem_free_debug(void *p)
|
||||
{
|
||||
// u32 cpu_sr = 0;
|
||||
// u32 len = 0;
|
||||
#if USING_ADD_HEADER && TLS_OS_FREERTOS
|
||||
u32* intMemPtr = NULL;
|
||||
u8 isrstatus = 0;
|
||||
|
||||
isrstatus = tls_get_isr_count();
|
||||
if(isrstatus == 0)
|
||||
{
|
||||
tls_os_sem_acquire(mem_sem, 0);
|
||||
// cpu_sr = tls_os_set_critical();
|
||||
}
|
||||
|
||||
intMemPtr = (u32*)p;
|
||||
if(p)
|
||||
{
|
||||
intMemPtr -= 2;
|
||||
if (*intMemPtr == OS_MEM_FLAG)
|
||||
{
|
||||
extern void vPortFree( void *pv );
|
||||
vPortFree(intMemPtr);
|
||||
intMemPtr = NULL;
|
||||
}
|
||||
else if (*intMemPtr == NON_OS_MEM_FLAG)
|
||||
{
|
||||
total_mem_size += *(intMemPtr + 1);
|
||||
free(intMemPtr);
|
||||
intMemPtr = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("mem_free_debug ptr error!!!!!\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
if(isrstatus == 0)
|
||||
{
|
||||
// tls_os_release_critical(cpu_sr);
|
||||
tls_os_sem_release(mem_sem);
|
||||
}
|
||||
#elif TLS_OS_LITEOS
|
||||
int ret = LOS_MemFree(OS_SYS_MEM_ADDR, p);
|
||||
if(ret)
|
||||
{
|
||||
printf("mem free error\n");
|
||||
}
|
||||
#else //UCOSII
|
||||
tls_os_sem_acquire(mem_sem, 0);
|
||||
cpu_sr = tls_os_set_critical();
|
||||
free(p);
|
||||
tls_os_release_critical(cpu_sr);
|
||||
tls_os_sem_release(mem_sem);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void * mem_realloc_debug(void *mem_address, u32 size)
|
||||
{
|
||||
u32 * mem_re_addr = NULL;
|
||||
// u32 cpu_sr = 0;
|
||||
u32 length = size;
|
||||
|
||||
#if USING_ADD_HEADER && TLS_OS_FREERTOS
|
||||
length = size + 2*4;
|
||||
|
||||
if(tls_get_isr_count() > 0)
|
||||
{
|
||||
extern void *pvPortMalloc( size_t xWantedSize );
|
||||
mem_re_addr = pvPortMalloc(length);
|
||||
if (mem_re_addr)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if(mem_address != NULL)
|
||||
{
|
||||
if (*((u32 *)mem_address-1)> size)
|
||||
{
|
||||
memcpy((u8 *)(mem_re_addr + 2), (u8 *)mem_address, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy((u8 *)(mem_re_addr + 2), (u8 *)mem_address, *((u32 *)mem_address-1));
|
||||
}
|
||||
mem_free_debug(mem_address);
|
||||
mem_address = NULL;
|
||||
}
|
||||
if(mem_re_addr)
|
||||
{
|
||||
*mem_re_addr = OS_MEM_FLAG;
|
||||
mem_re_addr ++;
|
||||
*mem_re_addr = length;
|
||||
mem_re_addr ++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tls_os_sem_acquire(mem_sem, 0);
|
||||
// cpu_sr = tls_os_set_critical();
|
||||
mem_re_addr = (u32*)malloc(length);
|
||||
if(mem_re_addr && mem_address)
|
||||
{
|
||||
if (*((u32 *)mem_address-1)> size)
|
||||
{
|
||||
memcpy((u8 *)(mem_re_addr + 2), (u8 *)mem_address, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy((u8 *)(mem_re_addr + 2), (u8 *)mem_address, *((u32 *)mem_address-1));
|
||||
}
|
||||
*mem_re_addr = NON_OS_MEM_FLAG;
|
||||
mem_re_addr ++;
|
||||
*mem_re_addr = length;
|
||||
mem_re_addr ++;
|
||||
total_mem_size -= length;
|
||||
}
|
||||
// tls_os_release_critical(cpu_sr);
|
||||
tls_os_sem_release(mem_sem);
|
||||
|
||||
mem_free_debug(mem_address);
|
||||
}
|
||||
#elif TLS_OS_LITEOS
|
||||
mem_re_addr = LOS_MemRealloc(OS_SYS_MEM_ADDR, mem_address, size);
|
||||
#else
|
||||
tls_os_sem_acquire(mem_sem, 0);
|
||||
cpu_sr = tls_os_set_critical();
|
||||
mem_re_addr = realloc(mem_address, length);
|
||||
tls_os_release_critical(cpu_sr);
|
||||
tls_os_sem_release(mem_sem);
|
||||
#endif
|
||||
//if(mem_re_addr == NULL)
|
||||
//{
|
||||
// printf("realloc error \r\n");
|
||||
//}
|
||||
return mem_re_addr;
|
||||
}
|
||||
|
||||
void *mem_calloc_debug(u32 n, u32 size)
|
||||
{
|
||||
// u32 cpu_sr = 0;
|
||||
u32 *buffer = NULL;
|
||||
u32 length = 0;
|
||||
|
||||
#if USING_ADD_HEADER && TLS_OS_FREERTOS
|
||||
length = n*size;
|
||||
length += 2*4;
|
||||
|
||||
if(tls_get_isr_count() > 0)
|
||||
{
|
||||
extern void *pvPortMalloc( size_t xWantedSize );
|
||||
buffer = pvPortMalloc(length);
|
||||
if(buffer)
|
||||
{
|
||||
memset(buffer, 0, length);
|
||||
*buffer = OS_MEM_FLAG;
|
||||
buffer ++;
|
||||
*buffer = length;
|
||||
buffer ++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tls_os_sem_acquire(mem_sem, 0);
|
||||
// cpu_sr = tls_os_set_critical();
|
||||
buffer = (u32*)malloc(length);
|
||||
if(buffer)
|
||||
{
|
||||
memset(buffer, 0, length);
|
||||
*buffer = NON_OS_MEM_FLAG;
|
||||
buffer ++;
|
||||
*buffer = length;
|
||||
buffer ++;
|
||||
total_mem_size -= length;
|
||||
}
|
||||
|
||||
// tls_os_release_critical(cpu_sr);
|
||||
tls_os_sem_release(mem_sem);
|
||||
}
|
||||
#elif TLS_OS_LITEOS
|
||||
buffer = LOS_MemAlloc(OS_SYS_MEM_ADDR, n*size);
|
||||
if(buffer)
|
||||
{
|
||||
memset(buffer, 0, n*size);
|
||||
}
|
||||
#else //UCOSII
|
||||
tls_os_sem_acquire(mem_sem, 0);
|
||||
cpu_sr = tls_os_set_critical();
|
||||
buffer = (u32*)calloc(n,size);
|
||||
tls_os_release_critical(cpu_sr);
|
||||
tls_os_sem_release(mem_sem);
|
||||
#endif
|
||||
//if(buffer == NULL)
|
||||
//{
|
||||
// printf("calloc error \r\n");
|
||||
//}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
#endif /* WM_MEM_DEBUG */
|
||||
|
||||
extern u32 __heap_end;
|
||||
extern u32 __heap_start;
|
||||
|
||||
u32 tls_mem_get_avail_heapsize(void)
|
||||
{
|
||||
#if TLS_OS_FREERTOS
|
||||
#if USING_ADD_HEADER
|
||||
u32 availablemem = 0;
|
||||
// u32 cpu_sr;
|
||||
|
||||
tls_os_sem_acquire(mem_sem, 0);
|
||||
//cpu_sr = tls_os_set_critical();
|
||||
availablemem = total_mem_size;
|
||||
// tls_os_release_critical(cpu_sr);
|
||||
tls_os_sem_release(mem_sem);
|
||||
|
||||
return availablemem&0xFFFFF000;
|
||||
#else
|
||||
u8 *p = NULL;
|
||||
u32 startpos = 0;
|
||||
u32 stoppos = 0;
|
||||
u32 laststartpos = 0;
|
||||
static u32 last_avail_heapsize = 0;
|
||||
u32 cpu_sr = 0;
|
||||
|
||||
if (!memory_manager_initialized) {
|
||||
tls_os_status_t os_status;
|
||||
memory_manager_initialized = true;
|
||||
//
|
||||
// NOTE: If two thread allocate the very first allocation simultaneously
|
||||
// it could cause double initialization of the memory manager. This is a
|
||||
// highly unlikely scenario and will occur in debug versions only.
|
||||
//
|
||||
os_status = tls_os_sem_create(&mem_sem, 1);
|
||||
if(os_status != TLS_OS_SUCCESS)
|
||||
printf("mem_alloc_debug: tls_os_sem_create mem_sem error\n");
|
||||
}
|
||||
|
||||
tls_os_sem_acquire(mem_sem, 0);
|
||||
cpu_sr = tls_os_set_critical();
|
||||
if (last_avail_heapsize)
|
||||
{
|
||||
startpos = last_avail_heapsize;
|
||||
stoppos = last_avail_heapsize*2;
|
||||
if (startpos > ((u32)&__heap_end - (u32)&__heap_start))
|
||||
{
|
||||
startpos = (u32)&__heap_end - (u32)&__heap_start;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
startpos = (u32)&__heap_end - (u32)&__heap_start;
|
||||
stoppos = (u32)&__heap_end - (u32)&__heap_start;
|
||||
}
|
||||
|
||||
for (;startpos <= stoppos;)
|
||||
{
|
||||
p = malloc(startpos);
|
||||
if (p)
|
||||
{
|
||||
free(p);
|
||||
if (startpos < 1024 || (stoppos - startpos) < 1024
|
||||
|| (startpos == ((u32)&__heap_end - (u32)&__heap_start)))
|
||||
{
|
||||
last_avail_heapsize = startpos;
|
||||
goto END;
|
||||
}
|
||||
laststartpos = startpos;
|
||||
startpos = (stoppos + startpos)>>1;
|
||||
}
|
||||
else
|
||||
{
|
||||
stoppos = startpos;
|
||||
if (laststartpos)
|
||||
{
|
||||
startpos = (laststartpos + stoppos)/2;
|
||||
}
|
||||
else
|
||||
{
|
||||
startpos = startpos>>1;
|
||||
}
|
||||
if (startpos < 1024 || (stoppos - startpos) < 1024)
|
||||
{
|
||||
last_avail_heapsize = startpos;
|
||||
goto END;
|
||||
}
|
||||
}
|
||||
}
|
||||
END:
|
||||
tls_os_release_critical(cpu_sr);
|
||||
tls_os_sem_release(mem_sem);
|
||||
return startpos;
|
||||
#endif
|
||||
#elif TLS_OS_LITEOS
|
||||
LOS_MEM_POOL_STATUS status = {0};
|
||||
if (LOS_MemInfoGet(OS_SYS_MEM_ADDR, &status) == LOS_NOK) {
|
||||
return 0;
|
||||
}
|
||||
return status.totalFreeSize;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -22,8 +22,6 @@
|
||||
#include "wm_irq.h"
|
||||
#include "wm_mem.h"
|
||||
|
||||
#define ATTRIBUTE_ISR __attribute__((isr))
|
||||
|
||||
|
||||
//TODO
|
||||
#define HR_SD_ADC_CONFIG_REG 0
|
||||
@@ -504,8 +502,6 @@ u32 adc_get_interVolt(void)
|
||||
}
|
||||
voltValue = voltValue/10;
|
||||
code = voltValue;
|
||||
voltValue = voltValue;
|
||||
adc_offset = adc_offset;
|
||||
tls_adc_stop(0);
|
||||
voltValue = ((voltValue - adc_offset)*685/20+1200000)*2;
|
||||
value = voltValue - voltValue*10/100;
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#include "wm_pmu.h"
|
||||
|
||||
|
||||
#define ATTRIBUTE_ISR __attribute__((isr))
|
||||
static u16 dma_used_bit = 0;
|
||||
struct tls_dma_channels {
|
||||
unsigned char channels[8]; /* list of channels */
|
||||
|
||||
@@ -33,6 +33,14 @@ extern ATTRIBUTE_ISR void DMA_Channel2_IRQHandler(void);
|
||||
extern ATTRIBUTE_ISR void DMA_Channel3_IRQHandler(void);
|
||||
extern ATTRIBUTE_ISR void DMA_Channel4_7_IRQHandler(void);
|
||||
extern ATTRIBUTE_ISR void ADC_IRQHandler(void);
|
||||
//extern ATTRIBUTE_ISR void tls_touchsensor_irq_handler(void);
|
||||
extern ATTRIBUTE_ISR void GPSEC_IRQ_Handler(void);
|
||||
extern ATTRIBUTE_ISR void RSA_IRQ_Handler(void);
|
||||
extern ATTRIBUTE_ISR void TIM0_5_IRQ_Handler(void);
|
||||
extern ATTRIBUTE_ISR void SPI_HS_IRQ_Handler(void);
|
||||
extern ATTRIBUTE_ISR void MAC_IRQ_Handler(void);
|
||||
extern ATTRIBUTE_ISR void SEC_IRQ_Handler(void);
|
||||
extern ATTRIBUTE_ISR void PMU_IRQ_Handler(void);
|
||||
|
||||
static u32 irqen_status = 0;
|
||||
|
||||
@@ -71,6 +79,14 @@ void tls_irq_init(void)
|
||||
csi_vic_set_vector(DMA_Channel2_IRQn, (uint32_t)DMA_Channel2_IRQHandler);
|
||||
csi_vic_set_vector(DMA_Channel3_IRQn, (uint32_t)DMA_Channel3_IRQHandler);
|
||||
csi_vic_set_vector(DMA_Channel4_7_IRQn, (uint32_t)DMA_Channel4_7_IRQHandler);
|
||||
//csi_vic_set_vector(TOUCH_IRQn, (uint32_t)tls_touchsensor_irq_handler);
|
||||
csi_vic_set_vector(RSA_IRQn, (uint32_t)RSA_IRQ_Handler);
|
||||
csi_vic_set_vector(CRYPTION_IRQn, (uint32_t)GPSEC_IRQ_Handler);
|
||||
csi_vic_set_vector(TIMER_IRQn, (uint32_t)TIM0_5_IRQ_Handler);
|
||||
csi_vic_set_vector(SPI_HS_IRQn, (uint32_t)SPI_HS_IRQ_Handler);
|
||||
csi_vic_set_vector(MAC_IRQn, (uint32_t)MAC_IRQ_Handler);
|
||||
csi_vic_set_vector(SEC_IRQn, (uint32_t)SEC_IRQ_Handler);
|
||||
csi_vic_set_vector(PMU_IRQn, (uint32_t)PMU_IRQ_Handler);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include "core_804.h"
|
||||
#include "wm_pmu.h"
|
||||
|
||||
#define ATTRIBUTE_ISR __attribute__((isr))
|
||||
|
||||
static struct tls_spi_port *spi_port = NULL;
|
||||
|
||||
|
||||
@@ -13,8 +13,6 @@
|
||||
#include "wm_cpu.h"
|
||||
#include "wm_gpio.h"
|
||||
|
||||
#define ATTRIBUTE_ISR __attribute__((isr))
|
||||
|
||||
typedef void (*touchsensor_cb)(u32 status);
|
||||
touchsensor_cb tc_callback = NULL;
|
||||
/**
|
||||
|
||||
@@ -16,7 +16,6 @@ module_name = get_path_info(rebase_path("."), "name")
|
||||
kernel_module(module_name) {
|
||||
sources = [
|
||||
"wm_main.c",
|
||||
#"tls_sys.c",
|
||||
]
|
||||
include_dirs = [
|
||||
".",
|
||||
|
||||
@@ -13,8 +13,6 @@
|
||||
*****************************************************************************/
|
||||
#include <string.h>
|
||||
#include "wm_irq.h"
|
||||
#include "tls_sys.h"
|
||||
|
||||
#include "wm_regs.h"
|
||||
#include "wm_type_def.h"
|
||||
#include "wm_timer.h"
|
||||
@@ -26,13 +24,9 @@
|
||||
#include "wm_internal_flash.h"
|
||||
#include "wm_efuse.h"
|
||||
#include "wm_debug.h"
|
||||
#include "wm_netif.h"
|
||||
#include "wm_at_ri_init.h"
|
||||
#include "wm_config.h"
|
||||
#include "wm_osal.h"
|
||||
//#include "wm_http_client.h"
|
||||
#include "wm_cpu.h"
|
||||
#include "wm_webserver.h"
|
||||
#include "wm_io.h"
|
||||
#include "wm_mem.h"
|
||||
#include "wm_wl_task.h"
|
||||
@@ -45,54 +39,14 @@
|
||||
#include "wm_ram_config.h"
|
||||
#include "wm_uart.h"
|
||||
#include "los_task.h"
|
||||
#if TLS_CONFIG_ONLY_FACTORY_ATCMD
|
||||
#include "factory_atcmd.h"
|
||||
#endif
|
||||
|
||||
#if !defined(__MICROLIB) && TLS_OS_FREERTOS
|
||||
|
||||
/* c librayr mutex */
|
||||
tls_os_sem_t *libc_sem;
|
||||
/*----------------------------------------------------------------------------
|
||||
* Standard Library multithreading interface
|
||||
*---------------------------------------------------------------------------*/
|
||||
|
||||
/*--------------------------- _mutex_initialize -----------------------------*/
|
||||
|
||||
int _mutex_initialize (u32 *mutex)
|
||||
{
|
||||
/* Allocate and initialize a system mutex. */
|
||||
//tls_os_sem_create(&libc_sem, 1);
|
||||
//mutex = (u32 *)libc_sem;
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
||||
/*--------------------------- _mutex_acquire --------------------------------*/
|
||||
|
||||
void _mutex_acquire (u32 *mutex)
|
||||
{
|
||||
//u8 err;
|
||||
/* Acquire a system mutex, lock stdlib resources. */
|
||||
tls_os_sem_acquire(libc_sem, 0);
|
||||
}
|
||||
|
||||
|
||||
/*--------------------------- _mutex_release --------------------------------*/
|
||||
|
||||
void _mutex_release (u32 *mutex)
|
||||
{
|
||||
/* Release a system mutex, unlock stdlib resources. */
|
||||
tls_os_sem_release(libc_sem);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#define TASK_START_STK_SIZE 640 /* Size of each task's stacks (# of WORDs) */
|
||||
/*If you want to delete main task after it works, you can open this MACRO below*/
|
||||
#define MAIN_TASK_DELETE_AFTER_START_FTR 0
|
||||
|
||||
#if !TLS_OS_LITEOS
|
||||
u8 *TaskStartStk = NULL;
|
||||
#endif
|
||||
tls_os_task_t tststarthdl = NULL;
|
||||
|
||||
#define FW_MAJOR_VER 0x1
|
||||
@@ -137,48 +91,12 @@ void vApplicationIdleHook( void )
|
||||
|
||||
void wm_gpio_config()
|
||||
{
|
||||
#if (TLS_CONFIG_HOSTIF&&TLS_CONFIG_UART)
|
||||
int at_port = tls_uart_get_at_cmd_port();
|
||||
#endif
|
||||
/* must call first */
|
||||
wm_gpio_af_disable();
|
||||
|
||||
wm_uart0_tx_config(WM_IO_PB_19);
|
||||
wm_uart0_rx_config(WM_IO_PB_20);
|
||||
|
||||
/*Please Attention, only one IO's multiplex can be used at one times' configuration. */
|
||||
|
||||
/*AT command's port multiplex*/
|
||||
#if (TLS_CONFIG_HOSTIF&&TLS_CONFIG_UART)
|
||||
switch(at_port)
|
||||
{
|
||||
case TLS_UART_1:
|
||||
wm_uart1_rx_config(WM_IO_PB_07);
|
||||
wm_uart1_tx_config(WM_IO_PB_06);
|
||||
break;
|
||||
case TLS_UART_2:
|
||||
wm_uart2_rx_config(WM_IO_PA_03);
|
||||
wm_uart2_tx_scio_config(WM_IO_PA_02);
|
||||
break;
|
||||
case TLS_UART_3:
|
||||
wm_uart3_rx_config(WM_IO_PB_01);
|
||||
wm_uart3_tx_config(WM_IO_PB_00);
|
||||
break;
|
||||
case TLS_UART_4:
|
||||
wm_uart4_rx_config(WM_IO_PA_09);
|
||||
wm_uart4_tx_config(WM_IO_PA_08);
|
||||
break;
|
||||
case TLS_UART_5:
|
||||
wm_uart5_rx_config(WM_IO_PA_13);
|
||||
wm_uart5_tx_config(WM_IO_PA_12);
|
||||
break;
|
||||
default:
|
||||
wm_uart1_rx_config(WM_IO_PB_07);
|
||||
wm_uart1_tx_config(WM_IO_PB_06);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if (TLS_CONFIG_LS_SPI)
|
||||
wm_spi_cs_config(WM_IO_PB_04);
|
||||
wm_spi_ck_config(WM_IO_PB_02);
|
||||
@@ -186,25 +104,11 @@ void wm_gpio_config()
|
||||
wm_spi_do_config(WM_IO_PB_05);
|
||||
#endif
|
||||
}
|
||||
#if MAIN_TASK_DELETE_AFTER_START_FTR
|
||||
void task_start_free()
|
||||
{
|
||||
if (TaskStartStk)
|
||||
{
|
||||
tls_mem_free(TaskStartStk);
|
||||
TaskStartStk = NULL;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
int main(void)
|
||||
{
|
||||
u32 value = 0;
|
||||
/*32K switch to use RC circuit & calibration*/
|
||||
tls_pmu_clk_select(0);
|
||||
#if (TLS_CONFIG_HOSTIF&&TLS_CONFIG_UART)
|
||||
/*Configure uart port for user's AT Command*/
|
||||
tls_uart_set_at_cmd_port(TLS_UART_1);
|
||||
#endif
|
||||
/*Switch to DBG*/
|
||||
value = tls_reg_read32(HR_PMU_BK_REG);
|
||||
value &= ~(BIT(19));
|
||||
@@ -222,10 +126,6 @@ int main(void)
|
||||
|
||||
tls_sys_clk_set(CPU_CLK_80M);
|
||||
tls_os_init(NULL);
|
||||
#if !defined(__MICROLIB) && TLS_OS_FREERTOS
|
||||
/* before use malloc() function, must create mutex used by c_lib */
|
||||
tls_os_sem_create(&libc_sem, 1);
|
||||
#endif
|
||||
|
||||
/*configure wake up source begin*/
|
||||
csi_vic_set_wakeup_irq(SDIO_IRQn);
|
||||
@@ -258,6 +158,16 @@ int main(void)
|
||||
csi_vic_set_wakeup_irq(TIMER_IRQn);
|
||||
csi_vic_set_wakeup_irq(WDG_IRQn);
|
||||
/*configure wake up source end*/
|
||||
#if TLS_OS_LITEOS
|
||||
tls_os_task_create(&tststarthdl, "firstThr",
|
||||
task_start,
|
||||
(void *)0,
|
||||
(void *)NULL,
|
||||
TASK_START_STK_SIZE * sizeof(u32), /* 任务栈的大小 */
|
||||
1,
|
||||
0);
|
||||
tls_os_start_scheduler();
|
||||
#else
|
||||
TaskStartStk = tls_mem_alloc(sizeof(u32)*TASK_START_STK_SIZE);
|
||||
if (TaskStartStk)
|
||||
{
|
||||
@@ -274,6 +184,7 @@ int main(void)
|
||||
{
|
||||
while(1);
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -300,25 +211,6 @@ void disp_version_info(void)
|
||||
TLS_DBGPRT_INFO("****************************************************************\n");
|
||||
}
|
||||
|
||||
#if TLS_OS_FREERTOS
|
||||
unsigned int total_mem_size;
|
||||
void tls_mem_get_init_available_size(void)
|
||||
{
|
||||
u8 *p = NULL;
|
||||
total_mem_size = (unsigned int)&__heap_end - (unsigned int)&__heap_start;
|
||||
while(total_mem_size > 512)
|
||||
{
|
||||
p = malloc(total_mem_size);
|
||||
if (p)
|
||||
{
|
||||
free(p);
|
||||
p = NULL;
|
||||
break;
|
||||
}
|
||||
total_mem_size = total_mem_size - 512;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void tls_pmu_chipsleep_callback(int sleeptime)
|
||||
{
|
||||
@@ -341,15 +233,11 @@ void tls_pmu_chipsleep_callback(int sleeptime)
|
||||
void task_start (void *data)
|
||||
{
|
||||
u8 enable = 0;
|
||||
u8 mac_addr[6] = {0x00, 0x25, 0x08, 0x09, 0x01, 0x0F};
|
||||
|
||||
#if TLS_CONFIG_CRYSTAL_24M
|
||||
tls_wl_hw_using_24m_crystal();
|
||||
#endif
|
||||
|
||||
#if TLS_OS_FREERTOS
|
||||
tls_mem_get_init_available_size();
|
||||
#endif
|
||||
/* must call first to configure gpio Alternate functions according the hardware design */
|
||||
wm_gpio_config();
|
||||
|
||||
@@ -372,63 +260,7 @@ void task_start (void *data)
|
||||
tls_param_load_factory_default();
|
||||
tls_param_init(); /*add param to init sysparam_lock sem*/
|
||||
|
||||
//extern void HalFlashFileInit(void);
|
||||
//HalFlashFileInit();
|
||||
|
||||
//void testHalFlashFile(void);
|
||||
//testHalFlashFile();
|
||||
#if TLS_OS_FREERTOS
|
||||
tls_get_tx_gain(&tx_gain_group[0]);
|
||||
TLS_DBGPRT_INFO("tx gain ");
|
||||
TLS_DBGPRT_DUMP((char *)(&tx_gain_group[0]), 27);
|
||||
if (tls_wifi_mem_cfg(WIFI_MEM_START_ADDR, 7, 7)) /*wifi tx&rx mem customized interface*/
|
||||
{
|
||||
TLS_DBGPRT_INFO("wl mem initial failured\n");
|
||||
}
|
||||
|
||||
tls_get_mac_addr(&mac_addr[0]);
|
||||
TLS_DBGPRT_INFO("mac addr ");
|
||||
TLS_DBGPRT_DUMP((char *)(&mac_addr[0]), 6);
|
||||
if(tls_wl_init(NULL, &mac_addr[0], NULL) == NULL)
|
||||
{
|
||||
TLS_DBGPRT_INFO("wl driver initial failured\n");
|
||||
}
|
||||
if (wpa_supplicant_init(mac_addr))
|
||||
{
|
||||
TLS_DBGPRT_INFO("supplicant initial failured\n");
|
||||
}
|
||||
/*wifi-temperature compensation,default:open*/
|
||||
tls_wifi_set_tempcomp_flag(0);
|
||||
tls_wifi_set_psm_chipsleep_flag(0);
|
||||
tls_wifi_psm_chipsleep_cb_register(tls_pmu_chipsleep_callback, NULL, NULL);
|
||||
tls_ethernet_init();
|
||||
|
||||
#if TLS_CONFIG_BT
|
||||
tls_bt_entry();
|
||||
#endif
|
||||
|
||||
tls_sys_init();
|
||||
#endif
|
||||
#if TLS_CONFIG_ONLY_FACTORY_ATCMD
|
||||
factory_atcmd_init();
|
||||
#else
|
||||
/*HOSTIF&UART*/
|
||||
#if TLS_CONFIG_HOSTIF
|
||||
tls_hostif_init();
|
||||
|
||||
#if (TLS_CONFIG_HS_SPI)
|
||||
tls_hspi_init();
|
||||
#endif
|
||||
|
||||
#if TLS_CONFIG_UART
|
||||
tls_uart_init();
|
||||
#endif
|
||||
|
||||
#if TLS_CONFIG_HTTP_CLIENT_TASK
|
||||
http_client_task_init();
|
||||
#endif
|
||||
|
||||
#endif
|
||||
tls_wifi_netif_event_init();
|
||||
|
||||
tls_param_get(TLS_PARAM_ID_PSM, &enable, TRUE);
|
||||
if (enable != TRUE)
|
||||
@@ -438,22 +270,12 @@ void task_start (void *data)
|
||||
}
|
||||
|
||||
UserMain();
|
||||
#if TLS_OS_FREERTOS
|
||||
tls_sys_auto_mode_run();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
extern void OHOS_SystemInit();
|
||||
OHOS_SystemInit();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
#if MAIN_TASK_DELETE_AFTER_START_FTR
|
||||
if (tststarthdl)
|
||||
{
|
||||
tls_os_task_del_by_task_handle(tststarthdl,task_start_free);
|
||||
}
|
||||
tls_os_time_delay(0x10000000);
|
||||
#else
|
||||
#if 1
|
||||
tls_os_time_delay(0x10000000);
|
||||
#else
|
||||
@@ -462,7 +284,6 @@ void task_start (void *data)
|
||||
tls_os_disp_task_stat_info();
|
||||
tls_os_time_delay(1000);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,10 @@ if (ohos_kernel_type == "liteos_m") {
|
||||
module_group(module_name) {
|
||||
modules = [
|
||||
"os\liteos",
|
||||
"bt",
|
||||
"wifi",
|
||||
"components\fs",
|
||||
"components\utils",
|
||||
]
|
||||
}
|
||||
}
|
||||
Executable
+157
@@ -0,0 +1,157 @@
|
||||
# Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import("//kernel/liteos_m/liteos.gni")
|
||||
module_switch = defined(LOSCFG_HALS_COMMUCATION_WIFI_LITE)
|
||||
module_name = get_path_info(rebase_path("."), "name")
|
||||
kernel_module(module_name) {
|
||||
sources = [
|
||||
"blehost/ext/tinycrypt/src/aes_decrypt.c",
|
||||
"blehost/ext/tinycrypt/src/aes_encrypt.c",
|
||||
"blehost/ext/tinycrypt/src/bleutils.c",
|
||||
"blehost/ext/tinycrypt/src/cbc_mode.c",
|
||||
"blehost/ext/tinycrypt/src/ccm_mode.c",
|
||||
"blehost/ext/tinycrypt/src/cmac_mode.c",
|
||||
"blehost/ext/tinycrypt/src/ctr_mode.c",
|
||||
"blehost/ext/tinycrypt/src/ctr_prng.c",
|
||||
"blehost/ext/tinycrypt/src/ecc.c",
|
||||
"blehost/ext/tinycrypt/src/ecc_dh.c",
|
||||
"blehost/ext/tinycrypt/src/ecc_dsa.c",
|
||||
"blehost/ext/tinycrypt/src/ecc_platform_specific.c",
|
||||
"blehost/ext/tinycrypt/src/hmac.c",
|
||||
"blehost/ext/tinycrypt/src/hmac_prng.c",
|
||||
"blehost/ext/tinycrypt/src/sha256.c",
|
||||
"blehost/nimble/host/mesh/src/access.c",
|
||||
"blehost/nimble/host/mesh/src/adv.c",
|
||||
"blehost/nimble/host/mesh/src/beacon.c",
|
||||
"blehost/nimble/host/mesh/src/cfg_cli.c",
|
||||
"blehost/nimble/host/mesh/src/cfg_srv.c",
|
||||
"blehost/nimble/host/mesh/src/crypto.c",
|
||||
"blehost/nimble/host/mesh/src/friend.c",
|
||||
"blehost/nimble/host/mesh/src/glue.c",
|
||||
"blehost/nimble/host/mesh/src/health_cli.c",
|
||||
"blehost/nimble/host/mesh/src/health_srv.c",
|
||||
"blehost/nimble/host/mesh/src/light_model.c",
|
||||
"blehost/nimble/host/mesh/src/lpn.c",
|
||||
"blehost/nimble/host/mesh/src/mesh.c",
|
||||
"blehost/nimble/host/mesh/src/model_cli.c",
|
||||
"blehost/nimble/host/mesh/src/model_srv.c",
|
||||
"blehost/nimble/host/mesh/src/net.c",
|
||||
"blehost/nimble/host/mesh/src/nodes.c",
|
||||
"blehost/nimble/host/mesh/src/prov.c",
|
||||
"blehost/nimble/host/mesh/src/proxy.c",
|
||||
"blehost/nimble/host/mesh/src/settings.c",
|
||||
"blehost/nimble/host/mesh/src/shell.c",
|
||||
"blehost/nimble/host/mesh/src/testing.c",
|
||||
"blehost/nimble/host/mesh/src/transport.c",
|
||||
"blehost/nimble/host/services/gap/src/ble_svc_gap.c",
|
||||
"blehost/nimble/host/services/gatt/src/ble_svc_gatt.c",
|
||||
"blehost/nimble/host/src/ble_att.c",
|
||||
"blehost/nimble/host/src/ble_att_clt.c",
|
||||
"blehost/nimble/host/src/ble_att_cmd.c",
|
||||
"blehost/nimble/host/src/ble_att_svr.c",
|
||||
"blehost/nimble/host/src/ble_eddystone.c",
|
||||
"blehost/nimble/host/src/ble_gap.c",
|
||||
"blehost/nimble/host/src/ble_gattc.c",
|
||||
"blehost/nimble/host/src/ble_gatts.c",
|
||||
"blehost/nimble/host/src/ble_gatts_lcl.c",
|
||||
"blehost/nimble/host/src/ble_hs.c",
|
||||
"blehost/nimble/host/src/ble_hs_adv.c",
|
||||
"blehost/nimble/host/src/ble_hs_atomic.c",
|
||||
"blehost/nimble/host/src/ble_hs_cfg.c",
|
||||
"blehost/nimble/host/src/ble_hs_conn.c",
|
||||
"blehost/nimble/host/src/ble_hs_flow.c",
|
||||
"blehost/nimble/host/src/ble_hs_hci.c",
|
||||
"blehost/nimble/host/src/ble_hs_hci_cmd.c",
|
||||
"blehost/nimble/host/src/ble_hs_hci_evt.c",
|
||||
"blehost/nimble/host/src/ble_hs_hci_util.c",
|
||||
"blehost/nimble/host/src/ble_hs_id.c",
|
||||
"blehost/nimble/host/src/ble_hs_log.c",
|
||||
"blehost/nimble/host/src/ble_hs_mbuf.c",
|
||||
"blehost/nimble/host/src/ble_hs_misc.c",
|
||||
"blehost/nimble/host/src/ble_hs_mqueue.c",
|
||||
"blehost/nimble/host/src/ble_hs_periodic_sync.c",
|
||||
"blehost/nimble/host/src/ble_hs_pvcy.c",
|
||||
"blehost/nimble/host/src/ble_hs_shutdown.c",
|
||||
"blehost/nimble/host/src/ble_hs_startup.c",
|
||||
"blehost/nimble/host/src/ble_hs_stop.c",
|
||||
"blehost/nimble/host/src/ble_ibeacon.c",
|
||||
"blehost/nimble/host/src/ble_l2cap.c",
|
||||
"blehost/nimble/host/src/ble_l2cap_coc.c",
|
||||
"blehost/nimble/host/src/ble_l2cap_sig.c",
|
||||
"blehost/nimble/host/src/ble_l2cap_sig_cmd.c",
|
||||
"blehost/nimble/host/src/ble_monitor.c",
|
||||
"blehost/nimble/host/src/ble_sm.c",
|
||||
"blehost/nimble/host/src/ble_sm_alg.c",
|
||||
"blehost/nimble/host/src/ble_sm_cmd.c",
|
||||
"blehost/nimble/host/src/ble_sm_lgcy.c",
|
||||
"blehost/nimble/host/src/ble_sm_sc.c",
|
||||
"blehost/nimble/host/src/ble_store.c",
|
||||
"blehost/nimble/host/src/ble_store_util.c",
|
||||
"blehost/nimble/host/src/ble_uuid.c",
|
||||
"blehost/nimble/host/store/config/src/ble_store_config.c",
|
||||
"blehost/nimble/host/store/config/src/ble_store_nvram.c",
|
||||
"blehost/nimble/host/store/config/src/wm_bt_storage.c",
|
||||
"blehost/nimble/host/store/ram/src/ble_store_ram.c",
|
||||
"blehost/nimble/host/util/src/addr.c",
|
||||
"blehost/nimble/transport/uart/src/ble_hci_uart.c",
|
||||
"blehost/porting/w800/src/endian.c",
|
||||
"blehost/porting/w800/src/mem.c",
|
||||
"blehost/porting/w800/src/nimble_port.c",
|
||||
"blehost/porting/w800/src/npl_os_freertos.c",
|
||||
"blehost/porting/w800/src/os_mbuf.c",
|
||||
"blehost/porting/w800/src/os_mempool.c",
|
||||
"blehost/porting/w800/src/os_msys_init.c",
|
||||
"blehost/porting/w800/src/tls_nimble.c",
|
||||
]
|
||||
include_dirs = [
|
||||
"blehost/ext/tinycrypt/include",
|
||||
"blehost/ext/tinycrypt/include/tinycrypt",
|
||||
"blehost/nimble/host/include",
|
||||
"blehost/nimble/host/include/host",
|
||||
"blehost/nimble/host/mesh/include",
|
||||
"blehost/nimble/host/mesh/include/mesh",
|
||||
"blehost/nimble/host/mesh/src",
|
||||
"blehost/nimble/host/porting/w800/include",
|
||||
"blehost/nimble/host/services/gap/include",
|
||||
"blehost/nimble/host/services/gap/include/services/gap",
|
||||
"blehost/nimble/host/services/gatt/include",
|
||||
"blehost/nimble/host/services/gatt/include/services/gatt",
|
||||
"blehost/nimble/host/src",
|
||||
"blehost/nimble/host/store/config/include",
|
||||
"blehost/nimble/host/store/config/include/store/config",
|
||||
"blehost/nimble/host/store/config/src",
|
||||
"blehost/nimble/host/store/ram/include",
|
||||
"blehost/nimble/host/store/ram/include/store/ram",
|
||||
"blehost/nimble/host/util/include",
|
||||
"blehost/nimble/host/util/include/host/util",
|
||||
"blehost/nimble/include",
|
||||
"blehost/nimble/include/nimble",
|
||||
"blehost/nimble/transport/uart/include/transport/uart",
|
||||
"blehost/nimble/transport/uart/include",
|
||||
"blehost/porting/w800/include",
|
||||
"blehost/porting/w800/include/hal",
|
||||
"blehost/porting/w800/include/log",
|
||||
"blehost/porting/w800/include/log_common",
|
||||
"blehost/porting/w800/include/logcfg",
|
||||
"blehost/porting/w800/include/mem",
|
||||
"blehost/porting/w800/include/modlog",
|
||||
"blehost/porting/w800/include/nimble",
|
||||
"blehost/porting/w800/include/os",
|
||||
"blehost/porting/w800/include/stats",
|
||||
"blehost/porting/w800/include/syscfg",
|
||||
"blehost/porting/w800/include/sysflash",
|
||||
"blehost/porting/w800/include/sysinit",
|
||||
]
|
||||
}
|
||||
|
||||
Executable
+30
@@ -0,0 +1,30 @@
|
||||
TOP_DIR = ../../..
|
||||
sinclude $(TOP_DIR)/tools/w800/conf.mk
|
||||
|
||||
ifndef PDIR
|
||||
GEN_LIBS = libblehost$(LIB_EXT)
|
||||
COMPONENTS_libblehost = ext/libext$(LIB_EXT) \
|
||||
nimble/libnimble$(LIB_EXT) \
|
||||
porting/libporting$(LIB_EXT)
|
||||
endif
|
||||
|
||||
#DEFINES +=
|
||||
|
||||
sinclude $(TOP_DIR)/tools/w800/rules.mk
|
||||
|
||||
INCLUDES := $(INCLUDES) -I $(PDIR)include \
|
||||
-I $(PDIR)nimble/host/include \
|
||||
-I $(PDIR)nimble/host/mesh/include \
|
||||
-I $(PDIR)nimble/host/services/gap/include \
|
||||
-I $(PDIR)nimble/host/services/gatt/include \
|
||||
-I $(PDIR)nimble/host/config/include \
|
||||
-I $(PDIR)nimble/host/ram/include \
|
||||
-I $(PDIR)nimble/host/util/include \
|
||||
-I $(PDIR)nimble/include \
|
||||
-I $(PDIR)nimble/transport/uart/include \
|
||||
-I $(PDIR)porting/w800/include \
|
||||
-I $(PDIR)ext/tinycrypt/include
|
||||
|
||||
INCLUDES := $(INCLUDES) -I ./ -I $(PDIR)include
|
||||
PDIR := ../$(PDIR)
|
||||
sinclude $(PDIR)Makefile
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
TOP_DIR = ../../../..
|
||||
sinclude $(TOP_DIR)/tools/w800/conf.mk
|
||||
|
||||
ifndef PDIR
|
||||
GEN_LIBS = libext$(LIB_EXT)
|
||||
COMPONENTS_libext = tinycrypt/libtinycrypt$(LIB_EXT)
|
||||
endif
|
||||
|
||||
#DEFINES +=
|
||||
|
||||
sinclude $(TOP_DIR)/tools/w800/rules.mk
|
||||
|
||||
INCLUDES := $(INCLUDES) -I ./ -I $(PDIR)include
|
||||
PDIR := ../$(PDIR)
|
||||
sinclude $(PDIR)Makefile
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
TOP_DIR = ../../../../..
|
||||
sinclude $(TOP_DIR)/tools/w800/conf.mk
|
||||
|
||||
ifndef PDIR
|
||||
GEN_LIBS = libtinycrypt$(LIB_EXT)
|
||||
COMPONENTS_libtinycrypt = src/libwmtinycrypt$(LIB_EXT)
|
||||
endif
|
||||
|
||||
#DEFINES +=
|
||||
|
||||
sinclude $(TOP_DIR)/tools/w800/rules.mk
|
||||
|
||||
INCLUDES := $(INCLUDES) -I ./ -I $(PDIR)include
|
||||
PDIR := ../$(PDIR)
|
||||
sinclude $(PDIR)Makefile
|
||||
@@ -0,0 +1,130 @@
|
||||
/* aes.h - TinyCrypt interface to an AES-128 implementation */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief -- Interface to an AES-128 implementation.
|
||||
*
|
||||
* Overview: AES-128 is a NIST approved block cipher specified in
|
||||
* FIPS 197. Block ciphers are deterministic algorithms that
|
||||
* perform a transformation specified by a symmetric key in fixed-
|
||||
* length data sets, also called blocks.
|
||||
*
|
||||
* Security: AES-128 provides approximately 128 bits of security.
|
||||
*
|
||||
* Usage: 1) call tc_aes128_set_encrypt/decrypt_key to set the key.
|
||||
*
|
||||
* 2) call tc_aes_encrypt/decrypt to process the data.
|
||||
*/
|
||||
|
||||
#ifndef __TC_AES_H__
|
||||
#define __TC_AES_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define Nb (4) /* number of columns (32-bit words) comprising the state */
|
||||
#define Nk (4) /* number of 32-bit words comprising the key */
|
||||
#define Nr (10) /* number of rounds */
|
||||
#define TC_AES_BLOCK_SIZE (Nb*Nk)
|
||||
#define TC_AES_KEY_SIZE (Nb*Nk)
|
||||
|
||||
typedef struct tc_aes_key_sched_struct {
|
||||
unsigned int words[Nb * (Nr + 1)];
|
||||
} *TCAesKeySched_t;
|
||||
|
||||
/**
|
||||
* @brief Set AES-128 encryption key
|
||||
* Uses key k to initialize s
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if: s == NULL or k == NULL
|
||||
* @note This implementation skips the additional steps required for keys
|
||||
* larger than 128 bits, and must not be used for AES-192 or
|
||||
* AES-256 key schedule -- see FIPS 197 for details
|
||||
* @param s IN/OUT -- initialized struct tc_aes_key_sched_struct
|
||||
* @param k IN -- points to the AES key
|
||||
*/
|
||||
int tc_aes128_set_encrypt_key(TCAesKeySched_t s, const uint8_t *k);
|
||||
|
||||
/**
|
||||
* @brief AES-128 Encryption procedure
|
||||
* Encrypts contents of in buffer into out buffer under key;
|
||||
* schedule s
|
||||
* @note Assumes s was initialized by aes_set_encrypt_key;
|
||||
* out and in point to 16 byte buffers
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if: out == NULL or in == NULL or s == NULL
|
||||
* @param out IN/OUT -- buffer to receive ciphertext block
|
||||
* @param in IN -- a plaintext block to encrypt
|
||||
* @param s IN -- initialized AES key schedule
|
||||
*/
|
||||
int tc_aes_encrypt(uint8_t *out, const uint8_t *in,
|
||||
const TCAesKeySched_t s);
|
||||
|
||||
/**
|
||||
* @brief Set the AES-128 decryption key
|
||||
* Uses key k to initialize s
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if: s == NULL or k == NULL
|
||||
* @note This is the implementation of the straightforward inverse cipher
|
||||
* using the cipher documented in FIPS-197 figure 12, not the
|
||||
* equivalent inverse cipher presented in Figure 15
|
||||
* @warning This routine skips the additional steps required for keys larger
|
||||
* than 128, and must not be used for AES-192 or AES-256 key
|
||||
* schedule -- see FIPS 197 for details
|
||||
* @param s IN/OUT -- initialized struct tc_aes_key_sched_struct
|
||||
* @param k IN -- points to the AES key
|
||||
*/
|
||||
int tc_aes128_set_decrypt_key(TCAesKeySched_t s, const uint8_t *k);
|
||||
|
||||
/**
|
||||
* @brief AES-128 Encryption procedure
|
||||
* Decrypts in buffer into out buffer under key schedule s
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if: out is NULL or in is NULL or s is NULL
|
||||
* @note Assumes s was initialized by aes_set_encrypt_key
|
||||
* out and in point to 16 byte buffers
|
||||
* @param out IN/OUT -- buffer to receive ciphertext block
|
||||
* @param in IN -- a plaintext block to encrypt
|
||||
* @param s IN -- initialized AES key schedule
|
||||
*/
|
||||
int tc_aes_decrypt(uint8_t *out, const uint8_t *in,
|
||||
const TCAesKeySched_t s);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_AES_H__ */
|
||||
@@ -0,0 +1,151 @@
|
||||
/* cbc_mode.h - TinyCrypt interface to a CBC mode implementation */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Interface to a CBC mode implementation.
|
||||
*
|
||||
* Overview: CBC (for "cipher block chaining") mode is a NIST approved mode of
|
||||
* operation defined in SP 800-38a. It can be used with any block
|
||||
* cipher to provide confidentiality of strings whose lengths are
|
||||
* multiples of the block_size of the underlying block cipher.
|
||||
* TinyCrypt hard codes AES as the block cipher.
|
||||
*
|
||||
* Security: CBC mode provides data confidentiality given that the maximum
|
||||
* number q of blocks encrypted under a single key satisfies
|
||||
* q < 2^63, which is not a practical constraint (it is considered a
|
||||
* good practice to replace the encryption when q == 2^56). CBC mode
|
||||
* provides NO data integrity.
|
||||
*
|
||||
* CBC mode assumes that the IV value input into the
|
||||
* tc_cbc_mode_encrypt is randomly generated. The TinyCrypt library
|
||||
* provides HMAC-PRNG module, which generates suitable IVs. Other
|
||||
* methods for generating IVs are acceptable, provided that the
|
||||
* values of the IVs generated appear random to any adversary,
|
||||
* including someone with complete knowledge of the system design.
|
||||
*
|
||||
* The randomness property on which CBC mode's security depends is
|
||||
* the unpredictability of the IV. Since it is unpredictable, this
|
||||
* means in practice that CBC mode requires that the IV is stored
|
||||
* somehow with the ciphertext in order to recover the plaintext.
|
||||
*
|
||||
* TinyCrypt CBC encryption prepends the IV to the ciphertext,
|
||||
* because this affords a more efficient (few buffers) decryption.
|
||||
* Hence tc_cbc_mode_encrypt assumes the ciphertext buffer is always
|
||||
* 16 bytes larger than the plaintext buffer.
|
||||
*
|
||||
* Requires: AES-128
|
||||
*
|
||||
* Usage: 1) call tc_cbc_mode_encrypt to encrypt data.
|
||||
*
|
||||
* 2) call tc_cbc_mode_decrypt to decrypt data.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __TC_CBC_MODE_H__
|
||||
#define __TC_CBC_MODE_H__
|
||||
|
||||
#include <tinycrypt/aes.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief CBC encryption procedure
|
||||
* CBC encrypts inlen bytes of the in buffer into the out buffer
|
||||
* using the encryption key schedule provided, prepends iv to out
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* out == NULL or
|
||||
* in == NULL or
|
||||
* ctr == NULL or
|
||||
* sched == NULL or
|
||||
* inlen == 0 or
|
||||
* (inlen % TC_AES_BLOCK_SIZE) != 0 or
|
||||
* (outlen % TC_AES_BLOCK_SIZE) != 0 or
|
||||
* outlen != inlen + TC_AES_BLOCK_SIZE
|
||||
* @note Assumes: - sched has been configured by aes_set_encrypt_key
|
||||
* - iv contains a 16 byte random string
|
||||
* - out buffer is large enough to hold the ciphertext + iv
|
||||
* - out buffer is a contiguous buffer
|
||||
* - in holds the plaintext and is a contiguous buffer
|
||||
* - inlen gives the number of bytes in the in buffer
|
||||
* @param out IN/OUT -- buffer to receive the ciphertext
|
||||
* @param outlen IN -- length of ciphertext buffer in bytes
|
||||
* @param in IN -- plaintext to encrypt
|
||||
* @param inlen IN -- length of plaintext buffer in bytes
|
||||
* @param iv IN -- the IV for the this encrypt/decrypt
|
||||
* @param sched IN -- AES key schedule for this encrypt
|
||||
*/
|
||||
int tc_cbc_mode_encrypt(uint8_t *out, unsigned int outlen, const uint8_t *in,
|
||||
unsigned int inlen, const uint8_t *iv,
|
||||
const TCAesKeySched_t sched);
|
||||
|
||||
/**
|
||||
* @brief CBC decryption procedure
|
||||
* CBC decrypts inlen bytes of the in buffer into the out buffer
|
||||
* using the provided encryption key schedule
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* out == NULL or
|
||||
* in == NULL or
|
||||
* sched == NULL or
|
||||
* inlen == 0 or
|
||||
* outlen == 0 or
|
||||
* (inlen % TC_AES_BLOCK_SIZE) != 0 or
|
||||
* (outlen % TC_AES_BLOCK_SIZE) != 0 or
|
||||
* outlen != inlen + TC_AES_BLOCK_SIZE
|
||||
* @note Assumes:- in == iv + ciphertext, i.e. the iv and the ciphertext are
|
||||
* contiguous. This allows for a very efficient decryption
|
||||
* algorithm that would not otherwise be possible
|
||||
* - sched was configured by aes_set_decrypt_key
|
||||
* - out buffer is large enough to hold the decrypted plaintext
|
||||
* and is a contiguous buffer
|
||||
* - inlen gives the number of bytes in the in buffer
|
||||
* @param out IN/OUT -- buffer to receive decrypted data
|
||||
* @param outlen IN -- length of plaintext buffer in bytes
|
||||
* @param in IN -- ciphertext to decrypt, including IV
|
||||
* @param inlen IN -- length of ciphertext buffer in bytes
|
||||
* @param iv IN -- the IV for the this encrypt/decrypt
|
||||
* @param sched IN -- AES key schedule for this decrypt
|
||||
*
|
||||
*/
|
||||
int tc_cbc_mode_decrypt(uint8_t *out, unsigned int outlen, const uint8_t *in,
|
||||
unsigned int inlen, const uint8_t *iv,
|
||||
const TCAesKeySched_t sched);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_CBC_MODE_H__ */
|
||||
@@ -0,0 +1,211 @@
|
||||
/* ccm_mode.h - TinyCrypt interface to a CCM mode implementation */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Interface to a CCM mode implementation.
|
||||
*
|
||||
* Overview: CCM (for "Counter with CBC-MAC") mode is a NIST approved mode of
|
||||
* operation defined in SP 800-38C.
|
||||
*
|
||||
* TinyCrypt CCM implementation accepts:
|
||||
*
|
||||
* 1) Both non-empty payload and associated data (it encrypts and
|
||||
* authenticates the payload and also authenticates the associated
|
||||
* data);
|
||||
* 2) Non-empty payload and empty associated data (it encrypts and
|
||||
* authenticates the payload);
|
||||
* 3) Non-empty associated data and empty payload (it degenerates to
|
||||
* an authentication mode on the associated data).
|
||||
*
|
||||
* TinyCrypt CCM implementation accepts associated data of any length
|
||||
* between 0 and (2^16 - 2^8) bytes.
|
||||
*
|
||||
* Security: The mac length parameter is an important parameter to estimate the
|
||||
* security against collision attacks (that aim at finding different
|
||||
* messages that produce the same authentication tag). TinyCrypt CCM
|
||||
* implementation accepts any even integer between 4 and 16, as
|
||||
* suggested in SP 800-38C.
|
||||
*
|
||||
* RFC-3610, which also specifies CCM, presents a few relevant
|
||||
* security suggestions, such as: it is recommended for most
|
||||
* applications to use a mac length greater than 8. Besides, the
|
||||
* usage of the same nonce for two different messages which are
|
||||
* encrypted with the same key destroys the security of CCM mode.
|
||||
*
|
||||
* Requires: AES-128
|
||||
*
|
||||
* Usage: 1) call tc_ccm_config to configure.
|
||||
*
|
||||
* 2) call tc_ccm_mode_encrypt to encrypt data and generate tag.
|
||||
*
|
||||
* 3) call tc_ccm_mode_decrypt to decrypt data and verify tag.
|
||||
*/
|
||||
|
||||
#ifndef __TC_CCM_MODE_H__
|
||||
#define __TC_CCM_MODE_H__
|
||||
|
||||
#include <tinycrypt/aes.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* max additional authenticated size in bytes: 2^16 - 2^8 = 65280 */
|
||||
#define TC_CCM_AAD_MAX_BYTES 0xff00
|
||||
|
||||
/* max message size in bytes: 2^(8L) = 2^16 = 65536 */
|
||||
#define TC_CCM_PAYLOAD_MAX_BYTES 0x10000
|
||||
|
||||
/* struct tc_ccm_mode_struct represents the state of a CCM computation */
|
||||
typedef struct tc_ccm_mode_struct {
|
||||
TCAesKeySched_t sched; /* AES key schedule */
|
||||
uint8_t *nonce; /* nonce required by CCM */
|
||||
unsigned int mlen; /* mac length in bytes (parameter t in SP-800 38C) */
|
||||
} *TCCcmMode_t;
|
||||
|
||||
/**
|
||||
* @brief CCM configuration procedure
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* c == NULL or
|
||||
* sched == NULL or
|
||||
* nonce == NULL or
|
||||
* mlen != {4, 6, 8, 10, 12, 16}
|
||||
* @param c -- CCM state
|
||||
* @param sched IN -- AES key schedule
|
||||
* @param nonce IN - nonce
|
||||
* @param nlen -- nonce length in bytes
|
||||
* @param mlen -- mac length in bytes (parameter t in SP-800 38C)
|
||||
*/
|
||||
int tc_ccm_config(TCCcmMode_t c, TCAesKeySched_t sched, uint8_t *nonce,
|
||||
unsigned int nlen, unsigned int mlen);
|
||||
|
||||
/**
|
||||
* @brief CCM tag generation and encryption procedure
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* out == NULL or
|
||||
* c == NULL or
|
||||
* ((plen > 0) and (payload == NULL)) or
|
||||
* ((alen > 0) and (associated_data == NULL)) or
|
||||
* (alen >= TC_CCM_AAD_MAX_BYTES) or
|
||||
* (plen >= TC_CCM_PAYLOAD_MAX_BYTES) or
|
||||
* (olen < plen + maclength)
|
||||
*
|
||||
* @param out OUT -- encrypted data
|
||||
* @param olen IN -- output length in bytes
|
||||
* @param associated_data IN -- associated data
|
||||
* @param alen IN -- associated data length in bytes
|
||||
* @param payload IN -- payload
|
||||
* @param plen IN -- payload length in bytes
|
||||
* @param c IN -- CCM state
|
||||
*
|
||||
* @note: out buffer should be at least (plen + c->mlen) bytes long.
|
||||
*
|
||||
* @note: The sequence b for encryption is formatted as follows:
|
||||
* b = [FLAGS | nonce | counter ], where:
|
||||
* FLAGS is 1 byte long
|
||||
* nonce is 13 bytes long
|
||||
* counter is 2 bytes long
|
||||
* The byte FLAGS is composed by the following 8 bits:
|
||||
* 0-2 bits: used to represent the value of q-1
|
||||
* 3-7 btis: always 0's
|
||||
*
|
||||
* @note: The sequence b for authentication is formatted as follows:
|
||||
* b = [FLAGS | nonce | length(mac length)], where:
|
||||
* FLAGS is 1 byte long
|
||||
* nonce is 13 bytes long
|
||||
* length(mac length) is 2 bytes long
|
||||
* The byte FLAGS is composed by the following 8 bits:
|
||||
* 0-2 bits: used to represent the value of q-1
|
||||
* 3-5 bits: mac length (encoded as: (mlen-2)/2)
|
||||
* 6: Adata (0 if alen == 0, and 1 otherwise)
|
||||
* 7: always 0
|
||||
*/
|
||||
int tc_ccm_generation_encryption(uint8_t *out, unsigned int olen,
|
||||
const uint8_t *associated_data,
|
||||
unsigned int alen, const uint8_t *payload,
|
||||
unsigned int plen, TCCcmMode_t c);
|
||||
|
||||
/**
|
||||
* @brief CCM decryption and tag verification procedure
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* out == NULL or
|
||||
* c == NULL or
|
||||
* ((plen > 0) and (payload == NULL)) or
|
||||
* ((alen > 0) and (associated_data == NULL)) or
|
||||
* (alen >= TC_CCM_AAD_MAX_BYTES) or
|
||||
* (plen >= TC_CCM_PAYLOAD_MAX_BYTES) or
|
||||
* (olen < plen - c->mlen)
|
||||
*
|
||||
* @param out OUT -- decrypted data
|
||||
* @param associated_data IN -- associated data
|
||||
* @param alen IN -- associated data length in bytes
|
||||
* @param payload IN -- payload
|
||||
* @param plen IN -- payload length in bytes
|
||||
* @param c IN -- CCM state
|
||||
*
|
||||
* @note: out buffer should be at least (plen - c->mlen) bytes long.
|
||||
*
|
||||
* @note: The sequence b for encryption is formatted as follows:
|
||||
* b = [FLAGS | nonce | counter ], where:
|
||||
* FLAGS is 1 byte long
|
||||
* nonce is 13 bytes long
|
||||
* counter is 2 bytes long
|
||||
* The byte FLAGS is composed by the following 8 bits:
|
||||
* 0-2 bits: used to represent the value of q-1
|
||||
* 3-7 btis: always 0's
|
||||
*
|
||||
* @note: The sequence b for authentication is formatted as follows:
|
||||
* b = [FLAGS | nonce | length(mac length)], where:
|
||||
* FLAGS is 1 byte long
|
||||
* nonce is 13 bytes long
|
||||
* length(mac length) is 2 bytes long
|
||||
* The byte FLAGS is composed by the following 8 bits:
|
||||
* 0-2 bits: used to represent the value of q-1
|
||||
* 3-5 bits: mac length (encoded as: (mlen-2)/2)
|
||||
* 6: Adata (0 if alen == 0, and 1 otherwise)
|
||||
* 7: always 0
|
||||
*/
|
||||
int tc_ccm_decryption_verification(uint8_t *out, unsigned int olen,
|
||||
const uint8_t *associated_data,
|
||||
unsigned int alen, const uint8_t *payload, unsigned int plen,
|
||||
TCCcmMode_t c);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_CCM_MODE_H__ */
|
||||
@@ -0,0 +1,194 @@
|
||||
/* cmac_mode.h -- interface to a CMAC implementation */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Interface to a CMAC implementation.
|
||||
*
|
||||
* Overview: CMAC is defined NIST in SP 800-38B, and is the standard algorithm
|
||||
* for computing a MAC using a block cipher. It can compute the MAC
|
||||
* for a byte string of any length. It is distinguished from CBC-MAC
|
||||
* in the processing of the final message block; CMAC uses a
|
||||
* different technique to compute the final message block is full
|
||||
* size or only partial, while CBC-MAC uses the same technique for
|
||||
* both. This difference permits CMAC to be applied to variable
|
||||
* length messages, while all messages authenticated by CBC-MAC must
|
||||
* be the same length.
|
||||
*
|
||||
* Security: AES128-CMAC mode of operation offers 64 bits of security against
|
||||
* collision attacks. Note however that an external attacker cannot
|
||||
* generate the tags him/herself without knowing the MAC key. In this
|
||||
* sense, to attack the collision property of AES128-CMAC, an
|
||||
* external attacker would need the cooperation of the legal user to
|
||||
* produce an exponentially high number of tags (e.g. 2^64) to
|
||||
* finally be able to look for collisions and benefit from them. As
|
||||
* an extra precaution, the current implementation allows to at most
|
||||
* 2^48 calls to the tc_cmac_update function before re-calling
|
||||
* tc_cmac_setup (allowing a new key to be set), as suggested in
|
||||
* Appendix B of SP 800-38B.
|
||||
*
|
||||
* Requires: AES-128
|
||||
*
|
||||
* Usage: This implementation provides a "scatter-gather" interface, so that
|
||||
* the CMAC value can be computed incrementally over a message
|
||||
* scattered in different segments throughout memory. Experience shows
|
||||
* this style of interface tends to minimize the burden of programming
|
||||
* correctly. Like all symmetric key operations, it is session
|
||||
* oriented.
|
||||
*
|
||||
* To begin a CMAC session, use tc_cmac_setup to initialize a struct
|
||||
* tc_cmac_struct with encryption key and buffer. Our implementation
|
||||
* always assume that the AES key to be the same size as the block
|
||||
* cipher block size. Once setup, this data structure can be used for
|
||||
* many CMAC computations.
|
||||
*
|
||||
* Once the state has been setup with a key, computing the CMAC of
|
||||
* some data requires three steps:
|
||||
*
|
||||
* (1) first use tc_cmac_init to initialize a new CMAC computation.
|
||||
* (2) next mix all of the data into the CMAC computation state using
|
||||
* tc_cmac_update. If all of the data resides in a single data
|
||||
* segment then only one tc_cmac_update call is needed; if data
|
||||
* is scattered throughout memory in n data segments, then n calls
|
||||
* will be needed. CMAC IS ORDER SENSITIVE, to be able to detect
|
||||
* attacks that swap bytes, so the order in which data is mixed
|
||||
* into the state is critical!
|
||||
* (3) Once all of the data for a message has been mixed, use
|
||||
* tc_cmac_final to compute the CMAC tag value.
|
||||
*
|
||||
* Steps (1)-(3) can be repeated as many times as you want to CMAC
|
||||
* multiple messages. A practical limit is 2^48 1K messages before you
|
||||
* have to change the key.
|
||||
*
|
||||
* Once you are done computing CMAC with a key, it is a good idea to
|
||||
* destroy the state so an attacker cannot recover the key; use
|
||||
* tc_cmac_erase to accomplish this.
|
||||
*/
|
||||
|
||||
#ifndef __TC_CMAC_MODE_H__
|
||||
#define __TC_CMAC_MODE_H__
|
||||
|
||||
#include <tinycrypt/aes.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* padding for last message block */
|
||||
#define TC_CMAC_PADDING 0x80
|
||||
|
||||
/* struct tc_cmac_struct represents the state of a CMAC computation */
|
||||
typedef struct tc_cmac_struct {
|
||||
/* initialization vector */
|
||||
uint8_t iv[TC_AES_BLOCK_SIZE];
|
||||
/* used if message length is a multiple of block_size bytes */
|
||||
uint8_t K1[TC_AES_BLOCK_SIZE];
|
||||
/* used if message length isn't a multiple block_size bytes */
|
||||
uint8_t K2[TC_AES_BLOCK_SIZE];
|
||||
/* where to put bytes that didn't fill a block */
|
||||
uint8_t leftover[TC_AES_BLOCK_SIZE];
|
||||
/* identifies the encryption key */
|
||||
unsigned int keyid;
|
||||
/* next available leftover location */
|
||||
unsigned int leftover_offset;
|
||||
/* AES key schedule */
|
||||
TCAesKeySched_t sched;
|
||||
/* calls to tc_cmac_update left before re-key */
|
||||
uint64_t countdown;
|
||||
} *TCCmacState_t;
|
||||
|
||||
/**
|
||||
* @brief Configures the CMAC state to use the given AES key
|
||||
* @return returns TC_CRYPTO_SUCCESS (1) after having configured the CMAC state
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* s == NULL or
|
||||
* key == NULL
|
||||
*
|
||||
* @param s IN/OUT -- the state to set up
|
||||
* @param key IN -- the key to use
|
||||
* @param sched IN -- AES key schedule
|
||||
*/
|
||||
int tc_cmac_setup(TCCmacState_t s, const uint8_t *key,
|
||||
TCAesKeySched_t sched);
|
||||
|
||||
/**
|
||||
* @brief Erases the CMAC state
|
||||
* @return returns TC_CRYPTO_SUCCESS (1) after having configured the CMAC state
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* s == NULL
|
||||
*
|
||||
* @param s IN/OUT -- the state to erase
|
||||
*/
|
||||
int tc_cmac_erase(TCCmacState_t s);
|
||||
|
||||
/**
|
||||
* @brief Initializes a new CMAC computation
|
||||
* @return returns TC_CRYPTO_SUCCESS (1) after having initialized the CMAC state
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* s == NULL
|
||||
*
|
||||
* @param s IN/OUT -- the state to initialize
|
||||
*/
|
||||
int tc_cmac_init(TCCmacState_t s);
|
||||
|
||||
/**
|
||||
* @brief Incrementally computes CMAC over the next data segment
|
||||
* @return returns TC_CRYPTO_SUCCESS (1) after successfully updating the CMAC state
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* s == NULL or
|
||||
* if data == NULL when dlen > 0
|
||||
*
|
||||
* @param s IN/OUT -- the CMAC state
|
||||
* @param data IN -- the next data segment to MAC
|
||||
* @param dlen IN -- the length of data in bytes
|
||||
*/
|
||||
int tc_cmac_update(TCCmacState_t s, const uint8_t *data, size_t dlen);
|
||||
|
||||
/**
|
||||
* @brief Generates the tag from the CMAC state
|
||||
* @return returns TC_CRYPTO_SUCCESS (1) after successfully generating the tag
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* tag == NULL or
|
||||
* s == NULL
|
||||
*
|
||||
* @param tag OUT -- the CMAC tag
|
||||
* @param s IN -- CMAC state
|
||||
*/
|
||||
int tc_cmac_final(uint8_t *tag, TCCmacState_t s);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_CMAC_MODE_H__ */
|
||||
@@ -0,0 +1,61 @@
|
||||
/* constants.h - TinyCrypt interface to constants */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief -- Interface to constants.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __TC_CONSTANTS_H__
|
||||
#define __TC_CONSTANTS_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL ((void *)0)
|
||||
#endif
|
||||
|
||||
#define TC_CRYPTO_SUCCESS 1
|
||||
#define TC_CRYPTO_FAIL 0
|
||||
|
||||
#define TC_ZERO_BYTE 0x00
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_CONSTANTS_H__ */
|
||||
@@ -0,0 +1,108 @@
|
||||
/* ctr_mode.h - TinyCrypt interface to CTR mode */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Interface to CTR mode.
|
||||
*
|
||||
* Overview: CTR (pronounced "counter") mode is a NIST approved mode of
|
||||
* operation defined in SP 800-38a. It can be used with any
|
||||
* block cipher to provide confidentiality of strings of any
|
||||
* length. TinyCrypt hard codes AES128 as the block cipher.
|
||||
*
|
||||
* Security: CTR mode achieves confidentiality only if the counter value is
|
||||
* never reused with a same encryption key. If the counter is
|
||||
* repeated, than an adversary might be able to defeat the scheme.
|
||||
*
|
||||
* A usual method to ensure different counter values refers to
|
||||
* initialize the counter in a given value (0, for example) and
|
||||
* increases it every time a new block is enciphered. This naturally
|
||||
* leaves to a limitation on the number q of blocks that can be
|
||||
* enciphered using a same key: q < 2^(counter size).
|
||||
*
|
||||
* TinyCrypt uses a counter of 32 bits. This means that after 2^32
|
||||
* block encryptions, the counter will be reused (thus losing CBC
|
||||
* security). 2^32 block encryptions should be enough for most of
|
||||
* applications targeting constrained devices. Applications intended
|
||||
* to encrypt a larger number of blocks must replace the key after
|
||||
* 2^32 block encryptions.
|
||||
*
|
||||
* CTR mode provides NO data integrity.
|
||||
*
|
||||
* Requires: AES-128
|
||||
*
|
||||
* Usage: 1) call tc_ctr_mode to process the data to encrypt/decrypt.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __TC_CTR_MODE_H__
|
||||
#define __TC_CTR_MODE_H__
|
||||
|
||||
#include <tinycrypt/aes.h>
|
||||
#include <tinycrypt/constants.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief CTR mode encryption/decryption procedure.
|
||||
* CTR mode encrypts (or decrypts) inlen bytes from in buffer into out buffer
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* out == NULL or
|
||||
* in == NULL or
|
||||
* ctr == NULL or
|
||||
* sched == NULL or
|
||||
* inlen == 0 or
|
||||
* outlen == 0 or
|
||||
* inlen != outlen
|
||||
* @note Assumes:- The current value in ctr has NOT been used with sched
|
||||
* - out points to inlen bytes
|
||||
* - in points to inlen bytes
|
||||
* - ctr is an integer counter in littleEndian format
|
||||
* - sched was initialized by aes_set_encrypt_key
|
||||
* @param out OUT -- produced ciphertext (plaintext)
|
||||
* @param outlen IN -- length of ciphertext buffer in bytes
|
||||
* @param in IN -- data to encrypt (or decrypt)
|
||||
* @param inlen IN -- length of input data in bytes
|
||||
* @param ctr IN/OUT -- the current counter value
|
||||
* @param sched IN -- an initialized AES key schedule
|
||||
*/
|
||||
int tc_ctr_mode(uint8_t *out, unsigned int outlen, const uint8_t *in,
|
||||
unsigned int inlen, uint8_t *ctr, const TCAesKeySched_t sched);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_CTR_MODE_H__ */
|
||||
@@ -0,0 +1,166 @@
|
||||
/* ctr_prng.h - TinyCrypt interface to a CTR-PRNG implementation */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016, Chris Morrison
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Interface to a CTR-PRNG implementation.
|
||||
*
|
||||
* Overview: A pseudo-random number generator (PRNG) generates a sequence
|
||||
* of numbers that have a distribution close to the one expected
|
||||
* for a sequence of truly random numbers. The NIST Special
|
||||
* Publication 800-90A specifies several mechanisms to generate
|
||||
* sequences of pseudo random numbers, including the CTR-PRNG one
|
||||
* which is based on AES. TinyCrypt implements CTR-PRNG with
|
||||
* AES-128.
|
||||
*
|
||||
* Security: A cryptographically secure PRNG depends on the existence of an
|
||||
* entropy source to provide a truly random seed as well as the
|
||||
* security of the primitives used as the building blocks (AES-128
|
||||
* in this instance).
|
||||
*
|
||||
* Requires: - AES-128
|
||||
*
|
||||
* Usage: 1) call tc_ctr_prng_init to seed the prng context
|
||||
*
|
||||
* 2) call tc_ctr_prng_reseed to mix in additional entropy into
|
||||
* the prng context
|
||||
*
|
||||
* 3) call tc_ctr_prng_generate to output the pseudo-random data
|
||||
*
|
||||
* 4) call tc_ctr_prng_uninstantiate to zero out the prng context
|
||||
*/
|
||||
|
||||
#ifndef __TC_CTR_PRNG_H__
|
||||
#define __TC_CTR_PRNG_H__
|
||||
|
||||
#include <tinycrypt/aes.h>
|
||||
|
||||
#define TC_CTR_PRNG_RESEED_REQ -1
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
/* updated each time another BLOCKLEN_BYTES bytes are produced */
|
||||
uint8_t V[TC_AES_BLOCK_SIZE];
|
||||
|
||||
/* updated whenever the PRNG is reseeded */
|
||||
struct tc_aes_key_sched_struct key;
|
||||
|
||||
/* number of requests since initialization/reseeding */
|
||||
uint64_t reseedCount;
|
||||
} TCCtrPrng_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief CTR-PRNG initialization procedure
|
||||
* Initializes prng context with entropy and personalization string (if any)
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* ctx == NULL,
|
||||
* entropy == NULL,
|
||||
* entropyLen < (TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE)
|
||||
* @note Only the first (TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE) bytes of
|
||||
* both the entropy and personalization inputs are used -
|
||||
* supplying additional bytes has no effect.
|
||||
* @param ctx IN/OUT -- the PRNG context to initialize
|
||||
* @param entropy IN -- entropy used to seed the PRNG
|
||||
* @param entropyLen IN -- entropy length in bytes
|
||||
* @param personalization IN -- personalization string used to seed the PRNG
|
||||
* (may be null)
|
||||
* @param plen IN -- personalization length in bytes
|
||||
*
|
||||
*/
|
||||
int tc_ctr_prng_init(TCCtrPrng_t *const ctx,
|
||||
uint8_t const *const entropy,
|
||||
unsigned int entropyLen,
|
||||
uint8_t const *const personalization,
|
||||
unsigned int pLen);
|
||||
|
||||
/**
|
||||
* @brief CTR-PRNG reseed procedure
|
||||
* Mixes entropy and additional_input into the prng context
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* ctx == NULL,
|
||||
* entropy == NULL,
|
||||
* entropylen < (TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE)
|
||||
* @note It is better to reseed an existing prng context rather than
|
||||
* re-initialise, so that any existing entropy in the context is
|
||||
* presereved. This offers some protection against undetected failures
|
||||
* of the entropy source.
|
||||
* @note Assumes tc_ctr_prng_init has been called for ctx
|
||||
* @param ctx IN/OUT -- the PRNG state
|
||||
* @param entropy IN -- entropy to mix into the prng
|
||||
* @param entropylen IN -- length of entropy in bytes
|
||||
* @param additional_input IN -- additional input to the prng (may be null)
|
||||
* @param additionallen IN -- additional input length in bytes
|
||||
*/
|
||||
int tc_ctr_prng_reseed(TCCtrPrng_t *const ctx,
|
||||
uint8_t const *const entropy,
|
||||
unsigned int entropyLen,
|
||||
uint8_t const *const additional_input,
|
||||
unsigned int additionallen);
|
||||
|
||||
/**
|
||||
* @brief CTR-PRNG generate procedure
|
||||
* Generates outlen pseudo-random bytes into out buffer, updates prng
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CTR_PRNG_RESEED_REQ (-1) if a reseed is needed
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* ctx == NULL,
|
||||
* out == NULL,
|
||||
* outlen >= 2^16
|
||||
* @note Assumes tc_ctr_prng_init has been called for ctx
|
||||
* @param ctx IN/OUT -- the PRNG context
|
||||
* @param additional_input IN -- additional input to the prng (may be null)
|
||||
* @param additionallen IN -- additional input length in bytes
|
||||
* @param out IN/OUT -- buffer to receive output
|
||||
* @param outlen IN -- size of out buffer in bytes
|
||||
*/
|
||||
int tc_ctr_prng_generate(TCCtrPrng_t *const ctx,
|
||||
uint8_t const *const additional_input,
|
||||
unsigned int additionallen,
|
||||
uint8_t *const out,
|
||||
unsigned int outlen);
|
||||
|
||||
/**
|
||||
* @brief CTR-PRNG uninstantiate procedure
|
||||
* Zeroes the internal state of the supplied prng context
|
||||
* @return none
|
||||
* @param ctx IN/OUT -- the PRNG context
|
||||
*/
|
||||
void tc_ctr_prng_uninstantiate(TCCtrPrng_t *const ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_CTR_PRNG_H__ */
|
||||
@@ -0,0 +1,545 @@
|
||||
/* ecc.h - TinyCrypt interface to common ECC functions */
|
||||
|
||||
/* Copyright (c) 2014, Kenneth MacKay
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief -- Interface to common ECC functions.
|
||||
*
|
||||
* Overview: This software is an implementation of common functions
|
||||
* necessary to elliptic curve cryptography. This implementation uses
|
||||
* curve NIST p-256.
|
||||
*
|
||||
* Security: The curve NIST p-256 provides approximately 128 bits of security.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __TC_UECC_H__
|
||||
#define __TC_UECC_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Word size (4 bytes considering 32-bits architectures) */
|
||||
#define uECC_WORD_SIZE 4
|
||||
|
||||
/* setting max number of calls to prng: */
|
||||
#ifndef uECC_RNG_MAX_TRIES
|
||||
#define uECC_RNG_MAX_TRIES 64
|
||||
#endif
|
||||
|
||||
/* defining data types to store word and bit counts: */
|
||||
typedef int8_t wordcount_t;
|
||||
typedef int16_t bitcount_t;
|
||||
/* defining data type for comparison result: */
|
||||
typedef int8_t cmpresult_t;
|
||||
/* defining data type to store ECC coordinate/point in 32bits words: */
|
||||
typedef unsigned int uECC_word_t;
|
||||
/* defining data type to store an ECC coordinate/point in 64bits words: */
|
||||
typedef uint64_t uECC_dword_t;
|
||||
|
||||
/* defining masks useful for ecc computations: */
|
||||
#define HIGH_BIT_SET 0x80000000
|
||||
#define uECC_WORD_BITS 32
|
||||
#define uECC_WORD_BITS_SHIFT 5
|
||||
#define uECC_WORD_BITS_MASK 0x01F
|
||||
|
||||
/* Number of words of 32 bits to represent an element of the the curve p-256: */
|
||||
#define NUM_ECC_WORDS 8
|
||||
/* Number of bytes to represent an element of the the curve p-256: */
|
||||
#define NUM_ECC_BYTES (uECC_WORD_SIZE*NUM_ECC_WORDS)
|
||||
|
||||
/* structure that represents an elliptic curve (e.g. p256):*/
|
||||
struct uECC_Curve_t;
|
||||
typedef const struct uECC_Curve_t *uECC_Curve;
|
||||
struct uECC_Curve_t {
|
||||
wordcount_t num_words;
|
||||
wordcount_t num_bytes;
|
||||
bitcount_t num_n_bits;
|
||||
uECC_word_t p[NUM_ECC_WORDS];
|
||||
uECC_word_t n[NUM_ECC_WORDS];
|
||||
uECC_word_t G[NUM_ECC_WORDS * 2];
|
||||
uECC_word_t b[NUM_ECC_WORDS];
|
||||
void (*double_jacobian)(uECC_word_t *X1, uECC_word_t *Y1, uECC_word_t *Z1,
|
||||
uECC_Curve curve);
|
||||
void (*x_side)(uECC_word_t *result, const uECC_word_t *x, uECC_Curve curve);
|
||||
void (*mmod_fast)(uECC_word_t *result, uECC_word_t *product);
|
||||
};
|
||||
|
||||
/*
|
||||
* @brief computes doubling of point ion jacobian coordinates, in place.
|
||||
* @param X1 IN/OUT -- x coordinate
|
||||
* @param Y1 IN/OUT -- y coordinate
|
||||
* @param Z1 IN/OUT -- z coordinate
|
||||
* @param curve IN -- elliptic curve
|
||||
*/
|
||||
void double_jacobian_default(uECC_word_t *X1, uECC_word_t *Y1,
|
||||
uECC_word_t *Z1, uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Computes x^3 + ax + b. result must not overlap x.
|
||||
* @param result OUT -- x^3 + ax + b
|
||||
* @param x IN -- value of x
|
||||
* @param curve IN -- elliptic curve
|
||||
*/
|
||||
void x_side_default(uECC_word_t *result, const uECC_word_t *x,
|
||||
uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Computes result = product % curve_p
|
||||
* from http://www.nsa.gov/ia/_files/nist-routines.pdf
|
||||
* @param result OUT -- product % curve_p
|
||||
* @param product IN -- value to be reduced mod curve_p
|
||||
*/
|
||||
void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int *product);
|
||||
|
||||
/* Bytes to words ordering: */
|
||||
#define BYTES_TO_WORDS_8(a, b, c, d, e, f, g, h) 0x##d##c##b##a, 0x##h##g##f##e
|
||||
#define BYTES_TO_WORDS_4(a, b, c, d) 0x##d##c##b##a
|
||||
#define BITS_TO_WORDS(num_bits) \
|
||||
((num_bits + ((uECC_WORD_SIZE * 8) - 1)) / (uECC_WORD_SIZE * 8))
|
||||
#define BITS_TO_BYTES(num_bits) ((num_bits + 7) / 8)
|
||||
|
||||
/* definition of curve NIST p-256: */
|
||||
static const struct uECC_Curve_t curve_secp256r1 = {
|
||||
NUM_ECC_WORDS,
|
||||
NUM_ECC_BYTES,
|
||||
256, /* num_n_bits */ {
|
||||
BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
|
||||
BYTES_TO_WORDS_8(FF, FF, FF, FF, 00, 00, 00, 00),
|
||||
BYTES_TO_WORDS_8(00, 00, 00, 00, 00, 00, 00, 00),
|
||||
BYTES_TO_WORDS_8(01, 00, 00, 00, FF, FF, FF, FF)
|
||||
}, {
|
||||
BYTES_TO_WORDS_8(51, 25, 63, FC, C2, CA, B9, F3),
|
||||
BYTES_TO_WORDS_8(84, 9E, 17, A7, AD, FA, E6, BC),
|
||||
BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
|
||||
BYTES_TO_WORDS_8(00, 00, 00, 00, FF, FF, FF, FF)
|
||||
}, {
|
||||
BYTES_TO_WORDS_8(96, C2, 98, D8, 45, 39, A1, F4),
|
||||
BYTES_TO_WORDS_8(A0, 33, EB, 2D, 81, 7D, 03, 77),
|
||||
BYTES_TO_WORDS_8(F2, 40, A4, 63, E5, E6, BC, F8),
|
||||
BYTES_TO_WORDS_8(47, 42, 2C, E1, F2, D1, 17, 6B),
|
||||
|
||||
BYTES_TO_WORDS_8(F5, 51, BF, 37, 68, 40, B6, CB),
|
||||
BYTES_TO_WORDS_8(CE, 5E, 31, 6B, 57, 33, CE, 2B),
|
||||
BYTES_TO_WORDS_8(16, 9E, 0F, 7C, 4A, EB, E7, 8E),
|
||||
BYTES_TO_WORDS_8(9B, 7F, 1A, FE, E2, 42, E3, 4F)
|
||||
}, {
|
||||
BYTES_TO_WORDS_8(4B, 60, D2, 27, 3E, 3C, CE, 3B),
|
||||
BYTES_TO_WORDS_8(F6, B0, 53, CC, B0, 06, 1D, 65),
|
||||
BYTES_TO_WORDS_8(BC, 86, 98, 76, 55, BD, EB, B3),
|
||||
BYTES_TO_WORDS_8(E7, 93, 3A, AA, D8, 35, C6, 5A)
|
||||
},
|
||||
&double_jacobian_default,
|
||||
&x_side_default,
|
||||
&vli_mmod_fast_secp256r1
|
||||
};
|
||||
|
||||
uECC_Curve uECC_secp256r1(void);
|
||||
|
||||
/*
|
||||
* @brief Generates a random integer in the range 0 < random < top.
|
||||
* Both random and top have num_words words.
|
||||
* @param random OUT -- random integer in the range 0 < random < top
|
||||
* @param top IN -- upper limit
|
||||
* @param num_words IN -- number of words
|
||||
* @return a random integer in the range 0 < random < top
|
||||
*/
|
||||
int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
|
||||
wordcount_t num_words);
|
||||
|
||||
|
||||
/* uECC_RNG_Function type
|
||||
* The RNG function should fill 'size' random bytes into 'dest'. It should
|
||||
* return 1 if 'dest' was filled with random data, or 0 if the random data could
|
||||
* not be generated. The filled-in values should be either truly random, or from
|
||||
* a cryptographically-secure PRNG.
|
||||
*
|
||||
* A correctly functioning RNG function must be set (using uECC_set_rng())
|
||||
* before calling uECC_make_key() or uECC_sign().
|
||||
*
|
||||
* Setting a correctly functioning RNG function improves the resistance to
|
||||
* side-channel attacks for uECC_shared_secret().
|
||||
*
|
||||
* A correct RNG function is set by default. If you are building on another
|
||||
* POSIX-compliant system that supports /dev/random or /dev/urandom, you can
|
||||
* define uECC_POSIX to use the predefined RNG.
|
||||
*/
|
||||
typedef int(*uECC_RNG_Function)(uint8_t *dest, unsigned int size);
|
||||
|
||||
/*
|
||||
* @brief Set the function that will be used to generate random bytes. The RNG
|
||||
* function should return 1 if the random data was generated, or 0 if the random
|
||||
* data could not be generated.
|
||||
*
|
||||
* @note On platforms where there is no predefined RNG function, this must be
|
||||
* called before uECC_make_key() or uECC_sign() are used.
|
||||
*
|
||||
* @param rng_function IN -- function that will be used to generate random bytes
|
||||
*/
|
||||
void uECC_set_rng(uECC_RNG_Function rng_function);
|
||||
|
||||
/*
|
||||
* @brief provides current uECC_RNG_Function.
|
||||
* @return Returns the function that will be used to generate random bytes.
|
||||
*/
|
||||
uECC_RNG_Function uECC_get_rng(void);
|
||||
|
||||
/*
|
||||
* @brief computes the size of a private key for the curve in bytes.
|
||||
* @param curve IN -- elliptic curve
|
||||
* @return size of a private key for the curve in bytes.
|
||||
*/
|
||||
int uECC_curve_private_key_size(uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief computes the size of a public key for the curve in bytes.
|
||||
* @param curve IN -- elliptic curve
|
||||
* @return the size of a public key for the curve in bytes.
|
||||
*/
|
||||
int uECC_curve_public_key_size(uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Compute the corresponding public key for a private key.
|
||||
* @param private_key IN -- The private key to compute the public key for
|
||||
* @param public_key OUT -- Will be filled in with the corresponding public key
|
||||
* @param curve
|
||||
* @return Returns 1 if key was computed successfully, 0 if an error occurred.
|
||||
*/
|
||||
int uECC_compute_public_key(const uint8_t *private_key,
|
||||
uint8_t *public_key, uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Compute public-key.
|
||||
* @return corresponding public-key.
|
||||
* @param result OUT -- public-key
|
||||
* @param private_key IN -- private-key
|
||||
* @param curve IN -- elliptic curve
|
||||
*/
|
||||
uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
|
||||
uECC_word_t *private_key, uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Regularize the bitcount for the private key so that attackers cannot
|
||||
* use a side channel attack to learn the number of leading zeros.
|
||||
* @return Regularized k
|
||||
* @param k IN -- private-key
|
||||
* @param k0 IN/OUT -- regularized k
|
||||
* @param k1 IN/OUT -- regularized k
|
||||
* @param curve IN -- elliptic curve
|
||||
*/
|
||||
uECC_word_t regularize_k(const uECC_word_t *const k, uECC_word_t *k0,
|
||||
uECC_word_t *k1, uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Point multiplication algorithm using Montgomery's ladder with co-Z
|
||||
* coordinates. See http://eprint.iacr.org/2011/338.pdf.
|
||||
* @note Result may overlap point.
|
||||
* @param result OUT -- returns scalar*point
|
||||
* @param point IN -- elliptic curve point
|
||||
* @param scalar IN -- scalar
|
||||
* @param initial_Z IN -- initial value for z
|
||||
* @param num_bits IN -- number of bits in scalar
|
||||
* @param curve IN -- elliptic curve
|
||||
*/
|
||||
void EccPoint_mult(uECC_word_t *result, const uECC_word_t *point,
|
||||
const uECC_word_t *scalar, const uECC_word_t *initial_Z,
|
||||
bitcount_t num_bits, uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Constant-time comparison to zero - secure way to compare long integers
|
||||
* @param vli IN -- very long integer
|
||||
* @param num_words IN -- number of words in the vli
|
||||
* @return 1 if vli == 0, 0 otherwise.
|
||||
*/
|
||||
uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief Check if 'point' is the point at infinity
|
||||
* @param point IN -- elliptic curve point
|
||||
* @param curve IN -- elliptic curve
|
||||
* @return if 'point' is the point at infinity, 0 otherwise.
|
||||
*/
|
||||
uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief computes the sign of left - right, in constant time.
|
||||
* @param left IN -- left term to be compared
|
||||
* @param right IN -- right term to be compared
|
||||
* @param num_words IN -- number of words
|
||||
* @return the sign of left - right
|
||||
*/
|
||||
cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right,
|
||||
wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief computes sign of left - right, not in constant time.
|
||||
* @note should not be used if inputs are part of a secret
|
||||
* @param left IN -- left term to be compared
|
||||
* @param right IN -- right term to be compared
|
||||
* @param num_words IN -- number of words
|
||||
* @return the sign of left - right
|
||||
*/
|
||||
cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left, const uECC_word_t *right,
|
||||
wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief Computes result = (left - right) % mod.
|
||||
* @note Assumes that (left < mod) and (right < mod), and that result does not
|
||||
* overlap mod.
|
||||
* @param result OUT -- (left - right) % mod
|
||||
* @param left IN -- leftright term in modular subtraction
|
||||
* @param right IN -- right term in modular subtraction
|
||||
* @param mod IN -- mod
|
||||
* @param num_words IN -- number of words
|
||||
*/
|
||||
void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, const uECC_word_t *mod,
|
||||
wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief Computes P' = (x1', y1', Z3), P + Q = (x3, y3, Z3) or
|
||||
* P => P', Q => P + Q
|
||||
* @note assumes Input P = (x1, y1, Z), Q = (x2, y2, Z)
|
||||
* @param X1 IN -- x coordinate of P
|
||||
* @param Y1 IN -- y coordinate of P
|
||||
* @param X2 IN -- x coordinate of Q
|
||||
* @param Y2 IN -- y coordinate of Q
|
||||
* @param curve IN -- elliptic curve
|
||||
*/
|
||||
void XYcZ_add(uECC_word_t *X1, uECC_word_t *Y1, uECC_word_t *X2,
|
||||
uECC_word_t *Y2, uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Computes (x1 * z^2, y1 * z^3)
|
||||
* @param X1 IN -- previous x1 coordinate
|
||||
* @param Y1 IN -- previous y1 coordinate
|
||||
* @param Z IN -- z value
|
||||
* @param curve IN -- elliptic curve
|
||||
*/
|
||||
void apply_z(uECC_word_t *X1, uECC_word_t *Y1, const uECC_word_t *const Z,
|
||||
uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Check if bit is set.
|
||||
* @return Returns nonzero if bit 'bit' of vli is set.
|
||||
* @warning It is assumed that the value provided in 'bit' is within the
|
||||
* boundaries of the word-array 'vli'.
|
||||
* @note The bit ordering layout assumed for vli is: {31, 30, ..., 0},
|
||||
* {63, 62, ..., 32}, {95, 94, ..., 64}, {127, 126,..., 96} for a vli consisting
|
||||
* of 4 uECC_word_t elements.
|
||||
*/
|
||||
uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit);
|
||||
|
||||
/*
|
||||
* @brief Computes result = product % mod, where product is 2N words long.
|
||||
* @param result OUT -- product % mod
|
||||
* @param mod IN -- module
|
||||
* @param num_words IN -- number of words
|
||||
* @warning Currently only designed to work for curve_p or curve_n.
|
||||
*/
|
||||
void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
|
||||
const uECC_word_t *mod, wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief Computes modular product (using curve->mmod_fast)
|
||||
* @param result OUT -- (left * right) mod % curve_p
|
||||
* @param left IN -- left term in product
|
||||
* @param right IN -- right term in product
|
||||
* @param curve IN -- elliptic curve
|
||||
*/
|
||||
void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Computes result = left - right.
|
||||
* @note Can modify in place.
|
||||
* @param result OUT -- left - right
|
||||
* @param left IN -- left term in subtraction
|
||||
* @param right IN -- right term in subtraction
|
||||
* @param num_words IN -- number of words
|
||||
* @return borrow
|
||||
*/
|
||||
uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief Constant-time comparison function(secure way to compare long ints)
|
||||
* @param left IN -- left term in comparison
|
||||
* @param right IN -- right term in comparison
|
||||
* @param num_words IN -- number of words
|
||||
* @return Returns 0 if left == right, 1 otherwise.
|
||||
*/
|
||||
uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right,
|
||||
wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief Computes (left * right) % mod
|
||||
* @param result OUT -- (left * right) % mod
|
||||
* @param left IN -- left term in product
|
||||
* @param right IN -- right term in product
|
||||
* @param mod IN -- mod
|
||||
* @param num_words IN -- number of words
|
||||
*/
|
||||
void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, const uECC_word_t *mod,
|
||||
wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief Computes (1 / input) % mod
|
||||
* @note All VLIs are the same size.
|
||||
* @note See "Euclid's GCD to Montgomery Multiplication to the Great Divide"
|
||||
* @param result OUT -- (1 / input) % mod
|
||||
* @param input IN -- value to be modular inverted
|
||||
* @param mod IN -- mod
|
||||
* @param num_words -- number of words
|
||||
*/
|
||||
void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
|
||||
const uECC_word_t *mod, wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief Sets dest = src.
|
||||
* @param dest OUT -- destination buffer
|
||||
* @param src IN -- origin buffer
|
||||
* @param num_words IN -- number of words
|
||||
*/
|
||||
void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src,
|
||||
wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief Computes (left + right) % mod.
|
||||
* @note Assumes that (left < mod) and right < mod), and that result does not
|
||||
* overlap mod.
|
||||
* @param result OUT -- (left + right) % mod.
|
||||
* @param left IN -- left term in addition
|
||||
* @param right IN -- right term in addition
|
||||
* @param mod IN -- mod
|
||||
* @param num_words IN -- number of words
|
||||
*/
|
||||
void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, const uECC_word_t *mod,
|
||||
wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief Counts the number of bits required to represent vli.
|
||||
* @param vli IN -- very long integer
|
||||
* @param max_words IN -- number of words
|
||||
* @return number of bits in given vli
|
||||
*/
|
||||
bitcount_t uECC_vli_numBits(const uECC_word_t *vli,
|
||||
const wordcount_t max_words);
|
||||
|
||||
/*
|
||||
* @brief Erases (set to 0) vli
|
||||
* @param vli IN -- very long integer
|
||||
* @param num_words IN -- number of words
|
||||
*/
|
||||
void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words);
|
||||
|
||||
/*
|
||||
* @brief check if it is a valid point in the curve
|
||||
* @param point IN -- point to be checked
|
||||
* @param curve IN -- elliptic curve
|
||||
* @return 0 if point is valid
|
||||
* @exception returns -1 if it is a point at infinity
|
||||
* @exception returns -2 if x or y is smaller than p,
|
||||
* @exception returns -3 if y^2 != x^3 + ax + b.
|
||||
*/
|
||||
int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Check if a public key is valid.
|
||||
* @param public_key IN -- The public key to be checked.
|
||||
* @return returns 0 if the public key is valid
|
||||
* @exception returns -1 if it is a point at infinity
|
||||
* @exception returns -2 if x or y is smaller than p,
|
||||
* @exception returns -3 if y^2 != x^3 + ax + b.
|
||||
* @exception returns -4 if public key is the group generator.
|
||||
*
|
||||
* @note Note that you are not required to check for a valid public key before
|
||||
* using any other uECC functions. However, you may wish to avoid spending CPU
|
||||
* time computing a shared secret or verifying a signature using an invalid
|
||||
* public key.
|
||||
*/
|
||||
int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve);
|
||||
|
||||
/*
|
||||
* @brief Converts an integer in uECC native format to big-endian bytes.
|
||||
* @param bytes OUT -- bytes representation
|
||||
* @param num_bytes IN -- number of bytes
|
||||
* @param native IN -- uECC native representation
|
||||
*/
|
||||
void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
|
||||
const unsigned int *native);
|
||||
|
||||
/*
|
||||
* @brief Converts big-endian bytes to an integer in uECC native format.
|
||||
* @param native OUT -- uECC native representation
|
||||
* @param bytes IN -- bytes representation
|
||||
* @param num_bytes IN -- number of bytes
|
||||
*/
|
||||
void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
|
||||
int num_bytes);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_UECC_H__ */
|
||||
@@ -0,0 +1,131 @@
|
||||
/* ecc_dh.h - TinyCrypt interface to EC-DH implementation */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2014, Kenneth MacKay
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief -- Interface to EC-DH implementation.
|
||||
*
|
||||
* Overview: This software is an implementation of EC-DH. This implementation
|
||||
* uses curve NIST p-256.
|
||||
*
|
||||
* Security: The curve NIST p-256 provides approximately 128 bits of security.
|
||||
*/
|
||||
|
||||
#ifndef __TC_ECC_DH_H__
|
||||
#define __TC_ECC_DH_H__
|
||||
|
||||
#include <tinycrypt/ecc.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Create a public/private key pair.
|
||||
* @return returns TC_CRYPTO_SUCCESS (1) if the key pair was generated successfully
|
||||
* returns TC_CRYPTO_FAIL (0) if error while generating key pair
|
||||
*
|
||||
* @param p_public_key OUT -- Will be filled in with the public key. Must be at
|
||||
* least 2 * the curve size (in bytes) long. For curve secp256r1, p_public_key
|
||||
* must be 64 bytes long.
|
||||
* @param p_private_key OUT -- Will be filled in with the private key. Must be as
|
||||
* long as the curve order (for secp256r1, p_private_key must be 32 bytes long).
|
||||
*
|
||||
* @note side-channel countermeasure: algorithm strengthened against timing
|
||||
* attack.
|
||||
* @warning A cryptographically-secure PRNG function must be set (using
|
||||
* uECC_set_rng()) before calling uECC_make_key().
|
||||
*/
|
||||
int uECC_make_key(uint8_t *p_public_key, uint8_t *p_private_key, uECC_Curve curve);
|
||||
|
||||
#ifdef ENABLE_TESTS
|
||||
|
||||
/**
|
||||
* @brief Create a public/private key pair given a specific d.
|
||||
*
|
||||
* @note THIS FUNCTION SHOULD BE CALLED ONLY FOR TEST PURPOSES. Refer to
|
||||
* uECC_make_key() function for real applications.
|
||||
*/
|
||||
int uECC_make_key_with_d(uint8_t *p_public_key, uint8_t *p_private_key,
|
||||
unsigned int *d, uECC_Curve curve);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Compute a shared secret given your secret key and someone else's
|
||||
* public key.
|
||||
* @return returns TC_CRYPTO_SUCCESS (1) if the shared secret was computed successfully
|
||||
* returns TC_CRYPTO_FAIL (0) otherwise
|
||||
*
|
||||
* @param p_secret OUT -- Will be filled in with the shared secret value. Must be
|
||||
* the same size as the curve size (for curve secp256r1, secret must be 32 bytes
|
||||
* long.
|
||||
* @param p_public_key IN -- The public key of the remote party.
|
||||
* @param p_private_key IN -- Your private key.
|
||||
*
|
||||
* @warning It is recommended to use the output of uECC_shared_secret() as the
|
||||
* input of a recommended Key Derivation Function (see NIST SP 800-108) in
|
||||
* order to produce a cryptographically secure symmetric key.
|
||||
*/
|
||||
int uECC_shared_secret(const uint8_t *p_public_key, const uint8_t *p_private_key,
|
||||
uint8_t *p_secret, uECC_Curve curve);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_ECC_DH_H__ */
|
||||
@@ -0,0 +1,139 @@
|
||||
/* ecc_dh.h - TinyCrypt interface to EC-DSA implementation */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2014, Kenneth MacKay
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief -- Interface to EC-DSA implementation.
|
||||
*
|
||||
* Overview: This software is an implementation of EC-DSA. This implementation
|
||||
* uses curve NIST p-256.
|
||||
*
|
||||
* Security: The curve NIST p-256 provides approximately 128 bits of security.
|
||||
*
|
||||
* Usage: - To sign: Compute a hash of the data you wish to sign (SHA-2 is
|
||||
* recommended) and pass it in to ecdsa_sign function along with your
|
||||
* private key and a random number. You must use a new non-predictable
|
||||
* random number to generate each new signature.
|
||||
* - To verify a signature: Compute the hash of the signed data using
|
||||
* the same hash as the signer and pass it to this function along with
|
||||
* the signer's public key and the signature values (r and s).
|
||||
*/
|
||||
|
||||
#ifndef __TC_ECC_DSA_H__
|
||||
#define __TC_ECC_DSA_H__
|
||||
|
||||
#include <tinycrypt/ecc.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Generate an ECDSA signature for a given hash value.
|
||||
* @return returns TC_CRYPTO_SUCCESS (1) if the signature generated successfully
|
||||
* returns TC_CRYPTO_FAIL (0) if an error occurred.
|
||||
*
|
||||
* @param p_private_key IN -- Your private key.
|
||||
* @param p_message_hash IN -- The hash of the message to sign.
|
||||
* @param p_hash_size IN -- The size of p_message_hash in bytes.
|
||||
* @param p_signature OUT -- Will be filled in with the signature value. Must be
|
||||
* at least 2 * curve size long (for secp256r1, signature must be 64 bytes long).
|
||||
*
|
||||
* @warning A cryptographically-secure PRNG function must be set (using
|
||||
* uECC_set_rng()) before calling uECC_sign().
|
||||
* @note Usage: Compute a hash of the data you wish to sign (SHA-2 is
|
||||
* recommended) and pass it in to this function along with your private key.
|
||||
* @note side-channel countermeasure: algorithm strengthened against timing
|
||||
* attack.
|
||||
*/
|
||||
int uECC_sign(const uint8_t *p_private_key, const uint8_t *p_message_hash,
|
||||
unsigned p_hash_size, uint8_t *p_signature, uECC_Curve curve);
|
||||
|
||||
#ifdef ENABLE_TESTS
|
||||
/*
|
||||
* THIS FUNCTION SHOULD BE CALLED FOR TEST PURPOSES ONLY.
|
||||
* Refer to uECC_sign() function for real applications.
|
||||
*/
|
||||
int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
|
||||
unsigned int hash_size, uECC_word_t *k, uint8_t *signature,
|
||||
uECC_Curve curve);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Verify an ECDSA signature.
|
||||
* @return returns TC_SUCCESS (1) if the signature is valid
|
||||
* returns TC_FAIL (0) if the signature is invalid.
|
||||
*
|
||||
* @param p_public_key IN -- The signer's public key.
|
||||
* @param p_message_hash IN -- The hash of the signed data.
|
||||
* @param p_hash_size IN -- The size of p_message_hash in bytes.
|
||||
* @param p_signature IN -- The signature values.
|
||||
*
|
||||
* @note Usage: Compute the hash of the signed data using the same hash as the
|
||||
* signer and pass it to this function along with the signer's public key and
|
||||
* the signature values (hash_size and signature).
|
||||
*/
|
||||
int uECC_verify(const uint8_t *p_public_key, const uint8_t *p_message_hash,
|
||||
unsigned int p_hash_size, const uint8_t *p_signature, uECC_Curve curve);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_ECC_DSA_H__ */
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
/* uECC_platform_specific.h - Interface to platform specific functions*/
|
||||
|
||||
/* Copyright (c) 2014, Kenneth MacKay
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* uECC_platform_specific.h -- Interface to platform specific functions
|
||||
*/
|
||||
|
||||
#ifndef __UECC_PLATFORM_SPECIFIC_H_
|
||||
#define __UECC_PLATFORM_SPECIFIC_H_
|
||||
|
||||
/*
|
||||
* The RNG function should fill 'size' random bytes into 'dest'. It should
|
||||
* return 1 if 'dest' was filled with random data, or 0 if the random data could
|
||||
* not be generated. The filled-in values should be either truly random, or from
|
||||
* a cryptographically-secure PRNG.
|
||||
*
|
||||
* A cryptographically-secure PRNG function must be set (using uECC_set_rng())
|
||||
* before calling uECC_make_key() or uECC_sign().
|
||||
*
|
||||
* Setting a cryptographically-secure PRNG function improves the resistance to
|
||||
* side-channel attacks for uECC_shared_secret().
|
||||
*
|
||||
* A correct PRNG function is set by default (default_RNG_defined = 1) and works
|
||||
* for some platforms, such as Unix and Linux. For other platforms, you may need
|
||||
* to provide another PRNG function.
|
||||
*/
|
||||
#define default_RNG_defined 0
|
||||
|
||||
int default_CSPRNG(uint8_t *dest, unsigned int size);
|
||||
|
||||
#endif /* __UECC_PLATFORM_SPECIFIC_H_ */
|
||||
@@ -0,0 +1,139 @@
|
||||
/* hmac.h - TinyCrypt interface to an HMAC implementation */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Interface to an HMAC implementation.
|
||||
*
|
||||
* Overview: HMAC is a message authentication code based on hash functions.
|
||||
* TinyCrypt hard codes SHA-256 as the hash function. A message
|
||||
* authentication code based on hash functions is also called a
|
||||
* keyed cryptographic hash function since it performs a
|
||||
* transformation specified by a key in an arbitrary length data
|
||||
* set into a fixed length data set (also called tag).
|
||||
*
|
||||
* Security: The security of the HMAC depends on the length of the key and
|
||||
* on the security of the hash function. Note that HMAC primitives
|
||||
* are much less affected by collision attacks than their
|
||||
* corresponding hash functions.
|
||||
*
|
||||
* Requires: SHA-256
|
||||
*
|
||||
* Usage: 1) call tc_hmac_set_key to set the HMAC key.
|
||||
*
|
||||
* 2) call tc_hmac_init to initialize a struct hash_state before
|
||||
* processing the data.
|
||||
*
|
||||
* 3) call tc_hmac_update to process the next input segment;
|
||||
* tc_hmac_update can be called as many times as needed to process
|
||||
* all of the segments of the input; the order is important.
|
||||
*
|
||||
* 4) call tc_hmac_final to out put the tag.
|
||||
*/
|
||||
|
||||
#ifndef __TC_HMAC_H__
|
||||
#define __TC_HMAC_H__
|
||||
|
||||
#include <tinycrypt/sha256.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct tc_hmac_state_struct {
|
||||
/* the internal state required by h */
|
||||
struct tc_sha256_state_struct hash_state;
|
||||
/* HMAC key schedule */
|
||||
uint8_t key[2 * TC_SHA256_BLOCK_SIZE];
|
||||
};
|
||||
typedef struct tc_hmac_state_struct *TCHmacState_t;
|
||||
|
||||
/**
|
||||
* @brief HMAC set key procedure
|
||||
* Configures ctx to use key
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if
|
||||
* ctx == NULL or
|
||||
* key == NULL or
|
||||
* key_size == 0
|
||||
* @param ctx IN/OUT -- the struct tc_hmac_state_struct to initial
|
||||
* @param key IN -- the HMAC key to configure
|
||||
* @param key_size IN -- the HMAC key size
|
||||
*/
|
||||
int tc_hmac_set_key(TCHmacState_t ctx, const uint8_t *key,
|
||||
unsigned int key_size);
|
||||
|
||||
/**
|
||||
* @brief HMAC init procedure
|
||||
* Initializes ctx to begin the next HMAC operation
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if: ctx == NULL or key == NULL
|
||||
* @param ctx IN/OUT -- struct tc_hmac_state_struct buffer to init
|
||||
*/
|
||||
int tc_hmac_init(TCHmacState_t ctx);
|
||||
|
||||
/**
|
||||
* @brief HMAC update procedure
|
||||
* Mixes data_length bytes addressed by data into state
|
||||
* @return returns TC_CRYPTO_SUCCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if: ctx == NULL or key == NULL
|
||||
* @note Assumes state has been initialized by tc_hmac_init
|
||||
* @param ctx IN/OUT -- state of HMAC computation so far
|
||||
* @param data IN -- data to incorporate into state
|
||||
* @param data_length IN -- size of data in bytes
|
||||
*/
|
||||
int tc_hmac_update(TCHmacState_t ctx, const void *data,
|
||||
unsigned int data_length);
|
||||
|
||||
/**
|
||||
* @brief HMAC final procedure
|
||||
* Writes the HMAC tag into the tag buffer
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* tag == NULL or
|
||||
* ctx == NULL or
|
||||
* key == NULL or
|
||||
* taglen != TC_SHA256_DIGEST_SIZE
|
||||
* @note ctx is erased before exiting. This should never be changed/removed.
|
||||
* @note Assumes the tag bufer is at least sizeof(hmac_tag_size(state)) bytes
|
||||
* state has been initialized by tc_hmac_init
|
||||
* @param tag IN/OUT -- buffer to receive computed HMAC tag
|
||||
* @param taglen IN -- size of tag in bytes
|
||||
* @param ctx IN/OUT -- the HMAC state for computing tag
|
||||
*/
|
||||
int tc_hmac_final(uint8_t *tag, unsigned int taglen, TCHmacState_t ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*__TC_HMAC_H__*/
|
||||
@@ -0,0 +1,164 @@
|
||||
/* hmac_prng.h - TinyCrypt interface to an HMAC-PRNG implementation */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Interface to an HMAC-PRNG implementation.
|
||||
*
|
||||
* Overview: A pseudo-random number generator (PRNG) generates a sequence
|
||||
* of numbers that have a distribution close to the one expected
|
||||
* for a sequence of truly random numbers. The NIST Special
|
||||
* Publication 800-90A specifies several mechanisms to generate
|
||||
* sequences of pseudo random numbers, including the HMAC-PRNG one
|
||||
* which is based on HMAC. TinyCrypt implements HMAC-PRNG with
|
||||
* certain modifications from the NIST SP 800-90A spec.
|
||||
*
|
||||
* Security: A cryptographically secure PRNG depends on the existence of an
|
||||
* entropy source to provide a truly random seed as well as the
|
||||
* security of the primitives used as the building blocks (HMAC and
|
||||
* SHA256, for TinyCrypt).
|
||||
*
|
||||
* The NIST SP 800-90A standard tolerates a null personalization,
|
||||
* while TinyCrypt requires a non-null personalization. This is
|
||||
* because a personalization string (the host name concatenated
|
||||
* with a time stamp, for example) is easily computed and might be
|
||||
* the last line of defense against failure of the entropy source.
|
||||
*
|
||||
* Requires: - SHA-256
|
||||
* - HMAC
|
||||
*
|
||||
* Usage: 1) call tc_hmac_prng_init to set the HMAC key and process the
|
||||
* personalization data.
|
||||
*
|
||||
* 2) call tc_hmac_prng_reseed to process the seed and additional
|
||||
* input.
|
||||
*
|
||||
* 3) call tc_hmac_prng_generate to out put the pseudo-random data.
|
||||
*/
|
||||
|
||||
#ifndef __TC_HMAC_PRNG_H__
|
||||
#define __TC_HMAC_PRNG_H__
|
||||
|
||||
#include <tinycrypt/sha256.h>
|
||||
#include <tinycrypt/hmac.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TC_HMAC_PRNG_RESEED_REQ -1
|
||||
|
||||
struct tc_hmac_prng_struct {
|
||||
/* the HMAC instance for this PRNG */
|
||||
struct tc_hmac_state_struct h;
|
||||
/* the PRNG key */
|
||||
uint8_t key[TC_SHA256_DIGEST_SIZE];
|
||||
/* PRNG state */
|
||||
uint8_t v[TC_SHA256_DIGEST_SIZE];
|
||||
/* calls to tc_hmac_prng_generate left before re-seed */
|
||||
unsigned int countdown;
|
||||
};
|
||||
|
||||
typedef struct tc_hmac_prng_struct *TCHmacPrng_t;
|
||||
|
||||
/**
|
||||
* @brief HMAC-PRNG initialization procedure
|
||||
* Initializes prng with personalization, disables tc_hmac_prng_generate
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* prng == NULL,
|
||||
* personalization == NULL,
|
||||
* plen > MAX_PLEN
|
||||
* @note Assumes: - personalization != NULL.
|
||||
* The personalization is a platform unique string (e.g., the host
|
||||
* name) and is the last line of defense against failure of the
|
||||
* entropy source
|
||||
* @warning NIST SP 800-90A specifies 3 items as seed material during
|
||||
* initialization: entropy seed, personalization, and an optional
|
||||
* nonce. TinyCrypts requires instead a non-null personalization
|
||||
* (which is easily computed) and indirectly requires an entropy
|
||||
* seed (since the reseed function is mandatorily called after
|
||||
* init)
|
||||
* @param prng IN/OUT -- the PRNG state to initialize
|
||||
* @param personalization IN -- personalization string
|
||||
* @param plen IN -- personalization length in bytes
|
||||
*/
|
||||
int tc_hmac_prng_init(TCHmacPrng_t prng,
|
||||
const uint8_t *personalization,
|
||||
unsigned int plen);
|
||||
|
||||
/**
|
||||
* @brief HMAC-PRNG reseed procedure
|
||||
* Mixes seed into prng, enables tc_hmac_prng_generate
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* prng == NULL,
|
||||
* seed == NULL,
|
||||
* seedlen < MIN_SLEN,
|
||||
* seendlen > MAX_SLEN,
|
||||
* additional_input != (const uint8_t *) 0 && additionallen == 0,
|
||||
* additional_input != (const uint8_t *) 0 && additionallen > MAX_ALEN
|
||||
* @note Assumes:- tc_hmac_prng_init has been called for prng
|
||||
* - seed has sufficient entropy.
|
||||
*
|
||||
* @param prng IN/OUT -- the PRNG state
|
||||
* @param seed IN -- entropy to mix into the prng
|
||||
* @param seedlen IN -- length of seed in bytes
|
||||
* @param additional_input IN -- additional input to the prng
|
||||
* @param additionallen IN -- additional input length in bytes
|
||||
*/
|
||||
int tc_hmac_prng_reseed(TCHmacPrng_t prng, const uint8_t *seed,
|
||||
unsigned int seedlen, const uint8_t *additional_input,
|
||||
unsigned int additionallen);
|
||||
|
||||
/**
|
||||
* @brief HMAC-PRNG generate procedure
|
||||
* Generates outlen pseudo-random bytes into out buffer, updates prng
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_HMAC_PRNG_RESEED_REQ (-1) if a reseed is needed
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* out == NULL,
|
||||
* prng == NULL,
|
||||
* outlen == 0,
|
||||
* outlen >= MAX_OUT
|
||||
* @note Assumes tc_hmac_prng_init has been called for prng
|
||||
* @param out IN/OUT -- buffer to receive output
|
||||
* @param outlen IN -- size of out buffer in bytes
|
||||
* @param prng IN/OUT -- the PRNG state
|
||||
*/
|
||||
int tc_hmac_prng_generate(uint8_t *out, unsigned int outlen, TCHmacPrng_t prng);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_HMAC_PRNG_H__ */
|
||||
@@ -0,0 +1,129 @@
|
||||
/* sha256.h - TinyCrypt interface to a SHA-256 implementation */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Interface to a SHA-256 implementation.
|
||||
*
|
||||
* Overview: SHA-256 is a NIST approved cryptographic hashing algorithm
|
||||
* specified in FIPS 180. A hash algorithm maps data of arbitrary
|
||||
* size to data of fixed length.
|
||||
*
|
||||
* Security: SHA-256 provides 128 bits of security against collision attacks
|
||||
* and 256 bits of security against pre-image attacks. SHA-256 does
|
||||
* NOT behave like a random oracle, but it can be used as one if
|
||||
* the string being hashed is prefix-free encoded before hashing.
|
||||
*
|
||||
* Usage: 1) call tc_sha256_init to initialize a struct
|
||||
* tc_sha256_state_struct before hashing a new string.
|
||||
*
|
||||
* 2) call tc_sha256_update to hash the next string segment;
|
||||
* tc_sha256_update can be called as many times as needed to hash
|
||||
* all of the segments of a string; the order is important.
|
||||
*
|
||||
* 3) call tc_sha256_final to out put the digest from a hashing
|
||||
* operation.
|
||||
*/
|
||||
|
||||
#ifndef __TC_SHA256_H__
|
||||
#define __TC_SHA256_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TC_SHA256_BLOCK_SIZE (64)
|
||||
#define TC_SHA256_DIGEST_SIZE (32)
|
||||
#define TC_SHA256_STATE_BLOCKS (TC_SHA256_DIGEST_SIZE/4)
|
||||
|
||||
struct tc_sha256_state_struct {
|
||||
unsigned int iv[TC_SHA256_STATE_BLOCKS];
|
||||
uint64_t bits_hashed;
|
||||
uint8_t leftover[TC_SHA256_BLOCK_SIZE];
|
||||
size_t leftover_offset;
|
||||
};
|
||||
|
||||
typedef struct tc_sha256_state_struct *TCSha256State_t;
|
||||
|
||||
/**
|
||||
* @brief SHA256 initialization procedure
|
||||
* Initializes s
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if s == NULL
|
||||
* @param s Sha256 state struct
|
||||
*/
|
||||
int tc_sha256_init(TCSha256State_t s);
|
||||
|
||||
/**
|
||||
* @brief SHA256 update procedure
|
||||
* Hashes data_length bytes addressed by data into state s
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* s == NULL,
|
||||
* s->iv == NULL,
|
||||
* data == NULL
|
||||
* @note Assumes s has been initialized by tc_sha256_init
|
||||
* @warning The state buffer 'leftover' is left in memory after processing
|
||||
* If your application intends to have sensitive data in this
|
||||
* buffer, remind to erase it after the data has been processed
|
||||
* @param s Sha256 state struct
|
||||
* @param data message to hash
|
||||
* @param datalen length of message to hash
|
||||
*/
|
||||
int tc_sha256_update(TCSha256State_t s, const uint8_t *data, size_t datalen);
|
||||
|
||||
/**
|
||||
* @brief SHA256 final procedure
|
||||
* Inserts the completed hash computation into digest
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* s == NULL,
|
||||
* s->iv == NULL,
|
||||
* digest == NULL
|
||||
* @note Assumes: s has been initialized by tc_sha256_init
|
||||
* digest points to at least TC_SHA256_DIGEST_SIZE bytes
|
||||
* @warning The state buffer 'leftover' is left in memory after processing
|
||||
* If your application intends to have sensitive data in this
|
||||
* buffer, remind to erase it after the data has been processed
|
||||
* @param digest unsigned eight bit integer
|
||||
* @param Sha256 state struct
|
||||
*/
|
||||
int tc_sha256_final(uint8_t *digest, TCSha256State_t s);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_SHA256_H__ */
|
||||
@@ -0,0 +1,95 @@
|
||||
/* utils.h - TinyCrypt interface to platform-dependent run-time operations */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief Interface to platform-dependent run-time operations.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __TC_UTILS_H__
|
||||
#define __TC_UTILS_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Copy the the buffer 'from' to the buffer 'to'.
|
||||
* @return returns TC_CRYPTO_SUCCESS (1)
|
||||
* returns TC_CRYPTO_FAIL (0) if:
|
||||
* from_len > to_len.
|
||||
*
|
||||
* @param to OUT -- destination buffer
|
||||
* @param to_len IN -- length of destination buffer
|
||||
* @param from IN -- origin buffer
|
||||
* @param from_len IN -- length of origin buffer
|
||||
*/
|
||||
unsigned int _copy(uint8_t *to, unsigned int to_len,
|
||||
const uint8_t *from, unsigned int from_len);
|
||||
|
||||
/**
|
||||
* @brief Set the value 'val' into the buffer 'to', 'len' times.
|
||||
*
|
||||
* @param to OUT -- destination buffer
|
||||
* @param val IN -- value to be set in 'to'
|
||||
* @param len IN -- number of times the value will be copied
|
||||
*/
|
||||
void _set(void *to, uint8_t val, unsigned int len);
|
||||
|
||||
/*
|
||||
* @brief AES specific doubling function, which utilizes
|
||||
* the finite field used by AES.
|
||||
* @return Returns a^2
|
||||
*
|
||||
* @param a IN/OUT -- value to be doubled
|
||||
*/
|
||||
uint8_t _double_byte(uint8_t a);
|
||||
|
||||
/*
|
||||
* @brief Constant-time algorithm to compare if two sequences of bytes are equal
|
||||
* @return Returns 0 if equal, and non-zero otherwise
|
||||
*
|
||||
* @param a IN -- sequence of bytes a
|
||||
* @param b IN -- sequence of bytes b
|
||||
* @param size IN -- size of sequences a and b
|
||||
*/
|
||||
int _compare(const uint8_t *a, const uint8_t *b, size_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TC_UTILS_H__ */
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
TOP_DIR = ../../../../../..
|
||||
sinclude $(TOP_DIR)/tools/w800/conf.mk
|
||||
|
||||
ifndef PDIR
|
||||
GEN_LIBS = libwmtinycrypt$(LIB_EXT)
|
||||
endif
|
||||
|
||||
#DEFINES +=
|
||||
|
||||
sinclude $(TOP_DIR)/tools/w800/rules.mk
|
||||
|
||||
INCLUDES := $(INCLUDES) -I $(PDIR)include
|
||||
PDIR := ../$(PDIR)
|
||||
sinclude $(PDIR)Makefile
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
/* aes_decrypt.c - TinyCrypt implementation of AES decryption procedure */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/aes.h>
|
||||
#include <tinycrypt/constants.h>
|
||||
#include <tinycrypt/utils.h>
|
||||
|
||||
static const uint8_t inv_sbox[256] = {
|
||||
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e,
|
||||
0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
|
||||
0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32,
|
||||
0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
|
||||
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49,
|
||||
0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
|
||||
0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50,
|
||||
0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
|
||||
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05,
|
||||
0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
|
||||
0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41,
|
||||
0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
|
||||
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8,
|
||||
0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
|
||||
0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b,
|
||||
0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
|
||||
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59,
|
||||
0x27, 0x80, 0xec, 0x5f, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
|
||||
0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d,
|
||||
0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
|
||||
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63,
|
||||
0x55, 0x21, 0x0c, 0x7d
|
||||
};
|
||||
|
||||
int tc_aes128_set_decrypt_key(TCAesKeySched_t s, const uint8_t *k)
|
||||
{
|
||||
return tc_aes128_set_encrypt_key(s, k);
|
||||
}
|
||||
|
||||
#define mult8(a)(_double_byte(_double_byte(_double_byte(a))))
|
||||
#define mult9(a)(mult8(a)^(a))
|
||||
#define multb(a)(mult8(a)^_double_byte(a)^(a))
|
||||
#define multd(a)(mult8(a)^_double_byte(_double_byte(a))^(a))
|
||||
#define multe(a)(mult8(a)^_double_byte(_double_byte(a))^_double_byte(a))
|
||||
|
||||
static inline void mult_row_column(uint8_t *out, const uint8_t *in)
|
||||
{
|
||||
out[0] = multe(in[0]) ^ multb(in[1]) ^ multd(in[2]) ^ mult9(in[3]);
|
||||
out[1] = mult9(in[0]) ^ multe(in[1]) ^ multb(in[2]) ^ multd(in[3]);
|
||||
out[2] = multd(in[0]) ^ mult9(in[1]) ^ multe(in[2]) ^ multb(in[3]);
|
||||
out[3] = multb(in[0]) ^ multd(in[1]) ^ mult9(in[2]) ^ multe(in[3]);
|
||||
}
|
||||
|
||||
static inline void inv_mix_columns(uint8_t *s)
|
||||
{
|
||||
uint8_t t[Nb * Nk];
|
||||
mult_row_column(t, s);
|
||||
mult_row_column(&t[Nb], s + Nb);
|
||||
mult_row_column(&t[2 * Nb], s + (2 * Nb));
|
||||
mult_row_column(&t[3 * Nb], s + (3 * Nb));
|
||||
(void)_copy(s, sizeof(t), t, sizeof(t));
|
||||
}
|
||||
|
||||
static inline void add_round_key(uint8_t *s, const unsigned int *k)
|
||||
{
|
||||
s[0] ^= (uint8_t)(k[0] >> 24);
|
||||
s[1] ^= (uint8_t)(k[0] >> 16);
|
||||
s[2] ^= (uint8_t)(k[0] >> 8);
|
||||
s[3] ^= (uint8_t)(k[0]);
|
||||
s[4] ^= (uint8_t)(k[1] >> 24);
|
||||
s[5] ^= (uint8_t)(k[1] >> 16);
|
||||
s[6] ^= (uint8_t)(k[1] >> 8);
|
||||
s[7] ^= (uint8_t)(k[1]);
|
||||
s[8] ^= (uint8_t)(k[2] >> 24);
|
||||
s[9] ^= (uint8_t)(k[2] >> 16);
|
||||
s[10] ^= (uint8_t)(k[2] >> 8);
|
||||
s[11] ^= (uint8_t)(k[2]);
|
||||
s[12] ^= (uint8_t)(k[3] >> 24);
|
||||
s[13] ^= (uint8_t)(k[3] >> 16);
|
||||
s[14] ^= (uint8_t)(k[3] >> 8);
|
||||
s[15] ^= (uint8_t)(k[3]);
|
||||
}
|
||||
|
||||
static inline void inv_sub_bytes(uint8_t *s)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for(i = 0; i < (Nb * Nk); ++i) {
|
||||
s[i] = inv_sbox[s[i]];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This inv_shift_rows also implements the matrix flip required for
|
||||
* inv_mix_columns, but performs it here to reduce the number of memory
|
||||
* operations.
|
||||
*/
|
||||
static inline void inv_shift_rows(uint8_t *s)
|
||||
{
|
||||
uint8_t t[Nb * Nk];
|
||||
t[0] = s[0];
|
||||
t[1] = s[13];
|
||||
t[2] = s[10];
|
||||
t[3] = s[7];
|
||||
t[4] = s[4];
|
||||
t[5] = s[1];
|
||||
t[6] = s[14];
|
||||
t[7] = s[11];
|
||||
t[8] = s[8];
|
||||
t[9] = s[5];
|
||||
t[10] = s[2];
|
||||
t[11] = s[15];
|
||||
t[12] = s[12];
|
||||
t[13] = s[9];
|
||||
t[14] = s[6];
|
||||
t[15] = s[3];
|
||||
(void)_copy(s, sizeof(t), t, sizeof(t));
|
||||
}
|
||||
|
||||
int tc_aes_decrypt(uint8_t *out, const uint8_t *in, const TCAesKeySched_t s)
|
||||
{
|
||||
uint8_t state[Nk * Nb];
|
||||
unsigned int i;
|
||||
|
||||
if(out == (uint8_t *) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
} else if(in == (const uint8_t *) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
} else if(s == (TCAesKeySched_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
(void)_copy(state, sizeof(state), in, sizeof(state));
|
||||
add_round_key(state, s->words + Nb * Nr);
|
||||
|
||||
for(i = Nr - 1; i > 0; --i) {
|
||||
inv_shift_rows(state);
|
||||
inv_sub_bytes(state);
|
||||
add_round_key(state, s->words + Nb * i);
|
||||
inv_mix_columns(state);
|
||||
}
|
||||
|
||||
inv_shift_rows(state);
|
||||
inv_sub_bytes(state);
|
||||
add_round_key(state, s->words);
|
||||
(void)_copy(out, sizeof(state), state, sizeof(state));
|
||||
/*zeroing out the state buffer */
|
||||
_set(state, TC_ZERO_BYTE, sizeof(state));
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
+207
@@ -0,0 +1,207 @@
|
||||
/* aes_encrypt.c - TinyCrypt implementation of AES encryption procedure */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/aes.h>
|
||||
#include <tinycrypt/utils.h>
|
||||
#include <tinycrypt/constants.h>
|
||||
|
||||
static const uint8_t sbox[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
|
||||
};
|
||||
|
||||
static inline unsigned int rotword(unsigned int a)
|
||||
{
|
||||
return (((a) >> 24) | ((a) << 8));
|
||||
}
|
||||
|
||||
#define subbyte(a, o)(sbox[((a) >> (o))&0xff] << (o))
|
||||
#define subword(a)(subbyte(a, 24)|subbyte(a, 16)|subbyte(a, 8)|subbyte(a, 0))
|
||||
|
||||
int tc_aes128_set_encrypt_key(TCAesKeySched_t s, const uint8_t *k)
|
||||
{
|
||||
const unsigned int rconst[11] = {
|
||||
0x00000000, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000,
|
||||
0x20000000, 0x40000000, 0x80000000, 0x1b000000, 0x36000000
|
||||
};
|
||||
unsigned int i;
|
||||
|
||||
if(s == (TCAesKeySched_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
} else if(k == (const uint8_t *) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
for(i = 0; i < Nk; ++i) {
|
||||
s->words[i] = (k[Nb * i] << 24) | (k[Nb * i + 1] << 16) |
|
||||
(k[Nb * i + 2] << 8) | (k[Nb * i + 3]);
|
||||
}
|
||||
|
||||
for(; i < (Nb * (Nr + 1)); ++i) {
|
||||
unsigned int t = s->words[i - 1];
|
||||
|
||||
if((i % Nk) == 0) {
|
||||
t = subword(rotword(t)) ^ rconst[i / Nk];
|
||||
}
|
||||
|
||||
s->words[i] = s->words[i - Nk] ^ t;
|
||||
}
|
||||
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
static inline void add_round_key(uint8_t *s, const unsigned int *k)
|
||||
{
|
||||
s[0] ^= (uint8_t)(k[0] >> 24);
|
||||
s[1] ^= (uint8_t)(k[0] >> 16);
|
||||
s[2] ^= (uint8_t)(k[0] >> 8);
|
||||
s[3] ^= (uint8_t)(k[0]);
|
||||
s[4] ^= (uint8_t)(k[1] >> 24);
|
||||
s[5] ^= (uint8_t)(k[1] >> 16);
|
||||
s[6] ^= (uint8_t)(k[1] >> 8);
|
||||
s[7] ^= (uint8_t)(k[1]);
|
||||
s[8] ^= (uint8_t)(k[2] >> 24);
|
||||
s[9] ^= (uint8_t)(k[2] >> 16);
|
||||
s[10] ^= (uint8_t)(k[2] >> 8);
|
||||
s[11] ^= (uint8_t)(k[2]);
|
||||
s[12] ^= (uint8_t)(k[3] >> 24);
|
||||
s[13] ^= (uint8_t)(k[3] >> 16);
|
||||
s[14] ^= (uint8_t)(k[3] >> 8);
|
||||
s[15] ^= (uint8_t)(k[3]);
|
||||
}
|
||||
|
||||
static inline void sub_bytes(uint8_t *s)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for(i = 0; i < (Nb * Nk); ++i) {
|
||||
s[i] = sbox[s[i]];
|
||||
}
|
||||
}
|
||||
|
||||
#define triple(a)(_double_byte(a)^(a))
|
||||
|
||||
static inline void mult_row_column(uint8_t *out, const uint8_t *in)
|
||||
{
|
||||
out[0] = _double_byte(in[0]) ^ triple(in[1]) ^ in[2] ^ in[3];
|
||||
out[1] = in[0] ^ _double_byte(in[1]) ^ triple(in[2]) ^ in[3];
|
||||
out[2] = in[0] ^ in[1] ^ _double_byte(in[2]) ^ triple(in[3]);
|
||||
out[3] = triple(in[0]) ^ in[1] ^ in[2] ^ _double_byte(in[3]);
|
||||
}
|
||||
|
||||
static inline void mix_columns(uint8_t *s)
|
||||
{
|
||||
uint8_t t[Nb * Nk];
|
||||
mult_row_column(t, s);
|
||||
mult_row_column(&t[Nb], s + Nb);
|
||||
mult_row_column(&t[2 * Nb], s + (2 * Nb));
|
||||
mult_row_column(&t[3 * Nb], s + (3 * Nb));
|
||||
(void) _copy(s, sizeof(t), t, sizeof(t));
|
||||
}
|
||||
|
||||
/*
|
||||
* This shift_rows also implements the matrix flip required for mix_columns, but
|
||||
* performs it here to reduce the number of memory operations.
|
||||
*/
|
||||
static inline void shift_rows(uint8_t *s)
|
||||
{
|
||||
uint8_t t[Nb * Nk];
|
||||
t[0] = s[0];
|
||||
t[1] = s[5];
|
||||
t[2] = s[10];
|
||||
t[3] = s[15];
|
||||
t[4] = s[4];
|
||||
t[5] = s[9];
|
||||
t[6] = s[14];
|
||||
t[7] = s[3];
|
||||
t[8] = s[8];
|
||||
t[9] = s[13];
|
||||
t[10] = s[2];
|
||||
t[11] = s[7];
|
||||
t[12] = s[12];
|
||||
t[13] = s[1];
|
||||
t[14] = s[6];
|
||||
t[15] = s[11];
|
||||
(void) _copy(s, sizeof(t), t, sizeof(t));
|
||||
}
|
||||
|
||||
int tc_aes_encrypt(uint8_t *out, const uint8_t *in, const TCAesKeySched_t s)
|
||||
{
|
||||
uint8_t state[Nk * Nb];
|
||||
unsigned int i;
|
||||
|
||||
if(out == (uint8_t *) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
} else if(in == (const uint8_t *) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
} else if(s == (TCAesKeySched_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
(void)_copy(state, sizeof(state), in, sizeof(state));
|
||||
add_round_key(state, s->words);
|
||||
|
||||
for(i = 0; i < (Nr - 1); ++i) {
|
||||
sub_bytes(state);
|
||||
shift_rows(state);
|
||||
mix_columns(state);
|
||||
add_round_key(state, s->words + Nb * (i + 1));
|
||||
}
|
||||
|
||||
sub_bytes(state);
|
||||
shift_rows(state);
|
||||
add_round_key(state, s->words + Nb * (i + 1));
|
||||
(void)_copy(out, sizeof(state), state, sizeof(state));
|
||||
/* zeroing out the state buffer */
|
||||
_set(state, TC_ZERO_BYTE, sizeof(state));
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
/* utils.c - TinyCrypt platform-dependent run-time operations */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/utils.h>
|
||||
#include <tinycrypt/constants.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define MASK_TWENTY_SEVEN 0x1b
|
||||
|
||||
unsigned int _copy(uint8_t *to, unsigned int to_len,
|
||||
const uint8_t *from, unsigned int from_len)
|
||||
{
|
||||
if(from_len <= to_len) {
|
||||
(void)memcpy(to, from, from_len);
|
||||
return from_len;
|
||||
} else {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
void _set(void *to, uint8_t val, unsigned int len)
|
||||
{
|
||||
(void)memset(to, val, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Doubles the value of a byte for values up to 127.
|
||||
*/
|
||||
uint8_t _double_byte(uint8_t a)
|
||||
{
|
||||
return ((a << 1) ^ ((a >> 7) * MASK_TWENTY_SEVEN));
|
||||
}
|
||||
|
||||
int _compare(const uint8_t *a, const uint8_t *b, size_t size)
|
||||
{
|
||||
const uint8_t *tempa = a;
|
||||
const uint8_t *tempb = b;
|
||||
uint8_t result = 0;
|
||||
|
||||
for(unsigned int i = 0; i < size; i++) {
|
||||
result |= tempa[i] ^ tempb[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
/* cbc_mode.c - TinyCrypt implementation of CBC mode encryption & decryption */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/cbc_mode.h>
|
||||
#include <tinycrypt/constants.h>
|
||||
#include <tinycrypt/utils.h>
|
||||
|
||||
int tc_cbc_mode_encrypt(uint8_t *out, unsigned int outlen, const uint8_t *in,
|
||||
unsigned int inlen, const uint8_t *iv,
|
||||
const TCAesKeySched_t sched)
|
||||
{
|
||||
uint8_t buffer[TC_AES_BLOCK_SIZE];
|
||||
unsigned int n, m;
|
||||
|
||||
/* input sanity check: */
|
||||
if(out == (uint8_t *) 0 ||
|
||||
in == (const uint8_t *) 0 ||
|
||||
sched == (TCAesKeySched_t) 0 ||
|
||||
inlen == 0 ||
|
||||
outlen == 0 ||
|
||||
(inlen % TC_AES_BLOCK_SIZE) != 0 ||
|
||||
(outlen % TC_AES_BLOCK_SIZE) != 0 ||
|
||||
outlen != inlen + TC_AES_BLOCK_SIZE) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
/* copy iv to the buffer */
|
||||
(void)_copy(buffer, TC_AES_BLOCK_SIZE, iv, TC_AES_BLOCK_SIZE);
|
||||
/* copy iv to the output buffer */
|
||||
(void)_copy(out, TC_AES_BLOCK_SIZE, iv, TC_AES_BLOCK_SIZE);
|
||||
out += TC_AES_BLOCK_SIZE;
|
||||
|
||||
for(n = m = 0; n < inlen; ++n) {
|
||||
buffer[m++] ^= *in++;
|
||||
|
||||
if(m == TC_AES_BLOCK_SIZE) {
|
||||
(void)tc_aes_encrypt(buffer, buffer, sched);
|
||||
(void)_copy(out, TC_AES_BLOCK_SIZE,
|
||||
buffer, TC_AES_BLOCK_SIZE);
|
||||
out += TC_AES_BLOCK_SIZE;
|
||||
m = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_cbc_mode_decrypt(uint8_t *out, unsigned int outlen, const uint8_t *in,
|
||||
unsigned int inlen, const uint8_t *iv,
|
||||
const TCAesKeySched_t sched)
|
||||
{
|
||||
uint8_t buffer[TC_AES_BLOCK_SIZE];
|
||||
const uint8_t *p;
|
||||
unsigned int n, m;
|
||||
|
||||
/* sanity check the inputs */
|
||||
if(out == (uint8_t *) 0 ||
|
||||
in == (const uint8_t *) 0 ||
|
||||
sched == (TCAesKeySched_t) 0 ||
|
||||
inlen == 0 ||
|
||||
outlen == 0 ||
|
||||
(inlen % TC_AES_BLOCK_SIZE) != 0 ||
|
||||
(outlen % TC_AES_BLOCK_SIZE) != 0 ||
|
||||
outlen != inlen - TC_AES_BLOCK_SIZE) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Note that in == iv + ciphertext, i.e. the iv and the ciphertext are
|
||||
* contiguous. This allows for a very efficient decryption algorithm
|
||||
* that would not otherwise be possible.
|
||||
*/
|
||||
p = iv;
|
||||
|
||||
for(n = m = 0; n < inlen; ++n) {
|
||||
if((n % TC_AES_BLOCK_SIZE) == 0) {
|
||||
(void)tc_aes_decrypt(buffer, in, sched);
|
||||
in += TC_AES_BLOCK_SIZE;
|
||||
m = 0;
|
||||
}
|
||||
|
||||
*out++ = buffer[m++] ^ *p++;
|
||||
}
|
||||
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
+262
@@ -0,0 +1,262 @@
|
||||
/* ccm_mode.c - TinyCrypt implementation of CCM mode */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/ccm_mode.h>
|
||||
#include <tinycrypt/constants.h>
|
||||
#include <tinycrypt/utils.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int tc_ccm_config(TCCcmMode_t c, TCAesKeySched_t sched, uint8_t *nonce,
|
||||
unsigned int nlen, unsigned int mlen)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(c == (TCCcmMode_t) 0 ||
|
||||
sched == (TCAesKeySched_t) 0 ||
|
||||
nonce == (uint8_t *) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
} else if(nlen != 13) {
|
||||
return TC_CRYPTO_FAIL; /* The allowed nonce size is: 13. See documentation.*/
|
||||
} else if((mlen < 4) || (mlen > 16) || (mlen & 1)) {
|
||||
return TC_CRYPTO_FAIL; /* The allowed mac sizes are: 4, 6, 8, 10, 12, 14, 16.*/
|
||||
}
|
||||
|
||||
c->mlen = mlen;
|
||||
c->sched = sched;
|
||||
c->nonce = nonce;
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Variation of CBC-MAC mode used in CCM.
|
||||
*/
|
||||
static void ccm_cbc_mac(uint8_t *T, const uint8_t *data, unsigned int dlen,
|
||||
unsigned int flag, TCAesKeySched_t sched)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
if(flag > 0) {
|
||||
T[0] ^= (uint8_t)(dlen >> 8);
|
||||
T[1] ^= (uint8_t)(dlen);
|
||||
dlen += 2;
|
||||
i = 2;
|
||||
} else {
|
||||
i = 0;
|
||||
}
|
||||
|
||||
while(i < dlen) {
|
||||
T[i++ % (Nb * Nk)] ^= *data++;
|
||||
|
||||
if(((i % (Nb * Nk)) == 0) || dlen == i) {
|
||||
(void) tc_aes_encrypt(T, T, sched);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Variation of CTR mode used in CCM.
|
||||
* The CTR mode used by CCM is slightly different than the conventional CTR
|
||||
* mode (the counter is increased before encryption, instead of after
|
||||
* encryption). Besides, it is assumed that the counter is stored in the last
|
||||
* 2 bytes of the nonce.
|
||||
*/
|
||||
static int ccm_ctr_mode(uint8_t *out, unsigned int outlen, const uint8_t *in,
|
||||
unsigned int inlen, uint8_t *ctr, const TCAesKeySched_t sched)
|
||||
{
|
||||
uint8_t buffer[TC_AES_BLOCK_SIZE];
|
||||
uint8_t nonce[TC_AES_BLOCK_SIZE];
|
||||
uint16_t block_num;
|
||||
unsigned int i;
|
||||
|
||||
/* input sanity check: */
|
||||
if(out == (uint8_t *) 0 ||
|
||||
in == (uint8_t *) 0 ||
|
||||
ctr == (uint8_t *) 0 ||
|
||||
sched == (TCAesKeySched_t) 0 ||
|
||||
inlen == 0 ||
|
||||
outlen == 0 ||
|
||||
outlen != inlen) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
/* copy the counter to the nonce */
|
||||
(void) _copy(nonce, sizeof(nonce), ctr, sizeof(nonce));
|
||||
/* select the last 2 bytes of the nonce to be incremented */
|
||||
block_num = (uint16_t)((nonce[14] << 8) | (nonce[15]));
|
||||
|
||||
for(i = 0; i < inlen; ++i) {
|
||||
if((i % (TC_AES_BLOCK_SIZE)) == 0) {
|
||||
block_num++;
|
||||
nonce[14] = (uint8_t)(block_num >> 8);
|
||||
nonce[15] = (uint8_t)(block_num);
|
||||
|
||||
if(!tc_aes_encrypt(buffer, nonce, sched)) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
/* update the output */
|
||||
*out++ = buffer[i % (TC_AES_BLOCK_SIZE)] ^ *in++;
|
||||
}
|
||||
|
||||
/* update the counter */
|
||||
ctr[14] = nonce[14];
|
||||
ctr[15] = nonce[15];
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_ccm_generation_encryption(uint8_t *out, unsigned int olen,
|
||||
const uint8_t *associated_data,
|
||||
unsigned int alen, const uint8_t *payload,
|
||||
unsigned int plen, TCCcmMode_t c)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if((out == (uint8_t *) 0) ||
|
||||
(c == (TCCcmMode_t) 0) ||
|
||||
((plen > 0) && (payload == (uint8_t *) 0)) ||
|
||||
((alen > 0) && (associated_data == (uint8_t *) 0)) ||
|
||||
(alen >= TC_CCM_AAD_MAX_BYTES) || /* associated data size unsupported */
|
||||
(plen >= TC_CCM_PAYLOAD_MAX_BYTES) || /* payload size unsupported */
|
||||
(olen < (plen + c->mlen))) { /* invalid output buffer size */
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
uint8_t b[Nb * Nk];
|
||||
uint8_t tag[Nb * Nk];
|
||||
unsigned int i;
|
||||
/* GENERATING THE AUTHENTICATION TAG: */
|
||||
/* formatting the sequence b for authentication: */
|
||||
b[0] = ((alen > 0) ? 0x40 : 0) | (((c->mlen - 2) / 2 << 3)) | (1);
|
||||
|
||||
for(i = 1; i <= 13; ++i) {
|
||||
b[i] = c->nonce[i - 1];
|
||||
}
|
||||
|
||||
b[14] = (uint8_t)(plen >> 8);
|
||||
b[15] = (uint8_t)(plen);
|
||||
/* computing the authentication tag using cbc-mac: */
|
||||
(void) tc_aes_encrypt(tag, b, c->sched);
|
||||
|
||||
if(alen > 0) {
|
||||
ccm_cbc_mac(tag, associated_data, alen, 1, c->sched);
|
||||
}
|
||||
|
||||
if(plen > 0) {
|
||||
ccm_cbc_mac(tag, payload, plen, 0, c->sched);
|
||||
}
|
||||
|
||||
/* ENCRYPTION: */
|
||||
/* formatting the sequence b for encryption: */
|
||||
b[0] = 1; /* q - 1 = 2 - 1 = 1 */
|
||||
b[14] = b[15] = TC_ZERO_BYTE;
|
||||
/* encrypting payload using ctr mode: */
|
||||
ccm_ctr_mode(out, plen, payload, plen, b, c->sched);
|
||||
b[14] = b[15] = TC_ZERO_BYTE; /* restoring initial counter for ctr_mode (0):*/
|
||||
/* encrypting b and adding the tag to the output: */
|
||||
(void) tc_aes_encrypt(b, b, c->sched);
|
||||
out += plen;
|
||||
|
||||
for(i = 0; i < c->mlen; ++i) {
|
||||
*out++ = tag[i] ^ b[i];
|
||||
}
|
||||
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_ccm_decryption_verification(uint8_t *out, unsigned int olen,
|
||||
const uint8_t *associated_data,
|
||||
unsigned int alen, const uint8_t *payload,
|
||||
unsigned int plen, TCCcmMode_t c)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if((out == (uint8_t *) 0) ||
|
||||
(c == (TCCcmMode_t) 0) ||
|
||||
((plen > 0) && (payload == (uint8_t *) 0)) ||
|
||||
((alen > 0) && (associated_data == (uint8_t *) 0)) ||
|
||||
(alen >= TC_CCM_AAD_MAX_BYTES) || /* associated data size unsupported */
|
||||
(plen >= TC_CCM_PAYLOAD_MAX_BYTES) || /* payload size unsupported */
|
||||
(olen < plen - c->mlen)) { /* invalid output buffer size */
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
uint8_t b[Nb * Nk];
|
||||
uint8_t tag[Nb * Nk];
|
||||
unsigned int i;
|
||||
/* DECRYPTION: */
|
||||
/* formatting the sequence b for decryption: */
|
||||
b[0] = 1; /* q - 1 = 2 - 1 = 1 */
|
||||
|
||||
for(i = 1; i < 14; ++i) {
|
||||
b[i] = c->nonce[i - 1];
|
||||
}
|
||||
|
||||
b[14] = b[15] = TC_ZERO_BYTE; /* initial counter value is 0 */
|
||||
/* decrypting payload using ctr mode: */
|
||||
ccm_ctr_mode(out, plen - c->mlen, payload, plen - c->mlen, b, c->sched);
|
||||
b[14] = b[15] = TC_ZERO_BYTE; /* restoring initial counter value (0) */
|
||||
/* encrypting b and restoring the tag from input: */
|
||||
(void) tc_aes_encrypt(b, b, c->sched);
|
||||
|
||||
for(i = 0; i < c->mlen; ++i) {
|
||||
tag[i] = *(payload + plen - c->mlen + i) ^ b[i];
|
||||
}
|
||||
|
||||
/* VERIFYING THE AUTHENTICATION TAG: */
|
||||
/* formatting the sequence b for authentication: */
|
||||
b[0] = ((alen > 0) ? 0x40 : 0) | (((c->mlen - 2) / 2 << 3)) | (1);
|
||||
|
||||
for(i = 1; i < 14; ++i) {
|
||||
b[i] = c->nonce[i - 1];
|
||||
}
|
||||
|
||||
b[14] = (uint8_t)((plen - c->mlen) >> 8);
|
||||
b[15] = (uint8_t)(plen - c->mlen);
|
||||
/* computing the authentication tag using cbc-mac: */
|
||||
(void) tc_aes_encrypt(b, b, c->sched);
|
||||
|
||||
if(alen > 0) {
|
||||
ccm_cbc_mac(b, associated_data, alen, 1, c->sched);
|
||||
}
|
||||
|
||||
if(plen > 0) {
|
||||
ccm_cbc_mac(b, out, plen - c->mlen, 0, c->sched);
|
||||
}
|
||||
|
||||
/* comparing the received tag and the computed one: */
|
||||
if(_compare(b, tag, c->mlen) == 0) {
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
} else {
|
||||
/* erase the decrypted buffer in case of mac validation failure: */
|
||||
_set(out, 0, plen - c->mlen);
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
}
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
/* cmac_mode.c - TinyCrypt CMAC mode implementation */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/aes.h>
|
||||
#include <tinycrypt/cmac_mode.h>
|
||||
#include <tinycrypt/constants.h>
|
||||
#include <tinycrypt/utils.h>
|
||||
|
||||
/* max number of calls until change the key (2^48).*/
|
||||
const static uint64_t MAX_CALLS = ((uint64_t)1 << 48);
|
||||
|
||||
/*
|
||||
* gf_wrap -- In our implementation, GF(2^128) is represented as a 16 byte
|
||||
* array with byte 0 the most significant and byte 15 the least significant.
|
||||
* High bit carry reduction is based on the primitive polynomial
|
||||
*
|
||||
* X^128 + X^7 + X^2 + X + 1,
|
||||
*
|
||||
* which leads to the reduction formula X^128 = X^7 + X^2 + X + 1. Indeed,
|
||||
* since 0 = (X^128 + X^7 + X^2 + 1) mod (X^128 + X^7 + X^2 + X + 1) and since
|
||||
* addition of polynomials with coefficients in Z/Z(2) is just XOR, we can
|
||||
* add X^128 to both sides to get
|
||||
*
|
||||
* X^128 = (X^7 + X^2 + X + 1) mod (X^128 + X^7 + X^2 + X + 1)
|
||||
*
|
||||
* and the coefficients of the polynomial on the right hand side form the
|
||||
* string 1000 0111 = 0x87, which is the value of gf_wrap.
|
||||
*
|
||||
* This gets used in the following way. Doubling in GF(2^128) is just a left
|
||||
* shift by 1 bit, except when the most significant bit is 1. In the latter
|
||||
* case, the relation X^128 = X^7 + X^2 + X + 1 says that the high order bit
|
||||
* that overflows beyond 128 bits can be replaced by addition of
|
||||
* X^7 + X^2 + X + 1 <--> 0x87 to the low order 128 bits. Since addition
|
||||
* in GF(2^128) is represented by XOR, we therefore only have to XOR 0x87
|
||||
* into the low order byte after a left shift when the starting high order
|
||||
* bit is 1.
|
||||
*/
|
||||
const unsigned char gf_wrap = 0x87;
|
||||
|
||||
/*
|
||||
* assumes: out != NULL and points to a GF(2^n) value to receive the
|
||||
* doubled value;
|
||||
* in != NULL and points to a 16 byte GF(2^n) value
|
||||
* to double;
|
||||
* the in and out buffers do not overlap.
|
||||
* effects: doubles the GF(2^n) value pointed to by "in" and places
|
||||
* the result in the GF(2^n) value pointed to by "out."
|
||||
*/
|
||||
void gf_double(uint8_t *out, uint8_t *in)
|
||||
{
|
||||
/* start with low order byte */
|
||||
uint8_t *x = in + (TC_AES_BLOCK_SIZE - 1);
|
||||
/* if msb == 1, we need to add the gf_wrap value, otherwise add 0 */
|
||||
uint8_t carry = (in[0] >> 7) ? gf_wrap : 0;
|
||||
out += (TC_AES_BLOCK_SIZE - 1);
|
||||
|
||||
for(;;) {
|
||||
*out-- = (*x << 1) ^ carry;
|
||||
|
||||
if(x == in) {
|
||||
break;
|
||||
}
|
||||
|
||||
carry = *x-- >> 7;
|
||||
}
|
||||
}
|
||||
|
||||
int tc_cmac_setup(TCCmacState_t s, const uint8_t *key, TCAesKeySched_t sched)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(s == (TCCmacState_t) 0 ||
|
||||
key == (const uint8_t *) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
/* put s into a known state */
|
||||
_set(s, 0, sizeof(*s));
|
||||
s->sched = sched;
|
||||
/* configure the encryption key used by the underlying block cipher */
|
||||
tc_aes128_set_encrypt_key(s->sched, key);
|
||||
/* compute s->K1 and s->K2 from s->iv using s->keyid */
|
||||
_set(s->iv, 0, TC_AES_BLOCK_SIZE);
|
||||
tc_aes_encrypt(s->iv, s->iv, s->sched);
|
||||
gf_double(s->K1, s->iv);
|
||||
gf_double(s->K2, s->K1);
|
||||
/* reset s->iv to 0 in case someone wants to compute now */
|
||||
tc_cmac_init(s);
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_cmac_erase(TCCmacState_t s)
|
||||
{
|
||||
if(s == (TCCmacState_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
/* destroy the current state */
|
||||
_set(s, 0, sizeof(*s));
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_cmac_init(TCCmacState_t s)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(s == (TCCmacState_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
/* CMAC starts with an all zero initialization vector */
|
||||
_set(s->iv, 0, TC_AES_BLOCK_SIZE);
|
||||
/* and the leftover buffer is empty */
|
||||
_set(s->leftover, 0, TC_AES_BLOCK_SIZE);
|
||||
s->leftover_offset = 0;
|
||||
/* Set countdown to max number of calls allowed before re-keying: */
|
||||
s->countdown = MAX_CALLS;
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_cmac_update(TCCmacState_t s, const uint8_t *data, size_t data_length)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
/* input sanity check: */
|
||||
if(s == (TCCmacState_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
if(data_length == 0) {
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
if(data == (const uint8_t *) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
if(s->countdown == 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
s->countdown--;
|
||||
|
||||
if(s->leftover_offset > 0) {
|
||||
/* last data added to s didn't end on a TC_AES_BLOCK_SIZE byte boundary */
|
||||
size_t remaining_space = TC_AES_BLOCK_SIZE - s->leftover_offset;
|
||||
|
||||
if(data_length < remaining_space) {
|
||||
/* still not enough data to encrypt this time either */
|
||||
_copy(&s->leftover[s->leftover_offset], data_length, data, data_length);
|
||||
s->leftover_offset += data_length;
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
/* leftover block is now full; encrypt it first */
|
||||
_copy(&s->leftover[s->leftover_offset],
|
||||
remaining_space,
|
||||
data,
|
||||
remaining_space);
|
||||
data_length -= remaining_space;
|
||||
data += remaining_space;
|
||||
s->leftover_offset = 0;
|
||||
|
||||
for(i = 0; i < TC_AES_BLOCK_SIZE; ++i) {
|
||||
s->iv[i] ^= s->leftover[i];
|
||||
}
|
||||
|
||||
tc_aes_encrypt(s->iv, s->iv, s->sched);
|
||||
}
|
||||
|
||||
/* CBC encrypt each (except the last) of the data blocks */
|
||||
while(data_length > TC_AES_BLOCK_SIZE) {
|
||||
for(i = 0; i < TC_AES_BLOCK_SIZE; ++i) {
|
||||
s->iv[i] ^= data[i];
|
||||
}
|
||||
|
||||
tc_aes_encrypt(s->iv, s->iv, s->sched);
|
||||
data += TC_AES_BLOCK_SIZE;
|
||||
data_length -= TC_AES_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
if(data_length > 0) {
|
||||
/* save leftover data for next time */
|
||||
_copy(s->leftover, data_length, data, data_length);
|
||||
s->leftover_offset = data_length;
|
||||
}
|
||||
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_cmac_final(uint8_t *tag, TCCmacState_t s)
|
||||
{
|
||||
uint8_t *k;
|
||||
unsigned int i;
|
||||
|
||||
/* input sanity check: */
|
||||
if(tag == (uint8_t *) 0 ||
|
||||
s == (TCCmacState_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
if(s->leftover_offset == TC_AES_BLOCK_SIZE) {
|
||||
/* the last message block is a full-sized block */
|
||||
k = (uint8_t *) s->K1;
|
||||
} else {
|
||||
/* the final message block is not a full-sized block */
|
||||
size_t remaining = TC_AES_BLOCK_SIZE - s->leftover_offset;
|
||||
_set(&s->leftover[s->leftover_offset], 0, remaining);
|
||||
s->leftover[s->leftover_offset] = TC_CMAC_PADDING;
|
||||
k = (uint8_t *) s->K2;
|
||||
}
|
||||
|
||||
for(i = 0; i < TC_AES_BLOCK_SIZE; ++i) {
|
||||
s->iv[i] ^= s->leftover[i] ^ k[i];
|
||||
}
|
||||
|
||||
tc_aes_encrypt(tag, s->iv, s->sched);
|
||||
/* erasing state: */
|
||||
tc_cmac_erase(s);
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
/* ctr_mode.c - TinyCrypt CTR mode implementation */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/constants.h>
|
||||
#include <tinycrypt/ctr_mode.h>
|
||||
#include <tinycrypt/utils.h>
|
||||
|
||||
int tc_ctr_mode(uint8_t *out, unsigned int outlen, const uint8_t *in,
|
||||
unsigned int inlen, uint8_t *ctr, const TCAesKeySched_t sched)
|
||||
{
|
||||
uint8_t buffer[TC_AES_BLOCK_SIZE];
|
||||
uint8_t nonce[TC_AES_BLOCK_SIZE];
|
||||
unsigned int block_num;
|
||||
unsigned int i;
|
||||
|
||||
/* input sanity check: */
|
||||
if(out == (uint8_t *) 0 ||
|
||||
in == (uint8_t *) 0 ||
|
||||
ctr == (uint8_t *) 0 ||
|
||||
sched == (TCAesKeySched_t) 0 ||
|
||||
inlen == 0 ||
|
||||
outlen == 0 ||
|
||||
outlen != inlen) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
/* copy the ctr to the nonce */
|
||||
(void)_copy(nonce, sizeof(nonce), ctr, sizeof(nonce));
|
||||
/* select the last 4 bytes of the nonce to be incremented */
|
||||
block_num = (nonce[12] << 24) | (nonce[13] << 16) |
|
||||
(nonce[14] << 8) | (nonce[15]);
|
||||
|
||||
for(i = 0; i < inlen; ++i) {
|
||||
if((i % (TC_AES_BLOCK_SIZE)) == 0) {
|
||||
/* encrypt data using the current nonce */
|
||||
if(tc_aes_encrypt(buffer, nonce, sched)) {
|
||||
block_num++;
|
||||
nonce[12] = (uint8_t)(block_num >> 24);
|
||||
nonce[13] = (uint8_t)(block_num >> 16);
|
||||
nonce[14] = (uint8_t)(block_num >> 8);
|
||||
nonce[15] = (uint8_t)(block_num);
|
||||
} else {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
/* update the output */
|
||||
*out++ = buffer[i % (TC_AES_BLOCK_SIZE)] ^ *in++;
|
||||
}
|
||||
|
||||
/* update the counter */
|
||||
ctr[12] = nonce[12];
|
||||
ctr[13] = nonce[13];
|
||||
ctr[14] = nonce[14];
|
||||
ctr[15] = nonce[15];
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
+276
@@ -0,0 +1,276 @@
|
||||
/* ctr_prng.c - TinyCrypt implementation of CTR-PRNG */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016, Chris Morrison
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/ctr_prng.h>
|
||||
#include <tinycrypt/utils.h>
|
||||
#include <tinycrypt/constants.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* This PRNG is based on the CTR_DRBG described in Recommendation for Random
|
||||
* Number Generation Using Deterministic Random Bit Generators,
|
||||
* NIST SP 800-90A Rev. 1.
|
||||
*
|
||||
* Annotations to particular steps (e.g. 10.2.1.2 Step 1) refer to the steps
|
||||
* described in that document.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Array incrementer
|
||||
* Treats the supplied array as one contiguous number (MSB in arr[0]), and
|
||||
* increments it by one
|
||||
* @return none
|
||||
* @param arr IN/OUT -- array to be incremented
|
||||
* @param len IN -- size of arr in bytes
|
||||
*/
|
||||
static void arrInc(uint8_t arr[], unsigned int len)
|
||||
{
|
||||
if(0 != arr) {
|
||||
for(unsigned int i = len; i > 0U; i--) {
|
||||
if(++arr[i - 1] != 0U) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CTR PRNG update
|
||||
* Updates the internal state of supplied the CTR PRNG context
|
||||
* increments it by one
|
||||
* @return none
|
||||
* @note Assumes: providedData is (TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE) bytes long
|
||||
* @param ctx IN/OUT -- CTR PRNG state
|
||||
* @param providedData IN -- data used when updating the internal state
|
||||
*/
|
||||
static void tc_ctr_prng_update(TCCtrPrng_t *const ctx, uint8_t const *const providedData)
|
||||
{
|
||||
if(0 != ctx) {
|
||||
/* 10.2.1.2 step 1 */
|
||||
uint8_t temp[TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE];
|
||||
unsigned int len = 0U;
|
||||
|
||||
/* 10.2.1.2 step 2 */
|
||||
while(len < sizeof temp) {
|
||||
unsigned int blocklen = sizeof(temp) - len;
|
||||
uint8_t output_block[TC_AES_BLOCK_SIZE];
|
||||
/* 10.2.1.2 step 2.1 */
|
||||
arrInc(ctx->V, sizeof ctx->V);
|
||||
|
||||
/* 10.2.1.2 step 2.2 */
|
||||
if(blocklen > TC_AES_BLOCK_SIZE) {
|
||||
blocklen = TC_AES_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
(void)tc_aes_encrypt(output_block, ctx->V, &ctx->key);
|
||||
/* 10.2.1.2 step 2.3/step 3 */
|
||||
memcpy(&(temp[len]), output_block, blocklen);
|
||||
len += blocklen;
|
||||
}
|
||||
|
||||
/* 10.2.1.2 step 4 */
|
||||
if(0 != providedData) {
|
||||
unsigned int i;
|
||||
|
||||
for(i = 0U; i < sizeof temp; i++) {
|
||||
temp[i] ^= providedData[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* 10.2.1.2 step 5 */
|
||||
(void)tc_aes128_set_encrypt_key(&ctx->key, temp);
|
||||
/* 10.2.1.2 step 6 */
|
||||
memcpy(ctx->V, &(temp[TC_AES_KEY_SIZE]), TC_AES_BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
int tc_ctr_prng_init(TCCtrPrng_t *const ctx,
|
||||
uint8_t const *const entropy,
|
||||
unsigned int entropyLen,
|
||||
uint8_t const *const personalization,
|
||||
unsigned int pLen)
|
||||
{
|
||||
int result = TC_CRYPTO_FAIL;
|
||||
uint8_t personalization_buf[TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE] = {0U};
|
||||
uint8_t seed_material[TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE];
|
||||
|
||||
if(0 != personalization) {
|
||||
/* 10.2.1.3.1 step 1 */
|
||||
unsigned int len = pLen;
|
||||
|
||||
if(len > sizeof personalization_buf) {
|
||||
len = sizeof personalization_buf;
|
||||
}
|
||||
|
||||
/* 10.2.1.3.1 step 2 */
|
||||
memcpy(personalization_buf, personalization, len);
|
||||
}
|
||||
|
||||
if((0 != ctx) && (0 != entropy) && (entropyLen >= sizeof seed_material)) {
|
||||
/* 10.2.1.3.1 step 3 */
|
||||
memcpy(seed_material, entropy, sizeof seed_material);
|
||||
|
||||
for(unsigned int i = 0U; i < sizeof seed_material; i++) {
|
||||
seed_material[i] ^= personalization_buf[i];
|
||||
}
|
||||
|
||||
/* 10.2.1.3.1 step 4 */
|
||||
uint8_t zeroArr[TC_AES_BLOCK_SIZE] = {0U};
|
||||
(void)tc_aes128_set_encrypt_key(&ctx->key, zeroArr);
|
||||
/* 10.2.1.3.1 step 5 */
|
||||
memset(ctx->V, 0x00, sizeof ctx->V);
|
||||
/* 10.2.1.3.1 step 6 */
|
||||
tc_ctr_prng_update(ctx, seed_material);
|
||||
/* 10.2.1.3.1 step 7 */
|
||||
ctx->reseedCount = 1U;
|
||||
result = TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int tc_ctr_prng_reseed(TCCtrPrng_t *const ctx,
|
||||
uint8_t const *const entropy,
|
||||
unsigned int entropyLen,
|
||||
uint8_t const *const additional_input,
|
||||
unsigned int additionallen)
|
||||
{
|
||||
int result = TC_CRYPTO_FAIL;
|
||||
uint8_t additional_input_buf[TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE] = {0U};
|
||||
|
||||
if(0 != additional_input) {
|
||||
/* 10.2.1.4.1 step 1 */
|
||||
unsigned int len = additionallen;
|
||||
|
||||
if(len > sizeof additional_input_buf) {
|
||||
len = sizeof additional_input_buf;
|
||||
}
|
||||
|
||||
/* 10.2.1.4.1 step 2 */
|
||||
memcpy(additional_input_buf, additional_input, len);
|
||||
}
|
||||
|
||||
unsigned int seedlen = (unsigned int)TC_AES_KEY_SIZE + (unsigned int)TC_AES_BLOCK_SIZE;
|
||||
|
||||
if((0 != ctx) && (entropyLen >= seedlen)) {
|
||||
uint8_t seed_material[TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE];
|
||||
/* 10.2.1.4.1 step 3 */
|
||||
memcpy(seed_material, entropy, sizeof seed_material);
|
||||
|
||||
for(unsigned int i = 0U; i < sizeof seed_material; i++) {
|
||||
seed_material[i] ^= additional_input_buf[i];
|
||||
}
|
||||
|
||||
/* 10.2.1.4.1 step 4 */
|
||||
tc_ctr_prng_update(ctx, seed_material);
|
||||
/* 10.2.1.4.1 step 5 */
|
||||
ctx->reseedCount = 1U;
|
||||
result = TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int tc_ctr_prng_generate(TCCtrPrng_t *const ctx,
|
||||
uint8_t const *const additional_input,
|
||||
unsigned int additionallen,
|
||||
uint8_t *const out,
|
||||
unsigned int outlen)
|
||||
{
|
||||
/* 2^48 - see section 10.2.1 */
|
||||
static const uint64_t MAX_REQS_BEFORE_RESEED = 0x1000000000000ULL;
|
||||
/* 2^19 bits - see section 10.2.1 */
|
||||
static const unsigned int MAX_BYTES_PER_REQ = 65536U;
|
||||
unsigned int result = TC_CRYPTO_FAIL;
|
||||
|
||||
if((0 != ctx) && (0 != out) && (outlen < MAX_BYTES_PER_REQ)) {
|
||||
/* 10.2.1.5.1 step 1 */
|
||||
if(ctx->reseedCount > MAX_REQS_BEFORE_RESEED) {
|
||||
result = TC_CTR_PRNG_RESEED_REQ;
|
||||
} else {
|
||||
uint8_t additional_input_buf[TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE] = {0U};
|
||||
|
||||
if(0 != additional_input) {
|
||||
/* 10.2.1.5.1 step 2 */
|
||||
unsigned int len = additionallen;
|
||||
|
||||
if(len > sizeof additional_input_buf) {
|
||||
len = sizeof additional_input_buf;
|
||||
}
|
||||
|
||||
memcpy(additional_input_buf, additional_input, len);
|
||||
tc_ctr_prng_update(ctx, additional_input_buf);
|
||||
}
|
||||
|
||||
/* 10.2.1.5.1 step 3 - implicit */
|
||||
/* 10.2.1.5.1 step 4 */
|
||||
unsigned int len = 0U;
|
||||
|
||||
while(len < outlen) {
|
||||
unsigned int blocklen = outlen - len;
|
||||
uint8_t output_block[TC_AES_BLOCK_SIZE];
|
||||
/* 10.2.1.5.1 step 4.1 */
|
||||
arrInc(ctx->V, sizeof ctx->V);
|
||||
/* 10.2.1.5.1 step 4.2 */
|
||||
(void)tc_aes_encrypt(output_block, ctx->V, &ctx->key);
|
||||
|
||||
/* 10.2.1.5.1 step 4.3/step 5 */
|
||||
if(blocklen > TC_AES_BLOCK_SIZE) {
|
||||
blocklen = TC_AES_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
memcpy(&(out[len]), output_block, blocklen);
|
||||
len += blocklen;
|
||||
}
|
||||
|
||||
/* 10.2.1.5.1 step 6 */
|
||||
tc_ctr_prng_update(ctx, additional_input_buf);
|
||||
/* 10.2.1.5.1 step 7 */
|
||||
ctx->reseedCount++;
|
||||
/* 10.2.1.5.1 step 8 */
|
||||
result = TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void tc_ctr_prng_uninstantiate(TCCtrPrng_t *const ctx)
|
||||
{
|
||||
if(0 != ctx) {
|
||||
memset(ctx->key.words, 0x00, sizeof ctx->key.words);
|
||||
memset(ctx->V, 0x00, sizeof ctx->V);
|
||||
ctx->reseedCount = 0U;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
+926
@@ -0,0 +1,926 @@
|
||||
/* ecc.c - TinyCrypt implementation of common ECC functions */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2014, Kenneth MacKay
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/ecc.h>
|
||||
#include <tinycrypt/ecc_platform_specific.h>
|
||||
#include <string.h>
|
||||
|
||||
/* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform
|
||||
* has access to enough entropy in order to feed the PRNG regularly. */
|
||||
#if default_RNG_defined
|
||||
static uECC_RNG_Function g_rng_function = &default_CSPRNG;
|
||||
#else
|
||||
static uECC_RNG_Function g_rng_function = 0;
|
||||
#endif
|
||||
|
||||
void uECC_set_rng(uECC_RNG_Function rng_function)
|
||||
{
|
||||
g_rng_function = rng_function;
|
||||
}
|
||||
|
||||
uECC_RNG_Function uECC_get_rng(void)
|
||||
{
|
||||
return g_rng_function;
|
||||
}
|
||||
|
||||
int uECC_curve_private_key_size(uECC_Curve curve)
|
||||
{
|
||||
return BITS_TO_BYTES(curve->num_n_bits);
|
||||
}
|
||||
|
||||
int uECC_curve_public_key_size(uECC_Curve curve)
|
||||
{
|
||||
return 2 * curve->num_bytes;
|
||||
}
|
||||
|
||||
void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words)
|
||||
{
|
||||
wordcount_t i;
|
||||
|
||||
for(i = 0; i < num_words; ++i) {
|
||||
vli[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t bits = 0;
|
||||
wordcount_t i;
|
||||
|
||||
for(i = 0; i < num_words; ++i) {
|
||||
bits |= vli[i];
|
||||
}
|
||||
|
||||
return (bits == 0);
|
||||
}
|
||||
|
||||
uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit)
|
||||
{
|
||||
return (vli[bit >> uECC_WORD_BITS_SHIFT] &
|
||||
((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
|
||||
}
|
||||
|
||||
/* Counts the number of words in vli. */
|
||||
static wordcount_t vli_numDigits(const uECC_word_t *vli,
|
||||
const wordcount_t max_words)
|
||||
{
|
||||
wordcount_t i;
|
||||
|
||||
/* Search from the end until we find a non-zero digit. We do it in reverse
|
||||
* because we expect that most digits will be nonzero. */
|
||||
for(i = max_words - 1; i >= 0 && vli[i] == 0; --i) {
|
||||
}
|
||||
|
||||
return (i + 1);
|
||||
}
|
||||
|
||||
bitcount_t uECC_vli_numBits(const uECC_word_t *vli,
|
||||
const wordcount_t max_words)
|
||||
{
|
||||
uECC_word_t i;
|
||||
uECC_word_t digit;
|
||||
wordcount_t num_digits = vli_numDigits(vli, max_words);
|
||||
|
||||
if(num_digits == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
digit = vli[num_digits - 1];
|
||||
|
||||
for(i = 0; digit; ++i) {
|
||||
digit >>= 1;
|
||||
}
|
||||
|
||||
return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
|
||||
}
|
||||
|
||||
void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src,
|
||||
wordcount_t num_words)
|
||||
{
|
||||
wordcount_t i;
|
||||
|
||||
for(i = 0; i < num_words; ++i) {
|
||||
dest[i] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
|
||||
const uECC_word_t *right,
|
||||
wordcount_t num_words)
|
||||
{
|
||||
wordcount_t i;
|
||||
|
||||
for(i = num_words - 1; i >= 0; --i) {
|
||||
if(left[i] > right[i]) {
|
||||
return 1;
|
||||
} else if(left[i] < right[i]) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right,
|
||||
wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t diff = 0;
|
||||
wordcount_t i;
|
||||
|
||||
for(i = num_words - 1; i >= 0; --i) {
|
||||
diff |= (left[i] ^ right[i]);
|
||||
}
|
||||
|
||||
return !(diff == 0);
|
||||
}
|
||||
|
||||
uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond)
|
||||
{
|
||||
return (p_true * (cond)) | (p_false * (!cond));
|
||||
}
|
||||
|
||||
/* Computes result = left - right, returning borrow, in constant time.
|
||||
* Can modify in place. */
|
||||
uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t borrow = 0;
|
||||
wordcount_t i;
|
||||
|
||||
for(i = 0; i < num_words; ++i) {
|
||||
uECC_word_t diff = left[i] - right[i] - borrow;
|
||||
uECC_word_t val = (diff > left[i]);
|
||||
borrow = cond_set(val, borrow, (diff != left[i]));
|
||||
result[i] = diff;
|
||||
}
|
||||
|
||||
return borrow;
|
||||
}
|
||||
|
||||
/* Computes result = left + right, returning carry, in constant time.
|
||||
* Can modify in place. */
|
||||
static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t carry = 0;
|
||||
wordcount_t i;
|
||||
|
||||
for(i = 0; i < num_words; ++i) {
|
||||
uECC_word_t sum = left[i] + right[i] + carry;
|
||||
uECC_word_t val = (sum < left[i]);
|
||||
carry = cond_set(val, carry, (sum != left[i]));
|
||||
result[i] = sum;
|
||||
}
|
||||
|
||||
return carry;
|
||||
}
|
||||
|
||||
cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right,
|
||||
wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t tmp[NUM_ECC_WORDS];
|
||||
uECC_word_t neg = !!uECC_vli_sub(tmp, left, right, num_words);
|
||||
uECC_word_t equal = uECC_vli_isZero(tmp, num_words);
|
||||
return (!equal - 2 * neg);
|
||||
}
|
||||
|
||||
/* Computes vli = vli >> 1. */
|
||||
static void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t *end = vli;
|
||||
uECC_word_t carry = 0;
|
||||
vli += num_words;
|
||||
|
||||
while(vli-- > end) {
|
||||
uECC_word_t temp = *vli;
|
||||
*vli = (temp >> 1) | carry;
|
||||
carry = temp << (uECC_WORD_BITS - 1);
|
||||
}
|
||||
}
|
||||
|
||||
static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0,
|
||||
uECC_word_t *r1, uECC_word_t *r2)
|
||||
{
|
||||
uECC_dword_t p = (uECC_dword_t)a * b;
|
||||
uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
|
||||
r01 += p;
|
||||
*r2 += (r01 < p);
|
||||
*r1 = r01 >> uECC_WORD_BITS;
|
||||
*r0 = (uECC_word_t)r01;
|
||||
}
|
||||
|
||||
/* Computes result = left * right. Result must be 2 * num_words long. */
|
||||
static void uECC_vli_mult(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t r0 = 0;
|
||||
uECC_word_t r1 = 0;
|
||||
uECC_word_t r2 = 0;
|
||||
wordcount_t i, k;
|
||||
|
||||
/* Compute each digit of result in sequence, maintaining the carries. */
|
||||
for(k = 0; k < num_words; ++k) {
|
||||
for(i = 0; i <= k; ++i) {
|
||||
muladd(left[i], right[k - i], &r0, &r1, &r2);
|
||||
}
|
||||
|
||||
result[k] = r0;
|
||||
r0 = r1;
|
||||
r1 = r2;
|
||||
r2 = 0;
|
||||
}
|
||||
|
||||
for(k = num_words; k < num_words * 2 - 1; ++k) {
|
||||
for(i = (k + 1) - num_words; i < num_words; ++i) {
|
||||
muladd(left[i], right[k - i], &r0, &r1, &r2);
|
||||
}
|
||||
|
||||
result[k] = r0;
|
||||
r0 = r1;
|
||||
r1 = r2;
|
||||
r2 = 0;
|
||||
}
|
||||
|
||||
result[num_words * 2 - 1] = r0;
|
||||
}
|
||||
|
||||
void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, const uECC_word_t *mod,
|
||||
wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t carry = uECC_vli_add(result, left, right, num_words);
|
||||
|
||||
if(carry || uECC_vli_cmp_unsafe(mod, result, num_words) != 1) {
|
||||
/* result > mod (result = mod + remainder), so subtract mod to get
|
||||
* remainder. */
|
||||
uECC_vli_sub(result, result, mod, num_words);
|
||||
}
|
||||
}
|
||||
|
||||
void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, const uECC_word_t *mod,
|
||||
wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t l_borrow = uECC_vli_sub(result, left, right, num_words);
|
||||
|
||||
if(l_borrow) {
|
||||
/* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
|
||||
* we can get the correct result from result + mod (with overflow). */
|
||||
uECC_vli_add(result, result, mod, num_words);
|
||||
}
|
||||
}
|
||||
|
||||
/* Computes result = product % mod, where product is 2N words long. */
|
||||
/* Currently only designed to work for curve_p or curve_n. */
|
||||
void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
|
||||
const uECC_word_t *mod, wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t mod_multiple[2 * NUM_ECC_WORDS];
|
||||
uECC_word_t tmp[2 * NUM_ECC_WORDS];
|
||||
uECC_word_t *v[2] = {tmp, product};
|
||||
uECC_word_t index;
|
||||
/* Shift mod so its highest set bit is at the maximum position. */
|
||||
bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) -
|
||||
uECC_vli_numBits(mod, num_words);
|
||||
wordcount_t word_shift = shift / uECC_WORD_BITS;
|
||||
wordcount_t bit_shift = shift % uECC_WORD_BITS;
|
||||
uECC_word_t carry = 0;
|
||||
uECC_vli_clear(mod_multiple, word_shift);
|
||||
|
||||
if(bit_shift > 0) {
|
||||
for(index = 0; index < (uECC_word_t)num_words; ++index) {
|
||||
mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry;
|
||||
carry = mod[index] >> (uECC_WORD_BITS - bit_shift);
|
||||
}
|
||||
} else {
|
||||
uECC_vli_set(mod_multiple + word_shift, mod, num_words);
|
||||
}
|
||||
|
||||
for(index = 1; shift >= 0; --shift) {
|
||||
uECC_word_t borrow = 0;
|
||||
wordcount_t i;
|
||||
|
||||
for(i = 0; i < num_words * 2; ++i) {
|
||||
uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow;
|
||||
|
||||
if(diff != v[index][i]) {
|
||||
borrow = (diff > v[index][i]);
|
||||
}
|
||||
|
||||
v[1 - index][i] = diff;
|
||||
}
|
||||
|
||||
/* Swap the index if there was no borrow */
|
||||
index = !(index ^ borrow);
|
||||
uECC_vli_rshift1(mod_multiple, num_words);
|
||||
mod_multiple[num_words - 1] |= mod_multiple[num_words] <<
|
||||
(uECC_WORD_BITS - 1);
|
||||
uECC_vli_rshift1(mod_multiple + num_words, num_words);
|
||||
}
|
||||
|
||||
uECC_vli_set(result, v[index], num_words);
|
||||
}
|
||||
|
||||
void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, const uECC_word_t *mod,
|
||||
wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t product[2 * NUM_ECC_WORDS];
|
||||
uECC_vli_mult(product, left, right, num_words);
|
||||
uECC_vli_mmod(result, product, mod, num_words);
|
||||
}
|
||||
|
||||
void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
|
||||
const uECC_word_t *right, uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t product[2 * NUM_ECC_WORDS];
|
||||
uECC_vli_mult(product, left, right, curve->num_words);
|
||||
curve->mmod_fast(result, product);
|
||||
}
|
||||
|
||||
static void uECC_vli_modSquare_fast(uECC_word_t *result,
|
||||
const uECC_word_t *left,
|
||||
uECC_Curve curve)
|
||||
{
|
||||
uECC_vli_modMult_fast(result, left, left, curve);
|
||||
}
|
||||
|
||||
|
||||
#define EVEN(vli) (!(vli[0] & 1))
|
||||
|
||||
static void vli_modInv_update(uECC_word_t *uv,
|
||||
const uECC_word_t *mod,
|
||||
wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t carry = 0;
|
||||
|
||||
if(!EVEN(uv)) {
|
||||
carry = uECC_vli_add(uv, uv, mod, num_words);
|
||||
}
|
||||
|
||||
uECC_vli_rshift1(uv, num_words);
|
||||
|
||||
if(carry) {
|
||||
uv[num_words - 1] |= HIGH_BIT_SET;
|
||||
}
|
||||
}
|
||||
|
||||
void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
|
||||
const uECC_word_t *mod, wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
|
||||
uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS];
|
||||
cmpresult_t cmpResult;
|
||||
|
||||
if(uECC_vli_isZero(input, num_words)) {
|
||||
uECC_vli_clear(result, num_words);
|
||||
return;
|
||||
}
|
||||
|
||||
uECC_vli_set(a, input, num_words);
|
||||
uECC_vli_set(b, mod, num_words);
|
||||
uECC_vli_clear(u, num_words);
|
||||
u[0] = 1;
|
||||
uECC_vli_clear(v, num_words);
|
||||
|
||||
while((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) {
|
||||
if(EVEN(a)) {
|
||||
uECC_vli_rshift1(a, num_words);
|
||||
vli_modInv_update(u, mod, num_words);
|
||||
} else if(EVEN(b)) {
|
||||
uECC_vli_rshift1(b, num_words);
|
||||
vli_modInv_update(v, mod, num_words);
|
||||
} else if(cmpResult > 0) {
|
||||
uECC_vli_sub(a, a, b, num_words);
|
||||
uECC_vli_rshift1(a, num_words);
|
||||
|
||||
if(uECC_vli_cmp_unsafe(u, v, num_words) < 0) {
|
||||
uECC_vli_add(u, u, mod, num_words);
|
||||
}
|
||||
|
||||
uECC_vli_sub(u, u, v, num_words);
|
||||
vli_modInv_update(u, mod, num_words);
|
||||
} else {
|
||||
uECC_vli_sub(b, b, a, num_words);
|
||||
uECC_vli_rshift1(b, num_words);
|
||||
|
||||
if(uECC_vli_cmp_unsafe(v, u, num_words) < 0) {
|
||||
uECC_vli_add(v, v, mod, num_words);
|
||||
}
|
||||
|
||||
uECC_vli_sub(v, v, u, num_words);
|
||||
vli_modInv_update(v, mod, num_words);
|
||||
}
|
||||
}
|
||||
|
||||
uECC_vli_set(result, u, num_words);
|
||||
}
|
||||
|
||||
/* ------ Point operations ------ */
|
||||
|
||||
void double_jacobian_default(uECC_word_t *X1, uECC_word_t *Y1,
|
||||
uECC_word_t *Z1, uECC_Curve curve)
|
||||
{
|
||||
/* t1 = X, t2 = Y, t3 = Z */
|
||||
uECC_word_t t4[NUM_ECC_WORDS];
|
||||
uECC_word_t t5[NUM_ECC_WORDS];
|
||||
wordcount_t num_words = curve->num_words;
|
||||
|
||||
if(uECC_vli_isZero(Z1, num_words)) {
|
||||
return;
|
||||
}
|
||||
|
||||
uECC_vli_modSquare_fast(t4, Y1, curve); /* t4 = y1^2 */
|
||||
uECC_vli_modMult_fast(t5, X1, t4, curve); /* t5 = x1*y1^2 = A */
|
||||
uECC_vli_modSquare_fast(t4, t4, curve); /* t4 = y1^4 */
|
||||
uECC_vli_modMult_fast(Y1, Y1, Z1, curve); /* t2 = y1*z1 = z3 */
|
||||
uECC_vli_modSquare_fast(Z1, Z1, curve); /* t3 = z1^2 */
|
||||
uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = x1 + z1^2 */
|
||||
uECC_vli_modAdd(Z1, Z1, Z1, curve->p, num_words); /* t3 = 2*z1^2 */
|
||||
uECC_vli_modSub(Z1, X1, Z1, curve->p, num_words); /* t3 = x1 - z1^2 */
|
||||
uECC_vli_modMult_fast(X1, X1, Z1, curve); /* t1 = x1^2 - z1^4 */
|
||||
uECC_vli_modAdd(Z1, X1, X1, curve->p, num_words); /* t3 = 2*(x1^2 - z1^4) */
|
||||
uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = 3*(x1^2 - z1^4) */
|
||||
|
||||
if(uECC_vli_testBit(X1, 0)) {
|
||||
uECC_word_t l_carry = uECC_vli_add(X1, X1, curve->p, num_words);
|
||||
uECC_vli_rshift1(X1, num_words);
|
||||
X1[num_words - 1] |= l_carry << (uECC_WORD_BITS - 1);
|
||||
} else {
|
||||
uECC_vli_rshift1(X1, num_words);
|
||||
}
|
||||
|
||||
/* t1 = 3/2*(x1^2 - z1^4) = B */
|
||||
uECC_vli_modSquare_fast(Z1, X1, curve); /* t3 = B^2 */
|
||||
uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - A */
|
||||
uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - 2A = x3 */
|
||||
uECC_vli_modSub(t5, t5, Z1, curve->p, num_words); /* t5 = A - x3 */
|
||||
uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = B * (A - x3) */
|
||||
/* t4 = B * (A - x3) - y1^4 = y3: */
|
||||
uECC_vli_modSub(t4, X1, t4, curve->p, num_words);
|
||||
uECC_vli_set(X1, Z1, num_words);
|
||||
uECC_vli_set(Z1, Y1, num_words);
|
||||
uECC_vli_set(Y1, t4, num_words);
|
||||
}
|
||||
|
||||
void x_side_default(uECC_word_t *result,
|
||||
const uECC_word_t *x,
|
||||
uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */
|
||||
wordcount_t num_words = curve->num_words;
|
||||
uECC_vli_modSquare_fast(result, x, curve); /* r = x^2 */
|
||||
uECC_vli_modSub(result, result, _3, curve->p, num_words); /* r = x^2 - 3 */
|
||||
uECC_vli_modMult_fast(result, result, x, curve); /* r = x^3 - 3x */
|
||||
/* r = x^3 - 3x + b: */
|
||||
uECC_vli_modAdd(result, result, curve->b, curve->p, num_words);
|
||||
}
|
||||
|
||||
uECC_Curve uECC_secp256r1(void)
|
||||
{
|
||||
return &curve_secp256r1;
|
||||
}
|
||||
|
||||
void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int *product)
|
||||
{
|
||||
unsigned int tmp[NUM_ECC_WORDS];
|
||||
int carry;
|
||||
/* t */
|
||||
uECC_vli_set(result, product, NUM_ECC_WORDS);
|
||||
/* s1 */
|
||||
tmp[0] = tmp[1] = tmp[2] = 0;
|
||||
tmp[3] = product[11];
|
||||
tmp[4] = product[12];
|
||||
tmp[5] = product[13];
|
||||
tmp[6] = product[14];
|
||||
tmp[7] = product[15];
|
||||
carry = uECC_vli_add(tmp, tmp, tmp, NUM_ECC_WORDS);
|
||||
carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
|
||||
/* s2 */
|
||||
tmp[3] = product[12];
|
||||
tmp[4] = product[13];
|
||||
tmp[5] = product[14];
|
||||
tmp[6] = product[15];
|
||||
tmp[7] = 0;
|
||||
carry += uECC_vli_add(tmp, tmp, tmp, NUM_ECC_WORDS);
|
||||
carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
|
||||
/* s3 */
|
||||
tmp[0] = product[8];
|
||||
tmp[1] = product[9];
|
||||
tmp[2] = product[10];
|
||||
tmp[3] = tmp[4] = tmp[5] = 0;
|
||||
tmp[6] = product[14];
|
||||
tmp[7] = product[15];
|
||||
carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
|
||||
/* s4 */
|
||||
tmp[0] = product[9];
|
||||
tmp[1] = product[10];
|
||||
tmp[2] = product[11];
|
||||
tmp[3] = product[13];
|
||||
tmp[4] = product[14];
|
||||
tmp[5] = product[15];
|
||||
tmp[6] = product[13];
|
||||
tmp[7] = product[8];
|
||||
carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
|
||||
/* d1 */
|
||||
tmp[0] = product[11];
|
||||
tmp[1] = product[12];
|
||||
tmp[2] = product[13];
|
||||
tmp[3] = tmp[4] = tmp[5] = 0;
|
||||
tmp[6] = product[8];
|
||||
tmp[7] = product[10];
|
||||
carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
|
||||
/* d2 */
|
||||
tmp[0] = product[12];
|
||||
tmp[1] = product[13];
|
||||
tmp[2] = product[14];
|
||||
tmp[3] = product[15];
|
||||
tmp[4] = tmp[5] = 0;
|
||||
tmp[6] = product[9];
|
||||
tmp[7] = product[11];
|
||||
carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
|
||||
/* d3 */
|
||||
tmp[0] = product[13];
|
||||
tmp[1] = product[14];
|
||||
tmp[2] = product[15];
|
||||
tmp[3] = product[8];
|
||||
tmp[4] = product[9];
|
||||
tmp[5] = product[10];
|
||||
tmp[6] = 0;
|
||||
tmp[7] = product[12];
|
||||
carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
|
||||
/* d4 */
|
||||
tmp[0] = product[14];
|
||||
tmp[1] = product[15];
|
||||
tmp[2] = 0;
|
||||
tmp[3] = product[9];
|
||||
tmp[4] = product[10];
|
||||
tmp[5] = product[11];
|
||||
tmp[6] = 0;
|
||||
tmp[7] = product[13];
|
||||
carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
|
||||
|
||||
if(carry < 0) {
|
||||
do {
|
||||
carry += uECC_vli_add(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
|
||||
} while(carry < 0);
|
||||
} else {
|
||||
while(carry ||
|
||||
uECC_vli_cmp_unsafe(curve_secp256r1.p, result, NUM_ECC_WORDS) != 1) {
|
||||
carry -= uECC_vli_sub(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve)
|
||||
{
|
||||
return uECC_vli_isZero(point, curve->num_words * 2);
|
||||
}
|
||||
|
||||
void apply_z(uECC_word_t *X1, uECC_word_t *Y1, const uECC_word_t *const Z,
|
||||
uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t t1[NUM_ECC_WORDS];
|
||||
uECC_vli_modSquare_fast(t1, Z, curve); /* z^2 */
|
||||
uECC_vli_modMult_fast(X1, X1, t1, curve); /* x1 * z^2 */
|
||||
uECC_vli_modMult_fast(t1, t1, Z, curve); /* z^3 */
|
||||
uECC_vli_modMult_fast(Y1, Y1, t1, curve); /* y1 * z^3 */
|
||||
}
|
||||
|
||||
/* P = (x1, y1) => 2P, (x2, y2) => P' */
|
||||
static void XYcZ_initial_double(uECC_word_t *X1, uECC_word_t *Y1,
|
||||
uECC_word_t *X2, uECC_word_t *Y2,
|
||||
const uECC_word_t *const initial_Z,
|
||||
uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t z[NUM_ECC_WORDS];
|
||||
wordcount_t num_words = curve->num_words;
|
||||
|
||||
if(initial_Z) {
|
||||
uECC_vli_set(z, initial_Z, num_words);
|
||||
} else {
|
||||
uECC_vli_clear(z, num_words);
|
||||
z[0] = 1;
|
||||
}
|
||||
|
||||
uECC_vli_set(X2, X1, num_words);
|
||||
uECC_vli_set(Y2, Y1, num_words);
|
||||
apply_z(X1, Y1, z, curve);
|
||||
curve->double_jacobian(X1, Y1, z, curve);
|
||||
apply_z(X2, Y2, z, curve);
|
||||
}
|
||||
|
||||
void XYcZ_add(uECC_word_t *X1, uECC_word_t *Y1,
|
||||
uECC_word_t *X2, uECC_word_t *Y2,
|
||||
uECC_Curve curve)
|
||||
{
|
||||
/* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
|
||||
uECC_word_t t5[NUM_ECC_WORDS];
|
||||
wordcount_t num_words = curve->num_words;
|
||||
uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
|
||||
uECC_vli_modSquare_fast(t5, t5, curve); /* t5 = (x2 - x1)^2 = A */
|
||||
uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = x1*A = B */
|
||||
uECC_vli_modMult_fast(X2, X2, t5, curve); /* t3 = x2*A = C */
|
||||
uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
|
||||
uECC_vli_modSquare_fast(t5, Y2, curve); /* t5 = (y2 - y1)^2 = D */
|
||||
uECC_vli_modSub(t5, t5, X1, curve->p, num_words); /* t5 = D - B */
|
||||
uECC_vli_modSub(t5, t5, X2, curve->p, num_words); /* t5 = D - B - C = x3 */
|
||||
uECC_vli_modSub(X2, X2, X1, curve->p, num_words); /* t3 = C - B */
|
||||
uECC_vli_modMult_fast(Y1, Y1, X2, curve); /* t2 = y1*(C - B) */
|
||||
uECC_vli_modSub(X2, X1, t5, curve->p, num_words); /* t3 = B - x3 */
|
||||
uECC_vli_modMult_fast(Y2, Y2, X2, curve); /* t4 = (y2 - y1)*(B - x3) */
|
||||
uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y3 */
|
||||
uECC_vli_set(X2, t5, num_words);
|
||||
}
|
||||
|
||||
/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
|
||||
Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
|
||||
or P => P - Q, Q => P + Q
|
||||
*/
|
||||
static void XYcZ_addC(uECC_word_t *X1, uECC_word_t *Y1,
|
||||
uECC_word_t *X2, uECC_word_t *Y2,
|
||||
uECC_Curve curve)
|
||||
{
|
||||
/* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
|
||||
uECC_word_t t5[NUM_ECC_WORDS];
|
||||
uECC_word_t t6[NUM_ECC_WORDS];
|
||||
uECC_word_t t7[NUM_ECC_WORDS];
|
||||
wordcount_t num_words = curve->num_words;
|
||||
uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
|
||||
uECC_vli_modSquare_fast(t5, t5, curve); /* t5 = (x2 - x1)^2 = A */
|
||||
uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = x1*A = B */
|
||||
uECC_vli_modMult_fast(X2, X2, t5, curve); /* t3 = x2*A = C */
|
||||
uECC_vli_modAdd(t5, Y2, Y1, curve->p, num_words); /* t5 = y2 + y1 */
|
||||
uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
|
||||
uECC_vli_modSub(t6, X2, X1, curve->p, num_words); /* t6 = C - B */
|
||||
uECC_vli_modMult_fast(Y1, Y1, t6, curve); /* t2 = y1 * (C - B) = E */
|
||||
uECC_vli_modAdd(t6, X1, X2, curve->p, num_words); /* t6 = B + C */
|
||||
uECC_vli_modSquare_fast(X2, Y2, curve); /* t3 = (y2 - y1)^2 = D */
|
||||
uECC_vli_modSub(X2, X2, t6, curve->p, num_words); /* t3 = D - (B + C) = x3 */
|
||||
uECC_vli_modSub(t7, X1, X2, curve->p, num_words); /* t7 = B - x3 */
|
||||
uECC_vli_modMult_fast(Y2, Y2, t7, curve); /* t4 = (y2 - y1)*(B - x3) */
|
||||
/* t4 = (y2 - y1)*(B - x3) - E = y3: */
|
||||
uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words);
|
||||
uECC_vli_modSquare_fast(t7, t5, curve); /* t7 = (y2 + y1)^2 = F */
|
||||
uECC_vli_modSub(t7, t7, t6, curve->p, num_words); /* t7 = F - (B + C) = x3' */
|
||||
uECC_vli_modSub(t6, t7, X1, curve->p, num_words); /* t6 = x3' - B */
|
||||
uECC_vli_modMult_fast(t6, t6, t5, curve); /* t6 = (y2+y1)*(x3' - B) */
|
||||
/* t2 = (y2+y1)*(x3' - B) - E = y3': */
|
||||
uECC_vli_modSub(Y1, t6, Y1, curve->p, num_words);
|
||||
uECC_vli_set(X1, t7, num_words);
|
||||
}
|
||||
|
||||
void EccPoint_mult(uECC_word_t *result, const uECC_word_t *point,
|
||||
const uECC_word_t *scalar,
|
||||
const uECC_word_t *initial_Z,
|
||||
bitcount_t num_bits, uECC_Curve curve)
|
||||
{
|
||||
/* R0 and R1 */
|
||||
uECC_word_t Rx[2][NUM_ECC_WORDS];
|
||||
uECC_word_t Ry[2][NUM_ECC_WORDS];
|
||||
uECC_word_t z[NUM_ECC_WORDS];
|
||||
bitcount_t i;
|
||||
uECC_word_t nb;
|
||||
wordcount_t num_words = curve->num_words;
|
||||
uECC_vli_set(Rx[1], point, num_words);
|
||||
uECC_vli_set(Ry[1], point + num_words, num_words);
|
||||
XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve);
|
||||
|
||||
for(i = num_bits - 2; i > 0; --i) {
|
||||
nb = !uECC_vli_testBit(scalar, i);
|
||||
XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve);
|
||||
XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve);
|
||||
}
|
||||
|
||||
nb = !uECC_vli_testBit(scalar, 0);
|
||||
XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve);
|
||||
/* Find final 1/Z value. */
|
||||
uECC_vli_modSub(z, Rx[1], Rx[0], curve->p, num_words); /* X1 - X0 */
|
||||
uECC_vli_modMult_fast(z, z, Ry[1 - nb], curve); /* Yb * (X1 - X0) */
|
||||
uECC_vli_modMult_fast(z, z, point, curve); /* xP * Yb * (X1 - X0) */
|
||||
uECC_vli_modInv(z, z, curve->p, num_words); /* 1 / (xP * Yb * (X1 - X0))*/
|
||||
/* yP / (xP * Yb * (X1 - X0)) */
|
||||
uECC_vli_modMult_fast(z, z, point + num_words, curve);
|
||||
/* Xb * yP / (xP * Yb * (X1 - X0)) */
|
||||
uECC_vli_modMult_fast(z, z, Rx[1 - nb], curve);
|
||||
/* End 1/Z calculation */
|
||||
XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve);
|
||||
apply_z(Rx[0], Ry[0], z, curve);
|
||||
uECC_vli_set(result, Rx[0], num_words);
|
||||
uECC_vli_set(result + num_words, Ry[0], num_words);
|
||||
}
|
||||
|
||||
uECC_word_t regularize_k(const uECC_word_t *const k, uECC_word_t *k0,
|
||||
uECC_word_t *k1, uECC_Curve curve)
|
||||
{
|
||||
wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
|
||||
bitcount_t num_n_bits = curve->num_n_bits;
|
||||
uECC_word_t carry = uECC_vli_add(k0, k, curve->n, num_n_words) ||
|
||||
(num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
|
||||
uECC_vli_testBit(k0, num_n_bits));
|
||||
uECC_vli_add(k1, k0, curve->n, num_n_words);
|
||||
return carry;
|
||||
}
|
||||
|
||||
uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
|
||||
uECC_word_t *private_key,
|
||||
uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t tmp1[NUM_ECC_WORDS];
|
||||
uECC_word_t tmp2[NUM_ECC_WORDS];
|
||||
uECC_word_t *p2[2] = {tmp1, tmp2};
|
||||
uECC_word_t carry;
|
||||
/* Regularize the bitcount for the private key so that attackers cannot
|
||||
* use a side channel attack to learn the number of leading zeros. */
|
||||
carry = regularize_k(private_key, tmp1, tmp2, curve);
|
||||
EccPoint_mult(result, curve->G, p2[!carry], 0, curve->num_n_bits + 1, curve);
|
||||
|
||||
if(EccPoint_isZero(result, curve)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Converts an integer in uECC native format to big-endian bytes. */
|
||||
void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
|
||||
const unsigned int *native)
|
||||
{
|
||||
wordcount_t i;
|
||||
|
||||
for(i = 0; i < num_bytes; ++i) {
|
||||
unsigned b = num_bytes - 1 - i;
|
||||
bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE));
|
||||
}
|
||||
}
|
||||
|
||||
/* Converts big-endian bytes to an integer in uECC native format. */
|
||||
void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
|
||||
int num_bytes)
|
||||
{
|
||||
wordcount_t i;
|
||||
uECC_vli_clear(native, (num_bytes + (uECC_WORD_SIZE - 1)) / uECC_WORD_SIZE);
|
||||
|
||||
for(i = 0; i < num_bytes; ++i) {
|
||||
unsigned b = num_bytes - 1 - i;
|
||||
native[b / uECC_WORD_SIZE] |=
|
||||
(uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE));
|
||||
}
|
||||
}
|
||||
|
||||
int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
|
||||
wordcount_t num_words)
|
||||
{
|
||||
uECC_word_t mask = (uECC_word_t) -1;
|
||||
uECC_word_t tries;
|
||||
bitcount_t num_bits = uECC_vli_numBits(top, num_words);
|
||||
|
||||
if(!g_rng_function) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for(tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
|
||||
if(!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
random[num_words - 1] &=
|
||||
mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
|
||||
|
||||
if(!uECC_vli_isZero(random, num_words) &&
|
||||
uECC_vli_cmp(top, random, num_words) == 1) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t tmp1[NUM_ECC_WORDS];
|
||||
uECC_word_t tmp2[NUM_ECC_WORDS];
|
||||
wordcount_t num_words = curve->num_words;
|
||||
|
||||
/* The point at infinity is invalid. */
|
||||
if(EccPoint_isZero(point, curve)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* x and y must be smaller than p. */
|
||||
if(uECC_vli_cmp_unsafe(curve->p, point, num_words) != 1 ||
|
||||
uECC_vli_cmp_unsafe(curve->p, point + num_words, num_words) != 1) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
uECC_vli_modSquare_fast(tmp1, point + num_words, curve);
|
||||
curve->x_side(tmp2, point, curve); /* tmp2 = x^3 + ax + b */
|
||||
|
||||
/* Make sure that y^2 == x^3 + ax + b */
|
||||
if(uECC_vli_equal(tmp1, tmp2, num_words) != 0)
|
||||
{ return -3; }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t _public[NUM_ECC_WORDS * 2];
|
||||
uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
|
||||
uECC_vli_bytesToNative(
|
||||
_public + curve->num_words,
|
||||
public_key + curve->num_bytes,
|
||||
curve->num_bytes);
|
||||
|
||||
if(uECC_vli_cmp_unsafe(_public, curve->G, NUM_ECC_WORDS * 2) == 0) {
|
||||
return -4;
|
||||
}
|
||||
|
||||
return uECC_valid_point(_public, curve);
|
||||
}
|
||||
|
||||
int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
|
||||
uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t _private[NUM_ECC_WORDS];
|
||||
uECC_word_t _public[NUM_ECC_WORDS * 2];
|
||||
uECC_vli_bytesToNative(
|
||||
_private,
|
||||
private_key,
|
||||
BITS_TO_BYTES(curve->num_n_bits));
|
||||
|
||||
/* Make sure the private key is in the range [1, n-1]. */
|
||||
if(uECC_vli_isZero(_private, BITS_TO_WORDS(curve->num_n_bits))) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(uECC_vli_cmp(curve->n, _private, BITS_TO_WORDS(curve->num_n_bits)) != 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Compute public key. */
|
||||
if(!EccPoint_compute_public_key(_public, _private, curve)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public);
|
||||
uECC_vli_nativeToBytes(
|
||||
public_key +
|
||||
curve->num_bytes, curve->num_bytes, _public + curve->num_words);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+188
@@ -0,0 +1,188 @@
|
||||
/* ec_dh.c - TinyCrypt implementation of EC-DH */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2014, Kenneth MacKay
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <tinycrypt/constants.h>
|
||||
#include <tinycrypt/ecc.h>
|
||||
#include <tinycrypt/ecc_dh.h>
|
||||
#include <string.h>
|
||||
|
||||
#if default_RNG_defined
|
||||
static uECC_RNG_Function g_rng_function = &default_CSPRNG;
|
||||
#else
|
||||
static uECC_RNG_Function g_rng_function = 0;
|
||||
#endif
|
||||
|
||||
int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key,
|
||||
unsigned int *d, uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t _private[NUM_ECC_WORDS];
|
||||
uECC_word_t _public[NUM_ECC_WORDS * 2];
|
||||
/* This function is designed for test purposes-only (such as validating NIST
|
||||
* test vectors) as it uses a provided value for d instead of generating
|
||||
* it uniformly at random. */
|
||||
memcpy(_private, d, NUM_ECC_BYTES);
|
||||
|
||||
/* Computing public-key from private: */
|
||||
if(EccPoint_compute_public_key(_public, _private, curve)) {
|
||||
/* Converting buffers to correct bit order: */
|
||||
uECC_vli_nativeToBytes(private_key,
|
||||
BITS_TO_BYTES(curve->num_n_bits),
|
||||
_private);
|
||||
uECC_vli_nativeToBytes(public_key,
|
||||
curve->num_bytes,
|
||||
_public);
|
||||
uECC_vli_nativeToBytes(public_key + curve->num_bytes,
|
||||
curve->num_bytes,
|
||||
_public + curve->num_words);
|
||||
/* erasing temporary buffer used to store secret: */
|
||||
memset(_private, 0, NUM_ECC_BYTES);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t _random[NUM_ECC_WORDS * 2];
|
||||
uECC_word_t _private[NUM_ECC_WORDS];
|
||||
uECC_word_t _public[NUM_ECC_WORDS * 2];
|
||||
uECC_word_t tries;
|
||||
|
||||
for(tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
|
||||
/* Generating _private uniformly at random: */
|
||||
uECC_RNG_Function rng_function = uECC_get_rng();
|
||||
|
||||
if(!rng_function ||
|
||||
!rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS * uECC_WORD_SIZE)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
|
||||
uECC_vli_mmod(_private, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
|
||||
|
||||
/* Computing public-key from private: */
|
||||
if(EccPoint_compute_public_key(_public, _private, curve)) {
|
||||
/* Converting buffers to correct bit order: */
|
||||
uECC_vli_nativeToBytes(private_key,
|
||||
BITS_TO_BYTES(curve->num_n_bits),
|
||||
_private);
|
||||
uECC_vli_nativeToBytes(public_key,
|
||||
curve->num_bytes,
|
||||
_public);
|
||||
uECC_vli_nativeToBytes(public_key + curve->num_bytes,
|
||||
curve->num_bytes,
|
||||
_public + curve->num_words);
|
||||
/* erasing temporary buffer that stored secret: */
|
||||
memset(_private, 0, NUM_ECC_BYTES);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,
|
||||
uint8_t *secret, uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t _public[NUM_ECC_WORDS * 2];
|
||||
uECC_word_t _private[NUM_ECC_WORDS];
|
||||
uECC_word_t tmp[NUM_ECC_WORDS];
|
||||
uECC_word_t *p2[2] = {_private, tmp};
|
||||
uECC_word_t *initial_Z = 0;
|
||||
uECC_word_t carry;
|
||||
wordcount_t num_words = curve->num_words;
|
||||
wordcount_t num_bytes = curve->num_bytes;
|
||||
int r;
|
||||
/* Converting buffers to correct bit order: */
|
||||
uECC_vli_bytesToNative(_private,
|
||||
private_key,
|
||||
BITS_TO_BYTES(curve->num_n_bits));
|
||||
uECC_vli_bytesToNative(_public,
|
||||
public_key,
|
||||
num_bytes);
|
||||
uECC_vli_bytesToNative(_public + num_words,
|
||||
public_key + num_bytes,
|
||||
num_bytes);
|
||||
/* Regularize the bitcount for the private key so that attackers cannot use a
|
||||
* side channel attack to learn the number of leading zeros. */
|
||||
carry = regularize_k(_private, _private, tmp, curve);
|
||||
|
||||
/* If an RNG function was specified, try to get a random initial Z value to
|
||||
* improve protection against side-channel attacks. */
|
||||
if(g_rng_function) {
|
||||
if(!uECC_generate_random_int(p2[carry], curve->p, num_words)) {
|
||||
r = 0;
|
||||
goto clear_and_out;
|
||||
}
|
||||
|
||||
initial_Z = p2[carry];
|
||||
}
|
||||
|
||||
EccPoint_mult(_public, _public, p2[!carry], initial_Z, curve->num_n_bits + 1,
|
||||
curve);
|
||||
uECC_vli_nativeToBytes(secret, num_bytes, _public);
|
||||
r = !EccPoint_isZero(_public, curve);
|
||||
clear_and_out:
|
||||
/* erasing temporary buffer used to store secret: */
|
||||
memset(p2, 0, sizeof(p2));
|
||||
__asm__ __volatile__("" :: "g"(p2) : "memory");
|
||||
memset(tmp, 0, sizeof(tmp));
|
||||
__asm__ __volatile__("" :: "g"(tmp) : "memory");
|
||||
memset(_private, 0, sizeof(_private));
|
||||
__asm__ __volatile__("" :: "g"(_private) : "memory");
|
||||
return r;
|
||||
}
|
||||
+289
@@ -0,0 +1,289 @@
|
||||
/* ec_dsa.c - TinyCrypt implementation of EC-DSA */
|
||||
|
||||
/* Copyright (c) 2014, Kenneth MacKay
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/constants.h>
|
||||
#include <tinycrypt/ecc.h>
|
||||
#include <tinycrypt/ecc_dsa.h>
|
||||
|
||||
#if default_RNG_defined
|
||||
static uECC_RNG_Function g_rng_function = &default_CSPRNG;
|
||||
#else
|
||||
static uECC_RNG_Function g_rng_function = 0;
|
||||
#endif
|
||||
|
||||
static void bits2int(uECC_word_t *native, const uint8_t *bits,
|
||||
unsigned bits_size, uECC_Curve curve)
|
||||
{
|
||||
unsigned num_n_bytes = BITS_TO_BYTES(curve->num_n_bits);
|
||||
unsigned num_n_words = BITS_TO_WORDS(curve->num_n_bits);
|
||||
int shift;
|
||||
uECC_word_t carry;
|
||||
uECC_word_t *ptr;
|
||||
|
||||
if(bits_size > num_n_bytes) {
|
||||
bits_size = num_n_bytes;
|
||||
}
|
||||
|
||||
uECC_vli_clear(native, num_n_words);
|
||||
uECC_vli_bytesToNative(native, bits, bits_size);
|
||||
|
||||
if(bits_size * 8 <= (unsigned)curve->num_n_bits) {
|
||||
return;
|
||||
}
|
||||
|
||||
shift = bits_size * 8 - curve->num_n_bits;
|
||||
carry = 0;
|
||||
ptr = native + num_n_words;
|
||||
|
||||
while(ptr-- > native) {
|
||||
uECC_word_t temp = *ptr;
|
||||
*ptr = (temp >> shift) | carry;
|
||||
carry = temp << (uECC_WORD_BITS - shift);
|
||||
}
|
||||
|
||||
/* Reduce mod curve_n */
|
||||
if(uECC_vli_cmp_unsafe(curve->n, native, num_n_words) != 1) {
|
||||
uECC_vli_sub(native, native, curve->n, num_n_words);
|
||||
}
|
||||
}
|
||||
|
||||
int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
|
||||
unsigned hash_size, uECC_word_t *k, uint8_t *signature,
|
||||
uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t tmp[NUM_ECC_WORDS];
|
||||
uECC_word_t s[NUM_ECC_WORDS];
|
||||
uECC_word_t *k2[2] = {tmp, s};
|
||||
uECC_word_t p[NUM_ECC_WORDS * 2];
|
||||
uECC_word_t carry;
|
||||
wordcount_t num_words = curve->num_words;
|
||||
wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
|
||||
bitcount_t num_n_bits = curve->num_n_bits;
|
||||
|
||||
/* Make sure 0 < k < curve_n */
|
||||
if(uECC_vli_isZero(k, num_words) ||
|
||||
uECC_vli_cmp(curve->n, k, num_n_words) != 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
carry = regularize_k(k, tmp, s, curve);
|
||||
EccPoint_mult(p, curve->G, k2[!carry], 0, num_n_bits + 1, curve);
|
||||
|
||||
if(uECC_vli_isZero(p, num_words)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* If an RNG function was specified, get a random number
|
||||
to prevent side channel analysis of k. */
|
||||
if(!g_rng_function) {
|
||||
uECC_vli_clear(tmp, num_n_words);
|
||||
tmp[0] = 1;
|
||||
} else if(!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Prevent side channel analysis of uECC_vli_modInv() to determine
|
||||
bits of k / the private key by premultiplying by a random number */
|
||||
uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */
|
||||
uECC_vli_modInv(k, k, curve->n, num_n_words); /* k = 1 / k' */
|
||||
uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */
|
||||
uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
|
||||
/* tmp = d: */
|
||||
uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
|
||||
s[num_n_words - 1] = 0;
|
||||
uECC_vli_set(s, p, num_words);
|
||||
uECC_vli_modMult(s, tmp, s, curve->n, num_n_words); /* s = r*d */
|
||||
bits2int(tmp, message_hash, hash_size, curve);
|
||||
uECC_vli_modAdd(s, tmp, s, curve->n, num_n_words); /* s = e + r*d */
|
||||
uECC_vli_modMult(s, s, k, curve->n, num_n_words); /* s = (e + r*d) / k */
|
||||
|
||||
if(uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
|
||||
unsigned hash_size, uint8_t *signature, uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t _random[2 * NUM_ECC_WORDS];
|
||||
uECC_word_t k[NUM_ECC_WORDS];
|
||||
uECC_word_t tries;
|
||||
|
||||
for(tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
|
||||
/* Generating _random uniformly at random: */
|
||||
uECC_RNG_Function rng_function = uECC_get_rng();
|
||||
|
||||
if(!rng_function ||
|
||||
!rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS * uECC_WORD_SIZE)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// computing k as modular reduction of _random (see FIPS 186.4 B.5.1):
|
||||
uECC_vli_mmod(k, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
|
||||
|
||||
if(uECC_sign_with_k(private_key, message_hash, hash_size, k, signature,
|
||||
curve)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bitcount_t smax(bitcount_t a, bitcount_t b)
|
||||
{
|
||||
return (a > b ? a : b);
|
||||
}
|
||||
|
||||
int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
|
||||
unsigned hash_size, const uint8_t *signature,
|
||||
uECC_Curve curve)
|
||||
{
|
||||
uECC_word_t u1[NUM_ECC_WORDS], u2[NUM_ECC_WORDS];
|
||||
uECC_word_t z[NUM_ECC_WORDS];
|
||||
uECC_word_t sum[NUM_ECC_WORDS * 2];
|
||||
uECC_word_t rx[NUM_ECC_WORDS];
|
||||
uECC_word_t ry[NUM_ECC_WORDS];
|
||||
uECC_word_t tx[NUM_ECC_WORDS];
|
||||
uECC_word_t ty[NUM_ECC_WORDS];
|
||||
uECC_word_t tz[NUM_ECC_WORDS];
|
||||
const uECC_word_t *points[4];
|
||||
const uECC_word_t *point;
|
||||
bitcount_t num_bits;
|
||||
bitcount_t i;
|
||||
uECC_word_t _public[NUM_ECC_WORDS * 2];
|
||||
uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
|
||||
wordcount_t num_words = curve->num_words;
|
||||
wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
|
||||
rx[num_n_words - 1] = 0;
|
||||
r[num_n_words - 1] = 0;
|
||||
s[num_n_words - 1] = 0;
|
||||
uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
|
||||
uECC_vli_bytesToNative(_public + num_words, public_key + curve->num_bytes,
|
||||
curve->num_bytes);
|
||||
uECC_vli_bytesToNative(r, signature, curve->num_bytes);
|
||||
uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
|
||||
|
||||
/* r, s must not be 0. */
|
||||
if(uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* r, s must be < n. */
|
||||
if(uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 ||
|
||||
uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Calculate u1 and u2. */
|
||||
uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
|
||||
u1[num_n_words - 1] = 0;
|
||||
bits2int(u1, message_hash, hash_size, curve);
|
||||
uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
|
||||
uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */
|
||||
/* Calculate sum = G + Q. */
|
||||
uECC_vli_set(sum, _public, num_words);
|
||||
uECC_vli_set(sum + num_words, _public + num_words, num_words);
|
||||
uECC_vli_set(tx, curve->G, num_words);
|
||||
uECC_vli_set(ty, curve->G + num_words, num_words);
|
||||
uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
|
||||
XYcZ_add(tx, ty, sum, sum + num_words, curve);
|
||||
uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
|
||||
apply_z(sum, sum + num_words, z, curve);
|
||||
/* Use Shamir's trick to calculate u1*G + u2*Q */
|
||||
points[0] = 0;
|
||||
points[1] = curve->G;
|
||||
points[2] = _public;
|
||||
points[3] = sum;
|
||||
num_bits = smax(uECC_vli_numBits(u1, num_n_words),
|
||||
uECC_vli_numBits(u2, num_n_words));
|
||||
point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
|
||||
((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
|
||||
uECC_vli_set(rx, point, num_words);
|
||||
uECC_vli_set(ry, point + num_words, num_words);
|
||||
uECC_vli_clear(z, num_words);
|
||||
z[0] = 1;
|
||||
|
||||
for(i = num_bits - 2; i >= 0; --i) {
|
||||
uECC_word_t index;
|
||||
curve->double_jacobian(rx, ry, z, curve);
|
||||
index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
|
||||
point = points[index];
|
||||
|
||||
if(point) {
|
||||
uECC_vli_set(tx, point, num_words);
|
||||
uECC_vli_set(ty, point + num_words, num_words);
|
||||
apply_z(tx, ty, z, curve);
|
||||
uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
|
||||
XYcZ_add(tx, ty, rx, ry, curve);
|
||||
uECC_vli_modMult_fast(z, z, tz, curve);
|
||||
}
|
||||
}
|
||||
|
||||
uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
|
||||
apply_z(rx, ry, z, curve);
|
||||
|
||||
/* v = x1 (mod n) */
|
||||
if(uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) {
|
||||
uECC_vli_sub(rx, rx, curve->n, num_n_words);
|
||||
}
|
||||
|
||||
/* Accept only if v == r. */
|
||||
return (int)(uECC_vli_equal(rx, r, num_words) == 0);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/* uECC_platform_specific.c - Implementation of platform specific functions*/
|
||||
|
||||
/* Copyright (c) 2014, Kenneth MacKay
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* uECC_platform_specific.c -- Implementation of platform specific functions
|
||||
*/
|
||||
|
||||
|
||||
#if defined(unix) || defined(__linux__) || defined(__unix__) || \
|
||||
defined(__unix) | (defined(__APPLE__) && defined(__MACH__)) || \
|
||||
defined(uECC_POSIX)
|
||||
|
||||
/* Some POSIX-like system with /dev/urandom or /dev/random. */
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef O_CLOEXEC
|
||||
#define O_CLOEXEC 0
|
||||
#endif
|
||||
|
||||
int default_CSPRNG(uint8_t *dest, unsigned int size)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(dest == (uint8_t *) 0 || (size == 0))
|
||||
{ return 0; }
|
||||
|
||||
int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
|
||||
|
||||
if(fd == -1) {
|
||||
fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
|
||||
|
||||
if(fd == -1) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
char *ptr = (char *)dest;
|
||||
size_t left = (size_t) size;
|
||||
|
||||
while(left > 0) {
|
||||
ssize_t bytes_read = read(fd, ptr, left);
|
||||
|
||||
if(bytes_read <= 0) { // read failed
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
left -= bytes_read;
|
||||
ptr += bytes_read;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif /* platform */
|
||||
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
/* hmac.c - TinyCrypt implementation of the HMAC algorithm */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/hmac.h>
|
||||
#include <tinycrypt/constants.h>
|
||||
#include <tinycrypt/utils.h>
|
||||
|
||||
static void rekey(uint8_t *key, const uint8_t *new_key, unsigned int key_size)
|
||||
{
|
||||
const uint8_t inner_pad = (uint8_t) 0x36;
|
||||
const uint8_t outer_pad = (uint8_t) 0x5c;
|
||||
unsigned int i;
|
||||
|
||||
for(i = 0; i < key_size; ++i) {
|
||||
key[i] = inner_pad ^ new_key[i];
|
||||
key[i + TC_SHA256_BLOCK_SIZE] = outer_pad ^ new_key[i];
|
||||
}
|
||||
|
||||
for(; i < TC_SHA256_BLOCK_SIZE; ++i) {
|
||||
key[i] = inner_pad;
|
||||
key[i + TC_SHA256_BLOCK_SIZE] = outer_pad;
|
||||
}
|
||||
}
|
||||
|
||||
int tc_hmac_set_key(TCHmacState_t ctx, const uint8_t *key,
|
||||
unsigned int key_size)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(ctx == (TCHmacState_t) 0 ||
|
||||
key == (const uint8_t *) 0 ||
|
||||
key_size == 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
const uint8_t dummy_key[key_size];
|
||||
struct tc_hmac_state_struct dummy_state;
|
||||
|
||||
if(key_size <= TC_SHA256_BLOCK_SIZE) {
|
||||
/*
|
||||
* The next three lines consist of dummy calls just to avoid
|
||||
* certain timing attacks. Without these dummy calls,
|
||||
* adversaries would be able to learn whether the key_size is
|
||||
* greater than TC_SHA256_BLOCK_SIZE by measuring the time
|
||||
* consumed in this process.
|
||||
*/
|
||||
(void)tc_sha256_init(&dummy_state.hash_state);
|
||||
(void)tc_sha256_update(&dummy_state.hash_state,
|
||||
dummy_key,
|
||||
key_size);
|
||||
(void)tc_sha256_final(&dummy_state.key[TC_SHA256_DIGEST_SIZE],
|
||||
&dummy_state.hash_state);
|
||||
/* Actual code for when key_size <= TC_SHA256_BLOCK_SIZE: */
|
||||
rekey(ctx->key, key, key_size);
|
||||
} else {
|
||||
(void)tc_sha256_init(&ctx->hash_state);
|
||||
(void)tc_sha256_update(&ctx->hash_state, key, key_size);
|
||||
(void)tc_sha256_final(&ctx->key[TC_SHA256_DIGEST_SIZE],
|
||||
&ctx->hash_state);
|
||||
rekey(ctx->key,
|
||||
&ctx->key[TC_SHA256_DIGEST_SIZE],
|
||||
TC_SHA256_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_hmac_init(TCHmacState_t ctx)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(ctx == (TCHmacState_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
(void) tc_sha256_init(&ctx->hash_state);
|
||||
(void) tc_sha256_update(&ctx->hash_state, ctx->key, TC_SHA256_BLOCK_SIZE);
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_hmac_update(TCHmacState_t ctx,
|
||||
const void *data,
|
||||
unsigned int data_length)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(ctx == (TCHmacState_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
(void)tc_sha256_update(&ctx->hash_state, data, data_length);
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_hmac_final(uint8_t *tag, unsigned int taglen, TCHmacState_t ctx)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(tag == (uint8_t *) 0 ||
|
||||
taglen != TC_SHA256_DIGEST_SIZE ||
|
||||
ctx == (TCHmacState_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
(void) tc_sha256_final(tag, &ctx->hash_state);
|
||||
(void)tc_sha256_init(&ctx->hash_state);
|
||||
(void)tc_sha256_update(&ctx->hash_state,
|
||||
&ctx->key[TC_SHA256_BLOCK_SIZE],
|
||||
TC_SHA256_BLOCK_SIZE);
|
||||
(void)tc_sha256_update(&ctx->hash_state, tag, TC_SHA256_DIGEST_SIZE);
|
||||
(void)tc_sha256_final(tag, &ctx->hash_state);
|
||||
/* destroy the current state */
|
||||
_set(ctx, 0, sizeof(*ctx));
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
/* hmac_prng.c - TinyCrypt implementation of HMAC-PRNG */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/hmac_prng.h>
|
||||
#include <tinycrypt/hmac.h>
|
||||
#include <tinycrypt/constants.h>
|
||||
#include <tinycrypt/utils.h>
|
||||
|
||||
/*
|
||||
* min bytes in the seed string.
|
||||
* MIN_SLEN*8 must be at least the expected security level.
|
||||
*/
|
||||
static const unsigned int MIN_SLEN = 32;
|
||||
|
||||
/*
|
||||
* max bytes in the seed string;
|
||||
* SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
|
||||
*/
|
||||
static const unsigned int MAX_SLEN = UINT32_MAX;
|
||||
|
||||
/*
|
||||
* max bytes in the personalization string;
|
||||
* SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
|
||||
*/
|
||||
static const unsigned int MAX_PLEN = UINT32_MAX;
|
||||
|
||||
/*
|
||||
* max bytes in the additional_info string;
|
||||
* SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
|
||||
*/
|
||||
static const unsigned int MAX_ALEN = UINT32_MAX;
|
||||
|
||||
/*
|
||||
* max number of generates between re-seeds;
|
||||
* TinyCrypt accepts up to (2^32 - 1) which is the maximal value of
|
||||
* a 32-bit unsigned int variable, while SP800-90A specifies a maximum of 2^48.
|
||||
*/
|
||||
static const unsigned int MAX_GENS = UINT32_MAX;
|
||||
|
||||
/*
|
||||
* maximum bytes per generate call;
|
||||
* SP800-90A specifies a maximum up to 2^19.
|
||||
*/
|
||||
static const unsigned int MAX_OUT = (1 << 19);
|
||||
|
||||
/*
|
||||
* Assumes: prng != NULL, e != NULL, len >= 0.
|
||||
*/
|
||||
static void update(TCHmacPrng_t prng, const uint8_t *e, unsigned int len)
|
||||
{
|
||||
const uint8_t separator0 = 0x00;
|
||||
const uint8_t separator1 = 0x01;
|
||||
/* use current state, e and separator 0 to compute a new prng key: */
|
||||
(void)tc_hmac_init(&prng->h);
|
||||
(void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
|
||||
(void)tc_hmac_update(&prng->h, &separator0, sizeof(separator0));
|
||||
(void)tc_hmac_update(&prng->h, e, len);
|
||||
(void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h);
|
||||
/* configure the new prng key into the prng's instance of hmac */
|
||||
(void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
|
||||
/* use the new key to compute a new state variable v */
|
||||
(void)tc_hmac_init(&prng->h);
|
||||
(void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
|
||||
(void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
|
||||
/* use current state, e and separator 1 to compute a new prng key: */
|
||||
(void)tc_hmac_init(&prng->h);
|
||||
(void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
|
||||
(void)tc_hmac_update(&prng->h, &separator1, sizeof(separator1));
|
||||
(void)tc_hmac_update(&prng->h, e, len);
|
||||
(void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h);
|
||||
/* configure the new prng key into the prng's instance of hmac */
|
||||
(void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
|
||||
/* use the new key to compute a new state variable v */
|
||||
(void)tc_hmac_init(&prng->h);
|
||||
(void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
|
||||
(void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
|
||||
}
|
||||
|
||||
int tc_hmac_prng_init(TCHmacPrng_t prng,
|
||||
const uint8_t *personalization,
|
||||
unsigned int plen)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(prng == (TCHmacPrng_t) 0 ||
|
||||
personalization == (uint8_t *) 0 ||
|
||||
plen > MAX_PLEN) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
/* put the generator into a known state: */
|
||||
_set(prng->key, 0x00, sizeof(prng->key));
|
||||
_set(prng->v, 0x01, sizeof(prng->v));
|
||||
tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
|
||||
/* update assumes SOME key has been configured into HMAC */
|
||||
update(prng, personalization, plen);
|
||||
/* force a reseed before allowing tc_hmac_prng_generate to succeed: */
|
||||
prng->countdown = 0;
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_hmac_prng_reseed(TCHmacPrng_t prng,
|
||||
const uint8_t *seed,
|
||||
unsigned int seedlen,
|
||||
const uint8_t *additional_input,
|
||||
unsigned int additionallen)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(prng == (TCHmacPrng_t) 0 ||
|
||||
seed == (const uint8_t *) 0 ||
|
||||
seedlen < MIN_SLEN ||
|
||||
seedlen > MAX_SLEN) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
if(additional_input != (const uint8_t *) 0) {
|
||||
/*
|
||||
* Abort if additional_input is provided but has inappropriate
|
||||
* length
|
||||
*/
|
||||
if(additionallen == 0 ||
|
||||
additionallen > MAX_ALEN) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
} else {
|
||||
/* call update for the seed and additional_input */
|
||||
update(prng, seed, seedlen);
|
||||
update(prng, additional_input, additionallen);
|
||||
}
|
||||
} else {
|
||||
/* call update only for the seed */
|
||||
update(prng, seed, seedlen);
|
||||
}
|
||||
|
||||
/* ... and enable hmac_prng_generate */
|
||||
prng->countdown = MAX_GENS;
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_hmac_prng_generate(uint8_t *out, unsigned int outlen, TCHmacPrng_t prng)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(out == (uint8_t *) 0 ||
|
||||
prng == (TCHmacPrng_t) 0 ||
|
||||
outlen == 0 ||
|
||||
outlen > MAX_OUT) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
} else if(prng->countdown == 0) {
|
||||
return TC_HMAC_PRNG_RESEED_REQ;
|
||||
}
|
||||
|
||||
prng->countdown--;
|
||||
|
||||
while(outlen != 0) {
|
||||
/* operate HMAC in OFB mode to create "random" outputs */
|
||||
(void)tc_hmac_init(&prng->h);
|
||||
(void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
|
||||
(void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
|
||||
unsigned int bufferlen = (TC_SHA256_DIGEST_SIZE > outlen) ?
|
||||
outlen : TC_SHA256_DIGEST_SIZE;
|
||||
(void)_copy(out, bufferlen, prng->v, bufferlen);
|
||||
out += bufferlen;
|
||||
outlen = (outlen > TC_SHA256_DIGEST_SIZE) ?
|
||||
(outlen - TC_SHA256_DIGEST_SIZE) : 0;
|
||||
}
|
||||
|
||||
/* block future PRNG compromises from revealing past state */
|
||||
update(prng, prng->v, TC_SHA256_DIGEST_SIZE);
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
+235
@@ -0,0 +1,235 @@
|
||||
/* sha256.c - TinyCrypt SHA-256 crypto hash algorithm implementation */
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of Intel Corporation nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <tinycrypt/sha256.h>
|
||||
#include <tinycrypt/constants.h>
|
||||
#include <tinycrypt/utils.h>
|
||||
|
||||
static void compress(unsigned int *iv, const uint8_t *data);
|
||||
|
||||
int tc_sha256_init(TCSha256State_t s)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(s == (TCSha256State_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Setting the initial state values.
|
||||
* These values correspond to the first 32 bits of the fractional parts
|
||||
* of the square roots of the first 8 primes: 2, 3, 5, 7, 11, 13, 17
|
||||
* and 19.
|
||||
*/
|
||||
_set((uint8_t *) s, 0x00, sizeof(*s));
|
||||
s->iv[0] = 0x6a09e667;
|
||||
s->iv[1] = 0xbb67ae85;
|
||||
s->iv[2] = 0x3c6ef372;
|
||||
s->iv[3] = 0xa54ff53a;
|
||||
s->iv[4] = 0x510e527f;
|
||||
s->iv[5] = 0x9b05688c;
|
||||
s->iv[6] = 0x1f83d9ab;
|
||||
s->iv[7] = 0x5be0cd19;
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_sha256_update(TCSha256State_t s, const uint8_t *data, size_t datalen)
|
||||
{
|
||||
/* input sanity check: */
|
||||
if(s == (TCSha256State_t) 0 ||
|
||||
data == (void *) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
} else if(datalen == 0) {
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
while(datalen-- > 0) {
|
||||
s->leftover[s->leftover_offset++] = *(data++);
|
||||
|
||||
if(s->leftover_offset >= TC_SHA256_BLOCK_SIZE) {
|
||||
compress(s->iv, s->leftover);
|
||||
s->leftover_offset = 0;
|
||||
s->bits_hashed += (TC_SHA256_BLOCK_SIZE << 3);
|
||||
}
|
||||
}
|
||||
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
int tc_sha256_final(uint8_t *digest, TCSha256State_t s)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
/* input sanity check: */
|
||||
if(digest == (uint8_t *) 0 ||
|
||||
s == (TCSha256State_t) 0) {
|
||||
return TC_CRYPTO_FAIL;
|
||||
}
|
||||
|
||||
s->bits_hashed += (s->leftover_offset << 3);
|
||||
s->leftover[s->leftover_offset++] = 0x80; /* always room for one byte */
|
||||
|
||||
if(s->leftover_offset > (sizeof(s->leftover) - 8)) {
|
||||
/* there is not room for all the padding in this block */
|
||||
_set(s->leftover + s->leftover_offset, 0x00,
|
||||
sizeof(s->leftover) - s->leftover_offset);
|
||||
compress(s->iv, s->leftover);
|
||||
s->leftover_offset = 0;
|
||||
}
|
||||
|
||||
/* add the padding and the length in big-Endian format */
|
||||
_set(s->leftover + s->leftover_offset, 0x00,
|
||||
sizeof(s->leftover) - 8 - s->leftover_offset);
|
||||
s->leftover[sizeof(s->leftover) - 1] = (uint8_t)(s->bits_hashed);
|
||||
s->leftover[sizeof(s->leftover) - 2] = (uint8_t)(s->bits_hashed >> 8);
|
||||
s->leftover[sizeof(s->leftover) - 3] = (uint8_t)(s->bits_hashed >> 16);
|
||||
s->leftover[sizeof(s->leftover) - 4] = (uint8_t)(s->bits_hashed >> 24);
|
||||
s->leftover[sizeof(s->leftover) - 5] = (uint8_t)(s->bits_hashed >> 32);
|
||||
s->leftover[sizeof(s->leftover) - 6] = (uint8_t)(s->bits_hashed >> 40);
|
||||
s->leftover[sizeof(s->leftover) - 7] = (uint8_t)(s->bits_hashed >> 48);
|
||||
s->leftover[sizeof(s->leftover) - 8] = (uint8_t)(s->bits_hashed >> 56);
|
||||
/* hash the padding and length */
|
||||
compress(s->iv, s->leftover);
|
||||
|
||||
/* copy the iv out to digest */
|
||||
for(i = 0; i < TC_SHA256_STATE_BLOCKS; ++i) {
|
||||
unsigned int t = *((unsigned int *) &s->iv[i]);
|
||||
*digest++ = (uint8_t)(t >> 24);
|
||||
*digest++ = (uint8_t)(t >> 16);
|
||||
*digest++ = (uint8_t)(t >> 8);
|
||||
*digest++ = (uint8_t)(t);
|
||||
}
|
||||
|
||||
/* destroy the current state */
|
||||
_set(s, 0, sizeof(*s));
|
||||
return TC_CRYPTO_SUCCESS;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initializing SHA-256 Hash constant words K.
|
||||
* These values correspond to the first 32 bits of the fractional parts of the
|
||||
* cube roots of the first 64 primes between 2 and 311.
|
||||
*/
|
||||
static const unsigned int k256[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
|
||||
};
|
||||
|
||||
static inline unsigned int ROTR(unsigned int a, unsigned int n)
|
||||
{
|
||||
return (((a) >> n) | ((a) << (32 - n)));
|
||||
}
|
||||
|
||||
#define Sigma0(a)(ROTR((a), 2) ^ ROTR((a), 13) ^ ROTR((a), 22))
|
||||
#define Sigma1(a)(ROTR((a), 6) ^ ROTR((a), 11) ^ ROTR((a), 25))
|
||||
#define sigma0(a)(ROTR((a), 7) ^ ROTR((a), 18) ^ ((a) >> 3))
|
||||
#define sigma1(a)(ROTR((a), 17) ^ ROTR((a), 19) ^ ((a) >> 10))
|
||||
|
||||
#define Ch(a, b, c)(((a) & (b)) ^ ((~(a)) & (c)))
|
||||
#define Maj(a, b, c)(((a) & (b)) ^ ((a) & (c)) ^ ((b) & (c)))
|
||||
|
||||
static inline unsigned int BigEndian(const uint8_t **c)
|
||||
{
|
||||
unsigned int n = 0;
|
||||
n = (((unsigned int)(*((*c)++))) << 24);
|
||||
n |= ((unsigned int)(*((*c)++)) << 16);
|
||||
n |= ((unsigned int)(*((*c)++)) << 8);
|
||||
n |= ((unsigned int)(*((*c)++)));
|
||||
return n;
|
||||
}
|
||||
|
||||
static void compress(unsigned int *iv, const uint8_t *data)
|
||||
{
|
||||
unsigned int a, b, c, d, e, f, g, h;
|
||||
unsigned int t1, t2;
|
||||
unsigned int work_space[16];
|
||||
unsigned int i;
|
||||
a = iv[0];
|
||||
b = iv[1];
|
||||
c = iv[2];
|
||||
d = iv[3];
|
||||
e = iv[4];
|
||||
f = iv[5];
|
||||
g = iv[6];
|
||||
h = iv[7];
|
||||
|
||||
for(i = 0; i < 16; ++i) {
|
||||
unsigned int n = BigEndian(&data);
|
||||
t1 = work_space[i] = n;
|
||||
t1 += h + Sigma1(e) + Ch(e, f, g) + k256[i];
|
||||
t2 = Sigma0(a) + Maj(a, b, c);
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + t1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1 + t2;
|
||||
}
|
||||
|
||||
for(; i < 64; ++i) {
|
||||
unsigned int s0, s1;
|
||||
s0 = work_space[(i + 1) & 0x0f];
|
||||
s0 = sigma0(s0);
|
||||
s1 = work_space[(i + 14) & 0x0f];
|
||||
s1 = sigma1(s1);
|
||||
t1 = work_space[i & 0xf] += s0 + s1 + work_space[(i + 9) & 0xf];
|
||||
t1 += h + Sigma1(e) + Ch(e, f, g) + k256[i];
|
||||
t2 = Sigma0(a) + Maj(a, b, c);
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + t1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1 + t2;
|
||||
}
|
||||
|
||||
iv[0] += a;
|
||||
iv[1] += b;
|
||||
iv[2] += c;
|
||||
iv[3] += d;
|
||||
iv[4] += e;
|
||||
iv[5] += f;
|
||||
iv[6] += g;
|
||||
iv[7] += h;
|
||||
}
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
TOP_DIR = ../../../..
|
||||
sinclude $(TOP_DIR)/tools/w800/conf.mk
|
||||
|
||||
ifndef PDIR
|
||||
GEN_LIBS = libnimble$(LIB_EXT)
|
||||
COMPONENTS_libnimble = host/libhost$(LIB_EXT) transport/libtransport$(LIB_EXT)
|
||||
endif
|
||||
|
||||
#DEFINES +=
|
||||
|
||||
sinclude $(TOP_DIR)/tools/w800/rules.mk
|
||||
|
||||
INCLUDES := $(INCLUDES) -I ./ -I $(PDIR)include
|
||||
PDIR := ../$(PDIR)
|
||||
sinclude $(PDIR)Makefile
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user