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,57 @@
NAME := id2
$(NAME)_CFLAGS := -DPLATFORM_ALIOS
################################################################################
#diffrent config for KM, TEE, SE
LINUXHOST_ID2_CFLAGS = -DID2_OTP_SUPPORTED=1 -DID2_CRYPTO_TYPE_CONFIG=ID2_CRYPTO_TYPE_AES -DID2_OTP_LOCAL_TEST=1 -DID2_DEBUG=1
KM_ID2_CFLAGS = -DID2_OTP_SUPPORTED=1 -DID2_CRYPTO_TYPE_CONFIG=ID2_CRYPTO_TYPE_AES
TEE_ID2_CFLAGS = -DID2_OTP_SUPPORTED=0 -DID2_CRYPTO_TYPE_CONFIG=ID2_CRYPTO_TYPE_AES
SE_ID2_CFLAGS = -DID2_OTP_SUPPORTED=0 -DID2_CRYPTO_TYPE_CONFIG=ID2_CRYPTO_TYPE_3DES
ifeq ($(findstring linuxhost, $(BUILD_STRING)), linuxhost)
$(NAME)_CFLAGS += $(LINUXHOST_ID2_CFLAGS)
else ifeq ($(findstring mk3060, $(BUILD_STRING)), mk3060)
$(NAME)_CFLAGS += $(KM_ID2_CFLAGS)
else ifeq ($(findstring mk3080, $(BUILD_STRING)), mk3080)
$(NAME)_CFLAGS += $(KM_ID2_CFLAGS)
else ifeq ($(findstring mk5080, $(BUILD_STRING)), mk5080)
$(NAME)_CFLAGS += $(KM_ID2_CFLAGS)
else ifeq ($(findstring cb2201, $(BUILD_STRING)), cb2201)
$(NAME)_CFLAGS += $(TEE_ID2_CFLAGS)
else ifeq ($(findstring uno-91h, $(BUILD_STRING)), uno-91h)
$(NAME)_CFLAGS += $(KM_ID2_CFLAGS)
else ifeq ($(findstring hf-lpt230, $(BUILD_STRING)), hf-lpt230)
$(NAME)_CFLAGS += $(KM_ID2_CFLAGS)
else ifeq ($(findstring hf-lpb135, $(BUILD_STRING)), hf-lpb135)
$(NAME)_CFLAGS += $(KM_ID2_CFLAGS)
else ifeq ($(findstring hf-lpt130, $(BUILD_STRING)), hf-lpt130)
$(NAME)_CFLAGS += $(KM_ID2_CFLAGS)
else ifeq ($(findstring hf-lpb130, $(BUILD_STRING)), hf-lpb130)
$(NAME)_CFLAGS += $(KM_ID2_CFLAGS)
else ifeq ($(findstring amebaz, $(BUILD_STRING)), amebaz)
$(NAME)_CFLAGS += $(KM_ID2_CFLAGS)
endif
################################################################################
GLOBAL_INCLUDES += ../include/id2
$(NAME)_INCLUDES += ./src
$(NAME)_INCLUDES += ../irot/include
$(NAME)_SOURCES := \
./src/core/id2_client.c \
./src/core/km_impl.c \
./src/core/otp.c \
./src/alg/alg.c \
./src/alg/basex64.c \
./src/alg/hashsum.c \
./src/log/log.c \
./src/util/util.c \
./platform/irot_pal.c
$(NAME)_COMPONENTS += alicrypto
$(NAME)_COMPONENTS += irot

View file

@ -0,0 +1,70 @@
/*
* Copyright (C) 2018 Alibaba Group Holding Limited
*/
#ifdef PLATFORM_ALIOS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include "config.h"
#include "irot_pal.h"
#include "log/log.h"
#include "ali_crypto.h"
#include "crypto.h"
#include <aos/kernel.h>
void* irot_pal_memory_malloc(int size)
{
void* ptr;
ptr = aos_malloc(size);
return ptr;
}
void irot_pal_memory_free(void* ptr)
{
aos_free(ptr);
}
irot_result_t irot_pal_get_random(uint8_t* buf, uint32_t len)
{
ali_crypto_result result;
static int flag = 0;
uint32_t seed;
if (flag == 0)
{
seed = (uint32_t)aos_now_ms();
result = crypto_seed((uint8_t*)&seed, sizeof(seed));
if (result != ALI_CRYPTO_SUCCESS)
{
return IROT_ERROR_GENERIC;
}
flag = 1;
}
result = crypto_rand_gen(buf, len);
#if (ID2_LOCAL_TEST)
memset(buf, 0xAB, len);
#endif
if (result != ALI_CRYPTO_SUCCESS)
{
return IROT_ERROR_GENERIC;
}
return IROT_SUCCESS;
}
void irot_pal_log(const char* fmt, ...)
{
va_list args;
printf(" ID2=> ");
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
//fflush(stdout);
}
#endif

View file

