crypto.h 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  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_AKCIPHER 0x0000000d
  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_blkcipher;
  121. struct crypto_hash;
  122. struct crypto_tfm;
  123. struct crypto_type;
  124. struct skcipher_givcrypt_request;
  125. typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
  126. /**
  127. * DOC: Block Cipher Context Data Structures
  128. *
  129. * These data structures define the operating context for each block cipher
  130. * type.
  131. */
  132. struct crypto_async_request {
  133. struct list_head list;
  134. crypto_completion_t complete;
  135. void *data;
  136. struct crypto_tfm *tfm;
  137. u32 flags;
  138. };
  139. struct ablkcipher_request {
  140. struct crypto_async_request base;
  141. unsigned int nbytes;
  142. void *info;
  143. struct scatterlist *src;
  144. struct scatterlist *dst;
  145. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  146. };
  147. struct blkcipher_desc {
  148. struct crypto_blkcipher *tfm;
  149. void *info;
  150. u32 flags;
  151. };
  152. struct cipher_desc {
  153. struct crypto_tfm *tfm;
  154. void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  155. unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
  156. const u8 *src, unsigned int nbytes);
  157. void *info;
  158. };
  159. struct hash_desc {
  160. struct crypto_hash *tfm;
  161. u32 flags;
  162. };
  163. /**
  164. * DOC: Block Cipher Algorithm Definitions
  165. *
  166. * These data structures define modular crypto algorithm implementations,
  167. * managed via crypto_register_alg() and crypto_unregister_alg().
  168. */
  169. /**
  170. * struct ablkcipher_alg - asynchronous block cipher definition
  171. * @min_keysize: Minimum key size supported by the transformation. This is the
  172. * smallest key length supported by this transformation algorithm.
  173. * This must be set to one of the pre-defined values as this is
  174. * not hardware specific. Possible values for this field can be
  175. * found via git grep "_MIN_KEY_SIZE" include/crypto/
  176. * @max_keysize: Maximum key size supported by the transformation. This is the
  177. * largest key length supported by this transformation algorithm.
  178. * This must be set to one of the pre-defined values as this is
  179. * not hardware specific. Possible values for this field can be
  180. * found via git grep "_MAX_KEY_SIZE" include/crypto/
  181. * @setkey: Set key for the transformation. This function is used to either
  182. * program a supplied key into the hardware or store the key in the
  183. * transformation context for programming it later. Note that this
  184. * function does modify the transformation context. This function can
  185. * be called multiple times during the existence of the transformation
  186. * object, so one must make sure the key is properly reprogrammed into
  187. * the hardware. This function is also responsible for checking the key
  188. * length for validity. In case a software fallback was put in place in
  189. * the @cra_init call, this function might need to use the fallback if
  190. * the algorithm doesn't support all of the key sizes.
  191. * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
  192. * the supplied scatterlist containing the blocks of data. The crypto
  193. * API consumer is responsible for aligning the entries of the
  194. * scatterlist properly and making sure the chunks are correctly
  195. * sized. In case a software fallback was put in place in the
  196. * @cra_init call, this function might need to use the fallback if
  197. * the algorithm doesn't support all of the key sizes. In case the
  198. * key was stored in transformation context, the key might need to be
  199. * re-programmed into the hardware in this function. This function
  200. * shall not modify the transformation context, as this function may
  201. * be called in parallel with the same transformation object.
  202. * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
  203. * and the conditions are exactly the same.
  204. * @givencrypt: Update the IV for encryption. With this function, a cipher
  205. * implementation may provide the function on how to update the IV
  206. * for encryption.
  207. * @givdecrypt: Update the IV for decryption. This is the reverse of
  208. * @givencrypt .
  209. * @geniv: The transformation implementation may use an "IV generator" provided
  210. * by the kernel crypto API. Several use cases have a predefined
  211. * approach how IVs are to be updated. For such use cases, the kernel
  212. * crypto API provides ready-to-use implementations that can be
  213. * referenced with this variable.
  214. * @ivsize: IV size applicable for transformation. The consumer must provide an
  215. * IV of exactly that size to perform the encrypt or decrypt operation.
  216. *
  217. * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
  218. * mandatory and must be filled.
  219. */
  220. struct ablkcipher_alg {
  221. int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
  222. unsigned int keylen);
  223. int (*encrypt)(struct ablkcipher_request *req);
  224. int (*decrypt)(struct ablkcipher_request *req);
  225. int (*givencrypt)(struct skcipher_givcrypt_request *req);
  226. int (*givdecrypt)(struct skcipher_givcrypt_request *req);
  227. const char *geniv;
  228. unsigned int min_keysize;
  229. unsigned int max_keysize;
  230. unsigned int ivsize;
  231. };
  232. /**
  233. * struct blkcipher_alg - synchronous block cipher definition
  234. * @min_keysize: see struct ablkcipher_alg
  235. * @max_keysize: see struct ablkcipher_alg
  236. * @setkey: see struct ablkcipher_alg
  237. * @encrypt: see struct ablkcipher_alg
  238. * @decrypt: see struct ablkcipher_alg
  239. * @geniv: see struct ablkcipher_alg
  240. * @ivsize: see struct ablkcipher_alg
  241. *
  242. * All fields except @geniv and @ivsize are mandatory and must be filled.
  243. */
  244. struct blkcipher_alg {
  245. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  246. unsigned int keylen);
  247. int (*encrypt)(struct blkcipher_desc *desc,
  248. struct scatterlist *dst, struct scatterlist *src,
  249. unsigned int nbytes);
  250. int (*decrypt)(struct blkcipher_desc *desc,
  251. struct scatterlist *dst, struct scatterlist *src,
  252. unsigned int nbytes);
  253. const char *geniv;
  254. unsigned int min_keysize;
  255. unsigned int max_keysize;
  256. unsigned int ivsize;
  257. };
  258. /**
  259. * struct cipher_alg - single-block symmetric ciphers definition
  260. * @cia_min_keysize: Minimum key size supported by the transformation. This is
  261. * the smallest key length supported by this transformation
  262. * algorithm. This must be set to one of the pre-defined
  263. * values as this is not hardware specific. Possible values
  264. * for this field can be found via git grep "_MIN_KEY_SIZE"
  265. * include/crypto/
  266. * @cia_max_keysize: Maximum key size supported by the transformation. This is
  267. * the largest key length supported by this transformation
  268. * algorithm. This must be set to one of the pre-defined values
  269. * as this is not hardware specific. Possible values for this
  270. * field can be found via git grep "_MAX_KEY_SIZE"
  271. * include/crypto/
  272. * @cia_setkey: Set key for the transformation. This function is used to either
  273. * program a supplied key into the hardware or store the key in the
  274. * transformation context for programming it later. Note that this
  275. * function does modify the transformation context. This function
  276. * can be called multiple times during the existence of the
  277. * transformation object, so one must make sure the key is properly
  278. * reprogrammed into the hardware. This function is also
  279. * responsible for checking the key length for validity.
  280. * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
  281. * single block of data, which must be @cra_blocksize big. This
  282. * always operates on a full @cra_blocksize and it is not possible
  283. * to encrypt a block of smaller size. The supplied buffers must
  284. * therefore also be at least of @cra_blocksize size. Both the
  285. * input and output buffers are always aligned to @cra_alignmask.
  286. * In case either of the input or output buffer supplied by user
  287. * of the crypto API is not aligned to @cra_alignmask, the crypto
  288. * API will re-align the buffers. The re-alignment means that a
  289. * new buffer will be allocated, the data will be copied into the
  290. * new buffer, then the processing will happen on the new buffer,
  291. * then the data will be copied back into the original buffer and
  292. * finally the new buffer will be freed. In case a software
  293. * fallback was put in place in the @cra_init call, this function
  294. * might need to use the fallback if the algorithm doesn't support
  295. * all of the key sizes. In case the key was stored in
  296. * transformation context, the key might need to be re-programmed
  297. * into the hardware in this function. This function shall not
  298. * modify the transformation context, as this function may be
  299. * called in parallel with the same transformation object.
  300. * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
  301. * @cia_encrypt, and the conditions are exactly the same.
  302. *
  303. * All fields are mandatory and must be filled.
  304. */
  305. struct cipher_alg {
  306. unsigned int cia_min_keysize;
  307. unsigned int cia_max_keysize;
  308. int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  309. unsigned int keylen);
  310. void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  311. void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  312. };
  313. struct compress_alg {
  314. int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
  315. unsigned int slen, u8 *dst, unsigned int *dlen);
  316. int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
  317. unsigned int slen, u8 *dst, unsigned int *dlen);
  318. };
  319. #define cra_ablkcipher cra_u.ablkcipher
  320. #define cra_blkcipher cra_u.blkcipher
  321. #define cra_cipher cra_u.cipher
  322. #define cra_compress cra_u.compress
  323. /**
  324. * struct crypto_alg - definition of a cryptograpic cipher algorithm
  325. * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
  326. * CRYPTO_ALG_* flags for the flags which go in here. Those are
  327. * used for fine-tuning the description of the transformation
  328. * algorithm.
  329. * @cra_blocksize: Minimum block size of this transformation. The size in bytes
  330. * of the smallest possible unit which can be transformed with
  331. * this algorithm. The users must respect this value.
  332. * In case of HASH transformation, it is possible for a smaller
  333. * block than @cra_blocksize to be passed to the crypto API for
  334. * transformation, in case of any other transformation type, an
  335. * error will be returned upon any attempt to transform smaller
  336. * than @cra_blocksize chunks.
  337. * @cra_ctxsize: Size of the operational context of the transformation. This
  338. * value informs the kernel crypto API about the memory size
  339. * needed to be allocated for the transformation context.
  340. * @cra_alignmask: Alignment mask for the input and output data buffer. The data
  341. * buffer containing the input data for the algorithm must be
  342. * aligned to this alignment mask. The data buffer for the
  343. * output data must be aligned to this alignment mask. Note that
  344. * the Crypto API will do the re-alignment in software, but
  345. * only under special conditions and there is a performance hit.
  346. * The re-alignment happens at these occasions for different
  347. * @cra_u types: cipher -- For both input data and output data
  348. * buffer; ahash -- For output hash destination buf; shash --
  349. * For output hash destination buf.
  350. * This is needed on hardware which is flawed by design and
  351. * cannot pick data from arbitrary addresses.
  352. * @cra_priority: Priority of this transformation implementation. In case
  353. * multiple transformations with same @cra_name are available to
  354. * the Crypto API, the kernel will use the one with highest
  355. * @cra_priority.
  356. * @cra_name: Generic name (usable by multiple implementations) of the
  357. * transformation algorithm. This is the name of the transformation
  358. * itself. This field is used by the kernel when looking up the
  359. * providers of particular transformation.
  360. * @cra_driver_name: Unique name of the transformation provider. This is the
  361. * name of the provider of the transformation. This can be any
  362. * arbitrary value, but in the usual case, this contains the
  363. * name of the chip or provider and the name of the
  364. * transformation algorithm.
  365. * @cra_type: Type of the cryptographic transformation. This is a pointer to
  366. * struct crypto_type, which implements callbacks common for all
  367. * transformation types. There are multiple options:
  368. * &crypto_blkcipher_type, &crypto_ablkcipher_type,
  369. * &crypto_ahash_type, &crypto_rng_type.
  370. * This field might be empty. In that case, there are no common
  371. * callbacks. This is the case for: cipher, compress, shash.
  372. * @cra_u: Callbacks implementing the transformation. This is a union of
  373. * multiple structures. Depending on the type of transformation selected
  374. * by @cra_type and @cra_flags above, the associated structure must be
  375. * filled with callbacks. This field might be empty. This is the case
  376. * for ahash, shash.
  377. * @cra_init: Initialize the cryptographic transformation object. This function
  378. * is used to initialize the cryptographic transformation object.
  379. * This function is called only once at the instantiation time, right
  380. * after the transformation context was allocated. In case the
  381. * cryptographic hardware has some special requirements which need to
  382. * be handled by software, this function shall check for the precise
  383. * requirement of the transformation and put any software fallbacks
  384. * in place.
  385. * @cra_exit: Deinitialize the cryptographic transformation object. This is a
  386. * counterpart to @cra_init, used to remove various changes set in
  387. * @cra_init.
  388. * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
  389. * @cra_list: internally used
  390. * @cra_users: internally used
  391. * @cra_refcnt: internally used
  392. * @cra_destroy: internally used
  393. *
  394. * The struct crypto_alg describes a generic Crypto API algorithm and is common
  395. * for all of the transformations. Any variable not documented here shall not
  396. * be used by a cipher implementation as it is internal to the Crypto API.
  397. */
  398. struct crypto_alg {
  399. struct list_head cra_list;
  400. struct list_head cra_users;
  401. u32 cra_flags;
  402. unsigned int cra_blocksize;
  403. unsigned int cra_ctxsize;
  404. unsigned int cra_alignmask;
  405. int cra_priority;
  406. atomic_t cra_refcnt;
  407. char cra_name[CRYPTO_MAX_ALG_NAME];
  408. char cra_driver_name[CRYPTO_MAX_ALG_NAME];
  409. const struct crypto_type *cra_type;
  410. union {
  411. struct ablkcipher_alg ablkcipher;
  412. struct blkcipher_alg blkcipher;
  413. struct cipher_alg cipher;
  414. struct compress_alg compress;
  415. } cra_u;
  416. int (*cra_init)(struct crypto_tfm *tfm);
  417. void (*cra_exit)(struct crypto_tfm *tfm);
  418. void (*cra_destroy)(struct crypto_alg *alg);
  419. struct module *cra_module;
  420. } CRYPTO_MINALIGN_ATTR;
  421. /*
  422. * Algorithm registration interface.
  423. */
  424. int crypto_register_alg(struct crypto_alg *alg);
  425. int crypto_unregister_alg(struct crypto_alg *alg);
  426. int crypto_register_algs(struct crypto_alg *algs, int count);
  427. int crypto_unregister_algs(struct crypto_alg *algs, int count);
  428. /*
  429. * Algorithm query interface.
  430. */
  431. int crypto_has_alg(const char *name, u32 type, u32 mask);
  432. /*
  433. * Transforms: user-instantiated objects which encapsulate algorithms
  434. * and core processing logic. Managed via crypto_alloc_*() and
  435. * crypto_free_*(), as well as the various helpers below.
  436. */
  437. struct ablkcipher_tfm {
  438. int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
  439. unsigned int keylen);
  440. int (*encrypt)(struct ablkcipher_request *req);
  441. int (*decrypt)(struct ablkcipher_request *req);
  442. int (*givencrypt)(struct skcipher_givcrypt_request *req);
  443. int (*givdecrypt)(struct skcipher_givcrypt_request *req);
  444. struct crypto_ablkcipher *base;
  445. unsigned int ivsize;
  446. unsigned int reqsize;
  447. };
  448. struct blkcipher_tfm {
  449. void *iv;
  450. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  451. unsigned int keylen);
  452. int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  453. struct scatterlist *src, unsigned int nbytes);
  454. int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  455. struct scatterlist *src, unsigned int nbytes);
  456. };
  457. struct cipher_tfm {
  458. int (*cit_setkey)(struct crypto_tfm *tfm,
  459. const u8 *key, unsigned int keylen);
  460. void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  461. void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  462. };
  463. struct hash_tfm {
  464. int (*init)(struct hash_desc *desc);
  465. int (*update)(struct hash_desc *desc,
  466. struct scatterlist *sg, unsigned int nsg);
  467. int (*final)(struct hash_desc *desc, u8 *out);
  468. int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
  469. unsigned int nsg, u8 *out);
  470. int (*setkey)(struct crypto_hash *tfm, const u8 *key,
  471. unsigned int keylen);
  472. unsigned int digestsize;
  473. };
  474. struct compress_tfm {
  475. int (*cot_compress)(struct crypto_tfm *tfm,
  476. const u8 *src, unsigned int slen,
  477. u8 *dst, unsigned int *dlen);
  478. int (*cot_decompress)(struct crypto_tfm *tfm,
  479. const u8 *src, unsigned int slen,
  480. u8 *dst, unsigned int *dlen);
  481. };
  482. #define crt_ablkcipher crt_u.ablkcipher
  483. #define crt_blkcipher crt_u.blkcipher
  484. #define crt_cipher crt_u.cipher
  485. #define crt_hash crt_u.hash
  486. #define crt_compress crt_u.compress
  487. struct crypto_tfm {
  488. u32 crt_flags;
  489. union {
  490. struct ablkcipher_tfm ablkcipher;
  491. struct blkcipher_tfm blkcipher;
  492. struct cipher_tfm cipher;
  493. struct hash_tfm hash;
  494. struct compress_tfm compress;
  495. } crt_u;
  496. void (*exit)(struct crypto_tfm *tfm);
  497. struct crypto_alg *__crt_alg;
  498. void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
  499. };
  500. struct crypto_ablkcipher {
  501. struct crypto_tfm base;
  502. };
  503. struct crypto_blkcipher {
  504. struct crypto_tfm base;
  505. };
  506. struct crypto_cipher {
  507. struct crypto_tfm base;
  508. };
  509. struct crypto_comp {
  510. struct crypto_tfm base;
  511. };
  512. struct crypto_hash {
  513. struct crypto_tfm base;
  514. };
  515. enum {
  516. CRYPTOA_UNSPEC,
  517. CRYPTOA_ALG,
  518. CRYPTOA_TYPE,
  519. CRYPTOA_U32,
  520. __CRYPTOA_MAX,
  521. };
  522. #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
  523. /* Maximum number of (rtattr) parameters for each template. */
  524. #define CRYPTO_MAX_ATTRS 32
  525. struct crypto_attr_alg {
  526. char name[CRYPTO_MAX_ALG_NAME];
  527. };
  528. struct crypto_attr_type {
  529. u32 type;
  530. u32 mask;
  531. };
  532. struct crypto_attr_u32 {
  533. u32 num;
  534. };
  535. /*
  536. * Transform user interface.
  537. */
  538. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
  539. void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
  540. static inline void crypto_free_tfm(struct crypto_tfm *tfm)
  541. {
  542. return crypto_destroy_tfm(tfm, tfm);
  543. }
  544. int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
  545. /*
  546. * Transform helpers which query the underlying algorithm.
  547. */
  548. static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
  549. {
  550. return tfm->__crt_alg->cra_name;
  551. }
  552. static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
  553. {
  554. return tfm->__crt_alg->cra_driver_name;
  555. }
  556. static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
  557. {
  558. return tfm->__crt_alg->cra_priority;
  559. }
  560. static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
  561. {
  562. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
  563. }
  564. static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
  565. {
  566. return tfm->__crt_alg->cra_blocksize;
  567. }
  568. static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
  569. {
  570. return tfm->__crt_alg->cra_alignmask;
  571. }
  572. static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
  573. {
  574. return tfm->crt_flags;
  575. }
  576. static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
  577. {
  578. tfm->crt_flags |= flags;
  579. }
  580. static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
  581. {
  582. tfm->crt_flags &= ~flags;
  583. }
  584. static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
  585. {
  586. return tfm->__crt_ctx;
  587. }
  588. static inline unsigned int crypto_tfm_ctx_alignment(void)
  589. {
  590. struct crypto_tfm *tfm;
  591. return __alignof__(tfm->__crt_ctx);
  592. }
  593. /*
  594. * API wrappers.
  595. */
  596. static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
  597. struct crypto_tfm *tfm)
  598. {
  599. return (struct crypto_ablkcipher *)tfm;
  600. }
  601. static inline u32 crypto_skcipher_type(u32 type)
  602. {
  603. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  604. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  605. return type;
  606. }
  607. static inline u32 crypto_skcipher_mask(u32 mask)
  608. {
  609. mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  610. mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
  611. return mask;
  612. }
  613. /**
  614. * DOC: Asynchronous Block Cipher API
  615. *
  616. * Asynchronous block cipher API is used with the ciphers of type
  617. * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
  618. *
  619. * Asynchronous cipher operations imply that the function invocation for a
  620. * cipher request returns immediately before the completion of the operation.
  621. * The cipher request is scheduled as a separate kernel thread and therefore
  622. * load-balanced on the different CPUs via the process scheduler. To allow
  623. * the kernel crypto API to inform the caller about the completion of a cipher
  624. * request, the caller must provide a callback function. That function is
  625. * invoked with the cipher handle when the request completes.
  626. *
  627. * To support the asynchronous operation, additional information than just the
  628. * cipher handle must be supplied to the kernel crypto API. That additional
  629. * information is given by filling in the ablkcipher_request data structure.
  630. *
  631. * For the asynchronous block cipher API, the state is maintained with the tfm
  632. * cipher handle. A single tfm can be used across multiple calls and in
  633. * parallel. For asynchronous block cipher calls, context data supplied and
  634. * only used by the caller can be referenced the request data structure in
  635. * addition to the IV used for the cipher request. The maintenance of such
  636. * state information would be important for a crypto driver implementer to
  637. * have, because when calling the callback function upon completion of the
  638. * cipher operation, that callback function may need some information about
  639. * which operation just finished if it invoked multiple in parallel. This
  640. * state information is unused by the kernel crypto API.
  641. */
  642. /**
  643. * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
  644. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  645. * ablkcipher cipher
  646. * @type: specifies the type of the cipher
  647. * @mask: specifies the mask for the cipher
  648. *
  649. * Allocate a cipher handle for an ablkcipher. The returned struct
  650. * crypto_ablkcipher is the cipher handle that is required for any subsequent
  651. * API invocation for that ablkcipher.
  652. *
  653. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  654. * of an error, PTR_ERR() returns the error code.
  655. */
  656. struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
  657. u32 type, u32 mask);
  658. static inline struct crypto_tfm *crypto_ablkcipher_tfm(
  659. struct crypto_ablkcipher *tfm)
  660. {
  661. return &tfm->base;
  662. }
  663. /**
  664. * crypto_free_ablkcipher() - zeroize and free cipher handle
  665. * @tfm: cipher handle to be freed
  666. */
  667. static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
  668. {
  669. crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
  670. }
  671. /**
  672. * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
  673. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  674. * ablkcipher
  675. * @type: specifies the type of the cipher
  676. * @mask: specifies the mask for the cipher
  677. *
  678. * Return: true when the ablkcipher is known to the kernel crypto API; false
  679. * otherwise
  680. */
  681. static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
  682. u32 mask)
  683. {
  684. return crypto_has_alg(alg_name, crypto_skcipher_type(type),
  685. crypto_skcipher_mask(mask));
  686. }
  687. static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
  688. struct crypto_ablkcipher *tfm)
  689. {
  690. return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
  691. }
  692. /**
  693. * crypto_ablkcipher_ivsize() - obtain IV size
  694. * @tfm: cipher handle
  695. *
  696. * The size of the IV for the ablkcipher referenced by the cipher handle is
  697. * returned. This IV size may be zero if the cipher does not need an IV.
  698. *
  699. * Return: IV size in bytes
  700. */
  701. static inline unsigned int crypto_ablkcipher_ivsize(
  702. struct crypto_ablkcipher *tfm)
  703. {
  704. return crypto_ablkcipher_crt(tfm)->ivsize;
  705. }
  706. /**
  707. * crypto_ablkcipher_blocksize() - obtain block size of cipher
  708. * @tfm: cipher handle
  709. *
  710. * The block size for the ablkcipher referenced with the cipher handle is
  711. * returned. The caller may use that information to allocate appropriate
  712. * memory for the data returned by the encryption or decryption operation
  713. *
  714. * Return: block size of cipher
  715. */
  716. static inline unsigned int crypto_ablkcipher_blocksize(
  717. struct crypto_ablkcipher *tfm)
  718. {
  719. return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
  720. }
  721. static inline unsigned int crypto_ablkcipher_alignmask(
  722. struct crypto_ablkcipher *tfm)
  723. {
  724. return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
  725. }
  726. static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
  727. {
  728. return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
  729. }
  730. static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
  731. u32 flags)
  732. {
  733. crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
  734. }
  735. static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
  736. u32 flags)
  737. {
  738. crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
  739. }
  740. /**
  741. * crypto_ablkcipher_setkey() - set key for cipher
  742. * @tfm: cipher handle
  743. * @key: buffer holding the key
  744. * @keylen: length of the key in bytes
  745. *
  746. * The caller provided key is set for the ablkcipher referenced by the cipher
  747. * handle.
  748. *
  749. * Note, the key length determines the cipher type. Many block ciphers implement
  750. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  751. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  752. * is performed.
  753. *
  754. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  755. */
  756. static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
  757. const u8 *key, unsigned int keylen)
  758. {
  759. struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
  760. return crt->setkey(crt->base, key, keylen);
  761. }
  762. /**
  763. * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
  764. * @req: ablkcipher_request out of which the cipher handle is to be obtained
  765. *
  766. * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
  767. * data structure.
  768. *
  769. * Return: crypto_ablkcipher handle
  770. */
  771. static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
  772. struct ablkcipher_request *req)
  773. {
  774. return __crypto_ablkcipher_cast(req->base.tfm);
  775. }
  776. /**
  777. * crypto_ablkcipher_encrypt() - encrypt plaintext
  778. * @req: reference to the ablkcipher_request handle that holds all information
  779. * needed to perform the cipher operation
  780. *
  781. * Encrypt plaintext data using the ablkcipher_request handle. That data
  782. * structure and how it is filled with data is discussed with the
  783. * ablkcipher_request_* functions.
  784. *
  785. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  786. */
  787. static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
  788. {
  789. struct ablkcipher_tfm *crt =
  790. crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
  791. return crt->encrypt(req);
  792. }
  793. /**
  794. * crypto_ablkcipher_decrypt() - decrypt ciphertext
  795. * @req: reference to the ablkcipher_request handle that holds all information
  796. * needed to perform the cipher operation
  797. *
  798. * Decrypt ciphertext data using the ablkcipher_request handle. That data
  799. * structure and how it is filled with data is discussed with the
  800. * ablkcipher_request_* functions.
  801. *
  802. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  803. */
  804. static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
  805. {
  806. struct ablkcipher_tfm *crt =
  807. crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
  808. return crt->decrypt(req);
  809. }
  810. /**
  811. * DOC: Asynchronous Cipher Request Handle
  812. *
  813. * The ablkcipher_request data structure contains all pointers to data
  814. * required for the asynchronous cipher operation. This includes the cipher
  815. * handle (which can be used by multiple ablkcipher_request instances), pointer
  816. * to plaintext and ciphertext, asynchronous callback function, etc. It acts
  817. * as a handle to the ablkcipher_request_* API calls in a similar way as
  818. * ablkcipher handle to the crypto_ablkcipher_* API calls.
  819. */
  820. /**
  821. * crypto_ablkcipher_reqsize() - obtain size of the request data structure
  822. * @tfm: cipher handle
  823. *
  824. * Return: number of bytes
  825. */
  826. static inline unsigned int crypto_ablkcipher_reqsize(
  827. struct crypto_ablkcipher *tfm)
  828. {
  829. return crypto_ablkcipher_crt(tfm)->reqsize;
  830. }
  831. /**
  832. * ablkcipher_request_set_tfm() - update cipher handle reference in request
  833. * @req: request handle to be modified
  834. * @tfm: cipher handle that shall be added to the request handle
  835. *
  836. * Allow the caller to replace the existing ablkcipher handle in the request
  837. * data structure with a different one.
  838. */
  839. static inline void ablkcipher_request_set_tfm(
  840. struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
  841. {
  842. req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
  843. }
  844. static inline struct ablkcipher_request *ablkcipher_request_cast(
  845. struct crypto_async_request *req)
  846. {
  847. return container_of(req, struct ablkcipher_request, base);
  848. }
  849. /**
  850. * ablkcipher_request_alloc() - allocate request data structure
  851. * @tfm: cipher handle to be registered with the request
  852. * @gfp: memory allocation flag that is handed to kmalloc by the API call.
  853. *
  854. * Allocate the request data structure that must be used with the ablkcipher
  855. * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
  856. * handle is registered in the request data structure.
  857. *
  858. * Return: allocated request handle in case of success; IS_ERR() is true in case
  859. * of an error, PTR_ERR() returns the error code.
  860. */
  861. static inline struct ablkcipher_request *ablkcipher_request_alloc(
  862. struct crypto_ablkcipher *tfm, gfp_t gfp)
  863. {
  864. struct ablkcipher_request *req;
  865. req = kmalloc(sizeof(struct ablkcipher_request) +
  866. crypto_ablkcipher_reqsize(tfm), gfp);
  867. if (likely(req))
  868. ablkcipher_request_set_tfm(req, tfm);
  869. return req;
  870. }
  871. /**
  872. * ablkcipher_request_free() - zeroize and free request data structure
  873. * @req: request data structure cipher handle to be freed
  874. */
  875. static inline void ablkcipher_request_free(struct ablkcipher_request *req)
  876. {
  877. kzfree(req);
  878. }
  879. /**
  880. * ablkcipher_request_set_callback() - set asynchronous callback function
  881. * @req: request handle
  882. * @flags: specify zero or an ORing of the flags
  883. * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
  884. * increase the wait queue beyond the initial maximum size;
  885. * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
  886. * @compl: callback function pointer to be registered with the request handle
  887. * @data: The data pointer refers to memory that is not used by the kernel
  888. * crypto API, but provided to the callback function for it to use. Here,
  889. * the caller can provide a reference to memory the callback function can
  890. * operate on. As the callback function is invoked asynchronously to the
  891. * related functionality, it may need to access data structures of the
  892. * related functionality which can be referenced using this pointer. The
  893. * callback function can access the memory via the "data" field in the
  894. * crypto_async_request data structure provided to the callback function.
  895. *
  896. * This function allows setting the callback function that is triggered once the
  897. * cipher operation completes.
  898. *
  899. * The callback function is registered with the ablkcipher_request handle and
  900. * must comply with the following template
  901. *
  902. * void callback_function(struct crypto_async_request *req, int error)
  903. */
  904. static inline void ablkcipher_request_set_callback(
  905. struct ablkcipher_request *req,
  906. u32 flags, crypto_completion_t compl, void *data)
  907. {
  908. req->base.complete = compl;
  909. req->base.data = data;
  910. req->base.flags = flags;
  911. }
  912. /**
  913. * ablkcipher_request_set_crypt() - set data buffers
  914. * @req: request handle
  915. * @src: source scatter / gather list
  916. * @dst: destination scatter / gather list
  917. * @nbytes: number of bytes to process from @src
  918. * @iv: IV for the cipher operation which must comply with the IV size defined
  919. * by crypto_ablkcipher_ivsize
  920. *
  921. * This function allows setting of the source data and destination data
  922. * scatter / gather lists.
  923. *
  924. * For encryption, the source is treated as the plaintext and the
  925. * destination is the ciphertext. For a decryption operation, the use is
  926. * reversed - the source is the ciphertext and the destination is the plaintext.
  927. */
  928. static inline void ablkcipher_request_set_crypt(
  929. struct ablkcipher_request *req,
  930. struct scatterlist *src, struct scatterlist *dst,
  931. unsigned int nbytes, void *iv)
  932. {
  933. req->src = src;
  934. req->dst = dst;
  935. req->nbytes = nbytes;
  936. req->info = iv;
  937. }
  938. /**
  939. * DOC: Synchronous Block Cipher API
  940. *
  941. * The synchronous block cipher API is used with the ciphers of type
  942. * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
  943. *
  944. * Synchronous calls, have a context in the tfm. But since a single tfm can be
  945. * used in multiple calls and in parallel, this info should not be changeable
  946. * (unless a lock is used). This applies, for example, to the symmetric key.
  947. * However, the IV is changeable, so there is an iv field in blkcipher_tfm
  948. * structure for synchronous blkcipher api. So, its the only state info that can
  949. * be kept for synchronous calls without using a big lock across a tfm.
  950. *
  951. * The block cipher API allows the use of a complete cipher, i.e. a cipher
  952. * consisting of a template (a block chaining mode) and a single block cipher
  953. * primitive (e.g. AES).
  954. *
  955. * The plaintext data buffer and the ciphertext data buffer are pointed to
  956. * by using scatter/gather lists. The cipher operation is performed
  957. * on all segments of the provided scatter/gather lists.
  958. *
  959. * The kernel crypto API supports a cipher operation "in-place" which means that
  960. * the caller may provide the same scatter/gather list for the plaintext and
  961. * cipher text. After the completion of the cipher operation, the plaintext
  962. * data is replaced with the ciphertext data in case of an encryption and vice
  963. * versa for a decryption. The caller must ensure that the scatter/gather lists
  964. * for the output data point to sufficiently large buffers, i.e. multiples of
  965. * the block size of the cipher.
  966. */
  967. static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
  968. struct crypto_tfm *tfm)
  969. {
  970. return (struct crypto_blkcipher *)tfm;
  971. }
  972. static inline struct crypto_blkcipher *crypto_blkcipher_cast(
  973. struct crypto_tfm *tfm)
  974. {
  975. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
  976. return __crypto_blkcipher_cast(tfm);
  977. }
  978. /**
  979. * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
  980. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  981. * blkcipher cipher
  982. * @type: specifies the type of the cipher
  983. * @mask: specifies the mask for the cipher
  984. *
  985. * Allocate a cipher handle for a block cipher. The returned struct
  986. * crypto_blkcipher is the cipher handle that is required for any subsequent
  987. * API invocation for that block cipher.
  988. *
  989. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  990. * of an error, PTR_ERR() returns the error code.
  991. */
  992. static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
  993. const char *alg_name, u32 type, u32 mask)
  994. {
  995. type &= ~CRYPTO_ALG_TYPE_MASK;
  996. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  997. mask |= CRYPTO_ALG_TYPE_MASK;
  998. return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
  999. }
  1000. static inline struct crypto_tfm *crypto_blkcipher_tfm(
  1001. struct crypto_blkcipher *tfm)
  1002. {
  1003. return &tfm->base;
  1004. }
  1005. /**
  1006. * crypto_free_blkcipher() - zeroize and free the block cipher handle
  1007. * @tfm: cipher handle to be freed
  1008. */
  1009. static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
  1010. {
  1011. crypto_free_tfm(crypto_blkcipher_tfm(tfm));
  1012. }
  1013. /**
  1014. * crypto_has_blkcipher() - Search for the availability of a block cipher
  1015. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1016. * block cipher
  1017. * @type: specifies the type of the cipher
  1018. * @mask: specifies the mask for the cipher
  1019. *
  1020. * Return: true when the block cipher is known to the kernel crypto API; false
  1021. * otherwise
  1022. */
  1023. static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
  1024. {
  1025. type &= ~CRYPTO_ALG_TYPE_MASK;
  1026. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  1027. mask |= CRYPTO_ALG_TYPE_MASK;
  1028. return crypto_has_alg(alg_name, type, mask);
  1029. }
  1030. /**
  1031. * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
  1032. * @tfm: cipher handle
  1033. *
  1034. * Return: The character string holding the name of the cipher
  1035. */
  1036. static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
  1037. {
  1038. return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
  1039. }
  1040. static inline struct blkcipher_tfm *crypto_blkcipher_crt(
  1041. struct crypto_blkcipher *tfm)
  1042. {
  1043. return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
  1044. }
  1045. static inline struct blkcipher_alg *crypto_blkcipher_alg(
  1046. struct crypto_blkcipher *tfm)
  1047. {
  1048. return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
  1049. }
  1050. /**
  1051. * crypto_blkcipher_ivsize() - obtain IV size
  1052. * @tfm: cipher handle
  1053. *
  1054. * The size of the IV for the block cipher referenced by the cipher handle is
  1055. * returned. This IV size may be zero if the cipher does not need an IV.
  1056. *
  1057. * Return: IV size in bytes
  1058. */
  1059. static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
  1060. {
  1061. return crypto_blkcipher_alg(tfm)->ivsize;
  1062. }
  1063. /**
  1064. * crypto_blkcipher_blocksize() - obtain block size of cipher
  1065. * @tfm: cipher handle
  1066. *
  1067. * The block size for the block cipher referenced with the cipher handle is
  1068. * returned. The caller may use that information to allocate appropriate
  1069. * memory for the data returned by the encryption or decryption operation.
  1070. *
  1071. * Return: block size of cipher
  1072. */
  1073. static inline unsigned int crypto_blkcipher_blocksize(
  1074. struct crypto_blkcipher *tfm)
  1075. {
  1076. return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
  1077. }
  1078. static inline unsigned int crypto_blkcipher_alignmask(
  1079. struct crypto_blkcipher *tfm)
  1080. {
  1081. return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
  1082. }
  1083. static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
  1084. {
  1085. return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
  1086. }
  1087. static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
  1088. u32 flags)
  1089. {
  1090. crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
  1091. }
  1092. static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
  1093. u32 flags)
  1094. {
  1095. crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
  1096. }
  1097. /**
  1098. * crypto_blkcipher_setkey() - set key for cipher
  1099. * @tfm: cipher handle
  1100. * @key: buffer holding the key
  1101. * @keylen: length of the key in bytes
  1102. *
  1103. * The caller provided key is set for the block cipher referenced by the cipher
  1104. * handle.
  1105. *
  1106. * Note, the key length determines the cipher type. Many block ciphers implement
  1107. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  1108. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  1109. * is performed.
  1110. *
  1111. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  1112. */
  1113. static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
  1114. const u8 *key, unsigned int keylen)
  1115. {
  1116. return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
  1117. key, keylen);
  1118. }
  1119. /**
  1120. * crypto_blkcipher_encrypt() - encrypt plaintext
  1121. * @desc: reference to the block cipher handle with meta data
  1122. * @dst: scatter/gather list that is filled by the cipher operation with the
  1123. * ciphertext
  1124. * @src: scatter/gather list that holds the plaintext
  1125. * @nbytes: number of bytes of the plaintext to encrypt.
  1126. *
  1127. * Encrypt plaintext data using the IV set by the caller with a preceding
  1128. * call of crypto_blkcipher_set_iv.
  1129. *
  1130. * The blkcipher_desc data structure must be filled by the caller and can
  1131. * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
  1132. * with the block cipher handle; desc.flags is filled with either
  1133. * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
  1134. *
  1135. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  1136. */
  1137. static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
  1138. struct scatterlist *dst,
  1139. struct scatterlist *src,
  1140. unsigned int nbytes)
  1141. {
  1142. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  1143. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  1144. }
  1145. /**
  1146. * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
  1147. * @desc: reference to the block cipher handle with meta data
  1148. * @dst: scatter/gather list that is filled by the cipher operation with the
  1149. * ciphertext
  1150. * @src: scatter/gather list that holds the plaintext
  1151. * @nbytes: number of bytes of the plaintext to encrypt.
  1152. *
  1153. * Encrypt plaintext data with the use of an IV that is solely used for this
  1154. * cipher operation. Any previously set IV is not used.
  1155. *
  1156. * The blkcipher_desc data structure must be filled by the caller and can
  1157. * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
  1158. * with the block cipher handle; desc.info is filled with the IV to be used for
  1159. * the current operation; desc.flags is filled with either
  1160. * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
  1161. *
  1162. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  1163. */
  1164. static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
  1165. struct scatterlist *dst,
  1166. struct scatterlist *src,
  1167. unsigned int nbytes)
  1168. {
  1169. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  1170. }
  1171. /**
  1172. * crypto_blkcipher_decrypt() - decrypt ciphertext
  1173. * @desc: reference to the block cipher handle with meta data
  1174. * @dst: scatter/gather list that is filled by the cipher operation with the
  1175. * plaintext
  1176. * @src: scatter/gather list that holds the ciphertext
  1177. * @nbytes: number of bytes of the ciphertext to decrypt.
  1178. *
  1179. * Decrypt ciphertext data using the IV set by the caller with a preceding
  1180. * call of crypto_blkcipher_set_iv.
  1181. *
  1182. * The blkcipher_desc data structure must be filled by the caller as documented
  1183. * for the crypto_blkcipher_encrypt call above.
  1184. *
  1185. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  1186. *
  1187. */
  1188. static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
  1189. struct scatterlist *dst,
  1190. struct scatterlist *src,
  1191. unsigned int nbytes)
  1192. {
  1193. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  1194. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  1195. }
  1196. /**
  1197. * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
  1198. * @desc: reference to the block cipher handle with meta data
  1199. * @dst: scatter/gather list that is filled by the cipher operation with the
  1200. * plaintext
  1201. * @src: scatter/gather list that holds the ciphertext
  1202. * @nbytes: number of bytes of the ciphertext to decrypt.
  1203. *
  1204. * Decrypt ciphertext data with the use of an IV that is solely used for this
  1205. * cipher operation. Any previously set IV is not used.
  1206. *
  1207. * The blkcipher_desc data structure must be filled by the caller as documented
  1208. * for the crypto_blkcipher_encrypt_iv call above.
  1209. *
  1210. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  1211. */
  1212. static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
  1213. struct scatterlist *dst,
  1214. struct scatterlist *src,
  1215. unsigned int nbytes)
  1216. {
  1217. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  1218. }
  1219. /**
  1220. * crypto_blkcipher_set_iv() - set IV for cipher
  1221. * @tfm: cipher handle
  1222. * @src: buffer holding the IV
  1223. * @len: length of the IV in bytes
  1224. *
  1225. * The caller provided IV is set for the block cipher referenced by the cipher
  1226. * handle.
  1227. */
  1228. static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
  1229. const u8 *src, unsigned int len)
  1230. {
  1231. memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
  1232. }
  1233. /**
  1234. * crypto_blkcipher_get_iv() - obtain IV from cipher
  1235. * @tfm: cipher handle
  1236. * @dst: buffer filled with the IV
  1237. * @len: length of the buffer dst
  1238. *
  1239. * The caller can obtain the IV set for the block cipher referenced by the
  1240. * cipher handle and store it into the user-provided buffer. If the buffer
  1241. * has an insufficient space, the IV is truncated to fit the buffer.
  1242. */
  1243. static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
  1244. u8 *dst, unsigned int len)
  1245. {
  1246. memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
  1247. }
  1248. /**
  1249. * DOC: Single Block Cipher API
  1250. *
  1251. * The single block cipher API is used with the ciphers of type
  1252. * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
  1253. *
  1254. * Using the single block cipher API calls, operations with the basic cipher
  1255. * primitive can be implemented. These cipher primitives exclude any block
  1256. * chaining operations including IV handling.
  1257. *
  1258. * The purpose of this single block cipher API is to support the implementation
  1259. * of templates or other concepts that only need to perform the cipher operation
  1260. * on one block at a time. Templates invoke the underlying cipher primitive
  1261. * block-wise and process either the input or the output data of these cipher
  1262. * operations.
  1263. */
  1264. static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
  1265. {
  1266. return (struct crypto_cipher *)tfm;
  1267. }
  1268. static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
  1269. {
  1270. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  1271. return __crypto_cipher_cast(tfm);
  1272. }
  1273. /**
  1274. * crypto_alloc_cipher() - allocate single block cipher handle
  1275. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1276. * single block cipher
  1277. * @type: specifies the type of the cipher
  1278. * @mask: specifies the mask for the cipher
  1279. *
  1280. * Allocate a cipher handle for a single block cipher. The returned struct
  1281. * crypto_cipher is the cipher handle that is required for any subsequent API
  1282. * invocation for that single block cipher.
  1283. *
  1284. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  1285. * of an error, PTR_ERR() returns the error code.
  1286. */
  1287. static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
  1288. u32 type, u32 mask)
  1289. {
  1290. type &= ~CRYPTO_ALG_TYPE_MASK;
  1291. type |= CRYPTO_ALG_TYPE_CIPHER;
  1292. mask |= CRYPTO_ALG_TYPE_MASK;
  1293. return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
  1294. }
  1295. static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
  1296. {
  1297. return &tfm->base;
  1298. }
  1299. /**
  1300. * crypto_free_cipher() - zeroize and free the single block cipher handle
  1301. * @tfm: cipher handle to be freed
  1302. */
  1303. static inline void crypto_free_cipher(struct crypto_cipher *tfm)
  1304. {
  1305. crypto_free_tfm(crypto_cipher_tfm(tfm));
  1306. }
  1307. /**
  1308. * crypto_has_cipher() - Search for the availability of a single block cipher
  1309. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1310. * single block cipher
  1311. * @type: specifies the type of the cipher
  1312. * @mask: specifies the mask for the cipher
  1313. *
  1314. * Return: true when the single block cipher is known to the kernel crypto API;
  1315. * false otherwise
  1316. */
  1317. static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
  1318. {
  1319. type &= ~CRYPTO_ALG_TYPE_MASK;
  1320. type |= CRYPTO_ALG_TYPE_CIPHER;
  1321. mask |= CRYPTO_ALG_TYPE_MASK;
  1322. return crypto_has_alg(alg_name, type, mask);
  1323. }
  1324. static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
  1325. {
  1326. return &crypto_cipher_tfm(tfm)->crt_cipher;
  1327. }
  1328. /**
  1329. * crypto_cipher_blocksize() - obtain block size for cipher
  1330. * @tfm: cipher handle
  1331. *
  1332. * The block size for the single block cipher referenced with the cipher handle
  1333. * tfm is returned. The caller may use that information to allocate appropriate
  1334. * memory for the data returned by the encryption or decryption operation
  1335. *
  1336. * Return: block size of cipher
  1337. */
  1338. static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
  1339. {
  1340. return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
  1341. }
  1342. static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
  1343. {
  1344. return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
  1345. }
  1346. static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
  1347. {
  1348. return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
  1349. }
  1350. static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
  1351. u32 flags)
  1352. {
  1353. crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
  1354. }
  1355. static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
  1356. u32 flags)
  1357. {
  1358. crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
  1359. }
  1360. /**
  1361. * crypto_cipher_setkey() - set key for cipher
  1362. * @tfm: cipher handle
  1363. * @key: buffer holding the key
  1364. * @keylen: length of the key in bytes
  1365. *
  1366. * The caller provided key is set for the single block cipher referenced by the
  1367. * cipher handle.
  1368. *
  1369. * Note, the key length determines the cipher type. Many block ciphers implement
  1370. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  1371. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  1372. * is performed.
  1373. *
  1374. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  1375. */
  1376. static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
  1377. const u8 *key, unsigned int keylen)
  1378. {
  1379. return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
  1380. key, keylen);
  1381. }
  1382. /**
  1383. * crypto_cipher_encrypt_one() - encrypt one block of plaintext
  1384. * @tfm: cipher handle
  1385. * @dst: points to the buffer that will be filled with the ciphertext
  1386. * @src: buffer holding the plaintext to be encrypted
  1387. *
  1388. * Invoke the encryption operation of one block. The caller must ensure that
  1389. * the plaintext and ciphertext buffers are at least one block in size.
  1390. */
  1391. static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
  1392. u8 *dst, const u8 *src)
  1393. {
  1394. crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
  1395. dst, src);
  1396. }
  1397. /**
  1398. * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
  1399. * @tfm: cipher handle
  1400. * @dst: points to the buffer that will be filled with the plaintext
  1401. * @src: buffer holding the ciphertext to be decrypted
  1402. *
  1403. * Invoke the decryption operation of one block. The caller must ensure that
  1404. * the plaintext and ciphertext buffers are at least one block in size.
  1405. */
  1406. static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
  1407. u8 *dst, const u8 *src)
  1408. {
  1409. crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
  1410. dst, src);
  1411. }
  1412. /**
  1413. * DOC: Synchronous Message Digest API
  1414. *
  1415. * The synchronous message digest API is used with the ciphers of type
  1416. * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto)
  1417. */
  1418. static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
  1419. {
  1420. return (struct crypto_hash *)tfm;
  1421. }
  1422. static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
  1423. {
  1424. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
  1425. CRYPTO_ALG_TYPE_HASH_MASK);
  1426. return __crypto_hash_cast(tfm);
  1427. }
  1428. /**
  1429. * crypto_alloc_hash() - allocate synchronous message digest handle
  1430. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1431. * message digest cipher
  1432. * @type: specifies the type of the cipher
  1433. * @mask: specifies the mask for the cipher
  1434. *
  1435. * Allocate a cipher handle for a message digest. The returned struct
  1436. * crypto_hash is the cipher handle that is required for any subsequent
  1437. * API invocation for that message digest.
  1438. *
  1439. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  1440. * of an error, PTR_ERR() returns the error code.
  1441. */
  1442. static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
  1443. u32 type, u32 mask)
  1444. {
  1445. type &= ~CRYPTO_ALG_TYPE_MASK;
  1446. mask &= ~CRYPTO_ALG_TYPE_MASK;
  1447. type |= CRYPTO_ALG_TYPE_HASH;
  1448. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  1449. return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
  1450. }
  1451. static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
  1452. {
  1453. return &tfm->base;
  1454. }
  1455. /**
  1456. * crypto_free_hash() - zeroize and free message digest handle
  1457. * @tfm: cipher handle to be freed
  1458. */
  1459. static inline void crypto_free_hash(struct crypto_hash *tfm)
  1460. {
  1461. crypto_free_tfm(crypto_hash_tfm(tfm));
  1462. }
  1463. /**
  1464. * crypto_has_hash() - Search for the availability of a message digest
  1465. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  1466. * message digest cipher
  1467. * @type: specifies the type of the cipher
  1468. * @mask: specifies the mask for the cipher
  1469. *
  1470. * Return: true when the message digest cipher is known to the kernel crypto
  1471. * API; false otherwise
  1472. */
  1473. static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
  1474. {
  1475. type &= ~CRYPTO_ALG_TYPE_MASK;
  1476. mask &= ~CRYPTO_ALG_TYPE_MASK;
  1477. type |= CRYPTO_ALG_TYPE_HASH;
  1478. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  1479. return crypto_has_alg(alg_name, type, mask);
  1480. }
  1481. static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
  1482. {
  1483. return &crypto_hash_tfm(tfm)->crt_hash;
  1484. }
  1485. /**
  1486. * crypto_hash_blocksize() - obtain block size for message digest
  1487. * @tfm: cipher handle
  1488. *
  1489. * The block size for the message digest cipher referenced with the cipher
  1490. * handle is returned.
  1491. *
  1492. * Return: block size of cipher
  1493. */
  1494. static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
  1495. {
  1496. return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
  1497. }
  1498. static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
  1499. {
  1500. return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
  1501. }
  1502. /**
  1503. * crypto_hash_digestsize() - obtain message digest size
  1504. * @tfm: cipher handle
  1505. *
  1506. * The size for the message digest created by the message digest cipher
  1507. * referenced with the cipher handle is returned.
  1508. *
  1509. * Return: message digest size
  1510. */
  1511. static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
  1512. {
  1513. return crypto_hash_crt(tfm)->digestsize;
  1514. }
  1515. static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
  1516. {
  1517. return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
  1518. }
  1519. static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
  1520. {
  1521. crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
  1522. }
  1523. static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
  1524. {
  1525. crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
  1526. }
  1527. /**
  1528. * crypto_hash_init() - (re)initialize message digest handle
  1529. * @desc: cipher request handle that to be filled by caller --
  1530. * desc.tfm is filled with the hash cipher handle;
  1531. * desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0.
  1532. *
  1533. * The call (re-)initializes the message digest referenced by the hash cipher
  1534. * request handle. Any potentially existing state created by previous
  1535. * operations is discarded.
  1536. *
  1537. * Return: 0 if the message digest initialization was successful; < 0 if an
  1538. * error occurred
  1539. */
  1540. static inline int crypto_hash_init(struct hash_desc *desc)
  1541. {
  1542. return crypto_hash_crt(desc->tfm)->init(desc);
  1543. }
  1544. /**
  1545. * crypto_hash_update() - add data to message digest for processing
  1546. * @desc: cipher request handle
  1547. * @sg: scatter / gather list pointing to the data to be added to the message
  1548. * digest
  1549. * @nbytes: number of bytes to be processed from @sg
  1550. *
  1551. * Updates the message digest state of the cipher handle pointed to by the
  1552. * hash cipher request handle with the input data pointed to by the
  1553. * scatter/gather list.
  1554. *
  1555. * Return: 0 if the message digest update was successful; < 0 if an error
  1556. * occurred
  1557. */
  1558. static inline int crypto_hash_update(struct hash_desc *desc,
  1559. struct scatterlist *sg,
  1560. unsigned int nbytes)
  1561. {
  1562. return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
  1563. }
  1564. /**
  1565. * crypto_hash_final() - calculate message digest
  1566. * @desc: cipher request handle
  1567. * @out: message digest output buffer -- The caller must ensure that the out
  1568. * buffer has a sufficient size (e.g. by using the crypto_hash_digestsize
  1569. * function).
  1570. *
  1571. * Finalize the message digest operation and create the message digest
  1572. * based on all data added to the cipher handle. The message digest is placed
  1573. * into the output buffer.
  1574. *
  1575. * Return: 0 if the message digest creation was successful; < 0 if an error
  1576. * occurred
  1577. */
  1578. static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
  1579. {
  1580. return crypto_hash_crt(desc->tfm)->final(desc, out);
  1581. }
  1582. /**
  1583. * crypto_hash_digest() - calculate message digest for a buffer
  1584. * @desc: see crypto_hash_final()
  1585. * @sg: see crypto_hash_update()
  1586. * @nbytes: see crypto_hash_update()
  1587. * @out: see crypto_hash_final()
  1588. *
  1589. * This function is a "short-hand" for the function calls of crypto_hash_init,
  1590. * crypto_hash_update and crypto_hash_final. The parameters have the same
  1591. * meaning as discussed for those separate three functions.
  1592. *
  1593. * Return: 0 if the message digest creation was successful; < 0 if an error
  1594. * occurred
  1595. */
  1596. static inline int crypto_hash_digest(struct hash_desc *desc,
  1597. struct scatterlist *sg,
  1598. unsigned int nbytes, u8 *out)
  1599. {
  1600. return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
  1601. }
  1602. /**
  1603. * crypto_hash_setkey() - set key for message digest
  1604. * @hash: cipher handle
  1605. * @key: buffer holding the key
  1606. * @keylen: length of the key in bytes
  1607. *
  1608. * The caller provided key is set for the message digest cipher. The cipher
  1609. * handle must point to a keyed hash in order for this function to succeed.
  1610. *
  1611. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  1612. */
  1613. static inline int crypto_hash_setkey(struct crypto_hash *hash,
  1614. const u8 *key, unsigned int keylen)
  1615. {
  1616. return crypto_hash_crt(hash)->setkey(hash, key, keylen);
  1617. }
  1618. static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
  1619. {
  1620. return (struct crypto_comp *)tfm;
  1621. }
  1622. static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
  1623. {
  1624. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
  1625. CRYPTO_ALG_TYPE_MASK);
  1626. return __crypto_comp_cast(tfm);
  1627. }
  1628. static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
  1629. u32 type, u32 mask)
  1630. {
  1631. type &= ~CRYPTO_ALG_TYPE_MASK;
  1632. type |= CRYPTO_ALG_TYPE_COMPRESS;
  1633. mask |= CRYPTO_ALG_TYPE_MASK;
  1634. return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
  1635. }
  1636. static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
  1637. {
  1638. return &tfm->base;
  1639. }
  1640. static inline void crypto_free_comp(struct crypto_comp *tfm)
  1641. {
  1642. crypto_free_tfm(crypto_comp_tfm(tfm));
  1643. }
  1644. static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
  1645. {
  1646. type &= ~CRYPTO_ALG_TYPE_MASK;
  1647. type |= CRYPTO_ALG_TYPE_COMPRESS;
  1648. mask |= CRYPTO_ALG_TYPE_MASK;
  1649. return crypto_has_alg(alg_name, type, mask);
  1650. }
  1651. static inline const char *crypto_comp_name(struct crypto_comp *tfm)
  1652. {
  1653. return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
  1654. }
  1655. static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
  1656. {
  1657. return &crypto_comp_tfm(tfm)->crt_compress;
  1658. }
  1659. static inline int crypto_comp_compress(struct crypto_comp *tfm,
  1660. const u8 *src, unsigned int slen,
  1661. u8 *dst, unsigned int *dlen)
  1662. {
  1663. return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
  1664. src, slen, dst, dlen);
  1665. }
  1666. static inline int crypto_comp_decompress(struct crypto_comp *tfm,
  1667. const u8 *src, unsigned int slen,
  1668. u8 *dst, unsigned int *dlen)
  1669. {
  1670. return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
  1671. src, slen, dst, dlen);
  1672. }
  1673. #endif /* _LINUX_CRYPTO_H */