blob: e2d2c3c62e685f4204b75742d5a73f76eb0c66d0 [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 Xu5d1d65f2015-05-11 17:48:12 +080093struct crypto_aead {
94 int (*encrypt)(struct aead_request *req);
95 int (*decrypt)(struct aead_request *req);
96 int (*givencrypt)(struct aead_givcrypt_request *req);
97 int (*givdecrypt)(struct aead_givcrypt_request *req);
98
99 struct crypto_aead *child;
100
101 unsigned int ivsize;
102 unsigned int authsize;
103 unsigned int reqsize;
104
105 struct crypto_tfm base;
106};
107
108static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
109{
110 return container_of(tfm, struct crypto_aead, base);
111}
112
113/**
114 * crypto_alloc_aead() - allocate AEAD cipher handle
115 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
116 * AEAD cipher
117 * @type: specifies the type of the cipher
118 * @mask: specifies the mask for the cipher
119 *
120 * Allocate a cipher handle for an AEAD. The returned struct
121 * crypto_aead is the cipher handle that is required for any subsequent
122 * API invocation for that AEAD.
123 *
124 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
125 * of an error, PTR_ERR() returns the error code.
126 */
127struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
128
129static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
130{
131 return &tfm->base;
132}
133
134/**
135 * crypto_free_aead() - zeroize and free aead handle
136 * @tfm: cipher handle to be freed
137 */
138static inline void crypto_free_aead(struct crypto_aead *tfm)
139{
140 crypto_destroy_tfm(tfm, crypto_aead_tfm(tfm));
141}
142
143static inline struct crypto_aead *crypto_aead_crt(struct crypto_aead *tfm)
144{
145 return tfm;
146}
147
148/**
149 * crypto_aead_ivsize() - obtain IV size
150 * @tfm: cipher handle
151 *
152 * The size of the IV for the aead referenced by the cipher handle is
153 * returned. This IV size may be zero if the cipher does not need an IV.
154 *
155 * Return: IV size in bytes
156 */
157static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
158{
159 return tfm->ivsize;
160}
161
162/**
163 * crypto_aead_authsize() - obtain maximum authentication data size
164 * @tfm: cipher handle
165 *
166 * The maximum size of the authentication data for the AEAD cipher referenced
167 * by the AEAD cipher handle is returned. The authentication data size may be
168 * zero if the cipher implements a hard-coded maximum.
169 *
170 * The authentication data may also be known as "tag value".
171 *
172 * Return: authentication data size / tag size in bytes
173 */
174static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
175{
176 return tfm->authsize;
177}
178
179/**
180 * crypto_aead_blocksize() - obtain block size of cipher
181 * @tfm: cipher handle
182 *
183 * The block size for the AEAD referenced with the cipher handle is returned.
184 * The caller may use that information to allocate appropriate memory for the
185 * data returned by the encryption or decryption operation
186 *
187 * Return: block size of cipher
188 */
189static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
190{
191 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
192}
193
194static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
195{
196 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
197}
198
199static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
200{
201 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
202}
203
204static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
205{
206 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
207}
208
209static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
210{
211 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
212}
213
214/**
215 * crypto_aead_setkey() - set key for cipher
216 * @tfm: cipher handle
217 * @key: buffer holding the key
218 * @keylen: length of the key in bytes
219 *
220 * The caller provided key is set for the AEAD referenced by the cipher
221 * handle.
222 *
223 * Note, the key length determines the cipher type. Many block ciphers implement
224 * different cipher modes depending on the key size, such as AES-128 vs AES-192
225 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
226 * is performed.
227 *
228 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
229 */
230int crypto_aead_setkey(struct crypto_aead *tfm,
231 const u8 *key, unsigned int keylen);
232
233/**
234 * crypto_aead_setauthsize() - set authentication data size
235 * @tfm: cipher handle
236 * @authsize: size of the authentication data / tag in bytes
237 *
238 * Set the authentication data size / tag size. AEAD requires an authentication
239 * tag (or MAC) in addition to the associated data.
240 *
241 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
242 */
243int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
244
245static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
246{
247 return __crypto_aead_cast(req->base.tfm);
248}
249
250/**
251 * crypto_aead_encrypt() - encrypt plaintext
252 * @req: reference to the aead_request handle that holds all information
253 * needed to perform the cipher operation
254 *
255 * Encrypt plaintext data using the aead_request handle. That data structure
256 * and how it is filled with data is discussed with the aead_request_*
257 * functions.
258 *
259 * IMPORTANT NOTE The encryption operation creates the authentication data /
260 * tag. That data is concatenated with the created ciphertext.
261 * The ciphertext memory size is therefore the given number of
262 * block cipher blocks + the size defined by the
263 * crypto_aead_setauthsize invocation. The caller must ensure
264 * that sufficient memory is available for the ciphertext and
265 * the authentication tag.
266 *
267 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
268 */
269static inline int crypto_aead_encrypt(struct aead_request *req)
270{
271 return crypto_aead_reqtfm(req)->encrypt(req);
272}
273
274/**
275 * crypto_aead_decrypt() - decrypt ciphertext
276 * @req: reference to the ablkcipher_request handle that holds all information
277 * needed to perform the cipher operation
278 *
279 * Decrypt ciphertext data using the aead_request handle. That data structure
280 * and how it is filled with data is discussed with the aead_request_*
281 * functions.
282 *
283 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
284 * authentication data / tag. That authentication data / tag
285 * must have the size defined by the crypto_aead_setauthsize
286 * invocation.
287 *
288 *
289 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
290 * cipher operation performs the authentication of the data during the
291 * decryption operation. Therefore, the function returns this error if
292 * the authentication of the ciphertext was unsuccessful (i.e. the
293 * integrity of the ciphertext or the associated data was violated);
294 * < 0 if an error occurred.
295 */
296static inline int crypto_aead_decrypt(struct aead_request *req)
297{
298 if (req->cryptlen < crypto_aead_authsize(crypto_aead_reqtfm(req)))
299 return -EINVAL;
300
301 return crypto_aead_reqtfm(req)->decrypt(req);
302}
303
304/**
305 * DOC: Asynchronous AEAD Request Handle
306 *
307 * The aead_request data structure contains all pointers to data required for
308 * the AEAD cipher operation. This includes the cipher handle (which can be
309 * used by multiple aead_request instances), pointer to plaintext and
310 * ciphertext, asynchronous callback function, etc. It acts as a handle to the
311 * aead_request_* API calls in a similar way as AEAD handle to the
312 * crypto_aead_* API calls.
313 */
314
315/**
316 * crypto_aead_reqsize() - obtain size of the request data structure
317 * @tfm: cipher handle
318 *
319 * Return: number of bytes
320 */
Herbert Xu996d98d2015-05-21 15:11:01 +0800321unsigned int crypto_aead_reqsize(struct crypto_aead *tfm);
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800322
323/**
324 * aead_request_set_tfm() - update cipher handle reference in request
325 * @req: request handle to be modified
326 * @tfm: cipher handle that shall be added to the request handle
327 *
328 * Allow the caller to replace the existing aead handle in the request
329 * data structure with a different one.
330 */
331static inline void aead_request_set_tfm(struct aead_request *req,
332 struct crypto_aead *tfm)
333{
334 req->base.tfm = crypto_aead_tfm(tfm->child);
335}
336
337/**
338 * aead_request_alloc() - allocate request data structure
339 * @tfm: cipher handle to be registered with the request
340 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
341 *
342 * Allocate the request data structure that must be used with the AEAD
343 * encrypt and decrypt API calls. During the allocation, the provided aead
344 * handle is registered in the request data structure.
345 *
346 * Return: allocated request handle in case of success; IS_ERR() is true in case
347 * of an error, PTR_ERR() returns the error code.
348 */
349static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
350 gfp_t gfp)
351{
352 struct aead_request *req;
353
354 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
355
356 if (likely(req))
357 aead_request_set_tfm(req, tfm);
358
359 return req;
360}
361
362/**
363 * aead_request_free() - zeroize and free request data structure
364 * @req: request data structure cipher handle to be freed
365 */
366static inline void aead_request_free(struct aead_request *req)
367{
368 kzfree(req);
369}
370
371/**
372 * aead_request_set_callback() - set asynchronous callback function
373 * @req: request handle
374 * @flags: specify zero or an ORing of the flags
375 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
376 * increase the wait queue beyond the initial maximum size;
377 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
378 * @compl: callback function pointer to be registered with the request handle
379 * @data: The data pointer refers to memory that is not used by the kernel
380 * crypto API, but provided to the callback function for it to use. Here,
381 * the caller can provide a reference to memory the callback function can
382 * operate on. As the callback function is invoked asynchronously to the
383 * related functionality, it may need to access data structures of the
384 * related functionality which can be referenced using this pointer. The
385 * callback function can access the memory via the "data" field in the
386 * crypto_async_request data structure provided to the callback function.
387 *
388 * Setting the callback function that is triggered once the cipher operation
389 * completes
390 *
391 * The callback function is registered with the aead_request handle and
392 * must comply with the following template
393 *
394 * void callback_function(struct crypto_async_request *req, int error)
395 */
396static inline void aead_request_set_callback(struct aead_request *req,
397 u32 flags,
398 crypto_completion_t compl,
399 void *data)
400{
401 req->base.complete = compl;
402 req->base.data = data;
403 req->base.flags = flags;
404}
405
406/**
407 * aead_request_set_crypt - set data buffers
408 * @req: request handle
409 * @src: source scatter / gather list
410 * @dst: destination scatter / gather list
411 * @cryptlen: number of bytes to process from @src
412 * @iv: IV for the cipher operation which must comply with the IV size defined
413 * by crypto_aead_ivsize()
414 *
415 * Setting the source data and destination data scatter / gather lists.
416 *
417 * For encryption, the source is treated as the plaintext and the
418 * destination is the ciphertext. For a decryption operation, the use is
419 * reversed - the source is the ciphertext and the destination is the plaintext.
420 *
Herbert Xu996d98d2015-05-21 15:11:01 +0800421 * For both src/dst the layout is associated data, skipped data,
422 * plain/cipher text, authentication tag.
423 *
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800424 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
425 * the caller must concatenate the ciphertext followed by the
426 * authentication tag and provide the entire data stream to the
427 * decryption operation (i.e. the data length used for the
428 * initialization of the scatterlist and the data length for the
429 * decryption operation is identical). For encryption, however,
430 * the authentication tag is created while encrypting the data.
431 * The destination buffer must hold sufficient space for the
432 * ciphertext and the authentication tag while the encryption
433 * invocation must only point to the plaintext data size. The
434 * following code snippet illustrates the memory usage
435 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
436 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
437 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
438 */
439static inline void aead_request_set_crypt(struct aead_request *req,
440 struct scatterlist *src,
441 struct scatterlist *dst,
442 unsigned int cryptlen, u8 *iv)
443{
444 req->src = src;
445 req->dst = dst;
446 req->cryptlen = cryptlen;
447 req->iv = iv;
448}
449
450/**
451 * aead_request_set_assoc() - set the associated data scatter / gather list
452 * @req: request handle
453 * @assoc: associated data scatter / gather list
454 * @assoclen: number of bytes to process from @assoc
455 *
Herbert Xu996d98d2015-05-21 15:11:01 +0800456 * Obsolete, do not use.
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800457 */
458static inline void aead_request_set_assoc(struct aead_request *req,
459 struct scatterlist *assoc,
460 unsigned int assoclen)
461{
462 req->assoc = assoc;
463 req->assoclen = assoclen;
Herbert Xu996d98d2015-05-21 15:11:01 +0800464 req->old = true;
465}
466
467/**
468 * aead_request_set_ad - set associated data information
469 * @req: request handle
470 * @assoclen: number of bytes in associated data
471 * @cryptoff: Number of bytes to skip after AD before plain/cipher text
472 *
473 * Setting the AD information. This function sets the length of
474 * the associated data and the number of bytes to skip after it to
475 * access the plain/cipher text.
476 */
477static inline void aead_request_set_ad(struct aead_request *req,
478 unsigned int assoclen,
479 unsigned int cryptoff)
480{
481 req->assoclen = assoclen;
482 req->cryptoff = cryptoff;
483 req->old = false;
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800484}
485
Herbert Xu743edf52007-12-10 16:18:01 +0800486static inline struct crypto_aead *aead_givcrypt_reqtfm(
487 struct aead_givcrypt_request *req)
488{
489 return crypto_aead_reqtfm(&req->areq);
490}
491
Herbert Xu3a282bd2007-12-08 20:13:15 +0800492static inline int crypto_aead_givencrypt(struct aead_givcrypt_request *req)
493{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800494 return aead_givcrypt_reqtfm(req)->givencrypt(req);
Herbert Xu3a282bd2007-12-08 20:13:15 +0800495};
496
497static inline int crypto_aead_givdecrypt(struct aead_givcrypt_request *req)
498{
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800499 return aead_givcrypt_reqtfm(req)->givdecrypt(req);
Herbert Xu3a282bd2007-12-08 20:13:15 +0800500};
501
502static inline void aead_givcrypt_set_tfm(struct aead_givcrypt_request *req,
503 struct crypto_aead *tfm)
504{
505 req->areq.base.tfm = crypto_aead_tfm(tfm);
506}
507
508static inline struct aead_givcrypt_request *aead_givcrypt_alloc(
509 struct crypto_aead *tfm, gfp_t gfp)
510{
511 struct aead_givcrypt_request *req;
512
513 req = kmalloc(sizeof(struct aead_givcrypt_request) +
514 crypto_aead_reqsize(tfm), gfp);
515
516 if (likely(req))
517 aead_givcrypt_set_tfm(req, tfm);
518
519 return req;
520}
521
522static inline void aead_givcrypt_free(struct aead_givcrypt_request *req)
523{
524 kfree(req);
525}
526
527static inline void aead_givcrypt_set_callback(
528 struct aead_givcrypt_request *req, u32 flags,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700529 crypto_completion_t compl, void *data)
Herbert Xu3a282bd2007-12-08 20:13:15 +0800530{
Mark Rustad3e3dc252014-07-25 02:53:38 -0700531 aead_request_set_callback(&req->areq, flags, compl, data);
Herbert Xu3a282bd2007-12-08 20:13:15 +0800532}
533
534static inline void aead_givcrypt_set_crypt(struct aead_givcrypt_request *req,
535 struct scatterlist *src,
536 struct scatterlist *dst,
537 unsigned int nbytes, void *iv)
538{
539 aead_request_set_crypt(&req->areq, src, dst, nbytes, iv);
540}
541
542static inline void aead_givcrypt_set_assoc(struct aead_givcrypt_request *req,
543 struct scatterlist *assoc,
544 unsigned int assoclen)
545{
546 aead_request_set_assoc(&req->areq, assoc, assoclen);
547}
548
549static inline void aead_givcrypt_set_giv(struct aead_givcrypt_request *req,
550 u8 *giv, u64 seq)
551{
552 req->giv = giv;
553 req->seq = seq;
554}
555
Herbert Xu743edf52007-12-10 16:18:01 +0800556#endif /* _CRYPTO_AEAD_H */