crypto.h 54 KB

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