2 {- OpenSSL::safe::output_do_not_edit_headers(); -}
6 openssl-pkeyutl - public key algorithm command
14 [B<-digest> I<algorithm>]
17 [B<-inkey> I<filename>|I<uri>]
18 [B<-keyform> B<DER>|B<PEM>|B<P12>|B<ENGINE>]
21 [B<-peerform> B<DER>|B<PEM>|B<P12>|B<ENGINE>]
31 [B<-kdf> I<algorithm>]
32 [B<-kdflen> I<length>]
33 [B<-pkeyopt> I<opt>:I<value>]
34 [B<-pkeyopt_passin> I<opt>[:I<passarg>]]
37 {- $OpenSSL::safe::opt_engine_synopsis -}[B<-engine_impl>]
38 {- $OpenSSL::safe::opt_r_synopsis -}
39 {- $OpenSSL::safe::opt_provider_synopsis -}
40 {- $OpenSSL::safe::opt_config_synopsis -}
44 This command can be used to perform low-level public key
45 operations using any supported algorithm.
53 Print out a usage message.
55 =item B<-in> I<filename>
57 This specifies the input filename to read data from or standard input
58 if this option is not specified.
62 This indicates that the input data is raw data, which is not hashed by any
63 message digest algorithm. The user can specify a digest algorithm by using
64 the B<-digest> option. This option can only be used with B<-sign> and
65 B<-verify> and must be used with the Ed25519 and Ed448 algorithms.
67 =item B<-digest> I<algorithm>
69 This specifies the digest algorithm which is used to hash the input data before
70 signing or verifying it with the input key. This option could be omitted if the
71 signature algorithm does not require one (for instance, EdDSA). If this option
72 is omitted but the signature algorithm requires one, a default value will be
73 used. For signature algorithms like RSA, DSA and ECDSA, SHA-256 will be the
74 default digest algorithm. For SM2, it will be SM3. If this option is present,
75 then the B<-rawin> option must be also specified.
77 =item B<-out> I<filename>
79 Specifies the output filename to write to or standard output by
82 =item B<-sigfile> I<file>
84 Signature file, required for B<-verify> operations only
86 =item B<-inkey> I<filename>|I<uri>
88 The input key, by default it should be a private key.
90 =item B<-keyform> B<DER>|B<PEM>|B<P12>|B<ENGINE>
92 The key format; unspecified by default.
93 See L<openssl-format-options(1)> for details.
95 =item B<-passin> I<arg>
97 The input key password source. For more information about the format of I<arg>
98 see L<openssl-passphrase-options(1)>.
100 =item B<-peerkey> I<file>
102 The peer key file, used by key derivation (agreement) operations.
104 =item B<-peerform> B<DER>|B<PEM>|B<P12>|B<ENGINE>
106 The peer key format; unspecified by default.
107 See L<openssl-format-options(1)> for details.
111 By default a private key is read from the key input.
112 With this option a public key is read instead.
113 If the input contains no public key but a private key, its public part is used.
117 The input is a certificate containing a public key.
121 Reverse the order of the input buffer. This is useful for some libraries
122 (such as CryptoAPI) which represent the buffer in little endian format.
126 Sign the input data (which must be a hash) and output the signed result. This
127 requires a private key.
131 Verify the input data (which must be a hash) against the signature file and
132 indicate if the verification succeeded or failed.
134 =item B<-verifyrecover>
136 Verify the input data (which must be a hash) and output the recovered data.
140 Encrypt the input data using a public key.
144 Decrypt the input data using a private key.
148 Derive a shared secret using the peer key.
150 =item B<-kdf> I<algorithm>
152 Use key derivation function I<algorithm>. The supported algorithms are
153 at present B<TLS1-PRF> and B<HKDF>.
154 Note: additional parameters and the KDF output length will normally have to be
155 set for this to work.
156 See L<EVP_PKEY_CTX_set_hkdf_md(3)> and L<EVP_PKEY_CTX_set_tls1_prf_md(3)>
157 for the supported string parameters of each algorithm.
159 =item B<-kdflen> I<length>
161 Set the output length for KDF.
163 =item B<-pkeyopt> I<opt>:I<value>
165 Public key options specified as opt:value. See NOTES below for more details.
167 =item B<-pkeyopt_passin> I<opt>[:I<passarg>]
169 Allows reading a public key option I<opt> from stdin or a password source.
170 If only I<opt> is specified, the user will be prompted to enter a password on
171 stdin. Alternatively, I<passarg> can be specified which can be any value
172 supported by L<openssl-passphrase-options(1)>.
176 hex dump the output data.
180 Parse the ASN.1 output data, this is useful when combined with the
181 B<-verifyrecover> option when an ASN1 structure is signed.
183 {- $OpenSSL::safe::opt_engine_item -}
185 {- output_off() if $disabled{"deprecated-3.0"}; "" -}
186 =item B<-engine_impl>
188 When used with the B<-engine> option, it specifies to also use
189 engine I<id> for crypto operations.
190 {- output_on() if $disabled{"deprecated-3.0"}; "" -}
192 {- $OpenSSL::safe::opt_r_item -}
194 {- $OpenSSL::safe::opt_provider_item -}
196 {- $OpenSSL::safe::opt_config_item -}
202 The operations and options supported vary according to the key algorithm
203 and its implementation. The OpenSSL operations and options are indicated below.
205 Unless otherwise mentioned all algorithms support the B<digest:>I<alg> option
206 which specifies the digest in use for sign, verify and verifyrecover operations.
207 The value I<alg> should represent a digest name as used in the
208 EVP_get_digestbyname() function for example B<sha1>. This value is not used to
209 hash the input data. It is used (by some algorithms) for sanity-checking the
210 lengths of data passed in and for creating the structures that make up the
211 signature (e.g. B<DigestInfo> in RSASSA PKCS#1 v1.5 signatures).
213 This command does not hash the input data (except where -rawin is used) but
214 rather it will use the data directly as input to the signature algorithm.
215 Depending on the key type, signature type, and mode of padding, the maximum
216 acceptable lengths of input data differ. The signed data can't be longer than
217 the key modulus with RSA. In case of ECDSA and DSA the data shouldn't be longer
218 than the field size, otherwise it will be silently truncated to the field size.
219 In any event the input size must not be larger than the largest supported digest
222 In other words, if the value of digest is B<sha1> the input should be the 20
223 bytes long binary encoding of the SHA-1 hash function output.
227 The RSA algorithm generally supports the encrypt, decrypt, sign,
228 verify and verifyrecover operations. However, some padding modes
229 support only a subset of these operations. The following additional
230 B<pkeyopt> values are supported:
234 =item B<rsa_padding_mode:>I<mode>
236 This sets the RSA padding mode. Acceptable values for I<mode> are B<pkcs1> for
237 PKCS#1 padding, B<none> for no padding, B<oaep>
238 for B<OAEP> mode, B<x931> for X9.31 mode and B<pss> for PSS.
240 In PKCS#1 padding, if the message digest is not set, then the supplied data is
241 signed or verified directly instead of using a B<DigestInfo> structure. If a
242 digest is set, then the B<DigestInfo> structure is used and its length
243 must correspond to the digest type.
245 Note, for B<pkcs1> padding, as a protection against the Bleichenbacher attack,
246 the decryption will not fail in case of padding check failures. Use B<none>
247 and manual inspection of the decrypted message to verify if the decrypted
248 value has correct PKCS#1 v1.5 padding.
250 For B<oaep> mode only encryption and decryption is supported.
252 For B<x931> if the digest type is set it is used to format the block data
253 otherwise the first byte is used to specify the X9.31 digest ID. Sign,
254 verify and verifyrecover are can be performed in this mode.
256 For B<pss> mode only sign and verify are supported and the digest type must be
259 =item B<rsa_pss_saltlen:>I<len>
261 For B<pss> mode only this option specifies the salt length. Three special
262 values are supported: B<digest> sets the salt length to the digest length,
263 B<max> sets the salt length to the maximum permissible value. When verifying
264 B<auto> causes the salt length to be automatically determined based on the
265 B<PSS> block structure.
267 =item B<rsa_mgf1_md:>I<digest>
269 For PSS and OAEP padding sets the MGF1 digest. If the MGF1 digest is not
270 explicitly set in PSS mode then the signing digest is used.
272 =item B<rsa_oaep_md:>I<digest>
274 Sets the digest used for the OAEP hash function. If not explicitly set then
277 =item B<rsa_pkcs1_implicit_rejection:>I<flag>
279 Disables (when set to 0) or enables (when set to 1) the use of implicit
280 rejection with PKCS#1 v1.5 decryption. When enabled (the default), as a
281 protection against Bleichenbacher attack, the library will generate a
282 deterministic random plaintext that it will return to the caller in case
283 of padding check failure.
284 When disabled, it's the callers' responsibility to handle the returned
285 errors in a side-channel free manner.
289 =head1 RSA-PSS ALGORITHM
291 The RSA-PSS algorithm is a restricted version of the RSA algorithm which only
292 supports the sign and verify operations with PSS padding. The following
293 additional B<-pkeyopt> values are supported:
297 =item B<rsa_padding_mode:>I<mode>, B<rsa_pss_saltlen:>I<len>,
298 B<rsa_mgf1_md:>I<digest>
300 These have the same meaning as the B<RSA> algorithm with some additional
301 restrictions. The padding mode can only be set to B<pss> which is the
304 If the key has parameter restrictions then the digest, MGF1
305 digest and salt length are set to the values specified in the parameters.
306 The digest and MG cannot be changed and the salt length cannot be set to a
307 value less than the minimum restriction.
313 The DSA algorithm supports signing and verification operations only. Currently
314 there are no additional B<-pkeyopt> options other than B<digest>. The SHA1
315 digest is assumed by default.
319 The DH algorithm only supports the derivation operation and no additional
324 The EC algorithm supports sign, verify and derive operations. The sign and
325 verify operations use ECDSA and derive uses ECDH. SHA1 is assumed by default for
326 the B<-pkeyopt> B<digest> option.
328 =head1 X25519 AND X448 ALGORITHMS
330 The X25519 and X448 algorithms support key derivation only. Currently there are
331 no additional options.
333 =head1 ED25519 AND ED448 ALGORITHMS
335 These algorithms only support signing and verifying. OpenSSL only implements the
336 "pure" variants of these algorithms so raw data can be passed directly to them
337 without hashing them first. The option B<-rawin> must be used with these
338 algorithms with no B<-digest> specified. Additionally OpenSSL only supports
339 "oneshot" operation with these algorithms. This means that the entire file to
340 be signed/verified must be read into memory before processing it. Signing or
341 Verifying very large files should be avoided. Additionally the size of the file
342 must be known for this to work. If the size of the file cannot be determined
343 (for example if the input is stdin) then the sign or verify operation will fail.
347 The SM2 algorithm supports sign, verify, encrypt and decrypt operations. For
348 the sign and verify operations, SM2 requires an Distinguishing ID string to
349 be passed in. The following B<-pkeyopt> value is supported:
353 =item B<distid:>I<string>
355 This sets the ID string used in SM2 sign or verify operations. While verifying
356 an SM2 signature, the ID string must be the same one used when signing the data.
357 Otherwise the verification will fail.
359 =item B<hexdistid:>I<hex_string>
361 This sets the ID string used in SM2 sign or verify operations. While verifying
362 an SM2 signature, the ID string must be the same one used when signing the data.
363 Otherwise the verification will fail. The ID string provided with this option
364 should be a valid hexadecimal value.
370 Sign some data using a private key:
372 openssl pkeyutl -sign -in file -inkey key.pem -out sig
374 Recover the signed data (e.g. if an RSA key is used):
376 openssl pkeyutl -verifyrecover -in sig -inkey key.pem
378 Verify the signature (e.g. a DSA key):
380 openssl pkeyutl -verify -in file -sigfile sig -inkey key.pem
382 Sign data using a message digest value (this is currently only valid for RSA):
384 openssl pkeyutl -sign -in file -inkey key.pem -out sig -pkeyopt digest:sha256
386 Derive a shared secret value:
388 openssl pkeyutl -derive -inkey key.pem -peerkey pubkey.pem -out secret
390 Hexdump 48 bytes of TLS1 PRF using digest B<SHA256> and shared secret and
391 seed consisting of the single byte 0xFF:
393 openssl pkeyutl -kdf TLS1-PRF -kdflen 48 -pkeyopt md:SHA256 \
394 -pkeyopt hexsecret:ff -pkeyopt hexseed:ff -hexdump
396 Derive a key using B<scrypt> where the password is read from command line:
398 openssl pkeyutl -kdf scrypt -kdflen 16 -pkeyopt_passin pass \
399 -pkeyopt hexsalt:aabbcc -pkeyopt N:16384 -pkeyopt r:8 -pkeyopt p:1
401 Derive using the same algorithm, but read key from environment variable MYPASS:
403 openssl pkeyutl -kdf scrypt -kdflen 16 -pkeyopt_passin pass:env:MYPASS \
404 -pkeyopt hexsalt:aabbcc -pkeyopt N:16384 -pkeyopt r:8 -pkeyopt p:1
406 Sign some data using an L<SM2(7)> private key and a specific ID:
408 openssl pkeyutl -sign -in file -inkey sm2.key -out sig -rawin -digest sm3 \
409 -pkeyopt distid:someid
411 Verify some data using an L<SM2(7)> certificate and a specific ID:
413 openssl pkeyutl -verify -certin -in file -inkey sm2.cert -sigfile sig \
414 -rawin -digest sm3 -pkeyopt distid:someid
416 Decrypt some data using a private key with OAEP padding using SHA256:
418 openssl pkeyutl -decrypt -in file -inkey key.pem -out secret \
419 -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha256
424 L<openssl-genpkey(1)>,
429 L<openssl-genrsa(1)>,
431 L<EVP_PKEY_CTX_set_hkdf_md(3)>,
432 L<EVP_PKEY_CTX_set_tls1_prf_md(3)>,
436 The B<-engine> option was deprecated in OpenSSL 3.0.
440 Copyright 2006-2023 The OpenSSL Project Authors. All Rights Reserved.
442 Licensed under the Apache License 2.0 (the "License"). You may not use
443 this file except in compliance with the License. You can obtain a copy
444 in the file LICENSE in the source distribution or at
445 L<https://www.openssl.org/source/license.html>.