crypto.h 64 KB

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