forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/arch/x86/crypto/glue_helper.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Shared glue code for 128bit block ciphers
34 *
....@@ -7,28 +8,13 @@
78 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
89 * CTR part based on code (crypto/ctr.c) by:
910 * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
10
- *
11
- * This program is free software; you can redistribute it and/or modify
12
- * it under the terms of the GNU General Public License as published by
13
- * the Free Software Foundation; either version 2 of the License, or
14
- * (at your option) any later version.
15
- *
16
- * This program is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU General Public License for more details.
20
- *
21
- * You should have received a copy of the GNU General Public License
22
- * along with this program; if not, write to the Free Software
23
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24
- * USA
25
- *
2611 */
2712
2813 #include <linux/module.h>
2914 #include <crypto/b128ops.h>
3015 #include <crypto/gf128mul.h>
3116 #include <crypto/internal/skcipher.h>
17
+#include <crypto/scatterwalk.h>
3218 #include <crypto/xts.h>
3319 #include <asm/crypto/glue_helper.h>
3420
....@@ -38,7 +24,7 @@
3824 void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
3925 const unsigned int bsize = 128 / 8;
4026 struct skcipher_walk walk;
41
- bool fpu_enabled = false;
27
+ bool fpu_enabled;
4228 unsigned int nbytes;
4329 int err;
4430
....@@ -51,7 +37,7 @@
5137 unsigned int i;
5238
5339 fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
54
- &walk, fpu_enabled, nbytes);
40
+ &walk, false, nbytes);
5541 for (i = 0; i < gctx->num_funcs; i++) {
5642 func_bytes = bsize * gctx->funcs[i].num_blocks;
5743
....@@ -69,10 +55,9 @@
6955 if (nbytes < bsize)
7056 break;
7157 }
58
+ glue_fpu_end(fpu_enabled);
7259 err = skcipher_walk_done(&walk, nbytes);
7360 }
74
-
75
- glue_fpu_end(fpu_enabled);
7661 return err;
7762 }
7863 EXPORT_SYMBOL_GPL(glue_ecb_req_128bit);
....@@ -115,7 +100,7 @@
115100 void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
116101 const unsigned int bsize = 128 / 8;
117102 struct skcipher_walk walk;
118
- bool fpu_enabled = false;
103
+ bool fpu_enabled;
119104 unsigned int nbytes;
120105 int err;
121106
....@@ -129,7 +114,7 @@
129114 u128 last_iv;
130115
131116 fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
132
- &walk, fpu_enabled, nbytes);
117
+ &walk, false, nbytes);
133118 /* Start of the last block. */
134119 src += nbytes / bsize - 1;
135120 dst += nbytes / bsize - 1;
....@@ -148,7 +133,8 @@
148133 src -= num_blocks - 1;
149134 dst -= num_blocks - 1;
150135
151
- gctx->funcs[i].fn_u.cbc(ctx, dst, src);
136
+ gctx->funcs[i].fn_u.cbc(ctx, (u8 *)dst,
137
+ (const u8 *)src);
152138
153139 nbytes -= func_bytes;
154140 if (nbytes < bsize)
....@@ -161,10 +147,10 @@
161147 done:
162148 u128_xor(dst, dst, (u128 *)walk.iv);
163149 *(u128 *)walk.iv = last_iv;
150
+ glue_fpu_end(fpu_enabled);
164151 err = skcipher_walk_done(&walk, nbytes);
165152 }
166153
167
- glue_fpu_end(fpu_enabled);
168154 return err;
169155 }
170156 EXPORT_SYMBOL_GPL(glue_cbc_decrypt_req_128bit);
....@@ -175,7 +161,7 @@
175161 void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
176162 const unsigned int bsize = 128 / 8;
177163 struct skcipher_walk walk;
178
- bool fpu_enabled = false;
164
+ bool fpu_enabled;
179165 unsigned int nbytes;
180166 int err;
181167
....@@ -189,7 +175,7 @@
189175 le128 ctrblk;
190176
191177 fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
192
- &walk, fpu_enabled, nbytes);
178
+ &walk, false, nbytes);
193179
194180 be128_to_le128(&ctrblk, (be128 *)walk.iv);
195181
....@@ -202,7 +188,9 @@
202188
203189 /* Process multi-block batch */
204190 do {
205
- gctx->funcs[i].fn_u.ctr(ctx, dst, src, &ctrblk);
191
+ gctx->funcs[i].fn_u.ctr(ctx, (u8 *)dst,
192
+ (const u8 *)src,
193
+ &ctrblk);
206194 src += num_blocks;
207195 dst += num_blocks;
208196 nbytes -= func_bytes;
....@@ -213,10 +201,9 @@
213201 }
214202
215203 le128_to_be128((be128 *)walk.iv, &ctrblk);
204
+ glue_fpu_end(fpu_enabled);
216205 err = skcipher_walk_done(&walk, nbytes);
217206 }
218
-
219
- glue_fpu_end(fpu_enabled);
220207
221208 if (nbytes) {
222209 le128 ctrblk;
....@@ -224,7 +211,8 @@
224211
225212 be128_to_le128(&ctrblk, (be128 *)walk.iv);
226213 memcpy(&tmp, walk.src.virt.addr, nbytes);
227
- gctx->funcs[gctx->num_funcs - 1].fn_u.ctr(ctx, &tmp, &tmp,
214
+ gctx->funcs[gctx->num_funcs - 1].fn_u.ctr(ctx, (u8 *)&tmp,
215
+ (const u8 *)&tmp,
228216 &ctrblk);
229217 memcpy(walk.dst.virt.addr, &tmp, nbytes);
230218 le128_to_be128((be128 *)walk.iv, &ctrblk);
....@@ -254,7 +242,8 @@
254242
255243 if (nbytes >= func_bytes) {
256244 do {
257
- gctx->funcs[i].fn_u.xts(ctx, dst, src,
245
+ gctx->funcs[i].fn_u.xts(ctx, (u8 *)dst,
246
+ (const u8 *)src,
258247 walk->iv);
259248
260249 src += num_blocks;
....@@ -274,17 +263,36 @@
274263 int glue_xts_req_128bit(const struct common_glue_ctx *gctx,
275264 struct skcipher_request *req,
276265 common_glue_func_t tweak_fn, void *tweak_ctx,
277
- void *crypt_ctx)
266
+ void *crypt_ctx, bool decrypt)
278267 {
268
+ const bool cts = (req->cryptlen % XTS_BLOCK_SIZE);
279269 const unsigned int bsize = 128 / 8;
270
+ struct skcipher_request subreq;
280271 struct skcipher_walk walk;
281272 bool fpu_enabled = false;
282
- unsigned int nbytes;
273
+ unsigned int nbytes, tail;
283274 int err;
275
+
276
+ if (req->cryptlen < XTS_BLOCK_SIZE)
277
+ return -EINVAL;
278
+
279
+ if (unlikely(cts)) {
280
+ struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
281
+
282
+ tail = req->cryptlen % XTS_BLOCK_SIZE + XTS_BLOCK_SIZE;
283
+
284
+ skcipher_request_set_tfm(&subreq, tfm);
285
+ skcipher_request_set_callback(&subreq,
286
+ crypto_skcipher_get_flags(tfm),
287
+ NULL, NULL);
288
+ skcipher_request_set_crypt(&subreq, req->src, req->dst,
289
+ req->cryptlen - tail, req->iv);
290
+ req = &subreq;
291
+ }
284292
285293 err = skcipher_walk_virt(&walk, req, false);
286294 nbytes = walk.nbytes;
287
- if (!nbytes)
295
+ if (err)
288296 return err;
289297
290298 /* set minimum length to bsize, for tweak_fn */
....@@ -296,20 +304,67 @@
296304 tweak_fn(tweak_ctx, walk.iv, walk.iv);
297305
298306 while (nbytes) {
307
+ fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
308
+ &walk, fpu_enabled,
309
+ nbytes < bsize ? bsize : nbytes);
299310 nbytes = __glue_xts_req_128bit(gctx, crypt_ctx, &walk);
311
+
312
+ glue_fpu_end(fpu_enabled);
313
+ fpu_enabled = false;
300314
301315 err = skcipher_walk_done(&walk, nbytes);
302316 nbytes = walk.nbytes;
303317 }
304318
319
+ if (unlikely(cts)) {
320
+ u8 *next_tweak, *final_tweak = req->iv;
321
+ struct scatterlist *src, *dst;
322
+ struct scatterlist s[2], d[2];
323
+ le128 b[2];
324
+
325
+ dst = src = scatterwalk_ffwd(s, req->src, req->cryptlen);
326
+ if (req->dst != req->src)
327
+ dst = scatterwalk_ffwd(d, req->dst, req->cryptlen);
328
+
329
+ if (decrypt) {
330
+ next_tweak = memcpy(b, req->iv, XTS_BLOCK_SIZE);
331
+ gf128mul_x_ble(b, b);
332
+ } else {
333
+ next_tweak = req->iv;
334
+ }
335
+
336
+ skcipher_request_set_crypt(&subreq, src, dst, XTS_BLOCK_SIZE,
337
+ next_tweak);
338
+
339
+ err = skcipher_walk_virt(&walk, req, false) ?:
340
+ skcipher_walk_done(&walk,
341
+ __glue_xts_req_128bit(gctx, crypt_ctx, &walk));
342
+ if (err)
343
+ goto out;
344
+
345
+ scatterwalk_map_and_copy(b, dst, 0, XTS_BLOCK_SIZE, 0);
346
+ memcpy(b + 1, b, tail - XTS_BLOCK_SIZE);
347
+ scatterwalk_map_and_copy(b, src, XTS_BLOCK_SIZE,
348
+ tail - XTS_BLOCK_SIZE, 0);
349
+ scatterwalk_map_and_copy(b, dst, 0, tail, 1);
350
+
351
+ skcipher_request_set_crypt(&subreq, dst, dst, XTS_BLOCK_SIZE,
352
+ final_tweak);
353
+
354
+ err = skcipher_walk_virt(&walk, req, false) ?:
355
+ skcipher_walk_done(&walk,
356
+ __glue_xts_req_128bit(gctx, crypt_ctx, &walk));
357
+ }
358
+
359
+out:
305360 glue_fpu_end(fpu_enabled);
306361
307362 return err;
308363 }
309364 EXPORT_SYMBOL_GPL(glue_xts_req_128bit);
310365
311
-void glue_xts_crypt_128bit_one(void *ctx, u128 *dst, const u128 *src, le128 *iv,
312
- common_glue_func_t fn)
366
+void glue_xts_crypt_128bit_one(const void *ctx, u8 *dst, const u8 *src,
367
+ le128 *iv, common_glue_func_t fn)
313368 {
314369 le128 ivblk = *iv;
315370
....@@ -317,13 +372,13 @@
317372 gf128mul_x_ble(iv, &ivblk);
318373
319374 /* CC <- T xor C */
320
- u128_xor(dst, src, (u128 *)&ivblk);
375
+ u128_xor((u128 *)dst, (const u128 *)src, (u128 *)&ivblk);
321376
322377 /* PP <- D(Key2,CC) */
323
- fn(ctx, (u8 *)dst, (u8 *)dst);
378
+ fn(ctx, dst, dst);
324379
325380 /* P <- T xor PP */
326
- u128_xor(dst, dst, (u128 *)&ivblk);
381
+ u128_xor((u128 *)dst, (u128 *)dst, (u128 *)&ivblk);
327382 }
328383 EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit_one);
329384