Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
91652be5 DH |
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> | |
91652be5 DH |
10 | */ |
11 | ||
6650c4de | 12 | #include <crypto/algapi.h> |
0eb76ba2 | 13 | #include <crypto/internal/cipher.h> |
043a4400 | 14 | #include <crypto/internal/skcipher.h> |
91652be5 DH |
15 | #include <linux/err.h> |
16 | #include <linux/init.h> | |
17 | #include <linux/kernel.h> | |
18 | #include <linux/module.h> | |
91652be5 | 19 | |
043a4400 HX |
20 | static int crypto_pcbc_encrypt_segment(struct skcipher_request *req, |
21 | struct skcipher_walk *walk, | |
d0b9007a | 22 | struct crypto_cipher *tfm) |
91652be5 | 23 | { |
91652be5 DH |
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; | |
251b7aea | 28 | u8 * const iv = walk->iv; |
91652be5 DH |
29 | |
30 | do { | |
d0b9007a | 31 | crypto_xor(iv, src, bsize); |
043a4400 | 32 | crypto_cipher_encrypt_one(tfm, dst, iv); |
45fe93df | 33 | crypto_xor_cpy(iv, dst, src, bsize); |
91652be5 DH |
34 | |
35 | src += bsize; | |
36 | dst += bsize; | |
37 | } while ((nbytes -= bsize) >= bsize); | |
38 | ||
39 | return nbytes; | |
40 | } | |
41 | ||
043a4400 HX |
42 | static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req, |
43 | struct skcipher_walk *walk, | |
d0b9007a | 44 | struct crypto_cipher *tfm) |
91652be5 | 45 | { |
91652be5 DH |
46 | int bsize = crypto_cipher_blocksize(tfm); |
47 | unsigned int nbytes = walk->nbytes; | |
48 | u8 *src = walk->src.virt.addr; | |
251b7aea | 49 | u8 * const iv = walk->iv; |
6650c4de | 50 | u8 tmpbuf[MAX_CIPHER_BLOCKSIZE]; |
91652be5 DH |
51 | |
52 | do { | |
53 | memcpy(tmpbuf, src, bsize); | |
d0b9007a | 54 | crypto_xor(iv, src, bsize); |
043a4400 | 55 | crypto_cipher_encrypt_one(tfm, src, iv); |
45fe93df | 56 | crypto_xor_cpy(iv, tmpbuf, src, bsize); |
91652be5 DH |
57 | |
58 | src += bsize; | |
59 | } while ((nbytes -= bsize) >= bsize); | |
60 | ||
91652be5 DH |
61 | return nbytes; |
62 | } | |
63 | ||
043a4400 | 64 | static int crypto_pcbc_encrypt(struct skcipher_request *req) |
91652be5 | 65 | { |
043a4400 | 66 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
0be487ba | 67 | struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
043a4400 HX |
68 | struct skcipher_walk walk; |
69 | unsigned int nbytes; | |
91652be5 DH |
70 | int err; |
71 | ||
043a4400 | 72 | err = skcipher_walk_virt(&walk, req, false); |
91652be5 DH |
73 | |
74 | while ((nbytes = walk.nbytes)) { | |
75 | if (walk.src.virt.addr == walk.dst.virt.addr) | |
043a4400 | 76 | nbytes = crypto_pcbc_encrypt_inplace(req, &walk, |
0be487ba | 77 | cipher); |
91652be5 | 78 | else |
043a4400 | 79 | nbytes = crypto_pcbc_encrypt_segment(req, &walk, |
0be487ba | 80 | cipher); |
043a4400 | 81 | err = skcipher_walk_done(&walk, nbytes); |
91652be5 DH |
82 | } |
83 | ||
84 | return err; | |
85 | } | |
86 | ||
043a4400 HX |
87 | static int crypto_pcbc_decrypt_segment(struct skcipher_request *req, |
88 | struct skcipher_walk *walk, | |
d0b9007a | 89 | struct crypto_cipher *tfm) |
91652be5 | 90 | { |
91652be5 DH |
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; | |
251b7aea | 95 | u8 * const iv = walk->iv; |
91652be5 DH |
96 | |
97 | do { | |
043a4400 | 98 | crypto_cipher_decrypt_one(tfm, dst, src); |
d0b9007a | 99 | crypto_xor(dst, iv, bsize); |
45fe93df | 100 | crypto_xor_cpy(iv, dst, src, bsize); |
91652be5 DH |
101 | |
102 | src += bsize; | |
103 | dst += bsize; | |
104 | } while ((nbytes -= bsize) >= bsize); | |
105 | ||
91652be5 DH |
106 | return nbytes; |
107 | } | |
108 | ||
043a4400 HX |
109 | static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req, |
110 | struct skcipher_walk *walk, | |
d0b9007a | 111 | struct crypto_cipher *tfm) |
91652be5 | 112 | { |
91652be5 DH |
113 | int bsize = crypto_cipher_blocksize(tfm); |
114 | unsigned int nbytes = walk->nbytes; | |
115 | u8 *src = walk->src.virt.addr; | |
251b7aea | 116 | u8 * const iv = walk->iv; |
6650c4de | 117 | u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32)); |
91652be5 DH |
118 | |
119 | do { | |
120 | memcpy(tmpbuf, src, bsize); | |
043a4400 | 121 | crypto_cipher_decrypt_one(tfm, src, src); |
d0b9007a | 122 | crypto_xor(src, iv, bsize); |
45fe93df | 123 | crypto_xor_cpy(iv, src, tmpbuf, bsize); |
91652be5 DH |
124 | |
125 | src += bsize; | |
126 | } while ((nbytes -= bsize) >= bsize); | |
127 | ||
91652be5 DH |
128 | return nbytes; |
129 | } | |
130 | ||
043a4400 | 131 | static int crypto_pcbc_decrypt(struct skcipher_request *req) |
91652be5 | 132 | { |
043a4400 | 133 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
0be487ba | 134 | struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
043a4400 HX |
135 | struct skcipher_walk walk; |
136 | unsigned int nbytes; | |
91652be5 DH |
137 | int err; |
138 | ||
043a4400 | 139 | err = skcipher_walk_virt(&walk, req, false); |
91652be5 DH |
140 | |
141 | while ((nbytes = walk.nbytes)) { | |
142 | if (walk.src.virt.addr == walk.dst.virt.addr) | |
043a4400 | 143 | nbytes = crypto_pcbc_decrypt_inplace(req, &walk, |
0be487ba | 144 | cipher); |
91652be5 | 145 | else |
043a4400 | 146 | nbytes = crypto_pcbc_decrypt_segment(req, &walk, |
0be487ba | 147 | cipher); |
043a4400 | 148 | err = skcipher_walk_done(&walk, nbytes); |
91652be5 DH |
149 | } |
150 | ||
151 | return err; | |
152 | } | |
153 | ||
043a4400 | 154 | static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb) |
91652be5 | 155 | { |
043a4400 | 156 | struct skcipher_instance *inst; |
ebc610e5 HX |
157 | int err; |
158 | ||
b3c16bfc | 159 | inst = skcipher_alloc_instance_simple(tmpl, tb); |
0be487ba EB |
160 | if (IS_ERR(inst)) |
161 | return PTR_ERR(inst); | |
91652be5 | 162 | |
043a4400 HX |
163 | inst->alg.encrypt = crypto_pcbc_encrypt; |
164 | inst->alg.decrypt = crypto_pcbc_decrypt; | |
91652be5 | 165 | |
043a4400 HX |
166 | err = skcipher_register_instance(tmpl, inst); |
167 | if (err) | |
0be487ba | 168 | inst->free(inst); |
b3c16bfc | 169 | |
043a4400 | 170 | return err; |
91652be5 DH |
171 | } |
172 | ||
173 | static struct crypto_template crypto_pcbc_tmpl = { | |
174 | .name = "pcbc", | |
043a4400 | 175 | .create = crypto_pcbc_create, |
91652be5 DH |
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 | ||
c4741b23 | 189 | subsys_initcall(crypto_pcbc_module_init); |
91652be5 DH |
190 | module_exit(crypto_pcbc_module_exit); |
191 | ||
192 | MODULE_LICENSE("GPL"); | |
0be487ba | 193 | MODULE_DESCRIPTION("PCBC block cipher mode of operation"); |
4943ba16 | 194 | MODULE_ALIAS_CRYPTO("pcbc"); |
0eb76ba2 | 195 | MODULE_IMPORT_NS(CRYPTO_INTERNAL); |