1 /* crypto/bn/bn_exp.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
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
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
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.
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
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:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
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
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
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
72 int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)
82 if ((t = BN_CTX_get(ctx)) == NULL) goto err;
84 { if (!BN_sqr(t,a,ctx)) goto err; }
86 { if (!BN_mul(t,a,b,ctx)) goto err; }
87 if (!BN_mod(ret,t,m,ctx)) goto err;
95 /* this one works - simple but works */
96 int BN_mod_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx)
103 tmp = BN_CTX_get(ctx);
104 if (v == NULL || tmp == NULL) goto err;
106 if (BN_copy(v,a) == NULL) goto err;
110 { if (BN_copy(r,a) == NULL) goto err; }
111 else { if (!BN_one(r)) goto err; }
113 for (i=1; i<bits; i++)
115 if (!BN_sqr(tmp,v,ctx)) goto err;
116 if (!BN_mod(v,tmp,m,ctx)) goto err;
117 if (BN_is_bit_set(p,i))
119 if (!BN_mul(tmp,r,v,ctx)) goto err;
120 if (!BN_mod(r,tmp,m,ctx)) goto err;
131 /* this one works - simple but works */
132 int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx)
138 if ((r == a) || (r == p))
139 rr = BN_CTX_get(ctx);
142 if ((v = BN_CTX_get(ctx)) == NULL) goto err;
144 if (BN_copy(v,a) == NULL) goto err;
148 { if (BN_copy(rr,a) == NULL) goto err; }
149 else { if (!BN_one(rr)) goto err; }
151 for (i=1; i<bits; i++)
153 if (!BN_sqr(v,v,ctx)) goto err;
154 if (BN_is_bit_set(p,i))
156 if (!BN_mul(rr,rr,v,ctx)) goto err;
161 if (r != rr) BN_copy(r,rr);
169 * This routine will dynamically check for the existance of an Atalla AXL-200
170 * SSL accelerator module. If one is found, the variable
171 * asi_accelerator_present is set to 1 and the function pointers
172 * ptr_ASI_xxxxxx above will be initialized to corresponding ASI API calls.
174 typedef int tfnASI_GetPerformanceStatistics(int reset_flag,
175 unsigned int *ret_buf);
176 typedef int tfnASI_GetHardwareConfig(long card_num, unsigned int *ret_buf);
177 typedef int tfnASI_RSAPrivateKeyOpFn(RSAPrivateKey * rsaKey,
178 unsigned char *output,
179 unsigned char *input,
180 unsigned int modulus_len);
182 static tfnASI_GetHardwareConfig *ptr_ASI_GetHardwareConfig;
183 static tfnASI_RSAPrivateKeyOpFn *ptr_ASI_RSAPrivateKeyOpFn;
184 static tfnASI_GetPerformanceStatistics *ptr_ASI_GetPerformanceStatistics;
185 static int asi_accelerator_present;
186 static int tried_atalla;
188 void atalla_initialize_accelerator_handle(void)
192 unsigned int config_buf[1024];
200 bzero((void *)config_buf, 1024);
203 * Check to see if the library is present on the system
205 dl_handle = dlopen("atasi.so", RTLD_NOW);
206 if (dl_handle == (void *) NULL)
208 /* printf("atasi.so library is not present on the system\n");
209 printf("No HW acceleration available\n");*/
214 * The library is present. Now we'll check to insure that the
215 * LDM is up and running. First we'll get the address of the
216 * function in the atasi library that we need to see if the
220 ptr_ASI_GetHardwareConfig =
221 (tfnASI_GetHardwareConfig *)dlsym(dl_handle,"ASI_GetHardwareConfig");
223 if (ptr_ASI_GetHardwareConfig)
226 * We found the call, now we'll get our config
227 * status. If we get a non 0 result, the LDM is not
228 * running and we cannot use the Atalla ASI *
231 status = (*ptr_ASI_GetHardwareConfig)(0L, config_buf);
234 printf("atasi.so library is present but not initialized\n");
235 printf("No HW acceleration available\n");
241 /* printf("We found the library, but not the function. Very Strange!\n");*/
246 * It looks like we have acceleration capabilities. Load up the
247 * pointers to our ASI API calls.
249 ptr_ASI_RSAPrivateKeyOpFn=
250 (tfnASI_RSAPrivateKeyOpFn *)dlsym(dl_handle, "ASI_RSAPrivateKeyOpFn");
251 if (ptr_ASI_RSAPrivateKeyOpFn == NULL)
253 /* printf("We found the library, but no RSA function. Very Strange!\n");*/
257 ptr_ASI_GetPerformanceStatistics =
258 (tfnASI_GetPerformanceStatistics *)dlsym(dl_handle, "ASI_GetPerformanceStatistics");
259 if (ptr_ASI_GetPerformanceStatistics == NULL)
261 /* printf("We found the library, but no stat function. Very Strange!\n");*/
266 * Indicate that acceleration is available
268 asi_accelerator_present = 1;
270 /* printf("This system has acceleration!\n");*/
275 /* make sure this only gets called once when bn_mod_exp calls bn_mod_exp_mont */
276 int BN_mod_exp_atalla(BIGNUM *r, BIGNUM *a, const BIGNUM *p, const BIGNUM *m)
283 RSAPrivateKey keydata;
285 atalla_initialize_accelerator_handle();
286 if(!asi_accelerator_present)
290 /* We should be able to run without size testing */
296 if(an <= ASIZE && pn <= ASIZE && mn <= ASIZE)
302 memset(abin,'\0',mn);
303 BN_bn2bin(a,abin+size-an);
309 memset(mbin,'\0',mn);
310 BN_bn2bin(m,mbin+size-mn);
314 memset(&keydata,'\0',sizeof keydata);
315 keydata.privateExponent.data=pbin;
316 keydata.privateExponent.len=pn;
317 keydata.modulus.data=mbin;
318 keydata.modulus.len=size;
320 ret=(*ptr_ASI_RSAPrivateKeyOpFn)(&keydata,rbin,abin,keydata.modulus.len);
321 /*fprintf(stderr,"!%s\n",BN_bn2hex(a));*/
324 BN_bin2bn(rbin,keydata.modulus.len,r);
325 /*fprintf(stderr,"?%s\n",BN_bn2hex(r));*/
331 #endif /* def ATALLA */
333 int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
343 if(BN_mod_exp_atalla(r,a,p,m))
345 /* If it fails, try the other methods (but don't try atalla again) */
350 /* I have finally been able to take out this pre-condition of
351 * the top bit being set. It was caused by an error in BN_div
352 * with negatives. There was also another problem when for a^b%m
353 * a >= m. eay 07-May-97 */
354 /* if ((m->d[m->top-1]&BN_TBIT) && BN_is_odd(m)) */
357 { ret=BN_mod_exp_mont(r,a,p,m,ctx,NULL); }
361 { ret=BN_mod_exp_recp(r,a,p,m,ctx); }
363 { ret=BN_mod_exp_simple(r,a,p,m,ctx); }
373 /* #ifdef RECP_MUL_MOD */
374 int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
375 const BIGNUM *m, BN_CTX *ctx)
377 int i,j,bits,ret=0,wstart,wend,window,wvalue;
380 BIGNUM val[TABLE_SIZE];
392 if ((aa = BN_CTX_get(ctx)) == NULL) goto err;
394 BN_RECP_CTX_init(&recp);
395 if (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;
400 if (!BN_mod(&(val[0]),a,m,ctx)) goto err; /* 1 */
401 if (!BN_mod_mul_reciprocal(aa,&(val[0]),&(val[0]),&recp,ctx))
404 if (bits <= 17) /* This is probably 3 or 0x10001, so just do singles */
406 else if (bits >= 256)
407 window=5; /* max size of window */
408 else if (bits >= 128)
417 if (!BN_mod_mul_reciprocal(&(val[i]),&(val[i-1]),aa,&recp,ctx))
422 start=1; /* This is used to avoid multiplication etc
423 * when there is only the value '1' in the
425 wvalue=0; /* The 'value' of the window */
426 wstart=bits-1; /* The top bit of the window */
427 wend=0; /* The bottom bit of the window */
429 if (!BN_one(r)) goto err;
433 if (BN_is_bit_set(p,wstart) == 0)
436 if (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))
438 if (wstart == 0) break;
442 /* We now have wstart on a 'set' bit, we now need to work out
443 * how bit a window to do. To do this we need to scan
444 * forward until the last set bit before the end of the
449 for (i=1; i<window; i++)
451 if (wstart-i < 0) break;
452 if (BN_is_bit_set(p,wstart-i))
460 /* wend is the size of the current window */
462 /* add the 'bytes above' */
466 if (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))
470 /* wvalue will be an odd number < 2^window */
471 if (!BN_mod_mul_reciprocal(r,r,&(val[wvalue>>1]),&recp,ctx))
474 /* move the 'window' down further */
478 if (wstart < 0) break;
484 BN_clear_free(&(val[i]));
485 BN_RECP_CTX_free(&recp);
490 /* #ifdef MONT_MUL_MOD */
491 int BN_mod_exp_mont(BIGNUM *rr, BIGNUM *a, const BIGNUM *p,
492 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
494 int i,j,bits,ret=0,wstart,wend,window,wvalue;
498 BIGNUM val[TABLE_SIZE];
499 BN_MONT_CTX *mont=NULL;
506 if(!tried_atalla && BN_mod_exp_atalla(rr,a,p,m))
508 /* If it fails, try the other methods */
513 BNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);
525 if (d == NULL || r == NULL) goto err;
527 /* If this is not done, things will break in the montgomery
536 if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
537 if (!BN_MONT_CTX_set(mont,m,ctx)) goto err;
542 if (BN_ucmp(a,m) >= 0)
544 BN_mod(&(val[0]),a,m,ctx);
549 if (!BN_to_montgomery(&(val[0]),aa,mont,ctx)) goto err; /* 1 */
550 if (!BN_mod_mul_montgomery(d,&(val[0]),&(val[0]),mont,ctx)) goto err; /* 2 */
552 if (bits <= 20) /* This is probably 3 or 0x10001, so just do singles */
554 else if (bits >= 256)
555 window=5; /* max size of window */
556 else if (bits >= 128)
565 if (!BN_mod_mul_montgomery(&(val[i]),&(val[i-1]),d,mont,ctx))
570 start=1; /* This is used to avoid multiplication etc
571 * when there is only the value '1' in the
573 wvalue=0; /* The 'value' of the window */
574 wstart=bits-1; /* The top bit of the window */
575 wend=0; /* The bottom bit of the window */
577 if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;
580 if (BN_is_bit_set(p,wstart) == 0)
584 if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))
587 if (wstart == 0) break;
591 /* We now have wstart on a 'set' bit, we now need to work out
592 * how bit a window to do. To do this we need to scan
593 * forward until the last set bit before the end of the
598 for (i=1; i<window; i++)
600 if (wstart-i < 0) break;
601 if (BN_is_bit_set(p,wstart-i))
609 /* wend is the size of the current window */
611 /* add the 'bytes above' */
615 if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))
619 /* wvalue will be an odd number < 2^window */
620 if (!BN_mod_mul_montgomery(r,r,&(val[wvalue>>1]),mont,ctx))
623 /* move the 'window' down further */
627 if (wstart < 0) break;
629 BN_from_montgomery(rr,r,mont,ctx);
632 if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
635 BN_clear_free(&(val[i]));
640 /* The old fallback, simple version :-) */
641 int BN_mod_exp_simple(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m,
644 int i,j,bits,ret=0,wstart,wend,window,wvalue,ts=0;
647 BIGNUM val[TABLE_SIZE];
658 if ((d = BN_CTX_get(ctx)) == NULL) goto err;
662 if (!BN_mod(&(val[0]),a,m,ctx)) goto err; /* 1 */
663 if (!BN_mod_mul(d,&(val[0]),&(val[0]),m,ctx))
666 if (bits <= 17) /* This is probably 3 or 0x10001, so just do singles */
668 else if (bits >= 256)
669 window=5; /* max size of window */
670 else if (bits >= 128)
679 if (!BN_mod_mul(&(val[i]),&(val[i-1]),d,m,ctx))
684 start=1; /* This is used to avoid multiplication etc
685 * when there is only the value '1' in the
687 wvalue=0; /* The 'value' of the window */
688 wstart=bits-1; /* The top bit of the window */
689 wend=0; /* The bottom bit of the window */
691 if (!BN_one(r)) goto err;
695 if (BN_is_bit_set(p,wstart) == 0)
698 if (!BN_mod_mul(r,r,r,m,ctx))
700 if (wstart == 0) break;
704 /* We now have wstart on a 'set' bit, we now need to work out
705 * how bit a window to do. To do this we need to scan
706 * forward until the last set bit before the end of the
711 for (i=1; i<window; i++)
713 if (wstart-i < 0) break;
714 if (BN_is_bit_set(p,wstart-i))
722 /* wend is the size of the current window */
724 /* add the 'bytes above' */
728 if (!BN_mod_mul(r,r,r,m,ctx))
732 /* wvalue will be an odd number < 2^window */
733 if (!BN_mod_mul(r,r,&(val[wvalue>>1]),m,ctx))
736 /* move the 'window' down further */
740 if (wstart < 0) break;
746 BN_clear_free(&(val[i]));