Skip to content

Commit 49cb83f

Browse files
author
Seppo Takalo
committed
Merge pull request #2 from ARMmbed/urandom
Use /dev/urandom on Linux.
2 parents eb42545 + db7abad commit 49cb83f

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
ifeq ($(shell uname -s),Linux)
2+
SRCS := $(wildcard linux/*)
3+
else
14
SRCS := $(wildcard source/*)
5+
endif
26
LIB := librand.a
37
EXPORT_HEADERS := mbed-client-randlib
48

linux/randLIB.c

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* Copyright (c) 2014-2015 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include <stdint.h>
17+
#include <stdlib.h>
18+
#include <sys/types.h>
19+
#include <sys/stat.h>
20+
#include <fcntl.h>
21+
#include <unistd.h>
22+
#include "randLIB.h"
23+
24+
/**
25+
* \brief Init seed for Pseudo Random.
26+
* On a Linux, this does nothing.
27+
*
28+
* \return None
29+
*
30+
*/
31+
void randLIB_seed_random(void)
32+
{
33+
}
34+
35+
/**
36+
* \brief Generate 8-bit random number.
37+
*
38+
* \param None
39+
* \return 8-bit random number
40+
*
41+
*/
42+
uint8_t randLIB_get_8bit(void)
43+
{
44+
uint8_t ret_val;
45+
randLIB_get_n_bytes_random(&ret_val, 1);
46+
return ret_val;
47+
}
48+
49+
/**
50+
* \brief Generate 16-bit random number.
51+
*
52+
* \param None
53+
* \return 16-bit random number
54+
*
55+
*/
56+
uint16_t randLIB_get_16bit(void)
57+
{
58+
uint16_t ret_val;
59+
60+
randLIB_get_n_bytes_random((uint8_t*)&ret_val, 2);
61+
return ret_val;
62+
}
63+
/**
64+
* \brief Generate 32-bit random number.
65+
*
66+
* \param None
67+
* \return 32-bit random number
68+
*
69+
*/
70+
uint32_t randLIB_get_32bit(void)
71+
{
72+
uint32_t ret_val;
73+
randLIB_get_n_bytes_random((uint8_t*)&ret_val, 4);
74+
return ret_val;
75+
}
76+
77+
78+
/**
79+
* \brief Generate n-bytes random numbers.
80+
*
81+
* \param data_ptr pointer where random will be stored
82+
* \param eight_bit_boundary how many bytes need random
83+
* \return 0 process valid
84+
* \return -1 Unsupported Parameters or failed to get random data.
85+
*
86+
*/
87+
int8_t randLIB_get_n_bytes_random(uint8_t *data_ptr, uint8_t eight_bit_boundary)
88+
{
89+
if ((data_ptr == 0) || (eight_bit_boundary == 0)) {
90+
return -1;
91+
}
92+
93+
int fd = open("/dev/urandom", O_RDONLY);
94+
if (fd != -1) {
95+
size_t len = read(fd, data_ptr, eight_bit_boundary);
96+
close(fd);
97+
if (len == eight_bit_boundary)
98+
return 0;
99+
}
100+
101+
return -1;
102+
}
103+
104+
/**
105+
* \brief Generate a random number within a range.
106+
*
107+
* The result is linearly distributed in the range [min..max], inclusive.
108+
*
109+
* \param min minimum value that can be generated
110+
* \param max maximum value that can be generated
111+
*/
112+
uint16_t randLIB_get_random_in_range(uint16_t min, uint16_t max)
113+
{
114+
/* This special case is potentially common, particularly in this routine's
115+
* first user (Trickle), so worth catching immediately */
116+
if (min == max) {
117+
return min;
118+
}
119+
120+
/* 16-bit arithmetic below fails in this extreme case; we can optimise it */
121+
if (max - min == 0xFFFF) {
122+
return randLIB_get_16bit();
123+
}
124+
125+
unsigned int values_needed = max + 1 - min;
126+
unsigned int band_size = 0x10000u / values_needed;
127+
unsigned int top_of_bands = band_size * values_needed;
128+
unsigned int result;
129+
do {
130+
result = randLIB_get_16bit();
131+
} while (result >= top_of_bands);
132+
133+
return min + (uint16_t)(result / band_size);
134+
}
135+
136+
/**
137+
* \brief Randomise a base 32-bit number by a jitter factor
138+
*
139+
* The result is linearly distributed in the jitter range, which is expressed
140+
* as fixed-point unsigned 1.15 values. For example, to produce a number in the
141+
* range [0.75 * base, 1.25 * base], set min_factor to 0x6000 and max_factor to
142+
* 0xA000.
143+
*
144+
* Result is clamped to 0xFFFFFFFF if it overflows.
145+
*
146+
* \param base The base 32-bit value
147+
* \param min_factor The minimum value for the random factor
148+
* \param max_factor The maximum value for the random factor
149+
*/
150+
uint32_t randLIB_randomise_base(uint32_t base, uint16_t min_factor, uint16_t max_factor)
151+
{
152+
uint16_t random_factor = randLIB_get_random_in_range(min_factor, max_factor);
153+
154+
/* 32x16-bit long multiplication, to get 48-bit result */
155+
uint32_t hi = (base >> 16) * random_factor;
156+
uint32_t lo = (base & 0xFFFF) * random_factor;
157+
/* Add halves, and take top 32 bits of 48-bit result */
158+
uint32_t res = hi + (lo >> 16);
159+
160+
/* Randomisation factor is *2^15, so need to shift up 1 more bit, avoiding overflow */
161+
if (res & 0x80000000) {
162+
res = 0xFFFFFFFF;
163+
} else {
164+
res = (res << 1) | ((lo >> 15) & 1);
165+
}
166+
167+
return res;
168+
}

0 commit comments

Comments
 (0)