Contiki-NG
csprng.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013, 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 * \file
35 * An OFB-AES-128-based CSPRNG.
36 * \author
37 * Konrad Krentz <konrad.krentz@gmail.com>
38 */
39
40/**
41 * \addtogroup csprng
42 * @{
43 */
44
45#include "lib/csprng.h"
46#include "lib/aes-128.h"
47#include "sys/cc.h"
48#include <string.h>
49
50/* Log configuration */
51#include "sys/log.h"
52#define LOG_MODULE "CSPRNG"
53#define LOG_LEVEL LOG_LEVEL_NONE
54
55static struct csprng_seed seed;
56static unsigned read_state_bytes;
57static bool seeded;
58
59/*---------------------------------------------------------------------------*/
60void
61csprng_feed(struct csprng_seed *new_seed)
62{
63 uint8_t i;
64
65 /*
66 * By XORing the current seed with the new seed, the seed of this CSPRNG
67 * remains secret as long as any of the mixed seeds remains secret.
68 */
69 for(i = 0; i < CSPRNG_SEED_LEN; i++) {
70 seed.u8[i] ^= new_seed->u8[i];
71 }
72
73 LOG_DBG("key = ");
74 LOG_DBG_BYTES(seed.key, CSPRNG_KEY_LEN);
75 LOG_DBG_("\n");
76 LOG_DBG("state = ");
77 LOG_DBG_BYTES(seed.state, CSPRNG_STATE_LEN);
78 LOG_DBG_("\n");
79
80 seeded = true;
81}
82/*---------------------------------------------------------------------------*/
83bool
84csprng_rand(uint8_t *result, unsigned len)
85{
86 unsigned pos;
87
88 if(!seeded) {
89 return false;
90 }
91
92 pos = MIN(len, CSPRNG_STATE_LEN - read_state_bytes);
93 memcpy(result, seed.state + read_state_bytes, pos);
94 read_state_bytes += pos;
95 if(pos == len) {
96 return true;
97 }
98
99 AES_128.set_key(seed.key);
100 for(; pos < len; pos += CSPRNG_STATE_LEN) {
101 AES_128.encrypt(seed.state);
102 read_state_bytes = MIN(len - pos, CSPRNG_STATE_LEN);
103 memcpy(result + pos, seed.state, read_state_bytes);
104 }
105
106 return true;
107}
108/*---------------------------------------------------------------------------*/
109
110/** @} */
AES-128.
Default definitions of C compiler quirk work-arounds.
An OFB-AES-128-based CSPRNG.
void csprng_feed(struct csprng_seed *new_seed)
Mixes a new seed with the current one.
Definition: csprng.c:61
bool csprng_rand(uint8_t *result, unsigned len)
Generates a cryptographic random number.
Definition: csprng.c:84
Header file for the logging system.
This is the structure of a seed.
Definition: csprng.h:76
uint8_t state[CSPRNG_STATE_LEN]
internal state of the CSPRNG
Definition: csprng.h:80
uint8_t key[CSPRNG_KEY_LEN]
AES-128 key of the CSPRNG.
Definition: csprng.h:79
uint8_t u8[CSPRNG_SEED_LEN]
for convenience
Definition: csprng.h:83