ossl-guide-libcrypto-introduction - OpenSSL Documentation
ossl-guide-libcrypto-introduction - OpenSSL Documentation
NAME
ossl-guide-libcrypto-introduction, crypto - OpenSSL Guide: An introduction to libcrypto
INTRODUCTION
The OpenSSL cryptography library ( libcrypto ) enables access to a wide range of cryptographic
algorithms used in various Internet standards. The services provided by this library are used by the
OpenSSL implementations of TLS and CMS, and they have also been used to implement many other
third party products and protocols.
The functionality includes symmetric encryption, public key cryptography, key agreement, certiGcate
handling, cryptographic hash functions, cryptographic pseudo-random number generators, message
authentication codes (MACs), key derivation functions (KDFs), and various utilities.
Algorithms
Cryptographic primitives such as the SHA256 digest, or AES encryption are referred to in OpenSSL
as "algorithms". Each algorithm may have multiple implementations available for use. For example
the RSA algorithm is available as a "default" implementation suitable for general use, and a "Gps"
implementation which has been validated to FIPS 140 standards for situations where that is
important. It is also possible that a third party could add additional implementations such as in a
hardware security module (HSM).
Operations
Different algorithms can be grouped together by their purpose. For example there are algorithms for
encryption, and different algorithms for digesting data. These different groups are known as
"operations" in OpenSSL. Each operation has a different set of functions associated with it. For
example to perform an encryption operation using AES (or any other encryption algorithm) you
would use the encryption functions detailed on the EVP_EncryptInit(3) page. Or to perform a digest
operation using SHA256 then you would use the digesting functions on the EVP_DigestInit(3) page.
ALGORITHM FETCHING
In order to use an algorithm an implementation for it must Grst be "fetched". Fetching is the process
of looking through the available implementations, applying selection criteria (via a property query
string), and Gnally choosing the implementation that will be used.
Two types of fetching are supported by OpenSSL - "Explicit fetching" and "Implicit fetching".
Explicit fetching
Explicit fetching involves directly calling a speciGc API to fetch an algorithm implementation from a
provider. This fetched object can then be passed to other APIs. These explicit fetching functions
usually have the name APINAME_fetch , where APINAME is the name of the operation. For example
EVP_MD_fetch(3) can be used to explicitly fetch a digest algorithm implementation. The user is
responsible for freeing the object returned from the APINAME_fetch function using APINAME_free
when it is no longer needed.
These fetching functions follow a fairly common pattern, where three arguments are passed:
See OSSL_LIB_CTX(3) for a more detailed description. This may be NULL to signify the default
(global) library context, or a context created by the user. Only providers loaded in this library
context (see OSSL_PROVIDER_load(3)) will be considered by the fetching function. In case no
provider has been loaded in this library context then the default provider will be loaded as a
fallback (see OSSL_PROVIDER-default(7)).
• An identiGer
For all currently implemented fetching functions this is the algorithm name. Each provider
supports a list of algorithm implementations. See the provider speciGc documentation for
information on the algorithm implementations available in each provider: "OPERATIONS AND
ALGORITHMS" in OSSL_PROVIDER-default(7), "OPERATIONS AND ALGORITHMS" in
OSSL_PROVIDER-FIPS(7), "OPERATIONS AND ALGORITHMS" in OSSL_PROVIDER-legacy(7)
and "OPERATIONS AND ALGORITHMS" in OSSL_PROVIDER-base(7).
Note, while providers may register algorithms against a list of names using a string with a colon
separated list of names, fetching algorithms using that format is currently unsupported.
The property query string used to guide selection of the algorithm implementation. See
"PROPERTY QUERY STRINGS" in ossl-guide-libraries-introduction(7).
The algorithm implementation that is fetched can then be used with other diverse functions that use
them. For example the EVP_DigestInit_ex(3) function takes as a parameter an EVP_MD object
which may have been returned from an earlier call to EVP_MD_fetch(3).
Implicit fetching
OpenSSL has a number of functions that return an algorithm object with no associated
implementation, such as EVP_sha256(3), EVP_aes_128_cbc(3), EVP_get_cipherbyname(3) or
EVP_get_digestbyname(3). These are present for compatibility with OpenSSL before version 3.0
where explicit fetching was not available.
When they are used with functions like EVP_DigestInit_ex(3) or EVP_CipherInit_ex(3), the actual
implementation to be used is fetched implicitly using default search criteria (which uses NULL for
the library context and property query string).
In some cases implicit fetching can also occur when a NULL algorithm parameter is supplied. In this
case an algorithm implementation is implicitly fetched using default search criteria and an algorithm
name that is consistent with the context in which it is being used.
Performance
If you perform the same operation many times with the same algorithm then it is recommended to
use a single explicit fetch of the algorithm and then reuse the explicitly fetched algorithm each
subsequent time. This will typically be faster than implicitly fetching the algorithm every time you
use it. See an example of Explicit fetching in "USING ALGORITHMS IN APPLICATIONS".
Prior to OpenSSL 3.0, functions such as EVP_sha256() which return a "const" object were used
directly to indicate the algorithm to use in various function calls. If you pass the return value of one
of these convenience functions to an operation then you are using implicit fetching. If you are
converting an application that worked with an OpenSSL version prior to OpenSSL 3.0 then consider
changing instances of implicit fetching to explicit fetching instead.
If an explicitly fetched object is not passed to an operation, then any implicit fetch will use an
internally cached prefetched object, but it will still be slower than passing the explicitly fetched
object directly.
The following functions can be used for explicit fetching:
• EVP_MD_fetch(3)
• EVP_CIPHER_fetch(3)
• EVP_KDF_fetch(3)
• EVP_MAC_fetch(3)
• EVP_KEM_fetch(3)
• OSSL_ENCODER_fetch(3)
• OSSL_DECODER_fetch(3)
Fetch a decoder algorithm implementation (e.g. to decode keys from a speciGed format).
• EVP_RAND_fetch(3)
FETCHING EXAMPLES
The following section provides a series of examples of fetching algorithm implementations.
Fetch any available implementation of SHA2-256 in the default context. Note that some algorithms
have aliases. So "SHA256" and "SHA2-256" are synonymous:
Fetch an implementation of SHA2-256 from the default provider in the default context:
Fetch an implementation of SHA2-256 that is not from the default provider in the default context:
Fetch an implementation of SHA2-256 that is preferably from the FIPS provider in the default
context:
Fetch an implementation of SHA2-256 from the default provider in the speciGed library context:
Load the legacy provider into the default context and then fetch an implementation of WHIRLPOOL
from it:
Note that in the above example the property string "provider=legacy" is optional since, assuming no
other providers have been loaded, the only implementation of the "whirlpool" algorithm is in the
"legacy" provider. Also note that the default provider should be explicitly loaded if it is required in
addition to other providers:
Most of these follow a common pattern. A "context" object is Grst created. For example for a digest
operation you would use an EVP_MD_CTX, and for an encryption/decryption operation you would
use an EVP_CIPHER_CTX. The operation is then initialised ready for use via an "init" function -
optionally passing in a set of parameters (using the OSSL_PARAM(3) type) to conGgure how the
operation should behave. Next data is fed into the operation in a series of "update" calls. The
operation is Gnalised using a "Gnal" call which will typically provide some kind of output. Finally the
context is cleaned up and freed.
The following shows a complete example for doing this process for digesting data using SHA256.
The process is similar for other operations such as encryption/decryption, signatures, message
authentication codes, etc. Additional examples can be found in the OpenSSL demos (see "DEMO
APPLICATIONS" in ossl-guide-libraries-introduction(7)).
#include <stdio.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
int main(void)
{
EVP_MD_CTX *ctx = NULL;
EVP_MD *sha256 = NULL;
const unsigned char msg[] = {
0x00, 0x01, 0x02, 0x03
};
unsigned int len = 0;
unsigned char *outdigest = NULL;
int ret = 1;
/*
* Fetch the SHA256 algorithm implementation for doing the digest. We're
* using the "default" library context here (first NULL parameter), and
* we're not supplying any particular search criteria for our SHA256
* implementation (second NULL parameter). Any SHA256 implementation will
* do.
* In a larger application this fetch would just be done once, and could
* be used for multiple calls to other operations such as
EVP_DigestInit_ex().
*/
sha256 = EVP_MD_fetch(NULL, "SHA256", NULL);
if (sha256 == NULL)
goto err;
/*
* Pass the message to be digested. This can be passed in over multiple
* EVP_DigestUpdate calls if necessary
*/
if (!EVP_DigestUpdate(ctx, msg, sizeof(msg)))
goto err;
ret = 0;
err:
/* Clean up all the resources we allocated */
OPENSSL_free(outdigest);
EVP_MD_free(sha256);
EVP_MD_CTX_free(ctx);
if (ret != 0)
ERR_print_errors_fp(stderr);
return ret;
}
Encoders and decoders are just algorithm implementations in the same way as any other algorithm
implementation in OpenSSL. They are implemented by providers. The OpenSSL encoders and
decoders are available in the default provider. They are also duplicated in the base provider.
As well as using encoders/decoders directly there are also some helper functions that can be used
for certain well known and commonly used formats. For example see PEM_read_PrivateKey(3) and
PEM_write_PrivateKey(3) for information about reading and writing key data from PEM encoded
Gles.
FURTHER READING
See ossl-guide-libssl-introduction(7) for an introduction to using libssl .
SEE ALSO
openssl(1), ssl(7), evp(7), OSSL_LIB_CTX(3), openssl-threads(7), property(7), OSSL_PROVIDER-
default(7), OSSL_PROVIDER-base(7), OSSL_PROVIDER-FIPS(7), OSSL_PROVIDER-legacy(7),
OSSL_PROVIDER-null(7), openssl-glossary(7), provider(7)
COPYRIGHT
Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this Gle except in
compliance with the License. You can obtain a copy in the Gle LICENSE in the source distribution or
at https://www.openssl.org/source/license.html.