Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_random.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
27#include "nilorea/n_random.h"
28
29#include "nilorea/n_common.h"
30#include "nilorea/n_hex.h"
31#include "nilorea/n_log.h"
32#include "nilorea/n_str.h"
33
34#include <errno.h>
35#include <stdio.h>
36#include <string.h>
37#include <unistd.h>
38
39/* getrandom(2) exists on glibc >= 2.25; guard its header behind that so the
40 * /dev/urandom path remains the portable fallback (musl, older glibc). */
41#if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))
42#define N_RANDOM_HAVE_GETRANDOM 1
43#include <sys/random.h>
44#endif
45
46/* Read n bytes from /dev/urandom into buf. Returns 0 on success, -1 on error. */
47static int n_random_from_urandom(unsigned char* buf, size_t n) {
48 FILE* f = fopen("/dev/urandom", "rb");
49 if (!f) {
50 n_log(LOG_ERR, "n_random_bytes: cannot open /dev/urandom: %s", strerror(errno));
51 return -1;
52 }
53 size_t got = fread(buf, 1, n, f);
54 fclose(f);
55 if (got != n) {
56 n_log(LOG_ERR, "n_random_bytes: short read from /dev/urandom (%zu of %zu)", got, n);
57 return -1;
58 }
59 return 0;
60}
61
62int n_random_bytes(unsigned char* buf, size_t n) {
63 if (!buf && n > 0) {
64 n_log(LOG_ERR, "n_random_bytes: NULL buffer with n %zu", n);
65 return -1;
66 }
67 if (n == 0) {
68 return 0;
69 }
70
71#ifdef N_RANDOM_HAVE_GETRANDOM
72 size_t got = 0;
73 while (got < n) {
74 ssize_t r = getrandom(buf + got, n - got, 0);
75 if (r < 0) {
76 if (errno == EINTR) continue;
77 break; /* fall through to the /dev/urandom fallback */
78 }
79 got += (size_t)r;
80 }
81 if (got == n) {
82 return 0;
83 }
84 /* getrandom did not deliver everything; finish from /dev/urandom */
85 return n_random_from_urandom(buf + got, n - got);
86#else
87 return n_random_from_urandom(buf, n);
88#endif
89}
90
91N_STR* n_random_hex_token(size_t nbytes) {
92 if (nbytes == 0) {
93 return n_hex_encode((const unsigned char*)"", 0);
94 }
95 unsigned char* buf = NULL;
96 Malloc(buf, unsigned char, nbytes);
97 __n_assert(buf, return NULL);
98 if (n_random_bytes(buf, nbytes) != 0) {
99 Free(buf);
100 return NULL;
101 }
102 N_STR* hex = n_hex_encode(buf, nbytes);
103 Free(buf);
104 return hex;
105}
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:204
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:279
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:263
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_ERR
error conditions
Definition n_log.h:76
N_STR * n_hex_encode(const unsigned char *data, size_t len)
encode len bytes of data as a lowercase hex N_STR (2*len characters); free with free_nstr.
Definition n_hex.c:48
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
A box including a string and his lenght.
Definition n_str.h:61
Common headers and low-level functions & define.
Hexadecimal encode/decode helpers.
Generic log system.
static int n_random_from_urandom(unsigned char *buf, size_t n)
Definition n_random.c:47
Cryptographically secure random bytes and hex tokens (POSIX)
N_STR and string function declaration.