Debian Codegraph @

lib/jitterentropy-status.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2026, Stephan Mueller <[email protected]>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20 #include <stdio.h>
21
22 #include "jitterentropy.h"
23 #include "jitterentropy-base.h"
24 #include "jitterentropy-internal.h"
25
26 /*
27 * Always validate the output with something like "jq -e .", when doing changes here.
28 *
29 * No JSON library is used here to keep dependencies slim and this is serialized only
30 * here.
31 */
32 int jent_status(const struct rand_data *ec, char *buf, size_t buflen)
33 {
34 size_t used;
35
36 if (!buf || buflen == 0)
37 return -1;
38
39 #define jent_add_to_status(...) \
40 { \
41 used = strlen(buf); \
42 if (used < buflen) \
43 snprintf(buf + used, buflen - used, __VA_ARGS__);\
44 }
45
46 /* needed as plain snprintf to make jent_add_to_status len calculation usable */
47 snprintf(buf, buflen, "{\n");
48
49 jent_add_to_status("\t\"version\": \"%u.%u.%u\",\n",
50 JENT_MAJVERSION, JENT_MINVERSION, JENT_PATCHLEVEL)
51
52 if (!ec)
53 goto out;
54
55 /*
56 * health
57 */
58 jent_add_to_status("\t\"healthFailure\": {\n");
59
60 jent_add_to_status("\t\t\"apt\": {\n");
61 jent_add_to_status("\t\t\t\"intermittent\": %s,\n",
62 ec->health_failure & JENT_APT_FAILURE ? "true" : "false");
63 jent_add_to_status("\t\t\t\"permanent\": %s\n",
64 ec->health_failure & JENT_APT_FAILURE_PERMANENT ? "true" : "false");
65 jent_add_to_status("\t\t},\n");
66
67 jent_add_to_status("\t\t\"rct\": {\n");
68 jent_add_to_status("\t\t\t\"intermittent\": %s,\n",
69 ec->health_failure & JENT_RCT_FAILURE ? "true" : "false");
70 jent_add_to_status("\t\t\t\"permanent\": %s\n",
71 ec->health_failure & JENT_RCT_FAILURE_PERMANENT ? "true" : "false");
72 jent_add_to_status("\t\t},\n");
73
74 jent_add_to_status("\t\t\"rctMemory\": {\n");
75 jent_add_to_status("\t\t\t\"intermittent\": %s,\n",
76 ec->health_failure & JENT_RCT_MEM_FAILURE ? "true" : "false");
77 jent_add_to_status("\t\t\t\"permanent\": %s\n",
78 ec->health_failure & JENT_RCT_MEM_FAILURE_PERMANENT ? "true" : "false");
79 jent_add_to_status("\t\t}");
80
81 #ifdef JENT_HEALTH_LAG_PREDICTOR
82 jent_add_to_status(",\n");
83
84 jent_add_to_status("\t\t\"lag\": {\n");
85 jent_add_to_status("\t\t\t\"intermittent\": %s,\n",
86 ec->health_failure & JENT_LAG_FAILURE ? "true" : "false");
87 jent_add_to_status("\t\t\t\"permanent\": %s\n",
88 ec->health_failure & JENT_LAG_FAILURE_PERMANENT ? "true" : "false");
89 jent_add_to_status("\t\t}\n");
90 #else
91 jent_add_to_status("\n");
92 #endif
93
94 jent_add_to_status("\t},\n");
95
96 /*
97 * runtime environment
98 */
99 jent_add_to_status( "\t\"runtimeEnvironment\": {\n");
100
101 jent_add_to_status("\t\t\"cpuCores\": %ld,\n", jent_ncpu());
102
103 jent_add_to_status("\t\t\"cpuCache\": {\n");
104 jent_add_to_status("\t\t\t\"l1Bytes\": %u,\n", jent_cache_size_roundup(0));
105 jent_add_to_status("\t\t\t\"allBytes\": %u\n", jent_cache_size_roundup(1));
106 jent_add_to_status("\t\t}\n");
107 jent_add_to_status("\t},\n");
108
109 /*
110 * configuration
111 */
112 jent_add_to_status( "\t\"configuration\": {\n");
113
114 jent_add_to_status( "\t\t\"osr\": %u,\n", ec->osr);
115 jent_add_to_status( "\t\t\"memoryBlockSizeBytes\": %u,\n", jent_memsize(ec->flags));
116
117 jent_add_to_status("\t\t\"hashLoopCount\": {\n");
118 jent_add_to_status("\t\t\t\"runtime\": %u,\n", jent_hashloop_cnt(ec->flags));
119 jent_add_to_status("\t\t\t\"initialization\": %u\n", jent_hashloop_cnt(ec->flags) * JENT_HASH_LOOP_INIT);
120 jent_add_to_status("\t\t},\n");
121
122 jent_add_to_status("\t\t\"memoryLoopCount\": {\n");
123 jent_add_to_status("\t\t\t\"runtime\": %u,\n", ec->memaccessloops);
124 jent_add_to_status("\t\t\t\"initialization\": %u\n", ec->memaccessloops * JENT_MEM_ACC_LOOP_INIT);
125 jent_add_to_status("\t\t},\n");
126
127 jent_add_to_status("\t\t\"secureMemory\": %s,\n", jent_secure_memory_supported() ? "true" : "false");
128 jent_add_to_status("\t\t\"internalTimer\": %s,\n", ec->enable_notime ? "true" : "false");
129 jent_add_to_status("\t\t\"fipsMode\": %s,\n", ec->fips_enabled ? "true" : "false");
130 jent_add_to_status("\t\t\"ntg1Mode\": %s,\n", !!(ec->flags & JENT_NTG1) ? "true" : "false");
131
132 jent_add_to_status("\t\t\"flags\": {\n");
133 jent_add_to_status("\t\t\t\"JENT_DISABLE_MEMORY_ACCESS\": %s,\n",
134 !!(ec->flags & JENT_DISABLE_MEMORY_ACCESS) ? "true" : "false");
135 jent_add_to_status("\t\t\t\"JENT_FORCE_INTERNAL_TIMER\": %s,\n",
136 !!(ec->flags & JENT_FORCE_INTERNAL_TIMER) ? "true" : "false");
137 jent_add_to_status("\t\t\t\"JENT_DISABLE_INTERNAL_TIMER\": %s,\n",
138 !!(ec->flags & JENT_DISABLE_INTERNAL_TIMER) ? "true" : "false");
139 jent_add_to_status("\t\t\t\"JENT_FORCE_FIPS\": %s,\n",
140 !!(ec->flags & JENT_FORCE_FIPS) ? "true" : "false");
141 jent_add_to_status("\t\t\t\"JENT_NTG1\": %s,\n",
142 !!(ec->flags & JENT_NTG1) ? "true" : "false");
143 jent_add_to_status("\t\t\t\"JENT_CACHE_ALL\": %s\n",
144 !!(ec->flags & JENT_CACHE_ALL) ? "true" : "false");
145 jent_add_to_status("\t\t}\n");
146 jent_add_to_status("\t}\n");
147
148 out:
149 jent_add_to_status("}\n");
150
151 used = strlen(buf);
152 return (used >= buflen - 1) ? -1 : 0;
153 #undef jent_add_to_status
154 }