113d40330Schristos=pod 213d40330Schristos 313d40330Schristos=head1 NAME 413d40330Schristos 513d40330Schristoscrypto - OpenSSL cryptographic library 613d40330Schristos 713d40330Schristos=head1 SYNOPSIS 813d40330Schristos 913d40330SchristosSee the individual manual pages for details. 1013d40330Schristos 1113d40330Schristos=head1 DESCRIPTION 1213d40330Schristos 13b0d17251SchristosThe OpenSSL crypto library (C<libcrypto>) implements a wide range of 14b0d17251Schristoscryptographic algorithms used in various Internet standards. The services 15b0d17251Schristosprovided by this library are used by the OpenSSL implementations of TLS and 16b0d17251SchristosCMS, and they have also been used to implement many other third party products 17b0d17251Schristosand protocols. 1813d40330Schristos 19b0d17251SchristosThe functionality includes symmetric encryption, public key cryptography, key 20b0d17251Schristosagreement, certificate handling, cryptographic hash functions, cryptographic 21b0d17251Schristospseudo-random number generators, message authentication codes (MACs), key 22b0d17251Schristosderivation functions (KDFs), and various utilities. 2313d40330Schristos 24b0d17251Schristos=head2 Algorithms 2513d40330Schristos 26b0d17251SchristosCryptographic primitives such as the SHA256 digest, or AES encryption are 27b0d17251Schristosreferred to in OpenSSL as "algorithms". Each algorithm may have multiple 28b0d17251Schristosimplementations available for use. For example the RSA algorithm is available as 29b0d17251Schristosa "default" implementation suitable for general use, and a "fips" implementation 30b0d17251Schristoswhich has been validated to FIPS standards for situations where that is 31b0d17251Schristosimportant. It is also possible that a third party could add additional 32b0d17251Schristosimplementations such as in a hardware security module (HSM). 3313d40330Schristos 34b0d17251Schristos=head2 Operations 35b0d17251Schristos 36b0d17251SchristosDifferent algorithms can be grouped together by their purpose. For example there 37b0d17251Schristosare algorithms for encryption, and different algorithms for digesting data. 38b0d17251SchristosThese different groups are known as "operations" in OpenSSL. Each operation 39b0d17251Schristoshas a different set of functions associated with it. For example to perform an 40b0d17251Schristosencryption operation using AES (or any other encryption algorithm) you would use 41b0d17251Schristosthe encryption functions detailed on the L<EVP_EncryptInit(3)> page. Or to 42b0d17251Schristosperform a digest operation using SHA256 then you would use the digesting 43b0d17251Schristosfunctions on the L<EVP_DigestInit(3)> page. 44b0d17251Schristos 45b0d17251Schristos=head2 Providers 46b0d17251Schristos 47b0d17251SchristosA provider in OpenSSL is a component that collects together algorithm 48b0d17251Schristosimplementations. In order to use an algorithm you must have at least one 49b0d17251Schristosprovider loaded that contains an implementation of it. OpenSSL comes with a 50b0d17251Schristosnumber of providers and they may also be obtained from third parties. If you 51b0d17251Schristosdon't load a provider explicitly (either in program code or via config) then the 52b0d17251SchristosOpenSSL built-in "default" provider will be automatically loaded. 53b0d17251Schristos 54b0d17251Schristos=head2 Library contexts 55b0d17251Schristos 56b0d17251SchristosA library context can be thought of as a "scope" within which configuration 57b0d17251Schristosoptions take effect. When a provider is loaded, it is only loaded within the 58b0d17251Schristosscope of a given library context. In this way it is possible for different 59b0d17251Schristoscomponents of a complex application to each use a different library context and 60b0d17251Schristoshave different providers loaded with different configuration settings. 61b0d17251Schristos 62b0d17251SchristosIf an application does not explicitly create a library context then the 63b0d17251Schristos"default" library context will be used. 64b0d17251Schristos 65b0d17251SchristosLibrary contexts are represented by the B<OSSL_LIB_CTX> type. Many OpenSSL API 66b0d17251Schristosfunctions take a library context as a parameter. Applications can always pass 67b0d17251SchristosB<NULL> for this parameter to just use the default library context. 68b0d17251Schristos 69b0d17251SchristosThe default library context is automatically created the first time it is 70b0d17251Schristosneeded. This will automatically load any available configuration file and will 71b0d17251Schristosinitialise OpenSSL for use. Unlike in earlier versions of OpenSSL (prior to 72b0d17251Schristos1.1.0) no explicit initialisation steps need to be taken. 73b0d17251Schristos 74b0d17251SchristosSimilarly when the application exits the default library context is 75b0d17251Schristosautomatically destroyed. No explicit de-initialisation steps need to be taken. 76b0d17251Schristos 77b0d17251SchristosSee L<OSSL_LIB_CTX(3)> for more information about library contexts. 78b0d17251SchristosSee also L</ALGORITHM FETCHING>. 79b0d17251Schristos 80b0d17251Schristos=head2 Multi-threaded applications 81b0d17251Schristos 82b0d17251SchristosAs long as OpenSSL has been built with support for threads (the default case 83b0d17251Schristoson most platforms) then most OpenSSL I<functions> are thread-safe in the sense 84b0d17251Schristosthat it is safe to call the same function from multiple threads at the same 85b0d17251Schristostime. However most OpenSSL I<data structures> are not thread-safe. For example 86b0d17251Schristosthe L<BIO_write(3)> and L<BIO_read(3)> functions are thread safe. However it 87b0d17251Schristoswould not be thread safe to call BIO_write() from one thread while calling 88b0d17251SchristosBIO_read() in another where both functions are passed the same B<BIO> object 89b0d17251Schristossince both of them may attempt to make changes to the same B<BIO> object. 90b0d17251Schristos 91b0d17251SchristosThere are exceptions to these rules. A small number of functions are not thread 92b0d17251Schristossafe at all. Where this is the case this restriction should be noted in the 93b0d17251Schristosdocumentation for the function. Similarly some data structures may be partially 94b0d17251Schristosor fully thread safe. For example it is safe to use an B<OSSL_LIB_CTX> in 95b0d17251Schristosmultiple threads. 96b0d17251Schristos 97b0d17251SchristosSee L<openssl-threads(7)> for a more detailed discussion on OpenSSL threading 98b0d17251Schristossupport. 99b0d17251Schristos 100b0d17251Schristos=head1 ALGORITHM FETCHING 101b0d17251Schristos 102b0d17251SchristosIn order to use an algorithm an implementation for it must first be "fetched". 103b0d17251SchristosFetching is the process of looking through the available implementations, 104b0d17251Schristosapplying selection criteria (via a property query string), and finally choosing 105b0d17251Schristosthe implementation that will be used. 106b0d17251Schristos 107b0d17251SchristosTwo types of fetching are supported by OpenSSL - explicit fetching and implicit 108b0d17251Schristosfetching. 109b0d17251Schristos 110b0d17251Schristos=head2 Property query strings 111b0d17251Schristos 112b0d17251SchristosWhen fetching an algorithm it is possible to specify a property query string to 113b0d17251Schristosguide the selection process. For example a property query string of 114b0d17251Schristos"provider=default" could be used to force the selection to only consider 115b0d17251Schristosalgorithm implementations in the default provider. 116b0d17251Schristos 117b0d17251SchristosProperty query strings can be specified explicitly as an argument to a function. 118b0d17251SchristosIt is also possible to specify a default property query string for the whole 1194170684fSchristoslibrary context using the L<EVP_set_default_properties(3)> or 1204170684fSchristosL<EVP_default_properties_enable_fips(3)> functions. Where both 121b0d17251Schristosdefault properties and function specific properties are specified then they are 122b0d17251Schristoscombined. Function specific properties will override default properties where 123b0d17251Schristosthere is a conflict. 124b0d17251Schristos 125b0d17251SchristosSee L<property(7)> for more information about properties. 126b0d17251Schristos 127b0d17251Schristos=head2 Explicit fetching 128b0d17251Schristos 129b0d17251SchristosUsers of the OpenSSL libraries never query a provider directly for an algorithm 130b0d17251Schristosimplementation. Instead, the diverse OpenSSL APIs often have explicit fetching 131b0d17251Schristosfunctions that do the work, and they return an appropriate algorithm object back 132b0d17251Schristosto the user. These functions usually have the name C<APINAME_fetch>, where 133b0d17251SchristosC<APINAME> is the name of the operation. For example L<EVP_MD_fetch(3)> can 134b0d17251Schristosbe used to explicitly fetch a digest algorithm implementation. The user is 135b0d17251Schristosresponsible for freeing the object returned from the C<APINAME_fetch> function 136b0d17251Schristosusing C<APINAME_free> when it is no longer needed. 137b0d17251Schristos 138b0d17251SchristosThese fetching functions follow a fairly common pattern, where three 139b0d17251Schristosarguments are passed: 140b0d17251Schristos 141b0d17251Schristos=over 4 142b0d17251Schristos 143b0d17251Schristos=item The library context 144b0d17251Schristos 145b0d17251SchristosSee L<OSSL_LIB_CTX(3)> for a more detailed description. 146b0d17251SchristosThis may be NULL to signify the default (global) library context, or a 147b0d17251Schristoscontext created by the user. Only providers loaded in this library context (see 148b0d17251SchristosL<OSSL_PROVIDER_load(3)>) will be considered by the fetching function. In case 149b0d17251Schristosno provider has been loaded in this library context then the default provider 150b0d17251Schristoswill be loaded as a fallback (see L<OSSL_PROVIDER-default(7)>). 151b0d17251Schristos 152b0d17251Schristos=item An identifier 153b0d17251Schristos 154b0d17251SchristosFor all currently implemented fetching functions this is the algorithm name. 155b0d17251Schristos 156b0d17251Schristos=item A property query string 157b0d17251Schristos 158b0d17251SchristosThe property query string used to guide selection of the algorithm 159b0d17251Schristosimplementation. 160b0d17251Schristos 161b0d17251Schristos=back 162b0d17251Schristos 163b0d17251SchristosThe algorithm implementation that is fetched can then be used with other diverse 164b0d17251Schristosfunctions that use them. For example the L<EVP_DigestInit_ex(3)> function takes 165b0d17251Schristosas a parameter an B<EVP_MD> object which may have been returned from an earlier 166b0d17251Schristoscall to L<EVP_MD_fetch(3)>. 167b0d17251Schristos 1684170684fSchristos=head2 Implicit fetching 169b0d17251Schristos 170b0d17251SchristosOpenSSL has a number of functions that return an algorithm object with no 171b0d17251Schristosassociated implementation, such as L<EVP_sha256(3)>, L<EVP_aes_128_cbc(3)>, 172b0d17251SchristosL<EVP_get_cipherbyname(3)> or L<EVP_get_digestbyname(3)>. These are present for 173b0d17251Schristoscompatibility with OpenSSL before version 3.0 where explicit fetching was not 174b0d17251Schristosavailable. 175b0d17251Schristos 176b0d17251SchristosWhen they are used with functions like L<EVP_DigestInit_ex(3)> or 177b0d17251SchristosL<EVP_CipherInit_ex(3)>, the actual implementation to be used is 178b0d17251Schristosfetched implicitly using default search criteria. 179b0d17251Schristos 180b0d17251SchristosIn some cases implicit fetching can also occur when a NULL algorithm parameter 181b0d17251Schristosis supplied. In this case an algorithm implementation is implicitly fetched 182b0d17251Schristosusing default search criteria and an algorithm name that is consistent with 183b0d17251Schristosthe context in which it is being used. 184b0d17251Schristos 185b0d17251SchristosFunctions that revolve around B<EVP_PKEY_CTX> and L<EVP_PKEY(3)>, such as 186b0d17251SchristosL<EVP_DigestSignInit(3)> and friends, all fetch the implementations 187b0d17251Schristosimplicitly. Because these functions involve both an operation type (such as 188b0d17251SchristosL<EVP_SIGNATURE(3)>) and an L<EVP_KEYMGMT(3)> for the L<EVP_PKEY(3)>, they try 189b0d17251Schristosthe following: 190b0d17251Schristos 191b0d17251Schristos=over 4 192b0d17251Schristos 193b0d17251Schristos=item 1. 194b0d17251Schristos 195b0d17251SchristosFetch the operation type implementation from any provider given a library 196b0d17251Schristoscontext and property string stored in the B<EVP_PKEY_CTX>. 197b0d17251Schristos 198b0d17251SchristosIf the provider of the operation type implementation is different from the 199b0d17251Schristosprovider of the L<EVP_PKEY(3)>'s L<EVP_KEYMGMT(3)> implementation, try to 200b0d17251Schristosfetch a L<EVP_KEYMGMT(3)> implementation in the same provider as the operation 201b0d17251Schristostype implementation and export the L<EVP_PKEY(3)> to it (effectively making a 202b0d17251Schristostemporary copy of the original key). 203b0d17251Schristos 204b0d17251SchristosIf anything in this step fails, the next step is used as a fallback. 205b0d17251Schristos 206b0d17251Schristos=item 2. 207b0d17251Schristos 208b0d17251SchristosAs a fallback, try to fetch the operation type implementation from the same 209b0d17251Schristosprovider as the original L<EVP_PKEY(3)>'s L<EVP_KEYMGMT(3)>, still using the 210*4778aedeSchristosproperty string from the B<EVP_PKEY_CTX>. 211b0d17251Schristos 212b0d17251Schristos=back 213b0d17251Schristos 2144170684fSchristos=head2 Performance 2154170684fSchristos 2164170684fSchristosIf you perform the same operation many times then it is recommended to use 2174170684fSchristosL</Explicit fetching> to prefetch an algorithm once initially, 2184170684fSchristosand then pass this created object to any operations that are currently 2194170684fSchristosusing L</Implicit fetching>. 2204170684fSchristosSee an example of Explicit fetching in L</USING ALGORITHMS IN APPLICATIONS>. 2214170684fSchristos 2224170684fSchristosPrior to OpenSSL 3.0, constant method tables (such as EVP_sha256()) were used 2234170684fSchristosdirectly to access methods. If you pass one of these convenience functions 2244170684fSchristosto an operation the fixed methods are ignored, and only the name is used to 2254170684fSchristosinternally fetch methods from a provider. 2264170684fSchristos 2274170684fSchristosIf the prefetched object is not passed to operations, then any implicit 2284170684fSchristosfetch will use the internally cached prefetched object, but it will 2294170684fSchristosstill be slower than passing the prefetched object directly. 2304170684fSchristos 2314170684fSchristosFetching via a provider offers more flexibility, but it is slower than the 2324170684fSchristosold method, since it must search for the algorithm in all loaded providers, 2334170684fSchristosand then populate the method table using provider supplied methods. 2344170684fSchristosInternally OpenSSL caches similar algorithms on the first fetch 2354170684fSchristos(so loading a digest caches all digests). 2364170684fSchristos 2374170684fSchristosThe following methods can be used for prefetching: 2384170684fSchristos 2394170684fSchristos=over 4 2404170684fSchristos 2414170684fSchristos=item L<EVP_MD_fetch(3)> 2424170684fSchristos 2434170684fSchristos=item L<EVP_CIPHER_fetch(3)> 2444170684fSchristos 2454170684fSchristos=item L<EVP_KDF_fetch(3)> 2464170684fSchristos 2474170684fSchristos=item L<EVP_MAC_fetch(3)> 2484170684fSchristos 2494170684fSchristos=item L<EVP_KEM_fetch(3)> 2504170684fSchristos 2514170684fSchristos=item L<OSSL_ENCODER_fetch(3)> 2524170684fSchristos 2534170684fSchristos=item L<OSSL_DECODER_fetch(3)> 2544170684fSchristos 2554170684fSchristos=item L<EVP_RAND_fetch(3)> 2564170684fSchristos 2574170684fSchristos=back 2584170684fSchristos 2594170684fSchristosThe following methods are used internally when performing operations: 2604170684fSchristos 2614170684fSchristos=over 4 2624170684fSchristos 2634170684fSchristos=item L<EVP_KEYMGMT_fetch(3)> 2644170684fSchristos 2654170684fSchristos=item L<EVP_KEYEXCH_fetch(3)> 2664170684fSchristos 2674170684fSchristos=item L<EVP_SIGNATURE_fetch(3)> 2684170684fSchristos 2694170684fSchristos=item L<OSSL_STORE_LOADER_fetch(3)> 2704170684fSchristos 2714170684fSchristos=back 2724170684fSchristos 2734170684fSchristosSee L<OSSL_PROVIDER-default(7)>, <OSSL_PROVIDER-fips(7)> and 2744170684fSchristos<OSSL_PROVIDER-legacy(7)>for a list of algorithm names that 2754170684fSchristoscan be fetched. 2764170684fSchristos 277b0d17251Schristos=head1 FETCHING EXAMPLES 278b0d17251Schristos 279b0d17251SchristosThe following section provides a series of examples of fetching algorithm 280b0d17251Schristosimplementations. 281b0d17251Schristos 282b0d17251SchristosFetch any available implementation of SHA2-256 in the default context. Note 283b0d17251Schristosthat some algorithms have aliases. So "SHA256" and "SHA2-256" are synonymous: 284b0d17251Schristos 285b0d17251Schristos EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", NULL); 286b0d17251Schristos ... 287b0d17251Schristos EVP_MD_free(md); 288b0d17251Schristos 289b0d17251SchristosFetch any available implementation of AES-128-CBC in the default context: 290b0d17251Schristos 291b0d17251Schristos EVP_CIPHER *cipher = EVP_CIPHER_fetch(NULL, "AES-128-CBC", NULL); 292b0d17251Schristos ... 293b0d17251Schristos EVP_CIPHER_free(cipher); 294b0d17251Schristos 295b0d17251SchristosFetch an implementation of SHA2-256 from the default provider in the default 296b0d17251Schristoscontext: 297b0d17251Schristos 298b0d17251Schristos EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", "provider=default"); 299b0d17251Schristos ... 300b0d17251Schristos EVP_MD_free(md); 301b0d17251Schristos 302b0d17251SchristosFetch an implementation of SHA2-256 that is not from the default provider in the 303b0d17251Schristosdefault context: 304b0d17251Schristos 305b0d17251Schristos EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", "provider!=default"); 306b0d17251Schristos ... 307b0d17251Schristos EVP_MD_free(md); 308b0d17251Schristos 309b0d17251SchristosFetch an implementation of SHA2-256 from the default provider in the specified 310b0d17251Schristoscontext: 311b0d17251Schristos 312b0d17251Schristos EVP_MD *md = EVP_MD_fetch(ctx, "SHA2-256", "provider=default"); 313b0d17251Schristos ... 314b0d17251Schristos EVP_MD_free(md); 315b0d17251Schristos 316b0d17251SchristosLoad the legacy provider into the default context and then fetch an 317b0d17251Schristosimplementation of WHIRLPOOL from it: 318b0d17251Schristos 319b0d17251Schristos /* This only needs to be done once - usually at application start up */ 320b0d17251Schristos OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy"); 321b0d17251Schristos 322b0d17251Schristos EVP_MD *md = EVP_MD_fetch(NULL, "WHIRLPOOL", "provider=legacy"); 323b0d17251Schristos ... 324b0d17251Schristos EVP_MD_free(md); 325b0d17251Schristos 326b0d17251SchristosNote that in the above example the property string "provider=legacy" is optional 327b0d17251Schristossince, assuming no other providers have been loaded, the only implementation of 328b0d17251Schristosthe "whirlpool" algorithm is in the "legacy" provider. Also note that the 329b0d17251Schristosdefault provider should be explicitly loaded if it is required in addition to 330b0d17251Schristosother providers: 331b0d17251Schristos 332b0d17251Schristos /* This only needs to be done once - usually at application start up */ 333b0d17251Schristos OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy"); 334b0d17251Schristos OSSL_PROVIDER *default = OSSL_PROVIDER_load(NULL, "default"); 335b0d17251Schristos 336b0d17251Schristos EVP_MD *md_whirlpool = EVP_MD_fetch(NULL, "whirlpool", NULL); 337b0d17251Schristos EVP_MD *md_sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL); 338b0d17251Schristos ... 339b0d17251Schristos EVP_MD_free(md_whirlpool); 340b0d17251Schristos EVP_MD_free(md_sha256); 341b0d17251Schristos 342b0d17251Schristos=head1 OPENSSL PROVIDERS 343b0d17251Schristos 344b0d17251SchristosOpenSSL comes with a set of providers. 345b0d17251Schristos 346b0d17251SchristosThe algorithms available in each of these providers may vary due to build time 347b0d17251Schristosconfiguration options. The L<openssl-list(1)> command can be used to list the 348b0d17251Schristoscurrently available algorithms. 349b0d17251Schristos 350b0d17251SchristosThe names of the algorithms shown from L<openssl-list(1)> can be used as an 351b0d17251Schristosalgorithm identifier to the appropriate fetching function. Also see the provider 352b0d17251Schristosspecific manual pages linked below for further details about using the 353b0d17251Schristosalgorithms available in each of the providers. 354b0d17251Schristos 355b0d17251SchristosAs well as the OpenSSL providers third parties can also implement providers. 356b0d17251SchristosFor information on writing a provider see L<provider(7)>. 357b0d17251Schristos 358b0d17251Schristos=head2 Default provider 359b0d17251Schristos 360b0d17251SchristosThe default provider is built in as part of the F<libcrypto> library and 361b0d17251Schristoscontains all of the most commonly used algorithm implementations. Should it be 362b0d17251Schristosneeded (if other providers are loaded and offer implementations of the same 363b0d17251Schristosalgorithms), the property query string "provider=default" can be used as a 364b0d17251Schristossearch criterion for these implementations. The default provider includes all 365b0d17251Schristosof the functionality in the base provider below. 366b0d17251Schristos 367b0d17251SchristosIf you don't load any providers at all then the "default" provider will be 368b0d17251Schristosautomatically loaded. If you explicitly load any provider then the "default" 369b0d17251Schristosprovider would also need to be explicitly loaded if it is required. 370b0d17251Schristos 371b0d17251SchristosSee L<OSSL_PROVIDER-default(7)>. 372b0d17251Schristos 373b0d17251Schristos=head2 Base provider 374b0d17251Schristos 375b0d17251SchristosThe base provider is built in as part of the F<libcrypto> library and contains 376b0d17251Schristosalgorithm implementations for encoding and decoding for OpenSSL keys. 377b0d17251SchristosShould it be needed (if other providers are loaded and offer 378b0d17251Schristosimplementations of the same algorithms), the property query string 379b0d17251Schristos"provider=base" can be used as a search criterion for these implementations. 380b0d17251SchristosSome encoding and decoding algorithm implementations are not FIPS algorithm 381b0d17251Schristosimplementations in themselves but support algorithms from the FIPS provider and 382b0d17251Schristosare allowed for use in "FIPS mode". The property query string "fips=yes" can be 383b0d17251Schristosused to select such algorithms. 384b0d17251Schristos 385b0d17251SchristosSee L<OSSL_PROVIDER-base(7)>. 386b0d17251Schristos 387b0d17251Schristos=head2 FIPS provider 388b0d17251Schristos 389b0d17251SchristosThe FIPS provider is a dynamically loadable module, and must therefore 390b0d17251Schristosbe loaded explicitly, either in code or through OpenSSL configuration 391b0d17251Schristos(see L<config(5)>). It contains algorithm implementations that have been 392b0d17251Schristosvalidated according to the FIPS 140-2 standard. Should it be needed (if other 393b0d17251Schristosproviders are loaded and offer implementations of the same algorithms), the 394b0d17251Schristosproperty query string "provider=fips" can be used as a search criterion for 395b0d17251Schristosthese implementations. All approved algorithm implementations in the FIPS 396b0d17251Schristosprovider can also be selected with the property "fips=yes". The FIPS provider 397b0d17251Schristosmay also contain non-approved algorithm implementations and these can be 398b0d17251Schristosselected with the property "fips=no". 399b0d17251Schristos 400b0d17251SchristosSee L<OSSL_PROVIDER-FIPS(7)> and L<fips_module(7)>. 401b0d17251Schristos 402b0d17251Schristos=head2 Legacy provider 403b0d17251Schristos 404b0d17251SchristosThe legacy provider is a dynamically loadable module, and must therefore 405b0d17251Schristosbe loaded explicitly, either in code or through OpenSSL configuration 406b0d17251Schristos(see L<config(5)>). It contains algorithm implementations that are considered 407b0d17251Schristosinsecure, or are no longer in common use such as MD2 or RC4. Should it be needed 408b0d17251Schristos(if other providers are loaded and offer implementations of the same algorithms), 409b0d17251Schristosthe property "provider=legacy" can be used as a search criterion for these 410b0d17251Schristosimplementations. 411b0d17251Schristos 412b0d17251SchristosSee L<OSSL_PROVIDER-legacy(7)>. 413b0d17251Schristos 414b0d17251Schristos=head2 Null provider 415b0d17251Schristos 416b0d17251SchristosThe null provider is built in as part of the F<libcrypto> library. It contains 417b0d17251Schristosno algorithms in it at all. When fetching algorithms the default provider will 418b0d17251Schristosbe automatically loaded if no other provider has been explicitly loaded. To 419b0d17251Schristosprevent that from happening you can explicitly load the null provider. 420b0d17251Schristos 421b0d17251SchristosSee L<OSSL_PROVIDER-null(7)>. 422b0d17251Schristos 423b0d17251Schristos=head1 USING ALGORITHMS IN APPLICATIONS 424b0d17251Schristos 425b0d17251SchristosCryptographic algorithms are made available to applications through use of the 426b0d17251Schristos"EVP" APIs. Each of the various operations such as encryption, digesting, 427b0d17251Schristosmessage authentication codes, etc., have a set of EVP function calls that can 428b0d17251Schristosbe invoked to use them. See the L<evp(7)> page for further details. 429b0d17251Schristos 430b0d17251SchristosMost of these follow a common pattern. A "context" object is first created. For 431b0d17251Schristosexample for a digest operation you would use an B<EVP_MD_CTX>, and for an 432b0d17251Schristosencryption/decryption operation you would use an B<EVP_CIPHER_CTX>. The 433b0d17251Schristosoperation is then initialised ready for use via an "init" function - optionally 434b0d17251Schristospassing in a set of parameters (using the L<OSSL_PARAM(3)> type) to configure how 435b0d17251Schristosthe operation should behave. Next data is fed into the operation in a series of 436b0d17251Schristos"update" calls. The operation is finalised using a "final" call which will 437b0d17251Schristostypically provide some kind of output. Finally the context is cleaned up and 438b0d17251Schristosfreed. 439b0d17251Schristos 440b0d17251SchristosThe following shows a complete example for doing this process for digesting 441b0d17251Schristosdata using SHA256. The process is similar for other operations such as 442b0d17251Schristosencryption/decryption, signatures, message authentication codes, etc. 443b0d17251Schristos 444b0d17251Schristos #include <stdio.h> 445b0d17251Schristos #include <openssl/evp.h> 446b0d17251Schristos #include <openssl/bio.h> 447b0d17251Schristos #include <openssl/err.h> 448b0d17251Schristos 449b0d17251Schristos int main(void) 450b0d17251Schristos { 451b0d17251Schristos EVP_MD_CTX *ctx = NULL; 452b0d17251Schristos EVP_MD *sha256 = NULL; 453b0d17251Schristos const unsigned char msg[] = { 454b0d17251Schristos 0x00, 0x01, 0x02, 0x03 455b0d17251Schristos }; 456b0d17251Schristos unsigned int len = 0; 457b0d17251Schristos unsigned char *outdigest = NULL; 458b0d17251Schristos int ret = 1; 459b0d17251Schristos 460b0d17251Schristos /* Create a context for the digest operation */ 461b0d17251Schristos ctx = EVP_MD_CTX_new(); 462b0d17251Schristos if (ctx == NULL) 463b0d17251Schristos goto err; 464b0d17251Schristos 465b0d17251Schristos /* 466b0d17251Schristos * Fetch the SHA256 algorithm implementation for doing the digest. We're 467b0d17251Schristos * using the "default" library context here (first NULL parameter), and 468b0d17251Schristos * we're not supplying any particular search criteria for our SHA256 469b0d17251Schristos * implementation (second NULL parameter). Any SHA256 implementation will 470b0d17251Schristos * do. 4714170684fSchristos * In a larger application this fetch would just be done once, and could 4724170684fSchristos * be used for multiple calls to other operations such as EVP_DigestInit_ex(). 473b0d17251Schristos */ 474b0d17251Schristos sha256 = EVP_MD_fetch(NULL, "SHA256", NULL); 475b0d17251Schristos if (sha256 == NULL) 476b0d17251Schristos goto err; 477b0d17251Schristos 478b0d17251Schristos /* Initialise the digest operation */ 479b0d17251Schristos if (!EVP_DigestInit_ex(ctx, sha256, NULL)) 480b0d17251Schristos goto err; 481b0d17251Schristos 482b0d17251Schristos /* 483b0d17251Schristos * Pass the message to be digested. This can be passed in over multiple 484b0d17251Schristos * EVP_DigestUpdate calls if necessary 485b0d17251Schristos */ 486b0d17251Schristos if (!EVP_DigestUpdate(ctx, msg, sizeof(msg))) 487b0d17251Schristos goto err; 488b0d17251Schristos 489b0d17251Schristos /* Allocate the output buffer */ 490b0d17251Schristos outdigest = OPENSSL_malloc(EVP_MD_get_size(sha256)); 491b0d17251Schristos if (outdigest == NULL) 492b0d17251Schristos goto err; 493b0d17251Schristos 494b0d17251Schristos /* Now calculate the digest itself */ 495b0d17251Schristos if (!EVP_DigestFinal_ex(ctx, outdigest, &len)) 496b0d17251Schristos goto err; 497b0d17251Schristos 498b0d17251Schristos /* Print out the digest result */ 499b0d17251Schristos BIO_dump_fp(stdout, outdigest, len); 500b0d17251Schristos 501b0d17251Schristos ret = 0; 502b0d17251Schristos 503b0d17251Schristos err: 504b0d17251Schristos /* Clean up all the resources we allocated */ 505b0d17251Schristos OPENSSL_free(outdigest); 506b0d17251Schristos EVP_MD_free(sha256); 507b0d17251Schristos EVP_MD_CTX_free(ctx); 508b0d17251Schristos if (ret != 0) 509b0d17251Schristos ERR_print_errors_fp(stderr); 510b0d17251Schristos return ret; 511b0d17251Schristos } 512b0d17251Schristos 513b0d17251Schristos=head1 CONFIGURATION 514b0d17251Schristos 515b0d17251SchristosBy default OpenSSL will load a configuration file when it is first used. This 516b0d17251Schristoswill set up various configuration settings within the default library context. 517b0d17251SchristosApplications that create their own library contexts may optionally configure 518b0d17251Schristosthem with a config file using the L<OSSL_LIB_CTX_load_config(3)> function. 519b0d17251Schristos 520b0d17251SchristosThe configuration file can be used to automatically load providers and set up 521b0d17251Schristosdefault property query strings. 522b0d17251Schristos 523b0d17251SchristosFor information on the OpenSSL configuration file format see L<config(5)>. 524b0d17251Schristos 525b0d17251Schristos=head1 ENCODING AND DECODING KEYS 526b0d17251Schristos 527b0d17251SchristosMany algorithms require the use of a key. Keys can be generated dynamically 528b0d17251Schristosusing the EVP APIs (for example see L<EVP_PKEY_Q_keygen(3)>). However it is often 529b0d17251Schristosnecessary to save or load keys (or their associated parameters) to or from some 530b0d17251Schristosexternal format such as PEM or DER (see L<openssl-glossary(7)>). OpenSSL uses 531b0d17251Schristosencoders and decoders to perform this task. 532b0d17251Schristos 533b0d17251SchristosEncoders and decoders are just algorithm implementations in the same way as 534b0d17251Schristosany other algorithm implementation in OpenSSL. They are implemented by 535b0d17251Schristosproviders. The OpenSSL encoders and decoders are available in the default 536b0d17251Schristosprovider. They are also duplicated in the base provider. 537b0d17251Schristos 538b0d17251SchristosFor information about encoders see L<OSSL_ENCODER_CTX_new_for_pkey(3)>. For 539b0d17251Schristosinformation about decoders see L<OSSL_DECODER_CTX_new_for_pkey(3)>. 540b0d17251Schristos 541b0d17251Schristos=head1 LIBRARY CONVENTIONS 542b0d17251Schristos 543b0d17251SchristosMany OpenSSL functions that "get" or "set" a value follow a naming convention 544b0d17251Schristosusing the numbers B<0> and B<1>, i.e. "get0", "get1", "set0" and "set1". This 545b0d17251Schristoscan also apply to some functions that "add" a value to an existing set, i.e. 546b0d17251Schristos"add0" and "add1". 547b0d17251Schristos 548b0d17251SchristosFor example the functions: 54913d40330Schristos 55013d40330Schristos int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev); 55113d40330Schristos int X509_add1_trust_object(X509 *x, const ASN1_OBJECT *obj); 55213d40330Schristos 553b0d17251SchristosIn the B<0> version the ownership of the object is passed to (for an add or set) 554b0d17251Schristosor retained by (for a get) the parent object. For example after calling the 555b0d17251SchristosX509_CRL_add0_revoked() function above, ownership of the I<rev> object is passed 556b0d17251Schristosto the I<crl> object. Therefore, after calling this function I<rev> should not 557b0d17251Schristosbe freed directly. It will be freed implicitly when I<crl> is freed. 55813d40330Schristos 559b0d17251SchristosIn the B<1> version the ownership of the object is not passed to or retained by 560b0d17251Schristosthe parent object. Instead a copy or "up ref" of the object is performed. So 561b0d17251Schristosafter calling the X509_add1_trust_object() function above the application will 562b0d17251Schristosstill be responsible for freeing the I<obj> value where appropriate. 56313d40330Schristos 56413d40330Schristos=head1 SEE ALSO 56513d40330Schristos 566b0d17251SchristosL<openssl(1)>, L<ssl(7)>, L<evp(7)>, L<OSSL_LIB_CTX(3)>, L<openssl-threads(7)>, 567b0d17251SchristosL<property(7)>, L<OSSL_PROVIDER-default(7)>, L<OSSL_PROVIDER-base(7)>, 568b0d17251SchristosL<OSSL_PROVIDER-FIPS(7)>, L<OSSL_PROVIDER-legacy(7)>, L<OSSL_PROVIDER-null(7)>, 569b0d17251SchristosL<openssl-glossary(7)>, L<provider(7)> 57013d40330Schristos 57113d40330Schristos=head1 COPYRIGHT 57213d40330Schristos 5734170684fSchristosCopyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved. 57413d40330Schristos 575b0d17251SchristosLicensed under the Apache License 2.0 (the "License"). You may not use 57613d40330Schristosthis file except in compliance with the License. You can obtain a copy 57713d40330Schristosin the file LICENSE in the source distribution or at 57813d40330SchristosL<https://www.openssl.org/source/license.html>. 57913d40330Schristos 58013d40330Schristos=cut 581