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,62 @@
/**
* Copyright (C) 2016 The YunOS Project. All rights reserved.
*/
#include "ali_crypto.h"
#include "ali_crypto_test.h"
void ali_crypto_test_entry(void)
{
int ret;
/* for gcov coverage */
ali_crypto_print_data("alicrypto TEST", (uint8_t *)"\n", 1);
ret = ali_crypto_init();
if (ret < 0) {
goto _OUT;
}
CRYPT_INF("Test hash:\n");
ret = ali_crypto_hash_test();
if (ret < 0) {
goto _OUT;
}
CRYPT_INF("Test hmac:\n");
ret = ali_crypto_hmac_test();
if (ret < 0) {
goto _OUT;
}
CRYPT_INF("Test rand:\n");
ret = ali_crypto_rand_test();
if (ret < 0) {
goto _OUT;
}
CRYPT_INF("Test aes:\n");
ret = ali_crypto_aes_test();
if (ret < 0) {
goto _OUT;
}
CRYPT_INF("Test rsa:\n");
ret = ali_crypto_rsa_test();
if (ret < 0) {
goto _OUT;
}
_OUT:
ali_crypto_cleanup();
return;
}
#if 0
int main(void)
{
ali_crypto_test_entry();
return 0;
}
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,29 @@
/**
* Copyright (C) 2016 The YunOS Project. All rights reserved.
*/
#include "ali_crypto_test.h"
void ali_crypto_print_data(const char *name, uint8_t *data, size_t size)
{
size_t i;
if (data == NULL || size == 0) {
CRYPT_ERR("print_data: no data\n");
return;
}
CRYPT_INF("%s size: %d\n", name, (int)size);
for (i = 0; i < size - size % 8; i += 8) {
CRYPT_INF("%s data: %02x%02x %02x%02x %02x%02x %02x%02x\n",
name,
data[i+0], data[i+1], data[i+2], data[i+3],
data[i+4], data[i+5], data[i+6], data[i+7]);
}
while(i < size) {
CRYPT_INF("%s data: %02x\n", name, data[i]);
i++;
}
return;
}

View file

@ -0,0 +1,245 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include "ali_crypto.h"
#include "ali_crypto_test.h"
#define TEST_DATA_SIZE (141)
static uint8_t _g_test_data[TEST_DATA_SIZE] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0x0d,
0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x13
};
/* openssl calculated result */
static uint8_t hash_md5[MD5_HASH_SIZE] = { 0x95, 0x79, 0xa2, 0x46, 0x8e, 0xbc,
0x5b, 0xd6, 0x45, 0x57, 0xbb, 0x4f,
0xaf, 0xae, 0x5a, 0x05 };
static int8_t hash_sha1[SHA1_HASH_SIZE] = { 0x54, 0x1d, 0x6f, 0x6e, 0x46,
0x7e, 0xfe, 0x1d, 0xa8, 0x66,
0x06, 0x34, 0xb0, 0x21, 0x3d,
0x65, 0xb8, 0xa4, 0x02, 0xca };
static uint8_t hash_sha224[SHA224_HASH_SIZE] = {
0x36, 0x94, 0x36, 0xf9, 0xd4, 0xe9, 0xbe, 0x59, 0xbb, 0x59,
0x5c, 0x73, 0x4a, 0xf0, 0xe8, 0x52, 0x48, 0x09, 0x84, 0x42,
0xec, 0x80, 0xdb, 0x86, 0x5a, 0x51, 0x44, 0x3a
};
static uint8_t hash_sha256[SHA256_HASH_SIZE] = {
0x3b, 0x7f, 0x52, 0xae, 0x5b, 0xe8, 0x09, 0x19, 0x02, 0x1a, 0x83,
0x8d, 0xcc, 0xc6, 0x01, 0xc3, 0x76, 0x41, 0x22, 0x64, 0x4b, 0x1c,
0x35, 0xa2, 0x9d, 0xd3, 0xc5, 0x76, 0x36, 0xd7, 0xda, 0x5f
};
static uint8_t hash_sha384[SHA384_HASH_SIZE] = {
0x21, 0xc7, 0x05, 0xb3, 0x37, 0x66, 0xf3, 0xb5, 0x0d, 0x51, 0xf5, 0x0f,
0x91, 0xfc, 0xa1, 0xcf, 0x78, 0x35, 0x82, 0x77, 0xfd, 0x2c, 0x31, 0xbb,
0x8a, 0x26, 0x6f, 0x2a, 0x82, 0x52, 0x1a, 0x70, 0xfd, 0xfc, 0xa2, 0xb7,
0xee, 0x7f, 0xb5, 0xfd, 0x9e, 0x20, 0x36, 0x91, 0xc6, 0xd6, 0x54, 0xa0
};
static uint8_t hash_sha512[SHA512_HASH_SIZE] = {
0x9e, 0xca, 0x2a, 0x96, 0x01, 0x48, 0x0f, 0xa2, 0x6b, 0x99, 0x27,
0x1a, 0x7f, 0x72, 0xe3, 0xa4, 0xee, 0x2f, 0x08, 0x92, 0x2e, 0xdb,
0xf7, 0x19, 0xd8, 0xcd, 0xcb, 0xfc, 0x8b, 0x56, 0x8c, 0x04, 0xfb,
0xb3, 0x69, 0xdf, 0x26, 0xfb, 0x0b, 0x9f, 0xbe, 0x1d, 0x42, 0xbd,
0x39, 0x87, 0x52, 0x16, 0x42, 0xac, 0x62, 0x57, 0x94, 0x59, 0xa4,
0xce, 0x8d, 0x69, 0x78, 0xb7, 0xf8, 0x95, 0xb8, 0x78
};
int ali_crypto_hash_test(void)
{
ali_crypto_result result;
hash_type_t type;
void * hash_ctx = NULL;
size_t hash_ctx_size;
uint8_t hash[MAX_HASH_SIZE];
uint8_t hash_all[MAX_HASH_SIZE];
/* for gcov coverage */
result = ali_hash_get_ctx_size(HASH_NONE, &hash_ctx_size);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
result = ali_hash_get_ctx_size(HASH_NONE, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
result = ali_hash_init(HASH_NONE, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
result = ali_hash_init(HASH_NONE, hash_ctx);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
result = ali_hash_update(_g_test_data, 13, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
result = ali_hash_update(NULL, 13, (void *)-1);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
result = ali_hash_final(hash, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
result = ali_hash_final(hash, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
result = ali_hash_reset(NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
result = ali_hash_copy_context(NULL, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
for (type = SHA1; type <= MD5; type++) {
if (type == SHA512 || type == SHA384) {
CRYPT_INF("hash not support hash 384 512\n");
continue;
}
result = ali_hash_get_ctx_size(type, &hash_ctx_size);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "get ctx size fail(%08x)\n", result);
}
hash_ctx = CRYPT_MALLOC(hash_ctx_size);
if (hash_ctx == NULL) {
GO_RET(result, "malloc(%d) fail\n", (int)hash_ctx_size);
}
CRYPT_MEMSET(hash_ctx, 0, hash_ctx_size);
result = ali_hash_init(type, hash_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "init fail(%08x)", result);
}
result = ali_hash_update(_g_test_data, 13, hash_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "update 1th fail(%08x)", result);
}
result = ali_hash_update(_g_test_data + 13, 63, hash_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "update 2th fail(%08x)", result);
}
result = ali_hash_update(_g_test_data + 13 + 63, 65, hash_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "update 3th fail(%08x)", result);
}
result = ali_hash_final(hash, hash_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "final fail(%08x)", result);
}
result = ali_hash_digest(type, _g_test_data, TEST_DATA_SIZE, hash_all);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "digest fail(%08x)", result);
}
/* for gcov coverage */
result = ali_hash_copy_context(hash_ctx, hash_ctx);
if (result == ALI_CRYPTO_SUCCESS) {
result = ALI_CRYPTO_ERROR;
goto _OUT;
}
result = ali_hash_reset(hash_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
result = ALI_CRYPTO_ERROR;
goto _OUT;
}
CRYPT_FREE(hash_ctx);
hash_ctx = NULL;
if (type == SHA1) {
if (CRYPT_MEMCMP(hash, hash_sha1, SHA1_HASH_SIZE) ||
CRYPT_MEMCMP(hash_all, hash_sha1, SHA1_HASH_SIZE)) {
ali_crypto_print_data("sha1", hash, SHA1_HASH_SIZE);
GO_RET(-1, "SHA1 test fail!");
} else {
CRYPT_INF("SHA1 test success!\n");
}
} else if (type == SHA224) {
if (CRYPT_MEMCMP(hash, hash_sha224, SHA224_HASH_SIZE) ||
CRYPT_MEMCMP(hash_all, hash_sha224, SHA224_HASH_SIZE)) {
ali_crypto_print_data("sha224", hash, SHA224_HASH_SIZE);
GO_RET(-1, "SHA224 test fail!\n");
} else {
CRYPT_INF("SHA224 test success!\n");
}
} else if (type == SHA256) {
if (CRYPT_MEMCMP(hash, hash_sha256, SHA256_HASH_SIZE) ||
CRYPT_MEMCMP(hash_all, hash_sha256, SHA256_HASH_SIZE)) {
ali_crypto_print_data("sha256", hash, SHA256_HASH_SIZE);
GO_RET(-1, "SHA256 test fail!\n");
} else {
CRYPT_INF("SHA256 test success!\n");
}
/* } else if (type == SHA384) {
if(CRYPT_MEMCMP(hash, hash_sha384, SHA384_HASH_SIZE) ||
CRYPT_MEMCMP(hash_all, hash_sha384,
SHA384_HASH_SIZE)) { ali_crypto_print_data("sha384", hash,
SHA384_HASH_SIZE); GO_RET(-1, "SHA384 test fail!\n"); } else {
CRYPT_INF("SHA384 test success!\n");
}
} else if (type == SHA512) {
if(CRYPT_MEMCMP(hash, hash_sha512, SHA512_HASH_SIZE) ||
CRYPT_MEMCMP(hash_all, hash_sha512,
SHA512_HASH_SIZE)) { ali_crypto_print_data("sha512", hash,
SHA512_HASH_SIZE); ali_crypto_print_data("sha512", hash_all,
SHA512_HASH_SIZE); GO_RET(-1, "SHA512 test fail!\n"); } else {
CRYPT_INF("SHA512 test success!\n");
}
*/
} else if (type == MD5) {
if (CRYPT_MEMCMP(hash, hash_md5, MD5_HASH_SIZE) ||
CRYPT_MEMCMP(hash_all, hash_md5, MD5_HASH_SIZE)) {
ali_crypto_print_data("md5", hash, MD5_HASH_SIZE);
GO_RET(-1, "md5 test fail!\n");
} else {
CRYPT_INF("md5 test success!\n");
}
}
}
return 0;
_OUT:
if (hash_ctx) {
CRYPT_FREE(hash_ctx);
}
return -1;
}

