blob: 94141dc1225eb80345f9c61f4ef014b30f41ef55 [file] [log] [blame]
Herbert Xu743edf52007-12-10 16:18:01 +08001/*
2 * AEAD: Authenticated Encryption with Associated Data
3 *
4 * Copyright (c) 2007 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
13#ifndef _CRYPTO_AEAD_H
14#define _CRYPTO_AEAD_H
15
16#include <linux/crypto.h>
17#include <linux/kernel.h>
Herbert Xu3a282bd2007-12-08 20:13:15 +080018#include <linux/slab.h>
Herbert Xu743edf52007-12-10 16:18:01 +080019
20/**
Herbert Xu5d1d65f2015-05-11 17:48:12 +080021 * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
22 *
23 * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
24 * (listed as type "aead" in /proc/crypto)
25 *
26 * The most prominent examples for this type of encryption is GCM and CCM.
27 * However, the kernel supports other types of AEAD ciphers which are defined
28 * with the following cipher string:
29 *
30 * authenc(keyed message digest, block cipher)
31 *
32 * For example: authenc(hmac(sha256), cbc(aes))
33 *
34 * The example code provided for the asynchronous block cipher operation
35 * applies here as well. Naturally all *ablkcipher* symbols must be exchanged
36 * the *aead* pendants discussed in the following. In addtion, for the AEAD
37 * operation, the aead_request_set_assoc function must be used to set the
38 * pointer to the associated data memory location before performing the
39 * encryption or decryption operation. In case of an encryption, the associated
40 * data memory is filled during the encryption operation. For decryption, the
41 * associated data memory must contain data that is used to verify the integrity
42 * of the decrypted data. Another deviation from the asynchronous block cipher
43 * operation is that the caller should explicitly check for -EBADMSG of the
44 * crypto_aead_decrypt. That error indicates an authentication error, i.e.
45 * a breach in the integrity of the message. In essence, that -EBADMSG error
46 * code is the key bonus an AEAD cipher has over "standard" block chaining
47 * modes.
48 */
49
50/**
51 * struct aead_request - AEAD request
52 * @base: Common attributes for async crypto requests
53 * @assoclen: Length in bytes of associated data for authentication
54 * @cryptlen: Length of data to be encrypted or decrypted
55 * @iv: Initialisation vector
56 * @assoc: Associated data
57 * @src: Source data
58 * @dst: Destination data
59 * @__ctx: Start of private context data
60 */
61struct aead_request {
62 struct crypto_async_request base;
63
Herbert Xu996d98d2015-05-21 15:11:01 +080064 bool old;
65
Herbert Xu5d1d65f2015-05-11 17:48:12 +080066 unsigned int assoclen;
67 unsigned int cryptlen;
68
69 u8 *iv;
70
71 struct scatterlist *assoc;
72 struct scatterlist *src;
73 struct scatterlist *dst;
74
75 void *__ctx[] CRYPTO_MINALIGN_ATTR;
76};
77
78/**
Herbert Xu743edf52007-12-10 16:18:01 +080079 * struct aead_givcrypt_request - AEAD request with IV generation
80 * @seq: Sequence number for IV generation
81 * @giv: Space for generated IV
82 * @areq: The AEAD request itself
83 */
84struct aead_givcrypt_request {
85 u64 seq;
86 u8 *giv;
87
88 struct aead_request areq;
89};
90
Herbert Xu63293c62015-05-21 15:11:08 +080091/**
92 * struct aead_alg - AEAD cipher definition
93 * @maxauthsize: Set the maximum authentication tag size supported by the
94 * transformation. A transformation may support smaller tag sizes.
95 * As the authentication tag is a message digest to ensure the
96 * integrity of the encrypted data, a consumer typically wants the
97 * largest authentication tag possible as defined by this
98 * variable.
99 * @setauthsize: Set authentication size for the AEAD transformation. This
100 * function is used to specify the consumer requested size of the
101 * authentication tag to be either generated by the transformation
102 * during encryption or the size of the authentication tag to be
103 * supplied during the decryption operation. This function is also
104 * responsible for checking the authentication tag size for
105 * validity.
106 * @setkey: see struct ablkcipher_alg
107 * @encrypt: see struct ablkcipher_alg
108 * @decrypt: see struct ablkcipher_alg
109 * @geniv: see struct ablkcipher_alg
110 * @ivsize: see struct ablkcipher_alg
111 *
112 * All fields except @ivsize is mandatory and must be filled.
113 */
114struct aead_alg {
115 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
116 unsigned int keylen);
117 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
118 int (*encrypt)(struct aead_request *req);
119 int (*decrypt)(struct aead_request *req);
120
121 const char *geniv;
122
123 unsigned int ivsize;
124 unsigned int maxauthsize;
125
126 struct crypto_alg base;
127};
128
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800129struct crypto_aead {
Herbert Xu63293c62015-05-21 15:11:08 +0800130 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
131 unsigned int keylen);
132 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800133 int (*encrypt)(struct aead_request *req);
134 int (*decrypt)(struct aead_request *req);
135 int (*givencrypt)(struct aead_givcrypt_request *req);
136 int (*givdecrypt)(struct aead_givcrypt_request *req);
137
138 struct crypto_aead *child;
139
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800140 unsigned int authsize;
141 unsigned int reqsize;
142
143 struct crypto_tfm base;
144};
145
146static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
147{
148 return container_of(tfm, struct crypto_aead, base);
149}
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 */
165struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
166
167static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
168{
169 return &tfm->base;
170}
171
172/**
173 * crypto_free_aead() - zeroize and free aead handle
174 * @tfm: cipher handle to be freed
175 */
176static inline void crypto_free_aead(struct crypto_aead *tfm)
177{
178 crypto_destroy_tfm(tfm, crypto_aead_tfm(tfm));
179}
180
181static inline struct crypto_aead *crypto_aead_crt(struct crypto_aead *tfm)
182{
183 return tfm;
184}
185
Herbert Xu30e4c012015-05-22 16:30:48 +0800186static inline struct old_aead_alg *crypto_old_aead_alg(struct crypto_aead *tfm)
187{
188 return &crypto_aead_tfm(tfm)->__crt_alg->cra_aead;
189}
190
191static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)
192{
193 return container_of(crypto_aead_tfm(tfm)->__crt_alg,
194 struct aead_alg, base);
195}
196
197static inline unsigned int crypto_aead_alg_ivsize(struct aead_alg *alg)
198{
199 return alg->base.cra_aead.encrypt ? alg->base.cra_aead.ivsize :
200 alg->ivsize;
201}
202
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800203/**
204 * crypto_aead_ivsize() - obtain IV size
205 * @tfm: cipher handle
206 *
207 * The size of the IV for the aead referenced by the cipher handle is
208 * returned. This IV size may be zero if the cipher does not need an IV.
209 *
210 * Return: IV size in bytes
211 */
212static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
213{
Herbert Xu30e4c012015-05-22 16:30:48 +0800214 return crypto_aead_alg_ivsize(crypto_aead_alg(tfm));
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800215}
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 */
229static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
230{
231 return tfm->authsize;
232}
233
234/**
235 * crypto_aead_blocksize() - obtain block size of cipher
236 * @tfm: cipher handle
237 *
238 * The block size for the AEAD referenced with the cipher handle is returned.
239 * The caller may use that information to allocate appropriate memory for the
240 * data returned by the encryption or decryption operation
241 *
242 * Return: block size of cipher
243 */
244static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
245{
246 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
247}
248
249static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
250{
251 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
252}
253
254static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
255{
256 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
257}
258
259static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
260{
261 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
262}
263
264static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
265{
266 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
267}
268
269/**
270 * crypto_aead_setkey() - set key for cipher
271 * @tfm: cipher handle
272 * @key: buffer holding the key
273 * @keylen: length of the key in bytes
274 *
275 * The caller provided key is set for the AEAD referenced by the cipher
276 * handle.
277 *
278 * Note, the key length determines the cipher type. Many block ciphers implement
279 * different cipher modes depending on the key size, such as AES-128 vs AES-192
280 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
281 * is performed.
282 *
283 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
284 */
285int crypto_aead_setkey(struct crypto_aead *tfm,
286 const u8 *key, unsigned int keylen);
287
288/**
289 * crypto_aead_setauthsize() - set authentication data size
290 * @tfm: cipher handle
291 * @authsize: size of the authentication data / tag in bytes
292 *
293 * Set the authentication data size / tag size. AEAD requires an authentication
294 * tag (or MAC) in addition to the associated data.
295 *
296 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
297 */
298int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
299
300static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
301{
302 return __crypto_aead_cast(req->base.tfm);
303}
304
305/**
306 * crypto_aead_encrypt() - encrypt plaintext
307 * @req: reference to the aead_request handle that holds all information
308 * needed to perform the cipher operation
309 *
310 * Encrypt plaintext data using the aead_request handle. That data structure
311 * and how it is filled with data is discussed with the aead_request_*
312 * functions.
313 *
314 * IMPORTANT NOTE The encryption operation creates the authentication data /
315 * tag. That data is concatenated with the created ciphertext.
316 * The ciphertext memory size is therefore the given number of
317 * block cipher blocks + the size defined by the
318 * crypto_aead_setauthsize invocation. The caller must ensure
319 * that sufficient memory is available for the ciphertext and
320 * the authentication tag.
321 *
322 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
323 */
324static inline int crypto_aead_encrypt(struct aead_request *req)
325{
326 return crypto_aead_reqtfm(req)->encrypt(req);
327}
328
329/**
330 * crypto_aead_decrypt() - decrypt ciphertext
331 * @req: reference to the ablkcipher_request handle that holds all information
332 * needed to perform the cipher operation
333 *
334 * Decrypt ciphertext data using the aead_request handle. That data structure
335 * and how it is filled with data is discussed with the aead_request_*
336 * functions.
337 *
338 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
339 * authentication data / tag. That authentication data / tag
340 * must have the size defined by the crypto_aead_setauthsize
341 * invocation.
342 *
343 *
344 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
345 * cipher operation performs the authentication of the data during the
346 * decryption operation. Therefore, the function returns this error if
347 * the authentication of the ciphertext was unsuccessful (i.e. the
348 * integrity of the ciphertext or the associated data was violated);
349 * < 0 if an error occurred.
350 */
351static inline int crypto_aead_decrypt(struct aead_request *req)
352{
353 if (req->cryptlen < crypto_aead_authsize(crypto_aead_reqtfm(req)))
354 return -EINVAL;
355
356 return crypto_aead_reqtfm(req)->decrypt(req);
357}
358
359/**
360 * DOC: Asynchronous AEAD Request Handle
361 *
362 * The aead_request data structure contains all pointers to data required for
363 * the AEAD cipher operation. This includes the cipher handle (which can be
364 * used by multiple aead_request instances), pointer to plaintext and
365 * ciphertext, asynchronous callback function, etc. It acts as a handle to the
366 * aead_request_* API calls in a similar way as AEAD handle to the
367 * crypto_aead_* API calls.
368 */
369
370/**
371 * crypto_aead_reqsize() - obtain size of the request data structure
372 * @tfm: cipher handle
373 *
374 * Return: number of bytes
375 */
Herbert Xu996d98d2015-05-21 15:11:01 +0800376unsigned int crypto_aead_reqsize(struct crypto_aead *tfm);
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800377
378/**
379 * aead_request_set_tfm() - update cipher handle reference in request
380 * @req: request handle to be modified
381 * @tfm: cipher handle that shall be added to the request handle
382 *
383 * Allow the caller to replace the existing aead handle in the request
384 * data structure with a different one.
385 */
386static inline void aead_request_set_tfm(struct aead_request *req,
387 struct crypto_aead *tfm)
388{
389 req->base.tfm = crypto_aead_tfm(tfm->child);
390}
391
392/**
393 * aead_request_alloc() - allocate request data structure
394 * @tfm: cipher handle to be registered with the request
395 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
396 *
397 * Allocate the request data structure that must be used with the AEAD
398 * encrypt and decrypt API calls. During the allocation, the provided aead
399 * handle is registered in the request data structure.
400 *
401 * Return: allocated request handle in case of success; IS_ERR() is true in case
402 * of an error, PTR_ERR() returns the error code.
403 */
404static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
405 gfp_t gfp)
406{
407 struct aead_request *req;
408
409 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
410
411 if (likely(req))
412 aead_request_set_tfm(req, tfm);
413
414 return req;
415}
416
417/**
418 * aead_request_free() - zeroize and free request data structure
419 * @req: request data structure cipher handle to be freed
420 */
421static inline void aead_request_free(struct aead_request *req)
422{
423 kzfree(req);
424}
425
426/**
427 * aead_request_set_callback() - set asynchronous callback function
428 * @req: request handle
429 * @flags: specify zero or an ORing of the flags
430 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
431 * increase the wait queue beyond the initial maximum size;
432 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
433 * @compl: callback function pointer to be registered with the request handle
434 * @data: The data pointer refers to memory that is not used by the kernel
435 * crypto API, but provided to the callback function for it to use. Here,
436 * the caller can provide a reference to memory the callback function can
437 * operate on. As the callback function is invoked asynchronously to the
438 * related functionality, it may need to access data structures of the
439 * related functionality which can be referenced using this pointer. The
440 * callback function can access the memory via the "data" field in the
441 * crypto_async_request data structure provided to the callback function.
442 *
443 * Setting the callback function that is triggered once the cipher operation
444 * completes
445 *
446 * The callback function is registered with the aead_request handle and
447 * must comply with the following template
448 *
449 * void callback_function(struct crypto_async_request *req, int error)
450 */
451static inline void aead_request_set_callback(struct aead_request *req,
452 u32 flags,
453 crypto_completion_t compl,
454 void *data)
455{
456 req->base.complete = compl;
457 req->base.data = data;
458 req->base.flags = flags;
459}
460
461/**
462 * aead_request_set_crypt - set data buffers
463 * @req: request handle
464 * @src: source scatter / gather list
465 * @dst: destination scatter / gather list
466 * @cryptlen: number of bytes to process from @src
467 * @iv: IV for the cipher operation which must comply with the IV size defined
468 * by crypto_aead_ivsize()
469 *
470 * Setting the source data and destination data scatter / gather lists.
471 *
472 * For encryption, the source is treated as the plaintext and the
473 * destination is the ciphertext. For a decryption operation, the use is
474 * reversed - the source is the ciphertext and the destination is the plaintext.
475 *
Herbert Xu996d98d2015-05-21 15:11:01 +0800476 * For both src/dst the layout is associated data, skipped data,
477 * plain/cipher text, authentication tag.
478 *
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800479 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
480 * the caller must concatenate the ciphertext followed by the
481 * authentication tag and provide the entire data stream to the
482 * decryption operation (i.e. the data length used for the
483 * initialization of the scatterlist and the data length for the
484 * decryption operation is identical). For encryption, however,
485 * the authentication tag is created while encrypting the data.
486 * The destination buffer must hold sufficient space for the
487 * ciphertext and the authentication tag while the encryption
488 * invocation must only point to the plaintext data size. The
489 * following code snippet illustrates the memory usage
490 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
491 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
492 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
493 */
494static inline void aead_request_set_crypt(struct aead_request *req,
495 struct scatterlist *src,
496 struct scatterlist *dst,
497 unsigned int cryptlen, u8 *iv)
498{
499 req->src = src;
500 req->dst = dst;
501 req->cryptlen = cryptlen;
502 req->iv = iv;
503}
504
505/**
506 * aead_request_set_assoc() - set the associated data scatter / gather list
507 * @req: request handle
508 * @assoc: associated data scatter / gather list
509 * @assoclen: number of bytes to process from @assoc
510 *
Herbert Xu996d98d2015-05-21 15:11:01 +0800511 * Obsolete, do not use.
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800512 */
513static inline void aead_request_set_assoc(struct aead_request *req,
514 struct scatterlist *assoc,
515 unsigned int assoclen)
516{
517 req->assoc = assoc;
518 req->assoclen = assoclen;
Herbert Xu996d98d2015-05-21 15:11:01 +0800519 req->old = true;
520}
521
522/**
523 * aead_request_set_ad - set associated data information
524 * @req: request handle
525 * @assoclen: number of bytes in associated data
Herbert Xu996d98d2015-05-21 15:11:01 +0800526 *
527 * Setting the AD information. This function sets the length of
528 * the associated data and the number of bytes to skip after it to
529 * access the plain/cipher text.
530 */
531static inline void aead_request_set_ad(struct aead_request *req,
Herbert Xu374d4ad2015-05-23 15:41:57 +0800532 unsigned int assoclen)
Herbert Xu996d98d2015-05-21 15:11:01 +0800533{
534 req->assoclen = assoclen;
Herbert Xu996d98d2015-05-21 15:11:01 +0800535 req->old = false;
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800536}
537
Herbert Xu743edf52007-12-10 16:18:01 +0800538static inline struct crypto_aead *aead_givcrypt_reqtfm(
539 struct aead_givcrypt_request *req)
540{
541 return crypto_aead_reqtfm(&req->areq);
542}
543
Herbert Xu3a282bd2007-12-08 20:13:15 +0800544static inline int crypto_aead_givencrypt(struct aead_givcrypt_request *req)
545{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800546 return aead_givcrypt_reqtfm(req)->givencrypt(req);
Herbert Xu3a282bd2007-12-08 20:13:15 +0800547};
548
549static inline int crypto_aead_givdecrypt(struct aead_givcrypt_request *req)
550{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800551 return aead_givcrypt_reqtfm(req)->givdecrypt(req);
Herbert Xu3a282bd2007-12-08 20:13:15 +0800552};
553
554static inline void aead_givcrypt_set_tfm(struct aead_givcrypt_request *req,
555 struct crypto_aead *tfm)
556{
557 req->areq.base.tfm = crypto_aead_tfm(tfm);
558}
559
560static inline struct aead_givcrypt_request *aead_givcrypt_alloc(
561 struct crypto_aead *tfm, gfp_t gfp)
562{
563 struct aead_givcrypt_request *req;
564
565 req = kmalloc(sizeof(struct aead_givcrypt_request) +
566 crypto_aead_reqsize(tfm), gfp);
567
568 if (likely(req))
569 aead_givcrypt_set_tfm(req, tfm);
570
571 return req;
572}
573
574static inline void aead_givcrypt_free(struct aead_givcrypt_request *req)
575{
576 kfree(req);
577}
578
579static inline void aead_givcrypt_set_callback(
580 struct aead_givcrypt_request *req, u32 flags,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700581 crypto_completion_t compl, void *data)
Herbert Xu3a282bd2007-12-08 20:13:15 +0800582{
Mark Rustad3e3dc252014-07-25 02:53:38 -0700583 aead_request_set_callback(&req->areq, flags, compl, data);
Herbert Xu3a282bd2007-12-08 20:13:15 +0800584}
585
586static inline void aead_givcrypt_set_crypt(struct aead_givcrypt_request *req,
587 struct scatterlist *src,
588 struct scatterlist *dst,
589 unsigned int nbytes, void *iv)
590{
591 aead_request_set_crypt(&req->areq, src, dst, nbytes, iv);
592}
593
594static inline void aead_givcrypt_set_assoc(struct aead_givcrypt_request *req,
595 struct scatterlist *assoc,
596 unsigned int assoclen)
597{
598 aead_request_set_assoc(&req->areq, assoc, assoclen);
599}
600
601static inline void aead_givcrypt_set_giv(struct aead_givcrypt_request *req,
602 u8 *giv, u64 seq)
603{
604 req->giv = giv;
605 req->seq = seq;
606}
607
Herbert Xu743edf52007-12-10 16:18:01 +0800608#endif /* _CRYPTO_AEAD_H */