1 // SPDX-License-Identifier: GPL-2.0-or-later << 2 /* 1 /* 3 * PCBC: Propagating Cipher Block Chaining mod 2 * PCBC: Propagating Cipher Block Chaining mode 4 * 3 * 5 * Copyright (C) 2006 Red Hat, Inc. All Rights 4 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. 6 * Written by David Howells (dhowells@redhat.c 5 * Written by David Howells (dhowells@redhat.com) 7 * 6 * 8 * Derived from cbc.c 7 * Derived from cbc.c 9 * - Copyright (c) 2006 Herbert Xu <herbert@go 8 * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> >> 9 * >> 10 * This program is free software; you can redistribute it and/or modify it >> 11 * under the terms of the GNU General Public License as published by the Free >> 12 * Software Foundation; either version 2 of the License, or (at your option) >> 13 * any later version. >> 14 * 10 */ 15 */ 11 16 12 #include <crypto/algapi.h> 17 #include <crypto/algapi.h> 13 #include <crypto/internal/cipher.h> << 14 #include <crypto/internal/skcipher.h> << 15 #include <linux/err.h> 18 #include <linux/err.h> 16 #include <linux/init.h> 19 #include <linux/init.h> 17 #include <linux/kernel.h> 20 #include <linux/kernel.h> 18 #include <linux/module.h> 21 #include <linux/module.h> >> 22 #include <linux/scatterlist.h> >> 23 #include <linux/slab.h> >> 24 >> 25 struct crypto_pcbc_ctx { >> 26 struct crypto_cipher *child; >> 27 }; >> 28 >> 29 static int crypto_pcbc_setkey(struct crypto_tfm *parent, const u8 *key, >> 30 unsigned int keylen) >> 31 { >> 32 struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(parent); >> 33 struct crypto_cipher *child = ctx->child; >> 34 int err; >> 35 >> 36 crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); >> 37 crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & >> 38 CRYPTO_TFM_REQ_MASK); >> 39 err = crypto_cipher_setkey(child, key, keylen); >> 40 crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & >> 41 CRYPTO_TFM_RES_MASK); >> 42 return err; >> 43 } 19 44 20 static int crypto_pcbc_encrypt_segment(struct !! 45 static int crypto_pcbc_encrypt_segment(struct blkcipher_desc *desc, 21 struct !! 46 struct blkcipher_walk *walk, 22 struct 47 struct crypto_cipher *tfm) 23 { 48 { >> 49 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = >> 50 crypto_cipher_alg(tfm)->cia_encrypt; 24 int bsize = crypto_cipher_blocksize(tf 51 int bsize = crypto_cipher_blocksize(tfm); 25 unsigned int nbytes = walk->nbytes; 52 unsigned int nbytes = walk->nbytes; 26 u8 *src = walk->src.virt.addr; 53 u8 *src = walk->src.virt.addr; 27 u8 *dst = walk->dst.virt.addr; 54 u8 *dst = walk->dst.virt.addr; 28 u8 * const iv = walk->iv; !! 55 u8 *iv = walk->iv; 29 56 30 do { 57 do { 31 crypto_xor(iv, src, bsize); 58 crypto_xor(iv, src, bsize); 32 crypto_cipher_encrypt_one(tfm, !! 59 fn(crypto_cipher_tfm(tfm), dst, iv); 33 crypto_xor_cpy(iv, dst, src, b !! 60 memcpy(iv, dst, bsize); >> 61 crypto_xor(iv, src, bsize); 34 62 35 src += bsize; 63 src += bsize; 36 dst += bsize; 64 dst += bsize; 37 } while ((nbytes -= bsize) >= bsize); 65 } while ((nbytes -= bsize) >= bsize); 38 66 39 return nbytes; 67 return nbytes; 40 } 68 } 41 69 42 static int crypto_pcbc_encrypt_inplace(struct !! 70 static int crypto_pcbc_encrypt_inplace(struct blkcipher_desc *desc, 43 struct !! 71 struct blkcipher_walk *walk, 44 struct 72 struct crypto_cipher *tfm) 45 { 73 { >> 74 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = >> 75 crypto_cipher_alg(tfm)->cia_encrypt; 46 int bsize = crypto_cipher_blocksize(tf 76 int bsize = crypto_cipher_blocksize(tfm); 47 unsigned int nbytes = walk->nbytes; 77 unsigned int nbytes = walk->nbytes; 48 u8 *src = walk->src.virt.addr; 78 u8 *src = walk->src.virt.addr; 49 u8 * const iv = walk->iv; !! 79 u8 *iv = walk->iv; 50 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE]; !! 80 u8 tmpbuf[bsize]; 51 81 52 do { 82 do { 53 memcpy(tmpbuf, src, bsize); 83 memcpy(tmpbuf, src, bsize); 54 crypto_xor(iv, src, bsize); 84 crypto_xor(iv, src, bsize); 55 crypto_cipher_encrypt_one(tfm, !! 85 fn(crypto_cipher_tfm(tfm), src, iv); 56 crypto_xor_cpy(iv, tmpbuf, src !! 86 memcpy(iv, tmpbuf, bsize); >> 87 crypto_xor(iv, src, bsize); 57 88 58 src += bsize; 89 src += bsize; 59 } while ((nbytes -= bsize) >= bsize); 90 } while ((nbytes -= bsize) >= bsize); 60 91 >> 92 memcpy(walk->iv, iv, bsize); >> 93 61 return nbytes; 94 return nbytes; 62 } 95 } 63 96 64 static int crypto_pcbc_encrypt(struct skcipher !! 97 static int crypto_pcbc_encrypt(struct blkcipher_desc *desc, >> 98 struct scatterlist *dst, struct scatterlist *src, >> 99 unsigned int nbytes) 65 { 100 { 66 struct crypto_skcipher *tfm = crypto_s !! 101 struct blkcipher_walk walk; 67 struct crypto_cipher *cipher = skciphe !! 102 struct crypto_blkcipher *tfm = desc->tfm; 68 struct skcipher_walk walk; !! 103 struct crypto_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm); 69 unsigned int nbytes; !! 104 struct crypto_cipher *child = ctx->child; 70 int err; 105 int err; 71 106 72 err = skcipher_walk_virt(&walk, req, f !! 107 blkcipher_walk_init(&walk, dst, src, nbytes); >> 108 err = blkcipher_walk_virt(desc, &walk); 73 109 74 while (walk.nbytes) { !! 110 while ((nbytes = walk.nbytes)) { 75 if (walk.src.virt.addr == walk 111 if (walk.src.virt.addr == walk.dst.virt.addr) 76 nbytes = crypto_pcbc_e !! 112 nbytes = crypto_pcbc_encrypt_inplace(desc, &walk, 77 !! 113 child); 78 else 114 else 79 nbytes = crypto_pcbc_e !! 115 nbytes = crypto_pcbc_encrypt_segment(desc, &walk, 80 !! 116 child); 81 err = skcipher_walk_done(&walk !! 117 err = blkcipher_walk_done(desc, &walk, nbytes); 82 } 118 } 83 119 84 return err; 120 return err; 85 } 121 } 86 122 87 static int crypto_pcbc_decrypt_segment(struct !! 123 static int crypto_pcbc_decrypt_segment(struct blkcipher_desc *desc, 88 struct !! 124 struct blkcipher_walk *walk, 89 struct 125 struct crypto_cipher *tfm) 90 { 126 { >> 127 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = >> 128 crypto_cipher_alg(tfm)->cia_decrypt; 91 int bsize = crypto_cipher_blocksize(tf 129 int bsize = crypto_cipher_blocksize(tfm); 92 unsigned int nbytes = walk->nbytes; 130 unsigned int nbytes = walk->nbytes; 93 u8 *src = walk->src.virt.addr; 131 u8 *src = walk->src.virt.addr; 94 u8 *dst = walk->dst.virt.addr; 132 u8 *dst = walk->dst.virt.addr; 95 u8 * const iv = walk->iv; !! 133 u8 *iv = walk->iv; 96 134 97 do { 135 do { 98 crypto_cipher_decrypt_one(tfm, !! 136 fn(crypto_cipher_tfm(tfm), dst, src); 99 crypto_xor(dst, iv, bsize); 137 crypto_xor(dst, iv, bsize); 100 crypto_xor_cpy(iv, dst, src, b !! 138 memcpy(iv, src, bsize); >> 139 crypto_xor(iv, dst, bsize); 101 140 102 src += bsize; 141 src += bsize; 103 dst += bsize; 142 dst += bsize; 104 } while ((nbytes -= bsize) >= bsize); 143 } while ((nbytes -= bsize) >= bsize); 105 144 >> 145 memcpy(walk->iv, iv, bsize); >> 146 106 return nbytes; 147 return nbytes; 107 } 148 } 108 149 109 static int crypto_pcbc_decrypt_inplace(struct !! 150 static int crypto_pcbc_decrypt_inplace(struct blkcipher_desc *desc, 110 struct !! 151 struct blkcipher_walk *walk, 111 struct 152 struct crypto_cipher *tfm) 112 { 153 { >> 154 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = >> 155 crypto_cipher_alg(tfm)->cia_decrypt; 113 int bsize = crypto_cipher_blocksize(tf 156 int bsize = crypto_cipher_blocksize(tfm); 114 unsigned int nbytes = walk->nbytes; 157 unsigned int nbytes = walk->nbytes; 115 u8 *src = walk->src.virt.addr; 158 u8 *src = walk->src.virt.addr; 116 u8 * const iv = walk->iv; !! 159 u8 *iv = walk->iv; 117 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __alig !! 160 u8 tmpbuf[bsize]; 118 161 119 do { 162 do { 120 memcpy(tmpbuf, src, bsize); 163 memcpy(tmpbuf, src, bsize); 121 crypto_cipher_decrypt_one(tfm, !! 164 fn(crypto_cipher_tfm(tfm), src, src); 122 crypto_xor(src, iv, bsize); 165 crypto_xor(src, iv, bsize); 123 crypto_xor_cpy(iv, src, tmpbuf !! 166 memcpy(iv, tmpbuf, bsize); >> 167 crypto_xor(iv, src, bsize); 124 168 125 src += bsize; 169 src += bsize; 126 } while ((nbytes -= bsize) >= bsize); 170 } while ((nbytes -= bsize) >= bsize); 127 171 >> 172 memcpy(walk->iv, iv, bsize); >> 173 128 return nbytes; 174 return nbytes; 129 } 175 } 130 176 131 static int crypto_pcbc_decrypt(struct skcipher !! 177 static int crypto_pcbc_decrypt(struct blkcipher_desc *desc, >> 178 struct scatterlist *dst, struct scatterlist *src, >> 179 unsigned int nbytes) 132 { 180 { 133 struct crypto_skcipher *tfm = crypto_s !! 181 struct blkcipher_walk walk; 134 struct crypto_cipher *cipher = skciphe !! 182 struct crypto_blkcipher *tfm = desc->tfm; 135 struct skcipher_walk walk; !! 183 struct crypto_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm); 136 unsigned int nbytes; !! 184 struct crypto_cipher *child = ctx->child; 137 int err; 185 int err; 138 186 139 err = skcipher_walk_virt(&walk, req, f !! 187 blkcipher_walk_init(&walk, dst, src, nbytes); >> 188 err = blkcipher_walk_virt(desc, &walk); 140 189 141 while (walk.nbytes) { !! 190 while ((nbytes = walk.nbytes)) { 142 if (walk.src.virt.addr == walk 191 if (walk.src.virt.addr == walk.dst.virt.addr) 143 nbytes = crypto_pcbc_d !! 192 nbytes = crypto_pcbc_decrypt_inplace(desc, &walk, 144 !! 193 child); 145 else 194 else 146 nbytes = crypto_pcbc_d !! 195 nbytes = crypto_pcbc_decrypt_segment(desc, &walk, 147 !! 196 child); 148 err = skcipher_walk_done(&walk !! 197 err = blkcipher_walk_done(desc, &walk, nbytes); 149 } 198 } 150 199 151 return err; 200 return err; 152 } 201 } 153 202 154 static int crypto_pcbc_create(struct crypto_te !! 203 static int crypto_pcbc_init_tfm(struct crypto_tfm *tfm) 155 { 204 { 156 struct skcipher_instance *inst; !! 205 struct crypto_instance *inst = (void *)tfm->__crt_alg; >> 206 struct crypto_spawn *spawn = crypto_instance_ctx(inst); >> 207 struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm); >> 208 struct crypto_cipher *cipher; >> 209 >> 210 cipher = crypto_spawn_cipher(spawn); >> 211 if (IS_ERR(cipher)) >> 212 return PTR_ERR(cipher); >> 213 >> 214 ctx->child = cipher; >> 215 return 0; >> 216 } >> 217 >> 218 static void crypto_pcbc_exit_tfm(struct crypto_tfm *tfm) >> 219 { >> 220 struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm); >> 221 crypto_free_cipher(ctx->child); >> 222 } >> 223 >> 224 static struct crypto_instance *crypto_pcbc_alloc(struct rtattr **tb) >> 225 { >> 226 struct crypto_instance *inst; >> 227 struct crypto_alg *alg; 157 int err; 228 int err; 158 229 159 inst = skcipher_alloc_instance_simple( !! 230 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER); >> 231 if (err) >> 232 return ERR_PTR(err); >> 233 >> 234 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, >> 235 CRYPTO_ALG_TYPE_MASK); >> 236 if (IS_ERR(alg)) >> 237 return ERR_CAST(alg); >> 238 >> 239 inst = crypto_alloc_instance("pcbc", alg); 160 if (IS_ERR(inst)) 240 if (IS_ERR(inst)) 161 return PTR_ERR(inst); !! 241 goto out_put_alg; 162 242 163 inst->alg.encrypt = crypto_pcbc_encryp !! 243 inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER; 164 inst->alg.decrypt = crypto_pcbc_decryp !! 244 inst->alg.cra_priority = alg->cra_priority; >> 245 inst->alg.cra_blocksize = alg->cra_blocksize; >> 246 inst->alg.cra_alignmask = alg->cra_alignmask; >> 247 inst->alg.cra_type = &crypto_blkcipher_type; 165 248 166 err = skcipher_register_instance(tmpl, !! 249 /* We access the data as u32s when xoring. */ 167 if (err) !! 250 inst->alg.cra_alignmask |= __alignof__(u32) - 1; 168 inst->free(inst); << 169 251 170 return err; !! 252 inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize; >> 253 inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize; >> 254 inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize; >> 255 >> 256 inst->alg.cra_ctxsize = sizeof(struct crypto_pcbc_ctx); >> 257 >> 258 inst->alg.cra_init = crypto_pcbc_init_tfm; >> 259 inst->alg.cra_exit = crypto_pcbc_exit_tfm; >> 260 >> 261 inst->alg.cra_blkcipher.setkey = crypto_pcbc_setkey; >> 262 inst->alg.cra_blkcipher.encrypt = crypto_pcbc_encrypt; >> 263 inst->alg.cra_blkcipher.decrypt = crypto_pcbc_decrypt; >> 264 >> 265 out_put_alg: >> 266 crypto_mod_put(alg); >> 267 return inst; >> 268 } >> 269 >> 270 static void crypto_pcbc_free(struct crypto_instance *inst) >> 271 { >> 272 crypto_drop_spawn(crypto_instance_ctx(inst)); >> 273 kfree(inst); 171 } 274 } 172 275 173 static struct crypto_template crypto_pcbc_tmpl 276 static struct crypto_template crypto_pcbc_tmpl = { 174 .name = "pcbc", 277 .name = "pcbc", 175 .create = crypto_pcbc_create, !! 278 .alloc = crypto_pcbc_alloc, >> 279 .free = crypto_pcbc_free, 176 .module = THIS_MODULE, 280 .module = THIS_MODULE, 177 }; 281 }; 178 282 179 static int __init crypto_pcbc_module_init(void 283 static int __init crypto_pcbc_module_init(void) 180 { 284 { 181 return crypto_register_template(&crypt 285 return crypto_register_template(&crypto_pcbc_tmpl); 182 } 286 } 183 287 184 static void __exit crypto_pcbc_module_exit(voi 288 static void __exit crypto_pcbc_module_exit(void) 185 { 289 { 186 crypto_unregister_template(&crypto_pcb 290 crypto_unregister_template(&crypto_pcbc_tmpl); 187 } 291 } 188 292 189 subsys_initcall(crypto_pcbc_module_init); !! 293 module_init(crypto_pcbc_module_init); 190 module_exit(crypto_pcbc_module_exit); 294 module_exit(crypto_pcbc_module_exit); 191 295 192 MODULE_LICENSE("GPL"); 296 MODULE_LICENSE("GPL"); 193 MODULE_DESCRIPTION("PCBC block cipher mode of !! 297 MODULE_DESCRIPTION("PCBC block cipher algorithm"); 194 MODULE_ALIAS_CRYPTO("pcbc"); 298 MODULE_ALIAS_CRYPTO("pcbc"); 195 MODULE_IMPORT_NS(CRYPTO_INTERNAL); << 196 299
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.