Linux 4.15.10
[linux/fpc-iii.git] / arch / mips / cavium-octeon / crypto / octeon-sha1.c
blob2b74b5b67caeb1647dd929b66c0354c74b9e6f8e
1 /*
2 * Cryptographic API.
4 * SHA1 Secure Hash Algorithm.
6 * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>.
8 * Based on crypto/sha1_generic.c, which is:
10 * Copyright (c) Alan Smithee.
11 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
12 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
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.
20 #include <linux/mm.h>
21 #include <crypto/sha.h>
22 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/module.h>
25 #include <asm/byteorder.h>
26 #include <asm/octeon/octeon.h>
27 #include <crypto/internal/hash.h>
29 #include "octeon-crypto.h"
32 * We pass everything as 64-bit. OCTEON can handle misaligned data.
35 static void octeon_sha1_store_hash(struct sha1_state *sctx)
37 u64 *hash = (u64 *)sctx->state;
38 union {
39 u32 word[2];
40 u64 dword;
41 } hash_tail = { { sctx->state[4], } };
43 write_octeon_64bit_hash_dword(hash[0], 0);
44 write_octeon_64bit_hash_dword(hash[1], 1);
45 write_octeon_64bit_hash_dword(hash_tail.dword, 2);
46 memzero_explicit(&hash_tail.word[0], sizeof(hash_tail.word[0]));
49 static void octeon_sha1_read_hash(struct sha1_state *sctx)
51 u64 *hash = (u64 *)sctx->state;
52 union {
53 u32 word[2];
54 u64 dword;
55 } hash_tail;
57 hash[0] = read_octeon_64bit_hash_dword(0);
58 hash[1] = read_octeon_64bit_hash_dword(1);
59 hash_tail.dword = read_octeon_64bit_hash_dword(2);
60 sctx->state[4] = hash_tail.word[0];
61 memzero_explicit(&hash_tail.dword, sizeof(hash_tail.dword));
64 static void octeon_sha1_transform(const void *_block)
66 const u64 *block = _block;
68 write_octeon_64bit_block_dword(block[0], 0);
69 write_octeon_64bit_block_dword(block[1], 1);
70 write_octeon_64bit_block_dword(block[2], 2);
71 write_octeon_64bit_block_dword(block[3], 3);
72 write_octeon_64bit_block_dword(block[4], 4);
73 write_octeon_64bit_block_dword(block[5], 5);
74 write_octeon_64bit_block_dword(block[6], 6);
75 octeon_sha1_start(block[7]);
78 static int octeon_sha1_init(struct shash_desc *desc)
80 struct sha1_state *sctx = shash_desc_ctx(desc);
82 sctx->state[0] = SHA1_H0;
83 sctx->state[1] = SHA1_H1;
84 sctx->state[2] = SHA1_H2;
85 sctx->state[3] = SHA1_H3;
86 sctx->state[4] = SHA1_H4;
87 sctx->count = 0;
89 return 0;
92 static void __octeon_sha1_update(struct sha1_state *sctx, const u8 *data,
93 unsigned int len)
95 unsigned int partial;
96 unsigned int done;
97 const u8 *src;
99 partial = sctx->count % SHA1_BLOCK_SIZE;
100 sctx->count += len;
101 done = 0;
102 src = data;
104 if ((partial + len) >= SHA1_BLOCK_SIZE) {
105 if (partial) {
106 done = -partial;
107 memcpy(sctx->buffer + partial, data,
108 done + SHA1_BLOCK_SIZE);
109 src = sctx->buffer;
112 do {
113 octeon_sha1_transform(src);
114 done += SHA1_BLOCK_SIZE;
115 src = data + done;
116 } while (done + SHA1_BLOCK_SIZE <= len);
118 partial = 0;
120 memcpy(sctx->buffer + partial, src, len - done);
123 static int octeon_sha1_update(struct shash_desc *desc, const u8 *data,
124 unsigned int len)
126 struct sha1_state *sctx = shash_desc_ctx(desc);
127 struct octeon_cop2_state state;
128 unsigned long flags;
131 * Small updates never reach the crypto engine, so the generic sha1 is
132 * faster because of the heavyweight octeon_crypto_enable() /
133 * octeon_crypto_disable().
135 if ((sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE)
136 return crypto_sha1_update(desc, data, len);
138 flags = octeon_crypto_enable(&state);
139 octeon_sha1_store_hash(sctx);
141 __octeon_sha1_update(sctx, data, len);
143 octeon_sha1_read_hash(sctx);
144 octeon_crypto_disable(&state, flags);
146 return 0;
149 static int octeon_sha1_final(struct shash_desc *desc, u8 *out)
151 struct sha1_state *sctx = shash_desc_ctx(desc);
152 static const u8 padding[64] = { 0x80, };
153 struct octeon_cop2_state state;
154 __be32 *dst = (__be32 *)out;
155 unsigned int pad_len;
156 unsigned long flags;
157 unsigned int index;
158 __be64 bits;
159 int i;
161 /* Save number of bits. */
162 bits = cpu_to_be64(sctx->count << 3);
164 /* Pad out to 56 mod 64. */
165 index = sctx->count & 0x3f;
166 pad_len = (index < 56) ? (56 - index) : ((64+56) - index);
168 flags = octeon_crypto_enable(&state);
169 octeon_sha1_store_hash(sctx);
171 __octeon_sha1_update(sctx, padding, pad_len);
173 /* Append length (before padding). */
174 __octeon_sha1_update(sctx, (const u8 *)&bits, sizeof(bits));
176 octeon_sha1_read_hash(sctx);
177 octeon_crypto_disable(&state, flags);
179 /* Store state in digest */
180 for (i = 0; i < 5; i++)
181 dst[i] = cpu_to_be32(sctx->state[i]);
183 /* Zeroize sensitive information. */
184 memset(sctx, 0, sizeof(*sctx));
186 return 0;
189 static int octeon_sha1_export(struct shash_desc *desc, void *out)
191 struct sha1_state *sctx = shash_desc_ctx(desc);
193 memcpy(out, sctx, sizeof(*sctx));
194 return 0;
197 static int octeon_sha1_import(struct shash_desc *desc, const void *in)
199 struct sha1_state *sctx = shash_desc_ctx(desc);
201 memcpy(sctx, in, sizeof(*sctx));
202 return 0;
205 static struct shash_alg octeon_sha1_alg = {
206 .digestsize = SHA1_DIGEST_SIZE,
207 .init = octeon_sha1_init,
208 .update = octeon_sha1_update,
209 .final = octeon_sha1_final,
210 .export = octeon_sha1_export,
211 .import = octeon_sha1_import,
212 .descsize = sizeof(struct sha1_state),
213 .statesize = sizeof(struct sha1_state),
214 .base = {
215 .cra_name = "sha1",
216 .cra_driver_name= "octeon-sha1",
217 .cra_priority = OCTEON_CR_OPCODE_PRIORITY,
218 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
219 .cra_blocksize = SHA1_BLOCK_SIZE,
220 .cra_module = THIS_MODULE,
224 static int __init octeon_sha1_mod_init(void)
226 if (!octeon_has_crypto())
227 return -ENOTSUPP;
228 return crypto_register_shash(&octeon_sha1_alg);
231 static void __exit octeon_sha1_mod_fini(void)
233 crypto_unregister_shash(&octeon_sha1_alg);
236 module_init(octeon_sha1_mod_init);
237 module_exit(octeon_sha1_mod_fini);
239 MODULE_LICENSE("GPL");
240 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm (OCTEON)");
241 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");