Contiki-NG
Loading...
Searching...
No Matches
cc2538-sram-seeder.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Hasso-Plattner-Institut.
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 csprng
35 * @{
36 *
37 * \file
38 * SRAM-based CSPRNG seeder.
39 * \author
40 * Konrad Krentz <konrad.krentz@gmail.com>
41 */
42
44#include "sys/rtimer.h"
45#include "lpm.h"
46#include <string.h>
47
48#define SRAM_BYTES 1024
49
50/* in the middle of the second half of the SRAM */
51static const uint8_t *sram_pointer = (uint8_t *)0x20001FFF;
52
53/*---------------------------------------------------------------------------*/
54static void
55reset_sram(void)
56{
57 /*
58 * Although it should not, the system gets stuck when sleeping less than
59 * two seconds at this point. Sleeping for two seconds seems to be safe.
60 */
61 rtimer_arch_schedule(RTIMER_NOW() + RTIMER_ARCH_SECOND * 2);
62 lpm_enter();
63}
64/*---------------------------------------------------------------------------*/
65void
67{
68 struct csprng_seed seed;
69 uint16_t byte_pos;
70 uint8_t bit_pos;
71 uint8_t sram_snapshot[SRAM_BYTES];
72 uint16_t i;
73 uint8_t j;
74 int bit1;
75 int bit2;
76
77 byte_pos = 0;
78 bit_pos = 0;
79
80 while(1) {
81 reset_sram();
82 memcpy(sram_snapshot, sram_pointer, SRAM_BYTES);
83 reset_sram();
84
85 /* von Neumann extractor */
86 for(i = 0; i < SRAM_BYTES; i++) {
87 for(j = 0; j < 8; j++) {
88 bit1 = (1 << j) & sram_snapshot[i];
89 bit2 = (1 << j) & sram_pointer[i];
90 if(bit1 < bit2) {
91 seed.u8[byte_pos] |= 1 << bit_pos++;
92 } else if(bit1 > bit2) {
93 seed.u8[byte_pos] &= ~(1 << bit_pos++);
94 }
95 if(bit_pos == 8) {
96 bit_pos = 0;
97 byte_pos++;
98 }
99 if(byte_pos == CSPRNG_SEED_LEN) {
100 csprng_feed(&seed);
101 return;
102 }
103 }
104 }
105 }
106}
107/*---------------------------------------------------------------------------*/
108
109/** @} */
SRAM-based CSPRNG seeder.
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition rtimer-arch.c:65
void csprng_feed(struct csprng_seed *new_seed)
Mixes a new seed with the current one.
Definition csprng.c:61
void cc2538_sram_seeder_seed(void)
This function will feed the CSPRNG with a new seed.
#define RTIMER_NOW()
Get the current clock time.
Definition rtimer.h:187
Header file for the real-time timer module.
This is the structure of a seed.
Definition csprng.h:76
uint8_t u8[(AES_128_KEY_LENGTH+AES_128_BLOCK_SIZE)]
for convenience
Definition csprng.h:83