From d1cb628eec8b730bbb451eeee275493bcb48972c Mon Sep 17 00:00:00 2001 From: redlineo Date: Mon, 27 Jan 2025 09:59:57 +0000 Subject: [PATCH] done interface part, need write encrypt code --- README.md | 2 +- aes.c | 88 ++++++++++++++++++++++++++-------------------------- red_crypto.c | 79 ++++++++++++++++++++++++++-------------------- red_crypto.h | 17 ++++++++++ 4 files changed, 107 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index 6901aad..02d6f52 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# RED_CRYPTO ver 0.6.0 +# RED_CRYPTO ver 0.6.5 See chapter about [Memory issue](#memory-issue) and [Memory usage](#memory-usage). I recommend using at the beginning internal FLASH or EEPROM, because you have some resources to realize the need for external memory. Or you can do not change your passwords, if there are very strong one's. diff --git a/aes.c b/aes.c index c480f72..4838fbf 100644 --- a/aes.c +++ b/aes.c @@ -1097,7 +1097,7 @@ void print_rnd_key(WORD key[]) /*********************** TEST FUNCTION DEFINITIONS ***********************/ #ifdef TEST_FUNCTIONS_ENABLED -void print_hex(BYTE str[], int len) +void red_print_hex(BYTE str[], int len) { int idx; @@ -1126,21 +1126,21 @@ int aes_ecb_test(void) //printf("* ECB mode:\n"); aes_key_setup(key[0], key_schedule, 256); //printf( "Key : "); - //print_hex(key[0], 32); + //red_print_hex(key[0], 32); for(idx = 0; idx < 2; idx++) { aes_encrypt(plaintext[idx], enc_buf, key_schedule, 256); //printf("\nPlaintext : "); - //print_hex(plaintext[idx], 16); + //red_print_hex(plaintext[idx], 16); //printf("\n-encrypted to: "); - //print_hex(enc_buf, 16); + //red_print_hex(enc_buf, 16); pass = pass && !memcmp(enc_buf, ciphertext[idx], 16); aes_decrypt(ciphertext[idx], enc_buf, key_schedule, 256); //printf("\nCiphertext : "); - //print_hex(ciphertext[idx], 16); + //red_print_hex(ciphertext[idx], 16); //printf("\n-decrypted to: "); - //print_hex(enc_buf, 16); + //red_print_hex(enc_buf, 16); pass = pass && !memcmp(enc_buf, plaintext[idx], 16); //printf("\n\n"); @@ -1171,26 +1171,26 @@ int aes_cbc_test(void) aes_key_setup(key[0], key_schedule, 256); //printf( "Key : "); - //print_hex(key[0], 32); + //red_print_hex(key[0], 32); //printf("\nIV : "); - //print_hex(iv[0], 16); + //red_print_hex(iv[0], 16); aes_encrypt_cbc(plaintext[0], 32, enc_buf, key_schedule, 256, iv[0]); //printf("\nPlaintext : "); - //print_hex(plaintext[0], 32); + //red_print_hex(plaintext[0], 32); //printf("\n-encrypted to: "); - //print_hex(enc_buf, 32); + //red_print_hex(enc_buf, 32); //printf("\nCiphertext : "); - //print_hex(ciphertext[0], 32); + //red_print_hex(ciphertext[0], 32); pass = pass && !memcmp(enc_buf, ciphertext[0], 32); aes_decrypt_cbc(ciphertext[0], 32, enc_buf, key_schedule, 256, iv[0]); //printf("\nCiphertext : "); - //print_hex(ciphertext[0], 32); + //red_print_hex(ciphertext[0], 32); //printf("\n-decrypted to: "); - //print_hex(enc_buf, 32); + //red_print_hex(enc_buf, 32); //printf("\nPlaintext : "); - //print_hex(plaintext[0], 32); + //red_print_hex(plaintext[0], 32); pass = pass && !memcmp(enc_buf, plaintext[0], 32); //printf("\n\n"); @@ -1219,22 +1219,22 @@ int aes_ctr_test(void) aes_key_setup(key[0], key_schedule, 256); //printf( "Key : "); - //print_hex(key[0], 32); + //red_print_hex(key[0], 32); //printf("\nIV : "); - //print_hex(iv[0], 16); + //red_print_hex(iv[0], 16); aes_encrypt_ctr(plaintext[0], 32, enc_buf, key_schedule, 256, iv[0]); //printf("\nPlaintext : "); - //print_hex(plaintext[0], 32); + //red_print_hex(plaintext[0], 32); //printf("\n-encrypted to: "); - //print_hex(enc_buf, 32); + //red_print_hex(enc_buf, 32); pass = pass && !memcmp(enc_buf, ciphertext[0], 32); aes_decrypt_ctr(ciphertext[0], 32, enc_buf, key_schedule, 256, iv[0]); //printf("\nCiphertext : "); - //print_hex(ciphertext[0], 32); + //red_print_hex(ciphertext[0], 32); //printf("\n-decrypted to: "); - //print_hex(enc_buf, 32); + //red_print_hex(enc_buf, 32); pass = pass && !memcmp(enc_buf, plaintext[0], 32); //printf("\n\n"); @@ -1273,70 +1273,70 @@ int aes_ccm_test(void) //printf("* CCM mode:\n"); //printf("Key : "); - //print_hex(key[0], 16); + //red_print_hex(key[0], 16); - //print_hex(plaintext[0], 4); - //print_hex(assoc[0], 8); - //print_hex(ciphertext[0], 8); - //print_hex(iv[0], 7); - //print_hex(key[0], 16); + //red_print_hex(plaintext[0], 4); + //red_print_hex(assoc[0], 8); + //red_print_hex(ciphertext[0], 8); + //red_print_hex(iv[0], 7); + //red_print_hex(key[0], 16); aes_encrypt_ccm(plaintext[0], 4, assoc[0], 8, iv[0], 7, enc_buf, &enc_buf_len, 4, key[0], 128); //printf("\nNONCE : "); - //print_hex(iv[0], 7); + //red_print_hex(iv[0], 7); //printf("\nAssoc. Data : "); - //print_hex(assoc[0], 8); + //red_print_hex(assoc[0], 8); //printf("\nPayload : "); - //print_hex(plaintext[0], 4); + //red_print_hex(plaintext[0], 4); //printf("\n-encrypted to: "); - //print_hex(enc_buf, enc_buf_len); + //red_print_hex(enc_buf, enc_buf_len); pass = pass && !memcmp(enc_buf, ciphertext[0], enc_buf_len); aes_decrypt_ccm(ciphertext[0], 8, assoc[0], 8, iv[0], 7, enc_buf, &enc_buf_len, 4, &mac_auth, key[0], 128); //printf("\n-Ciphertext : "); - //print_hex(ciphertext[0], 8); + //red_print_hex(ciphertext[0], 8); //printf("\n-decrypted to: "); - //print_hex(enc_buf, enc_buf_len); + //red_print_hex(enc_buf, enc_buf_len); //printf("\nAuthenticated: %d ", mac_auth); pass = pass && !memcmp(enc_buf, plaintext[0], enc_buf_len) && mac_auth; aes_encrypt_ccm(plaintext[1], 16, assoc[1], 16, iv[1], 8, enc_buf, &enc_buf_len, 6, key[0], 128); //printf("\n\nNONCE : "); - //print_hex(iv[1], 8); + //red_print_hex(iv[1], 8); //printf("\nAssoc. Data : "); - //print_hex(assoc[1], 16); + //red_print_hex(assoc[1], 16); //printf("\nPayload : "); - //print_hex(plaintext[1], 16); + //red_print_hex(plaintext[1], 16); //printf("\n-encrypted to: "); - //print_hex(enc_buf, enc_buf_len); + //red_print_hex(enc_buf, enc_buf_len); pass = pass && !memcmp(enc_buf, ciphertext[1], enc_buf_len); aes_decrypt_ccm(ciphertext[1], 22, assoc[1], 16, iv[1], 8, enc_buf, &enc_buf_len, 6, &mac_auth, key[0], 128); //printf("\n-Ciphertext : "); - //print_hex(ciphertext[1], 22); + //red_print_hex(ciphertext[1], 22); //printf("\n-decrypted to: "); - //print_hex(enc_buf, enc_buf_len); + //red_print_hex(enc_buf, enc_buf_len); //printf("\nAuthenticated: %d ", mac_auth); pass = pass && !memcmp(enc_buf, plaintext[1], enc_buf_len) && mac_auth; aes_encrypt_ccm(plaintext[2], 24, assoc[2], 20, iv[2], 12, enc_buf, &enc_buf_len, 8, key[0], 128); //printf("\n\nNONCE : "); - //print_hex(iv[2], 12); + //red_print_hex(iv[2], 12); //printf("\nAssoc. Data : "); - //print_hex(assoc[2], 20); + //red_print_hex(assoc[2], 20); //printf("\nPayload : "); - //print_hex(plaintext[2], 24); + //red_print_hex(plaintext[2], 24); //printf("\n-encrypted to: "); - //print_hex(enc_buf, enc_buf_len); + //red_print_hex(enc_buf, enc_buf_len); pass = pass && !memcmp(enc_buf, ciphertext[2], enc_buf_len); aes_decrypt_ccm(ciphertext[2], 32, assoc[2], 20, iv[2], 12, enc_buf, &enc_buf_len, 8, &mac_auth, key[0], 128); //printf("\n-Ciphertext : "); - //print_hex(ciphertext[2], 32); + //red_print_hex(ciphertext[2], 32); //printf("\n-decrypted to: "); - //print_hex(enc_buf, enc_buf_len); + //red_print_hex(enc_buf, enc_buf_len); //printf("\nAuthenticated: %d ", mac_auth); pass = pass && !memcmp(enc_buf, plaintext[2], enc_buf_len) && mac_auth; diff --git a/red_crypto.c b/red_crypto.c index 15d77a9..f614ab1 100644 --- a/red_crypto.c +++ b/red_crypto.c @@ -3,32 +3,43 @@ #include "red_crypto.h" void crypto_eeprom_init() { - uint8_t storage_size = INIT_STORAGE_SIZE; - uint8_t storage_pass_len = INIT_STORAGE_PASS_LEN; - typedef union { - uint8_t raw[3 + 3 + 1 + 1 + 4 + 1 + (uint32_t)storage_size * (uint32_t)storage_pass_len]; - struct { - uint8_t init_var[3]; // if in eeprom is bytes "RED", then it's not first flash or first power-on, or in eeprom is old red_crypto - uint8_t version[3]; // red_crypto library version - uint8_t storage_size; - uint8_t storage_pass_len; - uint32_t memory_usage; - uint8_t password_count; - uint8_t passwords[(uint32_t)storage_size * (uint32_t)storage_pass_len]; - }; - } red_crypto_storage; - - red_crypto_storage enc_pass; - //TODO: don't work with this var,copy it to global var in RAM + uint8_t red_var[3] = {'R', 'E', 'D'}; + // TODO: don't work with this var,copy it to global var in RAM eeconfig_read_user_datablock(enc_pass.raw); + if (memcmp(enc_pass.init_var, red_var, 3)) { // not empty eeprom +#undef INIT_STORAGE_SIZE +#define INIT_STORAGE_SIZE enc_pass.storage_size +#undef INIT_STORAGE_PASS_LEN +#define INIT_STORAGE_PASS_LEN enc_pass.storage_pass_len + } else { // empty eeprom + enc_pass.init_var[0] = 'R'; + enc_pass.init_var[1] = 'E'; + enc_pass.init_var[2] = 'D'; + enc_pass.version[0] = '0'; + enc_pass.version[1] = '6'; + enc_pass.version[2] = '5'; + enc_pass.memory_usage = 0; + enc_pass.password_count = 0; + enc_pass.storage_size = INIT_STORAGE_SIZE; + enc_pass.storage_pass_len = INIT_STORAGE_PASS_LEN; + for (uint8_t password_index = 0; password_index < enc_pass.storage_size; password_index++) { // must zero bytes in memory + for (uint8_t password_byte = 0; password_byte < enc_pass.storage_pass_len; password_byte++) { + enc_pass.passwords[password_index * enc_pass.storage_pass_len + password_byte] = 0x00; + } + } + eeconfig_update_user_datablock(enc_pass.raw); + } } -uint8_t *decrypted_passwords; +uint8_t **decrypted_passwords; // initialize array "decrypted_passwords" with 0x00 void init_dec_pass(void) { - // array = malloc(sizeof(int[ROWS][COLS])); // explicit 2D array notation - decrypted_passwords = malloc(sizeof(uint8_t[enc_pass.storage_size][enc_pass.storage_pass_len])); + decrypted_passwords = (uint8_t **)malloc(sizeof(uint8_t *) * enc_pass.storage_size); + for (uint8_t i = 0; i < enc_pass.storage_size; i++) { + decrypted_passwords[i] = (uint8_t *)malloc(sizeof(uint8_t) * enc_pass.storage_pass_len); + } + for (uint8_t i = 0; i < enc_pass.storage_size; i++) { for (uint8_t j = 0; j < enc_pass.storage_pass_len; j++) { decrypted_passwords[i][j] = 0x00; @@ -52,9 +63,9 @@ uint8_t decrypted_mode = 0; uint8_t red_menu_mode = 0; -void encrypt_pass_kuzn() { +void encrypt_pass_kuzn(void) { kuz_key_t key; - w128_t x; + // w128_t x; init_dec_pass(); @@ -62,10 +73,10 @@ void encrypt_pass_kuzn() { #ifdef USE_SHA256_KEY SHA256_CTX ctx; sha256_init(&ctx); - sha256_update(&ctx, readed_key, min_len(u_strlen(readed_key), MAX_KEY_LEN)); + sha256_update(&ctx, readed_key, red_min_len(u_strlen(readed_key), MAX_KEY_LEN)); BYTE result_key[SHA256_BLOCK_SIZE]; sha256_final(&ctx, result_key); - print_hex(result_key); + red_print_hex(result_key); kuz_set_encrypt_key(&key, result_key); #endif // if you don't have enough FLASH in MCU @@ -75,7 +86,7 @@ void encrypt_pass_kuzn() { } // decrypting passwords -void decrypt_pass_kuzn() { +void decrypt_pass_kuzn(void) { kuz_key_t key; w128_t x; @@ -85,10 +96,10 @@ void decrypt_pass_kuzn() { #ifdef USE_SHA256_KEY SHA256_CTX ctx; sha256_init(&ctx); - sha256_update(&ctx, readed_key, min_len(u_strlen(readed_key), MAX_KEY_LEN)); + sha256_update(&ctx, readed_key, red_min_len(u_strlen(readed_key), MAX_KEY_LEN)); BYTE result_key[SHA256_BLOCK_SIZE]; sha256_final(&ctx, result_key); - print_hex(result_key); + red_print_hex(result_key); kuz_set_decrypt_key(&key, result_key); #endif // if you don't have enough FLASH in MCU @@ -117,7 +128,7 @@ void decrypt_pass_kuzn() { // reading password's block for decrypting for (uint8_t byte_index = 0 + block_index * 16; byte_index < 16 + block_index * 16; byte_index++) { // x.b[byte_index % 16] = encrypted_passwords[password_index][byte_index]; - x.b[byte_index % 16] = enc_pass.passwords[password_index * enc_pass.storage_pass_len + byte_index] + x.b[byte_index % 16] = enc_pass.passwords[password_index * enc_pass.storage_pass_len + byte_index]; } // decrypting via Kuznechik @@ -155,7 +166,7 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record) { case RED_CRY_M: if (record->event.pressed) { #ifdef USE_RED_CRY_DEBUG - print_int(readed_key); + red_print_int(readed_key); #endif crypto_mode = 0; count_char_key = 0; @@ -178,28 +189,28 @@ uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record) { switch (keycode) { case RED_PASS1: if (record->event.pressed) { - send_chars_pass(decrypted_passwords[0]); + red_send_chars_pass(decrypted_passwords[0]); } break; case RED_PASS2: if (record->event.pressed) { - send_chars_pass(decrypted_passwords[1]); + red_send_chars_pass(decrypted_passwords[1]); } break; case RED_PASS3: if (record->event.pressed) { - send_chars_pass(decrypted_passwords[2]); + red_send_chars_pass(decrypted_passwords[2]); } break; case RED_PASS4: if (record->event.pressed) { - send_chars_pass(decrypted_passwords[3]); + red_send_chars_pass(decrypted_passwords[3]); } break; // if you want to add more keys for passwords, copy this `case` // case RED_PASSX: //change X here // if (record->event.pressed) { - // send_chars_pass(decrypted_passwords[X]); //change X here + // red_send_chars_pass(decrypted_passwords[X]); //change X here // } // break; } diff --git a/red_crypto.h b/red_crypto.h index 49b7db0..31863a1 100644 --- a/red_crypto.h +++ b/red_crypto.h @@ -69,7 +69,24 @@ enum red_crypto_keys { RED_PASS10 }; +// uint8_t storage_size = INIT_STORAGE_SIZE; +// uint8_t storage_pass_len = INIT_STORAGE_PASS_LEN; +# define EEPROM_PASS_OFFSET 3 + 3 + 1 + 1 + 4 + 1 +typedef union { + uint8_t raw[EEPROM_PASS_OFFSET + INIT_STORAGE_SIZE * INIT_STORAGE_PASS_LEN]; + struct { + uint8_t init_var[3]; // if in eeprom is bytes "RED", then it's not first flash or first power-on, or in eeprom is old red_crypto + uint8_t version[3]; // red_crypto library version + uint8_t storage_size; + uint8_t storage_pass_len; + uint32_t memory_usage; + uint8_t password_count; + uint8_t passwords[INIT_STORAGE_SIZE * INIT_STORAGE_PASS_LEN]; + }; +} red_crypto_storage_eeprom; + +red_crypto_storage_eeprom enc_pass; uint8_t crypto_process_record_user(uint16_t keycode, keyrecord_t *record); void encrypt_pass_kuzn(void);