source: trunk/cscrypt/bn_exp.c@ 3181

Last change on this file since 3181 was 3181, checked in by dingo35, 10 years ago

Adding threadsafety FIXMEs, feel free to join checking..

File size: 8.5 KB
Line 
1//FIXME Not checked on threadsafety yet; after checking please remove this line
2/* crypto/bn/bn_exp.c */
3/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * All rights reserved.
5 *
6 * This package is an SSL implementation written
7 * by Eric Young (eay@cryptsoft.com).
8 * The implementation was written so as to conform with Netscapes SSL.
9 *
10 * This library is free for commercial and non-commercial use as long as
11 * the following conditions are aheared to. The following conditions
12 * apply to all code found in this distribution, be it the RC4, RSA,
13 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
14 * included with this distribution is covered by the same copyright terms
15 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 *
17 * Copyright remains Eric Young's, and as such any Copyright notices in
18 * the code are not to be removed.
19 * If this package is used in a product, Eric Young should be given attribution
20 * as the author of the parts of the library used.
21 * This can be in the form of a textual message at program startup or
22 * in documentation (online or textual) provided with the package.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * "This product includes cryptographic software written by
35 * Eric Young (eay@cryptsoft.com)"
36 * The word 'cryptographic' can be left out if the rouines from the library
37 * being used are not cryptographic related :-).
38 * 4. If you include any Windows specific code (or a derivative thereof) from
39 * the apps directory (application code) you must include an acknowledgement:
40 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 *
42 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * The licence and distribution terms for any publically available version or
55 * derivative of this code cannot be changed. i.e. this code cannot simply be
56 * copied and put under another distribution licence
57 * [including the GNU Public Licence.]
58 */
59/* ====================================================================
60 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
61 *
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
64 * are met:
65 *
66 * 1. Redistributions of source code must retain the above copyright
67 * notice, this list of conditions and the following disclaimer.
68 *
69 * 2. Redistributions in binary form must reproduce the above copyright
70 * notice, this list of conditions and the following disclaimer in
71 * the documentation and/or other materials provided with the
72 * distribution.
73 *
74 * 3. All advertising materials mentioning features or use of this
75 * software must display the following acknowledgment:
76 * "This product includes software developed by the OpenSSL Project
77 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 *
79 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
80 * endorse or promote products derived from this software without
81 * prior written permission. For written permission, please contact
82 * openssl-core@openssl.org.
83 *
84 * 5. Products derived from this software may not be called "OpenSSL"
85 * nor may "OpenSSL" appear in their names without prior written
86 * permission of the OpenSSL Project.
87 *
88 * 6. Redistributions of any form whatsoever must retain the following
89 * acknowledgment:
90 * "This product includes software developed by the OpenSSL Project
91 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 *
93 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
94 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
96 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
97 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
98 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
99 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
100 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
102 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
103 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
104 * OF THE POSSIBILITY OF SUCH DAMAGE.
105 * ====================================================================
106 *
107 * This product includes cryptographic software written by Eric Young
108 * (eay@cryptsoft.com). This product includes software written by Tim
109 * Hudson (tjh@cryptsoft.com).
110 *
111 */
112
113
114#include <stdio.h>
115#include "bn_lcl.h"
116
117#define TABLE_SIZE 32
118
119/* slow but works */
120int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)
121 {
122 BIGNUM *t;
123 int r=0;
124
125 bn_check_top(a);
126 bn_check_top(b);
127 bn_check_top(m);
128
129 BN_CTX_start(ctx);
130 if ((t = BN_CTX_get(ctx)) == NULL) goto err;
131 if (a == b)
132 { if (!BN_sqr(t,a,ctx)) goto err; }
133 else
134 { if (!BN_mul(t,a,b,ctx)) goto err; }
135 if (!BN_mod(ret,t,m,ctx)) goto err;
136 r=1;
137err:
138 BN_CTX_end(ctx);
139 return(r);
140 }
141
142
143int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
144 BN_CTX *ctx)
145 {
146 int ret;
147
148 bn_check_top(a);
149 bn_check_top(p);
150 bn_check_top(m);
151
152 ret=BN_mod_exp_simple(r,a,p,m,ctx);
153
154 return(ret);
155 }
156
157
158
159
160
161/* The old fallback, simple version :-) */
162int BN_mod_exp_simple(BIGNUM *r, BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
163 BN_CTX *ctx)
164 {
165 int i,j,bits,ret=0,wstart,wend,window,wvalue,ts=0;
166 int start=1;
167 BIGNUM *d;
168 BIGNUM val[TABLE_SIZE];
169
170 bits=BN_num_bits(p);
171
172 if (bits == 0)
173 {
174 BN_one(r);
175 return(1);
176 }
177
178 BN_CTX_start(ctx);
179 if ((d = BN_CTX_get(ctx)) == NULL) goto err;
180
181 BN_init(&(val[0]));
182 ts=1;
183 if (!BN_mod(&(val[0]),a,m,ctx)) goto err; /* 1 */
184
185 window = BN_window_bits_for_exponent_size(bits);
186 if (window > 1)
187 {
188 if (!BN_mod_mul(d,&(val[0]),&(val[0]),m,ctx))
189 goto err; /* 2 */
190 j=1<<(window-1);
191 for (i=1; i<j; i++)
192 {
193 BN_init(&(val[i]));
194 if (!BN_mod_mul(&(val[i]),&(val[i-1]),d,m,ctx))
195 goto err;
196 }
197 ts=i;
198 }
199
200 start=1; /* This is used to avoid multiplication etc
201 * when there is only the value '1' in the
202 * buffer. */
203 wvalue=0; /* The 'value' of the window */
204 wstart=bits-1; /* The top bit of the window */
205 wend=0; /* The bottom bit of the window */
206
207 if (!BN_one(r)) goto err;
208
209 for (;;)
210 {
211 if (BN_is_bit_set(p,wstart) == 0)
212 {
213 if (!start)
214 if (!BN_mod_mul(r,r,r,m,ctx))
215 goto err;
216 if (wstart == 0) break;
217 wstart--;
218 continue;
219 }
220 /* We now have wstart on a 'set' bit, we now need to work out
221 * how bit a window to do. To do this we need to scan
222 * forward until the last set bit before the end of the
223 * window */
224 j=wstart;
225 wvalue=1;
226 wend=0;
227 for (i=1; i<window; i++)
228 {
229 if (wstart-i < 0) break;
230 if (BN_is_bit_set(p,wstart-i))
231 {
232 wvalue<<=(i-wend);
233 wvalue|=1;
234 wend=i;
235 }
236 }
237
238 /* wend is the size of the current window */
239 j=wend+1;
240 /* add the 'bytes above' */
241 if (!start)
242 for (i=0; i<j; i++)
243 {
244 if (!BN_mod_mul(r,r,r,m,ctx))
245 goto err;
246 }
247
248 /* wvalue will be an odd number < 2^window */
249 if (!BN_mod_mul(r,r,&(val[wvalue>>1]),m,ctx))
250 goto err;
251
252 /* move the 'window' down further */
253 wstart-=wend+1;
254 wvalue=0;
255 start=0;
256 if (wstart < 0) break;
257 }
258 ret=1;
259err:
260 BN_CTX_end(ctx);
261 for (i=0; i<ts; i++)
262 BN_clear_free(&(val[i]));
263 return(ret);
264 }
265
Note: See TracBrowser for help on using the repository browser.