Command docs: diverse small fixes
[openssl.git] / doc / man1 / openssl-pkeyutl.pod
1 =pod
2
3 =head1 NAME
4
5 openssl-pkeyutl - public key algorithm utility
6
7 =head1 SYNOPSIS
8
9 B<openssl> B<pkeyutl>
10 [B<-help>]
11 [B<-in> I<file>]
12 [B<-rawin>]
13 [B<-digest> I<algorithm>]
14 [B<-out> I<file>]
15 [B<-sigfile> I<file>]
16 [B<-inkey> I<file>]
17 [B<-keyform> B<DER>|B<PEM>|B<ENGINE>]
18 [B<-passin> I<arg>]
19 [B<-peerkey> I<file>]
20 [B<-peerform> B<DER>|B<PEM>|B<ENGINE>]
21 [B<-pubin>]
22 [B<-certin>]
23 [B<-rev>]
24 [B<-sign>]
25 [B<-verify>]
26 [B<-verifyrecover>]
27 [B<-encrypt>]
28 [B<-decrypt>]
29 [B<-derive>]
30 [B<-kdf> I<algorithm>]
31 [B<-kdflen> I<length>]
32 [B<-pkeyopt> I<opt>:I<value>]
33 [B<-pkeyopt_passin> I<opt>[:I<passarg>]]
34 [B<-hexdump>]
35 [B<-asn1parse>]
36 [B<-rand> I<files>]
37 [B<-writerand> I<file>]
38 [B<-engine> I<id>]
39 [B<-engine_impl>]
40
41 =for comment ifdef engine engine_impl
42
43 =head1 DESCRIPTION
44
45 This command can be used to perform low level public key
46 operations using any supported algorithm.
47
48 =head1 OPTIONS
49
50 =over 4
51
52 =item B<-help>
53
54 Print out a usage message.
55
56 =item B<-in> I<filename>
57
58 This specifies the input filename to read data from or standard input
59 if this option is not specified.
60
61 =item B<-rawin>
62
63 This indicates that the input data is raw data, which is not hashed by any
64 message digest algorithm. The user can specify a digest algorithm by using
65 the B<-digest> option. This option can only be used with B<-sign> and
66 B<-verify> and must be used with the Ed25519 and Ed448 algorithms.
67
68 =item B<-digest> I<algorithm>
69
70 This specifies the digest algorithm which is used to hash the input data before
71 signing or verifying it with the input key. This option could be omitted if the
72 signature algorithm does not require one (for instance, EdDSA). If this option
73 is omitted but the signature algorithm requires one, a default value will be
74 used. For signature algorithms like RSA, DSA and ECDSA, SHA-256 will be the
75 default digest algorithm. For SM2, it will be SM3. If this option is present,
76 then the B<-rawin> option must be also specified.
77
78 =item B<-out> I<filename>
79
80 Specifies the output filename to write to or standard output by
81 default.
82
83 =item B<-sigfile> I<file>
84
85 Signature file, required for B<-verify> operations only
86
87 =item B<-inkey> I<file>
88
89 The input key file, by default it should be a private key.
90
91 =item B<-keyform> B<DER>|B<PEM>|B<ENGINE>
92
93 The key format PEM, DER or ENGINE. Default is PEM.
94
95 =item B<-passin> I<arg>
96
97 The input key password source. For more information about the format of I<arg>
98 see L<openssl(1)/Pass phrase options>.
99
100 =item B<-peerkey> I<file>
101
102 The peer key file, used by key derivation (agreement) operations.
103
104 =item B<-peerform> B<DER>|B<PEM>|B<ENGINE>
105
106 The peer key format B<PEM>, B<DER> or B<ENGINE>. Default is B<PEM>.
107
108 =item B<-pubin>
109
110 The input file is a public key.
111
112 =item B<-certin>
113
114 The input is a certificate containing a public key.
115
116 =item B<-rev>
117
118 Reverse the order of the input buffer. This is useful for some libraries
119 (such as CryptoAPI) which represent the buffer in little endian format.
120
121 =item B<-sign>
122
123 Sign the input data (which must be a hash) and output the signed result. This
124 requires a private key.
125
126 =item B<-verify>
127
128 Verify the input data (which must be a hash) against the signature file and
129 indicate if the verification succeeded or failed.
130
131 =item B<-verifyrecover>
132
133 Verify the input data (which must be a hash) and output the recovered data.
134
135 =item B<-encrypt>
136
137 Encrypt the input data using a public key.
138
139 =item B<-decrypt>
140
141 Decrypt the input data using a private key.
142
143 =item B<-derive>
144
145 Derive a shared secret using the peer key.
146
147 =item B<-kdf> I<algorithm>
148
149 Use key derivation function I<algorithm>.  The supported algorithms are
150 at present B<TLS1-PRF> and B<HKDF>.
151 Note: additional parameters and the KDF output length will normally have to be
152 set for this to work.
153 See L<EVP_PKEY_CTX_set_hkdf_md(3)> and L<EVP_PKEY_CTX_set_tls1_prf_md(3)>
154 for the supported string parameters of each algorithm.
155
156 =item B<-kdflen> I<length>
157
158 Set the output length for KDF.
159
160 =item B<-pkeyopt> I<opt>:I<value>
161
162 Public key options specified as opt:value. See NOTES below for more details.
163
164 =item B<-pkeyopt_passin> I<opt>[:I<passarg>]
165
166 Allows reading a public key option I<opt> from stdin or a password source.
167 If only I<opt> is specified, the user will be prompted to enter a password on
168 stdin.  Alternatively, I<passarg> can be specified which can be any value
169 supported by L<openssl(1)/Pass phrase options>.
170
171 =item B<-hexdump>
172
173 hex dump the output data.
174
175 =item B<-asn1parse>
176
177 Parse the ASN.1 output data, this is useful when combined with the
178 B<-verifyrecover> option when an ASN1 structure is signed.
179
180 =item B<-rand> I<files>
181
182 The files containing random data used to seed the random number generator.
183 Multiple files can be specified separated by an OS-dependent character.
184 The separator is B<;> for MS-Windows, B<,> for OpenVMS, and B<:> for
185 all others.
186
187 =item B<-writerand> I<file>
188
189 Writes random data to the specified I<file> upon exit.
190 This can be used with a subsequent B<-rand> flag.
191
192 =item B<-engine> I<id>
193
194 Specifying an engine (by its unique I<id> string) will cause this command
195 to attempt to obtain a functional reference to the specified engine,
196 thus initialising it if needed. The engine will then be set as the default
197 for all available algorithms.
198
199 =item B<-engine_impl>
200
201 When used with the B<-engine> option, it specifies to also use
202 engine I<id> for crypto operations.
203
204 =back
205
206 =head1 NOTES
207
208 The operations and options supported vary according to the key algorithm
209 and its implementation. The OpenSSL operations and options are indicated below.
210
211 Unless otherwise mentioned all algorithms support the B<digest:>I<alg> option
212 which specifies the digest in use for sign, verify and verifyrecover operations.
213 The value I<alg> should represent a digest name as used in the
214 EVP_get_digestbyname() function for example B<sha1>. This value is not used to
215 hash the input data. It is used (by some algorithms) for sanity-checking the
216 lengths of data passed in and for creating the structures that make up the
217 signature (e.g. B<DigestInfo> in RSASSA PKCS#1 v1.5 signatures).
218
219 This command does not hash the input data (except where -rawin is used) but
220 rather it will use the data directly as input to the signature algorithm.
221 Depending on the key type, signature type, and mode of padding, the maximum
222 acceptable lengths of input data differ. The signed data can't be longer than
223 the key modulus with RSA. In case of ECDSA and DSA the data shouldn't be longer
224 than the field size, otherwise it will be silently truncated to the field size.
225 In any event the input size must not be larger than the largest supported digest
226 size.
227
228 In other words, if the value of digest is B<sha1> the input should be the 20
229 bytes long binary encoding of the SHA-1 hash function output.
230
231 =head1 RSA ALGORITHM
232
233 The RSA algorithm generally supports the encrypt, decrypt, sign,
234 verify and verifyrecover operations. However, some padding modes
235 support only a subset of these operations. The following additional
236 B<pkeyopt> values are supported:
237
238 =over 4
239
240 =item B<rsa_padding_mode:>I<mode>
241
242 This sets the RSA padding mode. Acceptable values for I<mode> are B<pkcs1> for
243 PKCS#1 padding, B<sslv23> for SSLv23 padding, B<none> for no padding, B<oaep>
244 for B<OAEP> mode, B<x931> for X9.31 mode and B<pss> for PSS.
245
246 In PKCS#1 padding if the message digest is not set then the supplied data is
247 signed or verified directly instead of using a B<DigestInfo> structure. If a
248 digest is set then the a B<DigestInfo> structure is used and its the length
249 must correspond to the digest type.
250
251 For B<oaep> mode only encryption and decryption is supported.
252
253 For B<x931> if the digest type is set it is used to format the block data
254 otherwise the first byte is used to specify the X9.31 digest ID. Sign,
255 verify and verifyrecover are can be performed in this mode.
256
257 For B<pss> mode only sign and verify are supported and the digest type must be
258 specified.
259
260 =item B<rsa_pss_saltlen:>I<len>
261
262 For B<pss> mode only this option specifies the salt length. Three special
263 values are supported: B<digest> sets the salt length to the digest length,
264 B<max> sets the salt length to the maximum permissible value. When verifying
265 B<auto> causes the salt length to be automatically determined based on the
266 B<PSS> block structure.
267
268 =item B<rsa_mgf1_md:>I<digest>
269
270 For PSS and OAEP padding sets the MGF1 digest. If the MGF1 digest is not
271 explicitly set in PSS mode then the signing digest is used.
272
273 =back
274
275 =head1 RSA-PSS ALGORITHM
276
277 The RSA-PSS algorithm is a restricted version of the RSA algorithm which only
278 supports the sign and verify operations with PSS padding. The following
279 additional B<-pkeyopt> values are supported:
280
281 =over 4
282
283 =item B<rsa_padding_mode:>I<mode>, B<rsa_pss_saltlen:>I<len>,
284 B<rsa_mgf1_md:>I<digest>
285
286 These have the same meaning as the B<RSA> algorithm with some additional
287 restrictions. The padding mode can only be set to B<pss> which is the
288 default value.
289
290 If the key has parameter restrictions than the digest, MGF1
291 digest and salt length are set to the values specified in the parameters.
292 The digest and MG cannot be changed and the salt length cannot be set to a
293 value less than the minimum restriction.
294
295 =back
296
297 =head1 DSA ALGORITHM
298
299 The DSA algorithm supports signing and verification operations only. Currently
300 there are no additional B<-pkeyopt> options other than B<digest>. The SHA1
301 digest is assumed by default.
302
303 =head1 DH ALGORITHM
304
305 The DH algorithm only supports the derivation operation and no additional
306 B<-pkeyopt> options.
307
308 =head1 EC ALGORITHM
309
310 The EC algorithm supports sign, verify and derive operations. The sign and
311 verify operations use ECDSA and derive uses ECDH. SHA1 is assumed by default for
312 the B<-pkeyopt> B<digest> option.
313
314 =head1 X25519 AND X448 ALGORITHMS
315
316 The X25519 and X448 algorithms support key derivation only. Currently there are
317 no additional options.
318
319 =head1 ED25519 AND ED448 ALGORITHMS
320
321 These algorithms only support signing and verifying. OpenSSL only implements the
322 "pure" variants of these algorithms so raw data can be passed directly to them
323 without hashing them first. The option B<-rawin> must be used with these
324 algorithms with no B<-digest> specified. Additionally OpenSSL only supports
325 "oneshot" operation with these algorithms. This means that the entire file to
326 be signed/verified must be read into memory before processing it. Signing or
327 Verifying very large files should be avoided. Additionally the size of the file
328 must be known for this to work. If the size of the file cannot be determined
329 (for example if the input is stdin) then the sign or verify operation will fail.
330
331 =head1 SM2
332
333 The SM2 algorithm supports sign, verify, encrypt and decrypt operations. For
334 the sign and verify operations, SM2 requires an ID string to be passed in. The
335 following B<-pkeyopt> value is supported:
336
337 =over 4
338
339 =item B<sm2_id:>I<string>
340
341 This sets the ID string used in SM2 sign or verify operations. While verifying
342 an SM2 signature, the ID string must be the same one used when signing the data.
343 Otherwise the verification will fail.
344
345 =item B<sm2_hex_id:>I<hex_string>
346
347 This sets the ID string used in SM2 sign or verify operations. While verifying
348 an SM2 signature, the ID string must be the same one used when signing the data.
349 Otherwise the verification will fail. The ID string provided with this option
350 should be a valid hexadecimal value.
351
352 =back
353
354 =head1 EXAMPLES
355
356 Sign some data using a private key:
357
358  openssl pkeyutl -sign -in file -inkey key.pem -out sig
359
360 Recover the signed data (e.g. if an RSA key is used):
361
362  openssl pkeyutl -verifyrecover -in sig -inkey key.pem
363
364 Verify the signature (e.g. a DSA key):
365
366  openssl pkeyutl -verify -in file -sigfile sig -inkey key.pem
367
368 Sign data using a message digest value (this is currently only valid for RSA):
369
370  openssl pkeyutl -sign -in file -inkey key.pem -out sig -pkeyopt digest:sha256
371
372 Derive a shared secret value:
373
374  openssl pkeyutl -derive -inkey key.pem -peerkey pubkey.pem -out secret
375
376 Hexdump 48 bytes of TLS1 PRF using digest B<SHA256> and shared secret and
377 seed consisting of the single byte 0xFF:
378
379  openssl pkeyutl -kdf TLS1-PRF -kdflen 48 -pkeyopt md:SHA256 \
380     -pkeyopt hexsecret:ff -pkeyopt hexseed:ff -hexdump
381
382 Derive a key using B<scrypt> where the password is read from command line:
383
384  openssl pkeyutl -kdf scrypt -kdflen 16 -pkeyopt_passin pass \
385     -pkeyopt hexsalt:aabbcc -pkeyopt N:16384 -pkeyopt r:8 -pkeyopt p:1
386
387 Derive using the same algorithm, but read key from environment variable MYPASS:
388
389  openssl pkeyutl -kdf scrypt -kdflen 16 -pkeyopt_passin pass:env:MYPASS \
390     -pkeyopt hexsalt:aabbcc -pkeyopt N:16384 -pkeyopt r:8 -pkeyopt p:1
391
392 Sign some data using an L<SM2(7)> private key and a specific ID:
393
394  openssl pkeyutl -sign -in file -inkey sm2.key -out sig -rawin -digest sm3 \
395     -pkeyopt sm2_id:someid
396
397 Verify some data using an L<SM2(7)> certificate and a specific ID:
398
399  openssl pkeyutl -verify -certin -in file -inkey sm2.cert -sigfile sig \
400     -rawin -digest sm3 -pkeyopt sm2_id:someid
401
402 =head1 SEE ALSO
403
404 L<openssl(1)>,
405 L<openssl-genpkey(1)>,
406 L<openssl-pkey(1)>,
407 L<openssl-rsautl(1)>
408 L<openssl-dgst(1)>,
409 L<openssl-rsa(1)>,
410 L<openssl-genrsa(1)>,
411 L<openssl-kdf(1)>
412 L<EVP_PKEY_CTX_set_hkdf_md(3)>,
413 L<EVP_PKEY_CTX_set_tls1_prf_md(3)>,
414
415
416 =head1 COPYRIGHT
417
418 Copyright 2006-2019 The OpenSSL Project Authors. All Rights Reserved.
419
420 Licensed under the Apache License 2.0 (the "License").  You may not use
421 this file except in compliance with the License.  You can obtain a copy
422 in the file LICENSE in the source distribution or at
423 L<https://www.openssl.org/source/license.html>.
424
425 =cut