void main(void) { // Arrays for input (plaintext) and key uint8_t input[8]; uint8_t key [16]; // Set up things for the code underneath (serial + trigger) inituart(248); // 4800 baud trigger_low(); // Print one helpful character to make the simulator happy putchar('\n'); // Let the SimpleSerial module fill in the input and key arrays while(1) { // Read data from SimpleSerial if(simpleserial_get(input, key, 8, 16)) { // We're done reading an input: encrypt in place and send back trigger_high(); tea_encrypt(input, key); trigger_low(); simpleserial_put(input, 8); } } }
void glitch_loop() { unsigned int i; unsigned int j; unsigned int cnt; unsigned char inc=0; while (1) { cnt = 0; trigger_high(); for(i = 0; i < 200; i++) { for(j = 0; j < 200; j++) { cnt++; } } trigger_low(); inc++; printf_tiny("%u: %u %u %u\n", inc, i, j, cnt); } }
void main_ss(uint8_t mode) { // 16 byte arrays for input (plaintext) and key uint8_t input[MAX_BLOCK_LEN]; uint8_t key [MAX_KEY_LEN]; uint8_t exp [MAX_BLOCK_LEN]; uint8_t status; uint8_t block_len; uint8_t key_len; void (*fp)(uint8_t*, uint8_t*, uint8_t*); switch(mode) { case SS_AES: block_len = AES_BLOCK_LEN; key_len = AES_KEY_LEN; fp = aes_auth; break; case SS_TEA: block_len = TEA_BLOCK_LEN; key_len = TEA_KEY_LEN; fp = tea_auth; break; case SS_XOR: block_len = XOR_BLOCK_LEN; key_len = XOR_KEY_LEN; fp = xor_auth; break; } // Let the SimpleSerial module fill in the input and key arrays while(1) { // Read data from SimpleSerial status = simpleserial_get(input, key, exp, block_len, key_len); if(status == ss_get_bad) continue; if(status == ss_get_enc) { // We're done reading an input: encrypt in place and send back trigger_high(); fp(input, key, exp); trigger_low(); simpleserial_put(input, block_len); } simpleserial_ack(); } }
int main ( void ) { platform_init(); init_uart(); trigger_setup(); putch('P'); putch('a'); putch('s'); putch('s'); putch('w'); putch('o'); putch('r'); putch('d'); putch(':'); putch(' '); /* Require password to unlock simple serial protocol. */ trigger_high(); if (read_pass() || test_pass()) { _delay_ms(BAD_PASS_DELAY); trigger_low(); soft_reset(); } trigger_low(); putch('\n'); putch('W'); putch('e'); putch('l'); putch('c'); putch('o'); putch('m'); putch('e'); putch('!'); putch('\n'); /* Super-Crappy Protocol works like this: Send kKEY Send pPLAINTEXT *** Encryption Occurs *** receive rRESPONSE e.g.: kE8E9EAEBEDEEEFF0F2F3F4F5F7F8F9FA\n p014BAF2278A69D331D5180103643E99A\n r6743C3D1519AB4F2CD9A78AB09A511BD\n */ char c; int ptr = 0; //Initial key aes_indep_init(); aes_indep_key(tmp); char state = 0; while(1){ c = getch(); if (c == 'x') { ptr = 0; state = IDLE; continue; } if (c == 'k') { ptr = 0; state = KEY; continue; } else if (c == 'p') { ptr = 0; state = PLAIN; continue; } else if (state == KEY) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, tmp); aes_indep_key(tmp); state = IDLE; } else { asciibuf[ptr++] = c; } } else if (state == PLAIN) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, pt); /* Do Encryption */ trigger_high(); aes_indep_enc(pt); /* encrypting the data block */ trigger_low(); /* Print Results */ hex_print(pt, 16, asciibuf); putch('r'); for(int i = 0; i < 32; i++){ putch(asciibuf[i]); } putch('\n'); state = IDLE; } else { if (ptr >= BUFLEN){ state = IDLE; } else { asciibuf[ptr++] = c; } } } } return 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'); */ /* Super-Crappy Protocol works like this: Send kKEY Send pPLAINTEXT *** Encryption Occurs *** receive rRESPONSE e.g.: kE8E9EAEBEDEEEFF0F2F3F4F5F7F8F9FA\n p014BAF2278A69D331D5180103643E99A\n r6743C3D1519AB4F2CD9A78AB09A511BD\n */ char c; int ptr = 0; //Initial key aes_indep_init(); aes_indep_key(tmp); char state = 0; while(1){ c = getch(); if (c == 'x') { ptr = 0; state = IDLE; continue; } if (c == 'k') { ptr = 0; state = KEY; continue; } else if (c == 'p') { ptr = 0; state = PLAIN; continue; } else if (state == KEY) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, tmp); aes_indep_key(tmp); state = IDLE; } else { asciibuf[ptr++] = c; } } else if (state == PLAIN) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, pt); /* Do Encryption */ trigger_high(); aes_indep_enc(pt); /* encrypting the data block */ trigger_low(); /* Print Results */ hex_print(pt, 16, asciibuf); putch('r'); for(int i = 0; i < 32; i++){ putch(asciibuf[i]); } putch('\n'); state = IDLE; } else { if (ptr >= BUFLEN){ state = IDLE; } else { asciibuf[ptr++] = c; } } } } return 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; }
int main ( void ) { init_uart0(); /* For 2 MHz crystal use this hack */ //BAUD0L_REG = 12; trigger_setup(); //OSCCAL only needed for internal oscillator mode, doesn't hurt anyway #ifdef OSCCAL OSCCAL = OSCCAL_UARTGOOD; _delay_ms(500); #else #ifdef VARYING_CLOCK #error "VARYING_CLOCK requested but target does not have OSCCAL register" #endif #endif /* Uncomment this to get a HELLO message for debug */ /* output_ch_0('h'); output_ch_0('e'); output_ch_0('l'); output_ch_0('l'); output_ch_0('o'); output_ch_0('\n'); */ /* Super-Crappy Protocol works like this: Send kKEY Send pPLAINTEXT *** Encryption Occurs *** receive rRESPONSE e.g.: kE8E9EAEBEDEEEFF0F2F3F4F5F7F8F9FA\n p014BAF2278A69D331D5180103643E99A\n r6743C3D1519AB4F2CD9A78AB09A511BD\n */ char c; int ptr = 0; //Initial key aes_indep_init(); aes_indep_key(tmp); char state = 0; #ifdef VARYING_CLOCK uint8_t newosc; #endif while(1){ c = input_ch_0(); if (c == 'x') { ptr = 0; state = IDLE; continue; } if (c == 'k') { ptr = 0; state = KEY; continue; } else if (c == 'p') { ptr = 0; state = PLAIN; continue; } else if (state == KEY) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, tmp); aes_indep_key(tmp); state = IDLE; } else { asciibuf[ptr++] = c; } } else if (state == PLAIN) { if ((c == '\n') || (c == '\r')) { asciibuf[ptr] = 0; hex_decode(asciibuf, ptr, pt); /* Do Encryption */ //The following is used for varying the clock #ifdef VARYING_CLOCK _delay_ms(25); #ifdef OSCCAL_CENTER newosc = (rand() & OSCCAL_VARY); OSCCAL = OSCCAL_CENTER + (int8_t)((int8_t)(OSCCAL_VARY/2) - (int8_t)newosc); #else OSCCAL = rand() & OSCMAX; #endif _delay_ms(1); #endif trigger_high(); aes_indep_enc(pt); /* encrypting the data block */ trigger_low(); #ifdef VARYING_CLOCK OSCCAL = OSCCAL_UARTGOOD; _delay_ms(100); #endif /* Print Results */ hex_print(pt, 16, asciibuf); output_ch_0('r'); for(int i = 0; i < 32; i++){ output_ch_0(asciibuf[i]); } output_ch_0('\n'); state = IDLE; } else { if (ptr >= BUFLEN){ state = IDLE; } else { asciibuf[ptr++] = c; } } } } return 1; }
void encrypt() { trigger_high(); aes_ecb_encrypt(buf, key); trigger_low(); }