1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
.\" $OpenBSD: RSA_new.3,v 1.14 2019/06/10 14:58:48 schwarze Exp $
.\" OpenSSL doc/man3/RSA_new.pod 99d63d46 Oct 26 13:56:48 2016 -0400
.\" OpenSSL doc/crypto/rsa.pod 35d2e327 Jun 3 16:19:49 2016 -0400
.\"
.\" This file was written by Ulf Moeller <ulf@openssl.org>.
.\" Copyright (c) 2000, 2002, 2016 The OpenSSL Project. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\"
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\"
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in
.\" the documentation and/or other materials provided with the
.\" distribution.
.\"
.\" 3. All advertising materials mentioning features or use of this
.\" software must display the following acknowledgment:
.\" "This product includes software developed by the OpenSSL Project
.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
.\"
.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
.\" endorse or promote products derived from this software without
.\" prior written permission. For written permission, please contact
.\" openssl-core@openssl.org.
.\"
.\" 5. Products derived from this software may not be called "OpenSSL"
.\" nor may "OpenSSL" appear in their names without prior written
.\" permission of the OpenSSL Project.
.\"
.\" 6. Redistributions of any form whatsoever must retain the following
.\" acknowledgment:
.\" "This product includes software developed by the OpenSSL Project
.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
.\" OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: June 10 2019 $
.Dt RSA_NEW 3
.Os
.Sh NAME
.Nm RSA_new ,
.Nm RSA_up_ref ,
.Nm RSA_free
.Nd allocate and free RSA objects
.Sh SYNOPSIS
.In openssl/rsa.h
.Ft RSA *
.Fn RSA_new void
.Ft int
.Fo RSA_up_ref
.Fa "RSA *rsa"
.Fc
.Ft void
.Fo RSA_free
.Fa "RSA *rsa"
.Fc
.Sh DESCRIPTION
The RSA functions implement RSA public key encryption and signatures
as defined in PKCS #1 v2.0 (RFC 2437).
.Pp
.Fn RSA_new
allocates and initializes an
.Vt RSA
structure, setting the reference count to 1.
It is equivalent to calling
.Xr RSA_new_method 3
with a
.Dv NULL
argument.
.Pp
.Fn RSA_up_ref
increments the reference count by 1.
.Pp
.Fn RSA_free
decrements the reference count by 1.
If it reaches 0, it calls the optional
.Fa finish
function set up with
.Xr RSA_meth_set_finish 3 ,
calls
.Xr ENGINE_finish 3
if
.Fa rsa
uses an engine, and frees the
.Vt RSA
structure and its components.
The key is erased before the memory is returned to the system.
If
.Fa rsa
is a
.Dv NULL
pointer, no action occurs.
.Pp
The
.Vt RSA
structure consists of several
.Vt BIGNUM
components.
It can contain public as well as private RSA keys:
.Bd -literal
typedef struct {
BIGNUM *n; // public modulus
BIGNUM *e; // public exponent
BIGNUM *d; // private exponent
BIGNUM *p; // secret prime factor
BIGNUM *q; // secret prime factor
BIGNUM *dmp1; // d mod (p-1)
BIGNUM *dmq1; // d mod (q-1)
BIGNUM *iqmp; // q^-1 mod p
// ...
} RSA;
.Ed
.Pp
In public keys, the private exponent
.Fa d
and the related secret values
.Fa p , q , dmp1 , dmp2 ,
and
.Fa iqmp
are
.Dv NULL .
.Pp
.Fa p ,
.Fa q ,
.Fa dmp1 ,
.Fa dmq1 ,
and
.Fa iqmp
may be
.Dv NULL
in private keys, but the RSA operations are much faster when these
values are available.
.Pp
Note that RSA keys may use non-standard
.Vt RSA_METHOD
implementations, either directly or by the use of
.Vt ENGINE
modules.
In some cases (e.g. an
.Vt ENGINE
providing support for hardware-embedded keys), these
.Vt BIGNUM
values will not be used by the implementation or may be used for
alternative data storage.
For this reason, applications should generally avoid using
.Vt RSA
structure elements directly and instead use API functions to query
or modify keys.
.Sh RETURN VALUES
If the allocation fails,
.Fn RSA_new
returns
.Dv NULL
and sets an error code that can be obtained by
.Xr ERR_get_error 3 .
Otherwise it returns a pointer to the newly allocated structure.
.Pp
.Fn RSA_up_ref
returns 1 for success or 0 for failure.
.Sh SEE ALSO
.Xr BN_new 3 ,
.Xr crypto 3 ,
.Xr d2i_RSAPublicKey 3 ,
.Xr DH_new 3 ,
.Xr DSA_new 3 ,
.Xr EVP_PKEY_set1_RSA 3 ,
.Xr RSA_blinding_on 3 ,
.Xr RSA_check_key 3 ,
.Xr RSA_generate_key 3 ,
.Xr RSA_get0_key 3 ,
.Xr RSA_get_ex_new_index 3 ,
.Xr RSA_meth_new 3 ,
.Xr RSA_padding_add_PKCS1_type_1 3 ,
.Xr RSA_print 3 ,
.Xr RSA_private_encrypt 3 ,
.Xr RSA_PSS_PARAMS_new 3 ,
.Xr RSA_public_encrypt 3 ,
.Xr RSA_set_method 3 ,
.Xr RSA_sign 3 ,
.Xr RSA_sign_ASN1_OCTET_STRING 3 ,
.Xr RSA_size 3
.Sh STANDARDS
SSL, PKCS #1 v2.0
.Pp
RSA was covered by a US patent which expired in September 2000.
.Sh HISTORY
.Fn RSA_new
and
.Fn RSA_free
appeared in SSLeay 0.4 or earlier and have been available since
.Ox 2.4 .
.Pp
.Fn RSA_up_ref
first appeared in OpenSSL 0.9.7 and has been available since
.Ox 3.2 .
|