diff options
Diffstat (limited to 'src/lib/libcrypto/dsa/dsa_key.c')
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_key.c | 78 |
1 files changed, 41 insertions, 37 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_key.c b/src/lib/libcrypto/dsa/dsa_key.c index 7747ed1416..2d11f59107 100644 --- a/src/lib/libcrypto/dsa/dsa_key.c +++ b/src/lib/libcrypto/dsa/dsa_key.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dsa_key.c,v 1.15 2014/06/12 15:49:28 deraadt Exp $ */ | 1 | /* $OpenBSD: dsa_key.c,v 1.16 2014/07/09 10:16:24 miod Exp $ */ |
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 | * |
@@ -66,63 +66,67 @@ | |||
66 | 66 | ||
67 | static int dsa_builtin_keygen(DSA *dsa); | 67 | static int dsa_builtin_keygen(DSA *dsa); |
68 | 68 | ||
69 | int DSA_generate_key(DSA *dsa) | 69 | int |
70 | { | 70 | DSA_generate_key(DSA *dsa) |
71 | if(dsa->meth->dsa_keygen) | 71 | { |
72 | if (dsa->meth->dsa_keygen) | ||
72 | return dsa->meth->dsa_keygen(dsa); | 73 | return dsa->meth->dsa_keygen(dsa); |
73 | return dsa_builtin_keygen(dsa); | 74 | return dsa_builtin_keygen(dsa); |
74 | } | 75 | } |
75 | 76 | ||
76 | static int dsa_builtin_keygen(DSA *dsa) | 77 | static int |
77 | { | 78 | dsa_builtin_keygen(DSA *dsa) |
78 | int ok=0; | 79 | { |
79 | BN_CTX *ctx=NULL; | 80 | int ok = 0; |
80 | BIGNUM *pub_key=NULL,*priv_key=NULL; | 81 | BN_CTX *ctx = NULL; |
82 | BIGNUM *pub_key = NULL, *priv_key = NULL; | ||
81 | 83 | ||
82 | if ((ctx=BN_CTX_new()) == NULL) goto err; | 84 | if ((ctx = BN_CTX_new()) == NULL) |
85 | goto err; | ||
83 | 86 | ||
84 | if (dsa->priv_key == NULL) | 87 | if (dsa->priv_key == NULL) { |
85 | { | 88 | if ((priv_key = BN_new()) == NULL) |
86 | if ((priv_key=BN_new()) == NULL) goto err; | 89 | goto err; |
87 | } | 90 | } else |
88 | else | ||
89 | priv_key=dsa->priv_key; | 91 | priv_key=dsa->priv_key; |
90 | 92 | ||
91 | do | 93 | do { |
92 | if (!BN_rand_range(priv_key,dsa->q)) goto err; | 94 | if (!BN_rand_range(priv_key, dsa->q)) |
93 | while (BN_is_zero(priv_key)); | 95 | goto err; |
96 | } while (BN_is_zero(priv_key)); | ||
94 | 97 | ||
95 | if (dsa->pub_key == NULL) | 98 | if (dsa->pub_key == NULL) { |
96 | { | 99 | if ((pub_key = BN_new()) == NULL) |
97 | if ((pub_key=BN_new()) == NULL) goto err; | 100 | goto err; |
98 | } | 101 | } else |
99 | else | ||
100 | pub_key=dsa->pub_key; | 102 | pub_key=dsa->pub_key; |
101 | 103 | ||
102 | { | 104 | { |
103 | BIGNUM local_prk; | 105 | BIGNUM local_prk; |
104 | BIGNUM *prk; | 106 | BIGNUM *prk; |
105 | 107 | ||
106 | if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) | 108 | if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) { |
107 | { | ||
108 | BN_init(&local_prk); | 109 | BN_init(&local_prk); |
109 | prk = &local_prk; | 110 | prk = &local_prk; |
110 | BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME); | 111 | BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME); |
111 | } | 112 | } else |
112 | else | ||
113 | prk = priv_key; | 113 | prk = priv_key; |
114 | 114 | ||
115 | if (!BN_mod_exp(pub_key,dsa->g,prk,dsa->p,ctx)) goto err; | 115 | if (!BN_mod_exp(pub_key, dsa->g, prk, dsa->p, ctx)) |
116 | goto err; | ||
116 | } | 117 | } |
117 | 118 | ||
118 | dsa->priv_key=priv_key; | 119 | dsa->priv_key = priv_key; |
119 | dsa->pub_key=pub_key; | 120 | dsa->pub_key = pub_key; |
120 | ok=1; | 121 | ok = 1; |
121 | 122 | ||
122 | err: | 123 | err: |
123 | if ((pub_key != NULL) && (dsa->pub_key == NULL)) BN_free(pub_key); | 124 | if (pub_key != NULL && dsa->pub_key == NULL) |
124 | if ((priv_key != NULL) && (dsa->priv_key == NULL)) BN_free(priv_key); | 125 | BN_free(pub_key); |
125 | if (ctx != NULL) BN_CTX_free(ctx); | 126 | if (priv_key != NULL && dsa->priv_key == NULL) |
126 | return(ok); | 127 | BN_free(priv_key); |
127 | } | 128 | if (ctx != NULL) |
129 | BN_CTX_free(ctx); | ||
130 | return ok; | ||
131 | } | ||
128 | #endif | 132 | #endif |