Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 2 | /* |
| 3 | * PCBC: Propagating Cipher Block Chaining mode |
| 4 | * |
| 5 | * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. |
| 6 | * Written by David Howells (dhowells@redhat.com) |
| 7 | * |
| 8 | * Derived from cbc.c |
| 9 | * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 10 | */ |
| 11 | |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 12 | #include <crypto/algapi.h> |
Ard Biesheuvel | 0eb76ba | 2020-12-11 13:27:15 +0100 | [diff] [blame] | 13 | #include <crypto/internal/cipher.h> |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 14 | #include <crypto/internal/skcipher.h> |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 19 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 20 | static int crypto_pcbc_encrypt_segment(struct skcipher_request *req, |
| 21 | struct skcipher_walk *walk, |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 22 | struct crypto_cipher *tfm) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 23 | { |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 24 | int bsize = crypto_cipher_blocksize(tfm); |
| 25 | unsigned int nbytes = walk->nbytes; |
| 26 | u8 *src = walk->src.virt.addr; |
| 27 | u8 *dst = walk->dst.virt.addr; |
Eric Biggers | 251b7ae | 2019-01-03 20:16:13 -0800 | [diff] [blame] | 28 | u8 * const iv = walk->iv; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 29 | |
| 30 | do { |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 31 | crypto_xor(iv, src, bsize); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 32 | crypto_cipher_encrypt_one(tfm, dst, iv); |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 33 | crypto_xor_cpy(iv, dst, src, bsize); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 34 | |
| 35 | src += bsize; |
| 36 | dst += bsize; |
| 37 | } while ((nbytes -= bsize) >= bsize); |
| 38 | |
| 39 | return nbytes; |
| 40 | } |
| 41 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 42 | static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req, |
| 43 | struct skcipher_walk *walk, |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 44 | struct crypto_cipher *tfm) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 45 | { |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 46 | int bsize = crypto_cipher_blocksize(tfm); |
| 47 | unsigned int nbytes = walk->nbytes; |
| 48 | u8 *src = walk->src.virt.addr; |
Eric Biggers | 251b7ae | 2019-01-03 20:16:13 -0800 | [diff] [blame] | 49 | u8 * const iv = walk->iv; |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 50 | u8 tmpbuf[MAX_CIPHER_BLOCKSIZE]; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 51 | |
| 52 | do { |
| 53 | memcpy(tmpbuf, src, bsize); |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 54 | crypto_xor(iv, src, bsize); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 55 | crypto_cipher_encrypt_one(tfm, src, iv); |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 56 | crypto_xor_cpy(iv, tmpbuf, src, bsize); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 57 | |
| 58 | src += bsize; |
| 59 | } while ((nbytes -= bsize) >= bsize); |
| 60 | |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 61 | return nbytes; |
| 62 | } |
| 63 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 64 | static int crypto_pcbc_encrypt(struct skcipher_request *req) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 65 | { |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 66 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame] | 67 | struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 68 | struct skcipher_walk walk; |
| 69 | unsigned int nbytes; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 70 | int err; |
| 71 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 72 | err = skcipher_walk_virt(&walk, req, false); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 73 | |
| 74 | while ((nbytes = walk.nbytes)) { |
| 75 | if (walk.src.virt.addr == walk.dst.virt.addr) |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 76 | nbytes = crypto_pcbc_encrypt_inplace(req, &walk, |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame] | 77 | cipher); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 78 | else |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 79 | nbytes = crypto_pcbc_encrypt_segment(req, &walk, |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame] | 80 | cipher); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 81 | err = skcipher_walk_done(&walk, nbytes); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | return err; |
| 85 | } |
| 86 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 87 | static int crypto_pcbc_decrypt_segment(struct skcipher_request *req, |
| 88 | struct skcipher_walk *walk, |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 89 | struct crypto_cipher *tfm) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 90 | { |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 91 | int bsize = crypto_cipher_blocksize(tfm); |
| 92 | unsigned int nbytes = walk->nbytes; |
| 93 | u8 *src = walk->src.virt.addr; |
| 94 | u8 *dst = walk->dst.virt.addr; |
Eric Biggers | 251b7ae | 2019-01-03 20:16:13 -0800 | [diff] [blame] | 95 | u8 * const iv = walk->iv; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 96 | |
| 97 | do { |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 98 | crypto_cipher_decrypt_one(tfm, dst, src); |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 99 | crypto_xor(dst, iv, bsize); |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 100 | crypto_xor_cpy(iv, dst, src, bsize); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 101 | |
| 102 | src += bsize; |
| 103 | dst += bsize; |
| 104 | } while ((nbytes -= bsize) >= bsize); |
| 105 | |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 106 | return nbytes; |
| 107 | } |
| 108 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 109 | static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req, |
| 110 | struct skcipher_walk *walk, |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 111 | struct crypto_cipher *tfm) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 112 | { |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 113 | int bsize = crypto_cipher_blocksize(tfm); |
| 114 | unsigned int nbytes = walk->nbytes; |
| 115 | u8 *src = walk->src.virt.addr; |
Eric Biggers | 251b7ae | 2019-01-03 20:16:13 -0800 | [diff] [blame] | 116 | u8 * const iv = walk->iv; |
Salvatore Mesoraca | 6650c4d | 2018-04-09 15:54:47 +0200 | [diff] [blame] | 117 | u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32)); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 118 | |
| 119 | do { |
| 120 | memcpy(tmpbuf, src, bsize); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 121 | crypto_cipher_decrypt_one(tfm, src, src); |
Herbert Xu | d0b9007 | 2007-11-20 17:49:49 +0800 | [diff] [blame] | 122 | crypto_xor(src, iv, bsize); |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 123 | crypto_xor_cpy(iv, src, tmpbuf, bsize); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 124 | |
| 125 | src += bsize; |
| 126 | } while ((nbytes -= bsize) >= bsize); |
| 127 | |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 128 | return nbytes; |
| 129 | } |
| 130 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 131 | static int crypto_pcbc_decrypt(struct skcipher_request *req) |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 132 | { |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 133 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame] | 134 | struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 135 | struct skcipher_walk walk; |
| 136 | unsigned int nbytes; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 137 | int err; |
| 138 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 139 | err = skcipher_walk_virt(&walk, req, false); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 140 | |
| 141 | while ((nbytes = walk.nbytes)) { |
| 142 | if (walk.src.virt.addr == walk.dst.virt.addr) |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 143 | nbytes = crypto_pcbc_decrypt_inplace(req, &walk, |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame] | 144 | cipher); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 145 | else |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 146 | nbytes = crypto_pcbc_decrypt_segment(req, &walk, |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame] | 147 | cipher); |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 148 | err = skcipher_walk_done(&walk, nbytes); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return err; |
| 152 | } |
| 153 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 154 | static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb) |
| 155 | { |
| 156 | struct skcipher_instance *inst; |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 157 | int err; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 158 | |
Herbert Xu | b3c16bf | 2019-12-20 13:29:40 +0800 | [diff] [blame] | 159 | inst = skcipher_alloc_instance_simple(tmpl, tb); |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame] | 160 | if (IS_ERR(inst)) |
| 161 | return PTR_ERR(inst); |
Herbert Xu | ebc610e | 2007-01-01 18:37:02 +1100 | [diff] [blame] | 162 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 163 | inst->alg.encrypt = crypto_pcbc_encrypt; |
| 164 | inst->alg.decrypt = crypto_pcbc_decrypt; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 165 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 166 | err = skcipher_register_instance(tmpl, inst); |
| 167 | if (err) |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame] | 168 | inst->free(inst); |
Herbert Xu | b3c16bf | 2019-12-20 13:29:40 +0800 | [diff] [blame] | 169 | |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 170 | return err; |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static struct crypto_template crypto_pcbc_tmpl = { |
| 174 | .name = "pcbc", |
Herbert Xu | 043a440 | 2016-11-22 20:08:27 +0800 | [diff] [blame] | 175 | .create = crypto_pcbc_create, |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 176 | .module = THIS_MODULE, |
| 177 | }; |
| 178 | |
| 179 | static int __init crypto_pcbc_module_init(void) |
| 180 | { |
| 181 | return crypto_register_template(&crypto_pcbc_tmpl); |
| 182 | } |
| 183 | |
| 184 | static void __exit crypto_pcbc_module_exit(void) |
| 185 | { |
| 186 | crypto_unregister_template(&crypto_pcbc_tmpl); |
| 187 | } |
| 188 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 189 | subsys_initcall(crypto_pcbc_module_init); |
David Howells | 91652be | 2006-12-16 12:09:02 +1100 | [diff] [blame] | 190 | module_exit(crypto_pcbc_module_exit); |
| 191 | |
| 192 | MODULE_LICENSE("GPL"); |
Eric Biggers | 0be487b | 2019-01-03 20:16:22 -0800 | [diff] [blame] | 193 | MODULE_DESCRIPTION("PCBC block cipher mode of operation"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 194 | MODULE_ALIAS_CRYPTO("pcbc"); |
Ard Biesheuvel | 0eb76ba | 2020-12-11 13:27:15 +0100 | [diff] [blame] | 195 | MODULE_IMPORT_NS(CRYPTO_INTERNAL); |