f730ba7b0301af2011a96522cd5bff5e2987a290
[openssl.git] / doc / man3 / EVP_DigestSignInit.pod
1 =pod
2
3 =head1 NAME
4
5 EVP_DigestSignInit_ex, EVP_DigestSignInit, EVP_DigestSignUpdate,
6 EVP_DigestSignFinal, EVP_DigestSign - EVP signing functions
7
8 =head1 SYNOPSIS
9
10  #include <openssl/evp.h>
11
12  int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
13                            const char *mdname, const char *props,
14                            EVP_PKEY *pkey, EVP_SIGNATURE *signature);
15  int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
16                         const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey);
17  int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
18  int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen);
19
20  int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret,
21                     size_t *siglen, const unsigned char *tbs,
22                     size_t tbslen);
23
24 =head1 DESCRIPTION
25
26 The EVP signature routines are a high level interface to digital signatures.
27 Input data is digested first before the signing takes place.
28
29 EVP_DigestSignInit_ex() sets up signing context B<ctx> to use a digest with the
30 name B<mdname> and private key B<pkey>. The signature algorithm B<signature>
31 will be used for the actual signing which must be compatible with the private
32 key. The name of the digest to be used is passed to the provider of the
33 signature algorithm in use. How that provider interprets the digest name is
34 provider specific. The provider may implement that digest directly itself or it
35 may (optionally) choose to fetch it (which could result in a digest from a
36 different provider being selected). If the provider supports fetching the digest
37 then it may use the B<props> argument for the properties to be used during the
38 fetch.
39
40 The B<signature> parameter may be NULL in which case a suitable signature
41 algorithm implementation will be implicitly fetched based on the type of key in
42 use. See L<provider(7)> for further information about providers and fetching
43 algorithms.
44
45 The OpenSSL default and legacy providers support fetching digests and can fetch
46 those digests from any available provider. The OpenSSL fips provider also
47 supports fetching digests but will only fetch digests that are themselves
48 implemented inside the fips provider.
49
50 B<ctx> must be created with EVP_MD_CTX_new() before calling this function. If
51 B<pctx> is not NULL, the EVP_PKEY_CTX of the signing operation will be written
52 to B<*pctx>: this can be used to set alternative signing options. Note that any
53 existing value in B<*pctx> is overwritten. The EVP_PKEY_CTX value returned must
54 not be freed directly by the application if B<ctx> is not assigned an
55 EVP_PKEY_CTX value before being passed to EVP_DigestSignInit_ex() (which means
56 the EVP_PKEY_CTX is created inside EVP_DigestSignInit_ex() and it will be freed
57 automatically when the EVP_MD_CTX is freed).
58
59 The digest B<mdname> may be NULL if the signing algorithm supports it. The
60 B<props> argument can always be NULL.
61
62 No B<EVP_PKEY_CTX> will be created by EVP_DigestSignInit_ex() if the passed
63 B<ctx> has already been assigned one via L<EVP_MD_CTX_set_ctx(3)>. See also
64 L<SM2(7)>.
65
66 Only EVP_PKEY types that support signing can be used with these functions. This
67 includes MAC algorithms where the MAC generation is considered as a form of
68 "signing". Built-in EVP_PKEY types supported by these functions are CMAC,
69 Poly1305, DSA, ECDSA, HMAC, RSA, SipHash, Ed25519 and Ed448.
70
71 Not all digests can be used for all key types. The following combinations apply.
72
73 =over 4
74
75 =item DSA
76
77 Supports SHA1, SHA224, SHA256, SHA384 and SHA512
78
79 =item ECDSA
80
81 Supports SHA1, SHA224, SHA256, SHA384, SHA512 and SM3
82
83 =item RSA with no padding
84
85 Supports no digests (the digest B<type> must be NULL)
86
87 =item RSA with X931 padding
88
89 Supports SHA1, SHA256, SHA384 and SHA512
90
91 =item All other RSA padding types
92
93 Support SHA1, SHA224, SHA256, SHA384, SHA512, MD5, MD5_SHA1, MD2, MD4, MDC2,
94 SHA3-224, SHA3-256, SHA3-384, SHA3-512
95
96 =item Ed25519 and Ed448
97
98 Support no digests (the digest B<type> must be NULL)
99
100 =item HMAC
101
102 Supports any digest
103
104 =item CMAC, Poly1305 and SipHash
105
106 Will ignore any digest provided.
107
108 =back
109
110 If RSA-PSS is used and restrictions apply then the digest must match.
111
112 EVP_DigestSignInit() works in the same way as EVP_DigestSignInit_ex() except
113 that the B<mdname> parameter will be inferred from the supplied digest B<type>,
114 and B<props> will be NULL. Where supplied the ENGINE B<e> will be used for the
115 signing and digest algorithm implementations. B<e> may be NULL.
116
117 EVP_DigestSignUpdate() hashes B<cnt> bytes of data at B<d> into the
118 signature context B<ctx>. This function can be called several times on the
119 same B<ctx> to include additional data.
120
121 EVP_DigestSignFinal() signs the data in B<ctx> and places the signature in B<sig>.
122 If B<sig> is B<NULL> then the maximum size of the output buffer is written to
123 the B<siglen> parameter. If B<sig> is not B<NULL> then before the call the
124 B<siglen> parameter should contain the length of the B<sig> buffer. If the
125 call is successful the signature is written to B<sig> and the amount of data
126 written to B<siglen>.
127
128 EVP_DigestSign() signs B<tbslen> bytes of data at B<tbs> and places the
129 signature in B<sig> and its length in B<siglen> in a similar way to
130 EVP_DigestSignFinal().
131
132 =head1 RETURN VALUES
133
134 EVP_DigestSignInit(), EVP_DigestSignUpdate(), EVP_DigestSignaFinal() and
135 EVP_DigestSign() return 1 for success and 0 or a negative value for failure. In
136 particular, a return value of -2 indicates the operation is not supported by the
137 public key algorithm.
138
139 The error codes can be obtained from L<ERR_get_error(3)>.
140
141 =head1 NOTES
142
143 The B<EVP> interface to digital signatures should almost always be used in
144 preference to the low level interfaces. This is because the code then becomes
145 transparent to the algorithm used and much more flexible.
146
147 EVP_DigestSign() is a one shot operation which signs a single block of data
148 in one function. For algorithms that support streaming it is equivalent to
149 calling EVP_DigestSignUpdate() and EVP_DigestSignFinal(). For algorithms which
150 do not support streaming (e.g. PureEdDSA) it is the only way to sign data.
151
152 In previous versions of OpenSSL there was a link between message digest types
153 and public key algorithms. This meant that "clone" digests such as EVP_dss1()
154 needed to be used to sign using SHA1 and DSA. This is no longer necessary and
155 the use of clone digest is now discouraged.
156
157 For some key types and parameters the random number generator must be seeded.
158 If the automatic seeding or reseeding of the OpenSSL CSPRNG fails due to
159 external circumstances (see L<RAND(7)>), the operation will fail.
160
161 The call to EVP_DigestSignFinal() internally finalizes a copy of the digest
162 context. This means that calls to EVP_DigestSignUpdate() and
163 EVP_DigestSignFinal() can be called later to digest and sign additional data.
164
165 Since only a copy of the digest context is ever finalized, the context must
166 be cleaned up after use by calling EVP_MD_CTX_free() or a memory leak
167 will occur.
168
169 The use of EVP_PKEY_size() with these functions is discouraged because some
170 signature operations may have a signature length which depends on the
171 parameters set. As a result EVP_PKEY_size() would have to return a value
172 which indicates the maximum possible signature for any set of parameters.
173
174 =head1 SEE ALSO
175
176 L<EVP_DigestVerifyInit(3)>,
177 L<EVP_DigestInit(3)>,
178 L<evp(7)>, L<HMAC(3)>, L<MD2(3)>,
179 L<MD5(3)>, L<MDC2(3)>, L<RIPEMD160(3)>,
180 L<SHA1(3)>, L<dgst(1)>,
181 L<RAND(7)>
182
183 =head1 HISTORY
184
185 EVP_DigestSignInit(), EVP_DigestSignUpdate() and EVP_DigestSignFinal()
186 were added in OpenSSL 1.0.0.
187
188 EVP_DigestSignInit_ex() was added in OpenSSL 3.0.
189
190 EVP_DigestSignUpdate() was converted from a macro to a function in OpenSSL 3.0.
191
192 =head1 COPYRIGHT
193
194 Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
195
196 Licensed under the Apache License 2.0 (the "License").  You may not use
197 this file except in compliance with the License.  You can obtain a copy
198 in the file LICENSE in the source distribution or at
199 L<https://www.openssl.org/source/license.html>.
200
201 =cut