xref: /freebsd-src/crypto/openssl/doc/man7/crypto.pod (revision aa7957345732816fb0ba8308798d2f79f45597f9)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5e71b7053SJung-uk Kimcrypto - OpenSSL cryptographic library
6e71b7053SJung-uk Kim
7e71b7053SJung-uk Kim=head1 SYNOPSIS
8e71b7053SJung-uk Kim
9e71b7053SJung-uk KimSee the individual manual pages for details.
10e71b7053SJung-uk Kim
11e71b7053SJung-uk Kim=head1 DESCRIPTION
12e71b7053SJung-uk Kim
13b077aed3SPierre ProncheryThe OpenSSL crypto library (C<libcrypto>) implements a wide range of
14b077aed3SPierre Proncherycryptographic algorithms used in various Internet standards. The services
15b077aed3SPierre Proncheryprovided by this library are used by the OpenSSL implementations of TLS and
16b077aed3SPierre ProncheryCMS, and they have also been used to implement many other third party products
17b077aed3SPierre Proncheryand protocols.
18e71b7053SJung-uk Kim
19b077aed3SPierre ProncheryThe functionality includes symmetric encryption, public key cryptography, key
20b077aed3SPierre Proncheryagreement, certificate handling, cryptographic hash functions, cryptographic
21b077aed3SPierre Proncherypseudo-random number generators, message authentication codes (MACs), key
22b077aed3SPierre Proncheryderivation functions (KDFs), and various utilities.
23e71b7053SJung-uk Kim
24b077aed3SPierre Pronchery=head2 Algorithms
25e71b7053SJung-uk Kim
26b077aed3SPierre ProncheryCryptographic primitives such as the SHA256 digest, or AES encryption are
27b077aed3SPierre Proncheryreferred to in OpenSSL as "algorithms". Each algorithm may have multiple
28b077aed3SPierre Proncheryimplementations available for use. For example the RSA algorithm is available as
29b077aed3SPierre Proncherya "default" implementation suitable for general use, and a "fips" implementation
30b077aed3SPierre Proncherywhich has been validated to FIPS standards for situations where that is
31b077aed3SPierre Proncheryimportant. It is also possible that a third party could add additional
32b077aed3SPierre Proncheryimplementations such as in a hardware security module (HSM).
33e71b7053SJung-uk Kim
34b077aed3SPierre Pronchery=head2 Operations
35b077aed3SPierre Pronchery
36b077aed3SPierre ProncheryDifferent algorithms can be grouped together by their purpose. For example there
37b077aed3SPierre Proncheryare algorithms for encryption, and different algorithms for digesting data.
38b077aed3SPierre ProncheryThese different groups are known as "operations" in OpenSSL. Each operation
39b077aed3SPierre Proncheryhas a different set of functions associated with it. For example to perform an
40b077aed3SPierre Proncheryencryption operation using AES (or any other encryption algorithm) you would use
41b077aed3SPierre Proncherythe encryption functions detailed on the L<EVP_EncryptInit(3)> page. Or to
42b077aed3SPierre Proncheryperform a digest operation using SHA256 then you would use the digesting
43b077aed3SPierre Proncheryfunctions on the L<EVP_DigestInit(3)> page.
44b077aed3SPierre Pronchery
45b077aed3SPierre Pronchery=head2 Providers
46b077aed3SPierre Pronchery
47b077aed3SPierre ProncheryA provider in OpenSSL is a component that collects together algorithm
48b077aed3SPierre Proncheryimplementations. In order to use an algorithm you must have at least one
49b077aed3SPierre Proncheryprovider loaded that contains an implementation of it. OpenSSL comes with a
50b077aed3SPierre Proncherynumber of providers and they may also be obtained from third parties. If you
51b077aed3SPierre Proncherydon't load a provider explicitly (either in program code or via config) then the
52b077aed3SPierre ProncheryOpenSSL built-in "default" provider will be automatically loaded.
53b077aed3SPierre Pronchery
54b077aed3SPierre Pronchery=head2 Library contexts
55b077aed3SPierre Pronchery
56b077aed3SPierre ProncheryA library context can be thought of as a "scope" within which configuration
57b077aed3SPierre Proncheryoptions take effect. When a provider is loaded, it is only loaded within the
58b077aed3SPierre Proncheryscope of a given library context. In this way it is possible for different
59b077aed3SPierre Proncherycomponents of a complex application to each use a different library context and
60b077aed3SPierre Proncheryhave different providers loaded with different configuration settings.
61b077aed3SPierre Pronchery
62b077aed3SPierre ProncheryIf an application does not explicitly create a library context then the
63b077aed3SPierre Pronchery"default" library context will be used.
64b077aed3SPierre Pronchery
65b077aed3SPierre ProncheryLibrary contexts are represented by the B<OSSL_LIB_CTX> type. Many OpenSSL API
66b077aed3SPierre Proncheryfunctions take a library context as a parameter. Applications can always pass
67b077aed3SPierre ProncheryB<NULL> for this parameter to just use the default library context.
68b077aed3SPierre Pronchery
69b077aed3SPierre ProncheryThe default library context is automatically created the first time it is
70b077aed3SPierre Proncheryneeded. This will automatically load any available configuration file and will
71b077aed3SPierre Proncheryinitialise OpenSSL for use. Unlike in earlier versions of OpenSSL (prior to
72b077aed3SPierre Pronchery1.1.0) no explicit initialisation steps need to be taken.
73b077aed3SPierre Pronchery
74b077aed3SPierre ProncherySimilarly when the application exits the default library context is
75b077aed3SPierre Proncheryautomatically destroyed. No explicit de-initialisation steps need to be taken.
76b077aed3SPierre Pronchery
77b077aed3SPierre ProncherySee L<OSSL_LIB_CTX(3)> for more information about library contexts.
78b077aed3SPierre ProncherySee also L</ALGORITHM FETCHING>.
79b077aed3SPierre Pronchery
80b077aed3SPierre Pronchery=head2 Multi-threaded applications
81b077aed3SPierre Pronchery
82b077aed3SPierre ProncheryAs long as OpenSSL has been built with support for threads (the default case
83b077aed3SPierre Proncheryon most platforms) then most OpenSSL I<functions> are thread-safe in the sense
84b077aed3SPierre Proncherythat it is safe to call the same function from multiple threads at the same
85b077aed3SPierre Proncherytime. However most OpenSSL I<data structures> are not thread-safe. For example
86b077aed3SPierre Proncherythe L<BIO_write(3)> and L<BIO_read(3)> functions are thread safe. However it
87b077aed3SPierre Proncherywould not be thread safe to call BIO_write() from one thread while calling
88b077aed3SPierre ProncheryBIO_read() in another where both functions are passed the same B<BIO> object
89b077aed3SPierre Proncherysince both of them may attempt to make changes to the same B<BIO> object.
90b077aed3SPierre Pronchery
91b077aed3SPierre ProncheryThere are exceptions to these rules. A small number of functions are not thread
92b077aed3SPierre Proncherysafe at all. Where this is the case this restriction should be noted in the
93b077aed3SPierre Proncherydocumentation for the function. Similarly some data structures may be partially
94b077aed3SPierre Proncheryor fully thread safe. For example it is safe to use an B<OSSL_LIB_CTX> in
95b077aed3SPierre Proncherymultiple threads.
96b077aed3SPierre Pronchery
97b077aed3SPierre ProncherySee L<openssl-threads(7)> for a more detailed discussion on OpenSSL threading
98b077aed3SPierre Proncherysupport.
99b077aed3SPierre Pronchery
100b077aed3SPierre Pronchery=head1 ALGORITHM FETCHING
101b077aed3SPierre Pronchery
102b077aed3SPierre ProncheryIn order to use an algorithm an implementation for it must first be "fetched".
103b077aed3SPierre ProncheryFetching is the process of looking through the available implementations,
104b077aed3SPierre Proncheryapplying selection criteria (via a property query string), and finally choosing
105b077aed3SPierre Proncherythe implementation that will be used.
106b077aed3SPierre Pronchery
107b077aed3SPierre ProncheryTwo types of fetching are supported by OpenSSL - explicit fetching and implicit
108b077aed3SPierre Proncheryfetching.
109b077aed3SPierre Pronchery
110b077aed3SPierre Pronchery=head2 Property query strings
111b077aed3SPierre Pronchery
112b077aed3SPierre ProncheryWhen fetching an algorithm it is possible to specify a property query string to
113b077aed3SPierre Proncheryguide the selection process. For example a property query string of
114b077aed3SPierre Pronchery"provider=default" could be used to force the selection to only consider
115b077aed3SPierre Proncheryalgorithm implementations in the default provider.
116b077aed3SPierre Pronchery
117b077aed3SPierre ProncheryProperty query strings can be specified explicitly as an argument to a function.
118b077aed3SPierre ProncheryIt is also possible to specify a default property query string for the whole
119b077aed3SPierre Proncherylibrary context using the L<EVP_set_default_properties(3)> or
120b077aed3SPierre ProncheryL<EVP_default_properties_enable_fips(3)> functions. Where both
121b077aed3SPierre Proncherydefault properties and function specific properties are specified then they are
122b077aed3SPierre Proncherycombined. Function specific properties will override default properties where
123b077aed3SPierre Proncherythere is a conflict.
124b077aed3SPierre Pronchery
125b077aed3SPierre ProncherySee L<property(7)> for more information about properties.
126b077aed3SPierre Pronchery
127b077aed3SPierre Pronchery=head2 Explicit fetching
128b077aed3SPierre Pronchery
129b077aed3SPierre ProncheryUsers of the OpenSSL libraries never query a provider directly for an algorithm
130b077aed3SPierre Proncheryimplementation. Instead, the diverse OpenSSL APIs often have explicit fetching
131b077aed3SPierre Proncheryfunctions that do the work, and they return an appropriate algorithm object back
132b077aed3SPierre Proncheryto the user. These functions usually have the name C<APINAME_fetch>, where
133b077aed3SPierre ProncheryC<APINAME> is the name of the operation. For example L<EVP_MD_fetch(3)> can
134b077aed3SPierre Proncherybe used to explicitly fetch a digest algorithm implementation. The user is
135b077aed3SPierre Proncheryresponsible for freeing the object returned from the C<APINAME_fetch> function
136b077aed3SPierre Proncheryusing C<APINAME_free> when it is no longer needed.
137b077aed3SPierre Pronchery
138b077aed3SPierre ProncheryThese fetching functions follow a fairly common pattern, where three
139b077aed3SPierre Proncheryarguments are passed:
140b077aed3SPierre Pronchery
141b077aed3SPierre Pronchery=over 4
142b077aed3SPierre Pronchery
143b077aed3SPierre Pronchery=item The library context
144b077aed3SPierre Pronchery
145b077aed3SPierre ProncherySee L<OSSL_LIB_CTX(3)> for a more detailed description.
146b077aed3SPierre ProncheryThis may be NULL to signify the default (global) library context, or a
147b077aed3SPierre Proncherycontext created by the user. Only providers loaded in this library context (see
148b077aed3SPierre ProncheryL<OSSL_PROVIDER_load(3)>) will be considered by the fetching function. In case
149b077aed3SPierre Proncheryno provider has been loaded in this library context then the default provider
150b077aed3SPierre Proncherywill be loaded as a fallback (see L<OSSL_PROVIDER-default(7)>).
151b077aed3SPierre Pronchery
152b077aed3SPierre Pronchery=item An identifier
153b077aed3SPierre Pronchery
154b077aed3SPierre ProncheryFor all currently implemented fetching functions this is the algorithm name.
155b077aed3SPierre Pronchery
156b077aed3SPierre Pronchery=item A property query string
157b077aed3SPierre Pronchery
158b077aed3SPierre ProncheryThe property query string used to guide selection of the algorithm
159b077aed3SPierre Proncheryimplementation.
160b077aed3SPierre Pronchery
161b077aed3SPierre Pronchery=back
162b077aed3SPierre Pronchery
163b077aed3SPierre ProncheryThe algorithm implementation that is fetched can then be used with other diverse
164b077aed3SPierre Proncheryfunctions that use them. For example the L<EVP_DigestInit_ex(3)> function takes
165b077aed3SPierre Proncheryas a parameter an B<EVP_MD> object which may have been returned from an earlier
166b077aed3SPierre Proncherycall to L<EVP_MD_fetch(3)>.
167b077aed3SPierre Pronchery
168b077aed3SPierre Pronchery=head2 Implicit fetching
169b077aed3SPierre Pronchery
170b077aed3SPierre ProncheryOpenSSL has a number of functions that return an algorithm object with no
171b077aed3SPierre Proncheryassociated implementation, such as L<EVP_sha256(3)>, L<EVP_aes_128_cbc(3)>,
172b077aed3SPierre ProncheryL<EVP_get_cipherbyname(3)> or L<EVP_get_digestbyname(3)>. These are present for
173b077aed3SPierre Proncherycompatibility with OpenSSL before version 3.0 where explicit fetching was not
174b077aed3SPierre Proncheryavailable.
175b077aed3SPierre Pronchery
176b077aed3SPierre ProncheryWhen they are used with functions like L<EVP_DigestInit_ex(3)> or
177b077aed3SPierre ProncheryL<EVP_CipherInit_ex(3)>, the actual implementation to be used is
178b077aed3SPierre Proncheryfetched implicitly using default search criteria.
179b077aed3SPierre Pronchery
180b077aed3SPierre ProncheryIn some cases implicit fetching can also occur when a NULL algorithm parameter
181b077aed3SPierre Proncheryis supplied. In this case an algorithm implementation is implicitly fetched
182b077aed3SPierre Proncheryusing default search criteria and an algorithm name that is consistent with
183b077aed3SPierre Proncherythe context in which it is being used.
184b077aed3SPierre Pronchery
185b077aed3SPierre ProncheryFunctions that revolve around B<EVP_PKEY_CTX> and L<EVP_PKEY(3)>, such as
186b077aed3SPierre ProncheryL<EVP_DigestSignInit(3)> and friends, all fetch the implementations
187b077aed3SPierre Proncheryimplicitly.  Because these functions involve both an operation type (such as
188b077aed3SPierre ProncheryL<EVP_SIGNATURE(3)>) and an L<EVP_KEYMGMT(3)> for the L<EVP_PKEY(3)>, they try
189b077aed3SPierre Proncherythe following:
190b077aed3SPierre Pronchery
191b077aed3SPierre Pronchery=over 4
192b077aed3SPierre Pronchery
193b077aed3SPierre Pronchery=item 1.
194b077aed3SPierre Pronchery
195b077aed3SPierre ProncheryFetch the operation type implementation from any provider given a library
196b077aed3SPierre Proncherycontext and property string stored in the B<EVP_PKEY_CTX>.
197b077aed3SPierre Pronchery
198b077aed3SPierre ProncheryIf the provider of the operation type implementation is different from the
199b077aed3SPierre Proncheryprovider of the L<EVP_PKEY(3)>'s L<EVP_KEYMGMT(3)> implementation, try to
200b077aed3SPierre Proncheryfetch a L<EVP_KEYMGMT(3)> implementation in the same provider as the operation
201b077aed3SPierre Proncherytype implementation and export the L<EVP_PKEY(3)> to it (effectively making a
202b077aed3SPierre Proncherytemporary copy of the original key).
203b077aed3SPierre Pronchery
204b077aed3SPierre ProncheryIf anything in this step fails, the next step is used as a fallback.
205b077aed3SPierre Pronchery
206b077aed3SPierre Pronchery=item 2.
207b077aed3SPierre Pronchery
208b077aed3SPierre ProncheryAs a fallback, try to fetch the operation type implementation from the same
209b077aed3SPierre Proncheryprovider as the original L<EVP_PKEY(3)>'s L<EVP_KEYMGMT(3)>, still using the
210*aa795734SPierre Proncheryproperty string from the B<EVP_PKEY_CTX>.
211b077aed3SPierre Pronchery
212b077aed3SPierre Pronchery=back
213b077aed3SPierre Pronchery
214b077aed3SPierre Pronchery=head2 Performance
215b077aed3SPierre Pronchery
216b077aed3SPierre ProncheryIf you perform the same operation many times then it is recommended to use
217b077aed3SPierre ProncheryL</Explicit fetching> to prefetch an algorithm once initially,
218b077aed3SPierre Proncheryand then pass this created object to any operations that are currently
219b077aed3SPierre Proncheryusing L</Implicit fetching>.
220b077aed3SPierre ProncherySee an example of Explicit fetching in L</USING ALGORITHMS IN APPLICATIONS>.
221b077aed3SPierre Pronchery
222b077aed3SPierre ProncheryPrior to OpenSSL 3.0, constant method tables (such as EVP_sha256()) were used
223b077aed3SPierre Proncherydirectly to access methods. If you pass one of these convenience functions
224b077aed3SPierre Proncheryto an operation the fixed methods are ignored, and only the name is used to
225b077aed3SPierre Proncheryinternally fetch methods from a provider.
226b077aed3SPierre Pronchery
227b077aed3SPierre ProncheryIf the prefetched object is not passed to operations, then any implicit
228b077aed3SPierre Proncheryfetch will use the internally cached prefetched object, but it will
229b077aed3SPierre Proncherystill be slower than passing the prefetched object directly.
230b077aed3SPierre Pronchery
231b077aed3SPierre ProncheryFetching via a provider offers more flexibility, but it is slower than the
232b077aed3SPierre Proncheryold method, since it must search for the algorithm in all loaded providers,
233b077aed3SPierre Proncheryand then populate the method table using provider supplied methods.
234b077aed3SPierre ProncheryInternally OpenSSL caches similar algorithms on the first fetch
235b077aed3SPierre Pronchery(so loading a digest caches all digests).
236b077aed3SPierre Pronchery
237b077aed3SPierre ProncheryThe following methods can be used for prefetching:
238b077aed3SPierre Pronchery
239b077aed3SPierre Pronchery=over 4
240b077aed3SPierre Pronchery
241b077aed3SPierre Pronchery=item L<EVP_MD_fetch(3)>
242b077aed3SPierre Pronchery
243b077aed3SPierre Pronchery=item L<EVP_CIPHER_fetch(3)>
244b077aed3SPierre Pronchery
245b077aed3SPierre Pronchery=item L<EVP_KDF_fetch(3)>
246b077aed3SPierre Pronchery
247b077aed3SPierre Pronchery=item L<EVP_MAC_fetch(3)>
248b077aed3SPierre Pronchery
249b077aed3SPierre Pronchery=item L<EVP_KEM_fetch(3)>
250b077aed3SPierre Pronchery
251b077aed3SPierre Pronchery=item L<OSSL_ENCODER_fetch(3)>
252b077aed3SPierre Pronchery
253b077aed3SPierre Pronchery=item L<OSSL_DECODER_fetch(3)>
254b077aed3SPierre Pronchery
255b077aed3SPierre Pronchery=item L<EVP_RAND_fetch(3)>
256b077aed3SPierre Pronchery
257b077aed3SPierre Pronchery=back
258b077aed3SPierre Pronchery
259b077aed3SPierre ProncheryThe following methods are used internally when performing operations:
260b077aed3SPierre Pronchery
261b077aed3SPierre Pronchery=over 4
262b077aed3SPierre Pronchery
263b077aed3SPierre Pronchery=item L<EVP_KEYMGMT_fetch(3)>
264b077aed3SPierre Pronchery
265b077aed3SPierre Pronchery=item L<EVP_KEYEXCH_fetch(3)>
266b077aed3SPierre Pronchery
267b077aed3SPierre Pronchery=item L<EVP_SIGNATURE_fetch(3)>
268b077aed3SPierre Pronchery
269b077aed3SPierre Pronchery=item L<OSSL_STORE_LOADER_fetch(3)>
270b077aed3SPierre Pronchery
271b077aed3SPierre Pronchery=back
272b077aed3SPierre Pronchery
273b077aed3SPierre ProncherySee L<OSSL_PROVIDER-default(7)>, <OSSL_PROVIDER-fips(7)> and
274b077aed3SPierre Pronchery<OSSL_PROVIDER-legacy(7)>for a list of algorithm names that
275b077aed3SPierre Proncherycan be fetched.
276b077aed3SPierre Pronchery
277b077aed3SPierre Pronchery=head1 FETCHING EXAMPLES
278b077aed3SPierre Pronchery
279b077aed3SPierre ProncheryThe following section provides a series of examples of fetching algorithm
280b077aed3SPierre Proncheryimplementations.
281b077aed3SPierre Pronchery
282b077aed3SPierre ProncheryFetch any available implementation of SHA2-256 in the default context. Note
283b077aed3SPierre Proncherythat some algorithms have aliases. So "SHA256" and "SHA2-256" are synonymous:
284b077aed3SPierre Pronchery
285b077aed3SPierre Pronchery EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", NULL);
286b077aed3SPierre Pronchery ...
287b077aed3SPierre Pronchery EVP_MD_free(md);
288b077aed3SPierre Pronchery
289b077aed3SPierre ProncheryFetch any available implementation of AES-128-CBC in the default context:
290b077aed3SPierre Pronchery
291b077aed3SPierre Pronchery EVP_CIPHER *cipher = EVP_CIPHER_fetch(NULL, "AES-128-CBC", NULL);
292b077aed3SPierre Pronchery ...
293b077aed3SPierre Pronchery EVP_CIPHER_free(cipher);
294b077aed3SPierre Pronchery
295b077aed3SPierre ProncheryFetch an implementation of SHA2-256 from the default provider in the default
296b077aed3SPierre Proncherycontext:
297b077aed3SPierre Pronchery
298b077aed3SPierre Pronchery EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", "provider=default");
299b077aed3SPierre Pronchery ...
300b077aed3SPierre Pronchery EVP_MD_free(md);
301b077aed3SPierre Pronchery
302b077aed3SPierre ProncheryFetch an implementation of SHA2-256 that is not from the default provider in the
303b077aed3SPierre Proncherydefault context:
304b077aed3SPierre Pronchery
305b077aed3SPierre Pronchery EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", "provider!=default");
306b077aed3SPierre Pronchery ...
307b077aed3SPierre Pronchery EVP_MD_free(md);
308b077aed3SPierre Pronchery
309b077aed3SPierre ProncheryFetch an implementation of SHA2-256 from the default provider in the specified
310b077aed3SPierre Proncherycontext:
311b077aed3SPierre Pronchery
312b077aed3SPierre Pronchery EVP_MD *md = EVP_MD_fetch(ctx, "SHA2-256", "provider=default");
313b077aed3SPierre Pronchery ...
314b077aed3SPierre Pronchery EVP_MD_free(md);
315b077aed3SPierre Pronchery
316b077aed3SPierre ProncheryLoad the legacy provider into the default context and then fetch an
317b077aed3SPierre Proncheryimplementation of WHIRLPOOL from it:
318b077aed3SPierre Pronchery
319b077aed3SPierre Pronchery /* This only needs to be done once - usually at application start up */
320b077aed3SPierre Pronchery OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy");
321b077aed3SPierre Pronchery
322b077aed3SPierre Pronchery EVP_MD *md = EVP_MD_fetch(NULL, "WHIRLPOOL", "provider=legacy");
323b077aed3SPierre Pronchery ...
324b077aed3SPierre Pronchery EVP_MD_free(md);
325b077aed3SPierre Pronchery
326b077aed3SPierre ProncheryNote that in the above example the property string "provider=legacy" is optional
327b077aed3SPierre Proncherysince, assuming no other providers have been loaded, the only implementation of
328b077aed3SPierre Proncherythe "whirlpool" algorithm is in the "legacy" provider. Also note that the
329b077aed3SPierre Proncherydefault provider should be explicitly loaded if it is required in addition to
330b077aed3SPierre Proncheryother providers:
331b077aed3SPierre Pronchery
332b077aed3SPierre Pronchery /* This only needs to be done once - usually at application start up */
333b077aed3SPierre Pronchery OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy");
334b077aed3SPierre Pronchery OSSL_PROVIDER *default = OSSL_PROVIDER_load(NULL, "default");
335b077aed3SPierre Pronchery
336b077aed3SPierre Pronchery EVP_MD *md_whirlpool = EVP_MD_fetch(NULL, "whirlpool", NULL);
337b077aed3SPierre Pronchery EVP_MD *md_sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL);
338b077aed3SPierre Pronchery ...
339b077aed3SPierre Pronchery EVP_MD_free(md_whirlpool);
340b077aed3SPierre Pronchery EVP_MD_free(md_sha256);
341b077aed3SPierre Pronchery
342b077aed3SPierre Pronchery=head1 OPENSSL PROVIDERS
343b077aed3SPierre Pronchery
344b077aed3SPierre ProncheryOpenSSL comes with a set of providers.
345b077aed3SPierre Pronchery
346b077aed3SPierre ProncheryThe algorithms available in each of these providers may vary due to build time
347b077aed3SPierre Proncheryconfiguration options. The L<openssl-list(1)> command can be used to list the
348b077aed3SPierre Proncherycurrently available algorithms.
349b077aed3SPierre Pronchery
350b077aed3SPierre ProncheryThe names of the algorithms shown from L<openssl-list(1)> can be used as an
351b077aed3SPierre Proncheryalgorithm identifier to the appropriate fetching function. Also see the provider
352b077aed3SPierre Proncheryspecific manual pages linked below for further details about using the
353b077aed3SPierre Proncheryalgorithms available in each of the providers.
354b077aed3SPierre Pronchery
355b077aed3SPierre ProncheryAs well as the OpenSSL providers third parties can also implement providers.
356b077aed3SPierre ProncheryFor information on writing a provider see L<provider(7)>.
357b077aed3SPierre Pronchery
358b077aed3SPierre Pronchery=head2 Default provider
359b077aed3SPierre Pronchery
360b077aed3SPierre ProncheryThe default provider is built in as part of the F<libcrypto> library and
361b077aed3SPierre Proncherycontains all of the most commonly used algorithm implementations. Should it be
362b077aed3SPierre Proncheryneeded (if other providers are loaded and offer implementations of the same
363b077aed3SPierre Proncheryalgorithms), the property query string "provider=default" can be used as a
364b077aed3SPierre Proncherysearch criterion for these implementations.  The default provider includes all
365b077aed3SPierre Proncheryof the functionality in the base provider below.
366b077aed3SPierre Pronchery
367b077aed3SPierre ProncheryIf you don't load any providers at all then the "default" provider will be
368b077aed3SPierre Proncheryautomatically loaded. If you explicitly load any provider then the "default"
369b077aed3SPierre Proncheryprovider would also need to be explicitly loaded if it is required.
370b077aed3SPierre Pronchery
371b077aed3SPierre ProncherySee L<OSSL_PROVIDER-default(7)>.
372b077aed3SPierre Pronchery
373b077aed3SPierre Pronchery=head2 Base provider
374b077aed3SPierre Pronchery
375b077aed3SPierre ProncheryThe base provider is built in as part of the F<libcrypto> library and contains
376b077aed3SPierre Proncheryalgorithm implementations for encoding and decoding for OpenSSL keys.
377b077aed3SPierre ProncheryShould it be needed (if other providers are loaded and offer
378b077aed3SPierre Proncheryimplementations of the same algorithms), the property query string
379b077aed3SPierre Pronchery"provider=base" can be used as a search criterion for these implementations.
380b077aed3SPierre ProncherySome encoding and decoding algorithm implementations are not FIPS algorithm
381b077aed3SPierre Proncheryimplementations in themselves but support algorithms from the FIPS provider and
382b077aed3SPierre Proncheryare allowed for use in "FIPS mode". The property query string "fips=yes" can be
383b077aed3SPierre Proncheryused to select such algorithms.
384b077aed3SPierre Pronchery
385b077aed3SPierre ProncherySee L<OSSL_PROVIDER-base(7)>.
386b077aed3SPierre Pronchery
387b077aed3SPierre Pronchery=head2 FIPS provider
388b077aed3SPierre Pronchery
389b077aed3SPierre ProncheryThe FIPS provider is a dynamically loadable module, and must therefore
390b077aed3SPierre Proncherybe loaded explicitly, either in code or through OpenSSL configuration
391b077aed3SPierre Pronchery(see L<config(5)>). It contains algorithm implementations that have been
392b077aed3SPierre Proncheryvalidated according to the FIPS 140-2 standard. Should it be needed (if other
393b077aed3SPierre Proncheryproviders are loaded and offer implementations of the same algorithms), the
394b077aed3SPierre Proncheryproperty query string "provider=fips" can be used as a search criterion for
395b077aed3SPierre Proncherythese implementations. All approved algorithm implementations in the FIPS
396b077aed3SPierre Proncheryprovider can also be selected with the property "fips=yes". The FIPS provider
397b077aed3SPierre Proncherymay also contain non-approved algorithm implementations and these can be
398b077aed3SPierre Proncheryselected with the property "fips=no".
399b077aed3SPierre Pronchery
400b077aed3SPierre ProncherySee L<OSSL_PROVIDER-FIPS(7)> and L<fips_module(7)>.
401b077aed3SPierre Pronchery
402b077aed3SPierre Pronchery=head2 Legacy provider
403b077aed3SPierre Pronchery
404b077aed3SPierre ProncheryThe legacy provider is a dynamically loadable module, and must therefore
405b077aed3SPierre Proncherybe loaded explicitly, either in code or through OpenSSL configuration
406b077aed3SPierre Pronchery(see L<config(5)>). It contains algorithm implementations that are considered
407b077aed3SPierre Proncheryinsecure, or are no longer in common use such as MD2 or RC4. Should it be needed
408b077aed3SPierre Pronchery(if other providers are loaded and offer implementations of the same algorithms),
409b077aed3SPierre Proncherythe property "provider=legacy" can be used as a search criterion for these
410b077aed3SPierre Proncheryimplementations.
411b077aed3SPierre Pronchery
412b077aed3SPierre ProncherySee L<OSSL_PROVIDER-legacy(7)>.
413b077aed3SPierre Pronchery
414b077aed3SPierre Pronchery=head2 Null provider
415b077aed3SPierre Pronchery
416b077aed3SPierre ProncheryThe null provider is built in as part of the F<libcrypto> library. It contains
417b077aed3SPierre Proncheryno algorithms in it at all. When fetching algorithms the default provider will
418b077aed3SPierre Proncherybe automatically loaded if no other provider has been explicitly loaded. To
419b077aed3SPierre Proncheryprevent that from happening you can explicitly load the null provider.
420b077aed3SPierre Pronchery
421b077aed3SPierre ProncherySee L<OSSL_PROVIDER-null(7)>.
422b077aed3SPierre Pronchery
423b077aed3SPierre Pronchery=head1 USING ALGORITHMS IN APPLICATIONS
424b077aed3SPierre Pronchery
425b077aed3SPierre ProncheryCryptographic algorithms are made available to applications through use of the
426b077aed3SPierre Pronchery"EVP" APIs. Each of the various operations such as encryption, digesting,
427b077aed3SPierre Proncherymessage authentication codes, etc., have a set of EVP function calls that can
428b077aed3SPierre Proncherybe invoked to use them. See the L<evp(7)> page for further details.
429b077aed3SPierre Pronchery
430b077aed3SPierre ProncheryMost of these follow a common pattern. A "context" object is first created. For
431b077aed3SPierre Proncheryexample for a digest operation you would use an B<EVP_MD_CTX>, and for an
432b077aed3SPierre Proncheryencryption/decryption operation you would use an B<EVP_CIPHER_CTX>. The
433b077aed3SPierre Proncheryoperation is then initialised ready for use via an "init" function - optionally
434b077aed3SPierre Proncherypassing in a set of parameters (using the L<OSSL_PARAM(3)> type) to configure how
435b077aed3SPierre Proncherythe operation should behave. Next data is fed into the operation in a series of
436b077aed3SPierre Pronchery"update" calls. The operation is finalised using a "final" call which will
437b077aed3SPierre Proncherytypically provide some kind of output. Finally the context is cleaned up and
438b077aed3SPierre Proncheryfreed.
439b077aed3SPierre Pronchery
440b077aed3SPierre ProncheryThe following shows a complete example for doing this process for digesting
441b077aed3SPierre Proncherydata using SHA256. The process is similar for other operations such as
442b077aed3SPierre Proncheryencryption/decryption, signatures, message authentication codes, etc.
443b077aed3SPierre Pronchery
444b077aed3SPierre Pronchery #include <stdio.h>
445b077aed3SPierre Pronchery #include <openssl/evp.h>
446b077aed3SPierre Pronchery #include <openssl/bio.h>
447b077aed3SPierre Pronchery #include <openssl/err.h>
448b077aed3SPierre Pronchery
449b077aed3SPierre Pronchery int main(void)
450b077aed3SPierre Pronchery {
451b077aed3SPierre Pronchery     EVP_MD_CTX *ctx = NULL;
452b077aed3SPierre Pronchery     EVP_MD *sha256 = NULL;
453b077aed3SPierre Pronchery     const unsigned char msg[] = {
454b077aed3SPierre Pronchery         0x00, 0x01, 0x02, 0x03
455b077aed3SPierre Pronchery     };
456b077aed3SPierre Pronchery     unsigned int len = 0;
457b077aed3SPierre Pronchery     unsigned char *outdigest = NULL;
458b077aed3SPierre Pronchery     int ret = 1;
459b077aed3SPierre Pronchery
460b077aed3SPierre Pronchery     /* Create a context for the digest operation */
461b077aed3SPierre Pronchery     ctx = EVP_MD_CTX_new();
462b077aed3SPierre Pronchery     if (ctx == NULL)
463b077aed3SPierre Pronchery         goto err;
464b077aed3SPierre Pronchery
465b077aed3SPierre Pronchery     /*
466b077aed3SPierre Pronchery      * Fetch the SHA256 algorithm implementation for doing the digest. We're
467b077aed3SPierre Pronchery      * using the "default" library context here (first NULL parameter), and
468b077aed3SPierre Pronchery      * we're not supplying any particular search criteria for our SHA256
469b077aed3SPierre Pronchery      * implementation (second NULL parameter). Any SHA256 implementation will
470b077aed3SPierre Pronchery      * do.
471b077aed3SPierre Pronchery      * In a larger application this fetch would just be done once, and could
472b077aed3SPierre Pronchery      * be used for multiple calls to other operations such as EVP_DigestInit_ex().
473b077aed3SPierre Pronchery      */
474b077aed3SPierre Pronchery     sha256 = EVP_MD_fetch(NULL, "SHA256", NULL);
475b077aed3SPierre Pronchery     if (sha256 == NULL)
476b077aed3SPierre Pronchery         goto err;
477b077aed3SPierre Pronchery
478b077aed3SPierre Pronchery    /* Initialise the digest operation */
479b077aed3SPierre Pronchery    if (!EVP_DigestInit_ex(ctx, sha256, NULL))
480b077aed3SPierre Pronchery        goto err;
481b077aed3SPierre Pronchery
482b077aed3SPierre Pronchery     /*
483b077aed3SPierre Pronchery      * Pass the message to be digested. This can be passed in over multiple
484b077aed3SPierre Pronchery      * EVP_DigestUpdate calls if necessary
485b077aed3SPierre Pronchery      */
486b077aed3SPierre Pronchery     if (!EVP_DigestUpdate(ctx, msg, sizeof(msg)))
487b077aed3SPierre Pronchery         goto err;
488b077aed3SPierre Pronchery
489b077aed3SPierre Pronchery     /* Allocate the output buffer */
490b077aed3SPierre Pronchery     outdigest = OPENSSL_malloc(EVP_MD_get_size(sha256));
491b077aed3SPierre Pronchery     if (outdigest == NULL)
492b077aed3SPierre Pronchery         goto err;
493b077aed3SPierre Pronchery
494b077aed3SPierre Pronchery     /* Now calculate the digest itself */
495b077aed3SPierre Pronchery     if (!EVP_DigestFinal_ex(ctx, outdigest, &len))
496b077aed3SPierre Pronchery         goto err;
497b077aed3SPierre Pronchery
498b077aed3SPierre Pronchery     /* Print out the digest result */
499b077aed3SPierre Pronchery     BIO_dump_fp(stdout, outdigest, len);
500b077aed3SPierre Pronchery
501b077aed3SPierre Pronchery     ret = 0;
502b077aed3SPierre Pronchery
503b077aed3SPierre Pronchery  err:
504b077aed3SPierre Pronchery     /* Clean up all the resources we allocated */
505b077aed3SPierre Pronchery     OPENSSL_free(outdigest);
506b077aed3SPierre Pronchery     EVP_MD_free(sha256);
507b077aed3SPierre Pronchery     EVP_MD_CTX_free(ctx);
508b077aed3SPierre Pronchery     if (ret != 0)
509b077aed3SPierre Pronchery        ERR_print_errors_fp(stderr);
510b077aed3SPierre Pronchery     return ret;
511b077aed3SPierre Pronchery }
512b077aed3SPierre Pronchery
513b077aed3SPierre Pronchery=head1 CONFIGURATION
514b077aed3SPierre Pronchery
515b077aed3SPierre ProncheryBy default OpenSSL will load a configuration file when it is first used. This
516b077aed3SPierre Proncherywill set up various configuration settings within the default library context.
517b077aed3SPierre ProncheryApplications that create their own library contexts may optionally configure
518b077aed3SPierre Proncherythem with a config file using the L<OSSL_LIB_CTX_load_config(3)> function.
519b077aed3SPierre Pronchery
520b077aed3SPierre ProncheryThe configuration file can be used to automatically load providers and set up
521b077aed3SPierre Proncherydefault property query strings.
522b077aed3SPierre Pronchery
523b077aed3SPierre ProncheryFor information on the OpenSSL configuration file format see L<config(5)>.
524b077aed3SPierre Pronchery
525b077aed3SPierre Pronchery=head1 ENCODING AND DECODING KEYS
526b077aed3SPierre Pronchery
527b077aed3SPierre ProncheryMany algorithms require the use of a key. Keys can be generated dynamically
528b077aed3SPierre Proncheryusing the EVP APIs (for example see L<EVP_PKEY_Q_keygen(3)>). However it is often
529b077aed3SPierre Proncherynecessary to save or load keys (or their associated parameters) to or from some
530b077aed3SPierre Proncheryexternal format such as PEM or DER (see L<openssl-glossary(7)>). OpenSSL uses
531b077aed3SPierre Proncheryencoders and decoders to perform this task.
532b077aed3SPierre Pronchery
533b077aed3SPierre ProncheryEncoders and decoders are just algorithm implementations in the same way as
534b077aed3SPierre Proncheryany other algorithm implementation in OpenSSL. They are implemented by
535b077aed3SPierre Proncheryproviders. The OpenSSL encoders and decoders are available in the default
536b077aed3SPierre Proncheryprovider. They are also duplicated in the base provider.
537b077aed3SPierre Pronchery
538b077aed3SPierre ProncheryFor information about encoders see L<OSSL_ENCODER_CTX_new_for_pkey(3)>. For
539b077aed3SPierre Proncheryinformation about decoders see L<OSSL_DECODER_CTX_new_for_pkey(3)>.
540b077aed3SPierre Pronchery
541b077aed3SPierre Pronchery=head1 LIBRARY CONVENTIONS
542b077aed3SPierre Pronchery
543b077aed3SPierre ProncheryMany OpenSSL functions that "get" or "set" a value follow a naming convention
544b077aed3SPierre Proncheryusing the numbers B<0> and B<1>, i.e. "get0", "get1", "set0" and "set1". This
545b077aed3SPierre Proncherycan also apply to some functions that "add" a value to an existing set, i.e.
546b077aed3SPierre Pronchery"add0" and "add1".
547b077aed3SPierre Pronchery
548b077aed3SPierre ProncheryFor example the functions:
549e71b7053SJung-uk Kim
550e71b7053SJung-uk Kim int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev);
551e71b7053SJung-uk Kim int X509_add1_trust_object(X509 *x, const ASN1_OBJECT *obj);
552e71b7053SJung-uk Kim
553b077aed3SPierre ProncheryIn the B<0> version the ownership of the object is passed to (for an add or set)
554b077aed3SPierre Proncheryor retained by (for a get) the parent object. For example after calling the
555b077aed3SPierre ProncheryX509_CRL_add0_revoked() function above, ownership of the I<rev> object is passed
556b077aed3SPierre Proncheryto the I<crl> object. Therefore, after calling this function I<rev> should not
557b077aed3SPierre Proncherybe freed directly. It will be freed implicitly when I<crl> is freed.
558e71b7053SJung-uk Kim
559b077aed3SPierre ProncheryIn the B<1> version the ownership of the object is not passed to or retained by
560b077aed3SPierre Proncherythe parent object. Instead a copy or "up ref" of the object is performed. So
561b077aed3SPierre Proncheryafter calling the X509_add1_trust_object() function above the application will
562b077aed3SPierre Proncherystill be responsible for freeing the I<obj> value where appropriate.
563e71b7053SJung-uk Kim
564e71b7053SJung-uk Kim=head1 SEE ALSO
565e71b7053SJung-uk Kim
566b077aed3SPierre ProncheryL<openssl(1)>, L<ssl(7)>, L<evp(7)>, L<OSSL_LIB_CTX(3)>, L<openssl-threads(7)>,
567b077aed3SPierre ProncheryL<property(7)>, L<OSSL_PROVIDER-default(7)>, L<OSSL_PROVIDER-base(7)>,
568b077aed3SPierre ProncheryL<OSSL_PROVIDER-FIPS(7)>, L<OSSL_PROVIDER-legacy(7)>, L<OSSL_PROVIDER-null(7)>,
569b077aed3SPierre ProncheryL<openssl-glossary(7)>, L<provider(7)>
570e71b7053SJung-uk Kim
571e71b7053SJung-uk Kim=head1 COPYRIGHT
572e71b7053SJung-uk Kim
573b077aed3SPierre ProncheryCopyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
574e71b7053SJung-uk Kim
575b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
576e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
577e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
578e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
579e71b7053SJung-uk Kim
580e71b7053SJung-uk Kim=cut
581