-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_wrapper.c
229 lines (191 loc) · 5.55 KB
/
crypto_wrapper.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
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
#include <furi_hal.h>
#include <lib/mlib/m-dict.h>
#include <toolbox/sha256.h>
#ifndef FURI_HAL_CRYPTO_ADVANCED_AVAIL
#include "crypto/gcm.h"
#endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
#include "crypto_wrapper.h"
DICT_DEF2(ESubGhzChatReplayDict, uint64_t, uint32_t)
struct ESugGhzChatCryptoCtx {
uint8_t key[KEY_BITS / 8];
#ifndef FURI_HAL_CRYPTO_ADVANCED_AVAIL
gcm_context gcm_ctx;
#endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
ESubGhzChatReplayDict_t replay_dict;
uint64_t run_id;
uint32_t counter;
};
struct ESubGhzChatCryptoMsg {
uint64_t run_id;
uint32_t counter;
uint8_t iv[IV_BYTES];
uint8_t tag[TAG_BYTES];
uint8_t data[0];
} __attribute__ ((packed));
void crypto_init(void)
{
#ifndef FURI_HAL_CRYPTO_ADVANCED_AVAIL
/* init the GCM and AES tables */
gcm_initialize();
#endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
}
void crypto_explicit_bzero(void *s, size_t len)
{
memset(s, 0, len);
asm volatile("" ::: "memory");
}
ESubGhzChatCryptoCtx *crypto_ctx_alloc(void)
{
ESubGhzChatCryptoCtx *ret = malloc(sizeof(ESubGhzChatCryptoCtx));
if (ret != NULL) {
memset(ret, 0, sizeof(ESubGhzChatCryptoCtx));
ESubGhzChatReplayDict_init(ret->replay_dict);
ret->run_id = 0;
ret->counter = 1;
}
return ret;
}
void crypto_ctx_free(ESubGhzChatCryptoCtx *ctx)
{
crypto_ctx_clear(ctx);
ESubGhzChatReplayDict_clear(ctx->replay_dict);
free(ctx);
}
void crypto_ctx_clear(ESubGhzChatCryptoCtx *ctx)
{
crypto_explicit_bzero(ctx->key, sizeof(ctx->key));
#ifndef FURI_HAL_CRYPTO_ADVANCED_AVAIL
crypto_explicit_bzero(&(ctx->gcm_ctx), sizeof(ctx->gcm_ctx));
#endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
ESubGhzChatReplayDict_reset(ctx->replay_dict);
ctx->run_id = 0;
ctx->counter = 1;
}
static uint64_t crypto_calc_run_id(FuriString *flipper_name, uint32_t tick)
{
const char *fn = furi_string_get_cstr(flipper_name);
size_t fn_len = strlen(fn);
uint8_t h_in[fn_len + sizeof(uint32_t)];
memcpy(h_in, fn, fn_len);
memcpy(h_in + fn_len, &tick, sizeof(uint32_t));
uint8_t h_out[256];
sha256(h_in, fn_len + sizeof(uint32_t), h_out);
uint64_t run_id;
memcpy(&run_id, h_out, sizeof(uint64_t));
return run_id;
}
bool crypto_ctx_set_key(ESubGhzChatCryptoCtx *ctx, const uint8_t *key,
FuriString *flipper_name, uint32_t tick)
{
ctx->run_id = crypto_calc_run_id(flipper_name, tick);
ctx->counter = 1;
memcpy(ctx->key, key, KEY_BITS / 8);
#ifdef FURI_HAL_CRYPTO_ADVANCED_AVAIL
return true;
#else /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
return (gcm_setkey(&(ctx->gcm_ctx), key, KEY_BITS / 8) == 0);
#endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
}
void crypto_ctx_get_key(ESubGhzChatCryptoCtx *ctx, uint8_t *key)
{
memcpy(key, ctx->key, KEY_BITS / 8);
}
bool crypto_ctx_decrypt(ESubGhzChatCryptoCtx *ctx, uint8_t *in, size_t in_len,
uint8_t *out)
{
if (in_len < MSG_OVERHEAD + 1) {
return false;
}
struct ESubGhzChatCryptoMsg *msg = (struct ESubGhzChatCryptoMsg *) in;
// check if message is stale, if yes, discard
uint32_t *counter = ESubGhzChatReplayDict_get(ctx->replay_dict,
msg->run_id);
if (counter != NULL) {
if (*counter >= __ntohl(msg->counter)) {
return false;
}
}
// decrypt and auth message
#ifdef FURI_HAL_CRYPTO_ADVANCED_AVAIL
bool ret = (furi_hal_crypto_gcm_decrypt_and_verify(ctx->key,
msg->iv,
(uint8_t *) msg, RUN_ID_BYTES + COUNTER_BYTES,
msg->data, out,
in_len - MSG_OVERHEAD,
msg->tag) == FuriHalCryptoGCMStateOk);
#else /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
bool ret = (gcm_auth_decrypt(&(ctx->gcm_ctx),
msg->iv, IV_BYTES,
(uint8_t *) msg, RUN_ID_BYTES + COUNTER_BYTES,
msg->data, out,
in_len - MSG_OVERHEAD,
msg->tag, TAG_BYTES) == 0);
#endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
// if auth was successful update replay dict
if (ret) {
ESubGhzChatReplayDict_set_at(ctx->replay_dict, msg->run_id,
__ntohl(msg->counter));
}
return ret;
}
bool crypto_ctx_encrypt(ESubGhzChatCryptoCtx *ctx, uint8_t *in, size_t in_len,
uint8_t *out)
{
struct ESubGhzChatCryptoMsg *msg = (struct ESubGhzChatCryptoMsg *) out;
// fill message header
msg->run_id = ctx->run_id;
msg->counter = __htonl(ctx->counter);
furi_hal_random_fill_buf(msg->iv, IV_BYTES);
// encrypt message and store tag in header
#ifdef FURI_HAL_CRYPTO_ADVANCED_AVAIL
bool ret = (furi_hal_crypto_gcm_encrypt_and_tag(ctx->key,
msg->iv,
(uint8_t *) msg, RUN_ID_BYTES + COUNTER_BYTES,
in, msg->data,
in_len,
msg->tag) == FuriHalCryptoGCMStateOk);
#else /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
bool ret = (gcm_crypt_and_tag(&(ctx->gcm_ctx), ENCRYPT,
msg->iv, IV_BYTES,
(uint8_t *) msg, RUN_ID_BYTES + COUNTER_BYTES,
in, msg->data,
in_len,
msg->tag, TAG_BYTES) == 0);
#endif /* FURI_HAL_CRYPTO_ADVANCED_AVAIL */
// update replay dict and increase internal counter
if (ret) {
ESubGhzChatReplayDict_set_at(ctx->replay_dict, ctx->run_id,
ctx->counter);
ctx->counter++;
}
return ret;
}
size_t crypto_ctx_dump_replay_dict(ESubGhzChatCryptoCtx *ctx,
CryptoCtxReplayDictWriter writer, void *writer_ctx)
{
size_t ret = 0;
ESubGhzChatReplayDict_it_t i;
for (ESubGhzChatReplayDict_it(i, ctx->replay_dict);
!ESubGhzChatReplayDict_end_p(i);
ESubGhzChatReplayDict_next(i), ret++) {
ESubGhzChatReplayDict_itref_t *ref =
ESubGhzChatReplayDict_ref(i);
if (!writer(ref->key, ref->value, writer_ctx)) {
break;
}
}
return ret;
}
size_t crypto_ctx_read_replay_dict(ESubGhzChatCryptoCtx *ctx,
CryptoCtxReplayDictReader reader, void *reader_ctx)
{
size_t ret = 0;
uint64_t run_id;
uint32_t counter;
while (reader(&run_id, &counter, reader_ctx)) {
ESubGhzChatReplayDict_set_at(ctx->replay_dict, run_id,
counter);
ret++;
}
return ret;
}