crypto.h 63 KB

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