View file

@ -0,0 +1,228 @@
/**
* Copyright (C) 2016 The YunOS Project. All rights reserved.
*/
#include "ali_crypto.h"
#include "ali_crypto_test.h"
#define TEST_KEY_SIZE (16)
#define TEST_DATA_SIZE (141)
static uint8_t test_key[TEST_KEY_SIZE] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x08, 0x07, 0x06, 0x05,
0x04, 0x03, 0x02, 0x01 };
static uint8_t _g_test_data[TEST_DATA_SIZE] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0x0d,
0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x13
};
/* openssl calculated result */
static uint8_t hmac_md5[MD5_HASH_SIZE] = { 0x20, 0xc5, 0xc6, 0xa7, 0x17, 0x6f,
0x27, 0xfe, 0x7a, 0x1d, 0x7e, 0x85,
0x5b, 0x5c, 0xa8, 0xc4 };
static uint8_t hmac_sha1[SHA1_HASH_SIZE] = { 0xe5, 0xdf, 0x48, 0xfe, 0x08,
0x91, 0x37, 0xa2, 0x55, 0x95,
0xbc, 0xf3, 0x76, 0x06, 0x92,
0x1e, 0x54, 0x98, 0xe0, 0x4b };
static uint8_t hmac_sha224[SHA224_HASH_SIZE] = {
0x1c, 0x47, 0x04, 0x45, 0xcd, 0xee, 0x00, 0x9a, 0x46, 0x66,
0x2e, 0x1e, 0xb7, 0x04, 0xc9, 0x8f, 0xd5, 0xbb, 0x90, 0x38,
0xbb, 0x93, 0x9a, 0x08, 0x47, 0xe7, 0x17, 0xca
};
static uint8_t hmac_sha256[SHA256_HASH_SIZE] = {
0xd5, 0xce, 0x2b, 0x95, 0xa3, 0xea, 0x70, 0x69, 0x6a, 0x29, 0xbf,
0xe7, 0x9b, 0xa2, 0xc9, 0x18, 0x27, 0x4d, 0x3f, 0xd7, 0xae, 0xe7,
0x81, 0x88, 0x2a, 0xe7, 0x19, 0x68, 0x47, 0x07, 0xa3, 0xb3
};
static uint8_t hmac_sha384[SHA384_HASH_SIZE] = {
0x26, 0x10, 0x72, 0x0d, 0xf1, 0x70, 0x03, 0x40, 0x65, 0x4c, 0x94, 0xf5,
0x45, 0xbc, 0xbc, 0xcc, 0xd4, 0x17, 0xf5, 0x70, 0x81, 0xda, 0x91, 0x99,
0xe0, 0xca, 0x7a, 0x8c, 0x9c, 0x15, 0x5b, 0x22, 0xe8, 0xaa, 0x1c, 0xcf,
0xef, 0xe4, 0x6e, 0xf2, 0xfb, 0xdb, 0x6a, 0xf2, 0x22, 0xae, 0x70, 0x78
};
static uint8_t hmac_sha512[SHA512_HASH_SIZE] = {
0x66, 0x43, 0xba, 0xfc, 0x6f, 0x9e, 0xa3, 0xf8, 0xbf, 0x3d, 0x46,
0x46, 0x26, 0xfb, 0x8f, 0xa4, 0x04, 0x4c, 0x8a, 0x07, 0xfa, 0xac,
0x1d, 0x16, 0x33, 0xe6, 0xbd, 0x65, 0x01, 0xe2, 0x44, 0x83, 0x45,
0x78, 0x25, 0xbc, 0x42, 0x4b, 0x25, 0x85, 0xe0, 0x2a, 0xb4, 0xff,
0x6b, 0x92, 0x0c, 0x50, 0xdb, 0x0c, 0x00, 0x6e, 0x4d, 0xd5, 0x5c,
0xcc, 0x4e, 0x9f, 0xba, 0x3f, 0xfd, 0x81, 0x3f, 0x0b
};
int ali_crypto_hmac_test(void)
{
ali_crypto_result result;
hash_type_t type;
void * hmac_ctx = NULL;
uint32_t hmac_ctx_size;
uint8_t md[MAX_HASH_SIZE];
/* for gcov coverage */
result = ali_hmac_get_ctx_size(MD5, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
result = ali_hmac_get_ctx_size(HASH_NONE, (size_t *)(&hmac_ctx_size));
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
result = ali_hmac_init(MD5, test_key, TEST_KEY_SIZE, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
result = ali_hmac_init(HASH_NONE, test_key, TEST_KEY_SIZE, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
result = ali_hmac_update(_g_test_data, 13, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
result = ali_hmac_final(md, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
result = ali_hmac_digest(MD5, NULL, TEST_KEY_SIZE, _g_test_data,
TEST_DATA_SIZE, md);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
result = ali_hmac_reset(NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
result = ali_hmac_copy_context(NULL, NULL);
if (result == ALI_CRYPTO_SUCCESS) {
return -1;
}
for (type = SHA1; type <= MD5; type++) {
if (type == SHA512 || type == SHA384) {
CRYPT_INF("hmac not support hash 384 512\n");
continue;
}
result = ali_hmac_get_ctx_size(type, (size_t *)(&hmac_ctx_size));
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "get ctx size fail(%08x)\n", result);
}
hmac_ctx = CRYPT_MALLOC(hmac_ctx_size);
if (hmac_ctx == NULL) {
GO_RET(ALI_CRYPTO_OUTOFMEM, "kmalloc(%08x) fail\n",
(int)hmac_ctx_size);
}
CRYPT_MEMSET(hmac_ctx, 0, hmac_ctx_size);
{
result = ali_hmac_init(type, test_key, TEST_KEY_SIZE, hmac_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "init fail(%08x)", result);
}
/* for gcov coverage */
result = ali_hmac_update(NULL, 13, hmac_ctx);
if (result == ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
result = ali_hmac_update(_g_test_data, 13, hmac_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "update 1th fail(%08x)", result);
}
result = ali_hmac_update(_g_test_data + 13, 63, hmac_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "update 2th fail(%08x)", result);
}
result = ali_hmac_update(_g_test_data + 13 + 63, 65, hmac_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "update 3th fail(%08x)", result);
}
result = ali_hmac_final(md, hmac_ctx);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "final fail(%08x)", result);
}
/* for gcov coverage */
result = ali_hmac_digest(HASH_NONE, test_key, TEST_KEY_SIZE,
_g_test_data, TEST_DATA_SIZE, md);
if (result == ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
result = ali_hmac_digest(type, test_key, TEST_KEY_SIZE,
_g_test_data, TEST_DATA_SIZE, md);
if (result != ALI_CRYPTO_SUCCESS) {
GO_RET(result, "digest fail(%08x)", result);
}
}
ali_hmac_copy_context(hmac_ctx, hmac_ctx);
ali_hmac_reset(hmac_ctx);
CRYPT_FREE(hmac_ctx);
hmac_ctx = NULL;
if (type == SHA1) {
if (CRYPT_MEMCMP(md, hmac_sha1, SHA1_HASH_SIZE)) {
ali_crypto_print_data("hmac-sha1", md, SHA1_HASH_SIZE);
GO_RET(-1, "HMAC-SHA1 test fail!\n");
} else {
CRYPT_INF("HMAC-SHA1 test success!\n");
}
} else if (type == SHA224) {
if (CRYPT_MEMCMP(md, hmac_sha224, SHA224_HASH_SIZE)) {
ali_crypto_print_data("hmac-sha224", md, SHA224_HASH_SIZE);
GO_RET(-1, "HMAC-SHA224 test fail!\n");
} else {
CRYPT_INF("HMAC-SHA224 test success!\n");
}
} else if (type == SHA256) {
if (CRYPT_MEMCMP(md, hmac_sha256, SHA256_HASH_SIZE)) {
ali_crypto_print_data("hmac-sha256", md, SHA256_HASH_SIZE);
GO_RET(-1, "HMAC-SHA256 test fail!\n");
} else {
CRYPT_INF("HMAC-SHA256 test success!\n");
}
/* } else if (type == SHA384) {
if(CRYPT_MEMCMP(md, hmac_sha384, SHA384_HASH_SIZE)) {
ali_crypto_print_data("hmac-sha384", md,
SHA384_HASH_SIZE); GO_RET(-1, "HMAC-SHA384 test fail!\n"); } else
{ CRYPT_INF("HMAC-SHA384 test success!\n");
}
} else if (type == SHA512) {
if(CRYPT_MEMCMP(md, hmac_sha512, SHA512_HASH_SIZE)) {
ali_crypto_print_data("hmac-sha512", md,
SHA512_HASH_SIZE); GO_RET(-1, "HMAC-SHA512 test fail!\n"); } else
{ CRYPT_INF("HMAC-SHA512 test success!\n");
}
*/
} else if (type == MD5) {
if (CRYPT_MEMCMP(md, hmac_md5, MD5_HASH_SIZE)) {
ali_crypto_print_data("hmac-md5", md, MD5_HASH_SIZE);
GO_RET(-1, "HMAC-MD5 test fail!\n");
} else {
CRYPT_INF("HMAC-MD5 test success!\n");
}
}
}
return 0;
_OUT:
if (hmac_ctx) {
CRYPT_FREE(hmac_ctx);
}
return -1;
}

