-
Notifications
You must be signed in to change notification settings - Fork 50
/
random.c
119 lines (103 loc) · 3.61 KB
/
random.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
The MIT License (MIT)
Copyright (c) 2015-2018 Douglas J. Bakkum
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include "random.h"
#include "memory.h"
#include "flags.h"
#include "flash.h"
#ifndef TESTING
#include "ataes132.h"
#include "sha2.h"
#include "mcu.h"
void random_init(void)
{
/* pass */
}
#else
#include <time.h>
void random_init(void)
{
srand(time(NULL));
}
#endif
uint32_t random_uint32(uint8_t update_seed)
{
uint32_t rn32;
uint8_t rn[4];
if (random_bytes(rn, 4, update_seed) != DBB_ERROR) {
memcpy(&rn32, rn, 4);
return rn32;
} else {
return 0;
}
}
int random_bytes(uint8_t *buf, uint32_t len, uint8_t update_seed)
{
uint32_t i = 0;
#ifndef TESTING
const uint8_t ataes_cmd[] = {0x02, 0x02, 0x00, 0x00, 0x00, 0x00}; // Pseudo RNG
const uint8_t ataes_cmd_up[] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x00}; // True RNG - writes to EEPROM
uint8_t ret, ataes_ret[20] = {0}; // Random command return packet [Count(1) || Return Code (1) | Data(16) || CRC (2)]
while (len > i) {
if (update_seed) {
ret = ataes_process(ataes_cmd_up, sizeof(ataes_cmd_up), ataes_ret, sizeof(ataes_ret));
update_seed = 0;
} else {
ret = ataes_process(ataes_cmd, sizeof(ataes_cmd), ataes_ret, sizeof(ataes_ret));
}
if (ret == DBB_OK && ataes_ret[0]) {
memcpy(buf + i, ataes_ret + 2, (len - i) < 16 ? (len - i) : 16);
} else {
HardFault_Handler();
return DBB_ERROR;
}
i += 16;
}
#ifndef BOOTLOADER
// Add ataes independent entropy from second chip (MCU UID)
uint8_t entropy[32];
uint32_t serial[4] = {0};
flash_read_unique_id(serial, 4);
sha256_Raw((uint8_t *)serial, sizeof(serial), entropy);
for (i = 0; i < sizeof(serial); i++) {
buf[i % len] ^= entropy[i % MEM_PAGE_LEN];
}
// Add ataes independent entropy from random bytes set during factory install
sha256_Raw((uint8_t *)(FLASH_BOOT_START), FLASH_BOOT_LEN, entropy);
sha256_Raw(entropy, sizeof(entropy), entropy);
sha256_Raw(entropy, sizeof(entropy), entropy);
for (i = 0; i < MEM_PAGE_LEN; i++) {
buf[i % len] ^= entropy[i % MEM_PAGE_LEN];
}
// Add ataes independent entropy from user (hashed device password)
memcpy(entropy, memory_report_user_entropy(), sizeof(entropy));
for (i = 0; i < MEM_PAGE_LEN; i++) {
buf[i % len] ^= entropy[i % MEM_PAGE_LEN];
}
#endif
#else
// Use standard libary for off chip RNG
(void) update_seed;
for (i = 0; i < len; i++) {
buf[i] = rand();
}
#endif
return DBB_OK;
}