ossl-guide-libcrypto-introduction.7ossl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. .\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42)
  2. .\"
  3. .\" Standard preamble:
  4. .\" ========================================================================
  5. .de Sp \" Vertical space (when we can't use .PP)
  6. .if t .sp .5v
  7. .if n .sp
  8. ..
  9. .de Vb \" Begin verbatim text
  10. .ft CW
  11. .nf
  12. .ne \\$1
  13. ..
  14. .de Ve \" End verbatim text
  15. .ft R
  16. .fi
  17. ..
  18. .\" Set up some character translations and predefined strings. \*(-- will
  19. .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
  20. .\" double quote, and \*(R" will give a right double quote. \*(C+ will
  21. .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
  22. .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
  23. .\" nothing in troff, for use with C<>.
  24. .tr \(*W-
  25. .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
  26. .ie n \{\
  27. . ds -- \(*W-
  28. . ds PI pi
  29. . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
  30. . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
  31. . ds L" ""
  32. . ds R" ""
  33. . ds C` ""
  34. . ds C' ""
  35. 'br\}
  36. .el\{\
  37. . ds -- \|\(em\|
  38. . ds PI \(*p
  39. . ds L" ``
  40. . ds R" ''
  41. . ds C`
  42. . ds C'
  43. 'br\}
  44. .\"
  45. .\" Escape single quotes in literal strings from groff's Unicode transform.
  46. .ie \n(.g .ds Aq \(aq
  47. .el .ds Aq '
  48. .\"
  49. .\" If the F register is >0, we'll generate index entries on stderr for
  50. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  51. .\" entries marked with X<> in POD. Of course, you'll have to process the
  52. .\" output yourself in some meaningful fashion.
  53. .\"
  54. .\" Avoid warning from groff about undefined register 'F'.
  55. .de IX
  56. ..
  57. .nr rF 0
  58. .if \n(.g .if rF .nr rF 1
  59. .if (\n(rF:(\n(.g==0)) \{\
  60. . if \nF \{\
  61. . de IX
  62. . tm Index:\\$1\t\\n%\t"\\$2"
  63. ..
  64. . if !\nF==2 \{\
  65. . nr % 0
  66. . nr F 2
  67. . \}
  68. . \}
  69. .\}
  70. .rr rF
  71. .\"
  72. .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
  73. .\" Fear. Run. Save yourself. No user-serviceable parts.
  74. . \" fudge factors for nroff and troff
  75. .if n \{\
  76. . ds #H 0
  77. . ds #V .8m
  78. . ds #F .3m
  79. . ds #[ \f1
  80. . ds #] \fP
  81. .\}
  82. .if t \{\
  83. . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
  84. . ds #V .6m
  85. . ds #F 0
  86. . ds #[ \&
  87. . ds #] \&
  88. .\}
  89. . \" simple accents for nroff and troff
  90. .if n \{\
  91. . ds ' \&
  92. . ds ` \&
  93. . ds ^ \&
  94. . ds , \&
  95. . ds ~ ~
  96. . ds /
  97. .\}
  98. .if t \{\
  99. . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
  100. . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
  101. . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
  102. . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
  103. . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
  104. . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
  105. .\}
  106. . \" troff and (daisy-wheel) nroff accents
  107. .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
  108. .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
  109. .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
  110. .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
  111. .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
  112. .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
  113. .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
  114. .ds ae a\h'-(\w'a'u*4/10)'e
  115. .ds Ae A\h'-(\w'A'u*4/10)'E
  116. . \" corrections for vroff
  117. .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
  118. .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
  119. . \" for low resolution devices (crt and lpr)
  120. .if \n(.H>23 .if \n(.V>19 \
  121. \{\
  122. . ds : e
  123. . ds 8 ss
  124. . ds o a
  125. . ds d- d\h'-1'\(ga
  126. . ds D- D\h'-1'\(hy
  127. . ds th \o'bp'
  128. . ds Th \o'LP'
  129. . ds ae ae
  130. . ds Ae AE
  131. .\}
  132. .rm #[ #] #H #V #F C
  133. .\" ========================================================================
  134. .\"
  135. .IX Title "OSSL-GUIDE-LIBCRYPTO-INTRODUCTION 7ossl"
  136. .TH OSSL-GUIDE-LIBCRYPTO-INTRODUCTION 7ossl "2024-09-03" "3.3.2" "OpenSSL"
  137. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  138. .\" way too many mistakes in technical documents.
  139. .if n .ad l
  140. .nh
  141. .SH "NAME"
  142. ossl\-guide\-libcrypto\-introduction, crypto
  143. \&\- OpenSSL Guide: An introduction to libcrypto
  144. .SH "INTRODUCTION"
  145. .IX Header "INTRODUCTION"
  146. The OpenSSL cryptography library (\f(CW\*(C`libcrypto\*(C'\fR) enables access to a wide range
  147. of cryptographic algorithms used in various Internet standards. The services
  148. provided by this library are used by the OpenSSL implementations of \s-1TLS\s0 and
  149. \&\s-1CMS,\s0 and they have also been used to implement many other third party products
  150. and protocols.
  151. .PP
  152. The functionality includes symmetric encryption, public key cryptography, key
  153. agreement, certificate handling, cryptographic hash functions, cryptographic
  154. pseudo-random number generators, message authentication codes (MACs), key
  155. derivation functions (KDFs), and various utilities.
  156. .SS "Algorithms"
  157. .IX Subsection "Algorithms"
  158. Cryptographic primitives such as the \s-1SHA256\s0 digest, or \s-1AES\s0 encryption are
  159. referred to in OpenSSL as \*(L"algorithms\*(R". Each algorithm may have multiple
  160. implementations available for use. For example the \s-1RSA\s0 algorithm is available as
  161. a \*(L"default\*(R" implementation suitable for general use, and a \*(L"fips\*(R" implementation
  162. which has been validated to \s-1FIPS 140\s0 standards for situations where that is
  163. important. It is also possible that a third party could add additional
  164. implementations such as in a hardware security module (\s-1HSM\s0).
  165. .PP
  166. Algorithms are implemented in providers. See
  167. \&\fBossl\-guide\-libraries\-introduction\fR\|(7) for information about providers.
  168. .SS "Operations"
  169. .IX Subsection "Operations"
  170. Different algorithms can be grouped together by their purpose. For example there
  171. are algorithms for encryption, and different algorithms for digesting data.
  172. These different groups are known as \*(L"operations\*(R" in OpenSSL. Each operation
  173. has a different set of functions associated with it. For example to perform an
  174. encryption operation using \s-1AES\s0 (or any other encryption algorithm) you would use
  175. the encryption functions detailed on the \fBEVP_EncryptInit\fR\|(3) page. Or to
  176. perform a digest operation using \s-1SHA256\s0 then you would use the digesting
  177. functions on the \fBEVP_DigestInit\fR\|(3) page.
  178. .SH "ALGORITHM FETCHING"
  179. .IX Header "ALGORITHM FETCHING"
  180. In order to use an algorithm an implementation for it must first be \*(L"fetched\*(R".
  181. Fetching is the process of looking through the available implementations,
  182. applying selection criteria (via a property query string), and finally choosing
  183. the implementation that will be used.
  184. .PP
  185. Two types of fetching are supported by OpenSSL \- \*(L"Explicit fetching\*(R" and
  186. \&\*(L"Implicit fetching\*(R".
  187. .SS "Explicit fetching"
  188. .IX Subsection "Explicit fetching"
  189. Explicit fetching involves directly calling a specific \s-1API\s0 to fetch an algorithm
  190. implementation from a provider. This fetched object can then be passed to other
  191. APIs. These explicit fetching functions usually have the name \f(CW\*(C`APINAME_fetch\*(C'\fR,
  192. where \f(CW\*(C`APINAME\*(C'\fR is the name of the operation. For example \fBEVP_MD_fetch\fR\|(3)
  193. can be used to explicitly fetch a digest algorithm implementation. The user is
  194. responsible for freeing the object returned from the \f(CW\*(C`APINAME_fetch\*(C'\fR function
  195. using \f(CW\*(C`APINAME_free\*(C'\fR when it is no longer needed.
  196. .PP
  197. These fetching functions follow a fairly common pattern, where three
  198. arguments are passed:
  199. .IP "The library context" 4
  200. .IX Item "The library context"
  201. See \s-1\fBOSSL_LIB_CTX\s0\fR\|(3) for a more detailed description.
  202. This may be \s-1NULL\s0 to signify the default (global) library context, or a
  203. context created by the user. Only providers loaded in this library context (see
  204. \&\fBOSSL_PROVIDER_load\fR\|(3)) will be considered by the fetching function. In case
  205. no provider has been loaded in this library context then the default provider
  206. will be loaded as a fallback (see \fBOSSL_PROVIDER\-default\fR\|(7)).
  207. .IP "An identifier" 4
  208. .IX Item "An identifier"
  209. For all currently implemented fetching functions this is the algorithm name.
  210. Each provider supports a list of algorithm implementations. See the provider
  211. specific documentation for information on the algorithm implementations
  212. available in each provider:
  213. \&\*(L"\s-1OPERATIONS AND ALGORITHMS\*(R"\s0 in \fBOSSL_PROVIDER\-default\fR\|(7),
  214. \&\*(L"\s-1OPERATIONS AND ALGORITHMS\*(R"\s0 in \s-1\fBOSSL_PROVIDER\-FIPS\s0\fR\|(7),
  215. \&\*(L"\s-1OPERATIONS AND ALGORITHMS\*(R"\s0 in \fBOSSL_PROVIDER\-legacy\fR\|(7) and
  216. \&\*(L"\s-1OPERATIONS AND ALGORITHMS\*(R"\s0 in \fBOSSL_PROVIDER\-base\fR\|(7).
  217. .Sp
  218. Note, while providers may register algorithms against a list of names using a
  219. string with a colon separated list of names, fetching algorithms using that
  220. format is currently unsupported.
  221. .IP "A property query string" 4
  222. .IX Item "A property query string"
  223. The property query string used to guide selection of the algorithm
  224. implementation. See
  225. \&\*(L"\s-1PROPERTY QUERY STRINGS\*(R"\s0 in \fBossl\-guide\-libraries\-introduction\fR\|(7).
  226. .PP
  227. The algorithm implementation that is fetched can then be used with other diverse
  228. functions that use them. For example the \fBEVP_DigestInit_ex\fR\|(3) function takes
  229. as a parameter an \fB\s-1EVP_MD\s0\fR object which may have been returned from an earlier
  230. call to \fBEVP_MD_fetch\fR\|(3).
  231. .SS "Implicit fetching"
  232. .IX Subsection "Implicit fetching"
  233. OpenSSL has a number of functions that return an algorithm object with no
  234. associated implementation, such as \fBEVP_sha256\fR\|(3), \fBEVP_aes_128_cbc\fR\|(3),
  235. \&\fBEVP_get_cipherbyname\fR\|(3) or \fBEVP_get_digestbyname\fR\|(3). These are present for
  236. compatibility with OpenSSL before version 3.0 where explicit fetching was not
  237. available.
  238. .PP
  239. When they are used with functions like \fBEVP_DigestInit_ex\fR\|(3) or
  240. \&\fBEVP_CipherInit_ex\fR\|(3), the actual implementation to be used is
  241. fetched implicitly using default search criteria (which uses \s-1NULL\s0 for the
  242. library context and property query string).
  243. .PP
  244. In some cases implicit fetching can also occur when a \s-1NULL\s0 algorithm parameter
  245. is supplied. In this case an algorithm implementation is implicitly fetched
  246. using default search criteria and an algorithm name that is consistent with
  247. the context in which it is being used.
  248. .PP
  249. Functions that use an \fB\s-1EVP_PKEY_CTX\s0\fR or an \s-1\fBEVP_PKEY\s0\fR\|(3), such as
  250. \&\fBEVP_DigestSignInit\fR\|(3), all fetch the implementations implicitly. Usually the
  251. algorithm to fetch is determined based on the type of key that is being used and
  252. the function that has been called.
  253. .SS "Performance"
  254. .IX Subsection "Performance"
  255. If you perform the same operation many times with the same algorithm then it is
  256. recommended to use a single explicit fetch of the algorithm and then reuse the
  257. explicitly fetched algorithm each subsequent time. This will typically be
  258. faster than implicitly fetching the algorithm every time you use it. See an
  259. example of Explicit fetching in \*(L"\s-1USING ALGORITHMS IN APPLICATIONS\*(R"\s0.
  260. .PP
  261. Prior to OpenSSL 3.0, functions such as \fBEVP_sha256()\fR which return a \*(L"const\*(R"
  262. object were used directly to indicate the algorithm to use in various function
  263. calls. If you pass the return value of one of these convenience functions to an
  264. operation then you are using implicit fetching. If you are converting an
  265. application that worked with an OpenSSL version prior to OpenSSL 3.0 then
  266. consider changing instances of implicit fetching to explicit fetching instead.
  267. .PP
  268. If an explicitly fetched object is not passed to an operation, then any implicit
  269. fetch will use an internally cached prefetched object, but it will
  270. still be slower than passing the explicitly fetched object directly.
  271. .PP
  272. The following functions can be used for explicit fetching:
  273. .IP "\fBEVP_MD_fetch\fR\|(3)" 4
  274. .IX Item "EVP_MD_fetch"
  275. Fetch a message digest/hashing algorithm implementation.
  276. .IP "\fBEVP_CIPHER_fetch\fR\|(3)" 4
  277. .IX Item "EVP_CIPHER_fetch"
  278. Fetch a symmetric cipher algorithm implementation.
  279. .IP "\fBEVP_KDF_fetch\fR\|(3)" 4
  280. .IX Item "EVP_KDF_fetch"
  281. Fetch a Key Derivation Function (\s-1KDF\s0) algorithm implementation.
  282. .IP "\fBEVP_MAC_fetch\fR\|(3)" 4
  283. .IX Item "EVP_MAC_fetch"
  284. Fetch a Message Authentication Code (\s-1MAC\s0) algorithm implementation.
  285. .IP "\fBEVP_KEM_fetch\fR\|(3)" 4
  286. .IX Item "EVP_KEM_fetch"
  287. Fetch a Key Encapsulation Mechanism (\s-1KEM\s0) algorithm implementation
  288. .IP "\fBOSSL_ENCODER_fetch\fR\|(3)" 4
  289. .IX Item "OSSL_ENCODER_fetch"
  290. Fetch an encoder algorithm implementation (e.g. to encode keys to a specified
  291. format).
  292. .IP "\fBOSSL_DECODER_fetch\fR\|(3)" 4
  293. .IX Item "OSSL_DECODER_fetch"
  294. Fetch a decoder algorithm implementation (e.g. to decode keys from a specified
  295. format).
  296. .IP "\fBEVP_RAND_fetch\fR\|(3)" 4
  297. .IX Item "EVP_RAND_fetch"
  298. Fetch a Pseudo Random Number Generator (\s-1PRNG\s0) algorithm implementation.
  299. .PP
  300. See \*(L"\s-1OPERATIONS AND ALGORITHMS\*(R"\s0 in \fBOSSL_PROVIDER\-default\fR\|(7),
  301. \&\*(L"\s-1OPERATIONS AND ALGORITHMS\*(R"\s0 in \s-1\fBOSSL_PROVIDER\-FIPS\s0\fR\|(7),
  302. \&\*(L"\s-1OPERATIONS AND ALGORITHMS\*(R"\s0 in \fBOSSL_PROVIDER\-legacy\fR\|(7) and
  303. \&\*(L"\s-1OPERATIONS AND ALGORITHMS\*(R"\s0 in \fBOSSL_PROVIDER\-base\fR\|(7) for a list of algorithm names
  304. that can be fetched.
  305. .SH "FETCHING EXAMPLES"
  306. .IX Header "FETCHING EXAMPLES"
  307. The following section provides a series of examples of fetching algorithm
  308. implementations.
  309. .PP
  310. Fetch any available implementation of \s-1SHA2\-256\s0 in the default context. Note
  311. that some algorithms have aliases. So \*(L"\s-1SHA256\*(R"\s0 and \*(L"\s-1SHA2\-256\*(R"\s0 are synonymous:
  312. .PP
  313. .Vb 3
  314. \& EVP_MD *md = EVP_MD_fetch(NULL, "SHA2\-256", NULL);
  315. \& ...
  316. \& EVP_MD_free(md);
  317. .Ve
  318. .PP
  319. Fetch any available implementation of \s-1AES\-128\-CBC\s0 in the default context:
  320. .PP
  321. .Vb 3
  322. \& EVP_CIPHER *cipher = EVP_CIPHER_fetch(NULL, "AES\-128\-CBC", NULL);
  323. \& ...
  324. \& EVP_CIPHER_free(cipher);
  325. .Ve
  326. .PP
  327. Fetch an implementation of \s-1SHA2\-256\s0 from the default provider in the default
  328. context:
  329. .PP
  330. .Vb 3
  331. \& EVP_MD *md = EVP_MD_fetch(NULL, "SHA2\-256", "provider=default");
  332. \& ...
  333. \& EVP_MD_free(md);
  334. .Ve
  335. .PP
  336. Fetch an implementation of \s-1SHA2\-256\s0 that is not from the default provider in the
  337. default context:
  338. .PP
  339. .Vb 3
  340. \& EVP_MD *md = EVP_MD_fetch(NULL, "SHA2\-256", "provider!=default");
  341. \& ...
  342. \& EVP_MD_free(md);
  343. .Ve
  344. .PP
  345. Fetch an implementation of \s-1SHA2\-256\s0 that is preferably from the \s-1FIPS\s0 provider in
  346. the default context:
  347. .PP
  348. .Vb 3
  349. \& EVP_MD *md = EVP_MD_fetch(NULL, "SHA2\-256", "provider=?fips");
  350. \& ...
  351. \& EVP_MD_free(md);
  352. .Ve
  353. .PP
  354. Fetch an implementation of \s-1SHA2\-256\s0 from the default provider in the specified
  355. library context:
  356. .PP
  357. .Vb 3
  358. \& EVP_MD *md = EVP_MD_fetch(libctx, "SHA2\-256", "provider=default");
  359. \& ...
  360. \& EVP_MD_free(md);
  361. .Ve
  362. .PP
  363. Load the legacy provider into the default context and then fetch an
  364. implementation of \s-1WHIRLPOOL\s0 from it:
  365. .PP
  366. .Vb 2
  367. \& /* This only needs to be done once \- usually at application start up */
  368. \& OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy");
  369. \&
  370. \& EVP_MD *md = EVP_MD_fetch(NULL, "WHIRLPOOL", "provider=legacy");
  371. \& ...
  372. \& EVP_MD_free(md);
  373. .Ve
  374. .PP
  375. Note that in the above example the property string \*(L"provider=legacy\*(R" is optional
  376. since, assuming no other providers have been loaded, the only implementation of
  377. the \*(L"whirlpool\*(R" algorithm is in the \*(L"legacy\*(R" provider. Also note that the
  378. default provider should be explicitly loaded if it is required in addition to
  379. other providers:
  380. .PP
  381. .Vb 3
  382. \& /* This only needs to be done once \- usually at application start up */
  383. \& OSSL_PROVIDER *legacy = OSSL_PROVIDER_load(NULL, "legacy");
  384. \& OSSL_PROVIDER *default = OSSL_PROVIDER_load(NULL, "default");
  385. \&
  386. \& EVP_MD *md_whirlpool = EVP_MD_fetch(NULL, "whirlpool", NULL);
  387. \& EVP_MD *md_sha256 = EVP_MD_fetch(NULL, "SHA2\-256", NULL);
  388. \& ...
  389. \& EVP_MD_free(md_whirlpool);
  390. \& EVP_MD_free(md_sha256);
  391. .Ve
  392. .SH "USING ALGORITHMS IN APPLICATIONS"
  393. .IX Header "USING ALGORITHMS IN APPLICATIONS"
  394. Cryptographic algorithms are made available to applications through use of the
  395. \&\*(L"\s-1EVP\*(R"\s0 APIs. Each of the various operations such as encryption, digesting,
  396. message authentication codes, etc., have a set of \s-1EVP\s0 function calls that can
  397. be invoked to use them. See the \fBevp\fR\|(7) page for further details.
  398. .PP
  399. Most of these follow a common pattern. A \*(L"context\*(R" object is first created. For
  400. example for a digest operation you would use an \fB\s-1EVP_MD_CTX\s0\fR, and for an
  401. encryption/decryption operation you would use an \fB\s-1EVP_CIPHER_CTX\s0\fR. The
  402. operation is then initialised ready for use via an \*(L"init\*(R" function \- optionally
  403. passing in a set of parameters (using the \s-1\fBOSSL_PARAM\s0\fR\|(3) type) to configure how
  404. the operation should behave. Next data is fed into the operation in a series of
  405. \&\*(L"update\*(R" calls. The operation is finalised using a \*(L"final\*(R" call which will
  406. typically provide some kind of output. Finally the context is cleaned up and
  407. freed.
  408. .PP
  409. The following shows a complete example for doing this process for digesting
  410. data using \s-1SHA256.\s0 The process is similar for other operations such as
  411. encryption/decryption, signatures, message authentication codes, etc. Additional
  412. examples can be found in the OpenSSL demos (see
  413. \&\*(L"\s-1DEMO APPLICATIONS\*(R"\s0 in \fBossl\-guide\-libraries\-introduction\fR\|(7)).
  414. .PP
  415. .Vb 4
  416. \& #include <stdio.h>
  417. \& #include <openssl/evp.h>
  418. \& #include <openssl/bio.h>
  419. \& #include <openssl/err.h>
  420. \&
  421. \& int main(void)
  422. \& {
  423. \& EVP_MD_CTX *ctx = NULL;
  424. \& EVP_MD *sha256 = NULL;
  425. \& const unsigned char msg[] = {
  426. \& 0x00, 0x01, 0x02, 0x03
  427. \& };
  428. \& unsigned int len = 0;
  429. \& unsigned char *outdigest = NULL;
  430. \& int ret = 1;
  431. \&
  432. \& /* Create a context for the digest operation */
  433. \& ctx = EVP_MD_CTX_new();
  434. \& if (ctx == NULL)
  435. \& goto err;
  436. \&
  437. \& /*
  438. \& * Fetch the SHA256 algorithm implementation for doing the digest. We\*(Aqre
  439. \& * using the "default" library context here (first NULL parameter), and
  440. \& * we\*(Aqre not supplying any particular search criteria for our SHA256
  441. \& * implementation (second NULL parameter). Any SHA256 implementation will
  442. \& * do.
  443. \& * In a larger application this fetch would just be done once, and could
  444. \& * be used for multiple calls to other operations such as EVP_DigestInit_ex().
  445. \& */
  446. \& sha256 = EVP_MD_fetch(NULL, "SHA256", NULL);
  447. \& if (sha256 == NULL)
  448. \& goto err;
  449. \&
  450. \& /* Initialise the digest operation */
  451. \& if (!EVP_DigestInit_ex(ctx, sha256, NULL))
  452. \& goto err;
  453. \&
  454. \& /*
  455. \& * Pass the message to be digested. This can be passed in over multiple
  456. \& * EVP_DigestUpdate calls if necessary
  457. \& */
  458. \& if (!EVP_DigestUpdate(ctx, msg, sizeof(msg)))
  459. \& goto err;
  460. \&
  461. \& /* Allocate the output buffer */
  462. \& outdigest = OPENSSL_malloc(EVP_MD_get_size(sha256));
  463. \& if (outdigest == NULL)
  464. \& goto err;
  465. \&
  466. \& /* Now calculate the digest itself */
  467. \& if (!EVP_DigestFinal_ex(ctx, outdigest, &len))
  468. \& goto err;
  469. \&
  470. \& /* Print out the digest result */
  471. \& BIO_dump_fp(stdout, outdigest, len);
  472. \&
  473. \& ret = 0;
  474. \&
  475. \& err:
  476. \& /* Clean up all the resources we allocated */
  477. \& OPENSSL_free(outdigest);
  478. \& EVP_MD_free(sha256);
  479. \& EVP_MD_CTX_free(ctx);
  480. \& if (ret != 0)
  481. \& ERR_print_errors_fp(stderr);
  482. \& return ret;
  483. \& }
  484. .Ve
  485. .SH "ENCODING AND DECODING KEYS"
  486. .IX Header "ENCODING AND DECODING KEYS"
  487. Many algorithms require the use of a key. Keys can be generated dynamically
  488. using the \s-1EVP\s0 APIs (for example see \fBEVP_PKEY_Q_keygen\fR\|(3)). However it is often
  489. necessary to save or load keys (or their associated parameters) to or from some
  490. external format such as \s-1PEM\s0 or \s-1DER\s0 (see \fBopenssl\-glossary\fR\|(7)). OpenSSL uses
  491. encoders and decoders to perform this task.
  492. .PP
  493. Encoders and decoders are just algorithm implementations in the same way as
  494. any other algorithm implementation in OpenSSL. They are implemented by
  495. providers. The OpenSSL encoders and decoders are available in the default
  496. provider. They are also duplicated in the base provider.
  497. .PP
  498. For information about encoders see \fBOSSL_ENCODER_CTX_new_for_pkey\fR\|(3). For
  499. information about decoders see \fBOSSL_DECODER_CTX_new_for_pkey\fR\|(3).
  500. .PP
  501. As well as using encoders/decoders directly there are also some helper functions
  502. that can be used for certain well known and commonly used formats. For example
  503. see \fBPEM_read_PrivateKey\fR\|(3) and \fBPEM_write_PrivateKey\fR\|(3) for information
  504. about reading and writing key data from \s-1PEM\s0 encoded files.
  505. .SH "FURTHER READING"
  506. .IX Header "FURTHER READING"
  507. See \fBossl\-guide\-libssl\-introduction\fR\|(7) for an introduction to using \f(CW\*(C`libssl\*(C'\fR.
  508. .SH "SEE ALSO"
  509. .IX Header "SEE ALSO"
  510. \&\fBopenssl\fR\|(1), \fBssl\fR\|(7), \fBevp\fR\|(7), \s-1\fBOSSL_LIB_CTX\s0\fR\|(3), \fBopenssl\-threads\fR\|(7),
  511. \&\fBproperty\fR\|(7), \fBOSSL_PROVIDER\-default\fR\|(7), \fBOSSL_PROVIDER\-base\fR\|(7),
  512. \&\s-1\fBOSSL_PROVIDER\-FIPS\s0\fR\|(7), \fBOSSL_PROVIDER\-legacy\fR\|(7), \fBOSSL_PROVIDER\-null\fR\|(7),
  513. \&\fBopenssl\-glossary\fR\|(7), \fBprovider\fR\|(7)
  514. .SH "COPYRIGHT"
  515. .IX Header "COPYRIGHT"
  516. Copyright 2000\-2024 The OpenSSL Project Authors. All Rights Reserved.
  517. .PP
  518. Licensed under the Apache License 2.0 (the \*(L"License\*(R"). You may not use
  519. this file except in compliance with the License. You can obtain a copy
  520. in the file \s-1LICENSE\s0 in the source distribution or at
  521. <https://www.openssl.org/source/license.html>.