usb: musb: ux500: Fix NULL pointer dereference at system PM
[linux/fpc-iii.git] / crypto / echainiv.c
blob343a74e96e2a2fea2285c59e8e340b9cc87e3c72
1 /*
2 * echainiv: Encrypted Chain IV Generator
4 * This generator generates an IV based on a sequence number by multiplying
5 * it with a salt and then encrypting it with the same key as used to encrypt
6 * the plain text. This algorithm requires that the block size be equal
7 * to the IV size. It is mainly useful for CBC.
9 * This generator can only be used by algorithms where authentication
10 * is performed after encryption (i.e., authenc).
12 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
21 #include <crypto/internal/geniv.h>
22 #include <crypto/scatterwalk.h>
23 #include <linux/err.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
30 static int echainiv_encrypt(struct aead_request *req)
32 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
33 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
34 struct aead_request *subreq = aead_request_ctx(req);
35 __be64 nseqno;
36 u64 seqno;
37 u8 *info;
38 unsigned int ivsize = crypto_aead_ivsize(geniv);
39 int err;
41 if (req->cryptlen < ivsize)
42 return -EINVAL;
44 aead_request_set_tfm(subreq, ctx->child);
46 info = req->iv;
48 if (req->src != req->dst) {
49 struct blkcipher_desc desc = {
50 .tfm = ctx->null,
53 err = crypto_blkcipher_encrypt(
54 &desc, req->dst, req->src,
55 req->assoclen + req->cryptlen);
56 if (err)
57 return err;
60 aead_request_set_callback(subreq, req->base.flags,
61 req->base.complete, req->base.data);
62 aead_request_set_crypt(subreq, req->dst, req->dst,
63 req->cryptlen, info);
64 aead_request_set_ad(subreq, req->assoclen);
66 memcpy(&nseqno, info + ivsize - 8, 8);
67 seqno = be64_to_cpu(nseqno);
68 memset(info, 0, ivsize);
70 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
72 do {
73 u64 a;
75 memcpy(&a, ctx->salt + ivsize - 8, 8);
77 a |= 1;
78 a *= seqno;
80 memcpy(info + ivsize - 8, &a, 8);
81 } while ((ivsize -= 8));
83 return crypto_aead_encrypt(subreq);
86 static int echainiv_decrypt(struct aead_request *req)
88 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
89 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
90 struct aead_request *subreq = aead_request_ctx(req);
91 crypto_completion_t compl;
92 void *data;
93 unsigned int ivsize = crypto_aead_ivsize(geniv);
95 if (req->cryptlen < ivsize)
96 return -EINVAL;
98 aead_request_set_tfm(subreq, ctx->child);
100 compl = req->base.complete;
101 data = req->base.data;
103 aead_request_set_callback(subreq, req->base.flags, compl, data);
104 aead_request_set_crypt(subreq, req->src, req->dst,
105 req->cryptlen - ivsize, req->iv);
106 aead_request_set_ad(subreq, req->assoclen + ivsize);
108 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
110 return crypto_aead_decrypt(subreq);
113 static int echainiv_aead_create(struct crypto_template *tmpl,
114 struct rtattr **tb)
116 struct aead_instance *inst;
117 struct crypto_aead_spawn *spawn;
118 struct aead_alg *alg;
119 int err;
121 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
123 if (IS_ERR(inst))
124 return PTR_ERR(inst);
126 spawn = aead_instance_ctx(inst);
127 alg = crypto_spawn_aead_alg(spawn);
129 err = -EINVAL;
130 if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize)
131 goto free_inst;
133 inst->alg.encrypt = echainiv_encrypt;
134 inst->alg.decrypt = echainiv_decrypt;
136 inst->alg.init = aead_init_geniv;
137 inst->alg.exit = aead_exit_geniv;
139 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
140 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
142 inst->free = aead_geniv_free;
144 err = aead_register_instance(tmpl, inst);
145 if (err)
146 goto free_inst;
148 out:
149 return err;
151 free_inst:
152 aead_geniv_free(inst);
153 goto out;
156 static void echainiv_free(struct crypto_instance *inst)
158 aead_geniv_free(aead_instance(inst));
161 static struct crypto_template echainiv_tmpl = {
162 .name = "echainiv",
163 .create = echainiv_aead_create,
164 .free = echainiv_free,
165 .module = THIS_MODULE,
168 static int __init echainiv_module_init(void)
170 return crypto_register_template(&echainiv_tmpl);
173 static void __exit echainiv_module_exit(void)
175 crypto_unregister_template(&echainiv_tmpl);
178 module_init(echainiv_module_init);
179 module_exit(echainiv_module_exit);
181 MODULE_LICENSE("GPL");
182 MODULE_DESCRIPTION("Encrypted Chain IV Generator");
183 MODULE_ALIAS_CRYPTO("echainiv");