aead.h 19 KB

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