diff options
Diffstat (limited to 'src/lib/libcrypto/dh/dh_key.c')
-rw-r--r-- | src/lib/libcrypto/dh/dh_key.c | 65 |
1 files changed, 61 insertions, 4 deletions
diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c index cede53bfc1..0c7eeaf260 100644 --- a/src/lib/libcrypto/dh/dh_key.c +++ b/src/lib/libcrypto/dh/dh_key.c | |||
@@ -62,8 +62,42 @@ | |||
62 | #include <openssl/rand.h> | 62 | #include <openssl/rand.h> |
63 | #include <openssl/dh.h> | 63 | #include <openssl/dh.h> |
64 | 64 | ||
65 | static int generate_key(DH *dh); | ||
66 | static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh); | ||
67 | static int dh_bn_mod_exp(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p, | ||
68 | const BIGNUM *m, BN_CTX *ctx, | ||
69 | BN_MONT_CTX *m_ctx); | ||
70 | static int dh_init(DH *dh); | ||
71 | static int dh_finish(DH *dh); | ||
72 | |||
65 | int DH_generate_key(DH *dh) | 73 | int DH_generate_key(DH *dh) |
66 | { | 74 | { |
75 | return dh->meth->generate_key(dh); | ||
76 | } | ||
77 | |||
78 | int DH_compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh) | ||
79 | { | ||
80 | return dh->meth->compute_key(key, pub_key, dh); | ||
81 | } | ||
82 | |||
83 | static DH_METHOD dh_ossl = { | ||
84 | "OpenSSL DH Method", | ||
85 | generate_key, | ||
86 | compute_key, | ||
87 | dh_bn_mod_exp, | ||
88 | dh_init, | ||
89 | dh_finish, | ||
90 | 0, | ||
91 | NULL | ||
92 | }; | ||
93 | |||
94 | DH_METHOD *DH_OpenSSL(void) | ||
95 | { | ||
96 | return &dh_ossl; | ||
97 | } | ||
98 | |||
99 | static int generate_key(DH *dh) | ||
100 | { | ||
67 | int ok=0; | 101 | int ok=0; |
68 | unsigned int i; | 102 | unsigned int i; |
69 | BN_CTX ctx; | 103 | BN_CTX ctx; |
@@ -103,7 +137,8 @@ int DH_generate_key(DH *dh) | |||
103 | } | 137 | } |
104 | mont=(BN_MONT_CTX *)dh->method_mont_p; | 138 | mont=(BN_MONT_CTX *)dh->method_mont_p; |
105 | 139 | ||
106 | if (!BN_mod_exp_mont(pub_key,dh->g,priv_key,dh->p,&ctx,mont)) goto err; | 140 | if (!dh->meth->bn_mod_exp(dh, pub_key,dh->g,priv_key,dh->p,&ctx,mont)) |
141 | goto err; | ||
107 | 142 | ||
108 | dh->pub_key=pub_key; | 143 | dh->pub_key=pub_key; |
109 | dh->priv_key=priv_key; | 144 | dh->priv_key=priv_key; |
@@ -118,7 +153,7 @@ err: | |||
118 | return(ok); | 153 | return(ok); |
119 | } | 154 | } |
120 | 155 | ||
121 | int DH_compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh) | 156 | static int compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh) |
122 | { | 157 | { |
123 | BN_CTX ctx; | 158 | BN_CTX ctx; |
124 | BN_MONT_CTX *mont; | 159 | BN_MONT_CTX *mont; |
@@ -126,7 +161,8 @@ int DH_compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh) | |||
126 | int ret= -1; | 161 | int ret= -1; |
127 | 162 | ||
128 | BN_CTX_init(&ctx); | 163 | BN_CTX_init(&ctx); |
129 | tmp= &(ctx.bn[ctx.tos++]); | 164 | BN_CTX_start(&ctx); |
165 | tmp = BN_CTX_get(&ctx); | ||
130 | 166 | ||
131 | if (dh->priv_key == NULL) | 167 | if (dh->priv_key == NULL) |
132 | { | 168 | { |
@@ -141,7 +177,7 @@ int DH_compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh) | |||
141 | } | 177 | } |
142 | 178 | ||
143 | mont=(BN_MONT_CTX *)dh->method_mont_p; | 179 | mont=(BN_MONT_CTX *)dh->method_mont_p; |
144 | if (!BN_mod_exp_mont(tmp,pub_key,dh->priv_key,dh->p,&ctx,mont)) | 180 | if (!dh->meth->bn_mod_exp(dh, tmp,pub_key,dh->priv_key,dh->p,&ctx,mont)) |
145 | { | 181 | { |
146 | DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); | 182 | DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); |
147 | goto err; | 183 | goto err; |
@@ -149,6 +185,27 @@ int DH_compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh) | |||
149 | 185 | ||
150 | ret=BN_bn2bin(tmp,key); | 186 | ret=BN_bn2bin(tmp,key); |
151 | err: | 187 | err: |
188 | BN_CTX_end(&ctx); | ||
152 | BN_CTX_free(&ctx); | 189 | BN_CTX_free(&ctx); |
153 | return(ret); | 190 | return(ret); |
154 | } | 191 | } |
192 | |||
193 | static int dh_bn_mod_exp(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p, | ||
194 | const BIGNUM *m, BN_CTX *ctx, | ||
195 | BN_MONT_CTX *m_ctx) | ||
196 | { | ||
197 | return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx); | ||
198 | } | ||
199 | |||
200 | static int dh_init(DH *dh) | ||
201 | { | ||
202 | dh->flags |= DH_FLAG_CACHE_MONT_P; | ||
203 | return(1); | ||
204 | } | ||
205 | |||
206 | static int dh_finish(DH *dh) | ||
207 | { | ||
208 | if(dh->method_mont_p) | ||
209 | BN_MONT_CTX_free((BN_MONT_CTX *)dh->method_mont_p); | ||
210 | return(1); | ||
211 | } | ||