159191Skris=pod
259191Skris
359191Skris=head1 NAME
459191Skris
559191SkrisRSA_set_default_method, RSA_get_default_method, RSA_set_method,
6109998SmarkmRSA_get_method, RSA_PKCS1_SSLeay, RSA_null_method, RSA_flags,
7109998SmarkmRSA_new_method - select RSA method
859191Skris
959191Skris=head1 SYNOPSIS
1059191Skris
1159191Skris #include <openssl/rsa.h>
1259191Skris
13109998Smarkm void RSA_set_default_method(const RSA_METHOD *meth);
1459191Skris
1559191Skris RSA_METHOD *RSA_get_default_method(void);
1659191Skris
17109998Smarkm int RSA_set_method(RSA *rsa, const RSA_METHOD *meth);
1859191Skris
19109998Smarkm RSA_METHOD *RSA_get_method(const RSA *rsa);
2059191Skris
2159191Skris RSA_METHOD *RSA_PKCS1_SSLeay(void);
2259191Skris
2359191Skris RSA_METHOD *RSA_null_method(void);
2459191Skris
25109998Smarkm int RSA_flags(const RSA *rsa);
2659191Skris
2759191Skris RSA *RSA_new_method(RSA_METHOD *method);
2859191Skris
2959191Skris=head1 DESCRIPTION
3059191Skris
3159191SkrisAn B<RSA_METHOD> specifies the functions that OpenSSL uses for RSA
32109998Smarkmoperations. By modifying the method, alternative implementations such as
33109998Smarkmhardware accelerators may be used. IMPORTANT: See the NOTES section for
34109998Smarkmimportant information about how these RSA API functions are affected by the
35109998Smarkmuse of B<ENGINE> API calls.
3659191Skris
37109998SmarkmInitially, the default RSA_METHOD is the OpenSSL internal implementation,
38109998Smarkmas returned by RSA_PKCS1_SSLeay().
3959191Skris
40109998SmarkmRSA_set_default_method() makes B<meth> the default method for all RSA
41109998Smarkmstructures created later. B<NB>: This is true only whilst no ENGINE has
42109998Smarkmbeen set as a default for RSA, so this function is no longer recommended.
4359191Skris
4459191SkrisRSA_get_default_method() returns a pointer to the current default
45194206SsimonRSA_METHOD. However, the meaningfulness of this result is dependent on
46109998Smarkmwhether the ENGINE API is being used, so this function is no longer 
47109998Smarkmrecommended.
4859191Skris
49109998SmarkmRSA_set_method() selects B<meth> to perform all operations using the key
50109998SmarkmB<rsa>. This will replace the RSA_METHOD used by the RSA key and if the
51109998Smarkmprevious method was supplied by an ENGINE, the handle to that ENGINE will
52109998Smarkmbe released during the change. It is possible to have RSA keys that only
53109998Smarkmwork with certain RSA_METHOD implementations (eg. from an ENGINE module
54109998Smarkmthat supports embedded hardware-protected keys), and in such cases
55109998Smarkmattempting to change the RSA_METHOD for the key can have unexpected
56109998Smarkmresults.
5759191Skris
58109998SmarkmRSA_get_method() returns a pointer to the RSA_METHOD being used by B<rsa>.
59109998SmarkmThis method may or may not be supplied by an ENGINE implementation, but if
60109998Smarkmit is, the return value can only be guaranteed to be valid as long as the
61109998SmarkmRSA key itself is valid and does not have its implementation changed by
62109998SmarkmRSA_set_method().
6359191Skris
64109998SmarkmRSA_flags() returns the B<flags> that are set for B<rsa>'s current
65109998SmarkmRSA_METHOD. See the BUGS section.
66109998Smarkm
67109998SmarkmRSA_new_method() allocates and initializes an RSA structure so that
68109998SmarkmB<engine> will be used for the RSA operations. If B<engine> is NULL, the
69109998Smarkmdefault ENGINE for RSA operations is used, and if no default ENGINE is set,
70109998Smarkmthe RSA_METHOD controlled by RSA_set_default_method() is used.
71109998Smarkm
7259191SkrisRSA_flags() returns the B<flags> that are set for B<rsa>'s current method.
7359191Skris
7459191SkrisRSA_new_method() allocates and initializes an B<RSA> structure so that
7559191SkrisB<method> will be used for the RSA operations. If B<method> is B<NULL>,
7659191Skristhe default method is used.
7759191Skris
7859191Skris=head1 THE RSA_METHOD STRUCTURE
7959191Skris
8059191Skris typedef struct rsa_meth_st
8159191Skris {
8259191Skris     /* name of the implementation */
8359191Skris	const char *name;
8459191Skris
8559191Skris     /* encrypt */
8659191Skris	int (*rsa_pub_enc)(int flen, unsigned char *from,
8759191Skris          unsigned char *to, RSA *rsa, int padding);
8859191Skris
8959191Skris     /* verify arbitrary data */
9059191Skris	int (*rsa_pub_dec)(int flen, unsigned char *from,
9159191Skris          unsigned char *to, RSA *rsa, int padding);
9259191Skris
9359191Skris     /* sign arbitrary data */
9459191Skris	int (*rsa_priv_enc)(int flen, unsigned char *from,
9559191Skris          unsigned char *to, RSA *rsa, int padding);
9659191Skris
9759191Skris     /* decrypt */
9859191Skris	int (*rsa_priv_dec)(int flen, unsigned char *from,
9959191Skris          unsigned char *to, RSA *rsa, int padding);
10059191Skris
10159191Skris     /* compute r0 = r0 ^ I mod rsa->n (May be NULL for some
10259191Skris                                        implementations) */
10359191Skris	int (*rsa_mod_exp)(BIGNUM *r0, BIGNUM *I, RSA *rsa);
10459191Skris
10559191Skris     /* compute r = a ^ p mod m (May be NULL for some implementations) */
10659191Skris	int (*bn_mod_exp)(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
10759191Skris          const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
10859191Skris
10959191Skris     /* called at RSA_new */
11059191Skris	int (*init)(RSA *rsa);
11159191Skris
11259191Skris     /* called at RSA_free */
11359191Skris	int (*finish)(RSA *rsa);
11459191Skris
11559191Skris     /* RSA_FLAG_EXT_PKEY        - rsa_mod_exp is called for private key
11659191Skris      *                            operations, even if p,q,dmp1,dmq1,iqmp
11759191Skris      *                            are NULL
11859191Skris      * RSA_FLAG_SIGN_VER        - enable rsa_sign and rsa_verify
11959191Skris      * RSA_METHOD_FLAG_NO_CHECK - don't check pub/private match
12059191Skris      */
12159191Skris	int flags;
12259191Skris
12359191Skris	char *app_data; /* ?? */
12459191Skris
12559191Skris     /* sign. For backward compatibility, this is used only
12659191Skris      * if (flags & RSA_FLAG_SIGN_VER)
12759191Skris      */
128269686Sjkim	int (*rsa_sign)(int type,
129269686Sjkim		const unsigned char *m, unsigned int m_length,
130269686Sjkim		unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
13159191Skris     /* verify. For backward compatibility, this is used only
13259191Skris      * if (flags & RSA_FLAG_SIGN_VER)
13359191Skris      */
134269686Sjkim	int (*rsa_verify)(int dtype,
135269686Sjkim		const unsigned char *m, unsigned int m_length,
136269686Sjkim		const unsigned char *sigbuf, unsigned int siglen,
137269686Sjkim								const RSA *rsa);
138269686Sjkim     /* keygen. If NULL builtin RSA key generation will be used */
139269686Sjkim	int (*rsa_keygen)(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
14059191Skris
14159191Skris } RSA_METHOD;
14259191Skris
14359191Skris=head1 RETURN VALUES
14459191Skris
145109998SmarkmRSA_PKCS1_SSLeay(), RSA_PKCS1_null_method(), RSA_get_default_method()
146109998Smarkmand RSA_get_method() return pointers to the respective RSA_METHODs.
14759191Skris
14859191SkrisRSA_set_default_method() returns no value.
14959191Skris
150109998SmarkmRSA_set_method() returns a pointer to the old RSA_METHOD implementation
151109998Smarkmthat was replaced. However, this return value should probably be ignored
152109998Smarkmbecause if it was supplied by an ENGINE, the pointer could be invalidated
153109998Smarkmat any time if the ENGINE is unloaded (in fact it could be unloaded as a
154109998Smarkmresult of the RSA_set_method() function releasing its handle to the
155109998SmarkmENGINE). For this reason, the return type may be replaced with a B<void>
156109998Smarkmdeclaration in a future release.
15759191Skris
158109998SmarkmRSA_new_method() returns NULL and sets an error code that can be obtained
159109998Smarkmby L<ERR_get_error(3)|ERR_get_error(3)> if the allocation fails. Otherwise
160109998Smarkmit returns a pointer to the newly allocated structure.
16159191Skris
162109998Smarkm=head1 NOTES
163109998Smarkm
164109998SmarkmAs of version 0.9.7, RSA_METHOD implementations are grouped together with
165109998Smarkmother algorithmic APIs (eg. DSA_METHOD, EVP_CIPHER, etc) into B<ENGINE>
166109998Smarkmmodules. If a default ENGINE is specified for RSA functionality using an
167109998SmarkmENGINE API function, that will override any RSA defaults set using the RSA
168109998SmarkmAPI (ie.  RSA_set_default_method()). For this reason, the ENGINE API is the
169109998Smarkmrecommended way to control default implementations for use in RSA and other
170109998Smarkmcryptographic algorithms.
171109998Smarkm
172109998Smarkm=head1 BUGS
173109998Smarkm
174109998SmarkmThe behaviour of RSA_flags() is a mis-feature that is left as-is for now
175109998Smarkmto avoid creating compatibility problems. RSA functionality, such as the
176109998Smarkmencryption functions, are controlled by the B<flags> value in the RSA key
177109998Smarkmitself, not by the B<flags> value in the RSA_METHOD attached to the RSA key
178109998Smarkm(which is what this function returns). If the flags element of an RSA key
179109998Smarkmis changed, the changes will be honoured by RSA functionality but will not
180109998Smarkmbe reflected in the return value of the RSA_flags() function - in effect
181109998SmarkmRSA_flags() behaves more like an RSA_default_flags() function (which does
182109998Smarkmnot currently exist).
183109998Smarkm
18459191Skris=head1 SEE ALSO
18559191Skris
18659191SkrisL<rsa(3)|rsa(3)>, L<RSA_new(3)|RSA_new(3)>
18759191Skris
18859191Skris=head1 HISTORY
18959191Skris
19059191SkrisRSA_new_method() and RSA_set_default_method() appeared in SSLeay 0.8.
19159191SkrisRSA_get_default_method(), RSA_set_method() and RSA_get_method() as
19259191Skriswell as the rsa_sign and rsa_verify components of RSA_METHOD were
19359191Skrisadded in OpenSSL 0.9.4.
19459191Skris
195109998SmarkmRSA_set_default_openssl_method() and RSA_get_default_openssl_method()
196109998Smarkmreplaced RSA_set_default_method() and RSA_get_default_method()
197109998Smarkmrespectively, and RSA_set_method() and RSA_new_method() were altered to use
198109998SmarkmB<ENGINE>s rather than B<RSA_METHOD>s during development of the engine
199109998Smarkmversion of OpenSSL 0.9.6. For 0.9.7, the handling of defaults in the ENGINE
200109998SmarkmAPI was restructured so that this change was reversed, and behaviour of the
201109998Smarkmother functions resembled more closely the previous behaviour. The
202109998Smarkmbehaviour of defaults in the ENGINE API now transparently overrides the
203109998Smarkmbehaviour of defaults in the RSA API without requiring changing these
204109998Smarkmfunction prototypes.
205109998Smarkm
20659191Skris=cut
207