skcipher.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Symmetric key ciphers.
  4. *
  5. * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #ifndef _CRYPTO_SKCIPHER_H
  8. #define _CRYPTO_SKCIPHER_H
  9. #include <linux/atomic.h>
  10. #include <linux/container_of.h>
  11. #include <linux/crypto.h>
  12. #include <linux/slab.h>
  13. #include <linux/string.h>
  14. #include <linux/types.h>
  15. /* Set this bit if the lskcipher operation is a continuation. */
  16. #define CRYPTO_LSKCIPHER_FLAG_CONT 0x00000001
  17. /* Set this bit if the lskcipher operation is final. */
  18. #define CRYPTO_LSKCIPHER_FLAG_FINAL 0x00000002
  19. /* The bit CRYPTO_TFM_REQ_MAY_SLEEP can also be set if needed. */
  20. /* Set this bit if the skcipher operation is a continuation. */
  21. #define CRYPTO_SKCIPHER_REQ_CONT 0x00000001
  22. /* Set this bit if the skcipher operation is not final. */
  23. #define CRYPTO_SKCIPHER_REQ_NOTFINAL 0x00000002
  24. struct scatterlist;
  25. /**
  26. * struct skcipher_request - Symmetric key cipher request
  27. * @cryptlen: Number of bytes to encrypt or decrypt
  28. * @iv: Initialisation Vector
  29. * @src: Source SG list
  30. * @dst: Destination SG list
  31. * @base: Underlying async request
  32. * @__ctx: Start of private context data
  33. */
  34. struct skcipher_request {
  35. unsigned int cryptlen;
  36. u8 *iv;
  37. struct scatterlist *src;
  38. struct scatterlist *dst;
  39. struct crypto_async_request base;
  40. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  41. };
  42. struct crypto_skcipher {
  43. unsigned int reqsize;
  44. struct crypto_tfm base;
  45. };
  46. struct crypto_sync_skcipher {
  47. struct crypto_skcipher base;
  48. };
  49. struct crypto_lskcipher {
  50. struct crypto_tfm base;
  51. };
  52. /*
  53. * struct skcipher_alg_common - common properties of skcipher_alg
  54. * @min_keysize: Minimum key size supported by the transformation. This is the
  55. * smallest key length supported by this transformation algorithm.
  56. * This must be set to one of the pre-defined values as this is
  57. * not hardware specific. Possible values for this field can be
  58. * found via git grep "_MIN_KEY_SIZE" include/crypto/
  59. * @max_keysize: Maximum key size supported by the transformation. This is the
  60. * largest key length supported by this transformation algorithm.
  61. * This must be set to one of the pre-defined values as this is
  62. * not hardware specific. Possible values for this field can be
  63. * found via git grep "_MAX_KEY_SIZE" include/crypto/
  64. * @ivsize: IV size applicable for transformation. The consumer must provide an
  65. * IV of exactly that size to perform the encrypt or decrypt operation.
  66. * @chunksize: Equal to the block size except for stream ciphers such as
  67. * CTR where it is set to the underlying block size.
  68. * @statesize: Size of the internal state for the algorithm.
  69. * @base: Definition of a generic crypto algorithm.
  70. */
  71. #define SKCIPHER_ALG_COMMON { \
  72. unsigned int min_keysize; \
  73. unsigned int max_keysize; \
  74. unsigned int ivsize; \
  75. unsigned int chunksize; \
  76. unsigned int statesize; \
  77. \
  78. struct crypto_alg base; \
  79. }
  80. struct skcipher_alg_common SKCIPHER_ALG_COMMON;
  81. /**
  82. * struct skcipher_alg - symmetric key cipher definition
  83. * @setkey: Set key for the transformation. This function is used to either
  84. * program a supplied key into the hardware or store the key in the
  85. * transformation context for programming it later. Note that this
  86. * function does modify the transformation context. This function can
  87. * be called multiple times during the existence of the transformation
  88. * object, so one must make sure the key is properly reprogrammed into
  89. * the hardware. This function is also responsible for checking the key
  90. * length for validity. In case a software fallback was put in place in
  91. * the @cra_init call, this function might need to use the fallback if
  92. * the algorithm doesn't support all of the key sizes.
  93. * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
  94. * the supplied scatterlist containing the blocks of data. The crypto
  95. * API consumer is responsible for aligning the entries of the
  96. * scatterlist properly and making sure the chunks are correctly
  97. * sized. In case a software fallback was put in place in the
  98. * @cra_init call, this function might need to use the fallback if
  99. * the algorithm doesn't support all of the key sizes. In case the
  100. * key was stored in transformation context, the key might need to be
  101. * re-programmed into the hardware in this function. This function
  102. * shall not modify the transformation context, as this function may
  103. * be called in parallel with the same transformation object.
  104. * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
  105. * and the conditions are exactly the same.
  106. * @export: Export partial state of the transformation. This function dumps the
  107. * entire state of the ongoing transformation into a provided block of
  108. * data so it can be @import 'ed back later on. This is useful in case
  109. * you want to save partial result of the transformation after
  110. * processing certain amount of data and reload this partial result
  111. * multiple times later on for multiple re-use. No data processing
  112. * happens at this point.
  113. * @import: Import partial state of the transformation. This function loads the
  114. * entire state of the ongoing transformation from a provided block of
  115. * data so the transformation can continue from this point onward. No
  116. * data processing happens at this point.
  117. * @init: Initialize the cryptographic transformation object. This function
  118. * is used to initialize the cryptographic transformation object.
  119. * This function is called only once at the instantiation time, right
  120. * after the transformation context was allocated. In case the
  121. * cryptographic hardware has some special requirements which need to
  122. * be handled by software, this function shall check for the precise
  123. * requirement of the transformation and put any software fallbacks
  124. * in place.
  125. * @exit: Deinitialize the cryptographic transformation object. This is a
  126. * counterpart to @init, used to remove various changes set in
  127. * @init.
  128. * @walksize: Equal to the chunk size except in cases where the algorithm is
  129. * considerably more efficient if it can operate on multiple chunks
  130. * in parallel. Should be a multiple of chunksize.
  131. * @co: see struct skcipher_alg_common
  132. *
  133. * All fields except @ivsize are mandatory and must be filled.
  134. */
  135. struct skcipher_alg {
  136. int (*setkey)(struct crypto_skcipher *tfm, const u8 *key,
  137. unsigned int keylen);
  138. int (*encrypt)(struct skcipher_request *req);
  139. int (*decrypt)(struct skcipher_request *req);
  140. int (*export)(struct skcipher_request *req, void *out);
  141. int (*import)(struct skcipher_request *req, const void *in);
  142. int (*init)(struct crypto_skcipher *tfm);
  143. void (*exit)(struct crypto_skcipher *tfm);
  144. unsigned int walksize;
  145. union {
  146. struct SKCIPHER_ALG_COMMON;
  147. struct skcipher_alg_common co;
  148. };
  149. };
  150. /**
  151. * struct lskcipher_alg - linear symmetric key cipher definition
  152. * @setkey: Set key for the transformation. This function is used to either
  153. * program a supplied key into the hardware or store the key in the
  154. * transformation context for programming it later. Note that this
  155. * function does modify the transformation context. This function can
  156. * be called multiple times during the existence of the transformation
  157. * object, so one must make sure the key is properly reprogrammed into
  158. * the hardware. This function is also responsible for checking the key
  159. * length for validity. In case a software fallback was put in place in
  160. * the @cra_init call, this function might need to use the fallback if
  161. * the algorithm doesn't support all of the key sizes.
  162. * @encrypt: Encrypt a number of bytes. This function is used to encrypt
  163. * the supplied data. This function shall not modify
  164. * the transformation context, as this function may be called
  165. * in parallel with the same transformation object. Data
  166. * may be left over if length is not a multiple of blocks
  167. * and there is more to come (final == false). The number of
  168. * left-over bytes should be returned in case of success.
  169. * The siv field shall be as long as ivsize + statesize with
  170. * the IV placed at the front. The state will be used by the
  171. * algorithm internally.
  172. * @decrypt: Decrypt a number of bytes. This is a reverse counterpart to
  173. * @encrypt and the conditions are exactly the same.
  174. * @init: Initialize the cryptographic transformation object. This function
  175. * is used to initialize the cryptographic transformation object.
  176. * This function is called only once at the instantiation time, right
  177. * after the transformation context was allocated.
  178. * @exit: Deinitialize the cryptographic transformation object. This is a
  179. * counterpart to @init, used to remove various changes set in
  180. * @init.
  181. * @co: see struct skcipher_alg_common
  182. */
  183. struct lskcipher_alg {
  184. int (*setkey)(struct crypto_lskcipher *tfm, const u8 *key,
  185. unsigned int keylen);
  186. int (*encrypt)(struct crypto_lskcipher *tfm, const u8 *src,
  187. u8 *dst, unsigned len, u8 *siv, u32 flags);
  188. int (*decrypt)(struct crypto_lskcipher *tfm, const u8 *src,
  189. u8 *dst, unsigned len, u8 *siv, u32 flags);
  190. int (*init)(struct crypto_lskcipher *tfm);
  191. void (*exit)(struct crypto_lskcipher *tfm);
  192. struct skcipher_alg_common co;
  193. };
  194. #define MAX_SYNC_SKCIPHER_REQSIZE 384
  195. /*
  196. * This performs a type-check against the "tfm" argument to make sure
  197. * all users have the correct skcipher tfm for doing on-stack requests.
  198. */
  199. #define SYNC_SKCIPHER_REQUEST_ON_STACK(name, tfm) \
  200. char __##name##_desc[sizeof(struct skcipher_request) + \
  201. MAX_SYNC_SKCIPHER_REQSIZE + \
  202. (!(sizeof((struct crypto_sync_skcipher *)1 == \
  203. (typeof(tfm))1))) \
  204. ] CRYPTO_MINALIGN_ATTR; \
  205. struct skcipher_request *name = (void *)__##name##_desc
  206. /**
  207. * DOC: Symmetric Key Cipher API
  208. *
  209. * Symmetric key cipher API is used with the ciphers of type
  210. * CRYPTO_ALG_TYPE_SKCIPHER (listed as type "skcipher" in /proc/crypto).
  211. *
  212. * Asynchronous cipher operations imply that the function invocation for a
  213. * cipher request returns immediately before the completion of the operation.
  214. * The cipher request is scheduled as a separate kernel thread and therefore
  215. * load-balanced on the different CPUs via the process scheduler. To allow
  216. * the kernel crypto API to inform the caller about the completion of a cipher
  217. * request, the caller must provide a callback function. That function is
  218. * invoked with the cipher handle when the request completes.
  219. *
  220. * To support the asynchronous operation, additional information than just the
  221. * cipher handle must be supplied to the kernel crypto API. That additional
  222. * information is given by filling in the skcipher_request data structure.
  223. *
  224. * For the symmetric key cipher API, the state is maintained with the tfm
  225. * cipher handle. A single tfm can be used across multiple calls and in
  226. * parallel. For asynchronous block cipher calls, context data supplied and
  227. * only used by the caller can be referenced the request data structure in
  228. * addition to the IV used for the cipher request. The maintenance of such
  229. * state information would be important for a crypto driver implementer to
  230. * have, because when calling the callback function upon completion of the
  231. * cipher operation, that callback function may need some information about
  232. * which operation just finished if it invoked multiple in parallel. This
  233. * state information is unused by the kernel crypto API.
  234. */
  235. static inline struct crypto_skcipher *__crypto_skcipher_cast(
  236. struct crypto_tfm *tfm)
  237. {
  238. return container_of(tfm, struct crypto_skcipher, base);
  239. }
  240. /**
  241. * crypto_alloc_skcipher() - allocate symmetric key cipher handle
  242. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  243. * skcipher cipher
  244. * @type: specifies the type of the cipher
  245. * @mask: specifies the mask for the cipher
  246. *
  247. * Allocate a cipher handle for an skcipher. The returned struct
  248. * crypto_skcipher is the cipher handle that is required for any subsequent
  249. * API invocation for that skcipher.
  250. *
  251. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  252. * of an error, PTR_ERR() returns the error code.
  253. */
  254. struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
  255. u32 type, u32 mask);
  256. struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(const char *alg_name,
  257. u32 type, u32 mask);
  258. /**
  259. * crypto_alloc_lskcipher() - allocate linear symmetric key cipher handle
  260. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  261. * lskcipher
  262. * @type: specifies the type of the cipher
  263. * @mask: specifies the mask for the cipher
  264. *
  265. * Allocate a cipher handle for an lskcipher. The returned struct
  266. * crypto_lskcipher is the cipher handle that is required for any subsequent
  267. * API invocation for that lskcipher.
  268. *
  269. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  270. * of an error, PTR_ERR() returns the error code.
  271. */
  272. struct crypto_lskcipher *crypto_alloc_lskcipher(const char *alg_name,
  273. u32 type, u32 mask);
  274. static inline struct crypto_tfm *crypto_skcipher_tfm(
  275. struct crypto_skcipher *tfm)
  276. {
  277. return &tfm->base;
  278. }
  279. static inline struct crypto_tfm *crypto_lskcipher_tfm(
  280. struct crypto_lskcipher *tfm)
  281. {
  282. return &tfm->base;
  283. }
  284. /**
  285. * crypto_free_skcipher() - zeroize and free cipher handle
  286. * @tfm: cipher handle to be freed
  287. *
  288. * If @tfm is a NULL or error pointer, this function does nothing.
  289. */
  290. static inline void crypto_free_skcipher(struct crypto_skcipher *tfm)
  291. {
  292. crypto_destroy_tfm(tfm, crypto_skcipher_tfm(tfm));
  293. }
  294. static inline void crypto_free_sync_skcipher(struct crypto_sync_skcipher *tfm)
  295. {
  296. crypto_free_skcipher(&tfm->base);
  297. }
  298. /**
  299. * crypto_free_lskcipher() - zeroize and free cipher handle
  300. * @tfm: cipher handle to be freed
  301. *
  302. * If @tfm is a NULL or error pointer, this function does nothing.
  303. */
  304. static inline void crypto_free_lskcipher(struct crypto_lskcipher *tfm)
  305. {
  306. crypto_destroy_tfm(tfm, crypto_lskcipher_tfm(tfm));
  307. }
  308. /**
  309. * crypto_has_skcipher() - Search for the availability of an skcipher.
  310. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  311. * skcipher
  312. * @type: specifies the type of the skcipher
  313. * @mask: specifies the mask for the skcipher
  314. *
  315. * Return: true when the skcipher is known to the kernel crypto API; false
  316. * otherwise
  317. */
  318. int crypto_has_skcipher(const char *alg_name, u32 type, u32 mask);
  319. static inline const char *crypto_skcipher_driver_name(
  320. struct crypto_skcipher *tfm)
  321. {
  322. return crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
  323. }
  324. static inline const char *crypto_lskcipher_driver_name(
  325. struct crypto_lskcipher *tfm)
  326. {
  327. return crypto_tfm_alg_driver_name(crypto_lskcipher_tfm(tfm));
  328. }
  329. static inline struct skcipher_alg_common *crypto_skcipher_alg_common(
  330. struct crypto_skcipher *tfm)
  331. {
  332. return container_of(crypto_skcipher_tfm(tfm)->__crt_alg,
  333. struct skcipher_alg_common, base);
  334. }
  335. static inline struct skcipher_alg *crypto_skcipher_alg(
  336. struct crypto_skcipher *tfm)
  337. {
  338. return container_of(crypto_skcipher_tfm(tfm)->__crt_alg,
  339. struct skcipher_alg, base);
  340. }
  341. static inline struct lskcipher_alg *crypto_lskcipher_alg(
  342. struct crypto_lskcipher *tfm)
  343. {
  344. return container_of(crypto_lskcipher_tfm(tfm)->__crt_alg,
  345. struct lskcipher_alg, co.base);
  346. }
  347. /**
  348. * crypto_skcipher_ivsize() - obtain IV size
  349. * @tfm: cipher handle
  350. *
  351. * The size of the IV for the skcipher referenced by the cipher handle is
  352. * returned. This IV size may be zero if the cipher does not need an IV.
  353. *
  354. * Return: IV size in bytes
  355. */
  356. static inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm)
  357. {
  358. return crypto_skcipher_alg_common(tfm)->ivsize;
  359. }
  360. static inline unsigned int crypto_sync_skcipher_ivsize(
  361. struct crypto_sync_skcipher *tfm)
  362. {
  363. return crypto_skcipher_ivsize(&tfm->base);
  364. }
  365. /**
  366. * crypto_lskcipher_ivsize() - obtain IV size
  367. * @tfm: cipher handle
  368. *
  369. * The size of the IV for the lskcipher referenced by the cipher handle is
  370. * returned. This IV size may be zero if the cipher does not need an IV.
  371. *
  372. * Return: IV size in bytes
  373. */
  374. static inline unsigned int crypto_lskcipher_ivsize(
  375. struct crypto_lskcipher *tfm)
  376. {
  377. return crypto_lskcipher_alg(tfm)->co.ivsize;
  378. }
  379. /**
  380. * crypto_skcipher_blocksize() - obtain block size of cipher
  381. * @tfm: cipher handle
  382. *
  383. * The block size for the skcipher referenced with the cipher handle is
  384. * returned. The caller may use that information to allocate appropriate
  385. * memory for the data returned by the encryption or decryption operation
  386. *
  387. * Return: block size of cipher
  388. */
  389. static inline unsigned int crypto_skcipher_blocksize(
  390. struct crypto_skcipher *tfm)
  391. {
  392. return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm));
  393. }
  394. /**
  395. * crypto_lskcipher_blocksize() - obtain block size of cipher
  396. * @tfm: cipher handle
  397. *
  398. * The block size for the lskcipher referenced with the cipher handle is
  399. * returned. The caller may use that information to allocate appropriate
  400. * memory for the data returned by the encryption or decryption operation
  401. *
  402. * Return: block size of cipher
  403. */
  404. static inline unsigned int crypto_lskcipher_blocksize(
  405. struct crypto_lskcipher *tfm)
  406. {
  407. return crypto_tfm_alg_blocksize(crypto_lskcipher_tfm(tfm));
  408. }
  409. /**
  410. * crypto_skcipher_chunksize() - obtain chunk size
  411. * @tfm: cipher handle
  412. *
  413. * The block size is set to one for ciphers such as CTR. However,
  414. * you still need to provide incremental updates in multiples of
  415. * the underlying block size as the IV does not have sub-block
  416. * granularity. This is known in this API as the chunk size.
  417. *
  418. * Return: chunk size in bytes
  419. */
  420. static inline unsigned int crypto_skcipher_chunksize(
  421. struct crypto_skcipher *tfm)
  422. {
  423. return crypto_skcipher_alg_common(tfm)->chunksize;
  424. }
  425. /**
  426. * crypto_lskcipher_chunksize() - obtain chunk size
  427. * @tfm: cipher handle
  428. *
  429. * The block size is set to one for ciphers such as CTR. However,
  430. * you still need to provide incremental updates in multiples of
  431. * the underlying block size as the IV does not have sub-block
  432. * granularity. This is known in this API as the chunk size.
  433. *
  434. * Return: chunk size in bytes
  435. */
  436. static inline unsigned int crypto_lskcipher_chunksize(
  437. struct crypto_lskcipher *tfm)
  438. {
  439. return crypto_lskcipher_alg(tfm)->co.chunksize;
  440. }
  441. /**
  442. * crypto_skcipher_statesize() - obtain state size
  443. * @tfm: cipher handle
  444. *
  445. * Some algorithms cannot be chained with the IV alone. They carry
  446. * internal state which must be replicated if data is to be processed
  447. * incrementally. The size of that state can be obtained with this
  448. * function.
  449. *
  450. * Return: state size in bytes
  451. */
  452. static inline unsigned int crypto_skcipher_statesize(
  453. struct crypto_skcipher *tfm)
  454. {
  455. return crypto_skcipher_alg_common(tfm)->statesize;
  456. }
  457. /**
  458. * crypto_lskcipher_statesize() - obtain state size
  459. * @tfm: cipher handle
  460. *
  461. * Some algorithms cannot be chained with the IV alone. They carry
  462. * internal state which must be replicated if data is to be processed
  463. * incrementally. The size of that state can be obtained with this
  464. * function.
  465. *
  466. * Return: state size in bytes
  467. */
  468. static inline unsigned int crypto_lskcipher_statesize(
  469. struct crypto_lskcipher *tfm)
  470. {
  471. return crypto_lskcipher_alg(tfm)->co.statesize;
  472. }
  473. static inline unsigned int crypto_sync_skcipher_blocksize(
  474. struct crypto_sync_skcipher *tfm)
  475. {
  476. return crypto_skcipher_blocksize(&tfm->base);
  477. }
  478. static inline unsigned int crypto_skcipher_alignmask(
  479. struct crypto_skcipher *tfm)
  480. {
  481. return crypto_tfm_alg_alignmask(crypto_skcipher_tfm(tfm));
  482. }
  483. static inline unsigned int crypto_lskcipher_alignmask(
  484. struct crypto_lskcipher *tfm)
  485. {
  486. return crypto_tfm_alg_alignmask(crypto_lskcipher_tfm(tfm));
  487. }
  488. static inline u32 crypto_skcipher_get_flags(struct crypto_skcipher *tfm)
  489. {
  490. return crypto_tfm_get_flags(crypto_skcipher_tfm(tfm));
  491. }
  492. static inline void crypto_skcipher_set_flags(struct crypto_skcipher *tfm,
  493. u32 flags)
  494. {
  495. crypto_tfm_set_flags(crypto_skcipher_tfm(tfm), flags);
  496. }
  497. static inline void crypto_skcipher_clear_flags(struct crypto_skcipher *tfm,
  498. u32 flags)
  499. {
  500. crypto_tfm_clear_flags(crypto_skcipher_tfm(tfm), flags);
  501. }
  502. static inline u32 crypto_sync_skcipher_get_flags(
  503. struct crypto_sync_skcipher *tfm)
  504. {
  505. return crypto_skcipher_get_flags(&tfm->base);
  506. }
  507. static inline void crypto_sync_skcipher_set_flags(
  508. struct crypto_sync_skcipher *tfm, u32 flags)
  509. {
  510. crypto_skcipher_set_flags(&tfm->base, flags);
  511. }
  512. static inline void crypto_sync_skcipher_clear_flags(
  513. struct crypto_sync_skcipher *tfm, u32 flags)
  514. {
  515. crypto_skcipher_clear_flags(&tfm->base, flags);
  516. }
  517. static inline u32 crypto_lskcipher_get_flags(struct crypto_lskcipher *tfm)
  518. {
  519. return crypto_tfm_get_flags(crypto_lskcipher_tfm(tfm));
  520. }
  521. static inline void crypto_lskcipher_set_flags(struct crypto_lskcipher *tfm,
  522. u32 flags)
  523. {
  524. crypto_tfm_set_flags(crypto_lskcipher_tfm(tfm), flags);
  525. }
  526. static inline void crypto_lskcipher_clear_flags(struct crypto_lskcipher *tfm,
  527. u32 flags)
  528. {
  529. crypto_tfm_clear_flags(crypto_lskcipher_tfm(tfm), flags);
  530. }
  531. /**
  532. * crypto_skcipher_setkey() - set key for cipher
  533. * @tfm: cipher handle
  534. * @key: buffer holding the key
  535. * @keylen: length of the key in bytes
  536. *
  537. * The caller provided key is set for the skcipher referenced by the cipher
  538. * handle.
  539. *
  540. * Note, the key length determines the cipher type. Many block ciphers implement
  541. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  542. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  543. * is performed.
  544. *
  545. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  546. */
  547. int crypto_skcipher_setkey(struct crypto_skcipher *tfm,
  548. const u8 *key, unsigned int keylen);
  549. static inline int crypto_sync_skcipher_setkey(struct crypto_sync_skcipher *tfm,
  550. const u8 *key, unsigned int keylen)
  551. {
  552. return crypto_skcipher_setkey(&tfm->base, key, keylen);
  553. }
  554. /**
  555. * crypto_lskcipher_setkey() - set key for cipher
  556. * @tfm: cipher handle
  557. * @key: buffer holding the key
  558. * @keylen: length of the key in bytes
  559. *
  560. * The caller provided key is set for the lskcipher referenced by the cipher
  561. * handle.
  562. *
  563. * Note, the key length determines the cipher type. Many block ciphers implement
  564. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  565. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  566. * is performed.
  567. *
  568. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  569. */
  570. int crypto_lskcipher_setkey(struct crypto_lskcipher *tfm,
  571. const u8 *key, unsigned int keylen);
  572. static inline unsigned int crypto_skcipher_min_keysize(
  573. struct crypto_skcipher *tfm)
  574. {
  575. return crypto_skcipher_alg_common(tfm)->min_keysize;
  576. }
  577. static inline unsigned int crypto_skcipher_max_keysize(
  578. struct crypto_skcipher *tfm)
  579. {
  580. return crypto_skcipher_alg_common(tfm)->max_keysize;
  581. }
  582. static inline unsigned int crypto_lskcipher_min_keysize(
  583. struct crypto_lskcipher *tfm)
  584. {
  585. return crypto_lskcipher_alg(tfm)->co.min_keysize;
  586. }
  587. static inline unsigned int crypto_lskcipher_max_keysize(
  588. struct crypto_lskcipher *tfm)
  589. {
  590. return crypto_lskcipher_alg(tfm)->co.max_keysize;
  591. }
  592. /**
  593. * crypto_skcipher_reqtfm() - obtain cipher handle from request
  594. * @req: skcipher_request out of which the cipher handle is to be obtained
  595. *
  596. * Return the crypto_skcipher handle when furnishing an skcipher_request
  597. * data structure.
  598. *
  599. * Return: crypto_skcipher handle
  600. */
  601. static inline struct crypto_skcipher *crypto_skcipher_reqtfm(
  602. struct skcipher_request *req)
  603. {
  604. return __crypto_skcipher_cast(req->base.tfm);
  605. }
  606. static inline struct crypto_sync_skcipher *crypto_sync_skcipher_reqtfm(
  607. struct skcipher_request *req)
  608. {
  609. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  610. return container_of(tfm, struct crypto_sync_skcipher, base);
  611. }
  612. /**
  613. * crypto_skcipher_encrypt() - encrypt plaintext
  614. * @req: reference to the skcipher_request handle that holds all information
  615. * needed to perform the cipher operation
  616. *
  617. * Encrypt plaintext data using the skcipher_request handle. That data
  618. * structure and how it is filled with data is discussed with the
  619. * skcipher_request_* functions.
  620. *
  621. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  622. */
  623. int crypto_skcipher_encrypt(struct skcipher_request *req);
  624. /**
  625. * crypto_skcipher_decrypt() - decrypt ciphertext
  626. * @req: reference to the skcipher_request handle that holds all information
  627. * needed to perform the cipher operation
  628. *
  629. * Decrypt ciphertext data using the skcipher_request handle. That data
  630. * structure and how it is filled with data is discussed with the
  631. * skcipher_request_* functions.
  632. *
  633. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  634. */
  635. int crypto_skcipher_decrypt(struct skcipher_request *req);
  636. /**
  637. * crypto_skcipher_export() - export partial state
  638. * @req: reference to the skcipher_request handle that holds all information
  639. * needed to perform the operation
  640. * @out: output buffer of sufficient size that can hold the state
  641. *
  642. * Export partial state of the transformation. This function dumps the
  643. * entire state of the ongoing transformation into a provided block of
  644. * data so it can be @import 'ed back later on. This is useful in case
  645. * you want to save partial result of the transformation after
  646. * processing certain amount of data and reload this partial result
  647. * multiple times later on for multiple re-use. No data processing
  648. * happens at this point.
  649. *
  650. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  651. */
  652. int crypto_skcipher_export(struct skcipher_request *req, void *out);
  653. /**
  654. * crypto_skcipher_import() - import partial state
  655. * @req: reference to the skcipher_request handle that holds all information
  656. * needed to perform the operation
  657. * @in: buffer holding the state
  658. *
  659. * Import partial state of the transformation. This function loads the
  660. * entire state of the ongoing transformation from a provided block of
  661. * data so the transformation can continue from this point onward. No
  662. * data processing happens at this point.
  663. *
  664. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  665. */
  666. int crypto_skcipher_import(struct skcipher_request *req, const void *in);
  667. /**
  668. * crypto_lskcipher_encrypt() - encrypt plaintext
  669. * @tfm: lskcipher handle
  670. * @src: source buffer
  671. * @dst: destination buffer
  672. * @len: number of bytes to process
  673. * @siv: IV + state for the cipher operation. The length of the IV must
  674. * comply with the IV size defined by crypto_lskcipher_ivsize. The
  675. * IV is then followed with a buffer with the length as specified by
  676. * crypto_lskcipher_statesize.
  677. * Encrypt plaintext data using the lskcipher handle.
  678. *
  679. * Return: >=0 if the cipher operation was successful, if positive
  680. * then this many bytes have been left unprocessed;
  681. * < 0 if an error occurred
  682. */
  683. int crypto_lskcipher_encrypt(struct crypto_lskcipher *tfm, const u8 *src,
  684. u8 *dst, unsigned len, u8 *siv);
  685. /**
  686. * crypto_lskcipher_decrypt() - decrypt ciphertext
  687. * @tfm: lskcipher handle
  688. * @src: source buffer
  689. * @dst: destination buffer
  690. * @len: number of bytes to process
  691. * @siv: IV + state for the cipher operation. The length of the IV must
  692. * comply with the IV size defined by crypto_lskcipher_ivsize. The
  693. * IV is then followed with a buffer with the length as specified by
  694. * crypto_lskcipher_statesize.
  695. *
  696. * Decrypt ciphertext data using the lskcipher handle.
  697. *
  698. * Return: >=0 if the cipher operation was successful, if positive
  699. * then this many bytes have been left unprocessed;
  700. * < 0 if an error occurred
  701. */
  702. int crypto_lskcipher_decrypt(struct crypto_lskcipher *tfm, const u8 *src,
  703. u8 *dst, unsigned len, u8 *siv);
  704. /**
  705. * DOC: Symmetric Key Cipher Request Handle
  706. *
  707. * The skcipher_request data structure contains all pointers to data
  708. * required for the symmetric key cipher operation. This includes the cipher
  709. * handle (which can be used by multiple skcipher_request instances), pointer
  710. * to plaintext and ciphertext, asynchronous callback function, etc. It acts
  711. * as a handle to the skcipher_request_* API calls in a similar way as
  712. * skcipher handle to the crypto_skcipher_* API calls.
  713. */
  714. /**
  715. * crypto_skcipher_reqsize() - obtain size of the request data structure
  716. * @tfm: cipher handle
  717. *
  718. * Return: number of bytes
  719. */
  720. static inline unsigned int crypto_skcipher_reqsize(struct crypto_skcipher *tfm)
  721. {
  722. return tfm->reqsize;
  723. }
  724. /**
  725. * skcipher_request_set_tfm() - update cipher handle reference in request
  726. * @req: request handle to be modified
  727. * @tfm: cipher handle that shall be added to the request handle
  728. *
  729. * Allow the caller to replace the existing skcipher handle in the request
  730. * data structure with a different one.
  731. */
  732. static inline void skcipher_request_set_tfm(struct skcipher_request *req,
  733. struct crypto_skcipher *tfm)
  734. {
  735. req->base.tfm = crypto_skcipher_tfm(tfm);
  736. }
  737. static inline void skcipher_request_set_sync_tfm(struct skcipher_request *req,
  738. struct crypto_sync_skcipher *tfm)
  739. {
  740. skcipher_request_set_tfm(req, &tfm->base);
  741. }
  742. static inline struct skcipher_request *skcipher_request_cast(
  743. struct crypto_async_request *req)
  744. {
  745. return container_of(req, struct skcipher_request, base);
  746. }
  747. /**
  748. * skcipher_request_alloc() - allocate request data structure
  749. * @tfm: cipher handle to be registered with the request
  750. * @gfp: memory allocation flag that is handed to kmalloc by the API call.
  751. *
  752. * Allocate the request data structure that must be used with the skcipher
  753. * encrypt and decrypt API calls. During the allocation, the provided skcipher
  754. * handle is registered in the request data structure.
  755. *
  756. * Return: allocated request handle in case of success, or NULL if out of memory
  757. */
  758. static inline struct skcipher_request *skcipher_request_alloc_noprof(
  759. struct crypto_skcipher *tfm, gfp_t gfp)
  760. {
  761. struct skcipher_request *req;
  762. req = kmalloc_noprof(sizeof(struct skcipher_request) +
  763. crypto_skcipher_reqsize(tfm), gfp);
  764. if (likely(req))
  765. skcipher_request_set_tfm(req, tfm);
  766. return req;
  767. }
  768. #define skcipher_request_alloc(...) alloc_hooks(skcipher_request_alloc_noprof(__VA_ARGS__))
  769. /**
  770. * skcipher_request_free() - zeroize and free request data structure
  771. * @req: request data structure cipher handle to be freed
  772. */
  773. static inline void skcipher_request_free(struct skcipher_request *req)
  774. {
  775. kfree_sensitive(req);
  776. }
  777. static inline void skcipher_request_zero(struct skcipher_request *req)
  778. {
  779. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  780. memzero_explicit(req, sizeof(*req) + crypto_skcipher_reqsize(tfm));
  781. }
  782. /**
  783. * skcipher_request_set_callback() - set asynchronous callback function
  784. * @req: request handle
  785. * @flags: specify zero or an ORing of the flags
  786. * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
  787. * increase the wait queue beyond the initial maximum size;
  788. * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
  789. * @compl: callback function pointer to be registered with the request handle
  790. * @data: The data pointer refers to memory that is not used by the kernel
  791. * crypto API, but provided to the callback function for it to use. Here,
  792. * the caller can provide a reference to memory the callback function can
  793. * operate on. As the callback function is invoked asynchronously to the
  794. * related functionality, it may need to access data structures of the
  795. * related functionality which can be referenced using this pointer. The
  796. * callback function can access the memory via the "data" field in the
  797. * crypto_async_request data structure provided to the callback function.
  798. *
  799. * This function allows setting the callback function that is triggered once the
  800. * cipher operation completes.
  801. *
  802. * The callback function is registered with the skcipher_request handle and
  803. * must comply with the following template::
  804. *
  805. * void callback_function(struct crypto_async_request *req, int error)
  806. */
  807. static inline void skcipher_request_set_callback(struct skcipher_request *req,
  808. u32 flags,
  809. crypto_completion_t compl,
  810. void *data)
  811. {
  812. req->base.complete = compl;
  813. req->base.data = data;
  814. req->base.flags = flags;
  815. }
  816. /**
  817. * skcipher_request_set_crypt() - set data buffers
  818. * @req: request handle
  819. * @src: source scatter / gather list
  820. * @dst: destination scatter / gather list
  821. * @cryptlen: number of bytes to process from @src
  822. * @iv: IV for the cipher operation which must comply with the IV size defined
  823. * by crypto_skcipher_ivsize
  824. *
  825. * This function allows setting of the source data and destination data
  826. * scatter / gather lists.
  827. *
  828. * For encryption, the source is treated as the plaintext and the
  829. * destination is the ciphertext. For a decryption operation, the use is
  830. * reversed - the source is the ciphertext and the destination is the plaintext.
  831. */
  832. static inline void skcipher_request_set_crypt(
  833. struct skcipher_request *req,
  834. struct scatterlist *src, struct scatterlist *dst,
  835. unsigned int cryptlen, void *iv)
  836. {
  837. req->src = src;
  838. req->dst = dst;
  839. req->cryptlen = cryptlen;
  840. req->iv = iv;
  841. }
  842. #endif /* _CRYPTO_SKCIPHER_H */