int test_egcd(testspec_t *t, FILE *ofp)
{
  mp_int in[5], out[3], t1 = g_zreg + 8, t2 = g_zreg + 9;
  mp_result res, expect;

  if(!parse_int_values(t, in, out, &expect))
    return imath_errno = MP_BADARG, 0;

  if((res = mp_int_egcd(in[0], in[1], in[2], in[3], in[4])) != expect)
    return imath_errno = res, 0;

  /* If we got an error we expected, return success immediately */
  if(expect != MP_OK)
    return 1;

  if((mp_int_compare(in[2], out[0]) != 0) ||
     (mp_int_compare(in[3], out[1]) != 0) ||
     (mp_int_compare(in[4], out[2]) != 0)) {
    int len, len2;
    char *str;

    /* Failure might occur because the tester computed x and y in a different
       way than we did.  Verify that the results are correct before reporting
       an error. */
    mp_int_mul(in[3], in[0], t1);
    mp_int_mul(in[4], in[1], t2);
    mp_int_add(t1, t2, t2);
    if(mp_int_compare(t2, in[2]) == 0) 
      return 1;

    mp_int_to_string(in[2], 10, g_output, OUTPUT_LIMIT);
    str = g_output + (len = strlen(g_output));
    *str++ = ',';
    mp_int_to_string(in[3], 10, str, OUTPUT_LIMIT - (len + 1));
    str = str + (len2 = strlen(str));
    *str++ = ',';
    mp_int_to_string(in[4], 10, str, OUTPUT_LIMIT - (len + len2 + 2));
    return imath_errno = OTHER_ERROR, 0;
  }

  return 1;
}
int test_mul(testspec_t *t, FILE *ofp)
{
  mp_int in[3], out[1];
  mp_result res, expect;

  if(!parse_int_values(t, in, out, &expect))
    return imath_errno = MP_BADARG, 0;

  if((res = mp_int_mul(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;
}
double get_multiply_time(int nt, int prec)
{
  clock_t start, end;
  mp_int values;
  int i;

  if((values = alloc_values(3, prec)) == NULL)
    return 0.0;
  randomize_values(values, 2, prec);

  start = clock(); 
  for(i = 0; i < nt; ++i)
    mp_int_mul(values, values + 1, values + 2);
  end = clock();

  release_values(values, 3);

  return clocks_to_seconds(start, end);
}
char* IP2Location_read128(FILE *handle, uint32_t position) 
{
	uint32_t b96_127 = IP2Location_read32(handle, position);
	uint32_t b64_95 = IP2Location_read32(handle, position + 4); 
	uint32_t b32_63 = IP2Location_read32(handle, position + 8);
	uint32_t b1_31 = IP2Location_read32(handle, position + 12);

	mpz_t result, multiplier, mp96_127, mp64_95, mp32_63, mp1_31;
	mp_int_init(&result);
	mp_int_init(&multiplier);
	mp_int_init(&mp96_127);
	mp_int_init(&mp64_95);
	mp_int_init(&mp32_63);
	mp_int_init(&mp1_31);
	
	mp_int_init_value(&multiplier, 65536);
	mp_int_mul(&multiplier, &multiplier, &multiplier);
	mp_int_init_value(&mp96_127, b96_127);
	mp_int_init_value(&mp64_95, b64_95);
	mp_int_init_value(&mp32_63, b32_63);
	mp_int_init_value(&mp1_31, b1_31);

	mp_int_mul(&mp1_31, &multiplier, &mp1_31);
	mp_int_mul(&mp1_31, &multiplier, &mp1_31);
	mp_int_mul(&mp1_31, &multiplier, &mp1_31);

	mp_int_mul(&mp32_63, &multiplier, &mp32_63);
	mp_int_mul(&mp32_63, &multiplier, &mp32_63);

	mp_int_mul(&mp64_95, &multiplier, &mp64_95);
	
	mp_int_add(&mp1_31, &mp32_63, &result);
	mp_int_add(&result, &mp64_95, &result);
	mp_int_add(&result, &mp96_127, &result);
	return IP2Location_mp2string(result);
}
Example #5
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;
}