1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Cryptographic API.
4 *
5 * SHA1 Secure Hash Algorithm.
6 *
7 * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>.
8 *
9 * Based on crypto/sha1_generic.c, which is:
10 *
11 * Copyright (c) Alan Smithee.
12 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
13 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
14 */
15
16 #include <asm/octeon/crypto.h>
17 #include <asm/octeon/octeon.h>
18
19 /*
20 * We pass everything as 64-bit. OCTEON can handle misaligned data.
21 */
22
octeon_sha1_store_hash(struct sha1_block_state * state)23 static void octeon_sha1_store_hash(struct sha1_block_state *state)
24 {
25 u64 *hash = (u64 *)&state->h[0];
26 union {
27 u32 word[2];
28 u64 dword;
29 } hash_tail = { { state->h[4], } };
30
31 write_octeon_64bit_hash_dword(hash[0], 0);
32 write_octeon_64bit_hash_dword(hash[1], 1);
33 write_octeon_64bit_hash_dword(hash_tail.dword, 2);
34 memzero_explicit(&hash_tail.word[0], sizeof(hash_tail.word[0]));
35 }
36
octeon_sha1_read_hash(struct sha1_block_state * state)37 static void octeon_sha1_read_hash(struct sha1_block_state *state)
38 {
39 u64 *hash = (u64 *)&state->h[0];
40 union {
41 u32 word[2];
42 u64 dword;
43 } hash_tail;
44
45 hash[0] = read_octeon_64bit_hash_dword(0);
46 hash[1] = read_octeon_64bit_hash_dword(1);
47 hash_tail.dword = read_octeon_64bit_hash_dword(2);
48 state->h[4] = hash_tail.word[0];
49 memzero_explicit(&hash_tail.dword, sizeof(hash_tail.dword));
50 }
51
sha1_blocks(struct sha1_block_state * state,const u8 * data,size_t nblocks)52 static void sha1_blocks(struct sha1_block_state *state,
53 const u8 *data, size_t nblocks)
54 {
55 struct octeon_cop2_state cop2_state;
56 unsigned long flags;
57
58 if (!octeon_has_crypto())
59 return sha1_blocks_generic(state, data, nblocks);
60
61 flags = octeon_crypto_enable(&cop2_state);
62 octeon_sha1_store_hash(state);
63
64 do {
65 const u64 *block = (const u64 *)data;
66
67 write_octeon_64bit_block_dword(block[0], 0);
68 write_octeon_64bit_block_dword(block[1], 1);
69 write_octeon_64bit_block_dword(block[2], 2);
70 write_octeon_64bit_block_dword(block[3], 3);
71 write_octeon_64bit_block_dword(block[4], 4);
72 write_octeon_64bit_block_dword(block[5], 5);
73 write_octeon_64bit_block_dword(block[6], 6);
74 octeon_sha1_start(block[7]);
75
76 data += SHA1_BLOCK_SIZE;
77 } while (--nblocks);
78
79 octeon_sha1_read_hash(state);
80 octeon_crypto_disable(&cop2_state, flags);
81 }
82