crypto.h 54 KB

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