xref: /openssl/doc/man1/openssl-pkeyutl.pod.in (revision 012353bd)
1=pod
2{- OpenSSL::safe::output_do_not_edit_headers(); -}
3
4=head1 NAME
5
6openssl-pkeyutl - public key algorithm command
7
8=head1 SYNOPSIS
9
10B<openssl> B<pkeyutl>
11[B<-help>]
12[B<-in> I<file>]
13[B<-rawin>]
14[B<-digest> I<algorithm>]
15[B<-out> I<file>]
16[B<-secret> I<file>]
17[B<-sigfile> I<file>]
18[B<-inkey> I<filename>|I<uri>]
19[B<-keyform> B<DER>|B<PEM>|B<P12>|B<ENGINE>]
20[B<-passin> I<arg>]
21[B<-peerkey> I<file>]
22[B<-peerform> B<DER>|B<PEM>|B<P12>|B<ENGINE>]
23[B<-pubin>]
24[B<-certin>]
25[B<-rev>]
26[B<-sign>]
27[B<-verify>]
28[B<-verifyrecover>]
29[B<-encrypt>]
30[B<-decrypt>]
31[B<-derive>]
32[B<-encap>]
33[B<-decap>]
34[B<-kdf> I<algorithm>]
35[B<-kdflen> I<length>]
36[B<-kemop> I<operation>]
37[B<-pkeyopt> I<opt>:I<value>]
38[B<-pkeyopt_passin> I<opt>[:I<passarg>]]
39[B<-hexdump>]
40[B<-asn1parse>]
41{- $OpenSSL::safe::opt_engine_synopsis -}[B<-engine_impl>]
42{- $OpenSSL::safe::opt_r_synopsis -}
43{- $OpenSSL::safe::opt_provider_synopsis -}
44{- $OpenSSL::safe::opt_config_synopsis -}
45
46=head1 DESCRIPTION
47
48This command can be used to perform low-level public key
49operations using any supported algorithm.
50
51By default the signing operation (see B<-sign> option) is assumed.
52
53=head1 OPTIONS
54
55=over 4
56
57=item B<-help>
58
59Print out a usage message.
60
61=item B<-in> I<filename>
62
63This specifies the input filename to read data from or standard input
64if this option is not specified.
65
66=item B<-rawin>
67
68This indicates that the signature or verification input data is raw data,
69which is not hashed by any message digest algorithm.
70Except with EdDSA,
71the user can specify a digest algorithm by using the B<-digest> option.
72For signature algorithms like RSA, DSA and ECDSA,
73the default digest algorithm is SHA-256. For SM2, it is SM3.
74
75This option can only be used with B<-sign> and B<-verify>.
76For EdDSA (the Ed25519 and Ed448 algorithms) this option
77is implied since OpenSSL 3.5, and required in earlier versions.
78
79The B<-digest> option implies B<-rawin> since OpenSSL 3.5.
80
81=item B<-digest> I<algorithm>
82
83This option can only be used with B<-sign> and B<-verify>.
84It specifies the digest algorithm that is used to hash the input data
85before signing or verifying it with the input key. This option could be omitted
86if the signature algorithm does not require preprocessing the input through
87a pluggable hash function before signing (for instance, EdDSA). If this option
88is omitted but the signature algorithm requires one and the B<-rawin> option
89is given, a default value will be used (see B<-rawin> for details).
90If this option is present, then the B<-rawin> option
91is implied since OpenSSL 3.5, and required in earlier versions.
92
93At this time, HashEdDSA (the ph or "prehash" variant of EdDSA) is not supported,
94so the B<-digest> option cannot be used with EdDSA.
95
96=item B<-out> I<filename>
97
98Specifies the output filename to write to or standard output by
99default.
100
101=item B<-secret> I<filename>
102
103Specifies the output filename to write the secret to on I<-encap>.
104
105=item B<-sigfile> I<file>
106
107Signature file, required and allowed for B<-verify> operations only
108
109=item B<-inkey> I<filename>|I<uri>
110
111The input key, by default it should be a private key.
112
113=item B<-keyform> B<DER>|B<PEM>|B<P12>|B<ENGINE>
114
115The key format; unspecified by default.
116See L<openssl-format-options(1)> for details.
117
118=item B<-passin> I<arg>
119
120The input key password source. For more information about the format of I<arg>
121see L<openssl-passphrase-options(1)>.
122
123=item B<-peerkey> I<file>
124
125The peer key file, used by key derivation (agreement) operations.
126
127=item B<-peerform> B<DER>|B<PEM>|B<P12>|B<ENGINE>
128
129The peer key format; unspecified by default.
130See L<openssl-format-options(1)> for details.
131
132=item B<-pubin>
133
134By default a private key is read from the key input.
135With this option a public key is read instead.
136If the input contains no public key but a private key, its public part is used.
137
138=item B<-certin>
139
140The input is a certificate containing a public key.
141
142=item B<-rev>
143
144Reverse the order of the input buffer. This is useful for some libraries
145(such as CryptoAPI) which represent the buffer in little-endian format.
146This cannot be used in conjunction with B<-rawin>.
147
148=item B<-sign>
149
150Sign the input data and output the signed result. This requires a private key.
151Using a message digest operation along with this is recommended,
152when applicable, see the B<-rawin> and B<-digest> options for details.
153Otherwise, the input data given with the B<-in> option is assumed to already
154be a digest, but this may then require an additional B<-pkeyopt> C<digest:>I<md>
155in some cases (e.g., RSA with the default PKCS#1 padding mode).
156Even for other algorithms like ECDSA, where the additional B<-pkeyopt> option
157does not affect signature output, it is recommended, as it enables
158checking that the input length is consistent with the intended digest.
159
160=item B<-verify>
161
162Verify the input data against the signature given with the B<-sigfile> option
163and indicate if the verification succeeded or failed.
164The input data given with the B<-in> option is assumed to be a hash value
165unless the B<-rawin> option is specified or implied.
166With raw data, when a digest algorithm is applicable, though it may be inferred
167from the signature or take a default value, it should also be specified.
168
169=item B<-verifyrecover>
170
171Verify the given signature and output the recovered data (signature payload).
172For example, in case of RSA PKCS#1 the recovered data is the B<EMSA-PKCS-v1_5>
173DER encoding of the digest algorithm OID and value as specified in
174L<RFC8017 Section 9.2|https://datatracker.ietf.org/doc/html/rfc8017#section-9.2>.
175
176Note that here the input given with the B<-in> option is not a signature input
177(as with the B<-sign> and B<-verify> options) but a signature output value,
178typically produced using the B<-sign> option.
179
180This option is available only for use with RSA keys.
181
182=item B<-encrypt>
183
184Encrypt the input data using a public key.
185
186=item B<-decrypt>
187
188Decrypt the input data using a private key.
189
190=item B<-derive>
191
192Derive a shared secret using the peer key.
193
194=item B<-encap>
195
196Encapsulate a generated secret using a private key.
197The encapsulated result (binary data) is written to standard output by default,
198or else to the file specified with I<-out>.
199The I<-secret> option must also be provided to specify the output file for the
200secret value generated in the encapsulation process.
201
202=item B<-decap>
203
204Decapsulate the secret using a private key.
205The result (binary data) is written to standard output by default, or else to
206the file specified with I<-out>.
207
208=item B<-kemop> I<operation>
209
210This option is used for I<-encap>/I<-decap> commands and specifies the KEM
211operation specific for the key algorithm when there is no default KEM
212operation.
213If the algorithm has the default KEM operation, this option can be omitted.
214
215See L<EVP_PKEY_CTX_set_kem_op(3)> and algorithm-specific KEM documentation e.g.
216L<EVP_KEM-RSA(7)>, L<EVP_KEM-EC(7)>, L<EVP_KEM-X25519(7)>, and
217L<EVP_KEM-X448(7)>.
218
219=item B<-kdf> I<algorithm>
220
221Use key derivation function I<algorithm>.  The supported algorithms are
222at present B<TLS1-PRF> and B<HKDF>.
223Note: additional parameters and the KDF output length will normally have to be
224set for this to work.
225See L<EVP_PKEY_CTX_set_hkdf_md(3)> and L<EVP_PKEY_CTX_set_tls1_prf_md(3)>
226for the supported string parameters of each algorithm.
227
228=item B<-kdflen> I<length>
229
230Set the output length for KDF.
231
232=item B<-pkeyopt> I<opt>:I<value>
233
234Public key options specified as opt:value. See NOTES below for more details.
235
236=item B<-pkeyopt_passin> I<opt>[:I<passarg>]
237
238Allows reading a public key option I<opt> from stdin or a password source.
239If only I<opt> is specified, the user will be prompted to enter a password on
240stdin.  Alternatively, I<passarg> can be specified which can be any value
241supported by L<openssl-passphrase-options(1)>.
242
243=item B<-hexdump>
244
245hex dump the output data.
246
247=item B<-asn1parse>
248
249Parse the ASN.1 output data to check its DER encoding and print any errors.
250When combined with the B<-verifyrecover> option, this may be useful only in case
251an ASN.1 DER-encoded structure had been signed directly (without hashing it).
252
253{- $OpenSSL::safe::opt_engine_item -}
254
255{- output_off() if $disabled{"deprecated-3.0"}; "" -}
256=item B<-engine_impl>
257
258When used with the B<-engine> option, it specifies to also use
259engine I<id> for crypto operations.
260{- output_on() if $disabled{"deprecated-3.0"}; "" -}
261
262{- $OpenSSL::safe::opt_r_item -}
263
264{- $OpenSSL::safe::opt_provider_item -}
265
266{- $OpenSSL::safe::opt_config_item -}
267
268=back
269
270=head1 NOTES
271
272The operations and options supported vary according to the key algorithm
273and its implementation. The OpenSSL operations and options are indicated below.
274
275Unless otherwise mentioned, all algorithms support the B<digest:>I<alg> option,
276which specifies the digest in use for the signing and verification operations.
277The value I<alg> should represent a digest name as used in the
278EVP_get_digestbyname() function for example B<sha1>. This value is not used to
279hash the input data. It is used (by some algorithms) for sanity-checking the
280lengths of data passed in and for creating the structures that make up the
281signature (e.g. B<DigestInfo> in RSASSA PKCS#1 v1.5 signatures).
282
283This command does not hash the input data (except where -rawin is used) but
284rather it will use the data directly as input to the signature algorithm.
285Depending on the key type, signature type, and mode of padding, the maximum
286acceptable lengths of input data differ. The signed data can't be longer than
287the key modulus with RSA. In case of ECDSA and DSA the data shouldn't be longer
288than the field size, otherwise it will be silently truncated to the field size.
289In any event the input size must not be larger than the largest supported digest
290size.
291
292In other words, if the value of digest is B<sha1> the input should be the 20
293bytes long binary encoding of the SHA-1 hash function output.
294
295=head1 RSA ALGORITHM
296
297The RSA algorithm generally supports the encrypt, decrypt, sign,
298verify and verifyrecover operations. However, some padding modes
299support only a subset of these operations. The following additional
300B<pkeyopt> values are supported:
301
302=over 4
303
304=item B<rsa_padding_mode:>I<mode>
305
306This sets the RSA padding mode. Acceptable values for I<mode> are B<pkcs1> for
307PKCS#1 padding, B<none> for no padding, B<oaep>
308for B<OAEP> mode, B<x931> for X9.31 mode and B<pss> for PSS.
309
310In PKCS#1 padding, if the message digest is not set, then the supplied data is
311signed or verified directly instead of using a B<DigestInfo> structure. If a
312digest is set, then the B<DigestInfo> structure is used and its length
313must correspond to the digest type.
314
315Note, for B<pkcs1> padding, as a protection against the Bleichenbacher attack,
316the decryption will not fail in case of padding check failures. Use B<none>
317and manual inspection of the decrypted message to verify if the decrypted
318value has correct PKCS#1 v1.5 padding.
319
320For B<oaep> mode only encryption and decryption is supported.
321
322For B<x931> if the digest type is set it is used to format the block data
323otherwise the first byte is used to specify the X9.31 digest ID. Sign,
324verify and verifyrecover are can be performed in this mode.
325
326For B<pss> mode only sign and verify are supported and the digest type must be
327specified.
328
329=item B<rsa_pss_saltlen:>I<len>
330
331For B<pss> mode only this option specifies the salt length. Three special
332values are supported: B<digest> sets the salt length to the digest length,
333B<max> sets the salt length to the maximum permissible value. When verifying
334B<auto> causes the salt length to be automatically determined based on the
335B<PSS> block structure.
336
337=item B<rsa_mgf1_md:>I<digest>
338
339For PSS and OAEP padding sets the MGF1 digest. If the MGF1 digest is not
340explicitly set in PSS mode then the signing digest is used.
341
342=item B<rsa_oaep_md:>I<digest>
343
344Sets the digest used for the OAEP hash function. If not explicitly set then
345SHA1 is used.
346
347=item B<rsa_pkcs1_implicit_rejection:>I<flag>
348
349Disables (when set to 0) or enables (when set to 1) the use of implicit
350rejection with PKCS#1 v1.5 decryption. When enabled (the default), as a
351protection against Bleichenbacher attack, the library will generate a
352deterministic random plaintext that it will return to the caller in case
353of padding check failure.
354When disabled, it's the callers' responsibility to handle the returned
355errors in a side-channel free manner.
356
357=back
358
359=head1 RSA-PSS ALGORITHM
360
361The RSA-PSS algorithm is a restricted version of the RSA algorithm which only
362supports the sign and verify operations with PSS padding. The following
363additional B<-pkeyopt> values are supported:
364
365=over 4
366
367=item B<rsa_padding_mode:>I<mode>, B<rsa_pss_saltlen:>I<len>,
368B<rsa_mgf1_md:>I<digest>
369
370These have the same meaning as the B<RSA> algorithm with some additional
371restrictions. The padding mode can only be set to B<pss> which is the
372default value.
373
374If the key has parameter restrictions then the digest, MGF1
375digest and salt length are set to the values specified in the parameters.
376The digest and MG cannot be changed and the salt length cannot be set to a
377value less than the minimum restriction.
378
379=back
380
381=head1 DSA ALGORITHM
382
383The DSA algorithm supports signing and verification operations only. Currently
384there are no additional B<-pkeyopt> options other than B<digest>. The SHA1
385digest is assumed by default.
386
387=head1 DH ALGORITHM
388
389The DH algorithm only supports the derivation operation and no additional
390B<-pkeyopt> options.
391
392=head1 EC ALGORITHM
393
394The EC algorithm supports sign, verify and derive operations. The sign and
395verify operations use ECDSA and derive uses ECDH. SHA1 is assumed by default for
396the B<-pkeyopt> B<digest> option.
397
398=head1 X25519 AND X448 ALGORITHMS
399
400The X25519 and X448 algorithms support key derivation only. Currently there are
401no additional options.
402
403=head1 ED25519 AND ED448 ALGORITHMS
404
405These algorithms only support signing and verifying. OpenSSL only implements the
406"pure" variants of these algorithms so raw data can be passed directly to them
407without hashing them first. The option B<-rawin> must be used with these
408algorithms with no B<-digest> specified. Additionally OpenSSL only supports
409"oneshot" operation with these algorithms. This means that the entire file to
410be signed/verified must be read into memory before processing it. Signing or
411Verifying very large files should be avoided. Additionally the size of the file
412must be known for this to work. If the size of the file cannot be determined
413(for example if the input is stdin) then the sign or verify operation will fail.
414
415=head1 SM2
416
417The SM2 algorithm supports sign, verify, encrypt and decrypt operations. For
418the sign and verify operations, SM2 requires an Distinguishing ID string to
419be passed in. The following B<-pkeyopt> value is supported:
420
421=over 4
422
423=item B<distid:>I<string>
424
425This sets the ID string used in SM2 sign or verify operations. While verifying
426an SM2 signature, the ID string must be the same one used when signing the data.
427Otherwise the verification will fail.
428
429=item B<hexdistid:>I<hex_string>
430
431This sets the ID string used in SM2 sign or verify operations. While verifying
432an SM2 signature, the ID string must be the same one used when signing the data.
433Otherwise the verification will fail. The ID string provided with this option
434should be a valid hexadecimal value.
435
436=back
437
438=head1 EXAMPLES
439
440Sign some data using a private key:
441
442 openssl pkeyutl -sign -in file -inkey key.pem -out sig
443
444Recover the signed data (e.g. if an RSA key is used):
445
446 openssl pkeyutl -verifyrecover -in sig -inkey key.pem
447
448Verify the signature (e.g. a DSA key):
449
450 openssl pkeyutl -verify -in file -sigfile sig -inkey key.pem
451
452Sign data using a message digest value (this is currently only valid for RSA):
453
454 openssl pkeyutl -sign -in file -inkey key.pem -out sig -pkeyopt digest:sha256
455
456Derive a shared secret value:
457
458 openssl pkeyutl -derive -inkey key.pem -peerkey pubkey.pem -out secret
459
460Hexdump 48 bytes of TLS1 PRF using digest B<SHA256> and shared secret and
461seed consisting of the single byte 0xFF:
462
463 openssl pkeyutl -kdf TLS1-PRF -kdflen 48 -pkeyopt md:SHA256 \
464    -pkeyopt hexsecret:ff -pkeyopt hexseed:ff -hexdump
465
466Derive a key using B<scrypt> where the password is read from command line:
467
468 openssl pkeyutl -kdf scrypt -kdflen 16 -pkeyopt_passin pass \
469    -pkeyopt hexsalt:aabbcc -pkeyopt N:16384 -pkeyopt r:8 -pkeyopt p:1
470
471Derive using the same algorithm, but read key from environment variable MYPASS:
472
473 openssl pkeyutl -kdf scrypt -kdflen 16 -pkeyopt_passin pass:env:MYPASS \
474    -pkeyopt hexsalt:aabbcc -pkeyopt N:16384 -pkeyopt r:8 -pkeyopt p:1
475
476Sign some data using an L<SM2(7)> private key and a specific ID:
477
478 openssl pkeyutl -sign -in file -inkey sm2.key -out sig -rawin -digest sm3 \
479    -pkeyopt distid:someid
480
481Verify some data using an L<SM2(7)> certificate and a specific ID:
482
483 openssl pkeyutl -verify -certin -in file -inkey sm2.cert -sigfile sig \
484    -rawin -digest sm3 -pkeyopt distid:someid
485
486Decrypt some data using a private key with OAEP padding using SHA256:
487
488 openssl pkeyutl -decrypt -in file -inkey key.pem -out secret \
489    -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha256
490
491=head1 SEE ALSO
492
493L<openssl(1)>,
494L<openssl-genpkey(1)>,
495L<openssl-pkey(1)>,
496L<openssl-rsautl(1)>
497L<openssl-dgst(1)>,
498L<openssl-rsa(1)>,
499L<openssl-genrsa(1)>,
500L<openssl-kdf(1)>
501L<EVP_PKEY_CTX_set_hkdf_md(3)>,
502L<EVP_PKEY_CTX_set_tls1_prf_md(3)>,
503
504=head1 HISTORY
505
506Since OpenSSL 3.5,
507the B<-digest> option implies B<-rawin>, and these two options are
508no longer required when signing or verifying with an Ed25519 or Ed448 key.
509
510The B<-engine> option was deprecated in OpenSSL 3.0.
511
512=head1 COPYRIGHT
513
514Copyright 2006-2024 The OpenSSL Project Authors. All Rights Reserved.
515
516Licensed under the Apache License 2.0 (the "License").  You may not use
517this file except in compliance with the License.  You can obtain a copy
518in the file LICENSE in the source distribution or at
519L<https://www.openssl.org/source/license.html>.
520
521=cut
522