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> << 13 #include <crypto/internal/cipher.h> << 14 #include <crypto/internal/skcipher.h> 17 #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/slab.h> >> 23 >> 24 struct crypto_pcbc_ctx { >> 25 struct crypto_cipher *child; >> 26 }; >> 27 >> 28 static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key, >> 29 unsigned int keylen) >> 30 { >> 31 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent); >> 32 struct crypto_cipher *child = ctx->child; >> 33 int err; >> 34 >> 35 crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); >> 36 crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) & >> 37 CRYPTO_TFM_REQ_MASK); >> 38 err = crypto_cipher_setkey(child, key, keylen); >> 39 crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) & >> 40 CRYPTO_TFM_RES_MASK); >> 41 return err; >> 42 } 19 43 20 static int crypto_pcbc_encrypt_segment(struct 44 static int crypto_pcbc_encrypt_segment(struct skcipher_request *req, 21 struct 45 struct skcipher_walk *walk, 22 struct 46 struct crypto_cipher *tfm) 23 { 47 { 24 int bsize = crypto_cipher_blocksize(tf 48 int bsize = crypto_cipher_blocksize(tfm); 25 unsigned int nbytes = walk->nbytes; 49 unsigned int nbytes = walk->nbytes; 26 u8 *src = walk->src.virt.addr; 50 u8 *src = walk->src.virt.addr; 27 u8 *dst = walk->dst.virt.addr; 51 u8 *dst = walk->dst.virt.addr; 28 u8 * const iv = walk->iv; !! 52 u8 *iv = walk->iv; 29 53 30 do { 54 do { 31 crypto_xor(iv, src, bsize); 55 crypto_xor(iv, src, bsize); 32 crypto_cipher_encrypt_one(tfm, 56 crypto_cipher_encrypt_one(tfm, dst, iv); 33 crypto_xor_cpy(iv, dst, src, b !! 57 memcpy(iv, dst, bsize); >> 58 crypto_xor(iv, src, bsize); 34 59 35 src += bsize; 60 src += bsize; 36 dst += bsize; 61 dst += bsize; 37 } while ((nbytes -= bsize) >= bsize); 62 } while ((nbytes -= bsize) >= bsize); 38 63 39 return nbytes; 64 return nbytes; 40 } 65 } 41 66 42 static int crypto_pcbc_encrypt_inplace(struct 67 static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req, 43 struct 68 struct skcipher_walk *walk, 44 struct 69 struct crypto_cipher *tfm) 45 { 70 { 46 int bsize = crypto_cipher_blocksize(tf 71 int bsize = crypto_cipher_blocksize(tfm); 47 unsigned int nbytes = walk->nbytes; 72 unsigned int nbytes = walk->nbytes; 48 u8 *src = walk->src.virt.addr; 73 u8 *src = walk->src.virt.addr; 49 u8 * const iv = walk->iv; !! 74 u8 *iv = walk->iv; 50 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE]; !! 75 u8 tmpbuf[bsize]; 51 76 52 do { 77 do { 53 memcpy(tmpbuf, src, bsize); 78 memcpy(tmpbuf, src, bsize); 54 crypto_xor(iv, src, bsize); 79 crypto_xor(iv, src, bsize); 55 crypto_cipher_encrypt_one(tfm, 80 crypto_cipher_encrypt_one(tfm, src, iv); 56 crypto_xor_cpy(iv, tmpbuf, src !! 81 memcpy(iv, tmpbuf, bsize); >> 82 crypto_xor(iv, src, bsize); 57 83 58 src += bsize; 84 src += bsize; 59 } while ((nbytes -= bsize) >= bsize); 85 } while ((nbytes -= bsize) >= bsize); 60 86 >> 87 memcpy(walk->iv, iv, bsize); >> 88 61 return nbytes; 89 return nbytes; 62 } 90 } 63 91 64 static int crypto_pcbc_encrypt(struct skcipher 92 static int crypto_pcbc_encrypt(struct skcipher_request *req) 65 { 93 { 66 struct crypto_skcipher *tfm = crypto_s 94 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 67 struct crypto_cipher *cipher = skciphe !! 95 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); >> 96 struct crypto_cipher *child = ctx->child; 68 struct skcipher_walk walk; 97 struct skcipher_walk walk; 69 unsigned int nbytes; 98 unsigned int nbytes; 70 int err; 99 int err; 71 100 72 err = skcipher_walk_virt(&walk, req, f 101 err = skcipher_walk_virt(&walk, req, false); 73 102 74 while (walk.nbytes) { !! 103 while ((nbytes = walk.nbytes)) { 75 if (walk.src.virt.addr == walk 104 if (walk.src.virt.addr == walk.dst.virt.addr) 76 nbytes = crypto_pcbc_e 105 nbytes = crypto_pcbc_encrypt_inplace(req, &walk, 77 !! 106 child); 78 else 107 else 79 nbytes = crypto_pcbc_e 108 nbytes = crypto_pcbc_encrypt_segment(req, &walk, 80 !! 109 child); 81 err = skcipher_walk_done(&walk 110 err = skcipher_walk_done(&walk, nbytes); 82 } 111 } 83 112 84 return err; 113 return err; 85 } 114 } 86 115 87 static int crypto_pcbc_decrypt_segment(struct 116 static int crypto_pcbc_decrypt_segment(struct skcipher_request *req, 88 struct 117 struct skcipher_walk *walk, 89 struct 118 struct crypto_cipher *tfm) 90 { 119 { 91 int bsize = crypto_cipher_blocksize(tf 120 int bsize = crypto_cipher_blocksize(tfm); 92 unsigned int nbytes = walk->nbytes; 121 unsigned int nbytes = walk->nbytes; 93 u8 *src = walk->src.virt.addr; 122 u8 *src = walk->src.virt.addr; 94 u8 *dst = walk->dst.virt.addr; 123 u8 *dst = walk->dst.virt.addr; 95 u8 * const iv = walk->iv; !! 124 u8 *iv = walk->iv; 96 125 97 do { 126 do { 98 crypto_cipher_decrypt_one(tfm, 127 crypto_cipher_decrypt_one(tfm, dst, src); 99 crypto_xor(dst, iv, bsize); 128 crypto_xor(dst, iv, bsize); 100 crypto_xor_cpy(iv, dst, src, b !! 129 memcpy(iv, src, bsize); >> 130 crypto_xor(iv, dst, bsize); 101 131 102 src += bsize; 132 src += bsize; 103 dst += bsize; 133 dst += bsize; 104 } while ((nbytes -= bsize) >= bsize); 134 } while ((nbytes -= bsize) >= bsize); 105 135 >> 136 memcpy(walk->iv, iv, bsize); >> 137 106 return nbytes; 138 return nbytes; 107 } 139 } 108 140 109 static int crypto_pcbc_decrypt_inplace(struct 141 static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req, 110 struct 142 struct skcipher_walk *walk, 111 struct 143 struct crypto_cipher *tfm) 112 { 144 { 113 int bsize = crypto_cipher_blocksize(tf 145 int bsize = crypto_cipher_blocksize(tfm); 114 unsigned int nbytes = walk->nbytes; 146 unsigned int nbytes = walk->nbytes; 115 u8 *src = walk->src.virt.addr; 147 u8 *src = walk->src.virt.addr; 116 u8 * const iv = walk->iv; !! 148 u8 *iv = walk->iv; 117 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __alig !! 149 u8 tmpbuf[bsize] __attribute__ ((aligned(__alignof__(u32)))); 118 150 119 do { 151 do { 120 memcpy(tmpbuf, src, bsize); 152 memcpy(tmpbuf, src, bsize); 121 crypto_cipher_decrypt_one(tfm, 153 crypto_cipher_decrypt_one(tfm, src, src); 122 crypto_xor(src, iv, bsize); 154 crypto_xor(src, iv, bsize); 123 crypto_xor_cpy(iv, src, tmpbuf !! 155 memcpy(iv, tmpbuf, bsize); >> 156 crypto_xor(iv, src, bsize); 124 157 125 src += bsize; 158 src += bsize; 126 } while ((nbytes -= bsize) >= bsize); 159 } while ((nbytes -= bsize) >= bsize); 127 160 >> 161 memcpy(walk->iv, iv, bsize); >> 162 128 return nbytes; 163 return nbytes; 129 } 164 } 130 165 131 static int crypto_pcbc_decrypt(struct skcipher 166 static int crypto_pcbc_decrypt(struct skcipher_request *req) 132 { 167 { 133 struct crypto_skcipher *tfm = crypto_s 168 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 134 struct crypto_cipher *cipher = skciphe !! 169 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); >> 170 struct crypto_cipher *child = ctx->child; 135 struct skcipher_walk walk; 171 struct skcipher_walk walk; 136 unsigned int nbytes; 172 unsigned int nbytes; 137 int err; 173 int err; 138 174 139 err = skcipher_walk_virt(&walk, req, f 175 err = skcipher_walk_virt(&walk, req, false); 140 176 141 while (walk.nbytes) { !! 177 while ((nbytes = walk.nbytes)) { 142 if (walk.src.virt.addr == walk 178 if (walk.src.virt.addr == walk.dst.virt.addr) 143 nbytes = crypto_pcbc_d 179 nbytes = crypto_pcbc_decrypt_inplace(req, &walk, 144 !! 180 child); 145 else 181 else 146 nbytes = crypto_pcbc_d 182 nbytes = crypto_pcbc_decrypt_segment(req, &walk, 147 !! 183 child); 148 err = skcipher_walk_done(&walk 184 err = skcipher_walk_done(&walk, nbytes); 149 } 185 } 150 186 151 return err; 187 return err; 152 } 188 } 153 189 >> 190 static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm) >> 191 { >> 192 struct skcipher_instance *inst = skcipher_alg_instance(tfm); >> 193 struct crypto_spawn *spawn = skcipher_instance_ctx(inst); >> 194 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); >> 195 struct crypto_cipher *cipher; >> 196 >> 197 cipher = crypto_spawn_cipher(spawn); >> 198 if (IS_ERR(cipher)) >> 199 return PTR_ERR(cipher); >> 200 >> 201 ctx->child = cipher; >> 202 return 0; >> 203 } >> 204 >> 205 static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm) >> 206 { >> 207 struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm); >> 208 >> 209 crypto_free_cipher(ctx->child); >> 210 } >> 211 >> 212 static void crypto_pcbc_free(struct skcipher_instance *inst) >> 213 { >> 214 crypto_drop_skcipher(skcipher_instance_ctx(inst)); >> 215 kfree(inst); >> 216 } >> 217 154 static int crypto_pcbc_create(struct crypto_te 218 static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb) 155 { 219 { 156 struct skcipher_instance *inst; 220 struct skcipher_instance *inst; >> 221 struct crypto_attr_type *algt; >> 222 struct crypto_spawn *spawn; >> 223 struct crypto_alg *alg; 157 int err; 224 int err; 158 225 159 inst = skcipher_alloc_instance_simple( !! 226 algt = crypto_get_attr_type(tb); 160 if (IS_ERR(inst)) !! 227 if (IS_ERR(algt)) 161 return PTR_ERR(inst); !! 228 return PTR_ERR(algt); >> 229 >> 230 if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) & >> 231 ~CRYPTO_ALG_INTERNAL) >> 232 return -EINVAL; >> 233 >> 234 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); >> 235 if (!inst) >> 236 return -ENOMEM; >> 237 >> 238 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER | >> 239 (algt->type & CRYPTO_ALG_INTERNAL), >> 240 CRYPTO_ALG_TYPE_MASK | >> 241 (algt->mask & CRYPTO_ALG_INTERNAL)); >> 242 err = PTR_ERR(alg); >> 243 if (IS_ERR(alg)) >> 244 goto err_free_inst; >> 245 >> 246 spawn = skcipher_instance_ctx(inst); >> 247 err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst), >> 248 CRYPTO_ALG_TYPE_MASK); >> 249 crypto_mod_put(alg); >> 250 if (err) >> 251 goto err_free_inst; >> 252 >> 253 err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg); >> 254 if (err) >> 255 goto err_drop_spawn; >> 256 >> 257 inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL; >> 258 inst->alg.base.cra_priority = alg->cra_priority; >> 259 inst->alg.base.cra_blocksize = alg->cra_blocksize; >> 260 inst->alg.base.cra_alignmask = alg->cra_alignmask; >> 261 >> 262 /* We access the data as u32s when xoring. */ >> 263 inst->alg.base.cra_alignmask |= __alignof__(u32) - 1; >> 264 >> 265 inst->alg.ivsize = alg->cra_blocksize; >> 266 inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize; >> 267 inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize; 162 268 >> 269 inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx); >> 270 >> 271 inst->alg.init = crypto_pcbc_init_tfm; >> 272 inst->alg.exit = crypto_pcbc_exit_tfm; >> 273 >> 274 inst->alg.setkey = crypto_pcbc_setkey; 163 inst->alg.encrypt = crypto_pcbc_encryp 275 inst->alg.encrypt = crypto_pcbc_encrypt; 164 inst->alg.decrypt = crypto_pcbc_decryp 276 inst->alg.decrypt = crypto_pcbc_decrypt; 165 277 >> 278 inst->free = crypto_pcbc_free; >> 279 166 err = skcipher_register_instance(tmpl, 280 err = skcipher_register_instance(tmpl, inst); 167 if (err) 281 if (err) 168 inst->free(inst); !! 282 goto err_drop_spawn; 169 283 >> 284 out: 170 return err; 285 return err; >> 286 >> 287 err_drop_spawn: >> 288 crypto_drop_spawn(spawn); >> 289 err_free_inst: >> 290 kfree(inst); >> 291 goto out; 171 } 292 } 172 293 173 static struct crypto_template crypto_pcbc_tmpl 294 static struct crypto_template crypto_pcbc_tmpl = { 174 .name = "pcbc", 295 .name = "pcbc", 175 .create = crypto_pcbc_create, 296 .create = crypto_pcbc_create, 176 .module = THIS_MODULE, 297 .module = THIS_MODULE, 177 }; 298 }; 178 299 179 static int __init crypto_pcbc_module_init(void 300 static int __init crypto_pcbc_module_init(void) 180 { 301 { 181 return crypto_register_template(&crypt 302 return crypto_register_template(&crypto_pcbc_tmpl); 182 } 303 } 183 304 184 static void __exit crypto_pcbc_module_exit(voi 305 static void __exit crypto_pcbc_module_exit(void) 185 { 306 { 186 crypto_unregister_template(&crypto_pcb 307 crypto_unregister_template(&crypto_pcbc_tmpl); 187 } 308 } 188 309 189 subsys_initcall(crypto_pcbc_module_init); !! 310 module_init(crypto_pcbc_module_init); 190 module_exit(crypto_pcbc_module_exit); 311 module_exit(crypto_pcbc_module_exit); 191 312 192 MODULE_LICENSE("GPL"); 313 MODULE_LICENSE("GPL"); 193 MODULE_DESCRIPTION("PCBC block cipher mode of !! 314 MODULE_DESCRIPTION("PCBC block cipher algorithm"); 194 MODULE_ALIAS_CRYPTO("pcbc"); 315 MODULE_ALIAS_CRYPTO("pcbc"); 195 MODULE_IMPORT_NS(CRYPTO_INTERNAL); << 196 316
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.