1 /* SPDX-License-Identifier: GPL-2.0 */
3 * sha512-ce-glue.c - SHA-384/SHA-512 using ARMv8 Crypto Extensions
5 * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
14 #include <asm/unaligned.h>
15 #include <crypto/internal/hash.h>
16 #include <crypto/sha.h>
17 #include <crypto/sha512_base.h>
18 #include <linux/cpufeature.h>
19 #include <linux/crypto.h>
20 #include <linux/module.h>
22 MODULE_DESCRIPTION("SHA-384/SHA-512 secure hash using ARMv8 Crypto Extensions");
23 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24 MODULE_LICENSE("GPL v2");
26 asmlinkage
void sha512_ce_transform(struct sha512_state
*sst
, u8
const *src
,
29 asmlinkage
void sha512_block_data_order(u64
*digest
, u8
const *src
, int blocks
);
31 static int sha512_ce_update(struct shash_desc
*desc
, const u8
*data
,
35 return sha512_base_do_update(desc
, data
, len
,
36 (sha512_block_fn
*)sha512_block_data_order
);
39 sha512_base_do_update(desc
, data
, len
,
40 (sha512_block_fn
*)sha512_ce_transform
);
46 static int sha512_ce_finup(struct shash_desc
*desc
, const u8
*data
,
47 unsigned int len
, u8
*out
)
49 if (!may_use_simd()) {
51 sha512_base_do_update(desc
, data
, len
,
52 (sha512_block_fn
*)sha512_block_data_order
);
53 sha512_base_do_finalize(desc
,
54 (sha512_block_fn
*)sha512_block_data_order
);
55 return sha512_base_finish(desc
, out
);
59 sha512_base_do_update(desc
, data
, len
,
60 (sha512_block_fn
*)sha512_ce_transform
);
61 sha512_base_do_finalize(desc
, (sha512_block_fn
*)sha512_ce_transform
);
63 return sha512_base_finish(desc
, out
);
66 static int sha512_ce_final(struct shash_desc
*desc
, u8
*out
)
68 if (!may_use_simd()) {
69 sha512_base_do_finalize(desc
,
70 (sha512_block_fn
*)sha512_block_data_order
);
71 return sha512_base_finish(desc
, out
);
75 sha512_base_do_finalize(desc
, (sha512_block_fn
*)sha512_ce_transform
);
77 return sha512_base_finish(desc
, out
);
80 static struct shash_alg algs
[] = { {
81 .init
= sha384_base_init
,
82 .update
= sha512_ce_update
,
83 .final
= sha512_ce_final
,
84 .finup
= sha512_ce_finup
,
85 .descsize
= sizeof(struct sha512_state
),
86 .digestsize
= SHA384_DIGEST_SIZE
,
87 .base
.cra_name
= "sha384",
88 .base
.cra_driver_name
= "sha384-ce",
89 .base
.cra_priority
= 200,
90 .base
.cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
91 .base
.cra_blocksize
= SHA512_BLOCK_SIZE
,
92 .base
.cra_module
= THIS_MODULE
,
94 .init
= sha512_base_init
,
95 .update
= sha512_ce_update
,
96 .final
= sha512_ce_final
,
97 .finup
= sha512_ce_finup
,
98 .descsize
= sizeof(struct sha512_state
),
99 .digestsize
= SHA512_DIGEST_SIZE
,
100 .base
.cra_name
= "sha512",
101 .base
.cra_driver_name
= "sha512-ce",
102 .base
.cra_priority
= 200,
103 .base
.cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
104 .base
.cra_blocksize
= SHA512_BLOCK_SIZE
,
105 .base
.cra_module
= THIS_MODULE
,
108 static int __init
sha512_ce_mod_init(void)
110 return crypto_register_shashes(algs
, ARRAY_SIZE(algs
));
113 static void __exit
sha512_ce_mod_fini(void)
115 crypto_unregister_shashes(algs
, ARRAY_SIZE(algs
));
118 module_cpu_feature_match(SHA512
, sha512_ce_mod_init
);
119 module_exit(sha512_ce_mod_fini
);