static mp_result read_int_value(mp_int z, char *str)
{
  int radix = 10;

  if(*str == '#') {
    ++str;
    switch(*str) {
    case 'x': case 'X':
      radix = 16;
      break;
    case 'd': case 'D':
      radix = 10;
      break;
    case 'o': case 'O':
      radix = 8;
      break;
    case 'b': case 'B':
      radix = 2;
      break;
    default:
      return MP_RANGE;
    }
    ++str;
  }

  return mp_int_read_string(z, radix, str);
}
Example #2
0
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;
}