2016-07-17 14:42:26 +02:00
|
|
|
/****************************************************************************
|
2018-08-27 15:14:01 +02:00
|
|
|
* drivers/crypto/dev_urandom.c
|
2016-07-17 14:42:26 +02:00
|
|
|
*
|
2021-03-20 19:54:56 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-07-17 14:42:26 +02:00
|
|
|
*
|
2021-03-20 19:54:56 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-07-17 14:42:26 +02:00
|
|
|
*
|
2021-03-20 19:54:56 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-07-17 14:42:26 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This random number generator is simple, fast and portable.
|
|
|
|
* Ref: https://en.wikipedia.org/wiki/Xorshift
|
|
|
|
*/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
2016-07-17 15:56:25 +02:00
|
|
|
#include <stdlib.h>
|
2016-07-17 14:42:26 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
2017-03-30 15:38:37 +02:00
|
|
|
#include <assert.h>
|
2016-07-17 15:56:25 +02:00
|
|
|
|
2016-07-21 22:05:44 +02:00
|
|
|
#include <nuttx/lib/lib.h>
|
2016-08-20 19:36:02 +02:00
|
|
|
#include <nuttx/lib/xorshift128.h>
|
2017-11-10 14:50:05 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2016-07-17 14:42:26 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2016-07-20 21:15:37 +02:00
|
|
|
#include <nuttx/drivers/drivers.h>
|
2017-03-30 15:38:37 +02:00
|
|
|
#include <nuttx/random.h>
|
2016-07-17 14:42:26 +02:00
|
|
|
|
2016-07-18 15:03:47 +02:00
|
|
|
#if defined(CONFIG_DEV_URANDOM) && !defined(CONFIG_DEV_URANDOM_ARCH)
|
|
|
|
|
2016-07-17 15:56:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DEV_URANDOM_CONGRUENTIAL) && \
|
2017-03-30 15:38:37 +02:00
|
|
|
!defined(CONFIG_DEV_URANDOM_XORSHIFT128) && \
|
|
|
|
!defined(CONFIG_DEV_URANDOM_RANDOM_POOL)
|
|
|
|
# ifdef CONFIG_CRYPTO_RANDOM_POOL
|
|
|
|
# define CONFIG_DEV_URANDOM_RANDOM_POOL 1
|
|
|
|
# else
|
|
|
|
# define CONFIG_DEV_URANDOM_XORSHIFT128 1
|
|
|
|
# endif
|
2016-07-17 15:56:25 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEV_URANDOM_XORSHIFT128
|
2016-08-21 15:47:53 +02:00
|
|
|
# define PRNG() do_xorshift128()
|
2017-03-30 15:38:37 +02:00
|
|
|
#elif defined(CONFIG_DEV_URANDOM_CONGRUENTIAL)
|
2016-08-21 15:47:53 +02:00
|
|
|
# define PRNG() do_congruential()
|
2016-07-17 15:56:25 +02:00
|
|
|
#endif
|
|
|
|
|
2016-07-17 14:42:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-08-21 15:47:53 +02:00
|
|
|
union xorshift128_state_u
|
2016-07-17 14:42:26 +02:00
|
|
|
{
|
2016-08-21 15:47:53 +02:00
|
|
|
struct xorshift128_state_s state;
|
|
|
|
uint8_t u[16];
|
2016-08-20 19:36:02 +02:00
|
|
|
};
|
2016-07-17 14:42:26 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define min(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
|
|
|
|
static ssize_t devurand_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t devurand_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int devurand_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-08-20 19:36:02 +02:00
|
|
|
static const struct file_operations g_urand_fops =
|
2016-07-17 14:42:26 +02:00
|
|
|
{
|
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
|
|
|
devurand_read, /* read */
|
|
|
|
devurand_write, /* write */
|
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
NULL, /* ioctl */
|
|
|
|
devurand_poll /* poll */
|
2016-07-17 14:42:26 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2016-08-21 15:47:53 +02:00
|
|
|
#ifdef CONFIG_DEV_URANDOM_XORSHIFT128
|
|
|
|
static union xorshift128_state_u g_prng;
|
|
|
|
#endif
|
|
|
|
|
2016-07-17 14:42:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-17 15:56:25 +02:00
|
|
|
/****************************************************************************
|
2016-08-21 15:47:53 +02:00
|
|
|
* Name: do_xorshift128
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEV_URANDOM_XORSHIFT128
|
|
|
|
static inline uint32_t do_xorshift128(void)
|
|
|
|
{
|
|
|
|
return xorshift128(&g_prng.state);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: do_congruential
|
2016-07-17 15:56:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEV_URANDOM_CONGRUENTIAL
|
2016-08-21 15:47:53 +02:00
|
|
|
static inline uint32_t do_congruential(void)
|
2016-07-17 15:56:25 +02:00
|
|
|
{
|
|
|
|
/* REVISIT: We could probably generate a 32-bit value with a single
|
|
|
|
* call to nrand().
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (uint32_t)nrand(65536L) << (uint32_t)nrand(65536L);
|
|
|
|
}
|
|
|
|
#endif
|
2016-07-17 14:42:26 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: devurand_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t devurand_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
2017-03-30 15:38:37 +02:00
|
|
|
#ifdef CONFIG_DEV_URANDOM_RANDOM_POOL
|
2019-09-23 20:58:31 +02:00
|
|
|
if (len > 0)
|
2017-03-30 15:38:37 +02:00
|
|
|
{
|
2020-12-12 07:16:07 +01:00
|
|
|
arc4random_buf(buffer, len);
|
2017-03-30 15:38:37 +02:00
|
|
|
}
|
2019-10-01 15:12:37 +02:00
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
#else
|
2019-10-01 15:12:37 +02:00
|
|
|
size_t n;
|
|
|
|
uint32_t rnd;
|
2016-07-17 14:42:26 +02:00
|
|
|
|
|
|
|
n = len;
|
|
|
|
|
2020-02-22 19:31:14 +01:00
|
|
|
/* Align buffer pointer to 4-byte boundary */
|
2016-07-17 14:42:26 +02:00
|
|
|
|
2016-07-17 15:56:25 +02:00
|
|
|
if (((uintptr_t)buffer & 0x03) != 0)
|
2016-07-17 14:42:26 +02:00
|
|
|
{
|
|
|
|
/* Generate a pseudo random number */
|
|
|
|
|
2016-07-17 15:56:25 +02:00
|
|
|
rnd = PRNG();
|
2016-07-17 14:42:26 +02:00
|
|
|
|
2016-07-17 15:56:25 +02:00
|
|
|
while (((uintptr_t)buffer & 0x03) != 0)
|
2016-07-17 14:42:26 +02:00
|
|
|
{
|
|
|
|
if (n <= 0)
|
|
|
|
{
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-07-17 15:11:09 +02:00
|
|
|
*buffer++ = rnd & 0xff;
|
2016-07-17 14:42:26 +02:00
|
|
|
rnd >>= 8;
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stuff buffer with PRNGs 4 bytes at a time */
|
|
|
|
|
|
|
|
while (n >= 4)
|
|
|
|
{
|
2016-07-17 15:56:25 +02:00
|
|
|
*(uint32_t *)buffer = PRNG();
|
2016-07-17 14:42:26 +02:00
|
|
|
buffer += 4;
|
|
|
|
n -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stuff remaining 1, 2, or 3 bytes */
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
/* Generate a pseudo random number */
|
|
|
|
|
2016-07-17 15:56:25 +02:00
|
|
|
rnd = PRNG();
|
2016-07-17 14:42:26 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2019-10-01 15:12:37 +02:00
|
|
|
*buffer++ = rnd & 0xff;
|
2016-07-17 14:42:26 +02:00
|
|
|
rnd >>= 8;
|
|
|
|
}
|
|
|
|
while (--n > 0);
|
|
|
|
}
|
2017-03-30 15:38:37 +02:00
|
|
|
#endif /* CONFIG_DEV_URANDOM_RANDOM_POOL */
|
2016-07-17 14:42:26 +02:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: devurand_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t devurand_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
2016-07-17 15:56:25 +02:00
|
|
|
/* Write can be used to re-seed the PRNG state. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEV_URANDOM_CONGRUENTIAL
|
|
|
|
unsigned int seed = 0;
|
2016-07-17 14:42:26 +02:00
|
|
|
|
2016-08-21 15:47:53 +02:00
|
|
|
len = min(len, sizeof(unsigned int));
|
|
|
|
memcpy(&seed, buffer, len);
|
2016-07-17 15:56:25 +02:00
|
|
|
srand(seed);
|
2016-08-21 15:47:53 +02:00
|
|
|
return len;
|
2019-09-23 20:58:31 +02:00
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
#elif defined(CONFIG_DEV_URANDOM_RANDOM_POOL)
|
|
|
|
const unsigned int alignmask = sizeof(uint32_t) - 1;
|
|
|
|
const size_t initlen = len;
|
|
|
|
uint32_t tmp = 0;
|
|
|
|
size_t currlen;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Seed entropy pool with data from user. */
|
|
|
|
|
|
|
|
if ((uintptr_t)buffer & alignmask)
|
|
|
|
{
|
|
|
|
/* Make unaligned input aligned. */
|
|
|
|
|
|
|
|
currlen = min(sizeof(uint32_t) - ((uintptr_t)buffer & alignmask), len);
|
|
|
|
memcpy(&tmp, buffer, currlen);
|
|
|
|
up_rngaddint(RND_SRC_SW, tmp);
|
|
|
|
|
|
|
|
len -= currlen;
|
|
|
|
buffer += currlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len >= sizeof(uint32_t))
|
|
|
|
{
|
|
|
|
/* Handle bulk aligned, word-sized data. */
|
|
|
|
|
|
|
|
DEBUGASSERT(((uintptr_t)buffer & alignmask) == 0);
|
|
|
|
currlen = len / sizeof(uint32_t);
|
|
|
|
up_rngaddentropy(RND_SRC_SW, (FAR uint32_t *)buffer, currlen);
|
|
|
|
buffer += currlen * sizeof(uint32_t);
|
|
|
|
len %= sizeof(uint32_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
/* Handle trailing bytes. */
|
|
|
|
|
|
|
|
DEBUGASSERT(len < sizeof(uint32_t));
|
|
|
|
memcpy(&tmp, buffer, len);
|
|
|
|
up_rngaddint(RND_SRC_SW, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reseeding of random number generator from entropy pool. */
|
|
|
|
|
|
|
|
up_rngreseed();
|
|
|
|
|
|
|
|
return initlen;
|
2016-07-17 15:56:25 +02:00
|
|
|
#else
|
2016-08-21 15:47:53 +02:00
|
|
|
len = min(len, sizeof(g_prng.u));
|
|
|
|
memcpy(&g_prng.u, buffer, len);
|
|
|
|
return len;
|
2016-07-17 15:56:25 +02:00
|
|
|
#endif
|
2016-07-17 14:42:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: devurand_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int devurand_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
fds->revents |= (fds->events & (POLLIN | POLLOUT));
|
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(fds->sem);
|
2016-07-17 14:42:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: devurandom_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register /dev/urandom
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void devurandom_register(void)
|
|
|
|
{
|
2016-08-21 15:47:53 +02:00
|
|
|
/* Seed the PRNG */
|
|
|
|
|
2016-07-17 15:56:25 +02:00
|
|
|
#ifdef CONFIG_DEV_URANDOM_CONGRUENTIAL
|
|
|
|
srand(10197);
|
2017-03-30 15:38:37 +02:00
|
|
|
#elif defined(CONFIG_DEV_URANDOM_RANDOM_POOL)
|
|
|
|
up_randompool_initialize();
|
2016-07-17 15:56:25 +02:00
|
|
|
#else
|
2016-08-21 15:47:53 +02:00
|
|
|
g_prng.state.w = 97;
|
|
|
|
g_prng.state.x = 101;
|
|
|
|
g_prng.state.y = g_prng.state.w << 17;
|
|
|
|
g_prng.state.z = g_prng.state.x << 25;
|
2016-07-17 15:56:25 +02:00
|
|
|
#endif
|
2016-07-17 14:42:26 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
register_driver("/dev/urandom", &g_urand_fops, 0666, NULL);
|
2016-07-17 14:42:26 +02:00
|
|
|
}
|
2016-07-18 15:03:47 +02:00
|
|
|
|
|
|
|
#endif /* CONFIG_DEV_URANDOM && CONFIG_DEV_URANDOM_ARCH */
|