]> git.ipfire.org Git - thirdparty/openssl.git/blob - doc/man7/crypto.pod
b45404cce0e9f98b0bbc5a680624baa2f64a7ff9
[thirdparty/openssl.git] / doc / man7 / crypto.pod
1 =pod
2
3 =head1 NAME
4
5 crypto - OpenSSL cryptographic library
6
7 =head1 SYNOPSIS
8
9 See the individual manual pages for details.
10
11 =head1 DESCRIPTION
12
13 The OpenSSL crypto library (C<libcrypto>) implements a wide range of
14 cryptographic algorithms used in various Internet standards. The services
15 provided by this library are used by the OpenSSL implementations of TLS and
16 CMS, and they have also been used to implement many other third party products
17 and protocols.
18
19 The functionality includes symmetric encryption, public key cryptography, key
20 agreement, certificate handling, cryptographic hash functions, cryptographic
21 pseudo-random number generators, message authentication codes (MACs), key
22 derivation functions (KDFs), and various utilities.
23
24 =head2 Algorithms
25
26 Cryptographic primitives such as the SHA256 digest, or AES encryption are
27 referred to in OpenSSL as "algorithms". Each algorithm may have multiple
28 implementations available for use. For example the RSA algorithm is available as
29 a "default" implementation suitable for general use, and a "fips" implementation
30 which has been validated to FIPS standards for situations where that is
31 important. It is also possible that a third party could add additional
32 implementations such as in a hardware security module (HSM).
33
34 =head2 Operations
35
36 Different algorithms can be grouped together by their purpose. For example there
37 are algorithms for encryption, and different algorithms for digesting data.
38 These different groups are known as "operations" in OpenSSL. Each operation
39 has a different set of functions associated with it. For example to perform an
40 encryption operation using AES (or any other encryption algorithm) you would use
41 the encryption functions detailed on the L<EVP_EncryptInit(3)> page. Or to
42 perform a digest operation using SHA256 then you would use the digesting
43 functions on the L<EVP_DigestInit(3)> page.
44
45 =head2 Providers
46
47 A provider in OpenSSL is a component that collects together algorithm
48 implementations. In order to use an algorithm you must have at least one
49 provider loaded that contains an implementation of it. OpenSSL comes with a
50 number of providers and they may also be obtained from third parties. If you
51 don't load a provider explicitly (either in program code or via config) then the
52 OpenSSL built-in "default" provider will be automatically loaded.
53
54 =head2 Library contexts
55
56 A library context can be thought of as a "scope" within which configuration
57 options take effect. When a provider is loaded, it is only loaded within the
58 scope of a given library context. In this way it is possible for different
59 components of a complex application to each use a different library context and
60 have different providers loaded with different configuration settings.
61
62 If an application does not explicitly create a library context then the
63 "default" library context will be used.
64
65 Library contexts are represented by the B<OSSL_LIB_CTX> type. Many OpenSSL API
66 functions take a library context as a parameter. Applications can always pass
67 B<NULL> for this parameter to just use the default library context.
68
69 The default library context is automatically created the first time it is
70 needed. This will automatically load any available configuration file and will
71 initialise OpenSSL for use. Unlike in earlier versions of OpenSSL (prior to
72 1.1.0) no explicit initialisation steps need to be taken.
73
74 Similarly when the application exits the default library context is
75 automatically destroyed. No explicit de-initialisation steps need to be taken.
76
77 See L<OSSL_LIB_CTX(3)> for more information about library contexts.
78 See also L</ALGORITHM FETCHING>.
79
80 =head2 Multi-threaded applications
81
82 As long as OpenSSL has been built with support for threads (the default case
83 on most platforms) then most OpenSSL I<functions> are thread-safe in the sense
84 that it is safe to call the same function from multiple threads at the same
85 time. However most OpenSSL I<data structures> are not thread-safe. For example
86 the L<BIO_write(3)> and L<BIO_read(3)> functions are thread safe. However it
87 would not be thread safe to call BIO_write() from one thread while calling
88 BIO_read() in another where both functions are passed the same B<BIO> object
89 since both of them may attempt to make changes to the same B<BIO> object.
90
91 There are exceptions to these rules. A small number of functions are not thread
92 safe at all. Where this is the case this restriction should be noted in the
93 documentation for the function. Similarly some data structures may be partially
94 or fully thread safe. For example it is safe to use an B<OSSL_LIB_CTX> in
95 multiple threads.
96
97 See L<openssl-threads(7)> for a more detailed discussion on OpenSSL threading
98 support.
99
100 =head1 ALGORITHM FETCHING
101
102 In order to use an algorithm an implementation for it must first be "fetched".
103 Fetching is the process of looking through the available implementations,
104 applying selection criteria (via a property query string), and finally choosing
105 the implementation that will be used.
106
107 Two types of fetching are supported by OpenSSL - explicit fetching and implicit
108 fetching.
109
110 =head2 Property query strings
111
112 When fetching an algorithm it is possible to specify a property query string to
113 guide the selection process. For example a property query string of
114 "provider=default" could be used to force the selection to only consider
115 algorithm implementations in the default provider.
116
117 Property query strings can be specified explicitly as an argument to a function.
118 It is also possible to specify a default property query string for the whole
119 library context using the L<EVP_set_default_properties(3)> function. Where both
120 default properties and function specific properties are specified then they are
121 combined. Function specific properties will override default properties where
122 there is a conflict.
123
124 See L<property(7)> for more information about properties.
125
126 =head2 Explicit fetching
127
128 Users of the OpenSSL libraries never query a provider directly for an algorithm
129 implementation. Instead, the diverse OpenSSL APIs often have explicit fetching
130 functions that do the work, and they return an appropriate algorithm object back
131 to the user. These functions usually have the name C<APINAME_fetch>, where
132 C<APINAME> is the name of the operation. For example L<EVP_MD_fetch(3)> can
133 be used to explicitly fetch a digest algorithm implementation. The user is
134 responsible for freeing the object returned from the C<APINAME_fetch> function
135 using C<APINAME_free> when it is no longer needed.
136
137 These fetching functions follow a fairly common pattern, where three
138 arguments are passed:
139
140 =over 4
141
142 =item The library context
143
144 See L<OSSL_LIB_CTX(3)> for a more detailed description.
145 This may be NULL to signify the default (global) library context, or a
146 context created by the user. Only providers loaded in this library context (see
147 L<OSSL_PROVIDER_load(3)>) will be considered by the fetching function. In case
148 no provider has been loaded in this library context then the default provider
149 will be loaded as a fallback (see L<OSSL_PROVIDER-default(7)>).
150
151 =item An identifier
152
153 For all currently implemented fetching functions this is the algorithm name.
154
155 =item A property query string
156
157 The property query string used to guide selection of the algorithm
158 implementation.
159
160 =back
161
162 The algorithm implementation that is fetched can then be used with other diverse
163 functions that use them. For example the L<EVP_DigestInit_ex(3)> function takes
164 as a parameter an B<EVP_MD> object which may have been returned from an earlier
165 call to L<EVP_MD_fetch(3)>.
166
167 =head2 Implicit fetch
168
169 OpenSSL has a number of functions that return an algorithm object with no
170 associated implementation, such as L<EVP_sha256(3)>,
171 L<EVP_blake2b512(3)> or L<EVP_aes_128_cbc(3)>. These are present for
172 compatibility with OpenSSL before version 3.0 where explicit fetching was not
173 available.
174
175 When they are used with functions like L<EVP_DigestInit_ex(3)> or
176 L<EVP_CipherInit_ex(3)>, the actual implementation to be used is
177 fetched implicitly using default search criteria.
178
179 In some cases implicit fetching can also occur when a NULL algorithm parameter
180 is supplied. In this case an algorithm implementation is implicitly fetched
181 using default search criteria and an algorithm name that is consistent with
182 the context in which it is being used.
183
184 =head1 FETCHING EXAMPLES
185
186 The following section provides a series of examples of fetching algorithm
187 implementations.
188
189 Fetch any available implementation of SHA2-256 in the default context. Note
190 that some algorithms have aliases. So "SHA256" and "SHA2-256" are synonymous:
191
192 EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", NULL);
193 ...
194 EVP_MD_free(md);
195
196 Fetch any available implementation of AES-128-CBC in the default context:
197
198 EVP_CIPHER *cipher = EVP_CIPHER_fetch(NULL, "AES-128-CBC", NULL);
199 ...
200 EVP_CIPHER_free(cipher);
201
202 Fetch an implementation of SHA2-256 from the default provider in the default
203 context:
204
205 EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", "provider=default");
206 ...
207 EVP_MD_free(md);
208
209 Fetch an implementation of SHA2-256 that is not from the default provider in the
210 default context:
211
212 EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", "provider!=default");
213 ...
214 EVP_MD_free(md);
215
216 Fetch an implementation of SHA2-256 from the default provider in the specified
217 context:
218
219 EVP_MD *md = EVP_MD_fetch(ctx, "SHA2-256", "provider=default");
220 ...
221 EVP_MD_free(md);
222
223 Load the legacy provider into the default context and then fetch an
224 implementation of WHIRLPOOL from it:
225
226 /* This only needs to be done once - usually at application start up */
227 OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy");
228
229 EVP_MD *md = EVP_MD_fetch(NULL, "WHIRLPOOL", "provider=legacy");
230 ...
231 EVP_MD_free(md);
232
233 Note that in the above example the property string "provider=legacy" is optional
234 since, assuming no other providers have been loaded, the only implementation of
235 the "whirlpool" algorithm is in the "legacy" provider. Also note that the
236 default provider should be explicitly loaded if it is required in addition to
237 other providers:
238
239 /* This only needs to be done once - usually at application start up */
240 OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy");
241 OSSL_PROVIDER *default = OSSL_PROVIDER_load(NULL, "default");
242
243 EVP_MD *md_whirlpool = EVP_MD_fetch(NULL, "whirlpool", NULL);
244 EVP_MD *md_sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL);
245 ...
246 EVP_MD_free(md_whirlpool);
247 EVP_MD_free(md_sha256);
248
249 =head1 OPENSSL PROVIDERS
250
251 OpenSSL comes with a set of providers.
252
253 The algorithms available in each of these providers may vary due to build time
254 configuration options. The L<openssl-list(1)> command can be used to list the
255 currently available algorithms.
256
257 The names of the algorithms shown from L<openssl-list(1)> can be used as an
258 algorithm identifier to the appropriate fetching function. Also see the provider
259 specific manual pages linked below for further details about using the
260 algorithms available in each of the providers.
261
262 As well as the OpenSSL providers third parties can also implemment providers.
263 For information on writing a provider see L<provider(7)>.
264
265 =head2 Default provider
266
267 The default provider is built in as part of the F<libcrypto> library and
268 contains all of the most commonly used algorithm implementations. Should it be
269 needed (if other providers are loaded and offer implementations of the same
270 algorithms), the property query string "provider=default" can be used as a
271 search criterion for these implementations. The default provider includes all
272 of the functionality in the base provider below.
273
274 If you don't load any providers at all then the "default" provider will be
275 automatically loaded. If you explicitly load any provider then the "default"
276 provider would also need to be explicitly loaded if it is required.
277
278 See L<OSSL_PROVIDER-default(7)>.
279
280 =head2 Base provider
281
282 The base provider is built in as part of the F<libcrypto> library and contains
283 algorithm implementations for encoding and decoding for OpenSSL keys.
284 Should it be needed (if other providers are loaded and offer
285 implementations of the same algorithms), the property query string
286 "provider=base" can be used as a search criterion for these implementations.
287 Some encoding and decoding algorithm implementations are not FIPS algorithm
288 implementations in themselves but support algorithms from the FIPS provider and
289 are allowed for use in "FIPS mode". The property query string "fips=yes" can be
290 used to select such algorithms.
291
292 See L<OSSL_PROVIDER-base(7)>.
293
294 =head2 FIPS provider
295
296 The FIPS provider is a dynamically loadable module, and must therefore
297 be loaded explicitly, either in code or through OpenSSL configuration
298 (see L<config(5)>). It contains algorithm implementations that have been
299 validated according to the FIPS 140-2 standard. Should it be needed (if other
300 providers are loaded and offer implementations of the same algorithms), the
301 property query string "provider=fips" can be used as a search criterion for
302 these implementations. All approved algorithm implementations in the FIPS
303 provider can also be selected with the property "fips=yes". The FIPS provider
304 may also contain non-approved algorithm implementations and these can be
305 selected with the property "fips=no".
306
307 See L<OSSL_PROVIDER-FIPS(7)> and L<fips_module(7)>.
308
309 =head2 Legacy provider
310
311 The legacy provider is a dynamically loadable module, and must therefore
312 be loaded explicitly, either in code or through OpenSSL configuration
313 (see L<config(5)>). It contains algorithm implementations that are considered
314 insecure, or are no longer in common use such as MD2 or RC4. Should it be needed
315 (if other providers are loaded and offer implementations of the same algorithms),
316 the property "provider=legacy" can be used as a search criterion for these
317 implementations.
318
319 See L<OSSL_PROVIDER-legacy(7)>.
320
321 =head2 Null provider
322
323 The null provider is built in as part of the F<libcrypto> library. It contains
324 no algorithms in it at all. When fetching algorithms the default provider will
325 be automatically loaded if no other provider has been explicitly loaded. To
326 prevent that from happening you can explicitly load the null provider.
327
328 See L<OSSL_PROVIDER-null(7)>.
329
330 =head1 USING ALGORITHMS IN APPLICATIONS
331
332 Cryptographic algorithms are made available to applications through use of the
333 "EVP" APIs. Each of the various operations such as encryption, digesting,
334 message authentication codes, etc., have a set of EVP function calls that can
335 be invoked to use them. See the L<evp(7)> page for further details.
336
337 Most of these follow a common pattern. A "context" object is first created. For
338 example for a digest operation you would use an B<EVP_MD_CTX>, and for an
339 encryption/decryption operation you would use an B<EVP_CIPHER_CTX>. The
340 operation is then initialised ready for use via an "init" function - optionally
341 passing in a set of parameters (using the B<OSSL_PARAM> type) to configure how
342 the operation should behave. Next data is fed into the operation in a series of
343 "update" calls. The operation is finalised using a "final" call which will
344 typically provide some kind of output. Finally the context is cleaned up and
345 freed.
346
347 The following shows a complete example for doing this process for digesting
348 data using SHA256. The process is similar for other operations such as
349 encryption/decryption, signatures, message authentication codes, etc.
350
351 #include <stdio.h>
352 #include <openssl/evp.h>
353 #include <openssl/bio.h>
354
355 int main(void)
356 {
357 EVP_MD_CTX *ctx = NULL;
358 EVP_MD *sha256 = NULL;
359 const unsigned char msg[] = {
360 0x00, 0x01, 0x02, 0x03
361 };
362 unsigned int len = 0;
363 unsigned char *outdigest = NULL;
364
365 /* Create a context for the digest operation */
366 ctx = EVP_MD_CTX_new();
367 if (ctx == NULL)
368 goto err;
369
370 /*
371 * Fetch the SHA256 algorithm implementation for doing the digest. We're
372 * using the "default" library context here (first NULL parameter), and
373 * we're not supplying any particular search criteria for our SHA256
374 * implementation (second NULL parameter). Any SHA256 implementation will
375 * do.
376 */
377 sha256 = EVP_MD_fetch(NULL, "SHA256", NULL);
378 if (sha256 == NULL)
379 goto err;
380
381 /* Initialise the digest operation */
382 if (!EVP_DigestInit_ex(ctx, sha256, NULL))
383 goto err;
384
385 /*
386 * Pass the message to be digested. This can be passed in over multiple
387 * EVP_DigestUpdate calls if necessary
388 */
389 if (!EVP_DigestUpdate(ctx, msg, sizeof(msg)))
390 goto err;
391
392 /* Allocate the output buffer */
393 outdigest = OPENSSL_malloc(EVP_MD_size(sha256));
394 if (outdigest == NULL)
395 goto err;
396
397 /* Now calculate the digest itself */
398 if (!EVP_DigestFinal_ex(ctx, outdigest, &len))
399 goto err;
400
401 /* Print out the digest result */
402 BIO_dump_fp(stdout, outdigest, len);
403
404 err:
405 /* Clean up all the resources we allocated */
406 OPENSSL_free(outdigest);
407 EVP_MD_free(sha256);
408 EVP_MD_CTX_free(ctx);
409 }
410
411 =head1 CONFIGURATION
412
413 By default OpenSSL will load a configuration file when it is first used. This
414 will set up various configuration settings within the default library context.
415 Applications that create their own library contexts may optionally configure
416 them with a config file using the L<OSSL_LIB_CTX_load_config(3)> function.
417
418 The configuration file can be used to automatically load providers and set up
419 default property query strings.
420
421 For information on the OpenSSL configuration file format see L<config(5)>.
422
423 =head1 ENCODING AND DECODING KEYS
424
425 Many algorithms require the use of a key. Keys can be generated dynamically
426 using the EVP APIs (for example see L<EVP_PKEY_Q_keygen(3)>). However it is often
427 necessary to save or load keys (or their associated parameters) to or from some
428 external format such as PEM or DER (see L<openssl-glossary(7)>). OpenSSL uses
429 encoders and decoders to perform this task.
430
431 Encoders and decoders are just algorithm implementations in the same way as
432 any other algorithm implementation in OpenSSL. They are implemented by
433 providers. The OpenSSL encoders and decoders are available in the default
434 provider. They are also duplicated in the base provider.
435
436 For information about encoders see L<OSSL_ENCODER_CTX_new_for_pkey(3)>. For
437 information about decoders see L<OSSL_DECODER_CTX_new_for_pkey(3)>.
438
439 =head1 LIBRARY CONVENTIONS
440
441 Many OpenSSL functions that "get" or "set" a value follow a naming convention
442 using the numbers B<0> and B<1>, i.e. "get0", "get1", "set0" and "set1". This
443 can also apply to some functions that "add" a value to an existing set, i.e.
444 "add0" and "add1".
445
446 For example the functions:
447
448 int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev);
449 int X509_add1_trust_object(X509 *x, const ASN1_OBJECT *obj);
450
451 In the B<0> version the ownership of the object is passed to (for an add or set)
452 or retained by (for a get) the parent object. For example after calling the
453 X509_CRL_add0_revoked() function above, ownership of the I<rev> object is passed
454 to the I<crl> object. Therefore, after calling this function I<rev> should not
455 be freed directly. It will be freed implicitly when I<crl> is freed.
456
457 In the B<1> version the ownership of the object is not passed to or retained by
458 the parent object. Instead a copy or "up ref" of the object is performed. So
459 after calling the X509_add1_trust_object() function above the application will
460 still be responsible for freeing the I<obj> value where appropriate.
461
462 =head1 SEE ALSO
463
464 L<openssl(1)>, L<ssl(7)>, L<evp(7)>, L<OSSL_LIB_CTX(3)>, L<openssl-threads(7)>,
465 L<property(7)>, L<OSSL_PROVIDER-default(7)>, L<OSSL_PROVIDER-base(7)>,
466 L<OSSL_PROVIDER-FIPS(7)>, L<OSSL_PROVIDER-legacy(7)>, L<OSSL_PROVIDER-null(7)>,
467 L<openssl-glossary(7)>, L<provider(7)>
468
469 =head1 COPYRIGHT
470
471 Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
472
473 Licensed under the Apache License 2.0 (the "License"). You may not use
474 this file except in compliance with the License. You can obtain a copy
475 in the file LICENSE in the source distribution or at
476 L<https://www.openssl.org/source/license.html>.
477
478 =cut