rel_1.6.0 init

This commit is contained in:
guocheng.kgc 2020-06-18 20:06:52 +08:00 committed by shengdong.dsd
commit 27b3e2883d
19359 changed files with 8093121 additions and 0 deletions

View file

@ -0,0 +1,28 @@
NAME := irot
GLOBAL_INCLUDES += ../include/irot
ifeq ($(findstring linuxhost, $(BUILD_STRING)), linuxhost)
$(NAME)_COMPONENTS := irot.km
else ifeq ($(findstring mk3060, $(BUILD_STRING)), mk3060)
$(NAME)_COMPONENTS := irot.km
else ifeq ($(findstring mk3080, $(BUILD_STRING)), mk3080)
$(NAME)_COMPONENTS := irot.km
else ifeq ($(findstring mk5080, $(BUILD_STRING)), mk5080)
$(NAME)_COMPONENTS := irot.km
else ifeq ($(findstring cb2201, $(BUILD_STRING)), cb2201)
$(NAME)_COMPONENTS := irot.tee
else ifeq ($(findstring uno-91h, $(BUILD_STRING)), uno-91h)
$(NAME)_COMPONENTS := irot.km
else ifeq ($(findstring hf-lpt230, $(BUILD_STRING)), hf-lpt230)
$(NAME)_COMPONENTS := irot.km
else ifeq ($(findstring hf-lpb135, $(BUILD_STRING)), hf-lpb135)
$(NAME)_COMPONENTS := irot.km
else ifeq ($(findstring hf-lpt130, $(BUILD_STRING)), hf-lpt130)
$(NAME)_COMPONENTS := irot.km
else ifeq ($(findstring hf-lpb130, $(BUILD_STRING)), hf-lpb130)
$(NAME)_COMPONENTS := irot.km
else ifeq ($(findstring amebaz, $(BUILD_STRING)), amebaz)
$(NAME)_COMPONENTS := irot.km
endif

View file

@ -0,0 +1,5 @@
NAME := libkm
$(NAME)_COMPONENTS := irot.km.platform alicrypto
$(NAME)_PREBUILT_LIBRARY := lib/$(HOST_ARCH)/libkm.a

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,181 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include "hal/wifi.h"
#include "hal/soc/flash.h"
#include "plat_gen.h"
#include "plat_comm.h"
#define DEV_ID_LEN 6
#define MIN_SECTOR_SIZE 4096
#define MIN_SECTOR_SHIFT 12
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
{
int ret = 0;
if (*id_len < DEV_ID_LEN) {
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
*id_len = DEV_ID_LEN;
return -1;
}
ret = hal_wifi_get_mac_addr(NULL, dev_id);
if (ret) {
PL_ERR("get mac addr failed\n");
return -1;
}
*id_len = DEV_ID_LEN;
return 0;
}
//for amebaz_dev not support return 0 directly
int open_rsvd_part(int flag)
{
return 0;
}
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
{
int ret = 0;
uint32_t erase_size = 0;
uint32_t pre_len = 0;
uint8_t *pre_buf = NULL;
uint32_t suf_len = 0;
uint8_t *suf_buf = NULL;
uint32_t first_off = 0;
uint32_t last_off = 0;
uint32_t fin_off = offset + data_len;
uint32_t tmp_off = 0;
if (data_len != 0 && data == NULL) {
PL_ERR("bad param \n");
return -1;
}
if (data_len == 0) {
return 0;
}
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
pre_len = offset - first_off;
if (pre_len) {
pre_buf = (uint8_t *)pl_malloc(pre_len);
if (pre_buf == NULL) {
PL_ERR("malloc pre buf failed\n");
return -1;
}
//store the data of first block that will be erased
tmp_off = first_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
pre_buf, pre_len);
if (ret || (tmp_off - first_off != pre_len)) {
PL_ERR("hal flash pre read failed\n");
ret = -1;
goto clean;
}
}
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
suf_len = last_off - fin_off;
if (suf_len) {
suf_buf = (uint8_t *)pl_malloc(suf_len);
if (suf_buf == NULL) {
PL_ERR("malloc suf buf failed\n");
ret = -1;
goto clean;
}
//read the data of last block that will be erased
tmp_off = fin_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
suf_buf, suf_len);
if (ret || (tmp_off - fin_off != suf_len)) {
PL_ERR("hal flash pre read failed\n");
goto clean;
}
}
erase_size = last_off - first_off;
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
if (ret) {
PL_ERR("flash erase failed ret %d\n", ret);
goto clean;
}
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
/* write first sector data that be erased*/
tmp_off = first_off;
if (pre_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
if (ret || (tmp_off - first_off) != pre_len) {
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
goto clean;
}
}
/* write the data need to write */
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
/* write last sector data that be erased */
if (suf_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
}
clean:
if (suf_buf) {
pl_free(suf_buf);
suf_buf = NULL;
}
if (pre_buf) {
pl_free(pre_buf);
pre_buf = NULL;
}
return ret;
}
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
{
uint32_t off_set = offset;
uint32_t len = 0;
int ret = 0;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
if (ret) {
PL_ERR("hal flash read failed\n");
return -1;
}
len = off_set - offset;
if (len != read_len) {
PL_ERR("read failed read_len is %d", (unsigned int)len);
return -1;
}
return 0;
}
int close_rsvd_part(int fd)
{
return 0;
}

View file

@ -0,0 +1,181 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include "hal/wifi.h"
#include "hal/soc/flash.h"
#include "plat_gen.h"
#include "plat_comm.h"
#define DEV_ID_LEN 6
#define MIN_SECTOR_SIZE 4096
#define MIN_SECTOR_SHIFT 12
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
{
int ret = 0;
if (*id_len < DEV_ID_LEN) {
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
*id_len = DEV_ID_LEN;
return -1;
}
ret = hal_wifi_get_mac_addr(NULL, dev_id);
if (ret) {
PL_ERR("get mac addr failed\n");
return -1;
}
*id_len = DEV_ID_LEN;
return 0;
}
//for rda5981x not support return 0 directly
int open_rsvd_part(int flag)
{
return 0;
}
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
{
int ret = 0;
uint32_t erase_size = 0;
uint32_t pre_len = 0;
uint8_t *pre_buf = NULL;
uint32_t suf_len = 0;
uint8_t *suf_buf = NULL;
uint32_t first_off = 0;
uint32_t last_off = 0;
uint32_t fin_off = offset + data_len;
uint32_t tmp_off = 0;
if (data_len != 0 && data == NULL) {
PL_ERR("bad param \n");
return -1;
}
if (data_len == 0) {
return 0;
}
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
pre_len = offset - first_off;
if (pre_len) {
pre_buf = (uint8_t *)pl_malloc(pre_len);
if (pre_buf == NULL) {
PL_ERR("malloc pre buf failed\n");
return -1;
}
//store the data of first block that will be erased
tmp_off = first_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
pre_buf, pre_len);
if (ret || (tmp_off - first_off != pre_len)) {
PL_ERR("hal flash pre read failed\n");
ret = -1;
goto clean;
}
}
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
suf_len = last_off - fin_off;
if (suf_len) {
suf_buf = (uint8_t *)pl_malloc(suf_len);
if (suf_buf == NULL) {
PL_ERR("malloc suf buf failed\n");
ret = -1;
goto clean;
}
//read the data of last block that will be erased
tmp_off = fin_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
suf_buf, suf_len);
if (ret || (tmp_off - fin_off != suf_len)) {
PL_ERR("hal flash pre read failed\n");
goto clean;
}
}
erase_size = last_off - first_off;
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
if (ret) {
PL_ERR("flash erase failed ret %d\n", ret);
goto clean;
}
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
/* write first sector data that be erased*/
tmp_off = first_off;
if (pre_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
if (ret || (tmp_off - first_off) != pre_len) {
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
goto clean;
}
}
/* write the data need to write */
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
/* write last sector data that be erased */
if (suf_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
}
clean:
if (suf_buf) {
pl_free(suf_buf);
suf_buf = NULL;
}
if (pre_buf) {
pl_free(pre_buf);
pre_buf = NULL;
}
return ret;
}
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
{
uint32_t off_set = offset;
uint32_t len = 0;
int ret = 0;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
if (ret) {
PL_ERR("hal flash read failed\n");
return -1;
}
len = off_set - offset;
if (len != read_len) {
PL_ERR("read failed read_len is %d", (unsigned int)len);
return -1;
}
return 0;
}
int close_rsvd_part(int fd)
{
return 0;
}

View file

@ -0,0 +1,181 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include "hal/wifi.h"
#include "hal/soc/flash.h"
#include "plat_gen.h"
#include "plat_comm.h"
#define DEV_ID_LEN 6
#define MIN_SECTOR_SIZE 4096
#define MIN_SECTOR_SHIFT 12
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
{
int ret = 0;
if (*id_len < DEV_ID_LEN) {
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
*id_len = DEV_ID_LEN;
return -1;
}
ret = hal_wifi_get_mac_addr(NULL, dev_id);
if (ret) {
PL_ERR("get mac addr failed\n");
return -1;
}
*id_len = DEV_ID_LEN;
return 0;
}
//for rda5981x not support return 0 directly
int open_rsvd_part(int flag)
{
return 0;
}
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
{
int ret = 0;
uint32_t erase_size = 0;
uint32_t pre_len = 0;
uint8_t *pre_buf = NULL;
uint32_t suf_len = 0;
uint8_t *suf_buf = NULL;
uint32_t first_off = 0;
uint32_t last_off = 0;
uint32_t fin_off = offset + data_len;
uint32_t tmp_off = 0;
if (data_len != 0 && data == NULL) {
PL_ERR("bad param \n");
return -1;
}
if (data_len == 0) {
return 0;
}
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
pre_len = offset - first_off;
if (pre_len) {
pre_buf = (uint8_t *)pl_malloc(pre_len);
if (pre_buf == NULL) {
PL_ERR("malloc pre buf failed\n");
return -1;
}
//store the data of first block that will be erased
tmp_off = first_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
pre_buf, pre_len);
if (ret || (tmp_off - first_off != pre_len)) {
PL_ERR("hal flash pre read failed\n");
ret = -1;
goto clean;
}
}
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
suf_len = last_off - fin_off;
if (suf_len) {
suf_buf = (uint8_t *)pl_malloc(suf_len);
if (suf_buf == NULL) {
PL_ERR("malloc suf buf failed\n");
ret = -1;
goto clean;
}
//read the data of last block that will be erased
tmp_off = fin_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
suf_buf, suf_len);
if (ret || (tmp_off - fin_off != suf_len)) {
PL_ERR("hal flash pre read failed\n");
goto clean;
}
}
erase_size = last_off - first_off;
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
if (ret) {
PL_ERR("flash erase failed ret %d\n", ret);
goto clean;
}
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
/* write first sector data that be erased*/
tmp_off = first_off;
if (pre_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
if (ret || (tmp_off - first_off) != pre_len) {
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
goto clean;
}
}
/* write the data need to write */
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
/* write last sector data that be erased */
if (suf_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
}
clean:
if (suf_buf) {
pl_free(suf_buf);
suf_buf = NULL;
}
if (pre_buf) {
pl_free(pre_buf);
pre_buf = NULL;
}
return ret;
}
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
{
uint32_t off_set = offset;
uint32_t len = 0;
int ret = 0;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
if (ret) {
PL_ERR("hal flash read failed\n");
return -1;
}
len = off_set - offset;
if (len != read_len) {
PL_ERR("read failed read_len is %d", (unsigned int)len);
return -1;
}
return 0;
}
int close_rsvd_part(int fd)
{
return 0;
}

View file

@ -0,0 +1,181 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include "hal/wifi.h"
#include "hal/soc/flash.h"
#include "plat_gen.h"
#include "plat_comm.h"
#define DEV_ID_LEN 6
#define MIN_SECTOR_SIZE 4096
#define MIN_SECTOR_SHIFT 12
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
{
int ret = 0;
if (*id_len < DEV_ID_LEN) {
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
*id_len = DEV_ID_LEN;
return -1;
}
ret = hal_wifi_get_mac_addr(NULL, dev_id);
if (ret) {
PL_ERR("get mac addr failed\n");
return -1;
}
*id_len = DEV_ID_LEN;
return 0;
}
//for rda5981x not support return 0 directly
int open_rsvd_part(int flag)
{
return 0;
}
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
{
int ret = 0;
uint32_t erase_size = 0;
uint32_t pre_len = 0;
uint8_t *pre_buf = NULL;
uint32_t suf_len = 0;
uint8_t *suf_buf = NULL;
uint32_t first_off = 0;
uint32_t last_off = 0;
uint32_t fin_off = offset + data_len;
uint32_t tmp_off = 0;
if (data_len != 0 && data == NULL) {
PL_ERR("bad param \n");
return -1;
}
if (data_len == 0) {
return 0;
}
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
pre_len = offset - first_off;
if (pre_len) {
pre_buf = (uint8_t *)pl_malloc(pre_len);
if (pre_buf == NULL) {
PL_ERR("malloc pre buf failed\n");
return -1;
}
//store the data of first block that will be erased
tmp_off = first_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
pre_buf, pre_len);
if (ret || (tmp_off - first_off != pre_len)) {
PL_ERR("hal flash pre read failed\n");
ret = -1;
goto clean;
}
}
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
suf_len = last_off - fin_off;
if (suf_len) {
suf_buf = (uint8_t *)pl_malloc(suf_len);
if (suf_buf == NULL) {
PL_ERR("malloc suf buf failed\n");
ret = -1;
goto clean;
}
//read the data of last block that will be erased
tmp_off = fin_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
suf_buf, suf_len);
if (ret || (tmp_off - fin_off != suf_len)) {
PL_ERR("hal flash pre read failed\n");
goto clean;
}
}
erase_size = last_off - first_off;
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
if (ret) {
PL_ERR("flash erase failed ret %d\n", ret);
goto clean;
}
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
/* write first sector data that be erased*/
tmp_off = first_off;
if (pre_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
if (ret || (tmp_off - first_off) != pre_len) {
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
goto clean;
}
}
/* write the data need to write */
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
/* write last sector data that be erased */
if (suf_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
}
clean:
if (suf_buf) {
pl_free(suf_buf);
suf_buf = NULL;
}
if (pre_buf) {
pl_free(pre_buf);
pre_buf = NULL;
}
return ret;
}
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
{
uint32_t off_set = offset;
uint32_t len = 0;
int ret = 0;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
if (ret) {
PL_ERR("hal flash read failed\n");
return -1;
}
len = off_set - offset;
if (len != read_len) {
PL_ERR("read failed read_len is %d", (unsigned int)len);
return -1;
}
return 0;
}
int close_rsvd_part(int fd)
{
return 0;
}

