summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authormarkus <>2007-08-21 21:03:46 +0000
committermarkus <>2007-08-21 21:03:46 +0000
commit7c27b50830c6058e8a7d8f2a3398c9c2f429b9f4 (patch)
treeba80337311f9b71517bd30b30349673438e220c4 /src/lib
parent335adc97a1cc8992c990307f49239ee1655b559b (diff)
downloadopenbsd-7c27b50830c6058e8a7d8f2a3398c9c2f429b9f4.tar.gz
openbsd-7c27b50830c6058e8a7d8f2a3398c9c2f429b9f4.tar.bz2
openbsd-7c27b50830c6058e8a7d8f2a3398c9c2f429b9f4.zip
http://openssl.org/news/patch-CVE-2007-3108.txt; ok pval, deraadt
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/bn/bn_mont.c78
-rw-r--r--src/lib/libssl/src/crypto/bn/bn_mont.c78
2 files changed, 130 insertions, 26 deletions
diff --git a/src/lib/libcrypto/bn/bn_mont.c b/src/lib/libcrypto/bn/bn_mont.c
index 3572e5a690..726d5f2b1b 100644
--- a/src/lib/libcrypto/bn/bn_mont.c
+++ b/src/lib/libcrypto/bn/bn_mont.c
@@ -122,7 +122,6 @@ int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
122 122
123 max=(nl+al+1); /* allow for overflow (no?) XXX */ 123 max=(nl+al+1); /* allow for overflow (no?) XXX */
124 if (bn_wexpand(r,max) == NULL) goto err; 124 if (bn_wexpand(r,max) == NULL) goto err;
125 if (bn_wexpand(ret,max) == NULL) goto err;
126 125
127 r->neg=a->neg^n->neg; 126 r->neg=a->neg^n->neg;
128 np=n->d; 127 np=n->d;
@@ -174,19 +173,70 @@ int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
174 } 173 }
175 bn_fix_top(r); 174 bn_fix_top(r);
176 175
177 /* mont->ri will be a multiple of the word size */ 176 /* mont->ri will be a multiple of the word size and below code
178#if 0 177 * is kind of BN_rshift(ret,r,mont->ri) equivalent */
179 BN_rshift(ret,r,mont->ri); 178 if (r->top <= ri)
180#else 179 {
181 ret->neg = r->neg; 180 ret->top=0;
182 x=ri; 181 retn=1;
182 goto err;
183 }
184 al=r->top-ri;
185
186# define BRANCH_FREE 1
187# if BRANCH_FREE
188 if (bn_wexpand(ret,ri) == NULL) goto err;
189 x=0-(((al-ri)>>(sizeof(al)*8-1))&1);
190 ret->top=x=(ri&~x)|(al&x); /* min(ri,al) */
191 ret->neg=r->neg;
192
183 rp=ret->d; 193 rp=ret->d;
184 ap= &(r->d[x]); 194 ap=&(r->d[ri]);
185 if (r->top < x) 195
186 al=0; 196 {
187 else 197 size_t m1,m2;
188 al=r->top-x; 198
199 v=bn_sub_words(rp,ap,np,ri);
200 /* this ----------------^^ works even in al<ri case
201 * thanks to zealous zeroing of top of the vector in the
202 * beginning. */
203
204 /* if (al==ri && !v) || al>ri) nrp=rp; else nrp=ap; */
205 /* in other words if subtraction result is real, then
206 * trick unconditional memcpy below to perform in-place
207 * "refresh" instead of actual copy. */
208 m1=0-(size_t)(((al-ri)>>(sizeof(al)*8-1))&1); /* al<ri */
209 m2=0-(size_t)(((ri-al)>>(sizeof(al)*8-1))&1); /* al>ri */
210 m1|=m2; /* (al!=ri) */
211 m1|=(0-(size_t)v); /* (al!=ri || v) */
212 m1&=~m2; /* (al!=ri || v) && !al>ri */
213 nrp=(BN_ULONG *)(((size_t)rp&~m1)|((size_t)ap&m1));
214 }
215
216 /* 'i<ri' is chosen to eliminate dependency on input data, even
217 * though it results in redundant copy in al<ri case. */
218 for (i=0,ri-=4; i<ri; i+=4)
219 {
220 BN_ULONG t1,t2,t3,t4;
221
222 t1=nrp[i+0];
223 t2=nrp[i+1];
224 t3=nrp[i+2]; ap[i+0]=0;
225 t4=nrp[i+3]; ap[i+1]=0;
226 rp[i+0]=t1; ap[i+2]=0;
227 rp[i+1]=t2; ap[i+3]=0;
228 rp[i+2]=t3;
229 rp[i+3]=t4;
230 }
231 for (ri+=4; i<ri; i++)
232 rp[i]=nrp[i], ap[i]=0;
233# else
234 if (bn_wexpand(ret,al) == NULL) goto err;
189 ret->top=al; 235 ret->top=al;
236 ret->neg=r->neg;
237
238 rp=ret->d;
239 ap=&(r->d[ri]);
190 al-=4; 240 al-=4;
191 for (i=0; i<al; i+=4) 241 for (i=0; i<al; i+=4)
192 { 242 {
@@ -204,7 +254,7 @@ int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
204 al+=4; 254 al+=4;
205 for (; i<al; i++) 255 for (; i<al; i++)
206 rp[i]=ap[i]; 256 rp[i]=ap[i];
207#endif 257# endif
208#else /* !MONT_WORD */ 258#else /* !MONT_WORD */
209 BIGNUM *t1,*t2; 259 BIGNUM *t1,*t2;
210 260
@@ -224,10 +274,12 @@ int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
224 if (!BN_rshift(ret,t2,mont->ri)) goto err; 274 if (!BN_rshift(ret,t2,mont->ri)) goto err;
225#endif /* MONT_WORD */ 275#endif /* MONT_WORD */
226 276
277#if !defined(BRANCH_FREE) || BRANCH_FREE==0
227 if (BN_ucmp(ret, &(mont->N)) >= 0) 278 if (BN_ucmp(ret, &(mont->N)) >= 0)
228 { 279 {
229 if (!BN_usub(ret,ret,&(mont->N))) goto err; 280 if (!BN_usub(ret,ret,&(mont->N))) goto err;
230 } 281 }
282#endif
231 retn=1; 283 retn=1;
232 err: 284 err:
233 BN_CTX_end(ctx); 285 BN_CTX_end(ctx);
diff --git a/src/lib/libssl/src/crypto/bn/bn_mont.c b/src/lib/libssl/src/crypto/bn/bn_mont.c
index 3572e5a690..726d5f2b1b 100644
--- a/src/lib/libssl/src/crypto/bn/bn_mont.c
+++ b/src/lib/libssl/src/crypto/bn/bn_mont.c
@@ -122,7 +122,6 @@ int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
122 122
123 max=(nl+al+1); /* allow for overflow (no?) XXX */ 123 max=(nl+al+1); /* allow for overflow (no?) XXX */
124 if (bn_wexpand(r,max) == NULL) goto err; 124 if (bn_wexpand(r,max) == NULL) goto err;
125 if (bn_wexpand(ret,max) == NULL) goto err;
126 125
127 r->neg=a->neg^n->neg; 126 r->neg=a->neg^n->neg;
128 np=n->d; 127 np=n->d;
@@ -174,19 +173,70 @@ int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
174 } 173 }
175 bn_fix_top(r); 174 bn_fix_top(r);
176 175
177 /* mont->ri will be a multiple of the word size */ 176 /* mont->ri will be a multiple of the word size and below code
178#if 0 177 * is kind of BN_rshift(ret,r,mont->ri) equivalent */
179 BN_rshift(ret,r,mont->ri); 178 if (r->top <= ri)
180#else 179 {
181 ret->neg = r->neg; 180 ret->top=0;
182 x=ri; 181 retn=1;
182 goto err;
183 }
184 al=r->top-ri;
185
186# define BRANCH_FREE 1
187# if BRANCH_FREE
188 if (bn_wexpand(ret,ri) == NULL) goto err;
189 x=0-(((al-ri)>>(sizeof(al)*8-1))&1);
190 ret->top=x=(ri&~x)|(al&x); /* min(ri,al) */
191 ret->neg=r->neg;
192
183 rp=ret->d; 193 rp=ret->d;
184 ap= &(r->d[x]); 194 ap=&(r->d[ri]);
185 if (r->top < x) 195
186 al=0; 196 {
187 else 197 size_t m1,m2;
188 al=r->top-x; 198
199 v=bn_sub_words(rp,ap,np,ri);
200 /* this ----------------^^ works even in al<ri case
201 * thanks to zealous zeroing of top of the vector in the
202 * beginning. */
203
204 /* if (al==ri && !v) || al>ri) nrp=rp; else nrp=ap; */
205 /* in other words if subtraction result is real, then
206 * trick unconditional memcpy below to perform in-place
207 * "refresh" instead of actual copy. */
208 m1=0-(size_t)(((al-ri)>>(sizeof(al)*8-1))&1); /* al<ri */
209 m2=0-(size_t)(((ri-al)>>(sizeof(al)*8-1))&1); /* al>ri */
210 m1|=m2; /* (al!=ri) */
211 m1|=(0-(size_t)v); /* (al!=ri || v) */
212 m1&=~m2; /* (al!=ri || v) && !al>ri */
213 nrp=(BN_ULONG *)(((size_t)rp&~m1)|((size_t)ap&m1));
214 }
215
216 /* 'i<ri' is chosen to eliminate dependency on input data, even
217 * though it results in redundant copy in al<ri case. */
218 for (i=0,ri-=4; i<ri; i+=4)
219 {
220 BN_ULONG t1,t2,t3,t4;
221
222 t1=nrp[i+0];
223 t2=nrp[i+1];
224 t3=nrp[i+2]; ap[i+0]=0;
225 t4=nrp[i+3]; ap[i+1]=0;
226 rp[i+0]=t1; ap[i+2]=0;
227 rp[i+1]=t2; ap[i+3]=0;
228 rp[i+2]=t3;
229 rp[i+3]=t4;
230 }
231 for (ri+=4; i<ri; i++)
232 rp[i]=nrp[i], ap[i]=0;
233# else
234 if (bn_wexpand(ret,al) == NULL) goto err;
189 ret->top=al; 235 ret->top=al;
236 ret->neg=r->neg;
237
238 rp=ret->d;
239 ap=&(r->d[ri]);
190 al-=4; 240 al-=4;
191 for (i=0; i<al; i+=4) 241 for (i=0; i<al; i+=4)
192 { 242 {
@@ -204,7 +254,7 @@ int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
204 al+=4; 254 al+=4;
205 for (; i<al; i++) 255 for (; i<al; i++)
206 rp[i]=ap[i]; 256 rp[i]=ap[i];
207#endif 257# endif
208#else /* !MONT_WORD */ 258#else /* !MONT_WORD */
209 BIGNUM *t1,*t2; 259 BIGNUM *t1,*t2;
210 260
@@ -224,10 +274,12 @@ int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
224 if (!BN_rshift(ret,t2,mont->ri)) goto err; 274 if (!BN_rshift(ret,t2,mont->ri)) goto err;
225#endif /* MONT_WORD */ 275#endif /* MONT_WORD */
226 276
277#if !defined(BRANCH_FREE) || BRANCH_FREE==0
227 if (BN_ucmp(ret, &(mont->N)) >= 0) 278 if (BN_ucmp(ret, &(mont->N)) >= 0)
228 { 279 {
229 if (!BN_usub(ret,ret,&(mont->N))) goto err; 280 if (!BN_usub(ret,ret,&(mont->N))) goto err;
230 } 281 }
282#endif
231 retn=1; 283 retn=1;
232 err: 284 err:
233 BN_CTX_end(ctx); 285 BN_CTX_end(ctx);