arm/arm64: KVM: Turn off vcpus on PSCI shutdown/reboot
[linux/fpc-iii.git] / arch / arm / crypto / sha1_neon_glue.c
blob6f1b411b1d55440b62ff7959b15ac7c9e9cb09d5
1 /*
2 * Glue code for the SHA1 Secure Hash Algorithm assembler implementation using
3 * ARM NEON instructions.
5 * Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi>
7 * This file is based on sha1_generic.c and sha1_ssse3_glue.c:
8 * Copyright (c) Alan Smithee.
9 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
10 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
11 * Copyright (c) Mathias Krause <minipli@googlemail.com>
12 * Copyright (c) Chandramouli Narayanan <mouli@linux.intel.com>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
21 #include <crypto/internal/hash.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/mm.h>
25 #include <linux/cryptohash.h>
26 #include <linux/types.h>
27 #include <crypto/sha.h>
28 #include <asm/byteorder.h>
29 #include <asm/neon.h>
30 #include <asm/simd.h>
31 #include <asm/crypto/sha1.h>
34 asmlinkage void sha1_transform_neon(void *state_h, const char *data,
35 unsigned int rounds);
38 static int sha1_neon_init(struct shash_desc *desc)
40 struct sha1_state *sctx = shash_desc_ctx(desc);
42 *sctx = (struct sha1_state){
43 .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
46 return 0;
49 static int __sha1_neon_update(struct shash_desc *desc, const u8 *data,
50 unsigned int len, unsigned int partial)
52 struct sha1_state *sctx = shash_desc_ctx(desc);
53 unsigned int done = 0;
55 sctx->count += len;
57 if (partial) {
58 done = SHA1_BLOCK_SIZE - partial;
59 memcpy(sctx->buffer + partial, data, done);
60 sha1_transform_neon(sctx->state, sctx->buffer, 1);
63 if (len - done >= SHA1_BLOCK_SIZE) {
64 const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
66 sha1_transform_neon(sctx->state, data + done, rounds);
67 done += rounds * SHA1_BLOCK_SIZE;
70 memcpy(sctx->buffer, data + done, len - done);
72 return 0;
75 static int sha1_neon_update(struct shash_desc *desc, const u8 *data,
76 unsigned int len)
78 struct sha1_state *sctx = shash_desc_ctx(desc);
79 unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
80 int res;
82 /* Handle the fast case right here */
83 if (partial + len < SHA1_BLOCK_SIZE) {
84 sctx->count += len;
85 memcpy(sctx->buffer + partial, data, len);
87 return 0;
90 if (!may_use_simd()) {
91 res = sha1_update_arm(desc, data, len);
92 } else {
93 kernel_neon_begin();
94 res = __sha1_neon_update(desc, data, len, partial);
95 kernel_neon_end();
98 return res;
102 /* Add padding and return the message digest. */
103 static int sha1_neon_final(struct shash_desc *desc, u8 *out)
105 struct sha1_state *sctx = shash_desc_ctx(desc);
106 unsigned int i, index, padlen;
107 __be32 *dst = (__be32 *)out;
108 __be64 bits;
109 static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
111 bits = cpu_to_be64(sctx->count << 3);
113 /* Pad out to 56 mod 64 and append length */
114 index = sctx->count % SHA1_BLOCK_SIZE;
115 padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
116 if (!may_use_simd()) {
117 sha1_update_arm(desc, padding, padlen);
118 sha1_update_arm(desc, (const u8 *)&bits, sizeof(bits));
119 } else {
120 kernel_neon_begin();
121 /* We need to fill a whole block for __sha1_neon_update() */
122 if (padlen <= 56) {
123 sctx->count += padlen;
124 memcpy(sctx->buffer + index, padding, padlen);
125 } else {
126 __sha1_neon_update(desc, padding, padlen, index);
128 __sha1_neon_update(desc, (const u8 *)&bits, sizeof(bits), 56);
129 kernel_neon_end();
132 /* Store state in digest */
133 for (i = 0; i < 5; i++)
134 dst[i] = cpu_to_be32(sctx->state[i]);
136 /* Wipe context */
137 memset(sctx, 0, sizeof(*sctx));
139 return 0;
142 static int sha1_neon_export(struct shash_desc *desc, void *out)
144 struct sha1_state *sctx = shash_desc_ctx(desc);
146 memcpy(out, sctx, sizeof(*sctx));
148 return 0;
151 static int sha1_neon_import(struct shash_desc *desc, const void *in)
153 struct sha1_state *sctx = shash_desc_ctx(desc);
155 memcpy(sctx, in, sizeof(*sctx));
157 return 0;
160 static struct shash_alg alg = {
161 .digestsize = SHA1_DIGEST_SIZE,
162 .init = sha1_neon_init,
163 .update = sha1_neon_update,
164 .final = sha1_neon_final,
165 .export = sha1_neon_export,
166 .import = sha1_neon_import,
167 .descsize = sizeof(struct sha1_state),
168 .statesize = sizeof(struct sha1_state),
169 .base = {
170 .cra_name = "sha1",
171 .cra_driver_name = "sha1-neon",
172 .cra_priority = 250,
173 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
174 .cra_blocksize = SHA1_BLOCK_SIZE,
175 .cra_module = THIS_MODULE,
179 static int __init sha1_neon_mod_init(void)
181 if (!cpu_has_neon())
182 return -ENODEV;
184 return crypto_register_shash(&alg);
187 static void __exit sha1_neon_mod_fini(void)
189 crypto_unregister_shash(&alg);
192 module_init(sha1_neon_mod_init);
193 module_exit(sha1_neon_mod_fini);
195 MODULE_LICENSE("GPL");
196 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, NEON accelerated");
197 MODULE_ALIAS("sha1");