]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
0a270321 HX |
2 | /* |
3 | * seqiv: Sequence Number IV Generator | |
4 | * | |
5 | * This generator generates an IV based on a sequence number by xoring it | |
6 | * with a salt. This algorithm is mainly useful for CTR and similar modes. | |
7 | * | |
8 | * Copyright (c) 2007 Herbert Xu <[email protected]> | |
0a270321 HX |
9 | */ |
10 | ||
661cfd0e | 11 | #include <crypto/internal/geniv.h> |
856e3f40 | 12 | #include <crypto/scatterwalk.h> |
3a01d0ee | 13 | #include <crypto/skcipher.h> |
0a270321 HX |
14 | #include <linux/err.h> |
15 | #include <linux/init.h> | |
16 | #include <linux/kernel.h> | |
17 | #include <linux/module.h> | |
5a0e3ad6 | 18 | #include <linux/slab.h> |
0a270321 HX |
19 | #include <linux/string.h> |
20 | ||
856e3f40 HX |
21 | static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err) |
22 | { | |
23 | struct aead_request *subreq = aead_request_ctx(req); | |
24 | struct crypto_aead *geniv; | |
25 | ||
26 | if (err == -EINPROGRESS) | |
27 | return; | |
28 | ||
29 | if (err) | |
30 | goto out; | |
31 | ||
32 | geniv = crypto_aead_reqtfm(req); | |
33 | memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv)); | |
34 | ||
35 | out: | |
453431a5 | 36 | kfree_sensitive(subreq->iv); |
856e3f40 HX |
37 | } |
38 | ||
39 | static void seqiv_aead_encrypt_complete(struct crypto_async_request *base, | |
40 | int err) | |
41 | { | |
42 | struct aead_request *req = base->data; | |
43 | ||
44 | seqiv_aead_encrypt_complete2(req, err); | |
45 | aead_request_complete(req, err); | |
46 | } | |
47 | ||
856e3f40 HX |
48 | static int seqiv_aead_encrypt(struct aead_request *req) |
49 | { | |
50 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); | |
659e7f52 | 51 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
856e3f40 HX |
52 | struct aead_request *subreq = aead_request_ctx(req); |
53 | crypto_completion_t compl; | |
54 | void *data; | |
55 | u8 *info; | |
dd04446e | 56 | unsigned int ivsize = 8; |
856e3f40 HX |
57 | int err; |
58 | ||
dd04446e HX |
59 | if (req->cryptlen < ivsize) |
60 | return -EINVAL; | |
61 | ||
659e7f52 | 62 | aead_request_set_tfm(subreq, ctx->child); |
856e3f40 HX |
63 | |
64 | compl = req->base.complete; | |
65 | data = req->base.data; | |
66 | info = req->iv; | |
67 | ||
856e3f40 | 68 | if (req->src != req->dst) { |
8d605398 | 69 | SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull); |
856e3f40 | 70 | |
8d605398 | 71 | skcipher_request_set_sync_tfm(nreq, ctx->sknull); |
ef22871f HX |
72 | skcipher_request_set_callback(nreq, req->base.flags, |
73 | NULL, NULL); | |
74 | skcipher_request_set_crypt(nreq, req->src, req->dst, | |
75 | req->assoclen + req->cryptlen, | |
76 | NULL); | |
77 | ||
78 | err = crypto_skcipher_encrypt(nreq); | |
856e3f40 HX |
79 | if (err) |
80 | return err; | |
81 | } | |
82 | ||
83 | if (unlikely(!IS_ALIGNED((unsigned long)info, | |
84 | crypto_aead_alignmask(geniv) + 1))) { | |
7e33d4d4 Y |
85 | info = kmemdup(req->iv, ivsize, req->base.flags & |
86 | CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : | |
87 | GFP_ATOMIC); | |
856e3f40 HX |
88 | if (!info) |
89 | return -ENOMEM; | |
90 | ||
856e3f40 HX |
91 | compl = seqiv_aead_encrypt_complete; |
92 | data = req; | |
93 | } | |
94 | ||
95 | aead_request_set_callback(subreq, req->base.flags, compl, data); | |
96 | aead_request_set_crypt(subreq, req->dst, req->dst, | |
97 | req->cryptlen - ivsize, info); | |
374d4ad1 | 98 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
856e3f40 HX |
99 | |
100 | crypto_xor(info, ctx->salt, ivsize); | |
101 | scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1); | |
102 | ||
103 | err = crypto_aead_encrypt(subreq); | |
104 | if (unlikely(info != req->iv)) | |
105 | seqiv_aead_encrypt_complete2(req, err); | |
106 | return err; | |
107 | } | |
108 | ||
856e3f40 HX |
109 | static int seqiv_aead_decrypt(struct aead_request *req) |
110 | { | |
111 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); | |
659e7f52 | 112 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
856e3f40 HX |
113 | struct aead_request *subreq = aead_request_ctx(req); |
114 | crypto_completion_t compl; | |
115 | void *data; | |
dd04446e HX |
116 | unsigned int ivsize = 8; |
117 | ||
118 | if (req->cryptlen < ivsize + crypto_aead_authsize(geniv)) | |
119 | return -EINVAL; | |
856e3f40 | 120 | |
659e7f52 | 121 | aead_request_set_tfm(subreq, ctx->child); |
856e3f40 HX |
122 | |
123 | compl = req->base.complete; | |
124 | data = req->base.data; | |
125 | ||
856e3f40 HX |
126 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
127 | aead_request_set_crypt(subreq, req->src, req->dst, | |
128 | req->cryptlen - ivsize, req->iv); | |
374d4ad1 | 129 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
856e3f40 HX |
130 | |
131 | scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); | |
856e3f40 HX |
132 | |
133 | return crypto_aead_decrypt(subreq); | |
134 | } | |
135 | ||
0677157b | 136 | static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb) |
14df4d80 | 137 | { |
856e3f40 | 138 | struct aead_instance *inst; |
0677157b | 139 | int err; |
14df4d80 | 140 | |
e72b48c5 | 141 | inst = aead_geniv_alloc(tmpl, tb); |
14df4d80 HX |
142 | |
143 | if (IS_ERR(inst)) | |
0677157b HX |
144 | return PTR_ERR(inst); |
145 | ||
0677157b | 146 | err = -EINVAL; |
dd04446e | 147 | if (inst->alg.ivsize != sizeof(u64)) |
0677157b | 148 | goto free_inst; |
c0ecf891 | 149 | |
b7dcfab4 | 150 | inst->alg.encrypt = seqiv_aead_encrypt; |
856e3f40 | 151 | inst->alg.decrypt = seqiv_aead_decrypt; |
14df4d80 | 152 | |
659e7f52 HX |
153 | inst->alg.init = aead_init_geniv; |
154 | inst->alg.exit = aead_exit_geniv; | |
856e3f40 | 155 | |
659e7f52 | 156 | inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx); |
5964f26c | 157 | inst->alg.base.cra_ctxsize += inst->alg.ivsize; |
856e3f40 | 158 | |
0677157b | 159 | err = aead_register_instance(tmpl, inst); |
0f8f6d86 | 160 | if (err) { |
0677157b | 161 | free_inst: |
0f8f6d86 EB |
162 | inst->free(inst); |
163 | } | |
164 | return err; | |
14df4d80 HX |
165 | } |
166 | ||
0a270321 HX |
167 | static struct crypto_template seqiv_tmpl = { |
168 | .name = "seqiv", | |
4688111e | 169 | .create = seqiv_aead_create, |
0a270321 HX |
170 | .module = THIS_MODULE, |
171 | }; | |
172 | ||
173 | static int __init seqiv_module_init(void) | |
174 | { | |
8a2cd1c4 | 175 | return crypto_register_template(&seqiv_tmpl); |
0a270321 HX |
176 | } |
177 | ||
178 | static void __exit seqiv_module_exit(void) | |
179 | { | |
180 | crypto_unregister_template(&seqiv_tmpl); | |
181 | } | |
182 | ||
c4741b23 | 183 | subsys_initcall(seqiv_module_init); |
0a270321 HX |
184 | module_exit(seqiv_module_exit); |
185 | ||
186 | MODULE_LICENSE("GPL"); | |
187 | MODULE_DESCRIPTION("Sequence Number IV Generator"); | |
4943ba16 | 188 | MODULE_ALIAS_CRYPTO("seqiv"); |