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