Constify the BIGNUM routines a bit more. The only trouble were the
[openssl.git] / crypto / bn / bn_gcd.c
index c80cecdc8de9eb125052016a7a52c8b2a7aa08ff..61c431fc7b4d1864d3df2cae482cf8ec018a8adb 100644 (file)
 #include "cryptlib.h"
 #include "bn_lcl.h"
 
-#ifndef NOPROTO
 static BIGNUM *euclid(BIGNUM *a, BIGNUM *b);
-#else
-static BIGNUM *euclid();
-#endif
 
-int BN_gcd(r,in_a,in_b,ctx)
-BIGNUM *r,*in_a,*in_b;
-BN_CTX *ctx;
+int BN_gcd(BIGNUM *r, BIGNUM *in_a, BIGNUM *in_b, BN_CTX *ctx)
        {
        BIGNUM *a,*b,*t;
        int ret=0;
@@ -76,8 +70,10 @@ BN_CTX *ctx;
        bn_check_top(in_a);
        bn_check_top(in_b);
 
-       a= &(ctx->bn[ctx->tos]);
-       b= &(ctx->bn[ctx->tos+1]);
+       BN_CTX_start(ctx);
+       a = BN_CTX_get(ctx);
+       b = BN_CTX_get(ctx);
+       if (a == NULL || b == NULL) goto err;
 
        if (BN_copy(a,in_a) == NULL) goto err;
        if (BN_copy(b,in_b) == NULL) goto err;
@@ -89,11 +85,11 @@ BN_CTX *ctx;
        if (BN_copy(r,t) == NULL) goto err;
        ret=1;
 err:
+       BN_CTX_end(ctx);
        return(ret);
        }
 
-static BIGNUM *euclid(a,b)
-BIGNUM *a,*b;
+static BIGNUM *euclid(BIGNUM *a, BIGNUM *b)
        {
        BIGNUM *t;
        int shifts=0;
@@ -148,26 +144,25 @@ err:
        }
 
 /* solves ax == 1 (mod n) */
-BIGNUM *BN_mod_inverse(in, a, n, ctx)
-BIGNUM *in;
-BIGNUM *a;
-BIGNUM *n;
-BN_CTX *ctx;
+BIGNUM *BN_mod_inverse(BIGNUM *in,
+       const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
        {
-       BIGNUM *A,*B,*X,*Y,*M,*D,*R;
+       BIGNUM *A,*B,*X,*Y,*M,*D,*R=NULL;
        BIGNUM *T,*ret=NULL;
        int sign;
 
        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;
+       BN_CTX_start(ctx);
+       A = BN_CTX_get(ctx);
+       B = BN_CTX_get(ctx);
+       X = BN_CTX_get(ctx);
+       D = BN_CTX_get(ctx);
+       M = BN_CTX_get(ctx);
+       Y = BN_CTX_get(ctx);
+       if (Y == NULL) goto err;
+
        if (in == NULL)
                R=BN_new();
        else
@@ -210,7 +205,7 @@ BN_CTX *ctx;
        ret=R;
 err:
        if ((ret == NULL) && (in == NULL)) BN_free(R);
-       ctx->tos-=6;
+       BN_CTX_end(ctx);
        return(ret);
        }