155714Skris/* crypto/bn/bn.h */ 255714Skris/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296341Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296341Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296341Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296341Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296341Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 58160814Ssimon/* ==================================================================== 59238405Sjkim * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. 60238405Sjkim * 61238405Sjkim * Redistribution and use in source and binary forms, with or without 62238405Sjkim * modification, are permitted provided that the following conditions 63238405Sjkim * are met: 64238405Sjkim * 65238405Sjkim * 1. Redistributions of source code must retain the above copyright 66296341Sdelphij * notice, this list of conditions and the following disclaimer. 67238405Sjkim * 68238405Sjkim * 2. Redistributions in binary form must reproduce the above copyright 69238405Sjkim * notice, this list of conditions and the following disclaimer in 70238405Sjkim * the documentation and/or other materials provided with the 71238405Sjkim * distribution. 72238405Sjkim * 73238405Sjkim * 3. All advertising materials mentioning features or use of this 74238405Sjkim * software must display the following acknowledgment: 75238405Sjkim * "This product includes software developed by the OpenSSL Project 76238405Sjkim * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77238405Sjkim * 78238405Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79238405Sjkim * endorse or promote products derived from this software without 80238405Sjkim * prior written permission. For written permission, please contact 81238405Sjkim * openssl-core@openssl.org. 82238405Sjkim * 83238405Sjkim * 5. Products derived from this software may not be called "OpenSSL" 84238405Sjkim * nor may "OpenSSL" appear in their names without prior written 85238405Sjkim * permission of the OpenSSL Project. 86238405Sjkim * 87238405Sjkim * 6. Redistributions of any form whatsoever must retain the following 88238405Sjkim * acknowledgment: 89238405Sjkim * "This product includes software developed by the OpenSSL Project 90238405Sjkim * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91238405Sjkim * 92238405Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93238405Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94238405Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95238405Sjkim * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96238405Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97238405Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98238405Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99238405Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100238405Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101238405Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102238405Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103238405Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE. 104238405Sjkim * ==================================================================== 105238405Sjkim * 106238405Sjkim * This product includes cryptographic software written by Eric Young 107238405Sjkim * (eay@cryptsoft.com). This product includes software written by Tim 108238405Sjkim * Hudson (tjh@cryptsoft.com). 109238405Sjkim * 110238405Sjkim */ 111238405Sjkim/* ==================================================================== 112160814Ssimon * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 113160814Ssimon * 114296341Sdelphij * Portions of the attached software ("Contribution") are developed by 115160814Ssimon * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. 116160814Ssimon * 117160814Ssimon * The Contribution is licensed pursuant to the Eric Young open source 118160814Ssimon * license provided above. 119160814Ssimon * 120296341Sdelphij * The binary polynomial arithmetic software is originally written by 121160814Ssimon * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. 122160814Ssimon * 123160814Ssimon */ 12455714Skris 12555714Skris#ifndef HEADER_BN_H 126296341Sdelphij# define HEADER_BN_H 12755714Skris 128296341Sdelphij# include <limits.h> 129296341Sdelphij# include <openssl/e_os2.h> 130296341Sdelphij# ifndef OPENSSL_NO_FP_API 131296341Sdelphij# include <stdio.h> /* FILE */ 132296341Sdelphij# endif 133296341Sdelphij# include <openssl/ossl_typ.h> 134296341Sdelphij# include <openssl/crypto.h> 13555714Skris 13655714Skris#ifdef __cplusplus 13755714Skrisextern "C" { 13855714Skris#endif 13955714Skris 140296341Sdelphij/* 141296341Sdelphij * These preprocessor symbols control various aspects of the bignum headers 142296341Sdelphij * and library code. They're not defined by any "normal" configuration, as 143296341Sdelphij * they are intended for development and testing purposes. NB: defining all 144296341Sdelphij * three can be useful for debugging application code as well as openssl 145296341Sdelphij * itself. BN_DEBUG - turn on various debugging alterations to the bignum 146296341Sdelphij * code BN_DEBUG_RAND - uses random poisoning of unused words to trip up 147160814Ssimon * mismanagement of bignum internals. You must also define BN_DEBUG. 148160814Ssimon */ 149160814Ssimon/* #define BN_DEBUG */ 150160814Ssimon/* #define BN_DEBUG_RAND */ 15155714Skris 152296341Sdelphij# ifndef OPENSSL_SMALL_FOOTPRINT 153296341Sdelphij# define BN_MUL_COMBA 154296341Sdelphij# define BN_SQR_COMBA 155296341Sdelphij# define BN_RECURSION 156296341Sdelphij# endif 15755714Skris 158296341Sdelphij/* 159296341Sdelphij * This next option uses the C libraries (2 word)/(1 word) function. If it is 160296341Sdelphij * not defined, I use my C version (which is slower). The reason for this 161296341Sdelphij * flag is that when the particular C compiler library routine is used, and 162296341Sdelphij * the library is linked with a different compiler, the library is missing. 163296341Sdelphij * This mostly happens when the library is built with gcc and then linked 164296341Sdelphij * using normal cc. This would be a common occurrence because gcc normally 165296341Sdelphij * produces code that is 2 times faster than system compilers for the big 166296341Sdelphij * number stuff. For machines with only one compiler (or shared libraries), 167296341Sdelphij * this should be on. Again this in only really a problem on machines using 168296341Sdelphij * "long long's", are 32bit, and are not using my assembler code. 169296341Sdelphij */ 170296341Sdelphij# if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \ 171109998Smarkm defined(OPENSSL_SYS_WIN32) || defined(linux) 172296341Sdelphij# ifndef BN_DIV2W 173296341Sdelphij# define BN_DIV2W 174296341Sdelphij# endif 17589837Skris# endif 17655714Skris 177296341Sdelphij/* 178296341Sdelphij * assuming long is 64bit - this is the DEC Alpha unsigned long long is only 179296341Sdelphij * 64 bits :-(, don't define BN_LLONG for the DEC Alpha 180296341Sdelphij */ 181296341Sdelphij# ifdef SIXTY_FOUR_BIT_LONG 182296341Sdelphij# define BN_ULLONG unsigned long long 183296341Sdelphij# define BN_ULONG unsigned long 184296341Sdelphij# define BN_LONG long 185296341Sdelphij# define BN_BITS 128 186296341Sdelphij# define BN_BYTES 8 187296341Sdelphij# define BN_BITS2 64 188296341Sdelphij# define BN_BITS4 32 189296341Sdelphij# define BN_MASK (0xffffffffffffffffffffffffffffffffLL) 190296341Sdelphij# define BN_MASK2 (0xffffffffffffffffL) 191296341Sdelphij# define BN_MASK2l (0xffffffffL) 192296341Sdelphij# define BN_MASK2h (0xffffffff00000000L) 193296341Sdelphij# define BN_MASK2h1 (0xffffffff80000000L) 194296341Sdelphij# define BN_TBIT (0x8000000000000000L) 195296341Sdelphij# define BN_DEC_CONV (10000000000000000000UL) 196296341Sdelphij# define BN_DEC_FMT1 "%lu" 197296341Sdelphij# define BN_DEC_FMT2 "%019lu" 198296341Sdelphij# define BN_DEC_NUM 19 199296341Sdelphij# define BN_HEX_FMT1 "%lX" 200296341Sdelphij# define BN_HEX_FMT2 "%016lX" 201296341Sdelphij# endif 20255714Skris 203296341Sdelphij/* 204296341Sdelphij * This is where the long long data type is 64 bits, but long is 32. For 205296341Sdelphij * machines where there are 64bit registers, this is the mode to use. IRIX, 206296341Sdelphij * on R4000 and above should use this mode, along with the relevant assembler 207296341Sdelphij * code :-). Do NOT define BN_LLONG. 20855714Skris */ 209296341Sdelphij# ifdef SIXTY_FOUR_BIT 210296341Sdelphij# undef BN_LLONG 211296341Sdelphij# undef BN_ULLONG 212296341Sdelphij# define BN_ULONG unsigned long long 213296341Sdelphij# define BN_LONG long long 214296341Sdelphij# define BN_BITS 128 215296341Sdelphij# define BN_BYTES 8 216296341Sdelphij# define BN_BITS2 64 217296341Sdelphij# define BN_BITS4 32 218296341Sdelphij# define BN_MASK2 (0xffffffffffffffffLL) 219296341Sdelphij# define BN_MASK2l (0xffffffffL) 220296341Sdelphij# define BN_MASK2h (0xffffffff00000000LL) 221296341Sdelphij# define BN_MASK2h1 (0xffffffff80000000LL) 222296341Sdelphij# define BN_TBIT (0x8000000000000000LL) 223296341Sdelphij# define BN_DEC_CONV (10000000000000000000ULL) 224296341Sdelphij# define BN_DEC_FMT1 "%llu" 225296341Sdelphij# define BN_DEC_FMT2 "%019llu" 226296341Sdelphij# define BN_DEC_NUM 19 227296341Sdelphij# define BN_HEX_FMT1 "%llX" 228296341Sdelphij# define BN_HEX_FMT2 "%016llX" 229296341Sdelphij# endif 23055714Skris 231296341Sdelphij# ifdef THIRTY_TWO_BIT 232296341Sdelphij# ifdef BN_LLONG 233296341Sdelphij# if defined(_WIN32) && !defined(__GNUC__) 234296341Sdelphij# define BN_ULLONG unsigned __int64 235296341Sdelphij# define BN_MASK (0xffffffffffffffffI64) 236296341Sdelphij# else 237296341Sdelphij# define BN_ULLONG unsigned long long 238296341Sdelphij# define BN_MASK (0xffffffffffffffffLL) 239296341Sdelphij# endif 240296341Sdelphij# endif 241296341Sdelphij# define BN_ULONG unsigned int 242296341Sdelphij# define BN_LONG int 243296341Sdelphij# define BN_BITS 64 244296341Sdelphij# define BN_BYTES 4 245296341Sdelphij# define BN_BITS2 32 246296341Sdelphij# define BN_BITS4 16 247296341Sdelphij# define BN_MASK2 (0xffffffffL) 248296341Sdelphij# define BN_MASK2l (0xffff) 249296341Sdelphij# define BN_MASK2h1 (0xffff8000L) 250296341Sdelphij# define BN_MASK2h (0xffff0000L) 251296341Sdelphij# define BN_TBIT (0x80000000L) 252296341Sdelphij# define BN_DEC_CONV (1000000000L) 253296341Sdelphij# define BN_DEC_FMT1 "%u" 254296341Sdelphij# define BN_DEC_FMT2 "%09u" 255296341Sdelphij# define BN_DEC_NUM 9 256296341Sdelphij# define BN_HEX_FMT1 "%X" 257296341Sdelphij# define BN_HEX_FMT2 "%08X" 258160814Ssimon# endif 25955714Skris 260296341Sdelphij/* 261296341Sdelphij * 2011-02-22 SMS. In various places, a size_t variable or a type cast to 262296341Sdelphij * size_t was used to perform integer-only operations on pointers. This 263296341Sdelphij * failed on VMS with 64-bit pointers (CC /POINTER_SIZE = 64) because size_t 264296341Sdelphij * is still only 32 bits. What's needed in these cases is an integer type 265296341Sdelphij * with the same size as a pointer, which size_t is not certain to be. The 266296341Sdelphij * only fix here is VMS-specific. 267238405Sjkim */ 268296341Sdelphij# if defined(OPENSSL_SYS_VMS) 269296341Sdelphij# if __INITIAL_POINTER_SIZE == 64 270296341Sdelphij# define PTR_SIZE_INT long long 271296341Sdelphij# else /* __INITIAL_POINTER_SIZE == 64 */ 272296341Sdelphij# define PTR_SIZE_INT int 273296341Sdelphij# endif /* __INITIAL_POINTER_SIZE == 64 [else] */ 274296341Sdelphij# else /* defined(OPENSSL_SYS_VMS) */ 275296341Sdelphij# define PTR_SIZE_INT size_t 276296341Sdelphij# endif /* defined(OPENSSL_SYS_VMS) [else] */ 27755714Skris 278296341Sdelphij# define BN_DEFAULT_BITS 1280 27955714Skris 280296341Sdelphij# define BN_FLG_MALLOCED 0x01 281296341Sdelphij# define BN_FLG_STATIC_DATA 0x02 282194206Ssimon 283296341Sdelphij/* 284296341Sdelphij * avoid leaking exponent information through timing, 285296341Sdelphij * BN_mod_exp_mont() will call BN_mod_exp_mont_consttime, 286296341Sdelphij * BN_div() will call BN_div_no_branch, 287296341Sdelphij * BN_mod_inverse() will call BN_mod_inverse_no_branch. 288296341Sdelphij */ 289296341Sdelphij# define BN_FLG_CONSTTIME 0x04 290194206Ssimon 291296341Sdelphij# ifdef OPENSSL_NO_DEPRECATED 292296341Sdelphij/* deprecated name for the flag */ 293296341Sdelphij# define BN_FLG_EXP_CONSTTIME BN_FLG_CONSTTIME 294296341Sdelphij/* 295296341Sdelphij * avoid leaking exponent information through timings 296296341Sdelphij * (BN_mod_exp_mont() will call BN_mod_exp_mont_consttime) 297296341Sdelphij */ 298296341Sdelphij# endif 29955714Skris 300296341Sdelphij# ifndef OPENSSL_NO_DEPRECATED 301296341Sdelphij# define BN_FLG_FREE 0x8000 302296341Sdelphij /* used for debuging */ 303296341Sdelphij# endif 304296341Sdelphij# define BN_set_flags(b,n) ((b)->flags|=(n)) 305296341Sdelphij# define BN_get_flags(b,n) ((b)->flags&(n)) 306296341Sdelphij 307296341Sdelphij/* 308296341Sdelphij * get a clone of a BIGNUM with changed flags, for *temporary* use only (the 309296341Sdelphij * two BIGNUMs cannot not be used in parallel!) 310296341Sdelphij */ 311296341Sdelphij# define BN_with_flags(dest,b,n) ((dest)->d=(b)->d, \ 312160814Ssimon (dest)->top=(b)->top, \ 313160814Ssimon (dest)->dmax=(b)->dmax, \ 314160814Ssimon (dest)->neg=(b)->neg, \ 315160814Ssimon (dest)->flags=(((dest)->flags & BN_FLG_MALLOCED) \ 316160814Ssimon | ((b)->flags & ~BN_FLG_MALLOCED) \ 317160814Ssimon | BN_FLG_STATIC_DATA \ 318160814Ssimon | (n))) 319160814Ssimon 320160814Ssimon/* Already declared in ossl_typ.h */ 321296341Sdelphij# if 0 322160814Ssimontypedef struct bignum_st BIGNUM; 323160814Ssimon/* Used for temp variables (declaration hidden in bn_lcl.h) */ 324160814Ssimontypedef struct bignum_ctx BN_CTX; 325160814Ssimontypedef struct bn_blinding_st BN_BLINDING; 326160814Ssimontypedef struct bn_mont_ctx_st BN_MONT_CTX; 327160814Ssimontypedef struct bn_recp_ctx_st BN_RECP_CTX; 328160814Ssimontypedef struct bn_gencb_st BN_GENCB; 329296341Sdelphij# endif 330160814Ssimon 331296341Sdelphijstruct bignum_st { 332296341Sdelphij BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit 333296341Sdelphij * chunks. */ 334296341Sdelphij int top; /* Index of last used d +1. */ 335296341Sdelphij /* The next are internal book keeping for bn_expand. */ 336296341Sdelphij int dmax; /* Size of the d array. */ 337296341Sdelphij int neg; /* one if the number is negative */ 338296341Sdelphij int flags; 339296341Sdelphij}; 34055714Skris 34155714Skris/* Used for montgomery multiplication */ 342296341Sdelphijstruct bn_mont_ctx_st { 343296341Sdelphij int ri; /* number of bits in R */ 344296341Sdelphij BIGNUM RR; /* used to convert to montgomery form */ 345296341Sdelphij BIGNUM N; /* The modulus */ 346296341Sdelphij BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 (Ni is only 347296341Sdelphij * stored for bignum algorithm) */ 348296341Sdelphij BN_ULONG n0[2]; /* least significant word(s) of Ni; (type 349296341Sdelphij * changed with 0.9.9, was "BN_ULONG n0;" 350296341Sdelphij * before) */ 351296341Sdelphij int flags; 352296341Sdelphij}; 35355714Skris 354296341Sdelphij/* 355296341Sdelphij * Used for reciprocal division/mod functions It cannot be shared between 356296341Sdelphij * threads 35755714Skris */ 358296341Sdelphijstruct bn_recp_ctx_st { 359296341Sdelphij BIGNUM N; /* the divisor */ 360296341Sdelphij BIGNUM Nr; /* the reciprocal */ 361296341Sdelphij int num_bits; 362296341Sdelphij int shift; 363296341Sdelphij int flags; 364296341Sdelphij}; 36555714Skris 366160814Ssimon/* Used for slow "generation" functions. */ 367296341Sdelphijstruct bn_gencb_st { 368296341Sdelphij unsigned int ver; /* To handle binary (in)compatibility */ 369296341Sdelphij void *arg; /* callback-specific data */ 370296341Sdelphij union { 371296341Sdelphij /* if(ver==1) - handles old style callbacks */ 372296341Sdelphij void (*cb_1) (int, int, void *); 373296341Sdelphij /* if(ver==2) - new callback style */ 374296341Sdelphij int (*cb_2) (int, int, BN_GENCB *); 375296341Sdelphij } cb; 376296341Sdelphij}; 377160814Ssimon/* Wrapper function to make using BN_GENCB easier, */ 378160814Ssimonint BN_GENCB_call(BN_GENCB *cb, int a, int b); 379160814Ssimon/* Macro to populate a BN_GENCB structure with an "old"-style callback */ 380296341Sdelphij# define BN_GENCB_set_old(gencb, callback, cb_arg) { \ 381296341Sdelphij BN_GENCB *tmp_gencb = (gencb); \ 382296341Sdelphij tmp_gencb->ver = 1; \ 383296341Sdelphij tmp_gencb->arg = (cb_arg); \ 384296341Sdelphij tmp_gencb->cb.cb_1 = (callback); } 385160814Ssimon/* Macro to populate a BN_GENCB structure with a "new"-style callback */ 386296341Sdelphij# define BN_GENCB_set(gencb, callback, cb_arg) { \ 387296341Sdelphij BN_GENCB *tmp_gencb = (gencb); \ 388296341Sdelphij tmp_gencb->ver = 2; \ 389296341Sdelphij tmp_gencb->arg = (cb_arg); \ 390296341Sdelphij tmp_gencb->cb.cb_2 = (callback); } 391160814Ssimon 392296341Sdelphij# define BN_prime_checks 0 /* default: select number of iterations based 393296341Sdelphij * on the size of the number */ 39455714Skris 395296341Sdelphij/* 396296341Sdelphij * number of Miller-Rabin iterations for an error rate of less than 2^-80 for 397296341Sdelphij * random 'b'-bit input, b >= 100 (taken from table 4.4 in the Handbook of 398296341Sdelphij * Applied Cryptography [Menezes, van Oorschot, Vanstone; CRC Press 1996]; 399296341Sdelphij * original paper: Damgaard, Landrock, Pomerance: Average case error 400296341Sdelphij * estimates for the strong probable prime test. -- Math. Comp. 61 (1993) 401296341Sdelphij * 177-194) 402296341Sdelphij */ 403296341Sdelphij# define BN_prime_checks_for_size(b) ((b) >= 1300 ? 2 : \ 40459191Skris (b) >= 850 ? 3 : \ 40559191Skris (b) >= 650 ? 4 : \ 40659191Skris (b) >= 550 ? 5 : \ 40759191Skris (b) >= 450 ? 6 : \ 40859191Skris (b) >= 400 ? 7 : \ 40959191Skris (b) >= 350 ? 8 : \ 41059191Skris (b) >= 300 ? 9 : \ 41159191Skris (b) >= 250 ? 12 : \ 41259191Skris (b) >= 200 ? 15 : \ 41359191Skris (b) >= 150 ? 18 : \ 41459191Skris /* b >= 100 */ 27) 41559191Skris 416296341Sdelphij# define BN_num_bytes(a) ((BN_num_bits(a)+7)/8) 417109998Smarkm 418160814Ssimon/* Note that BN_abs_is_word didn't work reliably for w == 0 until 0.9.8 */ 419296341Sdelphij# define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \ 420296341Sdelphij (((w) == 0) && ((a)->top == 0))) 421296341Sdelphij# define BN_is_zero(a) ((a)->top == 0) 422296341Sdelphij# define BN_is_one(a) (BN_abs_is_word((a),1) && !(a)->neg) 423296341Sdelphij# define BN_is_word(a,w) (BN_abs_is_word((a),(w)) && (!(w) || !(a)->neg)) 424296341Sdelphij# define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1)) 425109998Smarkm 426296341Sdelphij# define BN_one(a) (BN_set_word((a),1)) 427296341Sdelphij# define BN_zero_ex(a) \ 428296341Sdelphij do { \ 429296341Sdelphij BIGNUM *_tmp_bn = (a); \ 430296341Sdelphij _tmp_bn->top = 0; \ 431296341Sdelphij _tmp_bn->neg = 0; \ 432296341Sdelphij } while(0) 433296341Sdelphij# ifdef OPENSSL_NO_DEPRECATED 434296341Sdelphij# define BN_zero(a) BN_zero_ex(a) 435296341Sdelphij# else 436296341Sdelphij# define BN_zero(a) (BN_set_word((a),0)) 437296341Sdelphij# endif 43855714Skris 439109998Smarkmconst BIGNUM *BN_value_one(void); 440296341Sdelphijchar *BN_options(void); 44155714SkrisBN_CTX *BN_CTX_new(void); 442296341Sdelphij# ifndef OPENSSL_NO_DEPRECATED 443296341Sdelphijvoid BN_CTX_init(BN_CTX *c); 444296341Sdelphij# endif 445296341Sdelphijvoid BN_CTX_free(BN_CTX *c); 446296341Sdelphijvoid BN_CTX_start(BN_CTX *ctx); 44759191SkrisBIGNUM *BN_CTX_get(BN_CTX *ctx); 448296341Sdelphijvoid BN_CTX_end(BN_CTX *ctx); 449296341Sdelphijint BN_rand(BIGNUM *rnd, int bits, int top, int bottom); 450296341Sdelphijint BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom); 451296341Sdelphijint BN_rand_range(BIGNUM *rnd, const BIGNUM *range); 452296341Sdelphijint BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range); 453296341Sdelphijint BN_num_bits(const BIGNUM *a); 454296341Sdelphijint BN_num_bits_word(BN_ULONG); 45555714SkrisBIGNUM *BN_new(void); 456296341Sdelphijvoid BN_init(BIGNUM *); 457296341Sdelphijvoid BN_clear_free(BIGNUM *a); 45855714SkrisBIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b); 459296341Sdelphijvoid BN_swap(BIGNUM *a, BIGNUM *b); 460296341SdelphijBIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret); 461296341Sdelphijint BN_bn2bin(const BIGNUM *a, unsigned char *to); 462296341SdelphijBIGNUM *BN_mpi2bn(const unsigned char *s, int len, BIGNUM *ret); 463296341Sdelphijint BN_bn2mpi(const BIGNUM *a, unsigned char *to); 464296341Sdelphijint BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 465296341Sdelphijint BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 466296341Sdelphijint BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 467296341Sdelphijint BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 468296341Sdelphijint BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 469296341Sdelphijint BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx); 470160814Ssimon/** BN_set_negative sets sign of a BIGNUM 471160814Ssimon * \param b pointer to the BIGNUM object 472296341Sdelphij * \param n 0 if the BIGNUM b should be positive and a value != 0 otherwise 473160814Ssimon */ 474296341Sdelphijvoid BN_set_negative(BIGNUM *b, int n); 475160814Ssimon/** BN_is_negative returns 1 if the BIGNUM is negative 476160814Ssimon * \param a pointer to the BIGNUM object 477160814Ssimon * \return 1 if a < 0 and 0 otherwise 478160814Ssimon */ 479296341Sdelphij# define BN_is_negative(a) ((a)->neg != 0) 480109998Smarkm 481296341Sdelphijint BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, 482296341Sdelphij BN_CTX *ctx); 483296341Sdelphij# define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx)) 484296341Sdelphijint BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx); 485296341Sdelphijint BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, 486296341Sdelphij BN_CTX *ctx); 487296341Sdelphijint BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 488296341Sdelphij const BIGNUM *m); 489296341Sdelphijint BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, 490296341Sdelphij BN_CTX *ctx); 491296341Sdelphijint BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 492296341Sdelphij const BIGNUM *m); 493296341Sdelphijint BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, 494296341Sdelphij BN_CTX *ctx); 495296341Sdelphijint BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 496296341Sdelphijint BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 497296341Sdelphijint BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m); 498296341Sdelphijint BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, 499296341Sdelphij BN_CTX *ctx); 500296341Sdelphijint BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m); 501109998Smarkm 50259191SkrisBN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w); 50355714SkrisBN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w); 504296341Sdelphijint BN_mul_word(BIGNUM *a, BN_ULONG w); 505296341Sdelphijint BN_add_word(BIGNUM *a, BN_ULONG w); 506296341Sdelphijint BN_sub_word(BIGNUM *a, BN_ULONG w); 507296341Sdelphijint BN_set_word(BIGNUM *a, BN_ULONG w); 508109998SmarkmBN_ULONG BN_get_word(const BIGNUM *a); 509109998Smarkm 510296341Sdelphijint BN_cmp(const BIGNUM *a, const BIGNUM *b); 511296341Sdelphijvoid BN_free(BIGNUM *a); 512296341Sdelphijint BN_is_bit_set(const BIGNUM *a, int n); 513296341Sdelphijint BN_lshift(BIGNUM *r, const BIGNUM *a, int n); 514296341Sdelphijint BN_lshift1(BIGNUM *r, const BIGNUM *a); 515296341Sdelphijint BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 516109998Smarkm 517296341Sdelphijint BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 518296341Sdelphij const BIGNUM *m, BN_CTX *ctx); 519296341Sdelphijint BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 520296341Sdelphij const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 521160814Ssimonint BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, 522296341Sdelphij const BIGNUM *m, BN_CTX *ctx, 523296341Sdelphij BN_MONT_CTX *in_mont); 524296341Sdelphijint BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p, 525296341Sdelphij const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 526296341Sdelphijint BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, const BIGNUM *p1, 527296341Sdelphij const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m, 528296341Sdelphij BN_CTX *ctx, BN_MONT_CTX *m_ctx); 529296341Sdelphijint BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 530296341Sdelphij const BIGNUM *m, BN_CTX *ctx); 531109998Smarkm 532296341Sdelphijint BN_mask_bits(BIGNUM *a, int n); 533296341Sdelphij# ifndef OPENSSL_NO_FP_API 534296341Sdelphijint BN_print_fp(FILE *fp, const BIGNUM *a); 535296341Sdelphij# endif 536296341Sdelphij# ifdef HEADER_BIO_H 537296341Sdelphijint BN_print(BIO *fp, const BIGNUM *a); 538296341Sdelphij# else 539296341Sdelphijint BN_print(void *fp, const BIGNUM *a); 540296341Sdelphij# endif 541296341Sdelphijint BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx); 542296341Sdelphijint BN_rshift(BIGNUM *r, const BIGNUM *a, int n); 543296341Sdelphijint BN_rshift1(BIGNUM *r, const BIGNUM *a); 544296341Sdelphijvoid BN_clear(BIGNUM *a); 54555714SkrisBIGNUM *BN_dup(const BIGNUM *a); 546296341Sdelphijint BN_ucmp(const BIGNUM *a, const BIGNUM *b); 547296341Sdelphijint BN_set_bit(BIGNUM *a, int n); 548296341Sdelphijint BN_clear_bit(BIGNUM *a, int n); 549296341Sdelphijchar *BN_bn2hex(const BIGNUM *a); 550296341Sdelphijchar *BN_bn2dec(const BIGNUM *a); 551296341Sdelphijint BN_hex2bn(BIGNUM **a, const char *str); 552296341Sdelphijint BN_dec2bn(BIGNUM **a, const char *str); 553296341Sdelphijint BN_asc2bn(BIGNUM **a, const char *str); 554296341Sdelphijint BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 555296341Sdelphijint BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); /* returns 556296341Sdelphij * -2 for 557296341Sdelphij * error */ 558109998SmarkmBIGNUM *BN_mod_inverse(BIGNUM *ret, 559296341Sdelphij const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); 560109998SmarkmBIGNUM *BN_mod_sqrt(BIGNUM *ret, 561296341Sdelphij const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); 562160814Ssimon 563296341Sdelphijvoid BN_consttime_swap(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords); 564264266Sdelphij 565160814Ssimon/* Deprecated versions */ 566296341Sdelphij# ifndef OPENSSL_NO_DEPRECATED 567296341SdelphijBIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe, 568296341Sdelphij const BIGNUM *add, const BIGNUM *rem, 569296341Sdelphij void (*callback) (int, int, void *), void *cb_arg); 570296341Sdelphijint BN_is_prime(const BIGNUM *p, int nchecks, 571296341Sdelphij void (*callback) (int, int, void *), 572296341Sdelphij BN_CTX *ctx, void *cb_arg); 573296341Sdelphijint BN_is_prime_fasttest(const BIGNUM *p, int nchecks, 574296341Sdelphij void (*callback) (int, int, void *), BN_CTX *ctx, 575296341Sdelphij void *cb_arg, int do_trial_division); 576296341Sdelphij# endif /* !defined(OPENSSL_NO_DEPRECATED) */ 57755714Skris 578160814Ssimon/* Newer versions */ 579296341Sdelphijint BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add, 580296341Sdelphij const BIGNUM *rem, BN_GENCB *cb); 581296341Sdelphijint BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb); 582296341Sdelphijint BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, 583296341Sdelphij int do_trial_division, BN_GENCB *cb); 584160814Ssimon 585194206Ssimonint BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx); 586194206Ssimon 587194206Ssimonint BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, 588296341Sdelphij const BIGNUM *Xp, const BIGNUM *Xp1, 589296341Sdelphij const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx, 590296341Sdelphij BN_GENCB *cb); 591296341Sdelphijint BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, BIGNUM *Xp1, 592296341Sdelphij BIGNUM *Xp2, const BIGNUM *Xp, const BIGNUM *e, 593296341Sdelphij BN_CTX *ctx, BN_GENCB *cb); 594194206Ssimon 595296341SdelphijBN_MONT_CTX *BN_MONT_CTX_new(void); 59655714Skrisvoid BN_MONT_CTX_init(BN_MONT_CTX *ctx); 597296341Sdelphijint BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 598296341Sdelphij BN_MONT_CTX *mont, BN_CTX *ctx); 599296341Sdelphij# define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\ 600296341Sdelphij (r),(a),&((mont)->RR),(mont),(ctx)) 601296341Sdelphijint BN_from_montgomery(BIGNUM *r, const BIGNUM *a, 602296341Sdelphij BN_MONT_CTX *mont, BN_CTX *ctx); 60355714Skrisvoid BN_MONT_CTX_free(BN_MONT_CTX *mont); 604296341Sdelphijint BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx); 605296341SdelphijBN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from); 606160814SsimonBN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock, 607296341Sdelphij const BIGNUM *mod, BN_CTX *ctx); 60855714Skris 609160814Ssimon/* BN_BLINDING flags */ 610296341Sdelphij# define BN_BLINDING_NO_UPDATE 0x00000001 611296341Sdelphij# define BN_BLINDING_NO_RECREATE 0x00000002 612160814Ssimon 613238405SjkimBN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod); 61455714Skrisvoid BN_BLINDING_free(BN_BLINDING *b); 615296341Sdelphijint BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx); 616160814Ssimonint BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); 61755714Skrisint BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); 618160814Ssimonint BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *); 619296341Sdelphijint BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, 620296341Sdelphij BN_CTX *); 621296341Sdelphij# ifndef OPENSSL_NO_DEPRECATED 622160814Ssimonunsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *); 623160814Ssimonvoid BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long); 624296341Sdelphij# endif 625238405SjkimCRYPTO_THREADID *BN_BLINDING_thread_id(BN_BLINDING *); 626160814Ssimonunsigned long BN_BLINDING_get_flags(const BN_BLINDING *); 627160814Ssimonvoid BN_BLINDING_set_flags(BN_BLINDING *, unsigned long); 628160814SsimonBN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b, 629296341Sdelphij const BIGNUM *e, BIGNUM *m, BN_CTX *ctx, 630296341Sdelphij int (*bn_mod_exp) (BIGNUM *r, 631296341Sdelphij const BIGNUM *a, 632296341Sdelphij const BIGNUM *p, 633296341Sdelphij const BIGNUM *m, 634296341Sdelphij BN_CTX *ctx, 635296341Sdelphij BN_MONT_CTX *m_ctx), 636296341Sdelphij BN_MONT_CTX *m_ctx); 63755714Skris 638296341Sdelphij# ifndef OPENSSL_NO_DEPRECATED 639296341Sdelphijvoid BN_set_params(int mul, int high, int low, int mont); 640296341Sdelphijint BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */ 641296341Sdelphij# endif 64255714Skris 643296341Sdelphijvoid BN_RECP_CTX_init(BN_RECP_CTX *recp); 64455714SkrisBN_RECP_CTX *BN_RECP_CTX_new(void); 645296341Sdelphijvoid BN_RECP_CTX_free(BN_RECP_CTX *recp); 646296341Sdelphijint BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *rdiv, BN_CTX *ctx); 647296341Sdelphijint BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, 648296341Sdelphij BN_RECP_CTX *recp, BN_CTX *ctx); 649296341Sdelphijint BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 650296341Sdelphij const BIGNUM *m, BN_CTX *ctx); 651296341Sdelphijint BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, 652296341Sdelphij BN_RECP_CTX *recp, BN_CTX *ctx); 65355714Skris 654296341Sdelphij# ifndef OPENSSL_NO_EC2M 655238405Sjkim 656296341Sdelphij/* 657296341Sdelphij * Functions for arithmetic over binary polynomials represented by BIGNUMs. 658160814Ssimon * The BIGNUM::neg property of BIGNUMs representing binary polynomials is 659296341Sdelphij * ignored. Note that input arguments are not const so that their bit arrays 660296341Sdelphij * can be expanded to the appropriate size if needed. 661160814Ssimon */ 662160814Ssimon 663296341Sdelphij/* 664296341Sdelphij * r = a + b 665296341Sdelphij */ 666296341Sdelphijint BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 667296341Sdelphij# define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b) 668296341Sdelphij/* 669296341Sdelphij * r=a mod p 670296341Sdelphij */ 671296341Sdelphijint BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p); 672296341Sdelphij/* r = (a * b) mod p */ 673296341Sdelphijint BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 674296341Sdelphij const BIGNUM *p, BN_CTX *ctx); 675296341Sdelphij/* r = (a * a) mod p */ 676296341Sdelphijint BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 677296341Sdelphij/* r = (1 / b) mod p */ 678296341Sdelphijint BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx); 679296341Sdelphij/* r = (a / b) mod p */ 680296341Sdelphijint BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 681296341Sdelphij const BIGNUM *p, BN_CTX *ctx); 682296341Sdelphij/* r = (a ^ b) mod p */ 683296341Sdelphijint BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 684296341Sdelphij const BIGNUM *p, BN_CTX *ctx); 685296341Sdelphij/* r = sqrt(a) mod p */ 686296341Sdelphijint BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 687296341Sdelphij BN_CTX *ctx); 688296341Sdelphij/* r^2 + r = a mod p */ 689296341Sdelphijint BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 690296341Sdelphij BN_CTX *ctx); 691296341Sdelphij# define BN_GF2m_cmp(a, b) BN_ucmp((a), (b)) 692296341Sdelphij/*- 693296341Sdelphij * Some functions allow for representation of the irreducible polynomials 694160814Ssimon * as an unsigned int[], say p. The irreducible f(t) is then of the form: 695160814Ssimon * t^p[0] + t^p[1] + ... + t^p[k] 696160814Ssimon * where m = p[0] > p[1] > ... > p[k] = 0. 697160814Ssimon */ 698296341Sdelphij/* r = a mod p */ 699296341Sdelphijint BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[]); 700296341Sdelphij/* r = (a * b) mod p */ 701296341Sdelphijint BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 702296341Sdelphij const int p[], BN_CTX *ctx); 703296341Sdelphij/* r = (a * a) mod p */ 704296341Sdelphijint BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], 705296341Sdelphij BN_CTX *ctx); 706296341Sdelphij/* r = (1 / b) mod p */ 707296341Sdelphijint BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const int p[], 708296341Sdelphij BN_CTX *ctx); 709296341Sdelphij/* r = (a / b) mod p */ 710296341Sdelphijint BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 711296341Sdelphij const int p[], BN_CTX *ctx); 712296341Sdelphij/* r = (a ^ b) mod p */ 713296341Sdelphijint BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 714296341Sdelphij const int p[], BN_CTX *ctx); 715296341Sdelphij/* r = sqrt(a) mod p */ 716296341Sdelphijint BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, 717296341Sdelphij const int p[], BN_CTX *ctx); 718296341Sdelphij/* r^2 + r = a mod p */ 719296341Sdelphijint BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a, 720296341Sdelphij const int p[], BN_CTX *ctx); 721296341Sdelphijint BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max); 722296341Sdelphijint BN_GF2m_arr2poly(const int p[], BIGNUM *a); 723160814Ssimon 724296341Sdelphij# endif 725238405Sjkim 726296341Sdelphij/* 727296341Sdelphij * faster mod functions for the 'NIST primes' 0 <= a < p^2 728296341Sdelphij */ 729160814Ssimonint BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 730160814Ssimonint BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 731160814Ssimonint BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 732160814Ssimonint BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 733160814Ssimonint BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 734160814Ssimon 735160814Ssimonconst BIGNUM *BN_get0_nist_prime_192(void); 736160814Ssimonconst BIGNUM *BN_get0_nist_prime_224(void); 737160814Ssimonconst BIGNUM *BN_get0_nist_prime_256(void); 738160814Ssimonconst BIGNUM *BN_get0_nist_prime_384(void); 739160814Ssimonconst BIGNUM *BN_get0_nist_prime_521(void); 740160814Ssimon 74159191Skris/* library internal functions */ 74255714Skris 743296341Sdelphij# define bn_expand(a,bits) \ 744296341Sdelphij ( \ 745296341Sdelphij bits > (INT_MAX - BN_BITS2 + 1) ? \ 746296341Sdelphij NULL \ 747296341Sdelphij : \ 748296341Sdelphij (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax) ? \ 749296341Sdelphij (a) \ 750296341Sdelphij : \ 751296341Sdelphij bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2) \ 752296341Sdelphij ) 753296341Sdelphij 754296341Sdelphij# define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words))) 75559191SkrisBIGNUM *bn_expand2(BIGNUM *a, int words); 756296341Sdelphij# ifndef OPENSSL_NO_DEPRECATED 757160814SsimonBIGNUM *bn_dup_expand(const BIGNUM *a, int words); /* unused */ 758296341Sdelphij# endif 75959191Skris 760296341Sdelphij/*- 761296341Sdelphij * Bignum consistency macros 762160814Ssimon * There is one "API" macro, bn_fix_top(), for stripping leading zeroes from 763160814Ssimon * bignum data after direct manipulations on the data. There is also an 764160814Ssimon * "internal" macro, bn_check_top(), for verifying that there are no leading 765160814Ssimon * zeroes. Unfortunately, some auditing is required due to the fact that 766160814Ssimon * bn_fix_top() has become an overabused duct-tape because bignum data is 767160814Ssimon * occasionally passed around in an inconsistent state. So the following 768160814Ssimon * changes have been made to sort this out; 769160814Ssimon * - bn_fix_top()s implementation has been moved to bn_correct_top() 770160814Ssimon * - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and 771160814Ssimon * bn_check_top() is as before. 772160814Ssimon * - if BN_DEBUG *is* defined; 773160814Ssimon * - bn_check_top() tries to pollute unused words even if the bignum 'top' is 774160814Ssimon * consistent. (ed: only if BN_DEBUG_RAND is defined) 775160814Ssimon * - bn_fix_top() maps to bn_check_top() rather than "fixing" anything. 776160814Ssimon * The idea is to have debug builds flag up inconsistent bignums when they 777160814Ssimon * occur. If that occurs in a bn_fix_top(), we examine the code in question; if 778160814Ssimon * the use of bn_fix_top() was appropriate (ie. it follows directly after code 779160814Ssimon * that manipulates the bignum) it is converted to bn_correct_top(), and if it 780160814Ssimon * was not appropriate, we convert it permanently to bn_check_top() and track 781160814Ssimon * down the cause of the bug. Eventually, no internal code should be using the 782160814Ssimon * bn_fix_top() macro. External applications and libraries should try this with 783160814Ssimon * their own code too, both in terms of building against the openssl headers 784160814Ssimon * with BN_DEBUG defined *and* linking with a version of OpenSSL built with it 785160814Ssimon * defined. This not only improves external code, it provides more test 786160814Ssimon * coverage for openssl's own code. 787160814Ssimon */ 788160814Ssimon 789296341Sdelphij# ifdef BN_DEBUG 790160814Ssimon 791160814Ssimon/* We only need assert() when debugging */ 792296341Sdelphij# include <assert.h> 793160814Ssimon 794296341Sdelphij# ifdef BN_DEBUG_RAND 795160814Ssimon/* To avoid "make update" cvs wars due to BN_DEBUG, use some tricks */ 796296341Sdelphij# ifndef RAND_pseudo_bytes 797296341Sdelphijint RAND_pseudo_bytes(unsigned char *buf, int num); 798296341Sdelphij# define BN_DEBUG_TRIX 799296341Sdelphij# endif 800296341Sdelphij# define bn_pollute(a) \ 801296341Sdelphij do { \ 802296341Sdelphij const BIGNUM *_bnum1 = (a); \ 803296341Sdelphij if(_bnum1->top < _bnum1->dmax) { \ 804296341Sdelphij unsigned char _tmp_char; \ 805296341Sdelphij /* We cast away const without the compiler knowing, any \ 806296341Sdelphij * *genuinely* constant variables that aren't mutable \ 807296341Sdelphij * wouldn't be constructed with top!=dmax. */ \ 808296341Sdelphij BN_ULONG *_not_const; \ 809296341Sdelphij memcpy(&_not_const, &_bnum1->d, sizeof(BN_ULONG*)); \ 810296341Sdelphij /* Debug only - safe to ignore error return */ \ 811296341Sdelphij RAND_pseudo_bytes(&_tmp_char, 1); \ 812296341Sdelphij memset((unsigned char *)(_not_const + _bnum1->top), _tmp_char, \ 813296341Sdelphij (_bnum1->dmax - _bnum1->top) * sizeof(BN_ULONG)); \ 814296341Sdelphij } \ 815296341Sdelphij } while(0) 816296341Sdelphij# ifdef BN_DEBUG_TRIX 817296341Sdelphij# undef RAND_pseudo_bytes 818296341Sdelphij# endif 819296341Sdelphij# else 820296341Sdelphij# define bn_pollute(a) 821296341Sdelphij# endif 822296341Sdelphij# define bn_check_top(a) \ 823296341Sdelphij do { \ 824296341Sdelphij const BIGNUM *_bnum2 = (a); \ 825296341Sdelphij if (_bnum2 != NULL) { \ 826296341Sdelphij assert((_bnum2->top == 0) || \ 827296341Sdelphij (_bnum2->d[_bnum2->top - 1] != 0)); \ 828296341Sdelphij bn_pollute(_bnum2); \ 829296341Sdelphij } \ 830296341Sdelphij } while(0) 831160814Ssimon 832296341Sdelphij# define bn_fix_top(a) bn_check_top(a) 833160814Ssimon 834296341Sdelphij# define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2) 835296341Sdelphij# define bn_wcheck_size(bn, words) \ 836296341Sdelphij do { \ 837296341Sdelphij const BIGNUM *_bnum2 = (bn); \ 838296341Sdelphij assert((words) <= (_bnum2)->dmax && (words) >= (_bnum2)->top); \ 839296341Sdelphij /* avoid unused variable warning with NDEBUG */ \ 840296341Sdelphij (void)(_bnum2); \ 841296341Sdelphij } while(0) 842264266Sdelphij 843296341Sdelphij# else /* !BN_DEBUG */ 844160814Ssimon 845296341Sdelphij# define bn_pollute(a) 846296341Sdelphij# define bn_check_top(a) 847296341Sdelphij# define bn_fix_top(a) bn_correct_top(a) 848296341Sdelphij# define bn_check_size(bn, bits) 849296341Sdelphij# define bn_wcheck_size(bn, words) 850160814Ssimon 851296341Sdelphij# endif 852160814Ssimon 853296341Sdelphij# define bn_correct_top(a) \ 85459191Skris { \ 85559191Skris BN_ULONG *ftl; \ 856296341Sdelphij int tmp_top = (a)->top; \ 857296341Sdelphij if (tmp_top > 0) \ 858296341Sdelphij { \ 859296341Sdelphij for (ftl= &((a)->d[tmp_top-1]); tmp_top > 0; tmp_top--) \ 860296341Sdelphij if (*(ftl--)) break; \ 861296341Sdelphij (a)->top = tmp_top; \ 862296341Sdelphij } \ 863296341Sdelphij bn_pollute(a); \ 864296341Sdelphij } 86559191Skris 866296341SdelphijBN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, 867296341Sdelphij BN_ULONG w); 868109998SmarkmBN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); 869296341Sdelphijvoid bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num); 87059191SkrisBN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d); 871296341SdelphijBN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 872296341Sdelphij int num); 873296341SdelphijBN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 874296341Sdelphij int num); 87559191Skris 876160814Ssimon/* Primes from RFC 2409 */ 877160814SsimonBIGNUM *get_rfc2409_prime_768(BIGNUM *bn); 878160814SsimonBIGNUM *get_rfc2409_prime_1024(BIGNUM *bn); 87959191Skris 880160814Ssimon/* Primes from RFC 3526 */ 881160814SsimonBIGNUM *get_rfc3526_prime_1536(BIGNUM *bn); 882160814SsimonBIGNUM *get_rfc3526_prime_2048(BIGNUM *bn); 883160814SsimonBIGNUM *get_rfc3526_prime_3072(BIGNUM *bn); 884160814SsimonBIGNUM *get_rfc3526_prime_4096(BIGNUM *bn); 885160814SsimonBIGNUM *get_rfc3526_prime_6144(BIGNUM *bn); 886160814SsimonBIGNUM *get_rfc3526_prime_8192(BIGNUM *bn); 887160814Ssimon 888296341Sdelphijint BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom); 88976866Skris 89055714Skris/* BEGIN ERROR CODES */ 891296341Sdelphij/* 892296341Sdelphij * The following lines are auto generated by the script mkerr.pl. Any changes 89355714Skris * made after this point may be overwritten when the script is next run. 89455714Skris */ 89589837Skrisvoid ERR_load_BN_strings(void); 89655714Skris 89755714Skris/* Error codes for the BN functions. */ 89855714Skris 89955714Skris/* Function codes. */ 900296341Sdelphij# define BN_F_BNRAND 127 901296341Sdelphij# define BN_F_BN_BLINDING_CONVERT_EX 100 902296341Sdelphij# define BN_F_BN_BLINDING_CREATE_PARAM 128 903296341Sdelphij# define BN_F_BN_BLINDING_INVERT_EX 101 904296341Sdelphij# define BN_F_BN_BLINDING_NEW 102 905296341Sdelphij# define BN_F_BN_BLINDING_UPDATE 103 906296341Sdelphij# define BN_F_BN_BN2DEC 104 907296341Sdelphij# define BN_F_BN_BN2HEX 105 908296341Sdelphij# define BN_F_BN_CTX_GET 116 909296341Sdelphij# define BN_F_BN_CTX_NEW 106 910296341Sdelphij# define BN_F_BN_CTX_START 129 911296341Sdelphij# define BN_F_BN_DIV 107 912296341Sdelphij# define BN_F_BN_DIV_NO_BRANCH 138 913296341Sdelphij# define BN_F_BN_DIV_RECP 130 914296341Sdelphij# define BN_F_BN_EXP 123 915296341Sdelphij# define BN_F_BN_EXPAND2 108 916296341Sdelphij# define BN_F_BN_EXPAND_INTERNAL 120 917296341Sdelphij# define BN_F_BN_GF2M_MOD 131 918296341Sdelphij# define BN_F_BN_GF2M_MOD_EXP 132 919296341Sdelphij# define BN_F_BN_GF2M_MOD_MUL 133 920296341Sdelphij# define BN_F_BN_GF2M_MOD_SOLVE_QUAD 134 921296341Sdelphij# define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR 135 922296341Sdelphij# define BN_F_BN_GF2M_MOD_SQR 136 923296341Sdelphij# define BN_F_BN_GF2M_MOD_SQRT 137 924296341Sdelphij# define BN_F_BN_LSHIFT 145 925296341Sdelphij# define BN_F_BN_MOD_EXP2_MONT 118 926296341Sdelphij# define BN_F_BN_MOD_EXP_MONT 109 927296341Sdelphij# define BN_F_BN_MOD_EXP_MONT_CONSTTIME 124 928296341Sdelphij# define BN_F_BN_MOD_EXP_MONT_WORD 117 929296341Sdelphij# define BN_F_BN_MOD_EXP_RECP 125 930296341Sdelphij# define BN_F_BN_MOD_EXP_SIMPLE 126 931296341Sdelphij# define BN_F_BN_MOD_INVERSE 110 932296341Sdelphij# define BN_F_BN_MOD_INVERSE_NO_BRANCH 139 933296341Sdelphij# define BN_F_BN_MOD_LSHIFT_QUICK 119 934296341Sdelphij# define BN_F_BN_MOD_MUL_RECIPROCAL 111 935296341Sdelphij# define BN_F_BN_MOD_SQRT 121 936296341Sdelphij# define BN_F_BN_MPI2BN 112 937296341Sdelphij# define BN_F_BN_NEW 113 938296341Sdelphij# define BN_F_BN_RAND 114 939296341Sdelphij# define BN_F_BN_RAND_RANGE 122 940296341Sdelphij# define BN_F_BN_RSHIFT 146 941296341Sdelphij# define BN_F_BN_USUB 115 94255714Skris 94355714Skris/* Reason codes. */ 944296341Sdelphij# define BN_R_ARG2_LT_ARG3 100 945296341Sdelphij# define BN_R_BAD_RECIPROCAL 101 946296341Sdelphij# define BN_R_BIGNUM_TOO_LONG 114 947296341Sdelphij# define BN_R_BITS_TOO_SMALL 118 948296341Sdelphij# define BN_R_CALLED_WITH_EVEN_MODULUS 102 949296341Sdelphij# define BN_R_DIV_BY_ZERO 103 950296341Sdelphij# define BN_R_ENCODING_ERROR 104 951296341Sdelphij# define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105 952296341Sdelphij# define BN_R_INPUT_NOT_REDUCED 110 953296341Sdelphij# define BN_R_INVALID_LENGTH 106 954296341Sdelphij# define BN_R_INVALID_RANGE 115 955296341Sdelphij# define BN_R_INVALID_SHIFT 119 956296341Sdelphij# define BN_R_NOT_A_SQUARE 111 957296341Sdelphij# define BN_R_NOT_INITIALIZED 107 958296341Sdelphij# define BN_R_NO_INVERSE 108 959296341Sdelphij# define BN_R_NO_SOLUTION 116 960296341Sdelphij# define BN_R_P_IS_NOT_PRIME 112 961296341Sdelphij# define BN_R_TOO_MANY_ITERATIONS 113 962296341Sdelphij# define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109 96355714Skris 96455714Skris#ifdef __cplusplus 96555714Skris} 96655714Skris#endif 96755714Skris#endif 968