-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtlv8.c
545 lines (497 loc) · 16.7 KB
/
tlv8.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
* A TLV utility for esp32.
*
* Copyright (c) 2017 Emmanuel Merali
* https://github.com/ifullgaz/esp32-tlv8
*
* The MIT License (MIT)
*
* 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 <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#include "esp32-tlv8/tlv8.h"
#include "esp_log.h"
static const char *TAG = "ESP32-TLV8";
#define min(a,b) ((a) < (b) ? (a) : (b))
#define TLV8_MAX_DATA_LEN 255
struct _tlv8 {
uint8_t type;
uint32_t len;
struct {
TLV8_DATA_TYPE type;
union {
uint64_t uint64;
buffer_t data;
mbedtls_mpi *mpi;
};
} data;
};
struct _tlv8_encoder {
uint8_t type;
buffer_t data;
};
struct _tlv8_decoder {
uint8_t type;
buffer_t buffer;
int len;
int pos;
const unsigned char *data;
};
/***********************************************************************************************************
* TLV8
***********************************************************************************************************
* Private interface
***********************************************************************************************************/
static int tlv8_integer_len(uint64_t integer) {
int len = 0;
while (integer) {
len++;
integer = integer >> 8;
}
len-= (integer);
return len;
}
static tlv8_t tlv8_new(uint8_t type) {
tlv8_t tlv = (tlv8_t)malloc(sizeof(struct _tlv8));
if (tlv) {
memset(tlv, 0, sizeof(struct _tlv8));
tlv->type = type;
}
return tlv;
}
/***********************************************************************************************************
* Public interface
***********************************************************************************************************/
tlv8_t tlv8_new_separator(uint8_t type) {
tlv8_t tlv = tlv8_new(type);
if (tlv) {
tlv->data.type = TLV8_DATA_TYPE_SEPARATOR;
}
return tlv;
}
tlv8_t tlv8_new_with_integer(uint8_t type, uint64_t integer) {
tlv8_t tlv = tlv8_new(type);
if (tlv) {
tlv->data.type = TLV8_DATA_TYPE_INTEGER;
tlv->data.uint64 = integer;
tlv->len = tlv8_integer_len(integer);
}
return tlv;
}
tlv8_t tlv8_new_with_data(uint8_t type, void *data, int data_len) {
if (!data || !data_len) {
return NULL;
}
tlv8_t tlv = tlv8_new(type);
tlv->data.type = TLV8_DATA_TYPE_BYTES;
if (tlv && data_len) {
tlv->data.data = buffer_new(data_len);
if (!tlv->data.data) {
tlv8_free(tlv);
return NULL;
}
buffer_append(tlv->data.data, data, data_len);
tlv->len = data_len;
}
return tlv;
}
tlv8_t tlv8_new_with_string(uint8_t type, const char *string) {
if (!string) {
return NULL;
}
int str_len = strlen(string);
tlv8_t tlv = tlv8_new_with_data(type, (void *)string, str_len);
if (tlv) {
tlv->data.type = TLV8_DATA_TYPE_STRING;
}
return tlv;
}
tlv8_t tlv8_new_with_buffer(uint8_t type, buffer_t data) {
if (!data) {
return NULL;
}
return tlv8_new_with_data(type, (void *)buffer_get_data(data), buffer_get_length(data));
}
// Create a new TLV8 structure from mpi (type TLV8_DATA_TYPE_MPI)
tlv8_t tlv8_new_with_mpi(uint8_t type, mbedtls_mpi *mpi) {
mbedtls_mpi *cpy = utils_mpi_new();
if (!cpy) {
return NULL;
}
if (mbedtls_mpi_copy(cpy, mpi)) {
mbedtls_mpi_free(cpy);
return NULL;
}
tlv8_t tlv = tlv8_new(type);
if (tlv) {
tlv->data.type = TLV8_DATA_TYPE_MPI;
tlv->data.mpi = cpy;
tlv->len = mbedtls_mpi_size(tlv->data.mpi);
}
return tlv;
}
void tlv8_free(void *t) {
tlv8_t tlv = (tlv8_t)t;
if (tlv) {
if (tlv->data.type == TLV8_DATA_TYPE_MPI) {
mbedtls_mpi_free(tlv->data.mpi);
}
else if (tlv->data.type != TLV8_DATA_TYPE_SEPARATOR && tlv->data.type != TLV8_DATA_TYPE_INTEGER) {
buffer_free(tlv->data.data);
}
free(t);
}
}
uint8_t tlv8_get_type(tlv8_t tlv) {
return tlv->type;
}
uint64_t tlv8_get_integer_value(tlv8_t tlv) {
return tlv->data.uint64;
}
const char *tlv8_get_string_value(tlv8_t tlv) {
return (const char *)buffer_get_data(tlv->data.data);
}
buffer_t tlv8_get_data_value(tlv8_t tlv) {
return (buffer_t)tlv->data.data;
}
mbedtls_mpi *tlv8_get_mpi_value(tlv8_t tlv) {
return tlv->data.mpi;
}
/***********************************************************************************************************
* TLV Encoder
***********************************************************************************************************
* Private interface
***********************************************************************************************************/
static int tlv8_encoded_size(int len) {
int num_fragments = ceilf((float)len / (float)TLV8_MAX_DATA_LEN);
int size = (num_fragments << 1) + len; // 2 bytes * num_fragments + tlv->len - Always 1 for type + 1 for size
return size;
}
static void tlv8_encoder_write_buffer_separator(tlv8_encoder_t codec, tlv8_t tlv) {
buffer_append(codec->data, &(tlv->type), 1);
}
static void tlv8_encoder_write_buffer_integer(tlv8_encoder_t codec, tlv8_t tlv) {
uint8_t len = tlv->len;
uint64_t integer = tlv->data.uint64;
buffer_append(codec->data, &(tlv->type), 1);
buffer_append(codec->data, &len, 1);
while (len > 0) {
// Little endian
unsigned char val = (unsigned char)integer;
buffer_append(codec->data, &val, 1);
integer = integer >> 8;
len--;
}
}
static void tlv8_encoder_write_buffer_data(tlv8_encoder_t codec, tlv8_t tlv) {
int len = tlv->len;
const unsigned char *data = buffer_get_data(tlv->data.data);
int offset = 0;
do {
uint8_t size = min(len, TLV8_MAX_DATA_LEN);
buffer_append(codec->data, &(tlv->type), 1);
buffer_append(codec->data, &size, 1);
buffer_append(codec->data, (data + offset), size);
offset+= TLV8_MAX_DATA_LEN;
len-= TLV8_MAX_DATA_LEN;
} while (len > 0);
}
static void tlv8_encoder_write_buffer_mpi(tlv8_encoder_t codec, tlv8_t tlv) {
int len = tlv->len;
unsigned char bin[len];
memset(bin, 0, len);
mbedtls_mpi_write_binary(tlv->data.mpi, bin, len);
int offset = 0;
do {
uint8_t size = min(len, TLV8_MAX_DATA_LEN);
buffer_append(codec->data, &(tlv->type), 1);
buffer_append(codec->data, &size, 1);
buffer_append(codec->data, (bin + offset), size);
offset+= TLV8_MAX_DATA_LEN;
len-= TLV8_MAX_DATA_LEN;
} while (len > 0);
}
static void tlv8_encoder_write_buffer(tlv8_encoder_t codec, tlv8_t tlv) {
switch (tlv->data.type) {
case TLV8_DATA_TYPE_SEPARATOR:
tlv8_encoder_write_buffer_separator(codec, tlv); break;
case TLV8_DATA_TYPE_INTEGER:
tlv8_encoder_write_buffer_integer(codec, tlv); break;
case TLV8_DATA_TYPE_STRING:
case TLV8_DATA_TYPE_BYTES:
tlv8_encoder_write_buffer_data(codec, tlv); break;
case TLV8_DATA_TYPE_MPI:
tlv8_encoder_write_buffer_mpi(codec, tlv); break;
default:
break;
}
}
/***********************************************************************************************************
* Public interface
***********************************************************************************************************/
tlv8_encoder_t tlv8_encoder_new(buffer_t buffer) {
tlv8_encoder_t codec = (tlv8_encoder_t)malloc(sizeof(struct _tlv8_encoder));
if (codec) {
memset(codec, 0, sizeof(struct _tlv8_encoder));
codec->data = buffer;
}
return codec;
}
int tlv8_encoder_encode(tlv8_encoder_t codec, tlv8_t tlv) {
int size = tlv8_encoded_size(tlv->len);
if (!codec->data) {
codec->data = buffer_new(size);
if (!codec->data) {
return TLV8_ERR_ALLOC_FAILED;
}
}
else if (tlv->type == codec->type) {
// Should not encode 2 consecutive TLVs with the same type
return TLV8_ERR_TYPE_FORBIDDEN;
}
else if (buffer_ensure_available(codec->data, size) != UTILS_ERR_OK) {
return TLV8_ERR_ALLOC_FAILED;
}
tlv8_encoder_write_buffer(codec, tlv);
codec->type = tlv->type;
return TLV8_ERR_OK;
}
// Get data buffer
buffer_t tlv8_encoder_get_data(tlv8_encoder_t codec) {
return codec->data;
}
// Detach data buffer
buffer_t tlv8_encoder_detach_data(tlv8_encoder_t codec) {
buffer_t data = codec->data;
codec->data = NULL;
return data;
}
// Cleanup
void tlv8_encoder_free(void *c) {
tlv8_encoder_t codec = (tlv8_encoder_t)c;
if (codec) {
buffer_free(codec->data);
free(c);
}
}
/***********************************************************************************************************
* TLV Decoder
***********************************************************************************************************
* Private interface
***********************************************************************************************************/
static uint8_t tlv8_decoder_get_type_and_advance(tlv8_decoder_t codec) {
return codec->data[codec->pos++];
}
static uint8_t tlv8_decoder_get_size_and_advance(tlv8_decoder_t codec) {
return codec->data[codec->pos++];
}
static int tlv8_decoder_data_size(tlv8_decoder_t codec) {
int size = 0;
int start_pos = codec->pos;
do {
uint8_t type = tlv8_decoder_get_type_and_advance(codec);
if (type != codec->type) {
goto cleanup;
}
int len = tlv8_decoder_get_size_and_advance(codec);
size+= len;
// Jump to next TLV if any
codec->pos+= len;
} while (tlv8_decoder_has_next(codec));
cleanup:
// Restore initial pointer to type of TLV
codec->pos = start_pos;
return size;
}
static tlv8_t tlv8_decoder_next_tlv_separator(tlv8_decoder_t codec) {
tlv8_decoder_get_type_and_advance(codec);
return tlv8_new_separator(codec->type);
}
static tlv8_t tlv8_decoder_next_tlv_integer(tlv8_decoder_t codec) {
uint64_t integer = 0;
tlv8_decoder_get_type_and_advance(codec);
int size = tlv8_decoder_get_size_and_advance(codec);
for (int i = 0; i < size; i++) {
uint8_t byte = codec->data[codec->pos++];
integer = integer | ((uint64_t)byte << (8 * i));
}
return tlv8_new_with_integer(codec->type, integer);
}
static tlv8_t tlv8_decoder_next_tlv_data(tlv8_decoder_t codec) {
tlv8_t tlv = NULL;
int size = tlv8_decoder_data_size(codec);
int num_fragments = ceilf((float)size / (float)TLV8_MAX_DATA_LEN);
buffer_t data = buffer_new(size);
for (int i = 0; i < num_fragments; i++) {
tlv8_decoder_get_type_and_advance(codec);
uint8_t len = tlv8_decoder_get_size_and_advance(codec);
buffer_append(data, codec->data + codec->pos, len);
codec->pos+= len;
};
tlv = tlv8_new(codec->type);
tlv->data.type = TLV8_DATA_TYPE_BYTES;
tlv->data.data = data;
tlv->len = size;
return tlv;
}
static tlv8_t tlv8_decoder_next_tlv_string(tlv8_decoder_t codec) {
tlv8_t tlv = tlv8_decoder_next_tlv_data(codec);
if (tlv) {
tlv->data.type = TLV8_DATA_TYPE_STRING;
}
return tlv;
}
static tlv8_t tlv8_decoder_next_tlv_mpi(tlv8_decoder_t codec) {
tlv8_t tlv = NULL;
int size = tlv8_decoder_data_size(codec);
int num_fragments = ceilf((float)size / (float)TLV8_MAX_DATA_LEN);
char data[size];
int offset = 0;
for (int i = 0; i < num_fragments; i++) {
tlv8_decoder_get_type_and_advance(codec);
uint8_t len = tlv8_decoder_get_size_and_advance(codec);
memcpy(data + offset, codec->data + codec->pos, len);
offset+= len;
codec->pos+= len;
};
mbedtls_mpi *mpi = utils_mpi_new();
if (!mpi) {
return NULL;
}
if (mbedtls_mpi_read_binary(mpi, (const unsigned char *)data, size)) {
mbedtls_mpi_free(mpi);
return NULL;
}
tlv = tlv8_new(codec->type);
tlv->data.type = TLV8_DATA_TYPE_MPI;
tlv->data.mpi = mpi;
tlv->len = size;
return tlv;
}
/***********************************************************************************************************
* Public interface
***********************************************************************************************************/
tlv8_decoder_t tlv8_decoder_new(buffer_t data) {
if (!data) {
return NULL;
}
tlv8_decoder_t codec = (tlv8_decoder_t)malloc(sizeof(struct _tlv8_decoder));
if (codec) {
memset(codec, 0, sizeof(struct _tlv8_decoder));
codec->buffer = data;
codec->data = (const unsigned char *)buffer_get_data(data);
codec->len = buffer_get_length(data);
}
return codec;
}
// Detach data buffer
buffer_t tlv8_decoder_detach_data(tlv8_decoder_t codec) {
buffer_t data = codec->buffer;
codec->buffer = NULL;
return data;
}
int tlv8_decoder_has_next(tlv8_decoder_t codec) {
return codec->pos < codec->len;
}
uint8_t tlv8_decoder_peek_type(tlv8_decoder_t codec) {
codec->type = codec->data[codec->pos];
return codec->type;
}
tlv8_t tlv8_decoder_decode(tlv8_decoder_t codec, TLV8_DATA_TYPE type) {
if (!tlv8_decoder_has_next(codec)) {
return NULL;
}
switch (type) {
case TLV8_DATA_TYPE_SEPARATOR:
return tlv8_decoder_next_tlv_separator(codec);
case TLV8_DATA_TYPE_INTEGER:
return tlv8_decoder_next_tlv_integer(codec);
case TLV8_DATA_TYPE_STRING:
return tlv8_decoder_next_tlv_string(codec);
case TLV8_DATA_TYPE_BYTES:
return tlv8_decoder_next_tlv_data(codec);
case TLV8_DATA_TYPE_MPI:
return tlv8_decoder_next_tlv_mpi(codec);
default:
// Nothing to return, we don't know that type
return NULL;
}
}
void tlv8_decoder_free(void *c) {
tlv8_decoder_t codec = (tlv8_decoder_t)c;
if (codec) {
buffer_free(codec->buffer);
free(c);
}
}
/***********************************************************************************************************
* Convenience methods
***********************************************************************************************************/
buffer_t tlv8_encode(const array_t array) {
return tlv8_encode_array(array);
}
buffer_t tlv8_encode_array(const array_t array) {
tlv8_encoder_t codec = tlv8_encoder_new(NULL);
for (int i = 0; i < array_count(array); i++) {
tlv8_t tlv = (tlv8_t)array_at(array, i);
tlv8_encoder_encode(codec, tlv);
}
return tlv8_encoder_detach_data(codec);
}
buffer_t tlv8_encode_list(int count, ...) {
va_list list;
va_start(list, count);
tlv8_encoder_t codec = tlv8_encoder_new(NULL);
for (int i = 0; i < count; i++) {
tlv8_t tlv = va_arg(list, tlv8_t);
tlv8_encoder_encode(codec, tlv);
tlv8_free(tlv);
}
va_end(list);
return tlv8_encoder_detach_data(codec);
}
array_t tlv8_decode(const buffer_t buffer, const TLV8_DATA_TYPE *mapping) {
array_t array = array_new(tlv8_free);
tlv8_decoder_t decoder = tlv8_decoder_new(buffer);
while (tlv8_decoder_has_next(decoder)) {
uint8_t type = tlv8_decoder_peek_type(decoder);
TLV8_DATA_TYPE data_type = mapping[type];
tlv8_t tlv = tlv8_decoder_decode(decoder, data_type);
if (tlv) {
array_push(array, tlv);
}
}
tlv8_decoder_detach_data(decoder);
tlv8_decoder_free(decoder);
return array;
}
tlv8_t tlv8_tlv_of_type(array_t tlvs, uint8_t type) {
for (int i = 0; i < array_count(tlvs); i++) {
tlv8_t tlv = array_at(tlvs, i);
if (tlv8_get_type(tlv) == type) {
return tlv;
}
}
return NULL;
}