int main(int argc, const char **argv) { char buff[4096]; gmp_randstate_t state; gmp_randinit_default(state); mpz_t m, c, a; mpz_inits(m, c, a, NULL); rsa_private_t sk; rsa_public_t pk; if (argc == 2) gmp_randseed_ui(state, atoi(argv[1])); else gmp_randseed_ui(state, 0); int fella = 1; if (fella) { printf("Generating RSA parameters ...\n"); rsa_gen(&sk, &pk, state, BITS); rsa_write_private(&sk, "fella"); rsa_write_public(&pk, "fella.pub"); } else { printf("Reading RSA parameters ...\n"); rsa_read_private(&sk, "fella"); rsa_read_public(&pk, "fella.pub"); } //printf("public key:\n"); //printf("N = %s\n", mpz_get_str(buff, 10, pk.N)); //printf("e = %s\n\n", mpz_get_str(buff, 10, pk.e)); //printf("secret key:\n"); //printf("N = %s\n", mpz_get_str(buff, 10, sk.N)); //printf("d = %s\n", mpz_get_str(buff, 10, sk.d)); //printf("p = %s\n", mpz_get_str(buff, 10, sk.p)); //printf("q = %s\n", mpz_get_str(buff, 10, sk.q)); //printf("id_p = %s\n", mpz_get_str(buff, 10, sk.id_p)); //printf("id_q = %s\n", mpz_get_str(buff, 10, sk.id_q)); mpz_urandomb(m, state, BITS); //mpz_set_str(m, "12334324342343234234234234234324324324234324283758579832759871", 10); int i = 0, ct = 0, total = 10 * 16; printf("Testing a bunch (%d) of plaintexts ... \n", total); for (i = 0; i < total; i++) { rsa_enc(c, m, &pk); rsa_dec(a, c, &sk); if (mpz_cmp(a, m) != 0) { printf("%-6d plaintext: %s\n", i,mpz_get_str(buff, 10, m)); printf(" ciphertext: %s\n", mpz_get_str(buff, 10, c)); printf(" verify: %s\n", mpz_get_str(buff, 10, a)); ct ++; } mpz_urandomb(m, state, BITS); } if (ct == 0) printf("No problem!\n"); else printf("There were %d f**k ups.\n", ct); mpz_clears(m, c, a, NULL); gmp_randclear(state); rsa_free_private(&sk); rsa_free_public(&pk); return 0; }
uint8_t rsa_encrypt_oaep(void* dest, uint16_t* out_length, const void* src, uint16_t length_B, rsa_publickey_t* key, const rsa_oaep_parameter_t *p, const rsa_label_t* label, const void* seed){ if(!p){ p = &rsa_oaep_default_parameter; } if(!label){ label = &rsa_oaep_default_label; } uint16_t hv_len = (hfal_hash_getHashsize(p->hf)+7)/8; if(length_B > bigint_length_B(&key->modulus) - 2*hv_len - 2){ /* message too long */ return 1; } uint16_t buffer_len = bigint_length_B(&key->modulus); #if DEBUG cli_putstr("\r\n buffer_len = "); cli_hexdump_rev(&buffer_len, 2); cli_putstr("\r\n modulus_len = "); cli_hexdump_rev(&key->modulus.length_B, 2); #endif uint8_t* buffer = (uint8_t*)dest; uint8_t off; /* the following needs some explanation: * off is the offset which is used for compensating the effect of * changeendian() when it operates on multi-byte words. * */ off = (sizeof(bigint_word_t) - (bigint_get_first_set_bit(&key->modulus)/8+1) % sizeof(bigint_word_t)) % (sizeof(bigint_word_t)); buffer += off; buffer_len -= off; uint8_t* seed_buffer = buffer + 1; uint16_t db_len = buffer_len - hv_len - 1; uint8_t* db = seed_buffer + hv_len; uint16_t maskbuffer_len = db_len>hv_len?db_len:hv_len; uint8_t maskbuffer[maskbuffer_len]; bigint_t x; memset(dest, 0, seed_buffer - buffer + off); memset(db + hv_len, 0, db_len - hv_len - length_B -1); hfal_hash_mem(p->hf, db, label->label, label->length_b); db[db_len - length_B - 1] = 0x01; memcpy(db+db_len - length_B, src, length_B); if(seed){ memcpy(seed_buffer, seed, hv_len); }else{ /* generate random seed */ if(!prng_get_byte){ return 2; /* ERROR: no random generator specified */ } uint16_t i; for(i=0; i<hv_len; ++i){ seed_buffer[i] = prng_get_byte(); } } #if DEBUG cli_putstr("\r\n msg (raw, pre-feistel):\r\n"); cli_hexdump_block(dest, bigint_length_B(&key->modulus), 4, 16); #endif p->mgf(maskbuffer, seed_buffer, hv_len, db_len, p->mgf_parameter); memxor(db, maskbuffer, db_len); p->mgf(maskbuffer, db, db_len, hv_len, p->mgf_parameter); memxor(seed_buffer, maskbuffer, hv_len); #if DEBUG cli_putstr("\r\n msg (raw, post-feistel):\r\n"); cli_hexdump_block(dest, bigint_length_B(&key->modulus), 4, 16); #endif x.info = 0; x.length_B = key->modulus.length_B; x.wordv = dest; bigint_adjust(&x); rsa_os2ip(&x, NULL, bigint_length_B(&key->modulus)); #if DEBUG cli_putstr("\r\ninput-msg (pre enc):\r\n"); cli_hexdump_rev(&src, 2); cli_hexdump_block(src, length_B, 4, 16); #endif rsa_enc(&x, key); #if DEBUG cli_putstr("\r\ninput-msg (post enc):\r\n"); cli_hexdump_rev(&src, 2); cli_hexdump_block(src, length_B, 4, 16); #endif rsa_i2osp(NULL, &x, out_length); return 0; }