summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_gcd.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bn/bn_gcd.c')
-rw-r--r--src/lib/libcrypto/bn/bn_gcd.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/src/lib/libcrypto/bn/bn_gcd.c b/src/lib/libcrypto/bn/bn_gcd.c
index 64a76f4498..398207196b 100644
--- a/src/lib/libcrypto/bn/bn_gcd.c
+++ b/src/lib/libcrypto/bn/bn_gcd.c
@@ -61,6 +61,7 @@
61#include "bn_lcl.h" 61#include "bn_lcl.h"
62 62
63static BIGNUM *euclid(BIGNUM *a, BIGNUM *b); 63static BIGNUM *euclid(BIGNUM *a, BIGNUM *b);
64
64int BN_gcd(BIGNUM *r, BIGNUM *in_a, BIGNUM *in_b, BN_CTX *ctx) 65int BN_gcd(BIGNUM *r, BIGNUM *in_a, BIGNUM *in_b, BN_CTX *ctx)
65 { 66 {
66 BIGNUM *a,*b,*t; 67 BIGNUM *a,*b,*t;
@@ -69,8 +70,10 @@ int BN_gcd(BIGNUM *r, BIGNUM *in_a, BIGNUM *in_b, BN_CTX *ctx)
69 bn_check_top(in_a); 70 bn_check_top(in_a);
70 bn_check_top(in_b); 71 bn_check_top(in_b);
71 72
72 a= &(ctx->bn[ctx->tos]); 73 BN_CTX_start(ctx);
73 b= &(ctx->bn[ctx->tos+1]); 74 a = BN_CTX_get(ctx);
75 b = BN_CTX_get(ctx);
76 if (a == NULL || b == NULL) goto err;
74 77
75 if (BN_copy(a,in_a) == NULL) goto err; 78 if (BN_copy(a,in_a) == NULL) goto err;
76 if (BN_copy(b,in_b) == NULL) goto err; 79 if (BN_copy(b,in_b) == NULL) goto err;
@@ -82,6 +85,7 @@ int BN_gcd(BIGNUM *r, BIGNUM *in_a, BIGNUM *in_b, BN_CTX *ctx)
82 if (BN_copy(r,t) == NULL) goto err; 85 if (BN_copy(r,t) == NULL) goto err;
83 ret=1; 86 ret=1;
84err: 87err:
88 BN_CTX_end(ctx);
85 return(ret); 89 return(ret);
86 } 90 }
87 91
@@ -142,20 +146,22 @@ err:
142/* solves ax == 1 (mod n) */ 146/* solves ax == 1 (mod n) */
143BIGNUM *BN_mod_inverse(BIGNUM *in, BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) 147BIGNUM *BN_mod_inverse(BIGNUM *in, BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
144 { 148 {
145 BIGNUM *A,*B,*X,*Y,*M,*D,*R; 149 BIGNUM *A,*B,*X,*Y,*M,*D,*R=NULL;
146 BIGNUM *T,*ret=NULL; 150 BIGNUM *T,*ret=NULL;
147 int sign; 151 int sign;
148 152
149 bn_check_top(a); 153 bn_check_top(a);
150 bn_check_top(n); 154 bn_check_top(n);
151 155
152 A= &(ctx->bn[ctx->tos]); 156 BN_CTX_start(ctx);
153 B= &(ctx->bn[ctx->tos+1]); 157 A = BN_CTX_get(ctx);
154 X= &(ctx->bn[ctx->tos+2]); 158 B = BN_CTX_get(ctx);
155 D= &(ctx->bn[ctx->tos+3]); 159 X = BN_CTX_get(ctx);
156 M= &(ctx->bn[ctx->tos+4]); 160 D = BN_CTX_get(ctx);
157 Y= &(ctx->bn[ctx->tos+5]); 161 M = BN_CTX_get(ctx);
158 ctx->tos+=6; 162 Y = BN_CTX_get(ctx);
163 if (Y == NULL) goto err;
164
159 if (in == NULL) 165 if (in == NULL)
160 R=BN_new(); 166 R=BN_new();
161 else 167 else
@@ -198,7 +204,7 @@ BIGNUM *BN_mod_inverse(BIGNUM *in, BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
198 ret=R; 204 ret=R;
199err: 205err:
200 if ((ret == NULL) && (in == NULL)) BN_free(R); 206 if ((ret == NULL) && (in == NULL)) BN_free(R);
201 ctx->tos-=6; 207 BN_CTX_end(ctx);
202 return(ret); 208 return(ret);
203 } 209 }
204 210