crypto.h 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360
  1. /*
  2. * Scatterlist Cryptographic API.
  3. *
  4. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  5. * Copyright (c) 2002 David S. Miller (davem@redhat.com)
  6. * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
  7. *
  8. * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
  9. * and Nettle, by Niels Möller.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. */
  17. #ifndef _LINUX_CRYPTO_H
  18. #define _LINUX_CRYPTO_H
  19. #include <linux/atomic.h>
  20. #include <linux/kernel.h>
  21. #include <linux/list.h>
  22. #include <linux/bug.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <linux/uaccess.h>
  26. /*
  27. * Autoloaded crypto modules should only use a prefixed name to avoid allowing
  28. * arbitrary modules to be loaded. Loading from userspace may still need the
  29. * unprefixed names, so retains those aliases as well.
  30. * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
  31. * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
  32. * expands twice on the same line. Instead, use a separate base name for the
  33. * alias.
  34. */
  35. #define MODULE_ALIAS_CRYPTO(name) \
  36. __MODULE_INFO(alias, alias_userspace, name); \
  37. __MODULE_INFO(alias, alias_crypto, "crypto-" name)
  38. /*
  39. * Algorithm masks and types.
  40. */
  41. #define CRYPTO_ALG_TYPE_MASK 0x0000000f
  42. #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
  43. #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
  44. #define CRYPTO_ALG_TYPE_AEAD 0x00000003
  45. #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
  46. #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
  47. #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
  48. #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
  49. #define CRYPTO_ALG_TYPE_HASH 0x00000008
  50. #define CRYPTO_ALG_TYPE_SHASH 0x00000009
  51. #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
  52. #define CRYPTO_ALG_TYPE_RNG 0x0000000c
  53. #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
  54. #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
  55. #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
  56. #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
  57. #define CRYPTO_ALG_LARVAL 0x00000010
  58. #define CRYPTO_ALG_DEAD 0x00000020
  59. #define CRYPTO_ALG_DYING 0x00000040
  60. #define CRYPTO_ALG_ASYNC 0x00000080
  61. /*
  62. * Set this bit if and only if the algorithm requires another algorithm of
  63. * the same type to handle corner cases.
  64. */
  65. #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
  66. /*
  67. * This bit is set for symmetric key ciphers that have already been wrapped
  68. * with a generic IV generator to prevent them from being wrapped again.
  69. */
  70. #define CRYPTO_ALG_GENIV 0x00000200
  71. /*
  72. * Set if the algorithm has passed automated run-time testing. Note that
  73. * if there is no run-time testing for a given algorithm it is considered
  74. * to have passed.
  75. */
  76. #define CRYPTO_ALG_TESTED 0x00000400
  77. /*
  78. * Set if the algorithm is an instance that is build from templates.
  79. */
  80. #define CRYPTO_ALG_INSTANCE 0x00000800
  81. /* Set this bit if the algorithm provided is hardware accelerated but
  82. * not available to userspace via instruction set or so.
  83. */
  84. #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
  85. /*
  86. * Mark a cipher as a service implementation only usable by another
  87. * cipher and never by a normal user of the kernel crypto API
  88. */
  89. #define CRYPTO_ALG_INTERNAL 0x00002000
  90. /*
  91. * Transform masks and values (for crt_flags).
  92. */
  93. #define CRYPTO_TFM_REQ_MASK 0x000fff00
  94. #define CRYPTO_TFM_RES_MASK 0xfff00000
  95. #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
  96. #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
  97. #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
  98. #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
  99. #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
  100. #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
  101. #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
  102. #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
  103. /*
  104. * Miscellaneous stuff.
  105. */
  106. #define CRYPTO_MAX_ALG_NAME 64
  107. /*
  108. * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
  109. * declaration) is used to ensure that the crypto_tfm context structure is
  110. * aligned correctly for the given architecture so that there are no alignment
  111. * faults for C data types. In particular, this is required on platforms such
  112. * as arm where pointers are 32-bit aligned but there are data types such as
  113. * u64 which require 64-bit alignment.
  114. */
  115. #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
  116. #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
  117. struct scatterlist;
  118. struct crypto_ablkcipher;
  119. struct crypto_async_request;
  120. struct crypto_aead;
  121. struct crypto_blkcipher;
  122. struct crypto_hash;
  123. struct crypto_tfm;
  124. struct crypto_type;
  125. struct aead_givcrypt_request;
  126. struct skcipher_givcrypt_request;
  127. typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
  128. /**
  129. * DOC: Block Cipher Context Data Structures
  130. *
  131. * These data structures define the operating context for each block cipher
  132. * type.
  133. */
  134. struct crypto_async_request {
  135. struct list_head list;
  136. crypto_completion_t complete;
  137. void *data;
  138. struct crypto_tfm *tfm;
  139. u32 flags;
  140. };
  141. struct ablkcipher_request {
  142. struct crypto_async_request base;
  143. unsigned int nbytes;
  144. void *info;
  145. struct scatterlist *src;
  146. struct scatterlist *dst;
  147. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  148. };
  149. /**
  150. * struct aead_request - AEAD request
  151. * @base: Common attributes for async crypto requests
  152. * @assoclen: Length in bytes of associated data for authentication
  153. * @cryptlen: Length of data to be encrypted or decrypted
  154. * @iv: Initialisation vector
  155. * @assoc: Associated data
  156. * @src: Source data
  157. * @dst: Destination data
  158. * @__ctx: Start of private context data
  159. */
  160. struct aead_request {
  161. struct crypto_async_request base;
  162. unsigned int assoclen;
  163. unsigned int cryptlen;
  164. u8 *iv;
  165. struct scatterlist *assoc;
  166. struct scatterlist *src;
  167. struct scatterlist *dst;
  168. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  169. };
  170. struct blkcipher_desc {
  171. struct crypto_blkcipher *tfm;
  172. void *info;
  173. u32 flags;
  174. };
  175. struct cipher_desc {
  176. struct crypto_tfm *tfm;
  177. void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  178. unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
  179. const u8 *src, unsigned int nbytes);
  180. void *info;
  181. };
  182. struct hash_desc {
  183. struct crypto_hash *tfm;
  184. u32 flags;
  185. };
  186. /**
  187. * DOC: Block Cipher Algorithm Definitions
  188. *
  189. * These data structures define modular crypto algorithm implementations,
  190. * managed via crypto_register_alg() and crypto_unregister_alg().
  191. */
  192. /**
  193. * struct ablkcipher_alg - asynchronous block cipher definition
  194. * @min_keysize: Minimum key size supported by the transformation. This is the
  195. * smallest key length supported by this transformation algorithm.
  196. * This must be set to one of the pre-defined values as this is
  197. * not hardware specific. Possible values for this field can be
  198. * found via git grep "_MIN_KEY_SIZE" include/crypto/
  199. * @max_keysize: Maximum key size supported by the transformation. This is the
  200. * largest key length supported by this transformation algorithm.
  201. * This must be set to one of the pre-defined values as this is
  202. * not hardware specific. Possible values for this field can be
  203. * found via git grep "_MAX_KEY_SIZE" include/crypto/
  204. * @setkey: Set key for the transformation. This function is used to either
  205. * program a supplied key into the hardware or store the key in the
  206. * transformation context for programming it later. Note that this
  207. * function does modify the transformation context. This function can
  208. * be called multiple times during the existence of the transformation
  209. * object, so one must make sure the key is properly reprogrammed into
  210. * the hardware. This function is also responsible for checking the key
  211. * length for validity. In case a software fallback was put in place in
  212. * the @cra_init call, this function might need to use the fallback if
  213. * the algorithm doesn't support all of the key sizes.
  214. * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
  215. * the supplied scatterlist containing the blocks of data. The crypto
  216. * API consumer is responsible for aligning the entries of the
  217. * scatterlist properly and making sure the chunks are correctly
  218. * sized. In case a software fallback was put in place in the
  219. * @cra_init call, this function might need to use the fallback if
  220. * the algorithm doesn't support all of the key sizes. In case the
  221. * key was stored in transformation context, the key might need to be
  222. * re-programmed into the hardware in this function. This function
  223. * shall not modify the transformation context, as this function may
  224. * be called in parallel with the same transformation object.
  225. * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
  226. * and the conditions are exactly the same.
  227. * @givencrypt: Update the IV for encryption. With this function, a cipher
  228. * implementation may provide the function on how to update the IV
  229. * for encryption.
  230. * @givdecrypt: Update the IV for decryption. This is the reverse of
  231. * @givencrypt .
  232. * @geniv: The transformation implementation may use an "IV generator" provided
  233. * by the kernel crypto API. Several use cases have a predefined
  234. * approach how IVs are to be updated. For such use cases, the kernel
  235. * crypto API provides ready-to-use implementations that can be
  236. * referenced with this variable.
  237. * @ivsize: IV size applicable for transformation. The consumer must provide an
  238. * IV of exactly that size to perform the encrypt or decrypt operation.
  239. *
  240. * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
  241. * mandatory and must be filled.
  242. */
  243. struct ablkcipher_alg {
  244. int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
  245. unsigned int keylen);
  246. int (*encrypt)(struct ablkcipher_request *req);
  247. int (*decrypt)(struct ablkcipher_request *req);
  248. int (*givencrypt)(struct skcipher_givcrypt_request *req);
  249. int (*givdecrypt)(struct skcipher_givcrypt_request *req);
  250. const char *geniv;
  251. unsigned int min_keysize;
  252. unsigned int max_keysize;
  253. unsigned int ivsize;
  254. };
  255. /**
  256. * struct aead_alg - AEAD cipher definition
  257. * @maxauthsize: Set the maximum authentication tag size supported by the
  258. * transformation. A transformation may support smaller tag sizes.
  259. * As the authentication tag is a message digest to ensure the
  260. * integrity of the encrypted data, a consumer typically wants the
  261. * largest authentication tag possible as defined by this
  262. * variable.
  263. * @setauthsize: Set authentication size for the AEAD transformation. This
  264. * function is used to specify the consumer requested size of the
  265. * authentication tag to be either generated by the transformation
  266. * during encryption or the size of the authentication tag to be
  267. * supplied during the decryption operation. This function is also
  268. * responsible for checking the authentication tag size for
  269. * validity.
  270. * @setkey: see struct ablkcipher_alg
  271. * @encrypt: see struct ablkcipher_alg
  272. * @decrypt: see struct ablkcipher_alg
  273. * @givencrypt: see struct ablkcipher_alg
  274. * @givdecrypt: see struct ablkcipher_alg
  275. * @geniv: see struct ablkcipher_alg
  276. * @ivsize: see struct ablkcipher_alg
  277. *
  278. * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
  279. * mandatory and must be filled.
  280. */
  281. struct aead_alg {
  282. int (*setkey)(struct crypto_aead *tfm, const u8 *key,
  283. unsigned int keylen);
  284. int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
  285. int (*encrypt)(struct aead_request *req);
  286. int (*decrypt)(struct aead_request *req);
  287. int (*givencrypt)(struct aead_givcrypt_request *req);
  288. int (*givdecrypt)(struct aead_givcrypt_request *req);
  289. const char *geniv;
  290. unsigned int ivsize;
  291. unsigned int maxauthsize;
  292. };
  293. /**
  294. * struct blkcipher_alg - synchronous block cipher definition
  295. * @min_keysize: see struct ablkcipher_alg
  296. * @max_keysize: see struct ablkcipher_alg
  297. * @setkey: see struct ablkcipher_alg
  298. * @encrypt: see struct ablkcipher_alg
  299. * @decrypt: see struct ablkcipher_alg
  300. * @geniv: see struct ablkcipher_alg
  301. * @ivsize: see struct ablkcipher_alg
  302. *
  303. * All fields except @geniv and @ivsize are mandatory and must be filled.
  304. */
  305. struct blkcipher_alg {
  306. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  307. unsigned int keylen);
  308. int (*encrypt)(struct blkcipher_desc *desc,
  309. struct scatterlist *dst, struct scatterlist *src,
  310. unsigned int nbytes);
  311. int (*decrypt)(struct blkcipher_desc *desc,
  312. struct scatterlist *dst, struct scatterlist *src,
  313. unsigned int nbytes);
  314. const char *geniv;
  315. unsigned int min_keysize;
  316. unsigned int max_keysize;
  317. unsigned int ivsize;
  318. };
  319. /**
  320. * struct cipher_alg - single-block symmetric ciphers definition
  321. * @cia_min_keysize: Minimum key size supported by the transformation. This is
  322. * the smallest key length supported by this transformation
  323. * algorithm. This must be set to one of the pre-defined
  324. * values as this is not hardware specific. Possible values
  325. * for this field can be found via git grep "_MIN_KEY_SIZE"
  326. * include/crypto/
  327. * @cia_max_keysize: Maximum key size supported by the transformation. This is
  328. * the largest key length supported by this transformation
  329. * algorithm. This must be set to one of the pre-defined values
  330. * as this is not hardware specific. Possible values for this
  331. * field can be found via git grep "_MAX_KEY_SIZE"
  332. * include/crypto/
  333. * @cia_setkey: Set key for the transformation. This function is used to either
  334. * program a supplied key into the hardware or store the key in the
  335. * transformation context for programming it later. Note that this
  336. * function does modify the transformation context. This function
  337. * can be called multiple times during the existence of the
  338. * transformation object, so one must make sure the key is properly
  339. * reprogrammed into the hardware. This function is also
  340. * responsible for checking the key length for validity.
  341. * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
  342. * single block of data, which must be @cra_blocksize big. This
  343. * always operates on a full @cra_blocksize and it is not possible
  344. * to encrypt a block of smaller size. The supplied buffers must
  345. * therefore also be at least of @cra_blocksize size. Both the
  346. * input and output buffers are always aligned to @cra_alignmask.
  347. * In case either of the input or output buffer supplied by user
  348. * of the crypto API is not aligned to @cra_alignmask, the crypto
  349. * API will re-align the buffers. The re-alignment means that a
  350. * new buffer will be allocated, the data will be copied into the
  351. * new buffer, then the processing will happen on the new buffer,
  352. * then the data will be copied back into the original buffer and
  353. * finally the new buffer will be freed. In case a software
  354. * fallback was put in place in the @cra_init call, this function
  355. * might need to use the fallback if the algorithm doesn't support
  356. * all of the key sizes. In case the key was stored in
  357. * transformation context, the key might need to be re-programmed
  358. * into the hardware in this function. This function shall not
  359. * modify the transformation context, as this function may be
  360. * called in parallel with the same transformation object.
  361. * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
  362. * @cia_encrypt, and the conditions are exactly the same.
  363. *
  364. * All fields are mandatory and must be filled.
  365. */
  366. struct cipher_alg {
  367. unsigned int cia_min_keysize;
  368. unsigned int cia_max_keysize;
  369. int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  370. unsigned int keylen);
  371. void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  372. void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  373. };
  374. struct compress_alg {
  375. int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
  376. unsigned int slen, u8 *dst, unsigned int *dlen);
  377. int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
  378. unsigned int slen, u8 *dst, unsigned int *dlen);
  379. };
  380. #define cra_ablkcipher cra_u.ablkcipher
  381. #define cra_aead cra_u.aead
  382. #define cra_blkcipher cra_u.blkcipher
  383. #define cra_cipher cra_u.cipher
  384. #define cra_compress cra_u.compress
  385. /**
  386. * struct crypto_alg - definition of a cryptograpic cipher algorithm
  387. * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
  388. * CRYPTO_ALG_* flags for the flags which go in here. Those are
  389. * used for fine-tuning the description of the transformation
  390. * algorithm.
  391. * @cra_blocksize: Minimum block size of this transformation. The size in bytes
  392. * of the smallest possible unit which can be transformed with
  393. * this algorithm. The users must respect this value.
  394. * In case of HASH transformation, it is possible for a smaller
  395. * block than @cra_blocksize to be passed to the crypto API for
  396. * transformation, in case of any other transformation type, an
  397. * error will be returned upon any attempt to transform smaller
  398. * than @cra_blocksize chunks.
  399. * @cra_ctxsize: Size of the operational context of the transformation. This
  400. * value informs the kernel crypto API about the memory size
  401. * needed to be allocated for the transformation context.
  402. * @cra_alignmask: Alignment mask for the input and output data buffer. The data
  403. * buffer containing the input data for the algorithm must be
  404. * aligned to this alignment mask. The data buffer for the
  405. * output data must be aligned to this alignment mask. Note that
  406. * the Crypto API will do the re-alignment in software, but
  407. * only under special conditions and there is a performance hit.
  408. * The re-alignment happens at these occasions for different
  409. * @cra_u types: cipher -- For both input data and output data
  410. * buffer; ahash -- For output hash destination buf; shash --
  411. * For output hash destination buf.
  412. * This is needed on hardware which is flawed by design and
  413. * cannot pick data from arbitrary addresses.
  414. * @cra_priority: Priority of this transformation implementation. In case
  415. * multiple transformations with same @cra_name are available to
  416. * the Crypto API, the kernel will use the one with highest
  417. * @cra_priority.
  418. * @cra_name: Generic name (usable by multiple implementations) of the
  419. * transformation algorithm. This is the name of the transformation
  420. * itself. This field is used by the kernel when looking up the
  421. * providers of particular transformation.
  422. * @cra_driver_name: Unique name of the transformation provider. This is the
  423. * name of the provider of the transformation. This can be any
  424. * arbitrary value, but in the usual case, this contains the
  425. * name of the chip or provider and the name of the
  426. * transformation algorithm.
  427. * @cra_type: Type of the cryptographic transformation. This is a pointer to
  428. * struct crypto_type, which implements callbacks common for all
  429. * trasnformation types. There are multiple options:
  430. * &crypto_blkcipher_type, &crypto_ablkcipher_type,
  431. * &crypto_ahash_type, &crypto_aead_type, &crypto_rng_type.
  432. * This field might be empty. In that case, there are no common
  433. * callbacks. This is the case for: cipher, compress, shash.
  434. * @cra_u: Callbacks implementing the transformation. This is a union of
  435. * multiple structures. Depending on the type of transformation selected
  436. * by @cra_type and @cra_flags above, the associated structure must be
  437. * filled with callbacks. This field might be empty. This is the case
  438. * for ahash, shash.
  439. * @cra_init: Initialize the cryptographic transformation object. This function
  440. * is used to initialize the cryptographic transformation object.
  441. * This function is called only once at the instantiation time, right
  442. * after the transformation context was allocated. In case the
  443. * cryptographic hardware has some special requirements which need to
  444. * be handled by software, this function shall check for the precise
  445. * requirement of the transformation and put any software fallbacks
  446. * in place.
  447. * @cra_exit: Deinitialize the cryptographic transformation object. This is a
  448. * counterpart to @cra_init, used to remove various changes set in
  449. * @cra_init.
  450. * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
  451. * @cra_list: internally used
  452. * @cra_users: internally used
  453. * @cra_refcnt: internally used
  454. * @cra_destroy: internally used
  455. *
  456. * The struct crypto_alg describes a generic Crypto API algorithm and is common
  457. * for all of the transformations. Any variable not documented here shall not
  458. * be used by a cipher implementation as it is internal to the Crypto API.
  459. */
  460. struct crypto_alg {
  461. struct list_head cra_list;
  462. struct list_head cra_users;
  463. u32 cra_flags;
  464. unsigned int cra_blocksize;
  465. unsigned int cra_ctxsize;
  466. unsigned int cra_alignmask;
  467. int cra_priority;
  468. atomic_t cra_refcnt;
  469. char cra_name[CRYPTO_MAX_ALG_NAME];
  470. char cra_driver_name[CRYPTO_MAX_ALG_NAME];
  471. const struct crypto_type *cra_type;
  472. union {
  473. struct ablkcipher_alg ablkcipher;
  474. struct aead_alg aead;
  475. struct blkcipher_alg blkcipher;
  476. struct cipher_alg cipher;
  477. struct compress_alg compress;
  478. } cra_u;
  479. int (*cra_init)(struct crypto_tfm *tfm);
  480. void (*cra_exit)(struct crypto_tfm *tfm);
  481. void (*cra_destroy)(struct crypto_alg *alg);
  482. struct module *cra_module;
  483. };
  484. /*
  485. * Algorithm registration interface.
  486. */
  487. int crypto_register_alg(struct crypto_alg *alg);
  488. int crypto_unregister_alg(struct crypto_alg *alg);
  489. int crypto_register_algs(struct crypto_alg *algs, int count);
  490. int crypto_unregister_algs(struct crypto_alg *algs, int count);
  491. /*
  492. * Algorithm query interface.
  493. */
  494. int crypto_has_alg(const char *name, u32 type, u32 mask);
  495. /*
  496. * Transforms: user-instantiated objects which encapsulate algorithms
  497. * and core processing logic. Managed via crypto_alloc_*() and
  498. * crypto_free_*(), as well as the various helpers below.
  499. */
  500. struct ablkcipher_tfm {
  501. int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
  502. unsigned int keylen);
  503. int (*encrypt)(struct ablkcipher_request *req);
  504. int (*decrypt)(struct ablkcipher_request *req);
  505. int (*givencrypt)(struct skcipher_givcrypt_request *req);
  506. int (*givdecrypt)(struct skcipher_givcrypt_request *req);
  507. struct crypto_ablkcipher *base;
  508. unsigned int ivsize;
  509. unsigned int reqsize;
  510. };
  511. struct aead_tfm {
  512. int (*setkey)(struct crypto_aead *tfm, const u8 *key,
  513. unsigned int keylen);
  514. int (*encrypt)(struct aead_request *req);
  515. int (*decrypt)(struct aead_request *req);
  516. int (*givencrypt)(struct aead_givcrypt_request *req);
  517. int (*givdecrypt)(struct aead_givcrypt_request *req);
  518. struct crypto_aead *base;
  519. unsigned int ivsize;
  520. unsigned int authsize;
  521. unsigned int reqsize;
  522. };
  523. struct blkcipher_tfm {
  524. void *iv;
  525. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  526. unsigned int keylen);
  527. int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  528. struct scatterlist *src, unsigned int nbytes);
  529. int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  530. struct scatterlist *src, unsigned int nbytes);
  531. };
  532. struct cipher_tfm {
  533. int (*cit_setkey)(struct crypto_tfm *tfm,
  534. const u8 *key, unsigned int keylen);
  535. void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  536. void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  537. };
  538. struct hash_tfm {
  539. int (*init)(struct hash_desc *desc);
  540. int (*update)(struct hash_desc *desc,
  541. struct scatterlist *sg, unsigned int nsg);
  542. int (*final)(struct hash_desc *desc, u8 *out);
  543. int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
  544. unsigned int nsg, u8 *out);
  545. int (*setkey)(struct crypto_hash *tfm, const u8 *key,
  546. unsigned int keylen);
  547. unsigned int digestsize;
  548. };
  549. struct compress_tfm {
  550. int (*cot_compress)(struct crypto_tfm *tfm,
  551. const u8 *src, unsigned int slen,
  552. u8 *dst, unsigned int *dlen);
  553. int (*cot_decompress)(struct crypto_tfm *tfm,
  554. const u8 *src, unsigned int slen,
  555. u8 *dst, unsigned int *dlen);
  556. };
  557. #define crt_ablkcipher crt_u.ablkcipher
  558. #define crt_aead crt_u.aead
  559. #define crt_blkcipher crt_u.blkcipher
  560. #define crt_cipher crt_u.cipher
  561. #define crt_hash crt_u.hash
  562. #define crt_compress crt_u.compress
  563. struct crypto_tfm {
  564. u32 crt_flags;
  565. union {
  566. struct ablkcipher_tfm ablkcipher;
  567. struct aead_tfm aead;
  568. struct blkcipher_tfm blkcipher;
  569. struct cipher_tfm cipher;
  570. struct hash_tfm hash;
  571. struct compress_tfm compress;
  572. } crt_u;
  573. void (*exit)(struct crypto_tfm *tfm);
  574. struct crypto_alg *__crt_alg;
  575. void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
  576. };
  577. struct crypto_ablkcipher {
  578. struct crypto_tfm base;
  579. };
  580. struct crypto_aead {
  581. struct crypto_tfm base;
  582. };
  583. struct crypto_blkcipher {
  584. struct crypto_tfm base;
  585. };
  586. struct crypto_cipher {
  587. struct crypto_tfm base;
  588. };
  589. struct crypto_comp {
  590. struct crypto_tfm base;
  591. };
  592. struct crypto_hash {
  593. struct crypto_tfm base;
  594. };
  595. enum {
  596. CRYPTOA_UNSPEC,
  597. CRYPTOA_ALG,
  598. CRYPTOA_TYPE,
  599. CRYPTOA_U32,
  600. __CRYPTOA_MAX,
  601. };
  602. #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
  603. /* Maximum number of (rtattr) parameters for each template. */
  604. #define CRYPTO_MAX_ATTRS 32
  605. struct crypto_attr_alg {
  606. char name[CRYPTO_MAX_ALG_NAME];
  607. };
  608. struct crypto_attr_type {
  609. u32 type;
  610. u32 mask;
  611. };
  612. struct crypto_attr_u32 {
  613. u32 num;
  614. };
  615. /*
  616. * Transform user interface.
  617. */
  618. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
  619. void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
  620. static inline void crypto_free_tfm(struct crypto_tfm *tfm)
  621. {
  622. return crypto_destroy_tfm(tfm, tfm);
  623. }
  624. int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
  625. /*
  626. * Transform helpers which query the underlying algorithm.
  627. */
  628. static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
  629. {
  630. return tfm->__crt_alg->cra_name;
  631. }
  632. static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
  633. {
  634. return tfm->__crt_alg->cra_driver_name;
  635. }
  636. static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
  637. {
  638. return tfm->__crt_alg->cra_priority;
  639. }
  640. static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
  641. {
  642. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
  643. }
  644. static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
  645. {
  646. return tfm->__crt_alg->cra_blocksize;
  647. }
  648. static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
  649. {
  650. return tfm->__crt_alg->cra_alignmask;
  651. }
  652. static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
  653. {
  654. return tfm->crt_flags;
  655. }
  656. static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
  657. {
  658. tfm->crt_flags |= flags;
  659. }
  660. static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
  661. {
  662. tfm->crt_flags &= ~flags;
  663. }
  664. static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
  665. {
  666. return tfm->__crt_ctx;
  667. }
  668. static inline unsigned int crypto_tfm_ctx_alignment(void)
  669. {
  670. struct crypto_tfm *tfm;
  671. return __alignof__(tfm->__crt_ctx);
  672. }
  673. /*
  674. * API wrappers.
  675. */
  676. static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
  677. struct crypto_tfm *tfm)
  678. {
  679. return (struct crypto_ablkcipher *)tfm;
  680. }
  681. static inline u32 crypto_skcipher_type(u32 type)
  682. {
  683. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  684. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  685. return type;
  686. }
  687. static inline u32 crypto_skcipher_mask(u32 mask)
  688. {
  689. mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  690. mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
  691. return mask;
  692. }
  693. /**
  694. * DOC: Asynchronous Block Cipher API
  695. *
  696. * Asynchronous block cipher API is used with the ciphers of type
  697. * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
  698. *
  699. * Asynchronous cipher operations imply that the function invocation for a
  700. * cipher request returns immediately before the completion of the operation.
  701. * The cipher request is scheduled as a separate kernel thread and therefore
  702. * load-balanced on the different CPUs via the process scheduler. To allow
  703. * the kernel crypto API to inform the caller about the completion of a cipher
  704. * request, the caller must provide a callback function. That function is
  705. * invoked with the cipher handle when the request completes.
  706. *
  707. * To support the asynchronous operation, additional information than just the
  708. * cipher handle must be supplied to the kernel crypto API. That additional
  709. * information is given by filling in the ablkcipher_request data structure.
  710. *
  711. * For the asynchronous block cipher API, the state is maintained with the tfm
  712. * cipher handle. A single tfm can be used across multiple calls and in
  713. * parallel. For asynchronous block cipher calls, context data supplied and
  714. * only used by the caller can be referenced the request data structure in
  715. * addition to the IV used for the cipher request. The maintenance of such
  716. * state information would be important for a crypto driver implementer to
  717. * have, because when calling the callback function upon completion of the
  718. * cipher operation, that callback function may need some information about
  719. * which operation just finished if it invoked multiple in parallel. This
  720. * state information is unused by the kernel crypto API.
  721. */
  722. /**
  723. * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
  724. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  725. * ablkcipher cipher
  726. * @type: specifies the type of the cipher
  727. * @mask: specifies the mask for the cipher
  728. *
  729. * Allocate a cipher handle for an ablkcipher. The returned struct
  730. * crypto_ablkcipher is the cipher handle that is required for any subsequent
  731. * API invocation for that ablkcipher.
  732. *
  733. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  734. * of an error, PTR_ERR() returns the error code.
  735. */
  736. struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
  737. u32 type, u32 mask);
  738. static inline struct crypto_tfm *crypto_ablkcipher_tfm(
  739. struct crypto_ablkcipher *tfm)
  740. {
  741. return &tfm->base;
  742. }
  743. /**
  744. * crypto_free_ablkcipher() - zeroize and free cipher handle
  745. * @tfm: cipher handle to be freed
  746. */
  747. static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
  748. {
  749. crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
  750. }
  751. /**
  752. * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
  753. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  754. * ablkcipher
  755. * @type: specifies the type of the cipher
  756. * @mask: specifies the mask for the cipher
  757. *
  758. * Return: true when the ablkcipher is known to the kernel crypto API; false
  759. * otherwise
  760. */
  761. static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
  762. u32 mask)
  763. {
  764. return crypto_has_alg(alg_name, crypto_skcipher_type(type),
  765. crypto_skcipher_mask(mask));
  766. }
  767. static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
  768. struct crypto_ablkcipher *tfm)
  769. {
  770. return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
  771. }
  772. /**
  773. * crypto_ablkcipher_ivsize() - obtain IV size
  774. * @tfm: cipher handle
  775. *
  776. * The size of the IV for the ablkcipher referenced by the cipher handle is
  777. * returned. This IV size may be zero if the cipher does not need an IV.
  778. *
  779. * Return: IV size in bytes
  780. */
  781. static inline unsigned int crypto_ablkcipher_ivsize(
  782. struct crypto_ablkcipher *tfm)
  783. {
  784. return crypto_ablkcipher_crt(tfm)->ivsize;
  785. }
  786. /**
  787. * crypto_ablkcipher_blocksize() - obtain block size of cipher
  788. * @tfm: cipher handle
  789. *
  790. * The block size for the ablkcipher referenced with the cipher handle is
  791. * returned. The caller may use that information to allocate appropriate
  792. * memory for the data returned by the encryption or decryption operation
  793. *
  794. * Return: block size of cipher
  795. */
  796. static inline unsigned int crypto_ablkcipher_blocksize(
  797. struct crypto_ablkcipher *tfm)
  798. {
  799. return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
  800. }
  801. static inline unsigned int crypto_ablkcipher_alignmask(
  802. struct crypto_ablkcipher *tfm)
  803. {
  804. return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
  805. }
  806. static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
  807. {
  808. return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
  809. }
  810. static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
  811. u32 flags)
  812. {
  813. crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
  814. }
  815. static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
  816. u32 flags)
  817. {
  818. crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
  819. }
  820. /**
  821. * crypto_ablkcipher_setkey() - set key for cipher
  822. * @tfm: cipher handle
  823. * @key: buffer holding the key
  824. * @keylen: length of the key in bytes
  825. *
  826. * The caller provided key is set for the ablkcipher referenced by the cipher
  827. * handle.
  828. *
  829. * Note, the key length determines the cipher type. Many block ciphers implement
  830. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  831. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  832. * is performed.
  833. *
  834. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  835. */
  836. static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
  837. const u8 *key, unsigned int keylen)
  838. {
  839. struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
  840. return crt->setkey(crt->base, key, keylen);
  841. }
  842. /**
  843. * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
  844. * @req: ablkcipher_request out of which the cipher handle is to be obtained
  845. *
  846. * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
  847. * data structure.
  848. *
  849. * Return: crypto_ablkcipher handle
  850. */
  851. static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
  852. struct ablkcipher_request *req)
  853. {
  854. return __crypto_ablkcipher_cast(req->base.tfm);
  855. }
  856. /**
  857. * crypto_ablkcipher_encrypt() - encrypt plaintext
  858. * @req: reference to the ablkcipher_request handle that holds all information
  859. * needed to perform the cipher operation
  860. *
  861. * Encrypt plaintext data using the ablkcipher_request handle. That data
  862. * structure and how it is filled with data is discussed with the
  863. * ablkcipher_request_* functions.
  864. *
  865. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  866. */
  867. static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
  868. {
  869. struct ablkcipher_tfm *crt =
  870. crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
  871. return crt->encrypt(req);
  872. }
  873. /**
  874. * crypto_ablkcipher_decrypt() - decrypt ciphertext
  875. * @req: reference to the ablkcipher_request handle that holds all information
  876. * needed to perform the cipher operation
  877. *
  878. * Decrypt ciphertext data using the ablkcipher_request handle. That data
  879. * structure and how it is filled with data is discussed with the
  880. * ablkcipher_request_* functions.
  881. *
  882. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  883. */
  884. static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
  885. {
  886. struct ablkcipher_tfm *crt =
  887. crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
  888. return crt->decrypt(req);
  889. }
  890. /**
  891. * DOC: Asynchronous Cipher Request Handle
  892. *
  893. * The ablkcipher_request data structure contains all pointers to data
  894. * required for the asynchronous cipher operation. This includes the cipher
  895. * handle (which can be used by multiple ablkcipher_request instances), pointer
  896. * to plaintext and ciphertext, asynchronous callback function, etc. It acts
  897. * as a handle to the ablkcipher_request_* API calls in a similar way as
  898. * ablkcipher handle to the crypto_ablkcipher_* API calls.
  899. */
  900. /**
  901. * crypto_ablkcipher_reqsize() - obtain size of the request data structure
  902. * @tfm: cipher handle
  903. *
  904. * Return: number of bytes
  905. */
  906. static inline unsigned int crypto_ablkcipher_reqsize(
  907. struct crypto_ablkcipher *tfm)
  908. {
  909. return crypto_ablkcipher_crt(tfm)->reqsize;
  910. }
  911. /**
  912. * ablkcipher_request_set_tfm() - update cipher handle reference in request
  913. * @req: request handle to be modified
  914. * @tfm: cipher handle that shall be added to the request handle
  915. *
  916. * Allow the caller to replace the existing ablkcipher handle in the request
  917. * data structure with a different one.
  918. */
  919. static inline void ablkcipher_request_set_tfm(
  920. struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
  921. {
  922. req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
  923. }
  924. static inline struct ablkcipher_request *ablkcipher_request_cast(
  925. struct crypto_async_request *req)
  926. {
  927. return container_of(req, struct ablkcipher_request, base);
  928. }
  929. /**
  930. * ablkcipher_request_alloc() - allocate request data structure
  931. * @tfm: cipher handle to be registered with the request
  932. * @gfp: memory allocation flag that is handed to kmalloc by the API call.
  933. *
  934. * Allocate the request data structure that must be used with the ablkcipher
  935. * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
  936. * handle is registered in the request data structure.
  937. *
  938. * Return: allocated request handle in case of success; IS_ERR() is true in case
  939. * of an error, PTR_ERR() returns the error code.
  940. */
  941. static inline struct ablkcipher_request *ablkcipher_request_alloc(
  942. struct crypto_ablkcipher *tfm, gfp_t gfp)
  943. {
  944. struct ablkcipher_request *req;
  945. req = kmalloc(sizeof(struct ablkcipher_request) +
  946. crypto_ablkcipher_reqsize(tfm), gfp);
  947. if (likely(req))
  948. ablkcipher_request_set_tfm(req, tfm);
  949. return req;
  950. }
  951. /**
  952. * ablkcipher_request_free() - zeroize and free request data structure
  953. * @req: request data structure cipher handle to be freed
  954. */
  955. static inline void ablkcipher_request_free(struct ablkcipher_request *req)
  956. {
  957. kzfree(req);
  958. }
  959. /**
  960. * ablkcipher_request_set_callback() - set asynchronous callback function
  961. * @req: request handle
  962. * @flags: specify zero or an ORing of the flags
  963. * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
  964. * increase the wait queue beyond the initial maximum size;
  965. * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
  966. * @compl: callback function pointer to be registered with the request handle
  967. * @data: The data pointer refers to memory that is not used by the kernel
  968. * crypto API, but provided to the callback function for it to use. Here,
  969. * the caller can provide a reference to memory the callback function can
  970. * operate on. As the callback function is invoked asynchronously to the
  971. * related functionality, it may need to access data structures of the
  972. * related functionality which can be referenced using this pointer. The
  973. * callback function can access the memory via the "data" field in the
  974. * crypto_async_request data structure provided to the callback function.
  975. *
  976. * This function allows setting the callback function that is triggered once the
  977. * cipher operation completes.
  978. *
  979. * The callback function is registered with the ablkcipher_request handle and
  980. * must comply with the following template
  981. *
  982. * void callback_function(struct crypto_async_request *req, int error)
  983. */
  984. static inline void ablkcipher_request_set_callback(
  985. struct ablkcipher_request *req,
  986. u32 flags, crypto_completion_t compl, void *data)
  987. {
  988. req->base.complete = compl;
  989. req->base.data = data;
  990. req->base.flags = flags;
  991. }
  992. /**
  993. * ablkcipher_request_set_crypt() - set data buffers
  994. * @req: request handle
  995. * @src: source scatter / gather list
  996. * @dst: destination scatter / gather list
  997. * @nbytes: number of bytes to process from @src
  998. * @iv: IV for the cipher operation which must comply with the IV size defined
  999. * by crypto_ablkcipher_ivsize
  1000. *
  1001. * This function allows setting of the source data and destination data
  1002. * scatter / gather lists.
  1003. *
  1004. * For encryption, the source is treated as the plaintext and the
  1005. * destination is the ciphertext. For a decryption operation, the use is
  1006. * reversed - the source is the ciphertext and the destination is the plaintext.
  1007. */
  1008. static inline void ablkcipher_request_set_crypt(
  1009. struct ablkcipher_request *req,
  1010. struct scatterlist *src, struct scatterlist *dst,
  1011. unsigned int nbytes, void *iv)
  1012. {
  1013. req->src = src;
  1014. req->dst = dst;
  1015. req->nbytes = nbytes;
  1016. req->info = iv;
  1017. }
  1018. /**
  1019. * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
  1020. *
  1021. * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
  1022. * (listed as type "aead" in /proc/crypto)
  1023. *
  1024. * The most prominent examples for this type of encryption is GCM and CCM.
  1025. * However, the kernel supports other types of AEAD ciphers which are defined
  1026. * with the following cipher string:
  1027. *
  1028. * authenc(keyed message digest, block cipher)
  1029. *
  1030. * For example: authenc(hmac(sha256), cbc(aes))
  1031. *
  1032. * The example code provided for the asynchronous block cipher operation
  1033. * applies here as well. Naturally all *ablkcipher* symbols must be exchanged
  1034. * the *aead* pendants discussed in the following. In addtion, for the AEAD
  1035. * operation, the aead_request_set_assoc function must be used to set the
  1036. * pointer to the associated data memory location before performing the
  1037. * encryption or decryption operation. In case of an encryption, the associated
  1038. * data memory is filled during the encryption operation. For decryption, the
  1039. * associated data memory must contain data that is used to verify the integrity
  1040. * of the decrypted data. Another deviation from the asynchronous block cipher
  1041. * operation is that the caller should explicitly check for -EBADMSG of the
  1042. * crypto_aead_decrypt. That error indicates an authentication error, i.e.
  1043. * a breach in the integrity of the message. In essence, that -EBADMSG error
  1044. * code is the key bonus an AEAD cipher has over "standard" block chaining
  1045. * modes.
  1046. */
  1047. static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
  1048. {
  1049. return (struct crypto_aead *)tfm;
  1050. }
  1051. /**
  1052. * crypto_alloc_aead() - allocate AEAD cipher handle
  1053. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1054. * AEAD cipher
  1055. * @type: specifies the type of the cipher
  1056. * @mask: specifies the mask for the cipher
  1057. *
  1058. * Allocate a cipher handle for an AEAD. The returned struct
  1059. * crypto_aead is the cipher handle that is required for any subsequent
  1060. * API invocation for that AEAD.
  1061. *
  1062. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  1063. * of an error, PTR_ERR() returns the error code.
  1064. */
  1065. struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
  1066. static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
  1067. {
  1068. return &tfm->base;
  1069. }
  1070. /**
  1071. * crypto_free_aead() - zeroize and free aead handle
  1072. * @tfm: cipher handle to be freed
  1073. */
  1074. static inline void crypto_free_aead(struct crypto_aead *tfm)
  1075. {
  1076. crypto_free_tfm(crypto_aead_tfm(tfm));
  1077. }
  1078. static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
  1079. {
  1080. return &crypto_aead_tfm(tfm)->crt_aead;
  1081. }
  1082. /**
  1083. * crypto_aead_ivsize() - obtain IV size
  1084. * @tfm: cipher handle
  1085. *
  1086. * The size of the IV for the aead referenced by the cipher handle is
  1087. * returned. This IV size may be zero if the cipher does not need an IV.
  1088. *
  1089. * Return: IV size in bytes
  1090. */
  1091. static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
  1092. {
  1093. return crypto_aead_crt(tfm)->ivsize;
  1094. }
  1095. /**
  1096. * crypto_aead_authsize() - obtain maximum authentication data size
  1097. * @tfm: cipher handle
  1098. *
  1099. * The maximum size of the authentication data for the AEAD cipher referenced
  1100. * by the AEAD cipher handle is returned. The authentication data size may be
  1101. * zero if the cipher implements a hard-coded maximum.
  1102. *
  1103. * The authentication data may also be known as "tag value".
  1104. *
  1105. * Return: authentication data size / tag size in bytes
  1106. */
  1107. static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
  1108. {
  1109. return crypto_aead_crt(tfm)->authsize;
  1110. }
  1111. /**
  1112. * crypto_aead_blocksize() - obtain block size of cipher
  1113. * @tfm: cipher handle
  1114. *
  1115. * The block size for the AEAD referenced with the cipher handle is returned.
  1116. * The caller may use that information to allocate appropriate memory for the
  1117. * data returned by the encryption or decryption operation
  1118. *
  1119. * Return: block size of cipher
  1120. */
  1121. static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
  1122. {
  1123. return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
  1124. }
  1125. static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
  1126. {
  1127. return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
  1128. }
  1129. static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
  1130. {
  1131. return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
  1132. }
  1133. static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
  1134. {
  1135. crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
  1136. }
  1137. static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
  1138. {
  1139. crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
  1140. }
  1141. /**
  1142. * crypto_aead_setkey() - set key for cipher
  1143. * @tfm: cipher handle
  1144. * @key: buffer holding the key
  1145. * @keylen: length of the key in bytes
  1146. *
  1147. * The caller provided key is set for the AEAD referenced by the cipher
  1148. * handle.
  1149. *
  1150. * Note, the key length determines the cipher type. Many block ciphers implement
  1151. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  1152. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  1153. * is performed.
  1154. *
  1155. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  1156. */
  1157. static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
  1158. unsigned int keylen)
  1159. {
  1160. struct aead_tfm *crt = crypto_aead_crt(tfm);
  1161. return crt->setkey(crt->base, key, keylen);
  1162. }
  1163. /**
  1164. * crypto_aead_setauthsize() - set authentication data size
  1165. * @tfm: cipher handle
  1166. * @authsize: size of the authentication data / tag in bytes
  1167. *
  1168. * Set the authentication data size / tag size. AEAD requires an authentication
  1169. * tag (or MAC) in addition to the associated data.
  1170. *
  1171. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  1172. */
  1173. int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
  1174. static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
  1175. {
  1176. return __crypto_aead_cast(req->base.tfm);
  1177. }
  1178. /**
  1179. * crypto_aead_encrypt() - encrypt plaintext
  1180. * @req: reference to the aead_request handle that holds all information
  1181. * needed to perform the cipher operation
  1182. *
  1183. * Encrypt plaintext data using the aead_request handle. That data structure
  1184. * and how it is filled with data is discussed with the aead_request_*
  1185. * functions.
  1186. *
  1187. * IMPORTANT NOTE The encryption operation creates the authentication data /
  1188. * tag. That data is concatenated with the created ciphertext.
  1189. * The ciphertext memory size is therefore the given number of
  1190. * block cipher blocks + the size defined by the
  1191. * crypto_aead_setauthsize invocation. The caller must ensure
  1192. * that sufficient memory is available for the ciphertext and
  1193. * the authentication tag.
  1194. *
  1195. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  1196. */
  1197. static inline int crypto_aead_encrypt(struct aead_request *req)
  1198. {
  1199. return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
  1200. }
  1201. /**
  1202. * crypto_aead_decrypt() - decrypt ciphertext
  1203. * @req: reference to the ablkcipher_request handle that holds all information
  1204. * needed to perform the cipher operation
  1205. *
  1206. * Decrypt ciphertext data using the aead_request handle. That data structure
  1207. * and how it is filled with data is discussed with the aead_request_*
  1208. * functions.
  1209. *
  1210. * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
  1211. * authentication data / tag. That authentication data / tag
  1212. * must have the size defined by the crypto_aead_setauthsize
  1213. * invocation.
  1214. *
  1215. *
  1216. * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
  1217. * cipher operation performs the authentication of the data during the
  1218. * decryption operation. Therefore, the function returns this error if
  1219. * the authentication of the ciphertext was unsuccessful (i.e. the
  1220. * integrity of the ciphertext or the associated data was violated);
  1221. * < 0 if an error occurred.
  1222. */
  1223. static inline int crypto_aead_decrypt(struct aead_request *req)
  1224. {
  1225. if (req->cryptlen < crypto_aead_authsize(crypto_aead_reqtfm(req)))
  1226. return -EINVAL;
  1227. return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
  1228. }
  1229. /**
  1230. * DOC: Asynchronous AEAD Request Handle
  1231. *
  1232. * The aead_request data structure contains all pointers to data required for
  1233. * the AEAD cipher operation. This includes the cipher handle (which can be
  1234. * used by multiple aead_request instances), pointer to plaintext and
  1235. * ciphertext, asynchronous callback function, etc. It acts as a handle to the
  1236. * aead_request_* API calls in a similar way as AEAD handle to the
  1237. * crypto_aead_* API calls.
  1238. */
  1239. /**
  1240. * crypto_aead_reqsize() - obtain size of the request data structure
  1241. * @tfm: cipher handle
  1242. *
  1243. * Return: number of bytes
  1244. */
  1245. static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
  1246. {
  1247. return crypto_aead_crt(tfm)->reqsize;
  1248. }
  1249. /**
  1250. * aead_request_set_tfm() - update cipher handle reference in request
  1251. * @req: request handle to be modified
  1252. * @tfm: cipher handle that shall be added to the request handle
  1253. *
  1254. * Allow the caller to replace the existing aead handle in the request
  1255. * data structure with a different one.
  1256. */
  1257. static inline void aead_request_set_tfm(struct aead_request *req,
  1258. struct crypto_aead *tfm)
  1259. {
  1260. req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
  1261. }
  1262. /**
  1263. * aead_request_alloc() - allocate request data structure
  1264. * @tfm: cipher handle to be registered with the request
  1265. * @gfp: memory allocation flag that is handed to kmalloc by the API call.
  1266. *
  1267. * Allocate the request data structure that must be used with the AEAD
  1268. * encrypt and decrypt API calls. During the allocation, the provided aead
  1269. * handle is registered in the request data structure.
  1270. *
  1271. * Return: allocated request handle in case of success; IS_ERR() is true in case
  1272. * of an error, PTR_ERR() returns the error code.
  1273. */
  1274. static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
  1275. gfp_t gfp)
  1276. {
  1277. struct aead_request *req;
  1278. req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
  1279. if (likely(req))
  1280. aead_request_set_tfm(req, tfm);
  1281. return req;
  1282. }
  1283. /**
  1284. * aead_request_free() - zeroize and free request data structure
  1285. * @req: request data structure cipher handle to be freed
  1286. */
  1287. static inline void aead_request_free(struct aead_request *req)
  1288. {
  1289. kzfree(req);
  1290. }
  1291. /**
  1292. * aead_request_set_callback() - set asynchronous callback function
  1293. * @req: request handle
  1294. * @flags: specify zero or an ORing of the flags
  1295. * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
  1296. * increase the wait queue beyond the initial maximum size;
  1297. * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
  1298. * @compl: callback function pointer to be registered with the request handle
  1299. * @data: The data pointer refers to memory that is not used by the kernel
  1300. * crypto API, but provided to the callback function for it to use. Here,
  1301. * the caller can provide a reference to memory the callback function can
  1302. * operate on. As the callback function is invoked asynchronously to the
  1303. * related functionality, it may need to access data structures of the
  1304. * related functionality which can be referenced using this pointer. The
  1305. * callback function can access the memory via the "data" field in the
  1306. * crypto_async_request data structure provided to the callback function.
  1307. *
  1308. * Setting the callback function that is triggered once the cipher operation
  1309. * completes
  1310. *
  1311. * The callback function is registered with the aead_request handle and
  1312. * must comply with the following template
  1313. *
  1314. * void callback_function(struct crypto_async_request *req, int error)
  1315. */
  1316. static inline void aead_request_set_callback(struct aead_request *req,
  1317. u32 flags,
  1318. crypto_completion_t compl,
  1319. void *data)
  1320. {
  1321. req->base.complete = compl;
  1322. req->base.data = data;
  1323. req->base.flags = flags;
  1324. }
  1325. /**
  1326. * aead_request_set_crypt - set data buffers
  1327. * @req: request handle
  1328. * @src: source scatter / gather list
  1329. * @dst: destination scatter / gather list
  1330. * @cryptlen: number of bytes to process from @src
  1331. * @iv: IV for the cipher operation which must comply with the IV size defined
  1332. * by crypto_aead_ivsize()
  1333. *
  1334. * Setting the source data and destination data scatter / gather lists.
  1335. *
  1336. * For encryption, the source is treated as the plaintext and the
  1337. * destination is the ciphertext. For a decryption operation, the use is
  1338. * reversed - the source is the ciphertext and the destination is the plaintext.
  1339. *
  1340. * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
  1341. * the caller must concatenate the ciphertext followed by the
  1342. * authentication tag and provide the entire data stream to the
  1343. * decryption operation (i.e. the data length used for the
  1344. * initialization of the scatterlist and the data length for the
  1345. * decryption operation is identical). For encryption, however,
  1346. * the authentication tag is created while encrypting the data.
  1347. * The destination buffer must hold sufficient space for the
  1348. * ciphertext and the authentication tag while the encryption
  1349. * invocation must only point to the plaintext data size. The
  1350. * following code snippet illustrates the memory usage
  1351. * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
  1352. * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
  1353. * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
  1354. */
  1355. static inline void aead_request_set_crypt(struct aead_request *req,
  1356. struct scatterlist *src,
  1357. struct scatterlist *dst,
  1358. unsigned int cryptlen, u8 *iv)
  1359. {
  1360. req->src = src;
  1361. req->dst = dst;
  1362. req->cryptlen = cryptlen;
  1363. req->iv = iv;
  1364. }
  1365. /**
  1366. * aead_request_set_assoc() - set the associated data scatter / gather list
  1367. * @req: request handle
  1368. * @assoc: associated data scatter / gather list
  1369. * @assoclen: number of bytes to process from @assoc
  1370. *
  1371. * For encryption, the memory is filled with the associated data. For
  1372. * decryption, the memory must point to the associated data.
  1373. */
  1374. static inline void aead_request_set_assoc(struct aead_request *req,
  1375. struct scatterlist *assoc,
  1376. unsigned int assoclen)
  1377. {
  1378. req->assoc = assoc;
  1379. req->assoclen = assoclen;
  1380. }
  1381. /**
  1382. * DOC: Synchronous Block Cipher API
  1383. *
  1384. * The synchronous block cipher API is used with the ciphers of type
  1385. * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
  1386. *
  1387. * Synchronous calls, have a context in the tfm. But since a single tfm can be
  1388. * used in multiple calls and in parallel, this info should not be changeable
  1389. * (unless a lock is used). This applies, for example, to the symmetric key.
  1390. * However, the IV is changeable, so there is an iv field in blkcipher_tfm
  1391. * structure for synchronous blkcipher api. So, its the only state info that can
  1392. * be kept for synchronous calls without using a big lock across a tfm.
  1393. *
  1394. * The block cipher API allows the use of a complete cipher, i.e. a cipher
  1395. * consisting of a template (a block chaining mode) and a single block cipher
  1396. * primitive (e.g. AES).
  1397. *
  1398. * The plaintext data buffer and the ciphertext data buffer are pointed to
  1399. * by using scatter/gather lists. The cipher operation is performed
  1400. * on all segments of the provided scatter/gather lists.
  1401. *
  1402. * The kernel crypto API supports a cipher operation "in-place" which means that
  1403. * the caller may provide the same scatter/gather list for the plaintext and
  1404. * cipher text. After the completion of the cipher operation, the plaintext
  1405. * data is replaced with the ciphertext data in case of an encryption and vice
  1406. * versa for a decryption. The caller must ensure that the scatter/gather lists
  1407. * for the output data point to sufficiently large buffers, i.e. multiples of
  1408. * the block size of the cipher.
  1409. */
  1410. static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
  1411. struct crypto_tfm *tfm)
  1412. {
  1413. return (struct crypto_blkcipher *)tfm;
  1414. }
  1415. static inline struct crypto_blkcipher *crypto_blkcipher_cast(
  1416. struct crypto_tfm *tfm)
  1417. {
  1418. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
  1419. return __crypto_blkcipher_cast(tfm);
  1420. }
  1421. /**
  1422. * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
  1423. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1424. * blkcipher cipher
  1425. * @type: specifies the type of the cipher
  1426. * @mask: specifies the mask for the cipher
  1427. *
  1428. * Allocate a cipher handle for a block cipher. The returned struct
  1429. * crypto_blkcipher is the cipher handle that is required for any subsequent
  1430. * API invocation for that block cipher.
  1431. *
  1432. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  1433. * of an error, PTR_ERR() returns the error code.
  1434. */
  1435. static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
  1436. const char *alg_name, u32 type, u32 mask)
  1437. {
  1438. type &= ~CRYPTO_ALG_TYPE_MASK;
  1439. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  1440. mask |= CRYPTO_ALG_TYPE_MASK;
  1441. return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
  1442. }
  1443. static inline struct crypto_tfm *crypto_blkcipher_tfm(
  1444. struct crypto_blkcipher *tfm)
  1445. {
  1446. return &tfm->base;
  1447. }
  1448. /**
  1449. * crypto_free_blkcipher() - zeroize and free the block cipher handle
  1450. * @tfm: cipher handle to be freed
  1451. */
  1452. static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
  1453. {
  1454. crypto_free_tfm(crypto_blkcipher_tfm(tfm));
  1455. }
  1456. /**
  1457. * crypto_has_blkcipher() - Search for the availability of a block cipher
  1458. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1459. * block cipher
  1460. * @type: specifies the type of the cipher
  1461. * @mask: specifies the mask for the cipher
  1462. *
  1463. * Return: true when the block cipher is known to the kernel crypto API; false
  1464. * otherwise
  1465. */
  1466. static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
  1467. {
  1468. type &= ~CRYPTO_ALG_TYPE_MASK;
  1469. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  1470. mask |= CRYPTO_ALG_TYPE_MASK;
  1471. return crypto_has_alg(alg_name, type, mask);
  1472. }
  1473. /**
  1474. * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
  1475. * @tfm: cipher handle
  1476. *
  1477. * Return: The character string holding the name of the cipher
  1478. */
  1479. static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
  1480. {
  1481. return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
  1482. }
  1483. static inline struct blkcipher_tfm *crypto_blkcipher_crt(
  1484. struct crypto_blkcipher *tfm)
  1485. {
  1486. return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
  1487. }
  1488. static inline struct blkcipher_alg *crypto_blkcipher_alg(
  1489. struct crypto_blkcipher *tfm)
  1490. {
  1491. return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
  1492. }
  1493. /**
  1494. * crypto_blkcipher_ivsize() - obtain IV size
  1495. * @tfm: cipher handle
  1496. *
  1497. * The size of the IV for the block cipher referenced by the cipher handle is
  1498. * returned. This IV size may be zero if the cipher does not need an IV.
  1499. *
  1500. * Return: IV size in bytes
  1501. */
  1502. static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
  1503. {
  1504. return crypto_blkcipher_alg(tfm)->ivsize;
  1505. }
  1506. /**
  1507. * crypto_blkcipher_blocksize() - obtain block size of cipher
  1508. * @tfm: cipher handle
  1509. *
  1510. * The block size for the block cipher referenced with the cipher handle is
  1511. * returned. The caller may use that information to allocate appropriate
  1512. * memory for the data returned by the encryption or decryption operation.
  1513. *
  1514. * Return: block size of cipher
  1515. */
  1516. static inline unsigned int crypto_blkcipher_blocksize(
  1517. struct crypto_blkcipher *tfm)
  1518. {
  1519. return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
  1520. }
  1521. static inline unsigned int crypto_blkcipher_alignmask(
  1522. struct crypto_blkcipher *tfm)
  1523. {
  1524. return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
  1525. }
  1526. static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
  1527. {
  1528. return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
  1529. }
  1530. static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
  1531. u32 flags)
  1532. {
  1533. crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
  1534. }
  1535. static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
  1536. u32 flags)
  1537. {
  1538. crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
  1539. }
  1540. /**
  1541. * crypto_blkcipher_setkey() - set key for cipher
  1542. * @tfm: cipher handle
  1543. * @key: buffer holding the key
  1544. * @keylen: length of the key in bytes
  1545. *
  1546. * The caller provided key is set for the block cipher referenced by the cipher
  1547. * handle.
  1548. *
  1549. * Note, the key length determines the cipher type. Many block ciphers implement
  1550. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  1551. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  1552. * is performed.
  1553. *
  1554. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  1555. */
  1556. static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
  1557. const u8 *key, unsigned int keylen)
  1558. {
  1559. return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
  1560. key, keylen);
  1561. }
  1562. /**
  1563. * crypto_blkcipher_encrypt() - encrypt plaintext
  1564. * @desc: reference to the block cipher handle with meta data
  1565. * @dst: scatter/gather list that is filled by the cipher operation with the
  1566. * ciphertext
  1567. * @src: scatter/gather list that holds the plaintext
  1568. * @nbytes: number of bytes of the plaintext to encrypt.
  1569. *
  1570. * Encrypt plaintext data using the IV set by the caller with a preceding
  1571. * call of crypto_blkcipher_set_iv.
  1572. *
  1573. * The blkcipher_desc data structure must be filled by the caller and can
  1574. * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
  1575. * with the block cipher handle; desc.flags is filled with either
  1576. * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
  1577. *
  1578. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  1579. */
  1580. static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
  1581. struct scatterlist *dst,
  1582. struct scatterlist *src,
  1583. unsigned int nbytes)
  1584. {
  1585. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  1586. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  1587. }
  1588. /**
  1589. * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
  1590. * @desc: reference to the block cipher handle with meta data
  1591. * @dst: scatter/gather list that is filled by the cipher operation with the
  1592. * ciphertext
  1593. * @src: scatter/gather list that holds the plaintext
  1594. * @nbytes: number of bytes of the plaintext to encrypt.
  1595. *
  1596. * Encrypt plaintext data with the use of an IV that is solely used for this
  1597. * cipher operation. Any previously set IV is not used.
  1598. *
  1599. * The blkcipher_desc data structure must be filled by the caller and can
  1600. * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
  1601. * with the block cipher handle; desc.info is filled with the IV to be used for
  1602. * the current operation; desc.flags is filled with either
  1603. * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
  1604. *
  1605. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  1606. */
  1607. static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
  1608. struct scatterlist *dst,
  1609. struct scatterlist *src,
  1610. unsigned int nbytes)
  1611. {
  1612. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  1613. }
  1614. /**
  1615. * crypto_blkcipher_decrypt() - decrypt ciphertext
  1616. * @desc: reference to the block cipher handle with meta data
  1617. * @dst: scatter/gather list that is filled by the cipher operation with the
  1618. * plaintext
  1619. * @src: scatter/gather list that holds the ciphertext
  1620. * @nbytes: number of bytes of the ciphertext to decrypt.
  1621. *
  1622. * Decrypt ciphertext data using the IV set by the caller with a preceding
  1623. * call of crypto_blkcipher_set_iv.
  1624. *
  1625. * The blkcipher_desc data structure must be filled by the caller as documented
  1626. * for the crypto_blkcipher_encrypt call above.
  1627. *
  1628. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  1629. *
  1630. */
  1631. static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
  1632. struct scatterlist *dst,
  1633. struct scatterlist *src,
  1634. unsigned int nbytes)
  1635. {
  1636. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  1637. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  1638. }
  1639. /**
  1640. * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
  1641. * @desc: reference to the block cipher handle with meta data
  1642. * @dst: scatter/gather list that is filled by the cipher operation with the
  1643. * plaintext
  1644. * @src: scatter/gather list that holds the ciphertext
  1645. * @nbytes: number of bytes of the ciphertext to decrypt.
  1646. *
  1647. * Decrypt ciphertext data with the use of an IV that is solely used for this
  1648. * cipher operation. Any previously set IV is not used.
  1649. *
  1650. * The blkcipher_desc data structure must be filled by the caller as documented
  1651. * for the crypto_blkcipher_encrypt_iv call above.
  1652. *
  1653. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  1654. */
  1655. static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
  1656. struct scatterlist *dst,
  1657. struct scatterlist *src,
  1658. unsigned int nbytes)
  1659. {
  1660. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  1661. }
  1662. /**
  1663. * crypto_blkcipher_set_iv() - set IV for cipher
  1664. * @tfm: cipher handle
  1665. * @src: buffer holding the IV
  1666. * @len: length of the IV in bytes
  1667. *
  1668. * The caller provided IV is set for the block cipher referenced by the cipher
  1669. * handle.
  1670. */
  1671. static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
  1672. const u8 *src, unsigned int len)
  1673. {
  1674. memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
  1675. }
  1676. /**
  1677. * crypto_blkcipher_get_iv() - obtain IV from cipher
  1678. * @tfm: cipher handle
  1679. * @dst: buffer filled with the IV
  1680. * @len: length of the buffer dst
  1681. *
  1682. * The caller can obtain the IV set for the block cipher referenced by the
  1683. * cipher handle and store it into the user-provided buffer. If the buffer
  1684. * has an insufficient space, the IV is truncated to fit the buffer.
  1685. */
  1686. static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
  1687. u8 *dst, unsigned int len)
  1688. {
  1689. memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
  1690. }
  1691. /**
  1692. * DOC: Single Block Cipher API
  1693. *
  1694. * The single block cipher API is used with the ciphers of type
  1695. * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
  1696. *
  1697. * Using the single block cipher API calls, operations with the basic cipher
  1698. * primitive can be implemented. These cipher primitives exclude any block
  1699. * chaining operations including IV handling.
  1700. *
  1701. * The purpose of this single block cipher API is to support the implementation
  1702. * of templates or other concepts that only need to perform the cipher operation
  1703. * on one block at a time. Templates invoke the underlying cipher primitive
  1704. * block-wise and process either the input or the output data of these cipher
  1705. * operations.
  1706. */
  1707. static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
  1708. {
  1709. return (struct crypto_cipher *)tfm;
  1710. }
  1711. static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
  1712. {
  1713. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  1714. return __crypto_cipher_cast(tfm);
  1715. }
  1716. /**
  1717. * crypto_alloc_cipher() - allocate single block cipher handle
  1718. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1719. * single block cipher
  1720. * @type: specifies the type of the cipher
  1721. * @mask: specifies the mask for the cipher
  1722. *
  1723. * Allocate a cipher handle for a single block cipher. The returned struct
  1724. * crypto_cipher is the cipher handle that is required for any subsequent API
  1725. * invocation for that single block cipher.
  1726. *
  1727. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  1728. * of an error, PTR_ERR() returns the error code.
  1729. */
  1730. static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
  1731. u32 type, u32 mask)
  1732. {
  1733. type &= ~CRYPTO_ALG_TYPE_MASK;
  1734. type |= CRYPTO_ALG_TYPE_CIPHER;
  1735. mask |= CRYPTO_ALG_TYPE_MASK;
  1736. return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
  1737. }
  1738. static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
  1739. {
  1740. return &tfm->base;
  1741. }
  1742. /**
  1743. * crypto_free_cipher() - zeroize and free the single block cipher handle
  1744. * @tfm: cipher handle to be freed
  1745. */
  1746. static inline void crypto_free_cipher(struct crypto_cipher *tfm)
  1747. {
  1748. crypto_free_tfm(crypto_cipher_tfm(tfm));
  1749. }
  1750. /**
  1751. * crypto_has_cipher() - Search for the availability of a single block cipher
  1752. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1753. * single block cipher
  1754. * @type: specifies the type of the cipher
  1755. * @mask: specifies the mask for the cipher
  1756. *
  1757. * Return: true when the single block cipher is known to the kernel crypto API;
  1758. * false otherwise
  1759. */
  1760. static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
  1761. {
  1762. type &= ~CRYPTO_ALG_TYPE_MASK;
  1763. type |= CRYPTO_ALG_TYPE_CIPHER;
  1764. mask |= CRYPTO_ALG_TYPE_MASK;
  1765. return crypto_has_alg(alg_name, type, mask);
  1766. }
  1767. static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
  1768. {
  1769. return &crypto_cipher_tfm(tfm)->crt_cipher;
  1770. }
  1771. /**
  1772. * crypto_cipher_blocksize() - obtain block size for cipher
  1773. * @tfm: cipher handle
  1774. *
  1775. * The block size for the single block cipher referenced with the cipher handle
  1776. * tfm is returned. The caller may use that information to allocate appropriate
  1777. * memory for the data returned by the encryption or decryption operation
  1778. *
  1779. * Return: block size of cipher
  1780. */
  1781. static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
  1782. {
  1783. return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
  1784. }
  1785. static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
  1786. {
  1787. return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
  1788. }
  1789. static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
  1790. {
  1791. return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
  1792. }
  1793. static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
  1794. u32 flags)
  1795. {
  1796. crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
  1797. }
  1798. static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
  1799. u32 flags)
  1800. {
  1801. crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
  1802. }
  1803. /**
  1804. * crypto_cipher_setkey() - set key for cipher
  1805. * @tfm: cipher handle
  1806. * @key: buffer holding the key
  1807. * @keylen: length of the key in bytes
  1808. *
  1809. * The caller provided key is set for the single block cipher referenced by the
  1810. * cipher handle.
  1811. *
  1812. * Note, the key length determines the cipher type. Many block ciphers implement
  1813. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  1814. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  1815. * is performed.
  1816. *
  1817. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  1818. */
  1819. static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
  1820. const u8 *key, unsigned int keylen)
  1821. {
  1822. return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
  1823. key, keylen);
  1824. }
  1825. /**
  1826. * crypto_cipher_encrypt_one() - encrypt one block of plaintext
  1827. * @tfm: cipher handle
  1828. * @dst: points to the buffer that will be filled with the ciphertext
  1829. * @src: buffer holding the plaintext to be encrypted
  1830. *
  1831. * Invoke the encryption operation of one block. The caller must ensure that
  1832. * the plaintext and ciphertext buffers are at least one block in size.
  1833. */
  1834. static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
  1835. u8 *dst, const u8 *src)
  1836. {
  1837. crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
  1838. dst, src);
  1839. }
  1840. /**
  1841. * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
  1842. * @tfm: cipher handle
  1843. * @dst: points to the buffer that will be filled with the plaintext
  1844. * @src: buffer holding the ciphertext to be decrypted
  1845. *
  1846. * Invoke the decryption operation of one block. The caller must ensure that
  1847. * the plaintext and ciphertext buffers are at least one block in size.
  1848. */
  1849. static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
  1850. u8 *dst, const u8 *src)
  1851. {
  1852. crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
  1853. dst, src);
  1854. }
  1855. /**
  1856. * DOC: Synchronous Message Digest API
  1857. *
  1858. * The synchronous message digest API is used with the ciphers of type
  1859. * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto)
  1860. */
  1861. static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
  1862. {
  1863. return (struct crypto_hash *)tfm;
  1864. }
  1865. static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
  1866. {
  1867. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
  1868. CRYPTO_ALG_TYPE_HASH_MASK);
  1869. return __crypto_hash_cast(tfm);
  1870. }
  1871. /**
  1872. * crypto_alloc_hash() - allocate synchronous message digest handle
  1873. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1874. * message digest cipher
  1875. * @type: specifies the type of the cipher
  1876. * @mask: specifies the mask for the cipher
  1877. *
  1878. * Allocate a cipher handle for a message digest. The returned struct
  1879. * crypto_hash is the cipher handle that is required for any subsequent
  1880. * API invocation for that message digest.
  1881. *
  1882. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  1883. * of an error, PTR_ERR() returns the error code.
  1884. */
  1885. static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
  1886. u32 type, u32 mask)
  1887. {
  1888. type &= ~CRYPTO_ALG_TYPE_MASK;
  1889. mask &= ~CRYPTO_ALG_TYPE_MASK;
  1890. type |= CRYPTO_ALG_TYPE_HASH;
  1891. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  1892. return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
  1893. }
  1894. static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
  1895. {
  1896. return &tfm->base;
  1897. }
  1898. /**
  1899. * crypto_free_hash() - zeroize and free message digest handle
  1900. * @tfm: cipher handle to be freed
  1901. */
  1902. static inline void crypto_free_hash(struct crypto_hash *tfm)
  1903. {
  1904. crypto_free_tfm(crypto_hash_tfm(tfm));
  1905. }
  1906. /**
  1907. * crypto_has_hash() - Search for the availability of a message digest
  1908. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1909. * message digest cipher
  1910. * @type: specifies the type of the cipher
  1911. * @mask: specifies the mask for the cipher
  1912. *
  1913. * Return: true when the message digest cipher is known to the kernel crypto
  1914. * API; false otherwise
  1915. */
  1916. static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
  1917. {
  1918. type &= ~CRYPTO_ALG_TYPE_MASK;
  1919. mask &= ~CRYPTO_ALG_TYPE_MASK;
  1920. type |= CRYPTO_ALG_TYPE_HASH;
  1921. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  1922. return crypto_has_alg(alg_name, type, mask);
  1923. }
  1924. static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
  1925. {
  1926. return &crypto_hash_tfm(tfm)->crt_hash;
  1927. }
  1928. /**
  1929. * crypto_hash_blocksize() - obtain block size for message digest
  1930. * @tfm: cipher handle
  1931. *
  1932. * The block size for the message digest cipher referenced with the cipher
  1933. * handle is returned.
  1934. *
  1935. * Return: block size of cipher
  1936. */
  1937. static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
  1938. {
  1939. return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
  1940. }
  1941. static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
  1942. {
  1943. return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
  1944. }
  1945. /**
  1946. * crypto_hash_digestsize() - obtain message digest size
  1947. * @tfm: cipher handle
  1948. *
  1949. * The size for the message digest created by the message digest cipher
  1950. * referenced with the cipher handle is returned.
  1951. *
  1952. * Return: message digest size
  1953. */
  1954. static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
  1955. {
  1956. return crypto_hash_crt(tfm)->digestsize;
  1957. }
  1958. static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
  1959. {
  1960. return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
  1961. }
  1962. static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
  1963. {
  1964. crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
  1965. }
  1966. static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
  1967. {
  1968. crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
  1969. }
  1970. /**
  1971. * crypto_hash_init() - (re)initialize message digest handle
  1972. * @desc: cipher request handle that to be filled by caller --
  1973. * desc.tfm is filled with the hash cipher handle;
  1974. * desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0.
  1975. *
  1976. * The call (re-)initializes the message digest referenced by the hash cipher
  1977. * request handle. Any potentially existing state created by previous
  1978. * operations is discarded.
  1979. *
  1980. * Return: 0 if the message digest initialization was successful; < 0 if an
  1981. * error occurred
  1982. */
  1983. static inline int crypto_hash_init(struct hash_desc *desc)
  1984. {
  1985. return crypto_hash_crt(desc->tfm)->init(desc);
  1986. }
  1987. /**
  1988. * crypto_hash_update() - add data to message digest for processing
  1989. * @desc: cipher request handle
  1990. * @sg: scatter / gather list pointing to the data to be added to the message
  1991. * digest
  1992. * @nbytes: number of bytes to be processed from @sg
  1993. *
  1994. * Updates the message digest state of the cipher handle pointed to by the
  1995. * hash cipher request handle with the input data pointed to by the
  1996. * scatter/gather list.
  1997. *
  1998. * Return: 0 if the message digest update was successful; < 0 if an error
  1999. * occurred
  2000. */
  2001. static inline int crypto_hash_update(struct hash_desc *desc,
  2002. struct scatterlist *sg,
  2003. unsigned int nbytes)
  2004. {
  2005. return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
  2006. }
  2007. /**
  2008. * crypto_hash_final() - calculate message digest
  2009. * @desc: cipher request handle
  2010. * @out: message digest output buffer -- The caller must ensure that the out
  2011. * buffer has a sufficient size (e.g. by using the crypto_hash_digestsize
  2012. * function).
  2013. *
  2014. * Finalize the message digest operation and create the message digest
  2015. * based on all data added to the cipher handle. The message digest is placed
  2016. * into the output buffer.
  2017. *
  2018. * Return: 0 if the message digest creation was successful; < 0 if an error
  2019. * occurred
  2020. */
  2021. static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
  2022. {
  2023. return crypto_hash_crt(desc->tfm)->final(desc, out);
  2024. }
  2025. /**
  2026. * crypto_hash_digest() - calculate message digest for a buffer
  2027. * @desc: see crypto_hash_final()
  2028. * @sg: see crypto_hash_update()
  2029. * @nbytes: see crypto_hash_update()
  2030. * @out: see crypto_hash_final()
  2031. *
  2032. * This function is a "short-hand" for the function calls of crypto_hash_init,
  2033. * crypto_hash_update and crypto_hash_final. The parameters have the same
  2034. * meaning as discussed for those separate three functions.
  2035. *
  2036. * Return: 0 if the message digest creation was successful; < 0 if an error
  2037. * occurred
  2038. */
  2039. static inline int crypto_hash_digest(struct hash_desc *desc,
  2040. struct scatterlist *sg,
  2041. unsigned int nbytes, u8 *out)
  2042. {
  2043. return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
  2044. }
  2045. /**
  2046. * crypto_hash_setkey() - set key for message digest
  2047. * @hash: cipher handle
  2048. * @key: buffer holding the key
  2049. * @keylen: length of the key in bytes
  2050. *
  2051. * The caller provided key is set for the message digest cipher. The cipher
  2052. * handle must point to a keyed hash in order for this function to succeed.
  2053. *
  2054. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  2055. */
  2056. static inline int crypto_hash_setkey(struct crypto_hash *hash,
  2057. const u8 *key, unsigned int keylen)
  2058. {
  2059. return crypto_hash_crt(hash)->setkey(hash, key, keylen);
  2060. }
  2061. static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
  2062. {
  2063. return (struct crypto_comp *)tfm;
  2064. }
  2065. static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
  2066. {
  2067. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
  2068. CRYPTO_ALG_TYPE_MASK);
  2069. return __crypto_comp_cast(tfm);
  2070. }
  2071. static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
  2072. u32 type, u32 mask)
  2073. {
  2074. type &= ~CRYPTO_ALG_TYPE_MASK;
  2075. type |= CRYPTO_ALG_TYPE_COMPRESS;
  2076. mask |= CRYPTO_ALG_TYPE_MASK;
  2077. return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
  2078. }
  2079. static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
  2080. {
  2081. return &tfm->base;
  2082. }
  2083. static inline void crypto_free_comp(struct crypto_comp *tfm)
  2084. {
  2085. crypto_free_tfm(crypto_comp_tfm(tfm));
  2086. }
  2087. static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
  2088. {
  2089. type &= ~CRYPTO_ALG_TYPE_MASK;
  2090. type |= CRYPTO_ALG_TYPE_COMPRESS;
  2091. mask |= CRYPTO_ALG_TYPE_MASK;
  2092. return crypto_has_alg(alg_name, type, mask);
  2093. }
  2094. static inline const char *crypto_comp_name(struct crypto_comp *tfm)
  2095. {
  2096. return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
  2097. }
  2098. static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
  2099. {
  2100. return &crypto_comp_tfm(tfm)->crt_compress;
  2101. }
  2102. static inline int crypto_comp_compress(struct crypto_comp *tfm,
  2103. const u8 *src, unsigned int slen,
  2104. u8 *dst, unsigned int *dlen)
  2105. {
  2106. return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
  2107. src, slen, dst, dlen);
  2108. }
  2109. static inline int crypto_comp_decompress(struct crypto_comp *tfm,
  2110. const u8 *src, unsigned int slen,
  2111. u8 *dst, unsigned int *dlen)
  2112. {
  2113. return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
  2114. src, slen, dst, dlen);
  2115. }
  2116. #endif /* _LINUX_CRYPTO_H */