dh.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. void DH_clear_flags(DH *dh, int flags);
  156. int DH_test_flags(const DH *dh, int flags);
  157. void DH_set_flags(DH *dh, int flags);
  158. ENGINE *DH_get0_engine(DH *d);
  159. long DH_get_length(const DH *dh);
  160. int DH_set_length(DH *dh, long length);
  161. DH_METHOD *DH_meth_new(const char *name, int flags);
  162. void DH_meth_free(DH_METHOD *dhm);
  163. DH_METHOD *DH_meth_dup(const DH_METHOD *dhm);
  164. const char *DH_meth_get0_name(const DH_METHOD *dhm);
  165. int DH_meth_set1_name(DH_METHOD *dhm, const char *name);
  166. int DH_meth_get_flags(DH_METHOD *dhm);
  167. int DH_meth_set_flags(DH_METHOD *dhm, int flags);
  168. void *DH_meth_get0_app_data(const DH_METHOD *dhm);
  169. int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data);
  170. int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *);
  171. int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key) (DH *));
  172. int (*DH_meth_get_compute_key(const DH_METHOD *dhm))
  173. (unsigned char *key, const BIGNUM *pub_key, DH *dh);
  174. int DH_meth_set_compute_key(DH_METHOD *dhm,
  175. int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh));
  176. int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm))
  177. (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
  178. BN_CTX *, BN_MONT_CTX *);
  179. int DH_meth_set_bn_mod_exp(DH_METHOD *dhm,
  180. int (*bn_mod_exp) (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  181. const BIGNUM *, BN_CTX *, BN_MONT_CTX *));
  182. int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *);
  183. int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *));
  184. int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *);
  185. int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *));
  186. int (*DH_meth_get_generate_params(const DH_METHOD *dhm))
  187. (DH *, int, int, BN_GENCB *);
  188. int DH_meth_set_generate_params(DH_METHOD *dhm,
  189. int (*generate_params) (DH *, int, int, BN_GENCB *));
  190. # define EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len) \
  191. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  192. EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, NULL)
  193. # define EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len) \
  194. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  195. EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, len, NULL)
  196. # define EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ) \
  197. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  198. EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, typ, NULL)
  199. # define EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen) \
  200. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  201. EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL)
  202. # define EVP_PKEY_CTX_set_dh_rfc5114(ctx, gen) \
  203. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \
  204. EVP_PKEY_CTRL_DH_RFC5114, gen, NULL)
  205. # define EVP_PKEY_CTX_set_dhx_rfc5114(ctx, gen) \
  206. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \
  207. EVP_PKEY_CTRL_DH_RFC5114, gen, NULL)
  208. # define EVP_PKEY_CTX_set_dh_nid(ctx, nid) \
  209. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, \
  210. EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN, \
  211. EVP_PKEY_CTRL_DH_NID, nid, NULL)
  212. # define EVP_PKEY_CTX_set_dh_pad(ctx, pad) \
  213. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE, \
  214. EVP_PKEY_CTRL_DH_PAD, pad, NULL)
  215. # define EVP_PKEY_CTX_set_dh_kdf_type(ctx, kdf) \
  216. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  217. EVP_PKEY_OP_DERIVE, \
  218. EVP_PKEY_CTRL_DH_KDF_TYPE, kdf, NULL)
  219. # define EVP_PKEY_CTX_get_dh_kdf_type(ctx) \
  220. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  221. EVP_PKEY_OP_DERIVE, \
  222. EVP_PKEY_CTRL_DH_KDF_TYPE, -2, NULL)
  223. # define EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, oid) \
  224. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  225. EVP_PKEY_OP_DERIVE, \
  226. EVP_PKEY_CTRL_DH_KDF_OID, 0, (void *)(oid))
  227. # define EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, poid) \
  228. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  229. EVP_PKEY_OP_DERIVE, \
  230. EVP_PKEY_CTRL_GET_DH_KDF_OID, 0, (void *)(poid))
  231. # define EVP_PKEY_CTX_set_dh_kdf_md(ctx, md) \
  232. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  233. EVP_PKEY_OP_DERIVE, \
  234. EVP_PKEY_CTRL_DH_KDF_MD, 0, (void *)(md))
  235. # define EVP_PKEY_CTX_get_dh_kdf_md(ctx, pmd) \
  236. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  237. EVP_PKEY_OP_DERIVE, \
  238. EVP_PKEY_CTRL_GET_DH_KDF_MD, 0, (void *)(pmd))
  239. # define EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, len) \
  240. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  241. EVP_PKEY_OP_DERIVE, \
  242. EVP_PKEY_CTRL_DH_KDF_OUTLEN, len, NULL)
  243. # define EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, plen) \
  244. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  245. EVP_PKEY_OP_DERIVE, \
  246. EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, 0, (void *)(plen))
  247. # define EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p, plen) \
  248. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  249. EVP_PKEY_OP_DERIVE, \
  250. EVP_PKEY_CTRL_DH_KDF_UKM, plen, (void *)(p))
  251. # define EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p) \
  252. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  253. EVP_PKEY_OP_DERIVE, \
  254. EVP_PKEY_CTRL_GET_DH_KDF_UKM, 0, (void *)(p))
  255. # define EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN (EVP_PKEY_ALG_CTRL + 1)
  256. # define EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR (EVP_PKEY_ALG_CTRL + 2)
  257. # define EVP_PKEY_CTRL_DH_RFC5114 (EVP_PKEY_ALG_CTRL + 3)
  258. # define EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN (EVP_PKEY_ALG_CTRL + 4)
  259. # define EVP_PKEY_CTRL_DH_PARAMGEN_TYPE (EVP_PKEY_ALG_CTRL + 5)
  260. # define EVP_PKEY_CTRL_DH_KDF_TYPE (EVP_PKEY_ALG_CTRL + 6)
  261. # define EVP_PKEY_CTRL_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 7)
  262. # define EVP_PKEY_CTRL_GET_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 8)
  263. # define EVP_PKEY_CTRL_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 9)
  264. # define EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 10)
  265. # define EVP_PKEY_CTRL_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 11)
  266. # define EVP_PKEY_CTRL_GET_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 12)
  267. # define EVP_PKEY_CTRL_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 13)
  268. # define EVP_PKEY_CTRL_GET_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 14)
  269. # define EVP_PKEY_CTRL_DH_NID (EVP_PKEY_ALG_CTRL + 15)
  270. # define EVP_PKEY_CTRL_DH_PAD (EVP_PKEY_ALG_CTRL + 16)
  271. /* KDF types */
  272. # define EVP_PKEY_DH_KDF_NONE 1
  273. # ifndef OPENSSL_NO_CMS
  274. # define EVP_PKEY_DH_KDF_X9_42 2
  275. # endif
  276. # ifdef __cplusplus
  277. }
  278. # endif
  279. # endif
  280. #endif