• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Beken Corporation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "sys_rtos.h"
17 #include <os/os.h>
18 #include <common/bk_kernel_err.h>
19 
20 #include <common/bk_include.h>
21 #include <os/mem.h>
22 #include "security_func.h"
23 
24 #include <os/str.h>
25 #include "bk_uart.h"
26 
27 #if (CONFIG_SOC_BK7251)
28 #include "bk_security.h"
29 #endif
30 
31 
32 #if (CONFIG_SOC_BK7251)
sec_Command(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)33 void sec_Command(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
34 #if USE_SEC_TEST_CMD
35 {
36 	int sec = 0;
37 
38 	if (argc > 5 || argc < 2) {
39 		os_printf("Usage: sec.\r\n");
40 		return;
41 	}
42 
43 	if (0 == os_strcmp(argv[1], "aes")) {
44 		unsigned char aes256_key[32] = {0x27, 0x73, 0x1a, 0xe6, 0x23, 0x4e, 0xd5, 0xab, 0xfb, 0x07, 0xd5, 0xc8, 0x69, 0x5f, 0x4a, 0x30,
45 										0x1c, 0x38, 0x2a, 0x09, 0x5b, 0x6f, 0x47, 0xd0, 0x2c, 0x9e, 0x1a, 0xf3, 0x0b, 0x54, 0x6d, 0x82
46 									   };
47 		unsigned char aes256_block[16] = {0xa1, 0x2e, 0x84, 0x5c, 0x10, 0x93, 0xa6, 0x9f, 0x4e, 0xb0, 0x6d, 0xa4, 0x85, 0xa3, 0x8f, 0x5d};
48 		unsigned char aes256_ex_enc_expected[16] = {0x1b, 0x1e, 0x55, 0x4e, 0x65, 0xcb, 0x8d, 0x10, 0x9d, 0x95, 0x4e, 0xd4, 0x8d, 0xc2, 0xad, 0x6f};
49 
50 
51 		unsigned char aes256_enc_expected[16];
52 		unsigned char ex_aes256_block[16];
53 		void *ctx;
54 		int i, key_len;
55 
56 		if (argc == 3) {
57 			key_len = os_strtoul(argv[2], NULL, 10);;
58 		} else if (argc == 5) {
59 			key_len = os_strtoul(argv[2], NULL, 10);;
60 
61 			os_memset(aes256_block, 0, 16);
62 			os_memset(aes256_key, 0, 32);
63 			os_memcpy(aes256_block, argv[4], os_strlen(argv[4]));
64 			os_memcpy(aes256_key, argv[3], os_strlen(argv[3]));
65 		} else {
66 			os_printf("Usage: sec aes_en key block\r\n");
67 			return;
68 		}
69 
70 		os_printf("key len%d: ", key_len);
71 		for (i = 0; i < 32; i++)
72 			os_printf("%x ", aes256_key[i]);
73 		os_printf("\r\n");
74 
75 		os_printf("block: ");
76 		for (i = 0; i < 16; i++)
77 			os_printf("%x ", aes256_block[i]);
78 		os_printf("\r\n");
79 
80 		os_printf("hw encrypt\r\n");
81 		ctx = hal_aes_init(aes256_key, key_len);
82 		hal_aes_encrypt(ctx, aes256_block, aes256_enc_expected);
83 		os_printf("en: ");
84 		for (i = 0; i < 16; i++)
85 			os_printf("%x ", aes256_enc_expected[i]);
86 		os_printf("\r\n");
87 
88 		hal_aes_deinit(ctx);
89 
90 
91 		os_printf("sw decrypt\r\n");
92 		ctx = aes_decrypt_init(aes256_key, key_len);
93 		aes_decrypt(ctx, aes256_enc_expected, ex_aes256_block);
94 		os_printf("de: ");
95 		for (i = 0; i < 16; i++)
96 			os_printf("%x ", ex_aes256_block[i]);
97 		os_printf("\r\n");
98 
99 		aes_decrypt_deinit(ctx);
100 
101 		if (0 != os_memcmp(aes256_block, ex_aes256_block, 16))
102 			os_printf("encrypt decrypt err\r\n");
103 
104 
105 		os_memset(aes256_enc_expected, 0, 16);
106 		os_memset(ex_aes256_block, 0, 16);
107 
108 		os_printf("sw encrypt\r\n");
109 		ctx = aes_encrypt_init(aes256_key, key_len);
110 		aes_encrypt(ctx, aes256_block, aes256_enc_expected);
111 		os_printf("en: ");
112 		for (i = 0; i < 16; i++)
113 			os_printf("%x ", aes256_enc_expected[i]);
114 		os_printf("\r\n");
115 
116 		aes_encrypt_deinit(ctx);
117 
118 
119 		os_printf("hw decrypt\r\n");
120 		ctx = hal_aes_init(aes256_key, key_len);
121 		hal_aes_decrypt(ctx, aes256_enc_expected, ex_aes256_block);
122 		os_printf("de: ");
123 		for (i = 0; i < 16; i++)
124 			os_printf("%x ", ex_aes256_block[i]);
125 		os_printf("\r\n");
126 		hal_aes_deinit(ctx);
127 
128 	} else if (0 == os_strcmp(argv[1], "sha1")) {
129 		int i;
130 		unsigned long sha_digst[16];
131 		void *ctx;
132 
133 		os_memset(sha_digst, 0, 16 * 4);
134 
135 		if (argc == 2)
136 			argv[2] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
137 
138 		ctx = hal_sha1_init();
139 
140 		hal_sha1_update(ctx, argv[2], os_strlen(argv[2]));
141 		hal_sha1_finish(ctx, (UINT8 *)sha_digst);
142 
143 		os_printf("RESULT:");
144 		for (i = 0; i < 16 * 4; i++)
145 			os_printf("%x ", ((UINT8 *)sha_digst)[i]);
146 		os_printf("\r\n");
147 
148 	} else if (0 == os_strcmp(argv[1], "sha256")) {
149 		int i;
150 		unsigned long sha_digst[16];
151 		void *ctx;
152 
153 		os_memset(sha_digst, 0, 16 * 4);
154 
155 		if (argc == 2)
156 			argv[2] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
157 
158 		ctx = hal_sha256_init();
159 
160 		hal_sha256_update(ctx, argv[2], os_strlen(argv[2]));
161 		hal_sha256_finish(ctx, (UINT8 *)sha_digst);
162 
163 		os_printf("RESULT:");
164 		for (i = 0; i < 16 * 4; i++)
165 			os_printf("%x ", ((UINT8 *)sha_digst)[i]);
166 		os_printf("\r\n");
167 
168 
169 	} else if (0 == os_strcmp(argv[1], "sha256_st"))
170 		hal_sha256_self_test(1);
171 }
172 #else
173 {
174 }
175 #endif
176 
177 #endif
178 
179