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