1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5e71b7053SJung-uk KimBN_add, BN_sub, BN_mul, BN_sqr, BN_div, BN_mod, BN_nnmod, BN_mod_add, 6fdc418f1SGordon TetlowBN_mod_sub, BN_mod_mul, BN_mod_sqr, BN_mod_sqrt, BN_exp, BN_mod_exp, BN_gcd - 7e71b7053SJung-uk Kimarithmetic operations on BIGNUMs 8e71b7053SJung-uk Kim 9e71b7053SJung-uk Kim=head1 SYNOPSIS 10e71b7053SJung-uk Kim 11e71b7053SJung-uk Kim #include <openssl/bn.h> 12e71b7053SJung-uk Kim 13e71b7053SJung-uk Kim int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 14e71b7053SJung-uk Kim 15e71b7053SJung-uk Kim int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 16e71b7053SJung-uk Kim 17*a7148ab3SEnji Cooper int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 18e71b7053SJung-uk Kim 19*a7148ab3SEnji Cooper int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx); 20e71b7053SJung-uk Kim 21e71b7053SJung-uk Kim int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d, 22e71b7053SJung-uk Kim BN_CTX *ctx); 23e71b7053SJung-uk Kim 24e71b7053SJung-uk Kim int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 25e71b7053SJung-uk Kim 26e71b7053SJung-uk Kim int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 27e71b7053SJung-uk Kim 28*a7148ab3SEnji Cooper int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, 29e71b7053SJung-uk Kim BN_CTX *ctx); 30e71b7053SJung-uk Kim 31*a7148ab3SEnji Cooper int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, 32e71b7053SJung-uk Kim BN_CTX *ctx); 33e71b7053SJung-uk Kim 34*a7148ab3SEnji Cooper int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, 35e71b7053SJung-uk Kim BN_CTX *ctx); 36e71b7053SJung-uk Kim 37*a7148ab3SEnji Cooper int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 38e71b7053SJung-uk Kim 39*a7148ab3SEnji Cooper BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 40fdc418f1SGordon Tetlow 41*a7148ab3SEnji Cooper int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 42e71b7053SJung-uk Kim 43*a7148ab3SEnji Cooper int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 44e71b7053SJung-uk Kim const BIGNUM *m, BN_CTX *ctx); 45e71b7053SJung-uk Kim 46*a7148ab3SEnji Cooper int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 47e71b7053SJung-uk Kim 48e71b7053SJung-uk Kim=head1 DESCRIPTION 49e71b7053SJung-uk Kim 50e71b7053SJung-uk KimBN_add() adds I<a> and I<b> and places the result in I<r> (C<r=a+b>). 51e71b7053SJung-uk KimI<r> may be the same B<BIGNUM> as I<a> or I<b>. 52e71b7053SJung-uk Kim 53e71b7053SJung-uk KimBN_sub() subtracts I<b> from I<a> and places the result in I<r> (C<r=a-b>). 54e71b7053SJung-uk KimI<r> may be the same B<BIGNUM> as I<a> or I<b>. 55e71b7053SJung-uk Kim 56e71b7053SJung-uk KimBN_mul() multiplies I<a> and I<b> and places the result in I<r> (C<r=a*b>). 57e71b7053SJung-uk KimI<r> may be the same B<BIGNUM> as I<a> or I<b>. 58e71b7053SJung-uk KimFor multiplication by powers of 2, use L<BN_lshift(3)>. 59e71b7053SJung-uk Kim 60e71b7053SJung-uk KimBN_sqr() takes the square of I<a> and places the result in I<r> 61e71b7053SJung-uk Kim(C<r=a^2>). I<r> and I<a> may be the same B<BIGNUM>. 62e71b7053SJung-uk KimThis function is faster than BN_mul(r,a,a). 63e71b7053SJung-uk Kim 64e71b7053SJung-uk KimBN_div() divides I<a> by I<d> and places the result in I<dv> and the 65e71b7053SJung-uk Kimremainder in I<rem> (C<dv=a/d, rem=a%d>). Either of I<dv> and I<rem> may 66e71b7053SJung-uk Kimbe B<NULL>, in which case the respective value is not returned. 67e71b7053SJung-uk KimThe result is rounded towards zero; thus if I<a> is negative, the 68e71b7053SJung-uk Kimremainder will be zero or negative. 69e71b7053SJung-uk KimFor division by powers of 2, use BN_rshift(3). 70e71b7053SJung-uk Kim 71e71b7053SJung-uk KimBN_mod() corresponds to BN_div() with I<dv> set to B<NULL>. 72e71b7053SJung-uk Kim 7358f35182SJung-uk KimBN_nnmod() reduces I<a> modulo I<m> and places the nonnegative 74e71b7053SJung-uk Kimremainder in I<r>. 75e71b7053SJung-uk Kim 7658f35182SJung-uk KimBN_mod_add() adds I<a> to I<b> modulo I<m> and places the nonnegative 77e71b7053SJung-uk Kimresult in I<r>. 78e71b7053SJung-uk Kim 79e71b7053SJung-uk KimBN_mod_sub() subtracts I<b> from I<a> modulo I<m> and places the 8058f35182SJung-uk Kimnonnegative result in I<r>. 81e71b7053SJung-uk Kim 8258f35182SJung-uk KimBN_mod_mul() multiplies I<a> by I<b> and finds the nonnegative 83e71b7053SJung-uk Kimremainder respective to modulus I<m> (C<r=(a*b) mod m>). I<r> may be 84e71b7053SJung-uk Kimthe same B<BIGNUM> as I<a> or I<b>. For more efficient algorithms for 85e71b7053SJung-uk Kimrepeated computations using the same modulus, see 86e71b7053SJung-uk KimL<BN_mod_mul_montgomery(3)> and 87e71b7053SJung-uk KimL<BN_mod_mul_reciprocal(3)>. 88e71b7053SJung-uk Kim 89e71b7053SJung-uk KimBN_mod_sqr() takes the square of I<a> modulo B<m> and places the 90e71b7053SJung-uk Kimresult in I<r>. 91e71b7053SJung-uk Kim 92fdc418f1SGordon TetlowBN_mod_sqrt() returns the modular square root of I<a> such that 93fdc418f1SGordon TetlowC<in^2 = a (mod p)>. The modulus I<p> must be a 94fdc418f1SGordon Tetlowprime, otherwise an error or an incorrect "result" will be returned. 95fdc418f1SGordon TetlowThe result is stored into I<in> which can be NULL. The result will be 96fdc418f1SGordon Tetlownewly allocated in that case. 97fdc418f1SGordon Tetlow 98e71b7053SJung-uk KimBN_exp() raises I<a> to the I<p>-th power and places the result in I<r> 99e71b7053SJung-uk Kim(C<r=a^p>). This function is faster than repeated applications of 100e71b7053SJung-uk KimBN_mul(). 101e71b7053SJung-uk Kim 102e71b7053SJung-uk KimBN_mod_exp() computes I<a> to the I<p>-th power modulo I<m> (C<r=a^p % 103e71b7053SJung-uk Kimm>). This function uses less time and space than BN_exp(). Do not call this 104e71b7053SJung-uk Kimfunction when B<m> is even and any of the parameters have the 105e71b7053SJung-uk KimB<BN_FLG_CONSTTIME> flag set. 106e71b7053SJung-uk Kim 107e71b7053SJung-uk KimBN_gcd() computes the greatest common divisor of I<a> and I<b> and 108e71b7053SJung-uk Kimplaces the result in I<r>. I<r> may be the same B<BIGNUM> as I<a> or 109e71b7053SJung-uk KimI<b>. 110e71b7053SJung-uk Kim 111e71b7053SJung-uk KimFor all functions, I<ctx> is a previously allocated B<BN_CTX> used for 112e71b7053SJung-uk Kimtemporary variables; see L<BN_CTX_new(3)>. 113e71b7053SJung-uk Kim 114e71b7053SJung-uk KimUnless noted otherwise, the result B<BIGNUM> must be different from 115e71b7053SJung-uk Kimthe arguments. 116e71b7053SJung-uk Kim 117e0c4386eSCy Schubert=head1 NOTES 118e0c4386eSCy Schubert 119e0c4386eSCy SchubertFor modular operations such as BN_nnmod() or BN_mod_exp() it is an error 120e0c4386eSCy Schubertto use the same B<BIGNUM> object for the modulus as for the output. 121e0c4386eSCy Schubert 122e71b7053SJung-uk Kim=head1 RETURN VALUES 123e71b7053SJung-uk Kim 124fdc418f1SGordon TetlowThe BN_mod_sqrt() returns the result (possibly incorrect if I<p> is 125fdc418f1SGordon Tetlownot a prime), or NULL. 126fdc418f1SGordon Tetlow 127fdc418f1SGordon TetlowFor all remaining functions, 1 is returned for success, 0 on error. The return 128e71b7053SJung-uk Kimvalue should always be checked (e.g., C<if (!BN_add(r,a,b)) goto err;>). 129e71b7053SJung-uk KimThe error codes can be obtained by L<ERR_get_error(3)>. 130e71b7053SJung-uk Kim 131e71b7053SJung-uk Kim=head1 SEE ALSO 132e71b7053SJung-uk Kim 133e71b7053SJung-uk KimL<ERR_get_error(3)>, L<BN_CTX_new(3)>, 134e71b7053SJung-uk KimL<BN_add_word(3)>, L<BN_set_bit(3)> 135e71b7053SJung-uk Kim 136e71b7053SJung-uk Kim=head1 COPYRIGHT 137e71b7053SJung-uk Kim 138*a7148ab3SEnji CooperCopyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved. 139e71b7053SJung-uk Kim 140b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 141e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 142e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 143e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 144e71b7053SJung-uk Kim 145e71b7053SJung-uk Kim=cut 146