Home | History | Annotate | Download | only in dh

Lines Matching defs:dh

57 #include <openssl/dh.h>
75 DH *DH_new(void) {
76 DH *dh = OPENSSL_malloc(sizeof(DH));
77 if (dh == NULL) {
78 OPENSSL_PUT_ERROR(DH, ERR_R_MALLOC_FAILURE);
82 OPENSSL_memset(dh, 0, sizeof(DH));
84 CRYPTO_MUTEX_init(&dh->method_mont_p_lock);
86 dh->references = 1;
87 CRYPTO_new_ex_data(&dh->ex_data);
89 return dh;
92 void DH_free(DH *dh) {
93 if (dh == NULL) {
97 if (!CRYPTO_refcount_dec_and_test_zero(&dh->references)) {
101 CRYPTO_free_ex_data(&g_ex_data_class, dh, &dh->ex_data);
103 BN_MONT_CTX_free(dh->method_mont_p);
104 BN_clear_free(dh->p);
105 BN_clear_free(dh->g);
106 BN_clear_free(dh->q);
107 BN_clear_free(dh->j);
108 OPENSSL_free(dh->seed);
109 BN_clear_free(dh->counter);
110 BN_clear_free(dh->pub_key);
111 BN_clear_free(dh->priv_key);
112 CRYPTO_MUTEX_cleanup(&dh->method_mont_p_lock);
114 OPENSSL_free(dh);
117 void DH_get0_key(const DH *dh, const BIGNUM **out_pub_key,
120 *out_pub_key = dh->pub_key;
123 *out_priv_key = dh->priv_key;
127 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) {
129 BN_free(dh->pub_key);
130 dh->pub_key = pub_key;
134 BN_free(dh->priv_key);
135 dh->priv_key = priv_key;
141 void DH_get0_pqg(const DH *dh, const BIGNUM **out_p, const BIGNUM **out_q,
144 *out_p = dh->p;
147 *out_q = dh->q;
150 *out_g = dh->g;
154 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) {
155 if ((dh->p == NULL && p == NULL) ||
156 (dh->g == NULL && g == NULL)) {
161 BN_free(dh->p);
162 dh->p = p;
166 BN_free(dh->q);
167 dh->q = q;
171 BN_free(dh->g);
172 dh->g = g;
178 int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, BN_GENCB *cb) {
179 // We generate DH parameters as follows
197 // Since DH should be using a safe prime (both p and q are prime),
219 // Make sure |dh| has the necessary elements
220 if (dh->p == NULL) {
221 dh->p = BN_new();
222 if (dh->p == NULL) {
226 if (dh->g == NULL) {
227 dh->g = BN_new();
228 if (dh->g == NULL) {
234 OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
269 if (!BN_generate_prime_ex(dh->p, prime_bits, 1, t1, t2, cb)) {
275 if (!BN_set_word(dh->g, g)) {
282 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
292 int DH_generate_key(DH *dh) {
298 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
299 OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
308 if (dh->priv_key == NULL) {
315 priv_key = dh->priv_key;
318 if (dh->pub_key == NULL) {
324 pub_key = dh->pub_key;
327 if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
328 dh->p, ctx)) {
333 if (dh->q) {
334 if (!BN_rand_range_ex(priv_key, 2, dh->q)) {
339 unsigned priv_bits = dh->priv_length;
341 const unsigned p_bits = BN_num_bits(dh->p);
355 if (!BN_mod_exp_mont_consttime(pub_key, dh->g, priv_key, dh->p, ctx,
356 dh->method_mont_p)) {
360 dh->pub_key = pub_key;
361 dh->priv_key = priv_key;
366 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
369 if (dh->pub_key == NULL) {
372 if (dh->priv_key == NULL) {
379 int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
385 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
386 OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
400 if (dh->priv_key == NULL) {
401 OPENSSL_PUT_ERROR(DH, DH_R_NO_PRIVATE_VALUE);
405 if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
406 dh->p, ctx)) {
410 if (!DH_check_pub_key(dh, peers_key, &check_result) || check_result) {
411 OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
415 if (!BN_mod_exp_mont_consttime(shared_key, peers_key, dh->priv_key, dh->p,
416 ctx, dh->method_mont_p)) {
417 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
432 int DH_size(const DH *dh) { return BN_num_bytes(dh->p); }
434 unsigned DH_num_bits(const DH *dh) { return BN_num_bits(dh->p); }
436 int DH_up_ref(DH *dh) {
437 CRYPTO_refcount_inc(&dh->references);
456 static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
489 DH *DHparams_dup(const DH *dh) {
490 DH *ret = DH_new();
495 if (!int_dh_param_copy(ret, dh, -1)) {
513 int DH_set_ex_data(DH *d, int idx, void *arg) {
517 void *DH_get_ex_data(DH *d, int idx) {