blob: 177e6f46e2bbcdd765c4b749330ac3bf1d31d2c0 [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
Herbert Xu996d98d2015-05-21 15:11:01 +080055 * @cryptoff: Bytes to skip after AD before plain/cipher text
Herbert Xu5d1d65f2015-05-11 17:48:12 +080056 * @iv: Initialisation vector
57 * @assoc: Associated data
58 * @src: Source data
59 * @dst: Destination data
60 * @__ctx: Start of private context data
61 */
62struct aead_request {
63 struct crypto_async_request base;
64
Herbert Xu996d98d2015-05-21 15:11:01 +080065 bool old;
66
Herbert Xu5d1d65f2015-05-11 17:48:12 +080067 unsigned int assoclen;
68 unsigned int cryptlen;
Herbert Xu996d98d2015-05-21 15:11:01 +080069 unsigned int cryptoff;
Herbert Xu5d1d65f2015-05-11 17:48:12 +080070
71 u8 *iv;
72
73 struct scatterlist *assoc;
74 struct scatterlist *src;
75 struct scatterlist *dst;
76
77 void *__ctx[] CRYPTO_MINALIGN_ATTR;
78};
79
80/**
Herbert Xu743edf52007-12-10 16:18:01 +080081 * struct aead_givcrypt_request - AEAD request with IV generation
82 * @seq: Sequence number for IV generation
83 * @giv: Space for generated IV
84 * @areq: The AEAD request itself
85 */
86struct aead_givcrypt_request {
87 u64 seq;
88 u8 *giv;
89
90 struct aead_request areq;
91};
92
Herbert Xu63293c62015-05-21 15:11:08 +080093/**
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 ablkcipher_alg
109 * @encrypt: see struct ablkcipher_alg
110 * @decrypt: see struct ablkcipher_alg
111 * @geniv: see struct ablkcipher_alg
112 * @ivsize: see struct ablkcipher_alg
113 *
114 * All fields except @ivsize is mandatory and must be filled.
115 */
116struct aead_alg {
117 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
118 unsigned int keylen);
119 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
120 int (*encrypt)(struct aead_request *req);
121 int (*decrypt)(struct aead_request *req);
122
123 const char *geniv;
124
125 unsigned int ivsize;
126 unsigned int maxauthsize;
127
128 struct crypto_alg base;
129};
130
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800131struct crypto_aead {
Herbert Xu63293c62015-05-21 15:11:08 +0800132 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
133 unsigned int keylen);
134 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800135 int (*encrypt)(struct aead_request *req);
136 int (*decrypt)(struct aead_request *req);
137 int (*givencrypt)(struct aead_givcrypt_request *req);
138 int (*givdecrypt)(struct aead_givcrypt_request *req);
139
140 struct crypto_aead *child;
141
142 unsigned int ivsize;
143 unsigned int authsize;
Herbert Xu63293c62015-05-21 15:11:08 +0800144 unsigned int maxauthsize;
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800145 unsigned int reqsize;
146
147 struct crypto_tfm base;
148};
149
150static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
151{
152 return container_of(tfm, struct crypto_aead, base);
153}
154
155/**
156 * crypto_alloc_aead() - allocate AEAD cipher handle
157 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
158 * AEAD cipher
159 * @type: specifies the type of the cipher
160 * @mask: specifies the mask for the cipher
161 *
162 * Allocate a cipher handle for an AEAD. The returned struct
163 * crypto_aead is the cipher handle that is required for any subsequent
164 * API invocation for that AEAD.
165 *
166 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
167 * of an error, PTR_ERR() returns the error code.
168 */
169struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
170
171static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
172{
173 return &tfm->base;
174}
175
176/**
177 * crypto_free_aead() - zeroize and free aead handle
178 * @tfm: cipher handle to be freed
179 */
180static inline void crypto_free_aead(struct crypto_aead *tfm)
181{
182 crypto_destroy_tfm(tfm, crypto_aead_tfm(tfm));
183}
184
185static inline struct crypto_aead *crypto_aead_crt(struct crypto_aead *tfm)
186{
187 return tfm;
188}
189
190/**
191 * crypto_aead_ivsize() - obtain IV size
192 * @tfm: cipher handle
193 *
194 * The size of the IV for the aead referenced by the cipher handle is
195 * returned. This IV size may be zero if the cipher does not need an IV.
196 *
197 * Return: IV size in bytes
198 */
199static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
200{
201 return tfm->ivsize;
202}
203
204/**
205 * crypto_aead_authsize() - obtain maximum authentication data size
206 * @tfm: cipher handle
207 *
208 * The maximum size of the authentication data for the AEAD cipher referenced
209 * by the AEAD cipher handle is returned. The authentication data size may be
210 * zero if the cipher implements a hard-coded maximum.
211 *
212 * The authentication data may also be known as "tag value".
213 *
214 * Return: authentication data size / tag size in bytes
215 */
216static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
217{
218 return tfm->authsize;
219}
220
221/**
222 * crypto_aead_blocksize() - obtain block size of cipher
223 * @tfm: cipher handle
224 *
225 * The block size for the AEAD referenced with the cipher handle is returned.
226 * The caller may use that information to allocate appropriate memory for the
227 * data returned by the encryption or decryption operation
228 *
229 * Return: block size of cipher
230 */
231static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
232{
233 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
234}
235
236static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
237{
238 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
239}
240
241static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
242{
243 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
244}
245
246static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
247{
248 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
249}
250
251static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
252{
253 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
254}
255
256/**
257 * crypto_aead_setkey() - set key for cipher
258 * @tfm: cipher handle
259 * @key: buffer holding the key
260 * @keylen: length of the key in bytes
261 *
262 * The caller provided key is set for the AEAD referenced by the cipher
263 * handle.
264 *
265 * Note, the key length determines the cipher type. Many block ciphers implement
266 * different cipher modes depending on the key size, such as AES-128 vs AES-192
267 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
268 * is performed.
269 *
270 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
271 */
272int crypto_aead_setkey(struct crypto_aead *tfm,
273 const u8 *key, unsigned int keylen);
274
275/**
276 * crypto_aead_setauthsize() - set authentication data size
277 * @tfm: cipher handle
278 * @authsize: size of the authentication data / tag in bytes
279 *
280 * Set the authentication data size / tag size. AEAD requires an authentication
281 * tag (or MAC) in addition to the associated data.
282 *
283 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
284 */
285int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
286
287static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
288{
289 return __crypto_aead_cast(req->base.tfm);
290}
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 */
311static inline int crypto_aead_encrypt(struct aead_request *req)
312{
313 return crypto_aead_reqtfm(req)->encrypt(req);
314}
315
316/**
317 * crypto_aead_decrypt() - decrypt ciphertext
318 * @req: reference to the ablkcipher_request handle that holds all information
319 * needed to perform the cipher operation
320 *
321 * Decrypt ciphertext data using the aead_request handle. That data structure
322 * and how it is filled with data is discussed with the aead_request_*
323 * functions.
324 *
325 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
326 * authentication data / tag. That authentication data / tag
327 * must have the size defined by the crypto_aead_setauthsize
328 * invocation.
329 *
330 *
331 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
332 * cipher operation performs the authentication of the data during the
333 * decryption operation. Therefore, the function returns this error if
334 * the authentication of the ciphertext was unsuccessful (i.e. the
335 * integrity of the ciphertext or the associated data was violated);
336 * < 0 if an error occurred.
337 */
338static inline int crypto_aead_decrypt(struct aead_request *req)
339{
340 if (req->cryptlen < crypto_aead_authsize(crypto_aead_reqtfm(req)))
341 return -EINVAL;
342
343 return crypto_aead_reqtfm(req)->decrypt(req);
344}
345
346/**
347 * DOC: Asynchronous AEAD Request Handle
348 *
349 * The aead_request data structure contains all pointers to data required for
350 * the AEAD cipher operation. This includes the cipher handle (which can be
351 * used by multiple aead_request instances), pointer to plaintext and
352 * ciphertext, asynchronous callback function, etc. It acts as a handle to the
353 * aead_request_* API calls in a similar way as AEAD handle to the
354 * crypto_aead_* API calls.
355 */
356
357/**
358 * crypto_aead_reqsize() - obtain size of the request data structure
359 * @tfm: cipher handle
360 *
361 * Return: number of bytes
362 */
Herbert Xu996d98d2015-05-21 15:11:01 +0800363unsigned int crypto_aead_reqsize(struct crypto_aead *tfm);
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800364
365/**
366 * aead_request_set_tfm() - update cipher handle reference in request
367 * @req: request handle to be modified
368 * @tfm: cipher handle that shall be added to the request handle
369 *
370 * Allow the caller to replace the existing aead handle in the request
371 * data structure with a different one.
372 */
373static inline void aead_request_set_tfm(struct aead_request *req,
374 struct crypto_aead *tfm)
375{
376 req->base.tfm = crypto_aead_tfm(tfm->child);
377}
378
379/**
380 * aead_request_alloc() - allocate request data structure
381 * @tfm: cipher handle to be registered with the request
382 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
383 *
384 * Allocate the request data structure that must be used with the AEAD
385 * encrypt and decrypt API calls. During the allocation, the provided aead
386 * handle is registered in the request data structure.
387 *
388 * Return: allocated request handle in case of success; IS_ERR() is true in case
389 * of an error, PTR_ERR() returns the error code.
390 */
391static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
392 gfp_t gfp)
393{
394 struct aead_request *req;
395
396 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
397
398 if (likely(req))
399 aead_request_set_tfm(req, tfm);
400
401 return req;
402}
403
404/**
405 * aead_request_free() - zeroize and free request data structure
406 * @req: request data structure cipher handle to be freed
407 */
408static inline void aead_request_free(struct aead_request *req)
409{
410 kzfree(req);
411}
412
413/**
414 * aead_request_set_callback() - set asynchronous callback function
415 * @req: request handle
416 * @flags: specify zero or an ORing of the flags
417 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
418 * increase the wait queue beyond the initial maximum size;
419 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
420 * @compl: callback function pointer to be registered with the request handle
421 * @data: The data pointer refers to memory that is not used by the kernel
422 * crypto API, but provided to the callback function for it to use. Here,
423 * the caller can provide a reference to memory the callback function can
424 * operate on. As the callback function is invoked asynchronously to the
425 * related functionality, it may need to access data structures of the
426 * related functionality which can be referenced using this pointer. The
427 * callback function can access the memory via the "data" field in the
428 * crypto_async_request data structure provided to the callback function.
429 *
430 * Setting the callback function that is triggered once the cipher operation
431 * completes
432 *
433 * The callback function is registered with the aead_request handle and
434 * must comply with the following template
435 *
436 * void callback_function(struct crypto_async_request *req, int error)
437 */
438static inline void aead_request_set_callback(struct aead_request *req,
439 u32 flags,
440 crypto_completion_t compl,
441 void *data)
442{
443 req->base.complete = compl;
444 req->base.data = data;
445 req->base.flags = flags;
446}
447
448/**
449 * aead_request_set_crypt - set data buffers
450 * @req: request handle
451 * @src: source scatter / gather list
452 * @dst: destination scatter / gather list
453 * @cryptlen: number of bytes to process from @src
454 * @iv: IV for the cipher operation which must comply with the IV size defined
455 * by crypto_aead_ivsize()
456 *
457 * Setting the source data and destination data scatter / gather lists.
458 *
459 * For encryption, the source is treated as the plaintext and the
460 * destination is the ciphertext. For a decryption operation, the use is
461 * reversed - the source is the ciphertext and the destination is the plaintext.
462 *
Herbert Xu996d98d2015-05-21 15:11:01 +0800463 * For both src/dst the layout is associated data, skipped data,
464 * plain/cipher text, authentication tag.
465 *
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800466 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
467 * the caller must concatenate the ciphertext followed by the
468 * authentication tag and provide the entire data stream to the
469 * decryption operation (i.e. the data length used for the
470 * initialization of the scatterlist and the data length for the
471 * decryption operation is identical). For encryption, however,
472 * the authentication tag is created while encrypting the data.
473 * The destination buffer must hold sufficient space for the
474 * ciphertext and the authentication tag while the encryption
475 * invocation must only point to the plaintext data size. The
476 * following code snippet illustrates the memory usage
477 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
478 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
479 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
480 */
481static inline void aead_request_set_crypt(struct aead_request *req,
482 struct scatterlist *src,
483 struct scatterlist *dst,
484 unsigned int cryptlen, u8 *iv)
485{
486 req->src = src;
487 req->dst = dst;
488 req->cryptlen = cryptlen;
489 req->iv = iv;
490}
491
492/**
493 * aead_request_set_assoc() - set the associated data scatter / gather list
494 * @req: request handle
495 * @assoc: associated data scatter / gather list
496 * @assoclen: number of bytes to process from @assoc
497 *
Herbert Xu996d98d2015-05-21 15:11:01 +0800498 * Obsolete, do not use.
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800499 */
500static inline void aead_request_set_assoc(struct aead_request *req,
501 struct scatterlist *assoc,
502 unsigned int assoclen)
503{
504 req->assoc = assoc;
505 req->assoclen = assoclen;
Herbert Xu996d98d2015-05-21 15:11:01 +0800506 req->old = true;
507}
508
509/**
510 * aead_request_set_ad - set associated data information
511 * @req: request handle
512 * @assoclen: number of bytes in associated data
513 * @cryptoff: Number of bytes to skip after AD before plain/cipher text
514 *
515 * Setting the AD information. This function sets the length of
516 * the associated data and the number of bytes to skip after it to
517 * access the plain/cipher text.
518 */
519static inline void aead_request_set_ad(struct aead_request *req,
520 unsigned int assoclen,
521 unsigned int cryptoff)
522{
523 req->assoclen = assoclen;
524 req->cryptoff = cryptoff;
525 req->old = false;
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800526}
527
Herbert Xu743edf52007-12-10 16:18:01 +0800528static inline struct crypto_aead *aead_givcrypt_reqtfm(
529 struct aead_givcrypt_request *req)
530{
531 return crypto_aead_reqtfm(&req->areq);
532}
533
Herbert Xu3a282bd2007-12-08 20:13:15 +0800534static inline int crypto_aead_givencrypt(struct aead_givcrypt_request *req)
535{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800536 return aead_givcrypt_reqtfm(req)->givencrypt(req);
Herbert Xu3a282bd2007-12-08 20:13:15 +0800537};
538
539static inline int crypto_aead_givdecrypt(struct aead_givcrypt_request *req)
540{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800541 return aead_givcrypt_reqtfm(req)->givdecrypt(req);
Herbert Xu3a282bd2007-12-08 20:13:15 +0800542};
543
544static inline void aead_givcrypt_set_tfm(struct aead_givcrypt_request *req,
545 struct crypto_aead *tfm)
546{
547 req->areq.base.tfm = crypto_aead_tfm(tfm);
548}
549
550static inline struct aead_givcrypt_request *aead_givcrypt_alloc(
551 struct crypto_aead *tfm, gfp_t gfp)
552{
553 struct aead_givcrypt_request *req;
554
555 req = kmalloc(sizeof(struct aead_givcrypt_request) +
556 crypto_aead_reqsize(tfm), gfp);
557
558 if (likely(req))
559 aead_givcrypt_set_tfm(req, tfm);
560
561 return req;
562}
563
564static inline void aead_givcrypt_free(struct aead_givcrypt_request *req)
565{
566 kfree(req);
567}
568
569static inline void aead_givcrypt_set_callback(
570 struct aead_givcrypt_request *req, u32 flags,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700571 crypto_completion_t compl, void *data)
Herbert Xu3a282bd2007-12-08 20:13:15 +0800572{
Mark Rustad3e3dc252014-07-25 02:53:38 -0700573 aead_request_set_callback(&req->areq, flags, compl, data);
Herbert Xu3a282bd2007-12-08 20:13:15 +0800574}
575
576static inline void aead_givcrypt_set_crypt(struct aead_givcrypt_request *req,
577 struct scatterlist *src,
578 struct scatterlist *dst,
579 unsigned int nbytes, void *iv)
580{
581 aead_request_set_crypt(&req->areq, src, dst, nbytes, iv);
582}
583
584static inline void aead_givcrypt_set_assoc(struct aead_givcrypt_request *req,
585 struct scatterlist *assoc,
586 unsigned int assoclen)
587{
588 aead_request_set_assoc(&req->areq, assoc, assoclen);
589}
590
591static inline void aead_givcrypt_set_giv(struct aead_givcrypt_request *req,
592 u8 *giv, u64 seq)
593{
594 req->giv = giv;
595 req->seq = seq;
596}
597
Herbert Xu743edf52007-12-10 16:18:01 +0800598#endif /* _CRYPTO_AEAD_H */