core/core.c (view raw)
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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 |
#include "core.h"
#include "cbor.h"
#include "chacha20_poly1305.h"
#include "kangarootwelve128.h"
#include "memory.h"
#include "mldsa_native.h"
#include "mlkem_native.h"
#include "packet.h"
#include "peer_table.h"
#include "sha2.h"
#include "x25519.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define CIRCE_P1305_MAC_LEN 16
#define CIRCE_CORE_ASSERT(x, err) \
do \
{ \
if (!(x)) \
{ \
return err; \
} \
} \
while (false)
#define CIRCE_CORE_ASSERT_WITH_CLEANUP(x, err, errvar, label) \
do \
{ \
if (!(x)) \
{ \
(errvar) = (err); \
goto label; \
} \
} \
while (false)
/* GENERAL HELPER FUNCTIONS */
#if defined(__GNUC__) || defined(__clang__)
#define CIRCE_FIRST_FREE_OUTGOING_PACKET_ENTRY(ctx) \
__builtin_ffsll ((ctx)->outgoing_packet_table_free_mask)
#define CIRCE_FIRST_TAKEN_OUTGOING_PACKET_ENTRY(ctx) \
__builtin_ffsll (~((ctx)->outgoing_packet_table_free_mask))
#define CIRCE_FIRST_FREE_PEER_TABLE_ENTRY(ctx) \
__builtin_ffsll ((ctx)->peer_table_free_mask)
#else
#error Circe can currently only be built with GCC or Clang.
#endif
static bool
locations_equal (const struct circe_location a, const struct circe_location b)
{
if (a.ip_type != b.ip_type)
{
return false;
}
else if (a.port != b.port)
{
return false;
}
else if (a.ip_type == CIRCE_ADDRESS_IPV4)
{
return circe_memory_equal (a.ip_addr.ipv4, b.ip_addr.ipv4,
sizeof (a.ip_addr.ipv4));
}
else if (a.ip_type == CIRCE_ADDRESS_IPV6)
{
return circe_memory_equal (a.ip_addr.ipv6, b.ip_addr.ipv6,
sizeof (a.ip_addr.ipv6));
}
// What?
return false;
}
static enum circe_result
peer_table_lookup (struct circe_context *ctx,
const uint8_t identity[CIRCE_IDENTITY_LEN],
struct circe_peer **const out)
{
size_t start_idx = identity[0] % CIRCE_PEER_TABLE_LEN;
for (size_t i = 0; i < CIRCE_PEER_TABLE_LEN; ++i)
{
if (circe_memory_equal (
identity,
ctx->peer_table[(i + start_idx) % CIRCE_PEER_TABLE_LEN].identity,
CIRCE_IDENTITY_LEN))
{
*out = &(ctx->peer_table[(i + start_idx) % CIRCE_PEER_TABLE_LEN]);
return CIRCE_RESULT_SUCCESS;
}
}
size_t out_idx = CIRCE_FIRST_FREE_PEER_TABLE_ENTRY (ctx);
if (out_idx == 0)
{
return CIRCE_RESULT_PEER_TABLE_FULL;
}
if (!ctx->recall_peer_callback (ctx->ctx_handle, identity,
&ctx->peer_table[out_idx - 1]))
{
return CIRCE_RESULT_UNKNOWN_REMOTE;
}
ctx->peer_table_free_mask &= ~(1ull << (out_idx - 1));
ctx->peer_handshake_pending_mask |= (1ull << (out_idx - 1));
*out = &(ctx->peer_table[out_idx - 1]);
(*out)->next_handshake_stage = CIRCE_HANDSHAKE_STAGE_OPEN;
return CIRCE_RESULT_HANDSHAKE_PENDING;
}
static enum circe_result
unseal_payload (struct circe_peer *peer, struct circe_cbor_decoder *cd,
uint8_t *out, size_t *out_len)
{
enum circe_cbor_major_type major_type;
uint64_t field;
uint64_t sequence_number;
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (cd, &field),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (field == CIRCE_PACKET_FIELD_SEQ_NUMBER,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (cd, &sequence_number),
CIRCE_RESULT_INVALID_PACKET);
const uint8_t *end_of_aad = cd->cursor;
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (cd, &field),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (field == CIRCE_PACKET_FIELD_DATA_PAYLOAD,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_BYTESTR,
CIRCE_RESULT_INVALID_PACKET);
struct circe_cbor_buffer sealed;
CIRCE_CORE_ASSERT (circe_cbor_extract_bytestr (cd, &sealed),
CIRCE_RESULT_INVALID_PACKET);
// Make sure we got the full payload
CIRCE_CORE_ASSERT (sealed.buf_len
<= cd->buf_len - (cd->cursor - cd->buf_start),
CIRCE_RESULT_INVALID_PACKET);
uint8_t nonce[sizeof (uint64_t)];
for (unsigned int i = 0; i < sizeof (uint64_t); ++i)
{
nonce[i] = (sequence_number >> (i * 8)) & 0xff;
}
if (!tct_aead_chacha20_poly1305_decrypt_and_verify (
cd->buf_start, end_of_aad - cd->buf_start, peer->rx_key, nonce,
sealed.buf_start, sealed.buf_len, out))
{
return CIRCE_RESULT_INVALID_MAC;
}
*out_len
= sealed.buf_len - CIRCE_P1305_MAC_LEN; // Don't include the MAC's length
return CIRCE_RESULT_SUCCESS;
}
static void
close_without_saying_goodbye (struct circe_context *ctx,
struct circe_peer *remote_pt_entry)
{
size_t start_idx = remote_pt_entry->identity[0] % CIRCE_PEER_TABLE_LEN;
size_t peer_idx = CIRCE_PEER_TABLE_LEN;
for (size_t i = 0; i < CIRCE_PEER_TABLE_LEN; ++i)
{
if (circe_memory_equal (
remote_pt_entry->identity,
ctx->peer_table[(i + start_idx) % CIRCE_PEER_TABLE_LEN].identity,
CIRCE_IDENTITY_LEN))
{
peer_idx = start_idx;
break;
}
}
if (peer_idx == CIRCE_PEER_TABLE_LEN)
{
// Double-free, perhaps; just ignore it
return;
}
ctx->peer_handshake_pending_mask &= ~(1ull << peer_idx);
ctx->peer_table_free_mask |= 1ull << peer_idx;
circe_memory_set ((uint8_t *)&(ctx->peer_table[peer_idx]), 0x0,
sizeof (struct circe_peer));
}
/* END GENERAL HELPER FUNCTIONS */
/* HANDSHAKE PROCESSING FUNCTIONS */
static enum circe_result
process_opentunnel (struct circe_context *ctx, struct circe_cbor_decoder *cd,
struct circe_location remote,
struct circe_peer *remote_pt_entry)
{
enum circe_cbor_major_type major_type;
uint64_t field;
if (remote_pt_entry->next_handshake_stage != CIRCE_HANDSHAKE_STAGE_OPEN)
{
return CIRCE_RESULT_HS_OUT_OF_ORDER;
}
uint8_t remote_ecdh_frag[32];
// ECDH fragment
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (cd, &field),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (field == CIRCE_PACKET_FIELD_ECDH_FRAGMENT,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_BYTESTR,
CIRCE_RESULT_INVALID_PACKET);
struct circe_cbor_buffer buf;
CIRCE_CORE_ASSERT (circe_cbor_extract_bytestr (cd, &buf),
CIRCE_RESULT_INVALID_PACKET);
// Size of a Curve25519 ECDH fragment
CIRCE_CORE_ASSERT (buf.buf_len == 32, CIRCE_RESULT_INVALID_PACKET);
circe_memory_copy (buf.buf_start, remote_ecdh_frag, 32);
// MLKEM-768 public key
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (cd, &field),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (field == CIRCE_PACKET_FIELD_MLKEM_PUBKEY,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_BYTESTR,
CIRCE_RESULT_INVALID_PACKET);
struct circe_cbor_buffer mlkem_pubkey;
CIRCE_CORE_ASSERT (circe_cbor_extract_bytestr (cd, &mlkem_pubkey),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (mlkem_pubkey.buf_len == MLKEM_PUBLICKEYBYTES (768),
CIRCE_RESULT_INVALID_PACKET);
size_t ack1_idx = CIRCE_FIRST_FREE_OUTGOING_PACKET_ENTRY (ctx);
ctx->outgoing_packet_table_free_mask &= ~(1 << (ack1_idx - 1));
if (ack1_idx == 0)
{
circe_memory_set (mlkem_pubkey.buf_start, 0x0,
MLKEM_PUBLICKEYBYTES (768));
circe_memory_set (remote_ecdh_frag, 0x0, 32);
ctx->outgoing_packet_table_free_mask |= (1 << (ack1_idx - 1));
return CIRCE_RESULT_PACKET_TABLE_FULL;
}
size_t ack2_idx = CIRCE_FIRST_FREE_OUTGOING_PACKET_ENTRY (ctx);
ctx->outgoing_packet_table_free_mask &= ~(1 << (ack2_idx - 1));
if (ack2_idx == 0)
{
circe_memory_set (mlkem_pubkey.buf_start, 0x0,
MLKEM_PUBLICKEYBYTES (768));
circe_memory_set (remote_ecdh_frag, 0x0, 32);
ctx->outgoing_packet_table_free_mask |= (1 << (ack1_idx - 1));
ctx->outgoing_packet_table_free_mask |= (1 << (ack2_idx - 1));
return CIRCE_RESULT_PACKET_TABLE_FULL;
}
size_t ack3_idx = CIRCE_FIRST_FREE_OUTGOING_PACKET_ENTRY (ctx);
ctx->outgoing_packet_table_free_mask &= ~(1 << (ack3_idx - 1));
if (ack3_idx == 0)
{
circe_memory_set (mlkem_pubkey.buf_start, 0x0,
MLKEM_PUBLICKEYBYTES (768));
circe_memory_set (remote_ecdh_frag, 0x0, 32);
ctx->outgoing_packet_table_free_mask |= (1 << (ack1_idx - 1));
ctx->outgoing_packet_table_free_mask |= (1 << (ack2_idx - 1));
ctx->outgoing_packet_table_free_mask |= (1 << (ack3_idx - 1));
return CIRCE_RESULT_PACKET_TABLE_FULL;
}
uint8_t ecdh_privkey[32];
ctx->random_bytes_callback (ctx->ctx_handle, ecdh_privkey,
sizeof (ecdh_privkey));
const uint8_t u[32]
= { 0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
uint8_t my_ecdh_frag[32];
tct_x25519 (ecdh_privkey, u, my_ecdh_frag);
ctx->outgoing_packets[ack1_idx - 1].destination = remote;
ctx->outgoing_packets[ack2_idx - 1].destination = remote;
ctx->outgoing_packets[ack3_idx - 1].destination = remote;
struct circe_cbor_encoder ec = {
.buf_start = ctx->outgoing_packets[ack1_idx - 1].buffer,
.cursor = ctx->outgoing_packets[ack1_idx - 1].buffer,
.buf_len = CIRCE_MAX_UDP_PAYLOAD,
};
enum circe_result err = CIRCE_RESULT_SUCCESS;
// Version, identity, type, ECDH fragment, MLKEM encapsulated, payload == 6
// entries
CIRCE_CORE_ASSERT_WITH_CLEANUP (circe_cbor_emit_mapping_header (&ec, 6),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_VERSION),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_VERSION_V1),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_IDENTITY),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_bytestr (
&ec, (struct circe_cbor_buffer){ .buf_start = ctx->my_identity,
.buf_len = CIRCE_IDENTITY_LEN }),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_TYPE),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_ACKOPEN1),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_ECDH_FRAGMENT),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_bytestr (
&ec, (struct circe_cbor_buffer){ .buf_start = my_ecdh_frag,
.buf_len = sizeof (my_ecdh_frag) }),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
uint8_t qk_shared[MLKEM_BYTES];
uint8_t encapsulated[MLKEM_CIPHERTEXTBYTES (768)];
CIRCE_CORE_ASSERT_WITH_CLEANUP (
crypto_kem_enc (encapsulated, qk_shared, mlkem_pubkey.buf_start),
CIRCE_RESULT_KEX_FAILED, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_MLKEM_ENCAPSULATED),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_bytestr (
&ec, (struct circe_cbor_buffer){ .buf_start = encapsulated,
.buf_len = sizeof (encapsulated) }),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
ctx->outgoing_packets[ack1_idx - 1].length = ec.cursor - ec.buf_start;
uint8_t ek_shared[32];
tct_x25519 (ecdh_privkey, remote_ecdh_frag, ek_shared);
tct_turboshake128_init (remote_pt_entry->prk);
const uint8_t SALT[] = "circe/v1";
tct_turboshake128_absorb (remote_pt_entry->prk, SALT,
(uint64_t)(sizeof (SALT) - 1), 0x0);
tct_turboshake128_absorb (remote_pt_entry->prk, ek_shared,
(uint64_t)sizeof (ek_shared), 0x1);
tct_turboshake128_absorb (remote_pt_entry->prk, qk_shared,
(uint64_t)sizeof (qk_shared), 0x2);
uint8_t prk_to_expand[TCT_TURBOSHAKE128_STATE_LEN];
circe_memory_copy (remote_pt_entry->prk, prk_to_expand,
TCT_TURBOSHAKE128_STATE_LEN);
const uint8_t EXPANSION_DATA[] = "hs eurylochus";
tct_turboshake128_absorb (prk_to_expand, EXPANSION_DATA,
(uint64_t)(sizeof (EXPANSION_DATA) - 1), 0x3);
uint8_t th2[64];
tct_sha512 (cd->buf_start, cd->buf_len, th2);
tct_turboshake128_absorb (prk_to_expand, th2, sizeof (th2), 0x4);
uint8_t k_hs_e[32];
tct_turboshake128_squeeze_destructive (prk_to_expand, k_hs_e,
sizeof (k_hs_e));
uint8_t payload_clear[CIRCE_IDENTITY_LEN + MLDSA_BYTES (44)];
circe_memory_copy (ctx->my_identity, payload_clear, CIRCE_IDENTITY_LEN);
size_t _;
CIRCE_CORE_ASSERT_WITH_CLEANUP (
crypto_sign_signature (payload_clear + CIRCE_IDENTITY_LEN, &_,
payload_clear, CIRCE_IDENTITY_LEN, NULL, 0,
ctx->my_mldsa_privkey)
== 0,
CIRCE_RESULT_SIGNATURE_FAILED, err, process_opentunnel_cleanup);
uint8_t encrypted_buf[sizeof (payload_clear) + CIRCE_P1305_MAC_LEN];
uint8_t nonce[sizeof (uint64_t)]
= { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
ec.buf_start = ctx->outgoing_packets[ack2_idx - 1].buffer;
ec.cursor = ctx->outgoing_packets[ack2_idx - 1].buffer;
// buf_len stays the same, which is good because it's const
// Version, identity, type, ID payload 1 == 4 entries
CIRCE_CORE_ASSERT_WITH_CLEANUP (circe_cbor_emit_mapping_header (&ec, 4),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_VERSION),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_VERSION_V1),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_IDENTITY),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_bytestr (
&ec, (struct circe_cbor_buffer){ .buf_start = ctx->my_identity,
.buf_len = CIRCE_IDENTITY_LEN }),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_TYPE),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_ACKOPEN2),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
tct_aead_chacha20_poly1305_encrypt (
ec.buf_start, ec.cursor - ec.buf_start, k_hs_e, nonce, payload_clear,
sizeof (payload_clear) / 2, encrypted_buf,
encrypted_buf + sizeof (payload_clear) / 2);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_ID_PAYLOAD1),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_bytestr (
&ec,
(struct circe_cbor_buffer){ .buf_start = encrypted_buf,
.buf_len = sizeof (encrypted_buf) }),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
ctx->outgoing_packets[ack2_idx - 1].length = ec.cursor - ec.buf_start;
ec.buf_start = ctx->outgoing_packets[ack3_idx - 1].buffer;
ec.cursor = ctx->outgoing_packets[ack3_idx - 1].buffer;
// buf_len once again stays the same
// Version, identity, type, ID payload 1 == 4 entries
CIRCE_CORE_ASSERT_WITH_CLEANUP (circe_cbor_emit_mapping_header (&ec, 4),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_VERSION),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_VERSION_V1),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_IDENTITY),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_bytestr (
&ec, (struct circe_cbor_buffer){ .buf_start = ctx->my_identity,
.buf_len = CIRCE_IDENTITY_LEN }),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_TYPE),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_ACKOPEN2),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
nonce[0]++;
tct_aead_chacha20_poly1305_encrypt (
ec.buf_start, ec.cursor - ec.buf_start, k_hs_e, nonce,
payload_clear + sizeof (payload_clear) / 2, sizeof (payload_clear) / 2,
encrypted_buf, encrypted_buf + sizeof (payload_clear) / 2);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_ID_PAYLOAD2),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_bytestr (
&ec,
(struct circe_cbor_buffer){ .buf_start = encrypted_buf,
.buf_len = sizeof (encrypted_buf) }),
CIRCE_RESULT_INSUFFICIENT_MEM, err, process_opentunnel_cleanup);
ctx->outgoing_packets[ack3_idx - 1].length = ec.cursor - ec.buf_start;
process_opentunnel_cleanup:
if (err != CIRCE_RESULT_SUCCESS)
{
ctx->outgoing_packet_table_free_mask |= (1 << (ack1_idx - 1));
ctx->outgoing_packet_table_free_mask |= (1 << (ack2_idx - 1));
ctx->outgoing_packet_table_free_mask |= (1 << (ack3_idx - 1));
circe_memory_set ((uint8_t *)&ctx->outgoing_packets[ack1_idx - 1], 0x0,
sizeof (struct circe_outgoing_packet));
circe_memory_set ((uint8_t *)&ctx->outgoing_packets[ack2_idx - 1], 0x0,
sizeof (struct circe_outgoing_packet));
circe_memory_set ((uint8_t *)&ctx->outgoing_packets[ack3_idx - 1], 0x0,
sizeof (struct circe_outgoing_packet));
}
circe_memory_set (ecdh_privkey, 0x0, 32);
circe_memory_set (remote_ecdh_frag, 0x0, 32);
circe_memory_set (mlkem_pubkey.buf_start, 0x0, MLKEM_PUBLICKEYBYTES (768));
circe_memory_set (my_ecdh_frag, 0x0, 32);
circe_memory_set (qk_shared, 0x0, sizeof (qk_shared));
circe_memory_set (ek_shared, 0x0, sizeof (ek_shared));
circe_memory_set (encapsulated, 0x0, sizeof (encapsulated));
return err;
}
static enum circe_result
dispatch_identity_packets (struct circe_context *ctx,
struct circe_location remote,
struct circe_peer *remote_pt_entry, uint8_t th3[64])
{
enum circe_result err = CIRCE_RESULT_SUCCESS;
size_t id1_idx = CIRCE_FIRST_FREE_OUTGOING_PACKET_ENTRY (ctx);
ctx->outgoing_packet_table_free_mask &= ~(1 << (id1_idx - 1));
if (id1_idx == 0)
{
ctx->outgoing_packet_table_free_mask |= (1 << (id1_idx - 1));
return CIRCE_RESULT_PACKET_TABLE_FULL;
}
size_t id2_idx = CIRCE_FIRST_FREE_OUTGOING_PACKET_ENTRY (ctx);
ctx->outgoing_packet_table_free_mask &= ~(1 << (id2_idx - 1));
if (id2_idx == 0)
{
ctx->outgoing_packet_table_free_mask |= (1 << (id1_idx - 1));
ctx->outgoing_packet_table_free_mask |= (1 << (id2_idx - 1));
return CIRCE_RESULT_PACKET_TABLE_FULL;
}
uint8_t k_hs_p[32];
uint8_t EXPANSION_DATA[] = "hs polites";
uint8_t prk_to_expand[TCT_TURBOSHAKE128_STATE_LEN];
circe_memory_copy (remote_pt_entry->prk, prk_to_expand,
TCT_TURBOSHAKE128_STATE_LEN);
tct_turboshake128_absorb (prk_to_expand, EXPANSION_DATA,
sizeof (EXPANSION_DATA) - 1, 0x3);
tct_turboshake128_absorb (prk_to_expand, th3, 64, 0x4);
tct_turboshake128_squeeze_destructive (prk_to_expand, k_hs_p,
sizeof (k_hs_p));
circe_memory_set (prk_to_expand, 0x0, TCT_TURBOSHAKE128_STATE_LEN);
struct circe_cbor_encoder ec = {
.buf_len = CIRCE_MAX_UDP_PAYLOAD,
};
uint8_t id_payload[CIRCE_IDENTITY_LEN + MLDSA_BYTES (44)];
circe_memory_copy (ctx->my_identity, id_payload, CIRCE_IDENTITY_LEN);
size_t _;
CIRCE_CORE_ASSERT_WITH_CLEANUP (
crypto_sign_signature (id_payload + CIRCE_IDENTITY_LEN, &_, id_payload,
CIRCE_IDENTITY_LEN, NULL, 0x0,
ctx->my_mldsa_privkey)
== 0,
CIRCE_RESULT_SIGNATURE_FAILED, err, dispatch_identity_packets_cleanup);
for (size_t i = 0; i < 2; ++i)
{
ec.buf_start = ec.cursor
= (i == 0) ? ctx->outgoing_packets[id1_idx - 1].buffer
: ctx->outgoing_packets[id2_idx - 1].buffer;
// Version, identity, type, ID payload 1/2 == 4 entries
CIRCE_CORE_ASSERT_WITH_CLEANUP (circe_cbor_emit_mapping_header (&ec, 4),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
dispatch_identity_packets_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_VERSION),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
dispatch_identity_packets_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_VERSION_V1),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
dispatch_identity_packets_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_IDENTITY),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
dispatch_identity_packets_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_bytestr (
&ec,
(struct circe_cbor_buffer){ .buf_len = CIRCE_IDENTITY_LEN,
.buf_start = ctx->my_identity }),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
dispatch_identity_packets_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_TYPE),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
dispatch_identity_packets_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, (i == 0) ? CIRCE_PACKET_IDENTIFY1
: CIRCE_PACKET_IDENTIFY2),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
dispatch_identity_packets_cleanup);
uint8_t *old_cursor = ec.cursor;
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, (i == 0)
? CIRCE_PACKET_FIELD_ID_PAYLOAD1
: CIRCE_PACKET_FIELD_ID_PAYLOAD2),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
dispatch_identity_packets_cleanup);
struct circe_cbor_buffer out_buf
= { .buf_len = (CIRCE_IDENTITY_LEN + MLDSA_BYTES (44)) / 2
+ CIRCE_P1305_MAC_LEN,
.buf_start = remote_pt_entry->id_payload_halves[i] };
uint8_t nonce[sizeof (uint64_t)]
= { i, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
tct_aead_chacha20_poly1305_encrypt (
ec.buf_start, old_cursor - ec.buf_start, k_hs_p, nonce,
id_payload + i * (CIRCE_IDENTITY_LEN + MLDSA_BYTES (44)) / 2,
(CIRCE_IDENTITY_LEN + MLDSA_BYTES (44)) / 2, out_buf.buf_start,
out_buf.buf_start + (CIRCE_IDENTITY_LEN + MLDSA_BYTES (44)) / 2);
CIRCE_CORE_ASSERT_WITH_CLEANUP (circe_cbor_emit_bytestr (&ec, out_buf),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
dispatch_identity_packets_cleanup);
ctx->outgoing_packets[(i == 0) ? (id1_idx - 1) : (id2_idx - 1)]
.destination
= remote;
ctx->outgoing_packets[(i == 0) ? (id1_idx - 1) : (id2_idx - 1)].length
= ec.cursor - ec.buf_start;
}
dispatch_identity_packets_cleanup:
if (err != CIRCE_RESULT_SUCCESS)
{
ctx->outgoing_packet_table_free_mask |= (1 << (id1_idx - 1));
ctx->outgoing_packet_table_free_mask |= (1 << (id2_idx - 1));
circe_memory_set ((uint8_t *)&ctx->outgoing_packets[id1_idx - 1], 0x0,
sizeof (struct circe_outgoing_packet));
circe_memory_set ((uint8_t *)&ctx->outgoing_packets[id2_idx - 1], 0x0,
sizeof (struct circe_outgoing_packet));
}
circe_memory_set (id_payload, 0x0, sizeof (id_payload));
return err;
}
static enum circe_result
process_openack (struct circe_context *ctx, struct circe_cbor_decoder *cd,
struct circe_location remote,
struct circe_peer *remote_pt_entry, size_t stage)
{
enum circe_result err = CIRCE_RESULT_SUCCESS;
enum circe_cbor_major_type major_type;
struct circe_cbor_buffer buf;
uint64_t field;
size_t aad_len
= cd->cursor - cd->buf_start; // Will be the same for both stage 2 and 3
CIRCE_CORE_ASSERT_WITH_CLEANUP (
!(remote_pt_entry->packets_received_mask & (1 << (stage - 1))),
CIRCE_RESULT_DUPLICATE_HS_PACKET, err, process_openack_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
remote_pt_entry->next_handshake_stage == CIRCE_HANDSHAKE_STAGE_ACK,
CIRCE_RESULT_HS_OUT_OF_ORDER, err, process_openack_cleanup);
uint8_t remote_ecdh_frag[32];
struct circe_cbor_buffer mlkem_encapsulated;
uint8_t qk_shared[32];
uint8_t ek_shared[32];
switch (stage)
{
case 1:
// ECDH fragment
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (cd, &field),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (field == CIRCE_PACKET_FIELD_ECDH_FRAGMENT,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_BYTESTR,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_bytestr (cd, &buf),
CIRCE_RESULT_INVALID_PACKET);
// Size of a Curve25519 ECDH fragment
CIRCE_CORE_ASSERT (buf.buf_len == 32, CIRCE_RESULT_INVALID_PACKET);
circe_memory_copy (buf.buf_start, remote_ecdh_frag, 32);
// MLKEM-768 public key
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (cd, &field),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (field == CIRCE_PACKET_FIELD_MLKEM_ENCAPSULATED,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_BYTESTR,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_bytestr (cd, &mlkem_encapsulated),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (mlkem_encapsulated.buf_len
== MLKEM_CIPHERTEXTBYTES (768),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
crypto_kem_dec (qk_shared, mlkem_encapsulated.buf_start,
remote_pt_entry->my_mlkem_privkey)
== 0,
CIRCE_RESULT_KEX_FAILED, err, process_openack_cleanup);
tct_x25519 (remote_pt_entry->my_ecdh_privkey, remote_ecdh_frag,
ek_shared);
tct_turboshake128_init (remote_pt_entry->prk);
const uint8_t SALT[] = "circe/v1";
tct_turboshake128_absorb (remote_pt_entry->prk, SALT,
(uint64_t)(sizeof (SALT) - 1), 0x0);
tct_turboshake128_absorb (remote_pt_entry->prk, ek_shared,
(uint64_t)sizeof (ek_shared), 0x1);
tct_turboshake128_absorb (remote_pt_entry->prk, qk_shared,
(uint64_t)sizeof (qk_shared), 0x2);
break;
case 2:
case 3:
circe_memory_copy (cd->buf_start,
remote_pt_entry->id_payload_aads[stage - 2],
cd->cursor - cd->buf_start);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET, err, process_openack_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (circe_cbor_extract_unsigned (cd, &field),
CIRCE_RESULT_INVALID_PACKET, err,
process_openack_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
(stage == 2) ? (field == CIRCE_PACKET_FIELD_ID_PAYLOAD1)
: (field == CIRCE_PACKET_FIELD_ID_PAYLOAD2),
CIRCE_RESULT_INVALID_PACKET, err, process_openack_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_get_major_type (cd, &major_type),
CIRCE_RESULT_INVALID_PACKET, err, process_openack_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (major_type == CIRCE_CBOR_MAJOR_BYTESTR,
CIRCE_RESULT_INVALID_PACKET, err,
process_openack_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (circe_cbor_extract_bytestr (cd, &buf),
CIRCE_RESULT_INVALID_PACKET, err,
process_openack_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
buf.buf_len
== (CIRCE_IDENTITY_LEN + MLDSA_BYTES (44)) / 2
+ CIRCE_P1305_MAC_LEN,
CIRCE_RESULT_INVALID_PACKET, err, process_openack_cleanup);
circe_memory_copy (buf.buf_start,
remote_pt_entry->id_payload_halves[stage - 1],
buf.buf_len);
break;
default:
CIRCE_CORE_ASSERT_WITH_CLEANUP (false, CIRCE_RESULT_INTERNAL_ERROR, err,
process_openack_cleanup);
break;
}
process_openack_cleanup:
circe_memory_set (remote_ecdh_frag, 0x0, sizeof (remote_ecdh_frag));
circe_memory_set (ek_shared, 0x0, sizeof (ek_shared));
circe_memory_set (qk_shared, 0x0, sizeof (qk_shared));
if (err == CIRCE_RESULT_SUCCESS)
{
remote_pt_entry->packets_received_mask |= 1 << (stage - 1);
if (remote_pt_entry->packets_received_mask == 0b111)
{
remote_pt_entry->packets_received_mask = 0x0;
remote_pt_entry->next_handshake_stage = CIRCE_HANDSHAKE_STAGE_ID;
uint8_t prk_to_expand[TCT_TURBOSHAKE128_STATE_LEN];
circe_memory_copy (remote_pt_entry->prk, prk_to_expand,
TCT_TURBOSHAKE128_STATE_LEN);
const uint8_t EXPANSION_DATA[] = "hs eurylochus";
tct_turboshake128_absorb (prk_to_expand, EXPANSION_DATA,
sizeof (EXPANSION_DATA) - 1, 0x3);
tct_turboshake128_absorb (prk_to_expand, remote_pt_entry->my_th2,
sizeof (remote_pt_entry->my_th2), 0x4);
uint8_t k_hs_e[32];
tct_turboshake128_squeeze_destructive (prk_to_expand, k_hs_e,
sizeof (k_hs_e));
circe_memory_set (prk_to_expand, 0x0, TCT_TURBOSHAKE128_STATE_LEN);
uint8_t nonce[sizeof (uint64_t)]
= { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
uint8_t id_payload[CIRCE_IDENTITY_LEN + MLDSA_BYTES (44)];
if (!tct_aead_chacha20_poly1305_decrypt_and_verify (
remote_pt_entry->id_payload_aads[0], aad_len, k_hs_e, nonce,
remote_pt_entry->id_payload_halves[0],
(CIRCE_IDENTITY_LEN + MLDSA_BYTES (44)) / 2, id_payload))
{
circe_memory_set (id_payload, 0x0, sizeof (id_payload));
return CIRCE_RESULT_INVALID_MAC;
}
nonce[0]++;
if (!tct_aead_chacha20_poly1305_decrypt_and_verify (
remote_pt_entry->id_payload_aads[1], aad_len, k_hs_e, nonce,
remote_pt_entry->id_payload_halves[1],
(CIRCE_IDENTITY_LEN + MLDSA_BYTES (44)) / 2,
id_payload + (CIRCE_IDENTITY_LEN + MLDSA_BYTES (44)) / 2))
{
circe_memory_set (id_payload, 0x0, sizeof (id_payload));
return CIRCE_RESULT_INVALID_MAC;
}
if (crypto_sign_verify (id_payload + CIRCE_IDENTITY_LEN,
MLDSA_BYTES (44), id_payload,
CIRCE_IDENTITY_LEN, NULL, 0,
remote_pt_entry->mldsa_pubkey)
!= 0)
{
circe_memory_set (id_payload, 0x0, sizeof (id_payload));
return CIRCE_RESULT_INVALID_SIGNATURE;
}
circe_memory_set (id_payload, 0x0, sizeof (id_payload));
uint8_t th3[64];
tct_sha512 (id_payload, sizeof (id_payload), th3);
return dispatch_identity_packets (ctx, remote, remote_pt_entry, th3);
}
}
return err;
}
static enum circe_result
process_identify (struct circe_cbor_decoder *cd, struct circe_location remote,
struct circe_peer *remote_pt_entry, size_t stage)
{
}
static enum circe_result
dispatch_opentunnel (struct circe_location remote,
struct circe_peer *remote_pt_entry)
{
}
/* END HANDSHAKE PROCESSING FUNCTIONS */
/* PUBLIC API */
enum circe_result
circe_initialize_core (
struct circe_context *ctx, uint8_t identity[CIRCE_IDENTITY_LEN],
uint8_t mldsa_privkey[MLDSA_SECRETKEYBYTES (44)], uint64_t now_ms,
void *ctx_handle,
void (*random_bytes_callback) (void *, uint8_t *, uint32_t),
bool (*recall_peer_callback) (void *, const uint8_t[CIRCE_IDENTITY_LEN],
struct circe_peer *))
{
ctx->last_tick_ms = now_ms;
circe_memory_copy (identity, ctx->my_identity, CIRCE_IDENTITY_LEN);
circe_memory_copy (mldsa_privkey, ctx->my_mldsa_privkey,
MLDSA_SECRETKEYBYTES (44));
ctx->peer_handshake_pending_mask = 0x0ull;
ctx->peer_table_free_mask = ~0x0ull;
ctx->outgoing_packet_table_free_mask = ~0x0ull;
circe_memory_set ((uint8_t *)ctx->outgoing_packets, 0x0,
sizeof (ctx->outgoing_packets));
circe_memory_set ((uint8_t *)ctx->peer_table, 0x0, sizeof (ctx->peer_table));
ctx->ctx_handle = ctx_handle;
ctx->random_bytes_callback = random_bytes_callback;
ctx->recall_peer_callback = recall_peer_callback;
return CIRCE_RESULT_SUCCESS;
}
enum circe_result
circe_handle_packet (struct circe_context *ctx,
const struct circe_location remote,
const uint8_t *restrict in, size_t len,
uint8_t *restrict out, size_t *out_len, uint64_t now_ms,
struct circe_event *events, size_t *n_events)
{
*n_events = 0;
struct circe_cbor_decoder cd = {
.buf_start = in,
.buf_len = len,
.cursor = in,
};
enum circe_cbor_major_type major_type;
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (&cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_MAPPING,
CIRCE_RESULT_INVALID_PACKET);
uint64_t num_fields;
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (&cd, &num_fields),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (num_fields >= 2, CIRCE_RESULT_INVALID_PACKET);
// Version
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (&cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
uint64_t field;
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (&cd, &field),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (field == CIRCE_PACKET_FIELD_VERSION,
CIRCE_RESULT_INVALID_PACKET);
uint64_t version;
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (&cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (&cd, &version),
CIRCE_RESULT_INVALID_PACKET);
// If it's anything but v1 we can't handle it (there is no v2)
CIRCE_CORE_ASSERT (field == CIRCE_VERSION_V1, CIRCE_RESULT_INVALID_PACKET);
// Remote's identity
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (&cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (&cd, &field),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (field == CIRCE_PACKET_FIELD_IDENTITY,
CIRCE_RESULT_INVALID_PACKET);
struct circe_cbor_buffer remote_id_buf;
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (&cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_BYTESTR,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_bytestr (&cd, &remote_id_buf),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (remote_id_buf.buf_len == CIRCE_IDENTITY_LEN,
CIRCE_RESULT_INVALID_PACKET);
struct circe_peer *remote_pt_entry;
enum circe_result peer_in_ram
= peer_table_lookup (ctx, remote_id_buf.buf_start, &remote_pt_entry);
if (peer_in_ram == CIRCE_RESULT_UNKNOWN_REMOTE)
{
return CIRCE_RESULT_UNKNOWN_REMOTE;
}
// Packet type
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (&cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (&cd, &field),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (field == CIRCE_PACKET_FIELD_TYPE,
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (circe_cbor_get_major_type (&cd, &major_type),
CIRCE_RESULT_INVALID_PACKET);
CIRCE_CORE_ASSERT (major_type == CIRCE_CBOR_MAJOR_UNSIGNED,
CIRCE_RESULT_INVALID_PACKET);
uint64_t packet_type;
CIRCE_CORE_ASSERT (circe_cbor_extract_unsigned (&cd, &packet_type),
CIRCE_RESULT_INVALID_PACKET);
if ((packet_type != CIRCE_PACKET_ROUTE_CHALLENGE1)
&& (packet_type != CIRCE_PACKET_OPENTUNNEL)
&& (peer_in_ram == CIRCE_RESULT_SUCCESS))
{
// Verify that this came from our known remote location
bool good_location;
switch (remote_pt_entry->current_route)
{
case CIRCE_ROUTE_LAN0:
good_location = locations_equal (remote_pt_entry->lan0_loc, remote);
break;
case CIRCE_ROUTE_LAN1:
good_location = locations_equal (remote_pt_entry->lan1_loc, remote);
break;
case CIRCE_ROUTE_WAN:
good_location = locations_equal (remote_pt_entry->wan_loc, remote);
break;
case CIRCE_ROUTE_RELAY:
// What?
good_location = false;
break;
default:
return CIRCE_RESULT_INTERNAL_ERROR;
}
if (!good_location)
{
(*n_events)++;
events[0] = (struct circe_event){
.event_type = CIRCE_EVENT_REJECTED_REMOTE,
};
return CIRCE_RESULT_SUCCESS;
}
}
enum circe_result err;
switch (packet_type)
{
case CIRCE_PACKET_OPENTUNNEL:
if (peer_in_ram == CIRCE_RESULT_SUCCESS)
{
// We already have an open session, something is wrong
events[*n_events] = (struct circe_event){
.event_type = CIRCE_EVENT_REJECTED_REMOTE,
};
(*n_events)++;
return CIRCE_RESULT_SUCCESS;
}
else if (peer_in_ram == CIRCE_RESULT_HANDSHAKE_PENDING)
{
// This is the expected path
err = process_opentunnel (ctx, &cd, remote, remote_pt_entry);
if (err != CIRCE_RESULT_SUCCESS)
{
close_without_saying_goodbye (ctx, remote_pt_entry);
events[*n_events] = (struct circe_event){
.event_type = CIRCE_EVENT_REJECTED_REMOTE,
};
(*n_events)++;
return err;
}
events[*n_events] = (struct circe_event){
.event_type = CIRCE_EVENT_NEW_REMOTE,
};
(*n_events)++;
return CIRCE_RESULT_SUCCESS;
}
else
{
events[*n_events] = (struct circe_event){
.event_type = CIRCE_EVENT_REJECTED_REMOTE,
};
(*n_events)++;
return peer_in_ram;
}
case CIRCE_PACKET_ACKOPEN1:
case CIRCE_PACKET_ACKOPEN2:
case CIRCE_PACKET_ACKOPEN3:
err = process_openack (ctx, &cd, remote, remote_pt_entry,
(packet_type - CIRCE_PACKET_ACKOPEN1) + 1);
if (err != CIRCE_RESULT_SUCCESS)
{
close_without_saying_goodbye (ctx, remote_pt_entry);
events[*n_events] = (struct circe_event){
.event_type = CIRCE_EVENT_REJECTED_REMOTE,
};
(*n_events)++;
return err;
}
return CIRCE_RESULT_SUCCESS;
case CIRCE_PACKET_DATA:
events[*n_events] = (struct circe_event){
.event_type = CIRCE_EVENT_DATA_RECEIVED,
};
(*n_events)++;
return unseal_payload (remote_pt_entry, &cd, out, out_len);
case CIRCE_PACKET_HEARTBEAT:
case CIRCE_PACKET_CLOSETUNNEL1:
case CIRCE_PACKET_CLOSETUNNEL2:
case CIRCE_PACKET_ROUTE_CHALLENGE1:
case CIRCE_PACKET_ROUTE_CHALLENGE2:
default:
return CIRCE_RESULT_INVALID_PACKET;
}
}
enum circe_result
circe_send_outgoing_packet (struct circe_context *ctx,
const uint8_t remote_identity[CIRCE_IDENTITY_LEN],
const uint8_t *in, size_t len, uint64_t now_ms,
struct circe_event *events, size_t *n_events)
{
if (len > CIRCE_MTU)
{
return CIRCE_RESULT_OVER_MTU;
}
struct circe_peer *remote_pt_entry;
enum circe_result peer_in_ram
= peer_table_lookup (ctx, remote_identity, &remote_pt_entry);
if (peer_in_ram != CIRCE_RESULT_SUCCESS)
{
// Might return if e.g. unknown peer or if we need to finish the
// handshake
return peer_in_ram;
}
size_t next_entry_idx = CIRCE_FIRST_FREE_OUTGOING_PACKET_ENTRY (ctx);
if (next_entry_idx == 0)
{
// No free entries
return CIRCE_RESULT_PACKET_TABLE_FULL;
}
ctx->outgoing_packet_table_free_mask &= ~(1ull << (next_entry_idx - 1));
struct circe_cbor_encoder ec = {
.buf_start = ctx->outgoing_packets[next_entry_idx].buffer,
.buf_len = sizeof (ctx->outgoing_packets[next_entry_idx].buffer),
.cursor = ctx->outgoing_packets[next_entry_idx].buffer,
};
remote_pt_entry->last_seqn_tx++;
enum circe_result err = CIRCE_RESULT_SUCCESS;
// Version, identity, type, sequence number, payload == 5 pairs
CIRCE_CORE_ASSERT_WITH_CLEANUP (circe_cbor_emit_mapping_header (&ec, 5),
CIRCE_RESULT_INSUFFICIENT_MEM, err,
circe_send_outgoing_packet_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_VERSION),
CIRCE_RESULT_INSUFFICIENT_MEM, err, circe_send_outgoing_packet_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_VERSION_V1),
CIRCE_RESULT_INSUFFICIENT_MEM, err, circe_send_outgoing_packet_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_IDENTITY),
CIRCE_RESULT_INSUFFICIENT_MEM, err, circe_send_outgoing_packet_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_bytestr (
&ec, (struct circe_cbor_buffer){ .buf_len = CIRCE_IDENTITY_LEN,
.buf_start = ctx->my_identity }),
CIRCE_RESULT_INSUFFICIENT_MEM, err, circe_send_outgoing_packet_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_TYPE),
CIRCE_RESULT_INSUFFICIENT_MEM, err, circe_send_outgoing_packet_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_DATA),
CIRCE_RESULT_INSUFFICIENT_MEM, err, circe_send_outgoing_packet_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_SEQ_NUMBER),
CIRCE_RESULT_INSUFFICIENT_MEM, err, circe_send_outgoing_packet_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, remote_pt_entry->last_seqn_tx),
CIRCE_RESULT_INSUFFICIENT_MEM, err, circe_send_outgoing_packet_cleanup);
uint8_t sealed[CIRCE_MTU + CIRCE_P1305_MAC_LEN]; // Encrypted with MAC
uint8_t nonce[sizeof (uint64_t)];
for (size_t i = 0; i < sizeof (uint64_t); ++i)
{
nonce[i] = 0xff & (remote_pt_entry->last_seqn_tx >> (i * 8));
}
tct_aead_chacha20_poly1305_encrypt (ec.buf_start, ec.cursor - ec.buf_start,
remote_pt_entry->tx_key, nonce, in, len,
sealed, sealed + len);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_unsigned (&ec, CIRCE_PACKET_FIELD_DATA_PAYLOAD),
CIRCE_RESULT_INSUFFICIENT_MEM, err, circe_send_outgoing_packet_cleanup);
CIRCE_CORE_ASSERT_WITH_CLEANUP (
circe_cbor_emit_bytestr (
&ec,
(struct circe_cbor_buffer){ .buf_len = len + CIRCE_P1305_MAC_LEN,
.buf_start = sealed }),
CIRCE_RESULT_INSUFFICIENT_MEM, err, circe_send_outgoing_packet_cleanup);
enum circe_result relay_server_in_ram;
struct circe_peer *const relay_server_pt_entry = NULL;
switch (remote_pt_entry->current_route)
{
case CIRCE_ROUTE_LAN0:
ctx->outgoing_packets[next_entry_idx - 1].destination
= remote_pt_entry->lan0_loc;
break;
case CIRCE_ROUTE_LAN1:
ctx->outgoing_packets[next_entry_idx - 1].destination
= remote_pt_entry->lan1_loc;
break;
case CIRCE_ROUTE_WAN:
ctx->outgoing_packets[next_entry_idx - 1].destination
= remote_pt_entry->wan_loc;
break;
case CIRCE_ROUTE_RELAY:
relay_server_in_ram = peer_table_lookup (
ctx, remote_pt_entry->relay_server_identity,
(struct circe_peer * *const)&relay_server_pt_entry);
CIRCE_CORE_ASSERT_WITH_CLEANUP (relay_server_in_ram
== CIRCE_RESULT_SUCCESS,
CIRCE_RESULT_RELAY_LOOKUP_FAILED, err,
circe_send_outgoing_packet_cleanup);
ctx->outgoing_packets[next_entry_idx - 1].destination
= relay_server_pt_entry->wan_loc;
break;
}
ctx->outgoing_packets[next_entry_idx - 1].length = ec.cursor - ec.buf_start;
circe_send_outgoing_packet_cleanup:
if (err != CIRCE_RESULT_SUCCESS)
{
remote_pt_entry->last_seqn_tx--;
ctx->outgoing_packet_table_free_mask |= (1ull << (next_entry_idx - 1));
}
return err;
}
enum circe_result
circe_tick (struct circe_context *ctx, uint64_t now_ms,
struct circe_outgoing_packet *out, bool *new_packet_out)
{
if (now_ms < ctx->last_tick_ms)
{
return CIRCE_RESULT_INVALID_TIMESTAMP;
}
ctx->last_tick_ms = now_ms;
size_t packet_idx = CIRCE_FIRST_TAKEN_OUTGOING_PACKET_ENTRY (ctx);
if (packet_idx == 0)
{
*new_packet_out = false;
return CIRCE_RESULT_SUCCESS;
}
*new_packet_out = true;
*out = ctx->outgoing_packets[packet_idx - 1];
circe_memory_set ((uint8_t *)(&ctx->outgoing_packets[packet_idx - 1]), 0x0,
CIRCE_MAX_UDP_PAYLOAD);
ctx->outgoing_packet_table_free_mask |= (1ull << (packet_idx - 1));
return CIRCE_RESULT_SUCCESS;
}
/* END PUBLIC API */
|