David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 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 | |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 17 | #include <crypto/algapi.h> |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 18 | #include <crypto/internal/skcipher.h> |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 23 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 24 | static int crypto_pcbc_encrypt_segment(struct skcipher_request *req, |
| 25 | struct skcipher_walk *walk, |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 26 | struct crypto_cipher *tfm) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 27 | { |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 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; |
Eric Biggers | 251b7ae | 2019-01-03 20:16:13 -0800 | [diff] [blame] | 32 | u8 * const iv = walk->iv; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 33 | |
| 34 | do { |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 35 | crypto_xor(iv, src, bsize); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 36 | crypto_cipher_encrypt_one(tfm, dst, iv); |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 37 | crypto_xor_cpy(iv, dst, src, bsize); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 38 | |
| 39 | src += bsize; |
| 40 | dst += bsize; |
| 41 | } while ((nbytes -= bsize) >= bsize); |
| 42 | |
| 43 | return nbytes; |
| 44 | } |
| 45 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 46 | static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req, |
| 47 | struct skcipher_walk *walk, |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 48 | struct crypto_cipher *tfm) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 49 | { |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 50 | int bsize = crypto_cipher_blocksize(tfm); |
| 51 | unsigned int nbytes = walk->nbytes; |
| 52 | u8 *src = walk->src.virt.addr; |
Eric Biggers | 251b7ae | 2019-01-03 20:16:13 -0800 | [diff] [blame] | 53 | u8 * const iv = walk->iv; |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 54 | u8 tmpbuf[MAX_CIPHER_BLOCKSIZE]; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 55 | |
| 56 | do { |
| 57 | memcpy(tmpbuf, src, bsize); |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 58 | crypto_xor(iv, src, bsize); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 59 | crypto_cipher_encrypt_one(tfm, src, iv); |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 60 | crypto_xor_cpy(iv, tmpbuf, src, bsize); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 61 | |
| 62 | src += bsize; |
| 63 | } while ((nbytes -= bsize) >= bsize); |
| 64 | |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 65 | return nbytes; |
| 66 | } |
| 67 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 68 | static int crypto_pcbc_encrypt(struct skcipher_request *req) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 69 | { |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 70 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame^] | 71 | struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 72 | struct skcipher_walk walk; |
| 73 | unsigned int nbytes; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 74 | int err; |
| 75 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 76 | err = skcipher_walk_virt(&walk, req, false); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 77 | |
| 78 | while ((nbytes = walk.nbytes)) { |
| 79 | if (walk.src.virt.addr == walk.dst.virt.addr) |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 80 | nbytes = crypto_pcbc_encrypt_inplace(req, &walk, |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame^] | 81 | cipher); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 82 | else |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 83 | nbytes = crypto_pcbc_encrypt_segment(req, &walk, |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame^] | 84 | cipher); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 85 | err = skcipher_walk_done(&walk, nbytes); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return err; |
| 89 | } |
| 90 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 91 | static int crypto_pcbc_decrypt_segment(struct skcipher_request *req, |
| 92 | struct skcipher_walk *walk, |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 93 | struct crypto_cipher *tfm) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 94 | { |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 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; |
Eric Biggers | 251b7ae | 2019-01-03 20:16:13 -0800 | [diff] [blame] | 99 | u8 * const iv = walk->iv; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 100 | |
| 101 | do { |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 102 | crypto_cipher_decrypt_one(tfm, dst, src); |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 103 | crypto_xor(dst, iv, bsize); |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 104 | crypto_xor_cpy(iv, dst, src, bsize); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 105 | |
| 106 | src += bsize; |
| 107 | dst += bsize; |
| 108 | } while ((nbytes -= bsize) >= bsize); |
| 109 | |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 110 | return nbytes; |
| 111 | } |
| 112 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 113 | static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req, |
| 114 | struct skcipher_walk *walk, |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 115 | struct crypto_cipher *tfm) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 116 | { |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 117 | int bsize = crypto_cipher_blocksize(tfm); |
| 118 | unsigned int nbytes = walk->nbytes; |
| 119 | u8 *src = walk->src.virt.addr; |
Eric Biggers | 251b7ae | 2019-01-03 20:16:13 -0800 | [diff] [blame] | 120 | u8 * const iv = walk->iv; |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 121 | u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32)); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 122 | |
| 123 | do { |
| 124 | memcpy(tmpbuf, src, bsize); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 125 | crypto_cipher_decrypt_one(tfm, src, src); |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 126 | crypto_xor(src, iv, bsize); |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 127 | crypto_xor_cpy(iv, src, tmpbuf, bsize); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 128 | |
| 129 | src += bsize; |
| 130 | } while ((nbytes -= bsize) >= bsize); |
| 131 | |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 132 | return nbytes; |
| 133 | } |
| 134 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 135 | static int crypto_pcbc_decrypt(struct skcipher_request *req) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 136 | { |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 137 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame^] | 138 | struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 139 | struct skcipher_walk walk; |
| 140 | unsigned int nbytes; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 141 | int err; |
| 142 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 143 | err = skcipher_walk_virt(&walk, req, false); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 144 | |
| 145 | while ((nbytes = walk.nbytes)) { |
| 146 | if (walk.src.virt.addr == walk.dst.virt.addr) |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 147 | nbytes = crypto_pcbc_decrypt_inplace(req, &walk, |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame^] | 148 | cipher); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 149 | else |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 150 | nbytes = crypto_pcbc_decrypt_segment(req, &walk, |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame^] | 151 | cipher); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 152 | err = skcipher_walk_done(&walk, nbytes); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | return err; |
| 156 | } |
| 157 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 158 | static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb) |
| 159 | { |
| 160 | struct skcipher_instance *inst; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 161 | struct crypto_alg *alg; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 162 | int err; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 163 | |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame^] | 164 | inst = skcipher_alloc_instance_simple(tmpl, tb, &alg); |
| 165 | if (IS_ERR(inst)) |
| 166 | return PTR_ERR(inst); |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 167 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 168 | inst->alg.encrypt = crypto_pcbc_encrypt; |
| 169 | inst->alg.decrypt = crypto_pcbc_decrypt; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 170 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 171 | err = skcipher_register_instance(tmpl, inst); |
| 172 | if (err) |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame^] | 173 | inst->free(inst); |
Pan Bian | e5bde04 | 2018-11-22 18:00:16 +0800 | [diff] [blame] | 174 | crypto_mod_put(alg); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 175 | return err; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static struct crypto_template crypto_pcbc_tmpl = { |
| 179 | .name = "pcbc", |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 180 | .create = crypto_pcbc_create, |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 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 | module_init(crypto_pcbc_module_init); |
| 195 | module_exit(crypto_pcbc_module_exit); |
| 196 | |
| 197 | MODULE_LICENSE("GPL"); |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame^] | 198 | MODULE_DESCRIPTION("PCBC block cipher mode of operation"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 199 | MODULE_ALIAS_CRYPTO("pcbc"); |