OSDN Git Service

Merge 4.9.141 into android-4.9
[android-x86/kernel.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 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/unaligned.h>
13 #include <crypto/internal/hash.h>
14 #include <crypto/sha.h>
15 #include <crypto/sha1_base.h>
16 #include <linux/cpufeature.h>
17 #include <linux/crypto.h>
18 #include <linux/module.h>
19
20 MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
21 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
22 MODULE_LICENSE("GPL v2");
23
24 struct sha1_ce_state {
25         struct sha1_state       sst;
26         u32                     finalize;
27 };
28
29 asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
30                                   int blocks);
31 #ifdef CONFIG_CFI_CLANG
32 static inline void __cfi_sha1_ce_transform(struct sha1_state *sst,
33                                            u8 const *src, int blocks)
34 {
35         sha1_ce_transform((struct sha1_ce_state *)sst, src, blocks);
36 }
37 #define sha1_ce_transform __cfi_sha1_ce_transform
38 #endif
39
40 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
41 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
42
43 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
44 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
45
46 static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
47                           unsigned int len)
48 {
49         struct sha1_ce_state *sctx = shash_desc_ctx(desc);
50
51         sctx->finalize = 0;
52         kernel_neon_begin_partial(16);
53         sha1_base_do_update(desc, data, len,
54                             (sha1_block_fn *)sha1_ce_transform);
55         kernel_neon_end();
56
57         return 0;
58 }
59
60 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
61                          unsigned int len, u8 *out)
62 {
63         struct sha1_ce_state *sctx = shash_desc_ctx(desc);
64         bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE);
65
66         /*
67          * Allow the asm code to perform the finalization if there is no
68          * partial data and the input is a round multiple of the block size.
69          */
70         sctx->finalize = finalize;
71
72         kernel_neon_begin_partial(16);
73         sha1_base_do_update(desc, data, len,
74                             (sha1_block_fn *)sha1_ce_transform);
75         if (!finalize)
76                 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
77         kernel_neon_end();
78         return sha1_base_finish(desc, out);
79 }
80
81 static int sha1_ce_final(struct shash_desc *desc, u8 *out)
82 {
83         struct sha1_ce_state *sctx = shash_desc_ctx(desc);
84
85         sctx->finalize = 0;
86         kernel_neon_begin_partial(16);
87         sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
88         kernel_neon_end();
89         return sha1_base_finish(desc, out);
90 }
91
92 static struct shash_alg alg = {
93         .init                   = sha1_base_init,
94         .update                 = sha1_ce_update,
95         .final                  = sha1_ce_final,
96         .finup                  = sha1_ce_finup,
97         .descsize               = sizeof(struct sha1_ce_state),
98         .digestsize             = SHA1_DIGEST_SIZE,
99         .base                   = {
100                 .cra_name               = "sha1",
101                 .cra_driver_name        = "sha1-ce",
102                 .cra_priority           = 200,
103                 .cra_flags              = CRYPTO_ALG_TYPE_SHASH,
104                 .cra_blocksize          = SHA1_BLOCK_SIZE,
105                 .cra_module             = THIS_MODULE,
106         }
107 };
108
109 static int __init sha1_ce_mod_init(void)
110 {
111         return crypto_register_shash(&alg);
112 }
113
114 static void __exit sha1_ce_mod_fini(void)
115 {
116         crypto_unregister_shash(&alg);
117 }
118
119 module_cpu_feature_match(SHA1, sha1_ce_mod_init);
120 module_exit(sha1_ce_mod_fini);