1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved 4e1051a39Sopenharmony_ci * 5e1051a39Sopenharmony_ci * Licensed under the OpenSSL license (the "License"). You may not use 6e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 7e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 8e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 9e1051a39Sopenharmony_ci */ 10e1051a39Sopenharmony_ci 11e1051a39Sopenharmony_ci#ifndef HEADER_BN_H 12e1051a39Sopenharmony_ci# define HEADER_BN_H 13e1051a39Sopenharmony_ci 14e1051a39Sopenharmony_ci# include <openssl/e_os2.h> 15e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_STDIO 16e1051a39Sopenharmony_ci# include <stdio.h> 17e1051a39Sopenharmony_ci# endif 18e1051a39Sopenharmony_ci# include <openssl/opensslconf.h> 19e1051a39Sopenharmony_ci# include <openssl/ossl_typ.h> 20e1051a39Sopenharmony_ci# include <openssl/crypto.h> 21e1051a39Sopenharmony_ci# include <openssl/bnerr.h> 22e1051a39Sopenharmony_ci 23e1051a39Sopenharmony_ci#ifdef __cplusplus 24e1051a39Sopenharmony_ciextern "C" { 25e1051a39Sopenharmony_ci#endif 26e1051a39Sopenharmony_ci 27e1051a39Sopenharmony_ci/* 28e1051a39Sopenharmony_ci * 64-bit processor with LP64 ABI 29e1051a39Sopenharmony_ci */ 30e1051a39Sopenharmony_ci# ifdef SIXTY_FOUR_BIT_LONG 31e1051a39Sopenharmony_ci# define BN_ULONG unsigned long 32e1051a39Sopenharmony_ci# define BN_BYTES 8 33e1051a39Sopenharmony_ci# endif 34e1051a39Sopenharmony_ci 35e1051a39Sopenharmony_ci/* 36e1051a39Sopenharmony_ci * 64-bit processor other than LP64 ABI 37e1051a39Sopenharmony_ci */ 38e1051a39Sopenharmony_ci# ifdef SIXTY_FOUR_BIT 39e1051a39Sopenharmony_ci# define BN_ULONG unsigned long long 40e1051a39Sopenharmony_ci# define BN_BYTES 8 41e1051a39Sopenharmony_ci# endif 42e1051a39Sopenharmony_ci 43e1051a39Sopenharmony_ci# ifdef THIRTY_TWO_BIT 44e1051a39Sopenharmony_ci# define BN_ULONG unsigned int 45e1051a39Sopenharmony_ci# define BN_BYTES 4 46e1051a39Sopenharmony_ci# endif 47e1051a39Sopenharmony_ci 48e1051a39Sopenharmony_ci# define BN_BITS2 (BN_BYTES * 8) 49e1051a39Sopenharmony_ci# define BN_BITS (BN_BITS2 * 2) 50e1051a39Sopenharmony_ci# define BN_TBIT ((BN_ULONG)1 << (BN_BITS2 - 1)) 51e1051a39Sopenharmony_ci 52e1051a39Sopenharmony_ci# define BN_FLG_MALLOCED 0x01 53e1051a39Sopenharmony_ci# define BN_FLG_STATIC_DATA 0x02 54e1051a39Sopenharmony_ci 55e1051a39Sopenharmony_ci/* 56e1051a39Sopenharmony_ci * avoid leaking exponent information through timing, 57e1051a39Sopenharmony_ci * BN_mod_exp_mont() will call BN_mod_exp_mont_consttime, 58e1051a39Sopenharmony_ci * BN_div() will call BN_div_no_branch, 59e1051a39Sopenharmony_ci * BN_mod_inverse() will call bn_mod_inverse_no_branch. 60e1051a39Sopenharmony_ci */ 61e1051a39Sopenharmony_ci# define BN_FLG_CONSTTIME 0x04 62e1051a39Sopenharmony_ci# define BN_FLG_SECURE 0x08 63e1051a39Sopenharmony_ci 64e1051a39Sopenharmony_ci# if OPENSSL_API_COMPAT < 0x00908000L 65e1051a39Sopenharmony_ci/* deprecated name for the flag */ 66e1051a39Sopenharmony_ci# define BN_FLG_EXP_CONSTTIME BN_FLG_CONSTTIME 67e1051a39Sopenharmony_ci# define BN_FLG_FREE 0x8000 /* used for debugging */ 68e1051a39Sopenharmony_ci# endif 69e1051a39Sopenharmony_ci 70e1051a39Sopenharmony_civoid BN_set_flags(BIGNUM *b, int n); 71e1051a39Sopenharmony_ciint BN_get_flags(const BIGNUM *b, int n); 72e1051a39Sopenharmony_ci 73e1051a39Sopenharmony_ci/* Values for |top| in BN_rand() */ 74e1051a39Sopenharmony_ci#define BN_RAND_TOP_ANY -1 75e1051a39Sopenharmony_ci#define BN_RAND_TOP_ONE 0 76e1051a39Sopenharmony_ci#define BN_RAND_TOP_TWO 1 77e1051a39Sopenharmony_ci 78e1051a39Sopenharmony_ci/* Values for |bottom| in BN_rand() */ 79e1051a39Sopenharmony_ci#define BN_RAND_BOTTOM_ANY 0 80e1051a39Sopenharmony_ci#define BN_RAND_BOTTOM_ODD 1 81e1051a39Sopenharmony_ci 82e1051a39Sopenharmony_ci/* 83e1051a39Sopenharmony_ci * get a clone of a BIGNUM with changed flags, for *temporary* use only (the 84e1051a39Sopenharmony_ci * two BIGNUMs cannot be used in parallel!). Also only for *read only* use. The 85e1051a39Sopenharmony_ci * value |dest| should be a newly allocated BIGNUM obtained via BN_new() that 86e1051a39Sopenharmony_ci * has not been otherwise initialised or used. 87e1051a39Sopenharmony_ci */ 88e1051a39Sopenharmony_civoid BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags); 89e1051a39Sopenharmony_ci 90e1051a39Sopenharmony_ci/* Wrapper function to make using BN_GENCB easier */ 91e1051a39Sopenharmony_ciint BN_GENCB_call(BN_GENCB *cb, int a, int b); 92e1051a39Sopenharmony_ci 93e1051a39Sopenharmony_ciBN_GENCB *BN_GENCB_new(void); 94e1051a39Sopenharmony_civoid BN_GENCB_free(BN_GENCB *cb); 95e1051a39Sopenharmony_ci 96e1051a39Sopenharmony_ci/* Populate a BN_GENCB structure with an "old"-style callback */ 97e1051a39Sopenharmony_civoid BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *), 98e1051a39Sopenharmony_ci void *cb_arg); 99e1051a39Sopenharmony_ci 100e1051a39Sopenharmony_ci/* Populate a BN_GENCB structure with a "new"-style callback */ 101e1051a39Sopenharmony_civoid BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *), 102e1051a39Sopenharmony_ci void *cb_arg); 103e1051a39Sopenharmony_ci 104e1051a39Sopenharmony_civoid *BN_GENCB_get_arg(BN_GENCB *cb); 105e1051a39Sopenharmony_ci 106e1051a39Sopenharmony_ci# define BN_prime_checks 0 /* default: select number of iterations based 107e1051a39Sopenharmony_ci * on the size of the number */ 108e1051a39Sopenharmony_ci 109e1051a39Sopenharmony_ci/* 110e1051a39Sopenharmony_ci * BN_prime_checks_for_size() returns the number of Miller-Rabin iterations 111e1051a39Sopenharmony_ci * that will be done for checking that a random number is probably prime. The 112e1051a39Sopenharmony_ci * error rate for accepting a composite number as prime depends on the size of 113e1051a39Sopenharmony_ci * the prime |b|. The error rates used are for calculating an RSA key with 2 primes, 114e1051a39Sopenharmony_ci * and so the level is what you would expect for a key of double the size of the 115e1051a39Sopenharmony_ci * prime. 116e1051a39Sopenharmony_ci * 117e1051a39Sopenharmony_ci * This table is generated using the algorithm of FIPS PUB 186-4 118e1051a39Sopenharmony_ci * Digital Signature Standard (DSS), section F.1, page 117. 119e1051a39Sopenharmony_ci * (https://dx.doi.org/10.6028/NIST.FIPS.186-4) 120e1051a39Sopenharmony_ci * 121e1051a39Sopenharmony_ci * The following magma script was used to generate the output: 122e1051a39Sopenharmony_ci * securitybits:=125; 123e1051a39Sopenharmony_ci * k:=1024; 124e1051a39Sopenharmony_ci * for t:=1 to 65 do 125e1051a39Sopenharmony_ci * for M:=3 to Floor(2*Sqrt(k-1)-1) do 126e1051a39Sopenharmony_ci * S:=0; 127e1051a39Sopenharmony_ci * // Sum over m 128e1051a39Sopenharmony_ci * for m:=3 to M do 129e1051a39Sopenharmony_ci * s:=0; 130e1051a39Sopenharmony_ci * // Sum over j 131e1051a39Sopenharmony_ci * for j:=2 to m do 132e1051a39Sopenharmony_ci * s+:=(RealField(32)!2)^-(j+(k-1)/j); 133e1051a39Sopenharmony_ci * end for; 134e1051a39Sopenharmony_ci * S+:=2^(m-(m-1)*t)*s; 135e1051a39Sopenharmony_ci * end for; 136e1051a39Sopenharmony_ci * A:=2^(k-2-M*t); 137e1051a39Sopenharmony_ci * B:=8*(Pi(RealField(32))^2-6)/3*2^(k-2)*S; 138e1051a39Sopenharmony_ci * pkt:=2.00743*Log(2)*k*2^-k*(A+B); 139e1051a39Sopenharmony_ci * seclevel:=Floor(-Log(2,pkt)); 140e1051a39Sopenharmony_ci * if seclevel ge securitybits then 141e1051a39Sopenharmony_ci * printf "k: %5o, security: %o bits (t: %o, M: %o)\n",k,seclevel,t,M; 142e1051a39Sopenharmony_ci * break; 143e1051a39Sopenharmony_ci * end if; 144e1051a39Sopenharmony_ci * end for; 145e1051a39Sopenharmony_ci * if seclevel ge securitybits then break; end if; 146e1051a39Sopenharmony_ci * end for; 147e1051a39Sopenharmony_ci * 148e1051a39Sopenharmony_ci * It can be run online at: 149e1051a39Sopenharmony_ci * http://magma.maths.usyd.edu.au/calc 150e1051a39Sopenharmony_ci * 151e1051a39Sopenharmony_ci * And will output: 152e1051a39Sopenharmony_ci * k: 1024, security: 129 bits (t: 6, M: 23) 153e1051a39Sopenharmony_ci * 154e1051a39Sopenharmony_ci * k is the number of bits of the prime, securitybits is the level we want to 155e1051a39Sopenharmony_ci * reach. 156e1051a39Sopenharmony_ci * 157e1051a39Sopenharmony_ci * prime length | RSA key size | # MR tests | security level 158e1051a39Sopenharmony_ci * -------------+--------------|------------+--------------- 159e1051a39Sopenharmony_ci * (b) >= 6394 | >= 12788 | 3 | 256 bit 160e1051a39Sopenharmony_ci * (b) >= 3747 | >= 7494 | 3 | 192 bit 161e1051a39Sopenharmony_ci * (b) >= 1345 | >= 2690 | 4 | 128 bit 162e1051a39Sopenharmony_ci * (b) >= 1080 | >= 2160 | 5 | 128 bit 163e1051a39Sopenharmony_ci * (b) >= 852 | >= 1704 | 5 | 112 bit 164e1051a39Sopenharmony_ci * (b) >= 476 | >= 952 | 5 | 80 bit 165e1051a39Sopenharmony_ci * (b) >= 400 | >= 800 | 6 | 80 bit 166e1051a39Sopenharmony_ci * (b) >= 347 | >= 694 | 7 | 80 bit 167e1051a39Sopenharmony_ci * (b) >= 308 | >= 616 | 8 | 80 bit 168e1051a39Sopenharmony_ci * (b) >= 55 | >= 110 | 27 | 64 bit 169e1051a39Sopenharmony_ci * (b) >= 6 | >= 12 | 34 | 64 bit 170e1051a39Sopenharmony_ci */ 171e1051a39Sopenharmony_ci 172e1051a39Sopenharmony_ci# define BN_prime_checks_for_size(b) ((b) >= 3747 ? 3 : \ 173e1051a39Sopenharmony_ci (b) >= 1345 ? 4 : \ 174e1051a39Sopenharmony_ci (b) >= 476 ? 5 : \ 175e1051a39Sopenharmony_ci (b) >= 400 ? 6 : \ 176e1051a39Sopenharmony_ci (b) >= 347 ? 7 : \ 177e1051a39Sopenharmony_ci (b) >= 308 ? 8 : \ 178e1051a39Sopenharmony_ci (b) >= 55 ? 27 : \ 179e1051a39Sopenharmony_ci /* b >= 6 */ 34) 180e1051a39Sopenharmony_ci 181e1051a39Sopenharmony_ci# define BN_num_bytes(a) ((BN_num_bits(a)+7)/8) 182e1051a39Sopenharmony_ci 183e1051a39Sopenharmony_ciint BN_abs_is_word(const BIGNUM *a, const BN_ULONG w); 184e1051a39Sopenharmony_ciint BN_is_zero(const BIGNUM *a); 185e1051a39Sopenharmony_ciint BN_is_one(const BIGNUM *a); 186e1051a39Sopenharmony_ciint BN_is_word(const BIGNUM *a, const BN_ULONG w); 187e1051a39Sopenharmony_ciint BN_is_odd(const BIGNUM *a); 188e1051a39Sopenharmony_ci 189e1051a39Sopenharmony_ci# define BN_one(a) (BN_set_word((a),1)) 190e1051a39Sopenharmony_ci 191e1051a39Sopenharmony_civoid BN_zero_ex(BIGNUM *a); 192e1051a39Sopenharmony_ci 193e1051a39Sopenharmony_ci# if OPENSSL_API_COMPAT >= 0x00908000L 194e1051a39Sopenharmony_ci# define BN_zero(a) BN_zero_ex(a) 195e1051a39Sopenharmony_ci# else 196e1051a39Sopenharmony_ci# define BN_zero(a) (BN_set_word((a),0)) 197e1051a39Sopenharmony_ci# endif 198e1051a39Sopenharmony_ci 199e1051a39Sopenharmony_ciconst BIGNUM *BN_value_one(void); 200e1051a39Sopenharmony_cichar *BN_options(void); 201e1051a39Sopenharmony_ciBN_CTX *BN_CTX_new(void); 202e1051a39Sopenharmony_ciBN_CTX *BN_CTX_secure_new(void); 203e1051a39Sopenharmony_civoid BN_CTX_free(BN_CTX *c); 204e1051a39Sopenharmony_civoid BN_CTX_start(BN_CTX *ctx); 205e1051a39Sopenharmony_ciBIGNUM *BN_CTX_get(BN_CTX *ctx); 206e1051a39Sopenharmony_civoid BN_CTX_end(BN_CTX *ctx); 207e1051a39Sopenharmony_ciint BN_rand(BIGNUM *rnd, int bits, int top, int bottom); 208e1051a39Sopenharmony_ciint BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom); 209e1051a39Sopenharmony_ciint BN_rand_range(BIGNUM *rnd, const BIGNUM *range); 210e1051a39Sopenharmony_ciint BN_priv_rand_range(BIGNUM *rnd, const BIGNUM *range); 211e1051a39Sopenharmony_ciint BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom); 212e1051a39Sopenharmony_ciint BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range); 213e1051a39Sopenharmony_ciint BN_num_bits(const BIGNUM *a); 214e1051a39Sopenharmony_ciint BN_num_bits_word(BN_ULONG l); 215e1051a39Sopenharmony_ciint BN_security_bits(int L, int N); 216e1051a39Sopenharmony_ciBIGNUM *BN_new(void); 217e1051a39Sopenharmony_ciBIGNUM *BN_secure_new(void); 218e1051a39Sopenharmony_civoid BN_clear_free(BIGNUM *a); 219e1051a39Sopenharmony_ciBIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b); 220e1051a39Sopenharmony_civoid BN_swap(BIGNUM *a, BIGNUM *b); 221e1051a39Sopenharmony_ciBIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret); 222e1051a39Sopenharmony_ciint BN_bn2bin(const BIGNUM *a, unsigned char *to); 223e1051a39Sopenharmony_ciint BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen); 224e1051a39Sopenharmony_ciBIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret); 225e1051a39Sopenharmony_ciint BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen); 226e1051a39Sopenharmony_ciBIGNUM *BN_mpi2bn(const unsigned char *s, int len, BIGNUM *ret); 227e1051a39Sopenharmony_ciint BN_bn2mpi(const BIGNUM *a, unsigned char *to); 228e1051a39Sopenharmony_ciint BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 229e1051a39Sopenharmony_ciint BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 230e1051a39Sopenharmony_ciint BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 231e1051a39Sopenharmony_ciint BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 232e1051a39Sopenharmony_ciint BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 233e1051a39Sopenharmony_ciint BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx); 234e1051a39Sopenharmony_ci/** BN_set_negative sets sign of a BIGNUM 235e1051a39Sopenharmony_ci * \param b pointer to the BIGNUM object 236e1051a39Sopenharmony_ci * \param n 0 if the BIGNUM b should be positive and a value != 0 otherwise 237e1051a39Sopenharmony_ci */ 238e1051a39Sopenharmony_civoid BN_set_negative(BIGNUM *b, int n); 239e1051a39Sopenharmony_ci/** BN_is_negative returns 1 if the BIGNUM is negative 240e1051a39Sopenharmony_ci * \param b pointer to the BIGNUM object 241e1051a39Sopenharmony_ci * \return 1 if a < 0 and 0 otherwise 242e1051a39Sopenharmony_ci */ 243e1051a39Sopenharmony_ciint BN_is_negative(const BIGNUM *b); 244e1051a39Sopenharmony_ci 245e1051a39Sopenharmony_ciint BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, 246e1051a39Sopenharmony_ci BN_CTX *ctx); 247e1051a39Sopenharmony_ci# define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx)) 248e1051a39Sopenharmony_ciint BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx); 249e1051a39Sopenharmony_ciint BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, 250e1051a39Sopenharmony_ci BN_CTX *ctx); 251e1051a39Sopenharmony_ciint BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 252e1051a39Sopenharmony_ci const BIGNUM *m); 253e1051a39Sopenharmony_ciint BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, 254e1051a39Sopenharmony_ci BN_CTX *ctx); 255e1051a39Sopenharmony_ciint BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 256e1051a39Sopenharmony_ci const BIGNUM *m); 257e1051a39Sopenharmony_ciint BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, 258e1051a39Sopenharmony_ci BN_CTX *ctx); 259e1051a39Sopenharmony_ciint BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 260e1051a39Sopenharmony_ciint BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 261e1051a39Sopenharmony_ciint BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m); 262e1051a39Sopenharmony_ciint BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, 263e1051a39Sopenharmony_ci BN_CTX *ctx); 264e1051a39Sopenharmony_ciint BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m); 265e1051a39Sopenharmony_ci 266e1051a39Sopenharmony_ciBN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w); 267e1051a39Sopenharmony_ciBN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w); 268e1051a39Sopenharmony_ciint BN_mul_word(BIGNUM *a, BN_ULONG w); 269e1051a39Sopenharmony_ciint BN_add_word(BIGNUM *a, BN_ULONG w); 270e1051a39Sopenharmony_ciint BN_sub_word(BIGNUM *a, BN_ULONG w); 271e1051a39Sopenharmony_ciint BN_set_word(BIGNUM *a, BN_ULONG w); 272e1051a39Sopenharmony_ciBN_ULONG BN_get_word(const BIGNUM *a); 273e1051a39Sopenharmony_ci 274e1051a39Sopenharmony_ciint BN_cmp(const BIGNUM *a, const BIGNUM *b); 275e1051a39Sopenharmony_civoid BN_free(BIGNUM *a); 276e1051a39Sopenharmony_ciint BN_is_bit_set(const BIGNUM *a, int n); 277e1051a39Sopenharmony_ciint BN_lshift(BIGNUM *r, const BIGNUM *a, int n); 278e1051a39Sopenharmony_ciint BN_lshift1(BIGNUM *r, const BIGNUM *a); 279e1051a39Sopenharmony_ciint BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 280e1051a39Sopenharmony_ci 281e1051a39Sopenharmony_ciint BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 282e1051a39Sopenharmony_ci const BIGNUM *m, BN_CTX *ctx); 283e1051a39Sopenharmony_ciint BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 284e1051a39Sopenharmony_ci const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 285e1051a39Sopenharmony_ciint BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, 286e1051a39Sopenharmony_ci const BIGNUM *m, BN_CTX *ctx, 287e1051a39Sopenharmony_ci BN_MONT_CTX *in_mont); 288e1051a39Sopenharmony_ciint BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p, 289e1051a39Sopenharmony_ci const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 290e1051a39Sopenharmony_ciint BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, const BIGNUM *p1, 291e1051a39Sopenharmony_ci const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m, 292e1051a39Sopenharmony_ci BN_CTX *ctx, BN_MONT_CTX *m_ctx); 293e1051a39Sopenharmony_ciint BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 294e1051a39Sopenharmony_ci const BIGNUM *m, BN_CTX *ctx); 295e1051a39Sopenharmony_ci 296e1051a39Sopenharmony_ciint BN_mask_bits(BIGNUM *a, int n); 297e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_STDIO 298e1051a39Sopenharmony_ciint BN_print_fp(FILE *fp, const BIGNUM *a); 299e1051a39Sopenharmony_ci# endif 300e1051a39Sopenharmony_ciint BN_print(BIO *bio, const BIGNUM *a); 301e1051a39Sopenharmony_ciint BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx); 302e1051a39Sopenharmony_ciint BN_rshift(BIGNUM *r, const BIGNUM *a, int n); 303e1051a39Sopenharmony_ciint BN_rshift1(BIGNUM *r, const BIGNUM *a); 304e1051a39Sopenharmony_civoid BN_clear(BIGNUM *a); 305e1051a39Sopenharmony_ciBIGNUM *BN_dup(const BIGNUM *a); 306e1051a39Sopenharmony_ciint BN_ucmp(const BIGNUM *a, const BIGNUM *b); 307e1051a39Sopenharmony_ciint BN_set_bit(BIGNUM *a, int n); 308e1051a39Sopenharmony_ciint BN_clear_bit(BIGNUM *a, int n); 309e1051a39Sopenharmony_cichar *BN_bn2hex(const BIGNUM *a); 310e1051a39Sopenharmony_cichar *BN_bn2dec(const BIGNUM *a); 311e1051a39Sopenharmony_ciint BN_hex2bn(BIGNUM **a, const char *str); 312e1051a39Sopenharmony_ciint BN_dec2bn(BIGNUM **a, const char *str); 313e1051a39Sopenharmony_ciint BN_asc2bn(BIGNUM **a, const char *str); 314e1051a39Sopenharmony_ciint BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 315e1051a39Sopenharmony_ciint BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); /* returns 316e1051a39Sopenharmony_ci * -2 for 317e1051a39Sopenharmony_ci * error */ 318e1051a39Sopenharmony_ciBIGNUM *BN_mod_inverse(BIGNUM *ret, 319e1051a39Sopenharmony_ci const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); 320e1051a39Sopenharmony_ciBIGNUM *BN_mod_sqrt(BIGNUM *ret, 321e1051a39Sopenharmony_ci const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); 322e1051a39Sopenharmony_ci 323e1051a39Sopenharmony_civoid BN_consttime_swap(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords); 324e1051a39Sopenharmony_ci 325e1051a39Sopenharmony_ci/* Deprecated versions */ 326e1051a39Sopenharmony_ciDEPRECATEDIN_0_9_8(BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe, 327e1051a39Sopenharmony_ci const BIGNUM *add, 328e1051a39Sopenharmony_ci const BIGNUM *rem, 329e1051a39Sopenharmony_ci void (*callback) (int, int, 330e1051a39Sopenharmony_ci void *), 331e1051a39Sopenharmony_ci void *cb_arg)) 332e1051a39Sopenharmony_ciDEPRECATEDIN_0_9_8(int 333e1051a39Sopenharmony_ci BN_is_prime(const BIGNUM *p, int nchecks, 334e1051a39Sopenharmony_ci void (*callback) (int, int, void *), 335e1051a39Sopenharmony_ci BN_CTX *ctx, void *cb_arg)) 336e1051a39Sopenharmony_ciDEPRECATEDIN_0_9_8(int 337e1051a39Sopenharmony_ci BN_is_prime_fasttest(const BIGNUM *p, int nchecks, 338e1051a39Sopenharmony_ci void (*callback) (int, int, void *), 339e1051a39Sopenharmony_ci BN_CTX *ctx, void *cb_arg, 340e1051a39Sopenharmony_ci int do_trial_division)) 341e1051a39Sopenharmony_ci 342e1051a39Sopenharmony_ci/* Newer versions */ 343e1051a39Sopenharmony_ciint BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add, 344e1051a39Sopenharmony_ci const BIGNUM *rem, BN_GENCB *cb); 345e1051a39Sopenharmony_ciint BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb); 346e1051a39Sopenharmony_ciint BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, 347e1051a39Sopenharmony_ci int do_trial_division, BN_GENCB *cb); 348e1051a39Sopenharmony_ci 349e1051a39Sopenharmony_ciint BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx); 350e1051a39Sopenharmony_ci 351e1051a39Sopenharmony_ciint BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, 352e1051a39Sopenharmony_ci const BIGNUM *Xp, const BIGNUM *Xp1, 353e1051a39Sopenharmony_ci const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx, 354e1051a39Sopenharmony_ci BN_GENCB *cb); 355e1051a39Sopenharmony_ciint BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, BIGNUM *Xp1, 356e1051a39Sopenharmony_ci BIGNUM *Xp2, const BIGNUM *Xp, const BIGNUM *e, 357e1051a39Sopenharmony_ci BN_CTX *ctx, BN_GENCB *cb); 358e1051a39Sopenharmony_ci 359e1051a39Sopenharmony_ciBN_MONT_CTX *BN_MONT_CTX_new(void); 360e1051a39Sopenharmony_ciint BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 361e1051a39Sopenharmony_ci BN_MONT_CTX *mont, BN_CTX *ctx); 362e1051a39Sopenharmony_ciint BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, 363e1051a39Sopenharmony_ci BN_CTX *ctx); 364e1051a39Sopenharmony_ciint BN_from_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, 365e1051a39Sopenharmony_ci BN_CTX *ctx); 366e1051a39Sopenharmony_civoid BN_MONT_CTX_free(BN_MONT_CTX *mont); 367e1051a39Sopenharmony_ciint BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx); 368e1051a39Sopenharmony_ciBN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from); 369e1051a39Sopenharmony_ciBN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock, 370e1051a39Sopenharmony_ci const BIGNUM *mod, BN_CTX *ctx); 371e1051a39Sopenharmony_ci 372e1051a39Sopenharmony_ci/* BN_BLINDING flags */ 373e1051a39Sopenharmony_ci# define BN_BLINDING_NO_UPDATE 0x00000001 374e1051a39Sopenharmony_ci# define BN_BLINDING_NO_RECREATE 0x00000002 375e1051a39Sopenharmony_ci 376e1051a39Sopenharmony_ciBN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod); 377e1051a39Sopenharmony_civoid BN_BLINDING_free(BN_BLINDING *b); 378e1051a39Sopenharmony_ciint BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx); 379e1051a39Sopenharmony_ciint BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); 380e1051a39Sopenharmony_ciint BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx); 381e1051a39Sopenharmony_ciint BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *); 382e1051a39Sopenharmony_ciint BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, 383e1051a39Sopenharmony_ci BN_CTX *); 384e1051a39Sopenharmony_ci 385e1051a39Sopenharmony_ciint BN_BLINDING_is_current_thread(BN_BLINDING *b); 386e1051a39Sopenharmony_civoid BN_BLINDING_set_current_thread(BN_BLINDING *b); 387e1051a39Sopenharmony_ciint BN_BLINDING_lock(BN_BLINDING *b); 388e1051a39Sopenharmony_ciint BN_BLINDING_unlock(BN_BLINDING *b); 389e1051a39Sopenharmony_ci 390e1051a39Sopenharmony_ciunsigned long BN_BLINDING_get_flags(const BN_BLINDING *); 391e1051a39Sopenharmony_civoid BN_BLINDING_set_flags(BN_BLINDING *, unsigned long); 392e1051a39Sopenharmony_ciBN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b, 393e1051a39Sopenharmony_ci const BIGNUM *e, BIGNUM *m, BN_CTX *ctx, 394e1051a39Sopenharmony_ci int (*bn_mod_exp) (BIGNUM *r, 395e1051a39Sopenharmony_ci const BIGNUM *a, 396e1051a39Sopenharmony_ci const BIGNUM *p, 397e1051a39Sopenharmony_ci const BIGNUM *m, 398e1051a39Sopenharmony_ci BN_CTX *ctx, 399e1051a39Sopenharmony_ci BN_MONT_CTX *m_ctx), 400e1051a39Sopenharmony_ci BN_MONT_CTX *m_ctx); 401e1051a39Sopenharmony_ci 402e1051a39Sopenharmony_ciDEPRECATEDIN_0_9_8(void BN_set_params(int mul, int high, int low, int mont)) 403e1051a39Sopenharmony_ciDEPRECATEDIN_0_9_8(int BN_get_params(int which)) /* 0, mul, 1 high, 2 low, 3 404e1051a39Sopenharmony_ci * mont */ 405e1051a39Sopenharmony_ci 406e1051a39Sopenharmony_ciBN_RECP_CTX *BN_RECP_CTX_new(void); 407e1051a39Sopenharmony_civoid BN_RECP_CTX_free(BN_RECP_CTX *recp); 408e1051a39Sopenharmony_ciint BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *rdiv, BN_CTX *ctx); 409e1051a39Sopenharmony_ciint BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, 410e1051a39Sopenharmony_ci BN_RECP_CTX *recp, BN_CTX *ctx); 411e1051a39Sopenharmony_ciint BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 412e1051a39Sopenharmony_ci const BIGNUM *m, BN_CTX *ctx); 413e1051a39Sopenharmony_ciint BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, 414e1051a39Sopenharmony_ci BN_RECP_CTX *recp, BN_CTX *ctx); 415e1051a39Sopenharmony_ci 416e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_EC2M 417e1051a39Sopenharmony_ci 418e1051a39Sopenharmony_ci/* 419e1051a39Sopenharmony_ci * Functions for arithmetic over binary polynomials represented by BIGNUMs. 420e1051a39Sopenharmony_ci * The BIGNUM::neg property of BIGNUMs representing binary polynomials is 421e1051a39Sopenharmony_ci * ignored. Note that input arguments are not const so that their bit arrays 422e1051a39Sopenharmony_ci * can be expanded to the appropriate size if needed. 423e1051a39Sopenharmony_ci */ 424e1051a39Sopenharmony_ci 425e1051a39Sopenharmony_ci/* 426e1051a39Sopenharmony_ci * r = a + b 427e1051a39Sopenharmony_ci */ 428e1051a39Sopenharmony_ciint BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 429e1051a39Sopenharmony_ci# define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b) 430e1051a39Sopenharmony_ci/* 431e1051a39Sopenharmony_ci * r=a mod p 432e1051a39Sopenharmony_ci */ 433e1051a39Sopenharmony_ciint BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p); 434e1051a39Sopenharmony_ci/* r = (a * b) mod p */ 435e1051a39Sopenharmony_ciint BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 436e1051a39Sopenharmony_ci const BIGNUM *p, BN_CTX *ctx); 437e1051a39Sopenharmony_ci/* r = (a * a) mod p */ 438e1051a39Sopenharmony_ciint BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 439e1051a39Sopenharmony_ci/* r = (1 / b) mod p */ 440e1051a39Sopenharmony_ciint BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx); 441e1051a39Sopenharmony_ci/* r = (a / b) mod p */ 442e1051a39Sopenharmony_ciint BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 443e1051a39Sopenharmony_ci const BIGNUM *p, BN_CTX *ctx); 444e1051a39Sopenharmony_ci/* r = (a ^ b) mod p */ 445e1051a39Sopenharmony_ciint BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 446e1051a39Sopenharmony_ci const BIGNUM *p, BN_CTX *ctx); 447e1051a39Sopenharmony_ci/* r = sqrt(a) mod p */ 448e1051a39Sopenharmony_ciint BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 449e1051a39Sopenharmony_ci BN_CTX *ctx); 450e1051a39Sopenharmony_ci/* r^2 + r = a mod p */ 451e1051a39Sopenharmony_ciint BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 452e1051a39Sopenharmony_ci BN_CTX *ctx); 453e1051a39Sopenharmony_ci# define BN_GF2m_cmp(a, b) BN_ucmp((a), (b)) 454e1051a39Sopenharmony_ci/*- 455e1051a39Sopenharmony_ci * Some functions allow for representation of the irreducible polynomials 456e1051a39Sopenharmony_ci * as an unsigned int[], say p. The irreducible f(t) is then of the form: 457e1051a39Sopenharmony_ci * t^p[0] + t^p[1] + ... + t^p[k] 458e1051a39Sopenharmony_ci * where m = p[0] > p[1] > ... > p[k] = 0. 459e1051a39Sopenharmony_ci */ 460e1051a39Sopenharmony_ci/* r = a mod p */ 461e1051a39Sopenharmony_ciint BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[]); 462e1051a39Sopenharmony_ci/* r = (a * b) mod p */ 463e1051a39Sopenharmony_ciint BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 464e1051a39Sopenharmony_ci const int p[], BN_CTX *ctx); 465e1051a39Sopenharmony_ci/* r = (a * a) mod p */ 466e1051a39Sopenharmony_ciint BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], 467e1051a39Sopenharmony_ci BN_CTX *ctx); 468e1051a39Sopenharmony_ci/* r = (1 / b) mod p */ 469e1051a39Sopenharmony_ciint BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const int p[], 470e1051a39Sopenharmony_ci BN_CTX *ctx); 471e1051a39Sopenharmony_ci/* r = (a / b) mod p */ 472e1051a39Sopenharmony_ciint BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 473e1051a39Sopenharmony_ci const int p[], BN_CTX *ctx); 474e1051a39Sopenharmony_ci/* r = (a ^ b) mod p */ 475e1051a39Sopenharmony_ciint BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, 476e1051a39Sopenharmony_ci const int p[], BN_CTX *ctx); 477e1051a39Sopenharmony_ci/* r = sqrt(a) mod p */ 478e1051a39Sopenharmony_ciint BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, 479e1051a39Sopenharmony_ci const int p[], BN_CTX *ctx); 480e1051a39Sopenharmony_ci/* r^2 + r = a mod p */ 481e1051a39Sopenharmony_ciint BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a, 482e1051a39Sopenharmony_ci const int p[], BN_CTX *ctx); 483e1051a39Sopenharmony_ciint BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max); 484e1051a39Sopenharmony_ciint BN_GF2m_arr2poly(const int p[], BIGNUM *a); 485e1051a39Sopenharmony_ci 486e1051a39Sopenharmony_ci# endif 487e1051a39Sopenharmony_ci 488e1051a39Sopenharmony_ci/* 489e1051a39Sopenharmony_ci * faster mod functions for the 'NIST primes' 0 <= a < p^2 490e1051a39Sopenharmony_ci */ 491e1051a39Sopenharmony_ciint BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 492e1051a39Sopenharmony_ciint BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 493e1051a39Sopenharmony_ciint BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 494e1051a39Sopenharmony_ciint BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 495e1051a39Sopenharmony_ciint BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 496e1051a39Sopenharmony_ci 497e1051a39Sopenharmony_ciconst BIGNUM *BN_get0_nist_prime_192(void); 498e1051a39Sopenharmony_ciconst BIGNUM *BN_get0_nist_prime_224(void); 499e1051a39Sopenharmony_ciconst BIGNUM *BN_get0_nist_prime_256(void); 500e1051a39Sopenharmony_ciconst BIGNUM *BN_get0_nist_prime_384(void); 501e1051a39Sopenharmony_ciconst BIGNUM *BN_get0_nist_prime_521(void); 502e1051a39Sopenharmony_ci 503e1051a39Sopenharmony_ciint (*BN_nist_mod_func(const BIGNUM *p)) (BIGNUM *r, const BIGNUM *a, 504e1051a39Sopenharmony_ci const BIGNUM *field, BN_CTX *ctx); 505e1051a39Sopenharmony_ci 506e1051a39Sopenharmony_ciint BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range, 507e1051a39Sopenharmony_ci const BIGNUM *priv, const unsigned char *message, 508e1051a39Sopenharmony_ci size_t message_len, BN_CTX *ctx); 509e1051a39Sopenharmony_ci 510e1051a39Sopenharmony_ci/* Primes from RFC 2409 */ 511e1051a39Sopenharmony_ciBIGNUM *BN_get_rfc2409_prime_768(BIGNUM *bn); 512e1051a39Sopenharmony_ciBIGNUM *BN_get_rfc2409_prime_1024(BIGNUM *bn); 513e1051a39Sopenharmony_ci 514e1051a39Sopenharmony_ci/* Primes from RFC 3526 */ 515e1051a39Sopenharmony_ciBIGNUM *BN_get_rfc3526_prime_1536(BIGNUM *bn); 516e1051a39Sopenharmony_ciBIGNUM *BN_get_rfc3526_prime_2048(BIGNUM *bn); 517e1051a39Sopenharmony_ciBIGNUM *BN_get_rfc3526_prime_3072(BIGNUM *bn); 518e1051a39Sopenharmony_ciBIGNUM *BN_get_rfc3526_prime_4096(BIGNUM *bn); 519e1051a39Sopenharmony_ciBIGNUM *BN_get_rfc3526_prime_6144(BIGNUM *bn); 520e1051a39Sopenharmony_ciBIGNUM *BN_get_rfc3526_prime_8192(BIGNUM *bn); 521e1051a39Sopenharmony_ci 522e1051a39Sopenharmony_ci# if OPENSSL_API_COMPAT < 0x10100000L 523e1051a39Sopenharmony_ci# define get_rfc2409_prime_768 BN_get_rfc2409_prime_768 524e1051a39Sopenharmony_ci# define get_rfc2409_prime_1024 BN_get_rfc2409_prime_1024 525e1051a39Sopenharmony_ci# define get_rfc3526_prime_1536 BN_get_rfc3526_prime_1536 526e1051a39Sopenharmony_ci# define get_rfc3526_prime_2048 BN_get_rfc3526_prime_2048 527e1051a39Sopenharmony_ci# define get_rfc3526_prime_3072 BN_get_rfc3526_prime_3072 528e1051a39Sopenharmony_ci# define get_rfc3526_prime_4096 BN_get_rfc3526_prime_4096 529e1051a39Sopenharmony_ci# define get_rfc3526_prime_6144 BN_get_rfc3526_prime_6144 530e1051a39Sopenharmony_ci# define get_rfc3526_prime_8192 BN_get_rfc3526_prime_8192 531e1051a39Sopenharmony_ci# endif 532e1051a39Sopenharmony_ci 533e1051a39Sopenharmony_ciint BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom); 534e1051a39Sopenharmony_ci 535e1051a39Sopenharmony_ci 536e1051a39Sopenharmony_ci# ifdef __cplusplus 537e1051a39Sopenharmony_ci} 538e1051a39Sopenharmony_ci# endif 539e1051a39Sopenharmony_ci#endif 540