void rsa_generate_keys(struct rsa_key **public_key, struct rsa_key **private_key) { unsigned short p, q; unsigned long n, e, d; *public_key = (struct rsa_key*) calloc(1, sizeof(struct rsa_key)); *private_key = (struct rsa_key*) calloc(1, sizeof(struct rsa_key)); srand(time(NULL)); p = rsa_get_prime(); q = rsa_get_prime(); //printf("prime 1: %d\n", p); //printf("prime 2: %d\n", q); n = (unsigned long) p * q; //printf("n: %lu\n", n); rsa_get_keys(&e, &d, n, rsa_phi(p, q)); (*private_key)->key = d; (*private_key)->n = n; (*public_key)->key = e; (*public_key)->n = n; }
// Compute private key d, given n, p, and q. Assume e = 2^16+1 int rsa_compute_d(mpz_t d, const mpz_t n, const mpz_t p, const mpz_t q) { mpz_t e, phi_n, gcd; mpz_inits(e, phi_n, gcd, '\0'); rsa_phi(phi_n, p, q); mpz_set_ui(e, 65537); if (mpz_invert(d, e, phi_n) == 0) exit(EXIT_FAILURE); mpz_clears(phi_n, gcd, '\0'); return 0; }