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