aboutsummaryrefslogtreecommitdiffstats
path: root/librecrypt_fill_with_random_.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-05-14 22:20:02 +0200
committerMattias Andrée <m@maandree.se>2026-05-14 22:20:02 +0200
commitf07a9f80fb6f3099d75534c1e64f448d4b397931 (patch)
tree30ebf4afafb0e66226fa7f642db083341c8c96e4 /librecrypt_fill_with_random_.c
parentlibrecrypt.h: add @since (diff)
downloadlibrecrypt-f07a9f80fb6f3099d75534c1e64f448d4b397931.tar.gz
librecrypt-f07a9f80fb6f3099d75534c1e64f448d4b397931.tar.bz2
librecrypt-f07a9f80fb6f3099d75534c1e64f448d4b397931.tar.xz
Fix minor errors in the test and check that we are not writing out of bounds
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'librecrypt_fill_with_random_.c')
-rw-r--r--librecrypt_fill_with_random_.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/librecrypt_fill_with_random_.c b/librecrypt_fill_with_random_.c
index 1361652..a1acc84 100644
--- a/librecrypt_fill_with_random_.c
+++ b/librecrypt_fill_with_random_.c
@@ -106,8 +106,17 @@ main(void)
EXPECT(librecrypt_fill_with_random_(buf2, sizeof(buf1), NULL, NULL) == 0);
EXPECT(memcmp(buf1, buf2, sizeof(buf1)));
+ /* Check that it is not writing too much */
+ CANARY_FILL(buf1);
+ CANARY_FILL(buf2);
+ EXPECT(librecrypt_fill_with_random_(buf1, sizeof(buf1) / 2u, NULL, NULL) == 0);
+ EXPECT(librecrypt_fill_with_random_(buf2, sizeof(buf2) / 2u, NULL, NULL) == 0);
+ EXPECT(memcmp(buf1, buf2, sizeof(buf1) / 2u));
+ CANARY_CHECK(buf1, sizeof(buf1) / 2u);
+ CANARY_CHECK(buf2, sizeof(buf2) / 2u);
+
/* Check stateless all-at-once RNG */
- memset(buf1, 99, sizeof(buf1));
+ CANARY_FILL(buf1);
errno = 0;
EXPECT(librecrypt_fill_with_random_(buf1, sizeof(buf1), &zero, NULL) == 0);
EXPECT(errno == 0);
@@ -115,7 +124,7 @@ main(void)
EXPECT(!buf1[i]);
/* Check stateful chunked RNG (importantly, chunks are smaller than pattern) */
- memset(buf1, 99, sizeof(buf1));
+ CANARY_FILL(buf1);
s = 0u;
errno = 0;
EXPECT(librecrypt_fill_with_random_(buf1, sizeof(buf1), &seq, &s) == 0);
@@ -124,7 +133,7 @@ main(void)
EXPECT(buf1[i] == s);
/* Check stateful one-byte-per-call RNG */
- memset(buf1, 99, sizeof(buf1));
+ CANARY_FILL(buf1);
s = 0u;
errno = 0;
EXPECT(librecrypt_fill_with_random_(buf1, sizeof(buf1), &next, &s) == 0);
@@ -133,13 +142,12 @@ main(void)
EXPECT(buf1[i] == s);
/* Check failure in RNG */
- memset(buf1, 99, sizeof(buf1));
+ CANARY_FILL(buf1);
s = 0u;
errno = 0;
EXPECT(librecrypt_fill_with_random_(buf1, sizeof(buf1), &failer, NULL) == -1);
EXPECT(errno == EDOM);
- for (s = 0u, i = 0u; i < sizeof(buf1); i++, s++)
- EXPECT(buf1[i] == 99);
+ CANARY_CHECK(buf1, 0u);
/* Check function abort(3)s if RNG returns 0 */
EXPECT_ABORT(rv = librecrypt_fill_with_random_(buf1, sizeof(buf1), &zero_ret, NULL));