1 | =pod
|
---|
2 |
|
---|
3 | =head1 NAME
|
---|
4 |
|
---|
5 | EVP_MD_fetch, EVP_MD_up_ref, EVP_MD_free,
|
---|
6 | EVP_MD_get_params, EVP_MD_gettable_params,
|
---|
7 | EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_dup,
|
---|
8 | EVP_MD_CTX_copy, EVP_MD_CTX_copy_ex, EVP_MD_CTX_ctrl,
|
---|
9 | EVP_MD_CTX_set_params, EVP_MD_CTX_get_params,
|
---|
10 | EVP_MD_settable_ctx_params, EVP_MD_gettable_ctx_params,
|
---|
11 | EVP_MD_CTX_settable_params, EVP_MD_CTX_gettable_params,
|
---|
12 | EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags, EVP_MD_CTX_test_flags,
|
---|
13 | EVP_Q_digest, EVP_Digest, EVP_DigestInit_ex2, EVP_DigestInit_ex, EVP_DigestInit,
|
---|
14 | EVP_DigestUpdate, EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
|
---|
15 | EVP_MD_is_a, EVP_MD_get0_name, EVP_MD_get0_description,
|
---|
16 | EVP_MD_names_do_all, EVP_MD_get0_provider, EVP_MD_get_type,
|
---|
17 | EVP_MD_get_pkey_type, EVP_MD_get_size, EVP_MD_get_block_size, EVP_MD_get_flags,
|
---|
18 | EVP_MD_CTX_get0_name, EVP_MD_CTX_md, EVP_MD_CTX_get0_md, EVP_MD_CTX_get1_md,
|
---|
19 | EVP_MD_CTX_get_type, EVP_MD_CTX_get_size, EVP_MD_CTX_get_block_size,
|
---|
20 | EVP_MD_CTX_get0_md_data, EVP_MD_CTX_update_fn, EVP_MD_CTX_set_update_fn,
|
---|
21 | EVP_md_null,
|
---|
22 | EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj,
|
---|
23 | EVP_MD_CTX_get_pkey_ctx, EVP_MD_CTX_set_pkey_ctx,
|
---|
24 | EVP_MD_do_all_provided,
|
---|
25 | EVP_MD_type, EVP_MD_nid, EVP_MD_name, EVP_MD_pkey_type, EVP_MD_size,
|
---|
26 | EVP_MD_block_size, EVP_MD_flags, EVP_MD_CTX_size, EVP_MD_CTX_block_size,
|
---|
27 | EVP_MD_CTX_type, EVP_MD_CTX_pkey_ctx, EVP_MD_CTX_md_data
|
---|
28 | - EVP digest routines
|
---|
29 |
|
---|
30 | =head1 SYNOPSIS
|
---|
31 |
|
---|
32 | #include <openssl/evp.h>
|
---|
33 |
|
---|
34 | EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
|
---|
35 | const char *properties);
|
---|
36 | int EVP_MD_up_ref(EVP_MD *md);
|
---|
37 | void EVP_MD_free(EVP_MD *md);
|
---|
38 | int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[]);
|
---|
39 | const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest);
|
---|
40 | EVP_MD_CTX *EVP_MD_CTX_new(void);
|
---|
41 | int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
|
---|
42 | void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
|
---|
43 | void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
|
---|
44 | int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]);
|
---|
45 | int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]);
|
---|
46 | const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md);
|
---|
47 | const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md);
|
---|
48 | const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx);
|
---|
49 | const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx);
|
---|
50 | void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
|
---|
51 | void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
|
---|
52 | int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
|
---|
53 |
|
---|
54 | int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
|
---|
55 | const void *data, size_t datalen,
|
---|
56 | unsigned char *md, size_t *mdlen);
|
---|
57 | int EVP_Digest(const void *data, size_t count, unsigned char *md,
|
---|
58 | unsigned int *size, const EVP_MD *type, ENGINE *impl);
|
---|
59 | int EVP_DigestInit_ex2(EVP_MD_CTX *ctx, const EVP_MD *type,
|
---|
60 | const OSSL_PARAM params[]);
|
---|
61 | int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
|
---|
62 | int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
|
---|
63 | int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
|
---|
64 | int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
|
---|
65 |
|
---|
66 | EVP_MD_CTX *EVP_MD_CTX_dup(const EVP_MD_CTX *in);
|
---|
67 | int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
|
---|
68 |
|
---|
69 | int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
|
---|
70 | int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
|
---|
71 |
|
---|
72 | int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
|
---|
73 |
|
---|
74 | const char *EVP_MD_get0_name(const EVP_MD *md);
|
---|
75 | const char *EVP_MD_get0_description(const EVP_MD *md);
|
---|
76 | int EVP_MD_is_a(const EVP_MD *md, const char *name);
|
---|
77 | int EVP_MD_names_do_all(const EVP_MD *md,
|
---|
78 | void (*fn)(const char *name, void *data),
|
---|
79 | void *data);
|
---|
80 | const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md);
|
---|
81 | int EVP_MD_get_type(const EVP_MD *md);
|
---|
82 | int EVP_MD_get_pkey_type(const EVP_MD *md);
|
---|
83 | int EVP_MD_get_size(const EVP_MD *md);
|
---|
84 | int EVP_MD_get_block_size(const EVP_MD *md);
|
---|
85 | unsigned long EVP_MD_get_flags(const EVP_MD *md);
|
---|
86 |
|
---|
87 | const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx);
|
---|
88 | EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx);
|
---|
89 | const char *EVP_MD_CTX_get0_name(const EVP_MD_CTX *ctx);
|
---|
90 | int EVP_MD_CTX_get_size(const EVP_MD_CTX *ctx);
|
---|
91 | int EVP_MD_CTX_get_block_size(const EVP_MD_CTX *ctx);
|
---|
92 | int EVP_MD_CTX_get_type(const EVP_MD_CTX *ctx);
|
---|
93 | void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx);
|
---|
94 |
|
---|
95 | const EVP_MD *EVP_md_null(void);
|
---|
96 |
|
---|
97 | const EVP_MD *EVP_get_digestbyname(const char *name);
|
---|
98 | const EVP_MD *EVP_get_digestbynid(int type);
|
---|
99 | const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
|
---|
100 |
|
---|
101 | EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx);
|
---|
102 | void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
|
---|
103 |
|
---|
104 | void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
|
---|
105 | void (*fn)(EVP_MD *mac, void *arg),
|
---|
106 | void *arg);
|
---|
107 |
|
---|
108 | #define EVP_MD_type EVP_MD_get_type
|
---|
109 | #define EVP_MD_nid EVP_MD_get_type
|
---|
110 | #define EVP_MD_name EVP_MD_get0_name
|
---|
111 | #define EVP_MD_pkey_type EVP_MD_get_pkey_type
|
---|
112 | #define EVP_MD_size EVP_MD_get_size
|
---|
113 | #define EVP_MD_block_size EVP_MD_get_block_size
|
---|
114 | #define EVP_MD_flags EVP_MD_get_flags
|
---|
115 | #define EVP_MD_CTX_size EVP_MD_CTX_get_size
|
---|
116 | #define EVP_MD_CTX_block_size EVP_MD_CTX_get_block_size
|
---|
117 | #define EVP_MD_CTX_type EVP_MD_CTX_get_type
|
---|
118 | #define EVP_MD_CTX_pkey_ctx EVP_MD_CTX_get_pkey_ctx
|
---|
119 | #define EVP_MD_CTX_md_data EVP_MD_CTX_get0_md_data
|
---|
120 |
|
---|
121 | The following functions have been deprecated since OpenSSL 3.0, and can be
|
---|
122 | hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
|
---|
123 | see L<openssl_user_macros(7)>:
|
---|
124 |
|
---|
125 | const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
|
---|
126 |
|
---|
127 | int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
|
---|
128 | const void *data, size_t count);
|
---|
129 |
|
---|
130 | void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
|
---|
131 | int (*update)(EVP_MD_CTX *ctx,
|
---|
132 | const void *data, size_t count));
|
---|
133 |
|
---|
134 | =head1 DESCRIPTION
|
---|
135 |
|
---|
136 | The EVP digest routines are a high-level interface to message digests,
|
---|
137 | and should be used instead of the digest-specific functions.
|
---|
138 |
|
---|
139 | The B<EVP_MD> type is a structure for digest method implementation.
|
---|
140 |
|
---|
141 | =over 4
|
---|
142 |
|
---|
143 | =item EVP_MD_fetch()
|
---|
144 |
|
---|
145 | Fetches the digest implementation for the given I<algorithm> from any
|
---|
146 | provider offering it, within the criteria given by the I<properties>.
|
---|
147 | See L<crypto(7)/ALGORITHM FETCHING> for further information.
|
---|
148 |
|
---|
149 | The returned value must eventually be freed with EVP_MD_free().
|
---|
150 |
|
---|
151 | Fetched B<EVP_MD> structures are reference counted.
|
---|
152 |
|
---|
153 | =item EVP_MD_up_ref()
|
---|
154 |
|
---|
155 | Increments the reference count for an B<EVP_MD> structure.
|
---|
156 |
|
---|
157 | =item EVP_MD_free()
|
---|
158 |
|
---|
159 | Decrements the reference count for the fetched B<EVP_MD> structure.
|
---|
160 | If the reference count drops to 0 then the structure is freed.
|
---|
161 |
|
---|
162 | =item EVP_MD_CTX_new()
|
---|
163 |
|
---|
164 | Allocates and returns a digest context.
|
---|
165 |
|
---|
166 | =item EVP_MD_CTX_reset()
|
---|
167 |
|
---|
168 | Resets the digest context I<ctx>. This can be used to reuse an already
|
---|
169 | existing context.
|
---|
170 |
|
---|
171 | =item EVP_MD_CTX_free()
|
---|
172 |
|
---|
173 | Cleans up digest context I<ctx> and frees up the space allocated to it.
|
---|
174 |
|
---|
175 | =item EVP_MD_CTX_ctrl()
|
---|
176 |
|
---|
177 | I<This is a legacy method. EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params()
|
---|
178 | is the mechanism that should be used to set and get parameters that are used by
|
---|
179 | providers.>
|
---|
180 |
|
---|
181 | Performs digest-specific control actions on context I<ctx>. The control command
|
---|
182 | is indicated in I<cmd> and any additional arguments in I<p1> and I<p2>.
|
---|
183 | EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex2(). Other restrictions
|
---|
184 | may apply depending on the control type and digest implementation.
|
---|
185 |
|
---|
186 | If this function happens to be used with a fetched B<EVP_MD>, it will
|
---|
187 | translate the controls that are known to OpenSSL into L<OSSL_PARAM(3)>
|
---|
188 | parameters with keys defined by OpenSSL and call EVP_MD_CTX_get_params() or
|
---|
189 | EVP_MD_CTX_set_params() as is appropriate for each control command.
|
---|
190 |
|
---|
191 | See L</CONTROLS> below for more information, including what translations are
|
---|
192 | being done.
|
---|
193 |
|
---|
194 | =item EVP_MD_get_params()
|
---|
195 |
|
---|
196 | Retrieves the requested list of I<params> from a MD I<md>.
|
---|
197 | See L</PARAMETERS> below for more information.
|
---|
198 |
|
---|
199 | =item EVP_MD_CTX_get_params()
|
---|
200 |
|
---|
201 | Retrieves the requested list of I<params> from a MD context I<ctx>.
|
---|
202 | See L</PARAMETERS> below for more information.
|
---|
203 |
|
---|
204 | =item EVP_MD_CTX_set_params()
|
---|
205 |
|
---|
206 | Sets the list of I<params> into a MD context I<ctx>.
|
---|
207 | See L</PARAMETERS> below for more information.
|
---|
208 |
|
---|
209 | =item EVP_MD_gettable_params()
|
---|
210 |
|
---|
211 | Get a constant L<OSSL_PARAM(3)> array that describes the retrievable parameters
|
---|
212 | that can be used with EVP_MD_get_params().
|
---|
213 |
|
---|
214 | =item EVP_MD_gettable_ctx_params(), EVP_MD_CTX_gettable_params()
|
---|
215 |
|
---|
216 | Get a constant L<OSSL_PARAM(3)> array that describes the retrievable parameters
|
---|
217 | that can be used with EVP_MD_CTX_get_params(). EVP_MD_gettable_ctx_params()
|
---|
218 | returns the parameters that can be retrieved from the algorithm, whereas
|
---|
219 | EVP_MD_CTX_gettable_params() returns the parameters that can be retrieved
|
---|
220 | in the context's current state.
|
---|
221 |
|
---|
222 | =item EVP_MD_settable_ctx_params(), EVP_MD_CTX_settable_params()
|
---|
223 |
|
---|
224 | Get a constant L<OSSL_PARAM(3)> array that describes the settable parameters
|
---|
225 | that can be used with EVP_MD_CTX_set_params(). EVP_MD_settable_ctx_params()
|
---|
226 | returns the parameters that can be set from the algorithm, whereas
|
---|
227 | EVP_MD_CTX_settable_params() returns the parameters that can be set in the
|
---|
228 | context's current state.
|
---|
229 |
|
---|
230 | =item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
|
---|
231 |
|
---|
232 | Sets, clears and tests I<ctx> flags. See L</FLAGS> below for more information.
|
---|
233 |
|
---|
234 | =item EVP_Q_digest() is a quick one-shot digest function.
|
---|
235 |
|
---|
236 | It hashes I<datalen> bytes of data at I<data> using the digest algorithm
|
---|
237 | I<name>, which is fetched using the optional I<libctx> and I<propq> parameters.
|
---|
238 | The digest value is placed in I<md> and its length is written at I<mdlen>
|
---|
239 | if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
|
---|
240 |
|
---|
241 | =item EVP_Digest()
|
---|
242 |
|
---|
243 | A wrapper around the Digest Init_ex, Update and Final_ex functions.
|
---|
244 | Hashes I<count> bytes of data at I<data> using a digest I<type> from ENGINE
|
---|
245 | I<impl>. The digest value is placed in I<md> and its length is written at I<size>
|
---|
246 | if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
|
---|
247 | If I<impl> is NULL the default implementation of digest I<type> is used.
|
---|
248 |
|
---|
249 | =item EVP_DigestInit_ex2()
|
---|
250 |
|
---|
251 | Sets up digest context I<ctx> to use a digest I<type>.
|
---|
252 | I<type> is typically supplied by a function such as EVP_sha1(), or a
|
---|
253 | value explicitly fetched with EVP_MD_fetch().
|
---|
254 |
|
---|
255 | The parameters B<params> are set on the context after initialisation.
|
---|
256 |
|
---|
257 | The I<type> parameter can be NULL if I<ctx> has been already initialized
|
---|
258 | with another EVP_DigestInit_ex() call and has not been reset with
|
---|
259 | EVP_MD_CTX_reset().
|
---|
260 |
|
---|
261 | =item EVP_DigestInit_ex()
|
---|
262 |
|
---|
263 | Sets up digest context I<ctx> to use a digest I<type>.
|
---|
264 | I<type> is typically supplied by a function such as EVP_sha1(), or a
|
---|
265 | value explicitly fetched with EVP_MD_fetch().
|
---|
266 |
|
---|
267 | If I<impl> is non-NULL, its implementation of the digest I<type> is used if
|
---|
268 | there is one, and if not, the default implementation is used.
|
---|
269 |
|
---|
270 | The I<type> parameter can be NULL if I<ctx> has been already initialized
|
---|
271 | with another EVP_DigestInit_ex() call and has not been reset with
|
---|
272 | EVP_MD_CTX_reset().
|
---|
273 |
|
---|
274 | =item EVP_DigestUpdate()
|
---|
275 |
|
---|
276 | Hashes I<cnt> bytes of data at I<d> into the digest context I<ctx>. This
|
---|
277 | function can be called several times on the same I<ctx> to hash additional
|
---|
278 | data.
|
---|
279 |
|
---|
280 | =item EVP_DigestFinal_ex()
|
---|
281 |
|
---|
282 | Retrieves the digest value from I<ctx> and places it in I<md>. If the I<s>
|
---|
283 | parameter is not NULL then the number of bytes of data written (i.e. the
|
---|
284 | length of the digest) will be written to the integer at I<s>, at most
|
---|
285 | B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
|
---|
286 | no additional calls to EVP_DigestUpdate() can be made, but
|
---|
287 | EVP_DigestInit_ex2() can be called to initialize a new digest operation.
|
---|
288 |
|
---|
289 | =item EVP_DigestFinalXOF()
|
---|
290 |
|
---|
291 | Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
|
---|
292 | It retrieves the digest value from I<ctx> and places it in I<len>-sized I<md>.
|
---|
293 | After calling this function no additional calls to EVP_DigestUpdate() can be
|
---|
294 | made, but EVP_DigestInit_ex2() can be called to initialize a new operation.
|
---|
295 |
|
---|
296 | =item EVP_MD_CTX_dup()
|
---|
297 |
|
---|
298 | Can be used to duplicate the message digest state from I<in>. This is useful
|
---|
299 | to avoid multiple EVP_MD_fetch() calls or if large amounts of data are to be
|
---|
300 | hashed which only differ in the last few bytes.
|
---|
301 |
|
---|
302 | =item EVP_MD_CTX_copy_ex()
|
---|
303 |
|
---|
304 | Can be used to copy the message digest state from I<in> to I<out>. This is
|
---|
305 | useful if large amounts of data are to be hashed which only differ in the last
|
---|
306 | few bytes.
|
---|
307 |
|
---|
308 | =item EVP_DigestInit()
|
---|
309 |
|
---|
310 | Behaves in the same way as EVP_DigestInit_ex2() except it doesn't set any
|
---|
311 | parameters and calls EVP_MD_CTX_reset() so it cannot be used with an I<type>
|
---|
312 | of NULL.
|
---|
313 |
|
---|
314 | =item EVP_DigestFinal()
|
---|
315 |
|
---|
316 | Similar to EVP_DigestFinal_ex() except after computing the digest
|
---|
317 | the digest context I<ctx> is automatically cleaned up with EVP_MD_CTX_reset().
|
---|
318 |
|
---|
319 | =item EVP_MD_CTX_copy()
|
---|
320 |
|
---|
321 | Similar to EVP_MD_CTX_copy_ex() except the destination I<out> does not have to
|
---|
322 | be initialized.
|
---|
323 |
|
---|
324 | =item EVP_MD_is_a()
|
---|
325 |
|
---|
326 | Returns 1 if I<md> is an implementation of an algorithm that's
|
---|
327 | identifiable with I<name>, otherwise 0.
|
---|
328 |
|
---|
329 | If I<md> is a legacy digest (it's the return value from the likes of
|
---|
330 | EVP_sha256() rather than the result of an EVP_MD_fetch()), only cipher
|
---|
331 | names registered with the default library context (see
|
---|
332 | L<OSSL_LIB_CTX(3)>) will be considered.
|
---|
333 |
|
---|
334 | =item EVP_MD_get0_name(),
|
---|
335 | EVP_MD_CTX_get0_name()
|
---|
336 |
|
---|
337 | Return the name of the given message digest. For fetched message
|
---|
338 | digests with multiple names, only one of them is returned; it's
|
---|
339 | recommended to use EVP_MD_names_do_all() instead.
|
---|
340 |
|
---|
341 | =item EVP_MD_names_do_all()
|
---|
342 |
|
---|
343 | Traverses all names for the I<md>, and calls I<fn> with each name and
|
---|
344 | I<data>. This is only useful with fetched B<EVP_MD>s.
|
---|
345 |
|
---|
346 | =item EVP_MD_get0_description()
|
---|
347 |
|
---|
348 | Returns a description of the digest, meant for display and human consumption.
|
---|
349 | The description is at the discretion of the digest implementation.
|
---|
350 |
|
---|
351 | =item EVP_MD_get0_provider()
|
---|
352 |
|
---|
353 | Returns an B<OSSL_PROVIDER> pointer to the provider that implements the given
|
---|
354 | B<EVP_MD>.
|
---|
355 |
|
---|
356 | =item EVP_MD_get_size(),
|
---|
357 | EVP_MD_CTX_get_size()
|
---|
358 |
|
---|
359 | Return the size of the message digest when passed an B<EVP_MD> or an
|
---|
360 | B<EVP_MD_CTX> structure, i.e. the size of the hash.
|
---|
361 |
|
---|
362 | =item EVP_MD_get_block_size(),
|
---|
363 | EVP_MD_CTX_get_block_size()
|
---|
364 |
|
---|
365 | Return the block size of the message digest when passed an B<EVP_MD> or an
|
---|
366 | B<EVP_MD_CTX> structure.
|
---|
367 |
|
---|
368 | =item EVP_MD_get_type(),
|
---|
369 | EVP_MD_CTX_get_type()
|
---|
370 |
|
---|
371 | Return the NID of the OBJECT IDENTIFIER representing the given message digest
|
---|
372 | when passed an B<EVP_MD> structure. For example, C<EVP_MD_get_type(EVP_sha1())>
|
---|
373 | returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
|
---|
374 |
|
---|
375 | =item EVP_MD_CTX_get0_md_data()
|
---|
376 |
|
---|
377 | Return the digest method private data for the passed B<EVP_MD_CTX>.
|
---|
378 | The space is allocated by OpenSSL and has the size originally set with
|
---|
379 | EVP_MD_meth_set_app_datasize().
|
---|
380 |
|
---|
381 | =item EVP_MD_CTX_get0_md(), EVP_MD_CTX_get1_md()
|
---|
382 |
|
---|
383 | EVP_MD_CTX_get0_md() returns
|
---|
384 | the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>. This
|
---|
385 | will be the same B<EVP_MD> object originally passed to EVP_DigestInit_ex2() (or
|
---|
386 | other similar function) when the EVP_MD_CTX was first initialised. Note that
|
---|
387 | where explicit fetch is in use (see L<EVP_MD_fetch(3)>) the value returned from
|
---|
388 | this function will not have its reference count incremented and therefore it
|
---|
389 | should not be used after the EVP_MD_CTX is freed.
|
---|
390 | EVP_MD_CTX_get1_md() is the same except the ownership is passed to the
|
---|
391 | caller and is from the passed B<EVP_MD_CTX>.
|
---|
392 |
|
---|
393 | =item EVP_MD_CTX_set_update_fn()
|
---|
394 |
|
---|
395 | Sets the update function for I<ctx> to I<update>.
|
---|
396 | This is the function that is called by EVP_DigestUpdate(). If not set, the
|
---|
397 | update function from the B<EVP_MD> type specified at initialization is used.
|
---|
398 |
|
---|
399 | =item EVP_MD_CTX_update_fn()
|
---|
400 |
|
---|
401 | Returns the update function for I<ctx>.
|
---|
402 |
|
---|
403 | =item EVP_MD_get_flags()
|
---|
404 |
|
---|
405 | Returns the I<md> flags. Note that these are different from the B<EVP_MD_CTX>
|
---|
406 | ones. See L<EVP_MD_meth_set_flags(3)> for more information.
|
---|
407 |
|
---|
408 | =item EVP_MD_get_pkey_type()
|
---|
409 |
|
---|
410 | Returns the NID of the public key signing algorithm associated with this
|
---|
411 | digest. For example EVP_sha1() is associated with RSA so this will return
|
---|
412 | B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
|
---|
413 | longer linked this function is only retained for compatibility reasons.
|
---|
414 |
|
---|
415 | =item EVP_md_null()
|
---|
416 |
|
---|
417 | A "null" message digest that does nothing: i.e. the hash it returns is of zero
|
---|
418 | length.
|
---|
419 |
|
---|
420 | =item EVP_get_digestbyname(),
|
---|
421 | EVP_get_digestbynid(),
|
---|
422 | EVP_get_digestbyobj()
|
---|
423 |
|
---|
424 | Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
|
---|
425 | B<ASN1_OBJECT> structure respectively.
|
---|
426 |
|
---|
427 | The EVP_get_digestbyname() function is present for backwards compatibility with
|
---|
428 | OpenSSL prior to version 3 and is different to the EVP_MD_fetch() function
|
---|
429 | since it does not attempt to "fetch" an implementation of the cipher.
|
---|
430 | Additionally, it only knows about digests that are built-in to OpenSSL and have
|
---|
431 | an associated NID. Similarly EVP_get_digestbynid() and EVP_get_digestbyobj()
|
---|
432 | also return objects without an associated implementation.
|
---|
433 |
|
---|
434 | When the digest objects returned by these functions are used (such as in a call
|
---|
435 | to EVP_DigestInit_ex()) an implementation of the digest will be implicitly
|
---|
436 | fetched from the loaded providers. This fetch could fail if no suitable
|
---|
437 | implementation is available. Use EVP_MD_fetch() instead to explicitly fetch
|
---|
438 | the algorithm and an associated implementation from a provider.
|
---|
439 |
|
---|
440 | See L<crypto(7)/ALGORITHM FETCHING> for more information about fetching.
|
---|
441 |
|
---|
442 | The digest objects returned from these functions do not need to be freed with
|
---|
443 | EVP_MD_free().
|
---|
444 |
|
---|
445 | =item EVP_MD_CTX_get_pkey_ctx()
|
---|
446 |
|
---|
447 | Returns the B<EVP_PKEY_CTX> assigned to I<ctx>. The returned pointer should not
|
---|
448 | be freed by the caller.
|
---|
449 |
|
---|
450 | =item EVP_MD_CTX_set_pkey_ctx()
|
---|
451 |
|
---|
452 | Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
|
---|
453 | a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
|
---|
454 | L<EVP_DigestVerifyInit(3)>. The I<pctx> passed to this function should be freed
|
---|
455 | by the caller. A NULL I<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
|
---|
456 | assigned to I<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
|
---|
457 | depends on how the B<EVP_PKEY_CTX> is created.
|
---|
458 |
|
---|
459 | =item EVP_MD_do_all_provided()
|
---|
460 |
|
---|
461 | Traverses all messages digests implemented by all activated providers
|
---|
462 | in the given library context I<libctx>, and for each of the implementations,
|
---|
463 | calls the given function I<fn> with the implementation method and the given
|
---|
464 | I<arg> as argument.
|
---|
465 |
|
---|
466 | =back
|
---|
467 |
|
---|
468 | =head1 PARAMETERS
|
---|
469 |
|
---|
470 | See L<OSSL_PARAM(3)> for information about passing parameters.
|
---|
471 |
|
---|
472 | EVP_MD_CTX_set_params() can be used with the following OSSL_PARAM keys:
|
---|
473 |
|
---|
474 | =over 4
|
---|
475 |
|
---|
476 | =item "xoflen" (B<OSSL_DIGEST_PARAM_XOFLEN>) <unsigned integer>
|
---|
477 |
|
---|
478 | Sets the digest length for extendable output functions.
|
---|
479 | It is used by the SHAKE algorithm and should not exceed what can be given
|
---|
480 | using a B<size_t>.
|
---|
481 |
|
---|
482 | =item "pad-type" (B<OSSL_DIGEST_PARAM_PAD_TYPE>) <unsigned integer>
|
---|
483 |
|
---|
484 | Sets the padding type.
|
---|
485 | It is used by the MDC2 algorithm.
|
---|
486 |
|
---|
487 | =back
|
---|
488 |
|
---|
489 | EVP_MD_CTX_get_params() can be used with the following OSSL_PARAM keys:
|
---|
490 |
|
---|
491 | =over 4
|
---|
492 |
|
---|
493 | =item "micalg" (B<OSSL_PARAM_DIGEST_KEY_MICALG>) <UTF8 string>.
|
---|
494 |
|
---|
495 | Gets the digest Message Integrity Check algorithm string. This is used when
|
---|
496 | creating S/MIME multipart/signed messages, as specified in RFC 3851.
|
---|
497 | It may be used by external engines or providers.
|
---|
498 |
|
---|
499 | =back
|
---|
500 |
|
---|
501 | =head1 CONTROLS
|
---|
502 |
|
---|
503 | EVP_MD_CTX_ctrl() can be used to send the following standard controls:
|
---|
504 |
|
---|
505 | =over 4
|
---|
506 |
|
---|
507 | =item EVP_MD_CTRL_MICALG
|
---|
508 |
|
---|
509 | Gets the digest Message Integrity Check algorithm string. This is used when
|
---|
510 | creating S/MIME multipart/signed messages, as specified in RFC 3851.
|
---|
511 | The string value is written to I<p2>.
|
---|
512 |
|
---|
513 | When used with a fetched B<EVP_MD>, EVP_MD_CTX_get_params() gets called with
|
---|
514 | an L<OSSL_PARAM(3)> item with the key "micalg" (B<OSSL_DIGEST_PARAM_MICALG>).
|
---|
515 |
|
---|
516 | =item EVP_MD_CTRL_XOF_LEN
|
---|
517 |
|
---|
518 | This control sets the digest length for extendable output functions to I<p1>.
|
---|
519 | Sending this control directly should not be necessary, the use of
|
---|
520 | EVP_DigestFinalXOF() is preferred.
|
---|
521 | Currently used by SHAKE.
|
---|
522 |
|
---|
523 | When used with a fetched B<EVP_MD>, EVP_MD_CTX_get_params() gets called with
|
---|
524 | an L<OSSL_PARAM(3)> item with the key "xoflen" (B<OSSL_DIGEST_PARAM_XOFLEN>).
|
---|
525 |
|
---|
526 | =back
|
---|
527 |
|
---|
528 | =head1 FLAGS
|
---|
529 |
|
---|
530 | EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
|
---|
531 | can be used the manipulate and test these B<EVP_MD_CTX> flags:
|
---|
532 |
|
---|
533 | =over 4
|
---|
534 |
|
---|
535 | =item EVP_MD_CTX_FLAG_ONESHOT
|
---|
536 |
|
---|
537 | This flag instructs the digest to optimize for one update only, if possible.
|
---|
538 |
|
---|
539 | =for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
|
---|
540 |
|
---|
541 | =for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
|
---|
542 |
|
---|
543 | =for comment We currently avoid documenting flags that are only bit holder:
|
---|
544 | EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
|
---|
545 |
|
---|
546 | =item EVP_MD_CTX_FLAG_NO_INIT
|
---|
547 |
|
---|
548 | This flag instructs EVP_DigestInit() and similar not to initialise the
|
---|
549 | implementation specific data.
|
---|
550 |
|
---|
551 | =item EVP_MD_CTX_FLAG_FINALISE
|
---|
552 |
|
---|
553 | Some functions such as EVP_DigestSign only finalise copies of internal
|
---|
554 | contexts so additional data can be included after the finalisation call.
|
---|
555 | This is inefficient if this functionality is not required, and can be
|
---|
556 | disabled with this flag.
|
---|
557 |
|
---|
558 | =back
|
---|
559 |
|
---|
560 | =head1 RETURN VALUES
|
---|
561 |
|
---|
562 | =over 4
|
---|
563 |
|
---|
564 | =item EVP_MD_fetch()
|
---|
565 |
|
---|
566 | Returns a pointer to a B<EVP_MD> for success or NULL for failure.
|
---|
567 |
|
---|
568 | =item EVP_MD_up_ref()
|
---|
569 |
|
---|
570 | Returns 1 for success or 0 for failure.
|
---|
571 |
|
---|
572 | =item EVP_Q_digest(),
|
---|
573 | EVP_Digest(),
|
---|
574 | EVP_DigestInit_ex2(),
|
---|
575 | EVP_DigestInit_ex(),
|
---|
576 | EVP_DigestInit(),
|
---|
577 | EVP_DigestUpdate(),
|
---|
578 | EVP_DigestFinal_ex(),
|
---|
579 | EVP_DigestFinalXOF(), and
|
---|
580 | EVP_DigestFinal()
|
---|
581 |
|
---|
582 | return 1 for
|
---|
583 | success and 0 for failure.
|
---|
584 |
|
---|
585 | =item EVP_MD_CTX_ctrl()
|
---|
586 |
|
---|
587 | Returns 1 if successful or 0 for failure.
|
---|
588 |
|
---|
589 | =item EVP_MD_CTX_set_params(),
|
---|
590 | EVP_MD_CTX_get_params()
|
---|
591 |
|
---|
592 | Returns 1 if successful or 0 for failure.
|
---|
593 |
|
---|
594 | =item EVP_MD_CTX_settable_params(),
|
---|
595 | EVP_MD_CTX_gettable_params()
|
---|
596 |
|
---|
597 | Return an array of constant L<OSSL_PARAM(3)>s, or NULL if there is none
|
---|
598 | to get.
|
---|
599 |
|
---|
600 | =item EVP_MD_CTX_dup()
|
---|
601 |
|
---|
602 | Returns a new EVP_MD_CTX if successful or NULL on failure.
|
---|
603 |
|
---|
604 | =item EVP_MD_CTX_copy_ex()
|
---|
605 |
|
---|
606 | Returns 1 if successful or 0 for failure.
|
---|
607 |
|
---|
608 | =item EVP_MD_get_type(),
|
---|
609 | EVP_MD_get_pkey_type()
|
---|
610 |
|
---|
611 | Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
|
---|
612 | exists.
|
---|
613 |
|
---|
614 | =item EVP_MD_get_size(),
|
---|
615 | EVP_MD_get_block_size(),
|
---|
616 | EVP_MD_CTX_get_size(),
|
---|
617 | EVP_MD_CTX_get_block_size()
|
---|
618 |
|
---|
619 | Returns the digest or block size in bytes or -1 for failure.
|
---|
620 |
|
---|
621 | =item EVP_md_null()
|
---|
622 |
|
---|
623 | Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
|
---|
624 |
|
---|
625 | =item EVP_get_digestbyname(),
|
---|
626 | EVP_get_digestbynid(),
|
---|
627 | EVP_get_digestbyobj()
|
---|
628 |
|
---|
629 | Returns either an B<EVP_MD> structure or NULL if an error occurs.
|
---|
630 |
|
---|
631 | =item EVP_MD_CTX_set_pkey_ctx()
|
---|
632 |
|
---|
633 | This function has no return value.
|
---|
634 |
|
---|
635 | =item EVP_MD_names_do_all()
|
---|
636 |
|
---|
637 | Returns 1 if the callback was called for all names. A return value of 0 means
|
---|
638 | that the callback was not called for any names.
|
---|
639 |
|
---|
640 | =back
|
---|
641 |
|
---|
642 | =head1 NOTES
|
---|
643 |
|
---|
644 | The B<EVP> interface to message digests should almost always be used in
|
---|
645 | preference to the low-level interfaces. This is because the code then becomes
|
---|
646 | transparent to the digest used and much more flexible.
|
---|
647 |
|
---|
648 | New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
|
---|
649 | digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
|
---|
650 | are still in common use.
|
---|
651 |
|
---|
652 | For most applications the I<impl> parameter to EVP_DigestInit_ex() will be
|
---|
653 | set to NULL to use the default digest implementation.
|
---|
654 |
|
---|
655 | Ignoring failure returns of EVP_DigestInit_ex(), EVP_DigestInit_ex2(), or
|
---|
656 | EVP_DigestInit() can lead to undefined behavior on subsequent calls
|
---|
657 | updating or finalizing the B<EVP_MD_CTX> such as the EVP_DigestUpdate() or
|
---|
658 | EVP_DigestFinal() functions. The only valid calls on the B<EVP_MD_CTX>
|
---|
659 | when initialization fails are calls that attempt another initialization of
|
---|
660 | the context or release the context.
|
---|
661 |
|
---|
662 | The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
|
---|
663 | obsolete but are retained to maintain compatibility with existing code. New
|
---|
664 | applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
|
---|
665 | EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
|
---|
666 | instead of initializing and cleaning it up on each call and allow non default
|
---|
667 | implementations of digests to be specified.
|
---|
668 |
|
---|
669 | If digest contexts are not cleaned up after use,
|
---|
670 | memory leaks will occur.
|
---|
671 |
|
---|
672 | EVP_MD_CTX_get0_name(), EVP_MD_CTX_get_size(), EVP_MD_CTX_get_block_size(),
|
---|
673 | EVP_MD_CTX_get_type(), EVP_get_digestbynid() and EVP_get_digestbyobj() are
|
---|
674 | defined as macros.
|
---|
675 |
|
---|
676 | EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
|
---|
677 | or control.
|
---|
678 |
|
---|
679 | =head1 EXAMPLES
|
---|
680 |
|
---|
681 | This example digests the data "Test Message\n" and "Hello World\n", using the
|
---|
682 | digest name passed on the command line.
|
---|
683 |
|
---|
684 | #include <stdio.h>
|
---|
685 | #include <string.h>
|
---|
686 | #include <openssl/evp.h>
|
---|
687 |
|
---|
688 | int main(int argc, char *argv[])
|
---|
689 | {
|
---|
690 | EVP_MD_CTX *mdctx;
|
---|
691 | const EVP_MD *md;
|
---|
692 | char mess1[] = "Test Message\n";
|
---|
693 | char mess2[] = "Hello World\n";
|
---|
694 | unsigned char md_value[EVP_MAX_MD_SIZE];
|
---|
695 | unsigned int md_len, i;
|
---|
696 |
|
---|
697 | if (argv[1] == NULL) {
|
---|
698 | printf("Usage: mdtest digestname\n");
|
---|
699 | exit(1);
|
---|
700 | }
|
---|
701 |
|
---|
702 | md = EVP_get_digestbyname(argv[1]);
|
---|
703 | if (md == NULL) {
|
---|
704 | printf("Unknown message digest %s\n", argv[1]);
|
---|
705 | exit(1);
|
---|
706 | }
|
---|
707 |
|
---|
708 | mdctx = EVP_MD_CTX_new();
|
---|
709 | if (!EVP_DigestInit_ex2(mdctx, md, NULL)) {
|
---|
710 | printf("Message digest initialization failed.\n");
|
---|
711 | EVP_MD_CTX_free(mdctx);
|
---|
712 | exit(1);
|
---|
713 | }
|
---|
714 | if (!EVP_DigestUpdate(mdctx, mess1, strlen(mess1))) {
|
---|
715 | printf("Message digest update failed.\n");
|
---|
716 | EVP_MD_CTX_free(mdctx);
|
---|
717 | exit(1);
|
---|
718 | }
|
---|
719 | if (!EVP_DigestUpdate(mdctx, mess2, strlen(mess2))) {
|
---|
720 | printf("Message digest update failed.\n");
|
---|
721 | EVP_MD_CTX_free(mdctx);
|
---|
722 | exit(1);
|
---|
723 | }
|
---|
724 | if (!EVP_DigestFinal_ex(mdctx, md_value, &md_len)) {
|
---|
725 | printf("Message digest finalization failed.\n");
|
---|
726 | EVP_MD_CTX_free(mdctx);
|
---|
727 | exit(1);
|
---|
728 | }
|
---|
729 | EVP_MD_CTX_free(mdctx);
|
---|
730 |
|
---|
731 | printf("Digest is: ");
|
---|
732 | for (i = 0; i < md_len; i++)
|
---|
733 | printf("%02x", md_value[i]);
|
---|
734 | printf("\n");
|
---|
735 |
|
---|
736 | exit(0);
|
---|
737 | }
|
---|
738 |
|
---|
739 | =head1 SEE ALSO
|
---|
740 |
|
---|
741 | L<EVP_MD_meth_new(3)>,
|
---|
742 | L<openssl-dgst(1)>,
|
---|
743 | L<evp(7)>,
|
---|
744 | L<OSSL_PROVIDER(3)>,
|
---|
745 | L<OSSL_PARAM(3)>,
|
---|
746 | L<property(7)>,
|
---|
747 | L<crypto(7)/ALGORITHM FETCHING>,
|
---|
748 | L<provider-digest(7)>,
|
---|
749 | L<life_cycle-digest(7)>
|
---|
750 |
|
---|
751 | The full list of digest algorithms are provided below.
|
---|
752 |
|
---|
753 | L<EVP_blake2b512(3)>,
|
---|
754 | L<EVP_md2(3)>,
|
---|
755 | L<EVP_md4(3)>,
|
---|
756 | L<EVP_md5(3)>,
|
---|
757 | L<EVP_mdc2(3)>,
|
---|
758 | L<EVP_ripemd160(3)>,
|
---|
759 | L<EVP_sha1(3)>,
|
---|
760 | L<EVP_sha224(3)>,
|
---|
761 | L<EVP_sha3_224(3)>,
|
---|
762 | L<EVP_sm3(3)>,
|
---|
763 | L<EVP_whirlpool(3)>
|
---|
764 |
|
---|
765 | =head1 HISTORY
|
---|
766 |
|
---|
767 | The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
|
---|
768 | EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
|
---|
769 |
|
---|
770 | The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
|
---|
771 | later, so now EVP_sha1() can be used with RSA and DSA.
|
---|
772 |
|
---|
773 | The EVP_dss1() function was removed in OpenSSL 1.1.0.
|
---|
774 |
|
---|
775 | The EVP_MD_CTX_set_pkey_ctx() function was added in OpenSSL 1.1.1.
|
---|
776 |
|
---|
777 | The EVP_Q_digest(), EVP_DigestInit_ex2(),
|
---|
778 | EVP_MD_fetch(), EVP_MD_free(), EVP_MD_up_ref(),
|
---|
779 | EVP_MD_get_params(), EVP_MD_CTX_set_params(), EVP_MD_CTX_get_params(),
|
---|
780 | EVP_MD_gettable_params(), EVP_MD_gettable_ctx_params(),
|
---|
781 | EVP_MD_settable_ctx_params(), EVP_MD_CTX_settable_params() and
|
---|
782 | EVP_MD_CTX_gettable_params() functions were added in OpenSSL 3.0.
|
---|
783 |
|
---|
784 | The EVP_MD_type(), EVP_MD_nid(), EVP_MD_name(), EVP_MD_pkey_type(),
|
---|
785 | EVP_MD_size(), EVP_MD_block_size(), EVP_MD_flags(), EVP_MD_CTX_size(),
|
---|
786 | EVP_MD_CTX_block_size(), EVP_MD_CTX_type(), and EVP_MD_CTX_md_data()
|
---|
787 | functions were renamed to include C<get> or C<get0> in their names in
|
---|
788 | OpenSSL 3.0, respectively. The old names are kept as non-deprecated
|
---|
789 | alias macros.
|
---|
790 |
|
---|
791 | The EVP_MD_CTX_md() function was deprecated in OpenSSL 3.0; use
|
---|
792 | EVP_MD_CTX_get0_md() instead.
|
---|
793 | EVP_MD_CTX_update_fn() and EVP_MD_CTX_set_update_fn() were deprecated
|
---|
794 | in OpenSSL 3.0.
|
---|
795 |
|
---|
796 | EVP_MD_CTX_dup() was added in OpenSSL 3.1.
|
---|
797 |
|
---|
798 | =head1 COPYRIGHT
|
---|
799 |
|
---|
800 | Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
|
---|
801 |
|
---|
802 | Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
803 | this file except in compliance with the License. You can obtain a copy
|
---|
804 | in the file LICENSE in the source distribution or at
|
---|
805 | L<https://www.openssl.org/source/license.html>.
|
---|
806 |
|
---|
807 | =cut
|
---|