Aluksi pari apuohjelmaa, joilla luodaan yhteys ja tulostetaan merkkijonoja:
Edit: Kun käytät satunnaisuutta jostain lähteestä, älä käytä sitä suoraan vaan lisää se muihin lähteisiin (esim hash funktiolla) , ja muodosta tuloksesta satunnaisbittejä. Käytä näitä vain sivuihin, joiden käyttöön sinulla on lupa ja harvoin, sillä vaarana on palvelunesto. Muista noudattaa sääntöjä ja lakeja.
Nyt myös https satunnaisbittisivu (https://moijari.com:5005)on verkossa.
End of edit.
#ifdef FORT_USE_MOIJARICOM
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
int fort_connect(unsigned char *host, unsigned char *port)
{
struct addrinfo hints, *res, *resp;
int s, status;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if((status = getaddrinfo(host, port, &hints, &res)) != 0) {
fprintf(stderr,"\n%s: getaddrinfo", procname);
fprintf(stderr,", status %d", status);
fprintf(stderr,", gai_strerror(): %s", gai_strerror(status));
fprintf(stderr,", errno %d\n", errno);
fflush(stderr);
}
for(resp=res; resp!=NULL; resp = resp->ai_next) {
if((s = socket(resp->ai_family, resp->ai_socktype, resp->ai_protocol))<0)
continue;
if(connect(s, resp->ai_addr, resp->ai_addrlen) == 0)
break;
close(s);
}
freeaddrinfo(res);
return(s);
}
#include <stdarg.h>
void dbs_printf(unsigned char **buf, int *buf_length, const char *format, ...)
{
int count;
va_list args;
va_start(args, format);
count = snprintf(*buf, *buf_length, format, args) + 1;
va_end(args);
if(*buf_length < count) {
*buf_length = count;
*buf = realloc(*buf, *buf_length);
va_start(args, format);
count = snprintf(*buf, *buf_length, format, args) + 1;
va_end(args);
}
}
Seuraavana satunnaisuuden lukemisrutiini http-sivuille:
void fort_hash_http_page(unsigned char *host,unsigned char *port, unsigned char *page, unsigned char *hash)
{
int s,status,bytes,total;
if((s = fort_connect(host, port))<0) {
fprintf(stderr,"\n%s: cannot fort_connect()", procname);
fprintf(stderr,", status: %d", status);
fprintf(stderr,", errno: %d" , errno);
perror("fort_connect");
fflush(stderr);
}
unsigned char *format =
"GET %s HTTP/1.0\r\n"
"Host: %s\r\n";
static unsigned char *msg = NULL;
static int msg_length=0;
dbs_printf(&msg, &msg_length, format, page, host);
if((status=write(s, msg, strlen(msg)))<0) {
fprintf(stderr, "\n%s: write(), error: %d\n", procname, errno);
perror("write");
fflush(stderr);
}
HashCtx ctx;
char buffer[1024];
HashInit(&ctx);
total=0;
while((bytes = read(s, buffer, sizeof(buffer)))>0) {
//write(1,buffer,bytes);
HashUpdate(&ctx, buffer, bytes);
total+=bytes;
}
HashFinal(hash, &ctx);
fprintf(stdout,"fort_hash_http_page: %d bytes read", total);
fprintf(stdout,", sha256: ");
for(int c = 0;c < HashLen; c++) {
fprintf(stdout,"%02x", hash[c]);
}
fprintf(stdout,"\n");
fflush(stdout);
close(s);
}
Ja seuraavana satunnaisuuden lukurutiini https-sivuille:
void fort_hash_https_page(unsigned char *host,unsigned char *port, unsigned char *page, unsigned char *hash)
{
int s, status, bytes, total;
struct addrinfo hints, *res, *resp;
SSL_METHOD *method=NULL;
SSL_CTX *ctx=NULL;
SSL *ssl;
SSL_library_init(); //see: http://h30266.www3.hpe.com/odl/axpos/opsys/vmsos84/BA554_90007/ch04s03.html
OpenSSL_add_ssl_algorithms();
SSL_load_error_strings();
if((method = (SSL_METHOD *)
SSLv23_client_method()) == NULL) {
fprintf(stderr,"\n%s: cannot SSLv3_server_method()", procname);
fflush(stderr);
}
if((ctx=SSL_CTX_new(method)) == NULL) {
fprintf(stderr,"\n%s: cannot SSL_CTX_new()", procname);
fflush(stderr);
}
if((ssl=SSL_new(ctx)) == NULL) {
fprintf(stderr,"\n%s: cannot SSL_new()", procname);
fflush(stderr);
}
if((s = fort_connect(host, port))<0) {
fprintf(stderr,"\n%s: cannot fort_connect()", procname);
fprintf(stderr,", status: %d", status);
fprintf(stderr,", errno: %d" , errno);
perror("fort_connect");
fflush(stderr);
}
SSL_set_fd(ssl,s);
if((status=SSL_connect(ssl))<=0) {
fprintf(stderr,"\n%s: cannot SSL_connect()", procname);
fprintf(stderr,", status: %d", status);
fprintf(stderr,", errno: %d" , errno);
fprintf(stderr,", SSL_get_error(): %d\n", SSL_get_error(ssl,status));
perror("SSL_connect");
fflush(stderr);
}
unsigned char *format =
"GET %s HTTP/1.0\r\n"
"Host: %s\r\n";
static unsigned char *msg = NULL;
static int msg_length=0;
dbs_printf(&msg,&msg_length, format, page, host);
if((status=SSL_write(ssl, msg, strlen(msg)))<0) {
fprintf(stderr,"\n%s: SSL_write()", procname);
fprintf(stderr,", status: %d", status);
fprintf(stderr,", errno: %d", errno);
fprintf(stderr,", SSL_get_error(): %d", SSL_get_error(ssl,status));
perror("SSL_write");
fflush(stderr);
}
fflush(stdout);
HashCtx hashctx;
char buffer[1024];
HashInit(&hashctx);
total=0;
while((bytes=SSL_read(ssl, buffer, sizeof(buffer)))>0) {
//write(1,buffer,bytes);
HashUpdate(&hashctx, buffer, bytes);
total+=bytes;
}
fflush(stdout);
if(bytes<0) {
fprintf(stderr, "\n%s: SSL_read()", procname);
fprintf(stderr, ", status: %d", status);
fprintf(stderr, ", errno: %d", errno);
fprintf(stderr,", SSL_get_error(): %d", SSL_get_error(ssl,status));
perror("SSL_read");
fflush(stderr);
}
fflush(stdout);
HashFinal(hash, &hashctx);
fprintf(stdout,"dbs_hash_https_page: %d bytes read", total);
fprintf(stdout,", sha256: ");
for(int c = 0;c < HashLen; c++) {
fprintf(stdout,"%02x", hash[c]);
}
fprintf(stdout,"\n");
fflush(stdout);
SSL_shutdown(ssl);
SSL_free(ssl);
SSL_CTX_free(ctx);
close(s);
}
Ja kutsut, jotka käyttävät edellisiä:
#ifdef FORT_USE_MOIJARICOM
unsigned char hash[HashLen];
fort_hash_http_page("moijari.com", "5001", "/", hash);
. . .
fort_hash_https_page("moijari.com", "5005", "/", hash);
. . .
#endif
Vielä lyhennetyt listaukset http ja https sivuista (write(1,…) lauseen kommentti poistettu): Ensin http:
HTTP/1.0 200 OK
Location:
Server: ressuhttps5 v0.96
Date: Wed, 09 Dec 2020 16:09:33 GMT
Content‑Length: 107665
<!DOCTYPE html>
<html lang="fi">
<head>
<meta charset="UTF-8">
<title>Ressu random numbers</title>
<meta name="author" content="Jari Kuivaniemi">
<meta name="format-detection" content="telephone=no">
</head>
<body>
<h1>Ressu random numbers</h1>
<code>
xtDlKzcj6CpzUA9Q T‑ZKlo_TnLyitx5U 6JXokFUptT‑qeDIl JTRIgXXrIgNtT6vS nJ29kx3GlKPow_BC kvxrsSeNJFgzfSPL OWj&
#8209;x24mTYCI6Br‑ C‑wpuj_vJdBxC6l‑ oF1QaeChPYc2t_Ds kbxIkk4dbsvk6n87
_YAauolsZg8rW11W lbkAu0zW4CbZyTjl yXq8vMOAhl8GM69u nbrz9oUsCWKbdDtp Hn7OONT9meWo1poo dst4Oi93e70Sn6At gL8hCIhiHrNRlCJb
h1k84jd4MeRq00H2 ihs4m6BpICqk218P oXluzC4dgOrZyjl4
. . .
#8209;T PrsUHm2leFv5ag28 UkS01AOkrf4‑bwwU rEnXmZiBVl‑HcLg2 WerbBoY54Sf0W9Ph
RNtYbIedrKpIlX07 lI7BurKAwAgTr5PP k6zK7fZ_3_lbj0k_ lVLvhAPzth0m1FgO qypvJfZPmmxEK5o0 678zIRU08AeiSjYP Y3F05r3_mJWwPIXu
1YjxyNil1zNxX‑Ny 9‑oYfkpaRiUXYLNt ZGq3ID3IMSEtNyOb
sTSYILfkyFSHaepZ
<code>
<h1>Statistics</h1>
monobit: ones: 262412(50.054172%) zeroes: 261844(49.945828%), total: 524256<br>
bitwise monobit data: 1(0: 32723, 1: 32809) 2(0: 32798, 1: 32734) 4(0: 32780, 1: 32752) 8(0: 32772, 1: 32760) 16(0: 32859, 1:
32673) 32(0: 32941, 1: 32591) 64(0: 32831, 1: 32701) 128(0: 32708, 1: 32824)<br>
bitwise monobit total: 524256, lowest: 32591(6.216619%), highest: 32941(6.283381%)<br>
poker2: data: 0:65502 1:65717 2:65691 3:65218<br>
poker2: total: 262128, lowest: 65218(24.880211%), highest: 65717(25.070576%)<br>
poker4: data: 0:8298 1:8203 2:8119 3:8138 4:8161 5:8176 6:8299 7:8086 8:8052 9:8350 10:8267 11:8184 12:8233 13:8266 14:8153 1
5:8079<br>
poker4: total: 131064, lowest: 8052(6.143563%), highest: 8350(6.370933%)<br>
poker8: data: 0:247 1:263 2:249 3:266 4:255 5:228 6:251 7:239 8:278 9:270 10:272 11:272 12:293 13:254 14:259 15:271 16:260 17
:255
. . .
original url: <a href="https://moijari.com:5001">https://moijari.com:5001</a>
or <a href="https://moijari.com:5001">https://moijari.com:5001</a><br>
<br><br>
</body>
</html>
Ja https sivu:
HTTP/1.0 200 OK
Location:
Server: Ressu3 version 0.10 ©
<!doctype html>
<html lang="fi"><head><meta charset="utf-8"><title>Ressu random numbers</title><meta name="author" content="Jari Kuivaniemi"><meta name="copyr
ight" content="Jari Kuivaniemi"></head><body> <h1>Ressu random numbers</h1>
<code>
KPxIRgaloF0aT9qb m50LK37i91o0IeR6 zYs0MIsp0MNYJzfn 6zp4TEYwyVAAVnL2 Z2Rb4kBy318bcE1G cpXFEiYWuT9xDlhI DxinrPJQNstAn_gr
NgQis3splTVk11Xy kk5UQkU9qfQVR8cw 3C0JCr_k_Gw6XbGS
0szaE96f9oRavDIE cmwNE−lkEq0bi4HN mcSs−NRoAfqvsOMN nhail_10v4vLHUvx udpcVkqYnZArZW6K SeN3NnfBTr3ih1FW fzB7
AzeopJEqkY0c JBr0XL5Mjem24X3o qkQTKuCJAFfqNmDQ AaNU0XiRd0zDm7w6
. . .
iCi5IqV9GGwuPcfg P0TOAPjDpWTjFfml Q2p6a2Er11zoJsXG JbNG4−Rd_GsqwPTw BKDCD1FMzwBZ2Tzf J3WU4q2aX8ndCzBF xePgfF9QvP
whOO4b qoRWJ4zYNzJrSh7h XyIVheIhIFuL69l− s2WkOHhQTl_pCocc
7nTleVrEgdPy99jf nhqsW3aHnv5FT7gX 2XUXSIW6ICUV15yY BfnEgZrVViuGHVj0 −−yJaXi3z9lKdIgu 4GGsribNp8IxXrWc bgjn
8VDO_BZuQbcY hV6PQezdZEwvPWfb 8zxaacl4liNKX−vK rAz7_dI3−EVT4KiF
vWK8jhkPuyEkjc8Y </code>
<h1>Statistics</h1>
monobit: ones: 261605(49.900240%) zeroes: 262651(50.099760%), total: 524256<br>
bitwise monobit data: 1(0: 32707, 1: 32825) 2(0: 32487, 1: 33045) 4(0: 32584, 1: 32948) 8(0: 32557, 1: 32975) 16(0: 32705, 1:
32827) 32(0: 32950, 1: 32582) 64(0: 32604, 1: 32928) 128(0: 33011, 1: 32521)<br>
bitwise monobit total: 524256, lowest: 32487(6.196782%), highest: 33045(6.303218%)<br>
poker2: data: 0:65195 1:65810 2:65405 3:65718<br>
poker2: total: 262128, lowest: 65195(24.871437%), highest: 65810(25.106055%)<br>
poker4: data: 0:8121 1:8158 2:8116 3:8173 4:8250 5:8196 6:8231 7:8323 8:8043 9:8209 10:8311 11:8057 12:8213 13:8247 14:8127 1
5:8289<br>
. . .
runs: total: 524256<br>
runs8: any: 1:65078 2:227<br>
runs8: total: 65532<br>
average: total 8332443, count: 65532, average: 127.150749<br>
entropy: 7.997152<br>
<br><br>Ressu3 version 0.10 ©, sha256(97a32f735c9cc5e738e99a8ffe4cd7a195b88a3525e58d90fd53e7e8eab90e27)<br><br> original url:
<a href="https://moijari.com:5005">https://moijari.com:5005</a>
or <a href="https://moijari.com:5005">https://moijari.com:5005</a><br>
</body></html>
Tässä vielä esimerkki useamman lähteen käytöstä: palaset fort.c (https://moijari.com/?p=964) ohjelmasta: uusi satunnaisuus lisätään fort_reseed() funktiolla, joka tekee “laskutoimituksen” fort_key = hash(fort_key + cvar + uusi), eli lisää uusia satunnaisbittejä fort_key muuttujaan. Fort_key muuttujassa säilytetään “avain”, jonka avulla luodaan uudet käyttäjän tilaamat satunnaisbitit luodaan.
Fort_init täyttää ensin fort_key muuttujan ressun luomilla satunnaisbiteillä. Sen jälkeen fort_key:iin lisätään bittejä /dev/random ja/tai /dev/urandom laitteelta. Tämän jälkeen lisään satunnaisuuteen vielä bittejä vielä moijari.com:n 5001 ja 5005 porteista. Lopuksi ajetaan vielä fortin satunnaisbittejä luova osuus, jolta saadaan lisäksi sisäisesti luotuja satunnaisbittejä.
void fort_reseed(int len, unsigned char *buf)
{
HashCtx hash;
FORT_INTERNAL_EVENTS_START(22)
hash_init(&hash);
hash_update(&hash, fort_key, sizeof(fort_key));
hash_update(&hash, (unsigned char *)&cvar,
sizeof(cvar));
hash_update(&hash, buf, len);
hash_final(fort_key, &hash);
inccvar();
. . .
}
void fort_init()
{
. . .
#ifdef RESSU
ressu_genbuffer(sizeof(fort_key), fort_key);
dump_pools("Generate fort key w ressu");
#endif
. . .
unsigned char temp[64];
#ifdef FORT_USE_URANDOM
memset(temp, 0, sizeof(temp));
fort_readfile_xor(sizeof(temp), temp,
"/dev/urandom");
fort_reseed(sizeof(temp), temp);
dump_pools("Randomness from urandom");
#endif
#ifdef FORT_USE_RANDOM
memset(temp, 0, sizeof(temp));
fort_readfile_xor(sizeof(temp), temp,
"/dev/random");
fort_reseed(sizeof(temp), temp);
dump_pools("Randomness from random");
#endif
unsigned char hash[HashLen];
#ifdef FORT_USE_MOIJARICOM5001
fort_hash_http_page("moijari.com", "5001", "/", hash);
fort_reseed(sizeof(hash), hash);
dump_pools("Rand. from moijari.com:5001");
#endif
#ifdef FORT_USE_MOIJARICOM5005
fort_hash_https_page("moijari.com", "5005", "/", hash);
fort_reseed(sizeof(hash), hash);
dump_pools("Rand. from moijari.com:5005");
#endif
. . .
#ifdef FORT_INTERNAL_EVENTS
if(fort_internal_events) {
// Create some internal events
for(c=0; c<64; c++) {
FORT_INTERNAL_EVENTS_START(34)
fort_random_data(sizeof(temp), temp);
FORT_INTERNAL_EVENTS_END(35)
}
}
dump_pools("Internal events");
#endif
fort_reseed_count = 0;
fort_next_reseed = 0;
// Reseed fort_key with new events
fort_random_data(sizeof(temp), temp);
fort_reseed(sizeof(temp), temp);