View file

@ -0,0 +1,51 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#include "ali_crypto_test.h"
int ali_crypto_rand_test(void)
{
uint32_t i = 0;
uint8_t seed[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
size_t seed_len = 16;
uint8_t tmp_buf[32];
uint8_t rand_buf[32];
size_t rand_len = 32;
ali_crypto_result result;
result = ali_seed(seed, seed_len);
if (result != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "ali_seed fail(%08x)\n", result);
}
/* for gcov coverage */
result = ali_rand_gen(NULL, rand_len);
if (result == ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "gen rand fail(%08x)\n", result);
}
result = ali_rand_gen(rand_buf, rand_len);
if (result != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "gen rand fail(%08x)\n", result);
}
while (i++ < 10) {
CRYPT_MEMCPY(tmp_buf, rand_buf, rand_len);
result = ali_rand_gen(rand_buf, rand_len);
if (result != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "gen rand fail(%08x)\n", result);
}
if (!CRYPT_MEMCMP(tmp_buf, rand_buf, rand_len)) {
ali_crypto_print_data("tmp_buf", tmp_buf, rand_len);
ali_crypto_print_data("rand_buf", rand_buf, rand_len);
PRINT_RET(-1, "RAND test fail!\n");
}
}
CRYPT_INF("RAND test success!\n");
return 0;
}

