summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_recp.c
diff options
context:
space:
mode:
authorbeck <>2000-03-19 11:13:58 +0000
committerbeck <>2000-03-19 11:13:58 +0000
commit796d609550df3a33fc11468741c5d2f6d3df4c11 (patch)
tree6c6d539061caa20372dad0ac4ddb1dfae2fbe7fe /src/lib/libcrypto/bn/bn_recp.c
parent5be3114c1fd7e0dfea1e38d3abb4cbba75244419 (diff)
downloadopenbsd-796d609550df3a33fc11468741c5d2f6d3df4c11.tar.gz
openbsd-796d609550df3a33fc11468741c5d2f6d3df4c11.tar.bz2
openbsd-796d609550df3a33fc11468741c5d2f6d3df4c11.zip
OpenSSL 0.9.5 merge
*warning* this bumps shared lib minors for libssl and libcrypto from 2.1 to 2.2 if you are using the ssl26 packages for ssh and other things to work you will need to get new ones (see ~beck/libsslsnap/<arch>) on cvs or ~beck/src-patent.tar.gz on cvs
Diffstat (limited to 'src/lib/libcrypto/bn/bn_recp.c')
-rw-r--r--src/lib/libcrypto/bn/bn_recp.c43
1 files changed, 18 insertions, 25 deletions
diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c
index c1b0e230ea..a8796bd0aa 100644
--- a/src/lib/libcrypto/bn/bn_recp.c
+++ b/src/lib/libcrypto/bn/bn_recp.c
@@ -106,7 +106,8 @@ int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *x, BIGNUM *y, BN_RECP_CTX *recp,
106 int ret=0; 106 int ret=0;
107 BIGNUM *a; 107 BIGNUM *a;
108 108
109 a= &(ctx->bn[ctx->tos++]); 109 BN_CTX_start(ctx);
110 if ((a = BN_CTX_get(ctx)) == NULL) goto err;
110 if (y != NULL) 111 if (y != NULL)
111 { 112 {
112 if (x == y) 113 if (x == y)
@@ -120,33 +121,34 @@ int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *x, BIGNUM *y, BN_RECP_CTX *recp,
120 BN_div_recp(NULL,r,a,recp,ctx); 121 BN_div_recp(NULL,r,a,recp,ctx);
121 ret=1; 122 ret=1;
122err: 123err:
123 ctx->tos--; 124 BN_CTX_end(ctx);
124 return(ret); 125 return(ret);
125 } 126 }
126 127
127int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m, BN_RECP_CTX *recp, 128int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m, BN_RECP_CTX *recp,
128 BN_CTX *ctx) 129 BN_CTX *ctx)
129 { 130 {
130 int i,j,tos,ret=0,ex; 131 int i,j,ret=0;
131 BIGNUM *a,*b,*d,*r; 132 BIGNUM *a,*b,*d,*r;
132 133
133 tos=ctx->tos; 134 BN_CTX_start(ctx);
134 a= &(ctx->bn[ctx->tos++]); 135 a=BN_CTX_get(ctx);
135 b= &(ctx->bn[ctx->tos++]); 136 b=BN_CTX_get(ctx);
136 if (dv != NULL) 137 if (dv != NULL)
137 d=dv; 138 d=dv;
138 else 139 else
139 d= &(ctx->bn[ctx->tos++]); 140 d=BN_CTX_get(ctx);
140 if (rem != NULL) 141 if (rem != NULL)
141 r=rem; 142 r=rem;
142 else 143 else
143 r= &(ctx->bn[ctx->tos++]); 144 r=BN_CTX_get(ctx);
145 if (a == NULL || b == NULL || d == NULL || r == NULL) goto err;
144 146
145 if (BN_ucmp(m,&(recp->N)) < 0) 147 if (BN_ucmp(m,&(recp->N)) < 0)
146 { 148 {
147 BN_zero(d); 149 BN_zero(d);
148 BN_copy(r,m); 150 BN_copy(r,m);
149 ctx->tos=tos; 151 BN_CTX_end(ctx);
150 return(1); 152 return(1);
151 } 153 }
152 154
@@ -157,33 +159,24 @@ int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m, BN_RECP_CTX *recp,
157 */ 159 */
158 i=BN_num_bits(m); 160 i=BN_num_bits(m);
159 161
160 j=recp->num_bits*2; 162 j=recp->num_bits<<1;
161 if (j > i) 163 if (j>i) i=j;
162 { 164 j>>=1;
163 i=j;
164 ex=0;
165 }
166 else
167 {
168 ex=(i-j)/2;
169 }
170
171 j=i/2;
172 165
173 if (i != recp->shift) 166 if (i != recp->shift)
174 recp->shift=BN_reciprocal(&(recp->Nr),&(recp->N), 167 recp->shift=BN_reciprocal(&(recp->Nr),&(recp->N),
175 i,ctx); 168 i,ctx);
176 169
177 if (!BN_rshift(a,m,j-ex)) goto err; 170 if (!BN_rshift(a,m,j)) goto err;
178 if (!BN_mul(b,a,&(recp->Nr),ctx)) goto err; 171 if (!BN_mul(b,a,&(recp->Nr),ctx)) goto err;
179 if (!BN_rshift(d,b,j+ex)) goto err; 172 if (!BN_rshift(d,b,i-j)) goto err;
180 d->neg=0; 173 d->neg=0;
181 if (!BN_mul(b,&(recp->N),d,ctx)) goto err; 174 if (!BN_mul(b,&(recp->N),d,ctx)) goto err;
182 if (!BN_usub(r,m,b)) goto err; 175 if (!BN_usub(r,m,b)) goto err;
183 r->neg=0; 176 r->neg=0;
184 177
185 j=0;
186#if 1 178#if 1
179 j=0;
187 while (BN_ucmp(r,&(recp->N)) >= 0) 180 while (BN_ucmp(r,&(recp->N)) >= 0)
188 { 181 {
189 if (j++ > 2) 182 if (j++ > 2)
@@ -200,7 +193,7 @@ int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m, BN_RECP_CTX *recp,
200 d->neg=m->neg^recp->N.neg; 193 d->neg=m->neg^recp->N.neg;
201 ret=1; 194 ret=1;
202err: 195err:
203 ctx->tos=tos; 196 BN_CTX_end(ctx);
204 return(ret); 197 return(ret);
205 } 198 }
206 199