Correction.

Sylvain <[email protected]> Thu, 22 Aug 2024 09:54:34 -0700
Newsgroups gmane.os.netbsd.devel.security
Message-ID <[email protected]>
--Apple-Mail=_E569F5A9-FD5C-40F0-9652-6F4FB74337FF
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

Sorry, last version was still sending output to stdout. This one is =
writing directly to /dev/urandom


/*
Public Domain, please append your contributions, respectfully.
Version 1.0 - Sylvain Saucier ([email protected]) - Original author
Slight variation to make this program reseed urandom from userspace in =
NetBSD.
The use case for this would be during an installation on a machine =
without=20
proper hardware entropy sources.
*/

#include <stdint.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

typedef struct {
volatile uint64_t * s_output;
volatile uint64_t * s_input;
pthread_t thread;
} _C4_node;

typedef struct {
volatile uint64_t shared_io[3];
_C4_node t[3];
long num_threads;
} _C4_state;

int C4_hash_algo(volatile uint64_t * in, volatile uint64_t * out) {
uint64_t acc =3D 1;
uint64_t primes[128] =3D {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,=20
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107,
109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179,=20
181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251,=20
257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331,=20
337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409,=20
419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487,=20
491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577,=20
587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647};
for (int bit =3D 0; bit < 64; bit++) {
* in =3D ( * in << 13) | ( * in >> (64 - 13));
acc =3D (acc << bit) | (acc >> (64 - bit));
acc *=3D primes[(2 * bit) + (1 & * in)];* out +=3D acc ^ * in;
}* out ^=3D acc;
return 0;
}

void * C4_main(void * raw) {
_C4_node * self__C4_state =3D (_C4_node * ) raw;
while (1) {
C4_hash_algo(self__C4_state -> s_output, self__C4_state -> s_input);
}
return NULL;
}

void init(_C4_state * s, long _numt) {
for (int thr =3D 0; thr < _numt; thr++) {
s -> num_threads =3D _numt;
s -> shared_io[thr] =3D 0;
s -> t[thr].s_output =3D & (s -> shared_io[thr]);
s -> t[thr].s_input =3D & (s -> shared_io[(thr + 1) % _numt]);
pthread_create( & (s -> t[thr].thread), NULL, & C4_main, & (s -> =
t[thr]));
}
}

int main(int argc, const char * argv[]) {
uint64_t out;
_C4_state state;
long nproc =3D sysconf(_SC_NPROCESSORS_ONLN);
size_t buf_len =3D 1024;
char* userinput[buf_len];

if ( nproc < 2 ) {
fprintf(stderr, "Insufficient number of cores to sustain synthetic chaos =
effect.\n");
exit(EXIT_FAILURE);
};

init( & state, nproc > 2 ? 2 : nproc); //It is better to drive two =
theads simultaneously than three threads on two cores.

FILE* urandom =3D fopen("/dev/urandom", "a");
if ( urandom =3D=3D NULL ) {
fprintf(stderr, "Cannot open /dev/urandom.\n");
exit(EXIT_FAILURE);
}

for ( int x =3D 0 ; x < 4 ; x++ ) { //Obtain 256 bits of entropy
usleep(1000); //I observed decorrelation using 5 microseconds, 1 =
milliseconds should be good enough to provide last resort entropy.
out =3D 0; // do not reuse old entropy.
for (int thr =3D 0; thr < state.num_threads; thr++) {
out ^=3D state.shared_io[thr];
}
if ( fwrite( & out, sizeof(uint64_t), 1, urandom) !=3D 1 ) {
fprintf(stderr, "/dev/urandom write error.\n");
exit(EXIT_FAILURE);
}
}
fclose(urandom);
exit(EXIT_SUCCESS);
}


--Apple-Mail=_E569F5A9-FD5C-40F0-9652-6F4FB74337FF
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename=signature.asc
Content-Type: application/pgp-signature;
	name=signature.asc
Content-Description: Message signed with OpenPGP

-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEE1LybVkw8Jv0hSe4qkphLusuR2oQFAmbHbUoACgkQkphLusuR
2oRufxAAyyOAAE54u4VJm/395vP/mPTFyGXGiP6CCHFCR+eI2SjXAcUy03NIx+7t
0LE/Kg5ZSUuSdTETkyFj4XThzYJrG9LdCStqw7RCGQVGPMFLPJIqIKZcGpF04et2
8w7vyImKDz4IXmpl0fbc5214kBx2jB74snG2Q4gY+KOV4MZW0dH8C8D+pSTPvbm6
J1PKjHffSf+l3PJNNIeL+UvyHovQsM6MAJKOx1XodYk0gLJtiMsiyEXyF7HOkx34
bdf56J6r/6DYHHAHXs+mZZItWc9YmBvozMvIXpri6wcCxLg2wF+gMeyxELkaF30/
KFGWY0/X6Bp5NXbgr8De/Z1sk8I+b8qEkuoB2vDrjtSVSnWKSuH9sK99YeSAcvm7
c/qQtMSWqp6euCOIFxpyfwU2NEOU9dhdSZptwf0KuuCF8ABpDsMr5qGDJ+pP1HIH
Qv1NxqGEL71Z8CNe32KukFfhNZfMleVcxw24KnwZVRb3itvI3ucNVYwVS9TjDj4H
3fis1j8CNOgZLI3GPoECPdBlBoM90OOZnd3IlC84IlV7SRlACjKhCBFk060OyvNR
aOUCZlvGRrgPkLap449UmCydkRBim9v/U61hc8E/Xw/pskhYUcW+vvYx89Iudg9R
aZK64mvem2Onnc0iDh3RXOIyC/zVv3AXa/gsIHtRpwcxmCgIPmk=
=czgm
-----END PGP SIGNATURE-----

--Apple-Mail=_E569F5A9-FD5C-40F0-9652-6F4FB74337FF--