1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Glue code for SHA-1 implementation for SPE instructions (PPC) 4 * 5 * Based on generic implementation. 6 * 7 * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de> 8 */ 9 10 #include <crypto/internal/hash.h> 11 #include <linux/init.h> 12 #include <linux/module.h> 13 #include <linux/mm.h> 14 #include <linux/cryptohash.h> 15 #include <linux/types.h> 16 #include <crypto/sha.h> 17 #include <asm/byteorder.h> 18 #include <asm/switch_to.h> 19 #include <linux/hardirq.h> 20 21 /* 22 * MAX_BYTES defines the number of bytes that are allowed to be processed 23 * between preempt_disable() and preempt_enable(). SHA1 takes ~1000 24 * operations per 64 bytes. e500 cores can issue two arithmetic instructions 25 * per clock cycle using one 32/64 bit unit (SU1) and one 32 bit unit (SU2). 26 * Thus 2KB of input data will need an estimated maximum of 18,000 cycles. 27 * Headroom for cache misses included. Even with the low end model clocked 28 * at 667 MHz this equals to a critical time window of less than 27us. 29 * 30 */ 31 #define MAX_BYTES 2048 32 33 extern void ppc_spe_sha1_transform(u32 *state, const u8 *src, u32 blocks); 34 35 static void spe_begin(void) 36 { 37 /* We just start SPE operations and will save SPE registers later. */ 38 preempt_disable(); 39 enable_kernel_spe(); 40 } 41 42 static void spe_end(void) 43 { 44 disable_kernel_spe(); 45 /* reenable preemption */ 46 preempt_enable(); 47 } 48 49 static inline void ppc_sha1_clear_context(struct sha1_state *sctx) 50 { 51 int count = sizeof(struct sha1_state) >> 2; 52 u32 *ptr = (u32 *)sctx; 53 54 /* make sure we can clear the fast way */ 55 BUILD_BUG_ON(sizeof(struct sha1_state) % 4); 56 do { *ptr++ = 0; } while (--count); 57 } 58 59 static int ppc_spe_sha1_init(struct shash_desc *desc) 60 { 61 struct sha1_state *sctx = shash_desc_ctx(desc); 62 63 sctx->state[0] = SHA1_H0; 64 sctx->state[1] = SHA1_H1; 65 sctx->state[2] = SHA1_H2; 66 sctx->state[3] = SHA1_H3; 67 sctx->state[4] = SHA1_H4; 68 sctx->count = 0; 69 70 return 0; 71 } 72 73 static int ppc_spe_sha1_update(struct shash_desc *desc, const u8 *data, 74 unsigned int len) 75 { 76 struct sha1_state *sctx = shash_desc_ctx(desc); 77 const unsigned int offset = sctx->count & 0x3f; 78 const unsigned int avail = 64 - offset; 79 unsigned int bytes; 80 const u8 *src = data; 81 82 if (avail > len) { 83 sctx->count += len; 84 memcpy((char *)sctx->buffer + offset, src, len); 85 return 0; 86 } 87 88 sctx->count += len; 89 90 if (offset) { 91 memcpy((char *)sctx->buffer + offset, src, avail); 92 93 spe_begin(); 94 ppc_spe_sha1_transform(sctx->state, (const u8 *)sctx->buffer, 1); 95 spe_end(); 96 97 len -= avail; 98 src += avail; 99 } 100 101 while (len > 63) { 102 bytes = (len > MAX_BYTES) ? MAX_BYTES : len; 103 bytes = bytes & ~0x3f; 104 105 spe_begin(); 106 ppc_spe_sha1_transform(sctx->state, src, bytes >> 6); 107 spe_end(); 108 109 src += bytes; 110 len -= bytes; 111 }; 112 113 memcpy((char *)sctx->buffer, src, len); 114 return 0; 115 } 116 117 static int ppc_spe_sha1_final(struct shash_desc *desc, u8 *out) 118 { 119 struct sha1_state *sctx = shash_desc_ctx(desc); 120 const unsigned int offset = sctx->count & 0x3f; 121 char *p = (char *)sctx->buffer + offset; 122 int padlen; 123 __be64 *pbits = (__be64 *)(((char *)&sctx->buffer) + 56); 124 __be32 *dst = (__be32 *)out; 125 126 padlen = 55 - offset; 127 *p++ = 0x80; 128 129 spe_begin(); 130 131 if (padlen < 0) { 132 memset(p, 0x00, padlen + sizeof (u64)); 133 ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1); 134 p = (char *)sctx->buffer; 135 padlen = 56; 136 } 137 138 memset(p, 0, padlen); 139 *pbits = cpu_to_be64(sctx->count << 3); 140 ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1); 141 142 spe_end(); 143 144 dst[0] = cpu_to_be32(sctx->state[0]); 145 dst[1] = cpu_to_be32(sctx->state[1]); 146 dst[2] = cpu_to_be32(sctx->state[2]); 147 dst[3] = cpu_to_be32(sctx->state[3]); 148 dst[4] = cpu_to_be32(sctx->state[4]); 149 150 ppc_sha1_clear_context(sctx); 151 return 0; 152 } 153 154 static int ppc_spe_sha1_export(struct shash_desc *desc, void *out) 155 { 156 struct sha1_state *sctx = shash_desc_ctx(desc); 157 158 memcpy(out, sctx, sizeof(*sctx)); 159 return 0; 160 } 161 162 static int ppc_spe_sha1_import(struct shash_desc *desc, const void *in) 163 { 164 struct sha1_state *sctx = shash_desc_ctx(desc); 165 166 memcpy(sctx, in, sizeof(*sctx)); 167 return 0; 168 } 169 170 static struct shash_alg alg = { 171 .digestsize = SHA1_DIGEST_SIZE, 172 .init = ppc_spe_sha1_init, 173 .update = ppc_spe_sha1_update, 174 .final = ppc_spe_sha1_final, 175 .export = ppc_spe_sha1_export, 176 .import = ppc_spe_sha1_import, 177 .descsize = sizeof(struct sha1_state), 178 .statesize = sizeof(struct sha1_state), 179 .base = { 180 .cra_name = "sha1", 181 .cra_driver_name= "sha1-ppc-spe", 182 .cra_priority = 300, 183 .cra_blocksize = SHA1_BLOCK_SIZE, 184 .cra_module = THIS_MODULE, 185 } 186 }; 187 188 static int __init ppc_spe_sha1_mod_init(void) 189 { 190 return crypto_register_shash(&alg); 191 } 192 193 static void __exit ppc_spe_sha1_mod_fini(void) 194 { 195 crypto_unregister_shash(&alg); 196 } 197 198 module_init(ppc_spe_sha1_mod_init); 199 module_exit(ppc_spe_sha1_mod_fini); 200 201 MODULE_LICENSE("GPL"); 202 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, SPE optimized"); 203 204 MODULE_ALIAS_CRYPTO("sha1"); 205 MODULE_ALIAS_CRYPTO("sha1-ppc-spe"); 206