1#!/usr/bin/python3 2 3# This script converts the testvectors form a JSON format to C source and header file. 4 5import re 6import json 7import binascii 8 9in_out_path = "../test_vectors/" 10 11HEADER = "/*This is an automatically generated file!*/\n\n" 12INCLUDES = '#include "edhoc.h"\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n\n' 13STRUCT_DEF = "\n\ 14struct test_vector {\n\ 15 const uint8_t *method;\n\ 16 uint32_t method_len;\n\ 17 \n\ 18 const uint8_t *suites_i;\n\ 19 uint32_t suites_i_len;\n\ 20 \n\ 21 const uint8_t *suites_r;\n\ 22 uint32_t suites_r_len;\n\ 23 \n\ 24 const uint8_t *x_raw;\n\ 25 uint32_t x_raw_len;\n\ 26 \n\ 27 const uint8_t *g_x_raw;\n\ 28 uint32_t g_x_raw_len;\n\ 29 \n\ 30 const uint8_t *g_x;\n\ 31 uint32_t g_x_len;\n\ 32 \n\ 33 const uint8_t *c_i;\n\ 34 uint32_t c_i_len;\n\ 35 \n\ 36 const uint8_t *ead_1;\n\ 37 uint32_t ead_1_len;\n\ 38 \n\ 39 const uint8_t *message_1;\n\ 40 uint32_t message_1_len;\n\ 41 \n\ 42 const uint8_t *y_raw;\n\ 43 uint32_t y_raw_len;\n\ 44 \n\ 45 const uint8_t *g_y_raw;\n\ 46 uint32_t g_y_raw_len;\n\ 47 \n\ 48 const uint8_t *g_y;\n\ 49 uint32_t g_y_len;\n\ 50 \n\ 51 const uint8_t *g_xy_raw;\n\ 52 uint32_t g_xy_raw_len;\n\ 53 \n\ 54 const uint8_t *salt_raw;\n\ 55 uint32_t salt_raw_len;\n\ 56 \n\ 57 const uint8_t *prk_2e_raw;\n\ 58 uint32_t prk_2e_raw_len;\n\ 59 \n\ 60 const uint8_t *sk_r_raw;\n\ 61 uint32_t sk_r_raw_len;\n\ 62 \n\ 63 const uint8_t *pk_r_raw;\n\ 64 uint32_t pk_r_raw_len;\n\ 65 \n\ 66 const uint8_t *prk_3e2m_raw;\n\ 67 uint32_t prk_3e2m_raw_len;\n\ 68 \n\ 69 const uint8_t *c_r;\n\ 70 uint32_t c_r_len;\n\ 71 \n\ 72 const uint8_t *h_message_1_raw;\n\ 73 uint32_t h_message_1_raw_len;\n\ 74 \n\ 75 const uint8_t *h_message_1;\n\ 76 uint32_t h_message_1_len;\n\ 77 \n\ 78 const uint8_t *input_th_2;\n\ 79 uint32_t input_th_2_len;\n\ 80 \n\ 81 const uint8_t *th_2_raw;\n\ 82 uint32_t th_2_raw_len;\n\ 83 \n\ 84 const uint8_t *th_2;\n\ 85 uint32_t th_2_len;\n\ 86 \n\ 87 const uint8_t *id_cred_r;\n\ 88 uint32_t id_cred_r_len;\n\ 89 \n\ 90 const uint8_t *cred_r;\n\ 91 uint32_t cred_r_len;\n\ 92 \n\ 93 const uint8_t *ead_2;\n\ 94 uint32_t ead_2_len;\n\ 95 \n\ 96 const uint8_t *info_mac_2;\n\ 97 uint32_t info_mac_2_len;\n\ 98 \n\ 99 const uint8_t *mac_2_raw;\n\ 100 uint32_t mac_2_raw_len;\n\ 101 \n\ 102 const uint8_t *mac_2;\n\ 103 uint32_t mac_2_len;\n\ 104 \n\ 105 const uint8_t *m_2;\n\ 106 uint32_t m_2_len;\n\ 107 \n\ 108 const uint8_t *sig_or_mac_2_raw;\n\ 109 uint32_t sig_or_mac_2_raw_len;\n\ 110 \n\ 111 const uint8_t *sig_or_mac_2;\n\ 112 uint32_t sig_or_mac_2_len;\n\ 113 \n\ 114 const uint8_t *plaintext_2;\n\ 115 uint32_t plaintext_2_len;\n\ 116 \n\ 117 const uint8_t *info_keystream_2;\n\ 118 uint32_t info_keystream_2_len;\n\ 119 \n\ 120 const uint8_t *keystream_2_raw;\n\ 121 uint32_t keystream_2_raw_len;\n\ 122 \n\ 123 const uint8_t *ciphertext_2_raw;\n\ 124 uint32_t ciphertext_2_raw_len;\n\ 125 \n\ 126 const uint8_t *ciphertext_2;\n\ 127 uint32_t ciphertext_2_len;\n\ 128 \n\ 129 const uint8_t *message_2;\n\ 130 uint32_t message_2_len;\n\ 131 \n\ 132 const uint8_t *sk_i_raw;\n\ 133 uint32_t sk_i_raw_len;\n\ 134 \n\ 135 const uint8_t *pk_i_raw;\n\ 136 uint32_t pk_i_raw_len;\n\ 137 \n\ 138 const uint8_t *prk_4x3m_raw;\n\ 139 uint32_t prk_4x3m_raw_len;\n\ 140 \n\ 141 const uint8_t *input_TH_3;\n\ 142 uint32_t input_TH_3_len;\n\ 143 \n\ 144 const uint8_t *th_3_raw;\n\ 145 uint32_t th_3_raw_len;\n\ 146 \n\ 147 const uint8_t *th_3;\n\ 148 uint32_t th_3_len;\n\ 149 \n\ 150 const uint8_t *id_cred_i;\n\ 151 uint32_t id_cred_i_len;\n\ 152 \n\ 153 const uint8_t *cred_i;\n\ 154 uint32_t cred_i_len;\n\ 155 \n\ 156 const uint8_t *ead_3;\n\ 157 uint32_t ead_3_len;\n\ 158 \n\ 159 const uint8_t *info_mac_3;\n\ 160 uint32_t info_mac_3_len;\n\ 161 \n\ 162 const uint8_t *mac_3_raw;\n\ 163 uint32_t mac_3_raw_len;\n\ 164 \n\ 165 const uint8_t *mac_3;\n\ 166 uint32_t mac_3_len;\n\ 167 \n\ 168 const uint8_t *m_3;\n\ 169 uint32_t m_3_len;\n\ 170 \n\ 171 const uint8_t *sig_or_mac_3_raw;\n\ 172 uint32_t sig_or_mac_3_raw_len;\n\ 173 \n\ 174 const uint8_t *sig_or_mac_3;\n\ 175 uint32_t sig_or_mac_3_len;\n\ 176 \n\ 177 const uint8_t *p_3;\n\ 178 uint32_t p_3_len;\n\ 179 \n\ 180 const uint8_t *a_3;\n\ 181 uint32_t a_3_len;\n\ 182 \n\ 183 const uint8_t *info_k_3;\n\ 184 uint32_t info_k_3_len;\n\ 185 \n\ 186 const uint8_t *k_3_raw;\n\ 187 uint32_t k_3_raw_len;\n\ 188 \n\ 189 const uint8_t *info_iv_3;\n\ 190 uint32_t info_iv_3_len;\n\ 191 \n\ 192 const uint8_t *iv_3_raw;\n\ 193 uint32_t iv_3_raw_len;\n\ 194 \n\ 195 const uint8_t *ciphertext_3_raw;\n\ 196 uint32_t ciphertext_3_raw_len;\n\ 197 \n\ 198 const uint8_t *ciphertext_3;\n\ 199 uint32_t ciphertext_3_len;\n\ 200 \n\ 201 const uint8_t *message_3;\n\ 202 uint32_t message_3_len;\n\ 203 \n\ 204 const uint8_t *input_th_4;\n\ 205 uint32_t input_th_4_len;\n\ 206 \n\ 207 const uint8_t *th_4_raw;\n\ 208 uint32_t th_4_raw_len;\n\ 209 \n\ 210 const uint8_t *th_4;\n\ 211 uint32_t th_4_len;\n\ 212 \n\ 213 const uint8_t *ead_4;\n\ 214 uint32_t ead_4_len;\n\ 215 \n\ 216 const uint8_t *p_4;\n\ 217 uint32_t p_4_len;\n\ 218 \n\ 219 const uint8_t *a_4;\n\ 220 uint32_t a_4_len;\n\ 221 \n\ 222 const uint8_t *info_k_4;\n\ 223 uint32_t info_k_4_len;\n\ 224 \n\ 225 const uint8_t *k_4_raw;\n\ 226 uint32_t k_4_raw_len;\n\ 227 \n\ 228 const uint8_t *info_iv_4;\n\ 229 uint32_t info_iv_4_len;\n\ 230 \n\ 231 const uint8_t *iv_4_raw;\n\ 232 uint32_t iv_4_raw_len;\n\ 233 \n\ 234 const uint8_t *ciphertext_4_raw;\n\ 235 uint32_t ciphertext_4_raw_len;\n\ 236 \n\ 237 const uint8_t *ciphertext_4;\n\ 238 uint32_t ciphertext_4_len;\n\ 239 \n\ 240 const uint8_t *message_4;\n\ 241 uint32_t message_4_len;\n\ 242 \n\ 243 int32_t *oscore_aead_alg;\n\ 244 \n\ 245 int32_t *oscore_hash_alg;\n\ 246 \n\ 247 const uint8_t *client_sender_id_raw;\n\ 248 uint32_t client_sender_id_raw_len;\n\ 249 \n\ 250 const uint8_t *server_sender_id_raw;\n\ 251 uint32_t server_sender_id_raw_len;\n\ 252 \n\ 253 const uint8_t *info_oscore_secret;\n\ 254 uint32_t info_oscore_secret_len;\n\ 255 \n\ 256 const uint8_t *oscore_secret_raw;\n\ 257 uint32_t oscore_secret_raw_len;\n\ 258 \n\ 259 const uint8_t *info_oscore_salt;\n\ 260 uint32_t info_oscore_salt_len;\n\ 261 \n\ 262 const uint8_t *oscore_salt_raw;\n\ 263 uint32_t oscore_salt_raw_len;\n\ 264 \n\ 265 const uint8_t *key_update_nonce_raw;\n\ 266 uint32_t key_update_nonce_raw_len;\n\ 267 \n\ 268 const uint8_t *prk_4x3m_key_update_raw;\n\ 269 uint32_t prk_4x3m_key_update_raw_len;\n\ 270 \n\ 271 const uint8_t *oscore_secret_key_update_raw;\n\ 272 uint32_t oscore_secret_key_update_raw_len;\n\ 273 \n\ 274 const uint8_t *oscore_salt_key_update_raw;\n\ 275 uint32_t oscore_salt_key_update_raw_len;\n\ 276 \n\ 277 const uint8_t *ca;\n\ 278 uint32_t ca_len;\n\ 279 \n\ 280 const uint8_t *ca_pk;\n\ 281 uint32_t ca_pk_len;\n\ 282 \n\ 283 const uint8_t *i_raw;\n\ 284 uint32_t i_raw_len;\n\ 285 \n\ 286 const uint8_t *g_i_raw;\n\ 287 uint32_t g_i_raw_len;\n\ 288 \n\ 289 const uint8_t *r_raw;\n\ 290 uint32_t r_raw_len;\n\ 291 \n\ 292 const uint8_t *g_r_raw;\n\ 293 uint32_t g_r_raw_len;\n\ 294 \n\ 295 const uint8_t *c_i_raw;\n\ 296 uint32_t c_i_raw_len;\n\ 297 int32_t *c_i_raw_int;\n\ 298 \n\ 299 const uint8_t *c_r_raw;\n\ 300 uint32_t c_r_raw_len;\n\ 301 int32_t *c_r_raw_int;\n\ 302};\n" 303 304STRUCT_POPULATE_START = "\n\nstruct test_vector test_vectors[] = {\n" 305STRUCT_POPULATE = "\n\ 306 {\n\ 307 .method = NULL,\n\ 308 .method_len = 0,\n\ 309 \n\ 310 .suites_i = NULL,\n\ 311 .suites_i_len = 0,\n\ 312 \n\ 313 .suites_r = NULL,\n\ 314 .suites_r_len = 0,\n\ 315 \n\ 316 .x_raw = NULL,\n\ 317 .x_raw_len = 0,\n\ 318 \n\ 319 .g_x_raw = NULL,\n\ 320 .g_x_raw_len = 0,\n\ 321 \n\ 322 .g_x = NULL,\n\ 323 .g_x_len = 0,\n\ 324 \n\ 325 .c_i = NULL,\n\ 326 .c_i_len = 0,\n\ 327 \n\ 328 .ead_1 = NULL,\n\ 329 .ead_1_len = 0,\n\ 330 \n\ 331 .message_1 = NULL,\n\ 332 .message_1_len = 0,\n\ 333 \n\ 334 .y_raw = NULL,\n\ 335 .y_raw_len = 0,\n\ 336 \n\ 337 .g_y_raw = NULL,\n\ 338 .g_y_raw_len = 0,\n\ 339 \n\ 340 .g_y = NULL,\n\ 341 .g_y_len = 0,\n\ 342 \n\ 343 .g_xy_raw = NULL,\n\ 344 .g_xy_raw_len = 0,\n\ 345 \n\ 346 .salt_raw = NULL,\n\ 347 .salt_raw_len = 0,\n\ 348 \n\ 349 .prk_2e_raw = NULL,\n\ 350 .prk_2e_raw_len = 0,\n\ 351 \n\ 352 .sk_r_raw = NULL,\n\ 353 .sk_r_raw_len = 0,\n\ 354 \n\ 355 .pk_r_raw = NULL,\n\ 356 .pk_r_raw_len = 0,\n\ 357 \n\ 358 .prk_3e2m_raw = NULL,\n\ 359 .prk_3e2m_raw_len = 0,\n\ 360 \n\ 361 .c_r = NULL,\n\ 362 .c_r_len = 0,\n\ 363 \n\ 364 .h_message_1_raw = NULL,\n\ 365 .h_message_1_raw_len = 0,\n\ 366 \n\ 367 .h_message_1 = NULL,\n\ 368 .h_message_1_len = 0,\n\ 369 \n\ 370 .input_th_2 = NULL,\n\ 371 .input_th_2_len = 0,\n\ 372 \n\ 373 .th_2_raw = NULL,\n\ 374 .th_2_raw_len = 0,\n\ 375 \n\ 376 .th_2 = NULL,\n\ 377 .th_2_len = 0,\n\ 378 \n\ 379 .id_cred_r = NULL,\n\ 380 .id_cred_r_len = 0,\n\ 381 \n\ 382 .cred_r = NULL,\n\ 383 .cred_r_len = 0,\n\ 384 \n\ 385 .ead_2 = NULL,\n\ 386 .ead_2_len = 0,\n\ 387 \n\ 388 .info_mac_2 = NULL,\n\ 389 .info_mac_2_len = 0,\n\ 390 \n\ 391 .mac_2_raw = NULL,\n\ 392 .mac_2_raw_len = 0,\n\ 393 \n\ 394 .mac_2 = NULL,\n\ 395 .mac_2_len = 0,\n\ 396 \n\ 397 .m_2 = NULL,\n\ 398 .m_2_len = 0,\n\ 399 \n\ 400 .sig_or_mac_2_raw = NULL,\n\ 401 .sig_or_mac_2_raw_len = 0,\n\ 402 \n\ 403 .sig_or_mac_2 = NULL,\n\ 404 .sig_or_mac_2_len = 0,\n\ 405 \n\ 406 .plaintext_2 = NULL,\n\ 407 .plaintext_2_len = 0,\n\ 408 \n\ 409 .info_keystream_2 = NULL,\n\ 410 .info_keystream_2_len = 0,\n\ 411 \n\ 412 .keystream_2_raw = NULL,\n\ 413 .keystream_2_raw_len = 0,\n\ 414 \n\ 415 .ciphertext_2_raw = NULL,\n\ 416 .ciphertext_2_raw_len = 0,\n\ 417 \n\ 418 .ciphertext_2 = NULL,\n\ 419 .ciphertext_2_len = 0,\n\ 420 \n\ 421 .message_2 = NULL,\n\ 422 .message_2_len = 0,\n\ 423 \n\ 424 .sk_i_raw = NULL,\n\ 425 .sk_i_raw_len = 0,\n\ 426 \n\ 427 .pk_i_raw = NULL,\n\ 428 .pk_i_raw_len = 0,\n\ 429 \n\ 430 .prk_4x3m_raw = NULL,\n\ 431 .prk_4x3m_raw_len = 0,\n\ 432 \n\ 433 .input_TH_3 = NULL,\n\ 434 .input_TH_3_len = 0,\n\ 435 \n\ 436 .th_3_raw = NULL,\n\ 437 .th_3_raw_len = 0,\n\ 438 \n\ 439 .th_3 = NULL,\n\ 440 .th_3_len = 0,\n\ 441 \n\ 442 .id_cred_i = NULL,\n\ 443 .id_cred_i_len = 0,\n\ 444 \n\ 445 .cred_i = NULL,\n\ 446 .cred_i_len = 0,\n\ 447 \n\ 448 .ead_3 = NULL,\n\ 449 .ead_3_len = 0,\n\ 450 \n\ 451 .info_mac_3 = NULL,\n\ 452 .info_mac_3_len = 0,\n\ 453 \n\ 454 .mac_3_raw = NULL,\n\ 455 .mac_3_raw_len = 0,\n\ 456 \n\ 457 .mac_3 = NULL,\n\ 458 .mac_3_len = 0,\n\ 459 \n\ 460 .m_3 = NULL,\n\ 461 .m_3_len = 0,\n\ 462 \n\ 463 .sig_or_mac_3_raw = NULL,\n\ 464 .sig_or_mac_3_raw_len = 0,\n\ 465 \n\ 466 .sig_or_mac_3 = NULL,\n\ 467 .sig_or_mac_3_len = 0,\n\ 468 \n\ 469 .p_3 = NULL,\n\ 470 .p_3_len = 0,\n\ 471 \n\ 472 .a_3 = NULL,\n\ 473 .a_3_len = 0,\n\ 474 \n\ 475 .info_k_3 = NULL,\n\ 476 .info_k_3_len = 0,\n\ 477 \n\ 478 .k_3_raw = NULL,\n\ 479 .k_3_raw_len = 0,\n\ 480 \n\ 481 .info_iv_3 = NULL,\n\ 482 .info_iv_3_len = 0,\n\ 483 \n\ 484 .iv_3_raw = NULL,\n\ 485 .iv_3_raw_len = 0,\n\ 486 \n\ 487 .ciphertext_3_raw = NULL,\n\ 488 .ciphertext_3_raw_len = 0,\n\ 489 \n\ 490 .ciphertext_3 = NULL,\n\ 491 .ciphertext_3_len = 0,\n\ 492 \n\ 493 .message_3 = NULL,\n\ 494 .message_3_len = 0,\n\ 495 \n\ 496 .input_th_4 = NULL,\n\ 497 .input_th_4_len = 0,\n\ 498 \n\ 499 .th_4_raw = NULL,\n\ 500 .th_4_raw_len = 0,\n\ 501 \n\ 502 .th_4 = NULL,\n\ 503 .th_4_len = 0,\n\ 504 \n\ 505 .ead_4 = NULL,\n\ 506 .ead_4_len = 0,\n\ 507 \n\ 508 .p_4 = NULL,\n\ 509 .p_4_len = 0,\n\ 510 \n\ 511 .a_4 = NULL,\n\ 512 .a_4_len = 0,\n\ 513 \n\ 514 .info_k_4 = NULL,\n\ 515 .info_k_4_len = 0,\n\ 516 \n\ 517 .k_4_raw = NULL,\n\ 518 .k_4_raw_len = 0,\n\ 519 \n\ 520 .info_iv_4 = NULL,\n\ 521 .info_iv_4_len = 0,\n\ 522 \n\ 523 .iv_4_raw = NULL,\n\ 524 .iv_4_raw_len = 0,\n\ 525 \n\ 526 .ciphertext_4_raw = NULL,\n\ 527 .ciphertext_4_raw_len = 0,\n\ 528 \n\ 529 .ciphertext_4 = NULL,\n\ 530 .ciphertext_4_len = 0,\n\ 531 \n\ 532 .message_4 = NULL,\n\ 533 .message_4_len = 0,\n\ 534 \n\ 535 .oscore_aead_alg = NULL,\n\ 536 \n\ 537 .oscore_hash_alg = NULL,\n\ 538 \n\ 539 .client_sender_id_raw = NULL,\n\ 540 .client_sender_id_raw_len = 0,\n\ 541 \n\ 542 .server_sender_id_raw = NULL,\n\ 543 .server_sender_id_raw_len = 0,\n\ 544 \n\ 545 .info_oscore_secret = NULL,\n\ 546 .info_oscore_secret_len = 0,\n\ 547 \n\ 548 .oscore_secret_raw = NULL,\n\ 549 .oscore_secret_raw_len = 0,\n\ 550 \n\ 551 .info_oscore_salt = NULL,\n\ 552 .info_oscore_salt_len = 0,\n\ 553 \n\ 554 .oscore_salt_raw = NULL,\n\ 555 .oscore_salt_raw_len = 0,\n\ 556 \n\ 557 .key_update_nonce_raw = NULL,\n\ 558 .key_update_nonce_raw_len = 0,\n\ 559 \n\ 560 .prk_4x3m_key_update_raw = NULL,\n\ 561 .prk_4x3m_key_update_raw_len = 0,\n\ 562 \n\ 563 .oscore_secret_key_update_raw = NULL,\n\ 564 .oscore_secret_key_update_raw_len = 0,\n\ 565 \n\ 566 .oscore_salt_key_update_raw = NULL,\n\ 567 .oscore_salt_key_update_raw_len = 0,\n\ 568 \n\ 569 .ca = NULL,\n\ 570 .ca_len = 0,\n\ 571 \n\ 572 .ca_pk = NULL,\n\ 573 .ca_pk_len = 0,\n\ 574 \n\ 575 .i_raw = NULL,\n\ 576 .i_raw_len = 0,\n\ 577 \n\ 578 .g_i_raw = NULL,\n\ 579 .g_i_raw_len = 0,\n\ 580 \n\ 581 .r_raw = NULL,\n\ 582 .r_raw_len = 0,\n\ 583 \n\ 584 .g_r_raw = NULL,\n\ 585 .g_r_raw_len = 0,\n\ 586 \n\ 587 .c_i_raw = NULL,\n\ 588 .c_i_raw_len = 0,\n\ 589 .c_i_raw_int = NULL,\n\ 590 \n\ 591 .c_r_raw = NULL,\n\ 592 .c_r_raw_len = 0,\n\ 593 .c_r_raw_int = NULL,\n\ 594 },\n\ 595" 596STRUCT_POPULATE_END = "};" 597 598 599def print_c_array(in_string): 600 601 cnt = 1 602 if type(in_string) is str: 603 out_string = "[] = {\n\t" 604 for i in range(0, len(in_string), 2): 605 out_string = out_string + "0x" + in_string[i] + in_string[i + 1] 606 607 if i != len(in_string) - 2: 608 out_string = out_string + ", " 609 if cnt == 8: 610 out_string = out_string + "\n\t" 611 cnt = 0 612 else: 613 out_string = out_string + "};\n" 614 cnt = cnt + 1 615 616 if len(in_string) == 0: 617 out_string = out_string + "};\n" 618 619 if type(in_string) is int: 620 out_string = " = " + str(in_string) + ";\n" 621 622 # print(out_string) 623 return out_string 624 625 626def get_len_str(i, data): 627 if type(data["test_vector_1"][i]) is str: 628 return str(len(data["test_vector_1"][i]) >> 1) 629 else: 630 return "1" 631 632 633def populate_struct_body(input, val="dummy", key="dummy", array_name="dummy"): 634 635 pattern_str_len = " ." + key + "_len = 0" 636 repl_str_len = " ." + key + "_len = sizeof(" + array_name + ")" 637 pattern_str_val = " ." + key + " = NULL" 638 repl_str_val = " ." + key + " = " + array_name 639 640 pattern_int_val = " ." + key + " = NULL" 641 repl_int_val = " ." + key + " = &" + array_name 642 643 pattern_c_x_int_val = " ." + key + "_int = NULL" 644 # repl_c_x_str_val = " ." + key + \ 645 # " = {BSTR, {sizeof(" + array_name + "), " + array_name + "}}" 646 repl_c_x_int_val = " ." + key + "_int" + " = &" + array_name 647 648 # if (key == "c_r_raw") or (key == "c_i_raw"): 649 # if isinstance(val, str): 650 # return re.sub(pattern_c_x_str_val, repl_c_x_str_val, input) 651 # if isinstance(val, int): 652 # return re.sub(pattern_c_x_str_val, repl_c_x_int_val, input) 653 654 if isinstance(val, str): 655 changed_len = re.sub(pattern_str_len, repl_str_len, input) 656 changed_val_len = re.sub(pattern_str_val, repl_str_val, changed_len) 657 return changed_val_len 658 659 if isinstance(val, int): 660 if (key == "c_r_raw") or (key == "c_i_raw"): 661 return re.sub(pattern_c_x_int_val, repl_c_x_int_val, input) 662 changed_val_len = re.sub(pattern_int_val, repl_int_val, input) 663 return changed_val_len 664 665 666def main(): 667 with open(in_out_path + "edhoc-vectors-json_v11.txt", "r") as in_fp: 668 data = json.load(in_fp) 669 670 data_arrays = "\n" 671 struct_body_out = "\n" 672 673 test_vector_labels = list(data.keys()) 674 # print(test_vector_labels[0]) 675 vec_num = test_vector_labels[0] 676 677 for vec_num in test_vector_labels: 678 data_arrays += "\n/***************** " + vec_num + " ************/\n\n " 679 test_vector_elements = data[vec_num] 680 # print(test_vector_elements.keys()) 681 # print(test_vector_elements.values()) 682 683 struct_body = populate_struct_body(STRUCT_POPULATE) 684 for key in test_vector_elements.keys(): 685 # print(key, test_vector_elements[key]) 686 array_name = vec_num + "_" + key 687 type = test_vector_elements[key] 688 689 # if the value of an element is a int save it as int_32 690 if isinstance(type, int): 691 data_arrays = ( 692 data_arrays 693 + "int32_t " 694 + array_name 695 + print_c_array(test_vector_elements[key]) 696 ) 697 698 # if the value of an element is a string save it in uint8_t array 699 if isinstance(type, str): 700 data_arrays = ( 701 data_arrays 702 + "static const uint8_t " 703 + array_name 704 + print_c_array(test_vector_elements[key]) 705 ) 706 707 struct_body = populate_struct_body(struct_body, type, key, array_name) 708 709 struct_body_out += "/*--------------- " + vec_num + " ------------*/\n " 710 struct_body_out += struct_body 711 712 output = ( 713 HEADER 714 + INCLUDES 715 + STRUCT_DEF 716 + data_arrays 717 + STRUCT_POPULATE_START 718 + struct_body_out 719 + STRUCT_POPULATE_END 720 ) 721 with open(in_out_path + "edhoc_test_vectors.h", "w") as out_fp: 722 out_fp.write(output) 723 724 725if __name__ == "__main__": 726 main() 727