@ -0,0 +1,185 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdint.h>
#include "config.h"
#include "error_code.h"
#include "irot_pal.h"
#include "alg.h"
#include "log/log.h"
#if (ID2_USE_ALI_CRYPTO)
#include "ali_crypto.h"
#include "crypto.h"
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_3DES)
irot_result_t des3_ecb(const uint8_t* key, uint32_t key_len, const uint8_t* in, uint32_t in_len, uint8_t* out, uint32_t* out_len, uint8_t is_enc)
{
id2_log_debug("crypto lib: 3DES algorithm not supported.\n");
return IROT_ERROR_NOT_SUPPORTED;
}
#endif
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_AES)
irot_result_t aes_ecb(const uint8_t* key, uint32_t key_len, const uint8_t* in, uint32_t in_len, uint8_t* out, uint32_t* out_len, uint8_t is_enc)
{
irot_result_t ret = IROT_SUCCESS;
size_t ctx_size;
ali_crypto_result result;
void* aes_ctx = NULL;
int crypto_mode;
id2_log_debug("crypto lib: AES ecb algorithm.\n");
if (is_enc)
{
crypto_mode = 1;
}
else
{
crypto_mode = 0;
}
result = crypto_aes_get_ctx_size(AES_ECB, &ctx_size);
if (result != ALI_CRYPTO_SUCCESS)
{
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
aes_ctx = irot_pal_memory_malloc(ctx_size);
if (aes_ctx == NULL)
{
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
result = crypto_aes_init(AES_ECB, crypto_mode, key, NULL, key_len, NULL, aes_ctx);
if (result != ALI_CRYPTO_SUCCESS)
{
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
result = crypto_aes_process(in, out, in_len, aes_ctx);
if (result != ALI_CRYPTO_SUCCESS)
{
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
result = crypto_aes_finish(NULL, 0, NULL, NULL, SYM_NOPAD, aes_ctx);
if (result != ALI_CRYPTO_SUCCESS)
{
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
*out_len = in_len;
EXIT:
irot_pal_memory_free(aes_ctx);
return ret;
}
#endif
#else
#include <mbedtls/des.h>
#include <mbedtls/aes.h>
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_3DES)
irot_result_t des3_ecb(const uint8_t* key, uint32_t key_len, const uint8_t* in, uint32_t in_len, uint8_t* out, uint32_t* out_len, uint8_t is_enc)
{
irot_result_t ret = IROT_SUCCESS;
uint32_t i;
mbedtls_des3_context des_context;
mbedtls_des3_init(&des_context);
if ((in_len % DES_BLOCK_SIZE != 0) || (*out_len < in_len))
{
ret = IROT_ERROR_SHORT_BUFFER;
goto EXIT;
}
if (key_len == 0x10)
{
if (is_enc)
{
mbedtls_des3_set2key_enc(&des_context, key);
}
else
{
mbedtls_des3_set2key_dec(&des_context, key);
}
}
else if (key_len == 0x18)
{
if (is_enc)
{
mbedtls_des3_set3key_enc(&des_context, key);
}
else
{
mbedtls_des3_set3key_dec(&des_context, key);
}
}
else
{
ret = IROT_ERROR_BAD_PARAMETERS;
goto EXIT;
}
i = 0;
while (i < in_len)
{
mbedtls_des3_crypt_ecb(&des_context, in + i, out + i);
i += DES_BLOCK_SIZE;
}
*out_len = in_len;
EXIT:
mbedtls_des3_free(&des_context);
return ret;
}
#endif
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_AES)
irot_result_t aes_ecb(const uint8_t* key, uint32_t key_len, const uint8_t* in, uint32_t in_len, uint8_t* out, uint32_t* out_len, uint8_t is_enc)
{
irot_result_t ret = IROT_SUCCESS;
uint32_t i;
mbedtls_aes_context aes_context;
mbedtls_aes_init(&aes_context);
if ((in_len % AES_BLOCK_SIZE != 0) || (*out_len < in_len))
{
ret = IROT_ERROR_SHORT_BUFFER;
goto EXIT;
}
i = 0;
if (is_enc)
{
mbedtls_aes_setkey_enc(&aes_context, key, key_len * 8);
while (i < in_len)
{
mbedtls_aes_crypt_ecb(&aes_context, MBEDTLS_AES_ENCRYPT, in + i, out + i);
i += AES_BLOCK_SIZE;
}
}
else
{
mbedtls_aes_setkey_dec(&aes_context, key, key_len * 8);
while (i < in_len)
{
mbedtls_aes_crypt_ecb(&aes_context, MBEDTLS_AES_DECRYPT, in + i, out + i);
i += AES_BLOCK_SIZE;
}
}
*out_len = in_len;
EXIT:
mbedtls_aes_free(&aes_context);
return ret;
}
#endif
#endif

View file

@ -0,0 +1,21 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef __ALG_H__
#define __ALG_H__
#include <stdint.h>
#include "error_code.h"
enum
{
DES_BLOCK_SIZE = 0x08,
AES_BLOCK_SIZE = 0x10,
};
irot_result_t des3_ecb(const uint8_t* key, uint32_t key_len, const uint8_t* in, uint32_t in_len, uint8_t* out, uint32_t* out_len, uint8_t is_enc);
irot_result_t aes_ecb(const uint8_t* key, uint32_t key_len, const uint8_t* in, uint32_t in_len, uint8_t* out, uint32_t* out_len, uint8_t is_enc);
#endif

View file

@ -0,0 +1,129 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include "basex64.h"
static const unsigned char encoding_table[] =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
static const unsigned char decoding_table[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static const unsigned char mod_table[] = { 0, 2, 1 };
int basex64_encode(const uint8_t* input, uint32_t input_len, uint8_t* output, uint32_t* output_len)
{
int i, j;
uint32_t o_len = 4 * ((input_len + 2) / 3);
if (!input || !output || !output_len)
{
return -1;
}
if (output_len)
{
*output_len = o_len;
}
for (i = 0, j = 0; i < input_len;)
{
unsigned int octet_a = i < input_len ? input[i++] : 0;
unsigned int octet_b = i < input_len ? input[i++] : 0;
unsigned int octet_c = i < input_len ? input[i++] : 0;
unsigned int triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
output[j++] = encoding_table[(triple >> 18) & 0x3F];
output[j++] = encoding_table[(triple >> 12) & 0x3F];
output[j++] = encoding_table[(triple >> 6) & 0x3F];
output[j++] = encoding_table[(triple >> 0) & 0x3F];
}
for (i = 0; i < mod_table[input_len % 3]; i++)
{
output[o_len - 1 - i] = '=';
}
return 0;
}
int basex64_decode(const uint8_t* input, uint32_t input_len, uint8_t* output, uint32_t* output_len)
{
int i, j;
uint32_t o_len = input_len / 4 * 3;
if (!input || !output || !output_len)
{
return -1;
}
if (input[input_len - 1] == '=')
{
o_len--;
}
if (input[input_len - 2] == '=')
{
o_len--;
}
if (output_len)
{
*output_len = o_len;
}
for (i = 0, j = 0; i < input_len;)
{
unsigned int sextet_a = decoding_table[input[i++]];
unsigned int sextet_b = decoding_table[input[i++]];
unsigned int sextet_c = decoding_table[input[i++]];
unsigned int sextet_d = decoding_table[input[i++]];
unsigned int triple = (sextet_a << 18)
+ (sextet_b << 12)
+ (sextet_c << 6)
+ (sextet_d << 0);
if (j < o_len)
{
output[j++] = (triple >> 16) & 0xFF;
}
if (j < o_len)
{
output[j++] = (triple >> 8) & 0xFF;
}
if (j < o_len)
{
output[j++] = (triple >> 0) & 0xFF;
}
}
return 0;
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef _BASE64X_H_
#define _BASE64X_H_
#if defined(__cplusplus)
extern "C" {
#endif
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
int basex64_encode(const uint8_t* input, uint32_t input_len, uint8_t* output, uint32_t* output_len);
int basex64_decode(const uint8_t* input, uint32_t input_len, uint8_t* output, uint32_t* output_len);
#if defined(__cplusplus)
}
#endif
#endif

View file

@ -0,0 +1,120 @@
#include <stdint.h>
#include "error_code.h"
#include "irot_pal.h"
#include "hashsum.h"
#include "config.h"
#if (ID2_USE_ALI_CRYPTO)
#include "ali_crypto.h"
#include "crypto.h"
irot_result_t hash_sum(const uint8_t* in, uint32_t in_len, uint8_t* out, uint32_t* out_len, digest_t type)
{
irot_result_t ret = IROT_SUCCESS;
size_t ctx_size;
ali_crypto_result result;
void* ctx = NULL;
if (type == DIGEST_TYPE_SHA256)
{
if (*out_len < 32)
{
ret = IROT_ERROR_SHORT_BUFFER;
}
else
{
result = crypto_hash_get_ctx_size(SHA256, &ctx_size);
if (result != ALI_CRYPTO_SUCCESS)
{
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
ctx = irot_pal_memory_malloc(ctx_size);
if (ctx == NULL)
{
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
result = crypto_hash_init(SHA256, ctx);
if (result != ALI_CRYPTO_SUCCESS)
{
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
result = crypto_hash_update(in, in_len, ctx);
if (result != ALI_CRYPTO_SUCCESS)
{
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
result = crypto_hash_final(out, ctx);
if (result != ALI_CRYPTO_SUCCESS)
{
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
*out_len = 32;
}
}
else
{
ret = IROT_ERROR_NOT_SUPPORTED;
}
EXIT:
irot_pal_memory_free(ctx);
return ret;
}
#else
#include "sha2.c"
static int digest_sha256(const void *data, uint32_t length, unsigned char *digest)
{
SHA256_CTX *ctx = (SHA256_CTX *) irot_pal_memory_malloc(sizeof(SHA256_CTX));
if (NULL == ctx) {
return -1;
}
memset(ctx, 0, sizeof(SHA256_CTX));
SHA256_Init(ctx);
SHA256_Update(ctx, data, length);
SHA256_Final(digest, ctx);
irot_pal_memory_free(ctx);
return 0;
}
irot_result_t hash_sum(const uint8_t* in, uint32_t in_len, uint8_t* out, uint32_t* out_len, digest_t type)
{
irot_result_t ret = IROT_SUCCESS;
int ret;
if (type == DIGEST_TYPE_SHA256)
{
if (*out_len < 32)
{
ret = IROT_ERROR_SHORT_BUFFER;
}
else
{
ret = digest_sha256(in, in_len, out);
if(ret != 0)
{
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
*out_len = 32;
}
}
else
{
ret = IROT_ERROR_NOT_SUPPORTED;
}
EXIT:
return ret;
}
#endif

View file

@ -0,0 +1,18 @@
#ifndef __HASHSUM_H__
#define __HASHSUM_H__
typedef enum
{
DIGEST_TYPE_SHA1 = 0x00,
DIGEST_TYPE_SHA224 = 0x01,
DIGEST_TYPE_SHA256 = 0x02,
DIGEST_TYPE_SHA384 = 0x03,
DIGEST_TYPE_SHA512 = 0x04,
DIGEST_TYPE_SM3 = 0x05,
} digest_t;
irot_result_t hash_sum(const uint8_t* in, uint32_t in_len, uint8_t* out, uint32_t* out_len, digest_t type);
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,8 @@
#ifndef __KEY_NAME_H__
#define __KEY_NAME_H__
#define ID2_KEY_NAME "id2_key"
#define ID2_KEY_NAME_LEN (sizeof(ID2_KEY_NAME) - 1)
#endif

View file

@ -0,0 +1,207 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdint.h>
#include <string.h>
#include "km.h"
#include "config.h"
#include "alg/alg.h"
#if (ID2_KM_API_EMU)
#define ID2_LEN 24
#define MAX_KEY_LEN 32
static uint8_t s_id2[ID2_LEN] = {0};
static uint32_t s_id2_len = 0;
static uint8_t s_id2_key[MAX_KEY_LEN] = {0};
static uint32_t s_key_len = 0;
uint32_t km_generate_key(const char* name, const uint32_t name_len, km_key_type key_type, void* arg)
{
return KM_ERR_NOT_SUPPORTED;
}
uint32_t km_export_key(const char* name, const uint32_t name_len, km_format_t format, uint8_t* export_data, uint32_t* export_data_size)
{
return KM_ERR_NOT_SUPPORTED;
}
uint32_t km_mac(const char* name, const uint32_t name_len, km_sym_param* mac_params, const uint8_t* iv, const uint32_t iv_len, uint8_t* src, size_t src_len, uint8_t* mac, uint32_t* mac_len)
{
return KM_ERR_NOT_SUPPORTED;
}
uint32_t km_delete_key(const char* name, const uint32_t name_len)
{
return KM_ERR_NOT_SUPPORTED;
}
uint32_t km_delete_all()
{
return KM_ERR_NOT_SUPPORTED;
}
uint32_t km_envelope_begin(void **ctx, const char *name, const uint32_t name_len, uint8_t *iv, uint16_t iv_len, uint8_t *protected_key, uint32_t *protected_key_len, km_purpose_type is_enc)
{
return KM_ERR_NOT_SUPPORTED;
}
uint32_t km_envelope_update(void* ctx, uint8_t* src, uint32_t src_len, uint8_t* dest, uint32_t* dest_len)
{
return KM_ERR_NOT_SUPPORTED;
}
uint32_t km_envelope_finish(void* ctx, uint8_t* src, uint32_t src_len, uint8_t* dest, uint32_t* dest_len)
{
return KM_ERR_NOT_SUPPORTED;
}
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)
{
return KM_ERR_NOT_SUPPORTED;
}
uint32_t km_verify(const char* name, const uint32_t name_len, void* sign_params, const uint8_t* data, const size_t data_len, const uint8_t* signature, const size_t signature_len)
{
return KM_ERR_NOT_SUPPORTED;
}
////////////////////////////////////////////////////////////////////////////////
uint32_t km_asym_encrypt(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)
{
return KM_ERR_NOT_SUPPORTED;
}
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)
{
return KM_ERR_NOT_SUPPORTED;
}
////////////////////////////////////////////////////////////////////////////////
uint32_t km_init()
{
return KM_SUCCESS;
}
void km_cleanup()
{
}
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)
{
irot_result_t ret;
km_key_type key_type = cipher_params->key_type;
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;
uint8_t is_enc;
uint32_t output_len;
if ((block_mode != KM_ECB) || (padding_type != KM_NO_PADDING))
{
return KM_ERR_NOT_SUPPORTED;
}
is_enc = (purpose_type == KM_PURPOSE_ENCRYPT) ? 1 : 0;
output_len = *dest_len;
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_3DES)
if (key_type == KM_DES3)
{
ret = des3_ecb(s_id2_key, s_key_len, src, src_len, dest, &output_len, is_enc);
}
#endif
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_AES)
if (key_type == KM_AES)
{
ret = aes_ecb(s_id2_key, s_key_len, src, src_len, dest, &output_len, is_enc);
}
#endif
else
{
return KM_ERR_NOT_SUPPORTED;
}
if(ret != IROT_SUCCESS)
{
return KM_ERR_GENERIC;
}
*dest_len = output_len;
return KM_SUCCESS;
}
uint32_t km_set_id2(uint8_t* id2, uint32_t len)
{
if (len != ID2_LEN)
{
return KM_ERR_BAD_PARAMS;
}
memcpy(s_id2, id2, len);
s_id2_len = len;
return KM_SUCCESS;
}
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)
{
uint8_t key_type;
uint8_t* pkey;
uint32_t key_len;
key_type = key_data->type;
if ((key_type != KM_DES3) && (key_type != KM_AES))
{
return KM_ERR_BAD_PARAMS;
}
pkey = key_data->sym_key.key;
key_len = key_data->sym_key.key_bit >> 3;
memcpy(s_id2_key, pkey, key_len);
s_key_len = key_len;
return KM_SUCCESS;
}
uint32_t km_get_id2(uint8_t* id2, uint32_t* len)
{
if (*len < ID2_LEN)
{
return KM_ERR_SHORT_BUFFER;
}
if (s_id2_len == 0)
{
return KM_ERR_ITEM_NOT_FOUND;
}
else
{
memcpy(id2, s_id2, ID2_LEN);
}
return KM_SUCCESS;
}
uint32_t km_get_attestation(uint8_t* id, uint32_t* id_len)
{
if (id == NULL)
{
*id_len = ID2_LEN;
return KM_ERR_SHORT_BUFFER;
}
else
{
*id_len = ID2_LEN;
memset(id, 'A', *id_len);
return KM_SUCCESS;
}
}
#endif

View file

@ -0,0 +1,624 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "version.h"
#include "irot_pal.h"
#include "id2_client.h"
#include "log/log.h"
#include "util/util.h"
#include "alg/alg.h"
#include "alg/hashsum.h"
#include "km.h"
#include "keyname.h"
#if (ID2_OTP_SUPPORTED)
extern int is_id2_client_inited(void);
extern void id2_reset_cache(void);
enum
{
KEY_TYPE_AES_128 = 1,
KEY_TYPE_AES_192 = 2,
KEY_TYPE_AES_256 = 3,
KEY_TYPE_3DES_112 = 4,
KEY_TYPE_3DES_168 = 5,
};
#define ID2_OTP_SS_KEY_LEN 0x10
#define ID2_OTP_AUTH_CODE_VER 0x00010000
#define ID2_SS_KEY_LEN 16
#define ID2_ROUNDUP(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
enum
{
SHA256_HASH_SIZE = 32,
};
static uint8_t g_ss_key[ID2_SS_KEY_LEN] = {0};
////////////////////////////////////////////////////////////////////////////////
static uint8_t* get_otp_prov_id(uint8_t* prov_key, uint32_t key_size)
{
irot_result_t ret;
uint8_t* prov_id = NULL;
uint32_t prov_id_len;
if (prov_key == NULL || key_size == 0)
{
id2_log_error("%s: invalid input args\n", __FUNC_NAME__);
return NULL;
}
prov_id_len = 32;
prov_id = irot_pal_memory_malloc(prov_id_len);
if (prov_id == NULL)
{
id2_log_error("%s: malloc(%d) fail\n", __FUNC_NAME__, prov_id_len);
return NULL;
}
ret = hash_sum(prov_key, key_size,
prov_id, &prov_id_len, DIGEST_TYPE_SHA256);
if (ret != IROT_SUCCESS)
{
id2_log_error("%s: hal hash sum fail\n", __FUNC_NAME__, ret);
irot_pal_memory_free(prov_id);
return NULL;
}
return prov_id;;
}
static irot_result_t otp_get_rept_data(uint8_t key_type,
uint8_t* key_data, uint32_t key_len,
uint8_t* dev_fp, uint32_t dev_fp_len,
uint8_t* rept_data, uint32_t* rept_len)
{
irot_result_t ret;
uint32_t offset;
uint32_t padding;
uint32_t hash_len;
uint32_t data_len;
uint32_t total_len;
uint32_t block_size;
uint8_t* prov_key = NULL;
if (key_data == NULL || key_len == 0 ||
dev_fp == NULL || dev_fp_len == 0 ||
rept_data == NULL || rept_len == NULL)
{
id2_log_error("%s: invalid input args\n", __FUNC_NAME__);
return IROT_ERROR_BAD_PARAMETERS;
}
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_AES)
if (key_type == KEY_TYPE_AES_128 ||
key_type == KEY_TYPE_AES_192 || key_type == KEY_TYPE_AES_256)
{
block_size = AES_BLOCK_SIZE;
}
#elif (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_3DES)
if (key_type == KEY_TYPE_3DES_112 || key_type == KEY_TYPE_3DES_168)
{
block_size = DES_BLOCK_SIZE;
}
#endif
else
{
id2_log_error("%s: not support this id2 key type: %d\n", __FUNC_NAME__, key_type);
return IROT_ERROR_NOT_SUPPORTED;
}
prov_key = key_data;
id2_log_hex_data("prov_key", prov_key, key_len);
/* generate session key */
#if (ID2_OTP_LOCAL_TEST)
memset(g_ss_key, '0', ID2_SS_KEY_LEN);
#else
irot_pal_get_random(g_ss_key, ID2_SS_KEY_LEN);
#endif
/* key_type + ss_key + dev_fp */
data_len = 1 + ID2_SS_KEY_LEN + dev_fp_len;
padding = block_size - data_len % block_size;
total_len = SHA256_HASH_SIZE + data_len + padding;
if (*rept_len < total_len)
{
id2_log_error("%s: rept data short buffer, %d\n", __FUNC_NAME__, *rept_len);
*rept_len = total_len;
ret = IROT_ERROR_SHORT_BUFFER;
goto _out;
}
else
{
*rept_len = total_len;
}
offset = SHA256_HASH_SIZE;
memcpy(rept_data + offset, &key_type, 1);
offset += 1;
memcpy(rept_data + offset, g_ss_key, ID2_SS_KEY_LEN);
offset += ID2_SS_KEY_LEN;
memcpy(rept_data + offset, dev_fp, dev_fp_len);
offset += dev_fp_len;
memset(rept_data + offset, padding, padding);
id2_log_hex_data("rept_data", rept_data + SHA256_HASH_SIZE, data_len);
hash_len = SHA256_HASH_SIZE;
ret = hash_sum(
rept_data + SHA256_HASH_SIZE, data_len,
rept_data, &hash_len, DIGEST_TYPE_SHA256);
if (ret != IROT_SUCCESS)
{
id2_log_error("%s: hal hash sum fail, %d\n", __FUNC_NAME__, ret);
goto _out;
}
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_AES)
data_len += padding;
ret = aes_ecb(prov_key, key_len,
rept_data + SHA256_HASH_SIZE, data_len,
rept_data + SHA256_HASH_SIZE, &data_len, 1);
if (ret != IROT_SUCCESS)
{
id2_log_error("%s: rept data encrypt fail, %d\n", __FUNC_NAME__, ret);
goto _out;
}
#elif (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_3DES)
data_len += padding;
ret = des3_ecb(prov_key, key_len,
rept_data + SHA256_HASH_SIZE, data_len,
rept_data + SHA256_HASH_SIZE, &data_len, 1);
if (ret != IROT_SUCCESS)
{
id2_log_error("%s: rept data encrypt fail, %d\n", __FUNC_NAME__, ret);
goto _out;
}
#endif
_out:
return ret;
}
static irot_result_t otp_set_otp_data(uint8_t* otp_data, uint32_t len)
{
irot_result_t ret;
uint32_t key_type;
uint32_t key_size = 0;
uint32_t key_data_len = 0;
uint32_t kcv_data_len = 0;
uint32_t otp_data_len = 0;
uint32_t key_data_off = 0;
uint32_t kcv_data_off = 0;
uint8_t* id2_id = NULL;
uint8_t* id2_key = NULL;
uint32_t kmret;
if (otp_data == NULL || len == 0)
{
id2_log_error("%s: invalid input args\n", __FUNC_NAME__);
return IROT_ERROR_BAD_PARAMETERS;
}
id2_log_hex_data("otp_data", (uint8_t*)otp_data, len);
key_type = otp_data[0];
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_AES)
if (key_type == KEY_TYPE_AES_128)
{
key_size = 16;
key_data_len = ID2_ROUNDUP(key_size + 1, AES_BLOCK_SIZE);
kcv_data_len = ID2_ROUNDUP(ID2_ID_LEN + 1, AES_BLOCK_SIZE);
}
else if (key_type == KEY_TYPE_AES_192)
{
key_size = 24;
key_data_len = ID2_ROUNDUP(key_size + 1, AES_BLOCK_SIZE);
kcv_data_len = ID2_ROUNDUP(ID2_ID_LEN + 1, AES_BLOCK_SIZE);
}
else if (key_type == KEY_TYPE_AES_256)
{
key_size = 32;
key_data_len = ID2_ROUNDUP(key_size + 1, AES_BLOCK_SIZE);
kcv_data_len = ID2_ROUNDUP(ID2_ID_LEN + 1, AES_BLOCK_SIZE);
}
#elif (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_3DES)
if (key_type == KEY_TYPE_3DES_112)
{
key_size = 16;
key_data_len = ID2_ROUNDUP(key_size + 1, DES_BLOCK_SIZE);
kcv_data_len = ID2_ROUNDUP(ID2_ID_LEN + 1, DES_BLOCK_SIZE);
}
else if (key_type == KEY_TYPE_3DES_168)
{
key_size = 24;
key_data_len = ID2_ROUNDUP(key_size + 1, DES_BLOCK_SIZE);
kcv_data_len = ID2_ROUNDUP(ID2_ID_LEN + 1, DES_BLOCK_SIZE);
}
#else
#error("OTP not supported for RSA");
#endif
otp_data_len = 1 + ID2_ID_LEN + key_data_len + kcv_data_len;
if (len != otp_data_len)
{
id2_log_error("%s: otp len is not match: %d %d\n", __FUNC_NAME__, len, otp_data_len);
return IROT_ERROR_GENERIC;
}
key_data_off = 1 + ID2_ID_LEN;
kcv_data_off = 1 + ID2_ID_LEN + key_data_len;
id2_key = irot_pal_memory_malloc(key_data_len);
if (id2_key == NULL)
{
id2_log_error("%s: malloc %d fail\n", __FUNC_NAME__, key_data_len);
return IROT_ERROR_OUT_OF_MEMORY;
}
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_AES)
ret = aes_ecb(g_ss_key, ID2_SS_KEY_LEN,
otp_data + key_data_off, key_data_len,
id2_key, &key_data_len, 0);
if (ret != IROT_SUCCESS)
{
id2_log_error("%s: id2 key decrypt fail, %d\n", __FUNC_NAME__, ret);
goto _out;
}
#elif (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_3DES)
ret = des3_ecb(g_ss_key, ID2_SS_KEY_LEN,
otp_data + key_data_off, key_data_len,
id2_key, &key_data_len, 0);
if (ret != IROT_SUCCESS)
{
id2_log_error("%s: id2 key decrypt fail, %d\n", __FUNC_NAME__, ret);
goto _out;
}
#endif
//id2_log_hex_data("id2_key", id2_key, key_size);
id2_id = irot_pal_memory_malloc(kcv_data_len);
if (id2_id == NULL)
{
id2_log_error("%s: malloc %d fail\n", __FUNC_NAME__, kcv_data_len);
ret = IROT_ERROR_OUT_OF_MEMORY;
goto _out;
}
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_AES)
ret = aes_ecb(id2_key, key_size,
otp_data + kcv_data_off, kcv_data_len,
id2_id, &kcv_data_len, 0);
if (ret != IROT_SUCCESS)
{
id2_log_error("%s: id2 id decrypt fail, %d\n", __FUNC_NAME__, ret);
goto _out;
}
#elif (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_3DES)
ret = des3_ecb(id2_key, key_size,
otp_data + kcv_data_off, kcv_data_len,
id2_id, &kcv_data_len, 0);
if (ret != IROT_SUCCESS)
{
id2_log_error("%s: id2 id decrypt fail, %d\n", __FUNC_NAME__, ret);
goto _out;
}
#endif
id2_log_hex_data("id2_id", id2_id, ID2_ID_LEN);
if (!memcmp(id2_id, (uint8_t*)otp_data + 1, ID2_ID_LEN))
{
km_key_data_t key_struct;
if ((key_type == KEY_TYPE_AES_128) || (key_type == KEY_TYPE_AES_192) || (key_type == KEY_TYPE_AES_256))
{
key_struct.type = KM_AES;
}
else if ((key_type == KEY_TYPE_3DES_112) || (key_type == KEY_TYPE_3DES_168))
{
key_struct.type = KM_DES3;
}
else
{
ret = IROT_ERROR_NOT_SUPPORTED;
goto _out;
}
key_struct.sym_key.key_bit = (key_size << 3);
key_struct.sym_key.key = id2_key;
kmret = km_set_id2(id2_id, ID2_ID_LEN);
if (kmret != KM_SUCCESS)
{
id2_log_error("%s: KM set id2 error, 0x%08X\n", __FUNC_NAME__, kmret);
ret = IROT_ERROR_GENERIC;
goto _out;
}
kmret = km_delete_key(ID2_KEY_NAME, ID2_KEY_NAME_LEN);
if (kmret != KM_SUCCESS)
{
//id2_log_error("%s: KM delete key, 0x%08X\n", __FUNC_NAME__, kmret);
}
kmret = km_import_key(ID2_KEY_NAME, ID2_KEY_NAME_LEN, KM_KEY_FORMAT_RAW, &key_struct, sizeof(key_struct));
if (kmret != KM_SUCCESS)
{
id2_log_error("%s: KM import key error, 0x%08X\n", __FUNC_NAME__, kmret);
ret = IROT_ERROR_GENERIC;
goto _out;
}
}
else
{
ret = IROT_ERROR_GENERIC;
}
_out:
if (id2_id)
{
irot_pal_memory_free(id2_id);
}
if (id2_key)
{
irot_pal_memory_free(id2_key);
}
return ret;
}
////////////////////////////////////////////////////////////////////////////////
irot_result_t id2_client_get_prov_stat(bool* is_prov)
{
irot_result_t ret = IROT_SUCCESS;
uint8_t id2[ID2_ID_LEN];
uint32_t len = ID2_ID_LEN;
uint32_t kmret;
id2_log_debug("[%s] enter.\n", __FUNC_NAME__);
if (!is_id2_client_inited())
{
id2_log_error("ERROR: [%s] id2 client not inited.\n", __FUNC_NAME__);
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
if (is_prov == NULL)
{
id2_log_error("%s: invalid input arg\n", __FUNC_NAME__);
ret = IROT_ERROR_BAD_PARAMETERS;
goto EXIT;
}
kmret = km_get_id2(id2, &len);
if (kmret == KM_ERR_ITEM_NOT_FOUND)
{
*is_prov = false;
ret = IROT_SUCCESS;
id2_log_debug("OTP prov state: False\n");
goto EXIT;
}
else if (kmret != KM_SUCCESS)
{
id2_log_error("%s: KM get id2 fail, 0x%08X\n", __FUNC_NAME__, kmret);
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
id2_log_debug("OTP prov state: True\n");
*is_prov = true;
EXIT:
id2_log_debug("[%s] exit.\n", __FUNC_NAME__);
return ret;
}
enum
{
DEVICE_TEST_FP_LEN = 16,
};
irot_result_t id2_client_get_otp_auth_code(const uint8_t* token, uint32_t token_len,
uint8_t* auth_code, uint32_t* len)
{
irot_result_t ret = IROT_SUCCESS;
uint32_t kmret;
uint8_t use_type;
uint8_t key_type;
uint32_t key_size = 0;
uint32_t block_size = 0;
uint32_t rept_len = 0;
uint32_t dev_fp_len = 0;
uint8_t* dev_fp = NULL;
uint8_t* prov_id = NULL;
uint8_t* rept_data = NULL;
uint32_t auth_ver = ID2_OTP_AUTH_CODE_VER;
id2_log_debug("[%s] enter.\n", __FUNC_NAME__);
if (!is_id2_client_inited())
{
id2_log_error("ERROR: [%s] id2 client not inited.\n", __FUNC_NAME__);
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
if (token == NULL || token_len != 32 ||
auth_code == NULL || len == NULL)
{
id2_log_error("%s: invalid input args\n", __FUNC_NAME__);
ret = IROT_ERROR_BAD_PARAMETERS;
goto EXIT;
}
/* magic */
if (token[0] != 0x69)
{
id2_log_error("%s: invalid token magic: 0x%x\n", __FUNC_NAME__, token[0]);
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
use_type = token[1] - '0';
if (use_type != 0x01)
{
id2_log_error("%s: not support this use type: 0x%x\n", __FUNC_NAME__, use_type);
ret = IROT_ERROR_NOT_SUPPORTED;
goto EXIT;
}
key_type = token[2] - '0';
#if (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_AES)
if (key_type == KEY_TYPE_AES_128 ||
key_type == KEY_TYPE_AES_192 || key_type == KEY_TYPE_AES_256)
{
key_size = 16;
block_size = AES_BLOCK_SIZE;
}
#elif (ID2_CRYPTO_TYPE_CONFIG == ID2_CRYPTO_TYPE_3DES)
if (key_type == KEY_TYPE_3DES_112 || key_type == KEY_TYPE_3DES_168)
{
key_size = 16;
block_size = DES_BLOCK_SIZE;
}
#endif
else
{
id2_log_error("%s: not support this key type: 0x%x\n", __FUNC_NAME__, key_type);
ret = IROT_ERROR_NOT_SUPPORTED;
goto EXIT;
}
prov_id = get_otp_prov_id((uint8_t*)token + 3, key_size);
if (prov_id == NULL)
{
id2_log_error("%s: get prov id fail\n", __FUNC_NAME__);
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
id2_log_hex_data("prov_id", prov_id, 32);
#if ID2_OTP_LOCAL_TEST
dev_fp_len = DEVICE_TEST_FP_LEN;
#else
dev_fp_len = 0x100;
#endif
dev_fp = irot_pal_memory_malloc(dev_fp_len);
if (dev_fp == NULL)
{
id2_log_error("%s: malloc %d fail\n", __FUNC_NAME__, dev_fp_len);
ret = IROT_ERROR_OUT_OF_MEMORY;
goto EXIT;
}
#if ID2_OTP_LOCAL_TEST
kmret = KM_SUCCESS;
memset(dev_fp, '1', DEVICE_TEST_FP_LEN);
#else
kmret = km_get_attestation(dev_fp, &dev_fp_len);
#endif
if (kmret != KM_SUCCESS)
{
id2_log_error("%s: KM km_get_attestation error, 0x%08X\n", __FUNC_NAME__, kmret);
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
id2_log_hex_data("attestation", dev_fp, dev_fp_len);
/* one cipher block size and sha256 hash size */
rept_len = block_size + 32;
rept_len += 1 + ID2_OTP_SS_KEY_LEN + dev_fp_len + block_size;
rept_data = irot_pal_memory_malloc(rept_len);
if (rept_data == NULL)
{
id2_log_error("%s: malloc %d fail\n", __FUNC_NAME__, rept_len);
ret = IROT_ERROR_OUT_OF_MEMORY;
goto EXIT;
}
ret = otp_get_rept_data(key_type,
(uint8_t*)token + 3, key_size,
dev_fp, dev_fp_len, rept_data, &rept_len);
if (ret != IROT_SUCCESS)
{
id2_log_error("%s: get rept data fail, %d\n", __FUNC_NAME__, ret);
goto EXIT;
}
if (*len < 4 + 32 + rept_len)
{
id2_log_error("%s, auth code short buffer: %d\n", __FUNC_NAME__, *len);
*len = 4 + 32 + rept_len;
ret = IROT_ERROR_SHORT_BUFFER;
goto EXIT;
}
else
{
*len = 4 + 32 + rept_len;
}
memcpy(auth_code, &auth_ver, 4);
memcpy(auth_code + 4, prov_id, 32);
memcpy(auth_code + 4 + 32 , rept_data, rept_len);
id2_log_hex_data("otp_auth_code", auth_code, *len);
EXIT:
if (prov_id)
{
irot_pal_memory_free(prov_id);
}
if (dev_fp)
{
irot_pal_memory_free(dev_fp);
}
if (rept_data)
{
irot_pal_memory_free(rept_data);
}
id2_log_debug("[%s] exit.\n", __FUNC_NAME__);
return ret;
}
irot_result_t id2_client_load_otp_data(const uint8_t* otp_data, uint32_t len)
{
irot_result_t ret;
id2_log_debug("[%s] enter.\n", __FUNC_NAME__);
if (!is_id2_client_inited())
{
id2_log_error("ERROR: [%s] id2 client not inited.\n", __FUNC_NAME__);
ret = IROT_ERROR_GENERIC;
goto EXIT;
}
ret = otp_set_otp_data((uint8_t*)otp_data, len);
if (ret != IROT_SUCCESS)
{
id2_log_error("%s: set otp data fail, %d\n", __FUNC_NAME__, ret);
goto EXIT;
}
id2_reset_cache();
EXIT:
id2_log_debug("[%s] exit.\n", __FUNC_NAME__);
return ret;
}
#endif /* ID2_OTP_SUPPORTED */

View file

@ -0,0 +1,11 @@
/*
* Copyright (C) 2018 Alibaba Group Holding Limited
*/
#ifndef __VERSION_H__
#define __VERSION_H__
#define ID2_CLIENT_VERSION_NUMBER 0x00010104
#endif

View file

@ -0,0 +1,58 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "config.h"
#include "log.h"
#if (ID2_DEBUG)
#define COL_SIZE 0x10
void id2_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)
{
id2_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");
id2_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)
{
id2_log_debug("%s", buf);
}
else
{
id2_log_debug("%s\n", buf);
}
}
}
#endif

View file

@ -0,0 +1,24 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef __LOG_H__
#define __LOG_H__
#include "config.h"
extern void irot_pal_log(const char* fmt, ...);
void id2_log_hex_dump(const char* name, const uint8_t* in_data, uint32_t in_len);
#if ID2_DEBUG
#define id2_log_debug(fmt, args...) irot_pal_log(fmt, ##args)
#define id2_log_hex_data(name, in_data, in_len) id2_log_hex_dump(name, in_data, in_len)
#else
#define id2_log_debug(fmt, args...)
#define id2_log_hex_data(name, in_data, in_len)
#endif
#define id2_log_error(fmt, args...) irot_pal_log(fmt, ##args)
#endif

View file

@ -0,0 +1,170 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "util/util.h"
const static char* const ascii_table = "0123456789ABCDEF";
/* each of 0, 1, 2, ... -> "0123456789ABCDEF"*/
int hex_to_char(uint8_t hex)
{
int ch = -1;
if(hex <= 0x0F)
{
ch = ascii_table[hex];
}
return ch;
}
/* each of "0123456789ABCDEF" -> 0, 1, 2, ... */
int char_to_hex(char c)
{
int hex = -1;
if((c >= '0') && (c <= '9'))
{
hex = c - '0';
}
else if ((c >= 'a') && (c <= 'f'))
{
hex = c - 'a' + 10;
}
else if ((c >= 'A') && (c <= 'F'))
{
hex = c - 'A' + 10;
}
else
{
}
return hex;
}
int hex_to_ascii(const uint8_t* in_data, uint32_t in_len, uint8_t* out_buf, uint32_t* out_len)
{
int32_t i;
uint8_t v;
uint8_t high, low;
for (i = in_len - 1; i >= 0; i--)
{
v = in_data[i];
high = (v >> 4) & 0x0F;
low = (v & 0x0F);
out_buf[(i << 1)] = ascii_table[high];
out_buf[(i << 1) + 1] = ascii_table[low];
}
*out_len = in_len << 1;
return 0;
}
int ascii_to_hex(const uint8_t* in_data, uint32_t in_len, uint8_t* out_buf, uint32_t* out_len)
{
uint32_t i = 0;
uint32_t j = 0;
int h;
int l;
if ((in_data == NULL) || (out_buf == NULL) || (in_len % 2) != 0)
{
return -1;
}
if ((in_len >> 1) > *out_len)
{
return -1;
}
while (i < in_len)
{
h = char_to_hex(in_data[i]);
if (h < 0)
{
return -1;
}
l = char_to_hex(in_data[i + 1]);
if (l < 0)
{
return -1;
}
out_buf[j++] = (uint8_t)((h << 4) | (l & 0x0F));
i += 2;
}
*out_len = j;
return 0;
}
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;
}
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;
}
int pkcs5_unpading(uint8_t* in_data, uint32_t in_len, uint32_t* out_len, uint8_t block_len)
{
int i;
uint8_t v;
uint8_t pading_len;
if ((in_len == 0) || ((in_len % block_len) != 0))
{
return -1;
}
i = in_len - 1;
v = in_data[i];
pading_len = v;
if (pading_len > block_len)
{
return -1;
}
while (pading_len-- > 0)
{
if (in_data[i--] != v)
{
return -1;
}
}
*out_len = in_len - v;
return 0;
}

View file

@ -0,0 +1,20 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef __UTIL_H__
#define __UTIL_H__
#include <stdint.h>
int hex_to_char(uint8_t hex);
int char_to_hex(char c);
int hex_to_ascii(const uint8_t* in_data, uint32_t in_len, uint8_t* out_buf, uint32_t* out_len);
int ascii_to_hex(const uint8_t* in_data, uint32_t in_len, uint8_t* out_buf, uint32_t* out_len);
int pkcs5_pading(uint8_t* in_data, uint32_t in_len, uint32_t* out_len, uint8_t block_len);
int pkcs5_unpading(uint8_t* in_data, uint32_t in_len, uint32_t* out_len, uint8_t block_len);
#endif