diff options
Diffstat (limited to 'src/lib/libcrypto/bn/bn_sqr.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_sqr.c | 246 |
1 files changed, 118 insertions, 128 deletions
diff --git a/src/lib/libcrypto/bn/bn_sqr.c b/src/lib/libcrypto/bn/bn_sqr.c index 270d0cd348..68ec0a776d 100644 --- a/src/lib/libcrypto/bn/bn_sqr.c +++ b/src/lib/libcrypto/bn/bn_sqr.c | |||
@@ -5,21 +5,21 @@ | |||
5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
8 | * | 8 | * |
9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 | * included with this distribution is covered by the same copyright terms | 13 | * included with this distribution is covered by the same copyright terms |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | * | 15 | * |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
18 | * If this package is used in a product, Eric Young should be given attribution | 18 | * If this package is used in a product, Eric Young should be given attribution |
19 | * as the author of the parts of the library used. | 19 | * as the author of the parts of the library used. |
20 | * This can be in the form of a textual message at program startup or | 20 | * This can be in the form of a textual message at program startup or |
21 | * in documentation (online or textual) provided with the package. | 21 | * in documentation (online or textual) provided with the package. |
22 | * | 22 | * |
23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
25 | * are met: | 25 | * are met: |
@@ -34,10 +34,10 @@ | |||
34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
38 | * the apps directory (application code) you must include an acknowledgement: | 38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | * | 40 | * |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -49,7 +49,7 @@ | |||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
52 | * | 52 | * |
53 | * The licence and distribution terms for any publically available version or | 53 | * The licence and distribution terms for any publically available version or |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
@@ -62,135 +62,130 @@ | |||
62 | 62 | ||
63 | /* r must not be a */ | 63 | /* r must not be a */ |
64 | /* I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96 */ | 64 | /* I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96 */ |
65 | int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) | 65 | int |
66 | { | 66 | BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) |
67 | int max,al; | 67 | { |
68 | int max, al; | ||
68 | int ret = 0; | 69 | int ret = 0; |
69 | BIGNUM *tmp,*rr; | 70 | BIGNUM *tmp, *rr; |
70 | 71 | ||
71 | #ifdef BN_COUNT | 72 | #ifdef BN_COUNT |
72 | fprintf(stderr,"BN_sqr %d * %d\n",a->top,a->top); | 73 | fprintf(stderr, "BN_sqr %d * %d\n", a->top, a->top); |
73 | #endif | 74 | #endif |
74 | bn_check_top(a); | 75 | bn_check_top(a); |
75 | 76 | ||
76 | al=a->top; | 77 | al = a->top; |
77 | if (al <= 0) | 78 | if (al <= 0) { |
78 | { | 79 | r->top = 0; |
79 | r->top=0; | ||
80 | return 1; | 80 | return 1; |
81 | } | 81 | } |
82 | 82 | ||
83 | BN_CTX_start(ctx); | 83 | BN_CTX_start(ctx); |
84 | rr=(a != r) ? r : BN_CTX_get(ctx); | 84 | rr = (a != r) ? r : BN_CTX_get(ctx); |
85 | tmp=BN_CTX_get(ctx); | 85 | tmp = BN_CTX_get(ctx); |
86 | if (!rr || !tmp) goto err; | 86 | if (!rr || !tmp) |
87 | goto err; | ||
87 | 88 | ||
88 | max = 2 * al; /* Non-zero (from above) */ | 89 | max = 2 * al; /* Non-zero (from above) */ |
89 | if (bn_wexpand(rr,max) == NULL) goto err; | 90 | if (bn_wexpand(rr, max) == NULL) |
91 | goto err; | ||
90 | 92 | ||
91 | if (al == 4) | 93 | if (al == 4) { |
92 | { | ||
93 | #ifndef BN_SQR_COMBA | 94 | #ifndef BN_SQR_COMBA |
94 | BN_ULONG t[8]; | 95 | BN_ULONG t[8]; |
95 | bn_sqr_normal(rr->d,a->d,4,t); | 96 | bn_sqr_normal(rr->d, a->d, 4, t); |
96 | #else | 97 | #else |
97 | bn_sqr_comba4(rr->d,a->d); | 98 | bn_sqr_comba4(rr->d, a->d); |
98 | #endif | 99 | #endif |
99 | } | 100 | } else if (al == 8) { |
100 | else if (al == 8) | ||
101 | { | ||
102 | #ifndef BN_SQR_COMBA | 101 | #ifndef BN_SQR_COMBA |
103 | BN_ULONG t[16]; | 102 | BN_ULONG t[16]; |
104 | bn_sqr_normal(rr->d,a->d,8,t); | 103 | bn_sqr_normal(rr->d, a->d, 8, t); |
105 | #else | 104 | #else |
106 | bn_sqr_comba8(rr->d,a->d); | 105 | bn_sqr_comba8(rr->d, a->d); |
107 | #endif | 106 | #endif |
108 | } | 107 | } else { |
109 | else | ||
110 | { | ||
111 | #if defined(BN_RECURSION) | 108 | #if defined(BN_RECURSION) |
112 | if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) | 109 | if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) { |
113 | { | ||
114 | BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2]; | 110 | BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2]; |
115 | bn_sqr_normal(rr->d,a->d,al,t); | 111 | bn_sqr_normal(rr->d, a->d, al, t); |
116 | } | 112 | } else { |
117 | else | 113 | int j, k; |
118 | { | ||
119 | int j,k; | ||
120 | 114 | ||
121 | j=BN_num_bits_word((BN_ULONG)al); | 115 | j = BN_num_bits_word((BN_ULONG)al); |
122 | j=1<<(j-1); | 116 | j = 1 << (j - 1); |
123 | k=j+j; | 117 | k = j + j; |
124 | if (al == j) | 118 | if (al == j) { |
125 | { | 119 | if (bn_wexpand(tmp, k * 2) == NULL) |
126 | if (bn_wexpand(tmp,k*2) == NULL) goto err; | 120 | goto err; |
127 | bn_sqr_recursive(rr->d,a->d,al,tmp->d); | 121 | bn_sqr_recursive(rr->d, a->d, al, tmp->d); |
128 | } | 122 | } else { |
129 | else | 123 | if (bn_wexpand(tmp, max) == NULL) |
130 | { | 124 | goto err; |
131 | if (bn_wexpand(tmp,max) == NULL) goto err; | 125 | bn_sqr_normal(rr->d, a->d, al, tmp->d); |
132 | bn_sqr_normal(rr->d,a->d,al,tmp->d); | ||
133 | } | ||
134 | } | 126 | } |
127 | } | ||
135 | #else | 128 | #else |
136 | if (bn_wexpand(tmp,max) == NULL) goto err; | 129 | if (bn_wexpand(tmp, max) == NULL) |
137 | bn_sqr_normal(rr->d,a->d,al,tmp->d); | 130 | goto err; |
131 | bn_sqr_normal(rr->d, a->d, al, tmp->d); | ||
138 | #endif | 132 | #endif |
139 | } | 133 | } |
140 | 134 | ||
141 | rr->neg=0; | 135 | rr->neg = 0; |
142 | /* If the most-significant half of the top word of 'a' is zero, then | 136 | /* If the most-significant half of the top word of 'a' is zero, then |
143 | * the square of 'a' will max-1 words. */ | 137 | * the square of 'a' will max-1 words. */ |
144 | if(a->d[al - 1] == (a->d[al - 1] & BN_MASK2l)) | 138 | if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l)) |
145 | rr->top = max - 1; | 139 | rr->top = max - 1; |
146 | else | 140 | else |
147 | rr->top = max; | 141 | rr->top = max; |
148 | if (rr != r) BN_copy(r,rr); | 142 | if (rr != r) |
143 | BN_copy(r, rr); | ||
149 | ret = 1; | 144 | ret = 1; |
150 | err: | 145 | |
146 | err: | ||
151 | bn_check_top(rr); | 147 | bn_check_top(rr); |
152 | bn_check_top(tmp); | 148 | bn_check_top(tmp); |
153 | BN_CTX_end(ctx); | 149 | BN_CTX_end(ctx); |
154 | return(ret); | 150 | return (ret); |
155 | } | 151 | } |
156 | 152 | ||
157 | /* tmp must have 2*n words */ | 153 | /* tmp must have 2*n words */ |
158 | void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) | 154 | void |
159 | { | 155 | bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) |
160 | int i,j,max; | 156 | { |
157 | int i, j, max; | ||
161 | const BN_ULONG *ap; | 158 | const BN_ULONG *ap; |
162 | BN_ULONG *rp; | 159 | BN_ULONG *rp; |
163 | 160 | ||
164 | max=n*2; | 161 | max = n * 2; |
165 | ap=a; | 162 | ap = a; |
166 | rp=r; | 163 | rp = r; |
167 | rp[0]=rp[max-1]=0; | 164 | rp[0] = rp[max - 1] = 0; |
168 | rp++; | 165 | rp++; |
169 | j=n; | 166 | j = n; |
170 | 167 | ||
171 | if (--j > 0) | 168 | if (--j > 0) { |
172 | { | ||
173 | ap++; | 169 | ap++; |
174 | rp[j]=bn_mul_words(rp,ap,j,ap[-1]); | 170 | rp[j] = bn_mul_words(rp, ap, j, ap[-1]); |
175 | rp+=2; | 171 | rp += 2; |
176 | } | 172 | } |
177 | 173 | ||
178 | for (i=n-2; i>0; i--) | 174 | for (i = n - 2; i > 0; i--) { |
179 | { | ||
180 | j--; | 175 | j--; |
181 | ap++; | 176 | ap++; |
182 | rp[j]=bn_mul_add_words(rp,ap,j,ap[-1]); | 177 | rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]); |
183 | rp+=2; | 178 | rp += 2; |
184 | } | 179 | } |
185 | 180 | ||
186 | bn_add_words(r,r,r,max); | 181 | bn_add_words(r, r, r, max); |
187 | 182 | ||
188 | /* There will not be a carry */ | 183 | /* There will not be a carry */ |
189 | 184 | ||
190 | bn_sqr_words(tmp,a,n); | 185 | bn_sqr_words(tmp, a, n); |
191 | 186 | ||
192 | bn_add_words(r,r,tmp,max); | 187 | bn_add_words(r, r, tmp, max); |
193 | } | 188 | } |
194 | 189 | ||
195 | #ifdef BN_RECURSION | 190 | #ifdef BN_RECURSION |
196 | /* r is 2*n words in size, | 191 | /* r is 2*n words in size, |
@@ -203,92 +198,87 @@ void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) | |||
203 | * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0]) | 198 | * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0]) |
204 | * a[1]*b[1] | 199 | * a[1]*b[1] |
205 | */ | 200 | */ |
206 | void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t) | 201 | void |
207 | { | 202 | bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t) |
208 | int n=n2/2; | 203 | { |
209 | int zero,c1; | 204 | int n = n2 / 2; |
210 | BN_ULONG ln,lo,*p; | 205 | int zero, c1; |
206 | BN_ULONG ln, lo, *p; | ||
211 | 207 | ||
212 | #ifdef BN_COUNT | 208 | #ifdef BN_COUNT |
213 | fprintf(stderr," bn_sqr_recursive %d * %d\n",n2,n2); | 209 | fprintf(stderr, " bn_sqr_recursive %d * %d\n", n2, n2); |
214 | #endif | 210 | #endif |
215 | if (n2 == 4) | 211 | if (n2 == 4) { |
216 | { | ||
217 | #ifndef BN_SQR_COMBA | 212 | #ifndef BN_SQR_COMBA |
218 | bn_sqr_normal(r,a,4,t); | 213 | bn_sqr_normal(r, a, 4, t); |
219 | #else | 214 | #else |
220 | bn_sqr_comba4(r,a); | 215 | bn_sqr_comba4(r, a); |
221 | #endif | 216 | #endif |
222 | return; | 217 | return; |
223 | } | 218 | } else if (n2 == 8) { |
224 | else if (n2 == 8) | ||
225 | { | ||
226 | #ifndef BN_SQR_COMBA | 219 | #ifndef BN_SQR_COMBA |
227 | bn_sqr_normal(r,a,8,t); | 220 | bn_sqr_normal(r, a, 8, t); |
228 | #else | 221 | #else |
229 | bn_sqr_comba8(r,a); | 222 | bn_sqr_comba8(r, a); |
230 | #endif | 223 | #endif |
231 | return; | 224 | return; |
232 | } | 225 | } |
233 | if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) | 226 | if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) { |
234 | { | 227 | bn_sqr_normal(r, a, n2, t); |
235 | bn_sqr_normal(r,a,n2,t); | ||
236 | return; | 228 | return; |
237 | } | 229 | } |
238 | /* r=(a[0]-a[1])*(a[1]-a[0]) */ | 230 | /* r=(a[0]-a[1])*(a[1]-a[0]) */ |
239 | c1=bn_cmp_words(a,&(a[n]),n); | 231 | c1 = bn_cmp_words(a, &(a[n]), n); |
240 | zero=0; | 232 | zero = 0; |
241 | if (c1 > 0) | 233 | if (c1 > 0) |
242 | bn_sub_words(t,a,&(a[n]),n); | 234 | bn_sub_words(t, a, &(a[n]), n); |
243 | else if (c1 < 0) | 235 | else if (c1 < 0) |
244 | bn_sub_words(t,&(a[n]),a,n); | 236 | bn_sub_words(t, &(a[n]), a, n); |
245 | else | 237 | else |
246 | zero=1; | 238 | zero = 1; |
247 | 239 | ||
248 | /* The result will always be negative unless it is zero */ | 240 | /* The result will always be negative unless it is zero */ |
249 | p= &(t[n2*2]); | 241 | p = &(t[n2*2]); |
250 | 242 | ||
251 | if (!zero) | 243 | if (!zero) |
252 | bn_sqr_recursive(&(t[n2]),t,n,p); | 244 | bn_sqr_recursive(&(t[n2]), t, n, p); |
253 | else | 245 | else |
254 | memset(&(t[n2]),0,n2*sizeof(BN_ULONG)); | 246 | memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG)); |
255 | bn_sqr_recursive(r,a,n,p); | 247 | bn_sqr_recursive(r, a, n, p); |
256 | bn_sqr_recursive(&(r[n2]),&(a[n]),n,p); | 248 | bn_sqr_recursive(&(r[n2]), &(a[n]), n, p); |
257 | 249 | ||
258 | /* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero | 250 | /* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero |
259 | * r[10] holds (a[0]*b[0]) | 251 | * r[10] holds (a[0]*b[0]) |
260 | * r[32] holds (b[1]*b[1]) | 252 | * r[32] holds (b[1]*b[1]) |
261 | */ | 253 | */ |
262 | 254 | ||
263 | c1=(int)(bn_add_words(t,r,&(r[n2]),n2)); | 255 | c1 = (int)(bn_add_words(t, r, &(r[n2]), n2)); |
264 | 256 | ||
265 | /* t[32] is negative */ | 257 | /* t[32] is negative */ |
266 | c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2)); | 258 | c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2)); |
267 | 259 | ||
268 | /* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1]) | 260 | /* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1]) |
269 | * r[10] holds (a[0]*a[0]) | 261 | * r[10] holds (a[0]*a[0]) |
270 | * r[32] holds (a[1]*a[1]) | 262 | * r[32] holds (a[1]*a[1]) |
271 | * c1 holds the carry bits | 263 | * c1 holds the carry bits |
272 | */ | 264 | */ |
273 | c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2)); | 265 | c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2)); |
274 | if (c1) | 266 | if (c1) { |
275 | { | 267 | p = &(r[n + n2]); |
276 | p= &(r[n+n2]); | ||
277 | lo= *p; | 268 | lo= *p; |
278 | ln=(lo+c1)&BN_MASK2; | 269 | ln = (lo + c1) & BN_MASK2; |
279 | *p=ln; | 270 | *p = ln; |
280 | 271 | ||
281 | /* The overflow will stop before we over write | 272 | /* The overflow will stop before we over write |
282 | * words we should not overwrite */ | 273 | * words we should not overwrite */ |
283 | if (ln < (BN_ULONG)c1) | 274 | if (ln < (BN_ULONG)c1) { |
284 | { | 275 | do { |
285 | do { | ||
286 | p++; | 276 | p++; |
287 | lo= *p; | 277 | lo= *p; |
288 | ln=(lo+1)&BN_MASK2; | 278 | ln = (lo + 1) & BN_MASK2; |
289 | *p=ln; | 279 | *p = ln; |
290 | } while (ln == 0); | 280 | } while (ln == 0); |
291 | } | ||
292 | } | 281 | } |
293 | } | 282 | } |
283 | } | ||
294 | #endif | 284 | #endif |