int main (int argc, char *argv[]) { aes256_context ctx; FILE *in_file; long fileSize; FILE *out_file; uint8_t key[32]; uint8_t i; uint8_t fileBuf[16]; uint8_t *tmpBuf; time_t start, end; float gap; in_file = fopen(ENCODED_FILE_NAME, "rb"); out_file = fopen(DECODED_FILE_NAME, "wb"); if(in_file == NULL) exit(1); fseek(in_file, 0, SEEK_END); fileSize = ftell(in_file); rewind(in_file); //get a test key for (i = 0; i < sizeof(key);i++) key[i] = i; start = clock(); while( fread(fileBuf, 1, sizeof(fileBuf), in_file) ) { aes256_init(&ctx, key); aes256_decrypt_ecb(&ctx, fileBuf); fwrite(fileBuf, 1, sizeof(fileBuf), out_file); fileSize -= sizeof(fileBuf); if(0 < fileSize && fileSize < sizeof(fileBuf)) { tmpBuf = (uint8_t *)malloc(sizeof(uint8_t)*fileSize); fread(tmpBuf, 1, fileSize, in_file); aes256_init(&ctx, key); aes256_decrypt_ecb(&ctx, tmpBuf); fwrite(tmpBuf, 1, fileSize, out_file); break; } } aes256_done(&ctx); end = clock(); gap = (float) (end - start)/CLOCKS_PER_SEC; printf("걸린시간 : %f초", gap); fclose(in_file); fclose(out_file); return 0; }
void aes256_decrypt(const aes_secret& key, aes_block& block) { aes256_context context; aes256_init(&context, key.data()); aes256_decrypt_ecb(&context, block.data()); aes256_done(&context); }
int main(void) { aes256_context ctx; uint8_t key[32]; uint8_t buf[16], i; /* Put a test vector. */ for (i = 0; i < sizeof(buf); i++) buf[i] = i * 16 + i; for (i = 0; i < sizeof(key); i++) key[i] = i; DUMP("txt: ", i, buf, sizeof(buf)); DUMP("key: ", i, key, sizeof(key)); printf("---\n"); aes256_init(&ctx, key); aes256_encrypt_ecb(&ctx, buf); DUMP("enc: ", i, buf, sizeof(buf)); printf("tst: 8e a2 b7 ca 51 67 45 bf ea fc 49 90 4b 49 60 89\n"); aes256_init(&ctx, key); aes256_decrypt_ecb(&ctx, buf); DUMP("dec: ", i, buf, sizeof(buf)); aes256_done(&ctx); return (0); }
int main (int argc, char *argv[]) { aes256_context ctx; uint8_t key[32]; uint8_t buf[16], i; /* put a test vector */ //for (i = 0; i < sizeof(buf);i++) buf[i] = i * 16 + i; /*for (i = 0; i < sizeof(buf);i++)*/ strcpy(buf, "plain text to cipher"); for (i = 0; i < sizeof(key);i++) key[i] = i; DUMP("txt: ", i, buf, sizeof(buf)); DUMP("key: ", i, key, sizeof(key)); printf("---\n"); aes256_init(&ctx, key); aes256_encrypt_ecb(&ctx, buf); DUMP("enc: ", i, buf, sizeof(buf)); //printf("tst: 8e a2 b7 ca 51 67 45 bf ea fc 49 90 4b 49 60 89\n"); //aes256_init(&ctx, key); //aes256_init(&ctx, key); aes256_decrypt_ecb(&ctx, buf); DUMP("dec: ", i, buf, sizeof(buf)); aes256_done(&ctx); return 0; } /* main */
// ------------------------------------------------------------------------------------------------ String AES256::Decrypt(CSStr data) { // Is there any data to decrypt? if (!data || *data == 0) { return String(); } // Copy the data into an editable string String str(data); // Make sure that we have a size with a multiple of 16 if ((str.size() % 16) != 0) { str.resize(str.size() - (str.size() % 16) + 16); } // Decrypt inc chunks of 16 characters for (Uint32 n = 0; n < str.size(); n += 16) { aes256_decrypt_ecb(&m_Context, reinterpret_cast< Uint8 * >(&str[n])); } // Remove null characters in case the string was not a multiple of 16 when encrypted while (!str.empty() && str.back() == 0) { str.pop_back(); } // Return ownership of the encrypted string return std::move(str); }
void decrypt(uint8_t key[32], uint8_t *buf, unsigned long numbytes) { printf("\nBeginning decryption\n"); unsigned long offset; //printf("Creating %d threads over %d blocks\n", dimBlock.x*dimGrid.x, dimBlock.x); for (offset = 0; offset < numbytes; offset += AES_BLOCK_SIZE) aes256_decrypt_ecb(buf, offset); }
/* * aes解密脚本 */ PUBLIC unsigned char *script_data_decript(ScriptData *script, const unsigned char *data) { int i, index = 0; unsigned short chunkCount; memcpy(&chunkCount, &data[index], 2); // index += sizeof(unsigned short); unsigned short size = chunkCount << 4; unsigned char *result = MALLOC(size); memcpy(result, &data[2], size); for (i = 0; i < chunkCount; i++) { aes256_decrypt_ecb(&script->ctx, &result[index]); index += (CHUNK_SIZE); } return result; }
int main(int argc, char *argv[]) { aes256_context ctx; uint8_t k[32], v[16], x[24]; char key[45], msg[33]; int key_len, msg_len; unsigned int i; if (argc != 3) { printf("Uso: descifraping clave mensaje\n"); return 1; } memset(k, 0, sizeof(k)); memset(v, 0, sizeof(v)); memset(x, 0, sizeof(x)); memset(key, 'A', sizeof(key)); memset(msg, 'A', sizeof(msg)); key_len = strlen(argv[1]); msg_len = strlen(argv[2]); key_len = (key_len>44) ? 44 : key_len; msg_len = (msg_len>32) ? 32 : msg_len; strncpy(key+(44-key_len), argv[1], (key_len)); strncpy(msg+(32-msg_len), argv[2], (msg_len)); key[44]='\0'; msg[32]='\0'; printf("String clave (base64): %s\n", key); printf("String mensaje.......: %s\n", msg); base64_to_buf_r(k, (unsigned char *) key); base64_to_buf_r(x, (unsigned char *) msg); memcpy(k+24,x,8); memcpy(v,x+8,16); DUMP("CLAVE....: ", i, k, sizeof(k)); DUMP("ENTRADA..: ", i, x, sizeof(x)); DUMP("ENCRIPT..: ", i, v, sizeof(v)); aes256_init(&ctx, k); aes256_decrypt_ecb(&ctx, v); aes256_done(&ctx); DUMP("DESCRIPT.: ", i, v, sizeof(v)); printf("Resultado: %s\n\n", v); exit(EXIT_SUCCESS); }
int id_read(int handle, const id_desc_t *desc, mifare_tag_t *tag, void *id, int32_t *counter) { int i, block = desc->sector * 4; /* Generate per card key */ uint8_t aes_key[32]; memcpy(aes_key, desc->aes_key, 32); for (i = 0; i < 32; i++) aes_key[i] ^= tag->serial[i & 3]; uint8_t buf[16]; if (mifare_select(handle, tag)) return ID_ERROR_SELECT; if (mifare_login(handle, desc->sector, desc->key_type, desc->key)) return ID_ERROR_LOGIN; if (mifare_read_block(handle, block + 1, buf)) return ID_ERROR_READ_BLOCK; /* Decrypt block */ aes256_context ctx; aes256_init(&ctx, aes_key); aes256_decrypt_ecb(&ctx, buf); aes256_done(&ctx); if (memcmp(buf, desc->magic, 4) != 0) return ID_ERROR_INVALID_MAGIC; if (mifare_dec_value(handle, block + 2, 1)) return ID_ERROR_DEC_VALUE; if (mifare_read_value(handle, block + 2, counter)) return ID_ERROR_READ_VALUE; memcpy(id, &buf[4], 12); return ID_OK; }
/*! \fn main(void) * \brief Main function * \note For our security chain to be valid, EEP_BOOT_PWD_SET in eeprom needs to be set to BOOTLOADER_PWDOK_KEY */ int main(void) { /* Fetch bootkey in eeprom */ uint16_t current_bootkey_val = eeprom_read_word((uint16_t*)EEP_BOOTKEY_ADDR); // Bootkey in EEPROM uint8_t new_aes_key[AES_KEY_LENGTH/8]; // New AES encryption key uint8_t cur_aes_key[AES_KEY_LENGTH/8]; // AES encryption key uint8_t firmware_data[SPM_PAGESIZE]; // One page of firmware data aes256_context temp_aes_context; // AES context uint8_t cur_cbc_mac[16]; // Current CBCMAC val uint8_t temp_data[16]; // Temporary 16 bytes array RET_TYPE flash_init_result; // Flash initialization result uint16_t firmware_start_address = UINT16_MAX - MAX_FIRMWARE_SIZE - sizeof(cur_cbc_mac) - sizeof(cur_aes_key) + 1; // Start address of firmware in external memory uint16_t firmware_end_address = UINT16_MAX - sizeof(cur_cbc_mac) - sizeof(cur_aes_key) + 1; // End address of firmware in external memory /* Just in case we are going to disable the watch dog timer and disable interrupts */ cli(); wdt_reset(); wdt_clear_flag(); wdt_change_enable(); wdt_stop(); /* Check fuses: 2k words, SPIEN, BOD 4.3V, BOOTRST programming & ver disabled >> http://www.engbedded.com/fusecalc/ */ if ((boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS) != 0xFF) || (boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS) != 0xD8) || (boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS) != 0xF8) || (boot_lock_fuse_bits_get(GET_LOCK_BITS) != 0xFC)) { while(1); } /* If security isn't set in place yet, no point in launching the bootloader */ if (eeprom_read_byte((uint8_t*)EEP_BOOT_PWD_SET) != BOOTLOADER_PWDOK_KEY) { start_firmware(); } /* Check if the device is booting normally, if the bootloader was called, or unknown state */ if (current_bootkey_val == CORRECT_BOOTKEY) { /* Security system set, correct bootkey for firmware */ start_firmware(); } else if (current_bootkey_val != BOOTLOADER_BOOTKEY) { /* Security system set, bootkey isn't the bootloader one nor the main fw one... */ while(1); } /* By default, brick the device so it's an all or nothing update procedure */ eeprom_write_word((uint16_t*)EEP_BOOTKEY_ADDR, BRICKED_BOOTKEY); /* Init IOs */ UHWCON = 0x01; // Enable USB 3.3V LDO initFlashIOs(); // Init EXT Flash IOs spiUsartBegin(); // Init SPI Controller DDR_ACC_SS |= (1 << PORTID_ACC_SS); // Setup PORT for the Accelerometer SS PORT_ACC_SS |= (1 << PORTID_ACC_SS); // Setup PORT for the Accelerometer SS DDR_OLED_SS |= (1 << PORTID_OLED_SS); // Setup PORT for the OLED SS PORT_OLED_SS |= (1 << PORTID_OLED_SS); // Setup PORT for the OLED SS for (uint16_t i = 0; i < 20000; i++) asm volatile ("NOP"); /* Disable I2C block of the Accelerometer */ PORT_ACC_SS &= ~(1 << PORTID_ACC_SS); spiUsartTransfer(0x23); spiUsartTransfer(0x02); PORT_ACC_SS |= (1 << PORTID_ACC_SS); /* Check Flash */ flash_init_result = checkFlashID(); if (flash_init_result != RETURN_OK) { while(1); } for (uint8_t pass_number = 0; pass_number < 2; pass_number++) { /* Init CBCMAC encryption context*/ eeprom_read_block((void*)cur_aes_key, (void*)EEP_BOOT_PWD, sizeof(cur_aes_key)); memset((void*)cur_cbc_mac, 0x00, sizeof(cur_cbc_mac)); memset((void*)temp_data, 0x00, sizeof(temp_data)); aes256_init_ecb(&temp_aes_context, cur_aes_key); // Compute CBCMAC for between the start of the graphics zone until the max addressing space (65536) - the size of the CBCMAC for (uint16_t i = GRAPHIC_ZONE_START; i < (UINT16_MAX - sizeof(cur_cbc_mac) + 1); i += sizeof(cur_cbc_mac)) { // Read data from external flash flashRawRead(temp_data, i, sizeof(temp_data)); // If we got to the part containing to firmware if ((i >= firmware_start_address) && (i < firmware_end_address)) { // Append firmware data to current buffer uint16_t firmware_data_address = i - firmware_start_address; memcpy(firmware_data + (firmware_data_address & SPM_PAGE_SIZE_BYTES_BM), temp_data, sizeof(temp_data)); // If we have a full page in buffer, flash it firmware_data_address += sizeof(cur_cbc_mac); if (((firmware_data_address & SPM_PAGE_SIZE_BYTES_BM) == 0x0000) && (pass_number == 1)) { boot_program_page(firmware_data_address - SPM_PAGESIZE, firmware_data); } } // If we got to the part containing the encrypted new aes key (end of the for()) if (i >= firmware_end_address) { memcpy(new_aes_key + i - firmware_end_address, temp_data, sizeof(temp_data)); } // Continue computation of CBCMAC aesXorVectors(cur_cbc_mac, temp_data, sizeof(temp_data)); aes256_encrypt_ecb(&temp_aes_context, cur_cbc_mac); } // Read CBCMAC in memory and compare it with the computed value flashRawRead(temp_data, (UINT16_MAX - sizeof(cur_cbc_mac) + 1), sizeof(cur_cbc_mac)); if (pass_number == 0) { // First pass, compare CBCMAC and see if we do the next pass or start the firmware if (sideChannelSafeMemCmp(temp_data, cur_cbc_mac, sizeof(cur_cbc_mac)) != 0) { // No match, start the main firmware eeprom_write_word((uint16_t*)EEP_BOOTKEY_ADDR, CORRECT_BOOTKEY); start_firmware(); } else { // Otherwise, next pass! } } else { // Second pass, compare CBCMAC and then update AES keys if (sideChannelSafeMemCmp(temp_data, cur_cbc_mac, sizeof(cur_cbc_mac)) == 0) { // Fetch the encrypted new aes key from flash, decrypt it, store it aes256_decrypt_ecb(&temp_aes_context, new_aes_key); aes256_decrypt_ecb(&temp_aes_context, new_aes_key+16); eeprom_write_block((void*)new_aes_key, (void*)EEP_BOOT_PWD, sizeof(new_aes_key)); eeprom_write_word((uint16_t*)EEP_BOOTKEY_ADDR, CORRECT_BOOTKEY); start_firmware(); } else { // Fail, erase everything! >> maybe just write a while(1) in the future? for (uint16_t i = 0; i < MAX_FIRMWARE_SIZE; i += SPM_PAGESIZE) { boot_page_erase(i); boot_spm_busy_wait(); } while(1); } } } }
int main ( void ) { platform_init(); init_uart(); trigger_setup(); /* Uncomment this to get a HELLO message for debug */ /* putch('h'); putch('e'); putch('l'); putch('l'); putch('o'); putch('\n'); */ //Load Super-Secret key aes256_context ctx; uint8_t tmp32[32] = SECRET_KEY; aes256_init(&ctx, tmp32); //Load super-secret IV uint8_t iv[16] = IV; //Do this forever (we don't actually have a jumper to bootloader) uint8_t i; uint16_t crc; uint8_t c; while(1){ c = (uint8_t)getch(); if (c == 0){ //Initial Value crc = 0x0000; //Read 16 Bytes now for(i = 0; i < 16; i++){ c = (uint8_t)getch(); crc = _crc_xmodem_update(crc, c); //Save two copies, as one used for IV tmp32[i] = c; tmp32[i+16] = c; } //Validate CRC-16 uint16_t inpcrc = (uint16_t)getch() << 8; inpcrc |= (uint8_t)getch(); if (inpcrc == crc){ //CRC is OK - continue with decryption trigger_high(); aes256_decrypt_ecb(&ctx, tmp32); /* encrypting the data block */ trigger_low(); //Apply IV (first 16 bytes) for (i = 0; i < 16; i++){ tmp32[i] ^= iv[i]; } /* Comment this block out to always use initial IV, instead of performing actual CBC mode operation */ //Save IV for next time from original ciphertext for (i = 0; i < 16; i++){ iv[i] = tmp32[i+16]; } //Always send OK, done before checking //signature to ensure there is no timing //attack. This does mean user needs to //add some delay before sending next packet putch(COMM_OK); putch(COMM_OK); //Check the signature if ((tmp32[0] == SIGNATURE1) && (tmp32[1] == SIGNATURE2) && (tmp32[2] == SIGNATURE3) && (tmp32[3] == SIGNATURE4)){ //We now have 12 bytes of useful data to write to flash memory. //We don't actually write anything here though in real life would //probably do more than just delay a moment _delay_ms(1); } } else { putch(COMM_BADCHECKSUM); putch(COMM_BADCHECKSUM); } } } return 1; }
void *decryptor() { unsigned char buff_aes1[16]; unsigned char buff_aes2[16]; unsigned char buff_aes3[16]; unsigned char buff_aes4[16]; unsigned char buff_aes5[16]; unsigned char buff_aes6[16]; unsigned char buff_aes7[16]; unsigned char buff_aes8[16]; unsigned char buff_aes9[16]; unsigned char buff_aes10[16]; unsigned char buff_aes11[16]; unsigned char aes_dec[176], w_char1, w_char2, w_char3, w_char4; int temp, temp2; int index = 0, b; unsigned int filter = 32768,temp_r[9], temp_36extra[9]= {0}; int s=0; int j =0; unsigned char key[32]; int count_filewrite = 0; char out_name1[80] = "out1.bit"; char out_name2[80] = "out2.bit"; FILE *fp; unsigned char local_h2_buffer[176]; int count = 0; DWORD byteswritten = 0, bytesread = 0; aes256_context ctx; unsigned char i; fp = fopen(out_name1, "wb"); for (i = 0; i < sizeof(key); i++) key[i] = i; while(1) { sem_wait(&decryptor_start); memcpy(local_h2_buffer,h2_buffer,176); sem_post(&reciever_start); for(j=0; j<sizeof(buff_aes1); j++) { buff_aes1[j] = local_h2_buffer[j]; buff_aes2[j] = local_h2_buffer[j+16]; buff_aes3[j] = local_h2_buffer[j+32]; buff_aes4[j] = local_h2_buffer[j+48]; buff_aes5[j] = local_h2_buffer[j+64]; buff_aes6[j] = local_h2_buffer[j+80]; buff_aes7[j] = local_h2_buffer[j+96]; buff_aes8[j] = local_h2_buffer[j+112]; buff_aes9[j] = local_h2_buffer[j+128]; buff_aes10[j] = local_h2_buffer[j+144]; buff_aes11[j] = local_h2_buffer[j+160]; } aes256_init(&ctx, key); aes256_decrypt_ecb(&ctx, buff_aes1); aes256_decrypt_ecb(&ctx, buff_aes2); aes256_decrypt_ecb(&ctx, buff_aes3); aes256_decrypt_ecb(&ctx, buff_aes4); aes256_decrypt_ecb(&ctx, buff_aes5); aes256_decrypt_ecb(&ctx, buff_aes6); aes256_decrypt_ecb(&ctx, buff_aes7); aes256_decrypt_ecb(&ctx, buff_aes8); aes256_decrypt_ecb(&ctx, buff_aes9); aes256_decrypt_ecb(&ctx, buff_aes10); aes256_decrypt_ecb(&ctx, buff_aes11); aes256_done(&ctx); for(j=0; j<sizeof(buff_aes1); j++) { local_h2_buffer[j] = buff_aes1[j]; local_h2_buffer[j+16] = buff_aes2[j]; local_h2_buffer[j+32] = buff_aes3[j]; local_h2_buffer[j+48] = buff_aes4[j]; local_h2_buffer[j+64] = buff_aes5[j]; local_h2_buffer[j+80] = buff_aes6[j]; local_h2_buffer[j+96] = buff_aes7[j]; local_h2_buffer[j+112] = buff_aes8[j]; local_h2_buffer[j+128] = buff_aes9[j]; local_h2_buffer[j+144] = buff_aes10[j]; local_h2_buffer[j+160] = buff_aes11[j]; } fwrite((void *)local_h2_buffer,sizeof(char),176,fp); fwrite((void *)temp_36extra,sizeof(int),9,fp); count_filewrite = count_filewrite + 1; fclose(fp); sem_post(&melp_start); if (count_filewrite%2 == 1) fp = fopen(out_name2, "wb"); else fp = fopen(out_name1, "wb"); } pthread_exit(NULL); }
Error FileAccessEncrypted::open_and_parse(FileAccess *p_base,const Vector<uint8_t>& p_key,Mode p_mode) { print_line("open and parse!"); ERR_FAIL_COND_V(file!=NULL,ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(p_key.size()!=32,ERR_INVALID_PARAMETER); pos=0; eofed=false; if (p_mode==MODE_WRITE_AES256) { data.clear(); writing=true; file=p_base; mode=p_mode; key=p_key; } else if (p_mode==MODE_READ) { writing=false; key=p_key; uint32_t magic = p_base->get_32(); print_line("MAGIC: "+itos(magic)); ERR_FAIL_COND_V(magic!=COMP_MAGIC,ERR_FILE_UNRECOGNIZED); mode=Mode(p_base->get_32()); ERR_FAIL_INDEX_V(mode,MODE_MAX,ERR_FILE_CORRUPT); ERR_FAIL_COND_V(mode==0,ERR_FILE_CORRUPT); print_line("MODE: "+itos(mode)); unsigned char md5d[16]; p_base->get_buffer(md5d,16); length=p_base->get_64(); base=p_base->get_pos(); ERR_FAIL_COND_V(p_base->get_len() < base+length, ERR_FILE_CORRUPT ); int ds = length; if (ds % 16) { ds+=16-(ds % 16); } data.resize(ds); int blen = p_base->get_buffer(data.ptr(),ds); ERR_FAIL_COND_V(blen!=ds,ERR_FILE_CORRUPT); aes256_context ctx; aes256_init(&ctx,key.ptr()); for(size_t i=0;i<ds;i+=16) { aes256_decrypt_ecb(&ctx,&data[i]); } aes256_done(&ctx); data.resize(length); MD5_CTX md5; MD5Init(&md5); MD5Update(&md5,data.ptr(),data.size()); MD5Final(&md5); ERR_FAIL_COND_V(String::md5(md5.digest)!=String::md5(md5d),ERR_FILE_CORRUPT) ; file=p_base; } return OK; }