From 5bb050f4f7d4fd29b131aa28dc8449ddcf8a7360 Mon Sep 17 00:00:00 2001 From: Brayan Alberto Date: Sat, 11 Jul 2026 01:33:57 -0600 Subject: [PATCH] Zero scrypt scratch buffers before freeing --- lib-platform/crypto/crypto_scrypt.c | 6 ++++++ lib/crypto/crypto_scrypt-ref.c | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/lib-platform/crypto/crypto_scrypt.c b/lib-platform/crypto/crypto_scrypt.c index 6057f0af..6b137689 100644 --- a/lib-platform/crypto/crypto_scrypt.c +++ b/lib-platform/crypto/crypto_scrypt.c @@ -44,6 +44,7 @@ #include "crypto_scrypt_smix_sse2.h" #include "crypto_scrypt.h" +#include "insecure_memzero.h" static void (* smix_func)(uint8_t *, size_t, uint64_t, void *, void *) = NULL; @@ -144,12 +145,15 @@ crypto_scrypt_internal(const uint8_t * passwd, size_t passwdlen, PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen); /* Free memory. */ + insecure_memzero(V, (size_t)(128 * r * N)); #if defined(MAP_ANON) && defined(HAVE_MMAP) if (munmap(V0, (size_t)(128 * r * N))) goto err2; #else free(V0); #endif + insecure_memzero(XY, 256 * r + 64); + insecure_memzero(B, 128 * r * p); free(XY0); free(B0); @@ -157,8 +161,10 @@ crypto_scrypt_internal(const uint8_t * passwd, size_t passwdlen, return (0); err2: + insecure_memzero(XY, 256 * r + 64); free(XY0); err1: + insecure_memzero(B, 128 * r * p); free(B0); err0: /* Failure! */ diff --git a/lib/crypto/crypto_scrypt-ref.c b/lib/crypto/crypto_scrypt-ref.c index 0d56c505..74f98082 100644 --- a/lib/crypto/crypto_scrypt-ref.c +++ b/lib/crypto/crypto_scrypt-ref.c @@ -35,6 +35,7 @@ #include "sysendian.h" #include "crypto_scrypt.h" +#include "insecure_memzero.h" static void blkcpy(uint8_t *, uint8_t *, size_t); static void blkxor(uint8_t *, uint8_t *, size_t); @@ -267,6 +268,9 @@ crypto_scrypt(const uint8_t * passwd, size_t passwdlen, PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen); /* Free memory. */ + insecure_memzero(V, 128 * r * N); + insecure_memzero(XY, 256 * r); + insecure_memzero(B, 128 * r * p); free(V); free(XY); free(B); @@ -275,8 +279,10 @@ crypto_scrypt(const uint8_t * passwd, size_t passwdlen, return (0); err2: + insecure_memzero(XY, 256 * r); free(XY); err1: + insecure_memzero(B, 128 * r * p); free(B); err0: /* Failure! */