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