crypto.h 81 KB

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