1*2175Sjp161948=pod
2*2175Sjp161948
3*2175Sjp161948=head1 NAME
4*2175Sjp161948
5*2175Sjp161948BN_mod_mul_montgomery, BN_MONT_CTX_new, BN_MONT_CTX_init,
6*2175Sjp161948BN_MONT_CTX_free, BN_MONT_CTX_set, BN_MONT_CTX_copy,
7*2175Sjp161948BN_from_montgomery, BN_to_montgomery - Montgomery multiplication
8*2175Sjp161948
9*2175Sjp161948=head1 SYNOPSIS
10*2175Sjp161948
11*2175Sjp161948 #include <openssl/bn.h>
12*2175Sjp161948
13*2175Sjp161948 BN_MONT_CTX *BN_MONT_CTX_new(void);
14*2175Sjp161948 void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
15*2175Sjp161948 void BN_MONT_CTX_free(BN_MONT_CTX *mont);
16*2175Sjp161948
17*2175Sjp161948 int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *m, BN_CTX *ctx);
18*2175Sjp161948 BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
19*2175Sjp161948
20*2175Sjp161948 int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,
21*2175Sjp161948         BN_MONT_CTX *mont, BN_CTX *ctx);
22*2175Sjp161948
23*2175Sjp161948 int BN_from_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
24*2175Sjp161948         BN_CTX *ctx);
25*2175Sjp161948
26*2175Sjp161948 int BN_to_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
27*2175Sjp161948         BN_CTX *ctx);
28*2175Sjp161948
29*2175Sjp161948=head1 DESCRIPTION
30*2175Sjp161948
31*2175Sjp161948These functions implement Montgomery multiplication. They are used
32*2175Sjp161948automatically when L<BN_mod_exp(3)|BN_mod_exp(3)> is called with suitable input,
33*2175Sjp161948but they may be useful when several operations are to be performed
34*2175Sjp161948using the same modulus.
35*2175Sjp161948
36*2175Sjp161948BN_MONT_CTX_new() allocates and initializes a B<BN_MONT_CTX> structure.
37*2175Sjp161948BN_MONT_CTX_init() initializes an existing uninitialized B<BN_MONT_CTX>.
38*2175Sjp161948
39*2175Sjp161948BN_MONT_CTX_set() sets up the I<mont> structure from the modulus I<m>
40*2175Sjp161948by precomputing its inverse and a value R.
41*2175Sjp161948
42*2175Sjp161948BN_MONT_CTX_copy() copies the B<BN_MONT_CTX> I<from> to I<to>.
43*2175Sjp161948
44*2175Sjp161948BN_MONT_CTX_free() frees the components of the B<BN_MONT_CTX>, and, if
45*2175Sjp161948it was created by BN_MONT_CTX_new(), also the structure itself.
46*2175Sjp161948
47*2175Sjp161948BN_mod_mul_montgomery() computes Mont(I<a>,I<b>):=I<a>*I<b>*R^-1 and places
48*2175Sjp161948the result in I<r>.
49*2175Sjp161948
50*2175Sjp161948BN_from_montgomery() performs the Montgomery reduction I<r> = I<a>*R^-1.
51*2175Sjp161948
52*2175Sjp161948BN_to_montgomery() computes Mont(I<a>,R^2), i.e. I<a>*R.
53*2175Sjp161948Note that I<a> must be non-negative and smaller than the modulus.
54*2175Sjp161948
55*2175Sjp161948For all functions, I<ctx> is a previously allocated B<BN_CTX> used for
56*2175Sjp161948temporary variables.
57*2175Sjp161948
58*2175Sjp161948The B<BN_MONT_CTX> structure is defined as follows:
59*2175Sjp161948
60*2175Sjp161948 typedef struct bn_mont_ctx_st
61*2175Sjp161948        {
62*2175Sjp161948        int ri;         /* number of bits in R */
63*2175Sjp161948        BIGNUM RR;      /* R^2 (used to convert to Montgomery form) */
64*2175Sjp161948        BIGNUM N;       /* The modulus */
65*2175Sjp161948        BIGNUM Ni;      /* R*(1/R mod N) - N*Ni = 1
66*2175Sjp161948                         * (Ni is only stored for bignum algorithm) */
67*2175Sjp161948        BN_ULONG n0;    /* least significant word of Ni */
68*2175Sjp161948        int flags;
69*2175Sjp161948        } BN_MONT_CTX;
70*2175Sjp161948
71*2175Sjp161948BN_to_montgomery() is a macro.
72*2175Sjp161948
73*2175Sjp161948=head1 RETURN VALUES
74*2175Sjp161948
75*2175Sjp161948BN_MONT_CTX_new() returns the newly allocated B<BN_MONT_CTX>, and NULL
76*2175Sjp161948on error.
77*2175Sjp161948
78*2175Sjp161948BN_MONT_CTX_init() and BN_MONT_CTX_free() have no return values.
79*2175Sjp161948
80*2175Sjp161948For the other functions, 1 is returned for success, 0 on error.
81*2175Sjp161948The error codes can be obtained by L<ERR_get_error(3)|ERR_get_error(3)>.
82*2175Sjp161948
83*2175Sjp161948=head1 WARNING
84*2175Sjp161948
85*2175Sjp161948The inputs must be reduced modulo B<m>, otherwise the result will be
86*2175Sjp161948outside the expected range.
87*2175Sjp161948
88*2175Sjp161948=head1 SEE ALSO
89*2175Sjp161948
90*2175Sjp161948L<bn(3)|bn(3)>, L<ERR_get_error(3)|ERR_get_error(3)>, L<BN_add(3)|BN_add(3)>,
91*2175Sjp161948L<BN_CTX_new(3)|BN_CTX_new(3)>
92*2175Sjp161948
93*2175Sjp161948=head1 HISTORY
94*2175Sjp161948
95*2175Sjp161948BN_MONT_CTX_new(), BN_MONT_CTX_free(), BN_MONT_CTX_set(),
96*2175Sjp161948BN_mod_mul_montgomery(), BN_from_montgomery() and BN_to_montgomery()
97*2175Sjp161948are available in all versions of SSLeay and OpenSSL.
98*2175Sjp161948
99*2175Sjp161948BN_MONT_CTX_init() and BN_MONT_CTX_copy() were added in SSLeay 0.9.1b.
100*2175Sjp161948
101*2175Sjp161948=cut
102