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