#include #include #include #include #if defined(TEST_TARGET_encrypt) #include #include #include #else #include "init.h" #include "command.h" #include "unitt.h" #include "kern.h" #include "encrypt.h" #endif /************************************************************************************/ /************************************* Unit Test ************************************/ /************************************************************************************/ // #define EXIT_TEST extern uint64_t unitt_clock(void); static int test_0(void) { for (int i = 0; i < 100; i++) { if (0) { #if defined (EXIT_TEST) exit(0); #endif return UNITT_E_FAIL; } } return UNITT_E_OK; } static void unitt_task(void) { static UNITT_TCASE rand_tests[] = { UNITT_TCASE(test_0), // UNITT_TCASE(test_1), // UNITT_TCASE(test_2), }; static UNITT suites[] = { { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock }, }; UNITT_EXE(suites); } /************************************************************************************/ /************************************* Base Test ************************************/ /************************************************************************************/ static void test_des(void) { uint8_t key[8] = "hello"; uint8_t data_block[8] = {1, 2, 3, 4, 5, 6, 7, 9}; uint8_t processed_block[8]; int i; des_set_key(key); printf("des ecb encrypt: "); des_crypt_ecb(data_block, processed_block, DES_ENCRYPT); for (i = 0; i < 8; i++) { printf("%d ", processed_block[i]); } printf("\r\n"); printf("des ecb decrypt: "); des_crypt_ecb(processed_block, data_block, DES_DECRYPT); for (i = 0; i < 8; i++) { printf("%d ", data_block[i]); } printf("\r\n"); /////////////////////////////////////// printf("des cbc encrypt: "); des_crypt_cbc(data_block, processed_block, 8, DES_ENCRYPT); for (i = 0; i < 8; i++) { printf("%d ", processed_block[i]); } printf("\r\n"); printf("des cbc decrypt: "); des_crypt_cbc(processed_block, data_block, 8, DES_DECRYPT); for (i = 0; i < 8; i++) { printf("%d ", data_block[i]); } printf("\r\n"); } static void test_des3(void) { uint8_t key[24] = "hello world"; uint8_t data_block[8] = {1, 2, 3, 4, 5, 6, 7, 9}; uint8_t processed_block[8]; int i; des3_set_key2(key); printf("des3 ecb encrypt: "); des3_crypt_ecb(data_block, processed_block, DES_ENCRYPT); for (i = 0; i < 8; i++) { printf("%d ", processed_block[i]); } printf("\r\n"); printf("des3 ecb decrypt: "); des3_crypt_ecb(processed_block, data_block, DES_DECRYPT); for (i = 0; i < 8; i++) { printf("%d ", data_block[i]); } printf("\r\n"); /////////////////////////////////////// printf("des3 cbc encrypt: "); des3_crypt_cbc(data_block, processed_block, 8, DES_ENCRYPT); for (i = 0; i < 8; i++) { printf("%d ", processed_block[i]); } printf("\r\n"); printf("des3 cbc decrypt: "); des3_crypt_cbc(processed_block, data_block, 8, DES_DECRYPT); for (i = 0; i < 8; i++) { printf("%d ", data_block[i]); } printf("\r\n"); } static void test_base(void) { // test_des(); test_des3(); } /************************************************************************************/ /************************************* Command ************************************/ /************************************************************************************/ static void usage(void) { printf( "Usage: encrypt [opt] [arg] ...\n" "\n" "options:\n" " -e Specifies the function to execute, the default is the test\n" " Test base function\n" " Unit test\n" " Test des function\n" " Test des3 function\n" " -h Print help\n" " -v Print version\n" " -u [] Unit test period, unit ms, the default is 1000ms\n" "\n" ); } static int test(int argc, char *argv[]) { char *execute = NULL; int ut_period = 1000; /* reset getopt */ command_opt_init(); while (1) { int opt = command_getopt(argc, argv, "e:hvu::"); if (opt == -1) break; switch (opt) { case 'u' : if (command_optarg) ut_period = atoi(command_optarg); break; case 'e' : execute = command_optarg; break; case 'v' : printf("encrypt version %d.%d.%d\r\n", ENCRYPT_V_MAJOR, ENCRYPT_V_MINOR, ENCRYPT_V_PATCH); return 0; case '?': printf("Unknown option `%c`\r\n", command_optopt); return -1; case 'h' : default: usage(); return 0; } } if (execute) { if (!strcmp(execute, "base")) { test_base(); } else if (!strcmp(execute, "ut")) { #if defined(TEST_TARGET_encrypt) while (1) { unitt_task(); usleep(1000 * ut_period); } #else printf("create task %d\r\n", task_create(ut_period, unitt_task)); #endif } else if (!strcmp(execute, "des")) { test_des(); } else if (!strcmp(execute, "des3")) { test_des3(); } } else { test_base(); } return 0; } /************************************************************************************/ /************************************ Test entry ************************************/ /************************************************************************************/ #if defined(TEST_TARGET_encrypt) int main(int argc, char *argv[]) { return test(argc, argv); } #else void test_encrypt(void) { command_export("encrypt", test); } init_export_app(test_encrypt); #endif