A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
14 KiB

This is a first-cut at improving the callback mechanisms used in key-generation and prime-checking functions. Rather than explicitly passing callback functions and caller-defined context data for the callbacks, a new structure BN_GENCB is defined that encapsulates this; a pointer to the structure is passed to all such functions instead. This wrapper structure allows the encapsulation of "old" and "new" style callbacks - "new" callbacks return a boolean result on the understanding that returning FALSE should terminate keygen/primality processing. The BN_GENCB abstraction will allow future callback modifications without needing to break binary compatibility nor change the API function prototypes. The new API functions have been given names ending in "_ex" and the old functions are implemented as wrappers to the new ones. The OPENSSL_NO_DEPRECATED symbol has been introduced so that, if defined, declaration of the older functions will be skipped. NB: Some openssl-internal code will stick with the older callbacks for now, so appropriate "#undef" logic will be put in place - this is in case the user is *building* openssl (rather than *including* its headers) with this symbol defined. There is another change in the new _ex functions; the key-generation functions do not return key structures but operate on structures passed by the caller, the return value is a boolean. This will allow for a smoother transition to having key-generation as "virtual function" in the various ***_METHOD tables.
20 years ago
This is a first-cut at improving the callback mechanisms used in key-generation and prime-checking functions. Rather than explicitly passing callback functions and caller-defined context data for the callbacks, a new structure BN_GENCB is defined that encapsulates this; a pointer to the structure is passed to all such functions instead. This wrapper structure allows the encapsulation of "old" and "new" style callbacks - "new" callbacks return a boolean result on the understanding that returning FALSE should terminate keygen/primality processing. The BN_GENCB abstraction will allow future callback modifications without needing to break binary compatibility nor change the API function prototypes. The new API functions have been given names ending in "_ex" and the old functions are implemented as wrappers to the new ones. The OPENSSL_NO_DEPRECATED symbol has been introduced so that, if defined, declaration of the older functions will be skipped. NB: Some openssl-internal code will stick with the older callbacks for now, so appropriate "#undef" logic will be put in place - this is in case the user is *building* openssl (rather than *including* its headers) with this symbol defined. There is another change in the new _ex functions; the key-generation functions do not return key structures but operate on structures passed by the caller, the return value is a boolean. This will allow for a smoother transition to having key-generation as "virtual function" in the various ***_METHOD tables.
20 years ago
  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. # define DH_FLAG_NO_EXP_CONSTTIME 0x02
  29. /*
  30. * If this flag is set the DH method is FIPS compliant and can be used in
  31. * FIPS mode. This is set in the validated module method. If an application
  32. * sets this flag in its own methods it is its responsibility to ensure the
  33. * result is compliant.
  34. */
  35. # define DH_FLAG_FIPS_METHOD 0x0400
  36. /*
  37. * If this flag is set the operations normally disabled in FIPS mode are
  38. * permitted it is then the applications responsibility to ensure that the
  39. * usage is compliant.
  40. */
  41. # define DH_FLAG_NON_FIPS_ALLOW 0x0400
  42. /* Already defined in ossl_typ.h */
  43. /* typedef struct dh_st DH; */
  44. /* typedef struct dh_method DH_METHOD; */
  45. DECLARE_ASN1_ITEM(DHparams)
  46. # define DH_GENERATOR_2 2
  47. /* #define DH_GENERATOR_3 3 */
  48. # define DH_GENERATOR_5 5
  49. /* DH_check error codes */
  50. # define DH_CHECK_P_NOT_PRIME 0x01
  51. # define DH_CHECK_P_NOT_SAFE_PRIME 0x02
  52. # define DH_UNABLE_TO_CHECK_GENERATOR 0x04
  53. # define DH_NOT_SUITABLE_GENERATOR 0x08
  54. # define DH_CHECK_Q_NOT_PRIME 0x10
  55. # define DH_CHECK_INVALID_Q_VALUE 0x20
  56. # define DH_CHECK_INVALID_J_VALUE 0x40
  57. /* DH_check_pub_key error codes */
  58. # define DH_CHECK_PUBKEY_TOO_SMALL 0x01
  59. # define DH_CHECK_PUBKEY_TOO_LARGE 0x02
  60. # define DH_CHECK_PUBKEY_INVALID 0x04
  61. /*
  62. * primes p where (p-1)/2 is prime too are called "safe"; we define this for
  63. * backward compatibility:
  64. */
  65. # define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME
  66. # define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \
  67. (char *(*)())d2i_DHparams,(fp),(unsigned char **)(x))
  68. # define i2d_DHparams_fp(fp,x) ASN1_i2d_fp(i2d_DHparams,(fp), \
  69. (unsigned char *)(x))
  70. # define d2i_DHparams_bio(bp,x) ASN1_d2i_bio_of(DH,DH_new,d2i_DHparams,bp,x)
  71. # define i2d_DHparams_bio(bp,x) ASN1_i2d_bio_of_const(DH,i2d_DHparams,bp,x)
  72. DH *DHparams_dup(DH *);
  73. const DH_METHOD *DH_OpenSSL(void);
  74. void DH_set_default_method(const DH_METHOD *meth);
  75. const DH_METHOD *DH_get_default_method(void);
  76. int DH_set_method(DH *dh, const DH_METHOD *meth);
  77. DH *DH_new_method(ENGINE *engine);
  78. DH *DH_new(void);
  79. void DH_free(DH *dh);
  80. int DH_up_ref(DH *dh);
  81. int DH_bits(const DH *dh);
  82. int DH_size(const DH *dh);
  83. int DH_security_bits(const DH *dh);
  84. #define DH_get_ex_new_index(l, p, newf, dupf, freef) \
  85. CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, l, p, newf, dupf, freef)
  86. int DH_set_ex_data(DH *d, int idx, void *arg);
  87. void *DH_get_ex_data(DH *d, int idx);
  88. /* Deprecated version */
  89. DEPRECATEDIN_0_9_8(DH *DH_generate_parameters(int prime_len, int generator,
  90. void (*callback) (int, int,
  91. void *),
  92. void *cb_arg))
  93. /* New version */
  94. int DH_generate_parameters_ex(DH *dh, int prime_len, int generator,
  95. BN_GENCB *cb);
  96. int DH_check(const DH *dh, int *codes);
  97. int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *codes);
  98. int DH_generate_key(DH *dh);
  99. int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
  100. int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh);
  101. DH *d2i_DHparams(DH **a, const unsigned char **pp, long length);
  102. int i2d_DHparams(const DH *a, unsigned char **pp);
  103. DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length);
  104. int i2d_DHxparams(const DH *a, unsigned char **pp);
  105. # ifndef OPENSSL_NO_STDIO
  106. int DHparams_print_fp(FILE *fp, const DH *x);
  107. # endif
  108. int DHparams_print(BIO *bp, const DH *x);
  109. /* RFC 5114 parameters */
  110. DH *DH_get_1024_160(void);
  111. DH *DH_get_2048_224(void);
  112. DH *DH_get_2048_256(void);
  113. # ifndef OPENSSL_NO_CMS
  114. /* RFC2631 KDF */
  115. int DH_KDF_X9_42(unsigned char *out, size_t outlen,
  116. const unsigned char *Z, size_t Zlen,
  117. ASN1_OBJECT *key_oid,
  118. const unsigned char *ukm, size_t ukmlen, const EVP_MD *md);
  119. # endif
  120. void DH_get0_pqg(const DH *dh, BIGNUM **p, BIGNUM **q, BIGNUM **g);
  121. int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
  122. void DH_get0_key(const DH *dh, BIGNUM **pub_key, BIGNUM **priv_key);
  123. int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key);
  124. void DH_clear_flags(DH *dh, int flags);
  125. int DH_test_flags(const DH *dh, int flags);
  126. void DH_set_flags(DH *dh, int flags);
  127. ENGINE *DH_get0_engine(DH *d);
  128. long DH_get_length(const DH *dh);
  129. int DH_set_length(DH *dh, long length);
  130. DH_METHOD *DH_meth_new(const char *name, int flags);
  131. void DH_meth_free(DH_METHOD *dhm);
  132. DH_METHOD *DH_meth_dup(const DH_METHOD *dhm);
  133. const char *DH_meth_get0_name(const DH_METHOD *dhm);
  134. int DH_meth_set1_name(DH_METHOD *dhm, const char *name);
  135. int DH_meth_get_flags(DH_METHOD *dhm);
  136. int DH_meth_set_flags(DH_METHOD *dhm, int flags);
  137. void *DH_meth_get0_app_data(const DH_METHOD *dhm);
  138. int DH_meth_set0_app_data(DH_METHOD *dhm, void *app_data);
  139. int (*DH_meth_get_generate_key(const DH_METHOD *dhm)) (DH *);
  140. int DH_meth_set_generate_key(DH_METHOD *dhm, int (*generate_key) (DH *));
  141. int (*DH_meth_get_compute_key(const DH_METHOD *dhm))
  142. (unsigned char *key, const BIGNUM *pub_key, DH *dh);
  143. int DH_meth_set_compute_key(DH_METHOD *dhm,
  144. int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh));
  145. int (*DH_meth_get_bn_mod_exp(const DH_METHOD *dhm))
  146. (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *,
  147. BN_CTX *, BN_MONT_CTX *);
  148. int DH_meth_set_bn_mod_exp(DH_METHOD *dhm,
  149. int (*bn_mod_exp) (const DH *, BIGNUM *, const BIGNUM *, const BIGNUM *,
  150. const BIGNUM *, BN_CTX *, BN_MONT_CTX *));
  151. int (*DH_meth_get_init(const DH_METHOD *dhm))(DH *);
  152. int DH_meth_set_init(DH_METHOD *dhm, int (*init)(DH *));
  153. int (*DH_meth_get_finish(const DH_METHOD *dhm)) (DH *);
  154. int DH_meth_set_finish(DH_METHOD *dhm, int (*finish) (DH *));
  155. int (*DH_meth_get_generate_params(const DH_METHOD *dhm))
  156. (DH *, int, int, BN_GENCB *);
  157. int DH_meth_set_generate_params(DH_METHOD *dhm,
  158. int (*generate_params) (DH *, int, int, BN_GENCB *));
  159. # define EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len) \
  160. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  161. EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, NULL)
  162. # define EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len) \
  163. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  164. EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, len, NULL)
  165. # define EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ) \
  166. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  167. EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, typ, NULL)
  168. # define EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen) \
  169. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
  170. EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL)
  171. # define EVP_PKEY_CTX_set_dh_rfc5114(ctx, gen) \
  172. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \
  173. EVP_PKEY_CTRL_DH_RFC5114, gen, NULL)
  174. # define EVP_PKEY_CTX_set_dhx_rfc5114(ctx, gen) \
  175. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \
  176. EVP_PKEY_CTRL_DH_RFC5114, gen, NULL)
  177. # define EVP_PKEY_CTX_set_dh_kdf_type(ctx, kdf) \
  178. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  179. EVP_PKEY_OP_DERIVE, \
  180. EVP_PKEY_CTRL_DH_KDF_TYPE, kdf, NULL)
  181. # define EVP_PKEY_CTX_get_dh_kdf_type(ctx) \
  182. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  183. EVP_PKEY_OP_DERIVE, \
  184. EVP_PKEY_CTRL_DH_KDF_TYPE, -2, NULL)
  185. # define EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, oid) \
  186. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  187. EVP_PKEY_OP_DERIVE, \
  188. EVP_PKEY_CTRL_DH_KDF_OID, 0, (void *)oid)
  189. # define EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, poid) \
  190. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  191. EVP_PKEY_OP_DERIVE, \
  192. EVP_PKEY_CTRL_GET_DH_KDF_OID, 0, (void *)poid)
  193. # define EVP_PKEY_CTX_set_dh_kdf_md(ctx, md) \
  194. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  195. EVP_PKEY_OP_DERIVE, \
  196. EVP_PKEY_CTRL_DH_KDF_MD, 0, (void *)md)
  197. # define EVP_PKEY_CTX_get_dh_kdf_md(ctx, pmd) \
  198. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  199. EVP_PKEY_OP_DERIVE, \
  200. EVP_PKEY_CTRL_GET_DH_KDF_MD, 0, (void *)pmd)
  201. # define EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, len) \
  202. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  203. EVP_PKEY_OP_DERIVE, \
  204. EVP_PKEY_CTRL_DH_KDF_OUTLEN, len, NULL)
  205. # define EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, plen) \
  206. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  207. EVP_PKEY_OP_DERIVE, \
  208. EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, 0, (void *)plen)
  209. # define EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p, plen) \
  210. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  211. EVP_PKEY_OP_DERIVE, \
  212. EVP_PKEY_CTRL_DH_KDF_UKM, plen, (void *)p)
  213. # define EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p) \
  214. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
  215. EVP_PKEY_OP_DERIVE, \
  216. EVP_PKEY_CTRL_GET_DH_KDF_UKM, 0, (void *)p)
  217. # define EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN (EVP_PKEY_ALG_CTRL + 1)
  218. # define EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR (EVP_PKEY_ALG_CTRL + 2)
  219. # define EVP_PKEY_CTRL_DH_RFC5114 (EVP_PKEY_ALG_CTRL + 3)
  220. # define EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN (EVP_PKEY_ALG_CTRL + 4)
  221. # define EVP_PKEY_CTRL_DH_PARAMGEN_TYPE (EVP_PKEY_ALG_CTRL + 5)
  222. # define EVP_PKEY_CTRL_DH_KDF_TYPE (EVP_PKEY_ALG_CTRL + 6)
  223. # define EVP_PKEY_CTRL_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 7)
  224. # define EVP_PKEY_CTRL_GET_DH_KDF_MD (EVP_PKEY_ALG_CTRL + 8)
  225. # define EVP_PKEY_CTRL_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 9)
  226. # define EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 10)
  227. # define EVP_PKEY_CTRL_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 11)
  228. # define EVP_PKEY_CTRL_GET_DH_KDF_UKM (EVP_PKEY_ALG_CTRL + 12)
  229. # define EVP_PKEY_CTRL_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 13)
  230. # define EVP_PKEY_CTRL_GET_DH_KDF_OID (EVP_PKEY_ALG_CTRL + 14)
  231. /* KDF types */
  232. # define EVP_PKEY_DH_KDF_NONE 1
  233. # ifndef OPENSSL_NO_CMS
  234. # define EVP_PKEY_DH_KDF_X9_42 2
  235. # endif
  236. /* BEGIN ERROR CODES */
  237. /*
  238. * The following lines are auto generated by the script mkerr.pl. Any changes
  239. * made after this point may be overwritten when the script is next run.
  240. */
  241. void ERR_load_DH_strings(void);
  242. /* Error codes for the DH functions. */
  243. /* Function codes. */
  244. # define DH_F_COMPUTE_KEY 102
  245. # define DH_F_DHPARAMS_PRINT_FP 101
  246. # define DH_F_DH_BUILTIN_GENPARAMS 106
  247. # define DH_F_DH_CMS_DECRYPT 114
  248. # define DH_F_DH_CMS_SET_PEERKEY 115
  249. # define DH_F_DH_CMS_SET_SHARED_INFO 116
  250. # define DH_F_DH_METH_DUP 117
  251. # define DH_F_DH_METH_NEW 118
  252. # define DH_F_DH_METH_SET1_NAME 119
  253. # define DH_F_DH_NEW_METHOD 105
  254. # define DH_F_DH_PARAM_DECODE 107
  255. # define DH_F_DH_PRIV_DECODE 110
  256. # define DH_F_DH_PRIV_ENCODE 111
  257. # define DH_F_DH_PUB_DECODE 108
  258. # define DH_F_DH_PUB_ENCODE 109
  259. # define DH_F_DO_DH_PRINT 100
  260. # define DH_F_GENERATE_KEY 103
  261. # define DH_F_GENERATE_PARAMETERS 104
  262. # define DH_F_PKEY_DH_DERIVE 112
  263. # define DH_F_PKEY_DH_KEYGEN 113
  264. /* Reason codes. */
  265. # define DH_R_BAD_GENERATOR 101
  266. # define DH_R_BN_DECODE_ERROR 109
  267. # define DH_R_BN_ERROR 106
  268. # define DH_R_DECODE_ERROR 104
  269. # define DH_R_INVALID_PUBKEY 102
  270. # define DH_R_KDF_PARAMETER_ERROR 112
  271. # define DH_R_KEYS_NOT_SET 108
  272. # define DH_R_KEY_SIZE_TOO_SMALL 110
  273. # define DH_R_MODULUS_TOO_LARGE 103
  274. # define DH_R_NO_PARAMETERS_SET 107
  275. # define DH_R_NO_PRIVATE_VALUE 100
  276. # define DH_R_PARAMETER_ENCODING_ERROR 105
  277. # define DH_R_PEER_KEY_ERROR 111
  278. # define DH_R_SHARED_INFO_ERROR 113
  279. # ifdef __cplusplus
  280. }
  281. # endif
  282. # endif
  283. #endif