Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi...
[cascardo/linux.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 <linux/cpufeature.h>
16 #include <linux/crypto.h>
17 #include <linux/module.h>
18
19 MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
20 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
21 MODULE_LICENSE("GPL v2");
22
23 asmlinkage void sha1_ce_transform(int blocks, u8 const *src, u32 *state,
24                                   u8 *head, long bytes);
25
26 static int sha1_init(struct shash_desc *desc)
27 {
28         struct sha1_state *sctx = shash_desc_ctx(desc);
29
30         *sctx = (struct sha1_state){
31                 .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
32         };
33         return 0;
34 }
35
36 static int sha1_update(struct shash_desc *desc, const u8 *data,
37                        unsigned int len)
38 {
39         struct sha1_state *sctx = shash_desc_ctx(desc);
40         unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
41
42         sctx->count += len;
43
44         if ((partial + len) >= SHA1_BLOCK_SIZE) {
45                 int blocks;
46
47                 if (partial) {
48                         int p = SHA1_BLOCK_SIZE - partial;
49
50                         memcpy(sctx->buffer + partial, data, p);
51                         data += p;
52                         len -= p;
53                 }
54
55                 blocks = len / SHA1_BLOCK_SIZE;
56                 len %= SHA1_BLOCK_SIZE;
57
58                 kernel_neon_begin_partial(16);
59                 sha1_ce_transform(blocks, data, sctx->state,
60                                   partial ? sctx->buffer : NULL, 0);
61                 kernel_neon_end();
62
63                 data += blocks * SHA1_BLOCK_SIZE;
64                 partial = 0;
65         }
66         if (len)
67                 memcpy(sctx->buffer + partial, data, len);
68         return 0;
69 }
70
71 static int sha1_final(struct shash_desc *desc, u8 *out)
72 {
73         static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
74
75         struct sha1_state *sctx = shash_desc_ctx(desc);
76         __be64 bits = cpu_to_be64(sctx->count << 3);
77         __be32 *dst = (__be32 *)out;
78         int i;
79
80         u32 padlen = SHA1_BLOCK_SIZE
81                      - ((sctx->count + sizeof(bits)) % SHA1_BLOCK_SIZE);
82
83         sha1_update(desc, padding, padlen);
84         sha1_update(desc, (const u8 *)&bits, sizeof(bits));
85
86         for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
87                 put_unaligned_be32(sctx->state[i], dst++);
88
89         *sctx = (struct sha1_state){};
90         return 0;
91 }
92
93 static int sha1_finup(struct shash_desc *desc, const u8 *data,
94                       unsigned int len, u8 *out)
95 {
96         struct sha1_state *sctx = shash_desc_ctx(desc);
97         __be32 *dst = (__be32 *)out;
98         int blocks;
99         int i;
100
101         if (sctx->count || !len || (len % SHA1_BLOCK_SIZE)) {
102                 sha1_update(desc, data, len);
103                 return sha1_final(desc, out);
104         }
105
106         /*
107          * Use a fast path if the input is a multiple of 64 bytes. In
108          * this case, there is no need to copy data around, and we can
109          * perform the entire digest calculation in a single invocation
110          * of sha1_ce_transform()
111          */
112         blocks = len / SHA1_BLOCK_SIZE;
113
114         kernel_neon_begin_partial(16);
115         sha1_ce_transform(blocks, data, sctx->state, NULL, len);
116         kernel_neon_end();
117
118         for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
119                 put_unaligned_be32(sctx->state[i], dst++);
120
121         *sctx = (struct sha1_state){};
122         return 0;
123 }
124
125 static int sha1_export(struct shash_desc *desc, void *out)
126 {
127         struct sha1_state *sctx = shash_desc_ctx(desc);
128         struct sha1_state *dst = out;
129
130         *dst = *sctx;
131         return 0;
132 }
133
134 static int sha1_import(struct shash_desc *desc, const void *in)
135 {
136         struct sha1_state *sctx = shash_desc_ctx(desc);
137         struct sha1_state const *src = in;
138
139         *sctx = *src;
140         return 0;
141 }
142
143 static struct shash_alg alg = {
144         .init                   = sha1_init,
145         .update                 = sha1_update,
146         .final                  = sha1_final,
147         .finup                  = sha1_finup,
148         .export                 = sha1_export,
149         .import                 = sha1_import,
150         .descsize               = sizeof(struct sha1_state),
151         .digestsize             = SHA1_DIGEST_SIZE,
152         .statesize              = sizeof(struct sha1_state),
153         .base                   = {
154                 .cra_name               = "sha1",
155                 .cra_driver_name        = "sha1-ce",
156                 .cra_priority           = 200,
157                 .cra_flags              = CRYPTO_ALG_TYPE_SHASH,
158                 .cra_blocksize          = SHA1_BLOCK_SIZE,
159                 .cra_module             = THIS_MODULE,
160         }
161 };
162
163 static int __init sha1_ce_mod_init(void)
164 {
165         return crypto_register_shash(&alg);
166 }
167
168 static void __exit sha1_ce_mod_fini(void)
169 {
170         crypto_unregister_shash(&alg);
171 }
172
173 module_cpu_feature_match(SHA1, sha1_ce_mod_init);
174 module_exit(sha1_ce_mod_fini);