View file

@ -0,0 +1,181 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include "hal/wifi.h"
#include "hal/soc/flash.h"
#include "plat_gen.h"
#include "plat_comm.h"
#define DEV_ID_LEN 6
#define MIN_SECTOR_SIZE 4096
#define MIN_SECTOR_SHIFT 12
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
{
int ret = 0;
if (*id_len < DEV_ID_LEN) {
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
*id_len = DEV_ID_LEN;
return -1;
}
ret = hal_wifi_get_mac_addr(NULL, dev_id);
if (ret) {
PL_ERR("get mac addr failed\n");
return -1;
}
*id_len = DEV_ID_LEN;
return 0;
}
//for rda5981x not support return 0 directly
int open_rsvd_part(int flag)
{
return 0;
}
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
{
int ret = 0;
uint32_t erase_size = 0;
uint32_t pre_len = 0;
uint8_t *pre_buf = NULL;
uint32_t suf_len = 0;
uint8_t *suf_buf = NULL;
uint32_t first_off = 0;
uint32_t last_off = 0;
uint32_t fin_off = offset + data_len;
uint32_t tmp_off = 0;
if (data_len != 0 && data == NULL) {
PL_ERR("bad param \n");
return -1;
}
if (data_len == 0) {
return 0;
}
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
pre_len = offset - first_off;
if (pre_len) {
pre_buf = (uint8_t *)pl_malloc(pre_len);
if (pre_buf == NULL) {
PL_ERR("malloc pre buf failed\n");
return -1;
}
//store the data of first block that will be erased
tmp_off = first_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
pre_buf, pre_len);
if (ret || (tmp_off - first_off != pre_len)) {
PL_ERR("hal flash pre read failed\n");
ret = -1;
goto clean;
}
}
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
suf_len = last_off - fin_off;
if (suf_len) {
suf_buf = (uint8_t *)pl_malloc(suf_len);
if (suf_buf == NULL) {
PL_ERR("malloc suf buf failed\n");
ret = -1;
goto clean;
}
//read the data of last block that will be erased
tmp_off = fin_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
suf_buf, suf_len);
if (ret || (tmp_off - fin_off != suf_len)) {
PL_ERR("hal flash pre read failed\n");
goto clean;
}
}
erase_size = last_off - first_off;
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
if (ret) {
PL_ERR("flash erase failed ret %d\n", ret);
goto clean;
}
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
/* write first sector data that be erased*/
tmp_off = first_off;
if (pre_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
if (ret || (tmp_off - first_off) != pre_len) {
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
goto clean;
}
}
/* write the data need to write */
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
/* write last sector data that be erased */
if (suf_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
}
clean:
if (suf_buf) {
pl_free(suf_buf);
suf_buf = NULL;
}
if (pre_buf) {
pl_free(pre_buf);
pre_buf = NULL;
}
return ret;
}
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
{
uint32_t off_set = offset;
uint32_t len = 0;
int ret = 0;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
if (ret) {
PL_ERR("hal flash read failed\n");
return -1;
}
len = off_set - offset;
if (len != read_len) {
PL_ERR("read failed read_len is %d", (unsigned int)len);
return -1;
}
return 0;
}
int close_rsvd_part(int fd)
{
return 0;
}

View file

@ -0,0 +1,46 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#ifndef _PLAT_DBG_H_
#define _PLAT_DBG_H_
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define _DEPRESS_UNUSED_WARNING(_x) do { (_x) = (_x); } while (0)
#define PLAT_TAG "PL_LOG"
#ifndef PLATFORM_ANDROID
#define PL_ERR(_f, _a ...) printf("PL ERR %s %d: "_f, __FUNCTION__, __LINE__, ##_a)
#else
#include <android/log.h>
#define LOG_ERR(...) __android_log_print( \
ANDROID_LOG_ERROR, \
PLAT_TAG, \
__VA_ARGS__)
#define LOG_INF(...) __android_log_print( \
ANDROID_LOG_INFO, \
PLAT_TAG, \
__VA_ARGS__)
#define PL_ERR(_f, _a ...) LOG_ERR("ERR %s %d: "_f, __FUNCTION__, __LINE__, ##_a)
#endif /* PLATFORM_ANDROID */
#ifdef CONFIG_PL_DBG
void dump_data(char *name, uint8_t *data, uint32_t len);
#ifndef PLATFORM_ANDROID
#define PL_INF(_f, _a ...) printf("PL ERR %s %d: "_f, __FUNCTION__, __LINE__, ##_a)
#else
#define PL_INF(_f, _a ...) LOG_INF("%s %d: "_f, __FUNCTION__, __LINE__, ##_a)
#endif /* PLATFORM_ANDROID */
#else
#define PL_INF(_f, _a ...)
#endif
#define pl_malloc malloc
#define pl_free free
#endif /* _PLAT_DBG_H_ */

View file

@ -0,0 +1,77 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#ifndef _PLAT_GEN_H_
#define _PLAT_GEN_H_
#include <stdint.h>
typedef enum {
RO_READ = 1,
RO_WRITE = 2
} rsvd_part_perm_t;
/*
* get device unique id
*
* param: out: dev_id: device uinque id
* in_out: id_len: device uinque id length
*
* return: 0: success
* -1: fail
*
*/
int get_dev_id(uint8_t *dev_id, uint32_t *id_len);
/*
* open the reserved partition that user has read and write permission
*
* parametr: in: flag: RO_READ: user only has read permission
* RO_WRITE: user only has write permission
* RO_READ | RO_WRITE: user has read and write permission
*
* return: the new file descriptor: success
* -1: fail
*/
int open_rsvd_part(int flag);
/*
* write reserved partition
*
* parametr: in: fd: file handle, can be ignored if no file system
* offset: the offset of the reserved partition
* data: the data need to write
* data_len: the length of the data need to write
*
* return: 0: success
* -1: fail
*/
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len);
/*
* read reserved partition
*
* parametr: in: fd: file handle, can be ignored if no file system
* offset: the offset of the reserved partition
* read_len: the length of the data need to read
* out: buffer: the data read from the reserved part
* return: 0: success
-1: fail
*/
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len);
/*
* close the file descriptor of reserved partition, if no file system support, return 0 directly
*
* parametr: in: fd: file descriptor of the reserved part
* return: 0: success
-1: fail
*/
int close_rsvd_part(int fd);
#endif /* _PLAT_GEN_H_ */

View file

@ -0,0 +1,282 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "plat_gen.h"
#include "plat_comm.h"
#define DEV_ID_LEN 36
#define FLASH_BLOCK_LEN 2048
#if CONFIG_SUDO_SUPPORT
#define PROV_DATA_PATH "/usr/.security/.dev_key"
#define DIR_NAME "/usr/.security"
#else
#ifdef PLATFORM_ANDROID
#define PROV_DATA_PATH "/sdcard/Android/data/.security/.dev_key"
#define DIR_NAME "/sdcard/Android/data/.security"
#else
#define PROV_DATA_PATH "./.security/.dev_key"
#define DIR_NAME "./.security"
#endif
#endif
#define FILE_NAME PROV_DATA_PATH
#define pl_memcpy memcpy
#define pl_memset memset
#define pl_malloc malloc
#define pl_free free
static int _init_rsvd_part()
{
//if no rserved partition just create km file
DIR *dir;
size_t file_len = 0;
uint8_t *flash_block = NULL;
int fd = 0;
int fret = 0;
int ret = 0;
dir = opendir((char *)DIR_NAME);
if (NULL == dir) {
fret = mkdir(DIR_NAME, S_IRWXU |
S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH);
if (fret < 0) {
PL_ERR("mkdir failed errno is %d\n", errno);
return -1;
}
#ifndef PLATFORM_ANDROID
if (chmod(DIR_NAME, S_IRWXU |
S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH)) {
PL_ERR("chmod failed errno is %d\n", errno);
closedir(dir);
return -1;
}
#endif
} else {
closedir(dir);
}
//file has already exist
if (access(FILE_NAME, F_OK) == 0) {
PL_INF("file has already exist\n");
return 0;
}
//create FLASH_BLOCK_LEN file
fd = open(FILE_NAME, O_CREAT|O_RDWR, S_IRWXU | S_IRGRP | S_IROTH);
if (fd == -1) {
PL_ERR("open file failed errno %d\n", errno);
return -1;
}
#ifndef PLATFORM_ANDROID
if (fchmod(fd, S_IRWXU | S_IRGRP | S_IROTH)) {
PL_ERR("file chmod failed errno is %d\n", errno);
close(fd);
return -1;
}
#endif
flash_block = (uint8_t *)pl_malloc(FLASH_BLOCK_LEN);
if (!flash_block) {
PL_ERR("malloc failed\n");
ret = -1;
goto clean;
}
pl_memset(flash_block, 0, FLASH_BLOCK_LEN);
//fix file length first FLASH_BLOCK_LEN for km
file_len = write(fd, flash_block, FLASH_BLOCK_LEN);
if (file_len != FLASH_BLOCK_LEN) {
PL_ERR("seek failed errno %d\n", errno);
ret = -1;
goto clean;
}
//fix file length last FLASH_BLICK_LEN for prov
file_len = write(fd, flash_block, FLASH_BLOCK_LEN);
if (file_len != FLASH_BLOCK_LEN) {
PL_ERR("seek failed errno %d\n", errno);
ret = -1;
goto clean;
}
clean:
close(fd);
if (flash_block) {
pl_free(flash_block);
flash_block = NULL;
}
return ret;
}
#if 0
#define cpuid(in,a,b,c,d)
asm("cpuid": "=a" (a), "=b" (b), "=c" (c), "=d" (d) : "a" (in));
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
{
int i = 0;
unsigned long eax, ebx, ecx, edx;
unsigned long maxi, unused;
if (*id_len < DEV_ID_LEN) {
PL_ERR("short buffer id len is %d\n", *id_len);
*id_len = DEV_ID_LEN;
return -1;
}
cpuid (0, maxi, unused, unused, unused);
maxi &= 0xffff;
if (maxi < 3) {
PL_ERR("not support get cpuid\n");
return -1;
}
cpuid (1, eax, ebx, ecx, edx);
dev_id[0] = (eax & 0xff000000) >> 24;
dev_id[1] = (eax & 0x00ff0000) >> 16;
dev_id[2] = (eax & 0x0000ff00) >> 8;
dev_id[3] = eax & 0x000000ff;
return 0;
}
#endif
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
{
int ret = 0;
if (*id_len < DEV_ID_LEN) {
PL_ERR("short buffer id len is %d\n", *id_len);
*id_len = DEV_ID_LEN;
return -1;
}
#if 0
size_t read_len = 0;
int fd = -1;
fd = open("/sys/class/dmi/id/product_uuid", O_RDONLY);
if (fd == -1) {
PL_ERR("open file failed errno %d\n", errno);
return -1;
}
lseek(fd, 0, SEEK_SET);
read_len = read(fd, dev_id, DEV_ID_LEN);
if (read_len != DEV_ID_LEN) {
PL_ERR("read failed errno %d\n", errno);
ret = -1;
goto clean;
}
*id_len = DEV_ID_LEN;
clean:
close(fd);
#else
char *product_uuid = "12345678-1234-5678-ABCD-CBA987654321";
pl_memcpy(dev_id, product_uuid, DEV_ID_LEN);
*id_len = DEV_ID_LEN;
#endif
return ret;
}
int open_rsvd_part(int flag)
{
int fd = -1;
int ret = 0;
//check if file has already exist
if (access(FILE_NAME, F_OK)) {
ret = _init_rsvd_part();
if (ret) {
PL_ERR("init rsvd part failed\n");
return -1;
}
}
if (RO_READ == flag) {
fd = open(PROV_DATA_PATH, O_RDONLY);;
} else if (RO_WRITE == flag) {
fd = open(PROV_DATA_PATH, O_WRONLY);
} else if ((RO_READ | RO_WRITE) == flag) {
fd = open(PROV_DATA_PATH, O_RDWR, S_IRUSR|S_IWUSR);
} else {
PL_ERR("not support flag\n");
return -1;
}
if (fd == -1) {
PL_ERR("open file failed errno %d\n", errno);
return -1;
}
return fd;
}
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
{
uint32_t write_len = 0;
if (fd < 0 || (data_len != 0 && data == NULL)) {
PL_ERR("bad param \n");
return -1;
}
if (data_len == 0) {
return 0;
}
lseek(fd, offset, SEEK_SET);
write_len = write(fd, data, data_len);
if (write_len != data_len) {
PL_ERR("write failed errno is %d\n", errno);
return -1;
}
return 0;
}
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
{
uint32_t real_read_len = 0;
if (fd < 0) {
PL_ERR("bad params fd\n");
return -1;
}
lseek(fd, offset, SEEK_SET);
real_read_len = read(fd, buffer, read_len);
if (real_read_len != read_len) {
PL_ERR("read failed real read len is %d\n", real_read_len);
return -1;
}
return 0;
}
int close_rsvd_part(int fd)
{
return close(fd);
}

View file

