int main() { uint8_t key[16] = { 0xF3, 0x54, 0x1F, 0xA3, 0x4B, 0x33, 0x9C, 0x0D, 0x80, 0x23, 0x7A, 0xF9, 0x7C, 0x21, 0xD7, 0x3B }; uint8_t buf[16] = { 0x83, 0x85, 0x1F, 0xAB, 0x60, 0x41, 0xCD, 0xF5, 0x4A, 0x41, 0x6C, 0xDA, 0xF0, 0x12, 0xC2, 0xD4 }; #define n_enc 30 // number of encryptions volatile uint8_t counter = n_enc; // counter (must be volatile) uint8_t *param; param = aes128_init(key); while (counter > 0) { aes128_encrypt(buf, param); // actual AES encryption counter--; } counter--; // Endless loop while (1) { } }
void aes128_ctr_init(uint8_t *key, uint8_t *counter) { if (counter != NULL) { memcpy(g_counter, counter, 16); } if (key != NULL) { aes128_init(key); } }
uint8_t WaspAES::init(char* Password, uint16_t keySize){ uint8_t* key; // Key Initialition switch(keySize){ case 128: key = (uint8_t*) calloc(16,sizeof(uint8_t)); if (strlen(Password) < 17) { for (uint16_t i=0; i < strlen(Password);i++){ key[i] = Password[i]; } if (strlen(Password) < 16){ for(int aux = strlen(Password); aux < 16; aux++){ key[aux] = 0; } } } aes128_init(key, &ctx128); break; case 192: key = (uint8_t*) calloc(24,sizeof(uint8_t)); if (strlen(Password) < 25) { for (uint16_t i = 0; i < strlen(Password);i++){ key[i] = Password[i]; } if (strlen(Password) < 24){ for(int aux = strlen(Password); aux < 24; aux++){ key[aux] = 0; } } } aes192_init(key, &ctx192); break; case 256: key = (uint8_t*) calloc(32,sizeof(uint8_t)); if (strlen(Password) < 33) { for (uint16_t i = 0; i < strlen(Password);i++){ key[i] = Password[i]; } if (strlen(Password) < 32){ for(int aux = strlen(Password); aux < 32; aux++){ key[aux] = 0; } } } aes256_init(key, &ctx256); break; default: return 0; } return 1; }
void testrun_testkey_aes128(void){ uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; aes128_ctx_t ctx; uint8_t i; aes128_init(key, &ctx); cli_putstr("\r\n\r\n keyschedule test (FIPS 197):\r\n key: "); cli_hexdump(key, 16); for(i=0; i<11; ++i){ cli_putstr("\r\n index: "); cli_putc('0'+i/10); cli_putc('0'+i%10); cli_putstr(" roundkey "); cli_hexdump(ctx.key[i].ks, 16); } }
int main() { // Setup led_init(); trigger_init(); uart_init(); comm_init(); aes128_init(key, &ctx); // Globally enable interrupts sei(); // Tell the cardreader we're there protocol_send_ATR(); // Cardreader comm loop for(;;) { protocol_challenge_response(); } }
int main (void) { uint8_t j; aes128_ctx_t ctx; /* the context where the round keys are stored */ aes128_init(key, &ctx); /* generating the round keys from the 128 bit key */ wakeup_init(); sei(); //Activate interrupts rfm70_init(); j = 0; while(1) { set_sleep_mode(SLEEP_MODE_IDLE); sleep_mode(); if (interrupt_flag == 1) { interrupt_flag = 0; rfm70_mode_transmit(); rfm70_power(3); _delay_ms(50); read_vcc(); //setup the ADC data.info.type = 1; data.info.vcc = vcc; data.info.vcc_factor = VCC_SCALE_FACTOR; data.info.tempreture = 0; for (uint8_t i=0;i<11;i++) { data.info.data[i] = 0; } aes128_enc(data.raw, &ctx); /* encrypting the data block */ rfm70_transmit_message( data.raw, 16 ); _delay_ms(100); rfm70_mode_powerdown(); } } return 0; }
void testrun_test_aes(void){ uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; uint8_t data[16] = { 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34 }; aes128_ctx_t ctx; aes128_init(key, &ctx); cli_putstr("\r\n\r\n cipher test (FIPS 197):\r\n key: "); cli_hexdump(key, 16); cli_putstr("\r\n plaintext: "); cli_hexdump(data, 16); aes128_enc(data, &ctx); cli_putstr("\r\n ciphertext: "); cli_hexdump(data, 16); aes128_dec(data, &ctx); cli_putstr("\r\n plaintext: "); cli_hexdump(data, 16); cli_putstr("\r\n testing bcal:"); bcgen_ctx_t bcal_ctx; uint8_t r; r = bcal_cipher_init(&aes128_desc, key, 128, &bcal_ctx); cli_putstr("\r\n init = 0x"); cli_hexdump(&r, 1); bcal_cipher_enc(data, &bcal_ctx); cli_putstr("\r\n ciphertext: "); cli_hexdump(data, 16); bcal_cipher_dec(data, &bcal_ctx); cli_putstr("\r\n plaintext: "); cli_hexdump(data, 16); bcal_cipher_free(&bcal_ctx); }
void aes_128_ctr(unsigned char *key, size_t length, unsigned char nonce_counter[16], const unsigned char *input, unsigned char *output) { int c, i; size_t n = 0; unsigned char stream_block[16]; aes128_ctx_t ctx; aes128_init(key, &ctx); while( length-- ) { if( n == 0 ) { memcpy(stream_block,nonce_counter,16); aes128_enc(stream_block, &ctx); for( i = 16; i > 0; i-- ) if( ++nonce_counter[i - 1] != 0 ) break; } c = *input++; *output++ = (unsigned char)( c ^ stream_block[n] ); n = (n + 1) & 0x0F; } }
void testrun_test_aes(void){ uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; uint8_t data[16] = { 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34 }; aes128_ctx_t ctx; aes128_init(key, &ctx); cli_putstr_P(PSTR("\r\n\r\n cipher test (FIPS 197):\r\n key: ")); cli_hexdump(key, 16); cli_putstr_P(PSTR("\r\n plaintext: ")); cli_hexdump(data, 16); aes128_enc(data, &ctx); cli_putstr_P(PSTR("\r\n ciphertext: ")); cli_hexdump(data, 16); aes128_dec(data, &ctx); cli_putstr_P(PSTR("\r\n plaintext: ")); cli_hexdump(data, 16); }
int main ( void ) { uint8_t *bufcontents; uint8_t i; uint8_t tv[] = "foobar1"; uint16_t ticker = 0; uint8_t rc; aes128_ctx_t ctx; uint8_t key[] = "0123456789ABCDEF"; uint8_t IV[] = "FEDCBA9876543210"; uint8_t text[] = "Hello rfm12 world. I've gonna cipher you "; drive(LED1); drive(LED2); toggle_output(LED1); uart_init(); _delay_ms(250); _delay_ms(250); sei(); toggle_output(LED2); uart_putstr ("AVR Boot Ok\r\n"); _delay_ms(250); toggle_output(LED2); aes128_init(key,&ctx); while (1) { uart_putstr("key = "); uart_putstr(key); uart_putstr("\r\n"); uart_putstr("text = "); uart_putstr(text); uart_putstr("\r\n"); /* Ciphering in CBC mode */ memxor(text,IV,16); aes128_enc(text,&ctx); memxor(text+16,text,16); aes128_enc(text+16,&ctx); memxor(text+32,text+16,16); aes128_enc(text+32,&ctx); uart_putstr("text ciphered = \r\n"); uart_hexdump(text,sizeof(text)); /* Deciphering in CBC mode */ aes128_dec(text+32,&ctx); memxor(text+32,text+16,16); aes128_dec(text+16,&ctx); memxor(text+16,text,16); aes128_dec(text,&ctx); memxor(text,IV,16); uart_putstr("text unciphered = "); uart_putstr(text); uart_putstr("\r\n"); /* Let change the IV */ IV[0]++; _delay_ms(250); _delay_ms(250); _delay_ms(250); _delay_ms(250); } }
void aes_indep_key(uint8_t * key) { aes128_init(key, &ctx); }
void crypto_init(aes_ctx_t *ctx) { uint8_t key[16]; eeprom_read_block(key, EECONFIG_AES_KEY, AES_KEY_LEN); aes128_init(key, ctx); }
// encrypt single 128bit block. data is assumed to be 16 uint8_t's // key and iv are assumed to be both 128bit thus 16 uint8_t's void aes128_enc_single(const uint8_t* key, void* data){ aes128_ctx_t ctx; aes128_init(key, &ctx); aes128_enc(data, &ctx); }
/* * init * * This function sets up the password filling with zeros until it reacehs the * proper length depending on the 'keySize'. The possibilities are 128bits, * 192bits and 256bits. * * After padding with zeros, then an initialization function is called in order * to create all the round keys which are stored in the proper context struct * */ uint8_t WaspAES::init(char* password, uint16_t keySize) { uint8_t key[32]; memset( key, 0x00, sizeof(key) ); // Key Initialition with ZEROS until the proper length switch(keySize) { case 128: // key size to be used: 16 Bytes if (strlen(password) < 17) { for (uint16_t i=0; i < strlen(password);i++) { key[i] = password[i]; } if (strlen(password) < 16) { for(int aux = strlen(password); aux < 16; aux++) { key[aux] = 0; } } } // Once the password is padded with zeros // generate the round keys from the 128 bit key aes128_init(key, &ctx128); break; case 192: // key size to be used: 24 Bytes if (strlen(password) < 25) { for (uint16_t i = 0; i < strlen(password);i++) { key[i] = password[i]; } if (strlen(password) < 24) { for(int aux = strlen(password); aux < 24; aux++) { key[aux] = 0; } } } // Once the password is padded with zeros // generate the round keys from the 192 bit key aes192_init(key, &ctx192); break; case 256: // key size to be used: 32 Bytes if (strlen(password) < 33) { for (uint16_t i = 0; i < strlen(password);i++) { key[i] = password[i]; } if (strlen(password) < 32) { for(int aux = strlen(password); aux < 32; aux++) { key[aux] = 0; } } } // Once the password is padded with zeros // generate the round keys from the 256 bit key aes256_init(key, &ctx256); break; default: return 0; } return 1; }
int main(void) { io_init(); uint8_t input[16]; aes128_ctx_t ks, iks; printf("Build Date: %s\n", build_date); printf("Git: %s\n\n", build_git_sha); printf(" Photon DA AES\n"); printf("-----------------\n"); printf("ROUNDS = %lu\n", (unsigned long) ROUNDS); printf("DELAY = %lu\n", (unsigned long) DELAY); printf("STARTUP = %lu\n", (unsigned long) STARTUP); printf("-----------------\n"); printf("SBOX: 0x%.2X\n", (unsigned int) aes_sbox); printf("Input: 0x%.2X\n", (unsigned int) input); printf("Key: 0x%.2X\n", (unsigned int) key); printf("-----------------\n"); #if ROUNDS == 0 uint16_t writes = eeprom_read_word(&write_cycles); uint8_t byte = eeprom_read_byte(&input_byte); printf("write_cycles = %u\n", writes); printf("input_byte = 0x%.2X\n", byte); #endif #ifdef PHOTON printf("Computing S-Box only!\n"); #endif printf("\n"); #if STARTUP > 0 _delay_ms(STARTUP * 1000); #endif #ifdef VERBOSE printf("Printing AES Key:\n"); print_128(key); printf("\n"); #endif // aes128_init(const void* key, aes128_ctx_t* ctx); aes128_init(key, &ks); #ifdef VERBOSE printf("Printing AES SBOX:\n"); uint16_t s = 0; for(s=0; s < 256; s++) { printf("%.2X", aes_sbox[s]); if((s + 1) % 16 == 0) printf("\n"); } printf("\n"); #endif #if ROUNDS > 0 unsigned long round = 1; uint8_t i=0; unsigned long total_rounds = 256 * ROUNDS; while(round <= total_rounds) { reset_input(input, i); #else reset_input(input, byte); memcpy(&init,&input,sizeof(uint8_t)*16); printf("Input:\n"); print_128(input); printf("\n"); while(1) { memcpy(&input,&init,sizeof(uint8_t)*16); #endif memcpy(&iks,&ks,sizeof(aes128_ctx_t)); #ifdef VERBOSE #if ROUNDS > 0 printf("[%6ld]: Input:\n", round); #else printf("Input:\n"); #endif print_128(input); printf("\n"); #endif // void aes128_enc(void* buffer, aes128_ctx_t* ctx); aes128_enc(input, &ks); #ifdef VERBOSE #if ROUNDS > 0 printf("[%6ld]: Result:\n", round); #else printf("Result:\n"); #endif //Result gets written back to the input print_128(input); printf("\n"); #endif #if ROUNDS > 0 round++; if ( (round - 1) % ROUNDS == 0) i++; #endif #if DELAY > 0 _delay_ms(DELAY); #endif } #if ROUNDS > 0 printf("Exiting... after round %li\n", round - 1); #endif return(0); }