int test_divp2(testspec_t *t, FILE *ofp)
{
  mp_int in[4], out[2];
  mp_result res, expect;
  int p2;

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

  if((res = mp_int_to_int(in[1], &p2)) != MP_OK)
    return imath_errno = res, 0;

  if((res = mp_int_div_pow2(in[0], p2, in[2], in[3])) != expect)
    return imath_errno = res, 0;

  if(expect == MP_OK &&
     ((mp_int_compare(in[2], out[0]) != 0) ||
      (mp_int_compare(in[3], out[1]) != 0))) {
    int len;
    char *str;
    
    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));
    return imath_errno = OTHER_ERROR, 0;
  }

  return 1;
}
int test_divv(testspec_t *t, FILE *ofp)
{
  mp_int in[3], out[2];
  mp_result res, expect;
  int v, rem, orem;

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

  if((res = mp_int_to_int(in[1], &v)) != MP_OK)
    return imath_errno = res, 0;
  if((res = mp_int_to_int(out[1], &orem)) != MP_OK)
    return imath_errno = res, 0;

  if((res = mp_int_div_value(in[0], v, in[2], &rem)) != expect)
    return imath_errno = res, 0;

  if(expect == MP_OK &&
     ((mp_int_compare(in[2], out[0]) != 0) || (rem != orem))) {
    char *str;
    
    mp_int_to_string(in[2], 10, g_output, OUTPUT_LIMIT);
    str = g_output + strlen(g_output);
    *str++ = ',';
    sprintf(str, "%d", rem);
    return imath_errno = OTHER_ERROR, 0;
  }

  return 1;
}
int test_sub(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, "subv") == 0) {
    if((res = mp_int_to_int(in[1], &v)) != MP_OK)
      return imath_errno = res, 0;
    if((res = mp_int_sub_value(in[0], v, in[2])) != expect)
      return imath_errno = res, 0;
  } else {
    if((res = mp_int_sub(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;
}
Beispiel #4
0
static mp_result s_rsa_transform(mp_int msg, mp_int exp, 
				 mp_int mod, mp_int out)
{
  if(mp_int_compare_zero(msg) < 0 ||
     mp_int_compare(msg, mod) >= 0)
    return MP_RANGE;

  return mp_int_exptmod(msg, exp, mod, out);
}
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_exptmod(testspec_t *t, FILE *ofp)
{
  mp_int in[4], out[1];
  mp_result res, expect;

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

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

  if(expect == MP_OK && mp_int_compare(in[3], out[0]) != 0) {
    mp_int_to_string(in[3], 10, g_output, OUTPUT_LIMIT);
    return imath_errno = OTHER_ERROR, 0;
  }

  return 1;
}
int test_read_uns(testspec_t *t, FILE *ofp)
{
  mp_int out[1], in = g_zreg + 1;
  int in_len;
  mp_result res, expect;

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

  trim_line(t->input[0]);
  if((in_len = parse_binary(t->input[0], g_bin1, sizeof(g_bin1))) < 0)
    return imath_errno = MP_BADARG, 0;

  if((res = mp_int_read_unsigned(in, g_bin1, in_len)) != expect)
    return imath_errno = res, 0;

  if(expect == MP_OK && mp_int_compare(in, out[0]) != 0) {
    mp_int_to_string(in, 10, g_output, OUTPUT_LIMIT);
    return imath_errno = OTHER_ERROR, 0;
  }

  return 1;
}
Beispiel #8
0
static int
gmp_rsa_private_decrypt(int flen, const unsigned char* from,
			  unsigned char* to, RSA* rsa, int padding)
{
    unsigned char *ptr;
    size_t size;
    mpz_t in, out, n, e;

    if (padding != RSA_PKCS1_PADDING)
	return -1;

    size = RSA_size(rsa);
    if (flen > size)
	return -2;

    mpz_init(in);
    mpz_init(out);

    BN2mpz(n, rsa->n);
    BN2mpz(e, rsa->e);

    mpz_import(in, flen, 1, 1, 1, 0, from);

    if(mpz_cmp_ui(in, 0) < 0 ||
       mpz_cmp(in, n) >= 0) {
	size = 0;
	goto out;
    }

    if (rsa->p && rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp) {
	mpz_t p, q, dmp1, dmq1, iqmp;

	BN2mpz(p, rsa->p);
	BN2mpz(q, rsa->q);
	BN2mpz(dmp1, rsa->dmp1);
	BN2mpz(dmq1, rsa->dmq1);
	BN2mpz(iqmp, rsa->iqmp);

	rsa_private_calculate(in, p, q, dmp1, dmq1, iqmp, out);

	mpz_clear(p);
	mpz_clear(q);
	mpz_clear(dmp1);
	mpz_clear(dmq1);
	mpz_clear(iqmp);
    } else {
	mpz_t d;

#if 0
	if(mp_int_compare_zero(&in) < 0 ||
	   mp_int_compare(&in, &n) >= 0)
	    return MP_RANGE;
#endif

	BN2mpz(d, rsa->d);
	mpz_powm(out, in, d, n);
	mpz_clear(d);
    }

    ptr = to;
    {
	size_t ssize;
	mpz_export(ptr, &ssize, 1, 1, 1, 0, out);
	assert(size >= ssize);
	size = ssize;
    }

    /* head zero was skipped by mp_int_to_unsigned */
    if (*ptr != 2)
	return -3;
    size--; ptr++;
    while (size && *ptr != 0) {
	size--; ptr++;
    }
    if (size == 0)
	return -4;
    size--; ptr++;

    memmove(to, ptr, size);

out:
    mpz_clear(e);
    mpz_clear(n);
    mpz_clear(in);
    mpz_clear(out);

    return size;
}
Beispiel #9
0
static int
gmp_rsa_private_encrypt(int flen, const unsigned char* from,
			  unsigned char* to, RSA* rsa, int padding)
{
    unsigned char *p, *p0;
    size_t size;
    mpz_t in, out, n, e;

    if (padding != RSA_PKCS1_PADDING)
	return -1;

    size = RSA_size(rsa);

    if (size < RSA_PKCS1_PADDING_SIZE || size - RSA_PKCS1_PADDING_SIZE < flen)
	return -2;

    p0 = p = malloc(size);
    *p++ = 0;
    *p++ = 1;
    memset(p, 0xff, size - flen - 3);
    p += size - flen - 3;
    *p++ = 0;
    memcpy(p, from, flen);
    p += flen;
    assert((p - p0) == size);

    BN2mpz(n, rsa->n);
    BN2mpz(e, rsa->e);

    mpz_init(in);
    mpz_init(out);
    mpz_import(in, size, 1, 1, 1, 0, p0);
    free(p0);

#if 0
    if(mp_int_compare_zero(&in) < 0 ||
       mp_int_compare(&in, &n) >= 0) {
	size = 0;
	goto out;
    }
#endif

    if (rsa->p && rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp) {
	mpz_t p, q, dmp1, dmq1, iqmp;

	BN2mpz(p, rsa->p);
	BN2mpz(q, rsa->q);
	BN2mpz(dmp1, rsa->dmp1);
	BN2mpz(dmq1, rsa->dmq1);
	BN2mpz(iqmp, rsa->iqmp);

	rsa_private_calculate(in, p, q, dmp1, dmq1, iqmp, out);

	mpz_clear(p);
	mpz_clear(q);
	mpz_clear(dmp1);
	mpz_clear(dmq1);
	mpz_clear(iqmp);
    } else {
	mpz_t d;

	BN2mpz(d, rsa->d);
	mpz_powm(out, in, d, n);
	mpz_clear(d);
    }

    {
	size_t ssize;
	mpz_export(to, &ssize, 1, 1, 1, 0, out);
	assert(size >= ssize);
	size = ssize;
    }

    mpz_clear(e);
    mpz_clear(n);
    mpz_clear(in);
    mpz_clear(out);

    return size;
}