From f548845a298beffc640fec7ba2b0f595ca3a8269 Mon Sep 17 00:00:00 2001 From: Steven Dan Date: Mon, 30 Mar 2026 17:54:29 +0800 Subject: [PATCH] key_program --- lib_xua/lib_xua/src/hid/hid.xc | 3 +- .../src/extensions/program_key.c | 369 +++++++++--------- 2 files changed, 195 insertions(+), 177 deletions(-) diff --git a/lib_xua/lib_xua/src/hid/hid.xc b/lib_xua/lib_xua/src/hid/hid.xc index 6863685..a4b5bdf 100644 --- a/lib_xua/lib_xua/src/hid/hid.xc +++ b/lib_xua/lib_xua/src/hid/hid.xc @@ -83,7 +83,8 @@ XUD_Result_t HidInterfaceClassRequests( #endif } - if (buffer[1] == 0x77 && buffer[2] >= 0x82) + + if (buffer[1] == 0x77 && ((buffer[2] >= 0x82) || (buffer[2] == 0x5b))) { process_send_params(&buffer[1], datalength - 1); } diff --git a/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/program_key.c b/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/program_key.c index 8eb11a0..ff3f6b8 100644 --- a/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/program_key.c +++ b/sw_usb_audio/app_usb_aud_phaten_golden_6ch/src/extensions/program_key.c @@ -4,6 +4,7 @@ #include #include #include +#include "hmac.h" #include "xua_conf.h" #define WRITE_ENABLE_COMMAND (0x06) @@ -31,7 +32,7 @@ #define PRSCUR_COMMAND (0x42) #define ERSCUR_COMMAND (0x44) #define RDSCUR_COMMAND (0x48) -#define HMAC_LEN 60 +#define KEY_BLOCK_LEN 100 #define HMAC_GEN_LEN 20 @@ -40,19 +41,17 @@ extern unsigned short XUA_Endpoint0_getVendorId(); unsigned char hid_reply[64] = {0}; -#if 0 void printArrayHex1(uint8_t array[], int size, int elementsPerLine) { for (int i = 0; i < size; i++) { - printf("0x%x, ", array[i]); + debug_printf("0x%x ", array[i]); // 在达到每行元素个数时换行 if ((i + 1) % elementsPerLine == 0) { - printf("\n"); + debug_printf("\n"); } } - printf("\n"); + debug_printf("\n"); } -#endif #define settw(a,b) {__asm__ __volatile__("settw res[%0], %1": : "r" (a) , "r" (b));} #define setc(a,b) {__asm__ __volatile__("setc res[%0], %1": : "r" (a) , "r" (b));} @@ -79,7 +78,7 @@ typedef struct { clock qspiClkblk; } fl_QSPIPorts; */ -fl_QSPIPorts p_opt_qflash_tile0 = +fl_QSPIPorts p_opt_tile0 = { XS1_PORT_1B, XS1_PORT_1C, @@ -87,7 +86,7 @@ fl_QSPIPorts p_opt_qflash_tile0 = XS1_CLKBLK_3 }; -fl_QSPIPorts p_opt_qflash_tile1 = +fl_QSPIPorts p_opt_tile1 = { XS1_PORT_1F, XS1_PORT_1G, @@ -125,10 +124,12 @@ int flash_opt_enable_ports(fl_QSPIPorts *p_qflash) if (!result) { /* All okay.. */ + debug_printf("flash_opt_enable_ports success\n"); return 1; } else { + debug_printf("flash_opt_enable_ports failed\n"); return 0; } } @@ -143,15 +144,6 @@ void flash_cmd_opt(unsigned int cmd, unsigned char input[], unsigned int num_in, unsigned char output[], unsigned int num_out) { - debug_printf("flash_cmd_opt: cmd = %02x, num_in = %d, num_out = %d\n", cmd, num_in, num_out); - for (size_t i = 0; i < num_in; i++) - { - debug_printf("input[%d] = %02x\n", i, input[i]); - } - for (size_t i = 0; i < num_out; i++) - { - debug_printf("output[%d] = %02x\n", i, output[i]); - } fl_command(cmd,input,num_in,output, num_out); } @@ -182,7 +174,7 @@ void flash_opt_unlock(void) uint8_t did[3]; /* Reason: 使用 tile0 端口连接 Flash,保持原有逻辑 */ - if (flash_opt_enable_ports(&p_opt_qflash_tile0) == 0) { + if (flash_opt_enable_ports(&p_opt_tile0) == 0) { debug_printf("flash_opt_unlock failed\n"); return; } @@ -293,8 +285,6 @@ unsigned compare_buff_diff(const uint8_t* actual, const uint8_t* expected, unsig } -extern void hmac_sha1(const unsigned char *, unsigned long, const unsigned char * , unsigned long, unsigned char * ); - void cal_hmac(uint8_t uid[], uint8_t did[], uint8_t hmac_bin[]) { { @@ -317,49 +307,64 @@ void cal_hmac(uint8_t uid[], uint8_t did[], uint8_t hmac_bin[]) hmac_sha1(key_bin, HMAC_GEN_LEN, msg_bin, HMAC_GEN_LEN, hmac_bin); #if 0 - printf("hmac_bin: \n"); - printArrayHex1(hmac_bin, HMAC_LEN, 20); #endif } } -uint8_t key_verify_old(unsigned sec_write, uint8_t expected_bin[], unsigned offset) +uint8_t opt_key_read(uint8_t key[], unsigned offset) { - uint8_t read_bin[HMAC_LEN + 1]; - uint8_t comp_bin[HMAC_LEN + 1]; + uint8_t read_bin[KEY_BLOCK_LEN + 1]; + uint8_t ret = 0; + + if (flash_opt_enable_ports(&p_opt_tile0) == 0) + { + return 0; + } + + flash_opt_read(read_bin, KEY_BLOCK_LEN + 1); + for (int i = 0; i < 20; i++) + { + key[i] = read_bin[i + offset * 20 + 1]; + } + flash_opt_disable_ports(); + return 1; +} + +uint8_t key_verify_program(unsigned verify_write, uint8_t expected_bin[], unsigned offset) +{ + uint8_t read_bin[KEY_BLOCK_LEN + 1]; + uint8_t comp_bin[KEY_BLOCK_LEN + 1]; uint8_t uid[20]; uint8_t did[4]; uint8_t ret = 0; uint8_t hmac_bin[20]; - flash_opt_read(read_bin, HMAC_LEN + 1); + flash_opt_read(read_bin, KEY_BLOCK_LEN + 1); flash_opt_read_uid(uid, 20); flash_opt_read_did(did, 3); - cal_hmac(uid, did, hmac_bin); - if (sec_write) + if (verify_write) { - if (offset == 0) + if (offset == 1) { - for (int i = 0; i < 20; i++) + cal_hmac(uid, did, hmac_bin); + if (compare_buff_diff(hmac_bin, expected_bin, 20) == 0) { - read_bin[i + 1] = expected_bin[i]; - } - } - else if (offset == 1) - { - for (int i = 0; i < 20; i++) - { - read_bin[i + 21] = expected_bin[i]; + return 0; } } - flash_opt_write2(&read_bin[1], HMAC_LEN); + for (int i = 0; i < 20; i++) + { + read_bin[i + offset * 20 + 1] = expected_bin[i]; + } + + flash_opt_write2(&read_bin[1], KEY_BLOCK_LEN); delay_milliseconds(10); - flash_opt_read(comp_bin, HMAC_LEN + 1); - if (compare_buff_diff(&comp_bin[1], &read_bin[1], HMAC_LEN) == 1) + flash_opt_read(comp_bin, KEY_BLOCK_LEN + 1); + if (compare_buff_diff(&comp_bin[1], &read_bin[1], KEY_BLOCK_LEN) == 1) { debug_printf("compare OK\n"); ret = 1; @@ -367,50 +372,18 @@ uint8_t key_verify_old(unsigned sec_write, uint8_t expected_bin[], unsigned offs } else { - if (offset == 0) + + flash_opt_read(read_bin, KEY_BLOCK_LEN + 1); + if (compare_buff_diff(expected_bin, &read_bin[offset * 20 + 1], 20) == 1) { - flash_opt_read(read_bin, HMAC_LEN + 1); - if (compare_buff_diff(hmac_bin, &read_bin[1], 20) == 1) - { - debug_printf("key_verified\n"); - ret = 1; - } - } - else if (offset == 1) - { - flash_opt_read(read_bin, HMAC_LEN + 1); - if (compare_buff_diff(hmac_bin, &read_bin[21], 20) == 1) - { - debug_printf("key_verified\n"); - ret = 1; - } + debug_printf("key_verified\n"); + ret = 1; } } return ret; } -void flash_read_uid(uint8_t *uid) -{ - if (flash_opt_enable_ports(&p_opt_qflash_tile0) == 0) - { - return; - } - - flash_opt_read_uid(uid, 20); - flash_opt_disable_ports(); -} - -void flash_read_did(uint8_t *did) -{ - if (flash_opt_enable_ports(&p_opt_qflash_tile0) == 0) - { - return; - } - flash_opt_read_did(did, 3); - flash_opt_disable_ports(); -} - void get_key_ret(uint8_t *buffer) { //printf("get_key_ret2\n"); @@ -423,35 +396,6 @@ void get_key_ret(uint8_t *buffer) //printArrayHex1(buffer, 20, 20); } -unsigned char g_hid_pass_data[64]; -extern void hidSetChangePending(unsigned int); - -void user_read_hidpass(unsigned char * hidPassData) -{ - int i = 0; - for (i = 0; i < 64; i++) - { - hidPassData[i] = g_hid_pass_data[i]; - debug_printf("hidPassData[%d] = %02x\n", i, hidPassData[i]); - g_hid_pass_data[i] = 0; - } -} - -void user_set_hidpass(unsigned char *data) -{ -#if HID_CONTROLS - - for (int i = 0; i < 63; i++) - { - g_hid_pass_data[i] = data[i]; - debug_printf("g_hid_pass_data[%d] = %02x\n", i, g_hid_pass_data[i]); - } - - hidSetChangePending(0x1); -#endif -} - - static chanend_t g_flash_opt_c; void flash_opt_set_chan(chanend_t c) { @@ -466,72 +410,49 @@ void flash_opt_exchange_buffer(uint8_t in[64], uint8_t out[64]) { chan_in_buf_byte(g_flash_opt_c, out, 40); } -void flash_opt_key(chanend_t c) { - uint8_t buffer[64], reply[40] = {0, 0, 0, 0}; - uint32_t ret; - delay_milliseconds(2); - while (1) { - chan_in_buf_byte(c, buffer, 40); - if (flash_opt_enable_ports(&p_opt_qflash_tile1) == 0) { - reply[0] = 0; - } - else - { - switch (buffer[3]) - { - case 9: - flash_opt_read_did(&reply[1], 3); - reply[0] = 0x55; - break; - case 10: - flash_opt_read_uid(&reply[1], 21); - reply[0] = 0x55; - break; - case 8: - ret = key_verify_old(1, &buffer[4], 0); - if (ret == 1) - { - debug_printf("write key ret 0x55\n"); - reply[0] = 0x55; - } - else - { - reply[0] = 0; - } - break; - } - flash_opt_disable_ports(); - chan_out_buf_byte(c, reply, 40) ; - } - } - -} - -uint8_t key_validate(void) +uint8_t key_validate(uint8_t offset) { uint8_t ret = 0; - uint8_t data[1] = {0}; - if (flash_opt_enable_ports(&p_opt_qflash_tile0) == 0) + uint8_t read_bin[KEY_BLOCK_LEN + 1]; + uint8_t uid[20]; + uint8_t did[4]; + uint8_t hmac_bin[20]; + + if (flash_opt_enable_ports(&p_opt_tile0) == 0) { return 0; } - ret = key_verify_old(0, data, 0); - if (ret == 1) + + flash_opt_read(read_bin, KEY_BLOCK_LEN + 1); + flash_opt_read_uid(uid, 20); + flash_opt_read_did(did, 3); + + cal_hmac(uid, did, hmac_bin); + + if (compare_buff_diff(hmac_bin, &read_bin[offset * 20 + 1], 20) == 1) { - SetKeyFlag(0x20241224); + debug_printf("key_verified\n"); + ret = 1; } - ret = key_verify_old(0, data, 1); - if (ret == 1) - { - SetKeyFlag(0x20241224); - } flash_opt_disable_ports(); return ret; } +void read_uid_did(uint8_t uid[]) +{ + if (flash_opt_enable_ports(&p_opt_tile0) == 0) + { + return; + } + flash_opt_read_uid(uid, 20); + flash_opt_read_did(uid, 3); + flash_opt_disable_ports(); +} + + void program_key(uint8_t *buffer, int datalength) { if (buffer[0] == 0x77 && buffer[1] == 0x5B) @@ -541,14 +462,13 @@ void program_key(uint8_t *buffer, int datalength) { case 1: debug_printf("verify key\n"); - if (flash_opt_enable_ports(&p_opt_qflash_tile0) == 0) + if (flash_opt_enable_ports(&p_opt_tile0) == 0) { hid_reply[0] = 0; } else { - uint8_t data[1] = {0}; - ret = key_verify_old(0, data, 0); + ret = key_verify_program(0, &buffer[3], 1); if (ret == 1) { hid_reply[0] = 0x55; @@ -562,13 +482,13 @@ void program_key(uint8_t *buffer, int datalength) break; case 2: debug_printf("write key\n"); - if (flash_opt_enable_ports(&p_opt_qflash_tile0) == 0) + if (flash_opt_enable_ports(&p_opt_tile0) == 0) { hid_reply[0] = 0; } else { - ret = key_verify_old(1, &buffer[3], 0); + ret = key_verify_program(1, &buffer[3], 1); debug_printf("write key ret %d\n", ret); if (ret == 1) @@ -584,7 +504,7 @@ void program_key(uint8_t *buffer, int datalength) break; case 3: - if (flash_opt_enable_ports(&p_opt_qflash_tile0) == 0) + if (flash_opt_enable_ports(&p_opt_tile0) == 0) { hid_reply[0] = 0; } @@ -597,7 +517,7 @@ void program_key(uint8_t *buffer, int datalength) } break; case 4: - if (flash_opt_enable_ports(&p_opt_qflash_tile0) == 0) + if (flash_opt_enable_ports(&p_opt_tile0) == 0) { hid_reply[0] = 0; } @@ -610,7 +530,7 @@ void program_key(uint8_t *buffer, int datalength) } break; case 5: - if (flash_opt_enable_ports(&p_opt_qflash_tile0) == 0) + if (flash_opt_enable_ports(&p_opt_tile0) == 0) { hid_reply[0] = 0; } @@ -623,6 +543,7 @@ void program_key(uint8_t *buffer, int datalength) } break; case 6: +#if XUA_USB_EN == 1 debug_printf("get pid vid\n"); unsigned short pid = XUA_Endpoint0_getProductId(); unsigned short vid = XUA_Endpoint0_getVendorId(); @@ -631,22 +552,118 @@ void program_key(uint8_t *buffer, int datalength) hid_reply[4] = (vid >> 8) & 0xff; hid_reply[3] = vid & 0xff; hid_reply[0] = 0x55; +#endif break; case 7: + debug_printf("write aizip key\n"); + if (flash_opt_enable_ports(&p_opt_tile0) == 0) + { + hid_reply[0] = 0; + } + else + { + uint8_t ret = key_verify_program(1, &buffer[3], 0); + + debug_printf("write key ret %d\n", ret); + if (ret == 1) + { + hid_reply[0] = 0x55; + } + else + { + hid_reply[0] = 0; + } + flash_opt_disable_ports(); + } + break; + case 12: + debug_printf("verify aizip key\n"); + if (flash_opt_enable_ports(&p_opt_tile0) == 0) + { + hid_reply[0] = 0; + } + else + { + ret = key_verify_program(0, &buffer[3], 0); + if (ret == 1) + { + hid_reply[0] = 0x55; + } + else + { + hid_reply[0] = 0; + } + flash_opt_disable_ports(); + } + break; + case 13: + case 15: + case 17: + case 19: + case 21: + case 23: + case 25: + case 27: + case 29: + case 31: + case 33: + debug_printf("write key offset %d\n", ((buffer[2] - 13) / 2) + 2); + if (flash_opt_enable_ports(&p_opt_tile0) == 0) + { + hid_reply[0] = 0; + } + else + { + uint8_t ret = key_verify_program(1, &buffer[3], ((buffer[2] - 13) / 2) + 2); + + debug_printf("write key ret %d\n", ret); + if (ret == 1) + { + hid_reply[0] = 0x55; + } + else + { + hid_reply[0] = 0; + } + flash_opt_disable_ports(); + } + break; + case 14: + case 16: + case 18: + case 20: + case 22: + case 24: + case 26: + case 28: + case 30: + case 32: + case 34: + debug_printf("verify key offset %d\n", ((buffer[2] - 14) / 2) + 2); + if (flash_opt_enable_ports(&p_opt_tile0) == 0) + { + hid_reply[0] = 0; + } + else + { + ret = key_verify_program(0, &buffer[3], ((buffer[2] - 14) / 2) + 2); + if (ret == 1) + { + hid_reply[0] = 0x55; + } + else + { + hid_reply[0] = 0; + } + flash_opt_disable_ports(); + } + break; case 8: case 9: case 10: - //flash_opt_exchange_buffer(buffer, hid_reply); - // debug_printf("write aizip key tile 0 %02x\n", hid_reply[0]); - for (int i = 0; i < datalength; i++) - hid_reply[i] = 0; - + case 11: break; } } - else - { - //xmos_printf(&buffer[1], datalength - 1); - } }