blob: 0402b606fcfd444c41f5d9a2c7fe041e73e8cd61 [file] [log] [blame]
Herbert Xu1ae97822007-08-30 15:36:14 +08001/*
2 * AEAD: Authenticated Encryption with Associated Data
3 *
4 * This file provides API support for AEAD algorithms.
5 *
6 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 */
14
15#include <crypto/algapi.h>
16#include <linux/errno.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/slab.h>
21#include <linux/seq_file.h>
22
23static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
24 unsigned int keylen)
25{
26 struct aead_alg *aead = crypto_aead_alg(tfm);
27 unsigned long alignmask = crypto_aead_alignmask(tfm);
28 int ret;
29 u8 *buffer, *alignbuffer;
30 unsigned long absize;
31
32 absize = keylen + alignmask;
33 buffer = kmalloc(absize, GFP_ATOMIC);
34 if (!buffer)
35 return -ENOMEM;
36
37 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
38 memcpy(alignbuffer, key, keylen);
39 ret = aead->setkey(tfm, alignbuffer, keylen);
40 memset(alignbuffer, 0, keylen);
41 kfree(buffer);
42 return ret;
43}
44
45static int setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen)
46{
47 struct aead_alg *aead = crypto_aead_alg(tfm);
48 unsigned long alignmask = crypto_aead_alignmask(tfm);
49
50 if ((unsigned long)key & alignmask)
51 return setkey_unaligned(tfm, key, keylen);
52
53 return aead->setkey(tfm, key, keylen);
54}
55
Herbert Xu7ba683a2007-12-02 18:49:21 +110056int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
57{
58 int err;
59
60 if (authsize > crypto_aead_alg(tfm)->maxauthsize)
61 return -EINVAL;
62
63 if (crypto_aead_alg(tfm)->setauthsize) {
64 err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize);
65 if (err)
66 return err;
67 }
68
69 crypto_aead_crt(tfm)->authsize = authsize;
70 return 0;
71}
72EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
73
Herbert Xu1ae97822007-08-30 15:36:14 +080074static unsigned int crypto_aead_ctxsize(struct crypto_alg *alg, u32 type,
75 u32 mask)
76{
77 return alg->cra_ctxsize;
78}
79
Herbert Xu743edf52007-12-10 16:18:01 +080080static int no_givdecrypt(struct aead_givcrypt_request *req)
81{
82 return -ENOSYS;
83}
84
Herbert Xu1ae97822007-08-30 15:36:14 +080085static int crypto_init_aead_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
86{
87 struct aead_alg *alg = &tfm->__crt_alg->cra_aead;
88 struct aead_tfm *crt = &tfm->crt_aead;
89
Herbert Xu7ba683a2007-12-02 18:49:21 +110090 if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
Herbert Xu1ae97822007-08-30 15:36:14 +080091 return -EINVAL;
92
93 crt->setkey = setkey;
94 crt->encrypt = alg->encrypt;
95 crt->decrypt = alg->decrypt;
Herbert Xu743edf52007-12-10 16:18:01 +080096 crt->givencrypt = alg->givencrypt;
97 crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
Herbert Xu1ae97822007-08-30 15:36:14 +080098 crt->ivsize = alg->ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +110099 crt->authsize = alg->maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800100
101 return 0;
102}
103
104static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
105 __attribute__ ((unused));
106static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
107{
108 struct aead_alg *aead = &alg->cra_aead;
109
110 seq_printf(m, "type : aead\n");
111 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
112 seq_printf(m, "ivsize : %u\n", aead->ivsize);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100113 seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800114}
115
116const struct crypto_type crypto_aead_type = {
117 .ctxsize = crypto_aead_ctxsize,
118 .init = crypto_init_aead_ops,
119#ifdef CONFIG_PROC_FS
120 .show = crypto_aead_show,
121#endif
122};
123EXPORT_SYMBOL_GPL(crypto_aead_type);
124
125MODULE_LICENSE("GPL");
126MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");