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