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