hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/crypto/simd.c
....@@ -1,35 +1,42 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Shared crypto simd helpers
34 *
45 * Copyright (c) 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
56 * Copyright (c) 2016 Herbert Xu <herbert@gondor.apana.org.au>
7
+ * Copyright (c) 2019 Google LLC
68 *
79 * Based on aesni-intel_glue.c by:
810 * Copyright (C) 2008, Intel Corp.
911 * Author: Huang Ying <ying.huang@intel.com>
12
+ */
13
+
14
+/*
15
+ * Shared crypto SIMD helpers. These functions dynamically create and register
16
+ * an skcipher or AEAD algorithm that wraps another, internal algorithm. The
17
+ * wrapper ensures that the internal algorithm is only executed in a context
18
+ * where SIMD instructions are usable, i.e. where may_use_simd() returns true.
19
+ * If SIMD is already usable, the wrapper directly calls the internal algorithm.
20
+ * Otherwise it defers execution to a workqueue via cryptd.
1021 *
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.
22
+ * This is an alternative to the internal algorithm implementing a fallback for
23
+ * the !may_use_simd() case itself.
1524 *
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, see <http://www.gnu.org/licenses/>.
23
- *
25
+ * Note that the wrapper algorithm is asynchronous, i.e. it has the
26
+ * CRYPTO_ALG_ASYNC flag set. Therefore it won't be found by users who
27
+ * explicitly allocate a synchronous algorithm.
2428 */
2529
2630 #include <crypto/cryptd.h>
31
+#include <crypto/internal/aead.h>
2732 #include <crypto/internal/simd.h>
2833 #include <crypto/internal/skcipher.h>
2934 #include <linux/kernel.h>
3035 #include <linux/module.h>
3136 #include <linux/preempt.h>
3237 #include <asm/simd.h>
38
+
39
+/* skcipher support */
3340
3441 struct simd_skcipher_alg {
3542 const char *ialg_name;
....@@ -45,15 +52,11 @@
4552 {
4653 struct simd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
4754 struct crypto_skcipher *child = &ctx->cryptd_tfm->base;
48
- int err;
4955
5056 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
5157 crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(tfm) &
5258 CRYPTO_TFM_REQ_MASK);
53
- err = crypto_skcipher_setkey(child, key, key_len);
54
- crypto_skcipher_set_flags(tfm, crypto_skcipher_get_flags(child) &
55
- CRYPTO_TFM_RES_MASK);
56
- return err;
59
+ return crypto_skcipher_setkey(child, key, key_len);
5760 }
5861
5962 static int simd_skcipher_encrypt(struct skcipher_request *req)
....@@ -66,7 +69,7 @@
6669 subreq = skcipher_request_ctx(req);
6770 *subreq = *req;
6871
69
- if (!may_use_simd() ||
72
+ if (!crypto_simd_usable() ||
7073 (in_atomic() && cryptd_skcipher_queued(ctx->cryptd_tfm)))
7174 child = &ctx->cryptd_tfm->base;
7275 else
....@@ -87,7 +90,7 @@
8790 subreq = skcipher_request_ctx(req);
8891 *subreq = *req;
8992
90
- if (!may_use_simd() ||
93
+ if (!crypto_simd_usable() ||
9194 (in_atomic() && cryptd_skcipher_queued(ctx->cryptd_tfm)))
9295 child = &ctx->cryptd_tfm->base;
9396 else
....@@ -168,7 +171,8 @@
168171 drvname) >= CRYPTO_MAX_ALG_NAME)
169172 goto out_free_salg;
170173
171
- alg->base.cra_flags = CRYPTO_ALG_ASYNC;
174
+ alg->base.cra_flags = CRYPTO_ALG_ASYNC |
175
+ (ialg->base.cra_flags & CRYPTO_ALG_INHERITED_FLAGS);
172176 alg->base.cra_priority = ialg->base.cra_priority;
173177 alg->base.cra_blocksize = ialg->base.cra_blocksize;
174178 alg->base.cra_alignmask = ialg->base.cra_alignmask;
....@@ -272,4 +276,251 @@
272276 }
273277 EXPORT_SYMBOL_GPL(simd_unregister_skciphers);
274278
279
+/* AEAD support */
280
+
281
+struct simd_aead_alg {
282
+ const char *ialg_name;
283
+ struct aead_alg alg;
284
+};
285
+
286
+struct simd_aead_ctx {
287
+ struct cryptd_aead *cryptd_tfm;
288
+};
289
+
290
+static int simd_aead_setkey(struct crypto_aead *tfm, const u8 *key,
291
+ unsigned int key_len)
292
+{
293
+ struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
294
+ struct crypto_aead *child = &ctx->cryptd_tfm->base;
295
+
296
+ crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
297
+ crypto_aead_set_flags(child, crypto_aead_get_flags(tfm) &
298
+ CRYPTO_TFM_REQ_MASK);
299
+ return crypto_aead_setkey(child, key, key_len);
300
+}
301
+
302
+static int simd_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
303
+{
304
+ struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
305
+ struct crypto_aead *child = &ctx->cryptd_tfm->base;
306
+
307
+ return crypto_aead_setauthsize(child, authsize);
308
+}
309
+
310
+static int simd_aead_encrypt(struct aead_request *req)
311
+{
312
+ struct crypto_aead *tfm = crypto_aead_reqtfm(req);
313
+ struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
314
+ struct aead_request *subreq;
315
+ struct crypto_aead *child;
316
+
317
+ subreq = aead_request_ctx(req);
318
+ *subreq = *req;
319
+
320
+ if (!crypto_simd_usable() ||
321
+ (in_atomic() && cryptd_aead_queued(ctx->cryptd_tfm)))
322
+ child = &ctx->cryptd_tfm->base;
323
+ else
324
+ child = cryptd_aead_child(ctx->cryptd_tfm);
325
+
326
+ aead_request_set_tfm(subreq, child);
327
+
328
+ return crypto_aead_encrypt(subreq);
329
+}
330
+
331
+static int simd_aead_decrypt(struct aead_request *req)
332
+{
333
+ struct crypto_aead *tfm = crypto_aead_reqtfm(req);
334
+ struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
335
+ struct aead_request *subreq;
336
+ struct crypto_aead *child;
337
+
338
+ subreq = aead_request_ctx(req);
339
+ *subreq = *req;
340
+
341
+ if (!crypto_simd_usable() ||
342
+ (in_atomic() && cryptd_aead_queued(ctx->cryptd_tfm)))
343
+ child = &ctx->cryptd_tfm->base;
344
+ else
345
+ child = cryptd_aead_child(ctx->cryptd_tfm);
346
+
347
+ aead_request_set_tfm(subreq, child);
348
+
349
+ return crypto_aead_decrypt(subreq);
350
+}
351
+
352
+static void simd_aead_exit(struct crypto_aead *tfm)
353
+{
354
+ struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
355
+
356
+ cryptd_free_aead(ctx->cryptd_tfm);
357
+}
358
+
359
+static int simd_aead_init(struct crypto_aead *tfm)
360
+{
361
+ struct simd_aead_ctx *ctx = crypto_aead_ctx(tfm);
362
+ struct cryptd_aead *cryptd_tfm;
363
+ struct simd_aead_alg *salg;
364
+ struct aead_alg *alg;
365
+ unsigned reqsize;
366
+
367
+ alg = crypto_aead_alg(tfm);
368
+ salg = container_of(alg, struct simd_aead_alg, alg);
369
+
370
+ cryptd_tfm = cryptd_alloc_aead(salg->ialg_name, CRYPTO_ALG_INTERNAL,
371
+ CRYPTO_ALG_INTERNAL);
372
+ if (IS_ERR(cryptd_tfm))
373
+ return PTR_ERR(cryptd_tfm);
374
+
375
+ ctx->cryptd_tfm = cryptd_tfm;
376
+
377
+ reqsize = crypto_aead_reqsize(cryptd_aead_child(cryptd_tfm));
378
+ reqsize = max(reqsize, crypto_aead_reqsize(&cryptd_tfm->base));
379
+ reqsize += sizeof(struct aead_request);
380
+
381
+ crypto_aead_set_reqsize(tfm, reqsize);
382
+
383
+ return 0;
384
+}
385
+
386
+struct simd_aead_alg *simd_aead_create_compat(const char *algname,
387
+ const char *drvname,
388
+ const char *basename)
389
+{
390
+ struct simd_aead_alg *salg;
391
+ struct crypto_aead *tfm;
392
+ struct aead_alg *ialg;
393
+ struct aead_alg *alg;
394
+ int err;
395
+
396
+ tfm = crypto_alloc_aead(basename, CRYPTO_ALG_INTERNAL,
397
+ CRYPTO_ALG_INTERNAL | CRYPTO_ALG_ASYNC);
398
+ if (IS_ERR(tfm))
399
+ return ERR_CAST(tfm);
400
+
401
+ ialg = crypto_aead_alg(tfm);
402
+
403
+ salg = kzalloc(sizeof(*salg), GFP_KERNEL);
404
+ if (!salg) {
405
+ salg = ERR_PTR(-ENOMEM);
406
+ goto out_put_tfm;
407
+ }
408
+
409
+ salg->ialg_name = basename;
410
+ alg = &salg->alg;
411
+
412
+ err = -ENAMETOOLONG;
413
+ if (snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", algname) >=
414
+ CRYPTO_MAX_ALG_NAME)
415
+ goto out_free_salg;
416
+
417
+ if (snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
418
+ drvname) >= CRYPTO_MAX_ALG_NAME)
419
+ goto out_free_salg;
420
+
421
+ alg->base.cra_flags = CRYPTO_ALG_ASYNC |
422
+ (ialg->base.cra_flags & CRYPTO_ALG_INHERITED_FLAGS);
423
+ alg->base.cra_priority = ialg->base.cra_priority;
424
+ alg->base.cra_blocksize = ialg->base.cra_blocksize;
425
+ alg->base.cra_alignmask = ialg->base.cra_alignmask;
426
+ alg->base.cra_module = ialg->base.cra_module;
427
+ alg->base.cra_ctxsize = sizeof(struct simd_aead_ctx);
428
+
429
+ alg->ivsize = ialg->ivsize;
430
+ alg->maxauthsize = ialg->maxauthsize;
431
+ alg->chunksize = ialg->chunksize;
432
+
433
+ alg->init = simd_aead_init;
434
+ alg->exit = simd_aead_exit;
435
+
436
+ alg->setkey = simd_aead_setkey;
437
+ alg->setauthsize = simd_aead_setauthsize;
438
+ alg->encrypt = simd_aead_encrypt;
439
+ alg->decrypt = simd_aead_decrypt;
440
+
441
+ err = crypto_register_aead(alg);
442
+ if (err)
443
+ goto out_free_salg;
444
+
445
+out_put_tfm:
446
+ crypto_free_aead(tfm);
447
+ return salg;
448
+
449
+out_free_salg:
450
+ kfree(salg);
451
+ salg = ERR_PTR(err);
452
+ goto out_put_tfm;
453
+}
454
+EXPORT_SYMBOL_GPL(simd_aead_create_compat);
455
+
456
+struct simd_aead_alg *simd_aead_create(const char *algname,
457
+ const char *basename)
458
+{
459
+ char drvname[CRYPTO_MAX_ALG_NAME];
460
+
461
+ if (snprintf(drvname, CRYPTO_MAX_ALG_NAME, "simd-%s", basename) >=
462
+ CRYPTO_MAX_ALG_NAME)
463
+ return ERR_PTR(-ENAMETOOLONG);
464
+
465
+ return simd_aead_create_compat(algname, drvname, basename);
466
+}
467
+EXPORT_SYMBOL_GPL(simd_aead_create);
468
+
469
+void simd_aead_free(struct simd_aead_alg *salg)
470
+{
471
+ crypto_unregister_aead(&salg->alg);
472
+ kfree(salg);
473
+}
474
+EXPORT_SYMBOL_GPL(simd_aead_free);
475
+
476
+int simd_register_aeads_compat(struct aead_alg *algs, int count,
477
+ struct simd_aead_alg **simd_algs)
478
+{
479
+ int err;
480
+ int i;
481
+ const char *algname;
482
+ const char *drvname;
483
+ const char *basename;
484
+ struct simd_aead_alg *simd;
485
+
486
+ err = crypto_register_aeads(algs, count);
487
+ if (err)
488
+ return err;
489
+
490
+ for (i = 0; i < count; i++) {
491
+ WARN_ON(strncmp(algs[i].base.cra_name, "__", 2));
492
+ WARN_ON(strncmp(algs[i].base.cra_driver_name, "__", 2));
493
+ algname = algs[i].base.cra_name + 2;
494
+ drvname = algs[i].base.cra_driver_name + 2;
495
+ basename = algs[i].base.cra_driver_name;
496
+ simd = simd_aead_create_compat(algname, drvname, basename);
497
+ err = PTR_ERR(simd);
498
+ if (IS_ERR(simd))
499
+ goto err_unregister;
500
+ simd_algs[i] = simd;
501
+ }
502
+ return 0;
503
+
504
+err_unregister:
505
+ simd_unregister_aeads(algs, count, simd_algs);
506
+ return err;
507
+}
508
+EXPORT_SYMBOL_GPL(simd_register_aeads_compat);
509
+
510
+void simd_unregister_aeads(struct aead_alg *algs, int count,
511
+ struct simd_aead_alg **simd_algs)
512
+{
513
+ int i;
514
+
515
+ crypto_unregister_aeads(algs, count);
516
+
517
+ for (i = 0; i < count; i++) {
518
+ if (simd_algs[i]) {
519
+ simd_aead_free(simd_algs[i]);
520
+ simd_algs[i] = NULL;
521
+ }
522
+ }
523
+}
524
+EXPORT_SYMBOL_GPL(simd_unregister_aeads);
525
+
275526 MODULE_LICENSE("GPL");