1 // SPDX-License-Identifier: GPL-2.0-or-later 1 2 /* 3 * Cryptographic API. 4 * 5 * SHA1 Secure Hash Algorithm. 6 * 7 * Adapted for OCTEON by Aaro Koskinen <aaro.k 8 * 9 * Based on crypto/sha1_generic.c, which is: 10 * 11 * Copyright (c) Alan Smithee. 12 * Copyright (c) Andrew McDonald <andrew@mcdon 13 * Copyright (c) Jean-Francois Dive <jef@linux 14 */ 15 16 #include <linux/mm.h> 17 #include <crypto/sha1.h> 18 #include <crypto/sha1_base.h> 19 #include <linux/init.h> 20 #include <linux/types.h> 21 #include <linux/module.h> 22 #include <asm/byteorder.h> 23 #include <asm/octeon/octeon.h> 24 #include <crypto/internal/hash.h> 25 26 #include "octeon-crypto.h" 27 28 /* 29 * We pass everything as 64-bit. OCTEON can ha 30 */ 31 32 static void octeon_sha1_store_hash(struct sha1 33 { 34 u64 *hash = (u64 *)sctx->state; 35 union { 36 u32 word[2]; 37 u64 dword; 38 } hash_tail = { { sctx->state[4], } }; 39 40 write_octeon_64bit_hash_dword(hash[0], 41 write_octeon_64bit_hash_dword(hash[1], 42 write_octeon_64bit_hash_dword(hash_tai 43 memzero_explicit(&hash_tail.word[0], s 44 } 45 46 static void octeon_sha1_read_hash(struct sha1_ 47 { 48 u64 *hash = (u64 *)sctx->state; 49 union { 50 u32 word[2]; 51 u64 dword; 52 } hash_tail; 53 54 hash[0] = read_octeon_64bit_ha 55 hash[1] = read_octeon_64bit_ha 56 hash_tail.dword = read_octeon_64bit_ha 57 sctx->state[4] = hash_tail.word[0]; 58 memzero_explicit(&hash_tail.dword, siz 59 } 60 61 static void octeon_sha1_transform(const void * 62 { 63 const u64 *block = _block; 64 65 write_octeon_64bit_block_dword(block[0 66 write_octeon_64bit_block_dword(block[1 67 write_octeon_64bit_block_dword(block[2 68 write_octeon_64bit_block_dword(block[3 69 write_octeon_64bit_block_dword(block[4 70 write_octeon_64bit_block_dword(block[5 71 write_octeon_64bit_block_dword(block[6 72 octeon_sha1_start(block[7]); 73 } 74 75 static void __octeon_sha1_update(struct sha1_s 76 unsigned int 77 { 78 unsigned int partial; 79 unsigned int done; 80 const u8 *src; 81 82 partial = sctx->count % SHA1_BLOCK_SIZ 83 sctx->count += len; 84 done = 0; 85 src = data; 86 87 if ((partial + len) >= SHA1_BLOCK_SIZE 88 if (partial) { 89 done = -partial; 90 memcpy(sctx->buffer + 91 done + SHA1_BLO 92 src = sctx->buffer; 93 } 94 95 do { 96 octeon_sha1_transform( 97 done += SHA1_BLOCK_SIZ 98 src = data + done; 99 } while (done + SHA1_BLOCK_SIZ 100 101 partial = 0; 102 } 103 memcpy(sctx->buffer + partial, src, le 104 } 105 106 static int octeon_sha1_update(struct shash_des 107 unsigned int len) 108 { 109 struct sha1_state *sctx = shash_desc_c 110 struct octeon_cop2_state state; 111 unsigned long flags; 112 113 /* 114 * Small updates never reach the crypt 115 * faster because of the heavyweight o 116 * octeon_crypto_disable(). 117 */ 118 if ((sctx->count % SHA1_BLOCK_SIZE) + 119 return crypto_sha1_update(desc 120 121 flags = octeon_crypto_enable(&state); 122 octeon_sha1_store_hash(sctx); 123 124 __octeon_sha1_update(sctx, data, len); 125 126 octeon_sha1_read_hash(sctx); 127 octeon_crypto_disable(&state, flags); 128 129 return 0; 130 } 131 132 static int octeon_sha1_final(struct shash_desc 133 { 134 struct sha1_state *sctx = shash_desc_c 135 static const u8 padding[64] = { 0x80, 136 struct octeon_cop2_state state; 137 __be32 *dst = (__be32 *)out; 138 unsigned int pad_len; 139 unsigned long flags; 140 unsigned int index; 141 __be64 bits; 142 int i; 143 144 /* Save number of bits. */ 145 bits = cpu_to_be64(sctx->count << 3); 146 147 /* Pad out to 56 mod 64. */ 148 index = sctx->count & 0x3f; 149 pad_len = (index < 56) ? (56 - index) 150 151 flags = octeon_crypto_enable(&state); 152 octeon_sha1_store_hash(sctx); 153 154 __octeon_sha1_update(sctx, padding, pa 155 156 /* Append length (before padding). */ 157 __octeon_sha1_update(sctx, (const u8 * 158 159 octeon_sha1_read_hash(sctx); 160 octeon_crypto_disable(&state, flags); 161 162 /* Store state in digest */ 163 for (i = 0; i < 5; i++) 164 dst[i] = cpu_to_be32(sctx->sta 165 166 /* Zeroize sensitive information. */ 167 memset(sctx, 0, sizeof(*sctx)); 168 169 return 0; 170 } 171 172 static int octeon_sha1_export(struct shash_des 173 { 174 struct sha1_state *sctx = shash_desc_c 175 176 memcpy(out, sctx, sizeof(*sctx)); 177 return 0; 178 } 179 180 static int octeon_sha1_import(struct shash_des 181 { 182 struct sha1_state *sctx = shash_desc_c 183 184 memcpy(sctx, in, sizeof(*sctx)); 185 return 0; 186 } 187 188 static struct shash_alg octeon_sha1_alg = { 189 .digestsize = SHA1_DIGEST_SI 190 .init = sha1_base_init 191 .update = octeon_sha1_up 192 .final = octeon_sha1_fi 193 .export = octeon_sha1_ex 194 .import = octeon_sha1_im 195 .descsize = sizeof(struct 196 .statesize = sizeof(struct 197 .base = { 198 .cra_name = "sha1" 199 .cra_driver_name= "octeo 200 .cra_priority = OCTEON 201 .cra_blocksize = SHA1_B 202 .cra_module = THIS_M 203 } 204 }; 205 206 static int __init octeon_sha1_mod_init(void) 207 { 208 if (!octeon_has_crypto()) 209 return -ENOTSUPP; 210 return crypto_register_shash(&octeon_s 211 } 212 213 static void __exit octeon_sha1_mod_fini(void) 214 { 215 crypto_unregister_shash(&octeon_sha1_a 216 } 217 218 module_init(octeon_sha1_mod_init); 219 module_exit(octeon_sha1_mod_fini); 220 221 MODULE_LICENSE("GPL"); 222 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm 223 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@ik 224
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.