| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * ECB: Electronic CodeBook mode |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 7 | | - * under the terms of the GNU General Public License as published by the Free |
|---|
| 8 | | - * Software Foundation; either version 2 of the License, or (at your option) |
|---|
| 9 | | - * any later version. |
|---|
| 10 | | - * |
|---|
| 11 | 6 | */ |
|---|
| 12 | 7 | |
|---|
| 13 | 8 | #include <crypto/algapi.h> |
|---|
| 9 | +#include <crypto/internal/cipher.h> |
|---|
| 10 | +#include <crypto/internal/skcipher.h> |
|---|
| 14 | 11 | #include <linux/err.h> |
|---|
| 15 | 12 | #include <linux/init.h> |
|---|
| 16 | 13 | #include <linux/kernel.h> |
|---|
| 17 | 14 | #include <linux/module.h> |
|---|
| 18 | | -#include <linux/scatterlist.h> |
|---|
| 19 | | -#include <linux/slab.h> |
|---|
| 20 | 15 | |
|---|
| 21 | | -struct crypto_ecb_ctx { |
|---|
| 22 | | - struct crypto_cipher *child; |
|---|
| 23 | | -}; |
|---|
| 24 | | - |
|---|
| 25 | | -static int crypto_ecb_setkey(struct crypto_tfm *parent, const u8 *key, |
|---|
| 26 | | - unsigned int keylen) |
|---|
| 27 | | -{ |
|---|
| 28 | | - struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(parent); |
|---|
| 29 | | - struct crypto_cipher *child = ctx->child; |
|---|
| 30 | | - int err; |
|---|
| 31 | | - |
|---|
| 32 | | - crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
|---|
| 33 | | - crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & |
|---|
| 34 | | - CRYPTO_TFM_REQ_MASK); |
|---|
| 35 | | - err = crypto_cipher_setkey(child, key, keylen); |
|---|
| 36 | | - crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & |
|---|
| 37 | | - CRYPTO_TFM_RES_MASK); |
|---|
| 38 | | - return err; |
|---|
| 39 | | -} |
|---|
| 40 | | - |
|---|
| 41 | | -static int crypto_ecb_crypt(struct blkcipher_desc *desc, |
|---|
| 42 | | - struct blkcipher_walk *walk, |
|---|
| 43 | | - struct crypto_cipher *tfm, |
|---|
| 16 | +static int crypto_ecb_crypt(struct skcipher_request *req, |
|---|
| 17 | + struct crypto_cipher *cipher, |
|---|
| 44 | 18 | void (*fn)(struct crypto_tfm *, u8 *, const u8 *)) |
|---|
| 45 | 19 | { |
|---|
| 46 | | - int bsize = crypto_cipher_blocksize(tfm); |
|---|
| 20 | + const unsigned int bsize = crypto_cipher_blocksize(cipher); |
|---|
| 21 | + struct skcipher_walk walk; |
|---|
| 47 | 22 | unsigned int nbytes; |
|---|
| 48 | 23 | int err; |
|---|
| 49 | 24 | |
|---|
| 50 | | - err = blkcipher_walk_virt(desc, walk); |
|---|
| 25 | + err = skcipher_walk_virt(&walk, req, false); |
|---|
| 51 | 26 | |
|---|
| 52 | | - while ((nbytes = walk->nbytes)) { |
|---|
| 53 | | - u8 *wsrc = walk->src.virt.addr; |
|---|
| 54 | | - u8 *wdst = walk->dst.virt.addr; |
|---|
| 27 | + while ((nbytes = walk.nbytes) != 0) { |
|---|
| 28 | + const u8 *src = walk.src.virt.addr; |
|---|
| 29 | + u8 *dst = walk.dst.virt.addr; |
|---|
| 55 | 30 | |
|---|
| 56 | 31 | do { |
|---|
| 57 | | - fn(crypto_cipher_tfm(tfm), wdst, wsrc); |
|---|
| 32 | + fn(crypto_cipher_tfm(cipher), dst, src); |
|---|
| 58 | 33 | |
|---|
| 59 | | - wsrc += bsize; |
|---|
| 60 | | - wdst += bsize; |
|---|
| 34 | + src += bsize; |
|---|
| 35 | + dst += bsize; |
|---|
| 61 | 36 | } while ((nbytes -= bsize) >= bsize); |
|---|
| 62 | 37 | |
|---|
| 63 | | - err = blkcipher_walk_done(desc, walk, nbytes); |
|---|
| 38 | + err = skcipher_walk_done(&walk, nbytes); |
|---|
| 64 | 39 | } |
|---|
| 65 | 40 | |
|---|
| 66 | 41 | return err; |
|---|
| 67 | 42 | } |
|---|
| 68 | 43 | |
|---|
| 69 | | -static int crypto_ecb_encrypt(struct blkcipher_desc *desc, |
|---|
| 70 | | - struct scatterlist *dst, struct scatterlist *src, |
|---|
| 71 | | - unsigned int nbytes) |
|---|
| 44 | +static int crypto_ecb_encrypt(struct skcipher_request *req) |
|---|
| 72 | 45 | { |
|---|
| 73 | | - struct blkcipher_walk walk; |
|---|
| 74 | | - struct crypto_blkcipher *tfm = desc->tfm; |
|---|
| 75 | | - struct crypto_ecb_ctx *ctx = crypto_blkcipher_ctx(tfm); |
|---|
| 76 | | - struct crypto_cipher *child = ctx->child; |
|---|
| 46 | + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
|---|
| 47 | + struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
|---|
| 77 | 48 | |
|---|
| 78 | | - blkcipher_walk_init(&walk, dst, src, nbytes); |
|---|
| 79 | | - return crypto_ecb_crypt(desc, &walk, child, |
|---|
| 80 | | - crypto_cipher_alg(child)->cia_encrypt); |
|---|
| 49 | + return crypto_ecb_crypt(req, cipher, |
|---|
| 50 | + crypto_cipher_alg(cipher)->cia_encrypt); |
|---|
| 81 | 51 | } |
|---|
| 82 | 52 | |
|---|
| 83 | | -static int crypto_ecb_decrypt(struct blkcipher_desc *desc, |
|---|
| 84 | | - struct scatterlist *dst, struct scatterlist *src, |
|---|
| 85 | | - unsigned int nbytes) |
|---|
| 53 | +static int crypto_ecb_decrypt(struct skcipher_request *req) |
|---|
| 86 | 54 | { |
|---|
| 87 | | - struct blkcipher_walk walk; |
|---|
| 88 | | - struct crypto_blkcipher *tfm = desc->tfm; |
|---|
| 89 | | - struct crypto_ecb_ctx *ctx = crypto_blkcipher_ctx(tfm); |
|---|
| 90 | | - struct crypto_cipher *child = ctx->child; |
|---|
| 55 | + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
|---|
| 56 | + struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
|---|
| 91 | 57 | |
|---|
| 92 | | - blkcipher_walk_init(&walk, dst, src, nbytes); |
|---|
| 93 | | - return crypto_ecb_crypt(desc, &walk, child, |
|---|
| 94 | | - crypto_cipher_alg(child)->cia_decrypt); |
|---|
| 58 | + return crypto_ecb_crypt(req, cipher, |
|---|
| 59 | + crypto_cipher_alg(cipher)->cia_decrypt); |
|---|
| 95 | 60 | } |
|---|
| 96 | 61 | |
|---|
| 97 | | -static int crypto_ecb_init_tfm(struct crypto_tfm *tfm) |
|---|
| 62 | +static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb) |
|---|
| 98 | 63 | { |
|---|
| 99 | | - struct crypto_instance *inst = (void *)tfm->__crt_alg; |
|---|
| 100 | | - struct crypto_spawn *spawn = crypto_instance_ctx(inst); |
|---|
| 101 | | - struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(tfm); |
|---|
| 102 | | - struct crypto_cipher *cipher; |
|---|
| 103 | | - |
|---|
| 104 | | - cipher = crypto_spawn_cipher(spawn); |
|---|
| 105 | | - if (IS_ERR(cipher)) |
|---|
| 106 | | - return PTR_ERR(cipher); |
|---|
| 107 | | - |
|---|
| 108 | | - ctx->child = cipher; |
|---|
| 109 | | - return 0; |
|---|
| 110 | | -} |
|---|
| 111 | | - |
|---|
| 112 | | -static void crypto_ecb_exit_tfm(struct crypto_tfm *tfm) |
|---|
| 113 | | -{ |
|---|
| 114 | | - struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(tfm); |
|---|
| 115 | | - crypto_free_cipher(ctx->child); |
|---|
| 116 | | -} |
|---|
| 117 | | - |
|---|
| 118 | | -static struct crypto_instance *crypto_ecb_alloc(struct rtattr **tb) |
|---|
| 119 | | -{ |
|---|
| 120 | | - struct crypto_instance *inst; |
|---|
| 121 | | - struct crypto_alg *alg; |
|---|
| 64 | + struct skcipher_instance *inst; |
|---|
| 122 | 65 | int err; |
|---|
| 123 | 66 | |
|---|
| 124 | | - err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER); |
|---|
| 125 | | - if (err) |
|---|
| 126 | | - return ERR_PTR(err); |
|---|
| 127 | | - |
|---|
| 128 | | - alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, |
|---|
| 129 | | - CRYPTO_ALG_TYPE_MASK); |
|---|
| 130 | | - if (IS_ERR(alg)) |
|---|
| 131 | | - return ERR_CAST(alg); |
|---|
| 132 | | - |
|---|
| 133 | | - inst = crypto_alloc_instance("ecb", alg); |
|---|
| 67 | + inst = skcipher_alloc_instance_simple(tmpl, tb); |
|---|
| 134 | 68 | if (IS_ERR(inst)) |
|---|
| 135 | | - goto out_put_alg; |
|---|
| 69 | + return PTR_ERR(inst); |
|---|
| 136 | 70 | |
|---|
| 137 | | - inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER; |
|---|
| 138 | | - inst->alg.cra_priority = alg->cra_priority; |
|---|
| 139 | | - inst->alg.cra_blocksize = alg->cra_blocksize; |
|---|
| 140 | | - inst->alg.cra_alignmask = alg->cra_alignmask; |
|---|
| 141 | | - inst->alg.cra_type = &crypto_blkcipher_type; |
|---|
| 71 | + inst->alg.ivsize = 0; /* ECB mode doesn't take an IV */ |
|---|
| 142 | 72 | |
|---|
| 143 | | - inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize; |
|---|
| 144 | | - inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize; |
|---|
| 73 | + inst->alg.encrypt = crypto_ecb_encrypt; |
|---|
| 74 | + inst->alg.decrypt = crypto_ecb_decrypt; |
|---|
| 145 | 75 | |
|---|
| 146 | | - inst->alg.cra_ctxsize = sizeof(struct crypto_ecb_ctx); |
|---|
| 76 | + err = skcipher_register_instance(tmpl, inst); |
|---|
| 77 | + if (err) |
|---|
| 78 | + inst->free(inst); |
|---|
| 147 | 79 | |
|---|
| 148 | | - inst->alg.cra_init = crypto_ecb_init_tfm; |
|---|
| 149 | | - inst->alg.cra_exit = crypto_ecb_exit_tfm; |
|---|
| 150 | | - |
|---|
| 151 | | - inst->alg.cra_blkcipher.setkey = crypto_ecb_setkey; |
|---|
| 152 | | - inst->alg.cra_blkcipher.encrypt = crypto_ecb_encrypt; |
|---|
| 153 | | - inst->alg.cra_blkcipher.decrypt = crypto_ecb_decrypt; |
|---|
| 154 | | - |
|---|
| 155 | | -out_put_alg: |
|---|
| 156 | | - crypto_mod_put(alg); |
|---|
| 157 | | - return inst; |
|---|
| 158 | | -} |
|---|
| 159 | | - |
|---|
| 160 | | -static void crypto_ecb_free(struct crypto_instance *inst) |
|---|
| 161 | | -{ |
|---|
| 162 | | - crypto_drop_spawn(crypto_instance_ctx(inst)); |
|---|
| 163 | | - kfree(inst); |
|---|
| 80 | + return err; |
|---|
| 164 | 81 | } |
|---|
| 165 | 82 | |
|---|
| 166 | 83 | static struct crypto_template crypto_ecb_tmpl = { |
|---|
| 167 | 84 | .name = "ecb", |
|---|
| 168 | | - .alloc = crypto_ecb_alloc, |
|---|
| 169 | | - .free = crypto_ecb_free, |
|---|
| 85 | + .create = crypto_ecb_create, |
|---|
| 170 | 86 | .module = THIS_MODULE, |
|---|
| 171 | 87 | }; |
|---|
| 172 | 88 | |
|---|
| .. | .. |
|---|
| 180 | 96 | crypto_unregister_template(&crypto_ecb_tmpl); |
|---|
| 181 | 97 | } |
|---|
| 182 | 98 | |
|---|
| 183 | | -module_init(crypto_ecb_module_init); |
|---|
| 99 | +subsys_initcall(crypto_ecb_module_init); |
|---|
| 184 | 100 | module_exit(crypto_ecb_module_exit); |
|---|
| 185 | 101 | |
|---|
| 186 | 102 | MODULE_LICENSE("GPL"); |
|---|
| 187 | | -MODULE_DESCRIPTION("ECB block cipher algorithm"); |
|---|
| 103 | +MODULE_DESCRIPTION("ECB block cipher mode of operation"); |
|---|
| 188 | 104 | MODULE_ALIAS_CRYPTO("ecb"); |
|---|