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