mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-08-13 17:38:59 +00:00
update to cvs head srtp
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
*/
|
||||
/*
|
||||
*
|
||||
* Copyright(c) 2001-2005 Cisco Systems, Inc.
|
||||
* Copyright(c) 2001-2006 Cisco Systems, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -43,18 +43,36 @@
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "rand_source.h"
|
||||
|
||||
#ifdef DEV_URANDOM
|
||||
# include <fcntl.h> /* for open() */
|
||||
# include <unistd.h> /* for close() */
|
||||
#elif defined(HAVE_RAND_S)
|
||||
# define _CRT_RAND_S
|
||||
# include <stdlib.h>
|
||||
#else
|
||||
# include <stdio.h>
|
||||
#endif
|
||||
|
||||
/* global dev_rand_fdes is file descriptor for /dev/random */
|
||||
#include "rand_source.h"
|
||||
|
||||
static int dev_random_fdes = -1;
|
||||
|
||||
/*
|
||||
* global dev_rand_fdes is file descriptor for /dev/random
|
||||
*
|
||||
* This variable is also used to indicate that the random source has
|
||||
* been initialized. When this variable is set to the value of the
|
||||
* #define RAND_SOURCE_NOT_READY, it indicates that the random source
|
||||
* is not ready to be used. The value of the #define
|
||||
* RAND_SOURCE_READY is for use whenever that variable is used as an
|
||||
* indicator of the state of the random source, but not as a file
|
||||
* descriptor.
|
||||
*/
|
||||
|
||||
#define RAND_SOURCE_NOT_READY (-1)
|
||||
#define RAND_SOURCE_READY (17)
|
||||
|
||||
static int dev_random_fdes = RAND_SOURCE_NOT_READY;
|
||||
|
||||
|
||||
err_status_t
|
||||
@@ -68,10 +86,12 @@ rand_source_init(void) {
|
||||
dev_random_fdes = open(DEV_URANDOM, O_RDONLY);
|
||||
if (dev_random_fdes < 0)
|
||||
return err_status_init_fail;
|
||||
#elif defined(HAVE_RAND_S)
|
||||
dev_random_fdes = RAND_SOURCE_READY;
|
||||
#else
|
||||
/* no random source available; let the user know */
|
||||
fprintf(stderr, "WARNING: no real random source present!\n");
|
||||
dev_random_fdes = 17;
|
||||
dev_random_fdes = RAND_SOURCE_READY;
|
||||
#endif
|
||||
return err_status_ok;
|
||||
}
|
||||
@@ -85,25 +105,45 @@ rand_source_get_octet_string(void *dest, uint32_t len) {
|
||||
* written
|
||||
*/
|
||||
#ifdef DEV_URANDOM
|
||||
if (read(dev_random_fdes, dest, len) != len)
|
||||
return err_status_fail;
|
||||
uint8_t *dst = (uint8_t *)dest;
|
||||
while (len)
|
||||
{
|
||||
ssize_t num_read = read(dev_random_fdes, dst, len);
|
||||
if (num_read <= 0 || num_read > len)
|
||||
return err_status_fail;
|
||||
len -= num_read;
|
||||
dst += num_read;
|
||||
}
|
||||
#elif defined(HAVE_RAND_S)
|
||||
uint8_t *dst = (uint8_t *)dest;
|
||||
while (len)
|
||||
{
|
||||
unsigned int val;
|
||||
errno_t err = rand_s(&val);
|
||||
|
||||
if (err != 0)
|
||||
return err_status_fail;
|
||||
|
||||
*dst++ = val & 0xff;
|
||||
len--;
|
||||
}
|
||||
#else
|
||||
/* Generic C-library (rand()) version */
|
||||
/* This is a random source of last resort */
|
||||
uint8_t *dst = dest;
|
||||
uint8_t *dst = (uint8_t *)dest;
|
||||
while (len)
|
||||
{
|
||||
int val = rand();
|
||||
/* rand() returns 0-32767 (ugh) */
|
||||
/* Is this a good enough way to get random bytes?
|
||||
It is if it passes FIPS-140... */
|
||||
*dst++ = (uint8_t)(val & 0xff);
|
||||
*dst++ = val & 0xff;
|
||||
len--;
|
||||
}
|
||||
#endif
|
||||
return err_status_ok;
|
||||
}
|
||||
|
||||
|
||||
err_status_t
|
||||
rand_source_deinit(void) {
|
||||
if (dev_random_fdes < 0)
|
||||
@@ -112,7 +152,7 @@ rand_source_deinit(void) {
|
||||
#ifdef DEV_URANDOM
|
||||
close(dev_random_fdes);
|
||||
#endif
|
||||
dev_random_fdes = -1;
|
||||
dev_random_fdes = RAND_SOURCE_NOT_READY;
|
||||
|
||||
return err_status_ok;
|
||||
}
|
||||
|
Reference in New Issue
Block a user