576ea581b49372697bc0a68ab0bf516c8c48de9c
[openssl.git] / doc / man7 / provider-digest.pod
1 =pod
2
3 =head1 NAME
4
5 provider-digest - The digest library E<lt>-E<gt> provider functions
6
7 =head1 SYNOPSIS
8
9 =for openssl multiple includes
10
11  #include <openssl/core_numbers.h>
12  #include <openssl/core_names.h>
13
14  /*
15   * Digests support the following function signatures in OSSL_DISPATCH arrays.
16   * (The function signatures are not actual functions).
17   */
18
19  /* Context management */
20  void *OP_digest_newctx(void *provctx);
21  void OP_digest_freectx(void *dctx);
22  void *OP_digest_dupctx(void *dctx);
23
24  /* Digest generation */
25  int OP_digest_init(void *dctx);
26  int OP_digest_update(void *dctx, const unsigned char *in, size_t inl);
27  int OP_digest_final(void *dctx, unsigned char *out, size_t *outl,
28                      size_t outsz);
29  int OP_digest_digest(void *provctx, const unsigned char *in, size_t inl,
30                       unsigned char *out, size_t *outl, size_t outsz);
31
32  /* Digest parameter descriptors */
33  const OSSL_PARAM *OP_digest_gettable_params(void);
34
35  /* Digest operation parameter descriptors */
36  const OSSL_PARAM *OP_digest_gettable_ctx_params(void);
37  const OSSL_PARAM *OP_digest_settable_ctx_params(void);
38
39  /* Digest parameters */
40  int OP_digest_get_params(OSSL_PARAM params[]);
41
42  /* Digest operation parameters */
43  int OP_digest_set_ctx_params(void *dctx, const OSSL_PARAM params[]);
44  int OP_digest_get_ctx_params(void *dctx, OSSL_PARAM params[]);
45
46 =head1 DESCRIPTION
47
48 This documentation is primarily aimed at provider authors. See L<provider(7)>
49 for further information.
50
51 The DIGEST operation enables providers to implement digest algorithms and make
52 them available to applications via the API functions L<EVP_DigestInit_ex(3)>,
53 L<EVP_DigestUpdate(3)> and L<EVP_DigestFinal(3)> (and other related functions).
54
55 All "functions" mentioned here are passed as function pointers between
56 F<libcrypto> and the provider in B<OSSL_DISPATCH> arrays via
57 B<OSSL_ALGORITHM> arrays that are returned by the provider's
58 provider_query_operation() function
59 (see L<provider-base(7)/Provider Functions>).
60
61 All these "functions" have a corresponding function type definition
62 named B<OSSL_{name}_fn>, and a helper function to retrieve the
63 function pointer from an B<OSSL_DISPATCH> element named
64 B<OSSL_get_{name}>.
65 For example, the "function" OP_digest_newctx() has these:
66
67  typedef void *(OSSL_OP_digest_newctx_fn)(void *provctx);
68  static ossl_inline OSSL_OP_digest_newctx_fn
69      OSSL_get_OP_digest_newctx(const OSSL_DISPATCH *opf);
70
71 B<OSSL_DISPATCH> arrays are indexed by numbers that are provided as
72 macros in L<openssl-core_numbers.h(7)>, as follows:
73
74  OP_digest_newctx               OSSL_FUNC_DIGEST_NEWCTX
75  OP_digest_freectx              OSSL_FUNC_DIGEST_FREECTX
76  OP_digest_dupctx               OSSL_FUNC_DIGEST_DUPCTX
77
78  OP_digest_init                 OSSL_FUNC_DIGEST_INIT
79  OP_digest_update               OSSL_FUNC_DIGEST_UPDATE
80  OP_digest_final                OSSL_FUNC_DIGEST_FINAL
81  OP_digest_digest               OSSL_FUNC_DIGEST_DIGEST
82
83  OP_digest_get_params           OSSL_FUNC_DIGEST_GET_PARAMS
84  OP_digest_get_ctx_params       OSSL_FUNC_DIGEST_GET_CTX_PARAMS
85  OP_digest_set_ctx_params       OSSL_FUNC_DIGEST_SET_CTX_PARAMS
86
87  OP_digest_gettable_params      OSSL_FUNC_DIGEST_GETTABLE_PARAMS
88  OP_digest_gettable_ctx_params  OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS
89  OP_digest_settable_ctx_params  OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS
90
91 A digest algorithm implementation may not implement all of these functions.
92 In order to be usable all or none of OP_digest_newctx, OP_digest_freectx,
93 OP_digest_init, OP_digest_update and OP_digest_final should be implemented.
94 All other functions are optional.
95
96 =head2 Context Management Functions
97
98 OP_digest_newctx() should create and return a pointer to a provider side
99 structure for holding context information during a digest operation.
100 A pointer to this context will be passed back in a number of the other digest
101 operation function calls.
102 The parameter I<provctx> is the provider context generated during provider
103 initialisation (see L<provider(7)>).
104
105 OP_digest_freectx() is passed a pointer to the provider side digest context in
106 the I<dctx> parameter.
107 This function should free any resources associated with that context.
108
109 OP_digest_dupctx() should duplicate the provider side digest context in the
110 I<dctx> parameter and return the duplicate copy.
111
112 =head2 Digest Generation Functions
113
114 OP_digest_init() initialises a digest operation given a newly created
115 provider side digest context in the I<dctx> parameter.
116
117 OP_digest_update() is called to supply data to be digested as part of a
118 previously initialised digest operation.
119 The I<dctx> parameter contains a pointer to a previously initialised provider
120 side context.
121 OP_digest_update() should digest I<inl> bytes of data at the location pointed to
122 by I<in>.
123 OP_digest_update() may be called multiple times for a single digest operation.
124
125 OP_digest_final() generates a digest started through previous OP_digest_init()
126 and OP_digest_update() calls.
127 The I<dctx> parameter contains a pointer to the provider side context.
128 The digest should be written to I<*out> and the length of the digest to
129 I<*outl>.
130 The digest should not exceed I<outsz> bytes.
131
132 OP_digest_digest() is a "oneshot" digest function.
133 No provider side digest context is used.
134 Instead the provider context that was created during provider initialisation is
135 passed in the I<provctx> parameter (see L<provider(7)>).
136 I<inl> bytes at I<in> should be digested and the result should be stored at
137 I<out>. The length of the digest should be stored in I<*outl> which should not
138 exceed I<outsz> bytes.
139
140 =head2 Digest Parameters
141
142 See L<OSSL_PARAM(3)> for further details on the parameters structure used by
143 these functions.
144
145 OP_digest_get_params() gets details of the algorithm implementation
146 and stores them in I<params>.
147
148 OP_digest_set_ctx_params() sets digest operation parameters for the
149 provider side digest context I<dctx> to I<params>.
150 Any parameter settings are additional to any that were previously set.
151
152 OP_digest_get_ctx_params() gets digest operation details details from
153 the given provider side digest context I<dctx> and stores them in I<params>.
154
155 OP_digest_gettable_params(), OP_digest_gettable_ctx_params(), and
156 OP_digest_settable_ctx_params() all return constant B<OSSL_PARAM> arrays
157 as descriptors of the parameters that OP_digest_get_params(),
158 OP_digest_get_ctx_params(), and OP_digest_set_ctx_params() can handle,
159 respectively.
160
161 Parameters currently recognised by built-in digests with this function
162 are as follows. Not all parameters are relevant to, or are understood
163 by all digests:
164
165 =over 4
166
167 =item "blocksize" (B<OSSL_DIGEST_PARAM_BLOCK_SIZE>) <unsigned integer>
168
169 The digest block size.
170 The length of the "blocksize" parameter should not exceed that of a B<size_t>.
171
172 =item "size" (B<OSSL_DIGEST_PARAM_SIZE>) <unsigned integer>
173
174 The digest output size.
175 The length of the "size" parameter should not exceed that of a B<size_t>.
176
177 =item "flags" (B<OSSL_DIGEST_PARAM_FLAGS>) <unsigned integer>
178
179 Diverse flags that describe exceptional behaviour for the digest:
180
181 =over 4
182
183 =item B<EVP_MD_FLAG_ONESHOT>
184
185 This digest method can only handle one block of input.
186
187 =item B<EVP_MD_FLAG_XOF>
188
189 This digest method is an extensible-output function (XOF) and supports
190 setting the B<OSSL_DIGEST_PARAM_XOFLEN> parameter.
191
192 =item B<EVP_MD_FLAG_DIGALGID_NULL>
193
194 When setting up a DigestAlgorithmIdentifier, this flag will have the
195 parameter set to NULL by default.  Use this for PKCS#1.  I<Note: if
196 combined with EVP_MD_FLAG_DIGALGID_ABSENT, the latter will override.>
197
198 =item B<EVP_MD_FLAG_DIGALGID_ABSENT>
199
200 When setting up a DigestAlgorithmIdentifier, this flag will have the
201 parameter be left absent by default.  I<Note: if combined with
202 EVP_MD_FLAG_DIGALGID_NULL, the latter will be overridden.>
203
204 =item B<EVP_MD_FLAG_DIGALGID_CUSTOM>
205
206 Custom DigestAlgorithmIdentifier handling via ctrl, with
207 B<EVP_MD_FLAG_DIGALGID_ABSENT> as default.  I<Note: if combined with
208 EVP_MD_FLAG_DIGALGID_NULL, the latter will be overridden.>
209 Currently unused.
210
211 =back
212
213 The length of the "flags" parameter should equal that of an
214 B<unsigned long int>.
215
216 =back
217
218 =head2 Digest Context Parameters
219
220 OP_digest_set_ctx_params() sets digest parameters associated with the
221 given provider side digest context I<dctx> to I<params>.
222 Any parameter settings are additional to any that were previously set.
223 See L<OSSL_PARAM(3)> for further details on the parameters structure.
224
225 OP_digest_get_ctx_params() gets details of currently set parameters
226 values associated with the give provider side digest context I<dctx>
227 and stores them in I<params>.
228 See L<OSSL_PARAM(3)> for further details on the parameters structure.
229
230 Parameters currently recognised by built-in digests are as follows. Not all
231 parameters are relevant to, or are understood by all digests:
232
233 =over 4
234
235 =item "xoflen" (B<OSSL_DIGEST_PARAM_XOFLEN>) <unsigned integer>
236
237 Sets the digest length for extendable output functions.
238 The length of the "xoflen" parameter should not exceed that of a B<size_t>.
239
240 =item "ssl3-ms" (B<OSSL_DIGEST_PARAM_SSL3_MS>) <octet string>
241
242 This parameter is set by libssl in order to calculate a signature hash for an
243 SSLv3 CertificateVerify message as per RFC6101.
244 It is only set after all handshake messages have already been digested via
245 OP_digest_update() calls.
246 The parameter provides the master secret value to be added to the digest.
247 The digest implementation should calculate the complete digest as per RFC6101
248 section 5.6.8.
249 The next call after setting this parameter will be OP_digest_final().
250 This is only relevant for implementations of SHA1 or MD5_SHA1.
251
252 =item "pad_type" (B<OSSL_DIGEST_PARAM_PAD_TYPE>) <unsigned integer>
253
254 Sets the pad type to be used.
255 The only built-in digest that uses this is MDC2.
256 Normally the final MDC2 block is padded with 0s.
257 If the pad type is set to 2 then the final block is padded with 0x80 followed by
258 0s.
259
260 =item "micalg" (B<OSSL_DIGEST_PARAM_MICALG>) <UTF8 string>
261
262 Gets the digest Message Integrity Check algorithm string.
263 This is used when creating S/MIME multipart/signed messages, as specified in
264 RFC 5751.
265
266 =back
267
268 =head1 RETURN VALUES
269
270 OP_digest_newctx() and OP_digest_dupctx() should return the newly created
271 provider side digest context, or NULL on failure.
272
273 OP_digest_init(), OP_digest_update(), OP_digest_final(), OP_digest_digest(),
274 OP_digest_set_params() and OP_digest_get_params() should return 1 for success or
275 0 on error.
276
277 OP_digest_size() should return the digest size.
278
279 OP_digest_block_size() should return the block size of the underlying digest
280 algorithm.
281
282 =head1 SEE ALSO
283
284 L<provider(7)>
285
286 =head1 HISTORY
287
288 The provider DIGEST interface was introduced in OpenSSL 3.0.
289
290 =head1 COPYRIGHT
291
292 Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
293
294 Licensed under the Apache License 2.0 (the "License").  You may not use
295 this file except in compliance with the License.  You can obtain a copy
296 in the file LICENSE in the source distribution or at
297 L<https://www.openssl.org/source/license.html>.
298
299 =cut