cariboulabs-cariboulite/software/utils/libcariboulite/src/datatypes/entropy.c

58 wiersze
1.2 KiB
C

#ifndef ZF_LOG_LEVEL
#define ZF_LOG_LEVEL ZF_LOG_VERBOSE
#endif
#define ZF_LOG_DEF_SRCLOC ZF_LOG_SRCLOC_LONG
#define ZF_LOG_TAG "ENTROPY"
#include "zf_log/zf_log.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <linux/random.h>
#include <sys/ioctl.h>
#include "entropy.h"
typedef struct {
int bit_count; /* number of bits of entropy in data */
int byte_count; /* number of bytes of data in array */
unsigned char buf[1];
} entropy_t;
//=====================================================
int add_entropy(uint8_t byte)
{
int rand_fid = open("/dev/urandom", O_RDWR);
if (rand_fid != 0)
{
// error opening device
ZF_LOGE("Opening /dev/urandom device file failed");
return -1;
}
entropy_t ent =
{
.bit_count = 8,
.byte_count = 1,
.buf = {byte},
};
if (ioctl(rand_fid, RNDADDENTROPY, &ent) != 0)
{
ZF_LOGE("IOCTL to /dev/urandom device file failed");
}
if (close(rand_fid) !=0 )
{
ZF_LOGE("Closing /dev/urandom device file failed");
return -1;
}
return 0;
}