diff options
Diffstat (limited to 'crypto/bn/bn_gcd.c')
-rw-r--r-- | crypto/bn/bn_gcd.c | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/crypto/bn/bn_gcd.c b/crypto/bn/bn_gcd.c index 071bba3b4b..c80cecdc8d 100644 --- a/crypto/bn/bn_gcd.c +++ b/crypto/bn/bn_gcd.c @@ -73,8 +73,11 @@ BN_CTX *ctx; BIGNUM *a,*b,*t; int ret=0; - a=ctx->bn[ctx->tos]; - b=ctx->bn[ctx->tos+1]; + bn_check_top(in_a); + bn_check_top(in_b); + + a= &(ctx->bn[ctx->tos]); + b= &(ctx->bn[ctx->tos+1]); if (BN_copy(a,in_a) == NULL) goto err; if (BN_copy(b,in_b) == NULL) goto err; @@ -95,6 +98,9 @@ BIGNUM *a,*b; BIGNUM *t; int shifts=0; + bn_check_top(a); + bn_check_top(b); + for (;;) { if (BN_is_zero(b)) @@ -142,23 +148,30 @@ err: } /* solves ax == 1 (mod n) */ -BIGNUM *BN_mod_inverse(a, n, ctx) +BIGNUM *BN_mod_inverse(in, a, n, ctx) +BIGNUM *in; BIGNUM *a; BIGNUM *n; BN_CTX *ctx; { BIGNUM *A,*B,*X,*Y,*M,*D,*R; - BIGNUM *ret=NULL,*T; + BIGNUM *T,*ret=NULL; int sign; - A=ctx->bn[ctx->tos]; - B=ctx->bn[ctx->tos+1]; - X=ctx->bn[ctx->tos+2]; - D=ctx->bn[ctx->tos+3]; - M=ctx->bn[ctx->tos+4]; - Y=ctx->bn[ctx->tos+5]; + bn_check_top(a); + bn_check_top(n); + + A= &(ctx->bn[ctx->tos]); + B= &(ctx->bn[ctx->tos+1]); + X= &(ctx->bn[ctx->tos+2]); + D= &(ctx->bn[ctx->tos+3]); + M= &(ctx->bn[ctx->tos+4]); + Y= &(ctx->bn[ctx->tos+5]); ctx->tos+=6; - R=BN_new(); + if (in == NULL) + R=BN_new(); + else + R=in; if (R == NULL) goto err; BN_zero(X); @@ -175,7 +188,7 @@ BN_CTX *ctx; B=M; /* T has a struct, M does not */ - if (!BN_mul(T,D,X)) goto err; + if (!BN_mul(T,D,X,ctx)) goto err; if (!BN_add(T,T,Y)) goto err; M=Y; Y=X; @@ -196,7 +209,7 @@ BN_CTX *ctx; } ret=R; err: - if ((ret == NULL) && (R != NULL)) BN_free(R); + if ((ret == NULL) && (in == NULL)) BN_free(R); ctx->tos-=6; return(ret); } |