Contiki-NG
Toggle main menu visibility
Loading...
Searching...
No Matches
csprng.h
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 lib
35
* @{
36
*
37
* \defgroup crypto Cryptographic primitives
38
* @{
39
*
40
* \defgroup csprng Cryptographically-secure PRNG
41
* In contrast to a normal PRNG, a CSPRNG generates a stream of pseudo-random
42
* numbers that is indistinguishable from the uniform distribution to a
43
* computationally-bounded adversary who does not know the seed.
44
* @{
45
*
46
* \file
47
* An OFB-AES-128-based CSPRNG.
48
* \author
49
* Konrad Krentz <konrad.krentz@gmail.com>
50
*/
51
52
#ifndef CSPRNG_H_
53
#define CSPRNG_H_
54
55
#include "contiki.h"
56
#include "
lib/aes-128.h
"
57
#include <stddef.h>
58
#include <stdint.h>
59
#include <stdbool.h>
60
61
#ifdef CSPRNG_CONF_ENABLED
62
#define CSPRNG_ENABLED CSPRNG_CONF_ENABLED
63
#else
/* CSPRNG_CONF_ENABLED */
64
#define CSPRNG_ENABLED 0
65
#endif
/* CSPRNG_CONF_ENABLED */
66
67
#define CSPRNG_KEY_LEN AES_128_KEY_LENGTH
68
#define CSPRNG_STATE_LEN AES_128_BLOCK_SIZE
69
#define CSPRNG_SEED_LEN (CSPRNG_KEY_LEN + CSPRNG_STATE_LEN)
70
71
/** This is the structure of a seed. */
72
struct
csprng_seed
{
73
union
{
74
struct
{
75
uint8_t
key
[CSPRNG_KEY_LEN];
/**< AES-128 key of the CSPRNG */
76
uint8_t
state
[CSPRNG_STATE_LEN];
/**< internal state of the CSPRNG */
77
};
78
79
uint8_t
u8
[CSPRNG_SEED_LEN];
/**< for convenience */
80
};
81
};
82
83
/**
84
* \brief Mixes a new seed with the current one.
85
* \param new_seed Pointer to the new seed.
86
*
87
* This function is called at start up and/or at runtime by
88
* what we call a "seeder". Seeders generate seeds in arbi-
89
* trary ways and feed this CSPRNG with their generated seeds.
90
*/
91
void
csprng_feed
(
struct
csprng_seed
*new_seed);
92
93
/**
94
* \brief Generates a cryptographic random number.
95
* \param result The place to store the generated cryptographic random number.
96
* \param len The length of the cryptographic random number to be generated.
97
*
98
* We use output feedback mode (OFB) for generating cryptographic
99
* pseudo-random numbers [RFC 4086]. A potential problem with OFB
100
* is that OFB at some point enters a cycle. However, the
101
* expected cycle length given a random key and a random state
102
* is about 2^127 in our case [Davies and Parkin, The Average
103
* Cycle Size of The Key Stream in Output Feedback Encipherment].
104
* \return Returns true on success and false otherwise.
105
*/
106
bool
csprng_rand
(uint8_t *result,
size_t
len);
107
108
#endif
/* CSPRNG_H_ */
109
110
/** @} */
111
/** @} */
112
/** @} */
aes-128.h
AES-128.
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
csprng_seed
This is the structure of a seed.
Definition
csprng.h:72
csprng_seed::state
uint8_t state[AES_128_BLOCK_SIZE]
internal state of the CSPRNG
Definition
csprng.h:76
csprng_seed::u8
uint8_t u8[(AES_128_KEY_LENGTH+AES_128_BLOCK_SIZE)]
for convenience
Definition
csprng.h:79
csprng_seed::key
uint8_t key[AES_128_KEY_LENGTH]
AES-128 key of the CSPRNG.
Definition
csprng.h:75
os
lib
csprng.h
Generated on
for Contiki-NG by
1.17.0