xref: /linux/crypto/echainiv.c (revision e78f70bad29c5ae1e1076698b690b15794e9b81e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * echainiv: Encrypted Chain IV Generator
4  *
5  * This generator generates an IV based on a sequence number by multiplying
6  * it with a salt and then encrypting it with the same key as used to encrypt
7  * the plain text.  This algorithm requires that the block size be equal
8  * to the IV size.  It is mainly useful for CBC.
9  *
10  * This generator can only be used by algorithms where authentication
11  * is performed after encryption (i.e., authenc).
12  *
13  * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
14  */
15 
16 #include <crypto/internal/geniv.h>
17 #include <crypto/scatterwalk.h>
18 #include <crypto/skcipher.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 
26 static int echainiv_encrypt(struct aead_request *req)
27 {
28 	struct crypto_aead *geniv = crypto_aead_reqtfm(req);
29 	struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
30 	struct aead_request *subreq = aead_request_ctx(req);
31 	__be64 nseqno;
32 	u64 seqno;
33 	u8 *info;
34 	unsigned int ivsize = crypto_aead_ivsize(geniv);
35 
36 	if (req->cryptlen < ivsize)
37 		return -EINVAL;
38 
39 	aead_request_set_tfm(subreq, ctx->child);
40 
41 	info = req->iv;
42 
43 	if (req->src != req->dst)
44 		memcpy_sglist(req->dst, req->src,
45 			      req->assoclen + req->cryptlen);
46 
47 	aead_request_set_callback(subreq, req->base.flags,
48 				  req->base.complete, req->base.data);
49 	aead_request_set_crypt(subreq, req->dst, req->dst,
50 			       req->cryptlen, info);
51 	aead_request_set_ad(subreq, req->assoclen);
52 
53 	memcpy(&nseqno, info + ivsize - 8, 8);
54 	seqno = be64_to_cpu(nseqno);
55 	memset(info, 0, ivsize);
56 
57 	scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
58 
59 	do {
60 		u64 a;
61 
62 		memcpy(&a, ctx->salt + ivsize - 8, 8);
63 
64 		a |= 1;
65 		a *= seqno;
66 
67 		memcpy(info + ivsize - 8, &a, 8);
68 	} while ((ivsize -= 8));
69 
70 	return crypto_aead_encrypt(subreq);
71 }
72 
73 static int echainiv_decrypt(struct aead_request *req)
74 {
75 	struct crypto_aead *geniv = crypto_aead_reqtfm(req);
76 	struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
77 	struct aead_request *subreq = aead_request_ctx(req);
78 	crypto_completion_t compl;
79 	void *data;
80 	unsigned int ivsize = crypto_aead_ivsize(geniv);
81 
82 	if (req->cryptlen < ivsize)
83 		return -EINVAL;
84 
85 	aead_request_set_tfm(subreq, ctx->child);
86 
87 	compl = req->base.complete;
88 	data = req->base.data;
89 
90 	aead_request_set_callback(subreq, req->base.flags, compl, data);
91 	aead_request_set_crypt(subreq, req->src, req->dst,
92 			       req->cryptlen - ivsize, req->iv);
93 	aead_request_set_ad(subreq, req->assoclen + ivsize);
94 
95 	scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
96 
97 	return crypto_aead_decrypt(subreq);
98 }
99 
100 static int echainiv_aead_create(struct crypto_template *tmpl,
101 				struct rtattr **tb)
102 {
103 	struct aead_instance *inst;
104 	int err;
105 
106 	inst = aead_geniv_alloc(tmpl, tb);
107 
108 	if (IS_ERR(inst))
109 		return PTR_ERR(inst);
110 
111 	err = -EINVAL;
112 	if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize)
113 		goto free_inst;
114 
115 	inst->alg.encrypt = echainiv_encrypt;
116 	inst->alg.decrypt = echainiv_decrypt;
117 
118 	inst->alg.init = aead_init_geniv;
119 	inst->alg.exit = aead_exit_geniv;
120 
121 	inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
122 	inst->alg.base.cra_ctxsize += inst->alg.ivsize;
123 
124 	err = aead_register_instance(tmpl, inst);
125 	if (err) {
126 free_inst:
127 		inst->free(inst);
128 	}
129 	return err;
130 }
131 
132 static struct crypto_template echainiv_tmpl = {
133 	.name = "echainiv",
134 	.create = echainiv_aead_create,
135 	.module = THIS_MODULE,
136 };
137 
138 static int __init echainiv_module_init(void)
139 {
140 	return crypto_register_template(&echainiv_tmpl);
141 }
142 
143 static void __exit echainiv_module_exit(void)
144 {
145 	crypto_unregister_template(&echainiv_tmpl);
146 }
147 
148 module_init(echainiv_module_init);
149 module_exit(echainiv_module_exit);
150 
151 MODULE_LICENSE("GPL");
152 MODULE_DESCRIPTION("Encrypted Chain IV Generator");
153 MODULE_ALIAS_CRYPTO("echainiv");
154