crypto-API.tmpl 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
  4. <book id="KernelCryptoAPI">
  5. <bookinfo>
  6. <title>Linux Kernel Crypto API</title>
  7. <authorgroup>
  8. <author>
  9. <firstname>Stephan</firstname>
  10. <surname>Mueller</surname>
  11. <affiliation>
  12. <address>
  13. <email>smueller@chronox.de</email>
  14. </address>
  15. </affiliation>
  16. </author>
  17. <author>
  18. <firstname>Marek</firstname>
  19. <surname>Vasut</surname>
  20. <affiliation>
  21. <address>
  22. <email>marek@denx.de</email>
  23. </address>
  24. </affiliation>
  25. </author>
  26. </authorgroup>
  27. <copyright>
  28. <year>2014</year>
  29. <holder>Stephan Mueller</holder>
  30. </copyright>
  31. <legalnotice>
  32. <para>
  33. This documentation is free software; you can redistribute
  34. it and/or modify it under the terms of the GNU General Public
  35. License as published by the Free Software Foundation; either
  36. version 2 of the License, or (at your option) any later
  37. version.
  38. </para>
  39. <para>
  40. This program is distributed in the hope that it will be
  41. useful, but WITHOUT ANY WARRANTY; without even the implied
  42. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  43. See the GNU General Public License for more details.
  44. </para>
  45. <para>
  46. You should have received a copy of the GNU General Public
  47. License along with this program; if not, write to the Free
  48. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  49. MA 02111-1307 USA
  50. </para>
  51. <para>
  52. For more details see the file COPYING in the source
  53. distribution of Linux.
  54. </para>
  55. </legalnotice>
  56. </bookinfo>
  57. <toc></toc>
  58. <chapter id="Intro">
  59. <title>Kernel Crypto API Interface Specification</title>
  60. <sect1><title>Introduction</title>
  61. <para>
  62. The kernel crypto API offers a rich set of cryptographic ciphers as
  63. well as other data transformation mechanisms and methods to invoke
  64. these. This document contains a description of the API and provides
  65. example code.
  66. </para>
  67. <para>
  68. To understand and properly use the kernel crypto API a brief
  69. explanation of its structure is given. Based on the architecture,
  70. the API can be separated into different components. Following the
  71. architecture specification, hints to developers of ciphers are
  72. provided. Pointers to the API function call documentation are
  73. given at the end.
  74. </para>
  75. <para>
  76. The kernel crypto API refers to all algorithms as "transformations".
  77. Therefore, a cipher handle variable usually has the name "tfm".
  78. Besides cryptographic operations, the kernel crypto API also knows
  79. compression transformations and handles them the same way as ciphers.
  80. </para>
  81. <para>
  82. The kernel crypto API serves the following entity types:
  83. <itemizedlist>
  84. <listitem>
  85. <para>consumers requesting cryptographic services</para>
  86. </listitem>
  87. <listitem>
  88. <para>data transformation implementations (typically ciphers)
  89. that can be called by consumers using the kernel crypto
  90. API</para>
  91. </listitem>
  92. </itemizedlist>
  93. </para>
  94. <para>
  95. This specification is intended for consumers of the kernel crypto
  96. API as well as for developers implementing ciphers. This API
  97. specification, however, does not discuss all API calls available
  98. to data transformation implementations (i.e. implementations of
  99. ciphers and other transformations (such as CRC or even compression
  100. algorithms) that can register with the kernel crypto API).
  101. </para>
  102. <para>
  103. Note: The terms "transformation" and cipher algorithm are used
  104. interchangeably.
  105. </para>
  106. </sect1>
  107. <sect1><title>Terminology</title>
  108. <para>
  109. The transformation implementation is an actual code or interface
  110. to hardware which implements a certain transformation with precisely
  111. defined behavior.
  112. </para>
  113. <para>
  114. The transformation object (TFM) is an instance of a transformation
  115. implementation. There can be multiple transformation objects
  116. associated with a single transformation implementation. Each of
  117. those transformation objects is held by a crypto API consumer or
  118. another transformation. Transformation object is allocated when a
  119. crypto API consumer requests a transformation implementation.
  120. The consumer is then provided with a structure, which contains
  121. a transformation object (TFM).
  122. </para>
  123. <para>
  124. The structure that contains transformation objects may also be
  125. referred to as a "cipher handle". Such a cipher handle is always
  126. subject to the following phases that are reflected in the API calls
  127. applicable to such a cipher handle:
  128. </para>
  129. <orderedlist>
  130. <listitem>
  131. <para>Initialization of a cipher handle.</para>
  132. </listitem>
  133. <listitem>
  134. <para>Execution of all intended cipher operations applicable
  135. for the handle where the cipher handle must be furnished to
  136. every API call.</para>
  137. </listitem>
  138. <listitem>
  139. <para>Destruction of a cipher handle.</para>
  140. </listitem>
  141. </orderedlist>
  142. <para>
  143. When using the initialization API calls, a cipher handle is
  144. created and returned to the consumer. Therefore, please refer
  145. to all initialization API calls that refer to the data
  146. structure type a consumer is expected to receive and subsequently
  147. to use. The initialization API calls have all the same naming
  148. conventions of crypto_alloc_*.
  149. </para>
  150. <para>
  151. The transformation context is private data associated with
  152. the transformation object.
  153. </para>
  154. </sect1>
  155. </chapter>
  156. <chapter id="Architecture"><title>Kernel Crypto API Architecture</title>
  157. <sect1><title>Cipher algorithm types</title>
  158. <para>
  159. The kernel crypto API provides different API calls for the
  160. following cipher types:
  161. <itemizedlist>
  162. <listitem><para>Symmetric ciphers</para></listitem>
  163. <listitem><para>AEAD ciphers</para></listitem>
  164. <listitem><para>Message digest, including keyed message digest</para></listitem>
  165. <listitem><para>Random number generation</para></listitem>
  166. <listitem><para>User space interface</para></listitem>
  167. </itemizedlist>
  168. </para>
  169. </sect1>
  170. <sect1><title>Ciphers And Templates</title>
  171. <para>
  172. The kernel crypto API provides implementations of single block
  173. ciphers and message digests. In addition, the kernel crypto API
  174. provides numerous "templates" that can be used in conjunction
  175. with the single block ciphers and message digests. Templates
  176. include all types of block chaining mode, the HMAC mechanism, etc.
  177. </para>
  178. <para>
  179. Single block ciphers and message digests can either be directly
  180. used by a caller or invoked together with a template to form
  181. multi-block ciphers or keyed message digests.
  182. </para>
  183. <para>
  184. A single block cipher may even be called with multiple templates.
  185. However, templates cannot be used without a single cipher.
  186. </para>
  187. <para>
  188. See /proc/crypto and search for "name". For example:
  189. <itemizedlist>
  190. <listitem><para>aes</para></listitem>
  191. <listitem><para>ecb(aes)</para></listitem>
  192. <listitem><para>cmac(aes)</para></listitem>
  193. <listitem><para>ccm(aes)</para></listitem>
  194. <listitem><para>rfc4106(gcm(aes))</para></listitem>
  195. <listitem><para>sha1</para></listitem>
  196. <listitem><para>hmac(sha1)</para></listitem>
  197. <listitem><para>authenc(hmac(sha1),cbc(aes))</para></listitem>
  198. </itemizedlist>
  199. </para>
  200. <para>
  201. In these examples, "aes" and "sha1" are the ciphers and all
  202. others are the templates.
  203. </para>
  204. </sect1>
  205. <sect1><title>Synchronous And Asynchronous Operation</title>
  206. <para>
  207. The kernel crypto API provides synchronous and asynchronous
  208. API operations.
  209. </para>
  210. <para>
  211. When using the synchronous API operation, the caller invokes
  212. a cipher operation which is performed synchronously by the
  213. kernel crypto API. That means, the caller waits until the
  214. cipher operation completes. Therefore, the kernel crypto API
  215. calls work like regular function calls. For synchronous
  216. operation, the set of API calls is small and conceptually
  217. similar to any other crypto library.
  218. </para>
  219. <para>
  220. Asynchronous operation is provided by the kernel crypto API
  221. which implies that the invocation of a cipher operation will
  222. complete almost instantly. That invocation triggers the
  223. cipher operation but it does not signal its completion. Before
  224. invoking a cipher operation, the caller must provide a callback
  225. function the kernel crypto API can invoke to signal the
  226. completion of the cipher operation. Furthermore, the caller
  227. must ensure it can handle such asynchronous events by applying
  228. appropriate locking around its data. The kernel crypto API
  229. does not perform any special serialization operation to protect
  230. the caller's data integrity.
  231. </para>
  232. </sect1>
  233. <sect1><title>Crypto API Cipher References And Priority</title>
  234. <para>
  235. A cipher is referenced by the caller with a string. That string
  236. has the following semantics:
  237. <programlisting>
  238. template(single block cipher)
  239. </programlisting>
  240. where "template" and "single block cipher" is the aforementioned
  241. template and single block cipher, respectively. If applicable,
  242. additional templates may enclose other templates, such as
  243. <programlisting>
  244. template1(template2(single block cipher)))
  245. </programlisting>
  246. </para>
  247. <para>
  248. The kernel crypto API may provide multiple implementations of a
  249. template or a single block cipher. For example, AES on newer
  250. Intel hardware has the following implementations: AES-NI,
  251. assembler implementation, or straight C. Now, when using the
  252. string "aes" with the kernel crypto API, which cipher
  253. implementation is used? The answer to that question is the
  254. priority number assigned to each cipher implementation by the
  255. kernel crypto API. When a caller uses the string to refer to a
  256. cipher during initialization of a cipher handle, the kernel
  257. crypto API looks up all implementations providing an
  258. implementation with that name and selects the implementation
  259. with the highest priority.
  260. </para>
  261. <para>
  262. Now, a caller may have the need to refer to a specific cipher
  263. implementation and thus does not want to rely on the
  264. priority-based selection. To accommodate this scenario, the
  265. kernel crypto API allows the cipher implementation to register
  266. a unique name in addition to common names. When using that
  267. unique name, a caller is therefore always sure to refer to
  268. the intended cipher implementation.
  269. </para>
  270. <para>
  271. The list of available ciphers is given in /proc/crypto. However,
  272. that list does not specify all possible permutations of
  273. templates and ciphers. Each block listed in /proc/crypto may
  274. contain the following information -- if one of the components
  275. listed as follows are not applicable to a cipher, it is not
  276. displayed:
  277. </para>
  278. <itemizedlist>
  279. <listitem>
  280. <para>name: the generic name of the cipher that is subject
  281. to the priority-based selection -- this name can be used by
  282. the cipher allocation API calls (all names listed above are
  283. examples for such generic names)</para>
  284. </listitem>
  285. <listitem>
  286. <para>driver: the unique name of the cipher -- this name can
  287. be used by the cipher allocation API calls</para>
  288. </listitem>
  289. <listitem>
  290. <para>module: the kernel module providing the cipher
  291. implementation (or "kernel" for statically linked ciphers)</para>
  292. </listitem>
  293. <listitem>
  294. <para>priority: the priority value of the cipher implementation</para>
  295. </listitem>
  296. <listitem>
  297. <para>refcnt: the reference count of the respective cipher
  298. (i.e. the number of current consumers of this cipher)</para>
  299. </listitem>
  300. <listitem>
  301. <para>selftest: specification whether the self test for the
  302. cipher passed</para>
  303. </listitem>
  304. <listitem>
  305. <para>type:
  306. <itemizedlist>
  307. <listitem>
  308. <para>blkcipher for synchronous block ciphers</para>
  309. </listitem>
  310. <listitem>
  311. <para>ablkcipher for asynchronous block ciphers</para>
  312. </listitem>
  313. <listitem>
  314. <para>cipher for single block ciphers that may be used with
  315. an additional template</para>
  316. </listitem>
  317. <listitem>
  318. <para>shash for synchronous message digest</para>
  319. </listitem>
  320. <listitem>
  321. <para>ahash for asynchronous message digest</para>
  322. </listitem>
  323. <listitem>
  324. <para>aead for AEAD cipher type</para>
  325. </listitem>
  326. <listitem>
  327. <para>compression for compression type transformations</para>
  328. </listitem>
  329. <listitem>
  330. <para>rng for random number generator</para>
  331. </listitem>
  332. <listitem>
  333. <para>givcipher for cipher with associated IV generator
  334. (see the geniv entry below for the specification of the
  335. IV generator type used by the cipher implementation)</para>
  336. </listitem>
  337. </itemizedlist>
  338. </para>
  339. </listitem>
  340. <listitem>
  341. <para>blocksize: blocksize of cipher in bytes</para>
  342. </listitem>
  343. <listitem>
  344. <para>keysize: key size in bytes</para>
  345. </listitem>
  346. <listitem>
  347. <para>ivsize: IV size in bytes</para>
  348. </listitem>
  349. <listitem>
  350. <para>seedsize: required size of seed data for random number
  351. generator</para>
  352. </listitem>
  353. <listitem>
  354. <para>digestsize: output size of the message digest</para>
  355. </listitem>
  356. <listitem>
  357. <para>geniv: IV generation type:
  358. <itemizedlist>
  359. <listitem>
  360. <para>eseqiv for encrypted sequence number based IV
  361. generation</para>
  362. </listitem>
  363. <listitem>
  364. <para>seqiv for sequence number based IV generation</para>
  365. </listitem>
  366. <listitem>
  367. <para>chainiv for chain iv generation</para>
  368. </listitem>
  369. <listitem>
  370. <para>&lt;builtin&gt; is a marker that the cipher implements
  371. IV generation and handling as it is specific to the given
  372. cipher</para>
  373. </listitem>
  374. </itemizedlist>
  375. </para>
  376. </listitem>
  377. </itemizedlist>
  378. </sect1>
  379. <sect1><title>Key Sizes</title>
  380. <para>
  381. When allocating a cipher handle, the caller only specifies the
  382. cipher type. Symmetric ciphers, however, typically support
  383. multiple key sizes (e.g. AES-128 vs. AES-192 vs. AES-256).
  384. These key sizes are determined with the length of the provided
  385. key. Thus, the kernel crypto API does not provide a separate
  386. way to select the particular symmetric cipher key size.
  387. </para>
  388. </sect1>
  389. <sect1><title>Cipher Allocation Type And Masks</title>
  390. <para>
  391. The different cipher handle allocation functions allow the
  392. specification of a type and mask flag. Both parameters have
  393. the following meaning (and are therefore not covered in the
  394. subsequent sections).
  395. </para>
  396. <para>
  397. The type flag specifies the type of the cipher algorithm.
  398. The caller usually provides a 0 when the caller wants the
  399. default handling. Otherwise, the caller may provide the
  400. following selections which match the the aforementioned
  401. cipher types:
  402. </para>
  403. <itemizedlist>
  404. <listitem>
  405. <para>CRYPTO_ALG_TYPE_CIPHER Single block cipher</para>
  406. </listitem>
  407. <listitem>
  408. <para>CRYPTO_ALG_TYPE_COMPRESS Compression</para>
  409. </listitem>
  410. <listitem>
  411. <para>CRYPTO_ALG_TYPE_AEAD Authenticated Encryption with
  412. Associated Data (MAC)</para>
  413. </listitem>
  414. <listitem>
  415. <para>CRYPTO_ALG_TYPE_BLKCIPHER Synchronous multi-block cipher</para>
  416. </listitem>
  417. <listitem>
  418. <para>CRYPTO_ALG_TYPE_ABLKCIPHER Asynchronous multi-block cipher</para>
  419. </listitem>
  420. <listitem>
  421. <para>CRYPTO_ALG_TYPE_GIVCIPHER Asynchronous multi-block
  422. cipher packed together with an IV generator (see geniv field
  423. in the /proc/crypto listing for the known IV generators)</para>
  424. </listitem>
  425. <listitem>
  426. <para>CRYPTO_ALG_TYPE_DIGEST Raw message digest</para>
  427. </listitem>
  428. <listitem>
  429. <para>CRYPTO_ALG_TYPE_HASH Alias for CRYPTO_ALG_TYPE_DIGEST</para>
  430. </listitem>
  431. <listitem>
  432. <para>CRYPTO_ALG_TYPE_SHASH Synchronous multi-block hash</para>
  433. </listitem>
  434. <listitem>
  435. <para>CRYPTO_ALG_TYPE_AHASH Asynchronous multi-block hash</para>
  436. </listitem>
  437. <listitem>
  438. <para>CRYPTO_ALG_TYPE_RNG Random Number Generation</para>
  439. </listitem>
  440. <listitem>
  441. <para>CRYPTO_ALG_TYPE_PCOMPRESS Enhanced version of
  442. CRYPTO_ALG_TYPE_COMPRESS allowing for segmented compression /
  443. decompression instead of performing the operation on one
  444. segment only. CRYPTO_ALG_TYPE_PCOMPRESS is intended to replace
  445. CRYPTO_ALG_TYPE_COMPRESS once existing consumers are converted.</para>
  446. </listitem>
  447. </itemizedlist>
  448. <para>
  449. The mask flag restricts the type of cipher. The only allowed
  450. flag is CRYPTO_ALG_ASYNC to restrict the cipher lookup function
  451. to asynchronous ciphers. Usually, a caller provides a 0 for the
  452. mask flag.
  453. </para>
  454. <para>
  455. When the caller provides a mask and type specification, the
  456. caller limits the search the kernel crypto API can perform for
  457. a suitable cipher implementation for the given cipher name.
  458. That means, even when a caller uses a cipher name that exists
  459. during its initialization call, the kernel crypto API may not
  460. select it due to the used type and mask field.
  461. </para>
  462. </sect1>
  463. <sect1><title>Internal Structure of Kernel Crypto API</title>
  464. <para>
  465. The kernel crypto API has an internal structure where a cipher
  466. implementation may use many layers and indirections. This section
  467. shall help to clarify how the kernel crypto API uses
  468. various components to implement the complete cipher.
  469. </para>
  470. <para>
  471. The following subsections explain the internal structure based
  472. on existing cipher implementations. The first section addresses
  473. the most complex scenario where all other scenarios form a logical
  474. subset.
  475. </para>
  476. <sect2><title>Generic AEAD Cipher Structure</title>
  477. <para>
  478. The following ASCII art decomposes the kernel crypto API layers
  479. when using the AEAD cipher with the automated IV generation. The
  480. shown example is used by the IPSEC layer.
  481. </para>
  482. <para>
  483. For other use cases of AEAD ciphers, the ASCII art applies as
  484. well, but the caller may not use the AEAD cipher with a separate
  485. IV generator. In this case, the caller must generate the IV.
  486. </para>
  487. <para>
  488. The depicted example decomposes the AEAD cipher of GCM(AES) based
  489. on the generic C implementations (gcm.c, aes-generic.c, ctr.c,
  490. ghash-generic.c, seqiv.c). The generic implementation serves as an
  491. example showing the complete logic of the kernel crypto API.
  492. </para>
  493. <para>
  494. It is possible that some streamlined cipher implementations (like
  495. AES-NI) provide implementations merging aspects which in the view
  496. of the kernel crypto API cannot be decomposed into layers any more.
  497. In case of the AES-NI implementation, the CTR mode, the GHASH
  498. implementation and the AES cipher are all merged into one cipher
  499. implementation registered with the kernel crypto API. In this case,
  500. the concept described by the following ASCII art applies too. However,
  501. the decomposition of GCM into the individual sub-components
  502. by the kernel crypto API is not done any more.
  503. </para>
  504. <para>
  505. Each block in the following ASCII art is an independent cipher
  506. instance obtained from the kernel crypto API. Each block
  507. is accessed by the caller or by other blocks using the API functions
  508. defined by the kernel crypto API for the cipher implementation type.
  509. </para>
  510. <para>
  511. The blocks below indicate the cipher type as well as the specific
  512. logic implemented in the cipher.
  513. </para>
  514. <para>
  515. The ASCII art picture also indicates the call structure, i.e. who
  516. calls which component. The arrows point to the invoked block
  517. where the caller uses the API applicable to the cipher type
  518. specified for the block.
  519. </para>
  520. <programlisting>
  521. <![CDATA[
  522. kernel crypto API | IPSEC Layer
  523. |
  524. +-----------+ |
  525. | | (1)
  526. | aead | <----------------------------------- esp_output
  527. | (seqiv) | ---+
  528. +-----------+ |
  529. | (2)
  530. +-----------+ |
  531. | | <--+ (2)
  532. | aead | <----------------------------------- esp_input
  533. | (gcm) | ------------+
  534. +-----------+ |
  535. | (3) | (5)
  536. v v
  537. +-----------+ +-----------+
  538. | | | |
  539. | ablkcipher| | ahash |
  540. | (ctr) | ---+ | (ghash) |
  541. +-----------+ | +-----------+
  542. |
  543. +-----------+ | (4)
  544. | | <--+
  545. | cipher |
  546. | (aes) |
  547. +-----------+
  548. ]]>
  549. </programlisting>
  550. <para>
  551. The following call sequence is applicable when the IPSEC layer
  552. triggers an encryption operation with the esp_output function. During
  553. configuration, the administrator set up the use of rfc4106(gcm(aes)) as
  554. the cipher for ESP. The following call sequence is now depicted in the
  555. ASCII art above:
  556. </para>
  557. <orderedlist>
  558. <listitem>
  559. <para>
  560. esp_output() invokes crypto_aead_encrypt() to trigger an encryption
  561. operation of the AEAD cipher with IV generator.
  562. </para>
  563. <para>
  564. In case of GCM, the SEQIV implementation is registered as GIVCIPHER
  565. in crypto_rfc4106_alloc().
  566. </para>
  567. <para>
  568. The SEQIV performs its operation to generate an IV where the core
  569. function is seqiv_geniv().
  570. </para>
  571. </listitem>
  572. <listitem>
  573. <para>
  574. Now, SEQIV uses the AEAD API function calls to invoke the associated
  575. AEAD cipher. In our case, during the instantiation of SEQIV, the
  576. cipher handle for GCM is provided to SEQIV. This means that SEQIV
  577. invokes AEAD cipher operations with the GCM cipher handle.
  578. </para>
  579. <para>
  580. During instantiation of the GCM handle, the CTR(AES) and GHASH
  581. ciphers are instantiated. The cipher handles for CTR(AES) and GHASH
  582. are retained for later use.
  583. </para>
  584. <para>
  585. The GCM implementation is responsible to invoke the CTR mode AES and
  586. the GHASH cipher in the right manner to implement the GCM
  587. specification.
  588. </para>
  589. </listitem>
  590. <listitem>
  591. <para>
  592. The GCM AEAD cipher type implementation now invokes the ABLKCIPHER API
  593. with the instantiated CTR(AES) cipher handle.
  594. </para>
  595. <para>
  596. During instantiation of the CTR(AES) cipher, the CIPHER type
  597. implementation of AES is instantiated. The cipher handle for AES is
  598. retained.
  599. </para>
  600. <para>
  601. That means that the ABLKCIPHER implementation of CTR(AES) only
  602. implements the CTR block chaining mode. After performing the block
  603. chaining operation, the CIPHER implementation of AES is invoked.
  604. </para>
  605. </listitem>
  606. <listitem>
  607. <para>
  608. The ABLKCIPHER of CTR(AES) now invokes the CIPHER API with the AES
  609. cipher handle to encrypt one block.
  610. </para>
  611. </listitem>
  612. <listitem>
  613. <para>
  614. The GCM AEAD implementation also invokes the GHASH cipher
  615. implementation via the AHASH API.
  616. </para>
  617. </listitem>
  618. </orderedlist>
  619. <para>
  620. When the IPSEC layer triggers the esp_input() function, the same call
  621. sequence is followed with the only difference that the operation starts
  622. with step (2).
  623. </para>
  624. </sect2>
  625. <sect2><title>Generic Block Cipher Structure</title>
  626. <para>
  627. Generic block ciphers follow the same concept as depicted with the ASCII
  628. art picture above.
  629. </para>
  630. <para>
  631. For example, CBC(AES) is implemented with cbc.c, and aes-generic.c. The
  632. ASCII art picture above applies as well with the difference that only
  633. step (4) is used and the ABLKCIPHER block chaining mode is CBC.
  634. </para>
  635. </sect2>
  636. <sect2><title>Generic Keyed Message Digest Structure</title>
  637. <para>
  638. Keyed message digest implementations again follow the same concept as
  639. depicted in the ASCII art picture above.
  640. </para>
  641. <para>
  642. For example, HMAC(SHA256) is implemented with hmac.c and
  643. sha256_generic.c. The following ASCII art illustrates the
  644. implementation:
  645. </para>
  646. <programlisting>
  647. <![CDATA[
  648. kernel crypto API | Caller
  649. |
  650. +-----------+ (1) |
  651. | | <------------------ some_function
  652. | ahash |
  653. | (hmac) | ---+
  654. +-----------+ |
  655. | (2)
  656. +-----------+ |
  657. | | <--+
  658. | shash |
  659. | (sha256) |
  660. +-----------+
  661. ]]>
  662. </programlisting>
  663. <para>
  664. The following call sequence is applicable when a caller triggers
  665. an HMAC operation:
  666. </para>
  667. <orderedlist>
  668. <listitem>
  669. <para>
  670. The AHASH API functions are invoked by the caller. The HMAC
  671. implementation performs its operation as needed.
  672. </para>
  673. <para>
  674. During initialization of the HMAC cipher, the SHASH cipher type of
  675. SHA256 is instantiated. The cipher handle for the SHA256 instance is
  676. retained.
  677. </para>
  678. <para>
  679. At one time, the HMAC implementation requires a SHA256 operation
  680. where the SHA256 cipher handle is used.
  681. </para>
  682. </listitem>
  683. <listitem>
  684. <para>
  685. The HMAC instance now invokes the SHASH API with the SHA256
  686. cipher handle to calculate the message digest.
  687. </para>
  688. </listitem>
  689. </orderedlist>
  690. </sect2>
  691. </sect1>
  692. </chapter>
  693. <chapter id="Development"><title>Developing Cipher Algorithms</title>
  694. <sect1><title>Registering And Unregistering Transformation</title>
  695. <para>
  696. There are three distinct types of registration functions in
  697. the Crypto API. One is used to register a generic cryptographic
  698. transformation, while the other two are specific to HASH
  699. transformations and COMPRESSion. We will discuss the latter
  700. two in a separate chapter, here we will only look at the
  701. generic ones.
  702. </para>
  703. <para>
  704. Before discussing the register functions, the data structure
  705. to be filled with each, struct crypto_alg, must be considered
  706. -- see below for a description of this data structure.
  707. </para>
  708. <para>
  709. The generic registration functions can be found in
  710. include/linux/crypto.h and their definition can be seen below.
  711. The former function registers a single transformation, while
  712. the latter works on an array of transformation descriptions.
  713. The latter is useful when registering transformations in bulk.
  714. </para>
  715. <programlisting>
  716. int crypto_register_alg(struct crypto_alg *alg);
  717. int crypto_register_algs(struct crypto_alg *algs, int count);
  718. </programlisting>
  719. <para>
  720. The counterparts to those functions are listed below.
  721. </para>
  722. <programlisting>
  723. int crypto_unregister_alg(struct crypto_alg *alg);
  724. int crypto_unregister_algs(struct crypto_alg *algs, int count);
  725. </programlisting>
  726. <para>
  727. Notice that both registration and unregistration functions
  728. do return a value, so make sure to handle errors. A return
  729. code of zero implies success. Any return code &lt; 0 implies
  730. an error.
  731. </para>
  732. <para>
  733. The bulk registration / unregistration functions require
  734. that struct crypto_alg is an array of count size. These
  735. functions simply loop over that array and register /
  736. unregister each individual algorithm. If an error occurs,
  737. the loop is terminated at the offending algorithm definition.
  738. That means, the algorithms prior to the offending algorithm
  739. are successfully registered. Note, the caller has no way of
  740. knowing which cipher implementations have successfully
  741. registered. If this is important to know, the caller should
  742. loop through the different implementations using the single
  743. instance *_alg functions for each individual implementation.
  744. </para>
  745. </sect1>
  746. <sect1><title>Single-Block Symmetric Ciphers [CIPHER]</title>
  747. <para>
  748. Example of transformations: aes, arc4, ...
  749. </para>
  750. <para>
  751. This section describes the simplest of all transformation
  752. implementations, that being the CIPHER type used for symmetric
  753. ciphers. The CIPHER type is used for transformations which
  754. operate on exactly one block at a time and there are no
  755. dependencies between blocks at all.
  756. </para>
  757. <sect2><title>Registration specifics</title>
  758. <para>
  759. The registration of [CIPHER] algorithm is specific in that
  760. struct crypto_alg field .cra_type is empty. The .cra_u.cipher
  761. has to be filled in with proper callbacks to implement this
  762. transformation.
  763. </para>
  764. <para>
  765. See struct cipher_alg below.
  766. </para>
  767. </sect2>
  768. <sect2><title>Cipher Definition With struct cipher_alg</title>
  769. <para>
  770. Struct cipher_alg defines a single block cipher.
  771. </para>
  772. <para>
  773. Here are schematics of how these functions are called when
  774. operated from other part of the kernel. Note that the
  775. .cia_setkey() call might happen before or after any of these
  776. schematics happen, but must not happen during any of these
  777. are in-flight.
  778. </para>
  779. <para>
  780. <programlisting>
  781. KEY ---. PLAINTEXT ---.
  782. v v
  783. .cia_setkey() -&gt; .cia_encrypt()
  784. |
  785. '-----&gt; CIPHERTEXT
  786. </programlisting>
  787. </para>
  788. <para>
  789. Please note that a pattern where .cia_setkey() is called
  790. multiple times is also valid:
  791. </para>
  792. <para>
  793. <programlisting>
  794. KEY1 --. PLAINTEXT1 --. KEY2 --. PLAINTEXT2 --.
  795. v v v v
  796. .cia_setkey() -&gt; .cia_encrypt() -&gt; .cia_setkey() -&gt; .cia_encrypt()
  797. | |
  798. '---&gt; CIPHERTEXT1 '---&gt; CIPHERTEXT2
  799. </programlisting>
  800. </para>
  801. </sect2>
  802. </sect1>
  803. <sect1><title>Multi-Block Ciphers [BLKCIPHER] [ABLKCIPHER]</title>
  804. <para>
  805. Example of transformations: cbc(aes), ecb(arc4), ...
  806. </para>
  807. <para>
  808. This section describes the multi-block cipher transformation
  809. implementations for both synchronous [BLKCIPHER] and
  810. asynchronous [ABLKCIPHER] case. The multi-block ciphers are
  811. used for transformations which operate on scatterlists of
  812. data supplied to the transformation functions. They output
  813. the result into a scatterlist of data as well.
  814. </para>
  815. <sect2><title>Registration Specifics</title>
  816. <para>
  817. The registration of [BLKCIPHER] or [ABLKCIPHER] algorithms
  818. is one of the most standard procedures throughout the crypto API.
  819. </para>
  820. <para>
  821. Note, if a cipher implementation requires a proper alignment
  822. of data, the caller should use the functions of
  823. crypto_blkcipher_alignmask() or crypto_ablkcipher_alignmask()
  824. respectively to identify a memory alignment mask. The kernel
  825. crypto API is able to process requests that are unaligned.
  826. This implies, however, additional overhead as the kernel
  827. crypto API needs to perform the realignment of the data which
  828. may imply moving of data.
  829. </para>
  830. </sect2>
  831. <sect2><title>Cipher Definition With struct blkcipher_alg and ablkcipher_alg</title>
  832. <para>
  833. Struct blkcipher_alg defines a synchronous block cipher whereas
  834. struct ablkcipher_alg defines an asynchronous block cipher.
  835. </para>
  836. <para>
  837. Please refer to the single block cipher description for schematics
  838. of the block cipher usage. The usage patterns are exactly the same
  839. for [ABLKCIPHER] and [BLKCIPHER] as they are for plain [CIPHER].
  840. </para>
  841. </sect2>
  842. <sect2><title>Specifics Of Asynchronous Multi-Block Cipher</title>
  843. <para>
  844. There are a couple of specifics to the [ABLKCIPHER] interface.
  845. </para>
  846. <para>
  847. First of all, some of the drivers will want to use the
  848. Generic ScatterWalk in case the hardware needs to be fed
  849. separate chunks of the scatterlist which contains the
  850. plaintext and will contain the ciphertext. Please refer
  851. to the ScatterWalk interface offered by the Linux kernel
  852. scatter / gather list implementation.
  853. </para>
  854. </sect2>
  855. </sect1>
  856. <sect1><title>Hashing [HASH]</title>
  857. <para>
  858. Example of transformations: crc32, md5, sha1, sha256,...
  859. </para>
  860. <sect2><title>Registering And Unregistering The Transformation</title>
  861. <para>
  862. There are multiple ways to register a HASH transformation,
  863. depending on whether the transformation is synchronous [SHASH]
  864. or asynchronous [AHASH] and the amount of HASH transformations
  865. we are registering. You can find the prototypes defined in
  866. include/crypto/internal/hash.h:
  867. </para>
  868. <programlisting>
  869. int crypto_register_ahash(struct ahash_alg *alg);
  870. int crypto_register_shash(struct shash_alg *alg);
  871. int crypto_register_shashes(struct shash_alg *algs, int count);
  872. </programlisting>
  873. <para>
  874. The respective counterparts for unregistering the HASH
  875. transformation are as follows:
  876. </para>
  877. <programlisting>
  878. int crypto_unregister_ahash(struct ahash_alg *alg);
  879. int crypto_unregister_shash(struct shash_alg *alg);
  880. int crypto_unregister_shashes(struct shash_alg *algs, int count);
  881. </programlisting>
  882. </sect2>
  883. <sect2><title>Cipher Definition With struct shash_alg and ahash_alg</title>
  884. <para>
  885. Here are schematics of how these functions are called when
  886. operated from other part of the kernel. Note that the .setkey()
  887. call might happen before or after any of these schematics happen,
  888. but must not happen during any of these are in-flight. Please note
  889. that calling .init() followed immediately by .finish() is also a
  890. perfectly valid transformation.
  891. </para>
  892. <programlisting>
  893. I) DATA -----------.
  894. v
  895. .init() -&gt; .update() -&gt; .final() ! .update() might not be called
  896. ^ | | at all in this scenario.
  897. '----' '---&gt; HASH
  898. II) DATA -----------.-----------.
  899. v v
  900. .init() -&gt; .update() -&gt; .finup() ! .update() may not be called
  901. ^ | | at all in this scenario.
  902. '----' '---&gt; HASH
  903. III) DATA -----------.
  904. v
  905. .digest() ! The entire process is handled
  906. | by the .digest() call.
  907. '---------------&gt; HASH
  908. </programlisting>
  909. <para>
  910. Here is a schematic of how the .export()/.import() functions are
  911. called when used from another part of the kernel.
  912. </para>
  913. <programlisting>
  914. KEY--. DATA--.
  915. v v ! .update() may not be called
  916. .setkey() -&gt; .init() -&gt; .update() -&gt; .export() at all in this scenario.
  917. ^ | |
  918. '-----' '--&gt; PARTIAL_HASH
  919. ----------- other transformations happen here -----------
  920. PARTIAL_HASH--. DATA1--.
  921. v v
  922. .import -&gt; .update() -&gt; .final() ! .update() may not be called
  923. ^ | | at all in this scenario.
  924. '----' '--&gt; HASH1
  925. PARTIAL_HASH--. DATA2-.
  926. v v
  927. .import -&gt; .finup()
  928. |
  929. '---------------&gt; HASH2
  930. </programlisting>
  931. </sect2>
  932. <sect2><title>Specifics Of Asynchronous HASH Transformation</title>
  933. <para>
  934. Some of the drivers will want to use the Generic ScatterWalk
  935. in case the implementation needs to be fed separate chunks of the
  936. scatterlist which contains the input data. The buffer containing
  937. the resulting hash will always be properly aligned to
  938. .cra_alignmask so there is no need to worry about this.
  939. </para>
  940. </sect2>
  941. </sect1>
  942. </chapter>
  943. <chapter id="User"><title>User Space Interface</title>
  944. <sect1><title>Introduction</title>
  945. <para>
  946. The concepts of the kernel crypto API visible to kernel space is fully
  947. applicable to the user space interface as well. Therefore, the kernel
  948. crypto API high level discussion for the in-kernel use cases applies
  949. here as well.
  950. </para>
  951. <para>
  952. The major difference, however, is that user space can only act as a
  953. consumer and never as a provider of a transformation or cipher algorithm.
  954. </para>
  955. <para>
  956. The following covers the user space interface exported by the kernel
  957. crypto API. A working example of this description is libkcapi that
  958. can be obtained from [1]. That library can be used by user space
  959. applications that require cryptographic services from the kernel.
  960. </para>
  961. <para>
  962. Some details of the in-kernel kernel crypto API aspects do not
  963. apply to user space, however. This includes the difference between
  964. synchronous and asynchronous invocations. The user space API call
  965. is fully synchronous.
  966. </para>
  967. <para>
  968. [1] <ulink url="http://www.chronox.de/libkcapi.html">http://www.chronox.de/libkcapi.html</ulink>
  969. </para>
  970. </sect1>
  971. <sect1><title>User Space API General Remarks</title>
  972. <para>
  973. The kernel crypto API is accessible from user space. Currently,
  974. the following ciphers are accessible:
  975. </para>
  976. <itemizedlist>
  977. <listitem>
  978. <para>Message digest including keyed message digest (HMAC, CMAC)</para>
  979. </listitem>
  980. <listitem>
  981. <para>Symmetric ciphers</para>
  982. </listitem>
  983. <listitem>
  984. <para>AEAD ciphers</para>
  985. </listitem>
  986. <listitem>
  987. <para>Random Number Generators</para>
  988. </listitem>
  989. </itemizedlist>
  990. <para>
  991. The interface is provided via socket type using the type AF_ALG.
  992. In addition, the setsockopt option type is SOL_ALG. In case the
  993. user space header files do not export these flags yet, use the
  994. following macros:
  995. </para>
  996. <programlisting>
  997. #ifndef AF_ALG
  998. #define AF_ALG 38
  999. #endif
  1000. #ifndef SOL_ALG
  1001. #define SOL_ALG 279
  1002. #endif
  1003. </programlisting>
  1004. <para>
  1005. A cipher is accessed with the same name as done for the in-kernel
  1006. API calls. This includes the generic vs. unique naming schema for
  1007. ciphers as well as the enforcement of priorities for generic names.
  1008. </para>
  1009. <para>
  1010. To interact with the kernel crypto API, a socket must be
  1011. created by the user space application. User space invokes the cipher
  1012. operation with the send()/write() system call family. The result of the
  1013. cipher operation is obtained with the read()/recv() system call family.
  1014. </para>
  1015. <para>
  1016. The following API calls assume that the socket descriptor
  1017. is already opened by the user space application and discusses only
  1018. the kernel crypto API specific invocations.
  1019. </para>
  1020. <para>
  1021. To initialize the socket interface, the following sequence has to
  1022. be performed by the consumer:
  1023. </para>
  1024. <orderedlist>
  1025. <listitem>
  1026. <para>
  1027. Create a socket of type AF_ALG with the struct sockaddr_alg
  1028. parameter specified below for the different cipher types.
  1029. </para>
  1030. </listitem>
  1031. <listitem>
  1032. <para>
  1033. Invoke bind with the socket descriptor
  1034. </para>
  1035. </listitem>
  1036. <listitem>
  1037. <para>
  1038. Invoke accept with the socket descriptor. The accept system call
  1039. returns a new file descriptor that is to be used to interact with
  1040. the particular cipher instance. When invoking send/write or recv/read
  1041. system calls to send data to the kernel or obtain data from the
  1042. kernel, the file descriptor returned by accept must be used.
  1043. </para>
  1044. </listitem>
  1045. </orderedlist>
  1046. </sect1>
  1047. <sect1><title>In-place Cipher operation</title>
  1048. <para>
  1049. Just like the in-kernel operation of the kernel crypto API, the user
  1050. space interface allows the cipher operation in-place. That means that
  1051. the input buffer used for the send/write system call and the output
  1052. buffer used by the read/recv system call may be one and the same.
  1053. This is of particular interest for symmetric cipher operations where a
  1054. copying of the output data to its final destination can be avoided.
  1055. </para>
  1056. <para>
  1057. If a consumer on the other hand wants to maintain the plaintext and
  1058. the ciphertext in different memory locations, all a consumer needs
  1059. to do is to provide different memory pointers for the encryption and
  1060. decryption operation.
  1061. </para>
  1062. </sect1>
  1063. <sect1><title>Message Digest API</title>
  1064. <para>
  1065. The message digest type to be used for the cipher operation is
  1066. selected when invoking the bind syscall. bind requires the caller
  1067. to provide a filled struct sockaddr data structure. This data
  1068. structure must be filled as follows:
  1069. </para>
  1070. <programlisting>
  1071. struct sockaddr_alg sa = {
  1072. .salg_family = AF_ALG,
  1073. .salg_type = "hash", /* this selects the hash logic in the kernel */
  1074. .salg_name = "sha1" /* this is the cipher name */
  1075. };
  1076. </programlisting>
  1077. <para>
  1078. The salg_type value "hash" applies to message digests and keyed
  1079. message digests. Though, a keyed message digest is referenced by
  1080. the appropriate salg_name. Please see below for the setsockopt
  1081. interface that explains how the key can be set for a keyed message
  1082. digest.
  1083. </para>
  1084. <para>
  1085. Using the send() system call, the application provides the data that
  1086. should be processed with the message digest. The send system call
  1087. allows the following flags to be specified:
  1088. </para>
  1089. <itemizedlist>
  1090. <listitem>
  1091. <para>
  1092. MSG_MORE: If this flag is set, the send system call acts like a
  1093. message digest update function where the final hash is not
  1094. yet calculated. If the flag is not set, the send system call
  1095. calculates the final message digest immediately.
  1096. </para>
  1097. </listitem>
  1098. </itemizedlist>
  1099. <para>
  1100. With the recv() system call, the application can read the message
  1101. digest from the kernel crypto API. If the buffer is too small for the
  1102. message digest, the flag MSG_TRUNC is set by the kernel.
  1103. </para>
  1104. <para>
  1105. In order to set a message digest key, the calling application must use
  1106. the setsockopt() option of ALG_SET_KEY. If the key is not set the HMAC
  1107. operation is performed without the initial HMAC state change caused by
  1108. the key.
  1109. </para>
  1110. </sect1>
  1111. <sect1><title>Symmetric Cipher API</title>
  1112. <para>
  1113. The operation is very similar to the message digest discussion.
  1114. During initialization, the struct sockaddr data structure must be
  1115. filled as follows:
  1116. </para>
  1117. <programlisting>
  1118. struct sockaddr_alg sa = {
  1119. .salg_family = AF_ALG,
  1120. .salg_type = "skcipher", /* this selects the symmetric cipher */
  1121. .salg_name = "cbc(aes)" /* this is the cipher name */
  1122. };
  1123. </programlisting>
  1124. <para>
  1125. Before data can be sent to the kernel using the write/send system
  1126. call family, the consumer must set the key. The key setting is
  1127. described with the setsockopt invocation below.
  1128. </para>
  1129. <para>
  1130. Using the sendmsg() system call, the application provides the data that should be processed for encryption or decryption. In addition, the IV is
  1131. specified with the data structure provided by the sendmsg() system call.
  1132. </para>
  1133. <para>
  1134. The sendmsg system call parameter of struct msghdr is embedded into the
  1135. struct cmsghdr data structure. See recv(2) and cmsg(3) for more
  1136. information on how the cmsghdr data structure is used together with the
  1137. send/recv system call family. That cmsghdr data structure holds the
  1138. following information specified with a separate header instances:
  1139. </para>
  1140. <itemizedlist>
  1141. <listitem>
  1142. <para>
  1143. specification of the cipher operation type with one of these flags:
  1144. </para>
  1145. <itemizedlist>
  1146. <listitem>
  1147. <para>ALG_OP_ENCRYPT - encryption of data</para>
  1148. </listitem>
  1149. <listitem>
  1150. <para>ALG_OP_DECRYPT - decryption of data</para>
  1151. </listitem>
  1152. </itemizedlist>
  1153. </listitem>
  1154. <listitem>
  1155. <para>
  1156. specification of the IV information marked with the flag ALG_SET_IV
  1157. </para>
  1158. </listitem>
  1159. </itemizedlist>
  1160. <para>
  1161. The send system call family allows the following flag to be specified:
  1162. </para>
  1163. <itemizedlist>
  1164. <listitem>
  1165. <para>
  1166. MSG_MORE: If this flag is set, the send system call acts like a
  1167. cipher update function where more input data is expected
  1168. with a subsequent invocation of the send system call.
  1169. </para>
  1170. </listitem>
  1171. </itemizedlist>
  1172. <para>
  1173. Note: The kernel reports -EINVAL for any unexpected data. The caller
  1174. must make sure that all data matches the constraints given in
  1175. /proc/crypto for the selected cipher.
  1176. </para>
  1177. <para>
  1178. With the recv() system call, the application can read the result of
  1179. the cipher operation from the kernel crypto API. The output buffer
  1180. must be at least as large as to hold all blocks of the encrypted or
  1181. decrypted data. If the output data size is smaller, only as many
  1182. blocks are returned that fit into that output buffer size.
  1183. </para>
  1184. </sect1>
  1185. <sect1><title>AEAD Cipher API</title>
  1186. <para>
  1187. The operation is very similar to the symmetric cipher discussion.
  1188. During initialization, the struct sockaddr data structure must be
  1189. filled as follows:
  1190. </para>
  1191. <programlisting>
  1192. struct sockaddr_alg sa = {
  1193. .salg_family = AF_ALG,
  1194. .salg_type = "aead", /* this selects the symmetric cipher */
  1195. .salg_name = "gcm(aes)" /* this is the cipher name */
  1196. };
  1197. </programlisting>
  1198. <para>
  1199. Before data can be sent to the kernel using the write/send system
  1200. call family, the consumer must set the key. The key setting is
  1201. described with the setsockopt invocation below.
  1202. </para>
  1203. <para>
  1204. In addition, before data can be sent to the kernel using the
  1205. write/send system call family, the consumer must set the authentication
  1206. tag size. To set the authentication tag size, the caller must use the
  1207. setsockopt invocation described below.
  1208. </para>
  1209. <para>
  1210. Using the sendmsg() system call, the application provides the data that should be processed for encryption or decryption. In addition, the IV is
  1211. specified with the data structure provided by the sendmsg() system call.
  1212. </para>
  1213. <para>
  1214. The sendmsg system call parameter of struct msghdr is embedded into the
  1215. struct cmsghdr data structure. See recv(2) and cmsg(3) for more
  1216. information on how the cmsghdr data structure is used together with the
  1217. send/recv system call family. That cmsghdr data structure holds the
  1218. following information specified with a separate header instances:
  1219. </para>
  1220. <itemizedlist>
  1221. <listitem>
  1222. <para>
  1223. specification of the cipher operation type with one of these flags:
  1224. </para>
  1225. <itemizedlist>
  1226. <listitem>
  1227. <para>ALG_OP_ENCRYPT - encryption of data</para>
  1228. </listitem>
  1229. <listitem>
  1230. <para>ALG_OP_DECRYPT - decryption of data</para>
  1231. </listitem>
  1232. </itemizedlist>
  1233. </listitem>
  1234. <listitem>
  1235. <para>
  1236. specification of the IV information marked with the flag ALG_SET_IV
  1237. </para>
  1238. </listitem>
  1239. <listitem>
  1240. <para>
  1241. specification of the associated authentication data (AAD) with the
  1242. flag ALG_SET_AEAD_ASSOCLEN. The AAD is sent to the kernel together
  1243. with the plaintext / ciphertext. See below for the memory structure.
  1244. </para>
  1245. </listitem>
  1246. </itemizedlist>
  1247. <para>
  1248. The send system call family allows the following flag to be specified:
  1249. </para>
  1250. <itemizedlist>
  1251. <listitem>
  1252. <para>
  1253. MSG_MORE: If this flag is set, the send system call acts like a
  1254. cipher update function where more input data is expected
  1255. with a subsequent invocation of the send system call.
  1256. </para>
  1257. </listitem>
  1258. </itemizedlist>
  1259. <para>
  1260. Note: The kernel reports -EINVAL for any unexpected data. The caller
  1261. must make sure that all data matches the constraints given in
  1262. /proc/crypto for the selected cipher.
  1263. </para>
  1264. <para>
  1265. With the recv() system call, the application can read the result of
  1266. the cipher operation from the kernel crypto API. The output buffer
  1267. must be at least as large as defined with the memory structure below.
  1268. If the output data size is smaller, the cipher operation is not performed.
  1269. </para>
  1270. <para>
  1271. The authenticated decryption operation may indicate an integrity error.
  1272. Such breach in integrity is marked with the -EBADMSG error code.
  1273. </para>
  1274. <sect2><title>AEAD Memory Structure</title>
  1275. <para>
  1276. The AEAD cipher operates with the following information that
  1277. is communicated between user and kernel space as one data stream:
  1278. </para>
  1279. <itemizedlist>
  1280. <listitem>
  1281. <para>plaintext or ciphertext</para>
  1282. </listitem>
  1283. <listitem>
  1284. <para>associated authentication data (AAD)</para>
  1285. </listitem>
  1286. <listitem>
  1287. <para>authentication tag</para>
  1288. </listitem>
  1289. </itemizedlist>
  1290. <para>
  1291. The sizes of the AAD and the authentication tag are provided with
  1292. the sendmsg and setsockopt calls (see there). As the kernel knows
  1293. the size of the entire data stream, the kernel is now able to
  1294. calculate the right offsets of the data components in the data
  1295. stream.
  1296. </para>
  1297. <para>
  1298. The user space caller must arrange the aforementioned information
  1299. in the following order:
  1300. </para>
  1301. <itemizedlist>
  1302. <listitem>
  1303. <para>
  1304. AEAD encryption input: AAD || plaintext
  1305. </para>
  1306. </listitem>
  1307. <listitem>
  1308. <para>
  1309. AEAD decryption input: AAD || ciphertext || authentication tag
  1310. </para>
  1311. </listitem>
  1312. </itemizedlist>
  1313. <para>
  1314. The output buffer the user space caller provides must be at least as
  1315. large to hold the following data:
  1316. </para>
  1317. <itemizedlist>
  1318. <listitem>
  1319. <para>
  1320. AEAD encryption output: ciphertext || authentication tag
  1321. </para>
  1322. </listitem>
  1323. <listitem>
  1324. <para>
  1325. AEAD decryption output: plaintext
  1326. </para>
  1327. </listitem>
  1328. </itemizedlist>
  1329. </sect2>
  1330. </sect1>
  1331. <sect1><title>Random Number Generator API</title>
  1332. <para>
  1333. Again, the operation is very similar to the other APIs.
  1334. During initialization, the struct sockaddr data structure must be
  1335. filled as follows:
  1336. </para>
  1337. <programlisting>
  1338. struct sockaddr_alg sa = {
  1339. .salg_family = AF_ALG,
  1340. .salg_type = "rng", /* this selects the symmetric cipher */
  1341. .salg_name = "drbg_nopr_sha256" /* this is the cipher name */
  1342. };
  1343. </programlisting>
  1344. <para>
  1345. Depending on the RNG type, the RNG must be seeded. The seed is provided
  1346. using the setsockopt interface to set the key. For example, the
  1347. ansi_cprng requires a seed. The DRBGs do not require a seed, but
  1348. may be seeded.
  1349. </para>
  1350. <para>
  1351. Using the read()/recvmsg() system calls, random numbers can be obtained.
  1352. The kernel generates at most 128 bytes in one call. If user space
  1353. requires more data, multiple calls to read()/recvmsg() must be made.
  1354. </para>
  1355. <para>
  1356. WARNING: The user space caller may invoke the initially mentioned
  1357. accept system call multiple times. In this case, the returned file
  1358. descriptors have the same state.
  1359. </para>
  1360. </sect1>
  1361. <sect1><title>Zero-Copy Interface</title>
  1362. <para>
  1363. In addition to the send/write/read/recv system call family, the AF_ALG
  1364. interface can be accessed with the zero-copy interface of splice/vmsplice.
  1365. As the name indicates, the kernel tries to avoid a copy operation into
  1366. kernel space.
  1367. </para>
  1368. <para>
  1369. The zero-copy operation requires data to be aligned at the page boundary.
  1370. Non-aligned data can be used as well, but may require more operations of
  1371. the kernel which would defeat the speed gains obtained from the zero-copy
  1372. interface.
  1373. </para>
  1374. <para>
  1375. The system-interent limit for the size of one zero-copy operation is
  1376. 16 pages. If more data is to be sent to AF_ALG, user space must slice
  1377. the input into segments with a maximum size of 16 pages.
  1378. </para>
  1379. <para>
  1380. Zero-copy can be used with the following code example (a complete working
  1381. example is provided with libkcapi):
  1382. </para>
  1383. <programlisting>
  1384. int pipes[2];
  1385. pipe(pipes);
  1386. /* input data in iov */
  1387. vmsplice(pipes[1], iov, iovlen, SPLICE_F_GIFT);
  1388. /* opfd is the file descriptor returned from accept() system call */
  1389. splice(pipes[0], NULL, opfd, NULL, ret, 0);
  1390. read(opfd, out, outlen);
  1391. </programlisting>
  1392. </sect1>
  1393. <sect1><title>Setsockopt Interface</title>
  1394. <para>
  1395. In addition to the read/recv and send/write system call handling
  1396. to send and retrieve data subject to the cipher operation, a consumer
  1397. also needs to set the additional information for the cipher operation.
  1398. This additional information is set using the setsockopt system call
  1399. that must be invoked with the file descriptor of the open cipher
  1400. (i.e. the file descriptor returned by the accept system call).
  1401. </para>
  1402. <para>
  1403. Each setsockopt invocation must use the level SOL_ALG.
  1404. </para>
  1405. <para>
  1406. The setsockopt interface allows setting the following data using
  1407. the mentioned optname:
  1408. </para>
  1409. <itemizedlist>
  1410. <listitem>
  1411. <para>
  1412. ALG_SET_KEY -- Setting the key. Key setting is applicable to:
  1413. </para>
  1414. <itemizedlist>
  1415. <listitem>
  1416. <para>the skcipher cipher type (symmetric ciphers)</para>
  1417. </listitem>
  1418. <listitem>
  1419. <para>the hash cipher type (keyed message digests)</para>
  1420. </listitem>
  1421. <listitem>
  1422. <para>the AEAD cipher type</para>
  1423. </listitem>
  1424. <listitem>
  1425. <para>the RNG cipher type to provide the seed</para>
  1426. </listitem>
  1427. </itemizedlist>
  1428. </listitem>
  1429. <listitem>
  1430. <para>
  1431. ALG_SET_AEAD_AUTHSIZE -- Setting the authentication tag size
  1432. for AEAD ciphers. For a encryption operation, the authentication
  1433. tag of the given size will be generated. For a decryption operation,
  1434. the provided ciphertext is assumed to contain an authentication tag
  1435. of the given size (see section about AEAD memory layout below).
  1436. </para>
  1437. </listitem>
  1438. </itemizedlist>
  1439. </sect1>
  1440. <sect1><title>User space API example</title>
  1441. <para>
  1442. Please see [1] for libkcapi which provides an easy-to-use wrapper
  1443. around the aforementioned Netlink kernel interface. [1] also contains
  1444. a test application that invokes all libkcapi API calls.
  1445. </para>
  1446. <para>
  1447. [1] <ulink url="http://www.chronox.de/libkcapi.html">http://www.chronox.de/libkcapi.html</ulink>
  1448. </para>
  1449. </sect1>
  1450. </chapter>
  1451. <chapter id="API"><title>Programming Interface</title>
  1452. <para>
  1453. Please note that the kernel crypto API contains the AEAD givcrypt
  1454. API (crypto_aead_giv* and aead_givcrypt_* function calls in
  1455. include/crypto/aead.h). This API is obsolete and will be removed
  1456. in the future. To obtain the functionality of an AEAD cipher with
  1457. internal IV generation, use the IV generator as a regular cipher.
  1458. For example, rfc4106(gcm(aes)) is the AEAD cipher with external
  1459. IV generation and seqniv(rfc4106(gcm(aes))) implies that the kernel
  1460. crypto API generates the IV. Different IV generators are available.
  1461. </para>
  1462. <sect1><title>Block Cipher Context Data Structures</title>
  1463. !Pinclude/linux/crypto.h Block Cipher Context Data Structures
  1464. !Finclude/crypto/aead.h aead_request
  1465. </sect1>
  1466. <sect1><title>Block Cipher Algorithm Definitions</title>
  1467. !Pinclude/linux/crypto.h Block Cipher Algorithm Definitions
  1468. !Finclude/linux/crypto.h crypto_alg
  1469. !Finclude/linux/crypto.h ablkcipher_alg
  1470. !Finclude/crypto/aead.h aead_alg
  1471. !Finclude/linux/crypto.h blkcipher_alg
  1472. !Finclude/linux/crypto.h cipher_alg
  1473. !Finclude/crypto/rng.h rng_alg
  1474. </sect1>
  1475. <sect1><title>Asynchronous Block Cipher API</title>
  1476. !Pinclude/linux/crypto.h Asynchronous Block Cipher API
  1477. !Finclude/linux/crypto.h crypto_alloc_ablkcipher
  1478. !Finclude/linux/crypto.h crypto_free_ablkcipher
  1479. !Finclude/linux/crypto.h crypto_has_ablkcipher
  1480. !Finclude/linux/crypto.h crypto_ablkcipher_ivsize
  1481. !Finclude/linux/crypto.h crypto_ablkcipher_blocksize
  1482. !Finclude/linux/crypto.h crypto_ablkcipher_setkey
  1483. !Finclude/linux/crypto.h crypto_ablkcipher_reqtfm
  1484. !Finclude/linux/crypto.h crypto_ablkcipher_encrypt
  1485. !Finclude/linux/crypto.h crypto_ablkcipher_decrypt
  1486. </sect1>
  1487. <sect1><title>Asynchronous Cipher Request Handle</title>
  1488. !Pinclude/linux/crypto.h Asynchronous Cipher Request Handle
  1489. !Finclude/linux/crypto.h crypto_ablkcipher_reqsize
  1490. !Finclude/linux/crypto.h ablkcipher_request_set_tfm
  1491. !Finclude/linux/crypto.h ablkcipher_request_alloc
  1492. !Finclude/linux/crypto.h ablkcipher_request_free
  1493. !Finclude/linux/crypto.h ablkcipher_request_set_callback
  1494. !Finclude/linux/crypto.h ablkcipher_request_set_crypt
  1495. </sect1>
  1496. <sect1><title>Authenticated Encryption With Associated Data (AEAD) Cipher API</title>
  1497. !Pinclude/crypto/aead.h Authenticated Encryption With Associated Data (AEAD) Cipher API
  1498. !Finclude/crypto/aead.h crypto_alloc_aead
  1499. !Finclude/crypto/aead.h crypto_free_aead
  1500. !Finclude/crypto/aead.h crypto_aead_ivsize
  1501. !Finclude/crypto/aead.h crypto_aead_authsize
  1502. !Finclude/crypto/aead.h crypto_aead_blocksize
  1503. !Finclude/crypto/aead.h crypto_aead_setkey
  1504. !Finclude/crypto/aead.h crypto_aead_setauthsize
  1505. !Finclude/crypto/aead.h crypto_aead_encrypt
  1506. !Finclude/crypto/aead.h crypto_aead_decrypt
  1507. </sect1>
  1508. <sect1><title>Asynchronous AEAD Request Handle</title>
  1509. !Pinclude/crypto/aead.h Asynchronous AEAD Request Handle
  1510. !Finclude/crypto/aead.h crypto_aead_reqsize
  1511. !Finclude/crypto/aead.h aead_request_set_tfm
  1512. !Finclude/crypto/aead.h aead_request_alloc
  1513. !Finclude/crypto/aead.h aead_request_free
  1514. !Finclude/crypto/aead.h aead_request_set_callback
  1515. !Finclude/crypto/aead.h aead_request_set_crypt
  1516. !Finclude/crypto/aead.h aead_request_set_assoc
  1517. !Finclude/crypto/aead.h aead_request_set_ad
  1518. </sect1>
  1519. <sect1><title>Synchronous Block Cipher API</title>
  1520. !Pinclude/linux/crypto.h Synchronous Block Cipher API
  1521. !Finclude/linux/crypto.h crypto_alloc_blkcipher
  1522. !Finclude/linux/crypto.h crypto_free_blkcipher
  1523. !Finclude/linux/crypto.h crypto_has_blkcipher
  1524. !Finclude/linux/crypto.h crypto_blkcipher_name
  1525. !Finclude/linux/crypto.h crypto_blkcipher_ivsize
  1526. !Finclude/linux/crypto.h crypto_blkcipher_blocksize
  1527. !Finclude/linux/crypto.h crypto_blkcipher_setkey
  1528. !Finclude/linux/crypto.h crypto_blkcipher_encrypt
  1529. !Finclude/linux/crypto.h crypto_blkcipher_encrypt_iv
  1530. !Finclude/linux/crypto.h crypto_blkcipher_decrypt
  1531. !Finclude/linux/crypto.h crypto_blkcipher_decrypt_iv
  1532. !Finclude/linux/crypto.h crypto_blkcipher_set_iv
  1533. !Finclude/linux/crypto.h crypto_blkcipher_get_iv
  1534. </sect1>
  1535. <sect1><title>Single Block Cipher API</title>
  1536. !Pinclude/linux/crypto.h Single Block Cipher API
  1537. !Finclude/linux/crypto.h crypto_alloc_cipher
  1538. !Finclude/linux/crypto.h crypto_free_cipher
  1539. !Finclude/linux/crypto.h crypto_has_cipher
  1540. !Finclude/linux/crypto.h crypto_cipher_blocksize
  1541. !Finclude/linux/crypto.h crypto_cipher_setkey
  1542. !Finclude/linux/crypto.h crypto_cipher_encrypt_one
  1543. !Finclude/linux/crypto.h crypto_cipher_decrypt_one
  1544. </sect1>
  1545. <sect1><title>Synchronous Message Digest API</title>
  1546. !Pinclude/linux/crypto.h Synchronous Message Digest API
  1547. !Finclude/linux/crypto.h crypto_alloc_hash
  1548. !Finclude/linux/crypto.h crypto_free_hash
  1549. !Finclude/linux/crypto.h crypto_has_hash
  1550. !Finclude/linux/crypto.h crypto_hash_blocksize
  1551. !Finclude/linux/crypto.h crypto_hash_digestsize
  1552. !Finclude/linux/crypto.h crypto_hash_init
  1553. !Finclude/linux/crypto.h crypto_hash_update
  1554. !Finclude/linux/crypto.h crypto_hash_final
  1555. !Finclude/linux/crypto.h crypto_hash_digest
  1556. !Finclude/linux/crypto.h crypto_hash_setkey
  1557. </sect1>
  1558. <sect1><title>Message Digest Algorithm Definitions</title>
  1559. !Pinclude/crypto/hash.h Message Digest Algorithm Definitions
  1560. !Finclude/crypto/hash.h hash_alg_common
  1561. !Finclude/crypto/hash.h ahash_alg
  1562. !Finclude/crypto/hash.h shash_alg
  1563. </sect1>
  1564. <sect1><title>Asynchronous Message Digest API</title>
  1565. !Pinclude/crypto/hash.h Asynchronous Message Digest API
  1566. !Finclude/crypto/hash.h crypto_alloc_ahash
  1567. !Finclude/crypto/hash.h crypto_free_ahash
  1568. !Finclude/crypto/hash.h crypto_ahash_init
  1569. !Finclude/crypto/hash.h crypto_ahash_digestsize
  1570. !Finclude/crypto/hash.h crypto_ahash_reqtfm
  1571. !Finclude/crypto/hash.h crypto_ahash_reqsize
  1572. !Finclude/crypto/hash.h crypto_ahash_setkey
  1573. !Finclude/crypto/hash.h crypto_ahash_finup
  1574. !Finclude/crypto/hash.h crypto_ahash_final
  1575. !Finclude/crypto/hash.h crypto_ahash_digest
  1576. !Finclude/crypto/hash.h crypto_ahash_export
  1577. !Finclude/crypto/hash.h crypto_ahash_import
  1578. </sect1>
  1579. <sect1><title>Asynchronous Hash Request Handle</title>
  1580. !Pinclude/crypto/hash.h Asynchronous Hash Request Handle
  1581. !Finclude/crypto/hash.h ahash_request_set_tfm
  1582. !Finclude/crypto/hash.h ahash_request_alloc
  1583. !Finclude/crypto/hash.h ahash_request_free
  1584. !Finclude/crypto/hash.h ahash_request_set_callback
  1585. !Finclude/crypto/hash.h ahash_request_set_crypt
  1586. </sect1>
  1587. <sect1><title>Synchronous Message Digest API</title>
  1588. !Pinclude/crypto/hash.h Synchronous Message Digest API
  1589. !Finclude/crypto/hash.h crypto_alloc_shash
  1590. !Finclude/crypto/hash.h crypto_free_shash
  1591. !Finclude/crypto/hash.h crypto_shash_blocksize
  1592. !Finclude/crypto/hash.h crypto_shash_digestsize
  1593. !Finclude/crypto/hash.h crypto_shash_descsize
  1594. !Finclude/crypto/hash.h crypto_shash_setkey
  1595. !Finclude/crypto/hash.h crypto_shash_digest
  1596. !Finclude/crypto/hash.h crypto_shash_export
  1597. !Finclude/crypto/hash.h crypto_shash_import
  1598. !Finclude/crypto/hash.h crypto_shash_init
  1599. !Finclude/crypto/hash.h crypto_shash_update
  1600. !Finclude/crypto/hash.h crypto_shash_final
  1601. !Finclude/crypto/hash.h crypto_shash_finup
  1602. </sect1>
  1603. <sect1><title>Crypto API Random Number API</title>
  1604. !Pinclude/crypto/rng.h Random number generator API
  1605. !Finclude/crypto/rng.h crypto_alloc_rng
  1606. !Finclude/crypto/rng.h crypto_rng_alg
  1607. !Finclude/crypto/rng.h crypto_free_rng
  1608. !Finclude/crypto/rng.h crypto_rng_get_bytes
  1609. !Finclude/crypto/rng.h crypto_rng_reset
  1610. !Finclude/crypto/rng.h crypto_rng_seedsize
  1611. !Cinclude/crypto/rng.h
  1612. </sect1>
  1613. </chapter>
  1614. <chapter id="Code"><title>Code Examples</title>
  1615. <sect1><title>Code Example For Asynchronous Block Cipher Operation</title>
  1616. <programlisting>
  1617. struct tcrypt_result {
  1618. struct completion completion;
  1619. int err;
  1620. };
  1621. /* tie all data structures together */
  1622. struct ablkcipher_def {
  1623. struct scatterlist sg;
  1624. struct crypto_ablkcipher *tfm;
  1625. struct ablkcipher_request *req;
  1626. struct tcrypt_result result;
  1627. };
  1628. /* Callback function */
  1629. static void test_ablkcipher_cb(struct crypto_async_request *req, int error)
  1630. {
  1631. struct tcrypt_result *result = req-&gt;data;
  1632. if (error == -EINPROGRESS)
  1633. return;
  1634. result-&gt;err = error;
  1635. complete(&amp;result-&gt;completion);
  1636. pr_info("Encryption finished successfully\n");
  1637. }
  1638. /* Perform cipher operation */
  1639. static unsigned int test_ablkcipher_encdec(struct ablkcipher_def *ablk,
  1640. int enc)
  1641. {
  1642. int rc = 0;
  1643. if (enc)
  1644. rc = crypto_ablkcipher_encrypt(ablk-&gt;req);
  1645. else
  1646. rc = crypto_ablkcipher_decrypt(ablk-&gt;req);
  1647. switch (rc) {
  1648. case 0:
  1649. break;
  1650. case -EINPROGRESS:
  1651. case -EBUSY:
  1652. rc = wait_for_completion_interruptible(
  1653. &amp;ablk-&gt;result.completion);
  1654. if (!rc &amp;&amp; !ablk-&gt;result.err) {
  1655. reinit_completion(&amp;ablk-&gt;result.completion);
  1656. break;
  1657. }
  1658. default:
  1659. pr_info("ablkcipher encrypt returned with %d result %d\n",
  1660. rc, ablk-&gt;result.err);
  1661. break;
  1662. }
  1663. init_completion(&amp;ablk-&gt;result.completion);
  1664. return rc;
  1665. }
  1666. /* Initialize and trigger cipher operation */
  1667. static int test_ablkcipher(void)
  1668. {
  1669. struct ablkcipher_def ablk;
  1670. struct crypto_ablkcipher *ablkcipher = NULL;
  1671. struct ablkcipher_request *req = NULL;
  1672. char *scratchpad = NULL;
  1673. char *ivdata = NULL;
  1674. unsigned char key[32];
  1675. int ret = -EFAULT;
  1676. ablkcipher = crypto_alloc_ablkcipher("cbc-aes-aesni", 0, 0);
  1677. if (IS_ERR(ablkcipher)) {
  1678. pr_info("could not allocate ablkcipher handle\n");
  1679. return PTR_ERR(ablkcipher);
  1680. }
  1681. req = ablkcipher_request_alloc(ablkcipher, GFP_KERNEL);
  1682. if (IS_ERR(req)) {
  1683. pr_info("could not allocate request queue\n");
  1684. ret = PTR_ERR(req);
  1685. goto out;
  1686. }
  1687. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  1688. test_ablkcipher_cb,
  1689. &amp;ablk.result);
  1690. /* AES 256 with random key */
  1691. get_random_bytes(&amp;key, 32);
  1692. if (crypto_ablkcipher_setkey(ablkcipher, key, 32)) {
  1693. pr_info("key could not be set\n");
  1694. ret = -EAGAIN;
  1695. goto out;
  1696. }
  1697. /* IV will be random */
  1698. ivdata = kmalloc(16, GFP_KERNEL);
  1699. if (!ivdata) {
  1700. pr_info("could not allocate ivdata\n");
  1701. goto out;
  1702. }
  1703. get_random_bytes(ivdata, 16);
  1704. /* Input data will be random */
  1705. scratchpad = kmalloc(16, GFP_KERNEL);
  1706. if (!scratchpad) {
  1707. pr_info("could not allocate scratchpad\n");
  1708. goto out;
  1709. }
  1710. get_random_bytes(scratchpad, 16);
  1711. ablk.tfm = ablkcipher;
  1712. ablk.req = req;
  1713. /* We encrypt one block */
  1714. sg_init_one(&amp;ablk.sg, scratchpad, 16);
  1715. ablkcipher_request_set_crypt(req, &amp;ablk.sg, &amp;ablk.sg, 16, ivdata);
  1716. init_completion(&amp;ablk.result.completion);
  1717. /* encrypt data */
  1718. ret = test_ablkcipher_encdec(&amp;ablk, 1);
  1719. if (ret)
  1720. goto out;
  1721. pr_info("Encryption triggered successfully\n");
  1722. out:
  1723. if (ablkcipher)
  1724. crypto_free_ablkcipher(ablkcipher);
  1725. if (req)
  1726. ablkcipher_request_free(req);
  1727. if (ivdata)
  1728. kfree(ivdata);
  1729. if (scratchpad)
  1730. kfree(scratchpad);
  1731. return ret;
  1732. }
  1733. </programlisting>
  1734. </sect1>
  1735. <sect1><title>Code Example For Synchronous Block Cipher Operation</title>
  1736. <programlisting>
  1737. static int test_blkcipher(void)
  1738. {
  1739. struct crypto_blkcipher *blkcipher = NULL;
  1740. char *cipher = "cbc(aes)";
  1741. // AES 128
  1742. charkey =
  1743. "\x12\x34\x56\x78\x90\xab\xcd\xef\x12\x34\x56\x78\x90\xab\xcd\xef";
  1744. chariv =
  1745. "\x12\x34\x56\x78\x90\xab\xcd\xef\x12\x34\x56\x78\x90\xab\xcd\xef";
  1746. unsigned int ivsize = 0;
  1747. char *scratchpad = NULL; // holds plaintext and ciphertext
  1748. struct scatterlist sg;
  1749. struct blkcipher_desc desc;
  1750. int ret = -EFAULT;
  1751. blkcipher = crypto_alloc_blkcipher(cipher, 0, 0);
  1752. if (IS_ERR(blkcipher)) {
  1753. printk("could not allocate blkcipher handle for %s\n", cipher);
  1754. return -PTR_ERR(blkcipher);
  1755. }
  1756. if (crypto_blkcipher_setkey(blkcipher, key, strlen(key))) {
  1757. printk("key could not be set\n");
  1758. ret = -EAGAIN;
  1759. goto out;
  1760. }
  1761. ivsize = crypto_blkcipher_ivsize(blkcipher);
  1762. if (ivsize) {
  1763. if (ivsize != strlen(iv))
  1764. printk("IV length differs from expected length\n");
  1765. crypto_blkcipher_set_iv(blkcipher, iv, ivsize);
  1766. }
  1767. scratchpad = kmalloc(crypto_blkcipher_blocksize(blkcipher), GFP_KERNEL);
  1768. if (!scratchpad) {
  1769. printk("could not allocate scratchpad for %s\n", cipher);
  1770. goto out;
  1771. }
  1772. /* get some random data that we want to encrypt */
  1773. get_random_bytes(scratchpad, crypto_blkcipher_blocksize(blkcipher));
  1774. desc.flags = 0;
  1775. desc.tfm = blkcipher;
  1776. sg_init_one(&amp;sg, scratchpad, crypto_blkcipher_blocksize(blkcipher));
  1777. /* encrypt data in place */
  1778. crypto_blkcipher_encrypt(&amp;desc, &amp;sg, &amp;sg,
  1779. crypto_blkcipher_blocksize(blkcipher));
  1780. /* decrypt data in place
  1781. * crypto_blkcipher_decrypt(&amp;desc, &amp;sg, &amp;sg,
  1782. */ crypto_blkcipher_blocksize(blkcipher));
  1783. printk("Cipher operation completed\n");
  1784. return 0;
  1785. out:
  1786. if (blkcipher)
  1787. crypto_free_blkcipher(blkcipher);
  1788. if (scratchpad)
  1789. kzfree(scratchpad);
  1790. return ret;
  1791. }
  1792. </programlisting>
  1793. </sect1>
  1794. <sect1><title>Code Example For Use of Operational State Memory With SHASH</title>
  1795. <programlisting>
  1796. struct sdesc {
  1797. struct shash_desc shash;
  1798. char ctx[];
  1799. };
  1800. static struct sdescinit_sdesc(struct crypto_shash *alg)
  1801. {
  1802. struct sdescsdesc;
  1803. int size;
  1804. size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
  1805. sdesc = kmalloc(size, GFP_KERNEL);
  1806. if (!sdesc)
  1807. return ERR_PTR(-ENOMEM);
  1808. sdesc-&gt;shash.tfm = alg;
  1809. sdesc-&gt;shash.flags = 0x0;
  1810. return sdesc;
  1811. }
  1812. static int calc_hash(struct crypto_shashalg,
  1813. const unsigned chardata, unsigned int datalen,
  1814. unsigned chardigest) {
  1815. struct sdescsdesc;
  1816. int ret;
  1817. sdesc = init_sdesc(alg);
  1818. if (IS_ERR(sdesc)) {
  1819. pr_info("trusted_key: can't alloc %s\n", hash_alg);
  1820. return PTR_ERR(sdesc);
  1821. }
  1822. ret = crypto_shash_digest(&amp;sdesc-&gt;shash, data, datalen, digest);
  1823. kfree(sdesc);
  1824. return ret;
  1825. }
  1826. </programlisting>
  1827. </sect1>
  1828. <sect1><title>Code Example For Random Number Generator Usage</title>
  1829. <programlisting>
  1830. static int get_random_numbers(u8 *buf, unsigned int len)
  1831. {
  1832. struct crypto_rngrng = NULL;
  1833. chardrbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */
  1834. int ret;
  1835. if (!buf || !len) {
  1836. pr_debug("No output buffer provided\n");
  1837. return -EINVAL;
  1838. }
  1839. rng = crypto_alloc_rng(drbg, 0, 0);
  1840. if (IS_ERR(rng)) {
  1841. pr_debug("could not allocate RNG handle for %s\n", drbg);
  1842. return -PTR_ERR(rng);
  1843. }
  1844. ret = crypto_rng_get_bytes(rng, buf, len);
  1845. if (ret &lt; 0)
  1846. pr_debug("generation of random numbers failed\n");
  1847. else if (ret == 0)
  1848. pr_debug("RNG returned no data");
  1849. else
  1850. pr_debug("RNG returned %d bytes of data\n", ret);
  1851. out:
  1852. crypto_free_rng(rng);
  1853. return ret;
  1854. }
  1855. </programlisting>
  1856. </sect1>
  1857. </chapter>
  1858. </book>