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