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