void hmacmd5_interactive(void){ char key[101]; char msg[101]; uint8_t hmac[HMAC_MD5_BYTES]; uint8_t key_len, msg_len; cli_putstr_P(PSTR("\r\nHMAC-MD5:\r\nkey: ")); cli_getsn(key, 100); key_len = strlen(key); cli_putstr_P(PSTR("\r\nmsg: ")); cli_getsn(msg, 100); msg_len = strlen(msg); hmac_md5(hmac, key, key_len*8, msg, msg_len*8); cli_putstr_P(PSTR("\r\nhmac-md5: ")); cli_hexdump(hmac, HMAC_MD5_BYTES); }
int32_t getLength(void){ uint32_t len=0; char lenstr[25]; char* len2; for(;;){ memset(lenstr, 0, 21); cli_getsn(lenstr, 20); len2 = strstrip(lenstr); if(!strncasecmp(len2, "LEN", 3)){ while(*len2 && *len2!='=') len2++; if(*len2=='='){ do{ len2++; }while(*len2 && !isdigit((uint8_t)*len2)); len = my_strtoul(len2); //len=(uint32_t)strtoul(len2, NULL, 10); return len; } } else { if(!strncasecmp(len2, "EXIT", 4)){ return -1; } } } }
uint8_t read_bigint(bigint_t *a, char *prompt){ uint16_t read_length, actual_length; uint8_t off; uint8_t *buffer; char read_int_str[18]; cli_putstr(prompt); cli_putstr_P(PSTR("\r\n length: ")); cli_getsn(read_int_str, 16); read_length = own_atou(read_int_str); off = (sizeof(bigint_word_t) - (read_length % sizeof(bigint_word_t))) % sizeof(bigint_word_t); buffer = malloc(((read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t)) * sizeof(bigint_word_t)); if(!buffer){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return 2; } cli_putstr_P(PSTR("\r\n data: ")); memset(buffer, 0, sizeof(bigint_word_t)); actual_length = read_os(buffer + off, read_length, NULL); if(actual_length != read_length){ cli_putstr_P(PSTR("\r\nERROR: unexpected end of data!")); free(buffer); return 1; } a->wordv = (bigint_word_t*)buffer; a->length_W = (read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); a->info = 0; bigint_changeendianess(a); bigint_adjust(a); return 0; }
void md5_interactive(void){ char msg[101]; uint8_t hash[MD5_HASH_BYTES]; uint8_t msg_len; cli_putstr_P(PSTR("\r\nmsg: ")); cli_getsn(msg, 100); msg_len = strlen(msg); md5((void*)hash, msg, msg_len*8); cli_putstr_P(PSTR("\r\nmd5: ")); cli_hexdump(hash, MD5_HASH_BYTES); }
void run_seed_test(void){ uint8_t *msg, *ciph, *msg_; uint16_t ciph_len, msg_len; uint16_t msg_len_; uint16_t seed_len; uint8_t *seed, *seed_out; char read_int_str[18]; cli_putstr_P(PSTR("\r\n== test with given seed ==")); cli_putstr_P(PSTR("\r\n = message =")); cli_putstr_P(PSTR("\r\n length: ")); cli_getsn(read_int_str, 16); msg_len = own_atou(read_int_str); seed_len = rsa_pkcs1v15_compute_padlength_B(&pub_key.modulus, msg_len); seed = malloc(seed_len); #if DEBUG cli_putstr_P(PSTR("\r\nDBG: @seed: 0x")); cli_hexdump_rev(&seed, 2); #endif if(!seed){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; } seed_out = malloc(seed_len); #if DEBUG cli_putstr_P(PSTR("\r\nDBG: @seed_out: 0x")); cli_hexdump_rev(&seed_out, 2); #endif if(!seed_out){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; } msg = malloc(msg_len); #if DEBUG cli_putstr_P(PSTR("\r\nDBG: @msg: 0x")); cli_hexdump_rev(&msg, 2); #endif if(!msg){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; } ciph = malloc(bigint_length_B(&pub_key.modulus)); #if DEBUG cli_putstr_P(PSTR("\r\nDBG: @ciph: 0x")); cli_hexdump_rev(&ciph, 2); #endif if(!ciph){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; } msg_ = malloc(bigint_length_B(&pub_key.modulus) /* + sizeof(bigint_word_t) */ ); #if DEBUG cli_putstr_P(PSTR("\r\nDBG: @msg_: 0x")); cli_hexdump_rev(&msg_, 2); #endif if(!msg_){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; } cli_putstr_P(PSTR("\r\n data: ")); read_os(msg, msg_len, NULL); cli_putstr_P(PSTR("\r\n seed (0x")); cli_hexdump_rev(&seed_len, 2); cli_putstr_P(PSTR(" bytes): ")); read_os(seed, seed_len, NULL); cli_putstr_P(PSTR("\r\n encrypting ...")); /* cli_putstr_P(PSTR("\r\n plaintext:")); cli_hexdump_block(msg, msg_len, 4, 16); cli_putstr_P(PSTR("\r\n seed:")); cli_hexdump_block(seed, seed_len, 4, 16); */ #if DEBUG cli_putstr_P(PSTR("\r\n first prime:")); bigint_print_hex(&priv_key.components[0]); #endif rsa_encrypt_pkcs1v15(ciph, &ciph_len, msg, msg_len, &pub_key, seed); cli_putstr_P(PSTR("\r\n ciphertext:")); cli_hexdump_block(ciph, ciph_len, 4, 16); #if DEBUG cli_putstr_P(PSTR("\r\n first prime:")); bigint_print_hex(&priv_key.components[0]); #endif cli_putstr_P(PSTR("\r\n decrypting ... ")); rsa_decrypt_pkcs1v15(msg_, &msg_len_, ciph, ciph_len, &priv_key, seed_out); cli_putstr_P(PSTR("[done]")); if(msg_len != msg_len_){ char tstr[16]; cli_putstr_P(PSTR("\r\nERROR: wrong decrypted message length (")); itoa(msg_len_, tstr, 10); cli_putstr(tstr); cli_putstr_P(PSTR(" instead of ")); itoa(msg_len, tstr, 10); cli_putstr(tstr); cli_putc(')'); goto end; } if(memcmp(msg, msg_, msg_len)){ cli_putstr_P(PSTR("\r\nERROR: wrong decrypted message:")); cli_hexdump_block(msg_, msg_len_, 4, 16); cli_putstr_P(PSTR("\r\nreference:")); cli_hexdump_block(msg, msg_len, 4, 16); goto end; } if(memcmp(seed, seed_out, seed_len)){ cli_putstr_P(PSTR("\r\nERROR: wrong decrypted seed:")); cli_hexdump_block(seed_out, seed_len, 4, 16); cli_putstr_P(PSTR("\r\nreference:")); cli_hexdump_block(seed, seed_len, 4, 16); goto end; } cli_putstr_P(PSTR("\r\n >>OK<<")); end: free(msg_); free(ciph); free(msg); free(seed_out); free(seed); }