aead.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * AEAD: Authenticated Encryption with Associated Data
  4. *
  5. * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #ifndef _CRYPTO_AEAD_H
  8. #define _CRYPTO_AEAD_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/types.h>
  14. /**
  15. * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
  16. *
  17. * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
  18. * (listed as type "aead" in /proc/crypto)
  19. *
  20. * The most prominent examples for this type of encryption is GCM and CCM.
  21. * However, the kernel supports other types of AEAD ciphers which are defined
  22. * with the following cipher string:
  23. *
  24. * authenc(keyed message digest, block cipher)
  25. *
  26. * For example: authenc(hmac(sha256), cbc(aes))
  27. *
  28. * The example code provided for the symmetric key cipher operation applies
  29. * here as well. Naturally all *skcipher* symbols must be exchanged the *aead*
  30. * pendants discussed in the following. In addition, for the AEAD operation,
  31. * the aead_request_set_ad function must be used to set the pointer to the
  32. * associated data memory location before performing the encryption or
  33. * decryption operation. Another deviation from the asynchronous block cipher
  34. * operation is that the caller should explicitly check for -EBADMSG of the
  35. * crypto_aead_decrypt. That error indicates an authentication error, i.e.
  36. * a breach in the integrity of the message. In essence, that -EBADMSG error
  37. * code is the key bonus an AEAD cipher has over "standard" block chaining
  38. * modes.
  39. *
  40. * Memory Structure:
  41. *
  42. * The source scatterlist must contain the concatenation of
  43. * associated data || plaintext or ciphertext.
  44. *
  45. * The destination scatterlist has the same layout, except that the plaintext
  46. * (resp. ciphertext) will grow (resp. shrink) by the authentication tag size
  47. * during encryption (resp. decryption). The authentication tag is generated
  48. * during the encryption operation and appended to the ciphertext. During
  49. * decryption, the authentication tag is consumed along with the ciphertext and
  50. * used to verify the integrity of the plaintext and the associated data.
  51. *
  52. * In-place encryption/decryption is enabled by using the same scatterlist
  53. * pointer for both the source and destination.
  54. *
  55. * Even in the out-of-place case, space must be reserved in the destination for
  56. * the associated data, even though it won't be written to. This makes the
  57. * in-place and out-of-place cases more consistent. It is permissible for the
  58. * "destination" associated data to alias the "source" associated data.
  59. *
  60. * As with the other scatterlist crypto APIs, zero-length scatterlist elements
  61. * are not allowed in the used part of the scatterlist. Thus, if there is no
  62. * associated data, the first element must point to the plaintext/ciphertext.
  63. *
  64. * To meet the needs of IPsec, a special quirk applies to rfc4106, rfc4309,
  65. * rfc4543, and rfc7539esp ciphers. For these ciphers, the final 'ivsize' bytes
  66. * of the associated data buffer must contain a second copy of the IV. This is
  67. * in addition to the copy passed to aead_request_set_crypt(). These two IV
  68. * copies must not differ; different implementations of the same algorithm may
  69. * behave differently in that case. Note that the algorithm might not actually
  70. * treat the IV as associated data; nevertheless the length passed to
  71. * aead_request_set_ad() must include it.
  72. */
  73. struct crypto_aead;
  74. struct scatterlist;
  75. /**
  76. * struct aead_request - AEAD request
  77. * @base: Common attributes for async crypto requests
  78. * @assoclen: Length in bytes of associated data for authentication
  79. * @cryptlen: Length of data to be encrypted or decrypted
  80. * @iv: Initialisation vector
  81. * @src: Source data
  82. * @dst: Destination data
  83. * @__ctx: Start of private context data
  84. */
  85. struct aead_request {
  86. struct crypto_async_request base;
  87. unsigned int assoclen;
  88. unsigned int cryptlen;
  89. u8 *iv;
  90. struct scatterlist *src;
  91. struct scatterlist *dst;
  92. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  93. };
  94. /**
  95. * struct aead_alg - AEAD cipher definition
  96. * @maxauthsize: Set the maximum authentication tag size supported by the
  97. * transformation. A transformation may support smaller tag sizes.
  98. * As the authentication tag is a message digest to ensure the
  99. * integrity of the encrypted data, a consumer typically wants the
  100. * largest authentication tag possible as defined by this
  101. * variable.
  102. * @setauthsize: Set authentication size for the AEAD transformation. This
  103. * function is used to specify the consumer requested size of the
  104. * authentication tag to be either generated by the transformation
  105. * during encryption or the size of the authentication tag to be
  106. * supplied during the decryption operation. This function is also
  107. * responsible for checking the authentication tag size for
  108. * validity.
  109. * @setkey: see struct skcipher_alg
  110. * @encrypt: see struct skcipher_alg
  111. * @decrypt: see struct skcipher_alg
  112. * @ivsize: see struct skcipher_alg
  113. * @chunksize: see struct skcipher_alg
  114. * @init: Initialize the cryptographic transformation object. This function
  115. * is used to initialize the cryptographic transformation object.
  116. * This function is called only once at the instantiation time, right
  117. * after the transformation context was allocated. In case the
  118. * cryptographic hardware has some special requirements which need to
  119. * be handled by software, this function shall check for the precise
  120. * requirement of the transformation and put any software fallbacks
  121. * in place.
  122. * @exit: Deinitialize the cryptographic transformation object. This is a
  123. * counterpart to @init, used to remove various changes set in
  124. * @init.
  125. * @base: Definition of a generic crypto cipher algorithm.
  126. *
  127. * All fields except @ivsize is mandatory and must be filled.
  128. */
  129. struct aead_alg {
  130. int (*setkey)(struct crypto_aead *tfm, const u8 *key,
  131. unsigned int keylen);
  132. int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
  133. int (*encrypt)(struct aead_request *req);
  134. int (*decrypt)(struct aead_request *req);
  135. int (*init)(struct crypto_aead *tfm);
  136. void (*exit)(struct crypto_aead *tfm);
  137. unsigned int ivsize;
  138. unsigned int maxauthsize;
  139. unsigned int chunksize;
  140. struct crypto_alg base;
  141. };
  142. struct crypto_aead {
  143. unsigned int authsize;
  144. unsigned int reqsize;
  145. struct crypto_tfm base;
  146. };
  147. struct crypto_sync_aead {
  148. struct crypto_aead base;
  149. };
  150. #define MAX_SYNC_AEAD_REQSIZE 384
  151. #define SYNC_AEAD_REQUEST_ON_STACK(name, _tfm) \
  152. char __##name##_desc[sizeof(struct aead_request) + \
  153. MAX_SYNC_AEAD_REQSIZE \
  154. ] CRYPTO_MINALIGN_ATTR; \
  155. struct aead_request *name = \
  156. (((struct aead_request *)__##name##_desc)->base.tfm = \
  157. crypto_sync_aead_tfm((_tfm)), \
  158. (void *)__##name##_desc)
  159. static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
  160. {
  161. return container_of(tfm, struct crypto_aead, base);
  162. }
  163. /**
  164. * crypto_alloc_aead() - allocate AEAD cipher handle
  165. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  166. * AEAD cipher
  167. * @type: specifies the type of the cipher
  168. * @mask: specifies the mask for the cipher
  169. *
  170. * Allocate a cipher handle for an AEAD. The returned struct
  171. * crypto_aead is the cipher handle that is required for any subsequent
  172. * API invocation for that AEAD.
  173. *
  174. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  175. * of an error, PTR_ERR() returns the error code.
  176. */
  177. struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
  178. struct crypto_sync_aead *crypto_alloc_sync_aead(const char *alg_name, u32 type, u32 mask);
  179. static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
  180. {
  181. return &tfm->base;
  182. }
  183. static inline struct crypto_tfm *crypto_sync_aead_tfm(struct crypto_sync_aead *tfm)
  184. {
  185. return crypto_aead_tfm(&tfm->base);
  186. }
  187. /**
  188. * crypto_free_aead() - zeroize and free aead handle
  189. * @tfm: cipher handle to be freed
  190. *
  191. * If @tfm is a NULL or error pointer, this function does nothing.
  192. */
  193. static inline void crypto_free_aead(struct crypto_aead *tfm)
  194. {
  195. crypto_destroy_tfm(tfm, crypto_aead_tfm(tfm));
  196. }
  197. static inline void crypto_free_sync_aead(struct crypto_sync_aead *tfm)
  198. {
  199. crypto_free_aead(&tfm->base);
  200. }
  201. /**
  202. * crypto_has_aead() - Search for the availability of an aead.
  203. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  204. * aead
  205. * @type: specifies the type of the aead
  206. * @mask: specifies the mask for the aead
  207. *
  208. * Return: true when the aead is known to the kernel crypto API; false
  209. * otherwise
  210. */
  211. int crypto_has_aead(const char *alg_name, u32 type, u32 mask);
  212. static inline const char *crypto_aead_driver_name(struct crypto_aead *tfm)
  213. {
  214. return crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
  215. }
  216. static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)
  217. {
  218. return container_of(crypto_aead_tfm(tfm)->__crt_alg,
  219. struct aead_alg, base);
  220. }
  221. static inline unsigned int crypto_aead_alg_ivsize(struct aead_alg *alg)
  222. {
  223. return alg->ivsize;
  224. }
  225. /**
  226. * crypto_aead_ivsize() - obtain IV size
  227. * @tfm: cipher handle
  228. *
  229. * The size of the IV for the aead referenced by the cipher handle is
  230. * returned. This IV size may be zero if the cipher does not need an IV.
  231. *
  232. * Return: IV size in bytes
  233. */
  234. static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
  235. {
  236. return crypto_aead_alg_ivsize(crypto_aead_alg(tfm));
  237. }
  238. static inline unsigned int crypto_sync_aead_ivsize(struct crypto_sync_aead *tfm)
  239. {
  240. return crypto_aead_ivsize(&tfm->base);
  241. }
  242. /**
  243. * crypto_aead_authsize() - obtain maximum authentication data size
  244. * @tfm: cipher handle
  245. *
  246. * The maximum size of the authentication data for the AEAD cipher referenced
  247. * by the AEAD cipher handle is returned. The authentication data size may be
  248. * zero if the cipher implements a hard-coded maximum.
  249. *
  250. * The authentication data may also be known as "tag value".
  251. *
  252. * Return: authentication data size / tag size in bytes
  253. */
  254. static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
  255. {
  256. return tfm->authsize;
  257. }
  258. static inline unsigned int crypto_sync_aead_authsize(struct crypto_sync_aead *tfm)
  259. {
  260. return crypto_aead_authsize(&tfm->base);
  261. }
  262. static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg)
  263. {
  264. return alg->maxauthsize;
  265. }
  266. static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead)
  267. {
  268. return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead));
  269. }
  270. static inline unsigned int crypto_sync_aead_maxauthsize(struct crypto_sync_aead *tfm)
  271. {
  272. return crypto_aead_maxauthsize(&tfm->base);
  273. }
  274. /**
  275. * crypto_aead_blocksize() - obtain block size of cipher
  276. * @tfm: cipher handle
  277. *
  278. * The block size for the AEAD referenced with the cipher handle is returned.
  279. * The caller may use that information to allocate appropriate memory for the
  280. * data returned by the encryption or decryption operation
  281. *
  282. * Return: block size of cipher
  283. */
  284. static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
  285. {
  286. return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
  287. }
  288. static inline unsigned int crypto_sync_aead_blocksize(struct crypto_sync_aead *tfm)
  289. {
  290. return crypto_aead_blocksize(&tfm->base);
  291. }
  292. static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
  293. {
  294. return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
  295. }
  296. static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
  297. {
  298. return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
  299. }
  300. static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
  301. {
  302. crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
  303. }
  304. static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
  305. {
  306. crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
  307. }
  308. static inline u32 crypto_sync_aead_get_flags(struct crypto_sync_aead *tfm)
  309. {
  310. return crypto_aead_get_flags(&tfm->base);
  311. }
  312. static inline void crypto_sync_aead_set_flags(struct crypto_sync_aead *tfm, u32 flags)
  313. {
  314. crypto_aead_set_flags(&tfm->base, flags);
  315. }
  316. static inline void crypto_sync_aead_clear_flags(struct crypto_sync_aead *tfm, u32 flags)
  317. {
  318. crypto_aead_clear_flags(&tfm->base, flags);
  319. }
  320. /**
  321. * crypto_aead_setkey() - set key for cipher
  322. * @tfm: cipher handle
  323. * @key: buffer holding the key
  324. * @keylen: length of the key in bytes
  325. *
  326. * The caller provided key is set for the AEAD referenced by the cipher
  327. * handle.
  328. *
  329. * Note, the key length determines the cipher type. Many block ciphers implement
  330. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  331. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  332. * is performed.
  333. *
  334. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  335. */
  336. int crypto_aead_setkey(struct crypto_aead *tfm,
  337. const u8 *key, unsigned int keylen);
  338. static inline int crypto_sync_aead_setkey(struct crypto_sync_aead *tfm,
  339. const u8 *key, unsigned int keylen)
  340. {
  341. return crypto_aead_setkey(&tfm->base, key, keylen);
  342. }
  343. /**
  344. * crypto_aead_setauthsize() - set authentication data size
  345. * @tfm: cipher handle
  346. * @authsize: size of the authentication data / tag in bytes
  347. *
  348. * Set the authentication data size / tag size. AEAD requires an authentication
  349. * tag (or MAC) in addition to the associated data.
  350. *
  351. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  352. */
  353. int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
  354. static inline int crypto_sync_aead_setauthsize(struct crypto_sync_aead *tfm,
  355. unsigned int authsize)
  356. {
  357. return crypto_aead_setauthsize(&tfm->base, authsize);
  358. }
  359. static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
  360. {
  361. return __crypto_aead_cast(req->base.tfm);
  362. }
  363. static inline struct crypto_sync_aead *crypto_sync_aead_reqtfm(struct aead_request *req)
  364. {
  365. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  366. return container_of(tfm, struct crypto_sync_aead, base);
  367. }
  368. /**
  369. * crypto_aead_encrypt() - encrypt plaintext
  370. * @req: reference to the aead_request handle that holds all information
  371. * needed to perform the cipher operation
  372. *
  373. * Encrypt plaintext data using the aead_request handle. That data structure
  374. * and how it is filled with data is discussed with the aead_request_*
  375. * functions.
  376. *
  377. * IMPORTANT NOTE The encryption operation creates the authentication data /
  378. * tag. That data is concatenated with the created ciphertext.
  379. * The ciphertext memory size is therefore the given number of
  380. * block cipher blocks + the size defined by the
  381. * crypto_aead_setauthsize invocation. The caller must ensure
  382. * that sufficient memory is available for the ciphertext and
  383. * the authentication tag.
  384. *
  385. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  386. */
  387. int crypto_aead_encrypt(struct aead_request *req);
  388. /**
  389. * crypto_aead_decrypt() - decrypt ciphertext
  390. * @req: reference to the aead_request handle that holds all information
  391. * needed to perform the cipher operation
  392. *
  393. * Decrypt ciphertext data using the aead_request handle. That data structure
  394. * and how it is filled with data is discussed with the aead_request_*
  395. * functions.
  396. *
  397. * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
  398. * authentication data / tag. That authentication data / tag
  399. * must have the size defined by the crypto_aead_setauthsize
  400. * invocation.
  401. *
  402. *
  403. * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
  404. * cipher operation performs the authentication of the data during the
  405. * decryption operation. Therefore, the function returns this error if
  406. * the authentication of the ciphertext was unsuccessful (i.e. the
  407. * integrity of the ciphertext or the associated data was violated);
  408. * < 0 if an error occurred.
  409. */
  410. int crypto_aead_decrypt(struct aead_request *req);
  411. /**
  412. * DOC: Asynchronous AEAD Request Handle
  413. *
  414. * The aead_request data structure contains all pointers to data required for
  415. * the AEAD cipher operation. This includes the cipher handle (which can be
  416. * used by multiple aead_request instances), pointer to plaintext and
  417. * ciphertext, asynchronous callback function, etc. It acts as a handle to the
  418. * aead_request_* API calls in a similar way as AEAD handle to the
  419. * crypto_aead_* API calls.
  420. */
  421. /**
  422. * crypto_aead_reqsize() - obtain size of the request data structure
  423. * @tfm: cipher handle
  424. *
  425. * Return: number of bytes
  426. */
  427. static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
  428. {
  429. return tfm->reqsize;
  430. }
  431. /**
  432. * aead_request_set_tfm() - update cipher handle reference in request
  433. * @req: request handle to be modified
  434. * @tfm: cipher handle that shall be added to the request handle
  435. *
  436. * Allow the caller to replace the existing aead handle in the request
  437. * data structure with a different one.
  438. */
  439. static inline void aead_request_set_tfm(struct aead_request *req,
  440. struct crypto_aead *tfm)
  441. {
  442. req->base.tfm = crypto_aead_tfm(tfm);
  443. }
  444. static inline void aead_request_set_sync_tfm(struct aead_request *req,
  445. struct crypto_sync_aead *tfm)
  446. {
  447. aead_request_set_tfm(req, &tfm->base);
  448. }
  449. /**
  450. * aead_request_alloc() - allocate request data structure
  451. * @tfm: cipher handle to be registered with the request
  452. * @gfp: memory allocation flag that is handed to kmalloc by the API call.
  453. *
  454. * Allocate the request data structure that must be used with the AEAD
  455. * encrypt and decrypt API calls. During the allocation, the provided aead
  456. * handle is registered in the request data structure.
  457. *
  458. * Return: allocated request handle in case of success, or NULL if out of memory
  459. */
  460. static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
  461. gfp_t gfp)
  462. {
  463. struct aead_request *req;
  464. req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
  465. if (likely(req))
  466. aead_request_set_tfm(req, tfm);
  467. return req;
  468. }
  469. /**
  470. * aead_request_free() - zeroize and free request data structure
  471. * @req: request data structure cipher handle to be freed
  472. */
  473. static inline void aead_request_free(struct aead_request *req)
  474. {
  475. kfree_sensitive(req);
  476. }
  477. /**
  478. * aead_request_set_callback() - set asynchronous callback function
  479. * @req: request handle
  480. * @flags: specify zero or an ORing of the flags
  481. * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
  482. * increase the wait queue beyond the initial maximum size;
  483. * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
  484. * @compl: callback function pointer to be registered with the request handle
  485. * @data: The data pointer refers to memory that is not used by the kernel
  486. * crypto API, but provided to the callback function for it to use. Here,
  487. * the caller can provide a reference to memory the callback function can
  488. * operate on. As the callback function is invoked asynchronously to the
  489. * related functionality, it may need to access data structures of the
  490. * related functionality which can be referenced using this pointer. The
  491. * callback function can access the memory via the "data" field in the
  492. * crypto_async_request data structure provided to the callback function.
  493. *
  494. * Setting the callback function that is triggered once the cipher operation
  495. * completes
  496. *
  497. * The callback function is registered with the aead_request handle and
  498. * must comply with the following template::
  499. *
  500. * void callback_function(struct crypto_async_request *req, int error)
  501. */
  502. static inline void aead_request_set_callback(struct aead_request *req,
  503. u32 flags,
  504. crypto_completion_t compl,
  505. void *data)
  506. {
  507. req->base.complete = compl;
  508. req->base.data = data;
  509. req->base.flags = flags;
  510. }
  511. /**
  512. * aead_request_set_crypt - set data buffers
  513. * @req: request handle
  514. * @src: source scatter / gather list
  515. * @dst: destination scatter / gather list
  516. * @cryptlen: number of bytes to process from @src
  517. * @iv: IV for the cipher operation which must comply with the IV size defined
  518. * by crypto_aead_ivsize()
  519. *
  520. * Setting the source data and destination data scatter / gather lists which
  521. * hold the associated data concatenated with the plaintext or ciphertext. See
  522. * below for the authentication tag.
  523. *
  524. * For encryption, the source is treated as the plaintext and the
  525. * destination is the ciphertext. For a decryption operation, the use is
  526. * reversed - the source is the ciphertext and the destination is the plaintext.
  527. *
  528. * The memory structure for cipher operation has the following structure:
  529. *
  530. * - AEAD encryption input: assoc data || plaintext
  531. * - AEAD encryption output: assoc data || ciphertext || auth tag
  532. * - AEAD decryption input: assoc data || ciphertext || auth tag
  533. * - AEAD decryption output: assoc data || plaintext
  534. *
  535. * Albeit the kernel requires the presence of the AAD buffer, however,
  536. * the kernel does not fill the AAD buffer in the output case. If the
  537. * caller wants to have that data buffer filled, the caller must either
  538. * use an in-place cipher operation (i.e. same memory location for
  539. * input/output memory location).
  540. */
  541. static inline void aead_request_set_crypt(struct aead_request *req,
  542. struct scatterlist *src,
  543. struct scatterlist *dst,
  544. unsigned int cryptlen, u8 *iv)
  545. {
  546. req->src = src;
  547. req->dst = dst;
  548. req->cryptlen = cryptlen;
  549. req->iv = iv;
  550. }
  551. /**
  552. * aead_request_set_ad - set associated data information
  553. * @req: request handle
  554. * @assoclen: number of bytes in associated data
  555. *
  556. * Setting the AD information. This function sets the length of
  557. * the associated data.
  558. */
  559. static inline void aead_request_set_ad(struct aead_request *req,
  560. unsigned int assoclen)
  561. {
  562. req->assoclen = assoclen;
  563. }
  564. #endif /* _CRYPTO_AEAD_H */