void process_file(void) { int cmd = fd; int res = open(RESFILE, O_WRONLY); if ((cmd >= 0) && (res >= 0)) { uint8_t cmd_buf; read(cmd, &cmd_buf, 1); switch (cmd_buf) { case 'p': { uint8_t pass_buf[MAX_PASS_LENGTH]; read(cmd, pass_buf, MAX_PASS_LENGTH); uint8_t resp; if (security_validate_pass(pass_buf, MAX_PASS_LENGTH)) resp = 'K'; else resp = '~'; write(res, &resp, 1); break; } case 'g': { encrypt_config_t *config; security_get_config(&config); write(res, &(config->encryption_level), 1); break; } case 'c': { encrypt_config_t config; uint8_t encryption_level; read(cmd, &encryption_level, 1); config.encryption_level = encryption_level; security_flash_write_config(&config); break; } case 'n': { uint8_t pass_buf[MAX_PASS_LENGTH]; read(cmd, pass_buf, MAX_PASS_LENGTH); char resp; if (security_validate_pass(pass_buf, MAX_PASS_LENGTH)) { read(cmd, pass_buf, MAX_PASS_LENGTH); security_write_pass(pass_buf, MAX_PASS_LENGTH); resp = 'K'; } else { resp = '~'; } write(res, &resp, 1); break; } } } close(cmd); unlink(CMDFILE); close(res); }
/* Changes the encryption level of the drive. If encrypt is true: encrypt drive, else decrypt */ uint8_t sd_change_encryption(uint8_t slot, bool encrypt, bool change_key, uint8_t *old_passwd, uint8_t *new_passwd) { sd_mmc_err_t err; uint32_t i, nb_blocks; encrypt_config_t *config_ptr = NULL; security_get_config(&config_ptr); if ((encrypt == config_ptr->encryption_level) && !change_key) return CTRL_GOOD; if (change_key) { sha2(old_passwd, MAX_PASS_LENGTH, old_hash_cipher_key, 0); sha2(new_passwd, MAX_PASS_LENGTH, new_hash_cipher_key, 0); } if (old_hash_cipher_key == new_hash_cipher_key) return CTRL_GOOD; do { err = sd_mmc_check(slot); if ((SD_MMC_ERR_NO_CARD != err) && (SD_MMC_INIT_ONGOING != err) && (SD_MMC_OK != err)) { while (SD_MMC_ERR_NO_CARD != sd_mmc_check(slot)) { } } } while (SD_MMC_OK != err); nb_blocks = sd_mmc_get_capacity(slot) * (1024 / SD_MMC_BLOCK_SIZE); for (i = 0; i < nb_blocks / SD_BLOCKS_PER_ACCESS; ++i) { if (SD_MMC_OK != sd_mmc_init_read_blocks(slot, i, SD_BLOCKS_PER_ACCESS)) return CTRL_FAIL; if (SD_MMC_OK != sd_mmc_start_read_blocks(src_buf, SD_BLOCKS_PER_ACCESS)) return CTRL_FAIL; if (SD_MMC_OK != sd_mmc_wait_end_of_read_blocks()) return CTRL_FAIL; aes_set_key(&AVR32_AES, (unsigned int *)old_hash_cipher_key); ram_aes_ram(change_key ? false : encrypt, SD_MMC_BLOCK_SIZE * SD_BLOCKS_PER_ACCESS / sizeof(unsigned int), (unsigned int *)src_buf, (unsigned int *)dest_buf); if (change_key) { aes_set_key(&AVR32_AES, (unsigned int *)new_hash_cipher_key); ram_aes_ram(true, SD_MMC_BLOCK_SIZE * SD_BLOCKS_PER_ACCESS / sizeof(unsigned int), (unsigned int *)dest_buf, (unsigned int *)src_buf); } if (SD_MMC_OK != sd_mmc_init_write_blocks(slot, i, SD_BLOCKS_PER_ACCESS)) return CTRL_FAIL; if (SD_MMC_OK != sd_mmc_start_write_blocks(src_buf, SD_BLOCKS_PER_ACCESS)) return CTRL_FAIL; if (SD_MMC_OK != sd_mmc_wait_end_of_write_blocks()) return CTRL_FAIL; } return CTRL_GOOD; }