rsa_arduino/prng.cpp_bck
2024-03-05 14:15:03 +01:00

99 lines
2.3 KiB
Plaintext

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "prng.h"
#include <Arduino.h>
#define MAX_ENTRIES_ENTROPY 100
void init_entropy(){
struct entropy_pool s_pool;
randomSeed(analogRead(0));
while (s_entropy.pool_size < BUFSIZE){
s_entropy.buf[s_entropy.pool_size++] = random(300);
}
}
// Define a range ???
unsigned long prng(int seed){
//int32_t p = 4242;
unsigned long p = seed;
//p = 4242;
//p ^= p << 3; // XOR
// 1000010010010 -> 4242
// 1000010010010000 -> 33936
// 1001010000000010 -> 37890
//printf("%d\n", p);
// For small value, increase the value ?
p ^= (p << 1) | (p << 2);
// Generate number between 1000 and 5000 ???
// Or to add X bits to have the key number, for instance 32 bits
return p;
}
int prng2 () {
double *pool_entropy = entropy_pool();
int pos = 0;
entropy_cpu_clock(pool_entropy, 0, MAX_ENTRIES_ENTROPY);
pos++;
for(int i = 0; i < MAX_ENTRIES_ENTROPY; i++){
//printf("%f\n", pool_entropy[i]);
}
// Get from pool
// Generate an integer from 0 to 100 (with clock CPU) for instance
// And to pick up to the pool
// Generate entropy with the network ?
free(pool_entropy);
return(0);
}
double *entropy_pool(){
double *pool_entropy = (double *)malloc(sizeof(double) * MAX_ENTRIES_ENTROPY);
if (pool_entropy == NULL){
//printf("Failed to allocate variables\n");
exit(-1);
}
return pool_entropy;
}
void entropy_cpu_clock(double *entropy, int pos, int max){
clock_t start_t, end_t;
double total_t;
double res = 0;
start_t = clock();
//printf("Starting of the program, start_t = %ld\n", start_t);
//printf("Going to scan a big loop, start_t = %ld\n", start_t);
for(int i = 0; i < 10000000; i++) {
}
end_t = clock();
//printf("End of the big loop, end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
//printf("Total time taken by CPU: %f\n", total_t );
//printf("Exiting of the program...\n");
entropy[pos] = total_t;
// Test - generate an iteger from 0 to 100
// Multiply the 10 first entries
for (int i = 0; i < 10; i++){
res += entropy[0];
}
//printf("Pos: %f\n", ceil(res * 100));
}