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