xref: /freebsd/crypto/openssl/doc/man7/ossl-guide-libcrypto-introduction.pod (revision e7be843b4a162e68651d3911f0357ed464915629)
1*e7be843bSPierre Pronchery=pod
2*e7be843bSPierre Pronchery
3*e7be843bSPierre Pronchery=head1 NAME
4*e7be843bSPierre Pronchery
5*e7be843bSPierre Proncheryossl-guide-libcrypto-introduction, crypto
6*e7be843bSPierre Pronchery- OpenSSL Guide: An introduction to libcrypto
7*e7be843bSPierre Pronchery
8*e7be843bSPierre Pronchery
9*e7be843bSPierre Pronchery=head1 INTRODUCTION
10*e7be843bSPierre Pronchery
11*e7be843bSPierre ProncheryThe OpenSSL cryptography library (C<libcrypto>) enables access to a wide range
12*e7be843bSPierre Proncheryof cryptographic algorithms used in various Internet standards. The services
13*e7be843bSPierre Proncheryprovided by this library are used by the OpenSSL implementations of TLS and
14*e7be843bSPierre ProncheryCMS, and they have also been used to implement many other third party products
15*e7be843bSPierre Proncheryand protocols.
16*e7be843bSPierre Pronchery
17*e7be843bSPierre ProncheryThe functionality includes symmetric encryption, public key cryptography, key
18*e7be843bSPierre Proncheryagreement, certificate handling, cryptographic hash functions, cryptographic
19*e7be843bSPierre Proncherypseudo-random number generators, message authentication codes (MACs), key
20*e7be843bSPierre Proncheryderivation functions (KDFs), and various utilities.
21*e7be843bSPierre Pronchery
22*e7be843bSPierre Pronchery=head2 Algorithms
23*e7be843bSPierre Pronchery
24*e7be843bSPierre ProncheryCryptographic primitives such as the SHA256 digest, or AES encryption are
25*e7be843bSPierre Proncheryreferred to in OpenSSL as "algorithms". Each algorithm may have multiple
26*e7be843bSPierre Proncheryimplementations available for use. For example the RSA algorithm is available as
27*e7be843bSPierre Proncherya "default" implementation suitable for general use, and a "fips" implementation
28*e7be843bSPierre Proncherywhich has been validated to FIPS 140 standards for situations where that is
29*e7be843bSPierre Proncheryimportant. It is also possible that a third party could add additional
30*e7be843bSPierre Proncheryimplementations such as in a hardware security module (HSM).
31*e7be843bSPierre Pronchery
32*e7be843bSPierre ProncheryAlgorithms are implemented in providers. See
33*e7be843bSPierre ProncheryL<ossl-guide-libraries-introduction(7)> for information about providers.
34*e7be843bSPierre Pronchery
35*e7be843bSPierre Pronchery=head2 Operations
36*e7be843bSPierre Pronchery
37*e7be843bSPierre ProncheryDifferent algorithms can be grouped together by their purpose. For example there
38*e7be843bSPierre Proncheryare algorithms for encryption, and different algorithms for digesting data.
39*e7be843bSPierre ProncheryThese different groups are known as "operations" in OpenSSL. Each operation
40*e7be843bSPierre Proncheryhas a different set of functions associated with it. For example to perform an
41*e7be843bSPierre Proncheryencryption operation using AES (or any other encryption algorithm) you would use
42*e7be843bSPierre Proncherythe encryption functions detailed on the L<EVP_EncryptInit(3)> page. Or to
43*e7be843bSPierre Proncheryperform a digest operation using SHA256 then you would use the digesting
44*e7be843bSPierre Proncheryfunctions on the L<EVP_DigestInit(3)> page.
45*e7be843bSPierre Pronchery
46*e7be843bSPierre Pronchery=head1 ALGORITHM FETCHING
47*e7be843bSPierre Pronchery
48*e7be843bSPierre ProncheryIn order to use an algorithm an implementation for it must first be "fetched".
49*e7be843bSPierre ProncheryFetching is the process of looking through the available implementations,
50*e7be843bSPierre Proncheryapplying selection criteria (via a property query string), and finally choosing
51*e7be843bSPierre Proncherythe implementation that will be used.
52*e7be843bSPierre Pronchery
53*e7be843bSPierre ProncheryTwo types of fetching are supported by OpenSSL - L</Explicit fetching> and
54*e7be843bSPierre ProncheryL</Implicit fetching>.
55*e7be843bSPierre Pronchery
56*e7be843bSPierre Pronchery=head2 Explicit fetching
57*e7be843bSPierre Pronchery
58*e7be843bSPierre ProncheryExplicit fetching involves directly calling a specific API to fetch an algorithm
59*e7be843bSPierre Proncheryimplementation from a provider. This fetched object can then be passed to other
60*e7be843bSPierre ProncheryAPIs. These explicit fetching functions usually have the name C<APINAME_fetch>,
61*e7be843bSPierre Proncherywhere C<APINAME> is the name of the operation. For example L<EVP_MD_fetch(3)>
62*e7be843bSPierre Proncherycan be used to explicitly fetch a digest algorithm implementation. The user is
63*e7be843bSPierre Proncheryresponsible for freeing the object returned from the C<APINAME_fetch> function
64*e7be843bSPierre Proncheryusing C<APINAME_free> when it is no longer needed.
65*e7be843bSPierre Pronchery
66*e7be843bSPierre ProncheryThese fetching functions follow a fairly common pattern, where three
67*e7be843bSPierre Proncheryarguments are passed:
68*e7be843bSPierre Pronchery
69*e7be843bSPierre Pronchery=over 4
70*e7be843bSPierre Pronchery
71*e7be843bSPierre Pronchery=item The library context
72*e7be843bSPierre Pronchery
73*e7be843bSPierre ProncherySee L<OSSL_LIB_CTX(3)> for a more detailed description.
74*e7be843bSPierre ProncheryThis may be NULL to signify the default (global) library context, or a
75*e7be843bSPierre Proncherycontext created by the user. Only providers loaded in this library context (see
76*e7be843bSPierre ProncheryL<OSSL_PROVIDER_load(3)>) will be considered by the fetching function. In case
77*e7be843bSPierre Proncheryno provider has been loaded in this library context then the default provider
78*e7be843bSPierre Proncherywill be loaded as a fallback (see L<OSSL_PROVIDER-default(7)>).
79*e7be843bSPierre Pronchery
80*e7be843bSPierre Pronchery=item An identifier
81*e7be843bSPierre Pronchery
82*e7be843bSPierre ProncheryFor all currently implemented fetching functions this is the algorithm name.
83*e7be843bSPierre ProncheryEach provider supports a list of algorithm implementations. See the provider
84*e7be843bSPierre Proncheryspecific documentation for information on the algorithm implementations
85*e7be843bSPierre Proncheryavailable in each provider:
86*e7be843bSPierre ProncheryL<OSSL_PROVIDER-default(7)/OPERATIONS AND ALGORITHMS>,
87*e7be843bSPierre ProncheryL<OSSL_PROVIDER-FIPS(7)/OPERATIONS AND ALGORITHMS>,
88*e7be843bSPierre ProncheryL<OSSL_PROVIDER-legacy(7)/OPERATIONS AND ALGORITHMS> and
89*e7be843bSPierre ProncheryL<OSSL_PROVIDER-base(7)/OPERATIONS AND ALGORITHMS>.
90*e7be843bSPierre Pronchery
91*e7be843bSPierre ProncheryNote, while providers may register algorithms against a list of names using a
92*e7be843bSPierre Proncherystring with a colon separated list of names, fetching algorithms using that
93*e7be843bSPierre Proncheryformat is currently unsupported.
94*e7be843bSPierre Pronchery
95*e7be843bSPierre Pronchery=item A property query string
96*e7be843bSPierre Pronchery
97*e7be843bSPierre ProncheryThe property query string used to guide selection of the algorithm
98*e7be843bSPierre Proncheryimplementation. See
99*e7be843bSPierre ProncheryL<ossl-guide-libraries-introduction(7)/PROPERTY QUERY STRINGS>.
100*e7be843bSPierre Pronchery
101*e7be843bSPierre Pronchery=back
102*e7be843bSPierre Pronchery
103*e7be843bSPierre ProncheryThe algorithm implementation that is fetched can then be used with other diverse
104*e7be843bSPierre Proncheryfunctions that use them. For example the L<EVP_DigestInit_ex(3)> function takes
105*e7be843bSPierre Proncheryas a parameter an B<EVP_MD> object which may have been returned from an earlier
106*e7be843bSPierre Proncherycall to L<EVP_MD_fetch(3)>.
107*e7be843bSPierre Pronchery
108*e7be843bSPierre Pronchery=head2 Implicit fetching
109*e7be843bSPierre Pronchery
110*e7be843bSPierre ProncheryOpenSSL has a number of functions that return an algorithm object with no
111*e7be843bSPierre Proncheryassociated implementation, such as L<EVP_sha256(3)>, L<EVP_aes_128_cbc(3)>,
112*e7be843bSPierre ProncheryL<EVP_get_cipherbyname(3)> or L<EVP_get_digestbyname(3)>. These are present for
113*e7be843bSPierre Proncherycompatibility with OpenSSL before version 3.0 where explicit fetching was not
114*e7be843bSPierre Proncheryavailable.
115*e7be843bSPierre Pronchery
116*e7be843bSPierre ProncheryWhen they are used with functions like L<EVP_DigestInit_ex(3)> or
117*e7be843bSPierre ProncheryL<EVP_CipherInit_ex(3)>, the actual implementation to be used is
118*e7be843bSPierre Proncheryfetched implicitly using default search criteria (which uses NULL for the
119*e7be843bSPierre Proncherylibrary context and property query string).
120*e7be843bSPierre Pronchery
121*e7be843bSPierre ProncheryIn some cases implicit fetching can also occur when a NULL algorithm parameter
122*e7be843bSPierre Proncheryis supplied. In this case an algorithm implementation is implicitly fetched
123*e7be843bSPierre Proncheryusing default search criteria and an algorithm name that is consistent with
124*e7be843bSPierre Proncherythe context in which it is being used.
125*e7be843bSPierre Pronchery
126*e7be843bSPierre ProncheryFunctions that use an B<EVP_PKEY_CTX> or an L<EVP_PKEY(3)>, such as
127*e7be843bSPierre ProncheryL<EVP_DigestSignInit(3)>, all fetch the implementations implicitly. Usually the
128*e7be843bSPierre Proncheryalgorithm to fetch is determined based on the type of key that is being used and
129*e7be843bSPierre Proncherythe function that has been called.
130*e7be843bSPierre Pronchery
131*e7be843bSPierre Pronchery=head2 Performance
132*e7be843bSPierre Pronchery
133*e7be843bSPierre ProncheryIf you perform the same operation many times with the same algorithm then it is
134*e7be843bSPierre Proncheryrecommended to use a single explicit fetch of the algorithm and then reuse the
135*e7be843bSPierre Proncheryexplicitly fetched algorithm each subsequent time. This will typically be
136*e7be843bSPierre Proncheryfaster than implicitly fetching the algorithm every time you use it. See an
137*e7be843bSPierre Proncheryexample of Explicit fetching in L</USING ALGORITHMS IN APPLICATIONS>.
138*e7be843bSPierre Pronchery
139*e7be843bSPierre ProncheryPrior to OpenSSL 3.0, functions such as EVP_sha256() which return a "const"
140*e7be843bSPierre Proncheryobject were used directly to indicate the algorithm to use in various function
141*e7be843bSPierre Proncherycalls. If you pass the return value of one of these convenience functions to an
142*e7be843bSPierre Proncheryoperation then you are using implicit fetching. If you are converting an
143*e7be843bSPierre Proncheryapplication that worked with an OpenSSL version prior to OpenSSL 3.0 then
144*e7be843bSPierre Proncheryconsider changing instances of implicit fetching to explicit fetching instead.
145*e7be843bSPierre Pronchery
146*e7be843bSPierre ProncheryIf an explicitly fetched object is not passed to an operation, then any implicit
147*e7be843bSPierre Proncheryfetch will use an internally cached prefetched object, but it will
148*e7be843bSPierre Proncherystill be slower than passing the explicitly fetched object directly.
149*e7be843bSPierre Pronchery
150*e7be843bSPierre ProncheryThe following functions can be used for explicit fetching:
151*e7be843bSPierre Pronchery
152*e7be843bSPierre Pronchery=over 4
153*e7be843bSPierre Pronchery
154*e7be843bSPierre Pronchery=item L<EVP_MD_fetch(3)>
155*e7be843bSPierre Pronchery
156*e7be843bSPierre ProncheryFetch a message digest/hashing algorithm implementation.
157*e7be843bSPierre Pronchery
158*e7be843bSPierre Pronchery=item L<EVP_CIPHER_fetch(3)>
159*e7be843bSPierre Pronchery
160*e7be843bSPierre ProncheryFetch a symmetric cipher algorithm implementation.
161*e7be843bSPierre Pronchery
162*e7be843bSPierre Pronchery=item L<EVP_KDF_fetch(3)>
163*e7be843bSPierre Pronchery
164*e7be843bSPierre ProncheryFetch a Key Derivation Function (KDF) algorithm implementation.
165*e7be843bSPierre Pronchery
166*e7be843bSPierre Pronchery=item L<EVP_MAC_fetch(3)>
167*e7be843bSPierre Pronchery
168*e7be843bSPierre ProncheryFetch a Message Authentication Code (MAC) algorithm implementation.
169*e7be843bSPierre Pronchery
170*e7be843bSPierre Pronchery=item L<EVP_KEM_fetch(3)>
171*e7be843bSPierre Pronchery
172*e7be843bSPierre ProncheryFetch a Key Encapsulation Mechanism (KEM) algorithm implementation
173*e7be843bSPierre Pronchery
174*e7be843bSPierre Pronchery=item L<OSSL_ENCODER_fetch(3)>
175*e7be843bSPierre Pronchery
176*e7be843bSPierre ProncheryFetch an encoder algorithm implementation (e.g. to encode keys to a specified
177*e7be843bSPierre Proncheryformat).
178*e7be843bSPierre Pronchery
179*e7be843bSPierre Pronchery=item L<OSSL_DECODER_fetch(3)>
180*e7be843bSPierre Pronchery
181*e7be843bSPierre ProncheryFetch a decoder algorithm implementation (e.g. to decode keys from a specified
182*e7be843bSPierre Proncheryformat).
183*e7be843bSPierre Pronchery
184*e7be843bSPierre Pronchery=item L<EVP_RAND_fetch(3)>
185*e7be843bSPierre Pronchery
186*e7be843bSPierre ProncheryFetch a Pseudo Random Number Generator (PRNG) algorithm implementation.
187*e7be843bSPierre Pronchery
188*e7be843bSPierre Pronchery=back
189*e7be843bSPierre Pronchery
190*e7be843bSPierre ProncherySee L<OSSL_PROVIDER-default(7)/OPERATIONS AND ALGORITHMS>,
191*e7be843bSPierre ProncheryL<OSSL_PROVIDER-FIPS(7)/OPERATIONS AND ALGORITHMS>,
192*e7be843bSPierre ProncheryL<OSSL_PROVIDER-legacy(7)/OPERATIONS AND ALGORITHMS> and
193*e7be843bSPierre ProncheryL<OSSL_PROVIDER-base(7)/OPERATIONS AND ALGORITHMS> for a list of algorithm names
194*e7be843bSPierre Proncherythat can be fetched.
195*e7be843bSPierre Pronchery
196*e7be843bSPierre Pronchery=head1 FETCHING EXAMPLES
197*e7be843bSPierre Pronchery
198*e7be843bSPierre ProncheryThe following section provides a series of examples of fetching algorithm
199*e7be843bSPierre Proncheryimplementations.
200*e7be843bSPierre Pronchery
201*e7be843bSPierre ProncheryFetch any available implementation of SHA2-256 in the default context. Note
202*e7be843bSPierre Proncherythat some algorithms have aliases. So "SHA256" and "SHA2-256" are synonymous:
203*e7be843bSPierre Pronchery
204*e7be843bSPierre Pronchery EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", NULL);
205*e7be843bSPierre Pronchery ...
206*e7be843bSPierre Pronchery EVP_MD_free(md);
207*e7be843bSPierre Pronchery
208*e7be843bSPierre ProncheryFetch any available implementation of AES-128-CBC in the default context:
209*e7be843bSPierre Pronchery
210*e7be843bSPierre Pronchery EVP_CIPHER *cipher = EVP_CIPHER_fetch(NULL, "AES-128-CBC", NULL);
211*e7be843bSPierre Pronchery ...
212*e7be843bSPierre Pronchery EVP_CIPHER_free(cipher);
213*e7be843bSPierre Pronchery
214*e7be843bSPierre ProncheryFetch an implementation of SHA2-256 from the default provider in the default
215*e7be843bSPierre Proncherycontext:
216*e7be843bSPierre Pronchery
217*e7be843bSPierre Pronchery EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", "provider=default");
218*e7be843bSPierre Pronchery ...
219*e7be843bSPierre Pronchery EVP_MD_free(md);
220*e7be843bSPierre Pronchery
221*e7be843bSPierre ProncheryFetch an implementation of SHA2-256 that is not from the default provider in the
222*e7be843bSPierre Proncherydefault context:
223*e7be843bSPierre Pronchery
224*e7be843bSPierre Pronchery EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", "provider!=default");
225*e7be843bSPierre Pronchery ...
226*e7be843bSPierre Pronchery EVP_MD_free(md);
227*e7be843bSPierre Pronchery
228*e7be843bSPierre ProncheryFetch an implementation of SHA2-256 that is preferably from the FIPS provider in
229*e7be843bSPierre Proncherythe default context:
230*e7be843bSPierre Pronchery
231*e7be843bSPierre Pronchery EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", "provider=?fips");
232*e7be843bSPierre Pronchery ...
233*e7be843bSPierre Pronchery EVP_MD_free(md);
234*e7be843bSPierre Pronchery
235*e7be843bSPierre ProncheryFetch an implementation of SHA2-256 from the default provider in the specified
236*e7be843bSPierre Proncherylibrary context:
237*e7be843bSPierre Pronchery
238*e7be843bSPierre Pronchery EVP_MD *md = EVP_MD_fetch(libctx, "SHA2-256", "provider=default");
239*e7be843bSPierre Pronchery ...
240*e7be843bSPierre Pronchery EVP_MD_free(md);
241*e7be843bSPierre Pronchery
242*e7be843bSPierre ProncheryLoad the legacy provider into the default context and then fetch an
243*e7be843bSPierre Proncheryimplementation of WHIRLPOOL from it:
244*e7be843bSPierre Pronchery
245*e7be843bSPierre Pronchery /* This only needs to be done once - usually at application start up */
246*e7be843bSPierre Pronchery OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy");
247*e7be843bSPierre Pronchery
248*e7be843bSPierre Pronchery EVP_MD *md = EVP_MD_fetch(NULL, "WHIRLPOOL", "provider=legacy");
249*e7be843bSPierre Pronchery ...
250*e7be843bSPierre Pronchery EVP_MD_free(md);
251*e7be843bSPierre Pronchery
252*e7be843bSPierre ProncheryNote that in the above example the property string "provider=legacy" is optional
253*e7be843bSPierre Proncherysince, assuming no other providers have been loaded, the only implementation of
254*e7be843bSPierre Proncherythe "whirlpool" algorithm is in the "legacy" provider. Also note that the
255*e7be843bSPierre Proncherydefault provider should be explicitly loaded if it is required in addition to
256*e7be843bSPierre Proncheryother providers:
257*e7be843bSPierre Pronchery
258*e7be843bSPierre Pronchery /* This only needs to be done once - usually at application start up */
259*e7be843bSPierre Pronchery OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy");
260*e7be843bSPierre Pronchery OSSL_PROVIDER *default = OSSL_PROVIDER_load(NULL, "default");
261*e7be843bSPierre Pronchery
262*e7be843bSPierre Pronchery EVP_MD *md_whirlpool = EVP_MD_fetch(NULL, "whirlpool", NULL);
263*e7be843bSPierre Pronchery EVP_MD *md_sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL);
264*e7be843bSPierre Pronchery ...
265*e7be843bSPierre Pronchery EVP_MD_free(md_whirlpool);
266*e7be843bSPierre Pronchery EVP_MD_free(md_sha256);
267*e7be843bSPierre Pronchery
268*e7be843bSPierre Pronchery
269*e7be843bSPierre Pronchery=head1 USING ALGORITHMS IN APPLICATIONS
270*e7be843bSPierre Pronchery
271*e7be843bSPierre ProncheryCryptographic algorithms are made available to applications through use of the
272*e7be843bSPierre Pronchery"EVP" APIs. Each of the various operations such as encryption, digesting,
273*e7be843bSPierre Proncherymessage authentication codes, etc., have a set of EVP function calls that can
274*e7be843bSPierre Proncherybe invoked to use them. See the L<evp(7)> page for further details.
275*e7be843bSPierre Pronchery
276*e7be843bSPierre ProncheryMost of these follow a common pattern. A "context" object is first created. For
277*e7be843bSPierre Proncheryexample for a digest operation you would use an B<EVP_MD_CTX>, and for an
278*e7be843bSPierre Proncheryencryption/decryption operation you would use an B<EVP_CIPHER_CTX>. The
279*e7be843bSPierre Proncheryoperation is then initialised ready for use via an "init" function - optionally
280*e7be843bSPierre Proncherypassing in a set of parameters (using the L<OSSL_PARAM(3)> type) to configure how
281*e7be843bSPierre Proncherythe operation should behave. Next data is fed into the operation in a series of
282*e7be843bSPierre Pronchery"update" calls. The operation is finalised using a "final" call which will
283*e7be843bSPierre Proncherytypically provide some kind of output. Finally the context is cleaned up and
284*e7be843bSPierre Proncheryfreed.
285*e7be843bSPierre Pronchery
286*e7be843bSPierre ProncheryThe following shows a complete example for doing this process for digesting
287*e7be843bSPierre Proncherydata using SHA256. The process is similar for other operations such as
288*e7be843bSPierre Proncheryencryption/decryption, signatures, message authentication codes, etc. Additional
289*e7be843bSPierre Proncheryexamples can be found in the OpenSSL demos (see
290*e7be843bSPierre ProncheryL<ossl-guide-libraries-introduction(7)/DEMO APPLICATIONS>).
291*e7be843bSPierre Pronchery
292*e7be843bSPierre Pronchery #include <stdio.h>
293*e7be843bSPierre Pronchery #include <openssl/evp.h>
294*e7be843bSPierre Pronchery #include <openssl/bio.h>
295*e7be843bSPierre Pronchery #include <openssl/err.h>
296*e7be843bSPierre Pronchery
297*e7be843bSPierre Pronchery int main(void)
298*e7be843bSPierre Pronchery {
299*e7be843bSPierre Pronchery     EVP_MD_CTX *ctx = NULL;
300*e7be843bSPierre Pronchery     EVP_MD *sha256 = NULL;
301*e7be843bSPierre Pronchery     const unsigned char msg[] = {
302*e7be843bSPierre Pronchery         0x00, 0x01, 0x02, 0x03
303*e7be843bSPierre Pronchery     };
304*e7be843bSPierre Pronchery     unsigned int len = 0;
305*e7be843bSPierre Pronchery     unsigned char *outdigest = NULL;
306*e7be843bSPierre Pronchery     int ret = 1;
307*e7be843bSPierre Pronchery
308*e7be843bSPierre Pronchery     /* Create a context for the digest operation */
309*e7be843bSPierre Pronchery     ctx = EVP_MD_CTX_new();
310*e7be843bSPierre Pronchery     if (ctx == NULL)
311*e7be843bSPierre Pronchery         goto err;
312*e7be843bSPierre Pronchery
313*e7be843bSPierre Pronchery     /*
314*e7be843bSPierre Pronchery      * Fetch the SHA256 algorithm implementation for doing the digest. We're
315*e7be843bSPierre Pronchery      * using the "default" library context here (first NULL parameter), and
316*e7be843bSPierre Pronchery      * we're not supplying any particular search criteria for our SHA256
317*e7be843bSPierre Pronchery      * implementation (second NULL parameter). Any SHA256 implementation will
318*e7be843bSPierre Pronchery      * do.
319*e7be843bSPierre Pronchery      * In a larger application this fetch would just be done once, and could
320*e7be843bSPierre Pronchery      * be used for multiple calls to other operations such as EVP_DigestInit_ex().
321*e7be843bSPierre Pronchery      */
322*e7be843bSPierre Pronchery     sha256 = EVP_MD_fetch(NULL, "SHA256", NULL);
323*e7be843bSPierre Pronchery     if (sha256 == NULL)
324*e7be843bSPierre Pronchery         goto err;
325*e7be843bSPierre Pronchery
326*e7be843bSPierre Pronchery    /* Initialise the digest operation */
327*e7be843bSPierre Pronchery    if (!EVP_DigestInit_ex(ctx, sha256, NULL))
328*e7be843bSPierre Pronchery        goto err;
329*e7be843bSPierre Pronchery
330*e7be843bSPierre Pronchery     /*
331*e7be843bSPierre Pronchery      * Pass the message to be digested. This can be passed in over multiple
332*e7be843bSPierre Pronchery      * EVP_DigestUpdate calls if necessary
333*e7be843bSPierre Pronchery      */
334*e7be843bSPierre Pronchery     if (!EVP_DigestUpdate(ctx, msg, sizeof(msg)))
335*e7be843bSPierre Pronchery         goto err;
336*e7be843bSPierre Pronchery
337*e7be843bSPierre Pronchery     /* Allocate the output buffer */
338*e7be843bSPierre Pronchery     outdigest = OPENSSL_malloc(EVP_MD_get_size(sha256));
339*e7be843bSPierre Pronchery     if (outdigest == NULL)
340*e7be843bSPierre Pronchery         goto err;
341*e7be843bSPierre Pronchery
342*e7be843bSPierre Pronchery     /* Now calculate the digest itself */
343*e7be843bSPierre Pronchery     if (!EVP_DigestFinal_ex(ctx, outdigest, &len))
344*e7be843bSPierre Pronchery         goto err;
345*e7be843bSPierre Pronchery
346*e7be843bSPierre Pronchery     /* Print out the digest result */
347*e7be843bSPierre Pronchery     BIO_dump_fp(stdout, outdigest, len);
348*e7be843bSPierre Pronchery
349*e7be843bSPierre Pronchery     ret = 0;
350*e7be843bSPierre Pronchery
351*e7be843bSPierre Pronchery  err:
352*e7be843bSPierre Pronchery     /* Clean up all the resources we allocated */
353*e7be843bSPierre Pronchery     OPENSSL_free(outdigest);
354*e7be843bSPierre Pronchery     EVP_MD_free(sha256);
355*e7be843bSPierre Pronchery     EVP_MD_CTX_free(ctx);
356*e7be843bSPierre Pronchery     if (ret != 0)
357*e7be843bSPierre Pronchery        ERR_print_errors_fp(stderr);
358*e7be843bSPierre Pronchery     return ret;
359*e7be843bSPierre Pronchery }
360*e7be843bSPierre Pronchery
361*e7be843bSPierre Pronchery=head1 ENCODING AND DECODING KEYS
362*e7be843bSPierre Pronchery
363*e7be843bSPierre ProncheryMany algorithms require the use of a key. Keys can be generated dynamically
364*e7be843bSPierre Proncheryusing the EVP APIs (for example see L<EVP_PKEY_Q_keygen(3)>). However it is often
365*e7be843bSPierre Proncherynecessary to save or load keys (or their associated parameters) to or from some
366*e7be843bSPierre Proncheryexternal format such as PEM or DER (see L<openssl-glossary(7)>). OpenSSL uses
367*e7be843bSPierre Proncheryencoders and decoders to perform this task.
368*e7be843bSPierre Pronchery
369*e7be843bSPierre ProncheryEncoders and decoders are just algorithm implementations in the same way as
370*e7be843bSPierre Proncheryany other algorithm implementation in OpenSSL. They are implemented by
371*e7be843bSPierre Proncheryproviders. The OpenSSL encoders and decoders are available in the default
372*e7be843bSPierre Proncheryprovider. They are also duplicated in the base provider.
373*e7be843bSPierre Pronchery
374*e7be843bSPierre ProncheryFor information about encoders see L<OSSL_ENCODER_CTX_new_for_pkey(3)>. For
375*e7be843bSPierre Proncheryinformation about decoders see L<OSSL_DECODER_CTX_new_for_pkey(3)>.
376*e7be843bSPierre Pronchery
377*e7be843bSPierre ProncheryAs well as using encoders/decoders directly there are also some helper functions
378*e7be843bSPierre Proncherythat can be used for certain well known and commonly used formats. For example
379*e7be843bSPierre Proncherysee L<PEM_read_PrivateKey(3)> and L<PEM_write_PrivateKey(3)> for information
380*e7be843bSPierre Proncheryabout reading and writing key data from PEM encoded files.
381*e7be843bSPierre Pronchery
382*e7be843bSPierre Pronchery=head1 FURTHER READING
383*e7be843bSPierre Pronchery
384*e7be843bSPierre ProncherySee L<ossl-guide-libssl-introduction(7)> for an introduction to using C<libssl>.
385*e7be843bSPierre Pronchery
386*e7be843bSPierre Pronchery=head1 SEE ALSO
387*e7be843bSPierre Pronchery
388*e7be843bSPierre ProncheryL<openssl(1)>, L<ssl(7)>, L<evp(7)>, L<OSSL_LIB_CTX(3)>, L<openssl-threads(7)>,
389*e7be843bSPierre ProncheryL<property(7)>, L<OSSL_PROVIDER-default(7)>, L<OSSL_PROVIDER-base(7)>,
390*e7be843bSPierre ProncheryL<OSSL_PROVIDER-FIPS(7)>, L<OSSL_PROVIDER-legacy(7)>, L<OSSL_PROVIDER-null(7)>,
391*e7be843bSPierre ProncheryL<openssl-glossary(7)>, L<provider(7)>
392*e7be843bSPierre Pronchery
393*e7be843bSPierre Pronchery=head1 COPYRIGHT
394*e7be843bSPierre Pronchery
395*e7be843bSPierre ProncheryCopyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.
396*e7be843bSPierre Pronchery
397*e7be843bSPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
398*e7be843bSPierre Proncherythis file except in compliance with the License.  You can obtain a copy
399*e7be843bSPierre Proncheryin the file LICENSE in the source distribution or at
400*e7be843bSPierre ProncheryL<https://www.openssl.org/source/license.html>.
401*e7be843bSPierre Pronchery
402*e7be843bSPierre Pronchery=cut
403