Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_random.c

n_random regression: secure random bytes and hex token generation.

n_random regression: secure random bytes and hex token generation.

Author
Castagnier Mickael
Version
1.0
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "nilorea/n_log.h"
#include "nilorea/n_str.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
static int failures = 0;
/* Parse the -V LOG_LEVEL verbosity argument. */
void process_args(int argc, char** argv) {
int opt = 0;
while ((opt = getopt(argc, argv, "hvV:")) != EOF) {
switch (opt) {
case 'V':
if (!strncmp("LOG_NULL", optarg, 8))
else if (!strncmp("LOG_NOTICE", optarg, 10))
else if (!strncmp("LOG_INFO", optarg, 8))
else if (!strncmp("LOG_ERR", optarg, 7))
else if (!strncmp("LOG_DEBUG", optarg, 9))
else {
fprintf(stderr, "Unknown log level %s\n", optarg);
exit(1);
}
break;
case 'v':
fprintf(stderr, "ex_random\n");
exit(1);
case 'h':
default:
fprintf(stderr, "usage: %s [-V LOG_LEVEL]\n", argv[0]);
break;
}
}
}
static void test_bytes(void) {
unsigned char a[32];
unsigned char b[32];
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
if (n_random_bytes(a, sizeof(a)) != 0 || n_random_bytes(b, sizeof(b)) != 0) {
n_log(LOG_ERR, "n_random_bytes failed");
return;
}
/* two independent 32-byte draws colliding is cryptographically impossible */
if (memcmp(a, b, sizeof(a)) == 0) {
n_log(LOG_ERR, "two random draws were identical");
}
/* an all-zero 32-byte buffer after a fill is effectively impossible */
int all_zero = 1;
for (size_t i = 0; i < sizeof(a); i++) {
if (a[i] != 0) {
all_zero = 0;
break;
}
}
if (all_zero) {
n_log(LOG_ERR, "random buffer is all zero");
}
/* zero length is a no-op success; NULL with n>0 is rejected */
if (n_random_bytes(NULL, 0) != 0) {
n_log(LOG_ERR, "n_random_bytes(NULL,0) should succeed");
}
if (n_random_bytes(NULL, 8) == 0) {
n_log(LOG_ERR, "n_random_bytes(NULL,8) should fail");
}
}
static void test_token(void) {
if (!t1 || !t2) {
n_log(LOG_ERR, "n_random_hex_token returned NULL");
} else {
if (t1->written != 32 || t2->written != 32) {
n_log(LOG_ERR, "token length: got %zu/%zu expected 32", t1->written, t2->written);
}
for (size_t i = 0; i < t1->written; i++) {
if (!isxdigit((unsigned char)t1->data[i]) || (t1->data[i] >= 'A' && t1->data[i] <= 'F')) {
n_log(LOG_ERR, "token has a non-lowercase-hex character at %zu", i);
break;
}
}
if (strcmp(t1->data, t2->data) == 0) {
n_log(LOG_ERR, "two tokens were identical");
}
}
free_nstr(&t1);
free_nstr(&t2);
/* a zero-byte token is the empty string */
if (!t0 || t0->written != 0) {
n_log(LOG_ERR, "zero-byte token should be empty");
}
free_nstr(&t0);
}
int main(int argc, char** argv) {
process_args(argc, argv);
if (failures) {
n_log(LOG_ERR, "ex_random: %d failure(s)", failures);
return 1;
}
n_log(LOG_NOTICE, "ex_random: all checks passed");
return 0;
}
static int failures
int main(void)
void process_args(int argc, char **argv)
Definition ex_common.c:48
static void test_bytes(void)
Definition ex_random.c:70
static void test_token(void)
Definition ex_random.c:110
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
int n_random_bytes(unsigned char *buf, size_t n)
fill buf with n cryptographically secure random bytes (getrandom(2) when available,...
Definition n_random.c:62
N_STR * n_random_hex_token(size_t nbytes)
generate nbytes secure random bytes and return them as a lowercase hex N_STR (2*nbytes characters); f...
Definition n_random.c:91
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
Definition n_str.h:68
char * data
the string
Definition n_str.h:63
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:203
A box including a string and his lenght.
Definition n_str.h:61
Common headers and low-level functions & define.
Generic log system.
Cryptographically secure random bytes and hex tokens (POSIX)
N_STR and string function declaration.