OLD | NEW |
1 /* crypto/bn/bn.h */ | 1 /* crypto/bn/bn.h */ |
2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
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 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
56 * [including the GNU Public Licence.] | 56 * [including the GNU Public Licence.] |
57 */ | 57 */ |
58 /* ==================================================================== | 58 /* ==================================================================== |
| 59 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
| 60 * |
| 61 * Redistribution and use in source and binary forms, with or without |
| 62 * modification, are permitted provided that the following conditions |
| 63 * are met: |
| 64 * |
| 65 * 1. Redistributions of source code must retain the above copyright |
| 66 * notice, this list of conditions and the following disclaimer. |
| 67 * |
| 68 * 2. Redistributions in binary form must reproduce the above copyright |
| 69 * notice, this list of conditions and the following disclaimer in |
| 70 * the documentation and/or other materials provided with the |
| 71 * distribution. |
| 72 * |
| 73 * 3. All advertising materials mentioning features or use of this |
| 74 * software must display the following acknowledgment: |
| 75 * "This product includes software developed by the OpenSSL Project |
| 76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 77 * |
| 78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 79 * endorse or promote products derived from this software without |
| 80 * prior written permission. For written permission, please contact |
| 81 * openssl-core@openssl.org. |
| 82 * |
| 83 * 5. Products derived from this software may not be called "OpenSSL" |
| 84 * nor may "OpenSSL" appear in their names without prior written |
| 85 * permission of the OpenSSL Project. |
| 86 * |
| 87 * 6. Redistributions of any form whatsoever must retain the following |
| 88 * acknowledgment: |
| 89 * "This product includes software developed by the OpenSSL Project |
| 90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 91 * |
| 92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 103 * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 104 * ==================================================================== |
| 105 * |
| 106 * This product includes cryptographic software written by Eric Young |
| 107 * (eay@cryptsoft.com). This product includes software written by Tim |
| 108 * Hudson (tjh@cryptsoft.com). |
| 109 * |
| 110 */ |
| 111 /* ==================================================================== |
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | 112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
60 * | 113 * |
61 * Portions of the attached software ("Contribution") are developed by | 114 * Portions of the attached software ("Contribution") are developed by |
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. | 115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
63 * | 116 * |
64 * The Contribution is licensed pursuant to the Eric Young open source | 117 * The Contribution is licensed pursuant to the Eric Young open source |
65 * license provided above. | 118 * license provided above. |
66 * | 119 * |
67 * The binary polynomial arithmetic software is originally written by | 120 * The binary polynomial arithmetic software is originally written by |
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. | 121 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. |
69 * | 122 * |
70 */ | 123 */ |
71 | 124 |
72 #ifndef HEADER_BN_H | 125 #ifndef HEADER_BN_H |
73 #define HEADER_BN_H | 126 #define HEADER_BN_H |
74 | 127 |
75 #include <openssl/e_os2.h> | 128 #include <openssl/e_os2.h> |
76 #ifndef OPENSSL_NO_FP_API | 129 #ifndef OPENSSL_NO_FP_API |
77 #include <stdio.h> /* FILE */ | 130 #include <stdio.h> /* FILE */ |
78 #endif | 131 #endif |
79 #include <openssl/ossl_typ.h> | 132 #include <openssl/ossl_typ.h> |
| 133 #include <openssl/crypto.h> |
80 | 134 |
81 #ifdef __cplusplus | 135 #ifdef __cplusplus |
82 extern "C" { | 136 extern "C" { |
83 #endif | 137 #endif |
84 | 138 |
85 /* These preprocessor symbols control various aspects of the bignum headers and | 139 /* These preprocessor symbols control various aspects of the bignum headers and |
86 * library code. They're not defined by any "normal" configuration, as they are | 140 * library code. They're not defined by any "normal" configuration, as they are |
87 * intended for development and testing purposes. NB: defining all three can be | 141 * intended for development and testing purposes. NB: defining all three can be |
88 * useful for debugging application code as well as openssl itself. | 142 * useful for debugging application code as well as openssl itself. |
89 * | 143 * |
90 * BN_DEBUG - turn on various debugging alterations to the bignum code | 144 * BN_DEBUG - turn on various debugging alterations to the bignum code |
91 * BN_DEBUG_RAND - uses random poisoning of unused words to trip up | 145 * BN_DEBUG_RAND - uses random poisoning of unused words to trip up |
92 * mismanagement of bignum internals. You must also define BN_DEBUG. | 146 * mismanagement of bignum internals. You must also define BN_DEBUG. |
93 */ | 147 */ |
94 /* #define BN_DEBUG */ | 148 /* #define BN_DEBUG */ |
95 /* #define BN_DEBUG_RAND */ | 149 /* #define BN_DEBUG_RAND */ |
96 | 150 |
| 151 #ifndef OPENSSL_SMALL_FOOTPRINT |
97 #define BN_MUL_COMBA | 152 #define BN_MUL_COMBA |
98 #define BN_SQR_COMBA | 153 #define BN_SQR_COMBA |
99 #define BN_RECURSION | 154 #define BN_RECURSION |
| 155 #endif |
100 | 156 |
101 /* This next option uses the C libraries (2 word)/(1 word) function. | 157 /* This next option uses the C libraries (2 word)/(1 word) function. |
102 * If it is not defined, I use my C version (which is slower). | 158 * If it is not defined, I use my C version (which is slower). |
103 * The reason for this flag is that when the particular C compiler | 159 * The reason for this flag is that when the particular C compiler |
104 * library routine is used, and the library is linked with a different | 160 * library routine is used, and the library is linked with a different |
105 * compiler, the library is missing. This mostly happens when the | 161 * compiler, the library is missing. This mostly happens when the |
106 * library is built with gcc and then linked using normal cc. This would | 162 * library is built with gcc and then linked using normal cc. This would |
107 * be a common occurrence because gcc normally produces code that is | 163 * be a common occurrence because gcc normally produces code that is |
108 * 2 times faster than system compilers for the big number stuff. | 164 * 2 times faster than system compilers for the big number stuff. |
109 * For machines with only one compiler (or shared libraries), this should | 165 * For machines with only one compiler (or shared libraries), this should |
(...skipping 20 matching lines...) Expand all Loading... |
130 #define BN_MASK (0xffffffffffffffffffffffffffffffffLL) | 186 #define BN_MASK (0xffffffffffffffffffffffffffffffffLL) |
131 #define BN_MASK2 (0xffffffffffffffffL) | 187 #define BN_MASK2 (0xffffffffffffffffL) |
132 #define BN_MASK2l (0xffffffffL) | 188 #define BN_MASK2l (0xffffffffL) |
133 #define BN_MASK2h (0xffffffff00000000L) | 189 #define BN_MASK2h (0xffffffff00000000L) |
134 #define BN_MASK2h1 (0xffffffff80000000L) | 190 #define BN_MASK2h1 (0xffffffff80000000L) |
135 #define BN_TBIT (0x8000000000000000L) | 191 #define BN_TBIT (0x8000000000000000L) |
136 #define BN_DEC_CONV (10000000000000000000UL) | 192 #define BN_DEC_CONV (10000000000000000000UL) |
137 #define BN_DEC_FMT1 "%lu" | 193 #define BN_DEC_FMT1 "%lu" |
138 #define BN_DEC_FMT2 "%019lu" | 194 #define BN_DEC_FMT2 "%019lu" |
139 #define BN_DEC_NUM 19 | 195 #define BN_DEC_NUM 19 |
| 196 #define BN_HEX_FMT1 "%lX" |
| 197 #define BN_HEX_FMT2 "%016lX" |
140 #endif | 198 #endif |
141 | 199 |
142 /* This is where the long long data type is 64 bits, but long is 32. | 200 /* This is where the long long data type is 64 bits, but long is 32. |
143 * For machines where there are 64bit registers, this is the mode to use. | 201 * For machines where there are 64bit registers, this is the mode to use. |
144 * IRIX, on R4000 and above should use this mode, along with the relevant | 202 * IRIX, on R4000 and above should use this mode, along with the relevant |
145 * assembler code :-). Do NOT define BN_LLONG. | 203 * assembler code :-). Do NOT define BN_LLONG. |
146 */ | 204 */ |
147 #ifdef SIXTY_FOUR_BIT | 205 #ifdef SIXTY_FOUR_BIT |
148 #undef BN_LLONG | 206 #undef BN_LLONG |
149 #undef BN_ULLONG | 207 #undef BN_ULLONG |
150 #define BN_ULONG unsigned long long | 208 #define BN_ULONG unsigned long long |
151 #define BN_LONG long long | 209 #define BN_LONG long long |
152 #define BN_BITS 128 | 210 #define BN_BITS 128 |
153 #define BN_BYTES 8 | 211 #define BN_BYTES 8 |
154 #define BN_BITS2 64 | 212 #define BN_BITS2 64 |
155 #define BN_BITS4 32 | 213 #define BN_BITS4 32 |
156 #define BN_MASK2 (0xffffffffffffffffLL) | 214 #define BN_MASK2 (0xffffffffffffffffLL) |
157 #define BN_MASK2l (0xffffffffL) | 215 #define BN_MASK2l (0xffffffffL) |
158 #define BN_MASK2h (0xffffffff00000000LL) | 216 #define BN_MASK2h (0xffffffff00000000LL) |
159 #define BN_MASK2h1 (0xffffffff80000000LL) | 217 #define BN_MASK2h1 (0xffffffff80000000LL) |
160 #define BN_TBIT (0x8000000000000000LL) | 218 #define BN_TBIT (0x8000000000000000LL) |
161 #define BN_DEC_CONV (10000000000000000000ULL) | 219 #define BN_DEC_CONV (10000000000000000000ULL) |
162 #define BN_DEC_FMT1 "%llu" | 220 #define BN_DEC_FMT1 "%llu" |
163 #define BN_DEC_FMT2 "%019llu" | 221 #define BN_DEC_FMT2 "%019llu" |
164 #define BN_DEC_NUM 19 | 222 #define BN_DEC_NUM 19 |
| 223 #define BN_HEX_FMT1 "%llX" |
| 224 #define BN_HEX_FMT2 "%016llX" |
165 #endif | 225 #endif |
166 | 226 |
167 #ifdef THIRTY_TWO_BIT | 227 #ifdef THIRTY_TWO_BIT |
168 #ifdef BN_LLONG | 228 #ifdef BN_LLONG |
169 # if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__) | 229 # if defined(_WIN32) && !defined(__GNUC__) |
170 # define BN_ULLONG unsigned __int64 | 230 # define BN_ULLONG unsigned __int64 |
| 231 # define BN_MASK (0xffffffffffffffffI64) |
171 # else | 232 # else |
172 # define BN_ULLONG unsigned long long | 233 # define BN_ULLONG unsigned long long |
| 234 # define BN_MASK (0xffffffffffffffffLL) |
173 # endif | 235 # endif |
174 #endif | 236 #endif |
175 #define BN_ULONG» unsigned long | 237 #define BN_ULONG» unsigned int |
176 #define BN_LONG»» long | 238 #define BN_LONG»» int |
177 #define BN_BITS 64 | 239 #define BN_BITS 64 |
178 #define BN_BYTES 4 | 240 #define BN_BYTES 4 |
179 #define BN_BITS2 32 | 241 #define BN_BITS2 32 |
180 #define BN_BITS4 16 | 242 #define BN_BITS4 16 |
181 #ifdef OPENSSL_SYS_WIN32 | |
182 /* VC++ doesn't like the LL suffix */ | |
183 #define BN_MASK (0xffffffffffffffffL) | |
184 #else | |
185 #define BN_MASK (0xffffffffffffffffLL) | |
186 #endif | |
187 #define BN_MASK2 (0xffffffffL) | 243 #define BN_MASK2 (0xffffffffL) |
188 #define BN_MASK2l (0xffff) | 244 #define BN_MASK2l (0xffff) |
189 #define BN_MASK2h1 (0xffff8000L) | 245 #define BN_MASK2h1 (0xffff8000L) |
190 #define BN_MASK2h (0xffff0000L) | 246 #define BN_MASK2h (0xffff0000L) |
191 #define BN_TBIT (0x80000000L) | 247 #define BN_TBIT (0x80000000L) |
192 #define BN_DEC_CONV (1000000000L) | 248 #define BN_DEC_CONV (1000000000L) |
193 #define BN_DEC_FMT1» "%lu" | 249 #define BN_DEC_FMT1» "%u" |
194 #define BN_DEC_FMT2» "%09lu" | 250 #define BN_DEC_FMT2» "%09u" |
195 #define BN_DEC_NUM 9 | 251 #define BN_DEC_NUM 9 |
| 252 #define BN_HEX_FMT1 "%X" |
| 253 #define BN_HEX_FMT2 "%08X" |
196 #endif | 254 #endif |
197 | 255 |
198 #ifdef SIXTEEN_BIT | 256 /* 2011-02-22 SMS. |
199 #ifndef BN_DIV2W | 257 * In various places, a size_t variable or a type cast to size_t was |
200 #define BN_DIV2W | 258 * used to perform integer-only operations on pointers. This failed on |
201 #endif | 259 * VMS with 64-bit pointers (CC /POINTER_SIZE = 64) because size_t is |
202 #define BN_ULLONG» unsigned long | 260 * still only 32 bits. What's needed in these cases is an integer type |
203 #define BN_ULONG» unsigned short | 261 * with the same size as a pointer, which size_t is not certain to be. |
204 #define BN_LONG»» short | 262 * The only fix here is VMS-specific. |
205 #define BN_BITS»» 32 | 263 */ |
206 #define BN_BYTES» 2 | 264 #if defined(OPENSSL_SYS_VMS) |
207 #define BN_BITS2» 16 | 265 # if __INITIAL_POINTER_SIZE == 64 |
208 #define BN_BITS4» 8 | 266 # define PTR_SIZE_INT long long |
209 #define BN_MASK»» (0xffffffff) | 267 # else /* __INITIAL_POINTER_SIZE == 64 */ |
210 #define BN_MASK2» (0xffff) | 268 # define PTR_SIZE_INT int |
211 #define BN_MASK2l» (0xff) | 269 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */ |
212 #define BN_MASK2h1» (0xff80) | 270 #else /* defined(OPENSSL_SYS_VMS) */ |
213 #define BN_MASK2h» (0xff00) | 271 # define PTR_SIZE_INT size_t |
214 #define BN_TBIT»» (0x8000) | 272 #endif /* defined(OPENSSL_SYS_VMS) [else] */ |
215 #define BN_DEC_CONV» (100000) | |
216 #define BN_DEC_FMT1» "%u" | |
217 #define BN_DEC_FMT2» "%05u" | |
218 #define BN_DEC_NUM» 5 | |
219 #endif | |
220 | |
221 #ifdef EIGHT_BIT | |
222 #ifndef BN_DIV2W | |
223 #define BN_DIV2W | |
224 #endif | |
225 #define BN_ULLONG» unsigned short | |
226 #define BN_ULONG» unsigned char | |
227 #define BN_LONG»» char | |
228 #define BN_BITS»» 16 | |
229 #define BN_BYTES» 1 | |
230 #define BN_BITS2» 8 | |
231 #define BN_BITS4» 4 | |
232 #define BN_MASK»» (0xffff) | |
233 #define BN_MASK2» (0xff) | |
234 #define BN_MASK2l» (0xf) | |
235 #define BN_MASK2h1» (0xf8) | |
236 #define BN_MASK2h» (0xf0) | |
237 #define BN_TBIT»» (0x80) | |
238 #define BN_DEC_CONV» (100) | |
239 #define BN_DEC_FMT1» "%u" | |
240 #define BN_DEC_FMT2» "%02u" | |
241 #define BN_DEC_NUM» 2 | |
242 #endif | |
243 | 273 |
244 #define BN_DEFAULT_BITS 1280 | 274 #define BN_DEFAULT_BITS 1280 |
245 | 275 |
246 #define BN_FLG_MALLOCED 0x01 | 276 #define BN_FLG_MALLOCED 0x01 |
247 #define BN_FLG_STATIC_DATA 0x02 | 277 #define BN_FLG_STATIC_DATA 0x02 |
248 #define BN_FLG_CONSTTIME 0x04 /* avoid leaking exponent information throu
gh timing, | 278 #define BN_FLG_CONSTTIME 0x04 /* avoid leaking exponent information throu
gh timing, |
249 * BN_mod_exp_mont() will call BN_mod_exp_m
ont_consttime, | 279 * BN_mod_exp_mont() will call BN_mod_exp_m
ont_consttime, |
250 * BN_div() will call BN_div_no_branch, | 280 * BN_div() will call BN_div_no_branch, |
251 * BN_mod_inverse() will call BN_mod_invers
e_no_branch. | 281 * BN_mod_inverse() will call BN_mod_invers
e_no_branch. |
252 */ | 282 */ |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 }; | 326 }; |
297 | 327 |
298 /* Used for montgomery multiplication */ | 328 /* Used for montgomery multiplication */ |
299 struct bn_mont_ctx_st | 329 struct bn_mont_ctx_st |
300 { | 330 { |
301 int ri; /* number of bits in R */ | 331 int ri; /* number of bits in R */ |
302 BIGNUM RR; /* used to convert to montgomery form */ | 332 BIGNUM RR; /* used to convert to montgomery form */ |
303 BIGNUM N; /* The modulus */ | 333 BIGNUM N; /* The modulus */ |
304 BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 | 334 BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 |
305 * (Ni is only stored for bignum algorithm) */ | 335 * (Ni is only stored for bignum algorithm) */ |
306 #if 0 | 336 » BN_ULONG n0[2];/* least significant word(s) of Ni; |
307 » /* OpenSSL 0.9.9 preview: */ | 337 » (type changed with 0.9.9, was "BN_ULONG n0;" before) *
/ |
308 » BN_ULONG n0[2];/* least significant word(s) of Ni */ | |
309 #else | |
310 » BN_ULONG n0; /* least significant word of Ni */ | |
311 #endif | |
312 int flags; | 338 int flags; |
313 }; | 339 }; |
314 | 340 |
315 /* Used for reciprocal division/mod functions | 341 /* Used for reciprocal division/mod functions |
316 * It cannot be shared between threads | 342 * It cannot be shared between threads |
317 */ | 343 */ |
318 struct bn_recp_ctx_st | 344 struct bn_recp_ctx_st |
319 { | 345 { |
320 BIGNUM N; /* the divisor */ | 346 BIGNUM N; /* the divisor */ |
321 BIGNUM Nr; /* the reciprocal */ | 347 BIGNUM Nr; /* the reciprocal */ |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 int BN_rshift1(BIGNUM *r, const BIGNUM *a); | 523 int BN_rshift1(BIGNUM *r, const BIGNUM *a); |
498 void BN_clear(BIGNUM *a); | 524 void BN_clear(BIGNUM *a); |
499 BIGNUM *BN_dup(const BIGNUM *a); | 525 BIGNUM *BN_dup(const BIGNUM *a); |
500 int BN_ucmp(const BIGNUM *a, const BIGNUM *b); | 526 int BN_ucmp(const BIGNUM *a, const BIGNUM *b); |
501 int BN_set_bit(BIGNUM *a, int n); | 527 int BN_set_bit(BIGNUM *a, int n); |
502 int BN_clear_bit(BIGNUM *a, int n); | 528 int BN_clear_bit(BIGNUM *a, int n); |
503 char * BN_bn2hex(const BIGNUM *a); | 529 char * BN_bn2hex(const BIGNUM *a); |
504 char * BN_bn2dec(const BIGNUM *a); | 530 char * BN_bn2dec(const BIGNUM *a); |
505 int BN_hex2bn(BIGNUM **a, const char *str); | 531 int BN_hex2bn(BIGNUM **a, const char *str); |
506 int BN_dec2bn(BIGNUM **a, const char *str); | 532 int BN_dec2bn(BIGNUM **a, const char *str); |
| 533 int BN_asc2bn(BIGNUM **a, const char *str); |
507 int BN_gcd(BIGNUM *r,const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); | 534 int BN_gcd(BIGNUM *r,const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); |
508 int BN_kronecker(const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); /* returns -2
for error */ | 535 int BN_kronecker(const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); /* returns -2
for error */ |
509 BIGNUM *BN_mod_inverse(BIGNUM *ret, | 536 BIGNUM *BN_mod_inverse(BIGNUM *ret, |
510 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); | 537 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); |
511 BIGNUM *BN_mod_sqrt(BIGNUM *ret, | 538 BIGNUM *BN_mod_sqrt(BIGNUM *ret, |
512 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); | 539 const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx); |
513 | 540 |
514 /* Deprecated versions */ | 541 /* Deprecated versions */ |
515 #ifndef OPENSSL_NO_DEPRECATED | 542 #ifndef OPENSSL_NO_DEPRECATED |
516 BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe, | 543 BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe, |
517 const BIGNUM *add, const BIGNUM *rem, | 544 const BIGNUM *add, const BIGNUM *rem, |
518 void (*callback)(int,int,void *),void *cb_arg); | 545 void (*callback)(int,int,void *),void *cb_arg); |
519 int BN_is_prime(const BIGNUM *p,int nchecks, | 546 int BN_is_prime(const BIGNUM *p,int nchecks, |
520 void (*callback)(int,int,void *), | 547 void (*callback)(int,int,void *), |
521 BN_CTX *ctx,void *cb_arg); | 548 BN_CTX *ctx,void *cb_arg); |
522 int BN_is_prime_fasttest(const BIGNUM *p,int nchecks, | 549 int BN_is_prime_fasttest(const BIGNUM *p,int nchecks, |
523 void (*callback)(int,int,void *),BN_CTX *ctx,void *cb_arg, | 550 void (*callback)(int,int,void *),BN_CTX *ctx,void *cb_arg, |
524 int do_trial_division); | 551 int do_trial_division); |
525 #endif /* !defined(OPENSSL_NO_DEPRECATED) */ | 552 #endif /* !defined(OPENSSL_NO_DEPRECATED) */ |
526 | 553 |
527 /* Newer versions */ | 554 /* Newer versions */ |
528 int BN_generate_prime_ex(BIGNUM *ret,int bits,int safe, const BIGNUM *add, | 555 int BN_generate_prime_ex(BIGNUM *ret,int bits,int safe, const BIGNUM *add, |
529 const BIGNUM *rem, BN_GENCB *cb); | 556 const BIGNUM *rem, BN_GENCB *cb); |
530 int BN_is_prime_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, BN_GENCB *cb); | 557 int BN_is_prime_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, BN_GENCB *cb); |
531 int BN_is_prime_fasttest_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, | 558 int BN_is_prime_fasttest_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, |
532 int do_trial_division, BN_GENCB *cb); | 559 int do_trial_division, BN_GENCB *cb); |
533 | 560 |
534 int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx); | |
535 | |
536 int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, | |
537 const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2, | |
538 const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb); | |
539 int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, | |
540 BIGNUM *Xp1, BIGNUM *Xp2, | |
541 const BIGNUM *Xp, | |
542 const BIGNUM *e, BN_CTX *ctx, | |
543 BN_GENCB *cb); | |
544 | |
545 BN_MONT_CTX *BN_MONT_CTX_new(void ); | 561 BN_MONT_CTX *BN_MONT_CTX_new(void ); |
546 void BN_MONT_CTX_init(BN_MONT_CTX *ctx); | 562 void BN_MONT_CTX_init(BN_MONT_CTX *ctx); |
547 int BN_mod_mul_montgomery(BIGNUM *r,const BIGNUM *a,const BIGNUM *b, | 563 int BN_mod_mul_montgomery(BIGNUM *r,const BIGNUM *a,const BIGNUM *b, |
548 BN_MONT_CTX *mont, BN_CTX *ctx); | 564 BN_MONT_CTX *mont, BN_CTX *ctx); |
549 #define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\ | 565 #define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\ |
550 (r),(a),&((mont)->RR),(mont),(ctx)) | 566 (r),(a),&((mont)->RR),(mont),(ctx)) |
551 int BN_from_montgomery(BIGNUM *r,const BIGNUM *a, | 567 int BN_from_montgomery(BIGNUM *r,const BIGNUM *a, |
552 BN_MONT_CTX *mont, BN_CTX *ctx); | 568 BN_MONT_CTX *mont, BN_CTX *ctx); |
553 void BN_MONT_CTX_free(BN_MONT_CTX *mont); | 569 void BN_MONT_CTX_free(BN_MONT_CTX *mont); |
554 int BN_MONT_CTX_set(BN_MONT_CTX *mont,const BIGNUM *mod,BN_CTX *ctx); | 570 int BN_MONT_CTX_set(BN_MONT_CTX *mont,const BIGNUM *mod,BN_CTX *ctx); |
555 BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to,BN_MONT_CTX *from); | 571 BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to,BN_MONT_CTX *from); |
556 BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock, | 572 BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock, |
557 const BIGNUM *mod, BN_CTX *ctx); | 573 const BIGNUM *mod, BN_CTX *ctx); |
558 | 574 |
559 /* BN_BLINDING flags */ | 575 /* BN_BLINDING flags */ |
560 #define BN_BLINDING_NO_UPDATE 0x00000001 | 576 #define BN_BLINDING_NO_UPDATE 0x00000001 |
561 #define BN_BLINDING_NO_RECREATE 0x00000002 | 577 #define BN_BLINDING_NO_RECREATE 0x00000002 |
562 | 578 |
563 BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, /* const */ BIGN
UM *mod); | 579 BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod); |
564 void BN_BLINDING_free(BN_BLINDING *b); | 580 void BN_BLINDING_free(BN_BLINDING *b); |
565 int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx); | 581 int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx); |
566 int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); | 582 int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); |
567 int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); | 583 int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); |
568 int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *); | 584 int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *); |
569 int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *); | 585 int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *); |
| 586 #ifndef OPENSSL_NO_DEPRECATED |
570 unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *); | 587 unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *); |
571 void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long); | 588 void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long); |
| 589 #endif |
| 590 CRYPTO_THREADID *BN_BLINDING_thread_id(BN_BLINDING *); |
572 unsigned long BN_BLINDING_get_flags(const BN_BLINDING *); | 591 unsigned long BN_BLINDING_get_flags(const BN_BLINDING *); |
573 void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long); | 592 void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long); |
574 BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b, | 593 BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b, |
575 » const BIGNUM *e, /* const */ BIGNUM *m, BN_CTX *ctx, | 594 » const BIGNUM *e, BIGNUM *m, BN_CTX *ctx, |
576 int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | 595 int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
577 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx), | 596 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx), |
578 BN_MONT_CTX *m_ctx); | 597 BN_MONT_CTX *m_ctx); |
579 | 598 |
580 #ifndef OPENSSL_NO_DEPRECATED | 599 #ifndef OPENSSL_NO_DEPRECATED |
581 void BN_set_params(int mul,int high,int low,int mont); | 600 void BN_set_params(int mul,int high,int low,int mont); |
582 int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */ | 601 int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */ |
583 #endif | 602 #endif |
584 | 603 |
585 void BN_RECP_CTX_init(BN_RECP_CTX *recp); | 604 void BN_RECP_CTX_init(BN_RECP_CTX *recp); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | 637 int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
619 BN_CTX *ctx); /* r = sqrt(a) mod p */ | 638 BN_CTX *ctx); /* r = sqrt(a) mod p */ |
620 int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | 639 int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
621 BN_CTX *ctx); /* r^2 + r = a mod p */ | 640 BN_CTX *ctx); /* r^2 + r = a mod p */ |
622 #define BN_GF2m_cmp(a, b) BN_ucmp((a), (b)) | 641 #define BN_GF2m_cmp(a, b) BN_ucmp((a), (b)) |
623 /* Some functions allow for representation of the irreducible polynomials | 642 /* Some functions allow for representation of the irreducible polynomials |
624 * as an unsigned int[], say p. The irreducible f(t) is then of the form: | 643 * as an unsigned int[], say p. The irreducible f(t) is then of the form: |
625 * t^p[0] + t^p[1] + ... + t^p[k] | 644 * t^p[0] + t^p[1] + ... + t^p[k] |
626 * where m = p[0] > p[1] > ... > p[k] = 0. | 645 * where m = p[0] > p[1] > ... > p[k] = 0. |
627 */ | 646 */ |
628 int» BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[]); | 647 int» BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[]); |
629 /* r = a mod p */ | 648 /* r = a mod p */ |
630 int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | 649 int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
631 » const unsigned int p[], BN_CTX *ctx); /* r = (a * b) mod p */ | 650 » const int p[], BN_CTX *ctx); /* r = (a * b) mod p */ |
632 int» BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[], | 651 int» BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], |
633 BN_CTX *ctx); /* r = (a * a) mod p */ | 652 BN_CTX *ctx); /* r = (a * a) mod p */ |
634 int» BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const unsigned int p[], | 653 int» BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const int p[], |
635 BN_CTX *ctx); /* r = (1 / b) mod p */ | 654 BN_CTX *ctx); /* r = (1 / b) mod p */ |
636 int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | 655 int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
637 » const unsigned int p[], BN_CTX *ctx); /* r = (a / b) mod p */ | 656 » const int p[], BN_CTX *ctx); /* r = (a / b) mod p */ |
638 int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | 657 int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
639 » const unsigned int p[], BN_CTX *ctx); /* r = (a ^ b) mod p */ | 658 » const int p[], BN_CTX *ctx); /* r = (a ^ b) mod p */ |
640 int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, | 659 int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, |
641 » const unsigned int p[], BN_CTX *ctx); /* r = sqrt(a) mod p */ | 660 » const int p[], BN_CTX *ctx); /* r = sqrt(a) mod p */ |
642 int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a, | 661 int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a, |
643 » const unsigned int p[], BN_CTX *ctx); /* r^2 + r = a mod p */ | 662 » const int p[], BN_CTX *ctx); /* r^2 + r = a mod p */ |
644 int» BN_GF2m_poly2arr(const BIGNUM *a, unsigned int p[], int max); | 663 int» BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max); |
645 int» BN_GF2m_arr2poly(const unsigned int p[], BIGNUM *a); | 664 int» BN_GF2m_arr2poly(const int p[], BIGNUM *a); |
646 | 665 |
647 /* faster mod functions for the 'NIST primes' | 666 /* faster mod functions for the 'NIST primes' |
648 * 0 <= a < p^2 */ | 667 * 0 <= a < p^2 */ |
649 int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); | 668 int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); |
650 int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); | 669 int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); |
651 int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); | 670 int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); |
652 int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); | 671 int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); |
653 int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); | 672 int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); |
654 | 673 |
655 const BIGNUM *BN_get0_nist_prime_192(void); | 674 const BIGNUM *BN_get0_nist_prime_192(void); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 | 763 |
745 #define bn_pollute(a) | 764 #define bn_pollute(a) |
746 #define bn_check_top(a) | 765 #define bn_check_top(a) |
747 #define bn_fix_top(a) bn_correct_top(a) | 766 #define bn_fix_top(a) bn_correct_top(a) |
748 | 767 |
749 #endif | 768 #endif |
750 | 769 |
751 #define bn_correct_top(a) \ | 770 #define bn_correct_top(a) \ |
752 { \ | 771 { \ |
753 BN_ULONG *ftl; \ | 772 BN_ULONG *ftl; \ |
754 » if ((a)->top > 0) \ | 773 » int tmp_top = (a)->top; \ |
| 774 » if (tmp_top > 0) \ |
755 { \ | 775 { \ |
756 » » for (ftl= &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \ | 776 » » for (ftl= &((a)->d[tmp_top-1]); tmp_top > 0; tmp_top--) \ |
757 » » if (*(ftl--)) break; \ | 777 » » » if (*(ftl--)) break; \ |
| 778 » » (a)->top = tmp_top; \ |
758 } \ | 779 } \ |
759 bn_pollute(a); \ | 780 bn_pollute(a); \ |
760 } | 781 } |
761 | 782 |
762 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
; | 783 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
; |
763 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); | 784 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); |
764 void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num); | 785 void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num); |
765 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d); | 786 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d); |
766 BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int n
um); | 787 BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int n
um); |
767 BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int n
um); | 788 BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int n
um); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 #define BN_R_NO_INVERSE 108 | 867 #define BN_R_NO_INVERSE 108 |
847 #define BN_R_NO_SOLUTION 116 | 868 #define BN_R_NO_SOLUTION 116 |
848 #define BN_R_P_IS_NOT_PRIME 112 | 869 #define BN_R_P_IS_NOT_PRIME 112 |
849 #define BN_R_TOO_MANY_ITERATIONS 113 | 870 #define BN_R_TOO_MANY_ITERATIONS 113 |
850 #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109 | 871 #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109 |
851 | 872 |
852 #ifdef __cplusplus | 873 #ifdef __cplusplus |
853 } | 874 } |
854 #endif | 875 #endif |
855 #endif | 876 #endif |
OLD | NEW |