@ -0,0 +1,181 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include "hal/wifi.h"
#include "hal/soc/flash.h"
#include "plat_gen.h"
#include "plat_comm.h"
#define DEV_ID_LEN 6
#define MIN_SECTOR_SIZE 4096
#define MIN_SECTOR_SHIFT 12
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
{
int ret = 0;
if (*id_len < DEV_ID_LEN) {
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
*id_len = DEV_ID_LEN;
return -1;
}
ret = hal_wifi_get_mac_addr(NULL, dev_id);
if (ret) {
PL_ERR("get mac addr failed\n");
return -1;
}
*id_len = DEV_ID_LEN;
return 0;
}
//for mk3060 not support return 0 directly
int open_rsvd_part(int flag)
{
return 0;
}
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
{
int ret = 0;
uint32_t erase_size = 0;
uint32_t pre_len = 0;
uint8_t *pre_buf = NULL;
uint32_t suf_len = 0;
uint8_t *suf_buf = NULL;
uint32_t first_off = 0;
uint32_t last_off = 0;
uint32_t fin_off = offset + data_len;
uint32_t tmp_off = 0;
if (data_len != 0 && data == NULL) {
PL_ERR("bad param \n");
return -1;
}
if (data_len == 0) {
return 0;
}
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
pre_len = offset - first_off;
if (pre_len) {
pre_buf = (uint8_t *)pl_malloc(pre_len);
if (pre_buf == NULL) {
PL_ERR("malloc pre buf failed\n");
return -1;
}
//store the data of first block that will be erased
tmp_off = first_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
pre_buf, pre_len);
if (ret || (tmp_off - first_off != pre_len)) {
PL_ERR("hal flash pre read failed\n");
ret = -1;
goto clean;
}
}
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
suf_len = last_off - fin_off;
if (suf_len) {
suf_buf = (uint8_t *)pl_malloc(suf_len);
if (suf_buf == NULL) {
PL_ERR("malloc suf buf failed\n");
ret = -1;
goto clean;
}
//read the data of last block that will be erased
tmp_off = fin_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
suf_buf, suf_len);
if (ret || (tmp_off - fin_off != suf_len)) {
PL_ERR("hal flash pre read failed\n");
goto clean;
}
}
erase_size = last_off - first_off;
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
if (ret) {
PL_ERR("flash erase failed ret %d\n", ret);
goto clean;
}
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
/* write first sector data that be erased*/
tmp_off = first_off;
if (pre_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
if (ret || (tmp_off - first_off) != pre_len) {
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
goto clean;
}
}
/* write the data need to write */
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
/* write last sector data that be erased */
if (suf_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
}
clean:
if (suf_buf) {
pl_free(suf_buf);
suf_buf = NULL;
}
if (pre_buf) {
pl_free(pre_buf);
pre_buf = NULL;
}
return ret;
}
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
{
uint32_t off_set = offset;
uint32_t len = 0;
int ret = 0;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
if (ret) {
PL_ERR("hal flash read failed\n");
return -1;
}
len = off_set - offset;
if (len != read_len) {
PL_ERR("read failed read_len is %d", (unsigned int)len);
return -1;
}
return 0;
}
int close_rsvd_part(int fd)
{
return 0;
}

View file

@ -0,0 +1,181 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include "hal/wifi.h"
#include "hal/soc/flash.h"
#include "plat_gen.h"
#include "plat_comm.h"
#define DEV_ID_LEN 6
#define MIN_SECTOR_SIZE 4096
#define MIN_SECTOR_SHIFT 12
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
{
int ret = 0;
if (*id_len < DEV_ID_LEN) {
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
*id_len = DEV_ID_LEN;
return -1;
}
ret = hal_wifi_get_mac_addr(NULL, dev_id);
if (ret) {
PL_ERR("get mac addr failed\n");
return -1;
}
*id_len = DEV_ID_LEN;
return 0;
}
//for mk3080 not support return 0 directly
int open_rsvd_part(int flag)
{
return 0;
}
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
{
int ret = 0;
uint32_t erase_size = 0;
uint32_t pre_len = 0;
uint8_t *pre_buf = NULL;
uint32_t suf_len = 0;
uint8_t *suf_buf = NULL;
uint32_t first_off = 0;
uint32_t last_off = 0;
uint32_t fin_off = offset + data_len;
uint32_t tmp_off = 0;
if (data_len != 0 && data == NULL) {
PL_ERR("bad param \n");
return -1;
}
if (data_len == 0) {
return 0;
}
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
pre_len = offset - first_off;
if (pre_len) {
pre_buf = (uint8_t *)pl_malloc(pre_len);
if (pre_buf == NULL) {
PL_ERR("malloc pre buf failed\n");
return -1;
}
//store the data of first block that will be erased
tmp_off = first_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
pre_buf, pre_len);
if (ret || (tmp_off - first_off != pre_len)) {
PL_ERR("hal flash pre read failed\n");
ret = -1;
goto clean;
}
}
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
suf_len = last_off - fin_off;
if (suf_len) {
suf_buf = (uint8_t *)pl_malloc(suf_len);
if (suf_buf == NULL) {
PL_ERR("malloc suf buf failed\n");
ret = -1;
goto clean;
}
//read the data of last block that will be erased
tmp_off = fin_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
suf_buf, suf_len);
if (ret || (tmp_off - fin_off != suf_len)) {
PL_ERR("hal flash pre read failed\n");
goto clean;
}
}
erase_size = last_off - first_off;
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
if (ret) {
PL_ERR("flash erase failed ret %d\n", ret);
goto clean;
}
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
/* write first sector data that be erased*/
tmp_off = first_off;
if (pre_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
if (ret || (tmp_off - first_off) != pre_len) {
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
goto clean;
}
}
/* write the data need to write */
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
/* write last sector data that be erased */
if (suf_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
}
clean:
if (suf_buf) {
pl_free(suf_buf);
suf_buf = NULL;
}
if (pre_buf) {
pl_free(pre_buf);
pre_buf = NULL;
}
return ret;
}
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
{
uint32_t off_set = offset;
uint32_t len = 0;
int ret = 0;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
if (ret) {
PL_ERR("hal flash read failed\n");
return -1;
}
len = off_set - offset;
if (len != read_len) {
PL_ERR("read failed read_len is %d", (unsigned int)len);
return -1;
}
return 0;
}
int close_rsvd_part(int fd)
{
return 0;
}

View file

@ -0,0 +1,181 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include "hal/wifi.h"
#include "hal/soc/flash.h"
#include "plat_gen.h"
#include "plat_comm.h"
#define DEV_ID_LEN 6
#define MIN_SECTOR_SIZE 4096
#define MIN_SECTOR_SHIFT 12
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
{
int ret = 0;
if (*id_len < DEV_ID_LEN) {
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
*id_len = DEV_ID_LEN;
return -1;
}
ret = hal_wifi_get_mac_addr(NULL, dev_id);
if (ret) {
PL_ERR("get mac addr failed\n");
return -1;
}
*id_len = DEV_ID_LEN;
return 0;
}
//for mk3080 not support return 0 directly
int open_rsvd_part(int flag)
{
return 0;
}
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
{
int ret = 0;
uint32_t erase_size = 0;
uint32_t pre_len = 0;
uint8_t *pre_buf = NULL;
uint32_t suf_len = 0;
uint8_t *suf_buf = NULL;
uint32_t first_off = 0;
uint32_t last_off = 0;
uint32_t fin_off = offset + data_len;
uint32_t tmp_off = 0;
if (data_len != 0 && data == NULL) {
PL_ERR("bad param \n");
return -1;
}
if (data_len == 0) {
return 0;
}
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
pre_len = offset - first_off;
if (pre_len) {
pre_buf = (uint8_t *)pl_malloc(pre_len);
if (pre_buf == NULL) {
PL_ERR("malloc pre buf failed\n");
return -1;
}
//store the data of first block that will be erased
tmp_off = first_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
pre_buf, pre_len);
if (ret || (tmp_off - first_off != pre_len)) {
PL_ERR("hal flash pre read failed\n");
ret = -1;
goto clean;
}
}
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
suf_len = last_off - fin_off;
if (suf_len) {
suf_buf = (uint8_t *)pl_malloc(suf_len);
if (suf_buf == NULL) {
PL_ERR("malloc suf buf failed\n");
ret = -1;
goto clean;
}
//read the data of last block that will be erased
tmp_off = fin_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
suf_buf, suf_len);
if (ret || (tmp_off - fin_off != suf_len)) {
PL_ERR("hal flash pre read failed\n");
goto clean;
}
}
erase_size = last_off - first_off;
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
if (ret) {
PL_ERR("flash erase failed ret %d\n", ret);
goto clean;
}
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
/* write first sector data that be erased*/
tmp_off = first_off;
if (pre_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
if (ret || (tmp_off - first_off) != pre_len) {
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
goto clean;
}
}
/* write the data need to write */
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
/* write last sector data that be erased */
if (suf_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
}
clean:
if (suf_buf) {
pl_free(suf_buf);
suf_buf = NULL;
}
if (pre_buf) {
pl_free(pre_buf);
pre_buf = NULL;
}
return ret;
}
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
{
uint32_t off_set = offset;
uint32_t len = 0;
int ret = 0;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
if (ret) {
PL_ERR("hal flash read failed\n");
return -1;
}
len = off_set - offset;
if (len != read_len) {
PL_ERR("read failed read_len is %d", (unsigned int)len);
return -1;
}
return 0;
}
int close_rsvd_part(int fd)
{
return 0;
}

View file

@ -0,0 +1,13 @@
NAME := libplat_gen
GLOBAL_INCLUDES += include
ifeq ($(HOST_ARCH), linux)
HOST_NAME := $(HOST_ARCH)
else
HOST_NAME := $(shell echo $(CONFIG_SYSINFO_DEVICE_NAME)|tr A-Z a-z)
endif
$(NAME)_SOURCES := $(HOST_NAME)/plat_gen.c
$(NAME)_COMPONENTS := alicrypto

View file

@ -0,0 +1,181 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include "hal/wifi.h"
#include "hal/soc/flash.h"
#include "plat_gen.h"
#include "plat_comm.h"
#define DEV_ID_LEN 6
#define MIN_SECTOR_SIZE 4096
#define MIN_SECTOR_SHIFT 12
#define ROUNDUP(a, b) (((a) + ((b)-1)) & ~((b)-1))
#define ROUNDDOWN(a, b) ((a) & ~((b)-1))
int get_dev_id(uint8_t *dev_id, uint32_t *id_len)
{
int ret = 0;
if (*id_len < DEV_ID_LEN) {
PL_ERR("short buffer id len is %d\n", (unsigned int)*id_len);
*id_len = DEV_ID_LEN;
return -1;
}
ret = hal_wifi_get_mac_addr(NULL, dev_id);
if (ret) {
PL_ERR("get mac addr failed\n");
return -1;
}
*id_len = DEV_ID_LEN;
return 0;
}
//for rda5981x not support return 0 directly
int open_rsvd_part(int flag)
{
return 0;
}
int write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
{
int ret = 0;
uint32_t erase_size = 0;
uint32_t pre_len = 0;
uint8_t *pre_buf = NULL;
uint32_t suf_len = 0;
uint8_t *suf_buf = NULL;
uint32_t first_off = 0;
uint32_t last_off = 0;
uint32_t fin_off = offset + data_len;
uint32_t tmp_off = 0;
if (data_len != 0 && data == NULL) {
PL_ERR("bad param \n");
return -1;
}
if (data_len == 0) {
return 0;
}
first_off = ROUNDDOWN(offset, MIN_SECTOR_SIZE);
pre_len = offset - first_off;
if (pre_len) {
pre_buf = (uint8_t *)pl_malloc(pre_len);
if (pre_buf == NULL) {
PL_ERR("malloc pre buf failed\n");
return -1;
}
//store the data of first block that will be erased
tmp_off = first_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
pre_buf, pre_len);
if (ret || (tmp_off - first_off != pre_len)) {
PL_ERR("hal flash pre read failed\n");
ret = -1;
goto clean;
}
}
last_off = ROUNDUP(fin_off, MIN_SECTOR_SIZE);
suf_len = last_off - fin_off;
if (suf_len) {
suf_buf = (uint8_t *)pl_malloc(suf_len);
if (suf_buf == NULL) {
PL_ERR("malloc suf buf failed\n");
ret = -1;
goto clean;
}
//read the data of last block that will be erased
tmp_off = fin_off;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &tmp_off,
suf_buf, suf_len);
if (ret || (tmp_off - fin_off != suf_len)) {
PL_ERR("hal flash pre read failed\n");
goto clean;
}
}
erase_size = last_off - first_off;
ret = hal_flash_erase(HAL_PARTITION_PARAMETER_4, first_off, erase_size);
if (ret) {
PL_ERR("flash erase failed ret %d\n", ret);
goto clean;
}
PL_INF("pre_len is %d, suf len is %d, erase len is %d, first_off is %d\n",
(unsigned int)pre_len, (unsigned int)suf_len, (unsigned int)erase_size, (unsigned int)first_off);
/* write first sector data that be erased*/
tmp_off = first_off;
if (pre_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, pre_buf, pre_len);
if (ret || (tmp_off - first_off) != pre_len) {
PL_ERR("write pre buf failed %d, len %d\n", ret, (unsigned int)tmp_off);
goto clean;
}
}
/* write the data need to write */
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, data, data_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
/* write last sector data that be erased */
if (suf_len) {
ret = hal_flash_write(HAL_PARTITION_PARAMETER_4, &tmp_off, suf_buf, suf_len);
if (ret) {
PL_ERR("write pre buf failed %d\n", ret);
goto clean;
}
}
clean:
if (suf_buf) {
pl_free(suf_buf);
suf_buf = NULL;
}
if (pre_buf) {
pl_free(pre_buf);
pre_buf = NULL;
}
return ret;
}
int read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
{
uint32_t off_set = offset;
uint32_t len = 0;
int ret = 0;
ret = hal_flash_read(HAL_PARTITION_PARAMETER_4, &off_set, buffer, read_len);
if (ret) {
PL_ERR("hal flash read failed\n");
return -1;
}
len = off_set - offset;
if (len != read_len) {
PL_ERR("read failed read_len is %d", (unsigned int)len);
return -1;
}
return 0;
}
int close_rsvd_part(int fd)
{
return 0;
}

