ossl-guide-libcrypto-introduction.pod 15 KB

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