RSA::RSA() { mpz_init2(m_e, 1024); mpz_init2(m_p, 1024); mpz_init2(m_q, 1024); mpz_init2(m_d, 1024); mpz_init2(m_u, 1024); mpz_init2(m_dp, 1024); mpz_init2(m_dq, 1024); mpz_init2(m_mod, 1024); mpz_init2(m_mod2, 1024); }
guint32 srp_generate_salt_and_v(srp_t *srp, gchar *out) { mpz_t s; /* salt */ mpz_t v; mpz_t x; size_t count; if (!srp) return 0; mpz_init2(s, 256); mpz_urandomb(s, srp->rand, 256); mpz_export(out, &count, -1, 1, 0, 0, s); /*memset(buf, 0, 32);*/ srp_get_x(srp, x, out); srp_get_v_mpz(srp, v, x); mpz_export(out + 32, &count, -1, 1, 0, 0, v); mpz_clear(x); mpz_clear(v); mpz_clear(s); return 64; }
MPFR_HOT_FUNCTION_ATTR void mpfr_mpz_init2 (mpz_t z, mp_bitcnt_t n) { /* The condition on n is used below as the argument n will be ignored if the mpz_t is obtained from the MPFR stack of previously used mpz_t. Said otherwise, it z is expected to have a large size at the end, then it is better to allocate this size directly than to get a mpz_t of small size, with possibly several realloc's on it. But if n satisfies the condition and is larger than the stacked mpz_t, this may still yield useless realloc's. This is not ideal. We might consider to use mpz_init2 with the maximum size in mpfr_mpz_init to solve this issue. */ if (MPFR_LIKELY (n_alloc > 0 && n <= MPFR_POOL_MAX_SIZE * GMP_NUMB_BITS)) { /* Get a mpz_t from the MPFR stack of previously used mpz_t. It reduces memory pressure, and it allows to reuse a mpz_t that should be sufficiently big. */ MPFR_ASSERTD (n_alloc <= numberof (mpz_tab)); memcpy (z, &mpz_tab[--n_alloc], sizeof (mpz_t)); SIZ(z) = 0; } else { /* Call the real GMP function */ mpz_init2 (z, n); } }
void SRPGlobalInit() { mpz_init2(N, 256); mpz_import(N, 32, -1, sizeof(modulus[0]), 0, 0, modulus); gmp_randinit_default(gmprand); gmp_randseed_ui(gmprand, RandomGenSecure()); }
MEXP(unsigned long) nls_account_create(nls_t* nls, char* buf, unsigned long bufSize) { mpz_t s; /* salt */ mpz_t v; mpz_t x; if (!nls) return 0; if (bufSize < nls->username_len + 65) return 0; mpz_init2(s, 256); mpz_urandomb(s, nls->rand, 256); mpz_export(buf, (size_t*) 0, -1, 1, 0, 0, s); /*memset(buf, 0, 32);*/ nls_get_x(nls, x, buf); nls_get_v_mpz(nls, v, x); mpz_export(buf + 32, (size_t*) 0, -1, 1, 0, 0, v); mpz_clear(x); mpz_clear(v); mpz_clear(s); strcpy(buf + 64, nls->username); return nls->username_len + 65; }
MEXP(nls_t*) nls_account_change_proof(nls_t* nls, char* buf, const char* new_password, const char* B, const char* salt) { nls_t* nouveau; mpz_t s; /* salt */ if (!nls) return (nls_t*) 0; /* create new nls_t */ nouveau = nls_init_l(nls->username, nls->username_len, new_password, (unsigned long) strlen(new_password)); if (!nouveau) return (nls_t*) 0; /* fill buf */ nls_get_M1(nls, buf, B, salt); mpz_init2(s, 256); mpz_urandomb(s, nouveau->rand, 256); mpz_export(buf + 20, (size_t*) 0, -1, 1, 0, 0, s); nls_get_v(nouveau, buf + 52, buf + 20); mpz_clear(s); return nouveau; }
MEXP(void) nls_get_A(nls_t* nls, char* out) { mpz_t g; mpz_t A; size_t o; if (!nls) return; if (nls->A) { memcpy(out, nls->A, 32); return; } mpz_init_set_ui(g, NLS_VAR_g); mpz_init2(A, 256); mpz_powm(A, g, nls->a, nls->n); mpz_export(out, &o, -1, 1, 0, 0, A); mpz_clear(A); mpz_clear(g); nls->A = (char*) malloc(32); if (nls->A) memcpy(nls->A, out, 32); }
void nls_get_x(nls_t* nls, mpz_t x_c, const char* raw_salt) { char* userpass; uint8_t hash[20], final_hash[20]; SHA1Context shac; // build the string Username:Password userpass = (char*) malloc(nls->username_len + nls->password_len + 2); memcpy(userpass, nls->username, nls->username_len); userpass[nls->username_len] = ':'; memcpy(userpass + nls->username_len + 1, nls->password, nls->password_len); userpass[nls->username_len + nls->password_len + 1] = 0; // null-terminator // get the SHA-1 hash of the string SHA1Reset(&shac); SHA1Input(&shac, (uint8_t*) userpass, (nls->username_len + nls->password_len + 1)); SHA1Result(&shac, hash); free(userpass); // get the SHA-1 hash of the salt and user:pass hash SHA1Reset(&shac); SHA1Input(&shac, (uint8_t*) raw_salt, 32); SHA1Input(&shac, hash, 20); SHA1Result(&shac, final_hash); SHA1Reset(&shac); // create an arbitrary-length integer from the hash and return it mpz_init2(x_c, 160); mpz_import(x_c, 20, -1, 1, 0, 0, (char*) final_hash); }
void save_smooth_number(smooth_number_t n) { if (nb_global_smooth_numbers_found > nb_qr_primes + NB_VECTORS_OFFSET - 1) /* if we have sufficient smooth numbers, skip saving */ return; mpz_clear(tmp_matrix_row); /* tmp_matrix_row must be initialized already */ mpz_init2(tmp_matrix_row, nb_qr_primes); /* init a vector of *exactly* nb_qr_primes bits */ smooth_number_t tmp; mpz_init(tmp.value_x); mpz_init(tmp.value_x_squared); mpz_init(tmp.factors_vect); mpz_set(tmp.value_x, n.value_x); mpz_pow_ui(tmp.value_x_squared, n.value_x, 2); mpz_sub(tmp.value_x_squared, tmp.value_x_squared, N); mpz_set(tmp.factors_vect, n.factors_vect); if (my_rank == 0) /* master appends directly to the matrix */ { smooth_numbers[nb_global_smooth_numbers_found++] = tmp; push_row(&matrix, n.factors_vect); } else /* append smooth number to the temporary list, later they will be sent to master */ { temp_slaves_smooth_numbers[nb_temp_smooth_numbers++] = tmp; } }
static struct error_record *ct_label_type_parse(const struct expr *sym, struct expr **res) { const struct symbolic_constant *s; const struct datatype *dtype; uint8_t data[CT_LABEL_BIT_SIZE]; mpz_t value; for (s = ct_label_tbl->symbols; s->identifier != NULL; s++) { if (!strcmp(sym->identifier, s->identifier)) break; } dtype = sym->dtype; if (s->identifier == NULL) return error(&sym->location, "Could not parse %s", dtype->desc); if (s->value >= CT_LABEL_BIT_SIZE) return error(&sym->location, "%s: out of range (%u max)", s->identifier, s->value, CT_LABEL_BIT_SIZE); mpz_init2(value, dtype->size); mpz_setbit(value, s->value); mpz_export_data(data, value, BYTEORDER_HOST_ENDIAN, sizeof(data)); *res = constant_expr_alloc(&sym->location, dtype, dtype->byteorder, sizeof(data), data); mpz_clear(value); return NULL; }
void fmpz_poly_bit_unpack_unsigned(fmpz_poly_t poly, const fmpz_t f, mp_bitcnt_t bit_size) { slong len; mpz_t tmp; if (fmpz_sgn(f) < 0) { flint_printf("Exception (fmpz_poly_bit_unpack_unsigned). Expected an unsigned value.\n"); abort(); } if (bit_size == 0 || fmpz_is_zero(f)) { fmpz_poly_zero(poly); return; } len = (fmpz_bits(f) + bit_size - 1) / bit_size; mpz_init2(tmp, bit_size*len); flint_mpn_zero(tmp->_mp_d, tmp->_mp_alloc); fmpz_get_mpz(tmp, f); fmpz_poly_fit_length(poly, len); _fmpz_poly_bit_unpack_unsigned(poly->coeffs, len, tmp->_mp_d, bit_size); _fmpz_poly_set_length(poly, len); _fmpz_poly_normalise(poly); mpz_clear(tmp); }
static void srp_get_x(srp_t *srp, mpz_t x_c, const gchar *raw_salt) { gchar *userpass; guint8 hash[SHA1_HASH_SIZE], final_hash[SHA1_HASH_SIZE]; sha1_context ctx; ctx.version = SHA1_TYPE_NORMAL; // build the string Username:Password userpass = (gchar *) g_malloc(srp->username_len + srp->password_len + 2); memcpy(userpass, srp->username_upper, srp->username_len); userpass[srp->username_len] = ':'; memcpy(userpass + srp->username_len + 1, srp->password_upper, srp->password_len); userpass[srp->username_len + srp->password_len + 1] = 0; // null-terminator // get the SHA-1 hash of the string sha1_reset(&ctx); sha1_input(&ctx, (guint8 *) userpass, (srp->username_len + srp->password_len + 1)); sha1_digest(&ctx, hash); g_free(userpass); // get the SHA-1 hash of the salt and user:pass hash sha1_reset(&ctx); sha1_input(&ctx, (guint8 *) raw_salt, 32); sha1_input(&ctx, hash, 20); sha1_digest(&ctx, final_hash); // create an arbitrary-length integer from the hash and return it mpz_init2(x_c, 160); mpz_import(x_c, 20, -1, 1, 0, 0, (gchar *) final_hash); }
void srp_get_A(srp_t *srp, gchar *out) { mpz_t g; mpz_t A; size_t o; if (!srp) return; if (srp->A) { g_memmove(out, srp->A, 32); return; } mpz_init_set_ui(g, SRP_VAR_g); mpz_init2(A, 256); mpz_powm(A, g, srp->a, srp->n); mpz_export(out, &o, -1, 1, 0, 0, A); mpz_clear(A); mpz_clear(g); srp->A = (gchar *) g_malloc(32); if (srp->A) memcpy(srp->A, out, 32); }
MEXP(nls_t*) nls_init_l(const char* username, unsigned long username_length, const char* password, unsigned long password_length) { unsigned int i; char* d; /* destination */ const char* o; /* original */ nls_t* nls; nls = (nls_t*) malloc(sizeof(nls_t)); if (!nls) return (nls_t*) 0; nls->username_len = username_length; nls->password_len = password_length; nls->username = (char*) malloc(nls->username_len + 1); nls->password = (char*) malloc(nls->password_len + 1); if (!nls->username || !nls->password) { free(nls); return (nls_t*) 0; } d = (char*) nls->username; o = username; for (i = 0; i < nls->username_len; i++) { *d = (char) toupper(*o); d++; o++; } *((char*) nls->username + username_length) = 0; *((char*) nls->password + password_length) = 0; d = (char*) nls->password; o = password; for (i = 0; i < nls->password_len; i++) { *d = (char) toupper(*o); d++; o++; } mpz_init_set_str(nls->n, NLS_VAR_N_STR, 16); gmp_randinit_default(nls->rand); gmp_randseed_ui(nls->rand, nls_pre_seed()); mpz_init2(nls->a, 256); mpz_urandomm(nls->a, nls->rand, nls->n); /* generates the private key */ /* The following line replaces preceding 2 lines during testing. */ /*mpz_init_set_str(nls->a, "1234", 10);*/ nls->A = (char*) 0; nls->S = (char*) 0; nls->K = (char*) 0; nls->M1 = (char*) 0; nls->M2 = (char*) 0; return nls; }
template<> Obj GET_INTOBJ(Z_NR<double> &v) { mpz_t z; mpz_init2 (z, 8*sizeof(double)+1); mpz_set_d(z,v.getData()); Obj o = INT_mpz(z); mpz_clear(z); return o; }
template<> Obj GET_INTOBJ(Z_NR<long> &v) { mpz_t z; mpz_init2 (z, 8*sizeof(long)+1); mpz_set_si(z,v.getData()); Obj o = INT_mpz(z); mpz_clear(z); return o; }
void RSA::encrypt(char *msg, long size) { mpz_init2(m, 1024); mpz_init2(c, 1024); mpz_import(m, 128, 1, 1, 0, 0, msg); mpz_powm(c, m, m_e, m_mod); size_t count = (mpz_sizeinbase(c, 2) + 7)/8; memset(msg, 0, 128 - count); size_t countp; mpz_export(&msg[128 - count], &countp, 1, 1, 0, 0, c); mpz_clear(m); mpz_clear(c); }
int rsa_import_public_key(rsa_public_key_t *key, int endian, uint8_t *n, size_t n_len, uint8_t *e, size_t e_len) { if (n == NULL || n_len == 0) return -1; /* init key */ key->size = n_len << 3; if (e == NULL || e_len == 0) { mpz_init_set_ui(key->e, 65537); } else { mpz_init2(key->e, (e_len << 3) + GMP_NUMB_BITS); mpz_import(key->e, e_len, endian, 1, 0, 0, e); } mpz_init2(key->n, key->size + GMP_NUMB_BITS); /* import values */ mpz_import(key->n, n_len, endian, 1, 0, 0, n); return 0; }
void RSA::encrypt(char *msg, uint32 size) { mpz_t c,m; mpz_init2(m, 1024); mpz_init2(c, 1024); mpz_import(m, size, 1, 1, 0, 0, msg); mpz_powm(c, m, m_e, m_mod2); size_t count = (mpz_sizeinbase(c, 2) + 7)/8; memset(msg, 0, size - count); size_t countp; mpz_export(&msg[size - count], &countp, 1, 1, 0, 0, c); mpz_clear(m); mpz_clear(c); }
void RSA::decrypt(char* msg) const { mpz_t c, m; mpz_init2(c, 1024); mpz_init2(m, 1024); mpz_import(c, 128, 1, 1, 0, 0, msg); // m = c^d mod n mpz_powm(m, c, d, n); size_t count = (mpz_sizeinbase(m, 2) + 7) / 8; memset(msg, 0, 128 - count); mpz_export(msg + (128 - count), nullptr, 1, 1, 0, 0, m); mpz_clear(c); mpz_clear(m); }
int main() { mpz_t num; mpz_init2(num, 1000); /* initialize with 1000 bits */ mpz_ui_pow_ui(num, 2, 1000); printf("Answer: %lu\n", gmp_digital_sum(num)); mpz_clear(num); return 0; }
bool RSA::encrypt(char* msg, long size) { mpz_t plain,c; mpz_init2(plain, 1024); mpz_init2(c, 1024); mpz_import(plain, 128, 1, 1, 0, 0, msg); mpz_powm(c, plain, m_e, m_mod); size_t count = (mpz_sizeinbase(c, 2) + 7)/8; memset(msg, 0, 128 - count); mpz_export(&msg[128 - count], NULL, 1, 1, 0, 0, c); mpz_clear(c); mpz_clear(plain); return true; }
static void randseed_mt (gmp_randstate_t rstate, mpz_srcptr seed) { int i; size_t cnt; gmp_rand_mt_struct *p; mpz_t mod; /* Modulus. */ mpz_t seed1; /* Intermediate result. */ p = (gmp_rand_mt_struct *) RNG_STATE (rstate); mpz_init2 (mod, 19938L); mpz_init2 (seed1, 19937L); mpz_setbit (mod, 19937L); mpz_sub_ui (mod, mod, 20027L); mpz_mod (seed1, seed, mod); /* Reduce `seed' modulo `mod'. */ mpz_clear (mod); mpz_add_ui (seed1, seed1, 2L); /* seed1 is now ready. */ mangle_seed (seed1); /* Perform the mangling by powering. */ /* Copy the last bit into bit 31 of mt[0] and clear it. */ p->mt[0] = (mpz_tstbit (seed1, 19936L) != 0) ? 0x80000000 : 0; mpz_clrbit (seed1, 19936L); /* Split seed1 into N-1 32-bit chunks. */ mpz_export (&p->mt[1], &cnt, -1, sizeof (p->mt[1]), 0, 8 * sizeof (p->mt[1]) - 32, seed1); mpz_clear (seed1); cnt++; ASSERT (cnt <= N); while (cnt < N) p->mt[cnt++] = 0; /* Warm the generator up if necessary. */ if (WARM_UP != 0) for (i = 0; i < WARM_UP / N; i++) __gmp_mt_recalc_buffer (p->mt); p->mti = WARM_UP % N; }
int rsa_import_key(rsa_private_key_t *key, int endian, uint8_t *n, size_t n_len, uint8_t *e, size_t e_len, uint8_t *p, uint8_t *q) { mpz_t t1, t2, phi; if (n == NULL || n_len == 0 || (p == NULL && q == NULL)) return -1; /* init key */ key->size = n_len << 3; if (e == NULL || e_len == 0) { mpz_init_set_ui(key->e, 65537); } else { mpz_init2(key->e, (e_len << 3) + GMP_NUMB_BITS); mpz_import(key->e, e_len, endian, 1, 0, 0, e); } mpz_init2(key->n, key->size + GMP_NUMB_BITS); mpz_init2(key->p, key->size / 2 + GMP_NUMB_BITS); mpz_init2(key->q, key->size / 2 + GMP_NUMB_BITS); mpz_init2(key->d, key->size + GMP_NUMB_BITS); mpz_init2(key->u, key->size / 2 + GMP_NUMB_BITS); mpz_init2(t1, key->size / 2 + GMP_NUMB_BITS); mpz_init2(t2, key->size / 2 + GMP_NUMB_BITS); mpz_init2(phi, key->size + GMP_NUMB_BITS); /* import values */ mpz_import(key->n, n_len, endian, 1, 0, 0, n); if (p != NULL) mpz_import(key->p, n_len / 2, endian, 1, 0, 0, p); if (q != NULL) mpz_import(key->q, n_len / 2, endian, 1, 0, 0, q); if (p == NULL) mpz_tdiv_q(key->p, key->n, key->q); if (q == NULL) mpz_tdiv_q(key->q, key->n, key->p); /* p shall be smaller than q */ if (mpz_cmp(key->p, key->q) > 0) mpz_swap(key->p, key->q); /* calculate missing values */ mpz_sub_ui(t1, key->p, 1); mpz_sub_ui(t2, key->q, 1); mpz_mul(phi, t1, t2); mpz_invert(key->d, key->e, phi); mpz_invert(key->u, key->p, key->q); /* release helper variables */ mpz_clear(t1); mpz_clear(t2); mpz_clear(phi); /* test key */ if (rsa_test_key(key) != 0) { rsa_release_private_key(key); return -1; } return 0; }
void RSA::decrypt(char* msg) { boost::recursive_mutex::scoped_lock lockClass(rsaLock); mpz_t c, m; mpz_init2(c, 1024); mpz_init2(m, 1024); mpz_import(c, 128, 1, 1, 0, 0, msg); // m = c^d mod n mpz_powm(m, c, m_d, m_n); size_t count = (mpz_sizeinbase(m, 2) + 7)/8; memset(msg, 0, 128 - count); mpz_export(&msg[128 - count], nullptr, 1, 1, 0, 0, m); mpz_clear(c); mpz_clear(m); }
void SRPCalcB(LPNLS nls, const char *V) { mpz_t g; mpz_init(nls->b); mpz_init2(nls->v, 256); mpz_init2(nls->b_src, 256); mpz_init_set_ui(g, 0x2F); mpz_import(nls->v, 32, -1, 1, 0, 0, V); memcpy(nls->V, V, sizeof(nls->V)); mpz_urandomm(nls->b_src, gmprand, N); mpz_powm(nls->b, g, nls->b_src, N); mpz_add(nls->b, nls->b, nls->v); mpz_mod(nls->b, nls->b, N); mpz_export(nls->B, (size_t *)0, -1, 1, 0, 0, nls->b); mpz_clear(g); }
static int rsa_test_key(rsa_private_key_t *key) { mpz_t a, b, t; int res = 0; mpz_init2(a, key->size + GMP_NUMB_BITS); mpz_init2(b, key->size + GMP_NUMB_BITS); mpz_init2(t, key->size + GMP_NUMB_BITS); mpz_set_ui(t, 0xdeadbeef); mpz_powm(a, t, key->e, key->n); mpz_powm(b, a, key->d, key->n); if (mpz_cmp(t, b) != 0) res = -1; mpz_powm(a, t, key->d, key->n); mpz_powm(b, a, key->e, key->n); if (mpz_cmp(t, b) != 0) res = -1; mpz_clear(a); mpz_clear(b); mpz_clear(t); return res; }
void RSA::setKey(const char* p, const char* q) { boost::recursive_mutex::scoped_lock lockClass(rsaLock); mpz_t m_p, m_q, m_e; mpz_init2(m_p, 1024); mpz_init2(m_q, 1024); mpz_init(m_e); mpz_set_str(m_p, p, 10); mpz_set_str(m_q, q, 10); // e = 65537 mpz_set_ui(m_e, 65537); // n = p * q mpz_mul(m_n, m_p, m_q); // d = e^-1 mod (p - 1)(q - 1) mpz_t p_1, q_1, pq_1; mpz_init2(p_1, 1024); mpz_init2(q_1, 1024); mpz_init2(pq_1, 1024); mpz_sub_ui(p_1, m_p, 1); mpz_sub_ui(q_1, m_q, 1); // pq_1 = (p -1)(q - 1) mpz_mul(pq_1, p_1, q_1); // m_d = m_e^-1 mod (p - 1)(q - 1) mpz_invert(m_d, m_e, pq_1); mpz_clear(p_1); mpz_clear(q_1); mpz_clear(pq_1); mpz_clear(m_p); mpz_clear(m_q); mpz_clear(m_e); }
void RSA::setDecryptKey(const char* p, const char* q, const char* d) { mpz_set_str(m_p, p, 10); mpz_set_str(m_q, q, 10); mpz_set_str(m_d, d, 10); mpz_t pm1,qm1; mpz_init2(pm1,520); mpz_init2(qm1,520); mpz_sub_ui(pm1, m_p, 1); mpz_sub_ui(qm1, m_q, 1); mpz_invert(m_u, m_p, m_q); mpz_mod(m_dp, m_d, pm1); mpz_mod(m_dq, m_d, qm1); mpz_mul(m_mod, m_p, m_q); mpz_clear(pm1); mpz_clear(qm1); }
template<> Obj GET_INTOBJ(Z_NR<mpz_t> &v) { #ifdef FPLLL_VERSION mpz_t z; mpz_init2 (z, 8*sizeof(long)+1); v.get_mpz(z); Obj o = INT_mpz(z); mpz_clear(z); return o; #else return INT_mpz(v.getData()); #endif }