155714Skris/* crypto/dh/dh.h */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296341Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296341Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296341Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296341Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296341Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#ifndef HEADER_DH_H
60296341Sdelphij# define HEADER_DH_H
6155714Skris
62296341Sdelphij# include <openssl/e_os2.h>
63160814Ssimon
64296341Sdelphij# ifdef OPENSSL_NO_DH
65296341Sdelphij#  error DH is disabled.
66296341Sdelphij# endif
6755714Skris
68296341Sdelphij# ifndef OPENSSL_NO_BIO
69296341Sdelphij#  include <openssl/bio.h>
70296341Sdelphij# endif
71296341Sdelphij# include <openssl/ossl_typ.h>
72296341Sdelphij# ifndef OPENSSL_NO_DEPRECATED
73296341Sdelphij#  include <openssl/bn.h>
74296341Sdelphij# endif
75162911Ssimon
76296341Sdelphij# ifndef OPENSSL_DH_MAX_MODULUS_BITS
77296341Sdelphij#  define OPENSSL_DH_MAX_MODULUS_BITS    10000
78296341Sdelphij# endif
7955714Skris
80296341Sdelphij# define DH_FLAG_CACHE_MONT_P     0x01
81296341Sdelphij
82296341Sdelphij/*
83296341Sdelphij * new with 0.9.7h; the built-in DH
84296341Sdelphij * implementation now uses constant time
85296341Sdelphij * modular exponentiation for secret exponents
86296341Sdelphij * by default. This flag causes the
87296341Sdelphij * faster variable sliding window method to
88296341Sdelphij * be used for all exponents.
89238405Sjkim */
90296341Sdelphij# define DH_FLAG_NO_EXP_CONSTTIME 0x02
91238405Sjkim
92296341Sdelphij/*
93296341Sdelphij * If this flag is set the DH method is FIPS compliant and can be used in
94296341Sdelphij * FIPS mode. This is set in the validated module method. If an application
95296341Sdelphij * sets this flag in its own methods it is its reposibility to ensure the
96296341Sdelphij * result is compliant.
97296341Sdelphij */
98238405Sjkim
99296341Sdelphij# define DH_FLAG_FIPS_METHOD                     0x0400
100296341Sdelphij
101296341Sdelphij/*
102296341Sdelphij * If this flag is set the operations normally disabled in FIPS mode are
103238405Sjkim * permitted it is then the applications responsibility to ensure that the
104238405Sjkim * usage is compliant.
105238405Sjkim */
106238405Sjkim
107296341Sdelphij# define DH_FLAG_NON_FIPS_ALLOW                  0x0400
108238405Sjkim
10968651Skris#ifdef  __cplusplus
11068651Skrisextern "C" {
11168651Skris#endif
11268651Skris
113160814Ssimon/* Already defined in ossl_typ.h */
114160814Ssimon/* typedef struct dh_st DH; */
115160814Ssimon/* typedef struct dh_method DH_METHOD; */
11659191Skris
117296341Sdelphijstruct dh_method {
118296341Sdelphij    const char *name;
119296341Sdelphij    /* Methods here */
120296341Sdelphij    int (*generate_key) (DH *dh);
121296341Sdelphij    int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh);
122296341Sdelphij    /* Can be null */
123296341Sdelphij    int (*bn_mod_exp) (const DH *dh, BIGNUM *r, const BIGNUM *a,
124296341Sdelphij                       const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
125296341Sdelphij                       BN_MONT_CTX *m_ctx);
126296341Sdelphij    int (*init) (DH *dh);
127296341Sdelphij    int (*finish) (DH *dh);
128296341Sdelphij    int flags;
129296341Sdelphij    char *app_data;
130296341Sdelphij    /* If this is non-NULL, it will be used to generate parameters */
131296341Sdelphij    int (*generate_params) (DH *dh, int prime_len, int generator,
132296341Sdelphij                            BN_GENCB *cb);
133296341Sdelphij};
13459191Skris
135296341Sdelphijstruct dh_st {
136296341Sdelphij    /*
137296341Sdelphij     * This first argument is used to pick up errors when a DH is passed
138296341Sdelphij     * instead of a EVP_PKEY
139296341Sdelphij     */
140296341Sdelphij    int pad;
141296341Sdelphij    int version;
142296341Sdelphij    BIGNUM *p;
143296341Sdelphij    BIGNUM *g;
144296341Sdelphij    long length;                /* optional */
145296341Sdelphij    BIGNUM *pub_key;            /* g^x */
146296341Sdelphij    BIGNUM *priv_key;           /* x */
147296341Sdelphij    int flags;
148296341Sdelphij    BN_MONT_CTX *method_mont_p;
149296341Sdelphij    /* Place holders if we want to do X9.42 DH */
150296341Sdelphij    BIGNUM *q;
151296341Sdelphij    BIGNUM *j;
152296341Sdelphij    unsigned char *seed;
153296341Sdelphij    int seedlen;
154296341Sdelphij    BIGNUM *counter;
155296341Sdelphij    int references;
156296341Sdelphij    CRYPTO_EX_DATA ex_data;
157296341Sdelphij    const DH_METHOD *meth;
158296341Sdelphij    ENGINE *engine;
159296341Sdelphij};
16059191Skris
161296341Sdelphij# define DH_GENERATOR_2          2
162296341Sdelphij/* #define DH_GENERATOR_3       3 */
163296341Sdelphij# define DH_GENERATOR_5          5
16455714Skris
16555714Skris/* DH_check error codes */
166296341Sdelphij# define DH_CHECK_P_NOT_PRIME            0x01
167296341Sdelphij# define DH_CHECK_P_NOT_SAFE_PRIME       0x02
168296341Sdelphij# define DH_UNABLE_TO_CHECK_GENERATOR    0x04
169296341Sdelphij# define DH_NOT_SUITABLE_GENERATOR       0x08
17055714Skris
171160814Ssimon/* DH_check_pub_key error codes */
172296341Sdelphij# define DH_CHECK_PUBKEY_TOO_SMALL       0x01
173296341Sdelphij# define DH_CHECK_PUBKEY_TOO_LARGE       0x02
174160814Ssimon
175296341Sdelphij/*
176296341Sdelphij * primes p where (p-1)/2 is prime too are called "safe"; we define this for
177296341Sdelphij * backward compatibility:
178296341Sdelphij */
179296341Sdelphij# define DH_CHECK_P_NOT_STRONG_PRIME     DH_CHECK_P_NOT_SAFE_PRIME
18059191Skris
181296341Sdelphij# define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \
182296341Sdelphij                (char *(*)())d2i_DHparams,(fp),(unsigned char **)(x))
183296341Sdelphij# define i2d_DHparams_fp(fp,x) ASN1_i2d_fp(i2d_DHparams,(fp), \
184296341Sdelphij                (unsigned char *)(x))
185296341Sdelphij# define d2i_DHparams_bio(bp,x) ASN1_d2i_bio_of(DH,DH_new,d2i_DHparams,bp,x)
186296341Sdelphij# define i2d_DHparams_bio(bp,x) ASN1_i2d_bio_of_const(DH,i2d_DHparams,bp,x)
18755714Skris
188238405SjkimDH *DHparams_dup(DH *);
189238405Sjkim
190109998Smarkmconst DH_METHOD *DH_OpenSSL(void);
19159191Skris
192109998Smarkmvoid DH_set_default_method(const DH_METHOD *meth);
193109998Smarkmconst DH_METHOD *DH_get_default_method(void);
194109998Smarkmint DH_set_method(DH *dh, const DH_METHOD *meth);
195109998SmarkmDH *DH_new_method(ENGINE *engine);
19659191Skris
197296341SdelphijDH *DH_new(void);
198296341Sdelphijvoid DH_free(DH *dh);
199296341Sdelphijint DH_up_ref(DH *dh);
200296341Sdelphijint DH_size(const DH *dh);
20159191Skrisint DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
202296341Sdelphij                        CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
20359191Skrisint DH_set_ex_data(DH *d, int idx, void *arg);
20459191Skrisvoid *DH_get_ex_data(DH *d, int idx);
205160814Ssimon
206160814Ssimon/* Deprecated version */
207296341Sdelphij# ifndef OPENSSL_NO_DEPRECATED
208296341SdelphijDH *DH_generate_parameters(int prime_len, int generator,
209296341Sdelphij                           void (*callback) (int, int, void *), void *cb_arg);
210296341Sdelphij# endif                         /* !defined(OPENSSL_NO_DEPRECATED) */
211160814Ssimon
212160814Ssimon/* New version */
213296341Sdelphijint DH_generate_parameters_ex(DH *dh, int prime_len, int generator,
214296341Sdelphij                              BN_GENCB *cb);
215160814Ssimon
216296341Sdelphijint DH_check(const DH *dh, int *codes);
217296341Sdelphijint DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *codes);
218296341Sdelphijint DH_generate_key(DH *dh);
219296341Sdelphijint DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
220296341SdelphijDH *d2i_DHparams(DH **a, const unsigned char **pp, long length);
221296341Sdelphijint i2d_DHparams(const DH *a, unsigned char **pp);
222296341Sdelphij# ifndef OPENSSL_NO_FP_API
223296341Sdelphijint DHparams_print_fp(FILE *fp, const DH *x);
224296341Sdelphij# endif
225296341Sdelphij# ifndef OPENSSL_NO_BIO
226296341Sdelphijint DHparams_print(BIO *bp, const DH *x);
227296341Sdelphij# else
228296341Sdelphijint DHparams_print(char *bp, const DH *x);
229296341Sdelphij# endif
23055714Skris
231296341Sdelphij# define EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len) \
232296341Sdelphij        EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
233296341Sdelphij                        EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, NULL)
234238405Sjkim
235296341Sdelphij# define EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen) \
236296341Sdelphij        EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
237296341Sdelphij                        EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL)
238238405Sjkim
239296341Sdelphij# define EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN     (EVP_PKEY_ALG_CTRL + 1)
240296341Sdelphij# define EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR     (EVP_PKEY_ALG_CTRL + 2)
241238405Sjkim
24255714Skris/* BEGIN ERROR CODES */
243296341Sdelphij/*
244296341Sdelphij * The following lines are auto generated by the script mkerr.pl. Any changes
24555714Skris * made after this point may be overwritten when the script is next run.
24655714Skris */
24789837Skrisvoid ERR_load_DH_strings(void);
24855714Skris
24955714Skris/* Error codes for the DH functions. */
25055714Skris
25155714Skris/* Function codes. */
252296341Sdelphij# define DH_F_COMPUTE_KEY                                 102
253296341Sdelphij# define DH_F_DHPARAMS_PRINT_FP                           101
254296341Sdelphij# define DH_F_DH_BUILTIN_GENPARAMS                        106
255296341Sdelphij# define DH_F_DH_COMPUTE_KEY                              114
256296341Sdelphij# define DH_F_DH_GENERATE_KEY                             115
257296341Sdelphij# define DH_F_DH_GENERATE_PARAMETERS_EX                   116
258296341Sdelphij# define DH_F_DH_NEW_METHOD                               105
259296341Sdelphij# define DH_F_DH_PARAM_DECODE                             107
260296341Sdelphij# define DH_F_DH_PRIV_DECODE                              110
261296341Sdelphij# define DH_F_DH_PRIV_ENCODE                              111
262296341Sdelphij# define DH_F_DH_PUB_DECODE                               108
263296341Sdelphij# define DH_F_DH_PUB_ENCODE                               109
264296341Sdelphij# define DH_F_DO_DH_PRINT                                 100
265296341Sdelphij# define DH_F_GENERATE_KEY                                103
266296341Sdelphij# define DH_F_GENERATE_PARAMETERS                         104
267296341Sdelphij# define DH_F_PKEY_DH_DERIVE                              112
268296341Sdelphij# define DH_F_PKEY_DH_KEYGEN                              113
26955714Skris
27055714Skris/* Reason codes. */
271296341Sdelphij# define DH_R_BAD_GENERATOR                               101
272296341Sdelphij# define DH_R_BN_DECODE_ERROR                             109
273296341Sdelphij# define DH_R_BN_ERROR                                    106
274296341Sdelphij# define DH_R_DECODE_ERROR                                104
275296341Sdelphij# define DH_R_INVALID_PUBKEY                              102
276296341Sdelphij# define DH_R_KEYS_NOT_SET                                108
277296341Sdelphij# define DH_R_KEY_SIZE_TOO_SMALL                          110
278296341Sdelphij# define DH_R_MODULUS_TOO_LARGE                           103
279296341Sdelphij# define DH_R_NON_FIPS_METHOD                             111
280296341Sdelphij# define DH_R_NO_PARAMETERS_SET                           107
281296341Sdelphij# define DH_R_NO_PRIVATE_VALUE                            100
282296341Sdelphij# define DH_R_PARAMETER_ENCODING_ERROR                    105
28355714Skris
28455714Skris#ifdef  __cplusplus
28555714Skris}
28655714Skris#endif
28755714Skris#endif
288