crypto: hash - Use memzero_explicit() for clearing state

Without the barrier_data() inside memzero_explicit(), the compiler may
optimize away the state-clearing if it can tell that the state is not
used afterwards.

Signed-off-by: Arvind Sankar <nivedita@alum.mit.edu>
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
Arvind Sankar 2020-10-25 10:31:15 -04:00 committed by Herbert Xu
parent 1762818f25
commit 458c0480dc
8 changed files with 12 additions and 8 deletions

View File

@ -168,7 +168,7 @@ static int ghash_final(struct shash_desc *desc, u8 *dst)
put_unaligned_be64(ctx->digest[1], dst); put_unaligned_be64(ctx->digest[1], dst);
put_unaligned_be64(ctx->digest[0], dst + 8); put_unaligned_be64(ctx->digest[0], dst + 8);
*ctx = (struct ghash_desc_ctx){}; memzero_explicit(ctx, sizeof(*ctx));
return 0; return 0;
} }

View File

@ -177,7 +177,7 @@ void poly1305_final_arch(struct poly1305_desc_ctx *dctx, u8 *dst)
} }
poly1305_emit(&dctx->h, dst, dctx->s); poly1305_emit(&dctx->h, dst, dctx->s);
*dctx = (struct poly1305_desc_ctx){}; memzero_explicit(dctx, sizeof(*dctx));
} }
EXPORT_SYMBOL(poly1305_final_arch); EXPORT_SYMBOL(poly1305_final_arch);

View File

@ -94,7 +94,7 @@ static int sha3_final(struct shash_desc *desc, u8 *out)
if (digest_size & 4) if (digest_size & 4)
put_unaligned_le32(sctx->st[i], (__le32 *)digest); put_unaligned_le32(sctx->st[i], (__le32 *)digest);
*sctx = (struct sha3_state){}; memzero_explicit(sctx, sizeof(*sctx));
return 0; return 0;
} }

View File

@ -209,7 +209,7 @@ void poly1305_final_arch(struct poly1305_desc_ctx *dctx, u8 *dst)
} }
poly1305_simd_emit(&dctx->h, dst, dctx->s); poly1305_simd_emit(&dctx->h, dst, dctx->s);
*dctx = (struct poly1305_desc_ctx){}; memzero_explicit(dctx, sizeof(*dctx));
} }
EXPORT_SYMBOL(poly1305_final_arch); EXPORT_SYMBOL(poly1305_final_arch);

View File

@ -12,6 +12,7 @@
#include <crypto/sha.h> #include <crypto/sha.h>
#include <linux/crypto.h> #include <linux/crypto.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/string.h>
#include <asm/unaligned.h> #include <asm/unaligned.h>
@ -101,7 +102,7 @@ static inline int sha1_base_finish(struct shash_desc *desc, u8 *out)
for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++) for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
put_unaligned_be32(sctx->state[i], digest++); put_unaligned_be32(sctx->state[i], digest++);
*sctx = (struct sha1_state){}; memzero_explicit(sctx, sizeof(*sctx));
return 0; return 0;
} }

View File

@ -12,6 +12,7 @@
#include <crypto/sha.h> #include <crypto/sha.h>
#include <linux/crypto.h> #include <linux/crypto.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/string.h>
#include <asm/unaligned.h> #include <asm/unaligned.h>
@ -105,7 +106,7 @@ static inline int sha256_base_finish(struct shash_desc *desc, u8 *out)
for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be32)) for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be32))
put_unaligned_be32(sctx->state[i], digest++); put_unaligned_be32(sctx->state[i], digest++);
*sctx = (struct sha256_state){}; memzero_explicit(sctx, sizeof(*sctx));
return 0; return 0;
} }

View File

@ -12,6 +12,7 @@
#include <crypto/sha.h> #include <crypto/sha.h>
#include <linux/crypto.h> #include <linux/crypto.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/string.h>
#include <asm/unaligned.h> #include <asm/unaligned.h>
@ -126,7 +127,7 @@ static inline int sha512_base_finish(struct shash_desc *desc, u8 *out)
for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be64)) for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be64))
put_unaligned_be64(sctx->state[i], digest++); put_unaligned_be64(sctx->state[i], digest++);
*sctx = (struct sha512_state){}; memzero_explicit(sctx, sizeof(*sctx));
return 0; return 0;
} }

View File

@ -13,6 +13,7 @@
#include <crypto/sm3.h> #include <crypto/sm3.h>
#include <linux/crypto.h> #include <linux/crypto.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/string.h>
#include <asm/unaligned.h> #include <asm/unaligned.h>
typedef void (sm3_block_fn)(struct sm3_state *sst, u8 const *src, int blocks); typedef void (sm3_block_fn)(struct sm3_state *sst, u8 const *src, int blocks);
@ -104,7 +105,7 @@ static inline int sm3_base_finish(struct shash_desc *desc, u8 *out)
for (i = 0; i < SM3_DIGEST_SIZE / sizeof(__be32); i++) for (i = 0; i < SM3_DIGEST_SIZE / sizeof(__be32); i++)
put_unaligned_be32(sctx->state[i], digest++); put_unaligned_be32(sctx->state[i], digest++);
*sctx = (struct sm3_state){}; memzero_explicit(sctx, sizeof(*sctx));
return 0; return 0;
} }