2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
19 /* crypto/bn/bn_lib.c */
20 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
21 * All rights reserved.
23 * This package is an SSL implementation written
24 * by Eric Young (eay@cryptsoft.com).
25 * The implementation was written so as to conform with Netscapes SSL.
27 * This library is free for commercial and non-commercial use as long as
28 * the following conditions are aheared to. The following conditions
29 * apply to all code found in this distribution, be it the RC4, RSA,
30 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
31 * included with this distribution is covered by the same copyright terms
32 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
34 * Copyright remains Eric Young's, and as such any Copyright notices in
35 * the code are not to be removed.
36 * If this package is used in a product, Eric Young should be given attribution
37 * as the author of the parts of the library used.
38 * This can be in the form of a textual message at program startup or
39 * in documentation (online or textual) provided with the package.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * "This product includes cryptographic software written by
52 * Eric Young (eay@cryptsoft.com)"
53 * The word 'cryptographic' can be left out if the rouines from the library
54 * being used are not cryptographic related :-).
55 * 4. If you include any Windows specific code (or a derivative thereof) from
56 * the apps directory (application code) you must include an acknowledgement:
57 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
59 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * The licence and distribution terms for any publically available version or
72 * derivative of this code cannot be changed. i.e. this code cannot simply be
73 * copied and put under another distribution licence
74 * [including the GNU Public Licence.]
81 const char *BN_version
="Big Number" OPENSSL_VERSION_PTEXT
;
85 /* For a 32 bit machine
94 static int bn_limit_bits
=0;
95 static int bn_limit_num
=8; /* (1<<bn_limit_bits) */
96 static int bn_limit_bits_low
=0;
97 static int bn_limit_num_low
=8; /* (1<<bn_limit_bits_low) */
98 static int bn_limit_bits_high
=0;
99 static int bn_limit_num_high
=8; /* (1<<bn_limit_bits_high) */
100 static int bn_limit_bits_mont
=0;
101 static int bn_limit_num_mont
=8; /* (1<<bn_limit_bits_mont) */
103 void BN_set_params(int mult
, int high
, int low
, int mont
)
107 if (mult
> (sizeof(int)*8)-1)
108 mult
=sizeof(int)*8-1;
110 bn_limit_num
=1<<mult
;
114 if (high
> (sizeof(int)*8)-1)
115 high
=sizeof(int)*8-1;
116 bn_limit_bits_high
=high
;
117 bn_limit_num_high
=1<<high
;
121 if (low
> (sizeof(int)*8)-1)
123 bn_limit_bits_low
=low
;
124 bn_limit_num_low
=1<<low
;
128 if (mont
> (sizeof(int)*8)-1)
129 mont
=sizeof(int)*8-1;
130 bn_limit_bits_mont
=mont
;
131 bn_limit_num_mont
=1<<mont
;
135 int BN_get_params(int which
)
137 if (which
== 0) return(bn_limit_bits
);
138 else if (which
== 1) return(bn_limit_bits_high
);
139 else if (which
== 2) return(bn_limit_bits_low
);
140 else if (which
== 3) return(bn_limit_bits_mont
);
143 #endif /* BN_PARAMS_ENABLE */
145 BIGNUM
*BN_value_one(void)
147 static BN_ULONG data_one
=1L;
148 static BIGNUM const_one
={&data_one
,1,1,0};
153 char *BN_options(void)
156 static char data
[16];
162 sprintf(data
,"bn(%d,%d)",(int)sizeof(BN_ULLONG
)*8,
163 (int)sizeof(BN_ULONG
)*8);
165 sprintf(data
,"bn(%d,%d)",(int)sizeof(BN_ULONG
)*8,
166 (int)sizeof(BN_ULONG
)*8);
172 int BN_num_bits_word(BN_ULONG l
)
174 static const char bits
[256]={
175 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
176 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
177 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
178 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
179 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
180 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
181 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
182 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
183 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
184 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
185 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
186 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
187 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
188 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
189 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
190 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
193 #if defined(SIXTY_FOUR_BIT_LONG)
194 if (l
& 0xffffffff00000000L
)
196 if (l
& 0xffff000000000000L
)
198 if (l
& 0xff00000000000000L
)
200 return(bits
[(int)(l
>>56)]+56);
202 else return(bits
[(int)(l
>>48)]+48);
206 if (l
& 0x0000ff0000000000L
)
208 return(bits
[(int)(l
>>40)]+40);
210 else return(bits
[(int)(l
>>32)]+32);
215 #ifdef SIXTY_FOUR_BIT
216 if (l
& 0xffffffff00000000LL
)
218 if (l
& 0xffff000000000000LL
)
220 if (l
& 0xff00000000000000LL
)
222 return(bits
[(int)(l
>>56)]+56);
224 else return(bits
[(int)(l
>>48)]+48);
228 if (l
& 0x0000ff0000000000LL
)
230 return(bits
[(int)(l
>>40)]+40);
232 else return(bits
[(int)(l
>>32)]+32);
239 #if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
243 return(bits
[(int)(l
>>24L)]+24);
244 else return(bits
[(int)(l
>>16L)]+16);
249 #if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
251 return(bits
[(int)(l
>>8)]+8);
254 return(bits
[(int)(l
)] );
259 int BN_num_bits(const BIGNUM
*a
)
266 if (a
->top
== 0) return(0);
268 i
=(a
->top
-1)*BN_BITS2
;
271 #if !defined(NO_STDIO) && !defined(WIN16)
272 fprintf(stderr
,"BAD TOP VALUE\n");
276 return(i
+BN_num_bits_word(l
));
279 void BN_clear_free(BIGNUM
*a
)
283 if (a
== NULL
) return;
286 memset(a
->d
,0,a
->max
*sizeof(a
->d
[0]));
287 if (!(BN_get_flags(a
,BN_FLG_STATIC_DATA
)))
290 i
=BN_get_flags(a
,BN_FLG_MALLOCED
);
291 memset(a
,0,sizeof(BIGNUM
));
296 void BN_free(BIGNUM
*a
)
298 if (a
== NULL
) return;
299 if ((a
->d
!= NULL
) && !(BN_get_flags(a
,BN_FLG_STATIC_DATA
)))
301 a
->flags
|=BN_FLG_FREE
; /* REMOVE? */
302 if (a
->flags
& BN_FLG_MALLOCED
)
306 void BN_init(BIGNUM
*a
)
308 memset(a
,0,sizeof(BIGNUM
));
315 if ((ret
=(BIGNUM
*)Malloc(sizeof(BIGNUM
))) == NULL
)
317 BNerr(BN_F_BN_NEW
,ERR_R_MALLOC_FAILURE
);
320 ret
->flags
=BN_FLG_MALLOCED
;
328 /* This is an internal function that should not be used in applications.
329 * It ensures that 'b' has enough room for a 'words' word number number.
330 * It is mostly used by the various BIGNUM routines. If there is an error,
331 * NULL is returned. If not, 'b' is returned. */
333 BIGNUM
*bn_expand2(BIGNUM
*b
, int words
)
344 if (BN_get_flags(b
,BN_FLG_STATIC_DATA
))
346 BNerr(BN_F_BN_EXPAND2
,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA
);
349 a
=A
=(BN_ULONG
*)Malloc(sizeof(BN_ULONG
)*(words
+1));
352 BNerr(BN_F_BN_EXPAND2
,ERR_R_MALLOC_FAILURE
);
357 /* Check if the previous number needs to be copied */
361 /* This lot is an unrolled loop to copy b->top
362 * BN_ULONGs from B to A
365 * I have nothing against unrolling but it's usually done for
366 * several reasons, namely:
367 * - minimize percentage of decision making code, i.e. branches;
368 * - avoid cache trashing;
369 * - make it possible to schedule loads earlier;
370 * Now let's examine the code below. The cornerstone of C is
371 * "programmer is always right" and that's what we love it for:-)
372 * For this very reason C compilers have to be paranoid when it
373 * comes to data aliasing and assume the worst. Yeah, but what
374 * does it mean in real life? This means that loop body below will
375 * be compiled to sequence of loads immediately followed by stores
376 * as compiler assumes the worst, something in A==B+1 style. As a
377 * result CPU pipeline is going to starve for incoming data. Secondly
378 * if A and B happen to share same cache line such code is going to
379 * cause severe cache trashing. Both factors have severe impact on
380 * performance of modern CPUs and this is the reason why this
381 * particular piece of code is #ifdefed away and replaced by more
382 * "friendly" version found in #else section below. This comment
383 * also applies to BN_copy function.
385 * <appro@fy.chalmers.se>
387 for (i
=b
->top
&(~7); i
>0; i
-=8)
389 A
[0]=B
[0]; A
[1]=B
[1]; A
[2]=B
[2]; A
[3]=B
[3];
390 A
[4]=B
[4]; A
[5]=B
[5]; A
[6]=B
[6]; A
[7]=B
[7];
411 /* I need the 'case 0' entry for utrix cc.
412 * If the optimizer is turned on, it does the
413 * switch table by doing
416 * goto jump_table[a];
417 * If top is 0, this makes us jump to 0xffffffc
418 * which is rather bad :-(.
424 for (i
=b
->top
>>2; i
>0; i
--,A
+=4,B
+=4)
427 * The fact that the loop is unrolled
428 * 4-wise is a tribute to Intel. It's
429 * the one that doesn't have enough
430 * registers to accomodate more data.
431 * I'd unroll it 8-wise otherwise:-)
433 * <appro@fy.chalmers.se>
435 BN_ULONG a0
,a1
,a2
,a3
;
436 a0
=B
[0]; a1
=B
[1]; a2
=B
[2]; a3
=B
[3];
437 A
[0]=a0
; A
[1]=a1
; A
[2]=a2
; A
[3]=a3
;
444 case 0: ; /* ultrix cc workaround, see above */
453 /* Now need to zero any data between b->top and b->max */
456 for (i
=(b
->max
- b
->top
)>>3; i
>0; i
--,A
+=8)
458 A
[0]=0; A
[1]=0; A
[2]=0; A
[3]=0;
459 A
[4]=0; A
[5]=0; A
[6]=0; A
[7]=0;
461 for (i
=(b
->max
- b
->top
)&7; i
>0; i
--,A
++)
464 memset(A
,0,sizeof(BN_ULONG
)*(words
+1));
465 memcpy(A
,b
->d
,sizeof(b
->d
[0])*b
->top
);
470 /* memset(&(p[b->max]),0,((words+1)-b->max)*sizeof(BN_ULONG)); */
471 /* { int i; for (i=b->max; i<words+1; i++) p[i]=i;} */
477 BIGNUM
*BN_dup(const BIGNUM
*a
)
481 if (a
== NULL
) return NULL
;
486 if (r
== NULL
) return(NULL
);
487 return((BIGNUM
*)BN_copy(r
,a
));
490 BIGNUM
*BN_copy(BIGNUM
*a
, const BIGNUM
*b
)
498 if (a
== b
) return(a
);
499 if (bn_wexpand(a
,b
->top
) == NULL
) return(NULL
);
504 for (i
=b
->top
>>2; i
>0; i
--,A
+=4,B
+=4)
506 BN_ULONG a0
,a1
,a2
,a3
;
507 a0
=B
[0]; a1
=B
[1]; a2
=B
[2]; a3
=B
[3];
508 A
[0]=a0
; A
[1]=a1
; A
[2]=a2
; A
[3]=a3
;
515 case 0: ; /* ultrix cc workaround, see comments in bn_expand2 */
518 memcpy(a
->d
,b
->d
,sizeof(b
->d
[0])*b
->top
);
521 /* memset(&(a->d[b->top]),0,sizeof(a->d[0])*(a->max-b->top));*/
523 if ((a
->top
== 0) && (a
->d
!= NULL
))
529 void BN_clear(BIGNUM
*a
)
532 memset(a
->d
,0,a
->max
*sizeof(a
->d
[0]));
537 BN_ULONG
BN_get_word(BIGNUM
*a
)
544 if (n
> sizeof(BN_ULONG
))
546 for (i
=a
->top
-1; i
>=0; i
--)
548 #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */
549 ret
<<=BN_BITS4
; /* stops the compiler complaining */
559 int BN_set_word(BIGNUM
*a
, BN_ULONG w
)
563 if (bn_expand(a
,(int)(sizeof(BN_ULONG
)*8)) == NULL
) return(0);
565 n
=sizeof(BN_ULONG
)/BN_BYTES
;
568 a
->d
[0]=(BN_ULONG
)w
&BN_MASK2
;
569 if (a
->d
[0] != 0) a
->top
=1;
572 /* the following is done instead of
573 * w>>=BN_BITS2 so compilers don't complain
574 * on builds where sizeof(long) == BN_TYPES */
575 #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */
581 a
->d
[i
]=(BN_ULONG
)w
&BN_MASK2
;
582 if (a
->d
[i
] != 0) a
->top
=i
+1;
587 /* ignore negative */
588 BIGNUM
*BN_bin2bn(const unsigned char *s
, int len
, BIGNUM
*ret
)
594 if (ret
== NULL
) ret
=BN_new();
595 if (ret
== NULL
) return(NULL
);
603 if (bn_expand(ret
,(int)(n
+2)*8) == NULL
)
605 i
=((n
-1)/BN_BYTES
)+1;
606 m
=((n
-1)%(BN_BYTES
));
618 /* need to call this due to clear byte at top if avoiding
619 * having the top bit set (-ve number) */
624 /* ignore negative */
625 int BN_bn2bin(const BIGNUM
*a
, unsigned char *to
)
634 *(to
++)=(unsigned char)(l
>>(8*(i%BN_BYTES
)))&0xff;
639 int BN_ucmp(const BIGNUM
*a
, const BIGNUM
*b
)
642 BN_ULONG t1
,t2
,*ap
,*bp
;
648 if (i
!= 0) return(i
);
651 for (i
=a
->top
-1; i
>=0; i
--)
656 return(t1
> t2
?1:-1);
661 int BN_cmp(const BIGNUM
*a
, const BIGNUM
*b
)
667 if ((a
== NULL
) || (b
== NULL
))
680 if (a
->neg
!= b
->neg
)
688 else { gt
= -1; lt
=1; }
690 if (a
->top
> b
->top
) return(gt
);
691 if (a
->top
< b
->top
) return(lt
);
692 for (i
=a
->top
-1; i
>=0; i
--)
696 if (t1
> t2
) return(gt
);
697 if (t1
< t2
) return(lt
);
702 int BN_set_bit(BIGNUM
*a
, int n
)
710 if (bn_wexpand(a
,i
+1) == NULL
) return(0);
711 for(k
=a
->top
; k
<i
+1; k
++)
716 a
->d
[i
]|=(((BN_ULONG
)1)<<j
);
720 int BN_clear_bit(BIGNUM
*a
, int n
)
726 if (a
->top
<= i
) return(0);
728 a
->d
[i
]&=(~(((BN_ULONG
)1)<<j
));
733 int BN_is_bit_set(const BIGNUM
*a
, int n
)
737 if (n
< 0) return(0);
740 if (a
->top
<= i
) return(0);
741 return((a
->d
[i
]&(((BN_ULONG
)1)<<j
))?1:0);
744 int BN_mask_bits(BIGNUM
*a
, int n
)
750 if (w
>= a
->top
) return(0);
756 a
->d
[w
]&= ~(BN_MASK2
<<b
);
762 int bn_cmp_words(BN_ULONG
*a
, BN_ULONG
*b
, int n
)
769 if (aa
!= bb
) return((aa
> bb
)?1:-1);
770 for (i
=n
-2; i
>=0; i
--)
774 if (aa
!= bb
) return((aa
> bb
)?1:-1);