mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*
elessair 0:f269e3021894 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
elessair 0:f269e3021894 3 * SPDX-License-Identifier: Apache-2.0
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the License); you may
elessair 0:f269e3021894 5 * not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
elessair 0:f269e3021894 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 */
elessair 0:f269e3021894 16 #include <stdint.h>
elessair 0:f269e3021894 17 #include <stdlib.h>
elessair 0:f269e3021894 18 #include <stdbool.h>
elessair 0:f269e3021894 19 #include <limits.h>
elessair 0:f269e3021894 20 #include "randLIB.h"
elessair 0:f269e3021894 21 #include "platform/arm_hal_random.h"
elessair 0:f269e3021894 22
elessair 0:f269e3021894 23 #if ((RAND_MAX+1) & RAND_MAX) != 0
elessair 0:f269e3021894 24 #error "RAND_MAX isn't 2^n-1 :("
elessair 0:f269e3021894 25 #endif
elessair 0:f269e3021894 26
elessair 0:f269e3021894 27 /**
elessair 0:f269e3021894 28 * This library is made for getting random numbers for Timing needs in protocols.
elessair 0:f269e3021894 29 *
elessair 0:f269e3021894 30 * **not safe to use for security or cryptographic operations.**
elessair 0:f269e3021894 31 *
elessair 0:f269e3021894 32 */
elessair 0:f269e3021894 33
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 /**
elessair 0:f269e3021894 36 * \brief Init seed for Pseudo Random.
elessair 0:f269e3021894 37 *
elessair 0:f269e3021894 38 * \return None
elessair 0:f269e3021894 39 *
elessair 0:f269e3021894 40 */
elessair 0:f269e3021894 41 void randLIB_seed_random(void)
elessair 0:f269e3021894 42 {
elessair 0:f269e3021894 43 uint32_t rand_seed;
elessair 0:f269e3021894 44 arm_random_module_init();
elessair 0:f269e3021894 45 rand_seed = arm_random_seed_get();
elessair 0:f269e3021894 46 srand(rand_seed);
elessair 0:f269e3021894 47 }
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49 /**
elessair 0:f269e3021894 50 * \brief Generate 8-bit random number.
elessair 0:f269e3021894 51 *
elessair 0:f269e3021894 52 * \param None
elessair 0:f269e3021894 53 * \return 8-bit random number
elessair 0:f269e3021894 54 *
elessair 0:f269e3021894 55 */
elessair 0:f269e3021894 56 uint8_t randLIB_get_8bit(void)
elessair 0:f269e3021894 57 {
elessair 0:f269e3021894 58 return rand();
elessair 0:f269e3021894 59 }
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61 /**
elessair 0:f269e3021894 62 * \brief Generate 16-bit random number.
elessair 0:f269e3021894 63 *
elessair 0:f269e3021894 64 * \param None
elessair 0:f269e3021894 65 * \return 16-bit random number
elessair 0:f269e3021894 66 *
elessair 0:f269e3021894 67 */
elessair 0:f269e3021894 68 uint16_t randLIB_get_16bit(void)
elessair 0:f269e3021894 69 {
elessair 0:f269e3021894 70 uint16_t ret_val;
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 ret_val = rand();
elessair 0:f269e3021894 73 #if RAND_MAX == 0x7FFF
elessair 0:f269e3021894 74 ret_val |= (uint16_t) rand() << 15;
elessair 0:f269e3021894 75 #endif
elessair 0:f269e3021894 76
elessair 0:f269e3021894 77 return ret_val;
elessair 0:f269e3021894 78 }
elessair 0:f269e3021894 79 /**
elessair 0:f269e3021894 80 * \brief Generate 32-bit random number.
elessair 0:f269e3021894 81 *
elessair 0:f269e3021894 82 * \param None
elessair 0:f269e3021894 83 * \return 32-bit random number
elessair 0:f269e3021894 84 *
elessair 0:f269e3021894 85 */
elessair 0:f269e3021894 86 uint32_t randLIB_get_32bit(void)
elessair 0:f269e3021894 87 {
elessair 0:f269e3021894 88 uint32_t ret_val;
elessair 0:f269e3021894 89
elessair 0:f269e3021894 90 ret_val = rand();
elessair 0:f269e3021894 91 #if RAND_MAX == 0x7FFF
elessair 0:f269e3021894 92 ret_val |= (uint32_t) rand() << 15;
elessair 0:f269e3021894 93 ret_val |= (uint32_t) rand() << 30;
elessair 0:f269e3021894 94 #elif RAND_MAX == 0x3FFFFFFF /* IAR */
elessair 0:f269e3021894 95 ret_val |= (uint32_t) rand() << 30;
elessair 0:f269e3021894 96 #elif RAND_MAX == 0x7FFFFFFF
elessair 0:f269e3021894 97 ret_val |= (uint32_t) rand() << 31;
elessair 0:f269e3021894 98 #else
elessair 0:f269e3021894 99 #error "randLIB_get_32bit - odd RAND_MAX"
elessair 0:f269e3021894 100 #endif
elessair 0:f269e3021894 101
elessair 0:f269e3021894 102 return ret_val;
elessair 0:f269e3021894 103 }
elessair 0:f269e3021894 104
elessair 0:f269e3021894 105
elessair 0:f269e3021894 106 /**
elessair 0:f269e3021894 107 * \brief Generate n-bytes random numbers.
elessair 0:f269e3021894 108 *
elessair 0:f269e3021894 109 * \param data_ptr pointer where random will be stored
elessair 0:f269e3021894 110 * \param eight_bit_boundary how many bytes need random
elessair 0:f269e3021894 111 * \return 0 process valid
elessair 0:f269e3021894 112 * \return -1 Unsupported Parameters
elessair 0:f269e3021894 113 *
elessair 0:f269e3021894 114 */
elessair 0:f269e3021894 115 int8_t randLIB_get_n_bytes_random(uint8_t *data_ptr, uint8_t eight_bit_boundary)
elessair 0:f269e3021894 116 {
elessair 0:f269e3021894 117 if ((data_ptr == 0) || (eight_bit_boundary == 0)) {
elessair 0:f269e3021894 118 return -1;
elessair 0:f269e3021894 119 }
elessair 0:f269e3021894 120
elessair 0:f269e3021894 121 while (eight_bit_boundary) {
elessair 0:f269e3021894 122 *data_ptr++ = randLIB_get_8bit();
elessair 0:f269e3021894 123 eight_bit_boundary--;
elessair 0:f269e3021894 124 }
elessair 0:f269e3021894 125 return 0;
elessair 0:f269e3021894 126 }
elessair 0:f269e3021894 127
elessair 0:f269e3021894 128 /**
elessair 0:f269e3021894 129 * \brief Generate a random number within a range.
elessair 0:f269e3021894 130 *
elessair 0:f269e3021894 131 * The result is linearly distributed in the range [min..max], inclusive.
elessair 0:f269e3021894 132 *
elessair 0:f269e3021894 133 * \param min minimum value that can be generated
elessair 0:f269e3021894 134 * \param max maximum value that can be generated
elessair 0:f269e3021894 135 */
elessair 0:f269e3021894 136 uint16_t randLIB_get_random_in_range(uint16_t min, uint16_t max)
elessair 0:f269e3021894 137 {
elessair 0:f269e3021894 138 /* This special case is potentially common, particularly in this routine's
elessair 0:f269e3021894 139 * first user (Trickle), so worth catching immediately */
elessair 0:f269e3021894 140 if (min == max) {
elessair 0:f269e3021894 141 return min;
elessair 0:f269e3021894 142 }
elessair 0:f269e3021894 143
elessair 0:f269e3021894 144 /* 16-bit arithmetic below fails in this extreme case; we can optimise it */
elessair 0:f269e3021894 145 if (max - min == 0xFFFF) {
elessair 0:f269e3021894 146 return randLIB_get_16bit();
elessair 0:f269e3021894 147 }
elessair 0:f269e3021894 148
elessair 0:f269e3021894 149 /* We get RAND_MAX+1 values from rand() in the range [0..RAND_MAX], and
elessair 0:f269e3021894 150 * need to divvy them up into the number of values we need. And reroll any
elessair 0:f269e3021894 151 * odd values off the end as we insist every value having equal chance.
elessair 0:f269e3021894 152 *
elessair 0:f269e3021894 153 * Special handling for systems where RAND_MAX is 0x7FFF; we use our
elessair 0:f269e3021894 154 * randLIB_get_16bit() and have to be a bit more careful about
elessair 0:f269e3021894 155 * unsigned integer overflow. (On other systems rand() returns int,
elessair 0:f269e3021894 156 * so we can't overflow if we use unsigned int).
elessair 0:f269e3021894 157 *
elessair 0:f269e3021894 158 * Eg, range(1,3), RAND_MAX = 0x7FFFFFFF:
elessair 0:f269e3021894 159 * We have 3 bands of size 0x2AAAAAAA (0x80000000/3).
elessair 0:f269e3021894 160 *
elessair 0:f269e3021894 161 * We roll: 0x00000000..0x2AAAAAAA9 -> 1
elessair 0:f269e3021894 162 * 0x2AAAAAAA..0x555555553 -> 2
elessair 0:f269e3021894 163 * 0x55555554..0x7FFFFFFFD -> 3
elessair 0:f269e3021894 164 * 0x7FFFFFFE..0x7FFFFFFFF -> reroll
elessair 0:f269e3021894 165 *
elessair 0:f269e3021894 166 * (Bias problem clearly pretty insignificant there, but gets worse as
elessair 0:f269e3021894 167 * range increases).
elessair 0:f269e3021894 168 */
elessair 0:f269e3021894 169 unsigned int values_needed = max + 1 - min;
elessair 0:f269e3021894 170 #if RAND_MAX > 0xFFFF
elessair 0:f269e3021894 171 unsigned int band_size = (RAND_MAX + 1u) / values_needed;
elessair 0:f269e3021894 172 #elif UINT_MAX > 0xFFFF
elessair 0:f269e3021894 173 unsigned int band_size = 0x10000u / values_needed;
elessair 0:f269e3021894 174 #else
elessair 0:f269e3021894 175 /* Avoid the need for long division, at the expense of fractionally
elessair 0:f269e3021894 176 * increasing reroll chance. */
elessair 0:f269e3021894 177 unsigned int band_size = 0xFFFFu / values_needed;
elessair 0:f269e3021894 178 #endif
elessair 0:f269e3021894 179 unsigned int top_of_bands = band_size * values_needed;
elessair 0:f269e3021894 180 unsigned int result;
elessair 0:f269e3021894 181 do {
elessair 0:f269e3021894 182 #if RAND_MAX > 0xFFFF
elessair 0:f269e3021894 183 result = rand();
elessair 0:f269e3021894 184 #else
elessair 0:f269e3021894 185 result = randLIB_get_16bit();
elessair 0:f269e3021894 186 #endif
elessair 0:f269e3021894 187 } while (result >= top_of_bands);
elessair 0:f269e3021894 188
elessair 0:f269e3021894 189 return min + (uint16_t)(result / band_size);
elessair 0:f269e3021894 190 }
elessair 0:f269e3021894 191
elessair 0:f269e3021894 192 /**
elessair 0:f269e3021894 193 * \brief Randomise a base 32-bit number by a jitter factor
elessair 0:f269e3021894 194 *
elessair 0:f269e3021894 195 * The result is linearly distributed in the jitter range, which is expressed
elessair 0:f269e3021894 196 * as fixed-point unsigned 1.15 values. For example, to produce a number in the
elessair 0:f269e3021894 197 * range [0.75 * base, 1.25 * base], set min_factor to 0x6000 and max_factor to
elessair 0:f269e3021894 198 * 0xA000.
elessair 0:f269e3021894 199 *
elessair 0:f269e3021894 200 * Result is clamped to 0xFFFFFFFF if it overflows.
elessair 0:f269e3021894 201 *
elessair 0:f269e3021894 202 * \param base The base 32-bit value
elessair 0:f269e3021894 203 * \param min_factor The minimum value for the random factor
elessair 0:f269e3021894 204 * \param max_factor The maximum value for the random factor
elessair 0:f269e3021894 205 */
elessair 0:f269e3021894 206 uint32_t randLIB_randomise_base(uint32_t base, uint16_t min_factor, uint16_t max_factor)
elessair 0:f269e3021894 207 {
elessair 0:f269e3021894 208 uint16_t random_factor = randLIB_get_random_in_range(min_factor, max_factor);
elessair 0:f269e3021894 209
elessair 0:f269e3021894 210 /* 32x16-bit long multiplication, to get 48-bit result */
elessair 0:f269e3021894 211 uint32_t hi = (base >> 16) * random_factor;
elessair 0:f269e3021894 212 uint32_t lo = (base & 0xFFFF) * random_factor;
elessair 0:f269e3021894 213 /* Add halves, and take top 32 bits of 48-bit result */
elessair 0:f269e3021894 214 uint32_t res = hi + (lo >> 16);
elessair 0:f269e3021894 215
elessair 0:f269e3021894 216 /* Randomisation factor is *2^15, so need to shift up 1 more bit, avoiding overflow */
elessair 0:f269e3021894 217 if (res & 0x80000000) {
elessair 0:f269e3021894 218 res = 0xFFFFFFFF;
elessair 0:f269e3021894 219 } else {
elessair 0:f269e3021894 220 res = (res << 1) | ((lo >> 15) & 1);
elessair 0:f269e3021894 221 }
elessair 0:f269e3021894 222
elessair 0:f269e3021894 223 return res;
elessair 0:f269e3021894 224 }