xref: /freebsd/crypto/openssl/doc/man3/RSA_set_method.pod (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1=pod
2
3=head1 NAME
4
5RSA_set_default_method, RSA_get_default_method, RSA_set_method,
6RSA_get_method, RSA_PKCS1_OpenSSL, RSA_flags,
7RSA_new_method - select RSA method
8
9=head1 SYNOPSIS
10
11 #include <openssl/rsa.h>
12
13 void RSA_set_default_method(const RSA_METHOD *meth);
14
15 RSA_METHOD *RSA_get_default_method(void);
16
17 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth);
18
19 RSA_METHOD *RSA_get_method(const RSA *rsa);
20
21 RSA_METHOD *RSA_PKCS1_OpenSSL(void);
22
23 int RSA_flags(const RSA *rsa);
24
25 RSA *RSA_new_method(ENGINE *engine);
26
27=head1 DESCRIPTION
28
29An B<RSA_METHOD> specifies the functions that OpenSSL uses for RSA
30operations. By modifying the method, alternative implementations such as
31hardware accelerators may be used. IMPORTANT: See the NOTES section for
32important information about how these RSA API functions are affected by the
33use of B<ENGINE> API calls.
34
35Initially, the default RSA_METHOD is the OpenSSL internal implementation,
36as returned by RSA_PKCS1_OpenSSL().
37
38RSA_set_default_method() makes B<meth> the default method for all RSA
39structures created later.
40B<NB>: This is true only whilst no ENGINE has
41been set as a default for RSA, so this function is no longer recommended.
42This function is not thread-safe and should not be called at the same time
43as other OpenSSL functions.
44
45RSA_get_default_method() returns a pointer to the current default
46RSA_METHOD. However, the meaningfulness of this result is dependent on
47whether the ENGINE API is being used, so this function is no longer
48recommended.
49
50RSA_set_method() selects B<meth> to perform all operations using the key
51B<rsa>. This will replace the RSA_METHOD used by the RSA key and if the
52previous method was supplied by an ENGINE, the handle to that ENGINE will
53be released during the change. It is possible to have RSA keys that only
54work with certain RSA_METHOD implementations (eg. from an ENGINE module
55that supports embedded hardware-protected keys), and in such cases
56attempting to change the RSA_METHOD for the key can have unexpected
57results.
58
59RSA_get_method() returns a pointer to the RSA_METHOD being used by B<rsa>.
60This method may or may not be supplied by an ENGINE implementation, but if
61it is, the return value can only be guaranteed to be valid as long as the
62RSA key itself is valid and does not have its implementation changed by
63RSA_set_method().
64
65RSA_flags() returns the B<flags> that are set for B<rsa>'s current
66RSA_METHOD. See the BUGS section.
67
68RSA_new_method() allocates and initializes an RSA structure so that
69B<engine> will be used for the RSA operations. If B<engine> is NULL, the
70default ENGINE for RSA operations is used, and if no default ENGINE is set,
71the RSA_METHOD controlled by RSA_set_default_method() is used.
72
73RSA_flags() returns the B<flags> that are set for B<rsa>'s current method.
74
75RSA_new_method() allocates and initializes an B<RSA> structure so that
76B<method> will be used for the RSA operations. If B<method> is B<NULL>,
77the default method is used.
78
79=head1 THE RSA_METHOD STRUCTURE
80
81 typedef struct rsa_meth_st
82 {
83     /* name of the implementation */
84     const char *name;
85
86     /* encrypt */
87     int (*rsa_pub_enc)(int flen, unsigned char *from,
88                        unsigned char *to, RSA *rsa, int padding);
89
90     /* verify arbitrary data */
91     int (*rsa_pub_dec)(int flen, unsigned char *from,
92                        unsigned char *to, RSA *rsa, int padding);
93
94     /* sign arbitrary data */
95     int (*rsa_priv_enc)(int flen, unsigned char *from,
96                         unsigned char *to, RSA *rsa, int padding);
97
98     /* decrypt */
99     int (*rsa_priv_dec)(int flen, unsigned char *from,
100                         unsigned char *to, RSA *rsa, int padding);
101
102     /* compute r0 = r0 ^ I mod rsa->n (May be NULL for some implementations) */
103     int (*rsa_mod_exp)(BIGNUM *r0, BIGNUM *I, RSA *rsa);
104
105     /* compute r = a ^ p mod m (May be NULL for some implementations) */
106     int (*bn_mod_exp)(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
107                       const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
108
109     /* called at RSA_new */
110     int (*init)(RSA *rsa);
111
112     /* called at RSA_free */
113     int (*finish)(RSA *rsa);
114
115     /*
116      * RSA_FLAG_EXT_PKEY        - rsa_mod_exp is called for private key
117      *                            operations, even if p,q,dmp1,dmq1,iqmp
118      *                            are NULL
119      * RSA_METHOD_FLAG_NO_CHECK - don't check pub/private match
120      */
121     int flags;
122
123     char *app_data; /* ?? */
124
125     int (*rsa_sign)(int type,
126                     const unsigned char *m, unsigned int m_length,
127                     unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
128     int (*rsa_verify)(int dtype,
129                       const unsigned char *m, unsigned int m_length,
130                       const unsigned char *sigbuf, unsigned int siglen,
131                       const RSA *rsa);
132     /* keygen. If NULL builtin RSA key generation will be used */
133     int (*rsa_keygen)(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
134
135 } RSA_METHOD;
136
137=head1 RETURN VALUES
138
139RSA_PKCS1_OpenSSL(), RSA_PKCS1_null_method(), RSA_get_default_method()
140and RSA_get_method() return pointers to the respective RSA_METHODs.
141
142RSA_set_default_method() returns no value.
143
144RSA_set_method() returns a pointer to the old RSA_METHOD implementation
145that was replaced. However, this return value should probably be ignored
146because if it was supplied by an ENGINE, the pointer could be invalidated
147at any time if the ENGINE is unloaded (in fact it could be unloaded as a
148result of the RSA_set_method() function releasing its handle to the
149ENGINE). For this reason, the return type may be replaced with a B<void>
150declaration in a future release.
151
152RSA_new_method() returns NULL and sets an error code that can be obtained
153by L<ERR_get_error(3)> if the allocation fails. Otherwise
154it returns a pointer to the newly allocated structure.
155
156=head1 BUGS
157
158The behaviour of RSA_flags() is a mis-feature that is left as-is for now
159to avoid creating compatibility problems. RSA functionality, such as the
160encryption functions, are controlled by the B<flags> value in the RSA key
161itself, not by the B<flags> value in the RSA_METHOD attached to the RSA key
162(which is what this function returns). If the flags element of an RSA key
163is changed, the changes will be honoured by RSA functionality but will not
164be reflected in the return value of the RSA_flags() function - in effect
165RSA_flags() behaves more like an RSA_default_flags() function (which does
166not currently exist).
167
168=head1 SEE ALSO
169
170L<RSA_new(3)>
171
172=head1 HISTORY
173
174The RSA_null_method(), which was a partial attempt to avoid patent issues,
175was replaced to always return NULL in OpenSSL 1.1.1.
176
177=head1 COPYRIGHT
178
179Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
180
181Licensed under the OpenSSL license (the "License").  You may not use
182this file except in compliance with the License.  You can obtain a copy
183in the file LICENSE in the source distribution or at
184L<https://www.openssl.org/source/license.html>.
185
186=cut
187