dh.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright 1995-2018 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 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_1_1_0
  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_1_1_0
  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(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(DH, i2d_DHxparams, bp, x)
  94. DECLARE_ASN1_DUP_FUNCTION_name(DH, DHparams)
  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. DECLARE_ASN1_ENCODE_FUNCTIONS_only(DH, DHparams)
  128. DECLARE_ASN1_ENCODE_FUNCTIONS_only(DH, DHxparams)
  129. # ifndef OPENSSL_NO_STDIO
  130. int DHparams_print_fp(FILE *fp, const DH *x);
  131. # endif
  132. int DHparams_print(BIO *bp, const DH *x);
  133. /* RFC 5114 parameters */
  134. DH *DH_get_1024_160(void);
  135. DH *DH_get_2048_224(void);
  136. DH *DH_get_2048_256(void);
  137. /* Named parameters, currently RFC7919 */
  138. DH *DH_new_by_nid(int nid);
  139. int DH_get_nid(const DH *dh);
  140. # ifndef OPENSSL_NO_CMS
  141. /* RFC2631 KDF */
  142. int DH_KDF_X9_42(unsigned char *out, size_t outlen,
  143. const unsigned char *Z, size_t Zlen,
  144. ASN1_OBJECT *key_oid,
  145. const unsigned char *ukm, size_t ukmlen, const EVP_MD *md);
  146. # endif
  147. void DH_get0_pqg(const DH *dh,
  148. const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
  149. int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
  150. void DH_get0_key(const DH *dh,
  151. const BIGNUM **pub_key, const BIGNUM **priv_key);
  152. int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
  153. const BIGNUM *DH_get0_p(const DH *dh);
  154. const BIGNUM *DH_get0_q(const DH *dh);
  155. const BIGNUM *DH_get0_g(const DH *dh);
  156. const BIGNUM *DH_get0_priv_key(const DH *dh);
  157. const BIGNUM *DH_get0_pub_key(const DH *dh);
  158. void DH_clear_flags(DH *dh, int flags);
  159. int DH_test_flags(const DH *dh, int flags);
  160. void DH_set_flags(DH *dh, int flags);
  161. ENGINE *DH_get0_engine(DH *d);
  162. long DH_get_length(const DH *dh);
  163. int DH_set_length(DH *dh, long length);
  164. DH_METHOD *DH_meth_new(const char *name, int flags);
  165. void DH_meth_free(DH_METHOD *dhm);
  166. DH_METHOD *DH_meth_dup(const DH_METHOD *dhm);
  167. const char *DH_meth_get0_name(const DH_METHOD *dhm);
  168. int DH_meth_set1_name(DH_METHOD *dhm, const char *name);
  169. int DH_meth_get_flags(const DH_METHOD *dhm);
  170. int DH_meth_set_flags(DH_METHOD *dhm, int flags);
  171. void *DH_meth_get0_app_data(const DH_METHOD *dhm);
  172. int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data);
  173. int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *);
  174. int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key) (DH *));
  175. int (*DH_meth_get_compute_key(const DH_METHOD *dhm))
  176. (unsigned char *key, const BIGNUM *pub_key, DH *dh);
  177. int DH_meth_set_compute_key(DH_METHOD *dhm,
  178. int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh));
  179. int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm))
  180. (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
  181. BN_CTX *, BN_MONT_CTX *);
  182. int DH_meth_set_bn_mod_exp(DH_METHOD *dhm,
  183. int (*bn_mod_exp) (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  184. const BIGNUM *, BN_CTX *, BN_MONT_CTX *));
  185. int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *);
  186. int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *));
  187. int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *);
  188. int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *));
  189. int (*DH_meth_get_generate_params(const DH_METHOD *dhm))
  190. (DH *, int, int, BN_GENCB *);
  191. int DH_meth_set_generate_params(DH_METHOD *dhm,
  192. int (*generate_params) (DH *, int, int, BN_GENCB *));
  193. # define EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len) \
  194. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  195. EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, NULL)
  196. # define EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len) \
  197. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  198. EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, len, NULL)
  199. # define EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ) \
  200. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  201. EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, typ, NULL)
  202. # define EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen) \
  203. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  204. EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL)
  205. # define EVP_PKEY_CTX_set_dh_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_dhx_rfc5114(ctx, gen) \
  209. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \
  210. EVP_PKEY_CTRL_DH_RFC5114, gen, NULL)
  211. # define EVP_PKEY_CTX_set_dh_nid(ctx, nid) \
  212. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, \
  213. EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN, \
  214. EVP_PKEY_CTRL_DH_NID, nid, NULL)
  215. int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad);
  216. # define EVP_PKEY_CTX_set_dh_kdf_type(ctx, kdf) \
  217. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  218. EVP_PKEY_OP_DERIVE, \
  219. EVP_PKEY_CTRL_DH_KDF_TYPE, kdf, NULL)
  220. # define EVP_PKEY_CTX_get_dh_kdf_type(ctx) \
  221. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  222. EVP_PKEY_OP_DERIVE, \
  223. EVP_PKEY_CTRL_DH_KDF_TYPE, -2, NULL)
  224. # define EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, oid) \
  225. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  226. EVP_PKEY_OP_DERIVE, \
  227. EVP_PKEY_CTRL_DH_KDF_OID, 0, (void *)(oid))
  228. # define EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, poid) \
  229. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  230. EVP_PKEY_OP_DERIVE, \
  231. EVP_PKEY_CTRL_GET_DH_KDF_OID, 0, (void *)(poid))
  232. # define EVP_PKEY_CTX_set_dh_kdf_md(ctx, md) \
  233. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  234. EVP_PKEY_OP_DERIVE, \
  235. EVP_PKEY_CTRL_DH_KDF_MD, 0, (void *)(md))
  236. # define EVP_PKEY_CTX_get_dh_kdf_md(ctx, pmd) \
  237. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  238. EVP_PKEY_OP_DERIVE, \
  239. EVP_PKEY_CTRL_GET_DH_KDF_MD, 0, (void *)(pmd))
  240. # define EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, len) \
  241. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  242. EVP_PKEY_OP_DERIVE, \
  243. EVP_PKEY_CTRL_DH_KDF_OUTLEN, len, NULL)
  244. # define EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, plen) \
  245. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  246. EVP_PKEY_OP_DERIVE, \
  247. EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, 0, (void *)(plen))
  248. # define EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p, plen) \
  249. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  250. EVP_PKEY_OP_DERIVE, \
  251. EVP_PKEY_CTRL_DH_KDF_UKM, plen, (void *)(p))
  252. # define EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p) \
  253. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  254. EVP_PKEY_OP_DERIVE, \
  255. EVP_PKEY_CTRL_GET_DH_KDF_UKM, 0, (void *)(p))
  256. # define EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN (EVP_PKEY_ALG_CTRL + 1)
  257. # define EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR (EVP_PKEY_ALG_CTRL + 2)
  258. # define EVP_PKEY_CTRL_DH_RFC5114 (EVP_PKEY_ALG_CTRL + 3)
  259. # define EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN (EVP_PKEY_ALG_CTRL + 4)
  260. # define EVP_PKEY_CTRL_DH_PARAMGEN_TYPE (EVP_PKEY_ALG_CTRL + 5)
  261. # define EVP_PKEY_CTRL_DH_KDF_TYPE (EVP_PKEY_ALG_CTRL + 6)
  262. # define EVP_PKEY_CTRL_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 7)
  263. # define EVP_PKEY_CTRL_GET_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 8)
  264. # define EVP_PKEY_CTRL_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 9)
  265. # define EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 10)
  266. # define EVP_PKEY_CTRL_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 11)
  267. # define EVP_PKEY_CTRL_GET_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 12)
  268. # define EVP_PKEY_CTRL_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 13)
  269. # define EVP_PKEY_CTRL_GET_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 14)
  270. # define EVP_PKEY_CTRL_DH_NID (EVP_PKEY_ALG_CTRL + 15)
  271. # define EVP_PKEY_CTRL_DH_PAD (EVP_PKEY_ALG_CTRL + 16)
  272. /* KDF types */
  273. # define EVP_PKEY_DH_KDF_NONE 1
  274. # ifndef OPENSSL_NO_CMS
  275. # define EVP_PKEY_DH_KDF_X9_42 2
  276. # endif
  277. # ifdef __cplusplus
  278. }
  279. # endif
  280. # endif
  281. #endif