View file

@ -0,0 +1,5 @@
src = Glob('*.c')
component = aos_component('libkm', src)
component.add_comp_deps('security/irot/km/platform', 'security/alicrypto')
component.add_prebuilt_libs('lib/' + component.get_arch() + '/libkm.a')

View file

@ -0,0 +1,52 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef __CONFIG_H__
#define __CONFIG_H__
////////////////////////////////////////////////////////////////////////////////
#ifdef WIN32
#define snprintf _snprintf
#endif
////////////////////////////////////////////////////////////////////////////////
#define CHIP_DEBUG 0
////////////////////////////////////////////////////////////////////////////////
#define CHIP_TYPE_SECURE_MCU 1
#define CHIP_TYPE_SE_STD_CMD 2
#define CHIP_TYPE_SE_MTK_CMD 3
#define CHIP_CRYPTO_TYPE_3DES 1
#define CHIP_CRYPTO_TYPE_AES 2
#define CHIP_CRYPTO_TYPE_RSA 3
////////////////////////////////////////////////////////////////////////////////
#define CHIP_APDU_CMD_ADAPTER 1
#define CHIP_SEND_SELECT_COMMAND 1
#define CHIP_CONST_TEST_KEY 1
////////////////////////////////////////////////////////////////////////////////
#define CHIP_TYPE_CONFIG CHIP_TYPE_SE_STD_CMD
#define CHIP_CRYPTO_TYPE_CONFIG CHIP_CRYPTO_TYPE_3DES
////////////////////////////////////////////////////////////////////////////////
#if ((CHIP_TYPE_CONFIG != CHIP_TYPE_SECURE_MCU) && \
(CHIP_TYPE_CONFIG != CHIP_TYPE_SE_STD_CMD) && \
(CHIP_TYPE_CONFIG != CHIP_TYPE_SE_MTK_CMD))
#error("CHIP_TYPE_CONFIG error.");
#endif
#if ((CHIP_CRYPTO_TYPE_CONFIG != CHIP_CRYPTO_TYPE_3DES) && \
(CHIP_CRYPTO_TYPE_CONFIG != CHIP_CRYPTO_TYPE_AES) && \
(CHIP_CRYPTO_TYPE_CONFIG != CHIP_CRYPTO_TYPE_RSA))
#error("CHIP_CRYPTO_TYPE_CONFIG error.");
#endif
#endif

View file

@ -0,0 +1,40 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef __ERROR_CODE_H__
#define __ERROR_CODE_H__
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef enum
{
IROT_SUCCESS = 0, ///< The operation was successful.
IROT_ERROR_GENERIC = -1, ///< Non-specific casuse.
IROT_ERROR_BAD_PARAMETERS = -2, ///< Input parameters were invlid.
IROT_ERROR_SHORT_BUFFER =
-3, ///< The supplied buffer is too short for the output.
IROT_ERROR_EXCESS_DATA =
-4, ///< Too much data for the requested operation was passed.
IROT_ERROR_OUT_OF_MEMORY = -5, ///< System out of memory resources.
IROT_ERROR_COMMUNICATION = -7, ///< Communication error
IROT_ERROR_NOT_SUPPORTED =
-8, ///< The request operation is valid but is not supported in this
///< implementation.
IROT_ERROR_NOT_IMPLEMENTED =
-9, ///< The requested operation should exist but is not yet
///< implementation.
IROT_ERROR_TIMEOUT = -10, ///< Communication Timeout
IROT_ERROR_ITEM_NOT_FOUND = -11, ///< Id2 is not exist
} irot_result_t;
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,22 @@
/*
* Copyright (C) 2018 Alibaba Group Holding Limited
*/
#ifndef __ID_KEY_3DES_H__
#define __ID_KEY_3DES_H__
#if (CHIP_CONST_TEST_KEY)
const char *g_3des_id2 = "0102030405060708090A0B0C";
const uint8_t g_3des_key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 };
#else
const char *g_3des_id2 = "00AAAAAABBBBBB48B69CD200";
const uint8_t g_3des_key[] = {
0xBA, 0xA4, 0xCE, 0xE3, 0x1F, 0xFB, 0xF7, 0xDC,
0x1A, 0xD9, 0xD9, 0x58, 0xAB, 0x57, 0x7F, 0xFB,
};
#endif
#endif

View file

@ -0,0 +1,21 @@
/*
* Copyright (C) 2018 Alibaba Group Holding Limited
*/
#ifndef __ID_KEY_AES_H__
#define __ID_KEY_AES_H__
#if (CHIP_CONST_TEST_KEY)
const char *g_aes_id2 = "2122232425262728292A2B2C";
const uint8_t g_aes_key[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30 };
#else
const char *g_aes_id2 = "00AAAAAABBBBBBD4E805A600";
const uint8_t g_aes_key[] = { 0xC1, 0x67, 0x87, 0x84, 0x15, 0xA2, 0x92, 0x1D,
0x6D, 0x28, 0xBA, 0xA7, 0x1C, 0x99, 0x92, 0xB5 };
#endif
#endif

View file

@ -0,0 +1,87 @@
/*
* Copyright (C) 2018 Alibaba Group Holding Limited
*/
#ifndef __ID_KEY_RSA_H__
#define __ID_KEY_RSA_H__
const char *g_rsa_id2 = "5152535455565758595A5B5C";
const uint8_t g_rsa_n[] = {
0xAB, 0x25, 0xA0, 0x1A, 0x95, 0x0F, 0x8A, 0x04, 0x7E, 0xF9, 0x73, 0xEE,
0x68, 0xE3, 0x5D, 0xD8, 0x52, 0xCD, 0x47, 0xC2, 0x3C, 0xB7, 0x31, 0xAC,
0x26, 0xA1, 0x1B, 0x96, 0x10, 0x8B, 0x05, 0x7F, 0xFA, 0x74, 0xEF, 0x69,
0xE4, 0x5E, 0xD9, 0x53, 0xCE, 0x48, 0xC3, 0x3D, 0xB8, 0x32, 0xAD, 0x27,
0xA2, 0x1C, 0x97, 0x11, 0x8C, 0x06, 0x80, 0xFB, 0x75, 0xF0, 0x6A, 0xE5,
0x5F, 0xD9, 0x38, 0xD5, 0xC8, 0x4D, 0xD3, 0x58, 0xDE, 0x63, 0xE9, 0x6E,
0xF4, 0x79, 0xFF, 0x85, 0x0A, 0x90, 0x15, 0x9B, 0x20, 0xA6, 0x2B, 0xB1,
0x36, 0xBC, 0x41, 0xC7, 0x4C, 0xD2, 0x57, 0xDD, 0x62, 0xE8, 0x6D, 0xF3,
0x78, 0xFE, 0x84, 0x09, 0x8F, 0x14, 0x9A, 0x1F, 0xA5, 0x2A, 0xB0, 0x35,
0xBB, 0x40, 0xC6, 0x4B, 0xD1, 0x56, 0xDC, 0x61, 0xE7, 0x6C, 0xF2, 0x77,
0xFD, 0x83, 0x08, 0x8E, 0x86, 0x7C, 0x2E, 0xE1
};
const uint8_t g_rsa_e[] = { 0x01, 0x00, 0x01 };
const uint8_t g_rsa_d[] = {
0x93, 0xF7, 0x0F, 0x98, 0x6B, 0xCE, 0xE7, 0x70, 0x43, 0xA6, 0xBF, 0x48,
0x1B, 0x7E, 0x97, 0x1F, 0xF3, 0x56, 0x6E, 0xF7, 0xCB, 0x2E, 0x46, 0xCF,
0xA3, 0x06, 0x1E, 0xA7, 0x7A, 0xDD, 0xF6, 0x7F, 0x52, 0xB5, 0xCE, 0x57,
0x2A, 0x8D, 0xA6, 0x2F, 0x02, 0x65, 0x7E, 0x06, 0xDA, 0x3D, 0x55, 0xDE,
0xB2, 0x15, 0x2D, 0xB6, 0x89, 0xED, 0x05, 0x8E, 0x61, 0xC4, 0xDD, 0x66,
0x39, 0x9B, 0xBF, 0xBA, 0x0A, 0x67, 0x1A, 0xD2, 0x32, 0x8F, 0x42, 0xFA,
0x5A, 0xB7, 0x6B, 0x22, 0x82, 0xDF, 0x93, 0x4A, 0xAB, 0x07, 0xBB, 0x72,
0xD3, 0x2F, 0xE3, 0x9A, 0xFB, 0x58, 0x0B, 0xC3, 0x23, 0x80, 0x33, 0xEB,
0x4B, 0xA8, 0x5C, 0x13, 0x73, 0xD0, 0x84, 0x3B, 0x9B, 0xF8, 0xAC, 0x63,
0xC4, 0x20, 0xD4, 0x8B, 0xEC, 0x48, 0xFC, 0xB4, 0x14, 0x71, 0x24, 0xDC,
0x3C, 0x99, 0x4D, 0x04, 0xC8, 0x15, 0xF5, 0xC1
};
const uint8_t g_rsa_p[] = { 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0,
0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0,
0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0,
0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0,
0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0,
0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0,
0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0,
0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0x3E, 0xCD };
const uint8_t g_rsa_q[] = { 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1,
0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1,
0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1,
0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1,
0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1,
0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1,
0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1,
0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0x08, 0x65 };
const uint8_t g_rsa_dp[] = { 0x66, 0x44, 0xAE, 0xD0, 0x66, 0x44, 0xAE, 0xD0,
0x66, 0x44, 0xAE, 0xD0, 0x66, 0x44, 0xAE, 0xD0,
0x66, 0x44, 0xAE, 0xD0, 0x66, 0x44, 0xAE, 0xD0,
0x66, 0x44, 0xAE, 0xD0, 0x66, 0x44, 0xAE, 0xD0,
0x66, 0x44, 0xAE, 0xD0, 0x66, 0x44, 0xAE, 0xD0,
0x66, 0x44, 0xAE, 0xD0, 0x66, 0x44, 0xAE, 0xD0,
0x66, 0x44, 0xAE, 0xD0, 0x66, 0x44, 0xAE, 0xD0,
0x66, 0x44, 0xAE, 0xD0, 0x66, 0x44, 0x67, 0x4D };
const uint8_t g_rsa_dq[] = { 0x1A, 0xFC, 0x5E, 0x7D, 0x1A, 0xFC, 0x5E, 0x7D,
0x1A, 0xFC, 0x5E, 0x7D, 0x1A, 0xFC, 0x5E, 0x7D,
0x1A, 0xFC, 0x5E, 0x7D, 0x1A, 0xFC, 0x5E, 0x7D,
0x1A, 0xFC, 0x5E, 0x7D, 0x1A, 0xFC, 0x5E, 0x7D,
0x1A, 0xFC, 0x5E, 0x7D, 0x1A, 0xFC, 0x5E, 0x7D,
0x1A, 0xFC, 0x5E, 0x7D, 0x1A, 0xFC, 0x5E, 0x7D,
0x1A, 0xFC, 0x5E, 0x7D, 0x1A, 0xFC, 0x5E, 0x7D,
0x1A, 0xFC, 0x5E, 0x7D, 0x1A, 0xFC, 0x44, 0x95 };
const uint8_t g_rsa_qinv[] = { 0x44, 0x05, 0x16, 0x46, 0x08, 0x39, 0x19, 0xE1,
0x94, 0x18, 0x69, 0x17, 0x65, 0x8B, 0x2B, 0xB4,
0xD2, 0x9A, 0xA1, 0x88, 0xB7, 0xAE, 0x2F, 0x00,
0x64, 0x6A, 0xBC, 0xA1, 0x59, 0x56, 0xBE, 0x73,
0xEF, 0xC0, 0x01, 0x35, 0xF9, 0xF4, 0xAB, 0x85,
0x69, 0x07, 0x5B, 0x36, 0x9A, 0xCF, 0x28, 0x6F,
0x1F, 0xF3, 0x2C, 0xEF, 0xF5, 0xE2, 0x90, 0x82,
0xF5, 0xF8, 0x5D, 0x34, 0x5D, 0x2E, 0x26, 0x25 };
#endif

View file