View file

@ -0,0 +1,779 @@
/**
* Copyright (C) 2016 The YunOS Project. All rights reserved.
*/
#include "ali_crypto.h"
#include "ali_crypto_test.h"
#define RSA_KEY_LEN (128)
static uint8_t RSA_1024_N[128] = {
0xa5, 0x6e, 0x4a, 0x0e, 0x70, 0x10, 0x17, 0x58, 0x9a, 0x51, 0x87, 0xdc,
0x7e, 0xa8, 0x41, 0xd1, 0x56, 0xf2, 0xec, 0x0e, 0x36, 0xad, 0x52, 0xa4,
0x4d, 0xfe, 0xb1, 0xe6, 0x1f, 0x7a, 0xd9, 0x91, 0xd8, 0xc5, 0x10, 0x56,
0xff, 0xed, 0xb1, 0x62, 0xb4, 0xc0, 0xf2, 0x83, 0xa1, 0x2a, 0x88, 0xa3,
0x94, 0xdf, 0xf5, 0x26, 0xab, 0x72, 0x91, 0xcb, 0xb3, 0x07, 0xce, 0xab,
0xfc, 0xe0, 0xb1, 0xdf, 0xd5, 0xcd, 0x95, 0x08, 0x09, 0x6d, 0x5b, 0x2b,
0x8b, 0x6d, 0xf5, 0xd6, 0x71, 0xef, 0x63, 0x77, 0xc0, 0x92, 0x1c, 0xb2,
0x3c, 0x27, 0x0a, 0x70, 0xe2, 0x59, 0x8e, 0x6f, 0xf8, 0x9d, 0x19, 0xf1,
0x05, 0xac, 0xc2, 0xd3, 0xf0, 0xcb, 0x35, 0xf2, 0x92, 0x80, 0xe1, 0x38,
0x6b, 0x6f, 0x64, 0xc4, 0xef, 0x22, 0xe1, 0xe1, 0xf2, 0x0d, 0x0c, 0xe8,
0xcf, 0xfb, 0x22, 0x49, 0xbd, 0x9a, 0x21, 0x37
};
static uint8_t RSA_1024_E[3] = { 0x01, 0x00, 0x01 };
static uint8_t RSA_1024_D[128] = {
0x33, 0xa5, 0x04, 0x2a, 0x90, 0xb2, 0x7d, 0x4f, 0x54, 0x51, 0xca, 0x9b,
0xbb, 0xd0, 0xb4, 0x47, 0x71, 0xa1, 0x01, 0xaf, 0x88, 0x43, 0x40, 0xae,
0xf9, 0x88, 0x5f, 0x2a, 0x4b, 0xbe, 0x92, 0xe8, 0x94, 0xa7, 0x24, 0xac,
0x3c, 0x56, 0x8c, 0x8f, 0x97, 0x85, 0x3a, 0xd0, 0x7c, 0x02, 0x66, 0xc8,
0xc6, 0xa3, 0xca, 0x09, 0x29, 0xf1, 0xe8, 0xf1, 0x12, 0x31, 0x88, 0x44,
0x29, 0xfc, 0x4d, 0x9a, 0xe5, 0x5f, 0xee, 0x89, 0x6a, 0x10, 0xce, 0x70,
0x7c, 0x3e, 0xd7, 0xe7, 0x34, 0xe4, 0x47, 0x27, 0xa3, 0x95, 0x74, 0x50,
0x1a, 0x53, 0x26, 0x83, 0x10, 0x9c, 0x2a, 0xba, 0xca, 0xba, 0x28, 0x3c,
0x31, 0xb4, 0xbd, 0x2f, 0x53, 0xc3, 0xee, 0x37, 0xe3, 0x52, 0xce, 0xe3,
0x4f, 0x9e, 0x50, 0x3b, 0xd8, 0x0c, 0x06, 0x22, 0xad, 0x79, 0xc6, 0xdc,
0xee, 0x88, 0x35, 0x47, 0xc6, 0xa3, 0xb3, 0x25
};
static int _rsa_test_gen_key(void)
{
int ret, result;
uint8_t rsa_n[RSA_KEY_LEN];
uint8_t rsa_e[RSA_KEY_LEN];
uint8_t rsa_d[RSA_KEY_LEN];
uint8_t rsa_p[RSA_KEY_LEN];
uint8_t rsa_q[RSA_KEY_LEN];
uint8_t rsa_dp[RSA_KEY_LEN];
uint8_t rsa_dq[RSA_KEY_LEN];
uint8_t rsa_qp[RSA_KEY_LEN];
uint32_t n_size = RSA_KEY_LEN;
uint32_t e_size = RSA_KEY_LEN;
uint32_t d_size = RSA_KEY_LEN;
uint32_t p_size = RSA_KEY_LEN;
uint32_t q_size = RSA_KEY_LEN;
uint32_t dp_size = RSA_KEY_LEN;
uint32_t dq_size = RSA_KEY_LEN;
uint32_t qp_size = RSA_KEY_LEN;
uint8_t * pub_key = NULL;
uint8_t * key_pair = NULL;
size_t pub_key_len, key_pair_len;
uint8_t src_data[RSA_KEY_LEN];
uint8_t ciphertext[RSA_KEY_LEN];
uint8_t plaintext[RSA_KEY_LEN];
size_t src_size, dst_size;
rsa_padding_t rsa_padding;
(void)result;
CRYPT_INF("rsa gen key test!\n");
/* for gcov coverage */
ret = ali_rsa_get_pubkey_size(RSA_KEY_LEN << 3, NULL);
if (ret == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
ret = ali_rsa_get_pubkey_size(255, &pub_key_len);
if (ret == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
ret = ali_rsa_get_keypair_size(RSA_KEY_LEN << 3, NULL);
if (ret == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
ret = ali_rsa_get_keypair_size(255, &key_pair_len);
if (ret == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
ret = ali_rsa_init_keypair(RSA_KEY_LEN << 3, rsa_n, n_size, rsa_e, e_size,
rsa_d, d_size, NULL, 0, NULL, 0, NULL, 0, NULL,
0, NULL, 0, NULL);
if (ret == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
ret =
ali_rsa_init_pubkey(RSA_KEY_LEN << 3, rsa_n, n_size, rsa_e, e_size, NULL);
if (ret == ALI_CRYPTO_SUCCESS) {
return -1;
}
/* for gcov coverage */
ret =
ali_rsa_init_pubkey(RSA_KEY_LEN << 3, rsa_n, n_size, rsa_e, e_size, NULL);
if (ret == ALI_CRYPTO_SUCCESS) {
return -1;
}
ret = ali_rsa_get_pubkey_size(RSA_KEY_LEN << 3, &pub_key_len);
if (ret != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "init_key: get pubkey size fail(%08x)\n", ret)
}
ret = ali_rsa_get_keypair_size(RSA_KEY_LEN << 3, &key_pair_len);
if (ret != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "init_key: get keypair size fail(%08x)\n", ret)
}
pub_key = CRYPT_MALLOC(pub_key_len);
if (pub_key == NULL) {
GO_RET(-1, "init_key: malloc(%d) fail\n", (int)pub_key_len);
}
key_pair = CRYPT_MALLOC(key_pair_len);
if (pub_key == NULL) {
GO_RET(-1, "init_key: malloc(%d) fail\n", (int)pub_key_len);
}
/* for gcov coverage */
ret = ali_rsa_gen_keypair(RSA_KEY_LEN << 3, NULL, 0, NULL);
if (ret == ALI_CRYPTO_SUCCESS) {
GO_RET(ALI_CRYPTO_ERROR, "ali_rsa_gen_keypair: not expect\n");
}
ret =
ali_rsa_gen_keypair(RSA_KEY_LEN << 3, NULL, 0, (rsa_keypair_t *)key_pair);
if (ret != ALI_CRYPTO_SUCCESS) {
GO_RET(-1, "init_key: gen keypair fail(%08x)\n", ret);
}
/* for gcov coverage */
ret = ali_rsa_get_key_attr(RSA_MODULUS, NULL, rsa_n, (size_t *)&n_size);
if (ret == ALI_CRYPTO_SUCCESS) {
GO_RET(ALI_CRYPTO_ERROR, "ali_rsa_get_key_attr: not expect\n");
}
/* for gcov coverage */
ret = ali_rsa_get_key_attr((rsa_key_attr_t)-1, (rsa_keypair_t *)key_pair,
rsa_n, (size_t *)&n_size);
if (ret == ALI_CRYPTO_SUCCESS) {
GO_RET(ALI_CRYPTO_ERROR, "ali_rsa_get_key_attr: not expect\n");
}
/* get key attrs */
ret = ali_rsa_get_key_attr(RSA_MODULUS, (rsa_keypair_t *)key_pair, rsa_n,
(size_t *)&n_size);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
ret = ali_rsa_get_key_attr(RSA_PUBLIC_EXPONENT, (rsa_keypair_t *)key_pair,
rsa_e, (size_t *)&e_size);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
ret = ali_rsa_get_key_attr(RSA_PRIVATE_EXPONENT, (rsa_keypair_t *)key_pair,
rsa_d, (size_t *)&d_size);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
ret = ali_rsa_get_key_attr(RSA_PRIME1, (rsa_keypair_t *)key_pair, rsa_p,
(size_t *)&p_size);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
ret = ali_rsa_get_key_attr(RSA_PRIME2, (rsa_keypair_t *)key_pair, rsa_q,
(size_t *)&q_size);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
ret = ali_rsa_get_key_attr(RSA_EXPONENT1, (rsa_keypair_t *)key_pair, rsa_dp,
(size_t *)&dp_size);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
ret = ali_rsa_get_key_attr(RSA_EXPONENT2, (rsa_keypair_t *)key_pair, rsa_dq,
(size_t *)&dq_size);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
ret = ali_rsa_get_key_attr(RSA_COEFFICIENT, (rsa_keypair_t *)key_pair,
rsa_qp, (size_t *)&qp_size);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
/* for gcov coverage */
ret = ali_rsa_init_keypair(RSA_KEY_LEN << 3, rsa_n, 129, rsa_e, e_size,
rsa_d, d_size, NULL, 0, NULL, 0, NULL, 0, NULL,
0, NULL, 0, (rsa_keypair_t *)key_pair);
if (ret == ALI_CRYPTO_SUCCESS) {
GO_RET(ALI_CRYPTO_ERROR, "ali_rsa_init_keypair: not expect\n");
}
/* for gcov coverage */
ret = ali_rsa_init_keypair(RSA_KEY_LEN << 3, rsa_n, n_size, rsa_e, e_size,
rsa_d, d_size, rsa_p, p_size, rsa_q, q_size,
rsa_dp, dp_size, rsa_dq, dq_size, rsa_qp,
qp_size, (rsa_keypair_t *)key_pair);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
CRYPT_MEMSET(key_pair, 0, key_pair_len);
ret = ali_rsa_init_keypair(RSA_KEY_LEN << 3, rsa_n, n_size, rsa_e, e_size,
rsa_d, d_size, NULL, 0, NULL, 0, NULL, 0, NULL,
0, NULL, 0, (rsa_keypair_t *)key_pair);
if (ret != ALI_CRYPTO_SUCCESS) {
GO_RET(ret, "init_key: init keypair fail(%08x)\n", ret);
}
/* for gcov coverage */
ret = ali_rsa_init_pubkey(RSA_KEY_LEN << 3, rsa_n, 129, rsa_e, e_size,
(rsa_pubkey_t *)pub_key);
if (ret == ALI_CRYPTO_SUCCESS) {
GO_RET(ALI_CRYPTO_ERROR, "ali_rsa_init_pubkey: not expect\n");
}
CRYPT_MEMSET(pub_key, 0, pub_key_len);
ret = ali_rsa_init_pubkey(RSA_KEY_LEN << 3, rsa_n, n_size, rsa_e, e_size,
(rsa_pubkey_t *)pub_key);
if (ret != ALI_CRYPTO_SUCCESS) {
GO_RET(ALI_CRYPTO_ERROR, "init_key: init pub_key fail(%08x)\n", ret);
}
rsa_padding.type = RSAES_PKCS1_V1_5;
/* for gcov coverage */
src_size = RSA_KEY_LEN;
dst_size = RSA_KEY_LEN;
ret = ali_rsa_public_encrypt((const rsa_pubkey_t *)NULL, src_data, src_size,
ciphertext, &dst_size, rsa_padding);
if (ret == ALI_CRYPTO_SUCCESS) {
GO_RET(ALI_CRYPTO_ERROR, "ali_rsa_public_encrypt: not expect\n");
}
/* for gcov coverage */
ret = ali_rsa_private_decrypt(NULL, ciphertext, RSA_KEY_LEN, plaintext,
&dst_size, rsa_padding);
if (ret == ALI_CRYPTO_SUCCESS) {
GO_RET(ALI_CRYPTO_ERROR, "ali_rsa_private_decrypt: not expect\n");
}
CRYPT_MEMSET(&rsa_padding, 0, sizeof(rsa_padding_t));
rsa_padding.type = RSAES_PKCS1_V1_5;
src_size = RSA_KEY_LEN - 11;
CRYPT_MEMSET(src_data, 0xa, src_size);
dst_size = RSA_KEY_LEN;
ret = ali_rsa_public_encrypt((const rsa_pubkey_t *)pub_key, src_data,
src_size, ciphertext, &dst_size, rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS) {
GO_RET(ALI_CRYPTO_ERROR, "ali_rsa_public_encrypt: rsa_v1_5 fail %d\n",
ret);
}
ret =
ali_rsa_private_decrypt((const rsa_keypair_t *)key_pair, ciphertext,
RSA_KEY_LEN, plaintext, &dst_size, rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS || dst_size != src_size) {
GO_RET(ALI_CRYPTO_ERROR, "ali_rsa_private_decrypt: rsa_v1_5 fail %d\n",
ret);
}
if (CRYPT_MEMCMP(src_data, plaintext, src_size)) {
ali_crypto_print_data("pliantext", plaintext, src_size);
ali_crypto_print_data("ciphertext", ciphertext, dst_size);
PRINT_RET(-1, "RSA encrypt/decrypt with PKCS1_V1_5 test fail!\n");
} else {
CRYPT_INF("RSA encrypt/decrypt with PKCS1_V1_5 test success!\n");
}
CRYPT_FREE(pub_key);
CRYPT_FREE(key_pair);
return 0;
_OUT:
if (pub_key) {
CRYPT_FREE(pub_key);
}
if (key_pair) {
CRYPT_FREE(key_pair);
}
return -1;
}
static int _ali_crypto_init_key(rsa_keypair_t **keypair, rsa_pubkey_t **pubkey)
{
ali_crypto_result ret, result;
uint8_t rsa_n[RSA_KEY_LEN];
uint8_t rsa_e[RSA_KEY_LEN];
uint8_t rsa_d[RSA_KEY_LEN];
uint32_t n_size = RSA_KEY_LEN;
uint32_t e_size = RSA_KEY_LEN;
uint32_t d_size = RSA_KEY_LEN;
uint8_t * pub_key = NULL;
uint8_t * key_pair = NULL;
size_t pub_key_len, key_pair_len;
(void)e_size;
(void)rsa_d;
(void)rsa_e;
(void)rsa_n;
(void)result;
if (keypair == NULL || pubkey == NULL) {
PRINT_RET(-1, "init_key: invalid input args!\n");
}
ret = ali_rsa_get_pubkey_size(RSA_KEY_LEN << 3, &pub_key_len);
if (ret != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "init_key: get pubkey size fail(%08x)\n", ret);
}
ret = ali_rsa_get_keypair_size(RSA_KEY_LEN << 3, &key_pair_len);
if (ret != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "init_key: get keypair size fail(%08x)\n", ret);
}
pub_key = CRYPT_MALLOC(pub_key_len);
if (pub_key == NULL) {
GO_RET(-1, "init_key: malloc(%d) fail\n", (int)pub_key_len);
}
key_pair = CRYPT_MALLOC(key_pair_len);
if (pub_key == NULL) {
GO_RET(-1, "init_key: malloc(%d) fail\n", (int)pub_key_len);
}
#if 0
ret = ali_rsa_gen_keypair(RSA_KEY_LEN << 3, NULL, 0, (rsa_keypair_t *)key_pair);
if (ret != ALI_CRYPTO_SUCCESS) {
CRYPT_ERR("init_key: gen keypair fail(%08x)\n", ret);
goto _OUT;
}
/* get key attrs */
ret = ali_rsa_get_key_attr(RSA_MODULUS,
(rsa_keypair_t *)key_pair, rsa_n, (size_t *)&n_size);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
ret = ali_rsa_get_key_attr(RSA_PUBLIC_EXPONENT,
(rsa_keypair_t *)key_pair, rsa_e, (size_t *)&e_size);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
ret = ali_rsa_get_key_attr(RSA_PRIVATE_EXPONENT,
(rsa_keypair_t *)key_pair, rsa_d, (size_t *)&d_size);
if (ret != ALI_CRYPTO_SUCCESS) {
goto _OUT;
}
CRYPT_MEMSET(key_pair, 0, key_pair_len);
ret = ali_rsa_init_keypair(RSA_KEY_LEN << 3,
rsa_n, n_size, rsa_e, e_size, rsa_d, d_size,
NULL , 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, (rsa_keypair_t *)key_pair);
if (ret != ALI_CRYPTO_SUCCESS) {
CRYPT_ERR("init_key: init keypair fail(%08x)\n", ret);
goto _OUT;
}
CRYPT_MEMSET(pub_key, 0, pub_key_len);
ret = ali_rsa_init_pubkey(RSA_KEY_LEN << 3,
rsa_n, n_size, rsa_e, e_size, (rsa_pubkey_t *)pub_key);
if (ret != ALI_CRYPTO_SUCCESS) {
CRYPT_ERR("init_key: init pub_key fail(%08x)\n", ret);
goto _OUT;
}
#else
CRYPT_MEMSET(key_pair, 0, key_pair_len);
ret = ali_rsa_init_keypair(RSA_KEY_LEN << 3, RSA_1024_N, n_size, RSA_1024_E,
3, RSA_1024_D, d_size, NULL, 0, NULL, 0, NULL, 0,
NULL, 0, NULL, 0, (rsa_keypair_t *)key_pair);
if (ret != ALI_CRYPTO_SUCCESS) {
GO_RET(-1, "init_key: init keypair fail(%08x)\n", ret);
}
CRYPT_MEMSET(pub_key, 0, pub_key_len);
ret = ali_rsa_init_pubkey(RSA_KEY_LEN << 3, RSA_1024_N, n_size, RSA_1024_E,
3, (rsa_pubkey_t *)pub_key);
if (ret != ALI_CRYPTO_SUCCESS) {
GO_RET(-1, "init_key: init pub_key fail(%08x)\n", ret);
}
#endif
*pubkey = (rsa_pubkey_t *)pub_key;
*keypair = (rsa_keypair_t *)key_pair;
return 0;
_OUT:
if (pub_key) {
CRYPT_FREE(pub_key);
}
if (key_pair) {
CRYPT_FREE(key_pair);
}
return -1;
}
#if 0
static int _ali_crypto_encrypt_decrypt_nopad(
rsa_pubkey_t *pubkey, rsa_keypair_t *keypair)
{
ali_crypto_result ret;
uint8_t src_data[RSA_KEY_LEN];
uint8_t plaintext[RSA_KEY_LEN];
uint8_t ciphertext[RSA_KEY_LEN];
size_t src_size, dst_size;
rsa_padding_t rsa_padding;
if (pubkey == NULL || keypair == NULL) {
CRYPT_ERR("rsa_nopad: invalid input args!\n");
return -1;
}
rsa_padding.type = RSA_NOPAD;
src_size = RSA_KEY_LEN;
CRYPT_MEMSET(src_data, 0xa, src_size);
dst_size = RSA_KEY_LEN;
ret = ali_rsa_public_encrypt(pubkey, src_data, src_size,
ciphertext, &dst_size, rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS) {
CRYPT_ERR("rsa_nopad: public encrypt fail(%08x)\n", ret);
return -1;
}
ret = ali_rsa_private_decrypt(keypair, ciphertext, RSA_KEY_LEN,
plaintext, &dst_size, rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS || dst_size != src_size) {
CRYPT_ERR("rsa_nopad: private decrypt fail(%08x)\n", ret);
return -1;
}
if (CRYPT_MEMCMP(src_data, plaintext, src_size)) {
CRYPT_ERR("RSA encrypt/decrypt with no-padding test fail!\n");
ali_crypto_print_data("pliantext", plaintext, src_size);
ali_crypto_print_data("ciphertext", ciphertext, dst_size);
} else {
CRYPT_INF("RSA encrypt/decrypt with no-padding test success!\n");
}
return 0;
}
#endif
static int _ali_crypto_encrypt_decrypt_v1_5(rsa_pubkey_t * pubkey,
rsa_keypair_t *keypair)
{
ali_crypto_result ret;
uint8_t src_data[RSA_KEY_LEN];
uint8_t plaintext[RSA_KEY_LEN];
uint8_t ciphertext[RSA_KEY_LEN];
size_t src_size, dst_size;
rsa_padding_t rsa_padding;
if (pubkey == NULL || keypair == NULL) {
PRINT_RET(-1, "rsa_v1_5: invalid input args!\n");
}
CRYPT_MEMSET(&rsa_padding, 0, sizeof(rsa_padding_t));
rsa_padding.type = RSAES_PKCS1_V1_5;
src_size = RSA_KEY_LEN - 11;
CRYPT_MEMSET(src_data, 0xa, src_size);
dst_size = RSA_KEY_LEN;
ret = ali_rsa_public_encrypt(pubkey, src_data, src_size, ciphertext,
&dst_size, rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "rsa_v1_5: public encrypt fail(%08x)\n", ret);
}
/* for gcov coverage */
ret = ali_rsa_public_encrypt(pubkey, src_data, RSA_KEY_LEN, ciphertext,
&dst_size, rsa_padding);
if (ret == ALI_CRYPTO_SUCCESS) {
return -1;
}
ret = ali_rsa_private_decrypt(keypair, ciphertext, RSA_KEY_LEN, plaintext,
&dst_size, rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS || dst_size != src_size) {
PRINT_RET(-1, "rsa_v1_5: public decrypt fail(%08x)\n", ret);
}
if (CRYPT_MEMCMP(src_data, plaintext, src_size)) {
ali_crypto_print_data("pliantext", plaintext, src_size);
ali_crypto_print_data("ciphertext", ciphertext, dst_size);
PRINT_RET(-1, "RSA encrypt/decrypt with PKCS1_V1_5 test fail!\n");
} else {
CRYPT_INF("RSA encrypt/decrypt with PKCS1_V1_5 test success!\n");
}
return 0;
}
static int _ali_crypto_encrypt_decrypt_oaep(rsa_pubkey_t * pubkey,
rsa_keypair_t *keypair)
{
ali_crypto_result ret;
hash_type_t hash_type;
rsa_padding_t rsa_padding;
uint8_t src_data[RSA_KEY_LEN];
uint8_t plaintext[RSA_KEY_LEN];
uint8_t ciphertext[RSA_KEY_LEN];
size_t src_size, dst_size;
// uint8_t lparam[] = {0xe1, 0xe2, 0xe2, 0xe4, 0xe5};
if (pubkey == NULL || keypair == NULL) {
CRYPT_ERR("rsa_v1_5: invalid input args!\n");
PRINT_RET(-1, "rsa_v1_5: invalid input args!\n");
}
rsa_padding.type = RSAES_PKCS1_OAEP_MGF1;
for (hash_type = SHA1; hash_type <= MD5; hash_type++) {
if (hash_type == SHA512 || hash_type == SHA384) {
CRYPT_INF("rsa oeap not support hash 384 512\n");
continue;
}
/*
if (2*HASH_SIZE(hash_type) >= RSA_KEY_LEN - 2) {
continue;
}
*/
src_size = RSA_KEY_LEN - 2 * HASH_SIZE(hash_type) - 2;
CRYPT_MEMSET(src_data, 0xa, src_size);
/* without lparam */
rsa_padding.pad.rsaes_oaep.type = hash_type;
dst_size = RSA_KEY_LEN;
ret = ali_rsa_public_encrypt(pubkey, src_data, src_size, ciphertext,
&dst_size, rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS) {
PRINT_RET(
-1, "rsa_oaep: public encrypt(without lparam) fail(%08x)\n", ret);
}
ret = ali_rsa_private_decrypt(keypair, ciphertext, RSA_KEY_LEN,
plaintext, &dst_size, rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS || dst_size != src_size) {
PRINT_RET(-1,
"rsa_oaep: private decrypt(without lparam) fail(%08x)\n",
ret);
}
if (CRYPT_MEMCMP(src_data, plaintext, src_size)) {
ali_crypto_print_data("pliantext", plaintext, src_size);
ali_crypto_print_data("ciphertext", ciphertext, dst_size);
PRINT_RET(-1, "RSA encrypt/decrypt with PKCS1_OAEP(without lparam) "
"test fail!\n");
} else {
CRYPT_INF("RSA encrypt/decrypt with PKCS1_OAEP(without lparam) "
"test success!\n");
}
/* with lparam */
rsa_padding.pad.rsaes_oaep.type = hash_type;
dst_size = RSA_KEY_LEN;
ret = ali_rsa_public_encrypt(pubkey, src_data, src_size, ciphertext,
&dst_size, rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "rsa_oaep: public encrypt(with lparam) fail(%08x)\n",
ret);
}
ret = ali_rsa_private_decrypt(keypair, ciphertext, RSA_KEY_LEN,
plaintext, &dst_size, rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS || dst_size != src_size) {
PRINT_RET(-1, "rsa_oaep: private decrypt(with lparam) fail(%08x)\n",
ret);
}
if (CRYPT_MEMCMP(src_data, plaintext, src_size)) {
ali_crypto_print_data("pliantext", plaintext, src_size);
ali_crypto_print_data("ciphertext", ciphertext, dst_size);
PRINT_RET(
-1,
"RSA encrypt/decrypt with PKCS1_OAEP(with lparam) test fail!\n");
} else {
CRYPT_INF("RSA encrypt/decrypt with PKCS1_OAEP(with lparam) test "
"success!\n");
}
}
return 0;
}
static int _ali_crypto_sign_verify_v1_5(rsa_pubkey_t * pubkey,
rsa_keypair_t *keypair)
{
bool result1, result2;
ali_crypto_result ret;
hash_type_t hash_type;
uint8_t src_data[RSA_KEY_LEN];
uint8_t signature[RSA_KEY_LEN];
size_t src_size, dst_size;
rsa_padding_t rsa_padding;
if (pubkey == NULL || keypair == NULL) {
PRINT_RET(-1, "rsa_v1_5: invalid input args!\n");
}
rsa_padding.type = RSASSA_PKCS1_V1_5;
for (hash_type = SHA1; hash_type <= MD5; hash_type++) {
rsa_padding.pad.rsassa_v1_5.type = hash_type;
if (hash_type == SHA512 || hash_type == SHA384) {
CRYPT_INF("mbedtls rsa V1.5 not support hash 384 512\n");
continue;
}
#if 0
if (HASH_SIZE(hash_type) + 11 > RSA_KEY_LEN) {
continue;
}
#endif
src_size = HASH_SIZE(hash_type);
CRYPT_MEMSET(src_data, 0xa, src_size);
dst_size = RSA_KEY_LEN;
ret = ali_rsa_sign(keypair, src_data, src_size, signature, &dst_size,
rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "rsa_v1_5: sign fail(%08x)\n", ret);
}
ret = ali_rsa_verify(pubkey, src_data, src_size, signature, dst_size,
rsa_padding, &result1);
if (ret != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "rsa_v1_5: verify fail(%08x)\n", ret);
}
src_data[0] = src_data[0] ^ 0x1;
ret = ali_rsa_verify(pubkey, src_data, src_size, signature, dst_size,
rsa_padding, &result2);
if (ret == ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "rsa_v1_5: verify fail(%08x)\n", ret);
}
if (result1 == true && result2 == false) {
CRYPT_INF("RSA sign/verify with PKCS1_V1_5 success!\n");
} else {
PRINT_RET(-1, "RSA sign/verify with PKCS1_V1_5 fail!\n");
}
}
return 0;
}
static int _ali_crypto_sign_verify_pss(rsa_pubkey_t * pubkey,
rsa_keypair_t *keypair)
{
bool result1, result2;
ali_crypto_result ret;
hash_type_t hash_type;
uint8_t src_data[RSA_KEY_LEN];
uint8_t signature[RSA_KEY_LEN];
size_t src_size, dst_size;
rsa_padding_t rsa_padding;
if (pubkey == NULL || keypair == NULL) {
PRINT_RET(-1, "rsa_pss: invalid input args!\n");
}
rsa_padding.type = RSASSA_PKCS1_PSS_MGF1;
for (hash_type = SHA1; hash_type <= MD5; hash_type++) {
rsa_padding.pad.rsassa_pss.type = hash_type;
rsa_padding.pad.rsassa_pss.salt_len = 28;
if (hash_type == SHA512 || hash_type == SHA384) {
CRYPT_INF("mbedtls rsa pss not support hash 512\n");
continue;
}
#if 0
if (HASH_SIZE(hash_type) + 28 + 2 > RSA_KEY_LEN) {
continue;
}
#endif
src_size = HASH_SIZE(hash_type);
CRYPT_MEMSET(src_data, 0xa, src_size);
dst_size = RSA_KEY_LEN;
ret = ali_rsa_sign(keypair, src_data, src_size, signature, &dst_size,
rsa_padding);
if (ret != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "rsa_pss: sign fail(%08x)\n", ret);
}
ret = ali_rsa_verify(pubkey, src_data, src_size, signature, dst_size,
rsa_padding, &result1);
if (ret != ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "rsa_pss: verify fail(%08x)\n", ret);
}
src_data[0] = src_data[0] ^ 0x1;
ret = ali_rsa_verify(pubkey, src_data, src_size, signature, dst_size,
rsa_padding, &result2);
if (ret == ALI_CRYPTO_SUCCESS) {
PRINT_RET(-1, "rsa_pss: verify fail(%08x)\n", ret);
}
if (result1 == true && result2 == false) {
CRYPT_INF("RSA sign/verify with PKCS1_PSS_MGF1 success!\n");
} else {
PRINT_RET(-1, "RSA sign/verify with PKCS1_PSS_MGF1 fail!\n");
}
}
return 0;
}
int ali_crypto_rsa_test(void)
{
int ret;
rsa_pubkey_t * pubkey = NULL;
rsa_keypair_t *keypair = NULL;
ret = _rsa_test_gen_key();
if (ret < 0) {
goto _out;
}
ret = _ali_crypto_init_key(&keypair, &pubkey);
if (ret < 0) {
goto _out;
}
/* TODO */
#if 0
ret = _ali_crypto_encrypt_decrypt_nopad(pubkey, keypair);
if (ret < 0) {
goto _out;
}
#endif
ret = _ali_crypto_encrypt_decrypt_v1_5(pubkey, keypair);
if (ret < 0) {
goto _out;
}
ret = _ali_crypto_encrypt_decrypt_oaep(pubkey, keypair);
if (ret < 0) {
goto _out;
}
ret = _ali_crypto_sign_verify_v1_5(pubkey, keypair);
if (ret < 0) {
goto _out;
}
ret = _ali_crypto_sign_verify_pss(pubkey, keypair);
if (ret < 0) {
goto _out;
}
_out:
if (pubkey) {
CRYPT_FREE(pubkey);
}
if (keypair) {
CRYPT_FREE(keypair);
}
if (0 == ret) {
CRYPT_INF("================ALI crypto test SUCCESS!\n");
} else {
CRYPT_INF("================ALI crypto test FAIL!\n");
}
return ret;
}

