forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
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
....@@ -148,7 +134,8 @@
148134 src -= num_blocks - 1;
149135 dst -= num_blocks - 1;
150136
151
- gctx->funcs[i].fn_u.cbc(ctx, dst, src);
137
+ gctx->funcs[i].fn_u.cbc(ctx, (u8 *)dst,
138
+ (const u8 *)src);
152139
153140 nbytes -= func_bytes;
154141 if (nbytes < bsize)
....@@ -202,7 +189,9 @@
202189
203190 /* Process multi-block batch */
204191 do {
205
- gctx->funcs[i].fn_u.ctr(ctx, dst, src, &ctrblk);
192
+ gctx->funcs[i].fn_u.ctr(ctx, (u8 *)dst,
193
+ (const u8 *)src,
194
+ &ctrblk);
206195 src += num_blocks;
207196 dst += num_blocks;
208197 nbytes -= func_bytes;
....@@ -224,7 +213,8 @@
224213
225214 be128_to_le128(&ctrblk, (be128 *)walk.iv);
226215 memcpy(&tmp, walk.src.virt.addr, nbytes);
227
- gctx->funcs[gctx->num_funcs - 1].fn_u.ctr(ctx, &tmp, &tmp,
216
+ gctx->funcs[gctx->num_funcs - 1].fn_u.ctr(ctx, (u8 *)&tmp,
217
+ (const u8 *)&tmp,
228218 &ctrblk);
229219 memcpy(walk.dst.virt.addr, &tmp, nbytes);
230220 le128_to_be128((be128 *)walk.iv, &ctrblk);
....@@ -254,7 +244,8 @@
254244
255245 if (nbytes >= func_bytes) {
256246 do {
257
- gctx->funcs[i].fn_u.xts(ctx, dst, src,
247
+ gctx->funcs[i].fn_u.xts(ctx, (u8 *)dst,
248
+ (const u8 *)src,
258249 walk->iv);
259250
260251 src += num_blocks;
....@@ -274,17 +265,36 @@
274265 int glue_xts_req_128bit(const struct common_glue_ctx *gctx,
275266 struct skcipher_request *req,
276267 common_glue_func_t tweak_fn, void *tweak_ctx,
277
- void *crypt_ctx)
268
+ void *crypt_ctx, bool decrypt)
278269 {
270
+ const bool cts = (req->cryptlen % XTS_BLOCK_SIZE);
279271 const unsigned int bsize = 128 / 8;
272
+ struct skcipher_request subreq;
280273 struct skcipher_walk walk;
281274 bool fpu_enabled = false;
282
- unsigned int nbytes;
275
+ unsigned int nbytes, tail;
283276 int err;
277
+
278
+ if (req->cryptlen < XTS_BLOCK_SIZE)
279
+ return -EINVAL;
280
+
281
+ if (unlikely(cts)) {
282
+ struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
283
+
284
+ tail = req->cryptlen % XTS_BLOCK_SIZE + XTS_BLOCK_SIZE;
285
+
286
+ skcipher_request_set_tfm(&subreq, tfm);
287
+ skcipher_request_set_callback(&subreq,
288
+ crypto_skcipher_get_flags(tfm),
289
+ NULL, NULL);
290
+ skcipher_request_set_crypt(&subreq, req->src, req->dst,
291
+ req->cryptlen - tail, req->iv);
292
+ req = &subreq;
293
+ }
284294
285295 err = skcipher_walk_virt(&walk, req, false);
286296 nbytes = walk.nbytes;
287
- if (!nbytes)
297
+ if (err)
288298 return err;
289299
290300 /* set minimum length to bsize, for tweak_fn */
....@@ -302,14 +312,55 @@
302312 nbytes = walk.nbytes;
303313 }
304314
315
+ if (unlikely(cts)) {
316
+ u8 *next_tweak, *final_tweak = req->iv;
317
+ struct scatterlist *src, *dst;
318
+ struct scatterlist s[2], d[2];
319
+ le128 b[2];
320
+
321
+ dst = src = scatterwalk_ffwd(s, req->src, req->cryptlen);
322
+ if (req->dst != req->src)
323
+ dst = scatterwalk_ffwd(d, req->dst, req->cryptlen);
324
+
325
+ if (decrypt) {
326
+ next_tweak = memcpy(b, req->iv, XTS_BLOCK_SIZE);
327
+ gf128mul_x_ble(b, b);
328
+ } else {
329
+ next_tweak = req->iv;
330
+ }
331
+
332
+ skcipher_request_set_crypt(&subreq, src, dst, XTS_BLOCK_SIZE,
333
+ next_tweak);
334
+
335
+ err = skcipher_walk_virt(&walk, req, false) ?:
336
+ skcipher_walk_done(&walk,
337
+ __glue_xts_req_128bit(gctx, crypt_ctx, &walk));
338
+ if (err)
339
+ goto out;
340
+
341
+ scatterwalk_map_and_copy(b, dst, 0, XTS_BLOCK_SIZE, 0);
342
+ memcpy(b + 1, b, tail - XTS_BLOCK_SIZE);
343
+ scatterwalk_map_and_copy(b, src, XTS_BLOCK_SIZE,
344
+ tail - XTS_BLOCK_SIZE, 0);
345
+ scatterwalk_map_and_copy(b, dst, 0, tail, 1);
346
+
347
+ skcipher_request_set_crypt(&subreq, dst, dst, XTS_BLOCK_SIZE,
348
+ final_tweak);
349
+
350
+ err = skcipher_walk_virt(&walk, req, false) ?:
351
+ skcipher_walk_done(&walk,
352
+ __glue_xts_req_128bit(gctx, crypt_ctx, &walk));
353
+ }
354
+
355
+out:
305356 glue_fpu_end(fpu_enabled);
306357
307358 return err;
308359 }
309360 EXPORT_SYMBOL_GPL(glue_xts_req_128bit);
310361
311
-void glue_xts_crypt_128bit_one(void *ctx, u128 *dst, const u128 *src, le128 *iv,
312
- common_glue_func_t fn)
362
+void glue_xts_crypt_128bit_one(const void *ctx, u8 *dst, const u8 *src,
363
+ le128 *iv, common_glue_func_t fn)
313364 {
314365 le128 ivblk = *iv;
315366
....@@ -317,13 +368,13 @@
317368 gf128mul_x_ble(iv, &ivblk);
318369
319370 /* CC <- T xor C */
320
- u128_xor(dst, src, (u128 *)&ivblk);
371
+ u128_xor((u128 *)dst, (const u128 *)src, (u128 *)&ivblk);
321372
322373 /* PP <- D(Key2,CC) */
323
- fn(ctx, (u8 *)dst, (u8 *)dst);
374
+ fn(ctx, dst, dst);
324375
325376 /* P <- T xor PP */
326
- u128_xor(dst, dst, (u128 *)&ivblk);
377
+ u128_xor((u128 *)dst, (u128 *)dst, (u128 *)&ivblk);
327378 }
328379 EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit_one);
329380