forked from attermann/microReticulum_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNSIdentityBackup.h
More file actions
257 lines (221 loc) · 7.91 KB
/
Copy pathRNSIdentityBackup.h
File metadata and controls
257 lines (221 loc) · 7.91 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#pragma once
#if defined(HAS_RNS)
#include <string>
#define RNS_IDENTITY_KEY_SIZE 64
#define RNS_IDENTITY_HASH_SIZE 16
#define RNS_IDENTITY_BACKUP_ADDR 0x100
#define RNS_IDENTITY_BACKUP_MAGIC_0 0x52
#define RNS_IDENTITY_BACKUP_MAGIC_1 0x49
#define RNS_IDENTITY_BACKUP_MAGIC_2 0x44
#define RNS_IDENTITY_BACKUP_VERSION 0x01
#define RNS_IDENTITY_BACKUP_SIZE (4 + RNS_IDENTITY_KEY_SIZE + RNS_IDENTITY_HASH_SIZE + 1)
#define RNS_IDENTITY_FLAG_ACTIVE 0x01
#define RNS_IDENTITY_FLAG_BACKUP 0x02
#define RNS_IDENTITY_FLAG_MATCH 0x04
#if HAS_EEPROM && defined(EEPROM_SIZE) && (EEPROM_SIZE >= (RNS_IDENTITY_BACKUP_ADDR + RNS_IDENTITY_BACKUP_SIZE))
#define RNS_IDENTITY_BACKUP_AVAILABLE 1
#else
#define RNS_IDENTITY_BACKUP_AVAILABLE 0
#endif
std::string rns_identity_transport_path() {
return "./transport_identity";
}
bool rns_identity_load_key(RNS::Identity &identity, const RNS::Bytes &key) {
if (key.size() != RNS_IDENTITY_KEY_SIZE) return false;
return identity.load_private_key(key);
}
bool rns_identity_file_load(RNS::Identity &identity) {
#if defined(RNS_USE_FS)
identity = RNS::Identity::from_file(rns_identity_transport_path().c_str());
return (bool)identity;
#else
return false;
#endif
}
bool rns_identity_file_write(RNS::Identity identity) {
#if defined(RNS_USE_FS)
return identity.to_file(rns_identity_transport_path().c_str());
#else
return false;
#endif
}
bool rns_identity_backup_read(RNS::Bytes &key, RNS::Bytes *hash = nullptr) {
#if RNS_IDENTITY_BACKUP_AVAILABLE
uint8_t record[RNS_IDENTITY_BACKUP_SIZE];
for (uint16_t i = 0; i < RNS_IDENTITY_BACKUP_SIZE; i++) {
record[i] = EEPROM.read(RNS_IDENTITY_BACKUP_ADDR + i);
}
if (record[0] != RNS_IDENTITY_BACKUP_MAGIC_0 ||
record[1] != RNS_IDENTITY_BACKUP_MAGIC_1 ||
record[2] != RNS_IDENTITY_BACKUP_MAGIC_2 ||
record[3] != RNS_IDENTITY_BACKUP_VERSION) {
return false;
}
if (record[RNS_IDENTITY_BACKUP_SIZE - 1] != rns_config_crc(record, RNS_IDENTITY_BACKUP_SIZE - 1)) {
return false;
}
RNS::Bytes stored_key(record + 4, RNS_IDENTITY_KEY_SIZE);
RNS::Bytes stored_hash(record + 4 + RNS_IDENTITY_KEY_SIZE, RNS_IDENTITY_HASH_SIZE);
RNS::Identity identity(false);
if (!rns_identity_load_key(identity, stored_key)) return false;
if (identity.hash() != stored_hash) return false;
key = stored_key;
if (hash) *hash = stored_hash;
return true;
#else
return false;
#endif
}
bool rns_identity_backup_write_key(const RNS::Bytes &key) {
#if RNS_IDENTITY_BACKUP_AVAILABLE
RNS::Identity identity(false);
if (!rns_identity_load_key(identity, key)) return false;
uint8_t record[RNS_IDENTITY_BACKUP_SIZE];
memset(record, 0, sizeof(record));
record[0] = RNS_IDENTITY_BACKUP_MAGIC_0;
record[1] = RNS_IDENTITY_BACKUP_MAGIC_1;
record[2] = RNS_IDENTITY_BACKUP_MAGIC_2;
record[3] = RNS_IDENTITY_BACKUP_VERSION;
memcpy(record + 4, key.data(), RNS_IDENTITY_KEY_SIZE);
memcpy(record + 4 + RNS_IDENTITY_KEY_SIZE, identity.hash().data(), RNS_IDENTITY_HASH_SIZE);
record[RNS_IDENTITY_BACKUP_SIZE - 1] = rns_config_crc(record, RNS_IDENTITY_BACKUP_SIZE - 1);
for (uint16_t i = 0; i < RNS_IDENTITY_BACKUP_SIZE; i++) {
eeprom_update(RNS_IDENTITY_BACKUP_ADDR + i, record[i]);
}
return true;
#else
return false;
#endif
}
bool rns_identity_backup_write(const RNS::Identity &identity) {
if (!identity) return false;
return rns_identity_backup_write_key(identity.get_private_key());
}
bool rns_identity_restore_backup_to_transport(bool set_running_identity) {
RNS::Bytes key;
RNS::Bytes hash;
if (!rns_identity_backup_read(key, &hash)) return false;
RNS::Identity identity(false);
if (!rns_identity_load_key(identity, key)) return false;
if (!rns_identity_file_write(identity)) return false;
if (set_running_identity) {
RNS::Transport::identity(identity);
}
return true;
}
void rns_identity_prepare_transport_identity() {
RNS::Bytes backup_key;
RNS::Bytes backup_hash;
if (!rns_identity_backup_read(backup_key, &backup_hash)) return;
RNS::Identity file_identity(false);
if (rns_identity_file_load(file_identity) && file_identity.hash() == backup_hash) {
return;
}
RNS::Identity backup_identity(false);
if (rns_identity_load_key(backup_identity, backup_key) && rns_identity_file_write(backup_identity)) {
INFO("Restored RNS transport identity from EEPROM backup");
}
}
void rns_identity_finish_transport_identity() {
RNS::Identity active = RNS::Transport::identity();
if (!active) return;
RNS::Bytes backup_key;
RNS::Bytes backup_hash;
if (rns_identity_backup_read(backup_key, &backup_hash)) {
if (active.hash() != backup_hash) {
WARNING("RNS transport identity differed from EEPROM backup; using EEPROM backup");
rns_identity_restore_backup_to_transport(true);
}
return;
}
if (rns_identity_backup_write(active)) {
INFO("Backed up RNS transport identity to EEPROM");
}
}
void rns_identity_write_hash(const RNS::Bytes &hash) {
for (uint8_t i = 0; i < RNS_IDENTITY_HASH_SIZE; i++) {
escaped_serial_write(i < hash.size() ? hash[i] : 0x00);
}
}
void kiss_indicate_rns_identity_status() {
RNS::Identity active = RNS::Transport::identity();
RNS::Bytes backup_key;
RNS::Bytes backup_hash;
bool has_active = (bool)active;
bool has_backup = rns_identity_backup_read(backup_key, &backup_hash);
uint8_t flags = 0;
if (has_active) flags |= RNS_IDENTITY_FLAG_ACTIVE;
if (has_backup) flags |= RNS_IDENTITY_FLAG_BACKUP;
if (has_active && has_backup && active.hash() == backup_hash) flags |= RNS_IDENTITY_FLAG_MATCH;
serial_write(FEND);
serial_write(CMD_RNS_IDENTITY);
serial_write(RNS_IDENTITY_OP_STATUS);
serial_write(flags);
if (has_active) rns_identity_write_hash(active.hash());
else for (uint8_t i = 0; i < RNS_IDENTITY_HASH_SIZE; i++) serial_write(0x00);
if (has_backup) rns_identity_write_hash(backup_hash);
else for (uint8_t i = 0; i < RNS_IDENTITY_HASH_SIZE; i++) serial_write(0x00);
serial_write(FEND);
}
void kiss_indicate_rns_identity_key(uint8_t op, const RNS::Bytes &key) {
serial_write(FEND);
serial_write(CMD_RNS_IDENTITY);
serial_write(op);
for (uint8_t i = 0; i < key.size(); i++) {
escaped_serial_write(key[i]);
}
serial_write(FEND);
}
void kiss_indicate_rns_identity_hash(uint8_t op, const RNS::Bytes &hash) {
serial_write(FEND);
serial_write(CMD_RNS_IDENTITY);
serial_write(op);
rns_identity_write_hash(hash);
serial_write(FEND);
}
bool rns_identity_import_key(const uint8_t *data, uint8_t len) {
if (len != RNS_IDENTITY_KEY_SIZE) return false;
RNS::Bytes key(data, len);
RNS::Identity identity(false);
if (!rns_identity_load_key(identity, key)) return false;
if (!rns_identity_backup_write_key(key)) return false;
if (!rns_identity_file_write(identity)) return false;
RNS::Transport::identity(identity);
return true;
}
void handle_rns_identity_command(uint8_t *payload, uint8_t len) {
if (len < 1) {
kiss_indicate_error(ERROR_INVALID_CFG);
return;
}
uint8_t op = payload[0];
if (op == RNS_IDENTITY_OP_STATUS) {
kiss_indicate_rns_identity_status();
} else if (op == RNS_IDENTITY_OP_EXPORT) {
RNS::Identity active = RNS::Transport::identity();
if (!active) kiss_indicate_error(ERROR_INVALID_CFG);
else kiss_indicate_rns_identity_key(RNS_IDENTITY_OP_EXPORT, active.get_private_key());
} else if (op == RNS_IDENTITY_OP_IMPORT) {
if (!rns_identity_import_key(payload + 1, len - 1)) {
kiss_indicate_error(ERROR_INVALID_CFG);
return;
}
kiss_indicate_rns_identity_hash(RNS_IDENTITY_OP_IMPORT, RNS::Transport::identity().hash());
} else if (op == RNS_IDENTITY_OP_BACKUP) {
RNS::Identity active = RNS::Transport::identity();
if (!active || !rns_identity_backup_write(active)) {
kiss_indicate_error(ERROR_INVALID_CFG);
return;
}
kiss_indicate_rns_identity_hash(RNS_IDENTITY_OP_BACKUP, active.hash());
} else if (op == RNS_IDENTITY_OP_RESTORE) {
if (!rns_identity_restore_backup_to_transport(true)) {
kiss_indicate_error(ERROR_INVALID_CFG);
return;
}
kiss_indicate_rns_identity_hash(RNS_IDENTITY_OP_RESTORE, RNS::Transport::identity().hash());
} else {
kiss_indicate_error(ERROR_INVALID_CFG);
}
}
#endif