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: sha3-generic - Use API partial block handling

Use the Crypto API partial block handling.

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

+47 -61
+43 -58
crypto/sha3_generic.c
··· 9 9 * Ard Biesheuvel <ard.biesheuvel@linaro.org> 10 10 */ 11 11 #include <crypto/internal/hash.h> 12 - #include <linux/init.h> 13 - #include <linux/module.h> 14 - #include <linux/types.h> 15 12 #include <crypto/sha3.h> 13 + #include <linux/kernel.h> 14 + #include <linux/module.h> 15 + #include <linux/string.h> 16 16 #include <linux/unaligned.h> 17 17 18 18 /* ··· 161 161 int crypto_sha3_init(struct shash_desc *desc) 162 162 { 163 163 struct sha3_state *sctx = shash_desc_ctx(desc); 164 - unsigned int digest_size = crypto_shash_digestsize(desc->tfm); 165 - 166 - sctx->rsiz = 200 - 2 * digest_size; 167 - sctx->rsizw = sctx->rsiz / 8; 168 - sctx->partial = 0; 169 164 170 165 memset(sctx->st, 0, sizeof(sctx->st)); 171 166 return 0; 172 167 } 173 168 EXPORT_SYMBOL(crypto_sha3_init); 174 169 175 - int crypto_sha3_update(struct shash_desc *desc, const u8 *data, 176 - unsigned int len) 170 + static int crypto_sha3_update(struct shash_desc *desc, const u8 *data, 171 + unsigned int len) 177 172 { 173 + unsigned int rsiz = crypto_shash_blocksize(desc->tfm); 178 174 struct sha3_state *sctx = shash_desc_ctx(desc); 179 - unsigned int done; 180 - const u8 *src; 175 + unsigned int rsizw = rsiz / 8; 181 176 182 - done = 0; 183 - src = data; 177 + do { 178 + int i; 184 179 185 - if ((sctx->partial + len) > (sctx->rsiz - 1)) { 186 - if (sctx->partial) { 187 - done = -sctx->partial; 188 - memcpy(sctx->buf + sctx->partial, data, 189 - done + sctx->rsiz); 190 - src = sctx->buf; 191 - } 180 + for (i = 0; i < rsizw; i++) 181 + sctx->st[i] ^= get_unaligned_le64(data + 8 * i); 182 + keccakf(sctx->st); 192 183 193 - do { 194 - unsigned int i; 195 - 196 - for (i = 0; i < sctx->rsizw; i++) 197 - sctx->st[i] ^= get_unaligned_le64(src + 8 * i); 198 - keccakf(sctx->st); 199 - 200 - done += sctx->rsiz; 201 - src = data + done; 202 - } while (done + (sctx->rsiz - 1) < len); 203 - 204 - sctx->partial = 0; 205 - } 206 - memcpy(sctx->buf + sctx->partial, src, len - done); 207 - sctx->partial += (len - done); 208 - 209 - return 0; 184 + data += rsiz; 185 + len -= rsiz; 186 + } while (len >= rsiz); 187 + return len; 210 188 } 211 - EXPORT_SYMBOL(crypto_sha3_update); 212 189 213 - int crypto_sha3_final(struct shash_desc *desc, u8 *out) 190 + static int crypto_sha3_finup(struct shash_desc *desc, const u8 *src, 191 + unsigned int len, u8 *out) 214 192 { 215 - struct sha3_state *sctx = shash_desc_ctx(desc); 216 - unsigned int i, inlen = sctx->partial; 217 193 unsigned int digest_size = crypto_shash_digestsize(desc->tfm); 194 + unsigned int rsiz = crypto_shash_blocksize(desc->tfm); 195 + struct sha3_state *sctx = shash_desc_ctx(desc); 196 + __le64 block[SHA3_224_BLOCK_SIZE / 8] = {}; 218 197 __le64 *digest = (__le64 *)out; 198 + unsigned int rsizw = rsiz / 8; 199 + u8 *p; 200 + int i; 219 201 220 - sctx->buf[inlen++] = 0x06; 221 - memset(sctx->buf + inlen, 0, sctx->rsiz - inlen); 222 - sctx->buf[sctx->rsiz - 1] |= 0x80; 202 + p = memcpy(block, src, len); 203 + p[len++] = 0x06; 204 + p[rsiz - 1] |= 0x80; 223 205 224 - for (i = 0; i < sctx->rsizw; i++) 225 - sctx->st[i] ^= get_unaligned_le64(sctx->buf + 8 * i); 206 + for (i = 0; i < rsizw; i++) 207 + sctx->st[i] ^= le64_to_cpu(block[i]); 208 + memzero_explicit(block, sizeof(block)); 226 209 227 210 keccakf(sctx->st); 228 211 ··· 215 232 if (digest_size & 4) 216 233 put_unaligned_le32(sctx->st[i], (__le32 *)digest); 217 234 218 - memset(sctx, 0, sizeof(*sctx)); 219 235 return 0; 220 236 } 221 - EXPORT_SYMBOL(crypto_sha3_final); 222 237 223 238 static struct shash_alg algs[] = { { 224 239 .digestsize = SHA3_224_DIGEST_SIZE, 225 240 .init = crypto_sha3_init, 226 241 .update = crypto_sha3_update, 227 - .final = crypto_sha3_final, 228 - .descsize = sizeof(struct sha3_state), 242 + .finup = crypto_sha3_finup, 243 + .descsize = SHA3_STATE_SIZE, 229 244 .base.cra_name = "sha3-224", 230 245 .base.cra_driver_name = "sha3-224-generic", 246 + .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 231 247 .base.cra_blocksize = SHA3_224_BLOCK_SIZE, 232 248 .base.cra_module = THIS_MODULE, 233 249 }, { 234 250 .digestsize = SHA3_256_DIGEST_SIZE, 235 251 .init = crypto_sha3_init, 236 252 .update = crypto_sha3_update, 237 - .final = crypto_sha3_final, 238 - .descsize = sizeof(struct sha3_state), 253 + .finup = crypto_sha3_finup, 254 + .descsize = SHA3_STATE_SIZE, 239 255 .base.cra_name = "sha3-256", 240 256 .base.cra_driver_name = "sha3-256-generic", 257 + .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 241 258 .base.cra_blocksize = SHA3_256_BLOCK_SIZE, 242 259 .base.cra_module = THIS_MODULE, 243 260 }, { 244 261 .digestsize = SHA3_384_DIGEST_SIZE, 245 262 .init = crypto_sha3_init, 246 263 .update = crypto_sha3_update, 247 - .final = crypto_sha3_final, 248 - .descsize = sizeof(struct sha3_state), 264 + .finup = crypto_sha3_finup, 265 + .descsize = SHA3_STATE_SIZE, 249 266 .base.cra_name = "sha3-384", 250 267 .base.cra_driver_name = "sha3-384-generic", 268 + .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 251 269 .base.cra_blocksize = SHA3_384_BLOCK_SIZE, 252 270 .base.cra_module = THIS_MODULE, 253 271 }, { 254 272 .digestsize = SHA3_512_DIGEST_SIZE, 255 273 .init = crypto_sha3_init, 256 274 .update = crypto_sha3_update, 257 - .final = crypto_sha3_final, 258 - .descsize = sizeof(struct sha3_state), 275 + .finup = crypto_sha3_finup, 276 + .descsize = SHA3_STATE_SIZE, 259 277 .base.cra_name = "sha3-512", 260 278 .base.cra_driver_name = "sha3-512-generic", 279 + .base.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 261 280 .base.cra_blocksize = SHA3_512_BLOCK_SIZE, 262 281 .base.cra_module = THIS_MODULE, 263 282 } }; ··· 274 289 crypto_unregister_shashes(algs, ARRAY_SIZE(algs)); 275 290 } 276 291 277 - subsys_initcall(sha3_generic_mod_init); 292 + module_init(sha3_generic_mod_init); 278 293 module_exit(sha3_generic_mod_fini); 279 294 280 295 MODULE_LICENSE("GPL");
+4 -3
include/crypto/sha3.h
··· 5 5 #ifndef __CRYPTO_SHA3_H__ 6 6 #define __CRYPTO_SHA3_H__ 7 7 8 + #include <linux/types.h> 9 + 8 10 #define SHA3_224_DIGEST_SIZE (224 / 8) 9 11 #define SHA3_224_BLOCK_SIZE (200 - 2 * SHA3_224_DIGEST_SIZE) 10 12 ··· 21 19 22 20 #define SHA3_STATE_SIZE 200 23 21 22 + struct shash_desc; 23 + 24 24 struct sha3_state { 25 25 u64 st[SHA3_STATE_SIZE / 8]; 26 26 unsigned int rsiz; ··· 33 29 }; 34 30 35 31 int crypto_sha3_init(struct shash_desc *desc); 36 - int crypto_sha3_update(struct shash_desc *desc, const u8 *data, 37 - unsigned int len); 38 - int crypto_sha3_final(struct shash_desc *desc, u8 *out); 39 32 40 33 #endif