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