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