| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * sm3-ce-glue.c - SM3 secure hash using ARMv8.2 Crypto Extensions |
| 4 | * |
| 5 | * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org> |
| 6 | */ |
| 7 | |
| 8 | #include <crypto/internal/hash.h> |
| 9 | #include <crypto/sm3.h> |
| 10 | #include <crypto/sm3_base.h> |
| 11 | #include <linux/cpufeature.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | |
| 15 | #include <asm/simd.h> |
| 16 | |
| 17 | MODULE_DESCRIPTION("SM3 secure hash using ARMv8 Crypto Extensions" ); |
| 18 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>" ); |
| 19 | MODULE_LICENSE("GPL v2" ); |
| 20 | |
| 21 | asmlinkage void sm3_ce_transform(struct sm3_state *sst, u8 const *src, |
| 22 | int blocks); |
| 23 | |
| 24 | static int sm3_ce_update(struct shash_desc *desc, const u8 *data, |
| 25 | unsigned int len) |
| 26 | { |
| 27 | int remain; |
| 28 | |
| 29 | scoped_ksimd() { |
| 30 | remain = sm3_base_do_update_blocks(desc, data, len, block_fn: sm3_ce_transform); |
| 31 | } |
| 32 | return remain; |
| 33 | } |
| 34 | |
| 35 | static int sm3_ce_finup(struct shash_desc *desc, const u8 *data, |
| 36 | unsigned int len, u8 *out) |
| 37 | { |
| 38 | scoped_ksimd() { |
| 39 | sm3_base_do_finup(desc, src: data, len, block_fn: sm3_ce_transform); |
| 40 | } |
| 41 | return sm3_base_finish(desc, out); |
| 42 | } |
| 43 | |
| 44 | static struct shash_alg sm3_alg = { |
| 45 | .digestsize = SM3_DIGEST_SIZE, |
| 46 | .init = sm3_base_init, |
| 47 | .update = sm3_ce_update, |
| 48 | .finup = sm3_ce_finup, |
| 49 | .descsize = SM3_STATE_SIZE, |
| 50 | .base.cra_name = "sm3" , |
| 51 | .base.cra_driver_name = "sm3-ce" , |
| 52 | .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY | |
| 53 | CRYPTO_AHASH_ALG_FINUP_MAX, |
| 54 | .base.cra_blocksize = SM3_BLOCK_SIZE, |
| 55 | .base.cra_module = THIS_MODULE, |
| 56 | .base.cra_priority = 400, |
| 57 | }; |
| 58 | |
| 59 | static int __init sm3_ce_mod_init(void) |
| 60 | { |
| 61 | return crypto_register_shash(alg: &sm3_alg); |
| 62 | } |
| 63 | |
| 64 | static void __exit sm3_ce_mod_fini(void) |
| 65 | { |
| 66 | crypto_unregister_shash(alg: &sm3_alg); |
| 67 | } |
| 68 | |
| 69 | module_cpu_feature_match(SM3, sm3_ce_mod_init); |
| 70 | module_exit(sm3_ce_mod_fini); |
| 71 | |