^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* XTS: as defined in IEEE1619/D16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * http://grouper.ieee.org/groups/1619/email/pdf00086.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2007 Rik Snel <rsnel@cube.dyndns.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Based on ecb.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/internal/cipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <crypto/xts.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <crypto/b128ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <crypto/gf128mul.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct xts_tfm_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct crypto_skcipher *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct crypto_cipher *tweak;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct xts_instance_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct crypto_skcipher_spawn spawn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) char name[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct xts_request_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) le128 t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct scatterlist *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct scatterlist sg[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct skcipher_request subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static int xts_setkey(struct crypto_skcipher *parent, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct crypto_skcipher *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct crypto_cipher *tweak;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) err = xts_verify_key(parent, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) keylen /= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* we need two cipher instances: one to compute the initial 'tweak'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * by encrypting the IV (usually the 'plain' iv) and the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * one to encrypt and decrypt the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* tweak cipher, uses Key2 i.e. the second half of *key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) tweak = ctx->tweak;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) crypto_cipher_clear_flags(tweak, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) crypto_cipher_set_flags(tweak, crypto_skcipher_get_flags(parent) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) err = crypto_cipher_setkey(tweak, key + keylen, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* data cipher, uses Key1 i.e. the first half of *key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) child = ctx->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return crypto_skcipher_setkey(child, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * We compute the tweak masks twice (both before and after the ECB encryption or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * decryption) to avoid having to allocate a temporary buffer and/or make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * mutliple calls to the 'ecb(..)' instance, which usually would be slower than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * just doing the gf128mul_x_ble() calls again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static int xts_xor_tweak(struct skcipher_request *req, bool second_pass,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) bool enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct xts_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) const bool cts = (req->cryptlen % XTS_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) const int bs = XTS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct skcipher_walk w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) le128 t = rctx->t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (second_pass) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) req = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* set to our TFM to enforce correct alignment: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) skcipher_request_set_tfm(req, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) err = skcipher_walk_virt(&w, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) while (w.nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) unsigned int avail = w.nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) le128 *wsrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) le128 *wdst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) wsrc = w.src.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) wdst = w.dst.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (unlikely(cts) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) w.total - w.nbytes + avail < 2 * XTS_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!enc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (second_pass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) rctx->t = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) gf128mul_x_ble(&t, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) le128_xor(wdst, &t, wsrc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (enc && second_pass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) gf128mul_x_ble(&rctx->t, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) skcipher_walk_done(&w, avail - bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) le128_xor(wdst++, &t, wsrc++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) gf128mul_x_ble(&t, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) } while ((avail -= bs) >= bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) err = skcipher_walk_done(&w, avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static int xts_xor_tweak_pre(struct skcipher_request *req, bool enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return xts_xor_tweak(req, false, enc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int xts_xor_tweak_post(struct skcipher_request *req, bool enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return xts_xor_tweak(req, true, enc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static void xts_cts_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct skcipher_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) le128 b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct xts_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) scatterwalk_map_and_copy(&b, rctx->tail, 0, XTS_BLOCK_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) le128_xor(&b, &rctx->t, &b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) scatterwalk_map_and_copy(&b, rctx->tail, 0, XTS_BLOCK_SIZE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) skcipher_request_complete(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int xts_cts_final(struct skcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int (*crypt)(struct skcipher_request *req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) const struct xts_tfm_ctx *ctx =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int offset = req->cryptlen & ~(XTS_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct xts_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct skcipher_request *subreq = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int tail = req->cryptlen % XTS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) le128 b[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) rctx->tail = scatterwalk_ffwd(rctx->sg, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) offset - XTS_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) b[1] = b[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) scatterwalk_map_and_copy(b, req->src, offset, tail, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) le128_xor(b, &rctx->t, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE + tail, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) skcipher_request_set_tfm(subreq, ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) skcipher_request_set_callback(subreq, req->base.flags, xts_cts_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) skcipher_request_set_crypt(subreq, rctx->tail, rctx->tail,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) XTS_BLOCK_SIZE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) err = crypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) le128_xor(b, &rctx->t, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static void xts_encrypt_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct skcipher_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct xts_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) rctx->subreq.base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) err = xts_xor_tweak_post(req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (!err && unlikely(req->cryptlen % XTS_BLOCK_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) err = xts_cts_final(req, crypto_skcipher_encrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) skcipher_request_complete(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static void xts_decrypt_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct skcipher_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct xts_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) rctx->subreq.base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) err = xts_xor_tweak_post(req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!err && unlikely(req->cryptlen % XTS_BLOCK_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) err = xts_cts_final(req, crypto_skcipher_decrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) skcipher_request_complete(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static int xts_init_crypt(struct skcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) crypto_completion_t compl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) const struct xts_tfm_ctx *ctx =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct xts_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct skcipher_request *subreq = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (req->cryptlen < XTS_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) skcipher_request_set_tfm(subreq, ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) skcipher_request_set_callback(subreq, req->base.flags, compl, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) skcipher_request_set_crypt(subreq, req->dst, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) req->cryptlen & ~(XTS_BLOCK_SIZE - 1), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* calculate first value of T */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) crypto_cipher_encrypt_one(ctx->tweak, (u8 *)&rctx->t, req->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int xts_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct xts_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct skcipher_request *subreq = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) err = xts_init_crypt(req, xts_encrypt_done) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) xts_xor_tweak_pre(req, true) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) crypto_skcipher_encrypt(subreq) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) xts_xor_tweak_post(req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (err || likely((req->cryptlen % XTS_BLOCK_SIZE) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return xts_cts_final(req, crypto_skcipher_encrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static int xts_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct xts_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct skcipher_request *subreq = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) err = xts_init_crypt(req, xts_decrypt_done) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) xts_xor_tweak_pre(req, false) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) crypto_skcipher_decrypt(subreq) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) xts_xor_tweak_post(req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (err || likely((req->cryptlen % XTS_BLOCK_SIZE) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return xts_cts_final(req, crypto_skcipher_decrypt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static int xts_init_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct skcipher_instance *inst = skcipher_alg_instance(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct xts_instance_ctx *ictx = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct crypto_skcipher *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct crypto_cipher *tweak;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) child = crypto_spawn_skcipher(&ictx->spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (IS_ERR(child))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return PTR_ERR(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ctx->child = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) tweak = crypto_alloc_cipher(ictx->name, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (IS_ERR(tweak)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) crypto_free_skcipher(ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return PTR_ERR(tweak);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) ctx->tweak = tweak;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(child) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) sizeof(struct xts_request_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static void xts_exit_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) crypto_free_skcipher(ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) crypto_free_cipher(ctx->tweak);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static void xts_free_instance(struct skcipher_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct xts_instance_ctx *ictx = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) crypto_drop_skcipher(&ictx->spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static int xts_create(struct crypto_template *tmpl, struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct skcipher_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) struct xts_instance_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct skcipher_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) const char *cipher_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) cipher_name = crypto_attr_alg_name(tb[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (IS_ERR(cipher_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return PTR_ERR(cipher_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) ctx = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) cipher_name, 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (snprintf(ctx->name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) cipher_name) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) err = crypto_grab_skcipher(&ctx->spawn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) skcipher_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) ctx->name, 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) alg = crypto_skcipher_spawn_alg(&ctx->spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (alg->base.cra_blocksize != XTS_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (crypto_skcipher_alg_ivsize(alg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) err = crypto_inst_setname(skcipher_crypto_instance(inst), "xts",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) &alg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) cipher_name = alg->base.cra_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) /* Alas we screwed up the naming so we have to mangle the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * cipher name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (!strncmp(cipher_name, "ecb(", 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) unsigned len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) len = strlcpy(ctx->name, cipher_name + 4, sizeof(ctx->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (len < 2 || len >= sizeof(ctx->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (ctx->name[len - 1] != ')')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) ctx->name[len - 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) "xts(%s)", ctx->name) >= CRYPTO_MAX_ALG_NAME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) inst->alg.base.cra_priority = alg->base.cra_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) inst->alg.base.cra_blocksize = XTS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) inst->alg.base.cra_alignmask = alg->base.cra_alignmask |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) (__alignof__(u64) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) inst->alg.ivsize = XTS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) inst->alg.base.cra_ctxsize = sizeof(struct xts_tfm_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) inst->alg.init = xts_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) inst->alg.exit = xts_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) inst->alg.setkey = xts_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) inst->alg.encrypt = xts_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) inst->alg.decrypt = xts_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) inst->free = xts_free_instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) err = skcipher_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) xts_free_instance(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static struct crypto_template xts_tmpl = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) .name = "xts",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) .create = xts_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) static int __init xts_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return crypto_register_template(&xts_tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static void __exit xts_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) crypto_unregister_template(&xts_tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) subsys_initcall(xts_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) module_exit(xts_module_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) MODULE_DESCRIPTION("XTS block cipher mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) MODULE_ALIAS_CRYPTO("xts");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) MODULE_IMPORT_NS(CRYPTO_INTERNAL);