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