Contiki-NG
random.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 /**
31 * \addtogroup cc13xx-cc26xx-cpu
32 * @{
33 *
34 * \defgroup cc13xx-cc26xx-prng Pseudo Random Number Generator (PRNG) for CC13xx/CC26xx.
35 * @{
36 *
37 * Implementation based on Bob Jenkins' small noncryptographic PRNG.
38 * - http://burtleburtle.net/bob/rand/smallprng.html
39 *
40 * This file overrides os/lib/random.c. Note that the file name must
41 * match the original file for the override to work.
42 *
43 * \file
44 * Implementation of Pseudo Random Number Generator for CC13xx/CC26xx.
45 * \author
46 * Edvard Pettersen <e.pettersen@ti.com>
47 */
48/*---------------------------------------------------------------------------*/
49#include <contiki.h>
50/*---------------------------------------------------------------------------*/
51#include <stdint.h>
52/*---------------------------------------------------------------------------*/
53typedef struct {
54 uint32_t a;
55 uint32_t b;
56 uint32_t c;
57 uint32_t d;
58} ranctx_t;
59
60static ranctx_t ranctx;
61/*---------------------------------------------------------------------------*/
62#define rot32(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
63/*---------------------------------------------------------------------------*/
64/**
65 * \brief Generates a new random number using the PRNG.
66 * \return The random number.
67 */
68unsigned short
70{
71 uint32_t e;
72
73 e = ranctx.a - rot32(ranctx.b, 27);
74 ranctx.a = ranctx.b ^ rot32(ranctx.c, 17);
75 ranctx.b = ranctx.c + ranctx.d;
76 ranctx.c = ranctx.d + e;
77 ranctx.d = e + ranctx.a;
78
79 return (unsigned short)ranctx.d;
80}
81/*---------------------------------------------------------------------------*/
82/**
83 * \brief Initialize the PRNG.
84 * \param seed Seed for the PRNG.
85 */
86void
87random_init(unsigned short seed)
88{
89 uint32_t i;
90
91 ranctx.a = 0xf1ea5eed;
92 ranctx.b = ranctx.c = ranctx.d = (uint32_t)seed;
93 for(i = 0; i < 20; ++i) {
94 (void)random_rand();
95 }
96}
97/*---------------------------------------------------------------------------*/
98/**
99 * @}
100 * @}
101 */
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:84
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:58