blob: 6c12f30dbdd6d19f3efc16a5da5c2f27d9a2cac6 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rik Snelf19f5112007-09-19 20:23:13 +08002/* XTS: as defined in IEEE1619/D16
3 * http://grouper.ieee.org/groups/1619/email/pdf00086.pdf
Rik Snelf19f5112007-09-19 20:23:13 +08004 *
5 * Copyright (c) 2007 Rik Snel <rsnel@cube.dyndns.org>
6 *
Corentin LABBEddbc7362016-08-10 11:29:33 +02007 * Based on ecb.c
Rik Snelf19f5112007-09-19 20:23:13 +08008 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
Rik Snelf19f5112007-09-19 20:23:13 +08009 */
Ard Biesheuvel0eb76ba2020-12-11 13:27:15 +010010#include <crypto/internal/cipher.h>
Herbert Xuf1c131b2016-11-22 20:08:19 +080011#include <crypto/internal/skcipher.h>
12#include <crypto/scatterwalk.h>
Rik Snelf19f5112007-09-19 20:23:13 +080013#include <linux/err.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/scatterlist.h>
18#include <linux/slab.h>
19
Jussi Kivilinnace004552011-11-09 11:56:06 +080020#include <crypto/xts.h>
Rik Snelf19f5112007-09-19 20:23:13 +080021#include <crypto/b128ops.h>
22#include <crypto/gf128mul.h>
23
Eric Biggersa874f592020-07-10 20:34:28 -070024struct xts_tfm_ctx {
Herbert Xuf1c131b2016-11-22 20:08:19 +080025 struct crypto_skcipher *child;
Rik Snelf19f5112007-09-19 20:23:13 +080026 struct crypto_cipher *tweak;
27};
28
Herbert Xuf1c131b2016-11-22 20:08:19 +080029struct xts_instance_ctx {
30 struct crypto_skcipher_spawn spawn;
31 char name[CRYPTO_MAX_ALG_NAME];
32};
33
Eric Biggersa874f592020-07-10 20:34:28 -070034struct xts_request_ctx {
Ondrej Mosnáčeke55318c2017-04-02 21:19:14 +020035 le128 t;
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +030036 struct scatterlist *tail;
37 struct scatterlist sg[2];
Herbert Xuf1c131b2016-11-22 20:08:19 +080038 struct skcipher_request subreq;
39};
40
Eric Biggersa874f592020-07-10 20:34:28 -070041static int xts_setkey(struct crypto_skcipher *parent, const u8 *key,
42 unsigned int keylen)
Rik Snelf19f5112007-09-19 20:23:13 +080043{
Eric Biggersa874f592020-07-10 20:34:28 -070044 struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(parent);
Herbert Xuf1c131b2016-11-22 20:08:19 +080045 struct crypto_skcipher *child;
46 struct crypto_cipher *tweak;
Rik Snelf19f5112007-09-19 20:23:13 +080047 int err;
48
Herbert Xuf1c131b2016-11-22 20:08:19 +080049 err = xts_verify_key(parent, key, keylen);
Stephan Mueller28856a92016-02-09 15:37:47 +010050 if (err)
51 return err;
Rik Snelf19f5112007-09-19 20:23:13 +080052
Herbert Xuf1c131b2016-11-22 20:08:19 +080053 keylen /= 2;
54
Lucas De Marchi25985ed2011-03-30 22:57:33 -030055 /* we need two cipher instances: one to compute the initial 'tweak'
Rik Snelf19f5112007-09-19 20:23:13 +080056 * by encrypting the IV (usually the 'plain' iv) and the other
57 * one to encrypt and decrypt the data */
58
59 /* tweak cipher, uses Key2 i.e. the second half of *key */
Herbert Xuf1c131b2016-11-22 20:08:19 +080060 tweak = ctx->tweak;
61 crypto_cipher_clear_flags(tweak, CRYPTO_TFM_REQ_MASK);
62 crypto_cipher_set_flags(tweak, crypto_skcipher_get_flags(parent) &
Rik Snelf19f5112007-09-19 20:23:13 +080063 CRYPTO_TFM_REQ_MASK);
Herbert Xuf1c131b2016-11-22 20:08:19 +080064 err = crypto_cipher_setkey(tweak, key + keylen, keylen);
Rik Snelf19f5112007-09-19 20:23:13 +080065 if (err)
66 return err;
67
Rik Snelf19f5112007-09-19 20:23:13 +080068 /* data cipher, uses Key1 i.e. the first half of *key */
Herbert Xuf1c131b2016-11-22 20:08:19 +080069 child = ctx->child;
70 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
71 crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
72 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -060073 return crypto_skcipher_setkey(child, key, keylen);
Rik Snelf19f5112007-09-19 20:23:13 +080074}
75
Ondrej Mosnacek78105c72018-09-11 09:40:08 +020076/*
77 * We compute the tweak masks twice (both before and after the ECB encryption or
78 * decryption) to avoid having to allocate a temporary buffer and/or make
79 * mutliple calls to the 'ecb(..)' instance, which usually would be slower than
80 * just doing the gf128mul_x_ble() calls again.
81 */
Eric Biggersa874f592020-07-10 20:34:28 -070082static int xts_xor_tweak(struct skcipher_request *req, bool second_pass,
83 bool enc)
Rik Snelf19f5112007-09-19 20:23:13 +080084{
Eric Biggersa874f592020-07-10 20:34:28 -070085 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +020086 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +030087 const bool cts = (req->cryptlen % XTS_BLOCK_SIZE);
Herbert Xuf1c131b2016-11-22 20:08:19 +080088 const int bs = XTS_BLOCK_SIZE;
89 struct skcipher_walk w;
Ondrej Mosnacek78105c72018-09-11 09:40:08 +020090 le128 t = rctx->t;
Herbert Xuf1c131b2016-11-22 20:08:19 +080091 int err;
Rik Snelf19f5112007-09-19 20:23:13 +080092
Ondrej Mosnacek78105c72018-09-11 09:40:08 +020093 if (second_pass) {
94 req = &rctx->subreq;
95 /* set to our TFM to enforce correct alignment: */
96 skcipher_request_set_tfm(req, tfm);
Herbert Xuf1c131b2016-11-22 20:08:19 +080097 }
Ondrej Mosnacek78105c72018-09-11 09:40:08 +020098 err = skcipher_walk_virt(&w, req, false);
Herbert Xuf1c131b2016-11-22 20:08:19 +080099
100 while (w.nbytes) {
101 unsigned int avail = w.nbytes;
Ondrej Mosnáčeke55318c2017-04-02 21:19:14 +0200102 le128 *wsrc;
103 le128 *wdst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800104
105 wsrc = w.src.virt.addr;
106 wdst = w.dst.virt.addr;
107
108 do {
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300109 if (unlikely(cts) &&
110 w.total - w.nbytes + avail < 2 * XTS_BLOCK_SIZE) {
111 if (!enc) {
112 if (second_pass)
113 rctx->t = t;
114 gf128mul_x_ble(&t, &t);
115 }
116 le128_xor(wdst, &t, wsrc);
117 if (enc && second_pass)
118 gf128mul_x_ble(&rctx->t, &t);
119 skcipher_walk_done(&w, avail - bs);
120 return 0;
121 }
122
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200123 le128_xor(wdst++, &t, wsrc++);
124 gf128mul_x_ble(&t, &t);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800125 } while ((avail -= bs) >= bs);
126
127 err = skcipher_walk_done(&w, avail);
128 }
129
Herbert Xuf1c131b2016-11-22 20:08:19 +0800130 return err;
131}
132
Eric Biggersa874f592020-07-10 20:34:28 -0700133static int xts_xor_tweak_pre(struct skcipher_request *req, bool enc)
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200134{
Eric Biggersa874f592020-07-10 20:34:28 -0700135 return xts_xor_tweak(req, false, enc);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200136}
137
Eric Biggersa874f592020-07-10 20:34:28 -0700138static int xts_xor_tweak_post(struct skcipher_request *req, bool enc)
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200139{
Eric Biggersa874f592020-07-10 20:34:28 -0700140 return xts_xor_tweak(req, true, enc);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200141}
142
Eric Biggersa874f592020-07-10 20:34:28 -0700143static void xts_cts_done(struct crypto_async_request *areq, int err)
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300144{
145 struct skcipher_request *req = areq->data;
146 le128 b;
147
148 if (!err) {
Eric Biggersa874f592020-07-10 20:34:28 -0700149 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300150
151 scatterwalk_map_and_copy(&b, rctx->tail, 0, XTS_BLOCK_SIZE, 0);
152 le128_xor(&b, &rctx->t, &b);
153 scatterwalk_map_and_copy(&b, rctx->tail, 0, XTS_BLOCK_SIZE, 1);
154 }
155
156 skcipher_request_complete(req, err);
157}
158
Eric Biggersa874f592020-07-10 20:34:28 -0700159static int xts_cts_final(struct skcipher_request *req,
160 int (*crypt)(struct skcipher_request *req))
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300161{
Eric Biggersa874f592020-07-10 20:34:28 -0700162 const struct xts_tfm_ctx *ctx =
163 crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300164 int offset = req->cryptlen & ~(XTS_BLOCK_SIZE - 1);
Eric Biggersa874f592020-07-10 20:34:28 -0700165 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300166 struct skcipher_request *subreq = &rctx->subreq;
167 int tail = req->cryptlen % XTS_BLOCK_SIZE;
168 le128 b[2];
169 int err;
170
171 rctx->tail = scatterwalk_ffwd(rctx->sg, req->dst,
172 offset - XTS_BLOCK_SIZE);
173
174 scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE, 0);
Ard Biesheuvel958ea4e2020-07-21 09:05:54 +0300175 b[1] = b[0];
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300176 scatterwalk_map_and_copy(b, req->src, offset, tail, 0);
177
178 le128_xor(b, &rctx->t, b);
179
180 scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE + tail, 1);
181
182 skcipher_request_set_tfm(subreq, ctx->child);
Eric Biggersa874f592020-07-10 20:34:28 -0700183 skcipher_request_set_callback(subreq, req->base.flags, xts_cts_done,
184 req);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300185 skcipher_request_set_crypt(subreq, rctx->tail, rctx->tail,
186 XTS_BLOCK_SIZE, NULL);
187
188 err = crypt(subreq);
189 if (err)
190 return err;
191
192 scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE, 0);
193 le128_xor(b, &rctx->t, b);
194 scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE, 1);
195
196 return 0;
197}
198
Eric Biggersa874f592020-07-10 20:34:28 -0700199static void xts_encrypt_done(struct crypto_async_request *areq, int err)
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200200{
201 struct skcipher_request *req = areq->data;
202
Herbert Xu44427c02019-04-15 14:35:19 +0800203 if (!err) {
Eric Biggersa874f592020-07-10 20:34:28 -0700204 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Herbert Xu44427c02019-04-15 14:35:19 +0800205
206 rctx->subreq.base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
Eric Biggersa874f592020-07-10 20:34:28 -0700207 err = xts_xor_tweak_post(req, true);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300208
209 if (!err && unlikely(req->cryptlen % XTS_BLOCK_SIZE)) {
Eric Biggersa874f592020-07-10 20:34:28 -0700210 err = xts_cts_final(req, crypto_skcipher_encrypt);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300211 if (err == -EINPROGRESS)
212 return;
213 }
Herbert Xu44427c02019-04-15 14:35:19 +0800214 }
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200215
216 skcipher_request_complete(req, err);
217}
218
Eric Biggersa874f592020-07-10 20:34:28 -0700219static void xts_decrypt_done(struct crypto_async_request *areq, int err)
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300220{
221 struct skcipher_request *req = areq->data;
222
223 if (!err) {
Eric Biggersa874f592020-07-10 20:34:28 -0700224 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300225
226 rctx->subreq.base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
Eric Biggersa874f592020-07-10 20:34:28 -0700227 err = xts_xor_tweak_post(req, false);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300228
229 if (!err && unlikely(req->cryptlen % XTS_BLOCK_SIZE)) {
Eric Biggersa874f592020-07-10 20:34:28 -0700230 err = xts_cts_final(req, crypto_skcipher_decrypt);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300231 if (err == -EINPROGRESS)
232 return;
233 }
234 }
235
236 skcipher_request_complete(req, err);
237}
238
Eric Biggersa874f592020-07-10 20:34:28 -0700239static int xts_init_crypt(struct skcipher_request *req,
240 crypto_completion_t compl)
Herbert Xuf1c131b2016-11-22 20:08:19 +0800241{
Eric Biggersa874f592020-07-10 20:34:28 -0700242 const struct xts_tfm_ctx *ctx =
243 crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
244 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200245 struct skcipher_request *subreq = &rctx->subreq;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800246
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300247 if (req->cryptlen < XTS_BLOCK_SIZE)
248 return -EINVAL;
249
Herbert Xuf1c131b2016-11-22 20:08:19 +0800250 skcipher_request_set_tfm(subreq, ctx->child);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300251 skcipher_request_set_callback(subreq, req->base.flags, compl, req);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200252 skcipher_request_set_crypt(subreq, req->dst, req->dst,
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300253 req->cryptlen & ~(XTS_BLOCK_SIZE - 1), NULL);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800254
255 /* calculate first value of T */
256 crypto_cipher_encrypt_one(ctx->tweak, (u8 *)&rctx->t, req->iv);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300257
258 return 0;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800259}
260
Eric Biggersa874f592020-07-10 20:34:28 -0700261static int xts_encrypt(struct skcipher_request *req)
Herbert Xuf1c131b2016-11-22 20:08:19 +0800262{
Eric Biggersa874f592020-07-10 20:34:28 -0700263 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200264 struct skcipher_request *subreq = &rctx->subreq;
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300265 int err;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800266
Eric Biggersa874f592020-07-10 20:34:28 -0700267 err = xts_init_crypt(req, xts_encrypt_done) ?:
268 xts_xor_tweak_pre(req, true) ?:
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300269 crypto_skcipher_encrypt(subreq) ?:
Eric Biggersa874f592020-07-10 20:34:28 -0700270 xts_xor_tweak_post(req, true);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300271
272 if (err || likely((req->cryptlen % XTS_BLOCK_SIZE) == 0))
273 return err;
274
Eric Biggersa874f592020-07-10 20:34:28 -0700275 return xts_cts_final(req, crypto_skcipher_encrypt);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800276}
277
Eric Biggersa874f592020-07-10 20:34:28 -0700278static int xts_decrypt(struct skcipher_request *req)
Herbert Xuf1c131b2016-11-22 20:08:19 +0800279{
Eric Biggersa874f592020-07-10 20:34:28 -0700280 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200281 struct skcipher_request *subreq = &rctx->subreq;
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300282 int err;
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200283
Eric Biggersa874f592020-07-10 20:34:28 -0700284 err = xts_init_crypt(req, xts_decrypt_done) ?:
285 xts_xor_tweak_pre(req, false) ?:
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300286 crypto_skcipher_decrypt(subreq) ?:
Eric Biggersa874f592020-07-10 20:34:28 -0700287 xts_xor_tweak_post(req, false);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300288
289 if (err || likely((req->cryptlen % XTS_BLOCK_SIZE) == 0))
290 return err;
291
Eric Biggersa874f592020-07-10 20:34:28 -0700292 return xts_cts_final(req, crypto_skcipher_decrypt);
Rik Snelf19f5112007-09-19 20:23:13 +0800293}
294
Eric Biggersa874f592020-07-10 20:34:28 -0700295static int xts_init_tfm(struct crypto_skcipher *tfm)
Rik Snelf19f5112007-09-19 20:23:13 +0800296{
Herbert Xuf1c131b2016-11-22 20:08:19 +0800297 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
298 struct xts_instance_ctx *ictx = skcipher_instance_ctx(inst);
Eric Biggersa874f592020-07-10 20:34:28 -0700299 struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800300 struct crypto_skcipher *child;
301 struct crypto_cipher *tweak;
Rik Snelf19f5112007-09-19 20:23:13 +0800302
Herbert Xuf1c131b2016-11-22 20:08:19 +0800303 child = crypto_spawn_skcipher(&ictx->spawn);
304 if (IS_ERR(child))
305 return PTR_ERR(child);
Rik Snelf19f5112007-09-19 20:23:13 +0800306
Herbert Xuf1c131b2016-11-22 20:08:19 +0800307 ctx->child = child;
308
309 tweak = crypto_alloc_cipher(ictx->name, 0, 0);
310 if (IS_ERR(tweak)) {
311 crypto_free_skcipher(ctx->child);
312 return PTR_ERR(tweak);
Rik Snelf19f5112007-09-19 20:23:13 +0800313 }
314
Herbert Xuf1c131b2016-11-22 20:08:19 +0800315 ctx->tweak = tweak;
Rik Snelf19f5112007-09-19 20:23:13 +0800316
Herbert Xuf1c131b2016-11-22 20:08:19 +0800317 crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(child) +
Eric Biggersa874f592020-07-10 20:34:28 -0700318 sizeof(struct xts_request_ctx));
Rik Snelf19f5112007-09-19 20:23:13 +0800319
320 return 0;
321}
322
Eric Biggersa874f592020-07-10 20:34:28 -0700323static void xts_exit_tfm(struct crypto_skcipher *tfm)
Rik Snelf19f5112007-09-19 20:23:13 +0800324{
Eric Biggersa874f592020-07-10 20:34:28 -0700325 struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800326
327 crypto_free_skcipher(ctx->child);
Rik Snelf19f5112007-09-19 20:23:13 +0800328 crypto_free_cipher(ctx->tweak);
329}
330
Eric Biggersa874f592020-07-10 20:34:28 -0700331static void xts_free_instance(struct skcipher_instance *inst)
Rik Snelf19f5112007-09-19 20:23:13 +0800332{
Eric Biggersa874f592020-07-10 20:34:28 -0700333 struct xts_instance_ctx *ictx = skcipher_instance_ctx(inst);
334
335 crypto_drop_skcipher(&ictx->spawn);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800336 kfree(inst);
Rik Snelf19f5112007-09-19 20:23:13 +0800337}
338
Eric Biggersa874f592020-07-10 20:34:28 -0700339static int xts_create(struct crypto_template *tmpl, struct rtattr **tb)
Rik Snelf19f5112007-09-19 20:23:13 +0800340{
Herbert Xuf1c131b2016-11-22 20:08:19 +0800341 struct skcipher_instance *inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800342 struct xts_instance_ctx *ctx;
343 struct skcipher_alg *alg;
344 const char *cipher_name;
Herbert Xu89027572017-02-26 12:24:10 +0800345 u32 mask;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800346 int err;
347
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700348 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
349 if (err)
350 return err;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800351
352 cipher_name = crypto_attr_alg_name(tb[1]);
353 if (IS_ERR(cipher_name))
354 return PTR_ERR(cipher_name);
355
356 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
357 if (!inst)
358 return -ENOMEM;
359
360 ctx = skcipher_instance_ctx(inst);
361
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800362 err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst),
363 cipher_name, 0, mask);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800364 if (err == -ENOENT) {
365 err = -ENAMETOOLONG;
366 if (snprintf(ctx->name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
367 cipher_name) >= CRYPTO_MAX_ALG_NAME)
368 goto err_free_inst;
369
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800370 err = crypto_grab_skcipher(&ctx->spawn,
371 skcipher_crypto_instance(inst),
372 ctx->name, 0, mask);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800373 }
374
375 if (err)
376 goto err_free_inst;
377
378 alg = crypto_skcipher_spawn_alg(&ctx->spawn);
379
380 err = -EINVAL;
381 if (alg->base.cra_blocksize != XTS_BLOCK_SIZE)
Eric Biggers732e5402020-02-25 20:59:24 -0800382 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800383
384 if (crypto_skcipher_alg_ivsize(alg))
Eric Biggers732e5402020-02-25 20:59:24 -0800385 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800386
387 err = crypto_inst_setname(skcipher_crypto_instance(inst), "xts",
388 &alg->base);
389 if (err)
Eric Biggers732e5402020-02-25 20:59:24 -0800390 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800391
392 err = -EINVAL;
393 cipher_name = alg->base.cra_name;
394
395 /* Alas we screwed up the naming so we have to mangle the
396 * cipher name.
397 */
398 if (!strncmp(cipher_name, "ecb(", 4)) {
399 unsigned len;
400
401 len = strlcpy(ctx->name, cipher_name + 4, sizeof(ctx->name));
402 if (len < 2 || len >= sizeof(ctx->name))
Eric Biggers732e5402020-02-25 20:59:24 -0800403 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800404
405 if (ctx->name[len - 1] != ')')
Eric Biggers732e5402020-02-25 20:59:24 -0800406 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800407
408 ctx->name[len - 1] = 0;
409
410 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
Christophe Jaillet5125e4e2017-09-26 08:17:44 +0200411 "xts(%s)", ctx->name) >= CRYPTO_MAX_ALG_NAME) {
412 err = -ENAMETOOLONG;
Eric Biggers732e5402020-02-25 20:59:24 -0800413 goto err_free_inst;
Christophe Jaillet5125e4e2017-09-26 08:17:44 +0200414 }
Herbert Xuf1c131b2016-11-22 20:08:19 +0800415 } else
Eric Biggers732e5402020-02-25 20:59:24 -0800416 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800417
Herbert Xuf1c131b2016-11-22 20:08:19 +0800418 inst->alg.base.cra_priority = alg->base.cra_priority;
419 inst->alg.base.cra_blocksize = XTS_BLOCK_SIZE;
420 inst->alg.base.cra_alignmask = alg->base.cra_alignmask |
421 (__alignof__(u64) - 1);
422
423 inst->alg.ivsize = XTS_BLOCK_SIZE;
424 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) * 2;
425 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) * 2;
426
Eric Biggersa874f592020-07-10 20:34:28 -0700427 inst->alg.base.cra_ctxsize = sizeof(struct xts_tfm_ctx);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800428
Eric Biggersa874f592020-07-10 20:34:28 -0700429 inst->alg.init = xts_init_tfm;
430 inst->alg.exit = xts_exit_tfm;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800431
Eric Biggersa874f592020-07-10 20:34:28 -0700432 inst->alg.setkey = xts_setkey;
433 inst->alg.encrypt = xts_encrypt;
434 inst->alg.decrypt = xts_decrypt;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800435
Eric Biggersa874f592020-07-10 20:34:28 -0700436 inst->free = xts_free_instance;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800437
438 err = skcipher_register_instance(tmpl, inst);
Eric Biggers732e5402020-02-25 20:59:24 -0800439 if (err) {
Herbert Xuf1c131b2016-11-22 20:08:19 +0800440err_free_inst:
Eric Biggersa874f592020-07-10 20:34:28 -0700441 xts_free_instance(inst);
Eric Biggers732e5402020-02-25 20:59:24 -0800442 }
443 return err;
Rik Snelf19f5112007-09-19 20:23:13 +0800444}
445
Eric Biggersa874f592020-07-10 20:34:28 -0700446static struct crypto_template xts_tmpl = {
Rik Snelf19f5112007-09-19 20:23:13 +0800447 .name = "xts",
Eric Biggersa874f592020-07-10 20:34:28 -0700448 .create = xts_create,
Rik Snelf19f5112007-09-19 20:23:13 +0800449 .module = THIS_MODULE,
450};
451
Eric Biggersa874f592020-07-10 20:34:28 -0700452static int __init xts_module_init(void)
Rik Snelf19f5112007-09-19 20:23:13 +0800453{
Eric Biggersa874f592020-07-10 20:34:28 -0700454 return crypto_register_template(&xts_tmpl);
Rik Snelf19f5112007-09-19 20:23:13 +0800455}
456
Eric Biggersa874f592020-07-10 20:34:28 -0700457static void __exit xts_module_exit(void)
Rik Snelf19f5112007-09-19 20:23:13 +0800458{
Eric Biggersa874f592020-07-10 20:34:28 -0700459 crypto_unregister_template(&xts_tmpl);
Rik Snelf19f5112007-09-19 20:23:13 +0800460}
461
Eric Biggersa874f592020-07-10 20:34:28 -0700462subsys_initcall(xts_module_init);
463module_exit(xts_module_exit);
Rik Snelf19f5112007-09-19 20:23:13 +0800464
465MODULE_LICENSE("GPL");
466MODULE_DESCRIPTION("XTS block cipher mode");
Kees Cook4943ba12014-11-24 16:32:38 -0800467MODULE_ALIAS_CRYPTO("xts");
Ard Biesheuvel0eb76ba2020-12-11 13:27:15 +0100468MODULE_IMPORT_NS(CRYPTO_INTERNAL);