@ -0,0 +1,297 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef __IROT_HAL_H__
#define __IROT_HAL_H__
#include <stdint.h>
#include "error_code.h"
#ifdef __cplusplus
extern "C"
{
#endif
#define KEY_ID_ID2 0x00 ///< Key is used by ID2.(3DES, AES or RSA)
typedef enum
{
CIPHER_TYPE_INVALID = 0x00,
CIPHER_TYPE_AES = 0x01,
CIPHER_TYPE_3DES = 0x03,
CIPHER_TYPE_SM4 = 0x04,
} cipher_t;
typedef enum
{
BLOCK_MODE_ECB = 0x00,
BLOCK_MODE_CBC = 0x01,
BLOCK_MODE_CTR = 0x02,
} block_mode_t;
typedef enum
{
SYM_PADDING_NOPADDING = 0x00,
SYM_PADDING_PKCS5 = 0x02,
SYM_PADDING_PKCS7 = 0x03,
} irot_sym_padding_t;
typedef enum
{
ASYM_PADDING_NOPADDING = 0x00,
ASYM_PADDING_PKCS1 = 0x01,
} irot_asym_padding_t;
typedef enum
{
MODE_DECRYPT = 0x00,
MODE_ENCRYPT = 0x01,
} crypto_mode_t;
typedef enum
{
ASYM_TYPE_RSA_MD5_PKCS1 = 0x00,
ASYM_TYPE_RSA_SHA1_PKCS1 = 0x01,
ASYM_TYPE_RSA_SHA256_PKCS1 = 0x02,
ASYM_TYPE_RSA_SHA384_PKCS1 = 0x03,
ASYM_TYPE_RSA_SHA512_PKCS1 = 0x04,
ASYM_TYPE_SM3_SM2 = 0x05,
ASYM_TYPE_ECDSA = 0x06,
} asym_sign_verify_t;
typedef enum
{
HASH_TYPE_SHA1 = 0x00,
HASH_TYPE_SHA224 = 0x01,
HASH_TYPE_SHA256 = 0x02,
HASH_TYPE_SHA384 = 0x03,
HASH_TYPE_SHA512 = 0x04,
HASH_TYPE_SM3 = 0x05,
} hash_t;
typedef struct _sym_crypto_param_t
{
block_mode_t block_mode; ///< block mode
irot_sym_padding_t padding_type; ///< padding type
crypto_mode_t mode; ///< mode(encrypt or decrypt)
} sym_crypto_param_t;
enum
{
KEY_TYPE_3DES = 0x01,
KEY_TYPE_AES = 0x02,
KEY_TYPE_SM4 = 0x03,
KEY_TYPE_RSA_PUBLIC = 0x04,
KEY_TYPE_RSA_PRIVATE = 0x05,
KEY_TYPE_RSA_CRT_PRIVATE = 0x06,
};
enum
{
LENGTH_DES3_2KEY = 16,
LENGTH_DES3_3KEY = 24,
};
enum
{
LENGTH_AES_128 = 16,
LENGTH_AES_192 = 24,
LENGTH_AES_256 = 32,
};
enum
{
LENGTH_RSA_1024 = 128,
};
// key object
typedef struct
{
struct
{
uint8_t key_object_type; ///< the key object type
} head;
struct
{
uint8_t buf[0x04]; ///< placeholder for key
} body;
} key_object;
typedef struct
{
struct
{
uint8_t key_object_type;
} head;
struct
{
uint8_t *key_value; ///< the key value
uint32_t key_len; ///< the key length(bytes)
} body;
} key_object_sym;
typedef struct
{
struct
{
uint8_t key_object_type;
} head;
struct
{
uint8_t *e; ///< public exponent
uint32_t e_len; ///< public exponent length(bytes)
uint8_t *n; ///< public modulus
uint32_t n_len; ///< public modulus length(bytes)
} body;
} key_object_rsa_public;
typedef struct
{
struct
{
uint8_t key_object_type;
} head;
struct
{
uint8_t *d; ///< private exponent
uint32_t d_len; ///< private exponent length(bytes)
uint8_t *n; ///< private modulus
uint32_t n_len; ///< private modulus length(bytes)
} body;
} key_object_rsa_private;
typedef struct
{
struct
{
uint8_t key_object_type;
} head;
struct
{
uint8_t *p; ///< 1st prime factor
uint8_t *q; ///< 2st prime factor
uint8_t *dp; ///< d % (p - 1)
uint8_t *dq; ///< d % (q - 1)
uint8_t *qinv; ///< (1/q) % p
uint32_t len; ///< the length for the 5 parameters must with the
///< same length(bytes)
} body;
} key_object_rsa_crt_private;
irot_result_t irot_hal_init(void);
/**
* @brief get the ID2 value, the length is 12 bytes with hex format.
*
* @param id2 output buffer.
* @param len input with the id2 buffer size, ouput the real id2 length.
*
* @return @see irot_result_t
*/
irot_result_t irot_hal_get_id2(uint8_t *id2, uint32_t *len);
/**
* @brief encrypt or decrypt the data with symmetric algorithms.
*
* @param key_obj if this key object is not null, then use this parameter as
* the key, else use the internal key identified by the key id parameter.
* @param key_id identify the internal key.
* @param in input data
* @param in_len input data length.
* @param out output buffer.
* @param out_len input with the output buffer size, ouput the real data
* length.
* @param crypto_param @see sym_crypto_param_t
*
* @return @see irot_result_t
*/
irot_result_t irot_hal_sym_crypto(key_object *key_obj, uint8_t key_id,
const uint8_t *iv, uint32_t iv_len,
const uint8_t *in, uint32_t in_len,
uint8_t *out, uint32_t *out_len,
sym_crypto_param_t *crypto_param);
/**
* @brief compute the signature result with the asymmetric algorithms.
*
* @param key_obj if this key object is not null, then use this parameter as
* the key, else use the internal key identified by the key id parameter.
* @param key_id identify the internal key.
* @param in input data
* @param in_len input data length.
* @param out output buffer.
* @param out_len input with the output buffer size, ouput the real data
* length.
* @param type @see asym_sign_verify_t
*
* @return @see irot_result_t
*/
irot_result_t irot_hal_asym_priv_sign(key_object *key_obj, uint8_t key_id,
const uint8_t *in, uint32_t in_len,
uint8_t *out, uint32_t *out_len,
asym_sign_verify_t type);
/**
* @brief decrypt the data with the asymmetric algorithms.
*
* @param key_obj if this key object is not null, then use this parameter as
* the key, else use the internal key identified by the key id parameter.
* @param key_id identify the internal key.
* @param in input data
* @param in_len input data length.
* @param out output buffer.
* @param out_len input with the output buffer size, ouput the real data
* length.
* @param padding @see asym_padding_t
*
* @return @see irot_result_t
*/
irot_result_t irot_hal_asym_priv_decrypt(key_object *key_obj,
uint8_t key_id, const uint8_t *in,
uint32_t in_len, uint8_t *out,
uint32_t * out_len,
irot_asym_padding_t padding);
/**
* @brief compute the hash result with the hash algorithms.
*
* @param in input data.
* @param in_len input data length.
* @param out output data buffer.
* @param out_len input with the output buffer size, ouput the real hash
* data length.
* @param type @see hast_t
*
* @return @see irot_result_t
*/
irot_result_t irot_hal_hash_sum(const uint8_t *in, uint32_t in_len,
uint8_t *out, uint32_t *out_len,
hash_t type);
/**
* @brief generate random number with the given length.
*
* @param buf output buffer.
* @param len the output length to be generated with random bytes.
*
* @return @see irot_result_t
*/
irot_result_t irot_hal_get_random(uint8_t *buf, uint32_t len);
/*
* @brief irot_hal release.
* @param handle.
*
* @return @see irot_result_t
*/
irot_result_t irot_hal_cleanup(void);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,55 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef __SE_DRIVER_H__
#define __SE_DRIVER_H__
#include <stdint.h>
#include "error_code.h"
#ifdef __cplusplus
extern "C"
#endif
/**
* @brief open session and connect to SE.
*
* @param handle
*
* @return @see irot_result_t
*/
irot_result_t
se_open_session(void **handle);
/**
* @brief transmit APDU to SE.
*
* @param handle
* @param cmd_apdu Command APDU(ISO7816-4).
* @param cmd_len Command APDU length
* @param rsp_buf response APDU buffer.
* @param rsp_len input with response APDU buffer length, output with real
* response APDU length (SW in last two bytes).
*
* @return
*/
irot_result_t se_transmit(void *handle, const uint8_t *cmd_apdu,
uint32_t cmd_len, uint8_t *rsp_buf,
uint32_t *rsp_len);
/**
* @brief close session and disconnect to SE.
*
* @param handle
*
* @return @see irot_result_t
*/
irot_result_t se_close_session(void *handle);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -0,0 +1,59 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <string.h>
#include <stdlib.h>
#include "chip_config.h"
#include "irot_hal.h"
#include "log/chiplog.h"
#if (CHIP_TYPE_CONFIG == CHIP_TYPE_SECURE_MCU)
irot_result_t irot_hal_init(void)
{
return IROT_SUCCESS;
}
irot_result_t irot_hal_cleanup(void)
{
return IROT_SUCCESS;
}
irot_result_t irot_hal_get_id2(uint8_t *id2, uint32_t *len)
{
chip_log("irot_hal_get_id2 not implemented !!!\n");
return IROT_ERROR_NOT_IMPLEMENTED;
}
irot_result_t irot_hal_sym_crypto(key_object *key_obj, uint8_t key_id,
const uint8_t *iv, uint32_t iv_len,
const uint8_t *in, uint32_t in_len,
uint8_t *out, uint32_t *out_len,
sym_crypto_param_t *crypto_param)
{
chip_log("irot_hal_sym_crypto not implemented !!!\n");
return IROT_ERROR_NOT_IMPLEMENTED;
}
#if (CHIP_CRYPTO_TYPE_CONFIG == CHIP_CRYPTO_TYPE_RSA)
irot_result_t irot_hal_asym_priv_sign(key_object *key_obj, uint8_t key_id,
const uint8_t *in, uint32_t in_len,
uint8_t *out, uint32_t *out_len,
asym_sign_verify_t type)
{
chip_log("irot_hal_asym_priv_sign not implemented !!!\n");
return IROT_ERROR_NOT_IMPLEMENTED;
}
irot_result_t irot_hal_asym_priv_decrypt(key_object *key_obj, uint8_t key_id,
const uint8_t *in, uint32_t in_len,
uint8_t *out, uint32_t *out_len,
irot_asym_padding_t padding)
{
chip_log("irot_hal_asym_priv_decrypt not implemented !!!\n");
return IROT_ERROR_NOT_IMPLEMENTED;
}
#endif
#endif

View file

@ -0,0 +1,119 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include <string.h>
#include "se_driver.h"
#include "log/chiplog.h"
#ifdef WIN32
#include <windows.h>
#include <winscard.h>
static const char * g_readerName = "OMNIKEY Smart Card Reader USB 0";
static SCARDCONTEXT g_scardContext;
static SCARDHANDLE g_hCard;
static const SCARD_IO_REQUEST *g_lpIoSendPci;
typedef struct
{
int counter;
} State;
irot_result_t se_open_session(void **handle)
{
LONG lRet;
DWORD dwPro;
State *pState;
lRet = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &g_scardContext);
if (lRet != SCARD_S_SUCCESS) {
return IROT_ERROR_COMMUNICATION;
}
dwPro = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1;
lRet =
SCardConnect(g_scardContext, g_readerName, SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &g_hCard, &dwPro);
if (lRet != SCARD_S_SUCCESS) {
return IROT_ERROR_COMMUNICATION;
}
switch (dwPro) {
case SCARD_PROTOCOL_T0: {
g_lpIoSendPci = SCARD_PCI_T0;
} break;
case SCARD_PROTOCOL_T1: {
g_lpIoSendPci = SCARD_PCI_T1;
} break;
default: {
g_lpIoSendPci = SCARD_PCI_RAW;
} break;
}
pState = (State *)malloc(sizeof(State));
pState->counter = 0;
*handle = pState;
return IROT_SUCCESS;
}
irot_result_t se_transmit(void *handle, const uint8_t *cmd_apdu,
uint32_t cmd_len, uint8_t *rsp_buf, uint32_t *rsp_len)
{
LONG lRet;
State *pState;
pState = (State *)handle;
pState->counter++;
printf("==> APDU Command Counter: %d\n", pState->counter);
lRet = SCardTransmit(g_hCard, g_lpIoSendPci, cmd_apdu, cmd_len, NULL,
rsp_buf, (LPDWORD)rsp_len);
if (lRet != SCARD_S_SUCCESS) {
return IROT_ERROR_COMMUNICATION;
} else {
return IROT_SUCCESS;
}
}
irot_result_t se_close_session(void *handle)
{
LONG lRet;
State *pState;
pState = (State *)handle;
free(pState);
lRet = SCardDisconnect(g_hCard, SCARD_UNPOWER_CARD);
if (lRet != SCARD_S_SUCCESS) {
return IROT_ERROR_COMMUNICATION;
}
lRet = SCardReleaseContext(g_scardContext);
if (lRet != SCARD_S_SUCCESS) {
return IROT_ERROR_COMMUNICATION;
}
return IROT_SUCCESS;
}
#else
irot_result_t se_open_session(void **handle)
{
return IROT_SUCCESS;
}
irot_result_t se_transmit(void *handle, const uint8_t *cmd_apdu,
const uint32_t cmd_len, uint8_t *rsp_buf,
uint32_t *rsp_len)
{
// implements SE transmit on your chip!
chip_log("se transmit not implemented !!!\n");
memset(rsp_buf, 0x00, *rsp_len);
*rsp_len = 0x10;
return IROT_SUCCESS;
}
irot_result_t se_close_session(void *handle)
{
return IROT_SUCCESS;
}
#endif

View file

@ -0,0 +1,19 @@
CHIPNAME = chip_template
NAME := se
LIBSE := .
$(NAME)_INCLUDES += $(LIBSE)/../include
$(NAME)_INCLUDES += $(LIBSE)/src
$(NAME)_INCLUDES += $(LIBSE)/chipset/$(CHIPNAME)/include
$(NAME)_INCLUDES += $(LIBSE)/chipset/$(CHIPNAME)
$(NAME)_SOURCES += \
$(LIBSE)/src/core/km_to_irot.c \
$(LIBSE)/src/core/std_se_adapter.c \
$(LIBSE)/src/core/mtk_se_adapter.c \
$(LIBSE)/src/log/chiplog.c \
$(LIBSE)/chipset/$(CHIPNAME)/irot_impl/irot_hal.c \
$(LIBSE)/chipset/$(CHIPNAME)/se_driver_impl/se_driver.c \

View file

