111 lines
1.9 KiB
C
111 lines
1.9 KiB
C
/* vxRandLib.c - VxWorks rand lib callbacks for openssl */
|
|
|
|
/*
|
|
* Copyright (c) 2014, 2016, 2019, 2021, 2024 Wind River Systems, Inc.
|
|
*
|
|
* The right to copy, distribute, modify or otherwise make use
|
|
* of this software may be licensed only pursuant to the terms
|
|
* of an applicable Wind River license agreement.
|
|
*/
|
|
|
|
/*
|
|
DESCRIPTION
|
|
This library provides openssl rand lib callbacks, that in turn call the VxWorks
|
|
Random Number Library
|
|
*/
|
|
|
|
#include <vxWorks.h>
|
|
#include <stdLib.h>
|
|
#include <stdint.h>
|
|
#include <unistd.h>
|
|
#include <tickLib.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
/*
|
|
* Writes `len` random bytes into `buf`.
|
|
* Arguments:
|
|
* buf - pointer to a buffer provided by the caller
|
|
* len - number of random bytes to generate
|
|
*
|
|
* Returns:
|
|
* 0 on success
|
|
*/
|
|
int fill_random_bytes(uint8_t *buf, int len) {
|
|
|
|
int i;
|
|
|
|
srand((unsigned int)tickGet());
|
|
|
|
for (i = 0; i < len; i++) {
|
|
/* take lowest 8 bits */
|
|
buf[i] = (uint8_t)(rand() & 0xFF);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
static int vxRandLibSeed(const void *buf, int num)
|
|
{
|
|
|
|
fill_random_bytes ((uint8_t *)buf, num);
|
|
srand (buf);
|
|
|
|
return OK;
|
|
}
|
|
|
|
static int vxRandLibBytes(unsigned char *buf, int num)
|
|
{
|
|
fill_random_bytes ((uint8_t *)buf, num);
|
|
return OK;
|
|
}
|
|
|
|
static void vxRandLibCleanup(void)
|
|
{
|
|
|
|
}
|
|
|
|
static int vxRandLibAdd(const void *buf, int num, double entropy)
|
|
{
|
|
fill_random_bytes ((uint8_t *)buf, num);
|
|
srand (buf);
|
|
|
|
return OK;
|
|
}
|
|
|
|
static int vxRandLibPseudorand(unsigned char *buf, int num)
|
|
{
|
|
|
|
fill_random_bytes ((uint8_t *)buf, num);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static int vxRandLibStatus(void)
|
|
{
|
|
int i;
|
|
|
|
for (i=0; i< 2; i++)
|
|
sleep (1);
|
|
|
|
return OK;
|
|
}
|
|
|
|
static struct rand_meth_st vxRandLibMethSt =
|
|
{
|
|
vxRandLibSeed,
|
|
vxRandLibBytes,
|
|
vxRandLibCleanup,
|
|
vxRandLibAdd,
|
|
vxRandLibPseudorand,
|
|
vxRandLibStatus
|
|
};
|
|
|
|
|
|
struct rand_meth_st * vxRandLibMeth(void)
|
|
{
|
|
return &vxRandLibMethSt;
|
|
}
|