dh.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef HEADER_DH_H
  10. # define HEADER_DH_H
  11. # include <openssl/opensslconf.h>
  12. # ifndef OPENSSL_NO_DH
  13. # include <openssl/e_os2.h>
  14. # include <openssl/bio.h>
  15. # include <openssl/asn1.h>
  16. # include <openssl/ossl_typ.h>
  17. # if OPENSSL_API_COMPAT < 0x10100000L
  18. # include <openssl/bn.h>
  19. # endif
  20. # include <openssl/dherr.h>
  21. # ifdef __cplusplus
  22. extern "C" {
  23. # endif
  24. # ifndef OPENSSL_DH_MAX_MODULUS_BITS
  25. # define OPENSSL_DH_MAX_MODULUS_BITS 10000
  26. # endif
  27. # ifndef OPENSSL_DH_CHECK_MAX_MODULUS_BITS
  28. # define OPENSSL_DH_CHECK_MAX_MODULUS_BITS 32768
  29. # endif
  30. # define OPENSSL_DH_FIPS_MIN_MODULUS_BITS 1024
  31. # define DH_FLAG_CACHE_MONT_P 0x01
  32. # if OPENSSL_API_COMPAT < 0x10100000L
  33. /*
  34. * Does nothing. Previously this switched off constant time behaviour.
  35. */
  36. # define DH_FLAG_NO_EXP_CONSTTIME 0x00
  37. # endif
  38. /*
  39. * If this flag is set the DH method is FIPS compliant and can be used in
  40. * FIPS mode. This is set in the validated module method. If an application
  41. * sets this flag in its own methods it is its responsibility to ensure the
  42. * result is compliant.
  43. */
  44. # define DH_FLAG_FIPS_METHOD 0x0400
  45. /*
  46. * If this flag is set the operations normally disabled in FIPS mode are
  47. * permitted it is then the applications responsibility to ensure that the
  48. * usage is compliant.
  49. */
  50. # define DH_FLAG_NON_FIPS_ALLOW 0x0400
  51. /* Already defined in ossl_typ.h */
  52. /* typedef struct dh_st DH; */
  53. /* typedef struct dh_method DH_METHOD; */
  54. DECLARE_ASN1_ITEM(DHparams)
  55. # define DH_GENERATOR_2 2
  56. /* #define DH_GENERATOR_3 3 */
  57. # define DH_GENERATOR_5 5
  58. /* DH_check error codes */
  59. # define DH_CHECK_P_NOT_PRIME 0x01
  60. # define DH_CHECK_P_NOT_SAFE_PRIME 0x02
  61. # define DH_UNABLE_TO_CHECK_GENERATOR 0x04
  62. # define DH_NOT_SUITABLE_GENERATOR 0x08
  63. # define DH_CHECK_Q_NOT_PRIME 0x10
  64. # define DH_CHECK_INVALID_Q_VALUE 0x20
  65. # define DH_CHECK_INVALID_J_VALUE 0x40
  66. /* DH_check_pub_key error codes */
  67. # define DH_CHECK_PUBKEY_TOO_SMALL 0x01
  68. # define DH_CHECK_PUBKEY_TOO_LARGE 0x02
  69. # define DH_CHECK_PUBKEY_INVALID 0x04
  70. /*
  71. * primes p where (p-1)/2 is prime too are called "safe"; we define this for
  72. * backward compatibility:
  73. */
  74. # define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME
  75. # define d2i_DHparams_fp(fp,x) \
  76. (DH *)ASN1_d2i_fp((char *(*)())DH_new, \
  77. (char *(*)())d2i_DHparams, \
  78. (fp), \
  79. (unsigned char **)(x))
  80. # define i2d_DHparams_fp(fp,x) \
  81. ASN1_i2d_fp(i2d_DHparams,(fp), (unsigned char *)(x))
  82. # define d2i_DHparams_bio(bp,x) \
  83. ASN1_d2i_bio_of(DH, DH_new, d2i_DHparams, bp, x)
  84. # define i2d_DHparams_bio(bp,x) \
  85. ASN1_i2d_bio_of_const(DH,i2d_DHparams,bp,x)
  86. # define d2i_DHxparams_fp(fp,x) \
  87. (DH *)ASN1_d2i_fp((char *(*)())DH_new, \
  88. (char *(*)())d2i_DHxparams, \
  89. (fp), \
  90. (unsigned char **)(x))
  91. # define i2d_DHxparams_fp(fp,x) \
  92. ASN1_i2d_fp(i2d_DHxparams,(fp), (unsigned char *)(x))
  93. # define d2i_DHxparams_bio(bp,x) \
  94. ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, bp, x)
  95. # define i2d_DHxparams_bio(bp,x) \
  96. ASN1_i2d_bio_of_const(DH, i2d_DHxparams, bp, x)
  97. DH *DHparams_dup(DH *);
  98. const DH_METHOD *DH_OpenSSL(void);
  99. void DH_set_default_method(const DH_METHOD *meth);
  100. const DH_METHOD *DH_get_default_method(void);
  101. int DH_set_method(DH *dh, const DH_METHOD *meth);
  102. DH *DH_new_method(ENGINE *engine);
  103. DH *DH_new(void);
  104. void DH_free(DH *dh);
  105. int DH_up_ref(DH *dh);
  106. int DH_bits(const DH *dh);
  107. int DH_size(const DH *dh);
  108. int DH_security_bits(const DH *dh);
  109. #define DH_get_ex_new_index(l, p, newf, dupf, freef) \
  110. CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, l, p, newf, dupf, freef)
  111. int DH_set_ex_data(DH *d, int idx, void *arg);
  112. void *DH_get_ex_data(DH *d, int idx);
  113. /* Deprecated version */
  114. DEPRECATEDIN_0_9_8(DH *DH_generate_parameters(int prime_len, int generator,
  115. void (*callback) (int, int,
  116. void *),
  117. void *cb_arg))
  118. /* New version */
  119. int DH_generate_parameters_ex(DH *dh, int prime_len, int generator,
  120. BN_GENCB *cb);
  121. int DH_check_params_ex(const DH *dh);
  122. int DH_check_ex(const DH *dh);
  123. int DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key);
  124. int DH_check_params(const DH *dh, int *ret);
  125. int DH_check(const DH *dh, int *codes);
  126. int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *codes);
  127. int DH_generate_key(DH *dh);
  128. int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
  129. int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh);
  130. DH *d2i_DHparams(DH **a, const unsigned char **pp, long length);
  131. int i2d_DHparams(const DH *a, unsigned char **pp);
  132. DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length);
  133. int i2d_DHxparams(const DH *a, unsigned char **pp);
  134. # ifndef OPENSSL_NO_STDIO
  135. int DHparams_print_fp(FILE *fp, const DH *x);
  136. # endif
  137. int DHparams_print(BIO *bp, const DH *x);
  138. /* RFC 5114 parameters */
  139. DH *DH_get_1024_160(void);
  140. DH *DH_get_2048_224(void);
  141. DH *DH_get_2048_256(void);
  142. /* Named parameters, currently RFC7919 */
  143. DH *DH_new_by_nid(int nid);
  144. int DH_get_nid(const DH *dh);
  145. # ifndef OPENSSL_NO_CMS
  146. /* RFC2631 KDF */
  147. int DH_KDF_X9_42(unsigned char *out, size_t outlen,
  148. const unsigned char *Z, size_t Zlen,
  149. ASN1_OBJECT *key_oid,
  150. const unsigned char *ukm, size_t ukmlen, const EVP_MD *md);
  151. # endif
  152. void DH_get0_pqg(const DH *dh,
  153. const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
  154. int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
  155. void DH_get0_key(const DH *dh,
  156. const BIGNUM **pub_key, const BIGNUM **priv_key);
  157. int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
  158. const BIGNUM *DH_get0_p(const DH *dh);
  159. const BIGNUM *DH_get0_q(const DH *dh);
  160. const BIGNUM *DH_get0_g(const DH *dh);
  161. const BIGNUM *DH_get0_priv_key(const DH *dh);
  162. const BIGNUM *DH_get0_pub_key(const DH *dh);
  163. void DH_clear_flags(DH *dh, int flags);
  164. int DH_test_flags(const DH *dh, int flags);
  165. void DH_set_flags(DH *dh, int flags);
  166. ENGINE *DH_get0_engine(DH *d);
  167. long DH_get_length(const DH *dh);
  168. int DH_set_length(DH *dh, long length);
  169. DH_METHOD *DH_meth_new(const char *name, int flags);
  170. void DH_meth_free(DH_METHOD *dhm);
  171. DH_METHOD *DH_meth_dup(const DH_METHOD *dhm);
  172. const char *DH_meth_get0_name(const DH_METHOD *dhm);
  173. int DH_meth_set1_name(DH_METHOD *dhm, const char *name);
  174. int DH_meth_get_flags(const DH_METHOD *dhm);
  175. int DH_meth_set_flags(DH_METHOD *dhm, int flags);
  176. void *DH_meth_get0_app_data(const DH_METHOD *dhm);
  177. int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data);
  178. int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *);
  179. int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key) (DH *));
  180. int (*DH_meth_get_compute_key(const DH_METHOD *dhm))
  181. (unsigned char *key, const BIGNUM *pub_key, DH *dh);
  182. int DH_meth_set_compute_key(DH_METHOD *dhm,
  183. int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh));
  184. int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm))
  185. (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
  186. BN_CTX *, BN_MONT_CTX *);
  187. int DH_meth_set_bn_mod_exp(DH_METHOD *dhm,
  188. int (*bn_mod_exp) (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  189. const BIGNUM *, BN_CTX *, BN_MONT_CTX *));
  190. int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *);
  191. int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *));
  192. int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *);
  193. int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *));
  194. int (*DH_meth_get_generate_params(const DH_METHOD *dhm))
  195. (DH *, int, int, BN_GENCB *);
  196. int DH_meth_set_generate_params(DH_METHOD *dhm,
  197. int (*generate_params) (DH *, int, int, BN_GENCB *));
  198. # define EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len) \
  199. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  200. EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, NULL)
  201. # define EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len) \
  202. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  203. EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, len, NULL)
  204. # define EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ) \
  205. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  206. EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, typ, NULL)
  207. # define EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen) \
  208. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  209. EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL)
  210. # define EVP_PKEY_CTX_set_dh_rfc5114(ctx, gen) \
  211. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \
  212. EVP_PKEY_CTRL_DH_RFC5114, gen, NULL)
  213. # define EVP_PKEY_CTX_set_dhx_rfc5114(ctx, gen) \
  214. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \
  215. EVP_PKEY_CTRL_DH_RFC5114, gen, NULL)
  216. # define EVP_PKEY_CTX_set_dh_nid(ctx, nid) \
  217. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, \
  218. EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN, \
  219. EVP_PKEY_CTRL_DH_NID, nid, NULL)
  220. # define EVP_PKEY_CTX_set_dh_pad(ctx, pad) \
  221. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE, \
  222. EVP_PKEY_CTRL_DH_PAD, pad, NULL)
  223. # define EVP_PKEY_CTX_set_dh_kdf_type(ctx, kdf) \
  224. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  225. EVP_PKEY_OP_DERIVE, \
  226. EVP_PKEY_CTRL_DH_KDF_TYPE, kdf, NULL)
  227. # define EVP_PKEY_CTX_get_dh_kdf_type(ctx) \
  228. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  229. EVP_PKEY_OP_DERIVE, \
  230. EVP_PKEY_CTRL_DH_KDF_TYPE, -2, NULL)
  231. # define EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, oid) \
  232. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  233. EVP_PKEY_OP_DERIVE, \
  234. EVP_PKEY_CTRL_DH_KDF_OID, 0, (void *)(oid))
  235. # define EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, poid) \
  236. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  237. EVP_PKEY_OP_DERIVE, \
  238. EVP_PKEY_CTRL_GET_DH_KDF_OID, 0, (void *)(poid))
  239. # define EVP_PKEY_CTX_set_dh_kdf_md(ctx, md) \
  240. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  241. EVP_PKEY_OP_DERIVE, \
  242. EVP_PKEY_CTRL_DH_KDF_MD, 0, (void *)(md))
  243. # define EVP_PKEY_CTX_get_dh_kdf_md(ctx, pmd) \
  244. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  245. EVP_PKEY_OP_DERIVE, \
  246. EVP_PKEY_CTRL_GET_DH_KDF_MD, 0, (void *)(pmd))
  247. # define EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, len) \
  248. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  249. EVP_PKEY_OP_DERIVE, \
  250. EVP_PKEY_CTRL_DH_KDF_OUTLEN, len, NULL)
  251. # define EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, plen) \
  252. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  253. EVP_PKEY_OP_DERIVE, \
  254. EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, 0, (void *)(plen))
  255. # define EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p, plen) \
  256. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  257. EVP_PKEY_OP_DERIVE, \
  258. EVP_PKEY_CTRL_DH_KDF_UKM, plen, (void *)(p))
  259. # define EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p) \
  260. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  261. EVP_PKEY_OP_DERIVE, \
  262. EVP_PKEY_CTRL_GET_DH_KDF_UKM, 0, (void *)(p))
  263. # define EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN (EVP_PKEY_ALG_CTRL + 1)
  264. # define EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR (EVP_PKEY_ALG_CTRL + 2)
  265. # define EVP_PKEY_CTRL_DH_RFC5114 (EVP_PKEY_ALG_CTRL + 3)
  266. # define EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN (EVP_PKEY_ALG_CTRL + 4)
  267. # define EVP_PKEY_CTRL_DH_PARAMGEN_TYPE (EVP_PKEY_ALG_CTRL + 5)
  268. # define EVP_PKEY_CTRL_DH_KDF_TYPE (EVP_PKEY_ALG_CTRL + 6)
  269. # define EVP_PKEY_CTRL_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 7)
  270. # define EVP_PKEY_CTRL_GET_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 8)
  271. # define EVP_PKEY_CTRL_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 9)
  272. # define EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 10)
  273. # define EVP_PKEY_CTRL_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 11)
  274. # define EVP_PKEY_CTRL_GET_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 12)
  275. # define EVP_PKEY_CTRL_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 13)
  276. # define EVP_PKEY_CTRL_GET_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 14)
  277. # define EVP_PKEY_CTRL_DH_NID (EVP_PKEY_ALG_CTRL + 15)
  278. # define EVP_PKEY_CTRL_DH_PAD (EVP_PKEY_ALG_CTRL + 16)
  279. /* KDF types */
  280. # define EVP_PKEY_DH_KDF_NONE 1
  281. # ifndef OPENSSL_NO_CMS
  282. # define EVP_PKEY_DH_KDF_X9_42 2
  283. # endif
  284. # ifdef __cplusplus
  285. }
  286. # endif
  287. # endif
  288. #endif