crypto.h 54 KB

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