void readData(ENC_ctx *ENC_ctx_receiver,const message_ctx *ciphermessage, unsigned char *out, unsigned int *output_size) { CCM_decrypt(out, output_size, ENC_ctx_receiver,ciphermessage); if (PRINT_inputs_outputs && *output_size != 0){ printf("\n-->Decrypt: %s\n", out); print_hex_memory(out, *output_size); printf("\n"); } }
void sendData(ENC_ctx *ENC_ctx_sender, const unsigned char *input, const unsigned int input_size, message_ctx *ciphermessage) { if (PRINT_inputs_outputs){ printf("\n-->Encrypt: %s\n", input); print_hex_memory(input, input_size); printf("\n"); } CCM_encrypt(ciphermessage, ENC_ctx_sender, input, input_size); ciphermessage->tag=0xFF; if (PRINT_messages){ print_message(ciphermessage); printf("\n"); } }
//structure of the messages send through the channel //tag can be: //0x00: First message of STS_protocol (master) //0x01: Second message of STS_protocol (slave) //0x02: third message of STS_protocol (master) //0x03: 4th message of STS_protocol (slave) //0x04: 5th message of STS_protocol (master) //0x05: optimal 6th message of STS_protocol (slave) //0xFF: message containing enciphered data void print_message(const message_ctx *message){ if (message->tag == 0x00){ printf("\n message1_master (generator c)"); printf("\n length = %d bytes",message->length); printf("\n data:"); print_hex_memory(message->data,message->length); printf("\n"); } else if (message->tag == 0x01){ printf("\n message1_slave (c^b mod p)"); printf("\n length = %d bytes",message->length); printf("\n data:"); print_hex_memory(message->data,message->length); printf("\n"); } else if (message->tag == 0x02){ printf("\n message2_master (c^a mod p)"); printf("\n length = %d bytes",message->length); printf("\n data:"); print_hex_memory(message->data,message->length); printf("\n"); } else if (message->tag == 0x03){ printf("\n message2_slave (E_k{S_b{hash(c^b, c^a)}})"); printf("\n length = %d bytes",message->length); printf("\n associated data (sequence number) = %x ",message->seq_number); printf("\n data:"); print_hex_memory(message->data,message->length); printf("\n"); } else if (message->tag == 0x04){ printf("\n message3_master (E_k{S_a{hash(c^a, c^b)}})"); printf("\n length = %d bytes",message->length); printf("\n associated data (sequence number) = %d ",message->seq_number); printf("\n data:"); print_hex_memory(message->data,message->length); printf("\n"); } else if (message->tag == 0x05){ printf("\n message3_slave (protocol unsuccessful, key not accepted)"); printf("\n"); } else if (message->tag == 0xFF){ printf("\n ciphered data"); printf("\n length = %d bytes",message->length); printf("\n associated data (sequence number) = %d ",message->seq_number); printf("\n data:"); print_hex_memory(message->data,message->length); printf("\n"); } };
int main ( int argc, char *argv[] ) { int n,j, cport_nr=22, /* /dev/ttyAMA0 */ bdrate=9600; /* 9600 baud */ unsigned char buf[128]; // Parse the command line options for (j = 1; j < argc; j++) { int more = j+1 < argc; // There are more arguments if (!strcmp(argv[j],"--port") && more) { cport_nr=atoi(argv[++j]); } else if (!strcmp(argv[j],"--baud") && more) { bdrate=atoi(argv[++j]); } else if (!strcmp(argv[j],"--debug")) { debug=1; } else if (!strcmp(argv[j],"--verbose")) { verbose=1; } else if (!strcmp(argv[j],"--demo")) { demoMode=1; } else if (!strcmp(argv[j],"--help")) { showHelp(); exit(0); } else { fprintf(stderr, "Unknown or not enough arguments for option '%s'.\n\n", argv[j]); showHelp(); exit(1); } } if (debug==1) { printf("Using port %i\n", cport_nr); printf("Baud Rate %i\n", bdrate); } unsigned char requestString[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xEB, 0x90, 0xEB, 0x90, 0xEB, 0x90, 0x16, 0xA0, 0x00, 0xB1, 0xA7, 0x7F}; int sentChars; if (demoMode == 0) { if(RS232_OpenComport(cport_nr, bdrate)) { if (debug == 1) printf("Can not open comport\n"); return(0); } sentChars = RS232_SendBuf(cport_nr,requestString,18); if(sentChars<0) { if (debug == 1) printf("Can not send request\n"); return(0); } } else { sentChars = 18; } if (debug == 1) { printf("Sent %i bytes: ", sentChars); print_hex_memory((char *)requestString, 18); } if (demoMode == 0) { usleep(100000); n = RS232_PollComport(cport_nr, buf, 127); } else { //Manually fake a response unsigned char buf2[] = {0xEB, 0x90, 0xEB, 0x90, 0xEB, 0x90, 0x00, 0xA0, 0x18, 0xC9, 0x05, 0x36, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x4E, 0x04, 0xAE, 0x05, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x01, 0x01, 0x2E, 0x5D, 0x01, 0x00, 0x21, 0xF2, 0x7F, 0x00}; memcpy(buf, buf2, 36); n=36; } if(n > 0) { buf[n] = 0; /* always put a "null" at the end of a string! */ if (debug == 1) { printf("received %i bytes\n", n); print_hex_memory((char *)buf, n); } if (verbose == 1) process_string_verbose((char *)buf); else process_string((char *)buf); } return(0); }