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