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