dh.h 13 KB

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