159191Skris=pod 259191Skris 359191Skris=head1 NAME 459191Skris 559191SkrisBN_mod_mul_montgomery, BN_MONT_CTX_new, BN_MONT_CTX_init, 659191SkrisBN_MONT_CTX_free, BN_MONT_CTX_set, BN_MONT_CTX_copy, 759191SkrisBN_from_montgomery, BN_to_montgomery - Montgomery multiplication 859191Skris 959191Skris=head1 SYNOPSIS 1059191Skris 1159191Skris #include <openssl/bn.h> 1259191Skris 1359191Skris BN_MONT_CTX *BN_MONT_CTX_new(void); 1459191Skris void BN_MONT_CTX_init(BN_MONT_CTX *ctx); 1559191Skris void BN_MONT_CTX_free(BN_MONT_CTX *mont); 1659191Skris 1759191Skris int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *m, BN_CTX *ctx); 1859191Skris BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from); 1959191Skris 2059191Skris int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b, 2159191Skris BN_MONT_CTX *mont, BN_CTX *ctx); 2259191Skris 2359191Skris int BN_from_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont, 2459191Skris BN_CTX *ctx); 2559191Skris 2659191Skris int BN_to_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont, 2759191Skris BN_CTX *ctx); 2859191Skris 2959191Skris=head1 DESCRIPTION 3059191Skris 3159191SkrisThese functions implement Montgomery multiplication. They are used 3259191Skrisautomatically when L<BN_mod_exp(3)|BN_mod_exp(3)> is called with suitable input, 3359191Skrisbut they may be useful when several operations are to be performed 3459191Skrisusing the same modulus. 3559191Skris 3659191SkrisBN_MONT_CTX_new() allocates and initializes a B<BN_MONT_CTX> structure. 3759191SkrisBN_MONT_CTX_init() initializes an existing uninitialized B<BN_MONT_CTX>. 3859191Skris 39109998SmarkmBN_MONT_CTX_set() sets up the I<mont> structure from the modulus I<m> 4059191Skrisby precomputing its inverse and a value R. 4159191Skris 42109998SmarkmBN_MONT_CTX_copy() copies the B<BN_MONT_CTX> I<from> to I<to>. 4359191Skris 4459191SkrisBN_MONT_CTX_free() frees the components of the B<BN_MONT_CTX>, and, if 4559191Skrisit was created by BN_MONT_CTX_new(), also the structure itself. 4659191Skris 47109998SmarkmBN_mod_mul_montgomery() computes Mont(I<a>,I<b>):=I<a>*I<b>*R^-1 and places 48109998Smarkmthe result in I<r>. 4959191Skris 50109998SmarkmBN_from_montgomery() performs the Montgomery reduction I<r> = I<a>*R^-1. 5159191Skris 52109998SmarkmBN_to_montgomery() computes Mont(I<a>,R^2), i.e. I<a>*R. 53109998SmarkmNote that I<a> must be non-negative and smaller than the modulus. 5459191Skris 55109998SmarkmFor all functions, I<ctx> is a previously allocated B<BN_CTX> used for 5659191Skristemporary variables. 5759191Skris 5859191SkrisThe B<BN_MONT_CTX> structure is defined as follows: 5959191Skris 6059191Skris typedef struct bn_mont_ctx_st 6159191Skris { 6259191Skris int ri; /* number of bits in R */ 6359191Skris BIGNUM RR; /* R^2 (used to convert to Montgomery form) */ 6459191Skris BIGNUM N; /* The modulus */ 6559191Skris BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 6659191Skris * (Ni is only stored for bignum algorithm) */ 6759191Skris BN_ULONG n0; /* least significant word of Ni */ 6859191Skris int flags; 6959191Skris } BN_MONT_CTX; 7059191Skris 7159191SkrisBN_to_montgomery() is a macro. 7259191Skris 7359191Skris=head1 RETURN VALUES 7459191Skris 7559191SkrisBN_MONT_CTX_new() returns the newly allocated B<BN_MONT_CTX>, and NULL 7659191Skrison error. 7759191Skris 7859191SkrisBN_MONT_CTX_init() and BN_MONT_CTX_free() have no return values. 7959191Skris 8059191SkrisFor the other functions, 1 is returned for success, 0 on error. 8159191SkrisThe error codes can be obtained by L<ERR_get_error(3)|ERR_get_error(3)>. 8259191Skris 83109998Smarkm=head1 WARNING 84109998Smarkm 85109998SmarkmThe inputs must be reduced modulo B<m>, otherwise the result will be 86109998Smarkmoutside the expected range. 87109998Smarkm 8859191Skris=head1 SEE ALSO 8959191Skris 90109998SmarkmL<bn(3)|bn(3)>, L<ERR_get_error(3)|ERR_get_error(3)>, L<BN_add(3)|BN_add(3)>, 9159191SkrisL<BN_CTX_new(3)|BN_CTX_new(3)> 9259191Skris 9359191Skris=head1 HISTORY 9459191Skris 9559191SkrisBN_MONT_CTX_new(), BN_MONT_CTX_free(), BN_MONT_CTX_set(), 9659191SkrisBN_mod_mul_montgomery(), BN_from_montgomery() and BN_to_montgomery() 9759191Skrisare available in all versions of SSLeay and OpenSSL. 9859191Skris 9959191SkrisBN_MONT_CTX_init() and BN_MONT_CTX_copy() were added in SSLeay 0.9.1b. 10059191Skris 10159191Skris=cut 102