Home | History | Annotate | Download | only in hcrypto

Lines Matching defs:dh

1 /*	$NetBSD: dh.c,v 1.3 2023/06/19 21:41:43 christos Exp $	*/
44 #include <dh.h>
47 * @page page_dh DH - Diffie-Hellman key exchange
58 * Create a new DH object using DH_new_method(NULL), see DH_new_method().
60 * @return a newly allocated DH object.
65 DH *
72 * Create a new DH object from the given engine, if the NULL is used,
73 * the default engine is used. Free the DH object with DH_free().
75 * @param engine The engine to use to allocate the DH object.
77 * @return a newly allocated DH object.
82 DH *
85 DH *dh;
87 dh = calloc(1, sizeof(*dh));
88 if (dh == NULL)
91 dh->references = 1;
95 dh->engine = engine;
97 dh->engine = ENGINE_get_default_DH();
100 if (dh->engine) {
101 dh->meth = ENGINE_get_DH(dh->engine);
102 if (dh->meth == NULL) {
104 free(dh);
109 if (dh->meth == NULL)
110 dh->meth = DH_get_default_method();
112 (*dh->meth->init)(dh);
114 return dh;
118 * Free a DH object and release related resources, like ENGINE, that
121 * @param dh object to be freed.
127 DH_free(DH *dh)
129 if (dh->references <= 0)
132 if (--dh->references > 0)
135 (*dh->meth->finish)(dh);
137 if (dh->engine)
138 ENGINE_finish(dh->engine);
141 free_if(dh->p);
142 free_if(dh->g);
143 free_if(dh->pub_key);
144 free_if(dh->priv_key);
145 free_if(dh->q);
146 free_if(dh->j);
147 free_if(dh->counter);
150 memset(dh, 0, sizeof(*dh));
151 free(dh);
155 * Add a reference to the DH object. The object should be free with
158 * @param dh the object to increase the reference count too.
167 DH_up_ref(DH *dh)
169 return ++dh->references;
175 * @param dh The DH object to get the size from.
183 DH_size(const DH *dh)
185 return BN_num_bytes(dh->p);
189 * Set the data index idx in the DH object to data.
191 * @param dh DH object.
201 DH_set_ex_data(DH *dh, int idx, void *data)
203 dh->ex_data.sk = data;
208 * Get the data for index idx in the DH object.
210 * @param dh DH object.
219 DH_get_ex_data(DH *dh, int idx)
221 return dh->ex_data.sk;
225 * Generate DH parameters for the DH object give parameters.
227 * @param dh The DH object to generate parameters for.
238 DH_generate_parameters_ex(DH *dh, int prime_len, int generator, BN_GENCB *cb)
240 if (dh->meth->generate_params)
241 return dh->meth->generate_params(dh, prime_len, generator, cb);
248 * @param dh the local peer DH parameters.
259 DH_check_pubkey(const DH *dh, const BIGNUM *pub_key, int *codes)
295 if (BN_cmp(sum, dh->p) >= 0)
306 if (BN_cmp(bn, dh->g) == 0) {
331 * Generate a new DH private-public key pair. The dh parameter must be
332 * allocted first with DH_new(). dh->p and dp->g must be set.
334 * @param dh dh parameter.
342 DH_generate_key(DH *dh)
344 return dh->meth->generate_key(dh);
353 * @param dh the dh key pair.
362 const BIGNUM *peer_pub_key, DH *dh)
371 if (!DH_check_pubkey(dh, peer_pub_key, &codes) || codes != 0)
374 return dh->meth->compute_key(shared_key, peer_pub_key, dh);
378 * Set a new method for the DH keypair.
380 * @param dh dh parameter.
381 * @param method the new method for the DH parameter.
389 DH_set_method(DH *dh, const DH_METHOD *method)
391 (*dh->meth->finish)(dh);
392 if (dh->engine) {
393 ENGINE_finish(dh->engine);
394 dh->engine = NULL;
396 dh->meth = method;
397 (*dh->meth->init)(dh);
406 dh_null_generate_key(DH *dh)
412 dh_null_compute_key(unsigned char *shared,const BIGNUM *pub, DH *dh)
418 dh_null_init(DH *dh)
424 dh_null_finish(DH *dh)
430 dh_null_generate_params(DH *dh, int prime_num, int len, BN_GENCB *cb)
436 "hcrypto null DH",
451 * Return the dummy DH implementation.
465 * Set the default DH implementation.
479 * Return the default DH implementation.
515 i2d_DHparams(DH *dh, unsigned char **pp)
523 if (bn2heim_int(dh->p, &data.prime) ||
524 bn2heim_int(dh->g, &data.base))