@ -0,0 +1,220 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <string.h>
#include "chip_config.h"
#include "km.h"
#include "irot_hal.h"
#include "log/chiplog.h"
#define ID2_LEN 24
#define ID2_KEY_NAME "id2_key"
#define ID2_KEY_NAME_LEN (sizeof(ID2_KEY_NAME) - 1)
static void dump_chip_conf_info()
{
switch (CHIP_TYPE_CONFIG) {
case CHIP_TYPE_SECURE_MCU: {
chip_log_debug("CHIP_TYPE_CONFIG : %s\n",
"CHIP_TYPE_SECURE_MCU");
} break;
case CHIP_TYPE_SE_STD_CMD: {
chip_log_debug("CHIP_TYPE_CONFIG : %s\n",
"CHIP_TYPE_SE_STD_CMD");
} break;
case CHIP_TYPE_SE_MTK_CMD: {
chip_log_debug("CHIP_TYPE_CONFIG : %s\n",
"CHIP_TYPE_SE_MTK_CMD");
} break;
default: { } break; }
switch (CHIP_CRYPTO_TYPE_CONFIG) {
case CHIP_CRYPTO_TYPE_3DES: {
chip_log_debug("CHIP_CRYPTO_TYPE_CONFIG : %s\n",
"CHIP_CRYPTO_TYPE_3DES");
} break;
case CHIP_CRYPTO_TYPE_AES: {
chip_log_debug("CHIP_CRYPTO_TYPE_CONFIG : %s\n",
"CHIP_CRYPTO_TYPE_AES");
} break;
case CHIP_CRYPTO_TYPE_RSA: {
chip_log_debug("CHIP_CRYPTO_TYPE_CONFIG : %s\n",
"CHIP_CRYPTO_TYPE_RSA");
} break;
default: { } break; }
chip_log_debug("========================================\n");
}
uint32_t km_init()
{
irot_result_t ret;
dump_chip_conf_info();
ret = irot_hal_init();
if (ret == IROT_SUCCESS) {
return KM_SUCCESS;
} else {
return KM_ERR_GENERIC;
}
}
void km_cleanup()
{
irot_hal_cleanup();
}
uint32_t km_get_id2(uint8_t *id2, uint32_t *len)
{
irot_result_t ret;
ret = irot_hal_get_id2(id2, len);
if (ret == IROT_ERROR_ITEM_NOT_FOUND) {
return KM_ERR_ITEM_NOT_FOUND;
} else if (ret != IROT_SUCCESS) {
return KM_ERR_GENERIC;
} else {
return KM_SUCCESS;
}
}
static uint32_t check_km_key_name(const char *name, const uint32_t name_len)
{
if ((name == NULL) || (name_len != ID2_KEY_NAME_LEN)) {
return KM_ERR_BAD_PARAMS;
}
if (memcmp(ID2_KEY_NAME, name, ID2_KEY_NAME_LEN) != 0) {
return KM_ERR_BAD_PARAMS;
}
return KM_SUCCESS;
}
#if (CHIP_CRYPTO_TYPE_CONFIG == CHIP_CRYPTO_TYPE_3DES || \
CHIP_CRYPTO_TYPE_CONFIG == CHIP_CRYPTO_TYPE_AES)
uint32_t km_cipher(const char *name, const uint32_t name_len,
km_sym_param *cipher_params, const uint8_t *iv,
const uint32_t iv_len, uint8_t *src, size_t src_len,
uint8_t *dest, size_t *dest_len)
{
km_block_mode_type block_mode = cipher_params->cipher_param.block_mode;
km_padding_type padding_type = cipher_params->cipher_param.padding_type;
km_purpose_type purpose_type = cipher_params->cipher_param.purpose_type;
sym_crypto_param_t sym_crypto_param;
uint32_t out_len;
irot_result_t ret;
uint32_t kmret;
kmret = check_km_key_name(name, name_len);
if (kmret != KM_SUCCESS) {
return kmret;
}
if ((block_mode != KM_ECB) || (padding_type != KM_NO_PADDING)) {
return KM_ERR_BAD_PARAMS;
}
sym_crypto_param.block_mode = BLOCK_MODE_ECB;
sym_crypto_param.padding_type = SYM_PADDING_NOPADDING;
if (purpose_type == KM_PURPOSE_ENCRYPT) {
sym_crypto_param.mode = MODE_ENCRYPT;
} else if (purpose_type == KM_PURPOSE_DECRYPT) {
sym_crypto_param.mode = MODE_DECRYPT;
} else {
return KM_ERR_BAD_PARAMS;
}
out_len = *dest_len;
ret = irot_hal_sym_crypto(NULL, KEY_ID_ID2, NULL, 0, src, src_len, dest,
&out_len, &sym_crypto_param);
if (ret != IROT_SUCCESS) {
return KM_ERR_GENERIC;
}
*dest_len = out_len;
return KM_SUCCESS;
}
#endif
#if (CHIP_CRYPTO_TYPE_CONFIG == CHIP_CRYPTO_TYPE_RSA)
uint32_t km_sign(const char *name, const uint32_t name_len, void *sign_params,
const uint8_t *data, const size_t data_len, uint8_t *out,
size_t *out_len)
{
km_sign_param * kmsign_params = (km_sign_param *)sign_params;
uint32_t output_len;
asym_sign_verify_t asym_sign_verify_type;
irot_result_t ret;
uint32_t kmret;
kmret = check_km_key_name(name, name_len);
if (kmret != KM_SUCCESS) {
return kmret;
}
if (kmsign_params->padding_type != KM_PKCS1) {
return KM_ERR_BAD_PARAMS;
}
if (kmsign_params->digest_type == KM_SHA1) {
asym_sign_verify_type = ASYM_TYPE_RSA_SHA1_PKCS1;
} else if (kmsign_params->digest_type == KM_SHA256) {
asym_sign_verify_type = ASYM_TYPE_RSA_SHA256_PKCS1;
} else {
return KM_ERR_BAD_PARAMS;
}
output_len = *out_len;
ret = irot_hal_asym_priv_sign(NULL, 0, data, data_len, out, &output_len,
asym_sign_verify_type);
if (ret != IROT_SUCCESS) {
return KM_ERR_GENERIC;
}
*out_len = output_len;
return KM_SUCCESS;
}
uint32_t km_asym_decrypt(const char *name, const uint32_t name_len,
void *enc_params, const uint8_t *src,
const size_t src_len, uint8_t *dest, size_t *dest_len)
{
km_enc_param *kmenc_params = enc_params;
uint32_t out_len;
irot_result_t ret;
uint32_t kmret;
kmret = check_km_key_name(name, name_len);
if (kmret != KM_SUCCESS) {
return kmret;
}
if (kmenc_params->padding_type != KM_PKCS1) {
return KM_ERR_BAD_PARAMS;
}
out_len = *dest_len;
ret = irot_hal_asym_priv_decrypt(NULL, 0, src, src_len, dest, &out_len,
ASYM_PADDING_PKCS1);
if (ret != IROT_SUCCESS) {
return KM_ERR_GENERIC;
}
*dest_len = out_len;
return KM_SUCCESS;
}
#endif
uint32_t km_set_id2(uint8_t *id2, uint32_t len)
{
return KM_ERR_NOT_SUPPORTED;
}
uint32_t km_import_key(const char *name, const uint32_t name_len,
km_format_t format, const km_key_data_t *key_data,
const uint32_t key_data_len)
{
return KM_ERR_NOT_SUPPORTED;
}
uint32_t km_get_attestation(uint8_t *id, uint32_t *id_len)
{
return KM_ERR_NOT_SUPPORTED;
}

View file

