aead.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
  148. {
  149. return container_of(tfm, struct crypto_aead, base);
  150. }
  151. /**
  152. * crypto_alloc_aead() - allocate AEAD cipher handle
  153. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  154. * AEAD cipher
  155. * @type: specifies the type of the cipher
  156. * @mask: specifies the mask for the cipher
  157. *
  158. * Allocate a cipher handle for an AEAD. The returned struct
  159. * crypto_aead is the cipher handle that is required for any subsequent
  160. * API invocation for that AEAD.
  161. *
  162. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  163. * of an error, PTR_ERR() returns the error code.
  164. */
  165. struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
  166. static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
  167. {
  168. return &tfm->base;
  169. }
  170. /**
  171. * crypto_free_aead() - zeroize and free aead handle
  172. * @tfm: cipher handle to be freed
  173. *
  174. * If @tfm is a NULL or error pointer, this function does nothing.
  175. */
  176. static inline void crypto_free_aead(struct crypto_aead *tfm)
  177. {
  178. crypto_destroy_tfm(tfm, crypto_aead_tfm(tfm));
  179. }
  180. /**
  181. * crypto_has_aead() - Search for the availability of an aead.
  182. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  183. * aead
  184. * @type: specifies the type of the aead
  185. * @mask: specifies the mask for the aead
  186. *
  187. * Return: true when the aead is known to the kernel crypto API; false
  188. * otherwise
  189. */
  190. int crypto_has_aead(const char *alg_name, u32 type, u32 mask);
  191. static inline const char *crypto_aead_driver_name(struct crypto_aead *tfm)
  192. {
  193. return crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
  194. }
  195. static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)
  196. {
  197. return container_of(crypto_aead_tfm(tfm)->__crt_alg,
  198. struct aead_alg, base);
  199. }
  200. static inline unsigned int crypto_aead_alg_ivsize(struct aead_alg *alg)
  201. {
  202. return alg->ivsize;
  203. }
  204. /**
  205. * crypto_aead_ivsize() - obtain IV size
  206. * @tfm: cipher handle
  207. *
  208. * The size of the IV for the aead referenced by the cipher handle is
  209. * returned. This IV size may be zero if the cipher does not need an IV.
  210. *
  211. * Return: IV size in bytes
  212. */
  213. static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
  214. {
  215. return crypto_aead_alg_ivsize(crypto_aead_alg(tfm));
  216. }
  217. /**
  218. * crypto_aead_authsize() - obtain maximum authentication data size
  219. * @tfm: cipher handle
  220. *
  221. * The maximum size of the authentication data for the AEAD cipher referenced
  222. * by the AEAD cipher handle is returned. The authentication data size may be
  223. * zero if the cipher implements a hard-coded maximum.
  224. *
  225. * The authentication data may also be known as "tag value".
  226. *
  227. * Return: authentication data size / tag size in bytes
  228. */
  229. static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
  230. {
  231. return tfm->authsize;
  232. }
  233. static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg)
  234. {
  235. return alg->maxauthsize;
  236. }
  237. static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead)
  238. {
  239. return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead));
  240. }
  241. /**
  242. * crypto_aead_blocksize() - obtain block size of cipher
  243. * @tfm: cipher handle
  244. *
  245. * The block size for the AEAD referenced with the cipher handle is returned.
  246. * The caller may use that information to allocate appropriate memory for the
  247. * data returned by the encryption or decryption operation
  248. *
  249. * Return: block size of cipher
  250. */
  251. static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
  252. {
  253. return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
  254. }
  255. static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
  256. {
  257. return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
  258. }
  259. static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
  260. {
  261. return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
  262. }
  263. static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
  264. {
  265. crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
  266. }
  267. static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
  268. {
  269. crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
  270. }
  271. /**
  272. * crypto_aead_setkey() - set key for cipher
  273. * @tfm: cipher handle
  274. * @key: buffer holding the key
  275. * @keylen: length of the key in bytes
  276. *
  277. * The caller provided key is set for the AEAD referenced by the cipher
  278. * handle.
  279. *
  280. * Note, the key length determines the cipher type. Many block ciphers implement
  281. * different cipher modes depending on the key size, such as AES-128 vs AES-192
  282. * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
  283. * is performed.
  284. *
  285. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  286. */
  287. int crypto_aead_setkey(struct crypto_aead *tfm,
  288. const u8 *key, unsigned int keylen);
  289. /**
  290. * crypto_aead_setauthsize() - set authentication data size
  291. * @tfm: cipher handle
  292. * @authsize: size of the authentication data / tag in bytes
  293. *
  294. * Set the authentication data size / tag size. AEAD requires an authentication
  295. * tag (or MAC) in addition to the associated data.
  296. *
  297. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  298. */
  299. int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
  300. static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
  301. {
  302. return __crypto_aead_cast(req->base.tfm);
  303. }
  304. /**
  305. * crypto_aead_encrypt() - encrypt plaintext
  306. * @req: reference to the aead_request handle that holds all information
  307. * needed to perform the cipher operation
  308. *
  309. * Encrypt plaintext data using the aead_request handle. That data structure
  310. * and how it is filled with data is discussed with the aead_request_*
  311. * functions.
  312. *
  313. * IMPORTANT NOTE The encryption operation creates the authentication data /
  314. * tag. That data is concatenated with the created ciphertext.
  315. * The ciphertext memory size is therefore the given number of
  316. * block cipher blocks + the size defined by the
  317. * crypto_aead_setauthsize invocation. The caller must ensure
  318. * that sufficient memory is available for the ciphertext and
  319. * the authentication tag.
  320. *
  321. * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  322. */
  323. int crypto_aead_encrypt(struct aead_request *req);
  324. /**
  325. * crypto_aead_decrypt() - decrypt ciphertext
  326. * @req: reference to the aead_request handle that holds all information
  327. * needed to perform the cipher operation
  328. *
  329. * Decrypt ciphertext data using the aead_request handle. That data structure
  330. * and how it is filled with data is discussed with the aead_request_*
  331. * functions.
  332. *
  333. * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
  334. * authentication data / tag. That authentication data / tag
  335. * must have the size defined by the crypto_aead_setauthsize
  336. * invocation.
  337. *
  338. *
  339. * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
  340. * cipher operation performs the authentication of the data during the
  341. * decryption operation. Therefore, the function returns this error if
  342. * the authentication of the ciphertext was unsuccessful (i.e. the
  343. * integrity of the ciphertext or the associated data was violated);
  344. * < 0 if an error occurred.
  345. */
  346. int crypto_aead_decrypt(struct aead_request *req);
  347. /**
  348. * DOC: Asynchronous AEAD Request Handle
  349. *
  350. * The aead_request data structure contains all pointers to data required for
  351. * the AEAD cipher operation. This includes the cipher handle (which can be
  352. * used by multiple aead_request instances), pointer to plaintext and
  353. * ciphertext, asynchronous callback function, etc. It acts as a handle to the
  354. * aead_request_* API calls in a similar way as AEAD handle to the
  355. * crypto_aead_* API calls.
  356. */
  357. /**
  358. * crypto_aead_reqsize() - obtain size of the request data structure
  359. * @tfm: cipher handle
  360. *
  361. * Return: number of bytes
  362. */
  363. static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
  364. {
  365. return tfm->reqsize;
  366. }
  367. /**
  368. * aead_request_set_tfm() - update cipher handle reference in request
  369. * @req: request handle to be modified
  370. * @tfm: cipher handle that shall be added to the request handle
  371. *
  372. * Allow the caller to replace the existing aead handle in the request
  373. * data structure with a different one.
  374. */
  375. static inline void aead_request_set_tfm(struct aead_request *req,
  376. struct crypto_aead *tfm)
  377. {
  378. req->base.tfm = crypto_aead_tfm(tfm);
  379. }
  380. /**
  381. * aead_request_alloc() - allocate request data structure
  382. * @tfm: cipher handle to be registered with the request
  383. * @gfp: memory allocation flag that is handed to kmalloc by the API call.
  384. *
  385. * Allocate the request data structure that must be used with the AEAD
  386. * encrypt and decrypt API calls. During the allocation, the provided aead
  387. * handle is registered in the request data structure.
  388. *
  389. * Return: allocated request handle in case of success, or NULL if out of memory
  390. */
  391. static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
  392. gfp_t gfp)
  393. {
  394. struct aead_request *req;
  395. req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
  396. if (likely(req))
  397. aead_request_set_tfm(req, tfm);
  398. return req;
  399. }
  400. /**
  401. * aead_request_free() - zeroize and free request data structure
  402. * @req: request data structure cipher handle to be freed
  403. */
  404. static inline void aead_request_free(struct aead_request *req)
  405. {
  406. kfree_sensitive(req);
  407. }
  408. /**
  409. * aead_request_set_callback() - set asynchronous callback function
  410. * @req: request handle
  411. * @flags: specify zero or an ORing of the flags
  412. * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
  413. * increase the wait queue beyond the initial maximum size;
  414. * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
  415. * @compl: callback function pointer to be registered with the request handle
  416. * @data: The data pointer refers to memory that is not used by the kernel
  417. * crypto API, but provided to the callback function for it to use. Here,
  418. * the caller can provide a reference to memory the callback function can
  419. * operate on. As the callback function is invoked asynchronously to the
  420. * related functionality, it may need to access data structures of the
  421. * related functionality which can be referenced using this pointer. The
  422. * callback function can access the memory via the "data" field in the
  423. * crypto_async_request data structure provided to the callback function.
  424. *
  425. * Setting the callback function that is triggered once the cipher operation
  426. * completes
  427. *
  428. * The callback function is registered with the aead_request handle and
  429. * must comply with the following template::
  430. *
  431. * void callback_function(struct crypto_async_request *req, int error)
  432. */
  433. static inline void aead_request_set_callback(struct aead_request *req,
  434. u32 flags,
  435. crypto_completion_t compl,
  436. void *data)
  437. {
  438. req->base.complete = compl;
  439. req->base.data = data;
  440. req->base.flags = flags;
  441. }
  442. /**
  443. * aead_request_set_crypt - set data buffers
  444. * @req: request handle
  445. * @src: source scatter / gather list
  446. * @dst: destination scatter / gather list
  447. * @cryptlen: number of bytes to process from @src
  448. * @iv: IV for the cipher operation which must comply with the IV size defined
  449. * by crypto_aead_ivsize()
  450. *
  451. * Setting the source data and destination data scatter / gather lists which
  452. * hold the associated data concatenated with the plaintext or ciphertext. See
  453. * below for the authentication tag.
  454. *
  455. * For encryption, the source is treated as the plaintext and the
  456. * destination is the ciphertext. For a decryption operation, the use is
  457. * reversed - the source is the ciphertext and the destination is the plaintext.
  458. *
  459. * The memory structure for cipher operation has the following structure:
  460. *
  461. * - AEAD encryption input: assoc data || plaintext
  462. * - AEAD encryption output: assoc data || ciphertext || auth tag
  463. * - AEAD decryption input: assoc data || ciphertext || auth tag
  464. * - AEAD decryption output: assoc data || plaintext
  465. *
  466. * Albeit the kernel requires the presence of the AAD buffer, however,
  467. * the kernel does not fill the AAD buffer in the output case. If the
  468. * caller wants to have that data buffer filled, the caller must either
  469. * use an in-place cipher operation (i.e. same memory location for
  470. * input/output memory location).
  471. */
  472. static inline void aead_request_set_crypt(struct aead_request *req,
  473. struct scatterlist *src,
  474. struct scatterlist *dst,
  475. unsigned int cryptlen, u8 *iv)
  476. {
  477. req->src = src;
  478. req->dst = dst;
  479. req->cryptlen = cryptlen;
  480. req->iv = iv;
  481. }
  482. /**
  483. * aead_request_set_ad - set associated data information
  484. * @req: request handle
  485. * @assoclen: number of bytes in associated data
  486. *
  487. * Setting the AD information. This function sets the length of
  488. * the associated data.
  489. */
  490. static inline void aead_request_set_ad(struct aead_request *req,
  491. unsigned int assoclen)
  492. {
  493. req->assoclen = assoclen;
  494. }
  495. #endif /* _CRYPTO_AEAD_H */