summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rsa/rsa_eay.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_eay.c')
-rw-r--r--src/lib/libcrypto/rsa/rsa_eay.c43
1 files changed, 27 insertions, 16 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c
index b7d2460754..8b8a1e279a 100644
--- a/src/lib/libcrypto/rsa/rsa_eay.c
+++ b/src/lib/libcrypto/rsa/rsa_eay.c
@@ -61,6 +61,7 @@
61#include <openssl/bn.h> 61#include <openssl/bn.h>
62#include <openssl/rsa.h> 62#include <openssl/rsa.h>
63#include <openssl/rand.h> 63#include <openssl/rand.h>
64#include <openssl/engine.h>
64 65
65#ifndef RSA_NULL 66#ifndef RSA_NULL
66 67
@@ -97,16 +98,18 @@ RSA_METHOD *RSA_PKCS1_SSLeay(void)
97static int RSA_eay_public_encrypt(int flen, unsigned char *from, 98static int RSA_eay_public_encrypt(int flen, unsigned char *from,
98 unsigned char *to, RSA *rsa, int padding) 99 unsigned char *to, RSA *rsa, int padding)
99 { 100 {
101 const RSA_METHOD *meth;
100 BIGNUM f,ret; 102 BIGNUM f,ret;
101 int i,j,k,num=0,r= -1; 103 int i,j,k,num=0,r= -1;
102 unsigned char *buf=NULL; 104 unsigned char *buf=NULL;
103 BN_CTX *ctx=NULL; 105 BN_CTX *ctx=NULL;
104 106
107 meth = ENGINE_get_RSA(rsa->engine);
105 BN_init(&f); 108 BN_init(&f);
106 BN_init(&ret); 109 BN_init(&ret);
107 if ((ctx=BN_CTX_new()) == NULL) goto err; 110 if ((ctx=BN_CTX_new()) == NULL) goto err;
108 num=BN_num_bytes(rsa->n); 111 num=BN_num_bytes(rsa->n);
109 if ((buf=(unsigned char *)Malloc(num)) == NULL) 112 if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
110 { 113 {
111 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE); 114 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
112 goto err; 115 goto err;
@@ -143,7 +146,7 @@ static int RSA_eay_public_encrypt(int flen, unsigned char *from,
143 goto err; 146 goto err;
144 } 147 }
145 148
146 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, 149 if (!meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
147 rsa->_method_mod_n)) goto err; 150 rsa->_method_mod_n)) goto err;
148 151
149 /* put in leading 0 bytes if the number is less than the 152 /* put in leading 0 bytes if the number is less than the
@@ -161,7 +164,7 @@ err:
161 if (buf != NULL) 164 if (buf != NULL)
162 { 165 {
163 memset(buf,0,num); 166 memset(buf,0,num);
164 Free(buf); 167 OPENSSL_free(buf);
165 } 168 }
166 return(r); 169 return(r);
167 } 170 }
@@ -169,17 +172,19 @@ err:
169static int RSA_eay_private_encrypt(int flen, unsigned char *from, 172static int RSA_eay_private_encrypt(int flen, unsigned char *from,
170 unsigned char *to, RSA *rsa, int padding) 173 unsigned char *to, RSA *rsa, int padding)
171 { 174 {
175 const RSA_METHOD *meth;
172 BIGNUM f,ret; 176 BIGNUM f,ret;
173 int i,j,k,num=0,r= -1; 177 int i,j,k,num=0,r= -1;
174 unsigned char *buf=NULL; 178 unsigned char *buf=NULL;
175 BN_CTX *ctx=NULL; 179 BN_CTX *ctx=NULL;
176 180
181 meth = ENGINE_get_RSA(rsa->engine);
177 BN_init(&f); 182 BN_init(&f);
178 BN_init(&ret); 183 BN_init(&ret);
179 184
180 if ((ctx=BN_CTX_new()) == NULL) goto err; 185 if ((ctx=BN_CTX_new()) == NULL) goto err;
181 num=BN_num_bytes(rsa->n); 186 num=BN_num_bytes(rsa->n);
182 if ((buf=(unsigned char *)Malloc(num)) == NULL) 187 if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
183 { 188 {
184 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE); 189 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
185 goto err; 190 goto err;
@@ -213,10 +218,10 @@ static int RSA_eay_private_encrypt(int flen, unsigned char *from,
213 (rsa->dmp1 != NULL) && 218 (rsa->dmp1 != NULL) &&
214 (rsa->dmq1 != NULL) && 219 (rsa->dmq1 != NULL) &&
215 (rsa->iqmp != NULL)) ) 220 (rsa->iqmp != NULL)) )
216 { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; } 221 { if (!meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
217 else 222 else
218 { 223 {
219 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err; 224 if (!meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err;
220 } 225 }
221 226
222 if (rsa->flags & RSA_FLAG_BLINDING) 227 if (rsa->flags & RSA_FLAG_BLINDING)
@@ -237,7 +242,7 @@ err:
237 if (buf != NULL) 242 if (buf != NULL)
238 { 243 {
239 memset(buf,0,num); 244 memset(buf,0,num);
240 Free(buf); 245 OPENSSL_free(buf);
241 } 246 }
242 return(r); 247 return(r);
243 } 248 }
@@ -245,12 +250,14 @@ err:
245static int RSA_eay_private_decrypt(int flen, unsigned char *from, 250static int RSA_eay_private_decrypt(int flen, unsigned char *from,
246 unsigned char *to, RSA *rsa, int padding) 251 unsigned char *to, RSA *rsa, int padding)
247 { 252 {
253 const RSA_METHOD *meth;
248 BIGNUM f,ret; 254 BIGNUM f,ret;
249 int j,num=0,r= -1; 255 int j,num=0,r= -1;
250 unsigned char *p; 256 unsigned char *p;
251 unsigned char *buf=NULL; 257 unsigned char *buf=NULL;
252 BN_CTX *ctx=NULL; 258 BN_CTX *ctx=NULL;
253 259
260 meth = ENGINE_get_RSA(rsa->engine);
254 BN_init(&f); 261 BN_init(&f);
255 BN_init(&ret); 262 BN_init(&ret);
256 ctx=BN_CTX_new(); 263 ctx=BN_CTX_new();
@@ -258,7 +265,7 @@ static int RSA_eay_private_decrypt(int flen, unsigned char *from,
258 265
259 num=BN_num_bytes(rsa->n); 266 num=BN_num_bytes(rsa->n);
260 267
261 if ((buf=(unsigned char *)Malloc(num)) == NULL) 268 if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
262 { 269 {
263 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE); 270 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
264 goto err; 271 goto err;
@@ -287,10 +294,10 @@ static int RSA_eay_private_decrypt(int flen, unsigned char *from,
287 (rsa->dmp1 != NULL) && 294 (rsa->dmp1 != NULL) &&
288 (rsa->dmq1 != NULL) && 295 (rsa->dmq1 != NULL) &&
289 (rsa->iqmp != NULL)) ) 296 (rsa->iqmp != NULL)) )
290 { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; } 297 { if (!meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
291 else 298 else
292 { 299 {
293 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) 300 if (!meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL))
294 goto err; 301 goto err;
295 } 302 }
296 303
@@ -330,7 +337,7 @@ err:
330 if (buf != NULL) 337 if (buf != NULL)
331 { 338 {
332 memset(buf,0,num); 339 memset(buf,0,num);
333 Free(buf); 340 OPENSSL_free(buf);
334 } 341 }
335 return(r); 342 return(r);
336 } 343 }
@@ -338,19 +345,21 @@ err:
338static int RSA_eay_public_decrypt(int flen, unsigned char *from, 345static int RSA_eay_public_decrypt(int flen, unsigned char *from,
339 unsigned char *to, RSA *rsa, int padding) 346 unsigned char *to, RSA *rsa, int padding)
340 { 347 {
348 const RSA_METHOD *meth;
341 BIGNUM f,ret; 349 BIGNUM f,ret;
342 int i,num=0,r= -1; 350 int i,num=0,r= -1;
343 unsigned char *p; 351 unsigned char *p;
344 unsigned char *buf=NULL; 352 unsigned char *buf=NULL;
345 BN_CTX *ctx=NULL; 353 BN_CTX *ctx=NULL;
346 354
355 meth = ENGINE_get_RSA(rsa->engine);
347 BN_init(&f); 356 BN_init(&f);
348 BN_init(&ret); 357 BN_init(&ret);
349 ctx=BN_CTX_new(); 358 ctx=BN_CTX_new();
350 if (ctx == NULL) goto err; 359 if (ctx == NULL) goto err;
351 360
352 num=BN_num_bytes(rsa->n); 361 num=BN_num_bytes(rsa->n);
353 buf=(unsigned char *)Malloc(num); 362 buf=(unsigned char *)OPENSSL_malloc(num);
354 if (buf == NULL) 363 if (buf == NULL)
355 { 364 {
356 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE); 365 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
@@ -374,7 +383,7 @@ static int RSA_eay_public_decrypt(int flen, unsigned char *from,
374 goto err; 383 goto err;
375 } 384 }
376 385
377 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, 386 if (!meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
378 rsa->_method_mod_n)) goto err; 387 rsa->_method_mod_n)) goto err;
379 388
380 p=buf; 389 p=buf;
@@ -402,17 +411,19 @@ err:
402 if (buf != NULL) 411 if (buf != NULL)
403 { 412 {
404 memset(buf,0,num); 413 memset(buf,0,num);
405 Free(buf); 414 OPENSSL_free(buf);
406 } 415 }
407 return(r); 416 return(r);
408 } 417 }
409 418
410static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa) 419static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)
411 { 420 {
421 const RSA_METHOD *meth;
412 BIGNUM r1,m1; 422 BIGNUM r1,m1;
413 int ret=0; 423 int ret=0;
414 BN_CTX *ctx; 424 BN_CTX *ctx;
415 425
426 meth = ENGINE_get_RSA(rsa->engine);
416 if ((ctx=BN_CTX_new()) == NULL) goto err; 427 if ((ctx=BN_CTX_new()) == NULL) goto err;
417 BN_init(&m1); 428 BN_init(&m1);
418 BN_init(&r1); 429 BN_init(&r1);
@@ -436,11 +447,11 @@ static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)
436 } 447 }
437 448
438 if (!BN_mod(&r1,I,rsa->q,ctx)) goto err; 449 if (!BN_mod(&r1,I,rsa->q,ctx)) goto err;
439 if (!rsa->meth->bn_mod_exp(&m1,&r1,rsa->dmq1,rsa->q,ctx, 450 if (!meth->bn_mod_exp(&m1,&r1,rsa->dmq1,rsa->q,ctx,
440 rsa->_method_mod_q)) goto err; 451 rsa->_method_mod_q)) goto err;
441 452
442 if (!BN_mod(&r1,I,rsa->p,ctx)) goto err; 453 if (!BN_mod(&r1,I,rsa->p,ctx)) goto err;
443 if (!rsa->meth->bn_mod_exp(r0,&r1,rsa->dmp1,rsa->p,ctx, 454 if (!meth->bn_mod_exp(r0,&r1,rsa->dmp1,rsa->p,ctx,
444 rsa->_method_mod_p)) goto err; 455 rsa->_method_mod_p)) goto err;
445 456
446 if (!BN_sub(r0,r0,&m1)) goto err; 457 if (!BN_sub(r0,r0,&m1)) goto err;