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