summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dsa
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/dsa')
-rw-r--r--src/lib/libcrypto/dsa/Makefile7
-rw-r--r--src/lib/libcrypto/dsa/dsa.h20
-rw-r--r--src/lib/libcrypto/dsa/dsa_asn1.c40
-rw-r--r--src/lib/libcrypto/dsa/dsa_err.c7
-rw-r--r--src/lib/libcrypto/dsa/dsa_gen.c35
-rw-r--r--src/lib/libcrypto/dsa/dsa_key.c16
-rw-r--r--src/lib/libcrypto/dsa/dsa_lib.c22
-rw-r--r--src/lib/libcrypto/dsa/dsa_ossl.c16
-rw-r--r--src/lib/libcrypto/dsa/dsa_sign.c50
-rw-r--r--src/lib/libcrypto/dsa/dsa_vrf.c29
10 files changed, 196 insertions, 46 deletions
diff --git a/src/lib/libcrypto/dsa/Makefile b/src/lib/libcrypto/dsa/Makefile
index 8073c4ecfe..5fef4ca5ad 100644
--- a/src/lib/libcrypto/dsa/Makefile
+++ b/src/lib/libcrypto/dsa/Makefile
@@ -99,8 +99,9 @@ dsa_asn1.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h
99dsa_asn1.o: ../../include/openssl/err.h ../../include/openssl/lhash.h 99dsa_asn1.o: ../../include/openssl/err.h ../../include/openssl/lhash.h
100dsa_asn1.o: ../../include/openssl/opensslconf.h 100dsa_asn1.o: ../../include/openssl/opensslconf.h
101dsa_asn1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h 101dsa_asn1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
102dsa_asn1.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h 102dsa_asn1.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
103dsa_asn1.o: ../../include/openssl/symhacks.h ../cryptlib.h dsa_asn1.c 103dsa_asn1.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
104dsa_asn1.o: ../cryptlib.h dsa_asn1.c
104dsa_depr.o: ../../e_os.h ../../include/openssl/asn1.h 105dsa_depr.o: ../../e_os.h ../../include/openssl/asn1.h
105dsa_depr.o: ../../include/openssl/bio.h ../../include/openssl/bn.h 106dsa_depr.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
106dsa_depr.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h 107dsa_depr.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
@@ -189,7 +190,7 @@ dsa_prn.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h
189dsa_prn.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h 190dsa_prn.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h
190dsa_prn.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h 191dsa_prn.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
191dsa_prn.o: ../cryptlib.h dsa_prn.c 192dsa_prn.o: ../cryptlib.h dsa_prn.c
192dsa_sign.o: ../../e_os.h ../../include/openssl/bio.h 193dsa_sign.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
193dsa_sign.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h 194dsa_sign.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
194dsa_sign.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h 195dsa_sign.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h
195dsa_sign.o: ../../include/openssl/err.h ../../include/openssl/lhash.h 196dsa_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
index ac50a5c846..a6f6d0b0b2 100644
--- a/src/lib/libcrypto/dsa/dsa.h
+++ b/src/lib/libcrypto/dsa/dsa.h
@@ -97,6 +97,21 @@
97 * be used for all exponents. 97 * be used for all exponents.
98 */ 98 */
99 99
100/* If this flag is set the DSA method is FIPS compliant and can be used
101 * in FIPS mode. This is set in the validated module method. If an
102 * application sets this flag in its own methods it is its reposibility
103 * to ensure the result is compliant.
104 */
105
106#define DSA_FLAG_FIPS_METHOD 0x0400
107
108/* If this flag is set the operations normally disabled in FIPS mode are
109 * permitted it is then the applications responsibility to ensure that the
110 * usage is compliant.
111 */
112
113#define DSA_FLAG_NON_FIPS_ALLOW 0x0400
114
100#ifdef __cplusplus 115#ifdef __cplusplus
101extern "C" { 116extern "C" {
102#endif 117#endif
@@ -272,6 +287,8 @@ void ERR_load_DSA_strings(void);
272#define DSA_F_DSAPARAMS_PRINT_FP 101 287#define DSA_F_DSAPARAMS_PRINT_FP 101
273#define DSA_F_DSA_DO_SIGN 112 288#define DSA_F_DSA_DO_SIGN 112
274#define DSA_F_DSA_DO_VERIFY 113 289#define DSA_F_DSA_DO_VERIFY 113
290#define DSA_F_DSA_GENERATE_KEY 124
291#define DSA_F_DSA_GENERATE_PARAMETERS_EX 123
275#define DSA_F_DSA_NEW_METHOD 103 292#define DSA_F_DSA_NEW_METHOD 103
276#define DSA_F_DSA_PARAM_DECODE 119 293#define DSA_F_DSA_PARAM_DECODE 119
277#define DSA_F_DSA_PRINT_FP 105 294#define DSA_F_DSA_PRINT_FP 105
@@ -282,6 +299,7 @@ void ERR_load_DSA_strings(void);
282#define DSA_F_DSA_SIGN 106 299#define DSA_F_DSA_SIGN 106
283#define DSA_F_DSA_SIGN_SETUP 107 300#define DSA_F_DSA_SIGN_SETUP 107
284#define DSA_F_DSA_SIG_NEW 109 301#define DSA_F_DSA_SIG_NEW 109
302#define DSA_F_DSA_SIG_PRINT 125
285#define DSA_F_DSA_VERIFY 108 303#define DSA_F_DSA_VERIFY 108
286#define DSA_F_I2D_DSA_SIG 111 304#define DSA_F_I2D_DSA_SIG 111
287#define DSA_F_OLD_DSA_PRIV_DECODE 122 305#define DSA_F_OLD_DSA_PRIV_DECODE 122
@@ -298,6 +316,8 @@ void ERR_load_DSA_strings(void);
298#define DSA_R_INVALID_DIGEST_TYPE 106 316#define DSA_R_INVALID_DIGEST_TYPE 106
299#define DSA_R_MISSING_PARAMETERS 101 317#define DSA_R_MISSING_PARAMETERS 101
300#define DSA_R_MODULUS_TOO_LARGE 103 318#define DSA_R_MODULUS_TOO_LARGE 103
319#define DSA_R_NEED_NEW_SETUP_VALUES 110
320#define DSA_R_NON_FIPS_DSA_METHOD 111
301#define DSA_R_NO_PARAMETERS_SET 107 321#define DSA_R_NO_PARAMETERS_SET 107
302#define DSA_R_PARAMETER_ENCODING_ERROR 105 322#define DSA_R_PARAMETER_ENCODING_ERROR 105
303 323
diff --git a/src/lib/libcrypto/dsa/dsa_asn1.c b/src/lib/libcrypto/dsa/dsa_asn1.c
index c37460b2d6..6058534374 100644
--- a/src/lib/libcrypto/dsa/dsa_asn1.c
+++ b/src/lib/libcrypto/dsa/dsa_asn1.c
@@ -61,6 +61,7 @@
61#include <openssl/dsa.h> 61#include <openssl/dsa.h>
62#include <openssl/asn1.h> 62#include <openssl/asn1.h>
63#include <openssl/asn1t.h> 63#include <openssl/asn1t.h>
64#include <openssl/rand.h>
64 65
65/* Override the default new methods */ 66/* Override the default new methods */
66static int sig_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 67static int sig_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
@@ -87,7 +88,7 @@ ASN1_SEQUENCE_cb(DSA_SIG, sig_cb) = {
87 ASN1_SIMPLE(DSA_SIG, s, CBIGNUM) 88 ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
88} ASN1_SEQUENCE_END_cb(DSA_SIG, DSA_SIG) 89} ASN1_SEQUENCE_END_cb(DSA_SIG, DSA_SIG)
89 90
90IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG) 91IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA_SIG, DSA_SIG, DSA_SIG)
91 92
92/* Override the default free and new methods */ 93/* Override the default free and new methods */
93static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 94static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
@@ -148,3 +149,40 @@ DSA *DSAparams_dup(DSA *dsa)
148 { 149 {
149 return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa); 150 return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
150 } 151 }
152
153int DSA_sign(int type, const unsigned char *dgst, int dlen, unsigned char *sig,
154 unsigned int *siglen, DSA *dsa)
155 {
156 DSA_SIG *s;
157 RAND_seed(dgst, dlen);
158 s=DSA_do_sign(dgst,dlen,dsa);
159 if (s == NULL)
160 {
161 *siglen=0;
162 return(0);
163 }
164 *siglen=i2d_DSA_SIG(s,&sig);
165 DSA_SIG_free(s);
166 return(1);
167 }
168
169/* data has already been hashed (probably with SHA or SHA-1). */
170/* returns
171 * 1: correct signature
172 * 0: incorrect signature
173 * -1: error
174 */
175int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
176 const unsigned char *sigbuf, int siglen, DSA *dsa)
177 {
178 DSA_SIG *s;
179 int ret=-1;
180
181 s = DSA_SIG_new();
182 if (s == NULL) return(ret);
183 if (d2i_DSA_SIG(&s,&sigbuf,siglen) == NULL) goto err;
184 ret=DSA_do_verify(dgst,dgst_len,s,dsa);
185err:
186 DSA_SIG_free(s);
187 return(ret);
188 }
diff --git a/src/lib/libcrypto/dsa/dsa_err.c b/src/lib/libcrypto/dsa/dsa_err.c
index bba984e92e..00545b7b9f 100644
--- a/src/lib/libcrypto/dsa/dsa_err.c
+++ b/src/lib/libcrypto/dsa/dsa_err.c
@@ -1,6 +1,6 @@
1/* crypto/dsa/dsa_err.c */ 1/* crypto/dsa/dsa_err.c */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2011 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -76,6 +76,8 @@ static ERR_STRING_DATA DSA_str_functs[]=
76{ERR_FUNC(DSA_F_DSAPARAMS_PRINT_FP), "DSAparams_print_fp"}, 76{ERR_FUNC(DSA_F_DSAPARAMS_PRINT_FP), "DSAparams_print_fp"},
77{ERR_FUNC(DSA_F_DSA_DO_SIGN), "DSA_do_sign"}, 77{ERR_FUNC(DSA_F_DSA_DO_SIGN), "DSA_do_sign"},
78{ERR_FUNC(DSA_F_DSA_DO_VERIFY), "DSA_do_verify"}, 78{ERR_FUNC(DSA_F_DSA_DO_VERIFY), "DSA_do_verify"},
79{ERR_FUNC(DSA_F_DSA_GENERATE_KEY), "DSA_generate_key"},
80{ERR_FUNC(DSA_F_DSA_GENERATE_PARAMETERS_EX), "DSA_generate_parameters_ex"},
79{ERR_FUNC(DSA_F_DSA_NEW_METHOD), "DSA_new_method"}, 81{ERR_FUNC(DSA_F_DSA_NEW_METHOD), "DSA_new_method"},
80{ERR_FUNC(DSA_F_DSA_PARAM_DECODE), "DSA_PARAM_DECODE"}, 82{ERR_FUNC(DSA_F_DSA_PARAM_DECODE), "DSA_PARAM_DECODE"},
81{ERR_FUNC(DSA_F_DSA_PRINT_FP), "DSA_print_fp"}, 83{ERR_FUNC(DSA_F_DSA_PRINT_FP), "DSA_print_fp"},
@@ -86,6 +88,7 @@ static ERR_STRING_DATA DSA_str_functs[]=
86{ERR_FUNC(DSA_F_DSA_SIGN), "DSA_sign"}, 88{ERR_FUNC(DSA_F_DSA_SIGN), "DSA_sign"},
87{ERR_FUNC(DSA_F_DSA_SIGN_SETUP), "DSA_sign_setup"}, 89{ERR_FUNC(DSA_F_DSA_SIGN_SETUP), "DSA_sign_setup"},
88{ERR_FUNC(DSA_F_DSA_SIG_NEW), "DSA_SIG_new"}, 90{ERR_FUNC(DSA_F_DSA_SIG_NEW), "DSA_SIG_new"},
91{ERR_FUNC(DSA_F_DSA_SIG_PRINT), "DSA_SIG_PRINT"},
89{ERR_FUNC(DSA_F_DSA_VERIFY), "DSA_verify"}, 92{ERR_FUNC(DSA_F_DSA_VERIFY), "DSA_verify"},
90{ERR_FUNC(DSA_F_I2D_DSA_SIG), "i2d_DSA_SIG"}, 93{ERR_FUNC(DSA_F_I2D_DSA_SIG), "i2d_DSA_SIG"},
91{ERR_FUNC(DSA_F_OLD_DSA_PRIV_DECODE), "OLD_DSA_PRIV_DECODE"}, 94{ERR_FUNC(DSA_F_OLD_DSA_PRIV_DECODE), "OLD_DSA_PRIV_DECODE"},
@@ -105,6 +108,8 @@ static ERR_STRING_DATA DSA_str_reasons[]=
105{ERR_REASON(DSA_R_INVALID_DIGEST_TYPE) ,"invalid digest type"}, 108{ERR_REASON(DSA_R_INVALID_DIGEST_TYPE) ,"invalid digest type"},
106{ERR_REASON(DSA_R_MISSING_PARAMETERS) ,"missing parameters"}, 109{ERR_REASON(DSA_R_MISSING_PARAMETERS) ,"missing parameters"},
107{ERR_REASON(DSA_R_MODULUS_TOO_LARGE) ,"modulus too large"}, 110{ERR_REASON(DSA_R_MODULUS_TOO_LARGE) ,"modulus too large"},
111{ERR_REASON(DSA_R_NEED_NEW_SETUP_VALUES) ,"need new setup values"},
112{ERR_REASON(DSA_R_NON_FIPS_DSA_METHOD) ,"non fips dsa method"},
108{ERR_REASON(DSA_R_NO_PARAMETERS_SET) ,"no parameters set"}, 113{ERR_REASON(DSA_R_NO_PARAMETERS_SET) ,"no parameters set"},
109{ERR_REASON(DSA_R_PARAMETER_ENCODING_ERROR),"parameter encoding error"}, 114{ERR_REASON(DSA_R_PARAMETER_ENCODING_ERROR),"parameter encoding error"},
110{0,NULL} 115{0,NULL}
diff --git a/src/lib/libcrypto/dsa/dsa_gen.c b/src/lib/libcrypto/dsa/dsa_gen.c
index cb0b4538a4..c398761d0d 100644
--- a/src/lib/libcrypto/dsa/dsa_gen.c
+++ b/src/lib/libcrypto/dsa/dsa_gen.c
@@ -81,13 +81,33 @@
81#include <openssl/sha.h> 81#include <openssl/sha.h>
82#include "dsa_locl.h" 82#include "dsa_locl.h"
83 83
84#ifdef OPENSSL_FIPS
85#include <openssl/fips.h>
86#endif
87
84int DSA_generate_parameters_ex(DSA *ret, int bits, 88int DSA_generate_parameters_ex(DSA *ret, int bits,
85 const unsigned char *seed_in, int seed_len, 89 const unsigned char *seed_in, int seed_len,
86 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) 90 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
87 { 91 {
92#ifdef OPENSSL_FIPS
93 if (FIPS_mode() && !(ret->meth->flags & DSA_FLAG_FIPS_METHOD)
94 && !(ret->flags & DSA_FLAG_NON_FIPS_ALLOW))
95 {
96 DSAerr(DSA_F_DSA_GENERATE_PARAMETERS_EX, DSA_R_NON_FIPS_DSA_METHOD);
97 return 0;
98 }
99#endif
88 if(ret->meth->dsa_paramgen) 100 if(ret->meth->dsa_paramgen)
89 return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len, 101 return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
90 counter_ret, h_ret, cb); 102 counter_ret, h_ret, cb);
103#ifdef OPENSSL_FIPS
104 else if (FIPS_mode())
105 {
106 return FIPS_dsa_generate_parameters_ex(ret, bits,
107 seed_in, seed_len,
108 counter_ret, h_ret, cb);
109 }
110#endif
91 else 111 else
92 { 112 {
93 const EVP_MD *evpmd; 113 const EVP_MD *evpmd;
@@ -105,12 +125,13 @@ int DSA_generate_parameters_ex(DSA *ret, int bits,
105 } 125 }
106 126
107 return dsa_builtin_paramgen(ret, bits, qbits, evpmd, 127 return dsa_builtin_paramgen(ret, bits, qbits, evpmd,
108 seed_in, seed_len, counter_ret, h_ret, cb); 128 seed_in, seed_len, NULL, counter_ret, h_ret, cb);
109 } 129 }
110 } 130 }
111 131
112int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits, 132int 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, 133 const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
134 unsigned char *seed_out,
114 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) 135 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb)
115 { 136 {
116 int ok=0; 137 int ok=0;
@@ -201,8 +222,10 @@ int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
201 } 222 }
202 223
203 /* step 2 */ 224 /* step 2 */
204 EVP_Digest(seed, qsize, md, NULL, evpmd, NULL); 225 if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL))
205 EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL); 226 goto err;
227 if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL))
228 goto err;
206 for (i = 0; i < qsize; i++) 229 for (i = 0; i < qsize; i++)
207 md[i]^=buf2[i]; 230 md[i]^=buf2[i];
208 231
@@ -251,7 +274,9 @@ int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
251 break; 274 break;
252 } 275 }
253 276
254 EVP_Digest(buf, qsize, md ,NULL, evpmd, NULL); 277 if (!EVP_Digest(buf, qsize, md ,NULL, evpmd,
278 NULL))
279 goto err;
255 280
256 /* step 8 */ 281 /* step 8 */
257 if (!BN_bin2bn(md, qsize, r0)) 282 if (!BN_bin2bn(md, qsize, r0))
@@ -332,6 +357,8 @@ err:
332 } 357 }
333 if (counter_ret != NULL) *counter_ret=counter; 358 if (counter_ret != NULL) *counter_ret=counter;
334 if (h_ret != NULL) *h_ret=h; 359 if (h_ret != NULL) *h_ret=h;
360 if (seed_out)
361 memcpy(seed_out, seed, qsize);
335 } 362 }
336 if(ctx) 363 if(ctx)
337 { 364 {
diff --git a/src/lib/libcrypto/dsa/dsa_key.c b/src/lib/libcrypto/dsa/dsa_key.c
index c4aa86bc6d..9cf669b921 100644
--- a/src/lib/libcrypto/dsa/dsa_key.c
+++ b/src/lib/libcrypto/dsa/dsa_key.c
@@ -64,12 +64,28 @@
64#include <openssl/dsa.h> 64#include <openssl/dsa.h>
65#include <openssl/rand.h> 65#include <openssl/rand.h>
66 66
67#ifdef OPENSSL_FIPS
68#include <openssl/fips.h>
69#endif
70
67static int dsa_builtin_keygen(DSA *dsa); 71static int dsa_builtin_keygen(DSA *dsa);
68 72
69int DSA_generate_key(DSA *dsa) 73int DSA_generate_key(DSA *dsa)
70 { 74 {
75#ifdef OPENSSL_FIPS
76 if (FIPS_mode() && !(dsa->meth->flags & DSA_FLAG_FIPS_METHOD)
77 && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW))
78 {
79 DSAerr(DSA_F_DSA_GENERATE_KEY, DSA_R_NON_FIPS_DSA_METHOD);
80 return 0;
81 }
82#endif
71 if(dsa->meth->dsa_keygen) 83 if(dsa->meth->dsa_keygen)
72 return dsa->meth->dsa_keygen(dsa); 84 return dsa->meth->dsa_keygen(dsa);
85#ifdef OPENSSL_FIPS
86 if (FIPS_mode())
87 return FIPS_dsa_generate_key(dsa);
88#endif
73 return dsa_builtin_keygen(dsa); 89 return dsa_builtin_keygen(dsa);
74 } 90 }
75 91
diff --git a/src/lib/libcrypto/dsa/dsa_lib.c b/src/lib/libcrypto/dsa/dsa_lib.c
index e9b75902db..96d8d0c4b4 100644
--- a/src/lib/libcrypto/dsa/dsa_lib.c
+++ b/src/lib/libcrypto/dsa/dsa_lib.c
@@ -70,6 +70,10 @@
70#include <openssl/dh.h> 70#include <openssl/dh.h>
71#endif 71#endif
72 72
73#ifdef OPENSSL_FIPS
74#include <openssl/fips.h>
75#endif
76
73const char DSA_version[]="DSA" OPENSSL_VERSION_PTEXT; 77const char DSA_version[]="DSA" OPENSSL_VERSION_PTEXT;
74 78
75static const DSA_METHOD *default_DSA_method = NULL; 79static const DSA_METHOD *default_DSA_method = NULL;
@@ -82,7 +86,16 @@ void DSA_set_default_method(const DSA_METHOD *meth)
82const DSA_METHOD *DSA_get_default_method(void) 86const DSA_METHOD *DSA_get_default_method(void)
83 { 87 {
84 if(!default_DSA_method) 88 if(!default_DSA_method)
89 {
90#ifdef OPENSSL_FIPS
91 if (FIPS_mode())
92 return FIPS_dsa_openssl();
93 else
94 return DSA_OpenSSL();
95#else
85 default_DSA_method = DSA_OpenSSL(); 96 default_DSA_method = DSA_OpenSSL();
97#endif
98 }
86 return default_DSA_method; 99 return default_DSA_method;
87 } 100 }
88 101
@@ -163,7 +176,7 @@ DSA *DSA_new_method(ENGINE *engine)
163 ret->method_mont_p=NULL; 176 ret->method_mont_p=NULL;
164 177
165 ret->references=1; 178 ret->references=1;
166 ret->flags=ret->meth->flags; 179 ret->flags=ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
167 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 180 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);
168 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) 181 if ((ret->meth->init != NULL) && !ret->meth->init(ret))
169 { 182 {
@@ -276,7 +289,8 @@ void *DSA_get_ex_data(DSA *d, int idx)
276DH *DSA_dup_DH(const DSA *r) 289DH *DSA_dup_DH(const DSA *r)
277 { 290 {
278 /* DSA has p, q, g, optional pub_key, optional priv_key. 291 /* DSA has p, q, g, optional pub_key, optional priv_key.
279 * DH has p, optional length, g, optional pub_key, optional priv_key. 292 * DH has p, optional length, g, optional pub_key, optional priv_key,
293 * optional q.
280 */ 294 */
281 295
282 DH *ret = NULL; 296 DH *ret = NULL;
@@ -290,7 +304,11 @@ DH *DSA_dup_DH(const DSA *r)
290 if ((ret->p = BN_dup(r->p)) == NULL) 304 if ((ret->p = BN_dup(r->p)) == NULL)
291 goto err; 305 goto err;
292 if (r->q != NULL) 306 if (r->q != NULL)
307 {
293 ret->length = BN_num_bits(r->q); 308 ret->length = BN_num_bits(r->q);
309 if ((ret->q = BN_dup(r->q)) == NULL)
310 goto err;
311 }
294 if (r->g != NULL) 312 if (r->g != NULL)
295 if ((ret->g = BN_dup(r->g)) == NULL) 313 if ((ret->g = BN_dup(r->g)) == NULL)
296 goto err; 314 goto err;
diff --git a/src/lib/libcrypto/dsa/dsa_ossl.c b/src/lib/libcrypto/dsa/dsa_ossl.c
index a3ddd7d281..b3d78e524c 100644
--- a/src/lib/libcrypto/dsa/dsa_ossl.c
+++ b/src/lib/libcrypto/dsa/dsa_ossl.c
@@ -136,6 +136,7 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
136 BN_CTX *ctx=NULL; 136 BN_CTX *ctx=NULL;
137 int reason=ERR_R_BN_LIB; 137 int reason=ERR_R_BN_LIB;
138 DSA_SIG *ret=NULL; 138 DSA_SIG *ret=NULL;
139 int noredo = 0;
139 140
140 BN_init(&m); 141 BN_init(&m);
141 BN_init(&xr); 142 BN_init(&xr);
@@ -150,7 +151,7 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
150 if (s == NULL) goto err; 151 if (s == NULL) goto err;
151 ctx=BN_CTX_new(); 152 ctx=BN_CTX_new();
152 if (ctx == NULL) goto err; 153 if (ctx == NULL) goto err;
153 154redo:
154 if ((dsa->kinv == NULL) || (dsa->r == NULL)) 155 if ((dsa->kinv == NULL) || (dsa->r == NULL))
155 { 156 {
156 if (!DSA_sign_setup(dsa,ctx,&kinv,&r)) goto err; 157 if (!DSA_sign_setup(dsa,ctx,&kinv,&r)) goto err;
@@ -161,6 +162,7 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
161 dsa->kinv=NULL; 162 dsa->kinv=NULL;
162 r=dsa->r; 163 r=dsa->r;
163 dsa->r=NULL; 164 dsa->r=NULL;
165 noredo = 1;
164 } 166 }
165 167
166 168
@@ -181,6 +183,18 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
181 183
182 ret=DSA_SIG_new(); 184 ret=DSA_SIG_new();
183 if (ret == NULL) goto err; 185 if (ret == NULL) goto err;
186 /* Redo if r or s is zero as required by FIPS 186-3: this is
187 * very unlikely.
188 */
189 if (BN_is_zero(r) || BN_is_zero(s))
190 {
191 if (noredo)
192 {
193 reason = DSA_R_NEED_NEW_SETUP_VALUES;
194 goto err;
195 }
196 goto redo;
197 }
184 ret->r = r; 198 ret->r = r;
185 ret->s = s; 199 ret->s = s;
186 200
diff --git a/src/lib/libcrypto/dsa/dsa_sign.c b/src/lib/libcrypto/dsa/dsa_sign.c
index 17555e5892..c3cc3642ce 100644
--- a/src/lib/libcrypto/dsa/dsa_sign.c
+++ b/src/lib/libcrypto/dsa/dsa_sign.c
@@ -61,30 +61,54 @@
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include <openssl/dsa.h> 62#include <openssl/dsa.h>
63#include <openssl/rand.h> 63#include <openssl/rand.h>
64#include <openssl/bn.h>
64 65
65DSA_SIG * DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) 66DSA_SIG * DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
66 { 67 {
68#ifdef OPENSSL_FIPS
69 if (FIPS_mode() && !(dsa->meth->flags & DSA_FLAG_FIPS_METHOD)
70 && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW))
71 {
72 DSAerr(DSA_F_DSA_DO_SIGN, DSA_R_NON_FIPS_DSA_METHOD);
73 return NULL;
74 }
75#endif
67 return dsa->meth->dsa_do_sign(dgst, dlen, dsa); 76 return dsa->meth->dsa_do_sign(dgst, dlen, dsa);
68 } 77 }
69 78
70int DSA_sign(int type, const unsigned char *dgst, int dlen, unsigned char *sig, 79int DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
71 unsigned int *siglen, DSA *dsa)
72 { 80 {
73 DSA_SIG *s; 81#ifdef OPENSSL_FIPS
74 RAND_seed(dgst, dlen); 82 if (FIPS_mode() && !(dsa->meth->flags & DSA_FLAG_FIPS_METHOD)
75 s=DSA_do_sign(dgst,dlen,dsa); 83 && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW))
76 if (s == NULL)
77 { 84 {
78 *siglen=0; 85 DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_NON_FIPS_DSA_METHOD);
79 return(0); 86 return 0;
80 } 87 }
81 *siglen=i2d_DSA_SIG(s,&sig); 88#endif
82 DSA_SIG_free(s); 89 return dsa->meth->dsa_sign_setup(dsa, ctx_in, kinvp, rp);
83 return(1);
84 } 90 }
85 91
86int DSA_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) 92DSA_SIG *DSA_SIG_new(void)
87 { 93 {
88 return dsa->meth->dsa_sign_setup(dsa, ctx_in, kinvp, rp); 94 DSA_SIG *sig;
95 sig = OPENSSL_malloc(sizeof(DSA_SIG));
96 if (!sig)
97 return NULL;
98 sig->r = NULL;
99 sig->s = NULL;
100 return sig;
101 }
102
103void DSA_SIG_free(DSA_SIG *sig)
104 {
105 if (sig)
106 {
107 if (sig->r)
108 BN_free(sig->r);
109 if (sig->s)
110 BN_free(sig->s);
111 OPENSSL_free(sig);
112 }
89 } 113 }
90 114
diff --git a/src/lib/libcrypto/dsa/dsa_vrf.c b/src/lib/libcrypto/dsa/dsa_vrf.c
index 226a75ff3f..674cb5fa5f 100644
--- a/src/lib/libcrypto/dsa/dsa_vrf.c
+++ b/src/lib/libcrypto/dsa/dsa_vrf.c
@@ -64,26 +64,13 @@
64int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, 64int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,
65 DSA *dsa) 65 DSA *dsa)
66 { 66 {
67#ifdef OPENSSL_FIPS
68 if (FIPS_mode() && !(dsa->meth->flags & DSA_FLAG_FIPS_METHOD)
69 && !(dsa->flags & DSA_FLAG_NON_FIPS_ALLOW))
70 {
71 DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_NON_FIPS_DSA_METHOD);
72 return -1;
73 }
74#endif
67 return dsa->meth->dsa_do_verify(dgst, dgst_len, sig, dsa); 75 return dsa->meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
68 } 76 }
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 */
76int 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);
86err:
87 DSA_SIG_free(s);
88 return(ret);
89 }