dh.h 14 KB

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