1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5 #include "rkcrypto_common.h"
6 #include "test_utils.h"
7
is_no_multi_blocksize(uint32_t mode)8 bool is_no_multi_blocksize(uint32_t mode)
9 {
10 if (mode == RK_CIPHER_MODE_CTS ||
11 mode == RK_CIPHER_MODE_CTR ||
12 mode == RK_CIPHER_MODE_CFB ||
13 mode == RK_CIPHER_MODE_OFB)
14 return true;
15 else
16 return false;
17 }
18
test_get_rng(uint8_t * trn,uint32_t len)19 void test_get_rng(uint8_t *trn, uint32_t len)
20 {
21 static int init_flag;
22
23 if (!init_flag) {
24 srand(time(NULL));
25 init_flag = 1;
26 }
27
28 for (uint32_t i = 0; i < len; i++)
29 trn[i] = rand() & 0xff;
30 }
31
test_dump_hex(char * var_name,const uint8_t * data,uint32_t len)32 void test_dump_hex(char *var_name, const uint8_t *data, uint32_t len)
33 {
34 uint32_t i;
35 char buffer[256];
36 char *p;
37
38 printf("=================== %s [%u] ================\n", var_name, len);
39
40 p = buffer;
41 for (i = 0; i < len; i++) {
42 if (i % 16 == 0 && i != 0) {
43 printf("%s\n", buffer);
44 p = buffer;
45 memset(buffer, 0x00, sizeof(buffer));
46 }
47 p += snprintf(p, 256, "%02x ", data[i]);
48 }
49 printf("%s\n", buffer);
50 }
51
52 struct test_name_map {
53 uint32_t id;
54 const char *name;
55 };
56
57 static const struct test_name_map algo_map_tbl[] = {
58 {RK_ALGO_AES, "AES"},
59 {RK_ALGO_DES, "DES"},
60 {RK_ALGO_TDES, "TDES"},
61 {RK_ALGO_SM4, "SM4"},
62
63 {RK_ALGO_MD5, "MD5"},
64 {RK_ALGO_SHA1, "SHA1"},
65 {RK_ALGO_SHA256, "SHA256"},
66 {RK_ALGO_SHA224, "SHA224"},
67 {RK_ALGO_SHA512, "SHA512"},
68 {RK_ALGO_SHA384, "SHA384"},
69 {RK_ALGO_SHA512_224, "SHA512_224"},
70 {RK_ALGO_SHA512_256, "SHA512_256"},
71 {RK_ALGO_SM3, "SM3"},
72
73 {RK_ALGO_HMAC_MD5, "HMAC_MD5"},
74 {RK_ALGO_HMAC_SHA1, "HMAC_SHA1"},
75 {RK_ALGO_HMAC_SHA256, "HMAC_SHA256"},
76 {RK_ALGO_HMAC_SHA512, "HMAC_SHA512"},
77 {RK_ALGO_HMAC_SM3, "HMAC_SM3"},
78 {RK_ALGO_CMAC_SM4, "CMAC_SM4"},
79 {RK_ALGO_CBCMAC_SM4, "CBCMAC_SM4"},
80 {RK_ALGO_CMAC_AES, "CMAC_AES"},
81 {RK_ALGO_CBCMAC_AES, "CBCMAC_AES"},
82 };
83
84 static const struct test_name_map mode_map_tbl[] = {
85 {RK_CIPHER_MODE_ECB, "ECB"},
86 {RK_CIPHER_MODE_CBC, "CBC"},
87 {RK_CIPHER_MODE_CTS, "CTS"},
88 {RK_CIPHER_MODE_CTR, "CTR"},
89 {RK_CIPHER_MODE_CFB, "CFB"},
90 {RK_CIPHER_MODE_OFB, "OFB"},
91 {RK_CIPHER_MODE_XTS, "XTS"},
92 {RK_CIPHER_MODE_CCM, "CCM"},
93 {RK_CIPHER_MODE_GCM, "GCM"},
94 };
95
96 static const struct test_name_map ops_map_tbl[] = {
97 {RK_OP_CIPHER_ENC, "ENCRYPT"},
98 {RK_OP_CIPHER_DEC, "DECRYPT"},
99 };
100
get_name_from_map(uint32_t id,const struct test_name_map * map,uint32_t map_cnt)101 static const char *get_name_from_map(uint32_t id, const struct test_name_map *map, uint32_t map_cnt)
102 {
103 uint32_t i;
104
105 for (i = 0; i < map_cnt; i++, map++) {
106 if (map->id == id)
107 return map->name;
108 }
109
110 return "Unknown";
111 }
112
test_algo_name(uint32_t algo)113 const char *test_algo_name(uint32_t algo)
114 {
115 return get_name_from_map(algo, &algo_map_tbl[0], ARRAY_SIZE(algo_map_tbl));
116 }
117
test_mode_name(uint32_t mode)118 const char *test_mode_name(uint32_t mode)
119 {
120 return get_name_from_map(mode, &mode_map_tbl[0], ARRAY_SIZE(mode_map_tbl));
121 }
122
test_op_name(uint32_t operation)123 const char *test_op_name(uint32_t operation)
124 {
125 return get_name_from_map(operation, &ops_map_tbl[0], ARRAY_SIZE(ops_map_tbl));
126 }
127
128