DKTP: Dual Key Tunneling Protocol 1.0.0.0a (A1)
A quantum secure dual-key encrypted tunneling protocol
dktp.h
Go to the documentation of this file.
1/* 2025 Quantum Resistant Cryptographic Solutions Corporation
2 * All Rights Reserved.
3 *
4 * NOTICE: This software and all accompanying materials are the exclusive
5 * property of Quantum Resistant Cryptographic Solutions Corporation (QRCS).
6 * The intellectual and technical concepts contained within this implementation
7 * are proprietary to QRCS and its authorized licensors and are protected under
8 * applicable U.S. and international copyright, patent, and trade secret laws.
9 *
10 * CRYPTOGRAPHIC STANDARDS:
11 * - This software includes implementations of cryptographic algorithms such as
12 * SHA3, AES, and others. These algorithms are public domain or standardized
13 * by organizations such as NIST and are NOT the property of QRCS.
14 * - However, all source code, optimizations, and implementations in this library
15 * are original works of QRCS and are protected under this license.
16 *
17 * RESTRICTIONS:
18 * - Redistribution, modification, or unauthorized distribution of this software,
19 * in whole or in part, is strictly prohibited.
20 * - This software is provided for non-commercial, educational, and research
21 * purposes only. Commercial use in any form is expressly forbidden.
22 * - Licensing and authorized distribution are solely at the discretion of QRCS.
23 * - Any use of this software implies acceptance of these restrictions.
24 *
25 * DISCLAIMER:
26 * This software is provided "as is," without warranty of any kind, express or
27 * implied, including but not limited to warranties of merchantability or fitness
28 * for a particular purpose. QRCS disclaims all liability for any direct, indirect,
29 * incidental, or consequential damages resulting from the use or misuse of this software.
30 *
31 * FULL LICENSE:
32 * This software is subject to the **Quantum Resistant Cryptographic Solutions
33 * Proprietary License (QRCS-PL)**. The complete license terms are included
34 * in the LICENSE.txt file distributed with this software.
35 *
36 * Written by: John G. Underhill
37 * Contact: contact@qrcscorp.ca
38 */
39
40#ifndef DKTP_H
41#define DKTP_H
42
43#include "rcs.h"
44#include "sha3.h"
45
86
91#define DKTP_CONFIG_DILITHIUM_KYBER
92
94//* \def DKTP_CONFIG_DILITHIUM_MCELIECE
95//* \brief Sets the asymmetric cryptographic primitive-set to Dilithium/McEliece.
96//*/
97//#define DKTP_CONFIG_DILITHIUM_MCELIECE
98
100//* \def DKTP_CONFIG_SPHINCS_MCELIECE
101//* \brief Sets the asymmetric cryptographic primitive-set to Sphincs+/McEliece.
102//*/
103//#define DKTP_CONFIG_SPHINCS_MCELIECE
104
105#include "dktpcommon.h"
106#include "socketbase.h"
107
108#if defined(DKTP_CONFIG_DILITHIUM_KYBER)
109# include "dilithium.h"
110# include "kyber.h"
111#elif defined(DKTP_CONFIG_DILITHIUM_MCELIECE)
112# include "dilithium.h"
113# include "mceliece.h"
114#elif defined(DKTP_CONFIG_SPHINCS_MCELIECE)
115# include "sphincsplus.h"
116# include "mceliece.h"
117#else
118# error Invalid parameter set!
119#endif
120
126#define DKTP_DOMAIN_IDENTITY_SIZE 16U
127
136static const char DKTP_DOMAIN_IDENTITY_STRING[DKTP_DOMAIN_IDENTITY_SIZE] = "QRCS:PDEF:DKTP1A";
137
142#define DKTP_ASYMMETRIC_RATCHET
143
148#define DKTP_CONFIG_SIZE 48U
149
154#define DKTP_HASH_SIZE 64U
155
160#define DKTP_MACKEY_SIZE 64U
161
166#define DKTP_MACTAG_SIZE 64U
167
172#define DKTP_SYMMETRIC_KEY_SIZE 64U
173
178#define DKTP_SYMMETRIC_NONCE_SIZE 32U
179
184#define DKTP_ASYMMETRIC_KEYCHAIN_COUNT 10U
185
190#define DKTP_CLIENT_PORT 31118U
191
196#define DKTP_CONNECTIONS_INIT 1000U
197
205#define DKTP_CONNECTIONS_MAX 50000U
206
211#define DKTP_CONNECTION_MTU 1500U
212
217#define DKTP_ERROR_SEQUENCE 0xFF00000000000000ULL
218
223#define DKTP_ERROR_MESSAGE_SIZE 1U
224
229#define DKTP_FLAG_SIZE 1U
230
235#define DKTP_HEADER_SIZE 21U
236
241#define DKTP_KEEPALIVE_STRING 20U
242
247#define DKTP_KEEPALIVE_TIMEOUT (120U * 1000U)
248
253#define DKTP_KEYID_SIZE 16U
254
259#define DKTP_MSGLEN_SIZE 4U
260
265#define DKTP_NONCE_SIZE 32U
266
271#define DKTP_SERVER_PORT 31119U
272
280#define DKTP_PACKET_TIME_THRESHOLD 60U
281
286#define DKTP_POLLING_INTERVAL (120U * 1000U)
287
292#define DKTP_PUBKEY_DURATION_DAYS 365U
293
298#define DKTP_PUBKEY_DURATION_SECONDS (DKTP_PUBKEY_DURATION_DAYS * 24U * 60U * 60U)
299
304#define DKTP_PUBKEY_LINE_LENGTH 64U
305
310#define DKTP_SECRET_SIZE 32U
311
316#define DKTP_SEQUENCE_SIZE 8U
317
322#define DKTP_SEQUENCE_TERMINATOR 0xFFFFFFFFUL
323
328#define DKTP_TIMESTAMP_SIZE 8U
329
334#define DKTP_TIMESTAMP_STRING_SIZE 20U
335
340#define DKTP_MESSAGE_MAX 0x3D090000UL
341
342#if defined(DKTP_CONFIG_DILITHIUM_KYBER)
343
348# define dktp_cipher_generate_keypair qsc_kyber_generate_keypair
353# define dktp_cipher_decapsulate qsc_kyber_decapsulate
358# define dktp_cipher_encapsulate qsc_kyber_encapsulate
363# define dktp_signature_generate_keypair qsc_dilithium_generate_keypair
368# define dktp_signature_sign qsc_dilithium_sign
373# define dktp_signature_verify qsc_dilithium_verify
374
376# if defined(QSC_DILITHIUM_S1P44) && defined(QSC_KYBER_S1K2P512)
377static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "dilithium-s1_kyber-s1_sha3_rcs";
378# elif defined(QSC_DILITHIUM_S3P65) && defined(QSC_KYBER_S3K3P768)
379static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "dilithium-s3_kyber-s3_sha3_rcs";
380# elif defined(QSC_DILITHIUM_S5P87) && defined(QSC_KYBER_S5K4P1024)
381static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "dilithium-s5_kyber-s5_sha3_rcs";
382# elif defined(QSC_DILITHIUM_S5P87) && defined(QSC_KYBER_S6K5P1280)
383static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "dilithium-s5_kyber-s6_sha3_rcs";
384# else
385# error Invalid parameter set!
386# endif
388
393# define DKTP_ASYMMETRIC_CIPHER_TEXT_SIZE (QSC_KYBER_CIPHERTEXT_SIZE)
394
399# define DKTP_ASYMMETRIC_DECAPSULATION_KEY_SIZE (QSC_KYBER_PRIVATEKEY_SIZE)
400
405# define DKTP_ASYMMETRIC_ENCAPSULATION_KEY_SIZE (QSC_KYBER_PUBLICKEY_SIZE)
406
411# define DKTP_ASYMMETRIC_SIGNING_KEY_SIZE (QSC_DILITHIUM_PRIVATEKEY_SIZE)
412
417# define DKTP_ASYMMETRIC_VERIFY_KEY_SIZE (QSC_DILITHIUM_PUBLICKEY_SIZE)
418
423# define DKTP_ASYMMETRIC_SIGNATURE_SIZE (QSC_DILITHIUM_SIGNATURE_SIZE)
424
425#elif defined(DKTP_CONFIG_DILITHIUM_MCELIECE)
430# define dktp_cipher_generate_keypair qsc_mceliece_generate_keypair
435# define dktp_cipher_decapsulate qsc_mceliece_decapsulate
440# define dktp_cipher_encapsulate qsc_mceliece_encapsulate
445# define dktp_signature_generate_keypair qsc_dilithium_generate_keypair
450# define dktp_signature_sign qsc_dilithium_sign
455# define dktp_signature_verify qsc_dilithium_verify
456
458# if defined(QSC_DILITHIUM_S1P44) && defined(QSC_MCELIECE_S1N3488T64)
459static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "dilithium-s1_mceliece-s1_sha3_rcs";
460# elif defined(QSC_DILITHIUM_S3P65) && defined(QSC_MCELIECE_S3N4608T96)
461static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "dilithium-s3_mceliece-s3_sha3_rcs";
462# elif defined(QSC_DILITHIUM_S5P87) && defined(QSC_MCELIECE_S5N6688T128)
463static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "dilithium-s5_mceliece-s5_sha3_rcs";
464# elif defined(QSC_DILITHIUM_S5P87) && defined(QSC_MCELIECE_S6N6960T119)
465static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "dilithium-s5_mceliece-s6_sha3_rcs";
466# elif defined(QSC_DILITHIUM_S5P87) && defined(QSC_MCELIECE_S7N8192T128)
467static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "dilithium-s5_mceliece-s7_sha3_rcs";
468# else
469# error Invalid parameter set!
470# endif
472
477# define DKTP_ASYMMETRIC_CIPHER_TEXT_SIZE (QSC_MCELIECE_CIPHERTEXT_SIZE)
478
483# define DKTP_ASYMMETRIC_DECAPSULATION_KEY_SIZE (QSC_MCELIECE_PRIVATEKEY_SIZE)
484
489# define DKTP_ASYMMETRIC_ENCAPSULATION_KEY_SIZE (QSC_MCELIECE_PUBLICKEY_SIZE)
490
495# define DKTP_ASYMMETRIC_SIGNING_KEY_SIZE (QSC_DILITHIUM_PRIVATEKEY_SIZE)
496
501# define DKTP_ASYMMETRIC_VERIFY_KEY_SIZE (QSC_DILITHIUM_PUBLICKEY_SIZE)
502
507# define DKTP_ASYMMETRIC_SIGNATURE_SIZE (QSC_DILITHIUM_SIGNATURE_SIZE)
508
509#elif defined(DKTP_CONFIG_SPHINCS_MCELIECE)
510
515# define dktp_cipher_generate_keypair qsc_mceliece_generate_keypair
520# define dktp_cipher_decapsulate qsc_mceliece_decapsulate
525# define dktp_cipher_encapsulate qsc_mceliece_encapsulate
530# define dktp_signature_generate_keypair qsc_sphincsplus_generate_keypair
535# define dktp_signature_sign qsc_sphincsplus_sign
540# define dktp_signature_verify qsc_sphincsplus_verify
541
543# if defined(QSC_SPHINCSPLUS_S1S128SHAKERF) && defined(QSC_MCELIECE_S1N3488T64)
544static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "sphincs-s1f_mceliece-s1_sha3_rcs";
545# elif defined(QSC_SPHINCSPLUS_S1S128SHAKERS) && defined(QSC_MCELIECE_S1N3488T64)
546static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "sphincs-s1s_mceliece-s1_sha3_rcs";
547# elif defined(QSC_SPHINCSPLUS_S3S192SHAKERF) && defined(QSC_MCELIECE_S3N4608T96)
548static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "sphincs-3f_mceliece-s3_sha3_rcs";
549# elif defined(QSC_SPHINCSPLUS_S3S192SHAKERS) && defined(QSC_MCELIECE_S3N4608T96)
550static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "sphincs-3s_mceliece-s3_sha3_rcs";
551# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERF) && defined(QSC_MCELIECE_S5N6688T128)
552static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "sphincs-s5f_mceliece-s5_sha3_rcs";
553# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERS) && defined(QSC_MCELIECE_S5N6688T128)
554static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "sphincs-s5s_mceliece-s5_sha3_rcs";
555# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERF) && defined(QSC_MCELIECE_S6N6960T119)
556static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "sphincs-s5f_mceliece-s6_sha3_rcs";
557# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERS) && defined(QSC_MCELIECE_S6N6960T119)
558static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "sphincs-s5s_mceliece-s6_sha3_rcs";
559# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERF) && defined(QSC_MCELIECE_S7N8192T128)
560static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "sphincs-s5f_mceliece-s7_sha3_rcs";
561# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERS) && defined(QSC_MCELIECE_S7N8192T128)
562static const char DKTP_CONFIG_STRING[DKTP_CONFIG_SIZE] = "sphincs-s5s_mceliece-s7_sha3_rcs";
563# else
564# error Invalid parameter set!
565# endif
567
572# define DKTP_ASYMMETRIC_CIPHER_TEXT_SIZE (QSC_MCELIECE_CIPHERTEXT_SIZE)
573
578# define DKTP_ASYMMETRIC_DECAPSULATION_KEY_SIZE (QSC_MCELIECE_PRIVATEKEY_SIZE)
579
584# define DKTP_ASYMMETRIC_ENCAPSULATION_KEY_SIZE (QSC_MCELIECE_PUBLICKEY_SIZE)
585
590# define DKTP_ASYMMETRIC_SIGNING_KEY_SIZE (QSC_SPHINCSPLUS_PRIVATEKEY_SIZE)
591
596# define DKTP_ASYMMETRIC_VERIFY_KEY_SIZE (QSC_SPHINCSPLUS_PUBLICKEY_SIZE)
597
602# define DKTP_ASYMMETRIC_SIGNATURE_SIZE (QSC_SPHINCSPLUS_SIGNATURE_SIZE)
603
604#else
605# error invalid parameter set!
606#endif
607
608/* public key encoding constants */
609
614#define DKTP_REMOTE_PEER_KEY_ENCODED_SIZE (DKTP_KEYID_SIZE + DKTP_TIMESTAMP_SIZE + DKTP_CONFIG_SIZE + DKTP_SECRET_SIZE + DKTP_ASYMMETRIC_VERIFY_KEY_SIZE)
615
620#define DKTP_LOCAL_PEER_KEY_ENCODED_SIZE (DKTP_KEYID_SIZE + DKTP_KEYID_SIZE + DKTP_TIMESTAMP_SIZE + DKTP_CONFIG_SIZE + DKTP_SECRET_SIZE + DKTP_ASYMMETRIC_SIGNING_KEY_SIZE + DKTP_ASYMMETRIC_VERIFY_KEY_SIZE)
621
622/* error code strings */
623
628#define DKTP_ERROR_STRING_DEPTH 30U
629
634#define DKTP_ERROR_STRING_WIDTH 128U
635
637static const char DKTP_ERROR_STRINGS[DKTP_ERROR_STRING_DEPTH][DKTP_ERROR_STRING_WIDTH] =
638{
639 "No error was detected",
640 "The socket accept function returned an error",
641 "The symmetric cipher had an authentication failure",
642 "The keep alive check failed",
643 "The communications channel has failed",
644 "The device could not make a connection to the remote host",
645 "The transmission failed at the KEX connection phase",
646 "The asymmetric cipher failed to decapsulate the shared secret",
647 "The decryption authentication has failed",
648 "The transmission failed at the KEX establish phase",
649 "The transmission failed at the KEX exchange phase",
650 "The public - key hash is invalid",
651 "The server has run out of socket connections",
652 "The expected input was invalid",
653 "The packet flag was unexpected",
654 "The keep alive has expired with no response",
655 "The decryption authentication has failed",
656 "The DKTP public key has expired ",
657 "The key identity is unrecognized",
658 "The ratchet operation has failed",
659 "The listener function failed to initialize",
660 "The server has run out of memory",
661 "The packet has valid time expired",
662 "The packet was received out of sequence",
663 "The random generator has failed",
664 "The receiver failed at the network layer",
665 "The transmitter failed at the network layer",
666 "The protocol string was not recognized",
667 "The expected data could not be verified",
668 "The remote peer key identity does not match the local key",
669};
671
676#define DKTP_MESSAGE_STRING_DEPTH 21U
681#define DKTP_MESSAGE_STRING_WIDTH 128U
682
684static const char DKTP_MESSAGE_STRINGS[DKTP_MESSAGE_STRING_DEPTH][DKTP_MESSAGE_STRING_WIDTH] =
685{
686 "The operation completed succesfully.",
687 "The socket server accept function failed.",
688 "The listener socket listener could not connect.",
689 "The listener socket could not bind to the address.",
690 "The listener socket could not be created.",
691 "The server is connected to remote host: ",
692 "The socket receive function failed.",
693 "The server had a memory allocation failure.",
694 "The key exchange has experienced a failure.",
695 "The server has disconnected from the remote host: ",
696 "The server has disconnected the client due to an error",
697 "The server has had a socket level error: ",
698 "The server has reached the maximum number of connections",
699 "The server listener socket has failed.",
700 "The server has run out of socket connections",
701 "The message decryption has failed",
702 "The keepalive function has failed",
703 "The keepalive period has been exceeded",
704 "The connection failed or was interrupted",
705 "The function received an invalid request",
706 "The remote peer identity does not match the local key",
707};
709
725
754
792
823
828DKTP_EXPORT_API typedef struct dktp_asymmetric_cipher_keypair
829{
830 uint8_t* deckey;
831 uint8_t* enckey;
833
838DKTP_EXPORT_API typedef struct dktp_asymmetric_signature_keypair
839{
840 uint8_t* sigkey;
841 uint8_t* verkey;
843
848DKTP_EXPORT_API typedef struct dktp_network_packet
849{
850 uint8_t flag;
851 uint32_t msglen;
852 uint64_t sequence;
853 uint64_t utctime;
854 uint8_t* pmessage;
856
869
884
889DKTP_EXPORT_API typedef struct dktp_keepalive_state
890{
891 qsc_socket target;
892 uint64_t etime;
893 uint64_t seqctr;
894 bool recd;
896
901DKTP_EXPORT_API typedef struct dktp_connection_state
902{
903 qsc_socket target;
904 qsc_rcs_state rxcpr;
905 qsc_rcs_state txcpr;
906#if defined(DKTP_ASYMMETRIC_RATCHET)
913#endif
914 uint64_t rxseq;
915 uint64_t txseq;
916 uint32_t cid;
918 bool receiver;
920
928DKTP_EXPORT_API void dktp_connection_close(dktp_connection_state* cns, dktp_errors err, bool notify);
929
936
944DKTP_EXPORT_API const char* dktp_error_to_string(dktp_errors error);
945
954DKTP_EXPORT_API void dktp_header_create(dktp_network_packet* packetout, dktp_flags flag, uint64_t sequence, uint32_t msglen);
955
968DKTP_EXPORT_API dktp_errors dktp_header_validate(dktp_connection_state* cns, const dktp_network_packet* packetin, dktp_flags kexflag, dktp_flags pktflag, uint64_t sequence, uint32_t msglen);
969
977DKTP_EXPORT_API void dktp_generate_keypair(dktp_remote_peer_key* enckey, dktp_local_peer_key* deckey, const uint8_t keyid[DKTP_KEYID_SIZE]);
978
986DKTP_EXPORT_API const char* dktp_get_error_description(dktp_messages emsg);
987
995DKTP_EXPORT_API void dktp_log_error(dktp_messages emsg, qsc_socket_exceptions err, const char* msg);
996
1002DKTP_EXPORT_API void dktp_log_message(dktp_messages emsg);
1003
1010DKTP_EXPORT_API void dktp_log_write(dktp_messages emsg, const char* msg);
1011
1017DKTP_EXPORT_API void dktp_packet_clear(dktp_network_packet* packet);
1018
1029DKTP_EXPORT_API dktp_errors dktp_packet_decrypt(dktp_connection_state* cns, uint8_t* message, size_t* msglen, const dktp_network_packet* packetin);
1030
1041DKTP_EXPORT_API dktp_errors dktp_packet_encrypt(dktp_connection_state* cns, dktp_network_packet* packetout, const uint8_t* message, size_t msglen);
1042
1049DKTP_EXPORT_API void dktp_packet_error_message(dktp_network_packet* packet, dktp_errors error);
1050
1057DKTP_EXPORT_API void dktp_packet_header_deserialize(const uint8_t* header, dktp_network_packet* packet);
1058
1065DKTP_EXPORT_API void dktp_packet_header_serialize(const dktp_network_packet* packet, uint8_t* header);
1066
1072DKTP_EXPORT_API void dktp_packet_set_utc_time(dktp_network_packet* packet);
1073
1081DKTP_EXPORT_API bool dktp_packet_time_valid(const dktp_network_packet* packet);
1082
1091DKTP_EXPORT_API size_t dktp_packet_to_stream(const dktp_network_packet* packet, uint8_t* pstream);
1092
1099DKTP_EXPORT_API void dktp_local_peer_key_deserialize(dktp_local_peer_key* lpk, const uint8_t slpk[DKTP_LOCAL_PEER_KEY_ENCODED_SIZE]);
1100
1106DKTP_EXPORT_API void dktp_local_peer_key_erase(dktp_local_peer_key* lpk);
1107
1114DKTP_EXPORT_API void dktp_local_peer_key_serialize(uint8_t slpk[DKTP_LOCAL_PEER_KEY_ENCODED_SIZE], const dktp_local_peer_key* lpk);
1115
1121DKTP_EXPORT_API void dktp_remote_peer_key_erase(dktp_remote_peer_key* rpk);
1122
1131DKTP_EXPORT_API bool dktp_remote_peer_key_compare(const dktp_remote_peer_key* a, const dktp_remote_peer_key* b);
1132
1140
1147DKTP_EXPORT_API void dktp_remote_peer_key_serialize(uint8_t srpk[DKTP_REMOTE_PEER_KEY_ENCODED_SIZE], const dktp_remote_peer_key* rpk);
1148
1155DKTP_EXPORT_API void dktp_stream_to_packet(const uint8_t* pstream, dktp_network_packet* packet);
1156
1157#endif
#define DKTP_REMOTE_PEER_KEY_ENCODED_SIZE
The peer key size.
Definition dktp.h:614
#define DKTP_ERROR_STRING_DEPTH
The depth of the DKTP error string array.
Definition dktp.h:628
DKTP_EXPORT_API void dktp_generate_keypair(dktp_remote_peer_key *enckey, dktp_local_peer_key *deckey, const uint8_t keyid[DKTP_KEYID_SIZE])
Generate a DKTP key-pair; generates the public and private asymmetric signature keys.
Definition dktp.c:180
DKTP_EXPORT_API void dktp_packet_clear(dktp_network_packet *packet)
Clear a packet's state.
Definition dktp.c:297
#define DKTP_ASYMMETRIC_SIGNING_KEY_SIZE
The byte size of the asymmetric signature signing-key array.
Definition dktp.h:411
#define DKTP_DOMAIN_IDENTITY_SIZE
The size of the domain identity string. This string size can be modified to accomodate different leng...
Definition dktp.h:126
DKTP_EXPORT_API dktp_errors dktp_packet_decrypt(dktp_connection_state *cns, uint8_t *message, size_t *msglen, const dktp_network_packet *packetin)
Decrypt a message and copy it to the message output.
Definition dktp.c:310
DKTP_EXPORT_API void dktp_connection_close(dktp_connection_state *cns, dktp_errors err, bool notify)
Close the network connection between hosts.
Definition dktp.c:11
DKTP_EXPORT_API bool dktp_packet_time_valid(const dktp_network_packet *packet)
Checks the local UTC seconds time against the packet sent time for validity within the packet time th...
Definition dktp.c:463
DKTP_EXPORT_API void dktp_local_peer_key_serialize(uint8_t slpk[DKTP_LOCAL_PEER_KEY_ENCODED_SIZE], const dktp_local_peer_key *lpk)
Serialize a local peer key structure.
Definition dktp.c:504
#define DKTP_CONFIG_SIZE
The size of the protocol configuration string.
Definition dktp.h:148
DKTP_EXPORT_API void dktp_log_message(dktp_messages emsg)
Log a message.
Definition dktp.c:260
DKTP_EXPORT_API void dktp_packet_set_utc_time(dktp_network_packet *packet)
Sets the local UTC seconds time in the packet header.
Definition dktp.c:458
#define DKTP_LOCAL_PEER_KEY_ENCODED_SIZE
The secret signature key size.
Definition dktp.h:620
DKTP_EXPORT_API bool dktp_remote_peer_key_compare(const dktp_remote_peer_key *a, const dktp_remote_peer_key *b)
Compares two public keys for equality.
Definition dktp.c:525
dktp_flags
The DKTP packet flags.
Definition dktp.h:798
@ dktp_flag_establish_response
Definition dktp.h:809
@ dktp_flag_session_establish_verify
Definition dktp.h:815
@ dktp_flag_unrecognized_protocol
Definition dktp.h:816
@ dktp_flag_establish_request
Definition dktp.h:808
@ dktp_flag_exchange_response
Definition dktp.h:807
@ dktp_flag_keep_alive_request
Definition dktp.h:810
@ dktp_flag_keep_alive_response
Definition dktp.h:811
@ dktp_flag_encrypted_message
Definition dktp.h:803
@ dktp_flag_asymmetric_ratchet_response
Definition dktp.h:818
@ dktp_flag_none
Definition dktp.h:799
@ dktp_flag_exstart_request
Definition dktp.h:804
@ dktp_flag_error_condition
Definition dktp.h:821
@ dktp_flag_connect_response
Definition dktp.h:801
@ dktp_flag_remote_connected
Definition dktp.h:812
@ dktp_flag_transfer_request
Definition dktp.h:820
@ dktp_flag_exchange_request
Definition dktp.h:806
@ dktp_flag_remote_terminated
Definition dktp.h:813
@ dktp_flag_connect_request
Definition dktp.h:800
@ dktp_flag_asymmetric_ratchet_request
Definition dktp.h:817
@ dktp_flag_session_established
Definition dktp.h:814
@ dktp_flag_connection_terminate
Definition dktp.h:802
@ dktp_flag_symmetric_ratchet_request
Definition dktp.h:819
@ dktp_flag_exstart_response
Definition dktp.h:805
DKTP_EXPORT_API const char * dktp_error_to_string(dktp_errors error)
Return a pointer to a string description of an error code.
Definition dktp.c:105
DKTP_EXPORT_API void dktp_connection_state_dispose(dktp_connection_state *cns)
Reset the connection state.
Definition dktp.c:80
#define DKTP_MESSAGE_STRING_DEPTH
The depth of the DKTP message string array.
Definition dktp.h:676
DKTP_EXPORT_API void dktp_local_peer_key_erase(dktp_local_peer_key *lpk)
Erase a local peer key.
Definition dktp.c:493
DKTP_EXPORT_API size_t dktp_packet_to_stream(const dktp_network_packet *packet, uint8_t *pstream)
Serialize a packet to a byte array.
Definition dktp.c:612
#define DKTP_MESSAGE_STRING_WIDTH
The width of each DKTP message string.
Definition dktp.h:681
DKTP_EXPORT_API void dktp_header_create(dktp_network_packet *packetout, dktp_flags flag, uint64_t sequence, uint32_t msglen)
Populate a packet header and set the creation time.
Definition dktp.c:119
dktp_messages
The logging message enumeration.
Definition dktp.h:731
@ dktp_messages_connect_success
Definition dktp.h:737
@ dktp_messages_peer_key_mismatch
Definition dktp.h:752
@ dktp_messages_keepalive_fail
Definition dktp.h:748
@ dktp_messages_listen_fail
Definition dktp.h:734
@ dktp_messages_connection_fail
Definition dktp.h:750
@ dktp_messages_queue_empty
Definition dktp.h:744
@ dktp_messages_listener_fail
Definition dktp.h:745
@ dktp_messages_socket_message
Definition dktp.h:743
@ dktp_messages_none
Definition dktp.h:732
@ dktp_messages_create_fail
Definition dktp.h:736
@ dktp_messages_kex_fail
Definition dktp.h:740
@ dktp_messages_disconnect_fail
Definition dktp.h:742
@ dktp_messages_decryption_fail
Definition dktp.h:747
@ dktp_messages_bind_fail
Definition dktp.h:735
@ dktp_messages_allocate_fail
Definition dktp.h:739
@ dktp_messages_accept_fail
Definition dktp.h:733
@ dktp_messages_sockalloc_fail
Definition dktp.h:746
@ dktp_messages_keepalive_timeout
Definition dktp.h:749
@ dktp_messages_receive_fail
Definition dktp.h:738
@ dktp_messages_invalid_request
Definition dktp.h:751
@ dktp_messages_disconnect
Definition dktp.h:741
DKTP_EXPORT_API dktp_errors dktp_header_validate(dktp_connection_state *cns, const dktp_network_packet *packetin, dktp_flags kexflag, dktp_flags pktflag, uint64_t sequence, uint32_t msglen)
Validate a packet header and timestamp.
Definition dktp.c:128
#define DKTP_SECRET_SIZE
The size of the shared secret for each channel.
Definition dktp.h:310
dktp_errors
The DKTP error values.
Definition dktp.h:760
@ dktp_error_none
Definition dktp.h:761
@ dktp_error_transmit_failure
Definition dktp.h:787
@ dktp_error_hash_invalid
Definition dktp.h:772
@ dktp_error_key_unrecognized
Definition dktp.h:779
@ dktp_error_decapsulation_failure
Definition dktp.h:768
@ dktp_error_receive_failure
Definition dktp.h:786
@ dktp_error_memory_allocation
Definition dktp.h:782
@ dktp_error_decryption_failure
Definition dktp.h:769
@ dktp_error_exchange_failure
Definition dktp.h:771
@ dktp_error_unknown_protocol
Definition dktp.h:788
@ dktp_error_peer_key_mismatch
Definition dktp.h:790
@ dktp_error_random_failure
Definition dktp.h:785
@ dktp_error_keychain_fail
Definition dktp.h:780
@ dktp_error_verify_failure
Definition dktp.h:789
@ dktp_error_packet_unsequenced
Definition dktp.h:784
@ dktp_error_invalid_input
Definition dktp.h:774
@ dktp_error_invalid_request
Definition dktp.h:775
@ dktp_error_accept_fail
Definition dktp.h:762
@ dktp_error_hosts_exceeded
Definition dktp.h:773
@ dktp_error_message_time_invalid
Definition dktp.h:783
@ dktp_error_bad_keep_alive
Definition dktp.h:764
@ dktp_error_establish_failure
Definition dktp.h:770
@ dktp_error_keepalive_expired
Definition dktp.h:776
@ dktp_error_connection_failure
Definition dktp.h:766
@ dktp_error_authentication_failure
Definition dktp.h:763
@ dktp_error_keepalive_timeout
Definition dktp.h:777
@ dktp_error_connect_failure
Definition dktp.h:767
@ dktp_error_listener_fail
Definition dktp.h:781
@ dktp_error_channel_down
Definition dktp.h:765
@ dktp_error_key_expired
Definition dktp.h:778
DKTP_EXPORT_API void dktp_packet_error_message(dktp_network_packet *packet, dktp_errors error)
Populate a packet structure with an error message.
Definition dktp.c:406
DKTP_EXPORT_API void dktp_local_peer_key_deserialize(dktp_local_peer_key *lpk, const uint8_t slpk[DKTP_LOCAL_PEER_KEY_ENCODED_SIZE])
Deserialize a local peer key structure and copy to an array.
Definition dktp.c:472
#define DKTP_ASYMMETRIC_VERIFY_KEY_SIZE
The byte size of the asymmetric signature verification-key array.
Definition dktp.h:417
DKTP_EXPORT_API void dktp_remote_peer_key_deserialize(dktp_remote_peer_key *rpk, const uint8_t srpk[DKTP_REMOTE_PEER_KEY_ENCODED_SIZE])
Deserialize a remote peer key and populate a remote peer key structure.
Definition dktp.c:548
DKTP_EXPORT_API void dktp_remote_peer_key_serialize(uint8_t srpk[DKTP_REMOTE_PEER_KEY_ENCODED_SIZE], const dktp_remote_peer_key *rpk)
Serialize a remote peer key structure and copy to an array.
Definition dktp.c:574
DKTP_EXPORT_API void dktp_packet_header_serialize(const dktp_network_packet *packet, uint8_t *header)
Serialize a packet header to a byte array.
Definition dktp.c:439
#define DKTP_ASYMMETRIC_ENCAPSULATION_KEY_SIZE
The byte size of the asymmetric cipher public-key array.
Definition dktp.h:405
DKTP_EXPORT_API const char * dktp_get_error_description(dktp_messages emsg)
Get the error string description.
Definition dktp.c:208
#define DKTP_ERROR_STRING_WIDTH
The width of each DKTP error string.
Definition dktp.h:634
DKTP_EXPORT_API void dktp_remote_peer_key_erase(dktp_remote_peer_key *rpk)
Erase a remote peer key.
Definition dktp.c:565
dktp_configuration
The asymmetric cryptographic primitive configuration.
Definition dktp.h:715
@ dktp_configuration_dilithium_ntru
Definition dktp.h:720
@ dktp_configuration_dilithium_kyber
Definition dktp.h:718
@ dktp_configuration_falcon_ntru
Definition dktp.h:723
@ dktp_configuration_falcon_kyber
Definition dktp.h:721
@ dktp_configuration_none
Definition dktp.h:716
@ dktp_configuration_falcon_mceliece
Definition dktp.h:722
@ dktp_configuration_dilithium_mceliece
Definition dktp.h:719
@ dktp_configuration_sphincs_mceliece
Definition dktp.h:717
DKTP_EXPORT_API void dktp_packet_header_deserialize(const uint8_t *header, dktp_network_packet *packet)
Deserialize a byte array to a packet header.
Definition dktp.c:420
DKTP_EXPORT_API void dktp_log_write(dktp_messages emsg, const char *msg)
Log a message and description.
Definition dktp.c:270
DKTP_EXPORT_API void dktp_stream_to_packet(const uint8_t *pstream, dktp_network_packet *packet)
Deserialize a byte array to a packet.
Definition dktp.c:591
DKTP_EXPORT_API dktp_errors dktp_packet_encrypt(dktp_connection_state *cns, dktp_network_packet *packetout, const uint8_t *message, size_t msglen)
Encrypt a message and build an output packet.
Definition dktp.c:369
#define DKTP_ASYMMETRIC_DECAPSULATION_KEY_SIZE
The byte size of the asymmetric cipher private-key array.
Definition dktp.h:399
DKTP_EXPORT_API void dktp_log_error(dktp_messages emsg, qsc_socket_exceptions err, const char *msg)
Log the message, socket error, and string description.
Definition dktp.c:223
#define DKTP_KEYID_SIZE
The DKTP key identity size.
Definition dktp.h:253
The DKTP asymmetric cipher key container.
Definition dktp.h:829
The DKTP asymmetric signature key container.
Definition dktp.h:839
The DKTP socket connection state structure.
Definition dktp.h:902
uint8_t enckey[DKTP_ASYMMETRIC_ENCAPSULATION_KEY_SIZE]
Definition dktp.h:908
uint64_t txseq
Definition dktp.h:915
dktp_flags exflag
Definition dktp.h:917
uint8_t pssr[DKTP_SECRET_SIZE]
Definition dktp.h:910
bool receiver
Definition dktp.h:918
qsc_rcs_state txcpr
Definition dktp.h:905
uint64_t rxseq
Definition dktp.h:914
uint8_t verkey[DKTP_ASYMMETRIC_VERIFY_KEY_SIZE]
Definition dktp.h:912
uint8_t deckey[DKTP_ASYMMETRIC_DECAPSULATION_KEY_SIZE]
Definition dktp.h:907
uint8_t pssl[DKTP_SECRET_SIZE]
Definition dktp.h:909
uint8_t sigkey[DKTP_ASYMMETRIC_SIGNING_KEY_SIZE]
Definition dktp.h:911
uint32_t cid
Definition dktp.h:916
qsc_socket target
Definition dktp.h:903
qsc_rcs_state rxcpr
Definition dktp.h:904
The DKTP keep alive state structure.
Definition dktp.h:890
bool recd
Definition dktp.h:894
uint64_t seqctr
Definition dktp.h:893
qsc_socket target
Definition dktp.h:891
uint64_t etime
Definition dktp.h:892
The DKTP server key structure.
Definition dktp.h:875
uint8_t peerid[DKTP_KEYID_SIZE]
Definition dktp.h:879
uint8_t pss[DKTP_SECRET_SIZE]
Definition dktp.h:880
uint64_t expiration
Definition dktp.h:876
uint8_t config[DKTP_CONFIG_SIZE]
Definition dktp.h:877
uint8_t keyid[DKTP_KEYID_SIZE]
Definition dktp.h:878
uint8_t verkey[DKTP_ASYMMETRIC_VERIFY_KEY_SIZE]
Definition dktp.h:882
uint8_t sigkey[DKTP_ASYMMETRIC_SIGNING_KEY_SIZE]
Definition dktp.h:881
The DKTP packet structure.
Definition dktp.h:849
uint8_t * pmessage
Definition dktp.h:854
uint32_t msglen
Definition dktp.h:851
uint64_t utctime
Definition dktp.h:853
uint64_t sequence
Definition dktp.h:852
uint8_t flag
Definition dktp.h:850
The DKTP client key structure.
Definition dktp.h:862
uint8_t pss[DKTP_SECRET_SIZE]
Definition dktp.h:866
uint64_t expiration
Definition dktp.h:863
uint8_t config[DKTP_CONFIG_SIZE]
Definition dktp.h:864
uint8_t keyid[DKTP_KEYID_SIZE]
Definition dktp.h:865
uint8_t verkey[DKTP_ASYMMETRIC_VERIFY_KEY_SIZE]
Definition dktp.h:867