blob: ad45b009774b1172eb9e7e34c3f2a14c1f6b65c4 [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 */
Herbert Xuf1c131b2016-11-22 20:08:19 +080010#include <crypto/internal/skcipher.h>
11#include <crypto/scatterwalk.h>
Rik Snelf19f5112007-09-19 20:23:13 +080012#include <linux/err.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/scatterlist.h>
17#include <linux/slab.h>
18
Jussi Kivilinnace004552011-11-09 11:56:06 +080019#include <crypto/xts.h>
Rik Snelf19f5112007-09-19 20:23:13 +080020#include <crypto/b128ops.h>
21#include <crypto/gf128mul.h>
22
Eric Biggersa874f592020-07-10 20:34:28 -070023struct xts_tfm_ctx {
Herbert Xuf1c131b2016-11-22 20:08:19 +080024 struct crypto_skcipher *child;
Rik Snelf19f5112007-09-19 20:23:13 +080025 struct crypto_cipher *tweak;
26};
27
Herbert Xuf1c131b2016-11-22 20:08:19 +080028struct xts_instance_ctx {
29 struct crypto_skcipher_spawn spawn;
30 char name[CRYPTO_MAX_ALG_NAME];
31};
32
Eric Biggersa874f592020-07-10 20:34:28 -070033struct xts_request_ctx {
Ondrej Mosnáčeke55318c2017-04-02 21:19:14 +020034 le128 t;
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +030035 struct scatterlist *tail;
36 struct scatterlist sg[2];
Herbert Xuf1c131b2016-11-22 20:08:19 +080037 struct skcipher_request subreq;
38};
39
Eric Biggersa874f592020-07-10 20:34:28 -070040static int xts_setkey(struct crypto_skcipher *parent, const u8 *key,
41 unsigned int keylen)
Rik Snelf19f5112007-09-19 20:23:13 +080042{
Eric Biggersa874f592020-07-10 20:34:28 -070043 struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(parent);
Herbert Xuf1c131b2016-11-22 20:08:19 +080044 struct crypto_skcipher *child;
45 struct crypto_cipher *tweak;
Rik Snelf19f5112007-09-19 20:23:13 +080046 int err;
47
Herbert Xuf1c131b2016-11-22 20:08:19 +080048 err = xts_verify_key(parent, key, keylen);
Stephan Mueller28856a92016-02-09 15:37:47 +010049 if (err)
50 return err;
Rik Snelf19f5112007-09-19 20:23:13 +080051
Herbert Xuf1c131b2016-11-22 20:08:19 +080052 keylen /= 2;
53
Lucas De Marchi25985ed2011-03-30 22:57:33 -030054 /* we need two cipher instances: one to compute the initial 'tweak'
Rik Snelf19f5112007-09-19 20:23:13 +080055 * by encrypting the IV (usually the 'plain' iv) and the other
56 * one to encrypt and decrypt the data */
57
58 /* tweak cipher, uses Key2 i.e. the second half of *key */
Herbert Xuf1c131b2016-11-22 20:08:19 +080059 tweak = ctx->tweak;
60 crypto_cipher_clear_flags(tweak, CRYPTO_TFM_REQ_MASK);
61 crypto_cipher_set_flags(tweak, crypto_skcipher_get_flags(parent) &
Rik Snelf19f5112007-09-19 20:23:13 +080062 CRYPTO_TFM_REQ_MASK);
Herbert Xuf1c131b2016-11-22 20:08:19 +080063 err = crypto_cipher_setkey(tweak, key + keylen, keylen);
Rik Snelf19f5112007-09-19 20:23:13 +080064 if (err)
65 return err;
66
Rik Snelf19f5112007-09-19 20:23:13 +080067 /* data cipher, uses Key1 i.e. the first half of *key */
Herbert Xuf1c131b2016-11-22 20:08:19 +080068 child = ctx->child;
69 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
70 crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
71 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -060072 return crypto_skcipher_setkey(child, key, keylen);
Rik Snelf19f5112007-09-19 20:23:13 +080073}
74
Ondrej Mosnacek78105c72018-09-11 09:40:08 +020075/*
76 * We compute the tweak masks twice (both before and after the ECB encryption or
77 * decryption) to avoid having to allocate a temporary buffer and/or make
78 * mutliple calls to the 'ecb(..)' instance, which usually would be slower than
79 * just doing the gf128mul_x_ble() calls again.
80 */
Eric Biggersa874f592020-07-10 20:34:28 -070081static int xts_xor_tweak(struct skcipher_request *req, bool second_pass,
82 bool enc)
Rik Snelf19f5112007-09-19 20:23:13 +080083{
Eric Biggersa874f592020-07-10 20:34:28 -070084 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +020085 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +030086 const bool cts = (req->cryptlen % XTS_BLOCK_SIZE);
Herbert Xuf1c131b2016-11-22 20:08:19 +080087 const int bs = XTS_BLOCK_SIZE;
88 struct skcipher_walk w;
Ondrej Mosnacek78105c72018-09-11 09:40:08 +020089 le128 t = rctx->t;
Herbert Xuf1c131b2016-11-22 20:08:19 +080090 int err;
Rik Snelf19f5112007-09-19 20:23:13 +080091
Ondrej Mosnacek78105c72018-09-11 09:40:08 +020092 if (second_pass) {
93 req = &rctx->subreq;
94 /* set to our TFM to enforce correct alignment: */
95 skcipher_request_set_tfm(req, tfm);
Herbert Xuf1c131b2016-11-22 20:08:19 +080096 }
Ondrej Mosnacek78105c72018-09-11 09:40:08 +020097 err = skcipher_walk_virt(&w, req, false);
Herbert Xuf1c131b2016-11-22 20:08:19 +080098
99 while (w.nbytes) {
100 unsigned int avail = w.nbytes;
Ondrej Mosnáčeke55318c2017-04-02 21:19:14 +0200101 le128 *wsrc;
102 le128 *wdst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800103
104 wsrc = w.src.virt.addr;
105 wdst = w.dst.virt.addr;
106
107 do {
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300108 if (unlikely(cts) &&
109 w.total - w.nbytes + avail < 2 * XTS_BLOCK_SIZE) {
110 if (!enc) {
111 if (second_pass)
112 rctx->t = t;
113 gf128mul_x_ble(&t, &t);
114 }
115 le128_xor(wdst, &t, wsrc);
116 if (enc && second_pass)
117 gf128mul_x_ble(&rctx->t, &t);
118 skcipher_walk_done(&w, avail - bs);
119 return 0;
120 }
121
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200122 le128_xor(wdst++, &t, wsrc++);
123 gf128mul_x_ble(&t, &t);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800124 } while ((avail -= bs) >= bs);
125
126 err = skcipher_walk_done(&w, avail);
127 }
128
Herbert Xuf1c131b2016-11-22 20:08:19 +0800129 return err;
130}
131
Eric Biggersa874f592020-07-10 20:34:28 -0700132static int xts_xor_tweak_pre(struct skcipher_request *req, bool enc)
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200133{
Eric Biggersa874f592020-07-10 20:34:28 -0700134 return xts_xor_tweak(req, false, enc);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200135}
136
Eric Biggersa874f592020-07-10 20:34:28 -0700137static int xts_xor_tweak_post(struct skcipher_request *req, bool enc)
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200138{
Eric Biggersa874f592020-07-10 20:34:28 -0700139 return xts_xor_tweak(req, true, enc);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200140}
141
Eric Biggersa874f592020-07-10 20:34:28 -0700142static void xts_cts_done(struct crypto_async_request *areq, int err)
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300143{
144 struct skcipher_request *req = areq->data;
145 le128 b;
146
147 if (!err) {
Eric Biggersa874f592020-07-10 20:34:28 -0700148 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300149
150 scatterwalk_map_and_copy(&b, rctx->tail, 0, XTS_BLOCK_SIZE, 0);
151 le128_xor(&b, &rctx->t, &b);
152 scatterwalk_map_and_copy(&b, rctx->tail, 0, XTS_BLOCK_SIZE, 1);
153 }
154
155 skcipher_request_complete(req, err);
156}
157
Eric Biggersa874f592020-07-10 20:34:28 -0700158static int xts_cts_final(struct skcipher_request *req,
159 int (*crypt)(struct skcipher_request *req))
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300160{
Eric Biggersa874f592020-07-10 20:34:28 -0700161 const struct xts_tfm_ctx *ctx =
162 crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300163 int offset = req->cryptlen & ~(XTS_BLOCK_SIZE - 1);
Eric Biggersa874f592020-07-10 20:34:28 -0700164 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300165 struct skcipher_request *subreq = &rctx->subreq;
166 int tail = req->cryptlen % XTS_BLOCK_SIZE;
167 le128 b[2];
168 int err;
169
170 rctx->tail = scatterwalk_ffwd(rctx->sg, req->dst,
171 offset - XTS_BLOCK_SIZE);
172
173 scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE, 0);
Ard Biesheuvel958ea4e2020-07-21 09:05:54 +0300174 b[1] = b[0];
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300175 scatterwalk_map_and_copy(b, req->src, offset, tail, 0);
176
177 le128_xor(b, &rctx->t, b);
178
179 scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE + tail, 1);
180
181 skcipher_request_set_tfm(subreq, ctx->child);
Eric Biggersa874f592020-07-10 20:34:28 -0700182 skcipher_request_set_callback(subreq, req->base.flags, xts_cts_done,
183 req);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300184 skcipher_request_set_crypt(subreq, rctx->tail, rctx->tail,
185 XTS_BLOCK_SIZE, NULL);
186
187 err = crypt(subreq);
188 if (err)
189 return err;
190
191 scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE, 0);
192 le128_xor(b, &rctx->t, b);
193 scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE, 1);
194
195 return 0;
196}
197
Eric Biggersa874f592020-07-10 20:34:28 -0700198static void xts_encrypt_done(struct crypto_async_request *areq, int err)
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200199{
200 struct skcipher_request *req = areq->data;
201
Herbert Xu44427c02019-04-15 14:35:19 +0800202 if (!err) {
Eric Biggersa874f592020-07-10 20:34:28 -0700203 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Herbert Xu44427c02019-04-15 14:35:19 +0800204
205 rctx->subreq.base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
Eric Biggersa874f592020-07-10 20:34:28 -0700206 err = xts_xor_tweak_post(req, true);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300207
208 if (!err && unlikely(req->cryptlen % XTS_BLOCK_SIZE)) {
Eric Biggersa874f592020-07-10 20:34:28 -0700209 err = xts_cts_final(req, crypto_skcipher_encrypt);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300210 if (err == -EINPROGRESS)
211 return;
212 }
Herbert Xu44427c02019-04-15 14:35:19 +0800213 }
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200214
215 skcipher_request_complete(req, err);
216}
217
Eric Biggersa874f592020-07-10 20:34:28 -0700218static void xts_decrypt_done(struct crypto_async_request *areq, int err)
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300219{
220 struct skcipher_request *req = areq->data;
221
222 if (!err) {
Eric Biggersa874f592020-07-10 20:34:28 -0700223 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300224
225 rctx->subreq.base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
Eric Biggersa874f592020-07-10 20:34:28 -0700226 err = xts_xor_tweak_post(req, false);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300227
228 if (!err && unlikely(req->cryptlen % XTS_BLOCK_SIZE)) {
Eric Biggersa874f592020-07-10 20:34:28 -0700229 err = xts_cts_final(req, crypto_skcipher_decrypt);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300230 if (err == -EINPROGRESS)
231 return;
232 }
233 }
234
235 skcipher_request_complete(req, err);
236}
237
Eric Biggersa874f592020-07-10 20:34:28 -0700238static int xts_init_crypt(struct skcipher_request *req,
239 crypto_completion_t compl)
Herbert Xuf1c131b2016-11-22 20:08:19 +0800240{
Eric Biggersa874f592020-07-10 20:34:28 -0700241 const struct xts_tfm_ctx *ctx =
242 crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
243 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200244 struct skcipher_request *subreq = &rctx->subreq;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800245
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300246 if (req->cryptlen < XTS_BLOCK_SIZE)
247 return -EINVAL;
248
Herbert Xuf1c131b2016-11-22 20:08:19 +0800249 skcipher_request_set_tfm(subreq, ctx->child);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300250 skcipher_request_set_callback(subreq, req->base.flags, compl, req);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200251 skcipher_request_set_crypt(subreq, req->dst, req->dst,
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300252 req->cryptlen & ~(XTS_BLOCK_SIZE - 1), NULL);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800253
254 /* calculate first value of T */
255 crypto_cipher_encrypt_one(ctx->tweak, (u8 *)&rctx->t, req->iv);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300256
257 return 0;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800258}
259
Eric Biggersa874f592020-07-10 20:34:28 -0700260static int xts_encrypt(struct skcipher_request *req)
Herbert Xuf1c131b2016-11-22 20:08:19 +0800261{
Eric Biggersa874f592020-07-10 20:34:28 -0700262 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200263 struct skcipher_request *subreq = &rctx->subreq;
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300264 int err;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800265
Eric Biggersa874f592020-07-10 20:34:28 -0700266 err = xts_init_crypt(req, xts_encrypt_done) ?:
267 xts_xor_tweak_pre(req, true) ?:
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300268 crypto_skcipher_encrypt(subreq) ?:
Eric Biggersa874f592020-07-10 20:34:28 -0700269 xts_xor_tweak_post(req, true);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300270
271 if (err || likely((req->cryptlen % XTS_BLOCK_SIZE) == 0))
272 return err;
273
Eric Biggersa874f592020-07-10 20:34:28 -0700274 return xts_cts_final(req, crypto_skcipher_encrypt);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800275}
276
Eric Biggersa874f592020-07-10 20:34:28 -0700277static int xts_decrypt(struct skcipher_request *req)
Herbert Xuf1c131b2016-11-22 20:08:19 +0800278{
Eric Biggersa874f592020-07-10 20:34:28 -0700279 struct xts_request_ctx *rctx = skcipher_request_ctx(req);
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200280 struct skcipher_request *subreq = &rctx->subreq;
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300281 int err;
Ondrej Mosnacek78105c72018-09-11 09:40:08 +0200282
Eric Biggersa874f592020-07-10 20:34:28 -0700283 err = xts_init_crypt(req, xts_decrypt_done) ?:
284 xts_xor_tweak_pre(req, false) ?:
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300285 crypto_skcipher_decrypt(subreq) ?:
Eric Biggersa874f592020-07-10 20:34:28 -0700286 xts_xor_tweak_post(req, false);
Ard Biesheuvel8083b1b2019-08-09 20:14:57 +0300287
288 if (err || likely((req->cryptlen % XTS_BLOCK_SIZE) == 0))
289 return err;
290
Eric Biggersa874f592020-07-10 20:34:28 -0700291 return xts_cts_final(req, crypto_skcipher_decrypt);
Rik Snelf19f5112007-09-19 20:23:13 +0800292}
293
Eric Biggersa874f592020-07-10 20:34:28 -0700294static int xts_init_tfm(struct crypto_skcipher *tfm)
Rik Snelf19f5112007-09-19 20:23:13 +0800295{
Herbert Xuf1c131b2016-11-22 20:08:19 +0800296 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
297 struct xts_instance_ctx *ictx = skcipher_instance_ctx(inst);
Eric Biggersa874f592020-07-10 20:34:28 -0700298 struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800299 struct crypto_skcipher *child;
300 struct crypto_cipher *tweak;
Rik Snelf19f5112007-09-19 20:23:13 +0800301
Herbert Xuf1c131b2016-11-22 20:08:19 +0800302 child = crypto_spawn_skcipher(&ictx->spawn);
303 if (IS_ERR(child))
304 return PTR_ERR(child);
Rik Snelf19f5112007-09-19 20:23:13 +0800305
Herbert Xuf1c131b2016-11-22 20:08:19 +0800306 ctx->child = child;
307
308 tweak = crypto_alloc_cipher(ictx->name, 0, 0);
309 if (IS_ERR(tweak)) {
310 crypto_free_skcipher(ctx->child);
311 return PTR_ERR(tweak);
Rik Snelf19f5112007-09-19 20:23:13 +0800312 }
313
Herbert Xuf1c131b2016-11-22 20:08:19 +0800314 ctx->tweak = tweak;
Rik Snelf19f5112007-09-19 20:23:13 +0800315
Herbert Xuf1c131b2016-11-22 20:08:19 +0800316 crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(child) +
Eric Biggersa874f592020-07-10 20:34:28 -0700317 sizeof(struct xts_request_ctx));
Rik Snelf19f5112007-09-19 20:23:13 +0800318
319 return 0;
320}
321
Eric Biggersa874f592020-07-10 20:34:28 -0700322static void xts_exit_tfm(struct crypto_skcipher *tfm)
Rik Snelf19f5112007-09-19 20:23:13 +0800323{
Eric Biggersa874f592020-07-10 20:34:28 -0700324 struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800325
326 crypto_free_skcipher(ctx->child);
Rik Snelf19f5112007-09-19 20:23:13 +0800327 crypto_free_cipher(ctx->tweak);
328}
329
Eric Biggersa874f592020-07-10 20:34:28 -0700330static void xts_free_instance(struct skcipher_instance *inst)
Rik Snelf19f5112007-09-19 20:23:13 +0800331{
Eric Biggersa874f592020-07-10 20:34:28 -0700332 struct xts_instance_ctx *ictx = skcipher_instance_ctx(inst);
333
334 crypto_drop_skcipher(&ictx->spawn);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800335 kfree(inst);
Rik Snelf19f5112007-09-19 20:23:13 +0800336}
337
Eric Biggersa874f592020-07-10 20:34:28 -0700338static int xts_create(struct crypto_template *tmpl, struct rtattr **tb)
Rik Snelf19f5112007-09-19 20:23:13 +0800339{
Herbert Xuf1c131b2016-11-22 20:08:19 +0800340 struct skcipher_instance *inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800341 struct xts_instance_ctx *ctx;
342 struct skcipher_alg *alg;
343 const char *cipher_name;
Herbert Xu89027572017-02-26 12:24:10 +0800344 u32 mask;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800345 int err;
346
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700347 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
348 if (err)
349 return err;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800350
351 cipher_name = crypto_attr_alg_name(tb[1]);
352 if (IS_ERR(cipher_name))
353 return PTR_ERR(cipher_name);
354
355 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
356 if (!inst)
357 return -ENOMEM;
358
359 ctx = skcipher_instance_ctx(inst);
360
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800361 err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst),
362 cipher_name, 0, mask);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800363 if (err == -ENOENT) {
364 err = -ENAMETOOLONG;
365 if (snprintf(ctx->name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
366 cipher_name) >= CRYPTO_MAX_ALG_NAME)
367 goto err_free_inst;
368
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800369 err = crypto_grab_skcipher(&ctx->spawn,
370 skcipher_crypto_instance(inst),
371 ctx->name, 0, mask);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800372 }
373
374 if (err)
375 goto err_free_inst;
376
377 alg = crypto_skcipher_spawn_alg(&ctx->spawn);
378
379 err = -EINVAL;
380 if (alg->base.cra_blocksize != XTS_BLOCK_SIZE)
Eric Biggers732e5402020-02-25 20:59:24 -0800381 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800382
383 if (crypto_skcipher_alg_ivsize(alg))
Eric Biggers732e5402020-02-25 20:59:24 -0800384 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800385
386 err = crypto_inst_setname(skcipher_crypto_instance(inst), "xts",
387 &alg->base);
388 if (err)
Eric Biggers732e5402020-02-25 20:59:24 -0800389 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800390
391 err = -EINVAL;
392 cipher_name = alg->base.cra_name;
393
394 /* Alas we screwed up the naming so we have to mangle the
395 * cipher name.
396 */
397 if (!strncmp(cipher_name, "ecb(", 4)) {
398 unsigned len;
399
400 len = strlcpy(ctx->name, cipher_name + 4, sizeof(ctx->name));
401 if (len < 2 || len >= sizeof(ctx->name))
Eric Biggers732e5402020-02-25 20:59:24 -0800402 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800403
404 if (ctx->name[len - 1] != ')')
Eric Biggers732e5402020-02-25 20:59:24 -0800405 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800406
407 ctx->name[len - 1] = 0;
408
409 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
Christophe Jaillet5125e4e2017-09-26 08:17:44 +0200410 "xts(%s)", ctx->name) >= CRYPTO_MAX_ALG_NAME) {
411 err = -ENAMETOOLONG;
Eric Biggers732e5402020-02-25 20:59:24 -0800412 goto err_free_inst;
Christophe Jaillet5125e4e2017-09-26 08:17:44 +0200413 }
Herbert Xuf1c131b2016-11-22 20:08:19 +0800414 } else
Eric Biggers732e5402020-02-25 20:59:24 -0800415 goto err_free_inst;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800416
Herbert Xuf1c131b2016-11-22 20:08:19 +0800417 inst->alg.base.cra_priority = alg->base.cra_priority;
418 inst->alg.base.cra_blocksize = XTS_BLOCK_SIZE;
419 inst->alg.base.cra_alignmask = alg->base.cra_alignmask |
420 (__alignof__(u64) - 1);
421
422 inst->alg.ivsize = XTS_BLOCK_SIZE;
423 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) * 2;
424 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) * 2;
425
Eric Biggersa874f592020-07-10 20:34:28 -0700426 inst->alg.base.cra_ctxsize = sizeof(struct xts_tfm_ctx);
Herbert Xuf1c131b2016-11-22 20:08:19 +0800427
Eric Biggersa874f592020-07-10 20:34:28 -0700428 inst->alg.init = xts_init_tfm;
429 inst->alg.exit = xts_exit_tfm;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800430
Eric Biggersa874f592020-07-10 20:34:28 -0700431 inst->alg.setkey = xts_setkey;
432 inst->alg.encrypt = xts_encrypt;
433 inst->alg.decrypt = xts_decrypt;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800434
Eric Biggersa874f592020-07-10 20:34:28 -0700435 inst->free = xts_free_instance;
Herbert Xuf1c131b2016-11-22 20:08:19 +0800436
437 err = skcipher_register_instance(tmpl, inst);
Eric Biggers732e5402020-02-25 20:59:24 -0800438 if (err) {
Herbert Xuf1c131b2016-11-22 20:08:19 +0800439err_free_inst:
Eric Biggersa874f592020-07-10 20:34:28 -0700440 xts_free_instance(inst);
Eric Biggers732e5402020-02-25 20:59:24 -0800441 }
442 return err;
Rik Snelf19f5112007-09-19 20:23:13 +0800443}
444
Eric Biggersa874f592020-07-10 20:34:28 -0700445static struct crypto_template xts_tmpl = {
Rik Snelf19f5112007-09-19 20:23:13 +0800446 .name = "xts",
Eric Biggersa874f592020-07-10 20:34:28 -0700447 .create = xts_create,
Rik Snelf19f5112007-09-19 20:23:13 +0800448 .module = THIS_MODULE,
449};
450
Eric Biggersa874f592020-07-10 20:34:28 -0700451static int __init xts_module_init(void)
Rik Snelf19f5112007-09-19 20:23:13 +0800452{
Eric Biggersa874f592020-07-10 20:34:28 -0700453 return crypto_register_template(&xts_tmpl);
Rik Snelf19f5112007-09-19 20:23:13 +0800454}
455
Eric Biggersa874f592020-07-10 20:34:28 -0700456static void __exit xts_module_exit(void)
Rik Snelf19f5112007-09-19 20:23:13 +0800457{
Eric Biggersa874f592020-07-10 20:34:28 -0700458 crypto_unregister_template(&xts_tmpl);
Rik Snelf19f5112007-09-19 20:23:13 +0800459}
460
Eric Biggersa874f592020-07-10 20:34:28 -0700461subsys_initcall(xts_module_init);
462module_exit(xts_module_exit);
Rik Snelf19f5112007-09-19 20:23:13 +0800463
464MODULE_LICENSE("GPL");
465MODULE_DESCRIPTION("XTS block cipher mode");
Kees Cook4943ba12014-11-24 16:32:38 -0800466MODULE_ALIAS_CRYPTO("xts");