1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * sha3-ce-glue.c - core SHA-3 transform using v8.2 Crypto Extensions
4 *
5 * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
6 *
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.
10 */
11
12 #include <asm/hwcap.h>
13 #include <asm/neon.h>
14 #include <asm/simd.h>
15 #include <asm/unaligned.h>
16 #include <crypto/internal/hash.h>
17 #include <crypto/internal/simd.h>
18 #include <crypto/sha3.h>
19 #include <linux/cpufeature.h>
20 #include <linux/crypto.h>
21 #include <linux/module.h>
22
23 MODULE_DESCRIPTION("SHA3 secure hash using ARMv8 Crypto Extensions");
24 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
25 MODULE_LICENSE("GPL v2");
26 MODULE_ALIAS_CRYPTO("sha3-224");
27 MODULE_ALIAS_CRYPTO("sha3-256");
28 MODULE_ALIAS_CRYPTO("sha3-384");
29 MODULE_ALIAS_CRYPTO("sha3-512");
30
31 asmlinkage void sha3_ce_transform(u64 *st, const u8 *data, int blocks,
32 int md_len);
33
sha3_update(struct shash_desc * desc,const u8 * data,unsigned int len)34 static int sha3_update(struct shash_desc *desc, const u8 *data,
35 unsigned int len)
36 {
37 struct sha3_state *sctx = shash_desc_ctx(desc);
38 unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
39
40 if (!crypto_simd_usable())
41 return crypto_sha3_update(desc, data, len);
42
43 if ((sctx->partial + len) >= sctx->rsiz) {
44 int blocks;
45
46 if (sctx->partial) {
47 int p = sctx->rsiz - sctx->partial;
48
49 memcpy(sctx->buf + sctx->partial, data, p);
50 kernel_neon_begin();
51 sha3_ce_transform(sctx->st, sctx->buf, 1, digest_size);
52 kernel_neon_end();
53
54 data += p;
55 len -= p;
56 sctx->partial = 0;
57 }
58
59 blocks = len / sctx->rsiz;
60 len %= sctx->rsiz;
61
62 if (blocks) {
63 kernel_neon_begin();
64 sha3_ce_transform(sctx->st, data, blocks, digest_size);
65 kernel_neon_end();
66 data += blocks * sctx->rsiz;
67 }
68 }
69
70 if (len) {
71 memcpy(sctx->buf + sctx->partial, data, len);
72 sctx->partial += len;
73 }
74 return 0;
75 }
76
sha3_final(struct shash_desc * desc,u8 * out)77 static int sha3_final(struct shash_desc *desc, u8 *out)
78 {
79 struct sha3_state *sctx = shash_desc_ctx(desc);
80 unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
81 __le64 *digest = (__le64 *)out;
82 int i;
83
84 if (!crypto_simd_usable())
85 return crypto_sha3_final(desc, out);
86
87 sctx->buf[sctx->partial++] = 0x06;
88 memset(sctx->buf + sctx->partial, 0, sctx->rsiz - sctx->partial);
89 sctx->buf[sctx->rsiz - 1] |= 0x80;
90
91 kernel_neon_begin();
92 sha3_ce_transform(sctx->st, sctx->buf, 1, digest_size);
93 kernel_neon_end();
94
95 for (i = 0; i < digest_size / 8; i++)
96 put_unaligned_le64(sctx->st[i], digest++);
97
98 if (digest_size & 4)
99 put_unaligned_le32(sctx->st[i], (__le32 *)digest);
100
101 *sctx = (struct sha3_state){};
102 return 0;
103 }
104
105 static struct shash_alg algs[] = { {
106 .digestsize = SHA3_224_DIGEST_SIZE,
107 .init = crypto_sha3_init,
108 .update = sha3_update,
109 .final = sha3_final,
110 .descsize = sizeof(struct sha3_state),
111 .base.cra_name = "sha3-224",
112 .base.cra_driver_name = "sha3-224-ce",
113 .base.cra_blocksize = SHA3_224_BLOCK_SIZE,
114 .base.cra_module = THIS_MODULE,
115 .base.cra_priority = 200,
116 }, {
117 .digestsize = SHA3_256_DIGEST_SIZE,
118 .init = crypto_sha3_init,
119 .update = sha3_update,
120 .final = sha3_final,
121 .descsize = sizeof(struct sha3_state),
122 .base.cra_name = "sha3-256",
123 .base.cra_driver_name = "sha3-256-ce",
124 .base.cra_blocksize = SHA3_256_BLOCK_SIZE,
125 .base.cra_module = THIS_MODULE,
126 .base.cra_priority = 200,
127 }, {
128 .digestsize = SHA3_384_DIGEST_SIZE,
129 .init = crypto_sha3_init,
130 .update = sha3_update,
131 .final = sha3_final,
132 .descsize = sizeof(struct sha3_state),
133 .base.cra_name = "sha3-384",
134 .base.cra_driver_name = "sha3-384-ce",
135 .base.cra_blocksize = SHA3_384_BLOCK_SIZE,
136 .base.cra_module = THIS_MODULE,
137 .base.cra_priority = 200,
138 }, {
139 .digestsize = SHA3_512_DIGEST_SIZE,
140 .init = crypto_sha3_init,
141 .update = sha3_update,
142 .final = sha3_final,
143 .descsize = sizeof(struct sha3_state),
144 .base.cra_name = "sha3-512",
145 .base.cra_driver_name = "sha3-512-ce",
146 .base.cra_blocksize = SHA3_512_BLOCK_SIZE,
147 .base.cra_module = THIS_MODULE,
148 .base.cra_priority = 200,
149 } };
150
sha3_neon_mod_init(void)151 static int __init sha3_neon_mod_init(void)
152 {
153 return crypto_register_shashes(algs, ARRAY_SIZE(algs));
154 }
155
sha3_neon_mod_fini(void)156 static void __exit sha3_neon_mod_fini(void)
157 {
158 crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
159 }
160
161 module_cpu_feature_match(SHA3, sha3_neon_mod_init);
162 module_exit(sha3_neon_mod_fini);
163