@ -0,0 +1,415 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include <string.h>
#include "chip_config.h"
#include "irot_hal.h"
#include "se_driver.h"
#include "log/chiplog.h"
#if (CHIP_APDU_CMD_ADAPTER && (CHIP_TYPE_CONFIG == CHIP_TYPE_SE_MTK_CMD))
////////////////////////////////////////////////////////////////////////////////
static int crc16_ccit_false(const uint8_t *in_data, uint32_t in_len,
uint16_t init_value, uint8_t *pout)
{
uint16_t poly = init_value;
uint16_t wcrc = 0xFFFF;
uint32_t i;
int j;
for (i = 0; i < in_len; ++i) {
wcrc ^= (in_data[i] << 8);
for (j = 0; j < 8; ++j) {
if (wcrc & 0x8000) {
wcrc = (wcrc << 1) ^ poly;
} else {
wcrc <<= 1;
}
}
}
wcrc ^= 0xFFFF;
pout[0] = (uint8_t)(wcrc >> 8);
pout[1] = (uint8_t)(wcrc & 0xFF);
return 0;
}
static int pkcs5_pading(uint8_t *in_data, uint32_t in_len, uint32_t *out_len,
uint8_t block_len)
{
uint8_t fill_len;
uint8_t fill_value;
fill_len = block_len - (in_len % block_len);
fill_value = fill_len;
while (fill_len) {
in_data[in_len++] = fill_value;
fill_len--;
}
*out_len = in_len;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
#define SE_ID2_LENGTH 12
#define CRC_INIT_VALUE 0x1021
#define CONST_KEY_ID 0x12
enum
{
INDEX_CLA = 0x00,
INDEX_INS = 0x01,
INDEX_P1 = 0x02,
INDEX_P2 = 0x03,
INDEX_LC = 0x04,
INDEX_DATA = 0x05,
};
#define CMD_APDU_HEAD_LENGTH 0x05
#define RSP_APDU_SW_LENGTH 0x02
#define MAX_CMD_APDU_LENGTH (CMD_APDU_HEAD_LENGTH + 255 + 1)
#define MAX_RSP_APDU_LENGTH (0x100 + RSP_APDU_SW_LENGTH)
#define BLOCK_DATA_LENGTH 0xF0
static irot_result_t open_session(void **handle)
{
irot_result_t ret;
chip_log_debug("=> SE open session\n");
ret = se_open_session(handle);
chip_log_debug("<= SE open session, (ret = %d).\n", ret);
if (ret != IROT_SUCCESS) {
chip_log_debug("ERROR: SE open session.\n");
}
return ret;
}
static irot_result_t close_session(void *handle)
{
irot_result_t ret;
chip_log_debug("=> SE close session.\n");
ret = se_close_session(handle);
chip_log_debug("<= SE close session, (ret = %d).\n", ret);
if (ret != IROT_SUCCESS) {
chip_log_debug("ERROR: SE close session.\n");
}
return ret;
}
static irot_result_t apdu_transmit_wrap(void *handle, uint8_t *cmd_buf,
uint32_t cmd_len, uint8_t *rsp_buf,
uint32_t *rsp_len,
uint32_t max_expect_len)
{
irot_result_t ret;
uint32_t counter = 0;
// backup the buffer length
uint32_t buf_len = *rsp_len;
do {
// ensure exit the loop
counter++;
if (counter >= 3) {
ret = IROT_ERROR_GENERIC;
break;
}
chip_log_debug("======================================================="
"=========================\n");
chip_log_hex_data("Command APDU:", cmd_buf, cmd_len);
// reset the buffer length
*rsp_len = buf_len;
// call driver send the APDU command
chip_log_debug("=> SE transmit.\n");
ret = se_transmit(handle, cmd_buf, cmd_len, rsp_buf, rsp_len);
chip_log_debug("<= SE transmit, (ret = %d, rsp_len = %d).\n", ret,
*rsp_len);
if (ret != IROT_SUCCESS) {
chip_log_debug("ERROR: SE transmit.\n");
break;
}
chip_log_hex_data("Response APDU:", rsp_buf, *rsp_len);
chip_log_debug("======================================================="
"=========================\n");
// length error
if ((*rsp_len < RSP_APDU_SW_LENGTH) ||
(*rsp_len > MAX_RSP_APDU_LENGTH)) {
chip_log_debug("ERROR: response apdu length.\n");
ret = IROT_ERROR_GENERIC;
break;
}
// CASE: return 0x9F
if ((*rsp_len == 0x02) && (rsp_buf[*rsp_len - 2] == 0x9F)) {
chip_log_debug("response apdu with 0x9F.\n");
cmd_buf[INDEX_CLA] = 0x00;
cmd_buf[INDEX_INS] = 0xB2;
cmd_buf[INDEX_P1] = 0xFF;
cmd_buf[INDEX_P2] = 0xFF;
cmd_buf[INDEX_LC] = rsp_buf[*rsp_len - 1];
cmd_len = CMD_APDU_HEAD_LENGTH;
continue;
}
// return 0x9000 OK
else if ((rsp_buf[*rsp_len - 2] == 0x90) &&
(rsp_buf[*rsp_len - 1] == 0x00)) {
*rsp_len -= 0x02;
break;
}
// other error
else {
chip_log_debug("ERROR: response apdu data error.\n");
ret = IROT_ERROR_GENERIC;
break;
}
} while (1);
return ret;
}
static void *session_handle = NULL;
irot_result_t irot_hal_init()
{
irot_result_t ret;
ret = open_session(&session_handle);
if (ret != IROT_SUCCESS) {
goto EXIT;
}
EXIT:
return ret;
}
irot_result_t irot_hal_cleanup()
{
return close_session(session_handle);
}
static irot_result_t select_application(void *handle, uint8_t *cmd_buf,
uint8_t *rsp_buf, uint32_t *rsp_len)
{
irot_result_t ret;
// select command
cmd_buf[INDEX_CLA] = 0x00;
cmd_buf[INDEX_INS] = 0xA4;
cmd_buf[INDEX_P1] = 0x00;
cmd_buf[INDEX_P2] = 0x0C;
cmd_buf[INDEX_LC] = 0x02;
cmd_buf[INDEX_DATA] = 0x1D;
cmd_buf[INDEX_DATA + 1] = 0x02;
ret = apdu_transmit_wrap(handle, cmd_buf, CMD_APDU_HEAD_LENGTH + 0x02,
rsp_buf, rsp_len, 0x00);
return ret;
}
irot_result_t irot_hal_get_id2(uint8_t *id, uint32_t *len)
{
irot_result_t ret;
uint8_t cmd_buf[MAX_CMD_APDU_LENGTH];
uint8_t rsp_buf[MAX_RSP_APDU_LENGTH];
uint32_t rsp_len = sizeof(rsp_buf);
uint8_t crc_buf[0x02];
// select application
#if CHIP_SEND_SELECT_COMMAND
ret = select_application(session_handle, cmd_buf, rsp_buf, &rsp_len);
if (ret != IROT_SUCCESS) {
goto EXIT;
}
#endif
// get ID
memset(cmd_buf, 0x00, CMD_APDU_HEAD_LENGTH);
cmd_buf[INDEX_CLA] = 0x00;
cmd_buf[INDEX_INS] = 0xDC;
cmd_buf[INDEX_P1] = 0xFF;
cmd_buf[INDEX_P2] = 0xFF;
cmd_buf[INDEX_LC] = 0x03;
cmd_buf[INDEX_DATA + 0] = 0x52;
cmd_buf[INDEX_DATA + 1] = 0xE6;
cmd_buf[INDEX_DATA + 2] = 0x02;
rsp_len = sizeof(rsp_buf);
ret =
apdu_transmit_wrap(session_handle, cmd_buf, CMD_APDU_HEAD_LENGTH + 0x03,
rsp_buf, &rsp_len, 0x11);
if (ret != IROT_SUCCESS) {
goto EXIT;
}
// 3 bytes head
if (rsp_len != (0x03 + SE_ID2_LENGTH + 0x02)) {
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
// ID2 data
if (rsp_buf[2] != SE_ID2_LENGTH) {
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
// check crc
crc16_ccit_false(rsp_buf, rsp_len - 2, CRC_INIT_VALUE, crc_buf);
if ((rsp_buf[rsp_len - 2] != crc_buf[0]) ||
(rsp_buf[rsp_len - 1] != crc_buf[1])) {
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
rsp_len -= (0x03 + 0x02);
if (rsp_len > *len) {
ret = IROT_ERROR_GENERIC;
goto EXIT;
} else {
// | 2 TAG | 1 len |
memcpy(id, rsp_buf + 3, rsp_len);
*len = rsp_len;
}
EXIT:
return ret;
}
#define DES_BLOCK_LENGTH 0x08
#define DES_BLOCK_PADING_MASK 0xF8
irot_result_t irot_hal_sym_crypto(key_object *key_obj, uint8_t key_id,
const uint8_t *iv, uint32_t iv_len,
const uint8_t *in, uint32_t in_len,
uint8_t *out, uint32_t *out_len,
sym_crypto_param_t *crypto_param)
{
irot_result_t ret;
uint8_t cmd_buf[MAX_CMD_APDU_LENGTH];
uint8_t rsp_buf[MAX_RSP_APDU_LENGTH];
uint32_t rsp_len = sizeof(rsp_buf);
uint32_t copy_len = 0;
uint32_t offset;
uint32_t max_expect_len;
int result;
uint8_t crc_buf[0x02];
uint32_t input_data_len = in_len;
// uint8_t block_mode = crypto_param->block_mode;
// uint8_t padding_type = crypto_param->padding_type;
uint8_t mode = crypto_param->mode;
uint32_t out_buf_len = *out_len;
uint32_t out_offset = 0;
if ((in == NULL) || (in_len == 0x00) ||
((in_len % DES_BLOCK_LENGTH) != 0) || (in_len > BLOCK_DATA_LENGTH)) {
ret = IROT_ERROR_BAD_PARAMETERS;
goto EXIT;
}
// select application
#if ID2_SEND_SELECT_COMMAND
ret = select_application(session_handle, cmd_buf, rsp_buf, &rsp_len);
if (ret != IROT_SUCCESS) {
goto EXIT;
}
#endif
memset(cmd_buf, 0x00, CMD_APDU_HEAD_LENGTH);
// send data in loop, may be more than 1 block
while (in_len > 0) {
cmd_buf[INDEX_CLA] = 0x00;
cmd_buf[INDEX_INS] = 0xDC;
// fill P1,P2
offset = INDEX_P1;
cmd_buf[offset++] = 0xFF;
cmd_buf[offset++] = 0xFF;
// skip LC
offset++;
cmd_buf[offset++] = 0x51;
cmd_buf[offset++] = 0x00;
cmd_buf[offset++] = 0x01;
cmd_buf[offset++] = (mode == MODE_ENCRYPT) ? 0x51 : 0x52;
cmd_buf[offset++] = 0x02;
cmd_buf[offset++] = CONST_KEY_ID;
copy_len = (in_len > BLOCK_DATA_LENGTH) ? BLOCK_DATA_LENGTH : in_len;
cmd_buf[offset++] = (uint8_t)((copy_len >> 8) & 0xFF);
cmd_buf[offset++] = (uint8_t)(copy_len & 0xFF);
// data
memcpy(&cmd_buf[offset], in, copy_len);
in += copy_len;
in_len -= copy_len;
offset += copy_len;
// the length of LC
cmd_buf[INDEX_LC] = (offset - CMD_APDU_HEAD_LENGTH) + 0x02;
crc16_ccit_false(cmd_buf, offset, CRC_INIT_VALUE, cmd_buf + offset);
offset += 0x02;
rsp_len = sizeof(rsp_buf);
if (mode == MODE_ENCRYPT) {
max_expect_len =
(copy_len + DES_BLOCK_LENGTH) & DES_BLOCK_PADING_MASK;
max_expect_len += 0x02;
} else {
max_expect_len = (copy_len - 1);
max_expect_len += 0x02;
}
ret = apdu_transmit_wrap(session_handle, cmd_buf, offset, rsp_buf,
&rsp_len, max_expect_len);
if (ret != IROT_SUCCESS) {
goto EXIT;
}
// check crc
crc16_ccit_false(rsp_buf, rsp_len - 2, CRC_INIT_VALUE, crc_buf);
if ((rsp_buf[rsp_len - 2] != crc_buf[0]) ||
(rsp_buf[rsp_len - 1] != crc_buf[1])) {
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
rsp_len -= 0x02;
// output data
if (rsp_len > out_buf_len) {
ret = IROT_ERROR_GENERIC;
goto EXIT;
} else {
memcpy(out + out_offset, rsp_buf, rsp_len);
out_offset += rsp_len;
out_buf_len -= rsp_len;
}
}
*out_len = out_offset;
if (mode == MODE_ENCRYPT) {
if ((input_data_len + DES_BLOCK_LENGTH) != *out_len) {
chip_log_debug("sym crypto encrypt data length error\n");
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
*out_len -= DES_BLOCK_LENGTH;
} else {
result = pkcs5_pading(out, *out_len, out_len, DES_BLOCK_LENGTH);
if (result != 0) {
ret = IROT_ERROR_BAD_PARAMETERS;
goto EXIT;
}
if (input_data_len != *out_len) {
chip_log_debug(
"sym crypto decrypt pkcs5 pading data length error\n");
}
}
EXIT:
return ret;
}
#if (CHIP_CRYPTO_TYPE_CONFIG == CHIP_CRYPTO_TYPE_RSA)
#error("CHIP_CRYPTO_TYPE_CONFIG error");
#endif
#endif

View file

@ -0,0 +1,520 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include <string.h>
#include "chip_config.h"
#include "irot_hal.h"
#include "se_driver.h"
#include "log/chiplog.h"
#if (CHIP_APDU_CMD_ADAPTER && (CHIP_TYPE_CONFIG == CHIP_TYPE_SE_STD_CMD))
#define SE_ID2_LENGTH 12
typedef enum
{
SE_DIGEST_TYPE_SHA1 = 0x00,
SE_DIGEST_TYPE_SHA224 = 0x01,
SE_DIGEST_TYPE_SHA256 = 0x02,
SE_DIGEST_TYPE_SHA384 = 0x03,
SE_DIGEST_TYPE_SHA512 = 0x04,
SE_DIGEST_TYPE_SM3 = 0x05,
} digest_t;
////////////////////////////////////////////////////////////////////////////////
typedef enum
{
MODE_SYMM_ENCRYPT = 0x51,
MODE_SYMM_DECRYPT = 0x52,
MODE_SYMM_COMPUTE_MAC = 0x53,
MODE_SYMM_VERIFY_MAC = 0x54,
} sym_mode_t;
typedef enum
{
TYPE_SYMM_DES_CBC_NOPADDING = 0x00,
TYPE_SYMM_DES_ECB_NOPADDING = 0x01,
TYPE_SYMM_AES_CBC_NOPADDING = 0x02,
TYPE_SYMM_AES_ECB_NOPADDING = 0x03,
TYPE_SYMM_DES_CBC_ISO9797_M1 = 0x04,
TYPE_SYMM_DES_CBC_ISO9797_M2 = 0x05,
TYPE_SYMM_AES_CBC_ISO9797_M1 = 0x06,
TYPE_SYMM_AES_CBC_ISO9797_M2 = 0x07,
TYPE_SYMM_SM4_CBC_NOPADDING = 0x10,
TYPE_SYMM_SM4_ECB_NOPADDING = 0x11,
TYPE_SYMM_SM7_CBC_NOPADDING = 0x12,
TYPE_SYMM_SM7_ECB_NOPADDING = 0x13,
TYPE_SYMM_SM4_CBC_ISO9797_M1 = 0x14,
TYPE_SYMM_SM4_CBC_ISO9797_M2 = 0x15,
TYPE_SYMM_SM7_CBC_ISO9797_M1 = 0x16,
TYPE_SYMM_SM7_CBC_ISO9797_M2 = 0x17,
} sym_cipher_t;
////////////////////////////////////////////////////////////////////////////////
typedef enum
{
MODE_ASYMM_ENCRYPT = 0x51,
MODE_ASYMM_DECRYPT = 0x52,
MODE_ASYMM_SIGN = 0x53,
MODE_ASYMM_VERIFY = 0x54,
} asym_mode_t;
typedef enum
{
TYPE_ASYMM_RSA_NOPADDING = 0x00,
TYPE_ASYMM_RSA_PKCS1 = 0x01,
TYPE_ASYMM_RSA_SHA1_PKCS1 = 0x01,
TYPE_ASYMM_RSA_SHA256_PKCS1 = 0x02,
TYPE_ASYMM_RSA_SHA384_PKCS1 = 0x03,
TYPE_ASYMM_RSA_SHA512_PKCS1 = 0x04,
TYPE_ASYMM_SM2_SM3 = 0x05,
TYPE_ASYMM_ECDSA = 0x06,
} asym_cipher_t;
enum
{
CLA_VALUE = 0x80,
};
enum
{
INDEX_CLA = 0x00,
INDEX_INS = 0x01,
INDEX_P1 = 0x02,
INDEX_P2 = 0x03,
INDEX_LC = 0x04,
INDEX_DATA = 0x05,
};
enum
{
INS_GET_ID = 0xF8,
INS_SYMMTRIC_ENCRYPT = 0xF6,
INS_ASYMMTRIC_ENCRYPT = 0xF4,
INS_GET_CHALLENGE = 0x84,
INS_COMPUTE_DIGEST = 0xF0,
INS_GET_RESPONSE = 0xC0,
};
#define CMD_APDU_HEAD_LENGTH 0x05
#define RSP_APDU_SW_LENGTH 0x02
#define MAX_CMD_APDU_LENGTH (CMD_APDU_HEAD_LENGTH + 255 + 1)
#define MAX_RSP_APDU_LENGTH (0x100 + RSP_APDU_SW_LENGTH)
#define P2_NOT_LAST_BLOCK 0x00
#define P2_LAST_BLOCK 0x01
#define BLOCK_DATA_LENGTH 0xF0
#define RSA_BLOCK_LENGTH 0x80
const static uint8_t ID2_APPLET_AID[] = { 0xA0, 0x00, 0x00, 0x00, 0x41,
0x6C, 0x69, 0x59, 0x75, 0x6E,
0x2E, 0x49, 0x44, 0x32 };
static irot_result_t open_session(void **handle)
{
irot_result_t ret;
chip_log_debug("=> SE open session\n");
ret = se_open_session(handle);
chip_log_debug("<= SE open session, (ret = %d).\n", ret);
if (ret != IROT_SUCCESS) {
chip_log_error("ERROR: SE open session.\n");
}
return ret;
}
static irot_result_t close_session(void *handle)
{
irot_result_t ret;
chip_log_debug("=> SE close session.\n");
ret = se_close_session(handle);
chip_log_debug("<= SE close session, (ret = %d).\n", ret);
if (ret != IROT_SUCCESS) {
chip_log_error("ERROR: SE close session.\n");
}
return ret;
}
static irot_result_t apdu_transmit_wrap(void *handle, uint8_t *cmd_buf,
uint32_t cmd_len, uint8_t *rsp_buf,
uint32_t *rsp_len)
{
irot_result_t ret;
uint32_t counter = 0;
// backup the buffer length
uint32_t buf_len = *rsp_len;
do {
// ensure exit the loop
counter++;
if (counter >= 3) {
ret = IROT_ERROR_GENERIC;
break;
}
chip_log_debug("======================================================="
"=========================\n");
chip_log_hex_data("Command APDU:", cmd_buf, cmd_len);
// reset the buffer length
*rsp_len = buf_len;
// call driver send the APDU command
chip_log_debug("=> SE transmit.\n");
ret = se_transmit(handle, cmd_buf, cmd_len, rsp_buf, rsp_len);
chip_log_debug("<= SE transmit, (ret = %d, rsp_len = %d).\n", ret,
*rsp_len);
if (ret != IROT_SUCCESS) {
chip_log_error("ERROR: SE transmit.\n");
break;
}
chip_log_hex_data("Response APDU:", rsp_buf, *rsp_len);
chip_log_debug("======================================================="
"=========================\n");
// length error
if ((*rsp_len < RSP_APDU_SW_LENGTH) ||
(*rsp_len > MAX_RSP_APDU_LENGTH)) {
chip_log_error("ERROR: response apdu length.\n");
ret = IROT_ERROR_GENERIC;
break;
}
// CASE: T=0 protocol return 0x61
if ((*rsp_len == 0x02) && (rsp_buf[*rsp_len - 2] == 0x61)) {
chip_log_debug("response apdu with 0x61.\n");
cmd_buf[INDEX_CLA] = 0x00;
cmd_buf[INDEX_INS] = INS_GET_RESPONSE;
cmd_buf[INDEX_P1] = 0x00;
cmd_buf[INDEX_P2] = 0x00;
cmd_buf[INDEX_LC] = rsp_buf[*rsp_len - 1];
cmd_len = CMD_APDU_HEAD_LENGTH;
continue;
}
// CASE: T=0 protocol return 0x6C
else if ((*rsp_len == 0x02) && (rsp_buf[*rsp_len - 2] == 0x6C)) {
chip_log_debug("response apdu with 0x6C.\n");
cmd_buf[INDEX_LC] = rsp_buf[*rsp_len - 1];
cmd_len = CMD_APDU_HEAD_LENGTH;
continue;
}
// return 0x9000 OK
else if ((rsp_buf[*rsp_len - 2] == 0x90) &&
(rsp_buf[*rsp_len - 1] == 0x00)) {
*rsp_len -= 0x02;
break;
}
// other error
else {
chip_log_error("ERROR: response apdu data error.\n");
ret = IROT_ERROR_GENERIC;
break;
}
} while (1);
return ret;
}
static irot_result_t select_application(void *handle)
{
irot_result_t ret;
uint8_t cmd_buf[MAX_CMD_APDU_LENGTH];
uint8_t rsp_buf[MAX_RSP_APDU_LENGTH];
uint32_t rsp_len = sizeof(rsp_buf);
// select command
cmd_buf[INDEX_CLA] = 0x00;
cmd_buf[INDEX_INS] = 0xA4;
cmd_buf[INDEX_P1] = 0x04;
cmd_buf[INDEX_P2] = 0x00;
cmd_buf[INDEX_LC] = sizeof(ID2_APPLET_AID);
memcpy(cmd_buf + INDEX_DATA, ID2_APPLET_AID, sizeof(ID2_APPLET_AID));
ret = apdu_transmit_wrap(handle, cmd_buf,
CMD_APDU_HEAD_LENGTH + sizeof(ID2_APPLET_AID),
rsp_buf, &rsp_len);
return ret;
}
static void *session_handle = NULL;
irot_result_t irot_hal_init()
{
irot_result_t ret;
ret = open_session(&session_handle);
if (ret != IROT_SUCCESS) {
goto EXIT;
}
// select application
#if CHIP_SEND_SELECT_COMMAND
ret = select_application(session_handle);
if (ret != IROT_SUCCESS) {
goto EXIT;
}
#endif
EXIT:
return ret;
}
irot_result_t irot_hal_cleanup()
{
return close_session(session_handle);
}
irot_result_t irot_hal_get_id2(uint8_t *id, uint32_t *len)
{
irot_result_t ret;
uint8_t cmd_buf[MAX_CMD_APDU_LENGTH];
uint8_t rsp_buf[MAX_RSP_APDU_LENGTH];
uint32_t rsp_len = sizeof(rsp_buf);
// get ID
memset(cmd_buf, 0x00, CMD_APDU_HEAD_LENGTH);
cmd_buf[INDEX_CLA] = CLA_VALUE;
cmd_buf[INDEX_INS] = INS_GET_ID;
cmd_buf[INDEX_LC] = 0x00;
rsp_len = sizeof(rsp_buf);
ret = apdu_transmit_wrap(session_handle, cmd_buf, CMD_APDU_HEAD_LENGTH,
rsp_buf, &rsp_len);
if (ret != IROT_SUCCESS) {
goto EXIT;
}
// 3 bytes head
if (rsp_len != (0x03 + SE_ID2_LENGTH)) {
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
// ID2 data
if (rsp_buf[2] != SE_ID2_LENGTH) {
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
rsp_len -= 0x03;
if (rsp_len > *len) {
ret = IROT_ERROR_GENERIC;
goto EXIT;
} else {
// | 2 TAG | 1 len |
memcpy(id, rsp_buf + 3, rsp_len);
*len = rsp_len;
}
EXIT:
return ret;
}
irot_result_t irot_hal_sym_crypto(key_object *key_obj, uint8_t key_id,
const uint8_t *iv, uint32_t iv_len,
const uint8_t *in, uint32_t in_len,
uint8_t *out, uint32_t *out_len,
sym_crypto_param_t *crypto_param)
{
irot_result_t ret;
uint8_t cmd_buf[MAX_CMD_APDU_LENGTH];
uint8_t rsp_buf[MAX_RSP_APDU_LENGTH];
uint32_t rsp_len = sizeof(rsp_buf);
uint8_t block_num = 0;
uint32_t copy_len = 0;
uint32_t offset;
// uint8_t block_mode = crypto_param->block_mode;
// uint8_t padding_type = crypto_param->padding_type;
uint8_t mode = crypto_param->mode;
uint32_t out_buf_len = *out_len;
uint32_t out_offset = 0;
memset(cmd_buf, 0x00, CMD_APDU_HEAD_LENGTH);
// send data in loop, may be more than 1 block
while (in_len > 0) {
cmd_buf[INDEX_CLA] = CLA_VALUE;
cmd_buf[INDEX_INS] = INS_SYMMTRIC_ENCRYPT;
// fill P1,P2
offset = INDEX_P1;
cmd_buf[offset++] = block_num;
cmd_buf[offset++] = P2_LAST_BLOCK;
// skip LC
offset++;
// extra 5 bytes in the first block
if (block_num == 0x00) {
cmd_buf[offset++] =
mode == MODE_ENCRYPT ? MODE_SYMM_ENCRYPT : MODE_SYMM_DECRYPT;
#if (CHIP_CRYPTO_TYPE_CONFIG == CHIP_CRYPTO_TYPE_3DES)
cmd_buf[offset++] = TYPE_SYMM_DES_ECB_NOPADDING;
#elif (CHIP_CRYPTO_TYPE_CONFIG == CHIP_CRYPTO_TYPE_AES)
cmd_buf[offset++] = TYPE_SYMM_AES_ECB_NOPADDING;
#else
#endif
cmd_buf[offset++] = key_id;
// data total length
cmd_buf[offset++] = (uint8_t)((in_len >> 8) & 0xFF);
cmd_buf[offset++] = (uint8_t)(in_len & 0xFF);
}
copy_len = (in_len > BLOCK_DATA_LENGTH) ? BLOCK_DATA_LENGTH : in_len;
// data
memcpy(&cmd_buf[offset], in, copy_len);
in += copy_len;
in_len -= copy_len;
offset += copy_len;
// the length of LC
cmd_buf[INDEX_LC] = (offset - CMD_APDU_HEAD_LENGTH);
rsp_len = sizeof(rsp_buf);
ret = apdu_transmit_wrap(session_handle, cmd_buf, offset, rsp_buf,
&rsp_len);
if (ret != IROT_SUCCESS) {
goto EXIT;
}
// fill output data
if (rsp_len > out_buf_len) {
ret = IROT_ERROR_GENERIC;
goto EXIT;
} else {
memcpy(out + out_offset, rsp_buf, rsp_len);
out_offset += rsp_len;
out_buf_len -= rsp_len;
}
block_num += 1;
}
*out_len = out_offset;
EXIT:
return ret;
}
#if (CHIP_CRYPTO_TYPE_CONFIG == CHIP_CRYPTO_TYPE_RSA)
static irot_result_t asymmetric_crypto(uint8_t mode, uint8_t cipher_type,
uint8_t key_id, const uint8_t *in_data,
uint32_t in_len, uint8_t *out_buf,
uint32_t *out_len)
{
irot_result_t ret;
uint8_t cmd_buf[MAX_CMD_APDU_LENGTH];
uint8_t rsp_buf[MAX_RSP_APDU_LENGTH];
uint32_t rsp_len = sizeof(rsp_buf);
uint8_t block_num = 0;
uint32_t copy_len = 0;
uint32_t offset;
uint32_t out_buf_len = *out_len;
uint32_t out_offset = 0;
memset(cmd_buf, 0x00, CMD_APDU_HEAD_LENGTH);
// send data in loop, may be more than 1 block
while (in_len > 0) {
cmd_buf[INDEX_CLA] = CLA_VALUE;
cmd_buf[INDEX_INS] = INS_ASYMMTRIC_ENCRYPT;
// fill P1,P2
offset = INDEX_P1;
cmd_buf[offset++] = block_num;
cmd_buf[offset++] =
(in_len <= RSA_BLOCK_LENGTH) ? P2_LAST_BLOCK : P2_NOT_LAST_BLOCK;
// skip LC
offset++;
// include extra 5 bytes in the first block
if (block_num == 0x00) {
cmd_buf[offset++] = mode;
cmd_buf[offset++] = cipher_type;
cmd_buf[offset++] = key_id;
// total length
cmd_buf[offset++] = (uint8_t)((in_len >> 8) & 0xFF);
cmd_buf[offset++] = (uint8_t)(in_len & 0xFF);
}
copy_len = (in_len > RSA_BLOCK_LENGTH) ? RSA_BLOCK_LENGTH : in_len;
// data
memcpy(&cmd_buf[offset], in_data, copy_len);
in_data += copy_len;
in_len -= copy_len;
offset += copy_len;
// the length of LC
cmd_buf[INDEX_LC] = (offset - CMD_APDU_HEAD_LENGTH);
rsp_len = sizeof(rsp_buf);
ret = apdu_transmit_wrap(session_handle, cmd_buf, offset, rsp_buf,
&rsp_len);
if (ret != IROT_SUCCESS) {
goto EXIT;
}
// fill output data
if (rsp_len > out_buf_len) {
ret = IROT_ERROR_GENERIC;
goto EXIT;
} else {
memcpy(out_buf + out_offset, rsp_buf, rsp_len);
out_offset += rsp_len;
out_buf_len -= rsp_len;
}
block_num += 1;
}
*out_len = out_offset;
EXIT:
return ret;
}
irot_result_t irot_hal_asym_priv_sign(key_object *key_obj, uint8_t key_id,
const uint8_t *in, uint32_t in_len,
uint8_t *sign, uint32_t *sign_len,
asym_sign_verify_t type)
{
irot_result_t ret;
uint8_t mode;
uint8_t cipher_type;
mode = MODE_ASYMM_SIGN;
switch (type) {
case ASYM_TYPE_RSA_SHA1_PKCS1: {
cipher_type = TYPE_ASYMM_RSA_SHA1_PKCS1;
} break;
case ASYM_TYPE_RSA_SHA256_PKCS1: {
cipher_type = TYPE_ASYMM_RSA_SHA256_PKCS1;
} break;
default: {
return IROT_ERROR_BAD_PARAMETERS;
} break;
}
ret =
asymmetric_crypto(mode, cipher_type, key_id, in, in_len, sign, sign_len);
return ret;
}
irot_result_t irot_hal_asym_priv_decrypt(key_object *key_obj, uint8_t key_id,
const uint8_t *in, uint32_t in_len,
uint8_t *out, uint32_t *out_len,
irot_asym_padding_t padding)
{
irot_result_t ret;
uint8_t mode;
uint8_t cipher_type;
mode = MODE_ASYMM_DECRYPT;
if (padding != ASYM_PADDING_PKCS1) {
return IROT_ERROR_BAD_PARAMETERS;
}
cipher_type = TYPE_ASYMM_RSA_PKCS1;
ret =
asymmetric_crypto(mode, cipher_type, key_id, in, in_len, out, out_len);
return ret;
}
#endif
#endif

View file

@ -0,0 +1,59 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "chip_config.h"
#include "chiplog.h"
void chip_log(const char *fmt, ...)
{
va_list args;
printf(" ID2=> ");
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
#if (CHIP_DEBUG)
#define COL_SIZE 0x10
void chip_log_hex_dump(const char *name, const uint8_t *in_data,
uint32_t in_len)
{
uint32_t i;
char buf[80];
int pos;
if (name) {
chip_log_debug("%s [length = 0x%04X]\n", name, in_len);
}
i = 0;
pos = 0;
memset(buf, 0x00, sizeof(buf));
while (i < in_len) {
pos += snprintf(buf + pos, sizeof(buf) - pos, "%02X ", in_data[i]);
i++;
if (i % COL_SIZE == 0x00) {
pos += snprintf(buf + pos, sizeof(buf) - pos, "\n");
chip_log_debug("%s", buf);
pos = 0;
} else if (i % COL_SIZE == (COL_SIZE >> 1)) {
pos += snprintf(buf + pos, sizeof(buf) - pos, " ");
} else {
}
}
if (pos > 0) {
if (i % COL_SIZE == 0x00) {
chip_log_debug("%s", buf);
} else {
chip_log_debug("%s\n", buf);
}
}
}
#endif

View file

@ -0,0 +1,26 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef __CHIP_LOG_H__
#define __CHIP_LOG_H__
#include "chip_config.h"
void chip_log(const char *fmt, ...);
void chip_log_hex_dump(const char *name, const uint8_t *in_data,
uint32_t in_len);
#if (CHIP_DEBUG)
#define chip_log_debug(fmt, args...) chip_log(fmt, ##args)
#define chip_log_hex_data(name, in_data, in_len) \
chip_log_hex_dump(name, in_data, in_len)
#else
#define chip_log_debug(fmt, args...)
#define chip_log_hex_data(name, in_data, in_len)
#endif
#define chip_log_error(fmt, args...) chip_log(fmt, ##args)
#endif

View file

@ -0,0 +1,25 @@
CHIPNAME = "chip_template"
gincs = Split('''
''')
incs = Split('''
../include
./src
./chipset/%s/include
./chipset/%s/
'''%(CHIPNAME, CHIPNAME))
src = Split('''
./src/core/km_to_irot.c
./src/core/std_se_adapter.c
./src/core/mtk_se_adapter.c
./src/log/chiplog.c
./chipset/%s/irot_impl/irot_hal.c
./chipset/%s/se_driver_impl/se_driver.c
'''%(CHIPNAME, CHIPNAME))
comp = aos_component('se', src)
comp.add_global_includes(*gincs)
comp.add_includes(*incs)

Binary file not shown.

View file

@ -0,0 +1,2 @@
NAME := libkm_tee
$(NAME)_PREBUILT_LIBRARY := lib/$(HOST_ARCH)/libkm_tee.a