Contiki-NG
Loading...
Searching...
No Matches
random.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005, Swedish Institute of Computer Science
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 Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 */
32
33/**
34 * \addtogroup random
35 * @{
36 *
37 * \file
38 * Utility functions for non-cryptographic random numbers.
39 * \author
40 * Konrad Krentz <konrad.krentz@gmail.com>
41 */
42
43#include "lib/random.h"
44#include "lib/csprng.h"
45#include "net/linkaddr.h"
46#include "net/netstack.h"
47#include <string.h>
48
49/* Log configuration */
50#include "sys/log.h"
51#define LOG_MODULE "random"
52#define LOG_LEVEL LOG_LEVEL_NONE
53
54/*---------------------------------------------------------------------------*/
55void
56random_init(void)
57{
58 uint64_t seed;
59#if CSPRNG_ENABLED
60 if(csprng_rand((uint8_t *)&seed, sizeof(seed))) {
61 LOG_DBG("Generated seed with CSPRNG: 0x%" PRIx64 "\n", seed);
62 } else
63#endif /* CSPRNG_ENABLED */
64 {
65#if LINKADDR_SIZE < 8
66 if(NETSTACK_RADIO.get_object(RADIO_PARAM_64BIT_ADDR, &seed, sizeof(seed))
67 == RADIO_RESULT_OK) {
68 LOG_DBG("Using 64-bit address as seed: 0x%" PRIx64 "\n", seed);
69 } else
70#endif /* LINKADDR_SIZE < 8 */
71 {
72 memcpy(&seed, linkaddr_node_addr.u8, sizeof(linkaddr_node_addr.u8));
73 LOG_DBG("Using %zu-byte linkaddr as seed: 0x%" PRIx64 "\n",
74 (size_t)LINKADDR_SIZE,
75 seed);
76 }
77 }
78 RANDOM_PRNG.seed(seed);
79}
80/*---------------------------------------------------------------------------*/
81
82/** @} */
An OFB-AES-128-based CSPRNG.
bool csprng_rand(uint8_t *result, size_t len)
Generates a cryptographic random number.
Definition csprng.c:81
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition linkaddr.c:48
@ RADIO_RESULT_OK
The parameter was set/read successfully.
Definition radio.h:480
@ RADIO_PARAM_64BIT_ADDR
Long (64 bits) address for the radio, which is used by the address filter.
Definition radio.h:263
Header file for the link-layer address representation.
Header file for the logging system.
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for generating non-cryptographic random numbers.