^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) /* LRW: as defined by Cyril Guyot in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * http://grouper.ieee.org/groups/1619/email/pdf00017.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2006 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) /* This implementation is checked against the test vectors in the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * document and by a test vector provided by Ken Buchanan at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * https://www.mail-archive.com/stds-p1619@listserv.ieee.org/msg00173.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * The test vectors are included in the testing module tcrypt.[ch] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <crypto/b128ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <crypto/gf128mul.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define LRW_BLOCK_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct lrw_tfm_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct crypto_skcipher *child;
^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) * optimizes multiplying a random (non incrementing, as at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * start of a new sector) value with key2, we could also have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * used 4k optimization tables or no optimization at all. In the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * latter case we would have to store key2 here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct gf128mul_64k *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * stores:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * key2*{ 0,0,...0,0,0,0,1 }, key2*{ 0,0,...0,0,0,1,1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * key2*{ 0,0,...0,0,1,1,1 }, key2*{ 0,0,...0,1,1,1,1 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * key2*{ 0,0,...1,1,1,1,1 }, etc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * needed for optimized multiplication of incrementing values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * with key2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) be128 mulinc[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct lrw_request_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) be128 t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct skcipher_request subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static inline void lrw_setbit128_bbe(void *b, int bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) __set_bit(bit ^ (0x80 -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #ifdef __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) BITS_PER_LONG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) BITS_PER_BYTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ), b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static int lrw_setkey(struct crypto_skcipher *parent, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct lrw_tfm_ctx *ctx = crypto_skcipher_ctx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct crypto_skcipher *child = ctx->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int err, bsize = LRW_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) const u8 *tweak = key + keylen - bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) be128 tmp = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) err = crypto_skcipher_setkey(child, key, keylen - bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (ctx->table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) gf128mul_free_64k(ctx->table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* initialize multiplication table for Key2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ctx->table = gf128mul_init_64k_bbe((be128 *)tweak);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!ctx->table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* initialize optimization table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) for (i = 0; i < 128; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) lrw_setbit128_bbe(&tmp, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) ctx->mulinc[i] = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) gf128mul_64k_bbe(&ctx->mulinc[i], ctx->table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * Returns the number of trailing '1' bits in the words of the counter, which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * represented by 4 32-bit words, arranged from least to most significant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * At the same time, increments the counter by one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * For example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * u32 counter[4] = { 0xFFFFFFFF, 0x1, 0x0, 0x0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * int i = lrw_next_index(&counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * // i == 33, counter == { 0x0, 0x2, 0x0, 0x0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int lrw_next_index(u32 *counter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) int i, res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) for (i = 0; i < 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (counter[i] + 1 != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return res + ffz(counter[i]++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) counter[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) res += 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * If we get here, then x == 128 and we are incrementing the counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * from all ones to all zeros. This means we must return index 127, i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * the one corresponding to key2*{ 1,...,1 }.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return 127;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * We compute the tweak masks twice (both before and after the ECB encryption or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * decryption) to avoid having to allocate a temporary buffer and/or make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * mutliple calls to the 'ecb(..)' instance, which usually would be slower than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * just doing the lrw_next_index() calls again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int lrw_xor_tweak(struct skcipher_request *req, bool second_pass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) const int bs = LRW_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) const struct lrw_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct lrw_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) be128 t = rctx->t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct skcipher_walk w;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) __be32 *iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) u32 counter[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (second_pass) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) req = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* set to our TFM to enforce correct alignment: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) skcipher_request_set_tfm(req, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) err = skcipher_walk_virt(&w, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) iv = (__be32 *)w.iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) counter[0] = be32_to_cpu(iv[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) counter[1] = be32_to_cpu(iv[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) counter[2] = be32_to_cpu(iv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) counter[3] = be32_to_cpu(iv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) while (w.nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) unsigned int avail = w.nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) be128 *wsrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) be128 *wdst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) wsrc = w.src.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) wdst = w.dst.virt.addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) be128_xor(wdst++, &t, wsrc++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* T <- I*Key2, using the optimization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * discussed in the specification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) be128_xor(&t, &t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) &ctx->mulinc[lrw_next_index(counter)]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) } while ((avail -= bs) >= bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (second_pass && w.nbytes == w.total) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) iv[0] = cpu_to_be32(counter[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) iv[1] = cpu_to_be32(counter[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) iv[2] = cpu_to_be32(counter[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) iv[3] = cpu_to_be32(counter[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) err = skcipher_walk_done(&w, avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static int lrw_xor_tweak_pre(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return lrw_xor_tweak(req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int lrw_xor_tweak_post(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return lrw_xor_tweak(req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static void lrw_crypt_done(struct crypto_async_request *areq, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct skcipher_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct lrw_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) rctx->subreq.base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) err = lrw_xor_tweak_post(req);
^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) skcipher_request_complete(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static void lrw_init_crypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) const struct lrw_tfm_ctx *ctx =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct lrw_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct skcipher_request *subreq = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) skcipher_request_set_tfm(subreq, ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) skcipher_request_set_callback(subreq, req->base.flags, lrw_crypt_done,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* pass req->iv as IV (will be used by xor_tweak, ECB will ignore it) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) skcipher_request_set_crypt(subreq, req->dst, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) req->cryptlen, req->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* calculate first value of T */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) memcpy(&rctx->t, req->iv, sizeof(rctx->t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* T <- I*Key2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) gf128mul_64k_bbe(&rctx->t, ctx->table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static int lrw_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct lrw_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) struct skcipher_request *subreq = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) lrw_init_crypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return lrw_xor_tweak_pre(req) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) crypto_skcipher_encrypt(subreq) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) lrw_xor_tweak_post(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static int lrw_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct lrw_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct skcipher_request *subreq = &rctx->subreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) lrw_init_crypt(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return lrw_xor_tweak_pre(req) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) crypto_skcipher_decrypt(subreq) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) lrw_xor_tweak_post(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static int lrw_init_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct skcipher_instance *inst = skcipher_alg_instance(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct lrw_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) struct crypto_skcipher *cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) cipher = crypto_spawn_skcipher(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (IS_ERR(cipher))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return PTR_ERR(cipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ctx->child = cipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(cipher) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) sizeof(struct lrw_request_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static void lrw_exit_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct lrw_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (ctx->table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) gf128mul_free_64k(ctx->table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) crypto_free_skcipher(ctx->child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static void lrw_free_instance(struct skcipher_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) crypto_drop_skcipher(skcipher_instance_ctx(inst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int lrw_create(struct crypto_template *tmpl, struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct crypto_skcipher_spawn *spawn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct skcipher_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct skcipher_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) const char *cipher_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) char ecb_name[CRYPTO_MAX_ALG_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) cipher_name = crypto_attr_alg_name(tb[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (IS_ERR(cipher_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return PTR_ERR(cipher_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) spawn = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) err = crypto_grab_skcipher(spawn, skcipher_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) cipher_name, 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (snprintf(ecb_name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) cipher_name) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) err = crypto_grab_skcipher(spawn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) skcipher_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) ecb_name, 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) alg = crypto_skcipher_spawn_alg(spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (alg->base.cra_blocksize != LRW_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (crypto_skcipher_alg_ivsize(alg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) err = crypto_inst_setname(skcipher_crypto_instance(inst), "lrw",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) &alg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) cipher_name = alg->base.cra_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /* Alas we screwed up the naming so we have to mangle the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * cipher name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (!strncmp(cipher_name, "ecb(", 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) unsigned len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) len = strlcpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (len < 2 || len >= sizeof(ecb_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (ecb_name[len - 1] != ')')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) ecb_name[len - 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) "lrw(%s)", ecb_name) >= CRYPTO_MAX_ALG_NAME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) inst->alg.base.cra_priority = alg->base.cra_priority;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) inst->alg.base.cra_blocksize = LRW_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) inst->alg.base.cra_alignmask = alg->base.cra_alignmask |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) (__alignof__(be128) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) inst->alg.ivsize = LRW_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) LRW_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) LRW_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) inst->alg.base.cra_ctxsize = sizeof(struct lrw_tfm_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) inst->alg.init = lrw_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) inst->alg.exit = lrw_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) inst->alg.setkey = lrw_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) inst->alg.encrypt = lrw_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) inst->alg.decrypt = lrw_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) inst->free = lrw_free_instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) err = skcipher_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) lrw_free_instance(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static struct crypto_template lrw_tmpl = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) .name = "lrw",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) .create = lrw_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static int __init lrw_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return crypto_register_template(&lrw_tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static void __exit lrw_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) crypto_unregister_template(&lrw_tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) subsys_initcall(lrw_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) module_exit(lrw_module_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) MODULE_DESCRIPTION("LRW block cipher mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) MODULE_ALIAS_CRYPTO("lrw");