GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / arm64 / crypto / sha1-ce-glue.c
1 /*
2  * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
3  *
4  * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <asm/neon.h>
12 #include <asm/simd.h>
13 #include <asm/unaligned.h>
14 #include <crypto/internal/hash.h>
15 #include <crypto/sha.h>
16 #include <crypto/sha1_base.h>
17 #include <linux/cpufeature.h>
18 #include <linux/crypto.h>
19 #include <linux/module.h>
20
21 MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
22 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
23 MODULE_LICENSE("GPL v2");
24
25 struct sha1_ce_state {
26         struct sha1_state       sst;
27         u32                     finalize;
28 };
29
30 asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
31                                   int blocks);
32
33 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
34 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
35
36 static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
37                           unsigned int len)
38 {
39         struct sha1_ce_state *sctx = shash_desc_ctx(desc);
40
41         if (!may_use_simd())
42                 return crypto_sha1_update(desc, data, len);
43
44         sctx->finalize = 0;
45         kernel_neon_begin();
46         sha1_base_do_update(desc, data, len,
47                             (sha1_block_fn *)sha1_ce_transform);
48         kernel_neon_end();
49
50         return 0;
51 }
52
53 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
54                          unsigned int len, u8 *out)
55 {
56         struct sha1_ce_state *sctx = shash_desc_ctx(desc);
57         bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len;
58
59         if (!may_use_simd())
60                 return crypto_sha1_finup(desc, data, len, out);
61
62         /*
63          * Allow the asm code to perform the finalization if there is no
64          * partial data and the input is a round multiple of the block size.
65          */
66         sctx->finalize = finalize;
67
68         kernel_neon_begin();
69         sha1_base_do_update(desc, data, len,
70                             (sha1_block_fn *)sha1_ce_transform);
71         if (!finalize)
72                 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
73         kernel_neon_end();
74         return sha1_base_finish(desc, out);
75 }
76
77 static int sha1_ce_final(struct shash_desc *desc, u8 *out)
78 {
79         struct sha1_ce_state *sctx = shash_desc_ctx(desc);
80
81         if (!may_use_simd())
82                 return crypto_sha1_finup(desc, NULL, 0, out);
83
84         sctx->finalize = 0;
85         kernel_neon_begin();
86         sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
87         kernel_neon_end();
88         return sha1_base_finish(desc, out);
89 }
90
91 static struct shash_alg alg = {
92         .init                   = sha1_base_init,
93         .update                 = sha1_ce_update,
94         .final                  = sha1_ce_final,
95         .finup                  = sha1_ce_finup,
96         .descsize               = sizeof(struct sha1_ce_state),
97         .digestsize             = SHA1_DIGEST_SIZE,
98         .base                   = {
99                 .cra_name               = "sha1",
100                 .cra_driver_name        = "sha1-ce",
101                 .cra_priority           = 200,
102                 .cra_flags              = CRYPTO_ALG_TYPE_SHASH,
103                 .cra_blocksize          = SHA1_BLOCK_SIZE,
104                 .cra_module             = THIS_MODULE,
105         }
106 };
107
108 static int __init sha1_ce_mod_init(void)
109 {
110         return crypto_register_shash(&alg);
111 }
112
113 static void __exit sha1_ce_mod_fini(void)
114 {
115         crypto_unregister_shash(&alg);
116 }
117
118 module_cpu_feature_match(SHA1, sha1_ce_mod_init);
119 module_exit(sha1_ce_mod_fini);