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