Commit 36f87a4a29cb8cd291169483079fde34bad4ef16

Authored by Steffen Klassert
Committed by Herbert Xu
1 parent 4e4ed83be6

crypto: xcbc - Fix alignment calculation of xcbc_tfm_ctx

The alignment calculation of xcbc_tfm_ctx uses alg->cra_alignmask
and not alg->cra_alignmask + 1 as it should. This led to frequent
crashes during the selftest of xcbc(aes-asm) on x86_64
machines. This patch fixes this. Also we use the alignmask
of xcbc and not the alignmask of the underlying algorithm
for the alignmnent calculation in xcbc_create now.

Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Showing 1 changed file with 5 additions and 3 deletions Inline Diff

1 /* 1 /*
2 * Copyright (C)2006 USAGI/WIDE Project 2 * Copyright (C)2006 USAGI/WIDE Project
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or 6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version. 7 * (at your option) any later version.
8 * 8 *
9 * This program is distributed in the hope that it will be useful, 9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software 15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * 17 *
18 * Author: 18 * Author:
19 * Kazunori Miyazawa <miyazawa@linux-ipv6.org> 19 * Kazunori Miyazawa <miyazawa@linux-ipv6.org>
20 */ 20 */
21 21
22 #include <crypto/internal/hash.h> 22 #include <crypto/internal/hash.h>
23 #include <linux/err.h> 23 #include <linux/err.h>
24 #include <linux/kernel.h> 24 #include <linux/kernel.h>
25 25
26 static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101, 26 static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
27 0x02020202, 0x02020202, 0x02020202, 0x02020202, 27 0x02020202, 0x02020202, 0x02020202, 0x02020202,
28 0x03030303, 0x03030303, 0x03030303, 0x03030303}; 28 0x03030303, 0x03030303, 0x03030303, 0x03030303};
29 29
30 /* 30 /*
31 * +------------------------ 31 * +------------------------
32 * | <parent tfm> 32 * | <parent tfm>
33 * +------------------------ 33 * +------------------------
34 * | xcbc_tfm_ctx 34 * | xcbc_tfm_ctx
35 * +------------------------ 35 * +------------------------
36 * | consts (block size * 2) 36 * | consts (block size * 2)
37 * +------------------------ 37 * +------------------------
38 */ 38 */
39 struct xcbc_tfm_ctx { 39 struct xcbc_tfm_ctx {
40 struct crypto_cipher *child; 40 struct crypto_cipher *child;
41 u8 ctx[]; 41 u8 ctx[];
42 }; 42 };
43 43
44 /* 44 /*
45 * +------------------------ 45 * +------------------------
46 * | <shash desc> 46 * | <shash desc>
47 * +------------------------ 47 * +------------------------
48 * | xcbc_desc_ctx 48 * | xcbc_desc_ctx
49 * +------------------------ 49 * +------------------------
50 * | odds (block size) 50 * | odds (block size)
51 * +------------------------ 51 * +------------------------
52 * | prev (block size) 52 * | prev (block size)
53 * +------------------------ 53 * +------------------------
54 */ 54 */
55 struct xcbc_desc_ctx { 55 struct xcbc_desc_ctx {
56 unsigned int len; 56 unsigned int len;
57 u8 ctx[]; 57 u8 ctx[];
58 }; 58 };
59 59
60 static int crypto_xcbc_digest_setkey(struct crypto_shash *parent, 60 static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
61 const u8 *inkey, unsigned int keylen) 61 const u8 *inkey, unsigned int keylen)
62 { 62 {
63 unsigned long alignmask = crypto_shash_alignmask(parent); 63 unsigned long alignmask = crypto_shash_alignmask(parent);
64 struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent); 64 struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
65 int bs = crypto_shash_blocksize(parent); 65 int bs = crypto_shash_blocksize(parent);
66 u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1); 66 u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
67 int err = 0; 67 int err = 0;
68 u8 key1[bs]; 68 u8 key1[bs];
69 69
70 if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen))) 70 if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
71 return err; 71 return err;
72 72
73 crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs); 73 crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
74 crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2); 74 crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
75 crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks); 75 crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
76 76
77 return crypto_cipher_setkey(ctx->child, key1, bs); 77 return crypto_cipher_setkey(ctx->child, key1, bs);
78 78
79 } 79 }
80 80
81 static int crypto_xcbc_digest_init(struct shash_desc *pdesc) 81 static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
82 { 82 {
83 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm); 83 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
84 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc); 84 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
85 int bs = crypto_shash_blocksize(pdesc->tfm); 85 int bs = crypto_shash_blocksize(pdesc->tfm);
86 u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs; 86 u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
87 87
88 ctx->len = 0; 88 ctx->len = 0;
89 memset(prev, 0, bs); 89 memset(prev, 0, bs);
90 90
91 return 0; 91 return 0;
92 } 92 }
93 93
94 static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p, 94 static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
95 unsigned int len) 95 unsigned int len)
96 { 96 {
97 struct crypto_shash *parent = pdesc->tfm; 97 struct crypto_shash *parent = pdesc->tfm;
98 unsigned long alignmask = crypto_shash_alignmask(parent); 98 unsigned long alignmask = crypto_shash_alignmask(parent);
99 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent); 99 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
100 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc); 100 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
101 struct crypto_cipher *tfm = tctx->child; 101 struct crypto_cipher *tfm = tctx->child;
102 int bs = crypto_shash_blocksize(parent); 102 int bs = crypto_shash_blocksize(parent);
103 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1); 103 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
104 u8 *prev = odds + bs; 104 u8 *prev = odds + bs;
105 105
106 /* checking the data can fill the block */ 106 /* checking the data can fill the block */
107 if ((ctx->len + len) <= bs) { 107 if ((ctx->len + len) <= bs) {
108 memcpy(odds + ctx->len, p, len); 108 memcpy(odds + ctx->len, p, len);
109 ctx->len += len; 109 ctx->len += len;
110 return 0; 110 return 0;
111 } 111 }
112 112
113 /* filling odds with new data and encrypting it */ 113 /* filling odds with new data and encrypting it */
114 memcpy(odds + ctx->len, p, bs - ctx->len); 114 memcpy(odds + ctx->len, p, bs - ctx->len);
115 len -= bs - ctx->len; 115 len -= bs - ctx->len;
116 p += bs - ctx->len; 116 p += bs - ctx->len;
117 117
118 crypto_xor(prev, odds, bs); 118 crypto_xor(prev, odds, bs);
119 crypto_cipher_encrypt_one(tfm, prev, prev); 119 crypto_cipher_encrypt_one(tfm, prev, prev);
120 120
121 /* clearing the length */ 121 /* clearing the length */
122 ctx->len = 0; 122 ctx->len = 0;
123 123
124 /* encrypting the rest of data */ 124 /* encrypting the rest of data */
125 while (len > bs) { 125 while (len > bs) {
126 crypto_xor(prev, p, bs); 126 crypto_xor(prev, p, bs);
127 crypto_cipher_encrypt_one(tfm, prev, prev); 127 crypto_cipher_encrypt_one(tfm, prev, prev);
128 p += bs; 128 p += bs;
129 len -= bs; 129 len -= bs;
130 } 130 }
131 131
132 /* keeping the surplus of blocksize */ 132 /* keeping the surplus of blocksize */
133 if (len) { 133 if (len) {
134 memcpy(odds, p, len); 134 memcpy(odds, p, len);
135 ctx->len = len; 135 ctx->len = len;
136 } 136 }
137 137
138 return 0; 138 return 0;
139 } 139 }
140 140
141 static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out) 141 static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
142 { 142 {
143 struct crypto_shash *parent = pdesc->tfm; 143 struct crypto_shash *parent = pdesc->tfm;
144 unsigned long alignmask = crypto_shash_alignmask(parent); 144 unsigned long alignmask = crypto_shash_alignmask(parent);
145 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent); 145 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
146 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc); 146 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
147 struct crypto_cipher *tfm = tctx->child; 147 struct crypto_cipher *tfm = tctx->child;
148 int bs = crypto_shash_blocksize(parent); 148 int bs = crypto_shash_blocksize(parent);
149 u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1); 149 u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
150 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1); 150 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
151 u8 *prev = odds + bs; 151 u8 *prev = odds + bs;
152 unsigned int offset = 0; 152 unsigned int offset = 0;
153 153
154 if (ctx->len != bs) { 154 if (ctx->len != bs) {
155 unsigned int rlen; 155 unsigned int rlen;
156 u8 *p = odds + ctx->len; 156 u8 *p = odds + ctx->len;
157 157
158 *p = 0x80; 158 *p = 0x80;
159 p++; 159 p++;
160 160
161 rlen = bs - ctx->len -1; 161 rlen = bs - ctx->len -1;
162 if (rlen) 162 if (rlen)
163 memset(p, 0, rlen); 163 memset(p, 0, rlen);
164 164
165 offset += bs; 165 offset += bs;
166 } 166 }
167 167
168 crypto_xor(prev, odds, bs); 168 crypto_xor(prev, odds, bs);
169 crypto_xor(prev, consts + offset, bs); 169 crypto_xor(prev, consts + offset, bs);
170 170
171 crypto_cipher_encrypt_one(tfm, out, prev); 171 crypto_cipher_encrypt_one(tfm, out, prev);
172 172
173 return 0; 173 return 0;
174 } 174 }
175 175
176 static int xcbc_init_tfm(struct crypto_tfm *tfm) 176 static int xcbc_init_tfm(struct crypto_tfm *tfm)
177 { 177 {
178 struct crypto_cipher *cipher; 178 struct crypto_cipher *cipher;
179 struct crypto_instance *inst = (void *)tfm->__crt_alg; 179 struct crypto_instance *inst = (void *)tfm->__crt_alg;
180 struct crypto_spawn *spawn = crypto_instance_ctx(inst); 180 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
181 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm); 181 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
182 182
183 cipher = crypto_spawn_cipher(spawn); 183 cipher = crypto_spawn_cipher(spawn);
184 if (IS_ERR(cipher)) 184 if (IS_ERR(cipher))
185 return PTR_ERR(cipher); 185 return PTR_ERR(cipher);
186 186
187 ctx->child = cipher; 187 ctx->child = cipher;
188 188
189 return 0; 189 return 0;
190 }; 190 };
191 191
192 static void xcbc_exit_tfm(struct crypto_tfm *tfm) 192 static void xcbc_exit_tfm(struct crypto_tfm *tfm)
193 { 193 {
194 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm); 194 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
195 crypto_free_cipher(ctx->child); 195 crypto_free_cipher(ctx->child);
196 } 196 }
197 197
198 static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb) 198 static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
199 { 199 {
200 struct shash_instance *inst; 200 struct shash_instance *inst;
201 struct crypto_alg *alg; 201 struct crypto_alg *alg;
202 unsigned long alignmask;
202 int err; 203 int err;
203 204
204 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH); 205 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
205 if (err) 206 if (err)
206 return err; 207 return err;
207 208
208 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, 209 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
209 CRYPTO_ALG_TYPE_MASK); 210 CRYPTO_ALG_TYPE_MASK);
210 if (IS_ERR(alg)) 211 if (IS_ERR(alg))
211 return PTR_ERR(alg); 212 return PTR_ERR(alg);
212 213
213 switch(alg->cra_blocksize) { 214 switch(alg->cra_blocksize) {
214 case 16: 215 case 16:
215 break; 216 break;
216 default: 217 default:
217 goto out_put_alg; 218 goto out_put_alg;
218 } 219 }
219 220
220 inst = shash_alloc_instance("xcbc", alg); 221 inst = shash_alloc_instance("xcbc", alg);
221 err = PTR_ERR(inst); 222 err = PTR_ERR(inst);
222 if (IS_ERR(inst)) 223 if (IS_ERR(inst))
223 goto out_put_alg; 224 goto out_put_alg;
224 225
225 err = crypto_init_spawn(shash_instance_ctx(inst), alg, 226 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
226 shash_crypto_instance(inst), 227 shash_crypto_instance(inst),
227 CRYPTO_ALG_TYPE_MASK); 228 CRYPTO_ALG_TYPE_MASK);
228 if (err) 229 if (err)
229 goto out_free_inst; 230 goto out_free_inst;
230 231
232 alignmask = alg->cra_alignmask | 3;
233 inst->alg.base.cra_alignmask = alignmask;
231 inst->alg.base.cra_priority = alg->cra_priority; 234 inst->alg.base.cra_priority = alg->cra_priority;
232 inst->alg.base.cra_blocksize = alg->cra_blocksize; 235 inst->alg.base.cra_blocksize = alg->cra_blocksize;
233 inst->alg.base.cra_alignmask = alg->cra_alignmask | 3;
234 236
235 inst->alg.digestsize = alg->cra_blocksize; 237 inst->alg.digestsize = alg->cra_blocksize;
236 inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx), 238 inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
237 crypto_tfm_ctx_alignment()) + 239 crypto_tfm_ctx_alignment()) +
238 (alg->cra_alignmask & 240 (alignmask &
239 ~(crypto_tfm_ctx_alignment() - 1)) + 241 ~(crypto_tfm_ctx_alignment() - 1)) +
240 alg->cra_blocksize * 2; 242 alg->cra_blocksize * 2;
241 243
242 inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx), 244 inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
243 alg->cra_alignmask) + 245 alignmask + 1) +
244 alg->cra_blocksize * 2; 246 alg->cra_blocksize * 2;
245 inst->alg.base.cra_init = xcbc_init_tfm; 247 inst->alg.base.cra_init = xcbc_init_tfm;
246 inst->alg.base.cra_exit = xcbc_exit_tfm; 248 inst->alg.base.cra_exit = xcbc_exit_tfm;
247 249
248 inst->alg.init = crypto_xcbc_digest_init; 250 inst->alg.init = crypto_xcbc_digest_init;
249 inst->alg.update = crypto_xcbc_digest_update; 251 inst->alg.update = crypto_xcbc_digest_update;
250 inst->alg.final = crypto_xcbc_digest_final; 252 inst->alg.final = crypto_xcbc_digest_final;
251 inst->alg.setkey = crypto_xcbc_digest_setkey; 253 inst->alg.setkey = crypto_xcbc_digest_setkey;
252 254
253 err = shash_register_instance(tmpl, inst); 255 err = shash_register_instance(tmpl, inst);
254 if (err) { 256 if (err) {
255 out_free_inst: 257 out_free_inst:
256 shash_free_instance(shash_crypto_instance(inst)); 258 shash_free_instance(shash_crypto_instance(inst));
257 } 259 }
258 260
259 out_put_alg: 261 out_put_alg:
260 crypto_mod_put(alg); 262 crypto_mod_put(alg);
261 return err; 263 return err;
262 } 264 }
263 265
264 static struct crypto_template crypto_xcbc_tmpl = { 266 static struct crypto_template crypto_xcbc_tmpl = {
265 .name = "xcbc", 267 .name = "xcbc",
266 .create = xcbc_create, 268 .create = xcbc_create,
267 .free = shash_free_instance, 269 .free = shash_free_instance,
268 .module = THIS_MODULE, 270 .module = THIS_MODULE,
269 }; 271 };
270 272
271 static int __init crypto_xcbc_module_init(void) 273 static int __init crypto_xcbc_module_init(void)
272 { 274 {
273 return crypto_register_template(&crypto_xcbc_tmpl); 275 return crypto_register_template(&crypto_xcbc_tmpl);
274 } 276 }
275 277
276 static void __exit crypto_xcbc_module_exit(void) 278 static void __exit crypto_xcbc_module_exit(void)
277 { 279 {
278 crypto_unregister_template(&crypto_xcbc_tmpl); 280 crypto_unregister_template(&crypto_xcbc_tmpl);
279 } 281 }
280 282
281 module_init(crypto_xcbc_module_init); 283 module_init(crypto_xcbc_module_init);
282 module_exit(crypto_xcbc_module_exit); 284 module_exit(crypto_xcbc_module_exit);
283 285
284 MODULE_LICENSE("GPL"); 286 MODULE_LICENSE("GPL");
285 MODULE_DESCRIPTION("XCBC keyed hash algorithm"); 287 MODULE_DESCRIPTION("XCBC keyed hash algorithm");