Skip to content

Commit dd56df3

Browse files
committed
Small secure random changes
1 parent 80029bf commit dd56df3

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/common/random.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#ifndef ZSTD_RANDOM_H
1212
#define ZSTD_RANDOM_H
1313

14+
#include "mem.h"
15+
1416
/*
1517
* Portability helpers for secure random, exposes the following:
1618
* - `HAS_SECURE_RANDOM` a macro that determines if secure random API is available on
@@ -24,10 +26,11 @@
2426

2527
#define HAS_SECURE_RANDOM
2628
#include <sys/random.h>
27-
static size_t getSecureRandom(void *buf, size_t buflen) {
29+
MEM_STATIC size_t getSecureRandom(void *buf, size_t buflen) {
2830
return getrandom(buf, buflen, GRND_NONBLOCK) != (ssize_t) buflen;
2931
}
3032

33+
3134
#endif
3235

3336

lib/compress/zstd_compress.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,10 @@ ZSTD_reset_matchState(ZSTD_matchState_t* ms,
19101910
int needTagTableInit = 1;
19111911
#ifdef HAS_SECURE_RANDOM
19121912
if(forWho == ZSTD_resetTarget_CCtx) {
1913+
/* TODO: We might not need to generate secure random everytime. For example,
1914+
* we could generate 256 bit of secure random and roll it using a cryptographic
1915+
* hash function. This optimization could lend some performance wins for very
1916+
* small inputs. */
19131917
size_t randomGenerated = getSecureRandom(&ms->hashSalt, sizeof(ms->hashSalt));
19141918
if (!randomGenerated) {
19151919
/* We've successfully generated secure random, so we don't need to explicitly memset

0 commit comments

Comments
 (0)