View file

@ -0,0 +1,51 @@
/**
* Copyright (C) 2017 The YunOS Project. All rights reserved.
*/
#ifndef _ALI_CRYPTO_TEST_H_
#define _ALI_CRYPTO_TEST_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ali_crypto.h"
#define CRYPT_ERR(_f, ...) \
printf("E %s %d: "_f, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define CRYPT_INF(_f, ...) \
printf("I %s %d: "_f, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#ifdef MBEDTLS_IOT_PLAT_AOS
#include <aos/kernel.h>
#define CRYPT_MALLOC aos_malloc
#define CRYPT_FREE aos_free
#else
#define CRYPT_MALLOC malloc
#define CRYPT_FREE free
#endif
#define CRYPT_MEMSET memset
#define CRYPT_MEMCPY memcpy
#define CRYPT_MEMCMP memcmp
#define PRINT_RET(_ret, _f, ...) \
do { \
CRYPT_ERR(_f, ##__VA_ARGS__); \
return _ret; \
} while (0);
#define GO_RET(_ret, _f, ...) \
do { \
CRYPT_ERR(_f, ##__VA_ARGS__); \
result = _ret; \
goto _OUT; \
} while (0);
void ali_crypto_print_data(const char *name, uint8_t *data, size_t size);
int ali_crypto_hash_test(void);
int ali_crypto_hmac_test(void);
int ali_crypto_rand_test(void);
int ali_crypto_aes_test(void);
int ali_crypto_rsa_test(void);
#endif