mp_result find_strong_prime(mp_int seed, FILE *fb) { mp_result res; mpz_t t; mp_int_init(&t); for(;;) { if ((res = find_prime(seed, fb)) != MP_TRUE) break; if ((res = mp_int_copy(seed, &t)) != MP_OK) break; if ((res = mp_int_mul_pow2(&t, 1, &t)) != MP_OK || (res = mp_int_add_value(&t, 1, &t)) != MP_OK) break; if ((res = mp_int_is_prime(&t)) == MP_TRUE) { if (fb != NULL) fputc('!', fb); res = mp_int_copy(&t, seed); break; } else if (res != MP_FALSE) break; if (fb != NULL) fputc('x', fb); if ((res = mp_int_add_value(seed, 2, seed)) != MP_OK) break; } mp_int_clear(&t); return res; }
/* Find the first apparent prime in ascending order from z */ mp_result mp_int_find_prime(mp_int z) { mp_result res; if(mp_int_is_even(z) && ((res = mp_int_add_value(z, 1, z)) != MP_OK)) return res; while((res = mp_int_is_prime(z)) == MP_FALSE) { if((res = mp_int_add_value(z, 2, z)) != MP_OK) break; } return res; }
int test_add(testspec_t *t, FILE *ofp) { mp_int in[3], out[1]; int v; mp_result res, expect; if(!parse_int_values(t, in, out, &expect)) return imath_errno = MP_BADARG, 0; if(strcmp(t->code, "addv") == 0) { if((res = mp_int_to_int(in[1], &v)) != MP_OK) return imath_errno = res, 0; if((res = mp_int_add_value(in[0], v, in[2])) != expect) return imath_errno = res, 0; } else { if((res = mp_int_add(in[0], in[1], in[2])) != expect) return imath_errno = res, 0; } if(expect == MP_OK && mp_int_compare(in[2], out[0]) != 0) { mp_int_to_string(in[2], 10, g_output, OUTPUT_LIMIT); return imath_errno = OTHER_ERROR, 0; } return 1; }
mp_result find_prime(mp_int seed, FILE *fb) { mp_result res; int count = 0; if(mp_int_is_even(seed)) if((res = mp_int_add_value(seed, 1, seed)) != MP_OK) return res; while((res = mp_int_is_prime(seed)) == MP_FALSE) { ++count; if(fb != NULL && (count % 50) == 0) fputc('.', fb); if((res = mp_int_add_value(seed, 2, seed)) != MP_OK) return res; } if(res == MP_TRUE && fb != NULL) fputc('+', fb); return res; }
int main(int argc, char *argv[]) { int opt, modbits; FILE *ofp = stdout; char *expt = NULL; rsa_key the_key; mp_result res; /* Process command-line arguments */ while((opt = getopt(argc, argv, "e:")) != EOF) { switch(opt) { case 'e': expt = optarg; break; default: fprintf(stderr, "Usage: rsakey [-e <expt>] <modbits> [<outfile>]\n"); return 1; } } if(optind >= argc) { fprintf(stderr, "Error: You must specify the number of modulus bits.\n"); fprintf(stderr, "Usage: rsakey [-e <expt>] <modbits> [<outfile>]\n"); return 1; } modbits = (int) strtol(argv[optind++], NULL, 0); if(modbits < CHAR_BIT) { fprintf(stderr, "Error: Invalid value for number of modulus bits.\n"); return 1; } if(modbits % 2 == 1) ++modbits; /* Check if output file is specified */ if(optind < argc) { if((ofp = fopen(argv[optind], "wt")) == NULL) { fprintf(stderr, "Error: Unable to open output file for writing.\n" " - Filename: %s\n" " - Error: %s\n", argv[optind], strerror(errno)); return 1; } } if((res = rsa_key_init(&the_key)) != MP_OK) { fprintf(stderr, "Error initializing RSA key structure:\n" " - %s (%d)\n", mp_error_string(res), res); return 1; } /* If specified, try to load the key exponent */ if(expt != NULL) { if((res = mp_int_read_string(&(the_key.e), 10, expt)) != MP_OK) { fprintf(stderr, "Error: Invalid value for encryption exponent.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } } if((res = mp_int_randomize(&(the_key.p), (modbits / 2))) != MP_OK) { fprintf(stderr, "Error: Unable to randomize first prime.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } fprintf(stderr, "p: "); find_prime(&(the_key.p), stderr); if((res = mp_int_randomize(&(the_key.q), (modbits / 2))) != MP_OK) { fprintf(stderr, "Error: Unable to randomize second prime.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } fprintf(stderr, "\nq: "); find_prime(&(the_key.q), stderr); fputc('\n', stderr); /* Temporarily, the key's "n" field will be (p - 1) * (q - 1) for purposes of computing the decryption exponent. */ mp_int_mul(&(the_key.p), &(the_key.q), &(the_key.n)); mp_int_sub(&(the_key.n), &(the_key.p), &(the_key.n)); mp_int_sub(&(the_key.n), &(the_key.q), &(the_key.n)); mp_int_add_value(&(the_key.n), 1, &(the_key.n)); if(expt == NULL && (res = mp_int_randomize(&(the_key.e), (modbits / 2))) != MP_OK) { fprintf(stderr, "Error: Unable to randomize encryption exponent.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } while((res = mp_int_invmod(&(the_key.e), &(the_key.n), &(the_key.d))) != MP_OK) { if(expt != NULL) { fprintf(stderr, "Error: Unable to compute decryption exponent.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } if((res = mp_int_randomize(&(the_key.e), (modbits / 2))) != MP_OK) { fprintf(stderr, "Error: Unable to re-randomize encryption exponent.\n" " - %s (%d)\n", mp_error_string(res), res); goto EXIT; } } /* Recompute the real modulus, now that exponents are done. */ mp_int_mul(&(the_key.p), &(the_key.q), &(the_key.n)); /* Write completed key to the specified output file */ rsa_key_write(&the_key, ofp); EXIT: fclose(ofp); rsa_key_clear(&the_key); return 0; }