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> 18 #include <crypto/internal/skcipher.h> 15 #include <linux/err.h> 19 #include <linux/err.h> 16 #include <linux/init.h> 20 #include <linux/init.h> 17 #include <linux/kernel.h> 21 #include <linux/kernel.h> 18 #include <linux/module.h> 22 #include <linux/module.h> 19 23 20 static int crypto_pcbc_encrypt_segment(struct 24 static int crypto_pcbc_encrypt_segment(struct skcipher_request *req, 21 struct 25 struct skcipher_walk *walk, 22 struct 26 struct crypto_cipher *tfm) 23 { 27 { 24 int bsize = crypto_cipher_blocksize(tf 28 int bsize = crypto_cipher_blocksize(tfm); 25 unsigned int nbytes = walk->nbytes; 29 unsigned int nbytes = walk->nbytes; 26 u8 *src = walk->src.virt.addr; 30 u8 *src = walk->src.virt.addr; 27 u8 *dst = walk->dst.virt.addr; 31 u8 *dst = walk->dst.virt.addr; 28 u8 * const iv = walk->iv; 32 u8 * const iv = walk->iv; 29 33 30 do { 34 do { 31 crypto_xor(iv, src, bsize); 35 crypto_xor(iv, src, bsize); 32 crypto_cipher_encrypt_one(tfm, 36 crypto_cipher_encrypt_one(tfm, dst, iv); 33 crypto_xor_cpy(iv, dst, src, b 37 crypto_xor_cpy(iv, dst, src, bsize); 34 38 35 src += bsize; 39 src += bsize; 36 dst += bsize; 40 dst += bsize; 37 } while ((nbytes -= bsize) >= bsize); 41 } while ((nbytes -= bsize) >= bsize); 38 42 39 return nbytes; 43 return nbytes; 40 } 44 } 41 45 42 static int crypto_pcbc_encrypt_inplace(struct 46 static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req, 43 struct 47 struct skcipher_walk *walk, 44 struct 48 struct crypto_cipher *tfm) 45 { 49 { 46 int bsize = crypto_cipher_blocksize(tf 50 int bsize = crypto_cipher_blocksize(tfm); 47 unsigned int nbytes = walk->nbytes; 51 unsigned int nbytes = walk->nbytes; 48 u8 *src = walk->src.virt.addr; 52 u8 *src = walk->src.virt.addr; 49 u8 * const iv = walk->iv; 53 u8 * const iv = walk->iv; 50 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE]; 54 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE]; 51 55 52 do { 56 do { 53 memcpy(tmpbuf, src, bsize); 57 memcpy(tmpbuf, src, bsize); 54 crypto_xor(iv, src, bsize); 58 crypto_xor(iv, src, bsize); 55 crypto_cipher_encrypt_one(tfm, 59 crypto_cipher_encrypt_one(tfm, src, iv); 56 crypto_xor_cpy(iv, tmpbuf, src 60 crypto_xor_cpy(iv, tmpbuf, src, bsize); 57 61 58 src += bsize; 62 src += bsize; 59 } while ((nbytes -= bsize) >= bsize); 63 } while ((nbytes -= bsize) >= bsize); 60 64 61 return nbytes; 65 return nbytes; 62 } 66 } 63 67 64 static int crypto_pcbc_encrypt(struct skcipher 68 static int crypto_pcbc_encrypt(struct skcipher_request *req) 65 { 69 { 66 struct crypto_skcipher *tfm = crypto_s 70 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 67 struct crypto_cipher *cipher = skciphe 71 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); 68 struct skcipher_walk walk; 72 struct skcipher_walk walk; 69 unsigned int nbytes; 73 unsigned int nbytes; 70 int err; 74 int err; 71 75 72 err = skcipher_walk_virt(&walk, req, f 76 err = skcipher_walk_virt(&walk, req, false); 73 77 74 while (walk.nbytes) { !! 78 while ((nbytes = walk.nbytes)) { 75 if (walk.src.virt.addr == walk 79 if (walk.src.virt.addr == walk.dst.virt.addr) 76 nbytes = crypto_pcbc_e 80 nbytes = crypto_pcbc_encrypt_inplace(req, &walk, 77 81 cipher); 78 else 82 else 79 nbytes = crypto_pcbc_e 83 nbytes = crypto_pcbc_encrypt_segment(req, &walk, 80 84 cipher); 81 err = skcipher_walk_done(&walk 85 err = skcipher_walk_done(&walk, nbytes); 82 } 86 } 83 87 84 return err; 88 return err; 85 } 89 } 86 90 87 static int crypto_pcbc_decrypt_segment(struct 91 static int crypto_pcbc_decrypt_segment(struct skcipher_request *req, 88 struct 92 struct skcipher_walk *walk, 89 struct 93 struct crypto_cipher *tfm) 90 { 94 { 91 int bsize = crypto_cipher_blocksize(tf 95 int bsize = crypto_cipher_blocksize(tfm); 92 unsigned int nbytes = walk->nbytes; 96 unsigned int nbytes = walk->nbytes; 93 u8 *src = walk->src.virt.addr; 97 u8 *src = walk->src.virt.addr; 94 u8 *dst = walk->dst.virt.addr; 98 u8 *dst = walk->dst.virt.addr; 95 u8 * const iv = walk->iv; 99 u8 * const iv = walk->iv; 96 100 97 do { 101 do { 98 crypto_cipher_decrypt_one(tfm, 102 crypto_cipher_decrypt_one(tfm, dst, src); 99 crypto_xor(dst, iv, bsize); 103 crypto_xor(dst, iv, bsize); 100 crypto_xor_cpy(iv, dst, src, b 104 crypto_xor_cpy(iv, dst, src, bsize); 101 105 102 src += bsize; 106 src += bsize; 103 dst += bsize; 107 dst += bsize; 104 } while ((nbytes -= bsize) >= bsize); 108 } while ((nbytes -= bsize) >= bsize); 105 109 106 return nbytes; 110 return nbytes; 107 } 111 } 108 112 109 static int crypto_pcbc_decrypt_inplace(struct 113 static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req, 110 struct 114 struct skcipher_walk *walk, 111 struct 115 struct crypto_cipher *tfm) 112 { 116 { 113 int bsize = crypto_cipher_blocksize(tf 117 int bsize = crypto_cipher_blocksize(tfm); 114 unsigned int nbytes = walk->nbytes; 118 unsigned int nbytes = walk->nbytes; 115 u8 *src = walk->src.virt.addr; 119 u8 *src = walk->src.virt.addr; 116 u8 * const iv = walk->iv; 120 u8 * const iv = walk->iv; 117 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __alig 121 u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32)); 118 122 119 do { 123 do { 120 memcpy(tmpbuf, src, bsize); 124 memcpy(tmpbuf, src, bsize); 121 crypto_cipher_decrypt_one(tfm, 125 crypto_cipher_decrypt_one(tfm, src, src); 122 crypto_xor(src, iv, bsize); 126 crypto_xor(src, iv, bsize); 123 crypto_xor_cpy(iv, src, tmpbuf 127 crypto_xor_cpy(iv, src, tmpbuf, bsize); 124 128 125 src += bsize; 129 src += bsize; 126 } while ((nbytes -= bsize) >= bsize); 130 } while ((nbytes -= bsize) >= bsize); 127 131 128 return nbytes; 132 return nbytes; 129 } 133 } 130 134 131 static int crypto_pcbc_decrypt(struct skcipher 135 static int crypto_pcbc_decrypt(struct skcipher_request *req) 132 { 136 { 133 struct crypto_skcipher *tfm = crypto_s 137 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 134 struct crypto_cipher *cipher = skciphe 138 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); 135 struct skcipher_walk walk; 139 struct skcipher_walk walk; 136 unsigned int nbytes; 140 unsigned int nbytes; 137 int err; 141 int err; 138 142 139 err = skcipher_walk_virt(&walk, req, f 143 err = skcipher_walk_virt(&walk, req, false); 140 144 141 while (walk.nbytes) { !! 145 while ((nbytes = walk.nbytes)) { 142 if (walk.src.virt.addr == walk 146 if (walk.src.virt.addr == walk.dst.virt.addr) 143 nbytes = crypto_pcbc_d 147 nbytes = crypto_pcbc_decrypt_inplace(req, &walk, 144 148 cipher); 145 else 149 else 146 nbytes = crypto_pcbc_d 150 nbytes = crypto_pcbc_decrypt_segment(req, &walk, 147 151 cipher); 148 err = skcipher_walk_done(&walk 152 err = skcipher_walk_done(&walk, nbytes); 149 } 153 } 150 154 151 return err; 155 return err; 152 } 156 } 153 157 154 static int crypto_pcbc_create(struct crypto_te 158 static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb) 155 { 159 { 156 struct skcipher_instance *inst; 160 struct skcipher_instance *inst; >> 161 struct crypto_alg *alg; 157 int err; 162 int err; 158 163 159 inst = skcipher_alloc_instance_simple( !! 164 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg); 160 if (IS_ERR(inst)) 165 if (IS_ERR(inst)) 161 return PTR_ERR(inst); 166 return PTR_ERR(inst); 162 167 163 inst->alg.encrypt = crypto_pcbc_encryp 168 inst->alg.encrypt = crypto_pcbc_encrypt; 164 inst->alg.decrypt = crypto_pcbc_decryp 169 inst->alg.decrypt = crypto_pcbc_decrypt; 165 170 166 err = skcipher_register_instance(tmpl, 171 err = skcipher_register_instance(tmpl, inst); 167 if (err) 172 if (err) 168 inst->free(inst); 173 inst->free(inst); 169 !! 174 crypto_mod_put(alg); 170 return err; 175 return err; 171 } 176 } 172 177 173 static struct crypto_template crypto_pcbc_tmpl 178 static struct crypto_template crypto_pcbc_tmpl = { 174 .name = "pcbc", 179 .name = "pcbc", 175 .create = crypto_pcbc_create, 180 .create = crypto_pcbc_create, 176 .module = THIS_MODULE, 181 .module = THIS_MODULE, 177 }; 182 }; 178 183 179 static int __init crypto_pcbc_module_init(void 184 static int __init crypto_pcbc_module_init(void) 180 { 185 { 181 return crypto_register_template(&crypt 186 return crypto_register_template(&crypto_pcbc_tmpl); 182 } 187 } 183 188 184 static void __exit crypto_pcbc_module_exit(voi 189 static void __exit crypto_pcbc_module_exit(void) 185 { 190 { 186 crypto_unregister_template(&crypto_pcb 191 crypto_unregister_template(&crypto_pcbc_tmpl); 187 } 192 } 188 193 189 subsys_initcall(crypto_pcbc_module_init); !! 194 module_init(crypto_pcbc_module_init); 190 module_exit(crypto_pcbc_module_exit); 195 module_exit(crypto_pcbc_module_exit); 191 196 192 MODULE_LICENSE("GPL"); 197 MODULE_LICENSE("GPL"); 193 MODULE_DESCRIPTION("PCBC block cipher mode of 198 MODULE_DESCRIPTION("PCBC block cipher mode of operation"); 194 MODULE_ALIAS_CRYPTO("pcbc"); 199 MODULE_ALIAS_CRYPTO("pcbc"); 195 MODULE_IMPORT_NS(CRYPTO_INTERNAL); << 196 200
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.