diff options
Diffstat (limited to 'src/lib/libcrypto/dsa')
| -rw-r--r-- | src/lib/libcrypto/dsa/Makefile | 7 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa.h | 307 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_ameth.c | 704 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_asn1.c | 150 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_depr.c | 106 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_err.c | 125 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_gen.c | 344 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_key.c | 128 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_lib.c | 311 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_locl.h | 60 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_ossl.c | 398 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_pmeth.c | 318 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_prn.c | 121 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_sign.c (renamed from src/lib/libcrypto/dsa/dsa_utl.c) | 45 | ||||
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_vrf.c | 89 |
15 files changed, 3184 insertions, 29 deletions
diff --git a/src/lib/libcrypto/dsa/Makefile b/src/lib/libcrypto/dsa/Makefile index 5fef4ca5ad..8073c4ecfe 100644 --- a/src/lib/libcrypto/dsa/Makefile +++ b/src/lib/libcrypto/dsa/Makefile | |||
| @@ -99,9 +99,8 @@ dsa_asn1.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | |||
| 99 | dsa_asn1.o: ../../include/openssl/err.h ../../include/openssl/lhash.h | 99 | dsa_asn1.o: ../../include/openssl/err.h ../../include/openssl/lhash.h |
| 100 | dsa_asn1.o: ../../include/openssl/opensslconf.h | 100 | dsa_asn1.o: ../../include/openssl/opensslconf.h |
| 101 | dsa_asn1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | 101 | dsa_asn1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h |
| 102 | dsa_asn1.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h | 102 | dsa_asn1.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h |
| 103 | dsa_asn1.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | 103 | dsa_asn1.o: ../../include/openssl/symhacks.h ../cryptlib.h dsa_asn1.c |
| 104 | dsa_asn1.o: ../cryptlib.h dsa_asn1.c | ||
| 105 | dsa_depr.o: ../../e_os.h ../../include/openssl/asn1.h | 104 | dsa_depr.o: ../../e_os.h ../../include/openssl/asn1.h |
| 106 | dsa_depr.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | 105 | dsa_depr.o: ../../include/openssl/bio.h ../../include/openssl/bn.h |
| 107 | dsa_depr.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | 106 | dsa_depr.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h |
| @@ -190,7 +189,7 @@ dsa_prn.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | |||
| 190 | dsa_prn.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h | 189 | dsa_prn.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h |
| 191 | dsa_prn.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | 190 | dsa_prn.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h |
| 192 | dsa_prn.o: ../cryptlib.h dsa_prn.c | 191 | dsa_prn.o: ../cryptlib.h dsa_prn.c |
| 193 | dsa_sign.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h | 192 | dsa_sign.o: ../../e_os.h ../../include/openssl/bio.h |
| 194 | dsa_sign.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | 193 | dsa_sign.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h |
| 195 | dsa_sign.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | 194 | dsa_sign.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h |
| 196 | dsa_sign.o: ../../include/openssl/err.h ../../include/openssl/lhash.h | 195 | dsa_sign.o: ../../include/openssl/err.h ../../include/openssl/lhash.h |
diff --git a/src/lib/libcrypto/dsa/dsa.h b/src/lib/libcrypto/dsa/dsa.h new file mode 100644 index 0000000000..ac50a5c846 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa.h | |||
| @@ -0,0 +1,307 @@ | |||
| 1 | /* crypto/dsa/dsa.h */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* | ||
| 60 | * The DSS routines are based on patches supplied by | ||
| 61 | * Steven Schoch <schoch@sheba.arc.nasa.gov>. He basically did the | ||
| 62 | * work and I have just tweaked them a little to fit into my | ||
| 63 | * stylistic vision for SSLeay :-) */ | ||
| 64 | |||
| 65 | #ifndef HEADER_DSA_H | ||
| 66 | #define HEADER_DSA_H | ||
| 67 | |||
| 68 | #include <openssl/e_os2.h> | ||
| 69 | |||
| 70 | #ifdef OPENSSL_NO_DSA | ||
| 71 | #error DSA is disabled. | ||
| 72 | #endif | ||
| 73 | |||
| 74 | #ifndef OPENSSL_NO_BIO | ||
| 75 | #include <openssl/bio.h> | ||
| 76 | #endif | ||
| 77 | #include <openssl/crypto.h> | ||
| 78 | #include <openssl/ossl_typ.h> | ||
| 79 | |||
| 80 | #ifndef OPENSSL_NO_DEPRECATED | ||
| 81 | #include <openssl/bn.h> | ||
| 82 | #ifndef OPENSSL_NO_DH | ||
| 83 | # include <openssl/dh.h> | ||
| 84 | #endif | ||
| 85 | #endif | ||
| 86 | |||
| 87 | #ifndef OPENSSL_DSA_MAX_MODULUS_BITS | ||
| 88 | # define OPENSSL_DSA_MAX_MODULUS_BITS 10000 | ||
| 89 | #endif | ||
| 90 | |||
| 91 | #define DSA_FLAG_CACHE_MONT_P 0x01 | ||
| 92 | #define DSA_FLAG_NO_EXP_CONSTTIME 0x02 /* new with 0.9.7h; the built-in DSA | ||
| 93 | * implementation now uses constant time | ||
| 94 | * modular exponentiation for secret exponents | ||
| 95 | * by default. This flag causes the | ||
| 96 | * faster variable sliding window method to | ||
| 97 | * be used for all exponents. | ||
| 98 | */ | ||
| 99 | |||
| 100 | #ifdef __cplusplus | ||
| 101 | extern "C" { | ||
| 102 | #endif | ||
| 103 | |||
| 104 | /* Already defined in ossl_typ.h */ | ||
| 105 | /* typedef struct dsa_st DSA; */ | ||
| 106 | /* typedef struct dsa_method DSA_METHOD; */ | ||
| 107 | |||
| 108 | typedef struct DSA_SIG_st | ||
| 109 | { | ||
| 110 | BIGNUM *r; | ||
| 111 | BIGNUM *s; | ||
| 112 | } DSA_SIG; | ||
| 113 | |||
| 114 | struct dsa_method | ||
| 115 | { | ||
| 116 | const char *name; | ||
| 117 | DSA_SIG * (*dsa_do_sign)(const unsigned char *dgst, int dlen, DSA *dsa); | ||
| 118 | int (*dsa_sign_setup)(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, | ||
| 119 | BIGNUM **rp); | ||
| 120 | int (*dsa_do_verify)(const unsigned char *dgst, int dgst_len, | ||
| 121 | DSA_SIG *sig, DSA *dsa); | ||
| 122 | int (*dsa_mod_exp)(DSA *dsa, BIGNUM *rr, BIGNUM *a1, BIGNUM *p1, | ||
| 123 | BIGNUM *a2, BIGNUM *p2, BIGNUM *m, BN_CTX *ctx, | ||
| 124 | BN_MONT_CTX *in_mont); | ||
| 125 | int (*bn_mod_exp)(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p, | ||
| 126 | const BIGNUM *m, BN_CTX *ctx, | ||
| 127 | BN_MONT_CTX *m_ctx); /* Can be null */ | ||
| 128 | int (*init)(DSA *dsa); | ||
| 129 | int (*finish)(DSA *dsa); | ||
| 130 | int flags; | ||
| 131 | char *app_data; | ||
| 132 | /* If this is non-NULL, it is used to generate DSA parameters */ | ||
| 133 | int (*dsa_paramgen)(DSA *dsa, int bits, | ||
| 134 | const unsigned char *seed, int seed_len, | ||
| 135 | int *counter_ret, unsigned long *h_ret, | ||
| 136 | BN_GENCB *cb); | ||
| 137 | /* If this is non-NULL, it is used to generate DSA keys */ | ||
| 138 | int (*dsa_keygen)(DSA *dsa); | ||
| 139 | }; | ||
| 140 | |||
| 141 | struct dsa_st | ||
| 142 | { | ||
| 143 | /* This first variable is used to pick up errors where | ||
| 144 | * a DSA is passed instead of of a EVP_PKEY */ | ||
| 145 | int pad; | ||
| 146 | long version; | ||
| 147 | int write_params; | ||
| 148 | BIGNUM *p; | ||
| 149 | BIGNUM *q; /* == 20 */ | ||
| 150 | BIGNUM *g; | ||
| 151 | |||
| 152 | BIGNUM *pub_key; /* y public key */ | ||
| 153 | BIGNUM *priv_key; /* x private key */ | ||
| 154 | |||
| 155 | BIGNUM *kinv; /* Signing pre-calc */ | ||
| 156 | BIGNUM *r; /* Signing pre-calc */ | ||
| 157 | |||
| 158 | int flags; | ||
| 159 | /* Normally used to cache montgomery values */ | ||
| 160 | BN_MONT_CTX *method_mont_p; | ||
| 161 | int references; | ||
| 162 | CRYPTO_EX_DATA ex_data; | ||
| 163 | const DSA_METHOD *meth; | ||
| 164 | /* functional reference if 'meth' is ENGINE-provided */ | ||
| 165 | ENGINE *engine; | ||
| 166 | }; | ||
| 167 | |||
| 168 | #define d2i_DSAparams_fp(fp,x) (DSA *)ASN1_d2i_fp((char *(*)())DSA_new, \ | ||
| 169 | (char *(*)())d2i_DSAparams,(fp),(unsigned char **)(x)) | ||
| 170 | #define i2d_DSAparams_fp(fp,x) ASN1_i2d_fp(i2d_DSAparams,(fp), \ | ||
| 171 | (unsigned char *)(x)) | ||
| 172 | #define d2i_DSAparams_bio(bp,x) ASN1_d2i_bio_of(DSA,DSA_new,d2i_DSAparams,bp,x) | ||
| 173 | #define i2d_DSAparams_bio(bp,x) ASN1_i2d_bio_of_const(DSA,i2d_DSAparams,bp,x) | ||
| 174 | |||
| 175 | |||
| 176 | DSA *DSAparams_dup(DSA *x); | ||
| 177 | DSA_SIG * DSA_SIG_new(void); | ||
| 178 | void DSA_SIG_free(DSA_SIG *a); | ||
| 179 | int i2d_DSA_SIG(const DSA_SIG *a, unsigned char **pp); | ||
| 180 | DSA_SIG * d2i_DSA_SIG(DSA_SIG **v, const unsigned char **pp, long length); | ||
| 181 | |||
| 182 | DSA_SIG * DSA_do_sign(const unsigned char *dgst,int dlen,DSA *dsa); | ||
| 183 | int DSA_do_verify(const unsigned char *dgst,int dgst_len, | ||
| 184 | DSA_SIG *sig,DSA *dsa); | ||
| 185 | |||
| 186 | const DSA_METHOD *DSA_OpenSSL(void); | ||
| 187 | |||
| 188 | void DSA_set_default_method(const DSA_METHOD *); | ||
| 189 | const DSA_METHOD *DSA_get_default_method(void); | ||
| 190 | int DSA_set_method(DSA *dsa, const DSA_METHOD *); | ||
| 191 | |||
| 192 | DSA * DSA_new(void); | ||
| 193 | DSA * DSA_new_method(ENGINE *engine); | ||
| 194 | void DSA_free (DSA *r); | ||
| 195 | /* "up" the DSA object's reference count */ | ||
| 196 | int DSA_up_ref(DSA *r); | ||
| 197 | int DSA_size(const DSA *); | ||
| 198 | /* next 4 return -1 on error */ | ||
| 199 | int DSA_sign_setup( DSA *dsa,BN_CTX *ctx_in,BIGNUM **kinvp,BIGNUM **rp); | ||
| 200 | int DSA_sign(int type,const unsigned char *dgst,int dlen, | ||
| 201 | unsigned char *sig, unsigned int *siglen, DSA *dsa); | ||
| 202 | int DSA_verify(int type,const unsigned char *dgst,int dgst_len, | ||
| 203 | const unsigned char *sigbuf, int siglen, DSA *dsa); | ||
| 204 | int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 205 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); | ||
| 206 | int DSA_set_ex_data(DSA *d, int idx, void *arg); | ||
| 207 | void *DSA_get_ex_data(DSA *d, int idx); | ||
| 208 | |||
| 209 | DSA * d2i_DSAPublicKey(DSA **a, const unsigned char **pp, long length); | ||
| 210 | DSA * d2i_DSAPrivateKey(DSA **a, const unsigned char **pp, long length); | ||
| 211 | DSA * d2i_DSAparams(DSA **a, const unsigned char **pp, long length); | ||
| 212 | |||
| 213 | /* Deprecated version */ | ||
| 214 | #ifndef OPENSSL_NO_DEPRECATED | ||
| 215 | DSA * DSA_generate_parameters(int bits, | ||
| 216 | unsigned char *seed,int seed_len, | ||
| 217 | int *counter_ret, unsigned long *h_ret,void | ||
| 218 | (*callback)(int, int, void *),void *cb_arg); | ||
| 219 | #endif /* !defined(OPENSSL_NO_DEPRECATED) */ | ||
| 220 | |||
| 221 | /* New version */ | ||
| 222 | int DSA_generate_parameters_ex(DSA *dsa, int bits, | ||
| 223 | const unsigned char *seed,int seed_len, | ||
| 224 | int *counter_ret, unsigned long *h_ret, BN_GENCB *cb); | ||
| 225 | |||
| 226 | int DSA_generate_key(DSA *a); | ||
| 227 | int i2d_DSAPublicKey(const DSA *a, unsigned char **pp); | ||
| 228 | int i2d_DSAPrivateKey(const DSA *a, unsigned char **pp); | ||
| 229 | int i2d_DSAparams(const DSA *a,unsigned char **pp); | ||
| 230 | |||
| 231 | #ifndef OPENSSL_NO_BIO | ||
| 232 | int DSAparams_print(BIO *bp, const DSA *x); | ||
| 233 | int DSA_print(BIO *bp, const DSA *x, int off); | ||
| 234 | #endif | ||
| 235 | #ifndef OPENSSL_NO_FP_API | ||
| 236 | int DSAparams_print_fp(FILE *fp, const DSA *x); | ||
| 237 | int DSA_print_fp(FILE *bp, const DSA *x, int off); | ||
| 238 | #endif | ||
| 239 | |||
| 240 | #define DSS_prime_checks 50 | ||
| 241 | /* Primality test according to FIPS PUB 186[-1], Appendix 2.1: | ||
| 242 | * 50 rounds of Rabin-Miller */ | ||
| 243 | #define DSA_is_prime(n, callback, cb_arg) \ | ||
| 244 | BN_is_prime(n, DSS_prime_checks, callback, NULL, cb_arg) | ||
| 245 | |||
| 246 | #ifndef OPENSSL_NO_DH | ||
| 247 | /* Convert DSA structure (key or just parameters) into DH structure | ||
| 248 | * (be careful to avoid small subgroup attacks when using this!) */ | ||
| 249 | DH *DSA_dup_DH(const DSA *r); | ||
| 250 | #endif | ||
| 251 | |||
| 252 | #define EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits) \ | ||
| 253 | EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN, \ | ||
| 254 | EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, nbits, NULL) | ||
| 255 | |||
| 256 | #define EVP_PKEY_CTRL_DSA_PARAMGEN_BITS (EVP_PKEY_ALG_CTRL + 1) | ||
| 257 | #define EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS (EVP_PKEY_ALG_CTRL + 2) | ||
| 258 | #define EVP_PKEY_CTRL_DSA_PARAMGEN_MD (EVP_PKEY_ALG_CTRL + 3) | ||
| 259 | |||
| 260 | /* BEGIN ERROR CODES */ | ||
| 261 | /* The following lines are auto generated by the script mkerr.pl. Any changes | ||
| 262 | * made after this point may be overwritten when the script is next run. | ||
| 263 | */ | ||
| 264 | void ERR_load_DSA_strings(void); | ||
| 265 | |||
| 266 | /* Error codes for the DSA functions. */ | ||
| 267 | |||
| 268 | /* Function codes. */ | ||
| 269 | #define DSA_F_D2I_DSA_SIG 110 | ||
| 270 | #define DSA_F_DO_DSA_PRINT 104 | ||
| 271 | #define DSA_F_DSAPARAMS_PRINT 100 | ||
| 272 | #define DSA_F_DSAPARAMS_PRINT_FP 101 | ||
| 273 | #define DSA_F_DSA_DO_SIGN 112 | ||
| 274 | #define DSA_F_DSA_DO_VERIFY 113 | ||
| 275 | #define DSA_F_DSA_NEW_METHOD 103 | ||
| 276 | #define DSA_F_DSA_PARAM_DECODE 119 | ||
| 277 | #define DSA_F_DSA_PRINT_FP 105 | ||
| 278 | #define DSA_F_DSA_PRIV_DECODE 115 | ||
| 279 | #define DSA_F_DSA_PRIV_ENCODE 116 | ||
| 280 | #define DSA_F_DSA_PUB_DECODE 117 | ||
| 281 | #define DSA_F_DSA_PUB_ENCODE 118 | ||
| 282 | #define DSA_F_DSA_SIGN 106 | ||
| 283 | #define DSA_F_DSA_SIGN_SETUP 107 | ||
| 284 | #define DSA_F_DSA_SIG_NEW 109 | ||
| 285 | #define DSA_F_DSA_VERIFY 108 | ||
| 286 | #define DSA_F_I2D_DSA_SIG 111 | ||
| 287 | #define DSA_F_OLD_DSA_PRIV_DECODE 122 | ||
| 288 | #define DSA_F_PKEY_DSA_CTRL 120 | ||
| 289 | #define DSA_F_PKEY_DSA_KEYGEN 121 | ||
| 290 | #define DSA_F_SIG_CB 114 | ||
| 291 | |||
| 292 | /* Reason codes. */ | ||
| 293 | #define DSA_R_BAD_Q_VALUE 102 | ||
| 294 | #define DSA_R_BN_DECODE_ERROR 108 | ||
| 295 | #define DSA_R_BN_ERROR 109 | ||
| 296 | #define DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 100 | ||
| 297 | #define DSA_R_DECODE_ERROR 104 | ||
| 298 | #define DSA_R_INVALID_DIGEST_TYPE 106 | ||
| 299 | #define DSA_R_MISSING_PARAMETERS 101 | ||
| 300 | #define DSA_R_MODULUS_TOO_LARGE 103 | ||
| 301 | #define DSA_R_NO_PARAMETERS_SET 107 | ||
| 302 | #define DSA_R_PARAMETER_ENCODING_ERROR 105 | ||
| 303 | |||
| 304 | #ifdef __cplusplus | ||
| 305 | } | ||
| 306 | #endif | ||
| 307 | #endif | ||
diff --git a/src/lib/libcrypto/dsa/dsa_ameth.c b/src/lib/libcrypto/dsa/dsa_ameth.c new file mode 100644 index 0000000000..376156ec5e --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_ameth.c | |||
| @@ -0,0 +1,704 @@ | |||
| 1 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | ||
| 2 | * project 2006. | ||
| 3 | */ | ||
| 4 | /* ==================================================================== | ||
| 5 | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * | ||
| 14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 15 | * notice, this list of conditions and the following disclaimer in | ||
| 16 | * the documentation and/or other materials provided with the | ||
| 17 | * distribution. | ||
| 18 | * | ||
| 19 | * 3. All advertising materials mentioning features or use of this | ||
| 20 | * software must display the following acknowledgment: | ||
| 21 | * "This product includes software developed by the OpenSSL Project | ||
| 22 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 23 | * | ||
| 24 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 25 | * endorse or promote products derived from this software without | ||
| 26 | * prior written permission. For written permission, please contact | ||
| 27 | * licensing@OpenSSL.org. | ||
| 28 | * | ||
| 29 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 30 | * nor may "OpenSSL" appear in their names without prior written | ||
| 31 | * permission of the OpenSSL Project. | ||
| 32 | * | ||
| 33 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 34 | * acknowledgment: | ||
| 35 | * "This product includes software developed by the OpenSSL Project | ||
| 36 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 37 | * | ||
| 38 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 39 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 41 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 42 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 47 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 48 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 49 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 50 | * ==================================================================== | ||
| 51 | * | ||
| 52 | * This product includes cryptographic software written by Eric Young | ||
| 53 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 54 | * Hudson (tjh@cryptsoft.com). | ||
| 55 | * | ||
| 56 | */ | ||
| 57 | |||
| 58 | #include <stdio.h> | ||
| 59 | #include "cryptlib.h" | ||
| 60 | #include <openssl/x509.h> | ||
| 61 | #include <openssl/asn1.h> | ||
| 62 | #include <openssl/dsa.h> | ||
| 63 | #include <openssl/bn.h> | ||
| 64 | #ifndef OPENSSL_NO_CMS | ||
| 65 | #include <openssl/cms.h> | ||
| 66 | #endif | ||
| 67 | #include "asn1_locl.h" | ||
| 68 | |||
| 69 | static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) | ||
| 70 | { | ||
| 71 | const unsigned char *p, *pm; | ||
| 72 | int pklen, pmlen; | ||
| 73 | int ptype; | ||
| 74 | void *pval; | ||
| 75 | ASN1_STRING *pstr; | ||
| 76 | X509_ALGOR *palg; | ||
| 77 | ASN1_INTEGER *public_key = NULL; | ||
| 78 | |||
| 79 | DSA *dsa = NULL; | ||
| 80 | |||
| 81 | if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) | ||
| 82 | return 0; | ||
| 83 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | ||
| 84 | |||
| 85 | |||
| 86 | if (ptype == V_ASN1_SEQUENCE) | ||
| 87 | { | ||
| 88 | pstr = pval; | ||
| 89 | pm = pstr->data; | ||
| 90 | pmlen = pstr->length; | ||
| 91 | |||
| 92 | if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen))) | ||
| 93 | { | ||
| 94 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR); | ||
| 95 | goto err; | ||
| 96 | } | ||
| 97 | |||
| 98 | } | ||
| 99 | else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) | ||
| 100 | { | ||
| 101 | if (!(dsa = DSA_new())) | ||
| 102 | { | ||
| 103 | DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE); | ||
| 104 | goto err; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | else | ||
| 108 | { | ||
| 109 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR); | ||
| 110 | goto err; | ||
| 111 | } | ||
| 112 | |||
| 113 | if (!(public_key=d2i_ASN1_INTEGER(NULL, &p, pklen))) | ||
| 114 | { | ||
| 115 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR); | ||
| 116 | goto err; | ||
| 117 | } | ||
| 118 | |||
| 119 | if (!(dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) | ||
| 120 | { | ||
| 121 | DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR); | ||
| 122 | goto err; | ||
| 123 | } | ||
| 124 | |||
| 125 | ASN1_INTEGER_free(public_key); | ||
| 126 | EVP_PKEY_assign_DSA(pkey, dsa); | ||
| 127 | return 1; | ||
| 128 | |||
| 129 | err: | ||
| 130 | if (public_key) | ||
| 131 | ASN1_INTEGER_free(public_key); | ||
| 132 | if (dsa) | ||
| 133 | DSA_free(dsa); | ||
| 134 | return 0; | ||
| 135 | |||
| 136 | } | ||
| 137 | |||
| 138 | static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) | ||
| 139 | { | ||
| 140 | DSA *dsa; | ||
| 141 | void *pval = NULL; | ||
| 142 | int ptype; | ||
| 143 | unsigned char *penc = NULL; | ||
| 144 | int penclen; | ||
| 145 | |||
| 146 | dsa=pkey->pkey.dsa; | ||
| 147 | if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) | ||
| 148 | { | ||
| 149 | ASN1_STRING *str; | ||
| 150 | str = ASN1_STRING_new(); | ||
| 151 | str->length = i2d_DSAparams(dsa, &str->data); | ||
| 152 | if (str->length <= 0) | ||
| 153 | { | ||
| 154 | DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); | ||
| 155 | goto err; | ||
| 156 | } | ||
| 157 | pval = str; | ||
| 158 | ptype = V_ASN1_SEQUENCE; | ||
| 159 | } | ||
| 160 | else | ||
| 161 | ptype = V_ASN1_UNDEF; | ||
| 162 | |||
| 163 | dsa->write_params=0; | ||
| 164 | |||
| 165 | penclen = i2d_DSAPublicKey(dsa, &penc); | ||
| 166 | |||
| 167 | if (penclen <= 0) | ||
| 168 | { | ||
| 169 | DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); | ||
| 170 | goto err; | ||
| 171 | } | ||
| 172 | |||
| 173 | if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA), | ||
| 174 | ptype, pval, penc, penclen)) | ||
| 175 | return 1; | ||
| 176 | |||
| 177 | err: | ||
| 178 | if (penc) | ||
| 179 | OPENSSL_free(penc); | ||
| 180 | if (pval) | ||
| 181 | ASN1_STRING_free(pval); | ||
| 182 | |||
| 183 | return 0; | ||
| 184 | } | ||
| 185 | |||
| 186 | /* In PKCS#8 DSA: you just get a private key integer and parameters in the | ||
| 187 | * AlgorithmIdentifier the pubkey must be recalculated. | ||
| 188 | */ | ||
| 189 | |||
| 190 | static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) | ||
| 191 | { | ||
| 192 | const unsigned char *p, *pm; | ||
| 193 | int pklen, pmlen; | ||
| 194 | int ptype; | ||
| 195 | void *pval; | ||
| 196 | ASN1_STRING *pstr; | ||
| 197 | X509_ALGOR *palg; | ||
| 198 | ASN1_INTEGER *privkey = NULL; | ||
| 199 | BN_CTX *ctx = NULL; | ||
| 200 | |||
| 201 | STACK_OF(ASN1_TYPE) *ndsa = NULL; | ||
| 202 | DSA *dsa = NULL; | ||
| 203 | |||
| 204 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) | ||
| 205 | return 0; | ||
| 206 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | ||
| 207 | |||
| 208 | /* Check for broken DSA PKCS#8, UGH! */ | ||
| 209 | if (*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) | ||
| 210 | { | ||
| 211 | ASN1_TYPE *t1, *t2; | ||
| 212 | if(!(ndsa = d2i_ASN1_SEQUENCE_ANY(NULL, &p, pklen))) | ||
| 213 | goto decerr; | ||
| 214 | if (sk_ASN1_TYPE_num(ndsa) != 2) | ||
| 215 | goto decerr; | ||
| 216 | /* Handle Two broken types: | ||
| 217 | * SEQUENCE {parameters, priv_key} | ||
| 218 | * SEQUENCE {pub_key, priv_key} | ||
| 219 | */ | ||
| 220 | |||
| 221 | t1 = sk_ASN1_TYPE_value(ndsa, 0); | ||
| 222 | t2 = sk_ASN1_TYPE_value(ndsa, 1); | ||
| 223 | if (t1->type == V_ASN1_SEQUENCE) | ||
| 224 | { | ||
| 225 | p8->broken = PKCS8_EMBEDDED_PARAM; | ||
| 226 | pval = t1->value.ptr; | ||
| 227 | } | ||
| 228 | else if (ptype == V_ASN1_SEQUENCE) | ||
| 229 | p8->broken = PKCS8_NS_DB; | ||
| 230 | else | ||
| 231 | goto decerr; | ||
| 232 | |||
| 233 | if (t2->type != V_ASN1_INTEGER) | ||
| 234 | goto decerr; | ||
| 235 | |||
| 236 | privkey = t2->value.integer; | ||
| 237 | } | ||
| 238 | else | ||
| 239 | { | ||
| 240 | const unsigned char *q = p; | ||
| 241 | if (!(privkey=d2i_ASN1_INTEGER(NULL, &p, pklen))) | ||
| 242 | goto decerr; | ||
| 243 | if (privkey->type == V_ASN1_NEG_INTEGER) | ||
| 244 | { | ||
| 245 | p8->broken = PKCS8_NEG_PRIVKEY; | ||
| 246 | ASN1_INTEGER_free(privkey); | ||
| 247 | if (!(privkey=d2i_ASN1_UINTEGER(NULL, &q, pklen))) | ||
| 248 | goto decerr; | ||
| 249 | } | ||
| 250 | if (ptype != V_ASN1_SEQUENCE) | ||
| 251 | goto decerr; | ||
| 252 | } | ||
| 253 | |||
| 254 | pstr = pval; | ||
| 255 | pm = pstr->data; | ||
| 256 | pmlen = pstr->length; | ||
| 257 | if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen))) | ||
| 258 | goto decerr; | ||
| 259 | /* We have parameters now set private key */ | ||
| 260 | if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) | ||
| 261 | { | ||
| 262 | DSAerr(DSA_F_DSA_PRIV_DECODE,DSA_R_BN_ERROR); | ||
| 263 | goto dsaerr; | ||
| 264 | } | ||
| 265 | /* Calculate public key */ | ||
| 266 | if (!(dsa->pub_key = BN_new())) | ||
| 267 | { | ||
| 268 | DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE); | ||
| 269 | goto dsaerr; | ||
| 270 | } | ||
| 271 | if (!(ctx = BN_CTX_new())) | ||
| 272 | { | ||
| 273 | DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE); | ||
| 274 | goto dsaerr; | ||
| 275 | } | ||
| 276 | |||
| 277 | if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) | ||
| 278 | { | ||
| 279 | DSAerr(DSA_F_DSA_PRIV_DECODE,DSA_R_BN_ERROR); | ||
| 280 | goto dsaerr; | ||
| 281 | } | ||
| 282 | |||
| 283 | EVP_PKEY_assign_DSA(pkey, dsa); | ||
| 284 | BN_CTX_free (ctx); | ||
| 285 | if(ndsa) | ||
| 286 | sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free); | ||
| 287 | else | ||
| 288 | ASN1_INTEGER_free(privkey); | ||
| 289 | |||
| 290 | return 1; | ||
| 291 | |||
| 292 | decerr: | ||
| 293 | DSAerr(DSA_F_DSA_PRIV_DECODE, EVP_R_DECODE_ERROR); | ||
| 294 | dsaerr: | ||
| 295 | BN_CTX_free (ctx); | ||
| 296 | if (privkey) | ||
| 297 | ASN1_INTEGER_free(privkey); | ||
| 298 | sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free); | ||
| 299 | DSA_free(dsa); | ||
| 300 | return 0; | ||
| 301 | } | ||
| 302 | |||
| 303 | static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) | ||
| 304 | { | ||
| 305 | ASN1_STRING *params = NULL; | ||
| 306 | ASN1_INTEGER *prkey = NULL; | ||
| 307 | unsigned char *dp = NULL; | ||
| 308 | int dplen; | ||
| 309 | |||
| 310 | params = ASN1_STRING_new(); | ||
| 311 | |||
| 312 | if (!params) | ||
| 313 | { | ||
| 314 | DSAerr(DSA_F_DSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE); | ||
| 315 | goto err; | ||
| 316 | } | ||
| 317 | |||
| 318 | params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data); | ||
| 319 | if (params->length <= 0) | ||
| 320 | { | ||
| 321 | DSAerr(DSA_F_DSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE); | ||
| 322 | goto err; | ||
| 323 | } | ||
| 324 | params->type = V_ASN1_SEQUENCE; | ||
| 325 | |||
| 326 | /* Get private key into integer */ | ||
| 327 | prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL); | ||
| 328 | |||
| 329 | if (!prkey) | ||
| 330 | { | ||
| 331 | DSAerr(DSA_F_DSA_PRIV_ENCODE,DSA_R_BN_ERROR); | ||
| 332 | goto err; | ||
| 333 | } | ||
| 334 | |||
| 335 | dplen = i2d_ASN1_INTEGER(prkey, &dp); | ||
| 336 | |||
| 337 | ASN1_INTEGER_free(prkey); | ||
| 338 | |||
| 339 | if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0, | ||
| 340 | V_ASN1_SEQUENCE, params, dp, dplen)) | ||
| 341 | goto err; | ||
| 342 | |||
| 343 | return 1; | ||
| 344 | |||
| 345 | err: | ||
| 346 | if (dp != NULL) | ||
| 347 | OPENSSL_free(dp); | ||
| 348 | if (params != NULL) | ||
| 349 | ASN1_STRING_free(params); | ||
| 350 | if (prkey != NULL) | ||
| 351 | ASN1_INTEGER_free(prkey); | ||
| 352 | return 0; | ||
| 353 | } | ||
| 354 | |||
| 355 | static int int_dsa_size(const EVP_PKEY *pkey) | ||
| 356 | { | ||
| 357 | return(DSA_size(pkey->pkey.dsa)); | ||
| 358 | } | ||
| 359 | |||
| 360 | static int dsa_bits(const EVP_PKEY *pkey) | ||
| 361 | { | ||
| 362 | return BN_num_bits(pkey->pkey.dsa->p); | ||
| 363 | } | ||
| 364 | |||
| 365 | static int dsa_missing_parameters(const EVP_PKEY *pkey) | ||
| 366 | { | ||
| 367 | DSA *dsa; | ||
| 368 | dsa=pkey->pkey.dsa; | ||
| 369 | if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL)) | ||
| 370 | return 1; | ||
| 371 | return 0; | ||
| 372 | } | ||
| 373 | |||
| 374 | static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) | ||
| 375 | { | ||
| 376 | BIGNUM *a; | ||
| 377 | |||
| 378 | if ((a=BN_dup(from->pkey.dsa->p)) == NULL) | ||
| 379 | return 0; | ||
| 380 | if (to->pkey.dsa->p != NULL) | ||
| 381 | BN_free(to->pkey.dsa->p); | ||
| 382 | to->pkey.dsa->p=a; | ||
| 383 | |||
| 384 | if ((a=BN_dup(from->pkey.dsa->q)) == NULL) | ||
| 385 | return 0; | ||
| 386 | if (to->pkey.dsa->q != NULL) | ||
| 387 | BN_free(to->pkey.dsa->q); | ||
| 388 | to->pkey.dsa->q=a; | ||
| 389 | |||
| 390 | if ((a=BN_dup(from->pkey.dsa->g)) == NULL) | ||
| 391 | return 0; | ||
| 392 | if (to->pkey.dsa->g != NULL) | ||
| 393 | BN_free(to->pkey.dsa->g); | ||
| 394 | to->pkey.dsa->g=a; | ||
| 395 | return 1; | ||
| 396 | } | ||
| 397 | |||
| 398 | static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) | ||
| 399 | { | ||
| 400 | if ( BN_cmp(a->pkey.dsa->p,b->pkey.dsa->p) || | ||
| 401 | BN_cmp(a->pkey.dsa->q,b->pkey.dsa->q) || | ||
| 402 | BN_cmp(a->pkey.dsa->g,b->pkey.dsa->g)) | ||
| 403 | return 0; | ||
| 404 | else | ||
| 405 | return 1; | ||
| 406 | } | ||
| 407 | |||
| 408 | static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) | ||
| 409 | { | ||
| 410 | if (BN_cmp(b->pkey.dsa->pub_key,a->pkey.dsa->pub_key) != 0) | ||
| 411 | return 0; | ||
| 412 | else | ||
| 413 | return 1; | ||
| 414 | } | ||
| 415 | |||
| 416 | static void int_dsa_free(EVP_PKEY *pkey) | ||
| 417 | { | ||
| 418 | DSA_free(pkey->pkey.dsa); | ||
| 419 | } | ||
| 420 | |||
| 421 | static void update_buflen(const BIGNUM *b, size_t *pbuflen) | ||
| 422 | { | ||
| 423 | size_t i; | ||
| 424 | if (!b) | ||
| 425 | return; | ||
| 426 | if (*pbuflen < (i = (size_t)BN_num_bytes(b))) | ||
| 427 | *pbuflen = i; | ||
| 428 | } | ||
| 429 | |||
| 430 | static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) | ||
| 431 | { | ||
| 432 | unsigned char *m=NULL; | ||
| 433 | int ret=0; | ||
| 434 | size_t buf_len=0; | ||
| 435 | const char *ktype = NULL; | ||
| 436 | |||
| 437 | const BIGNUM *priv_key, *pub_key; | ||
| 438 | |||
| 439 | if (ptype == 2) | ||
| 440 | priv_key = x->priv_key; | ||
| 441 | else | ||
| 442 | priv_key = NULL; | ||
| 443 | |||
| 444 | if (ptype > 0) | ||
| 445 | pub_key = x->pub_key; | ||
| 446 | else | ||
| 447 | pub_key = NULL; | ||
| 448 | |||
| 449 | if (ptype == 2) | ||
| 450 | ktype = "Private-Key"; | ||
| 451 | else if (ptype == 1) | ||
| 452 | ktype = "Public-Key"; | ||
| 453 | else | ||
| 454 | ktype = "DSA-Parameters"; | ||
| 455 | |||
| 456 | update_buflen(x->p, &buf_len); | ||
| 457 | update_buflen(x->q, &buf_len); | ||
| 458 | update_buflen(x->g, &buf_len); | ||
| 459 | update_buflen(priv_key, &buf_len); | ||
| 460 | update_buflen(pub_key, &buf_len); | ||
| 461 | |||
| 462 | m=(unsigned char *)OPENSSL_malloc(buf_len+10); | ||
| 463 | if (m == NULL) | ||
| 464 | { | ||
| 465 | DSAerr(DSA_F_DO_DSA_PRINT,ERR_R_MALLOC_FAILURE); | ||
| 466 | goto err; | ||
| 467 | } | ||
| 468 | |||
| 469 | if (priv_key) | ||
| 470 | { | ||
| 471 | if(!BIO_indent(bp,off,128)) | ||
| 472 | goto err; | ||
| 473 | if (BIO_printf(bp,"%s: (%d bit)\n",ktype, BN_num_bits(x->p)) | ||
| 474 | <= 0) goto err; | ||
| 475 | } | ||
| 476 | |||
| 477 | if (!ASN1_bn_print(bp,"priv:",priv_key,m,off)) | ||
| 478 | goto err; | ||
| 479 | if (!ASN1_bn_print(bp,"pub: ",pub_key,m,off)) | ||
| 480 | goto err; | ||
| 481 | if (!ASN1_bn_print(bp,"P: ",x->p,m,off)) goto err; | ||
| 482 | if (!ASN1_bn_print(bp,"Q: ",x->q,m,off)) goto err; | ||
| 483 | if (!ASN1_bn_print(bp,"G: ",x->g,m,off)) goto err; | ||
| 484 | ret=1; | ||
| 485 | err: | ||
| 486 | if (m != NULL) OPENSSL_free(m); | ||
| 487 | return(ret); | ||
| 488 | } | ||
| 489 | |||
| 490 | static int dsa_param_decode(EVP_PKEY *pkey, | ||
| 491 | const unsigned char **pder, int derlen) | ||
| 492 | { | ||
| 493 | DSA *dsa; | ||
| 494 | if (!(dsa = d2i_DSAparams(NULL, pder, derlen))) | ||
| 495 | { | ||
| 496 | DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB); | ||
| 497 | return 0; | ||
| 498 | } | ||
| 499 | EVP_PKEY_assign_DSA(pkey, dsa); | ||
| 500 | return 1; | ||
| 501 | } | ||
| 502 | |||
| 503 | static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder) | ||
| 504 | { | ||
| 505 | return i2d_DSAparams(pkey->pkey.dsa, pder); | ||
| 506 | } | ||
| 507 | |||
| 508 | static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, | ||
| 509 | ASN1_PCTX *ctx) | ||
| 510 | { | ||
| 511 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 0); | ||
| 512 | } | ||
| 513 | |||
| 514 | static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, | ||
| 515 | ASN1_PCTX *ctx) | ||
| 516 | { | ||
| 517 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 1); | ||
| 518 | } | ||
| 519 | |||
| 520 | |||
| 521 | static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, | ||
| 522 | ASN1_PCTX *ctx) | ||
| 523 | { | ||
| 524 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 2); | ||
| 525 | } | ||
| 526 | |||
| 527 | static int old_dsa_priv_decode(EVP_PKEY *pkey, | ||
| 528 | const unsigned char **pder, int derlen) | ||
| 529 | { | ||
| 530 | DSA *dsa; | ||
| 531 | if (!(dsa = d2i_DSAPrivateKey (NULL, pder, derlen))) | ||
| 532 | { | ||
| 533 | DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB); | ||
| 534 | return 0; | ||
| 535 | } | ||
| 536 | EVP_PKEY_assign_DSA(pkey, dsa); | ||
| 537 | return 1; | ||
| 538 | } | ||
| 539 | |||
| 540 | static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) | ||
| 541 | { | ||
| 542 | return i2d_DSAPrivateKey(pkey->pkey.dsa, pder); | ||
| 543 | } | ||
| 544 | |||
| 545 | static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg, | ||
| 546 | const ASN1_STRING *sig, | ||
| 547 | int indent, ASN1_PCTX *pctx) | ||
| 548 | { | ||
| 549 | DSA_SIG *dsa_sig; | ||
| 550 | const unsigned char *p; | ||
| 551 | if (!sig) | ||
| 552 | { | ||
| 553 | if (BIO_puts(bp, "\n") <= 0) | ||
| 554 | return 0; | ||
| 555 | else | ||
| 556 | return 1; | ||
| 557 | } | ||
| 558 | p = sig->data; | ||
| 559 | dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length); | ||
| 560 | if (dsa_sig) | ||
| 561 | { | ||
| 562 | int rv = 0; | ||
| 563 | size_t buf_len = 0; | ||
| 564 | unsigned char *m=NULL; | ||
| 565 | update_buflen(dsa_sig->r, &buf_len); | ||
| 566 | update_buflen(dsa_sig->s, &buf_len); | ||
| 567 | m = OPENSSL_malloc(buf_len+10); | ||
| 568 | if (m == NULL) | ||
| 569 | { | ||
| 570 | DSAerr(DSA_F_DSA_SIG_PRINT,ERR_R_MALLOC_FAILURE); | ||
| 571 | goto err; | ||
| 572 | } | ||
| 573 | |||
| 574 | if (BIO_write(bp, "\n", 1) != 1) | ||
| 575 | goto err; | ||
| 576 | |||
| 577 | if (!ASN1_bn_print(bp,"r: ",dsa_sig->r,m,indent)) | ||
| 578 | goto err; | ||
| 579 | if (!ASN1_bn_print(bp,"s: ",dsa_sig->s,m,indent)) | ||
| 580 | goto err; | ||
| 581 | rv = 1; | ||
| 582 | err: | ||
| 583 | if (m) | ||
| 584 | OPENSSL_free(m); | ||
| 585 | DSA_SIG_free(dsa_sig); | ||
| 586 | return rv; | ||
| 587 | } | ||
| 588 | return X509_signature_dump(bp, sig, indent); | ||
| 589 | } | ||
| 590 | |||
| 591 | static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) | ||
| 592 | { | ||
| 593 | switch (op) | ||
| 594 | { | ||
| 595 | case ASN1_PKEY_CTRL_PKCS7_SIGN: | ||
| 596 | if (arg1 == 0) | ||
| 597 | { | ||
| 598 | int snid, hnid; | ||
| 599 | X509_ALGOR *alg1, *alg2; | ||
| 600 | PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2); | ||
| 601 | if (alg1 == NULL || alg1->algorithm == NULL) | ||
| 602 | return -1; | ||
| 603 | hnid = OBJ_obj2nid(alg1->algorithm); | ||
| 604 | if (hnid == NID_undef) | ||
| 605 | return -1; | ||
| 606 | if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) | ||
| 607 | return -1; | ||
| 608 | X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); | ||
| 609 | } | ||
| 610 | return 1; | ||
| 611 | #ifndef OPENSSL_NO_CMS | ||
| 612 | case ASN1_PKEY_CTRL_CMS_SIGN: | ||
| 613 | if (arg1 == 0) | ||
| 614 | { | ||
| 615 | int snid, hnid; | ||
| 616 | X509_ALGOR *alg1, *alg2; | ||
| 617 | CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2); | ||
| 618 | if (alg1 == NULL || alg1->algorithm == NULL) | ||
| 619 | return -1; | ||
| 620 | hnid = OBJ_obj2nid(alg1->algorithm); | ||
| 621 | if (hnid == NID_undef) | ||
| 622 | return -1; | ||
| 623 | if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) | ||
| 624 | return -1; | ||
| 625 | X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); | ||
| 626 | } | ||
| 627 | return 1; | ||
| 628 | #endif | ||
| 629 | |||
| 630 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID: | ||
| 631 | *(int *)arg2 = NID_sha1; | ||
| 632 | return 2; | ||
| 633 | |||
| 634 | default: | ||
| 635 | return -2; | ||
| 636 | |||
| 637 | } | ||
| 638 | |||
| 639 | } | ||
| 640 | |||
| 641 | /* NB these are sorted in pkey_id order, lowest first */ | ||
| 642 | |||
| 643 | const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] = | ||
| 644 | { | ||
| 645 | |||
| 646 | { | ||
| 647 | EVP_PKEY_DSA2, | ||
| 648 | EVP_PKEY_DSA, | ||
| 649 | ASN1_PKEY_ALIAS | ||
| 650 | }, | ||
| 651 | |||
| 652 | { | ||
| 653 | EVP_PKEY_DSA1, | ||
| 654 | EVP_PKEY_DSA, | ||
| 655 | ASN1_PKEY_ALIAS | ||
| 656 | }, | ||
| 657 | |||
| 658 | { | ||
| 659 | EVP_PKEY_DSA4, | ||
| 660 | EVP_PKEY_DSA, | ||
| 661 | ASN1_PKEY_ALIAS | ||
| 662 | }, | ||
| 663 | |||
| 664 | { | ||
| 665 | EVP_PKEY_DSA3, | ||
| 666 | EVP_PKEY_DSA, | ||
| 667 | ASN1_PKEY_ALIAS | ||
| 668 | }, | ||
| 669 | |||
| 670 | { | ||
| 671 | EVP_PKEY_DSA, | ||
| 672 | EVP_PKEY_DSA, | ||
| 673 | 0, | ||
| 674 | |||
| 675 | "DSA", | ||
| 676 | "OpenSSL DSA method", | ||
| 677 | |||
| 678 | dsa_pub_decode, | ||
| 679 | dsa_pub_encode, | ||
| 680 | dsa_pub_cmp, | ||
| 681 | dsa_pub_print, | ||
| 682 | |||
| 683 | dsa_priv_decode, | ||
| 684 | dsa_priv_encode, | ||
| 685 | dsa_priv_print, | ||
| 686 | |||
| 687 | int_dsa_size, | ||
| 688 | dsa_bits, | ||
| 689 | |||
| 690 | dsa_param_decode, | ||
| 691 | dsa_param_encode, | ||
| 692 | dsa_missing_parameters, | ||
| 693 | dsa_copy_parameters, | ||
| 694 | dsa_cmp_parameters, | ||
| 695 | dsa_param_print, | ||
| 696 | dsa_sig_print, | ||
| 697 | |||
| 698 | int_dsa_free, | ||
| 699 | dsa_pkey_ctrl, | ||
| 700 | old_dsa_priv_decode, | ||
| 701 | old_dsa_priv_encode | ||
| 702 | } | ||
| 703 | }; | ||
| 704 | |||
diff --git a/src/lib/libcrypto/dsa/dsa_asn1.c b/src/lib/libcrypto/dsa/dsa_asn1.c new file mode 100644 index 0000000000..c37460b2d6 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_asn1.c | |||
| @@ -0,0 +1,150 @@ | |||
| 1 | /* dsa_asn1.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | ||
| 3 | * project 2000. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * | ||
| 12 | * 1. Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * | ||
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | * notice, this list of conditions and the following disclaimer in | ||
| 17 | * the documentation and/or other materials provided with the | ||
| 18 | * distribution. | ||
| 19 | * | ||
| 20 | * 3. All advertising materials mentioning features or use of this | ||
| 21 | * software must display the following acknowledgment: | ||
| 22 | * "This product includes software developed by the OpenSSL Project | ||
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 24 | * | ||
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 26 | * endorse or promote products derived from this software without | ||
| 27 | * prior written permission. For written permission, please contact | ||
| 28 | * licensing@OpenSSL.org. | ||
| 29 | * | ||
| 30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 31 | * nor may "OpenSSL" appear in their names without prior written | ||
| 32 | * permission of the OpenSSL Project. | ||
| 33 | * | ||
| 34 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 35 | * acknowledgment: | ||
| 36 | * "This product includes software developed by the OpenSSL Project | ||
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 38 | * | ||
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 51 | * ==================================================================== | ||
| 52 | * | ||
| 53 | * This product includes cryptographic software written by Eric Young | ||
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 55 | * Hudson (tjh@cryptsoft.com). | ||
| 56 | * | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/dsa.h> | ||
| 62 | #include <openssl/asn1.h> | ||
| 63 | #include <openssl/asn1t.h> | ||
| 64 | |||
| 65 | /* Override the default new methods */ | ||
| 66 | static int sig_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, | ||
| 67 | void *exarg) | ||
| 68 | { | ||
| 69 | if(operation == ASN1_OP_NEW_PRE) { | ||
| 70 | DSA_SIG *sig; | ||
| 71 | sig = OPENSSL_malloc(sizeof(DSA_SIG)); | ||
| 72 | if (!sig) | ||
| 73 | { | ||
| 74 | DSAerr(DSA_F_SIG_CB, ERR_R_MALLOC_FAILURE); | ||
| 75 | return 0; | ||
| 76 | } | ||
| 77 | sig->r = NULL; | ||
| 78 | sig->s = NULL; | ||
| 79 | *pval = (ASN1_VALUE *)sig; | ||
| 80 | return 2; | ||
| 81 | } | ||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | |||
| 85 | ASN1_SEQUENCE_cb(DSA_SIG, sig_cb) = { | ||
| 86 | ASN1_SIMPLE(DSA_SIG, r, CBIGNUM), | ||
| 87 | ASN1_SIMPLE(DSA_SIG, s, CBIGNUM) | ||
| 88 | } ASN1_SEQUENCE_END_cb(DSA_SIG, DSA_SIG) | ||
| 89 | |||
| 90 | IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG) | ||
| 91 | |||
| 92 | /* Override the default free and new methods */ | ||
| 93 | static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, | ||
| 94 | void *exarg) | ||
| 95 | { | ||
| 96 | if(operation == ASN1_OP_NEW_PRE) { | ||
| 97 | *pval = (ASN1_VALUE *)DSA_new(); | ||
| 98 | if(*pval) return 2; | ||
| 99 | return 0; | ||
| 100 | } else if(operation == ASN1_OP_FREE_PRE) { | ||
| 101 | DSA_free((DSA *)*pval); | ||
| 102 | *pval = NULL; | ||
| 103 | return 2; | ||
| 104 | } | ||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = { | ||
| 109 | ASN1_SIMPLE(DSA, version, LONG), | ||
| 110 | ASN1_SIMPLE(DSA, p, BIGNUM), | ||
| 111 | ASN1_SIMPLE(DSA, q, BIGNUM), | ||
| 112 | ASN1_SIMPLE(DSA, g, BIGNUM), | ||
| 113 | ASN1_SIMPLE(DSA, pub_key, BIGNUM), | ||
| 114 | ASN1_SIMPLE(DSA, priv_key, BIGNUM) | ||
| 115 | } ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey) | ||
| 116 | |||
| 117 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey) | ||
| 118 | |||
| 119 | ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = { | ||
| 120 | ASN1_SIMPLE(DSA, p, BIGNUM), | ||
| 121 | ASN1_SIMPLE(DSA, q, BIGNUM), | ||
| 122 | ASN1_SIMPLE(DSA, g, BIGNUM), | ||
| 123 | } ASN1_SEQUENCE_END_cb(DSA, DSAparams) | ||
| 124 | |||
| 125 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams) | ||
| 126 | |||
| 127 | /* DSA public key is a bit trickier... its effectively a CHOICE type | ||
| 128 | * decided by a field called write_params which can either write out | ||
| 129 | * just the public key as an INTEGER or the parameters and public key | ||
| 130 | * in a SEQUENCE | ||
| 131 | */ | ||
| 132 | |||
| 133 | ASN1_SEQUENCE(dsa_pub_internal) = { | ||
| 134 | ASN1_SIMPLE(DSA, pub_key, BIGNUM), | ||
| 135 | ASN1_SIMPLE(DSA, p, BIGNUM), | ||
| 136 | ASN1_SIMPLE(DSA, q, BIGNUM), | ||
| 137 | ASN1_SIMPLE(DSA, g, BIGNUM) | ||
| 138 | } ASN1_SEQUENCE_END_name(DSA, dsa_pub_internal) | ||
| 139 | |||
| 140 | ASN1_CHOICE_cb(DSAPublicKey, dsa_cb) = { | ||
| 141 | ASN1_SIMPLE(DSA, pub_key, BIGNUM), | ||
| 142 | ASN1_EX_COMBINE(0, 0, dsa_pub_internal) | ||
| 143 | } ASN1_CHOICE_END_cb(DSA, DSAPublicKey, write_params) | ||
| 144 | |||
| 145 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey) | ||
| 146 | |||
| 147 | DSA *DSAparams_dup(DSA *dsa) | ||
| 148 | { | ||
| 149 | return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa); | ||
| 150 | } | ||
diff --git a/src/lib/libcrypto/dsa/dsa_depr.c b/src/lib/libcrypto/dsa/dsa_depr.c new file mode 100644 index 0000000000..f2da680eb4 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_depr.c | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | /* crypto/dsa/dsa_depr.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * openssl-core@openssl.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | * This product includes cryptographic software written by Eric Young | ||
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 52 | * Hudson (tjh@cryptsoft.com). | ||
| 53 | * | ||
| 54 | */ | ||
| 55 | |||
| 56 | /* This file contains deprecated function(s) that are now wrappers to the new | ||
| 57 | * version(s). */ | ||
| 58 | |||
| 59 | #undef GENUINE_DSA | ||
| 60 | |||
| 61 | #ifdef GENUINE_DSA | ||
| 62 | /* Parameter generation follows the original release of FIPS PUB 186, | ||
| 63 | * Appendix 2.2 (i.e. use SHA as defined in FIPS PUB 180) */ | ||
| 64 | #define HASH EVP_sha() | ||
| 65 | #else | ||
| 66 | /* Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186, | ||
| 67 | * also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in | ||
| 68 | * FIPS PUB 180-1) */ | ||
| 69 | #define HASH EVP_sha1() | ||
| 70 | #endif | ||
| 71 | |||
| 72 | static void *dummy=&dummy; | ||
| 73 | |||
| 74 | #ifndef OPENSSL_NO_SHA | ||
| 75 | |||
| 76 | #include <stdio.h> | ||
| 77 | #include <time.h> | ||
| 78 | #include "cryptlib.h" | ||
| 79 | #include <openssl/evp.h> | ||
| 80 | #include <openssl/bn.h> | ||
| 81 | #include <openssl/dsa.h> | ||
| 82 | #include <openssl/rand.h> | ||
| 83 | #include <openssl/sha.h> | ||
| 84 | |||
| 85 | #ifndef OPENSSL_NO_DEPRECATED | ||
| 86 | DSA *DSA_generate_parameters(int bits, | ||
| 87 | unsigned char *seed_in, int seed_len, | ||
| 88 | int *counter_ret, unsigned long *h_ret, | ||
| 89 | void (*callback)(int, int, void *), | ||
| 90 | void *cb_arg) | ||
| 91 | { | ||
| 92 | BN_GENCB cb; | ||
| 93 | DSA *ret; | ||
| 94 | |||
| 95 | if ((ret=DSA_new()) == NULL) return NULL; | ||
| 96 | |||
| 97 | BN_GENCB_set_old(&cb, callback, cb_arg); | ||
| 98 | |||
| 99 | if(DSA_generate_parameters_ex(ret, bits, seed_in, seed_len, | ||
| 100 | counter_ret, h_ret, &cb)) | ||
| 101 | return ret; | ||
| 102 | DSA_free(ret); | ||
| 103 | return NULL; | ||
| 104 | } | ||
| 105 | #endif | ||
| 106 | #endif | ||
diff --git a/src/lib/libcrypto/dsa/dsa_err.c b/src/lib/libcrypto/dsa/dsa_err.c new file mode 100644 index 0000000000..bba984e92e --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_err.c | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | /* crypto/dsa/dsa_err.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * openssl-core@OpenSSL.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | * This product includes cryptographic software written by Eric Young | ||
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 52 | * Hudson (tjh@cryptsoft.com). | ||
| 53 | * | ||
| 54 | */ | ||
| 55 | |||
| 56 | /* NOTE: this file was auto generated by the mkerr.pl script: any changes | ||
| 57 | * made to it will be overwritten when the script next updates this file, | ||
| 58 | * only reason strings will be preserved. | ||
| 59 | */ | ||
| 60 | |||
| 61 | #include <stdio.h> | ||
| 62 | #include <openssl/err.h> | ||
| 63 | #include <openssl/dsa.h> | ||
| 64 | |||
| 65 | /* BEGIN ERROR CODES */ | ||
| 66 | #ifndef OPENSSL_NO_ERR | ||
| 67 | |||
| 68 | #define ERR_FUNC(func) ERR_PACK(ERR_LIB_DSA,func,0) | ||
| 69 | #define ERR_REASON(reason) ERR_PACK(ERR_LIB_DSA,0,reason) | ||
| 70 | |||
| 71 | static ERR_STRING_DATA DSA_str_functs[]= | ||
| 72 | { | ||
| 73 | {ERR_FUNC(DSA_F_D2I_DSA_SIG), "d2i_DSA_SIG"}, | ||
| 74 | {ERR_FUNC(DSA_F_DO_DSA_PRINT), "DO_DSA_PRINT"}, | ||
| 75 | {ERR_FUNC(DSA_F_DSAPARAMS_PRINT), "DSAparams_print"}, | ||
| 76 | {ERR_FUNC(DSA_F_DSAPARAMS_PRINT_FP), "DSAparams_print_fp"}, | ||
| 77 | {ERR_FUNC(DSA_F_DSA_DO_SIGN), "DSA_do_sign"}, | ||
| 78 | {ERR_FUNC(DSA_F_DSA_DO_VERIFY), "DSA_do_verify"}, | ||
| 79 | {ERR_FUNC(DSA_F_DSA_NEW_METHOD), "DSA_new_method"}, | ||
| 80 | {ERR_FUNC(DSA_F_DSA_PARAM_DECODE), "DSA_PARAM_DECODE"}, | ||
| 81 | {ERR_FUNC(DSA_F_DSA_PRINT_FP), "DSA_print_fp"}, | ||
| 82 | {ERR_FUNC(DSA_F_DSA_PRIV_DECODE), "DSA_PRIV_DECODE"}, | ||
| 83 | {ERR_FUNC(DSA_F_DSA_PRIV_ENCODE), "DSA_PRIV_ENCODE"}, | ||
| 84 | {ERR_FUNC(DSA_F_DSA_PUB_DECODE), "DSA_PUB_DECODE"}, | ||
| 85 | {ERR_FUNC(DSA_F_DSA_PUB_ENCODE), "DSA_PUB_ENCODE"}, | ||
| 86 | {ERR_FUNC(DSA_F_DSA_SIGN), "DSA_sign"}, | ||
| 87 | {ERR_FUNC(DSA_F_DSA_SIGN_SETUP), "DSA_sign_setup"}, | ||
| 88 | {ERR_FUNC(DSA_F_DSA_SIG_NEW), "DSA_SIG_new"}, | ||
| 89 | {ERR_FUNC(DSA_F_DSA_VERIFY), "DSA_verify"}, | ||
| 90 | {ERR_FUNC(DSA_F_I2D_DSA_SIG), "i2d_DSA_SIG"}, | ||
| 91 | {ERR_FUNC(DSA_F_OLD_DSA_PRIV_DECODE), "OLD_DSA_PRIV_DECODE"}, | ||
| 92 | {ERR_FUNC(DSA_F_PKEY_DSA_CTRL), "PKEY_DSA_CTRL"}, | ||
| 93 | {ERR_FUNC(DSA_F_PKEY_DSA_KEYGEN), "PKEY_DSA_KEYGEN"}, | ||
| 94 | {ERR_FUNC(DSA_F_SIG_CB), "SIG_CB"}, | ||
| 95 | {0,NULL} | ||
| 96 | }; | ||
| 97 | |||
| 98 | static ERR_STRING_DATA DSA_str_reasons[]= | ||
| 99 | { | ||
| 100 | {ERR_REASON(DSA_R_BAD_Q_VALUE) ,"bad q value"}, | ||
| 101 | {ERR_REASON(DSA_R_BN_DECODE_ERROR) ,"bn decode error"}, | ||
| 102 | {ERR_REASON(DSA_R_BN_ERROR) ,"bn error"}, | ||
| 103 | {ERR_REASON(DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE),"data too large for key size"}, | ||
| 104 | {ERR_REASON(DSA_R_DECODE_ERROR) ,"decode error"}, | ||
| 105 | {ERR_REASON(DSA_R_INVALID_DIGEST_TYPE) ,"invalid digest type"}, | ||
| 106 | {ERR_REASON(DSA_R_MISSING_PARAMETERS) ,"missing parameters"}, | ||
| 107 | {ERR_REASON(DSA_R_MODULUS_TOO_LARGE) ,"modulus too large"}, | ||
| 108 | {ERR_REASON(DSA_R_NO_PARAMETERS_SET) ,"no parameters set"}, | ||
| 109 | {ERR_REASON(DSA_R_PARAMETER_ENCODING_ERROR),"parameter encoding error"}, | ||
| 110 | {0,NULL} | ||
| 111 | }; | ||
| 112 | |||
| 113 | #endif | ||
| 114 | |||
| 115 | void ERR_load_DSA_strings(void) | ||
| 116 | { | ||
| 117 | #ifndef OPENSSL_NO_ERR | ||
| 118 | |||
| 119 | if (ERR_func_error_string(DSA_str_functs[0].error) == NULL) | ||
| 120 | { | ||
| 121 | ERR_load_strings(0,DSA_str_functs); | ||
| 122 | ERR_load_strings(0,DSA_str_reasons); | ||
| 123 | } | ||
| 124 | #endif | ||
| 125 | } | ||
diff --git a/src/lib/libcrypto/dsa/dsa_gen.c b/src/lib/libcrypto/dsa/dsa_gen.c new file mode 100644 index 0000000000..cb0b4538a4 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_gen.c | |||
| @@ -0,0 +1,344 @@ | |||
| 1 | /* crypto/dsa/dsa_gen.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #undef GENUINE_DSA | ||
| 60 | |||
| 61 | #ifdef GENUINE_DSA | ||
| 62 | /* Parameter generation follows the original release of FIPS PUB 186, | ||
| 63 | * Appendix 2.2 (i.e. use SHA as defined in FIPS PUB 180) */ | ||
| 64 | #define HASH EVP_sha() | ||
| 65 | #else | ||
| 66 | /* Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186, | ||
| 67 | * also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in | ||
| 68 | * FIPS PUB 180-1) */ | ||
| 69 | #define HASH EVP_sha1() | ||
| 70 | #endif | ||
| 71 | |||
| 72 | #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_SHA is defined */ | ||
| 73 | |||
| 74 | #ifndef OPENSSL_NO_SHA | ||
| 75 | |||
| 76 | #include <stdio.h> | ||
| 77 | #include "cryptlib.h" | ||
| 78 | #include <openssl/evp.h> | ||
| 79 | #include <openssl/bn.h> | ||
| 80 | #include <openssl/rand.h> | ||
| 81 | #include <openssl/sha.h> | ||
| 82 | #include "dsa_locl.h" | ||
| 83 | |||
| 84 | int DSA_generate_parameters_ex(DSA *ret, int bits, | ||
| 85 | const unsigned char *seed_in, int seed_len, | ||
| 86 | int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) | ||
| 87 | { | ||
| 88 | if(ret->meth->dsa_paramgen) | ||
| 89 | return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len, | ||
| 90 | counter_ret, h_ret, cb); | ||
| 91 | else | ||
| 92 | { | ||
| 93 | const EVP_MD *evpmd; | ||
| 94 | size_t qbits = bits >= 2048 ? 256 : 160; | ||
| 95 | |||
| 96 | if (bits >= 2048) | ||
| 97 | { | ||
| 98 | qbits = 256; | ||
| 99 | evpmd = EVP_sha256(); | ||
| 100 | } | ||
| 101 | else | ||
| 102 | { | ||
| 103 | qbits = 160; | ||
| 104 | evpmd = EVP_sha1(); | ||
| 105 | } | ||
| 106 | |||
| 107 | return dsa_builtin_paramgen(ret, bits, qbits, evpmd, | ||
| 108 | seed_in, seed_len, counter_ret, h_ret, cb); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits, | ||
| 113 | const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len, | ||
| 114 | int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) | ||
| 115 | { | ||
| 116 | int ok=0; | ||
| 117 | unsigned char seed[SHA256_DIGEST_LENGTH]; | ||
| 118 | unsigned char md[SHA256_DIGEST_LENGTH]; | ||
| 119 | unsigned char buf[SHA256_DIGEST_LENGTH],buf2[SHA256_DIGEST_LENGTH]; | ||
| 120 | BIGNUM *r0,*W,*X,*c,*test; | ||
| 121 | BIGNUM *g=NULL,*q=NULL,*p=NULL; | ||
| 122 | BN_MONT_CTX *mont=NULL; | ||
| 123 | int i, k, n=0, m=0, qsize = qbits >> 3; | ||
| 124 | int counter=0; | ||
| 125 | int r=0; | ||
| 126 | BN_CTX *ctx=NULL; | ||
| 127 | unsigned int h=2; | ||
| 128 | |||
| 129 | if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH && | ||
| 130 | qsize != SHA256_DIGEST_LENGTH) | ||
| 131 | /* invalid q size */ | ||
| 132 | return 0; | ||
| 133 | |||
| 134 | if (evpmd == NULL) | ||
| 135 | /* use SHA1 as default */ | ||
| 136 | evpmd = EVP_sha1(); | ||
| 137 | |||
| 138 | if (bits < 512) | ||
| 139 | bits = 512; | ||
| 140 | |||
| 141 | bits = (bits+63)/64*64; | ||
| 142 | |||
| 143 | /* NB: seed_len == 0 is special case: copy generated seed to | ||
| 144 | * seed_in if it is not NULL. | ||
| 145 | */ | ||
| 146 | if (seed_len && (seed_len < (size_t)qsize)) | ||
| 147 | seed_in = NULL; /* seed buffer too small -- ignore */ | ||
| 148 | if (seed_len > (size_t)qsize) | ||
| 149 | seed_len = qsize; /* App. 2.2 of FIPS PUB 186 allows larger SEED, | ||
| 150 | * but our internal buffers are restricted to 160 bits*/ | ||
| 151 | if (seed_in != NULL) | ||
| 152 | memcpy(seed, seed_in, seed_len); | ||
| 153 | |||
| 154 | if ((ctx=BN_CTX_new()) == NULL) | ||
| 155 | goto err; | ||
| 156 | |||
| 157 | if ((mont=BN_MONT_CTX_new()) == NULL) | ||
| 158 | goto err; | ||
| 159 | |||
| 160 | BN_CTX_start(ctx); | ||
| 161 | r0 = BN_CTX_get(ctx); | ||
| 162 | g = BN_CTX_get(ctx); | ||
| 163 | W = BN_CTX_get(ctx); | ||
| 164 | q = BN_CTX_get(ctx); | ||
| 165 | X = BN_CTX_get(ctx); | ||
| 166 | c = BN_CTX_get(ctx); | ||
| 167 | p = BN_CTX_get(ctx); | ||
| 168 | test = BN_CTX_get(ctx); | ||
| 169 | |||
| 170 | if (!BN_lshift(test,BN_value_one(),bits-1)) | ||
| 171 | goto err; | ||
| 172 | |||
| 173 | for (;;) | ||
| 174 | { | ||
| 175 | for (;;) /* find q */ | ||
| 176 | { | ||
| 177 | int seed_is_random; | ||
| 178 | |||
| 179 | /* step 1 */ | ||
| 180 | if(!BN_GENCB_call(cb, 0, m++)) | ||
| 181 | goto err; | ||
| 182 | |||
| 183 | if (!seed_len) | ||
| 184 | { | ||
| 185 | RAND_pseudo_bytes(seed, qsize); | ||
| 186 | seed_is_random = 1; | ||
| 187 | } | ||
| 188 | else | ||
| 189 | { | ||
| 190 | seed_is_random = 0; | ||
| 191 | seed_len=0; /* use random seed if 'seed_in' turns out to be bad*/ | ||
| 192 | } | ||
| 193 | memcpy(buf , seed, qsize); | ||
| 194 | memcpy(buf2, seed, qsize); | ||
| 195 | /* precompute "SEED + 1" for step 7: */ | ||
| 196 | for (i = qsize-1; i >= 0; i--) | ||
| 197 | { | ||
| 198 | buf[i]++; | ||
| 199 | if (buf[i] != 0) | ||
| 200 | break; | ||
| 201 | } | ||
| 202 | |||
| 203 | /* step 2 */ | ||
| 204 | EVP_Digest(seed, qsize, md, NULL, evpmd, NULL); | ||
| 205 | EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL); | ||
| 206 | for (i = 0; i < qsize; i++) | ||
| 207 | md[i]^=buf2[i]; | ||
| 208 | |||
| 209 | /* step 3 */ | ||
| 210 | md[0] |= 0x80; | ||
| 211 | md[qsize-1] |= 0x01; | ||
| 212 | if (!BN_bin2bn(md, qsize, q)) | ||
| 213 | goto err; | ||
| 214 | |||
| 215 | /* step 4 */ | ||
| 216 | r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, | ||
| 217 | seed_is_random, cb); | ||
| 218 | if (r > 0) | ||
| 219 | break; | ||
| 220 | if (r != 0) | ||
| 221 | goto err; | ||
| 222 | |||
| 223 | /* do a callback call */ | ||
| 224 | /* step 5 */ | ||
| 225 | } | ||
| 226 | |||
| 227 | if(!BN_GENCB_call(cb, 2, 0)) goto err; | ||
| 228 | if(!BN_GENCB_call(cb, 3, 0)) goto err; | ||
| 229 | |||
| 230 | /* step 6 */ | ||
| 231 | counter=0; | ||
| 232 | /* "offset = 2" */ | ||
| 233 | |||
| 234 | n=(bits-1)/160; | ||
| 235 | |||
| 236 | for (;;) | ||
| 237 | { | ||
| 238 | if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) | ||
| 239 | goto err; | ||
| 240 | |||
| 241 | /* step 7 */ | ||
| 242 | BN_zero(W); | ||
| 243 | /* now 'buf' contains "SEED + offset - 1" */ | ||
| 244 | for (k=0; k<=n; k++) | ||
| 245 | { | ||
| 246 | /* obtain "SEED + offset + k" by incrementing: */ | ||
| 247 | for (i = qsize-1; i >= 0; i--) | ||
| 248 | { | ||
| 249 | buf[i]++; | ||
| 250 | if (buf[i] != 0) | ||
| 251 | break; | ||
| 252 | } | ||
| 253 | |||
| 254 | EVP_Digest(buf, qsize, md ,NULL, evpmd, NULL); | ||
| 255 | |||
| 256 | /* step 8 */ | ||
| 257 | if (!BN_bin2bn(md, qsize, r0)) | ||
| 258 | goto err; | ||
| 259 | if (!BN_lshift(r0,r0,(qsize << 3)*k)) goto err; | ||
| 260 | if (!BN_add(W,W,r0)) goto err; | ||
| 261 | } | ||
| 262 | |||
| 263 | /* more of step 8 */ | ||
| 264 | if (!BN_mask_bits(W,bits-1)) goto err; | ||
| 265 | if (!BN_copy(X,W)) goto err; | ||
| 266 | if (!BN_add(X,X,test)) goto err; | ||
| 267 | |||
| 268 | /* step 9 */ | ||
| 269 | if (!BN_lshift1(r0,q)) goto err; | ||
| 270 | if (!BN_mod(c,X,r0,ctx)) goto err; | ||
| 271 | if (!BN_sub(r0,c,BN_value_one())) goto err; | ||
| 272 | if (!BN_sub(p,X,r0)) goto err; | ||
| 273 | |||
| 274 | /* step 10 */ | ||
| 275 | if (BN_cmp(p,test) >= 0) | ||
| 276 | { | ||
| 277 | /* step 11 */ | ||
| 278 | r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, | ||
| 279 | ctx, 1, cb); | ||
| 280 | if (r > 0) | ||
| 281 | goto end; /* found it */ | ||
| 282 | if (r != 0) | ||
| 283 | goto err; | ||
| 284 | } | ||
| 285 | |||
| 286 | /* step 13 */ | ||
| 287 | counter++; | ||
| 288 | /* "offset = offset + n + 1" */ | ||
| 289 | |||
| 290 | /* step 14 */ | ||
| 291 | if (counter >= 4096) break; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | end: | ||
| 295 | if(!BN_GENCB_call(cb, 2, 1)) | ||
| 296 | goto err; | ||
| 297 | |||
| 298 | /* We now need to generate g */ | ||
| 299 | /* Set r0=(p-1)/q */ | ||
| 300 | if (!BN_sub(test,p,BN_value_one())) goto err; | ||
| 301 | if (!BN_div(r0,NULL,test,q,ctx)) goto err; | ||
| 302 | |||
| 303 | if (!BN_set_word(test,h)) goto err; | ||
| 304 | if (!BN_MONT_CTX_set(mont,p,ctx)) goto err; | ||
| 305 | |||
| 306 | for (;;) | ||
| 307 | { | ||
| 308 | /* g=test^r0%p */ | ||
| 309 | if (!BN_mod_exp_mont(g,test,r0,p,ctx,mont)) goto err; | ||
| 310 | if (!BN_is_one(g)) break; | ||
| 311 | if (!BN_add(test,test,BN_value_one())) goto err; | ||
| 312 | h++; | ||
| 313 | } | ||
| 314 | |||
| 315 | if(!BN_GENCB_call(cb, 3, 1)) | ||
| 316 | goto err; | ||
| 317 | |||
| 318 | ok=1; | ||
| 319 | err: | ||
| 320 | if (ok) | ||
| 321 | { | ||
| 322 | if(ret->p) BN_free(ret->p); | ||
| 323 | if(ret->q) BN_free(ret->q); | ||
| 324 | if(ret->g) BN_free(ret->g); | ||
| 325 | ret->p=BN_dup(p); | ||
| 326 | ret->q=BN_dup(q); | ||
| 327 | ret->g=BN_dup(g); | ||
| 328 | if (ret->p == NULL || ret->q == NULL || ret->g == NULL) | ||
| 329 | { | ||
| 330 | ok=0; | ||
| 331 | goto err; | ||
| 332 | } | ||
| 333 | if (counter_ret != NULL) *counter_ret=counter; | ||
| 334 | if (h_ret != NULL) *h_ret=h; | ||
| 335 | } | ||
| 336 | if(ctx) | ||
| 337 | { | ||
| 338 | BN_CTX_end(ctx); | ||
| 339 | BN_CTX_free(ctx); | ||
| 340 | } | ||
| 341 | if (mont != NULL) BN_MONT_CTX_free(mont); | ||
| 342 | return ok; | ||
| 343 | } | ||
| 344 | #endif | ||
diff --git a/src/lib/libcrypto/dsa/dsa_key.c b/src/lib/libcrypto/dsa/dsa_key.c new file mode 100644 index 0000000000..c4aa86bc6d --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_key.c | |||
| @@ -0,0 +1,128 @@ | |||
| 1 | /* crypto/dsa/dsa_key.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <time.h> | ||
| 61 | #include "cryptlib.h" | ||
| 62 | #ifndef OPENSSL_NO_SHA | ||
| 63 | #include <openssl/bn.h> | ||
| 64 | #include <openssl/dsa.h> | ||
| 65 | #include <openssl/rand.h> | ||
| 66 | |||
| 67 | static int dsa_builtin_keygen(DSA *dsa); | ||
| 68 | |||
| 69 | int DSA_generate_key(DSA *dsa) | ||
| 70 | { | ||
| 71 | if(dsa->meth->dsa_keygen) | ||
| 72 | return dsa->meth->dsa_keygen(dsa); | ||
| 73 | return dsa_builtin_keygen(dsa); | ||
| 74 | } | ||
| 75 | |||
| 76 | static int dsa_builtin_keygen(DSA *dsa) | ||
| 77 | { | ||
| 78 | int ok=0; | ||
| 79 | BN_CTX *ctx=NULL; | ||
| 80 | BIGNUM *pub_key=NULL,*priv_key=NULL; | ||
| 81 | |||
| 82 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 83 | |||
| 84 | if (dsa->priv_key == NULL) | ||
| 85 | { | ||
| 86 | if ((priv_key=BN_new()) == NULL) goto err; | ||
| 87 | } | ||
| 88 | else | ||
| 89 | priv_key=dsa->priv_key; | ||
| 90 | |||
| 91 | do | ||
| 92 | if (!BN_rand_range(priv_key,dsa->q)) goto err; | ||
| 93 | while (BN_is_zero(priv_key)); | ||
| 94 | |||
| 95 | if (dsa->pub_key == NULL) | ||
| 96 | { | ||
| 97 | if ((pub_key=BN_new()) == NULL) goto err; | ||
| 98 | } | ||
| 99 | else | ||
| 100 | pub_key=dsa->pub_key; | ||
| 101 | |||
| 102 | { | ||
| 103 | BIGNUM local_prk; | ||
| 104 | BIGNUM *prk; | ||
| 105 | |||
| 106 | if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) | ||
| 107 | { | ||
| 108 | BN_init(&local_prk); | ||
| 109 | prk = &local_prk; | ||
| 110 | BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME); | ||
| 111 | } | ||
| 112 | else | ||
| 113 | prk = priv_key; | ||
| 114 | |||
| 115 | if (!BN_mod_exp(pub_key,dsa->g,prk,dsa->p,ctx)) goto err; | ||
| 116 | } | ||
| 117 | |||
| 118 | dsa->priv_key=priv_key; | ||
| 119 | dsa->pub_key=pub_key; | ||
| 120 | ok=1; | ||
| 121 | |||
| 122 | err: | ||
| 123 | if ((pub_key != NULL) && (dsa->pub_key == NULL)) BN_free(pub_key); | ||
| 124 | if ((priv_key != NULL) && (dsa->priv_key == NULL)) BN_free(priv_key); | ||
| 125 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 126 | return(ok); | ||
| 127 | } | ||
| 128 | #endif | ||
diff --git a/src/lib/libcrypto/dsa/dsa_lib.c b/src/lib/libcrypto/dsa/dsa_lib.c new file mode 100644 index 0000000000..e9b75902db --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_lib.c | |||
| @@ -0,0 +1,311 @@ | |||
| 1 | /* crypto/dsa/dsa_lib.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ | ||
| 60 | |||
| 61 | #include <stdio.h> | ||
| 62 | #include "cryptlib.h" | ||
| 63 | #include <openssl/bn.h> | ||
| 64 | #include <openssl/dsa.h> | ||
| 65 | #include <openssl/asn1.h> | ||
| 66 | #ifndef OPENSSL_NO_ENGINE | ||
| 67 | #include <openssl/engine.h> | ||
| 68 | #endif | ||
| 69 | #ifndef OPENSSL_NO_DH | ||
| 70 | #include <openssl/dh.h> | ||
| 71 | #endif | ||
| 72 | |||
| 73 | const char DSA_version[]="DSA" OPENSSL_VERSION_PTEXT; | ||
| 74 | |||
| 75 | static const DSA_METHOD *default_DSA_method = NULL; | ||
| 76 | |||
| 77 | void DSA_set_default_method(const DSA_METHOD *meth) | ||
| 78 | { | ||
| 79 | default_DSA_method = meth; | ||
| 80 | } | ||
| 81 | |||
| 82 | const DSA_METHOD *DSA_get_default_method(void) | ||
| 83 | { | ||
| 84 | if(!default_DSA_method) | ||
| 85 | default_DSA_method = DSA_OpenSSL(); | ||
| 86 | return default_DSA_method; | ||
| 87 | } | ||
| 88 | |||
| 89 | DSA *DSA_new(void) | ||
| 90 | { | ||
| 91 | return DSA_new_method(NULL); | ||
| 92 | } | ||
| 93 | |||
| 94 | int DSA_set_method(DSA *dsa, const DSA_METHOD *meth) | ||
| 95 | { | ||
| 96 | /* NB: The caller is specifically setting a method, so it's not up to us | ||
| 97 | * to deal with which ENGINE it comes from. */ | ||
| 98 | const DSA_METHOD *mtmp; | ||
| 99 | mtmp = dsa->meth; | ||
| 100 | if (mtmp->finish) mtmp->finish(dsa); | ||
| 101 | #ifndef OPENSSL_NO_ENGINE | ||
| 102 | if (dsa->engine) | ||
| 103 | { | ||
| 104 | ENGINE_finish(dsa->engine); | ||
| 105 | dsa->engine = NULL; | ||
| 106 | } | ||
| 107 | #endif | ||
| 108 | dsa->meth = meth; | ||
| 109 | if (meth->init) meth->init(dsa); | ||
| 110 | return 1; | ||
| 111 | } | ||
| 112 | |||
| 113 | DSA *DSA_new_method(ENGINE *engine) | ||
| 114 | { | ||
| 115 | DSA *ret; | ||
| 116 | |||
| 117 | ret=(DSA *)OPENSSL_malloc(sizeof(DSA)); | ||
| 118 | if (ret == NULL) | ||
| 119 | { | ||
| 120 | DSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); | ||
| 121 | return(NULL); | ||
| 122 | } | ||
| 123 | ret->meth = DSA_get_default_method(); | ||
| 124 | #ifndef OPENSSL_NO_ENGINE | ||
| 125 | if (engine) | ||
| 126 | { | ||
| 127 | if (!ENGINE_init(engine)) | ||
| 128 | { | ||
| 129 | DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); | ||
| 130 | OPENSSL_free(ret); | ||
| 131 | return NULL; | ||
| 132 | } | ||
| 133 | ret->engine = engine; | ||
| 134 | } | ||
| 135 | else | ||
| 136 | ret->engine = ENGINE_get_default_DSA(); | ||
| 137 | if(ret->engine) | ||
| 138 | { | ||
| 139 | ret->meth = ENGINE_get_DSA(ret->engine); | ||
| 140 | if(!ret->meth) | ||
| 141 | { | ||
| 142 | DSAerr(DSA_F_DSA_NEW_METHOD, | ||
| 143 | ERR_R_ENGINE_LIB); | ||
| 144 | ENGINE_finish(ret->engine); | ||
| 145 | OPENSSL_free(ret); | ||
| 146 | return NULL; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | #endif | ||
| 150 | |||
| 151 | ret->pad=0; | ||
| 152 | ret->version=0; | ||
| 153 | ret->write_params=1; | ||
| 154 | ret->p=NULL; | ||
| 155 | ret->q=NULL; | ||
| 156 | ret->g=NULL; | ||
| 157 | |||
| 158 | ret->pub_key=NULL; | ||
| 159 | ret->priv_key=NULL; | ||
| 160 | |||
| 161 | ret->kinv=NULL; | ||
| 162 | ret->r=NULL; | ||
| 163 | ret->method_mont_p=NULL; | ||
| 164 | |||
| 165 | ret->references=1; | ||
| 166 | ret->flags=ret->meth->flags; | ||
| 167 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); | ||
| 168 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) | ||
| 169 | { | ||
| 170 | #ifndef OPENSSL_NO_ENGINE | ||
| 171 | if (ret->engine) | ||
| 172 | ENGINE_finish(ret->engine); | ||
| 173 | #endif | ||
| 174 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); | ||
| 175 | OPENSSL_free(ret); | ||
| 176 | ret=NULL; | ||
| 177 | } | ||
| 178 | |||
| 179 | return(ret); | ||
| 180 | } | ||
| 181 | |||
| 182 | void DSA_free(DSA *r) | ||
| 183 | { | ||
| 184 | int i; | ||
| 185 | |||
| 186 | if (r == NULL) return; | ||
| 187 | |||
| 188 | i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_DSA); | ||
| 189 | #ifdef REF_PRINT | ||
| 190 | REF_PRINT("DSA",r); | ||
| 191 | #endif | ||
| 192 | if (i > 0) return; | ||
| 193 | #ifdef REF_CHECK | ||
| 194 | if (i < 0) | ||
| 195 | { | ||
| 196 | fprintf(stderr,"DSA_free, bad reference count\n"); | ||
| 197 | abort(); | ||
| 198 | } | ||
| 199 | #endif | ||
| 200 | |||
| 201 | if(r->meth->finish) | ||
| 202 | r->meth->finish(r); | ||
| 203 | #ifndef OPENSSL_NO_ENGINE | ||
| 204 | if(r->engine) | ||
| 205 | ENGINE_finish(r->engine); | ||
| 206 | #endif | ||
| 207 | |||
| 208 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); | ||
| 209 | |||
| 210 | if (r->p != NULL) BN_clear_free(r->p); | ||
| 211 | if (r->q != NULL) BN_clear_free(r->q); | ||
| 212 | if (r->g != NULL) BN_clear_free(r->g); | ||
| 213 | if (r->pub_key != NULL) BN_clear_free(r->pub_key); | ||
| 214 | if (r->priv_key != NULL) BN_clear_free(r->priv_key); | ||
| 215 | if (r->kinv != NULL) BN_clear_free(r->kinv); | ||
| 216 | if (r->r != NULL) BN_clear_free(r->r); | ||
| 217 | OPENSSL_free(r); | ||
| 218 | } | ||
| 219 | |||
| 220 | int DSA_up_ref(DSA *r) | ||
| 221 | { | ||
| 222 | int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA); | ||
| 223 | #ifdef REF_PRINT | ||
| 224 | REF_PRINT("DSA",r); | ||
| 225 | #endif | ||
| 226 | #ifdef REF_CHECK | ||
| 227 | if (i < 2) | ||
| 228 | { | ||
| 229 | fprintf(stderr, "DSA_up_ref, bad reference count\n"); | ||
| 230 | abort(); | ||
| 231 | } | ||
| 232 | #endif | ||
| 233 | return ((i > 1) ? 1 : 0); | ||
| 234 | } | ||
| 235 | |||
| 236 | int DSA_size(const DSA *r) | ||
| 237 | { | ||
| 238 | int ret,i; | ||
| 239 | ASN1_INTEGER bs; | ||
| 240 | unsigned char buf[4]; /* 4 bytes looks really small. | ||
| 241 | However, i2d_ASN1_INTEGER() will not look | ||
| 242 | beyond the first byte, as long as the second | ||
| 243 | parameter is NULL. */ | ||
| 244 | |||
| 245 | i=BN_num_bits(r->q); | ||
| 246 | bs.length=(i+7)/8; | ||
| 247 | bs.data=buf; | ||
| 248 | bs.type=V_ASN1_INTEGER; | ||
| 249 | /* If the top bit is set the asn1 encoding is 1 larger. */ | ||
| 250 | buf[0]=0xff; | ||
| 251 | |||
| 252 | i=i2d_ASN1_INTEGER(&bs,NULL); | ||
| 253 | i+=i; /* r and s */ | ||
| 254 | ret=ASN1_object_size(1,i,V_ASN1_SEQUENCE); | ||
| 255 | return(ret); | ||
| 256 | } | ||
| 257 | |||
| 258 | int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 259 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | ||
| 260 | { | ||
| 261 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, | ||
| 262 | new_func, dup_func, free_func); | ||
| 263 | } | ||
| 264 | |||
| 265 | int DSA_set_ex_data(DSA *d, int idx, void *arg) | ||
| 266 | { | ||
| 267 | return(CRYPTO_set_ex_data(&d->ex_data,idx,arg)); | ||
| 268 | } | ||
| 269 | |||
| 270 | void *DSA_get_ex_data(DSA *d, int idx) | ||
| 271 | { | ||
| 272 | return(CRYPTO_get_ex_data(&d->ex_data,idx)); | ||
| 273 | } | ||
| 274 | |||
| 275 | #ifndef OPENSSL_NO_DH | ||
| 276 | DH *DSA_dup_DH(const DSA *r) | ||
| 277 | { | ||
| 278 | /* DSA has p, q, g, optional pub_key, optional priv_key. | ||
| 279 | * DH has p, optional length, g, optional pub_key, optional priv_key. | ||
| 280 | */ | ||
| 281 | |||
| 282 | DH *ret = NULL; | ||
| 283 | |||
| 284 | if (r == NULL) | ||
| 285 | goto err; | ||
| 286 | ret = DH_new(); | ||
| 287 | if (ret == NULL) | ||
| 288 | goto err; | ||
| 289 | if (r->p != NULL) | ||
| 290 | if ((ret->p = BN_dup(r->p)) == NULL) | ||
| 291 | goto err; | ||
| 292 | if (r->q != NULL) | ||
| 293 | ret->length = BN_num_bits(r->q); | ||
| 294 | if (r->g != NULL) | ||
| 295 | if ((ret->g = BN_dup(r->g)) == NULL) | ||
| 296 | goto err; | ||
| 297 | if (r->pub_key != NULL) | ||
| 298 | if ((ret->pub_key = BN_dup(r->pub_key)) == NULL) | ||
| 299 | goto err; | ||
| 300 | if (r->priv_key != NULL) | ||
| 301 | if ((ret->priv_key = BN_dup(r->priv_key)) == NULL) | ||
| 302 | goto err; | ||
| 303 | |||
| 304 | return ret; | ||
| 305 | |||
| 306 | err: | ||
| 307 | if (ret != NULL) | ||
| 308 | DH_free(ret); | ||
| 309 | return NULL; | ||
| 310 | } | ||
| 311 | #endif | ||
diff --git a/src/lib/libcrypto/dsa/dsa_locl.h b/src/lib/libcrypto/dsa/dsa_locl.h new file mode 100644 index 0000000000..21e2e45242 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_locl.h | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | /* ==================================================================== | ||
| 2 | * Copyright (c) 2007 The OpenSSL Project. All rights reserved. | ||
| 3 | * | ||
| 4 | * Redistribution and use in source and binary forms, with or without | ||
| 5 | * modification, are permitted provided that the following conditions | ||
| 6 | * are met: | ||
| 7 | * | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * | ||
| 11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer in | ||
| 13 | * the documentation and/or other materials provided with the | ||
| 14 | * distribution. | ||
| 15 | * | ||
| 16 | * 3. All advertising materials mentioning features or use of this | ||
| 17 | * software must display the following acknowledgment: | ||
| 18 | * "This product includes software developed by the OpenSSL Project | ||
| 19 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
| 20 | * | ||
| 21 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 22 | * endorse or promote products derived from this software without | ||
| 23 | * prior written permission. For written permission, please contact | ||
| 24 | * openssl-core@openssl.org. | ||
| 25 | * | ||
| 26 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 27 | * nor may "OpenSSL" appear in their names without prior written | ||
| 28 | * permission of the OpenSSL Project. | ||
| 29 | * | ||
| 30 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 31 | * acknowledgment: | ||
| 32 | * "This product includes software developed by the OpenSSL Project | ||
| 33 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
| 34 | * | ||
| 35 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 47 | * ==================================================================== | ||
| 48 | * | ||
| 49 | * This product includes cryptographic software written by Eric Young | ||
| 50 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 51 | * Hudson (tjh@cryptsoft.com). | ||
| 52 | * | ||
| 53 | */ | ||
| 54 | |||
| 55 | #include <openssl/dsa.h> | ||
| 56 | |||
| 57 | int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits, | ||
| 58 | const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len, | ||
| 59 | unsigned char *seed_out, | ||
| 60 | int *counter_ret, unsigned long *h_ret, BN_GENCB *cb); | ||
diff --git a/src/lib/libcrypto/dsa/dsa_ossl.c b/src/lib/libcrypto/dsa/dsa_ossl.c new file mode 100644 index 0000000000..a3ddd7d281 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_ossl.c | |||
| @@ -0,0 +1,398 @@ | |||
| 1 | /* crypto/dsa/dsa_ossl.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ | ||
| 60 | |||
| 61 | #include <stdio.h> | ||
| 62 | #include "cryptlib.h" | ||
| 63 | #include <openssl/bn.h> | ||
| 64 | #include <openssl/sha.h> | ||
| 65 | #include <openssl/dsa.h> | ||
| 66 | #include <openssl/rand.h> | ||
| 67 | #include <openssl/asn1.h> | ||
| 68 | |||
| 69 | static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); | ||
| 70 | static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp); | ||
| 71 | static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, | ||
| 72 | DSA *dsa); | ||
| 73 | static int dsa_init(DSA *dsa); | ||
| 74 | static int dsa_finish(DSA *dsa); | ||
| 75 | |||
| 76 | static DSA_METHOD openssl_dsa_meth = { | ||
| 77 | "OpenSSL DSA method", | ||
| 78 | dsa_do_sign, | ||
| 79 | dsa_sign_setup, | ||
| 80 | dsa_do_verify, | ||
| 81 | NULL, /* dsa_mod_exp, */ | ||
| 82 | NULL, /* dsa_bn_mod_exp, */ | ||
| 83 | dsa_init, | ||
| 84 | dsa_finish, | ||
| 85 | 0, | ||
| 86 | NULL, | ||
| 87 | NULL, | ||
| 88 | NULL | ||
| 89 | }; | ||
| 90 | |||
| 91 | /* These macro wrappers replace attempts to use the dsa_mod_exp() and | ||
| 92 | * bn_mod_exp() handlers in the DSA_METHOD structure. We avoid the problem of | ||
| 93 | * having a the macro work as an expression by bundling an "err_instr". So; | ||
| 94 | * | ||
| 95 | * if (!dsa->meth->bn_mod_exp(dsa, r,dsa->g,&k,dsa->p,ctx, | ||
| 96 | * dsa->method_mont_p)) goto err; | ||
| 97 | * | ||
| 98 | * can be replaced by; | ||
| 99 | * | ||
| 100 | * DSA_BN_MOD_EXP(goto err, dsa, r, dsa->g, &k, dsa->p, ctx, | ||
| 101 | * dsa->method_mont_p); | ||
| 102 | */ | ||
| 103 | |||
| 104 | #define DSA_MOD_EXP(err_instr,dsa,rr,a1,p1,a2,p2,m,ctx,in_mont) \ | ||
| 105 | do { \ | ||
| 106 | int _tmp_res53; \ | ||
| 107 | if((dsa)->meth->dsa_mod_exp) \ | ||
| 108 | _tmp_res53 = (dsa)->meth->dsa_mod_exp((dsa), (rr), (a1), (p1), \ | ||
| 109 | (a2), (p2), (m), (ctx), (in_mont)); \ | ||
| 110 | else \ | ||
| 111 | _tmp_res53 = BN_mod_exp2_mont((rr), (a1), (p1), (a2), (p2), \ | ||
| 112 | (m), (ctx), (in_mont)); \ | ||
| 113 | if(!_tmp_res53) err_instr; \ | ||
| 114 | } while(0) | ||
| 115 | #define DSA_BN_MOD_EXP(err_instr,dsa,r,a,p,m,ctx,m_ctx) \ | ||
| 116 | do { \ | ||
| 117 | int _tmp_res53; \ | ||
| 118 | if((dsa)->meth->bn_mod_exp) \ | ||
| 119 | _tmp_res53 = (dsa)->meth->bn_mod_exp((dsa), (r), (a), (p), \ | ||
| 120 | (m), (ctx), (m_ctx)); \ | ||
| 121 | else \ | ||
| 122 | _tmp_res53 = BN_mod_exp_mont((r), (a), (p), (m), (ctx), (m_ctx)); \ | ||
| 123 | if(!_tmp_res53) err_instr; \ | ||
| 124 | } while(0) | ||
| 125 | |||
| 126 | const DSA_METHOD *DSA_OpenSSL(void) | ||
| 127 | { | ||
| 128 | return &openssl_dsa_meth; | ||
| 129 | } | ||
| 130 | |||
| 131 | static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | ||
| 132 | { | ||
| 133 | BIGNUM *kinv=NULL,*r=NULL,*s=NULL; | ||
| 134 | BIGNUM m; | ||
| 135 | BIGNUM xr; | ||
| 136 | BN_CTX *ctx=NULL; | ||
| 137 | int reason=ERR_R_BN_LIB; | ||
| 138 | DSA_SIG *ret=NULL; | ||
| 139 | |||
| 140 | BN_init(&m); | ||
| 141 | BN_init(&xr); | ||
| 142 | |||
| 143 | if (!dsa->p || !dsa->q || !dsa->g) | ||
| 144 | { | ||
| 145 | reason=DSA_R_MISSING_PARAMETERS; | ||
| 146 | goto err; | ||
| 147 | } | ||
| 148 | |||
| 149 | s=BN_new(); | ||
| 150 | if (s == NULL) goto err; | ||
| 151 | ctx=BN_CTX_new(); | ||
| 152 | if (ctx == NULL) goto err; | ||
| 153 | |||
| 154 | if ((dsa->kinv == NULL) || (dsa->r == NULL)) | ||
| 155 | { | ||
| 156 | if (!DSA_sign_setup(dsa,ctx,&kinv,&r)) goto err; | ||
| 157 | } | ||
| 158 | else | ||
| 159 | { | ||
| 160 | kinv=dsa->kinv; | ||
| 161 | dsa->kinv=NULL; | ||
| 162 | r=dsa->r; | ||
| 163 | dsa->r=NULL; | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | if (dlen > BN_num_bytes(dsa->q)) | ||
| 168 | /* if the digest length is greater than the size of q use the | ||
| 169 | * BN_num_bits(dsa->q) leftmost bits of the digest, see | ||
| 170 | * fips 186-3, 4.2 */ | ||
| 171 | dlen = BN_num_bytes(dsa->q); | ||
| 172 | if (BN_bin2bn(dgst,dlen,&m) == NULL) | ||
| 173 | goto err; | ||
| 174 | |||
| 175 | /* Compute s = inv(k) (m + xr) mod q */ | ||
| 176 | if (!BN_mod_mul(&xr,dsa->priv_key,r,dsa->q,ctx)) goto err;/* s = xr */ | ||
| 177 | if (!BN_add(s, &xr, &m)) goto err; /* s = m + xr */ | ||
| 178 | if (BN_cmp(s,dsa->q) > 0) | ||
| 179 | if (!BN_sub(s,s,dsa->q)) goto err; | ||
| 180 | if (!BN_mod_mul(s,s,kinv,dsa->q,ctx)) goto err; | ||
| 181 | |||
| 182 | ret=DSA_SIG_new(); | ||
| 183 | if (ret == NULL) goto err; | ||
| 184 | ret->r = r; | ||
| 185 | ret->s = s; | ||
| 186 | |||
| 187 | err: | ||
| 188 | if (!ret) | ||
| 189 | { | ||
| 190 | DSAerr(DSA_F_DSA_DO_SIGN,reason); | ||
| 191 | BN_free(r); | ||
| 192 | BN_free(s); | ||
| 193 | } | ||
| 194 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 195 | BN_clear_free(&m); | ||
| 196 | BN_clear_free(&xr); | ||
| 197 | if (kinv != NULL) /* dsa->kinv is NULL now if we used it */ | ||
| 198 | BN_clear_free(kinv); | ||
| 199 | return(ret); | ||
| 200 | } | ||
| 201 | |||
| 202 | static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | ||
| 203 | { | ||
| 204 | BN_CTX *ctx; | ||
| 205 | BIGNUM k,kq,*K,*kinv=NULL,*r=NULL; | ||
| 206 | int ret=0; | ||
| 207 | |||
| 208 | if (!dsa->p || !dsa->q || !dsa->g) | ||
| 209 | { | ||
| 210 | DSAerr(DSA_F_DSA_SIGN_SETUP,DSA_R_MISSING_PARAMETERS); | ||
| 211 | return 0; | ||
| 212 | } | ||
| 213 | |||
| 214 | BN_init(&k); | ||
| 215 | BN_init(&kq); | ||
| 216 | |||
| 217 | if (ctx_in == NULL) | ||
| 218 | { | ||
| 219 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 220 | } | ||
| 221 | else | ||
| 222 | ctx=ctx_in; | ||
| 223 | |||
| 224 | if ((r=BN_new()) == NULL) goto err; | ||
| 225 | |||
| 226 | /* Get random k */ | ||
| 227 | do | ||
| 228 | if (!BN_rand_range(&k, dsa->q)) goto err; | ||
| 229 | while (BN_is_zero(&k)); | ||
| 230 | if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) | ||
| 231 | { | ||
| 232 | BN_set_flags(&k, BN_FLG_CONSTTIME); | ||
| 233 | } | ||
| 234 | |||
| 235 | if (dsa->flags & DSA_FLAG_CACHE_MONT_P) | ||
| 236 | { | ||
| 237 | if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p, | ||
| 238 | CRYPTO_LOCK_DSA, | ||
| 239 | dsa->p, ctx)) | ||
| 240 | goto err; | ||
| 241 | } | ||
| 242 | |||
| 243 | /* Compute r = (g^k mod p) mod q */ | ||
| 244 | |||
| 245 | if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) | ||
| 246 | { | ||
| 247 | if (!BN_copy(&kq, &k)) goto err; | ||
| 248 | |||
| 249 | /* We do not want timing information to leak the length of k, | ||
| 250 | * so we compute g^k using an equivalent exponent of fixed length. | ||
| 251 | * | ||
| 252 | * (This is a kludge that we need because the BN_mod_exp_mont() | ||
| 253 | * does not let us specify the desired timing behaviour.) */ | ||
| 254 | |||
| 255 | if (!BN_add(&kq, &kq, dsa->q)) goto err; | ||
| 256 | if (BN_num_bits(&kq) <= BN_num_bits(dsa->q)) | ||
| 257 | { | ||
| 258 | if (!BN_add(&kq, &kq, dsa->q)) goto err; | ||
| 259 | } | ||
| 260 | |||
| 261 | K = &kq; | ||
| 262 | } | ||
| 263 | else | ||
| 264 | { | ||
| 265 | K = &k; | ||
| 266 | } | ||
| 267 | DSA_BN_MOD_EXP(goto err, dsa, r, dsa->g, K, dsa->p, ctx, | ||
| 268 | dsa->method_mont_p); | ||
| 269 | if (!BN_mod(r,r,dsa->q,ctx)) goto err; | ||
| 270 | |||
| 271 | /* Compute part of 's = inv(k) (m + xr) mod q' */ | ||
| 272 | if ((kinv=BN_mod_inverse(NULL,&k,dsa->q,ctx)) == NULL) goto err; | ||
| 273 | |||
| 274 | if (*kinvp != NULL) BN_clear_free(*kinvp); | ||
| 275 | *kinvp=kinv; | ||
| 276 | kinv=NULL; | ||
| 277 | if (*rp != NULL) BN_clear_free(*rp); | ||
| 278 | *rp=r; | ||
| 279 | ret=1; | ||
| 280 | err: | ||
| 281 | if (!ret) | ||
| 282 | { | ||
| 283 | DSAerr(DSA_F_DSA_SIGN_SETUP,ERR_R_BN_LIB); | ||
| 284 | if (r != NULL) | ||
| 285 | BN_clear_free(r); | ||
| 286 | } | ||
| 287 | if (ctx_in == NULL) BN_CTX_free(ctx); | ||
| 288 | BN_clear_free(&k); | ||
| 289 | BN_clear_free(&kq); | ||
| 290 | return(ret); | ||
| 291 | } | ||
| 292 | |||
| 293 | static int dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, | ||
| 294 | DSA *dsa) | ||
| 295 | { | ||
| 296 | BN_CTX *ctx; | ||
| 297 | BIGNUM u1,u2,t1; | ||
| 298 | BN_MONT_CTX *mont=NULL; | ||
| 299 | int ret = -1, i; | ||
| 300 | if (!dsa->p || !dsa->q || !dsa->g) | ||
| 301 | { | ||
| 302 | DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MISSING_PARAMETERS); | ||
| 303 | return -1; | ||
| 304 | } | ||
| 305 | |||
| 306 | i = BN_num_bits(dsa->q); | ||
| 307 | /* fips 186-3 allows only different sizes for q */ | ||
| 308 | if (i != 160 && i != 224 && i != 256) | ||
| 309 | { | ||
| 310 | DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_BAD_Q_VALUE); | ||
| 311 | return -1; | ||
| 312 | } | ||
| 313 | |||
| 314 | if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) | ||
| 315 | { | ||
| 316 | DSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MODULUS_TOO_LARGE); | ||
| 317 | return -1; | ||
| 318 | } | ||
| 319 | BN_init(&u1); | ||
| 320 | BN_init(&u2); | ||
| 321 | BN_init(&t1); | ||
| 322 | |||
| 323 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 324 | |||
| 325 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || | ||
| 326 | BN_ucmp(sig->r, dsa->q) >= 0) | ||
| 327 | { | ||
| 328 | ret = 0; | ||
| 329 | goto err; | ||
| 330 | } | ||
| 331 | if (BN_is_zero(sig->s) || BN_is_negative(sig->s) || | ||
| 332 | BN_ucmp(sig->s, dsa->q) >= 0) | ||
| 333 | { | ||
| 334 | ret = 0; | ||
| 335 | goto err; | ||
| 336 | } | ||
| 337 | |||
| 338 | /* Calculate W = inv(S) mod Q | ||
| 339 | * save W in u2 */ | ||
| 340 | if ((BN_mod_inverse(&u2,sig->s,dsa->q,ctx)) == NULL) goto err; | ||
| 341 | |||
| 342 | /* save M in u1 */ | ||
| 343 | if (dgst_len > (i >> 3)) | ||
| 344 | /* if the digest length is greater than the size of q use the | ||
| 345 | * BN_num_bits(dsa->q) leftmost bits of the digest, see | ||
| 346 | * fips 186-3, 4.2 */ | ||
| 347 | dgst_len = (i >> 3); | ||
| 348 | if (BN_bin2bn(dgst,dgst_len,&u1) == NULL) goto err; | ||
| 349 | |||
| 350 | /* u1 = M * w mod q */ | ||
| 351 | if (!BN_mod_mul(&u1,&u1,&u2,dsa->q,ctx)) goto err; | ||
| 352 | |||
| 353 | /* u2 = r * w mod q */ | ||
| 354 | if (!BN_mod_mul(&u2,sig->r,&u2,dsa->q,ctx)) goto err; | ||
| 355 | |||
| 356 | |||
| 357 | if (dsa->flags & DSA_FLAG_CACHE_MONT_P) | ||
| 358 | { | ||
| 359 | mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p, | ||
| 360 | CRYPTO_LOCK_DSA, dsa->p, ctx); | ||
| 361 | if (!mont) | ||
| 362 | goto err; | ||
| 363 | } | ||
| 364 | |||
| 365 | |||
| 366 | DSA_MOD_EXP(goto err, dsa, &t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx, mont); | ||
| 367 | /* BN_copy(&u1,&t1); */ | ||
| 368 | /* let u1 = u1 mod q */ | ||
| 369 | if (!BN_mod(&u1,&t1,dsa->q,ctx)) goto err; | ||
| 370 | |||
| 371 | /* V is now in u1. If the signature is correct, it will be | ||
| 372 | * equal to R. */ | ||
| 373 | ret=(BN_ucmp(&u1, sig->r) == 0); | ||
| 374 | |||
| 375 | err: | ||
| 376 | /* XXX: surely this is wrong - if ret is 0, it just didn't verify; | ||
| 377 | there is no error in BN. Test should be ret == -1 (Ben) */ | ||
| 378 | if (ret != 1) DSAerr(DSA_F_DSA_DO_VERIFY,ERR_R_BN_LIB); | ||
| 379 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 380 | BN_free(&u1); | ||
| 381 | BN_free(&u2); | ||
| 382 | BN_free(&t1); | ||
| 383 | return(ret); | ||
| 384 | } | ||
| 385 | |||
| 386 | static int dsa_init(DSA *dsa) | ||
| 387 | { | ||
| 388 | dsa->flags|=DSA_FLAG_CACHE_MONT_P; | ||
| 389 | return(1); | ||
| 390 | } | ||
| 391 | |||
| 392 | static int dsa_finish(DSA *dsa) | ||
| 393 | { | ||
| 394 | if(dsa->method_mont_p) | ||
| 395 | BN_MONT_CTX_free(dsa->method_mont_p); | ||
| 396 | return(1); | ||
| 397 | } | ||
| 398 | |||
diff --git a/src/lib/libcrypto/dsa/dsa_pmeth.c b/src/lib/libcrypto/dsa/dsa_pmeth.c new file mode 100644 index 0000000000..715d8d675b --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_pmeth.c | |||
| @@ -0,0 +1,318 @@ | |||
| 1 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | ||
| 2 | * project 2006. | ||
| 3 | */ | ||
| 4 | /* ==================================================================== | ||
| 5 | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * | ||
| 14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 15 | * notice, this list of conditions and the following disclaimer in | ||
| 16 | * the documentation and/or other materials provided with the | ||
| 17 | * distribution. | ||
| 18 | * | ||
| 19 | * 3. All advertising materials mentioning features or use of this | ||
| 20 | * software must display the following acknowledgment: | ||
| 21 | * "This product includes software developed by the OpenSSL Project | ||
| 22 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 23 | * | ||
| 24 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 25 | * endorse or promote products derived from this software without | ||
| 26 | * prior written permission. For written permission, please contact | ||
| 27 | * licensing@OpenSSL.org. | ||
| 28 | * | ||
| 29 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 30 | * nor may "OpenSSL" appear in their names without prior written | ||
| 31 | * permission of the OpenSSL Project. | ||
| 32 | * | ||
| 33 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 34 | * acknowledgment: | ||
| 35 | * "This product includes software developed by the OpenSSL Project | ||
| 36 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 37 | * | ||
| 38 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 39 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 41 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 42 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 47 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 48 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 49 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 50 | * ==================================================================== | ||
| 51 | * | ||
| 52 | * This product includes cryptographic software written by Eric Young | ||
| 53 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 54 | * Hudson (tjh@cryptsoft.com). | ||
| 55 | * | ||
| 56 | */ | ||
| 57 | |||
| 58 | #include <stdio.h> | ||
| 59 | #include "cryptlib.h" | ||
| 60 | #include <openssl/asn1t.h> | ||
| 61 | #include <openssl/x509.h> | ||
| 62 | #include <openssl/evp.h> | ||
| 63 | #include <openssl/bn.h> | ||
| 64 | #include "evp_locl.h" | ||
| 65 | #include "dsa_locl.h" | ||
| 66 | |||
| 67 | /* DSA pkey context structure */ | ||
| 68 | |||
| 69 | typedef struct | ||
| 70 | { | ||
| 71 | /* Parameter gen parameters */ | ||
| 72 | int nbits; /* size of p in bits (default: 1024) */ | ||
| 73 | int qbits; /* size of q in bits (default: 160) */ | ||
| 74 | const EVP_MD *pmd; /* MD for parameter generation */ | ||
| 75 | /* Keygen callback info */ | ||
| 76 | int gentmp[2]; | ||
| 77 | /* message digest */ | ||
| 78 | const EVP_MD *md; /* MD for the signature */ | ||
| 79 | } DSA_PKEY_CTX; | ||
| 80 | |||
| 81 | static int pkey_dsa_init(EVP_PKEY_CTX *ctx) | ||
| 82 | { | ||
| 83 | DSA_PKEY_CTX *dctx; | ||
| 84 | dctx = OPENSSL_malloc(sizeof(DSA_PKEY_CTX)); | ||
| 85 | if (!dctx) | ||
| 86 | return 0; | ||
| 87 | dctx->nbits = 1024; | ||
| 88 | dctx->qbits = 160; | ||
| 89 | dctx->pmd = NULL; | ||
| 90 | dctx->md = NULL; | ||
| 91 | |||
| 92 | ctx->data = dctx; | ||
| 93 | ctx->keygen_info = dctx->gentmp; | ||
| 94 | ctx->keygen_info_count = 2; | ||
| 95 | |||
| 96 | return 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | static int pkey_dsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) | ||
| 100 | { | ||
| 101 | DSA_PKEY_CTX *dctx, *sctx; | ||
| 102 | if (!pkey_dsa_init(dst)) | ||
| 103 | return 0; | ||
| 104 | sctx = src->data; | ||
| 105 | dctx = dst->data; | ||
| 106 | dctx->nbits = sctx->nbits; | ||
| 107 | dctx->qbits = sctx->qbits; | ||
| 108 | dctx->pmd = sctx->pmd; | ||
| 109 | dctx->md = sctx->md; | ||
| 110 | return 1; | ||
| 111 | } | ||
| 112 | |||
| 113 | static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx) | ||
| 114 | { | ||
| 115 | DSA_PKEY_CTX *dctx = ctx->data; | ||
| 116 | if (dctx) | ||
| 117 | OPENSSL_free(dctx); | ||
| 118 | } | ||
| 119 | |||
| 120 | static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, | ||
| 121 | const unsigned char *tbs, size_t tbslen) | ||
| 122 | { | ||
| 123 | int ret, type; | ||
| 124 | unsigned int sltmp; | ||
| 125 | DSA_PKEY_CTX *dctx = ctx->data; | ||
| 126 | DSA *dsa = ctx->pkey->pkey.dsa; | ||
| 127 | |||
| 128 | if (dctx->md) | ||
| 129 | type = EVP_MD_type(dctx->md); | ||
| 130 | else | ||
| 131 | type = NID_sha1; | ||
| 132 | |||
| 133 | ret = DSA_sign(type, tbs, tbslen, sig, &sltmp, dsa); | ||
| 134 | |||
| 135 | if (ret <= 0) | ||
| 136 | return ret; | ||
| 137 | *siglen = sltmp; | ||
| 138 | return 1; | ||
| 139 | } | ||
| 140 | |||
| 141 | static int pkey_dsa_verify(EVP_PKEY_CTX *ctx, | ||
| 142 | const unsigned char *sig, size_t siglen, | ||
| 143 | const unsigned char *tbs, size_t tbslen) | ||
| 144 | { | ||
| 145 | int ret, type; | ||
| 146 | DSA_PKEY_CTX *dctx = ctx->data; | ||
| 147 | DSA *dsa = ctx->pkey->pkey.dsa; | ||
| 148 | |||
| 149 | if (dctx->md) | ||
| 150 | type = EVP_MD_type(dctx->md); | ||
| 151 | else | ||
| 152 | type = NID_sha1; | ||
| 153 | |||
| 154 | ret = DSA_verify(type, tbs, tbslen, sig, siglen, dsa); | ||
| 155 | |||
| 156 | return ret; | ||
| 157 | } | ||
| 158 | |||
| 159 | static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) | ||
| 160 | { | ||
| 161 | DSA_PKEY_CTX *dctx = ctx->data; | ||
| 162 | switch (type) | ||
| 163 | { | ||
| 164 | case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS: | ||
| 165 | if (p1 < 256) | ||
| 166 | return -2; | ||
| 167 | dctx->nbits = p1; | ||
| 168 | return 1; | ||
| 169 | |||
| 170 | case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS: | ||
| 171 | if (p1 != 160 && p1 != 224 && p1 && p1 != 256) | ||
| 172 | return -2; | ||
| 173 | dctx->qbits = p1; | ||
| 174 | return 1; | ||
| 175 | |||
| 176 | case EVP_PKEY_CTRL_DSA_PARAMGEN_MD: | ||
| 177 | if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 && | ||
| 178 | EVP_MD_type((const EVP_MD *)p2) != NID_sha224 && | ||
| 179 | EVP_MD_type((const EVP_MD *)p2) != NID_sha256) | ||
| 180 | { | ||
| 181 | DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE); | ||
| 182 | return 0; | ||
| 183 | } | ||
| 184 | dctx->md = p2; | ||
| 185 | return 1; | ||
| 186 | |||
| 187 | case EVP_PKEY_CTRL_MD: | ||
| 188 | if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 && | ||
| 189 | EVP_MD_type((const EVP_MD *)p2) != NID_dsa && | ||
| 190 | EVP_MD_type((const EVP_MD *)p2) != NID_dsaWithSHA && | ||
| 191 | EVP_MD_type((const EVP_MD *)p2) != NID_sha224 && | ||
| 192 | EVP_MD_type((const EVP_MD *)p2) != NID_sha256 && | ||
| 193 | EVP_MD_type((const EVP_MD *)p2) != NID_sha384 && | ||
| 194 | EVP_MD_type((const EVP_MD *)p2) != NID_sha512) | ||
| 195 | { | ||
| 196 | DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE); | ||
| 197 | return 0; | ||
| 198 | } | ||
| 199 | dctx->md = p2; | ||
| 200 | return 1; | ||
| 201 | |||
| 202 | case EVP_PKEY_CTRL_DIGESTINIT: | ||
| 203 | case EVP_PKEY_CTRL_PKCS7_SIGN: | ||
| 204 | case EVP_PKEY_CTRL_CMS_SIGN: | ||
| 205 | return 1; | ||
| 206 | |||
| 207 | case EVP_PKEY_CTRL_PEER_KEY: | ||
| 208 | DSAerr(DSA_F_PKEY_DSA_CTRL, | ||
| 209 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | ||
| 210 | return -2; | ||
| 211 | default: | ||
| 212 | return -2; | ||
| 213 | |||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx, | ||
| 218 | const char *type, const char *value) | ||
| 219 | { | ||
| 220 | if (!strcmp(type, "dsa_paramgen_bits")) | ||
| 221 | { | ||
| 222 | int nbits; | ||
| 223 | nbits = atoi(value); | ||
| 224 | return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits); | ||
| 225 | } | ||
| 226 | if (!strcmp(type, "dsa_paramgen_q_bits")) | ||
| 227 | { | ||
| 228 | int qbits = atoi(value); | ||
| 229 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN, | ||
| 230 | EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, qbits, NULL); | ||
| 231 | } | ||
| 232 | if (!strcmp(type, "dsa_paramgen_md")) | ||
| 233 | { | ||
| 234 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN, | ||
| 235 | EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0, | ||
| 236 | (void *)EVP_get_digestbyname(value)); | ||
| 237 | } | ||
| 238 | return -2; | ||
| 239 | } | ||
| 240 | |||
| 241 | static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | ||
| 242 | { | ||
| 243 | DSA *dsa = NULL; | ||
| 244 | DSA_PKEY_CTX *dctx = ctx->data; | ||
| 245 | BN_GENCB *pcb, cb; | ||
| 246 | int ret; | ||
| 247 | if (ctx->pkey_gencb) | ||
| 248 | { | ||
| 249 | pcb = &cb; | ||
| 250 | evp_pkey_set_cb_translate(pcb, ctx); | ||
| 251 | } | ||
| 252 | else | ||
| 253 | pcb = NULL; | ||
| 254 | dsa = DSA_new(); | ||
| 255 | if (!dsa) | ||
| 256 | return 0; | ||
| 257 | ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd, | ||
| 258 | NULL, 0, NULL, NULL, NULL, pcb); | ||
| 259 | if (ret) | ||
| 260 | EVP_PKEY_assign_DSA(pkey, dsa); | ||
| 261 | else | ||
| 262 | DSA_free(dsa); | ||
| 263 | return ret; | ||
| 264 | } | ||
| 265 | |||
| 266 | static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | ||
| 267 | { | ||
| 268 | DSA *dsa = NULL; | ||
| 269 | if (ctx->pkey == NULL) | ||
| 270 | { | ||
| 271 | DSAerr(DSA_F_PKEY_DSA_KEYGEN, DSA_R_NO_PARAMETERS_SET); | ||
| 272 | return 0; | ||
| 273 | } | ||
| 274 | dsa = DSA_new(); | ||
| 275 | if (!dsa) | ||
| 276 | return 0; | ||
| 277 | EVP_PKEY_assign_DSA(pkey, dsa); | ||
| 278 | /* Note: if error return, pkey is freed by parent routine */ | ||
| 279 | if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey)) | ||
| 280 | return 0; | ||
| 281 | return DSA_generate_key(pkey->pkey.dsa); | ||
| 282 | } | ||
| 283 | |||
| 284 | const EVP_PKEY_METHOD dsa_pkey_meth = | ||
| 285 | { | ||
| 286 | EVP_PKEY_DSA, | ||
| 287 | EVP_PKEY_FLAG_AUTOARGLEN, | ||
| 288 | pkey_dsa_init, | ||
| 289 | pkey_dsa_copy, | ||
| 290 | pkey_dsa_cleanup, | ||
| 291 | |||
| 292 | 0, | ||
| 293 | pkey_dsa_paramgen, | ||
| 294 | |||
| 295 | 0, | ||
| 296 | pkey_dsa_keygen, | ||
| 297 | |||
| 298 | 0, | ||
| 299 | pkey_dsa_sign, | ||
| 300 | |||
| 301 | 0, | ||
| 302 | pkey_dsa_verify, | ||
| 303 | |||
| 304 | 0,0, | ||
| 305 | |||
| 306 | 0,0,0,0, | ||
| 307 | |||
| 308 | 0,0, | ||
| 309 | |||
| 310 | 0,0, | ||
| 311 | |||
| 312 | 0,0, | ||
| 313 | |||
| 314 | pkey_dsa_ctrl, | ||
| 315 | pkey_dsa_ctrl_str | ||
| 316 | |||
| 317 | |||
| 318 | }; | ||
diff --git a/src/lib/libcrypto/dsa/dsa_prn.c b/src/lib/libcrypto/dsa/dsa_prn.c new file mode 100644 index 0000000000..6f29f5e240 --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_prn.c | |||
| @@ -0,0 +1,121 @@ | |||
| 1 | /* crypto/dsa/dsa_prn.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | ||
| 3 | * project 2006. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * | ||
| 12 | * 1. Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * | ||
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | * notice, this list of conditions and the following disclaimer in | ||
| 17 | * the documentation and/or other materials provided with the | ||
| 18 | * distribution. | ||
| 19 | * | ||
| 20 | * 3. All advertising materials mentioning features or use of this | ||
| 21 | * software must display the following acknowledgment: | ||
| 22 | * "This product includes software developed by the OpenSSL Project | ||
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 24 | * | ||
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 26 | * endorse or promote products derived from this software without | ||
| 27 | * prior written permission. For written permission, please contact | ||
| 28 | * licensing@OpenSSL.org. | ||
| 29 | * | ||
| 30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 31 | * nor may "OpenSSL" appear in their names without prior written | ||
| 32 | * permission of the OpenSSL Project. | ||
| 33 | * | ||
| 34 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 35 | * acknowledgment: | ||
| 36 | * "This product includes software developed by the OpenSSL Project | ||
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 38 | * | ||
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 51 | * ==================================================================== | ||
| 52 | * | ||
| 53 | * This product includes cryptographic software written by Eric Young | ||
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 55 | * Hudson (tjh@cryptsoft.com). | ||
| 56 | * | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/evp.h> | ||
| 62 | #include <openssl/dsa.h> | ||
| 63 | |||
| 64 | #ifndef OPENSSL_NO_FP_API | ||
| 65 | int DSA_print_fp(FILE *fp, const DSA *x, int off) | ||
| 66 | { | ||
| 67 | BIO *b; | ||
| 68 | int ret; | ||
| 69 | |||
| 70 | if ((b=BIO_new(BIO_s_file())) == NULL) | ||
| 71 | { | ||
| 72 | DSAerr(DSA_F_DSA_PRINT_FP,ERR_R_BUF_LIB); | ||
| 73 | return(0); | ||
| 74 | } | ||
| 75 | BIO_set_fp(b,fp,BIO_NOCLOSE); | ||
| 76 | ret=DSA_print(b,x,off); | ||
| 77 | BIO_free(b); | ||
| 78 | return(ret); | ||
| 79 | } | ||
| 80 | |||
| 81 | int DSAparams_print_fp(FILE *fp, const DSA *x) | ||
| 82 | { | ||
| 83 | BIO *b; | ||
| 84 | int ret; | ||
| 85 | |||
| 86 | if ((b=BIO_new(BIO_s_file())) == NULL) | ||
| 87 | { | ||
| 88 | DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB); | ||
| 89 | return(0); | ||
| 90 | } | ||
| 91 | BIO_set_fp(b,fp,BIO_NOCLOSE); | ||
| 92 | ret=DSAparams_print(b, x); | ||
| 93 | BIO_free(b); | ||
| 94 | return(ret); | ||
| 95 | } | ||
| 96 | #endif | ||
| 97 | |||
| 98 | int DSA_print(BIO *bp, const DSA *x, int off) | ||
| 99 | { | ||
| 100 | EVP_PKEY *pk; | ||
| 101 | int ret; | ||
| 102 | pk = EVP_PKEY_new(); | ||
| 103 | if (!pk || !EVP_PKEY_set1_DSA(pk, (DSA *)x)) | ||
| 104 | return 0; | ||
| 105 | ret = EVP_PKEY_print_private(bp, pk, off, NULL); | ||
| 106 | EVP_PKEY_free(pk); | ||
| 107 | return ret; | ||
| 108 | } | ||
| 109 | |||
| 110 | int DSAparams_print(BIO *bp, const DSA *x) | ||
| 111 | { | ||
| 112 | EVP_PKEY *pk; | ||
| 113 | int ret; | ||
| 114 | pk = EVP_PKEY_new(); | ||
| 115 | if (!pk || !EVP_PKEY_set1_DSA(pk, (DSA *)x)) | ||
| 116 | return 0; | ||
| 117 | ret = EVP_PKEY_print_params(bp, pk, 4, NULL); | ||
| 118 | EVP_PKEY_free(pk); | ||
| 119 | return ret; | ||
| 120 | } | ||
| 121 | |||
diff --git a/src/lib/libcrypto/dsa/dsa_utl.c b/src/lib/libcrypto/dsa/dsa_sign.c index 24c021d120..17555e5892 100644 --- a/src/lib/libcrypto/dsa/dsa_utl.c +++ b/src/lib/libcrypto/dsa/dsa_sign.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* crypto/dsa/dsa_lib.c */ | 1 | /* crypto/dsa/dsa_sign.c */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -58,38 +58,33 @@ | |||
| 58 | 58 | ||
| 59 | /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ | 59 | /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ |
| 60 | 60 | ||
| 61 | #include <stdio.h> | ||
| 62 | #include "cryptlib.h" | 61 | #include "cryptlib.h" |
| 63 | #include <openssl/bn.h> | ||
| 64 | #include <openssl/dsa.h> | 62 | #include <openssl/dsa.h> |
| 65 | #include <openssl/asn1.h> | 63 | #include <openssl/rand.h> |
| 66 | #ifndef OPENSSL_NO_ENGINE | ||
| 67 | #include <openssl/engine.h> | ||
| 68 | #endif | ||
| 69 | #ifndef OPENSSL_NO_DH | ||
| 70 | #include <openssl/dh.h> | ||
| 71 | #endif | ||
| 72 | 64 | ||
| 73 | DSA_SIG *DSA_SIG_new(void) | 65 | DSA_SIG * DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) |
| 74 | { | 66 | { |
| 75 | DSA_SIG *sig; | 67 | return dsa->meth->dsa_do_sign(dgst, dlen, dsa); |
| 76 | sig = OPENSSL_malloc(sizeof(DSA_SIG)); | ||
| 77 | if (!sig) | ||
| 78 | return NULL; | ||
| 79 | sig->r = NULL; | ||
| 80 | sig->s = NULL; | ||
| 81 | return sig; | ||
| 82 | } | 68 | } |
| 83 | 69 | ||
| 84 | void DSA_SIG_free(DSA_SIG *sig) | 70 | int DSA_sign(int type, const unsigned char *dgst, int dlen, unsigned char *sig, |
| 71 | unsigned int *siglen, DSA *dsa) | ||
| 85 | { | 72 | { |
| 86 | if (sig) | 73 | DSA_SIG *s; |
| 74 | RAND_seed(dgst, dlen); | ||
| 75 | s=DSA_do_sign(dgst,dlen,dsa); | ||
| 76 | if (s == NULL) | ||
| 87 | { | 77 | { |
| 88 | if (sig->r) | 78 | *siglen=0; |
| 89 | BN_free(sig->r); | 79 | return(0); |
| 90 | if (sig->s) | ||
| 91 | BN_free(sig->s); | ||
| 92 | OPENSSL_free(sig); | ||
| 93 | } | 80 | } |
| 81 | *siglen=i2d_DSA_SIG(s,&sig); | ||
| 82 | DSA_SIG_free(s); | ||
| 83 | return(1); | ||
| 84 | } | ||
| 85 | |||
| 86 | int DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | ||
| 87 | { | ||
| 88 | return dsa->meth->dsa_sign_setup(dsa, ctx_in, kinvp, rp); | ||
| 94 | } | 89 | } |
| 95 | 90 | ||
diff --git a/src/lib/libcrypto/dsa/dsa_vrf.c b/src/lib/libcrypto/dsa/dsa_vrf.c new file mode 100644 index 0000000000..226a75ff3f --- /dev/null +++ b/src/lib/libcrypto/dsa/dsa_vrf.c | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | /* crypto/dsa/dsa_vrf.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ | ||
| 60 | |||
| 61 | #include "cryptlib.h" | ||
| 62 | #include <openssl/dsa.h> | ||
| 63 | |||
| 64 | int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, | ||
| 65 | DSA *dsa) | ||
| 66 | { | ||
| 67 | return dsa->meth->dsa_do_verify(dgst, dgst_len, sig, dsa); | ||
| 68 | } | ||
| 69 | |||
| 70 | /* data has already been hashed (probably with SHA or SHA-1). */ | ||
| 71 | /* returns | ||
| 72 | * 1: correct signature | ||
| 73 | * 0: incorrect signature | ||
| 74 | * -1: error | ||
| 75 | */ | ||
| 76 | int DSA_verify(int type, const unsigned char *dgst, int dgst_len, | ||
| 77 | const unsigned char *sigbuf, int siglen, DSA *dsa) | ||
| 78 | { | ||
| 79 | DSA_SIG *s; | ||
| 80 | int ret=-1; | ||
| 81 | |||
| 82 | s = DSA_SIG_new(); | ||
| 83 | if (s == NULL) return(ret); | ||
| 84 | if (d2i_DSA_SIG(&s,&sigbuf,siglen) == NULL) goto err; | ||
| 85 | ret=DSA_do_verify(dgst,dgst_len,s,dsa); | ||
| 86 | err: | ||
| 87 | DSA_SIG_free(s); | ||
| 88 | return(ret); | ||
| 89 | } | ||
