1 /* $OpenBSD: bn_local.h,v 1.40 2024/03/02 09:27:31 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 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. 8 * 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). 15 * 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. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 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)" 40 * 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 51 * SUCH DAMAGE. 52 * 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.] 57 */ 58 /* ==================================================================== 59 * Copyright (c) 1998-2000 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 112 #ifndef HEADER_BN_LOCAL_H 113 #define HEADER_BN_LOCAL_H 114 115 #include <openssl/opensslconf.h> 116 117 #include <openssl/bn.h> 118 119 __BEGIN_HIDDEN_DECLS 120 121 struct bignum_st { 122 BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */ 123 int top; /* Index of last used d +1. */ 124 /* The next are internal book keeping for bn_expand. */ 125 int dmax; /* Size of the d array. */ 126 int neg; /* one if the number is negative */ 127 int flags; 128 }; 129 130 struct bn_mont_ctx_st { 131 int ri; /* Number of bits in R */ 132 BIGNUM RR; /* Used to convert to Montgomery form */ 133 BIGNUM N; /* Modulus */ 134 135 /* Least significant word(s) of Ni; R*(1/R mod N) - N*Ni = 1 */ 136 BN_ULONG n0[2]; 137 138 int flags; 139 }; 140 141 /* Used for reciprocal division/mod functions 142 * It cannot be shared between threads 143 */ 144 typedef struct bn_recp_ctx_st { 145 BIGNUM N; /* the divisor */ 146 BIGNUM Nr; /* the reciprocal */ 147 int num_bits; 148 int shift; 149 int flags; 150 } BN_RECP_CTX; 151 152 /* Used for slow "generation" functions. */ 153 struct bn_gencb_st { 154 unsigned int ver; /* To handle binary (in)compatibility */ 155 void *arg; /* callback-specific data */ 156 union { 157 /* if(ver==1) - handles old style callbacks */ 158 void (*cb_1)(int, int, void *); 159 /* if(ver==2) - new callback style */ 160 int (*cb_2)(int, int, BN_GENCB *); 161 } cb; 162 }; 163 164 /* 165 * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions 166 * 167 * 168 * For window size 'w' (w >= 2) and a random 'b' bits exponent, 169 * the number of multiplications is a constant plus on average 170 * 171 * 2^(w-1) + (b-w)/(w+1); 172 * 173 * here 2^(w-1) is for precomputing the table (we actually need 174 * entries only for windows that have the lowest bit set), and 175 * (b-w)/(w+1) is an approximation for the expected number of 176 * w-bit windows, not counting the first one. 177 * 178 * Thus we should use 179 * 180 * w >= 6 if b > 671 181 * w = 5 if 671 > b > 239 182 * w = 4 if 239 > b > 79 183 * w = 3 if 79 > b > 23 184 * w <= 2 if 23 > b 185 * 186 * (with draws in between). Very small exponents are often selected 187 * with low Hamming weight, so we use w = 1 for b <= 23. 188 */ 189 #define BN_window_bits_for_exponent_size(b) \ 190 ((b) > 671 ? 6 : \ 191 (b) > 239 ? 5 : \ 192 (b) > 79 ? 4 : \ 193 (b) > 23 ? 3 : 1) 194 195 196 /* BN_mod_exp_mont_consttime is based on the assumption that the 197 * L1 data cache line width of the target processor is at least 198 * the following value. 199 */ 200 #define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH ( 64 ) 201 #define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1) 202 203 /* Window sizes optimized for fixed window size modular exponentiation 204 * algorithm (BN_mod_exp_mont_consttime). 205 * 206 * To achieve the security goals of BN_mode_exp_mont_consttime, the 207 * maximum size of the window must not exceed 208 * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). 209 * 210 * Window size thresholds are defined for cache line sizes of 32 and 64, 211 * cache line sizes where log_2(32)=5 and log_2(64)=6 respectively. A 212 * window size of 7 should only be used on processors that have a 128 213 * byte or greater cache line size. 214 */ 215 #if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64 216 217 # define BN_window_bits_for_ctime_exponent_size(b) \ 218 ((b) > 937 ? 6 : \ 219 (b) > 306 ? 5 : \ 220 (b) > 89 ? 4 : \ 221 (b) > 22 ? 3 : 1) 222 # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6) 223 224 #elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32 225 226 # define BN_window_bits_for_ctime_exponent_size(b) \ 227 ((b) > 306 ? 5 : \ 228 (b) > 89 ? 4 : \ 229 (b) > 22 ? 3 : 1) 230 # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5) 231 232 #endif 233 234 235 /* Pentium pro 16,16,16,32,64 */ 236 /* Alpha 16,16,16,16.64 */ 237 #define BN_MULL_SIZE_NORMAL (16) /* 32 */ 238 #define BN_MUL_RECURSIVE_SIZE_NORMAL (16) /* 32 less than */ 239 #define BN_SQR_RECURSIVE_SIZE_NORMAL (16) /* 32 */ 240 #define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32) /* 32 */ 241 #define BN_MONT_CTX_SET_SIZE_WORD (64) /* 32 */ 242 243 /* The least significant word of a BIGNUM. */ 244 #define BN_lsw(n) (((n)->top == 0) ? (BN_ULONG) 0 : (n)->d[0]) 245 246 BN_ULONG bn_add(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len, 247 const BN_ULONG *b, int b_len); 248 BN_ULONG bn_sub(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len, 249 const BN_ULONG *b, int b_len); 250 251 void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb); 252 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b); 253 void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b); 254 255 void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a); 256 void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a); 257 258 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 259 const BN_ULONG *np, const BN_ULONG *n0, int num); 260 261 void bn_correct_top(BIGNUM *a); 262 int bn_expand(BIGNUM *a, int bits); 263 int bn_wexpand(BIGNUM *a, int words); 264 265 BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 266 int num); 267 BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 268 int num); 269 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); 270 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); 271 void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num); 272 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d); 273 void bn_div_rem_words(BN_ULONG h, BN_ULONG l, BN_ULONG d, BN_ULONG *out_q, 274 BN_ULONG *out_r); 275 276 int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom); 277 int bn_rand_in_range(BIGNUM *rnd, const BIGNUM *lower_inc, const BIGNUM *upper_exc); 278 int bn_rand_interval(BIGNUM *rnd, BN_ULONG lower_word, const BIGNUM *upper_exc); 279 280 void BN_init(BIGNUM *); 281 282 int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx); 283 284 void BN_RECP_CTX_init(BN_RECP_CTX *recp); 285 BN_RECP_CTX *BN_RECP_CTX_new(void); 286 void BN_RECP_CTX_free(BN_RECP_CTX *recp); 287 int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *rdiv, BN_CTX *ctx); 288 int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, 289 BN_RECP_CTX *recp, BN_CTX *ctx); 290 int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 291 const BIGNUM *m, BN_CTX *ctx); 292 int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, 293 BN_RECP_CTX *recp, BN_CTX *ctx); 294 295 /* Explicitly const time / non-const time versions for internal use */ 296 int BN_mod_exp_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 297 const BIGNUM *m, BN_CTX *ctx); 298 int BN_mod_exp_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 299 const BIGNUM *m, BN_CTX *ctx); 300 int BN_mod_exp_mont_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 301 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 302 int BN_mod_exp_mont_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 303 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 304 int BN_div_nonct(BIGNUM *q, BIGNUM *r, const BIGNUM *n, const BIGNUM *d, 305 BN_CTX *ctx); 306 int BN_div_ct(BIGNUM *q, BIGNUM *r, const BIGNUM *n, const BIGNUM *d, 307 BN_CTX *ctx); 308 int BN_mod_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 309 int BN_mod_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 310 311 int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p, 312 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 313 int BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, const BIGNUM *p1, 314 const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m, 315 BN_CTX *ctx, BN_MONT_CTX *m_ctx); 316 317 int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 318 const BIGNUM *m, BN_CTX *ctx); 319 320 BIGNUM *BN_mod_inverse_ct(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n, 321 BN_CTX *ctx); 322 BIGNUM *BN_mod_inverse_nonct(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n, 323 BN_CTX *ctx); 324 int BN_gcd_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 325 int BN_gcd_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 326 327 int BN_swap_ct(BN_ULONG swap, BIGNUM *a, BIGNUM *b, size_t nwords); 328 329 int bn_copy(BIGNUM *dst, const BIGNUM *src); 330 331 int bn_isqrt(BIGNUM *out_sqrt, int *out_perfect, const BIGNUM *n, BN_CTX *ctx); 332 int bn_is_perfect_square(int *out_perfect, const BIGNUM *n, BN_CTX *ctx); 333 334 int bn_is_prime_bpsw(int *is_prime, const BIGNUM *n, BN_CTX *ctx, size_t rounds); 335 336 int bn_printf(BIO *bio, const BIGNUM *bn, int indent, const char *fmt, ...) 337 __attribute__((__format__ (printf, 4, 5))) 338 __attribute__((__nonnull__ (4))); 339 340 int bn_bn2hex_nosign(const BIGNUM *bn, char **out, size_t *out_len); 341 int bn_bn2hex_nibbles(const BIGNUM *bn, char **out, size_t *out_len); 342 343 __END_HIDDEN_DECLS 344 #endif /* !HEADER_BN_LOCAL_H */ 345