1 /* $OpenBSD: bn_local.h,v 1.46 2025/01/21 15:44:22 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 typedef struct bn_recp_ctx_st BN_RECP_CTX; 142 143 /* Used for slow "generation" functions. */ 144 struct bn_gencb_st { 145 unsigned int ver; /* To handle binary (in)compatibility */ 146 void *arg; /* callback-specific data */ 147 union { 148 /* if(ver==1) - handles old style callbacks */ 149 void (*cb_1)(int, int, void *); 150 /* if(ver==2) - new callback style */ 151 int (*cb_2)(int, int, BN_GENCB *); 152 } cb; 153 }; 154 155 /* 156 * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions 157 * 158 * 159 * For window size 'w' (w >= 2) and a random 'b' bits exponent, 160 * the number of multiplications is a constant plus on average 161 * 162 * 2^(w-1) + (b-w)/(w+1); 163 * 164 * here 2^(w-1) is for precomputing the table (we actually need 165 * entries only for windows that have the lowest bit set), and 166 * (b-w)/(w+1) is an approximation for the expected number of 167 * w-bit windows, not counting the first one. 168 * 169 * Thus we should use 170 * 171 * w >= 6 if b > 671 172 * w = 5 if 671 > b > 239 173 * w = 4 if 239 > b > 79 174 * w = 3 if 79 > b > 23 175 * w <= 2 if 23 > b 176 * 177 * (with draws in between). Very small exponents are often selected 178 * with low Hamming weight, so we use w = 1 for b <= 23. 179 */ 180 #define BN_window_bits_for_exponent_size(b) \ 181 ((b) > 671 ? 6 : \ 182 (b) > 239 ? 5 : \ 183 (b) > 79 ? 4 : \ 184 (b) > 23 ? 3 : 1) 185 186 187 /* BN_mod_exp_mont_consttime is based on the assumption that the 188 * L1 data cache line width of the target processor is at least 189 * the following value. 190 */ 191 #define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH ( 64 ) 192 #define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1) 193 194 /* Window sizes optimized for fixed window size modular exponentiation 195 * algorithm (BN_mod_exp_mont_consttime). 196 * 197 * To achieve the security goals of BN_mode_exp_mont_consttime, the 198 * maximum size of the window must not exceed 199 * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). 200 * 201 * Window size thresholds are defined for cache line sizes of 32 and 64, 202 * cache line sizes where log_2(32)=5 and log_2(64)=6 respectively. A 203 * window size of 7 should only be used on processors that have a 128 204 * byte or greater cache line size. 205 */ 206 #if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64 207 208 # define BN_window_bits_for_ctime_exponent_size(b) \ 209 ((b) > 937 ? 6 : \ 210 (b) > 306 ? 5 : \ 211 (b) > 89 ? 4 : \ 212 (b) > 22 ? 3 : 1) 213 # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6) 214 215 #elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32 216 217 # define BN_window_bits_for_ctime_exponent_size(b) \ 218 ((b) > 306 ? 5 : \ 219 (b) > 89 ? 4 : \ 220 (b) > 22 ? 3 : 1) 221 # define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5) 222 223 #endif 224 225 226 /* Pentium pro 16,16,16,32,64 */ 227 /* Alpha 16,16,16,16.64 */ 228 #define BN_MULL_SIZE_NORMAL (16) /* 32 */ 229 #define BN_MUL_RECURSIVE_SIZE_NORMAL (16) /* 32 less than */ 230 #define BN_SQR_RECURSIVE_SIZE_NORMAL (16) /* 32 */ 231 #define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32) /* 32 */ 232 #define BN_MONT_CTX_SET_SIZE_WORD (64) /* 32 */ 233 234 /* The least significant word of a BIGNUM. */ 235 #define BN_lsw(n) (((n)->top == 0) ? (BN_ULONG) 0 : (n)->d[0]) 236 237 BN_ULONG bn_add(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len, 238 const BN_ULONG *b, int b_len); 239 BN_ULONG bn_sub(BN_ULONG *r, int r_len, const BN_ULONG *a, int a_len, 240 const BN_ULONG *b, int b_len); 241 242 void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb); 243 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b); 244 void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b); 245 246 void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a); 247 void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a); 248 249 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 250 const BN_ULONG *np, const BN_ULONG *n0, int num); 251 252 void bn_correct_top(BIGNUM *a); 253 int bn_expand_bits(BIGNUM *a, size_t bits); 254 int bn_expand_bytes(BIGNUM *a, size_t bytes); 255 int bn_wexpand(BIGNUM *a, int words); 256 257 BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 258 int num); 259 BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 260 int num); 261 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); 262 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); 263 void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num); 264 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d); 265 void bn_div_rem_words(BN_ULONG h, BN_ULONG l, BN_ULONG d, BN_ULONG *out_q, 266 BN_ULONG *out_r); 267 268 int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom); 269 int bn_rand_in_range(BIGNUM *rnd, const BIGNUM *lower_inc, const BIGNUM *upper_exc); 270 int bn_rand_interval(BIGNUM *rnd, BN_ULONG lower_word, const BIGNUM *upper_exc); 271 272 void BN_init(BIGNUM *); 273 274 BN_RECP_CTX *BN_RECP_CTX_create(const BIGNUM *N); 275 void BN_RECP_CTX_free(BN_RECP_CTX *recp); 276 int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, 277 BN_CTX *ctx); 278 int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, 279 BN_RECP_CTX *recp, BN_CTX *ctx); 280 int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 281 const BIGNUM *m, BN_CTX *ctx); 282 283 /* Explicitly const time / non-const time versions for internal use */ 284 int BN_mod_exp_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 285 const BIGNUM *m, BN_CTX *ctx); 286 int BN_mod_exp_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 287 const BIGNUM *m, BN_CTX *ctx); 288 int BN_mod_exp_mont_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 289 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 290 int BN_mod_exp_mont_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 291 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 292 int BN_div_nonct(BIGNUM *q, BIGNUM *r, const BIGNUM *n, const BIGNUM *d, 293 BN_CTX *ctx); 294 int BN_div_ct(BIGNUM *q, BIGNUM *r, const BIGNUM *n, const BIGNUM *d, 295 BN_CTX *ctx); 296 int BN_mod_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 297 int BN_mod_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 298 299 int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p, 300 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 301 int BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, const BIGNUM *p1, 302 const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m, 303 BN_CTX *ctx, BN_MONT_CTX *m_ctx); 304 305 int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 306 const BIGNUM *m, BN_CTX *ctx); 307 308 BIGNUM *BN_mod_inverse_ct(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n, 309 BN_CTX *ctx); 310 BIGNUM *BN_mod_inverse_nonct(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n, 311 BN_CTX *ctx); 312 int BN_gcd_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 313 314 int BN_swap_ct(BN_ULONG swap, BIGNUM *a, BIGNUM *b, size_t nwords); 315 316 int bn_copy(BIGNUM *dst, const BIGNUM *src); 317 318 int bn_isqrt(BIGNUM *out_sqrt, int *out_perfect, const BIGNUM *n, BN_CTX *ctx); 319 int bn_is_perfect_square(int *out_perfect, const BIGNUM *n, BN_CTX *ctx); 320 321 int bn_is_prime_bpsw(int *is_prime, const BIGNUM *n, BN_CTX *ctx, size_t rounds); 322 323 int bn_printf(BIO *bio, const BIGNUM *bn, int indent, const char *fmt, ...) 324 __attribute__((__format__ (printf, 4, 5))) 325 __attribute__((__nonnull__ (4))); 326 327 int bn_bn2hex_nosign(const BIGNUM *bn, char **out, size_t *out_len); 328 int bn_bn2hex_nibbles(const BIGNUM *bn, char **out, size_t *out_len); 329 330 __END_HIDDEN_DECLS 331 #endif /* !HEADER_BN_LOCAL_H */ 332