Beispiel #1
0
int main(int argc, char *argv[]) {
  mp_result mode, len, res = 0;
  mp_size prec, radix;
  mpq_t value;
  char *buf;

  if (argc < 5) {
    fprintf(stderr, "Usage: rounding <mode> <precision> <radix> <value>\n");
    return 1;
  }

  if ((res = mp_rat_init(&value)) != MP_OK) {
    fprintf(stderr, "Error initializing: %s\n", mp_error_string(res));
    return 2;
  }

  mode = atoi(argv[1]);
  prec = atoi(argv[2]);
  radix = atoi(argv[3]);

  printf(
      "Rounding mode:   %d\n"
      "Precision:       %u digits\n"
      "Radix:           %u\n"
      "Input string:    \"%s\"\n",
      mode, prec, radix, argv[4]);

  if ((res = mp_rat_read_decimal(&value, radix, argv[4])) != MP_OK) {
    fprintf(stderr, "Error reading input string: %s\n", mp_error_string(res));
    goto CLEANUP;
  }

  len = mp_rat_decimal_len(&value, radix, prec);
  buf = malloc(len);

  if ((res = mp_rat_to_decimal(&value, radix, prec, mode, buf, len)) != MP_OK) {
    fprintf(stderr, "Error converting output: %s\n", mp_error_string(res));
  }

  printf("Result string:   \"%s\"\n", buf);
  free(buf);

CLEANUP:
  mp_rat_clear(&value);
  return res;
}
Beispiel #2
0
int main(int argc, char *argv[])
{
  mp_size   radix = 10;  /* Default output radix */
  mpq_t     value;
  mp_result res;
  char      *endp;

  if(argc < 2) {
    fprintf(stderr, "Usage: input <value> [output-base]\n");
    return 1;
  }
  if(argc > 2) {
    if((radix = atoi(argv[2])) < MP_MIN_RADIX ||
       (radix > MP_MAX_RADIX)) {
      fprintf(stderr, "Error:  Specified radix is out of range (%d)\n",
	      radix);
      return 1;
    }
  }

  /* Initialize a new value, initially zero; illustrates how to check
     for errors (e.g., out of memory) and display a message.  */
  if((res = mp_rat_init(&value)) != MP_OK) {
    fprintf(stderr, "Error in mp_rat_init(): %s\n", 
	    mp_error_string(res));
    return 1;
  }

  /* Read value in base 10 */
  if((res = mp_rat_read_ustring(&value, 0, argv[1], &endp)) != MP_OK) {
    fprintf(stderr, "Error in mp_rat_read_ustring(): %s\n",
	    mp_error_string(res));
    
    if(res == MP_TRUNC)
      fprintf(stderr, " -- remaining input is: %s\n", endp);
    
    mp_rat_clear(&value);
    return 1;
  }
  
  printf("Here is your value in base %d\n", radix);
  {
    mp_result buf_size, res;
    char *obuf;

    if(mp_rat_is_integer(&value)) {
      /* Allocate a buffer big enough to hold the given value, including
	 sign and zero terminator. */
      buf_size = mp_int_string_len(MP_NUMER_P(&value), radix);
      obuf = malloc(buf_size);

      /* Convert the value to a string in the desired radix. */
      if((res = mp_int_to_string(MP_NUMER_P(&value), radix, 
				 obuf, buf_size)) != MP_OK) {
	fprintf(stderr, "Converstion to base %d failed: %s\n",
		radix, mp_error_string(res));
	mp_rat_clear(&value);
	return 1;
      }
    } 
    else {
      /* Allocate a buffer big enough to hold the given value, including
	 sign and zero terminator. */
      buf_size = mp_rat_string_len(&value, radix);
      obuf = malloc(buf_size);

      /* Convert the value to a string in the desired radix. */
      if((res = mp_rat_to_string(&value, radix, obuf, buf_size)) != MP_OK) {
	fprintf(stderr, "Conversion to base %d failed: %s\n",
		radix, mp_error_string(res));
	mp_rat_clear(&value);
	return 1;
      }
    }
    fputs(obuf, stdout);
    fputc('\n', stdout);
    free(obuf);
  }

  /* When you are done with a value, it must be "cleared" to release
     the memory it occupies */
  mp_rat_clear(&value);
  return 0;
}
Beispiel #3
0
int main(int argc, char *argv[])
{
  int       opt, modbits;
  FILE     *ofp = stdout;
  mp_result res;
  find_f    find_func = find_prime;
  char      tag = 'p';
  mpz_t     value;

  /* Process command-line arguments */
  while((opt = getopt(argc, argv, "s")) != EOF) {
    switch(opt) {
    case 's':
      find_func = find_strong_prime;
      tag = 'P';
      break;
    default:
      fprintf(stderr, "Usage: randprime [-s] <bits> [<outfile>]\n");
      return 1;
    }
  }
  
  if(optind >= argc) {
    fprintf(stderr, "Error:  You must specify the number of significant bits.\n");
    fprintf(stderr, "Usage: randprime [-s] <bits> [<outfile>]\n");
    return 1;
  }
  modbits = (int) strtol(argv[optind++], NULL, 0);
  if(modbits < CHAR_BIT) {
    fprintf(stderr, "Error:  Invalid value for number of significant 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;
    }
  }
  
  mp_int_init(&value);
  if ((res = mp_int_randomize(&value, modbits - 1)) != MP_OK) {
    fprintf(stderr, "Error:  Unable to generate random start value.\n"
	    " - %s (%d)\n", mp_error_string(res), res);
    goto EXIT;
  }
  fprintf(stderr, "%c: ", tag);
  find_func(&value, stderr);
  fputc('\n', stderr);

  /* Write the completed value to the specified output file */
  {
    int len;
    char *obuf;

    len = mp_int_string_len(&value, 10);
    obuf = malloc(len);
    mp_int_to_string(&value, 10, obuf, len);
    fputs(obuf, ofp);
    fputc('\n', ofp);

    free(obuf);
  }

 EXIT:
  fclose(ofp);
  mp_int_clear(&value);
  return 0;
}
Beispiel #4
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;
}