void jacobian_double(struct jacobian_point *p, const struct domain_params *dp) { if (gcry_mpi_cmp_ui(p->z, 0)) { if (gcry_mpi_cmp_ui(p->y, 0)) { gcry_mpi_t t1, t2; t1 = gcry_mpi_snew(0); t2 = gcry_mpi_snew(0); gcry_mpi_mulm(t1, p->x, p->x, dp->m); gcry_mpi_addm(t2, t1, t1, dp->m); gcry_mpi_addm(t2, t2, t1, dp->m); gcry_mpi_mulm(t1, p->z, p->z, dp->m); gcry_mpi_mulm(t1, t1, t1, dp->m); gcry_mpi_mulm(t1, t1, dp->a, dp->m); gcry_mpi_addm(t1, t1, t2, dp->m); gcry_mpi_mulm(p->z, p->z, p->y, dp->m); gcry_mpi_addm(p->z, p->z, p->z, dp->m); gcry_mpi_mulm(p->y, p->y, p->y, dp->m); gcry_mpi_addm(p->y, p->y, p->y, dp->m); gcry_mpi_mulm(t2, p->x, p->y, dp->m); gcry_mpi_addm(t2, t2, t2, dp->m); gcry_mpi_mulm(p->x, t1, t1, dp->m); gcry_mpi_subm(p->x, p->x, t2, dp->m); gcry_mpi_subm(p->x, p->x, t2, dp->m); gcry_mpi_subm(t2, t2, p->x, dp->m); gcry_mpi_mulm(t1, t1, t2, dp->m); gcry_mpi_mulm(t2, p->y, p->y, dp->m); gcry_mpi_addm(t2, t2, t2, dp->m); gcry_mpi_subm(p->y, t1, t2, dp->m); gcry_mpi_release(t1); gcry_mpi_release(t2); } else gcry_mpi_set_ui(p->z, 0); } }
int ECDSA_verify(const char *msg, const struct affine_point *Q, const gcry_mpi_t sig, const struct curve_params *cp) { gcry_mpi_t e, r, s; struct affine_point X1, X2; int res = 0; r = gcry_mpi_new(0); s = gcry_mpi_new(0); gcry_mpi_div(s, r, sig, cp->dp.order, 0); if (gcry_mpi_cmp_ui(s, 0) <= 0 || gcry_mpi_cmp(s, cp->dp.order) >= 0 || gcry_mpi_cmp_ui(r, 0) <= 0 || gcry_mpi_cmp(r, cp->dp.order) >= 0) goto end; gcry_mpi_scan(&e, GCRYMPI_FMT_USG, msg, 64, NULL); gcry_mpi_mod(e, e, cp->dp.order); gcry_mpi_invm(s, s, cp->dp.order); gcry_mpi_mulm(e, e, s, cp->dp.order); X1 = pointmul(&cp->dp.base, e, &cp->dp); gcry_mpi_mulm(e, r, s, cp->dp.order); X2 = pointmul(Q, e, &cp->dp); point_add(&X1, &X2, &cp->dp); gcry_mpi_release(e); if (! point_is_zero(&X1)) { gcry_mpi_mod(s, X1.x, cp->dp.order); res = ! gcry_mpi_cmp(s, r); } point_release(&X1); point_release(&X2); end: gcry_mpi_release(r); gcry_mpi_release(s); return res; }
/* Algorithm 4.26 in the "Guide to Elliptic Curve Cryptography" */ int embedded_key_validation(const struct affine_point *p, const struct domain_params *dp) { if (gcry_mpi_cmp_ui(p->x, 0) < 0 || gcry_mpi_cmp(p->x, dp->m) >= 0 || gcry_mpi_cmp_ui(p->y, 0) < 0 || gcry_mpi_cmp(p->y, dp->m) >= 0) return 0; return ! point_is_zero(p) && point_on_curve(p, dp); }
int nuts_mpi_cmp(const nuts_mpi_t mpi1, const nuts_mpi_t mpi2) { if (mpi1 == NULL && mpi2 == NULL) { return 0; } else if (mpi1 == NULL && mpi2 != NULL) { return -1 * gcry_mpi_cmp_ui(mpi2->mpi, 0); return 0; } else if (mpi1 != NULL && mpi2 == NULL) { return gcry_mpi_cmp_ui(mpi1->mpi, 0); } else /* mpi1 != NULL && mpi2 != NULL */ { return gcry_mpi_cmp(mpi1->mpi, mpi2->mpi); } }
int point_decompress(struct affine_point *p, const gcry_mpi_t x, int yflag, const struct domain_params *dp) { gcry_mpi_t h, y; int res; h = gcry_mpi_snew(0); y = gcry_mpi_snew(0); gcry_mpi_mulm(h, x, x, dp->m); gcry_mpi_addm(h, h, dp->a, dp->m); gcry_mpi_mulm(h, h, x, dp->m); gcry_mpi_addm(h, h, dp->b, dp->m); if ((res = mod_root(y, h, dp->m))) if ((res = (gcry_mpi_cmp_ui(y, 0) || ! yflag))) { p->x = gcry_mpi_snew(0); p->y = gcry_mpi_snew(0); gcry_mpi_set(p->x, x); if (gcry_mpi_test_bit(y, 0) == yflag) gcry_mpi_set(p->y, y); else gcry_mpi_sub(p->y, dp->m, y); assert(point_on_curve(p, dp)); } gcry_mpi_release(h); gcry_mpi_release(y); return res; }
int nuts_mpi_cmp_ul(const nuts_mpi_t mpi, const unsigned long ul) { if (mpi != NULL) { return gcry_mpi_cmp_ui(mpi->mpi, ul); } else { return (ul > 0) ? -1 : 0; } }
/* Algorithms 4.29 and 4.30 in the "Guide to Elliptic Curve Cryptography" */ gcry_mpi_t ECDSA_sign(const char *msg, const gcry_mpi_t d, const struct curve_params *cp) { struct affine_point p1; gcry_mpi_t e, k, r, s; #if ECDSA_DETERMINISTIC struct aes256cprng *cprng; cprng = ecdsa_cprng_init(msg, d, cp); #endif r = gcry_mpi_snew(0); s = gcry_mpi_snew(0); Step1: #if ECDSA_DETERMINISTIC k = ecdsa_cprng_get_exponent(cprng, cp); #else k = get_random_exponent(cp); #endif p1 = pointmul(&cp->dp.base, k, &cp->dp); gcry_mpi_mod(r, p1.x, cp->dp.order); point_release(&p1); if (! gcry_mpi_cmp_ui(r, 0)) { gcry_mpi_release(k); goto Step1; } gcry_mpi_scan(&e, GCRYMPI_FMT_USG, msg, 64, NULL); gcry_mpi_set_flag(e, GCRYMPI_FLAG_SECURE); gcry_mpi_mod(e, e, cp->dp.order); gcry_mpi_mulm(s, d, r, cp->dp.order); gcry_mpi_addm(s, s, e, cp->dp.order); gcry_mpi_invm(e, k, cp->dp.order); gcry_mpi_mulm(s, s, e, cp->dp.order); gcry_mpi_release(e); gcry_mpi_release(k); if (! gcry_mpi_cmp_ui(s, 0)) goto Step1; gcry_mpi_mul(s, s, cp->dp.order); gcry_mpi_add(s, s, r); gcry_mpi_release(r); #if ECDSA_DETERMINISTIC ecdsa_cprng_done(cprng); #endif return s; }
static void mpi_export(void *buf, size_t buflen, const gcry_mpi_t x) { unsigned len; size_t nwritten; assert(gcry_mpi_cmp_ui(x, 0) >= 0); len = (gcry_mpi_get_nbits(x) + 7) / 8; assert(len <= buflen); memset(buf, 0, buflen); gcry_mpi_print(GCRYMPI_FMT_USG, buf + (buflen - len), len, &nwritten, x); assert(nwritten == len); }
static gcry_mpi_t mpi_import(const void *buf, size_t buflen) { gcry_mpi_t h; unsigned len; gcry_mpi_scan(&h, GCRYMPI_FMT_USG, buf, buflen, NULL); len = (gcry_mpi_get_nbits(h) + 7) / 8; assert(len <= buflen); assert(gcry_mpi_cmp_ui(h, 0) >= 0); return h; }
int mixin_key_and_curve(struct affine_point *P, gcry_mpi_t pubkey, const struct curve_params *cp) { int yflag, res; if ((yflag = (gcry_mpi_cmp(pubkey, cp->dp.m) >= 0))) gcry_mpi_sub(pubkey, pubkey, cp->dp.m); res = gcry_mpi_cmp_ui(pubkey, 0) >= 0 && gcry_mpi_cmp(pubkey, cp->dp.m) < 0 && point_decompress(P, pubkey, yflag, &cp->dp); return res; }
gcry_mpi_t get_random_exponent(const struct curve_params *cp) { int bits = gcry_mpi_get_nbits(cp->dp.order); gcry_mpi_t a; a = gcry_mpi_snew(0); do { gcry_mpi_randomize(a, bits, GCRY_STRONG_RANDOM); gcry_mpi_clear_highbit(a, bits); } while (! gcry_mpi_cmp_ui(a, 0) || gcry_mpi_cmp(a, cp->dp.order) >= 0); return a; }
char *ssh_gcry_bn2dec(bignum bn) { bignum bndup, num, ten; char *ret; int count, count2; int size, rsize; char decnum; size = gcry_mpi_get_nbits(bn) * 3; rsize = size / 10 + size / 1000 + 2; ret = malloc(rsize + 1); if (ret == NULL) { return NULL; } if (!gcry_mpi_cmp_ui(bn, 0)) { strcpy(ret, "0"); } else { ten = bignum_new(); if (ten == NULL) { SAFE_FREE(ret); return NULL; } num = bignum_new(); if (num == NULL) { SAFE_FREE(ret); bignum_safe_free(ten); return NULL; } for (bndup = gcry_mpi_copy(bn), bignum_set_word(ten, 10), count = rsize; count; count--) { gcry_mpi_div(bndup, num, bndup, ten, 0); for (decnum = 0, count2 = gcry_mpi_get_nbits(num); count2; decnum *= 2, decnum += (gcry_mpi_test_bit(num, count2 - 1) ? 1 : 0), count2--) ; ret[count - 1] = decnum + '0'; } for (count = 0; count < rsize && ret[count] == '0'; count++) ; for (count2 = 0; count2 < rsize - count; ++count2) { ret[count2] = ret[count2 + count]; } ret[count2] = 0; bignum_safe_free(num); bignum_safe_free(bndup); bignum_safe_free(ten); } return ret; }
struct affine_point jacobian_to_affine(const struct jacobian_point *p, const struct domain_params *dp) { struct affine_point r = point_new(); if (gcry_mpi_cmp_ui(p->z, 0)) { gcry_mpi_t h; h = gcry_mpi_snew(0); gcry_mpi_invm(h, p->z, dp->m); gcry_mpi_mulm(r.y, h, h, dp->m); gcry_mpi_mulm(r.x, p->x, r.y, dp->m); gcry_mpi_mulm(r.y, r.y, h, dp->m); gcry_mpi_mulm(r.y, r.y, p->y, dp->m); gcry_mpi_release(h); } return r; }
gboolean egg_dh_gen_pair (gcry_mpi_t prime, gcry_mpi_t base, guint bits, gcry_mpi_t *pub, gcry_mpi_t *priv) { guint pbits; g_return_val_if_fail (prime, FALSE); g_return_val_if_fail (base, FALSE); g_return_val_if_fail (pub, FALSE); g_return_val_if_fail (priv, FALSE); pbits = gcry_mpi_get_nbits (prime); g_return_val_if_fail (pbits > 1, FALSE); if (bits == 0) { bits = pbits; } else if (bits > pbits) { g_return_val_if_reached (FALSE); } /* * Generate a strong random number of bits, and not zero. * gcry_mpi_randomize bumps up to the next byte. Since we * need to have a value less than half of prime, we make sure * we bump down. */ *priv = gcry_mpi_snew (bits); g_return_val_if_fail (*priv, FALSE); while (gcry_mpi_cmp_ui (*priv, 0) == 0) gcry_mpi_randomize (*priv, bits, GCRY_STRONG_RANDOM); /* Secret key value must be less than half of p */ if (gcry_mpi_get_nbits (*priv) > bits) gcry_mpi_clear_highbit (*priv, bits); if (gcry_mpi_get_nbits (*priv) > pbits - 1) gcry_mpi_clear_highbit (*priv, pbits - 1); g_assert (gcry_mpi_cmp (prime, *priv) > 0); *pub = gcry_mpi_new (gcry_mpi_get_nbits (*priv)); g_return_val_if_fail (*pub, FALSE); gcry_mpi_powm (*pub, base, *priv, prime); return TRUE; }
void jacobian_affine_point_add(struct jacobian_point *p1, const struct affine_point *p2, const struct domain_params *dp) { if (! point_is_zero(p2)) { if (gcry_mpi_cmp_ui(p1->z, 0)) { gcry_mpi_t t1, t2, t3; t1 = gcry_mpi_snew(0); t2 = gcry_mpi_snew(0); gcry_mpi_mulm(t1, p1->z, p1->z, dp->m); gcry_mpi_mulm(t2, t1, p2->x, dp->m); gcry_mpi_mulm(t1, t1, p1->z, dp->m); gcry_mpi_mulm(t1, t1, p2->y, dp->m); if (! gcry_mpi_cmp(p1->x, t2)) { if (! gcry_mpi_cmp(p1->y, t1)) jacobian_double(p1, dp); else jacobian_load_zero(p1); } else { t3 = gcry_mpi_snew(0); gcry_mpi_subm(p1->x, p1->x, t2, dp->m); gcry_mpi_subm(p1->y, p1->y, t1, dp->m); gcry_mpi_mulm(p1->z, p1->z, p1->x, dp->m); gcry_mpi_mulm(t3, p1->x, p1->x, dp->m); gcry_mpi_mulm(t2, t2, t3, dp->m); gcry_mpi_mulm(t3, t3, p1->x, dp->m); gcry_mpi_mulm(t1, t1, t3, dp->m); gcry_mpi_mulm(p1->x, p1->y, p1->y, dp->m); gcry_mpi_subm(p1->x, p1->x, t3, dp->m); gcry_mpi_subm(p1->x, p1->x, t2, dp->m); gcry_mpi_subm(p1->x, p1->x, t2, dp->m); gcry_mpi_subm(t2, t2, p1->x, dp->m); gcry_mpi_mulm(p1->y, p1->y, t2, dp->m); gcry_mpi_subm(p1->y, p1->y, t1, dp->m); gcry_mpi_release(t3); } gcry_mpi_release(t1); gcry_mpi_release(t2); } else jacobian_load_affine(p1, p2); } }
static void check_generated_rsa_key (gcry_sexp_t key, unsigned long expected_e) { gcry_sexp_t skey, pkey, list; pkey = gcry_sexp_find_token (key, "public-key", 0); if (!pkey) fail ("public part missing in return value\n"); else { gcry_mpi_t e = NULL; list = gcry_sexp_find_token (pkey, "e", 0); if (!list || !(e=gcry_sexp_nth_mpi (list, 1, 0)) ) fail ("public exponent not found\n"); else if (!expected_e) { if (verbose) show_mpi ("public exponent: ", e); } else if ( gcry_mpi_cmp_ui (e, expected_e)) { show_mpi ("public exponent: ", e); fail ("public exponent is not %lu\n", expected_e); } gcry_sexp_release (list); gcry_mpi_release (e); gcry_sexp_release (pkey); } skey = gcry_sexp_find_token (key, "private-key", 0); if (!skey) fail ("private part missing in return value\n"); else { int rc = gcry_pk_testkey (skey); if (rc) fail ("gcry_pk_testkey failed: %s\n", gpg_strerror (rc)); gcry_sexp_release (skey); } }
void serialize_mpi(char *outbuf, int outlen, enum disp_format df, const gcry_mpi_t x) { switch(df) { case DF_BIN: do { int len = (gcry_mpi_get_nbits(x) + 7) / 8; assert(len <= outlen); memset(outbuf, 0, outlen - len); gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char*)outbuf + (outlen - len), len, NULL, x); } while (0); break; case DF_COMPACT: case DF_BASE36: do { const char *digits = get_digits(df); unsigned int digit_count = get_digit_count(df); gcry_mpi_t base, Q, R; int i; base = gcry_mpi_set_ui(NULL, digit_count); Q = gcry_mpi_copy(x); R = gcry_mpi_snew(0); for(i = outlen - 1; i >= 0; i--) { unsigned char digit = 0; gcry_mpi_div(Q, R, Q, base, 0); gcry_mpi_print(GCRYMPI_FMT_USG, &digit, 1, NULL, R); assert(digit < digit_count); outbuf[i] = digits[digit]; } assert(! gcry_mpi_cmp_ui(Q, 0)); gcry_mpi_release(base); gcry_mpi_release(Q); gcry_mpi_release(R); } while(0); break; default: assert(0); } }
int32_t ZrtpDH::checkPubKey(uint8_t *pubKeyBytes) const { gcry_mpi_t pubKeyOther = NULL; gcry_mpi_scan(&pubKeyOther, GCRYMPI_FMT_USG, pubKeyBytes, getDhSize(), NULL); if (pkType == DH2K) { if (gcry_mpi_cmp(bnP2048MinusOne, pubKeyOther) == 0) return 0; } else if (pkType == DH3K) { if (gcry_mpi_cmp(bnP3072MinusOne, pubKeyOther) == 0) return 0; } else { // if (gcry_mpi_cmp(bnP4096MinusOne, pubKeyOther) == 0) return 0; } if (gcry_mpi_cmp_ui(pubKeyOther, 1) == 0) { return 0; } gcry_mpi_release(pubKeyOther); return 1; }
int get_serialization_len(const gcry_mpi_t x, enum disp_format df) { int res; switch(df) { case DF_BIN: res = (gcry_mpi_get_nbits(x) + 7) / 8; break; case DF_BASE36: case DF_COMPACT: do { gcry_mpi_t base, Q; base = gcry_mpi_set_ui(NULL, get_digit_count(df)); Q = gcry_mpi_copy(x); for(res = 0; gcry_mpi_cmp_ui(Q, 0); res++) gcry_mpi_div(Q, NULL, Q, base, 0); gcry_mpi_release(base); gcry_mpi_release(Q); } while (0); break; default: assert(0); } return res; }
/* Check that the RSA secret key SKEY is valid. Swap parameters to the libgcrypt standard. */ static gpg_error_t rsa_key_check (struct rsa_secret_key_s *skey) { int err = 0; gcry_mpi_t t = gcry_mpi_snew (0); gcry_mpi_t t1 = gcry_mpi_snew (0); gcry_mpi_t t2 = gcry_mpi_snew (0); gcry_mpi_t phi = gcry_mpi_snew (0); /* Check that n == p * q. */ gcry_mpi_mul (t, skey->p, skey->q); if (gcry_mpi_cmp( t, skey->n) ) { log_error ("RSA oops: n != p * q\n"); err++; } /* Check that p is less than q. */ if (gcry_mpi_cmp (skey->p, skey->q) > 0) { gcry_mpi_t tmp; log_info ("swapping secret primes\n"); tmp = gcry_mpi_copy (skey->p); gcry_mpi_set (skey->p, skey->q); gcry_mpi_set (skey->q, tmp); gcry_mpi_release (tmp); /* Recompute u. */ gcry_mpi_invm (skey->u, skey->p, skey->q); } /* Check that e divides neither p-1 nor q-1. */ gcry_mpi_sub_ui (t, skey->p, 1 ); gcry_mpi_div (NULL, t, t, skey->e, 0); if (!gcry_mpi_cmp_ui( t, 0) ) { log_error ("RSA oops: e divides p-1\n"); err++; } gcry_mpi_sub_ui (t, skey->q, 1); gcry_mpi_div (NULL, t, t, skey->e, 0); if (!gcry_mpi_cmp_ui( t, 0)) { log_info ("RSA oops: e divides q-1\n" ); err++; } /* Check that d is correct. */ gcry_mpi_sub_ui (t1, skey->p, 1); gcry_mpi_sub_ui (t2, skey->q, 1); gcry_mpi_mul (phi, t1, t2); gcry_mpi_invm (t, skey->e, phi); if (gcry_mpi_cmp (t, skey->d)) { /* No: try universal exponent. */ gcry_mpi_gcd (t, t1, t2); gcry_mpi_div (t, NULL, phi, t, 0); gcry_mpi_invm (t, skey->e, t); if (gcry_mpi_cmp (t, skey->d)) { log_error ("RSA oops: bad secret exponent\n"); err++; } } /* Check for correctness of u. */ gcry_mpi_invm (t, skey->p, skey->q); if (gcry_mpi_cmp (t, skey->u)) { log_info ("RSA oops: bad u parameter\n"); err++; } if (err) log_info ("RSA secret key check failed\n"); gcry_mpi_release (t); gcry_mpi_release (t1); gcry_mpi_release (t2); gcry_mpi_release (phi); return err? gpg_error (GPG_ERR_BAD_SECKEY):0; }
int jacobian_is_zero(const struct jacobian_point *p) { return ! gcry_mpi_cmp_ui(p->z, 0); }
int point_is_zero(const struct affine_point *p) { return ! gcry_mpi_cmp_ui(p->x, 0) && ! gcry_mpi_cmp_ui(p->y, 0); }
static void gen_prime (gcry_mpi_t * ptest, unsigned int nbits, struct GNUNET_HashCode * hc) { /* Note: 2 is not included because it can be tested more easily by * looking at bit 0. The last entry in this list is marked by a zero */ static const uint16_t small_prime_numbers[] = { 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 0 }; #define DIM(v) (sizeof(v)/sizeof((v)[0])) static int no_of_small_prime_numbers = DIM (small_prime_numbers) - 1; gcry_mpi_t prime, pminus1, val_2, val_3, result; unsigned int i; unsigned int step; unsigned int mods[no_of_small_prime_numbers]; gcry_mpi_t tmp; gcry_mpi_t sp; GNUNET_assert (nbits >= 16); /* Make nbits fit into mpz_t implementation. */ val_2 = gcry_mpi_set_ui (NULL, 2); val_3 = gcry_mpi_set_ui (NULL, 3); prime = gcry_mpi_snew (0); result = gcry_mpi_new (0); pminus1 = gcry_mpi_new (0); *ptest = gcry_mpi_new (0); tmp = gcry_mpi_new (0); sp = gcry_mpi_new (0); while (1) { /* generate a random number */ mpz_randomize (prime, nbits, hc); /* Set high order bit to 1, set low order bit to 1. If we are * generating a secret prime we are most probably doing that * for RSA, to make sure that the modulus does have the * requested key size we set the 2 high order bits. */ gcry_mpi_set_bit (prime, nbits - 1); gcry_mpi_set_bit (prime, nbits - 2); gcry_mpi_set_bit (prime, 0); /* Calculate all remainders. */ for (i = 0; i < no_of_small_prime_numbers; i++) { size_t written; gcry_mpi_set_ui (sp, small_prime_numbers[i]); gcry_mpi_div (NULL, tmp, prime, sp, -1); mods[i] = 0; written = sizeof (unsigned int); GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) &mods[i], written, &written, tmp)); adjust ((unsigned char *) &mods[i], written, sizeof (unsigned int)); mods[i] = ntohl (mods[i]); } /* Now try some primes starting with prime. */ for (step = 0; step < 20000; step += 2) { /* Check against all the small primes we have in mods. */ for (i = 0; i < no_of_small_prime_numbers; i++) { uint16_t x = small_prime_numbers[i]; while (mods[i] + step >= x) mods[i] -= x; if (!(mods[i] + step)) break; } if (i < no_of_small_prime_numbers) continue; /* Found a multiple of an already known prime. */ gcry_mpi_add_ui (*ptest, prime, step); if (!gcry_mpi_test_bit (*ptest, nbits - 2)) break; /* Do a fast Fermat test now. */ gcry_mpi_sub_ui (pminus1, *ptest, 1); gcry_mpi_powm (result, val_2, pminus1, *ptest); if ((!gcry_mpi_cmp_ui (result, 1)) && (is_prime (*ptest, 5, hc))) { /* Got it. */ gcry_mpi_release (sp); gcry_mpi_release (tmp); gcry_mpi_release (val_2); gcry_mpi_release (val_3); gcry_mpi_release (result); gcry_mpi_release (pminus1); gcry_mpi_release (prime); return; } } } }
/** * Return true if n is probably a prime */ static int is_prime (gcry_mpi_t n, int steps, struct GNUNET_HashCode * hc) { gcry_mpi_t x; gcry_mpi_t y; gcry_mpi_t z; gcry_mpi_t nminus1; gcry_mpi_t a2; gcry_mpi_t q; unsigned int i, j, k; int rc = 0; unsigned int nbits; x = gcry_mpi_new (0); y = gcry_mpi_new (0); z = gcry_mpi_new (0); nminus1 = gcry_mpi_new (0); a2 = gcry_mpi_set_ui (NULL, 2); nbits = gcry_mpi_get_nbits (n); gcry_mpi_sub_ui (nminus1, n, 1); /* Find q and k, so that n = 1 + 2^k * q . */ q = gcry_mpi_set (NULL, nminus1); k = mpz_trailing_zeroes (q); mpz_tdiv_q_2exp (q, q, k); for (i = 0; i < steps; i++) { if (!i) { gcry_mpi_set_ui (x, 2); } else { mpz_randomize (x, nbits - 1, hc); GNUNET_assert (gcry_mpi_cmp (x, nminus1) < 0); GNUNET_assert (gcry_mpi_cmp_ui (x, 1) > 0); } gcry_mpi_powm (y, x, q, n); if (gcry_mpi_cmp_ui (y, 1) && gcry_mpi_cmp (y, nminus1)) { for (j = 1; j < k && gcry_mpi_cmp (y, nminus1); j++) { gcry_mpi_powm (y, y, a2, n); if (!gcry_mpi_cmp_ui (y, 1)) goto leave; /* Not a prime. */ } if (gcry_mpi_cmp (y, nminus1)) goto leave; /* Not a prime. */ } } rc = 1; /* May be a prime. */ leave: gcry_mpi_release (x); gcry_mpi_release (y); gcry_mpi_release (z); gcry_mpi_release (nminus1); gcry_mpi_release (q); gcry_mpi_release (a2); return rc; }
/* This tests checks that the low-level EC API yields the same result as using the high level API. The values have been taken from a test run using the high level API. */ static void basic_ec_math (void) { gpg_error_t err; gcry_ctx_t ctx; gcry_mpi_t P, A; gcry_mpi_point_t G, Q; gcry_mpi_t d; gcry_mpi_t x, y, z; wherestr = "basic_ec_math"; show ("checking basic math functions for EC\n"); P = hex2mpi ("0xfffffffffffffffffffffffffffffffeffffffffffffffff"); A = hex2mpi ("0xfffffffffffffffffffffffffffffffefffffffffffffffc"); G = make_point ("188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012", "7192B95FFC8DA78631011ED6B24CDD573F977A11E794811", "1"); d = hex2mpi ("D4EF27E32F8AD8E2A1C6DDEBB1D235A69E3CEF9BCE90273D"); Q = gcry_mpi_point_new (0); err = ec_p_new (&ctx, P, A); if (err) die ("ec_p_new failed: %s\n", gpg_strerror (err)); x = gcry_mpi_new (0); y = gcry_mpi_new (0); z = gcry_mpi_new (0); { /* A quick check that multiply by zero works. */ gcry_mpi_t tmp; tmp = gcry_mpi_new (0); gcry_mpi_ec_mul (Q, tmp, G, ctx); gcry_mpi_release (tmp); gcry_mpi_point_get (x, y, z, Q); if (gcry_mpi_cmp_ui (x, 0) || gcry_mpi_cmp_ui (y, 0) || gcry_mpi_cmp_ui (z, 0)) fail ("multiply a point by zero failed\n"); } gcry_mpi_ec_mul (Q, d, G, ctx); gcry_mpi_point_get (x, y, z, Q); if (cmp_mpihex (x, "222D9EC717C89D047E0898C9185B033CD11C0A981EE6DC66") || cmp_mpihex (y, "605DE0A82D70D3E0F84A127D0739ED33D657DF0D054BFDE8") || cmp_mpihex (z, "00B06B519071BC536999AC8F2D3934B3C1FC9EACCD0A31F88F")) fail ("computed public key does not match\n"); if (debug) { print_mpi ("Q.x", x); print_mpi ("Q.y", y); print_mpi ("Q.z", z); } if (gcry_mpi_ec_get_affine (x, y, Q, ctx)) fail ("failed to get affine coordinates\n"); if (cmp_mpihex (x, "008532093BA023F4D55C0424FA3AF9367E05F309DC34CDC3FE") || cmp_mpihex (y, "00C13CA9E617C6C8487BFF6A726E3C4F277913D97117939966")) fail ("computed affine coordinates of public key do not match\n"); if (debug) { print_mpi ("q.x", x); print_mpi ("q.y", y); } gcry_mpi_release (z); gcry_mpi_release (y); gcry_mpi_release (x); gcry_mpi_point_release (Q); gcry_mpi_release (d); gcry_mpi_point_release (G); gcry_mpi_release (A); gcry_mpi_release (P); gcry_ctx_release (ctx); }
static void set_get_point (void) { gcry_mpi_point_t point; gcry_mpi_t x, y, z; wherestr = "set_get_point"; show ("checking point setting functions\n"); point = gcry_mpi_point_new (0); x = gcry_mpi_set_ui (NULL, 17); y = gcry_mpi_set_ui (NULL, 42); z = gcry_mpi_set_ui (NULL, 11371); gcry_mpi_point_get (x, y, z, point); if (gcry_mpi_cmp_ui (x, 0) || gcry_mpi_cmp_ui (y, 0) || gcry_mpi_cmp_ui (z, 0)) fail ("new point not initialized to (0,0,0)\n"); gcry_mpi_point_snatch_get (x, y, z, point); point = NULL; if (gcry_mpi_cmp_ui (x, 0) || gcry_mpi_cmp_ui (y, 0) || gcry_mpi_cmp_ui (z, 0)) fail ("snatch_get failed\n"); gcry_mpi_release (x); gcry_mpi_release (y); gcry_mpi_release (z); point = gcry_mpi_point_new (0); x = gcry_mpi_set_ui (NULL, 17); y = gcry_mpi_set_ui (NULL, 42); z = gcry_mpi_set_ui (NULL, 11371); gcry_mpi_point_set (point, x, y, z); gcry_mpi_set_ui (x, 23); gcry_mpi_set_ui (y, 24); gcry_mpi_set_ui (z, 25); gcry_mpi_point_get (x, y, z, point); if (gcry_mpi_cmp_ui (x, 17) || gcry_mpi_cmp_ui (y, 42) || gcry_mpi_cmp_ui (z, 11371)) fail ("point_set/point_get failed\n"); gcry_mpi_point_snatch_set (point, x, y, z); x = gcry_mpi_new (0); y = gcry_mpi_new (0); z = gcry_mpi_new (0); gcry_mpi_point_get (x, y, z, point); if (gcry_mpi_cmp_ui (x, 17) || gcry_mpi_cmp_ui (y, 42) || gcry_mpi_cmp_ui (z, 11371)) fail ("point_snatch_set/point_get failed\n"); gcry_mpi_point_release (point); gcry_mpi_release (x); gcry_mpi_release (y); gcry_mpi_release (z); }
/* Check the math used with Twisted Edwards curves. */ static void twistededwards_math (void) { gpg_error_t err; gcry_ctx_t ctx; gcry_mpi_point_t G, Q; gcry_mpi_t k; gcry_mpi_t w, a, x, y, z, p, n, b, I; wherestr = "twistededwards_math"; show ("checking basic Twisted Edwards math\n"); err = gcry_mpi_ec_new (&ctx, NULL, "Ed25519"); if (err) die ("gcry_mpi_ec_new failed: %s\n", gpg_strerror (err)); k = hex2mpi ("2D3501E723239632802454EE5DDC406EFB0BDF18486A5BDE9C0390A9C2984004" "F47252B628C953625B8DEB5DBCB8DA97AA43A1892D11FA83596F42E0D89CB1B6"); G = gcry_mpi_ec_get_point ("g", ctx, 1); if (!G) die ("gcry_mpi_ec_get_point(G) failed\n"); Q = gcry_mpi_point_new (0); w = gcry_mpi_new (0); a = gcry_mpi_new (0); x = gcry_mpi_new (0); y = gcry_mpi_new (0); z = gcry_mpi_new (0); I = gcry_mpi_new (0); p = gcry_mpi_ec_get_mpi ("p", ctx, 1); n = gcry_mpi_ec_get_mpi ("n", ctx, 1); b = gcry_mpi_ec_get_mpi ("b", ctx, 1); /* Check: 2^{p-1} mod p == 1 */ gcry_mpi_sub_ui (a, p, 1); gcry_mpi_powm (w, GCRYMPI_CONST_TWO, a, p); if (gcry_mpi_cmp_ui (w, 1)) fail ("failed assertion: 2^{p-1} mod p == 1\n"); /* Check: p % 4 == 1 */ gcry_mpi_mod (w, p, GCRYMPI_CONST_FOUR); if (gcry_mpi_cmp_ui (w, 1)) fail ("failed assertion: p % 4 == 1\n"); /* Check: 2^{n-1} mod n == 1 */ gcry_mpi_sub_ui (a, n, 1); gcry_mpi_powm (w, GCRYMPI_CONST_TWO, a, n); if (gcry_mpi_cmp_ui (w, 1)) fail ("failed assertion: 2^{n-1} mod n == 1\n"); /* Check: b^{(p-1)/2} mod p == p-1 */ gcry_mpi_sub_ui (a, p, 1); gcry_mpi_div (x, NULL, a, GCRYMPI_CONST_TWO, -1); gcry_mpi_powm (w, b, x, p); gcry_mpi_abs (w); if (gcry_mpi_cmp (w, a)) fail ("failed assertion: b^{(p-1)/2} mod p == p-1\n"); /* I := 2^{(p-1)/4} mod p */ gcry_mpi_sub_ui (a, p, 1); gcry_mpi_div (x, NULL, a, GCRYMPI_CONST_FOUR, -1); gcry_mpi_powm (I, GCRYMPI_CONST_TWO, x, p); /* Check: I^2 mod p == p-1 */ gcry_mpi_powm (w, I, GCRYMPI_CONST_TWO, p); if (gcry_mpi_cmp (w, a)) fail ("failed assertion: I^2 mod p == p-1\n"); /* Check: G is on the curve */ if (!gcry_mpi_ec_curve_point (G, ctx)) fail ("failed assertion: G is on the curve\n"); /* Check: nG == (0,1) */ gcry_mpi_ec_mul (Q, n, G, ctx); if (gcry_mpi_ec_get_affine (x, y, Q, ctx)) fail ("failed to get affine coordinates\n"); if (gcry_mpi_cmp_ui (x, 0) || gcry_mpi_cmp_ui (y, 1)) fail ("failed assertion: nG == (0,1)\n"); /* Now two arbitrary point operations taken from the ed25519.py sample data. */ gcry_mpi_release (a); a = hex2mpi ("4f71d012df3c371af3ea4dc38385ca5bb7272f90cb1b008b3ed601c76de1d496" "e30cbf625f0a756a678d8f256d5325595cccc83466f36db18f0178eb9925edd3"); gcry_mpi_ec_mul (Q, a, G, ctx); if (gcry_mpi_ec_get_affine (x, y, Q, ctx)) fail ("failed to get affine coordinates\n"); if (cmp_mpihex (x, ("157f7361c577aad36f67ed33e38dc7be" "00014fecc2165ca5cee9eee19fe4d2c1")) || cmp_mpihex (y, ("5a69dbeb232276b38f3f5016547bb2a2" "4025645f0b820e72b8cad4f0a909a092"))) { fail ("sample point multiply failed:\n"); print_mpi ("r", a); print_mpi ("Rx", x); print_mpi ("Ry", y); } gcry_mpi_release (a); a = hex2mpi ("2d3501e723239632802454ee5ddc406efb0bdf18486a5bde9c0390a9c2984004" "f47252b628c953625b8deb5dbcb8da97aa43a1892d11fa83596f42e0d89cb1b6"); gcry_mpi_ec_mul (Q, a, G, ctx); if (gcry_mpi_ec_get_affine (x, y, Q, ctx)) fail ("failed to get affine coordinates\n"); if (cmp_mpihex (x, ("6218e309d40065fcc338b3127f468371" "82324bd01ce6f3cf81ab44e62959c82a")) || cmp_mpihex (y, ("5501492265e073d874d9e5b81e7f8784" "8a826e80cce2869072ac60c3004356e5"))) { fail ("sample point multiply failed:\n"); print_mpi ("r", a); print_mpi ("Rx", x); print_mpi ("Ry", y); } gcry_mpi_release (I); gcry_mpi_release (b); gcry_mpi_release (n); gcry_mpi_release (p); gcry_mpi_release (w); gcry_mpi_release (a); gcry_mpi_release (x); gcry_mpi_release (y); gcry_mpi_release (z); gcry_mpi_point_release (Q); gcry_mpi_point_release (G); gcry_mpi_release (k); gcry_ctx_release (ctx); }
static void test_cmp (void) { gpg_error_t rc; gcry_mpi_t zero, zero2; gcry_mpi_t one; gcry_mpi_t two; gcry_mpi_t all_ones; gcry_mpi_t opa1, opa2; gcry_mpi_t opa1s, opa2s; gcry_mpi_t opa0, opa02; zero = gcry_mpi_new (0); zero2= gcry_mpi_set_ui (NULL, 0); one = gcry_mpi_set_ui (NULL, 1); two = gcry_mpi_set_ui (NULL, 2); rc = gcry_mpi_scan (&all_ones, GCRYMPI_FMT_USG, ones, sizeof(ones), NULL); if (rc) die ("scanning number failed at line %d", __LINE__); opa0 = gcry_mpi_set_opaque (NULL, gcry_xstrdup ("a"), 0); opa02 = gcry_mpi_set_opaque (NULL, gcry_xstrdup ("b"), 0); opa1 = gcry_mpi_set_opaque (NULL, gcry_xstrdup ("aaaaaaaaaaaaaaaa"), 16*8); opa1s = gcry_mpi_set_opaque (NULL, gcry_xstrdup ("a"), 1*8); opa2 = gcry_mpi_set_opaque (NULL, gcry_xstrdup ("bbbbbbbbbbbbbbbb"), 16*8); opa2s = gcry_mpi_set_opaque (NULL, gcry_xstrdup ("b"), 1*8); /* Single limb test with cmp_ui */ if (gcry_mpi_cmp_ui (zero, 0)) fail ("mpi_cmp_ui failed at line %d", __LINE__); if (!(gcry_mpi_cmp_ui (zero, 1) < 0)) fail ("mpi_cmp_ui failed at line %d", __LINE__); if (!(gcry_mpi_cmp_ui (zero, (-1)) < 0)) fail ("mpi_cmp_ui failed at line %d", __LINE__); if (gcry_mpi_cmp_ui (two, 2)) fail ("mpi_cmp_ui failed at line %d", __LINE__); if (!(gcry_mpi_cmp_ui (two, 3) < 0)) fail ("mpi_cmp_ui failed at line %d", __LINE__); if (!(gcry_mpi_cmp_ui (two, 1) > 0)) fail ("mpi_cmp_ui failed at line %d", __LINE__); /* Multi limb tests with cmp_ui. */ if (!(gcry_mpi_cmp_ui (all_ones, 0) > 0)) fail ("mpi_cmp_ui failed at line %d", __LINE__); if (!(gcry_mpi_cmp_ui (all_ones, (-1)) > 0)) fail ("mpi_cmp_ui failed at line %d", __LINE__); /* Single limb test with cmp */ if (gcry_mpi_cmp (zero, zero2)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (zero, one) < 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (one, zero) > 0)) fail ("mpi_cmp failed at line %d", __LINE__); gcry_mpi_neg (one, one); if (!(gcry_mpi_cmp (zero, one) > 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (one, zero) < 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (one, two) < 0)) fail ("mpi_cmp failed at line %d", __LINE__); gcry_mpi_neg (one, one); if (!(gcry_mpi_cmp (one, two) < 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (two, one) > 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (one, all_ones) < 0)) fail ("mpi_cmp failed at line %d", __LINE__); /* Tests with opaque values. */ if (!(gcry_mpi_cmp (opa1, one) < 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (one, opa1) > 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (opa0, opa02) == 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (opa1s, opa1) < 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (opa2, opa1s) > 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (opa1, opa2) < 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (opa2, opa1) > 0)) fail ("mpi_cmp failed at line %d", __LINE__); if (!(gcry_mpi_cmp (opa1, opa1) == 0)) fail ("mpi_cmp failed at line %d", __LINE__); gcry_mpi_release(opa2s); gcry_mpi_release(opa2); gcry_mpi_release(opa1s); gcry_mpi_release(opa1); gcry_mpi_release(opa02); gcry_mpi_release(opa0); gcry_mpi_release(all_ones); gcry_mpi_release(two); gcry_mpi_release(one); gcry_mpi_release(zero2); gcry_mpi_release(zero); }
/* What we test here is that we don't overwrite our args and that using thne same mpi for several args works. */ static int test_powm (void) { int b_int = 17; int e_int = 3; int m_int = 19; gcry_mpi_t base = gcry_mpi_set_ui (NULL, b_int); gcry_mpi_t exp = gcry_mpi_set_ui (NULL, e_int); gcry_mpi_t mod = gcry_mpi_set_ui (NULL, m_int); gcry_mpi_t res = gcry_mpi_new (0); gcry_mpi_powm (res, base, exp, mod); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); if (gcry_mpi_cmp_ui (exp, e_int)) die ("test_powm_ui failed for exp at %d\n", __LINE__); if (gcry_mpi_cmp_ui (mod, m_int)) die ("test_powm failed for mod at %d\n", __LINE__); /* Check using base for the result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui (exp, e_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (base, base, exp, mod); if (gcry_mpi_cmp (res, base)) die ("test_powm failed at %d\n", __LINE__); if (gcry_mpi_cmp_ui (exp, e_int)) die ("test_powm_ui failed for exp at %d\n", __LINE__); if (gcry_mpi_cmp_ui (mod, m_int)) die ("test_powm failed for mod at %d\n", __LINE__); /* Check using exp for the result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui (exp, e_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (exp, base, exp, mod); if (gcry_mpi_cmp (res, exp)) die ("test_powm failed at %d\n", __LINE__); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); if (gcry_mpi_cmp_ui (mod, m_int)) die ("test_powm failed for mod at %d\n", __LINE__); /* Check using mod for the result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui (exp, e_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (mod, base, exp, mod); if (gcry_mpi_cmp (res, mod)) die ("test_powm failed at %d\n", __LINE__); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); if (gcry_mpi_cmp_ui (exp, e_int)) die ("test_powm_ui failed for exp at %d\n", __LINE__); /* Now check base ^ base mod mod. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (res, base, base, mod); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); if (gcry_mpi_cmp_ui (mod, m_int)) die ("test_powm failed for mod at %d\n", __LINE__); /* Check base ^ base mod mod with base as result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (base, base, base, mod); if (gcry_mpi_cmp (res, base)) die ("test_powm failed at %d\n", __LINE__); if (gcry_mpi_cmp_ui (mod, m_int)) die ("test_powm failed for mod at %d\n", __LINE__); /* Check base ^ base mod mod with mod as result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (mod, base, base, mod); if (gcry_mpi_cmp (res, mod)) die ("test_powm failed at %d\n", __LINE__); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); /* Now check base ^ base mod base. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_powm (res, base, base, base); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); /* Check base ^ base mod base with base as result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_powm (base, base, base, base); if (gcry_mpi_cmp (res, base)) die ("test_powm failed at %d\n", __LINE__); /* Check for a case: base is negative and expo is even. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_neg (base, base); gcry_mpi_set_ui (exp, e_int * 2); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (res, base, exp, mod); /* Result should be positive and it's 7 = (-17)^6 mod 19. */ if (gcry_mpi_is_neg (res) || gcry_mpi_cmp_ui (res, 7)) { if (verbose) { fprintf (stderr, "is_neg: %d\n", gcry_mpi_is_neg (res)); fprintf (stderr, "mpi: "); gcry_mpi_dump (res); putc ('\n', stderr); } die ("test_powm failed for negative base at %d\n", __LINE__); } gcry_mpi_release (base); gcry_mpi_release (exp); gcry_mpi_release (mod); gcry_mpi_release (res); /* Fixme: We should add the rest of the cases of course. */ return 1; }
/* What we test here is that we don't overwrite our args and that using thne same mpi for several args works. */ static int test_powm (void) { int b_int = 17; int e_int = 3; int m_int = 19; gcry_mpi_t base = gcry_mpi_set_ui (NULL, b_int); gcry_mpi_t exp = gcry_mpi_set_ui (NULL, e_int); gcry_mpi_t mod = gcry_mpi_set_ui (NULL, m_int); gcry_mpi_t res = gcry_mpi_new (0); gcry_mpi_powm (res, base, exp, mod); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); if (gcry_mpi_cmp_ui (exp, e_int)) die ("test_powm_ui failed for exp at %d\n", __LINE__); if (gcry_mpi_cmp_ui (mod, m_int)) die ("test_powm failed for mod at %d\n", __LINE__); /* Check using base for the result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui (exp, e_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (base, base, exp, mod); if (gcry_mpi_cmp (res, base)) die ("test_powm failed at %d\n", __LINE__); if (gcry_mpi_cmp_ui (exp, e_int)) die ("test_powm_ui failed for exp at %d\n", __LINE__); if (gcry_mpi_cmp_ui (mod, m_int)) die ("test_powm failed for mod at %d\n", __LINE__); /* Check using exp for the result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui (exp, e_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (exp, base, exp, mod); if (gcry_mpi_cmp (res, exp)) die ("test_powm failed at %d\n", __LINE__); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); if (gcry_mpi_cmp_ui (mod, m_int)) die ("test_powm failed for mod at %d\n", __LINE__); /* Check using mod for the result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui (exp, e_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (mod, base, exp, mod); if (gcry_mpi_cmp (res, mod)) die ("test_powm failed at %d\n", __LINE__); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); if (gcry_mpi_cmp_ui (exp, e_int)) die ("test_powm_ui failed for exp at %d\n", __LINE__); /* Now check base ^ base mod mod. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (res, base, base, mod); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); if (gcry_mpi_cmp_ui (mod, m_int)) die ("test_powm failed for mod at %d\n", __LINE__); /* Check base ^ base mod mod with base as result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (base, base, base, mod); if (gcry_mpi_cmp (res, base)) die ("test_powm failed at %d\n", __LINE__); if (gcry_mpi_cmp_ui (mod, m_int)) die ("test_powm failed for mod at %d\n", __LINE__); /* Check base ^ base mod mod with mod as result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_set_ui(mod, m_int); gcry_mpi_powm (mod, base, base, mod); if (gcry_mpi_cmp (res, mod)) die ("test_powm failed at %d\n", __LINE__); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); /* Now check base ^ base mod base. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_powm (res, base, base, base); if (gcry_mpi_cmp_ui (base, b_int)) die ("test_powm failed for base at %d\n", __LINE__); /* Check base ^ base mod base with base as result. */ gcry_mpi_set_ui (base, b_int); gcry_mpi_powm (base, base, base, base); if (gcry_mpi_cmp (res, base)) die ("test_powm failed at %d\n", __LINE__); /* Fixme: We should add the rest of the cases of course. */ return 1; }