dh.h 15 KB

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