Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

crypto: powerpc/sha256-spe - Use API partial block handling

Use the Crypto API partial block handling.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

+30 -137
+30 -137
arch/powerpc/crypto/sha256-spe-glue.c
··· 8 8 * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de> 9 9 */ 10 10 11 + #include <asm/switch_to.h> 11 12 #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 13 #include <crypto/sha2.h> 17 14 #include <crypto/sha256_base.h> 18 - #include <asm/byteorder.h> 19 - #include <asm/switch_to.h> 20 - #include <linux/hardirq.h> 15 + #include <linux/kernel.h> 16 + #include <linux/module.h> 17 + #include <linux/preempt.h> 21 18 22 19 /* 23 20 * MAX_BYTES defines the number of bytes that are allowed to be processed ··· 44 47 preempt_enable(); 45 48 } 46 49 47 - static inline void ppc_sha256_clear_context(struct sha256_state *sctx) 50 + static void ppc_spe_sha256_block(struct crypto_sha256_state *sctx, 51 + const u8 *src, int blocks) 48 52 { 49 - int count = sizeof(struct sha256_state) >> 2; 50 - u32 *ptr = (u32 *)sctx; 53 + do { 54 + /* cut input data into smaller blocks */ 55 + int unit = min(blocks, MAX_BYTES / SHA256_BLOCK_SIZE); 51 56 52 - /* make sure we can clear the fast way */ 53 - BUILD_BUG_ON(sizeof(struct sha256_state) % 4); 54 - do { *ptr++ = 0; } while (--count); 57 + spe_begin(); 58 + ppc_spe_sha256_transform(sctx->state, src, unit); 59 + spe_end(); 60 + 61 + src += unit * SHA256_BLOCK_SIZE; 62 + blocks -= unit; 63 + } while (blocks); 55 64 } 56 65 57 66 static int ppc_spe_sha256_update(struct shash_desc *desc, const u8 *data, 58 67 unsigned int len) 59 68 { 60 - struct sha256_state *sctx = shash_desc_ctx(desc); 61 - const unsigned int offset = sctx->count & 0x3f; 62 - const unsigned int avail = 64 - offset; 63 - unsigned int bytes; 64 - const u8 *src = data; 65 - 66 - if (avail > len) { 67 - sctx->count += len; 68 - memcpy((char *)sctx->buf + offset, src, len); 69 - return 0; 70 - } 71 - 72 - sctx->count += len; 73 - 74 - if (offset) { 75 - memcpy((char *)sctx->buf + offset, src, avail); 76 - 77 - spe_begin(); 78 - ppc_spe_sha256_transform(sctx->state, (const u8 *)sctx->buf, 1); 79 - spe_end(); 80 - 81 - len -= avail; 82 - src += avail; 83 - } 84 - 85 - while (len > 63) { 86 - /* cut input data into smaller blocks */ 87 - bytes = (len > MAX_BYTES) ? MAX_BYTES : len; 88 - bytes = bytes & ~0x3f; 89 - 90 - spe_begin(); 91 - ppc_spe_sha256_transform(sctx->state, src, bytes >> 6); 92 - spe_end(); 93 - 94 - src += bytes; 95 - len -= bytes; 96 - } 97 - 98 - memcpy((char *)sctx->buf, src, len); 99 - return 0; 69 + return sha256_base_do_update_blocks(desc, data, len, 70 + ppc_spe_sha256_block); 100 71 } 101 72 102 - static int ppc_spe_sha256_final(struct shash_desc *desc, u8 *out) 73 + static int ppc_spe_sha256_finup(struct shash_desc *desc, const u8 *src, 74 + unsigned int len, u8 *out) 103 75 { 104 - struct sha256_state *sctx = shash_desc_ctx(desc); 105 - const unsigned int offset = sctx->count & 0x3f; 106 - char *p = (char *)sctx->buf + offset; 107 - int padlen; 108 - __be64 *pbits = (__be64 *)(((char *)&sctx->buf) + 56); 109 - __be32 *dst = (__be32 *)out; 110 - 111 - padlen = 55 - offset; 112 - *p++ = 0x80; 113 - 114 - spe_begin(); 115 - 116 - if (padlen < 0) { 117 - memset(p, 0x00, padlen + sizeof (u64)); 118 - ppc_spe_sha256_transform(sctx->state, sctx->buf, 1); 119 - p = (char *)sctx->buf; 120 - padlen = 56; 121 - } 122 - 123 - memset(p, 0, padlen); 124 - *pbits = cpu_to_be64(sctx->count << 3); 125 - ppc_spe_sha256_transform(sctx->state, sctx->buf, 1); 126 - 127 - spe_end(); 128 - 129 - dst[0] = cpu_to_be32(sctx->state[0]); 130 - dst[1] = cpu_to_be32(sctx->state[1]); 131 - dst[2] = cpu_to_be32(sctx->state[2]); 132 - dst[3] = cpu_to_be32(sctx->state[3]); 133 - dst[4] = cpu_to_be32(sctx->state[4]); 134 - dst[5] = cpu_to_be32(sctx->state[5]); 135 - dst[6] = cpu_to_be32(sctx->state[6]); 136 - dst[7] = cpu_to_be32(sctx->state[7]); 137 - 138 - ppc_sha256_clear_context(sctx); 139 - return 0; 140 - } 141 - 142 - static int ppc_spe_sha224_final(struct shash_desc *desc, u8 *out) 143 - { 144 - __be32 D[SHA256_DIGEST_SIZE >> 2]; 145 - __be32 *dst = (__be32 *)out; 146 - 147 - ppc_spe_sha256_final(desc, (u8 *)D); 148 - 149 - /* avoid bytewise memcpy */ 150 - dst[0] = D[0]; 151 - dst[1] = D[1]; 152 - dst[2] = D[2]; 153 - dst[3] = D[3]; 154 - dst[4] = D[4]; 155 - dst[5] = D[5]; 156 - dst[6] = D[6]; 157 - 158 - /* clear sensitive data */ 159 - memzero_explicit(D, SHA256_DIGEST_SIZE); 160 - return 0; 161 - } 162 - 163 - static int ppc_spe_sha256_export(struct shash_desc *desc, void *out) 164 - { 165 - struct sha256_state *sctx = shash_desc_ctx(desc); 166 - 167 - memcpy(out, sctx, sizeof(*sctx)); 168 - return 0; 169 - } 170 - 171 - static int ppc_spe_sha256_import(struct shash_desc *desc, const void *in) 172 - { 173 - struct sha256_state *sctx = shash_desc_ctx(desc); 174 - 175 - memcpy(sctx, in, sizeof(*sctx)); 176 - return 0; 76 + sha256_base_do_finup(desc, src, len, ppc_spe_sha256_block); 77 + return sha256_base_finish(desc, out); 177 78 } 178 79 179 80 static struct shash_alg algs[2] = { { 180 81 .digestsize = SHA256_DIGEST_SIZE, 181 82 .init = sha256_base_init, 182 83 .update = ppc_spe_sha256_update, 183 - .final = ppc_spe_sha256_final, 184 - .export = ppc_spe_sha256_export, 185 - .import = ppc_spe_sha256_import, 186 - .descsize = sizeof(struct sha256_state), 187 - .statesize = sizeof(struct sha256_state), 84 + .finup = ppc_spe_sha256_finup, 85 + .descsize = sizeof(struct crypto_sha256_state), 188 86 .base = { 189 87 .cra_name = "sha256", 190 88 .cra_driver_name= "sha256-ppc-spe", 191 89 .cra_priority = 300, 90 + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY | 91 + CRYPTO_AHASH_ALG_FINUP_MAX, 192 92 .cra_blocksize = SHA256_BLOCK_SIZE, 193 93 .cra_module = THIS_MODULE, 194 94 } ··· 93 199 .digestsize = SHA224_DIGEST_SIZE, 94 200 .init = sha224_base_init, 95 201 .update = ppc_spe_sha256_update, 96 - .final = ppc_spe_sha224_final, 97 - .export = ppc_spe_sha256_export, 98 - .import = ppc_spe_sha256_import, 99 - .descsize = sizeof(struct sha256_state), 100 - .statesize = sizeof(struct sha256_state), 202 + .finup = ppc_spe_sha256_finup, 203 + .descsize = sizeof(struct crypto_sha256_state), 101 204 .base = { 102 205 .cra_name = "sha224", 103 206 .cra_driver_name= "sha224-ppc-spe", 104 207 .cra_priority = 300, 208 + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY | 209 + CRYPTO_AHASH_ALG_FINUP_MAX, 105 210 .cra_blocksize = SHA224_BLOCK_SIZE, 106 211 .cra_module = THIS_MODULE, 107 212 }