Skip to content

Commit 7e8da2e

Browse files
committed
Seeded RNG: Add Jitter RNG entropy source
Signed-off-by: Stephan Mueller <smueller@chronox.de>
1 parent 8f92449 commit 7e8da2e

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Changes 1.3.0-prerelease
1515

1616
* ML-DSA: add external-mu support; new API: lc_dilithium_ctx_external_mu
1717

18+
* Add optional Jitter RNG entropy source
19+
1820
Changes 1.2.0
1921
* Locking für seeded_rng added to avoid requiring the caller providing a lock
2022

README.fips.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ Leancrypto does not implement any entropy source. Yet, it implements support for
120120

121121
* `esdm`: This option uses the [ESDM](http://chronox.de/esdm/index.html) as entropy source.
122122

123+
* `jent`: This option uses the [Jitter RNG](http://chronox.de/jent/index.html) as entropy source.
124+
123125
NOTE: The default RNG used by leancrypto is the XDRBG. At the time of writing (beginning 2025), it is not yet FIPS-approved. However, SP800-90A is subject to revision at the time of writing and it is planned to add the XDRBG as an approved algorithm. Therefore, leancrypto selects XDRBG as default. If that shall be changed, the macros `LC_SEEDED_RNG_CTX_SIZE` and `LC_SEEDED_RNG_CTX` found in `drng/src/seeded_rng.c` must be set to either the Hash DRBG or HMAC DRBG at compile time.
124126

125127
## API and Usage Documentation

drng/src/meson.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ if (seeded_rng == 1)
8686
#Not required due to runtime linking
8787
#leancrypto_link += cc.find_library( 'esdm_rpc_client',
8888
# required: true)
89+
elif get_option('seedsource') == 'jent'
90+
src_fips += files([ 'seeded_rng_jent.c' ])
91+
leancrypto_link += cc.find_library( 'jitterentropy',
92+
required: true)
8993
elif (get_option('efi').enabled() or
9094
get_option('seedsource') == 'cpu')
9195
src_fips += files([ 'seeded_rng_cpu.c' ])

drng/src/seeded_rng_jent.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* ESDM Fast Entropy Source: Jitter RNG
3+
*
4+
* Copyright (C) 2022 - 2024, Stephan Mueller <smueller@chronox.de>
5+
*
6+
* License: see LICENSE file in root directory
7+
*
8+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
9+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
10+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
11+
* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
12+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
13+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
14+
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
15+
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
16+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
18+
* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
19+
* DAMAGE.
20+
*/
21+
22+
#include <jitterentropy.h>
23+
24+
#include "fips_mode.h"
25+
#include "seeded_rng.h"
26+
#include "seeded_rng_linux.h"
27+
#include "ret_checkers.h"
28+
#include "visibility.h"
29+
30+
static struct rand_data *esdm_jent_state = NULL;
31+
32+
void seeded_rng_noise_fini(void)
33+
{
34+
if (!esdm_jent_state)
35+
return;
36+
37+
jent_entropy_collector_free(esdm_jent_state);
38+
esdm_jent_state = NULL;
39+
}
40+
41+
int seeded_rng_noise_init(void)
42+
{
43+
unsigned int flags = 0;
44+
int ret;
45+
46+
/* Allow the init function to be called multiple times */
47+
seeded_rng_noise_fini();
48+
49+
flags |= fips140_mode_enabled() ? JENT_FORCE_FIPS : 0;
50+
51+
CKINT(jent_entropy_init_ex(0, flags));
52+
53+
esdm_jent_state = jent_entropy_collector_alloc(0, flags);
54+
CKNULL(esdm_jent_state, -EFAULT);
55+
56+
out:
57+
return ret;
58+
}
59+
60+
ssize_t get_full_entropy(uint8_t *buffer, size_t bufferlen)
61+
{
62+
ssize_t ret;
63+
64+
ret = jent_read_entropy_safe(&esdm_jent_state, (char *)buffer,
65+
bufferlen);
66+
67+
switch (ret) {
68+
case -1:
69+
ret = -EOPNOTSUPP;
70+
break;
71+
/* Temporary errors */
72+
case -2:
73+
case -3:
74+
case -5:
75+
ret = -ENODATA;
76+
break;
77+
/* Permanent errors */
78+
case -6:
79+
case -7:
80+
case -8:
81+
case -4:
82+
ret = -EFAULT;
83+
break;
84+
default:
85+
break;
86+
}
87+
88+
return ret;
89+
}

meson_options.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ option('seedsource', type: 'combo', value: 'builtin',
178178
choices: ['builtin',
179179
'cpu',
180180
'esdm',
181+
'jent',
181182
],
182183
description: '''Select the seed source for leancrypto
183184

0 commit comments

Comments
 (0)