aead.h 18 KB

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