QSMP: Quantum Secure Messaging Protocol 1.3.0.0a (A3)
A quantum secure encrypted tunneling protocol
qsmp.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: john.underhill@protonmail.com
38 */
39
40#ifndef QSMP_H
41#define QSMP_H
42
43#include "../../QSC/QSC/rcs.h"
44#include "../../QSC/QSC/sha3.h"
45
86
91#define QSMP_CONFIG_DILITHIUM_KYBER
92
94//* \def QSMP_CONFIG_DILITHIUM_MCELIECE
95//* \brief Sets the asymmetric cryptographic primitive-set to Dilithium/McEliece.
96//*/
97//#define QSMP_CONFIG_DILITHIUM_MCELIECE
98
100//* \def QSMP_CONFIG_SPHINCS_MCELIECE
101//* \brief Sets the asymmetric cryptographic primitive-set to Sphincs+/McEliece.
102//*/
103//#define QSMP_CONFIG_SPHINCS_MCELIECE
104
105#include "common.h"
106#include "../../QSC/QSC/socketbase.h"
107
108#if defined(QSMP_CONFIG_DILITHIUM_KYBER)
109# include "../../QSC/QSC/dilithium.h"
110# include "../../QSC/QSC/kyber.h"
111#elif defined(QSMP_CONFIG_DILITHIUM_MCELIECE)
112# include "../../QSC/QSC/dilithium.h"
113# include "../../QSC/QSC/mceliece.h"
114#elif defined(QSMP_CONFIG_SPHINCS_MCELIECE)
115# include "../../QSC/QSC/sphincsplus.h"
116# include "../../QSC/QSC/mceliece.h"
117#else
118# error Invalid parameter set!
119#endif
120
122//* \def QSMP_ASYMMETRIC_RATCHET
123//* \brief Enable the asymmetric ratchet option
124//*/
125//#define QSMP_ASYMMETRIC_RATCHET
126
131#define QSMP_CONFIG_SIZE 48
132
137#define QSMP_SIMPLEX_HASH_SIZE 32
138
143#define QSMP_SIMPLEX_MACKEY_SIZE 32
144
149#define QSMP_SIMPLEX_MACTAG_SIZE 32
150
155#define QSMP_SIMPLEX_SYMMETRIC_KEY_SIZE 32
156
161#define QSMP_SIMPLEX_SCHASH_SIZE 32
162
167#define QSMP_DUPLEX_HASH_SIZE 64
168
173#define QSMP_DUPLEX_MACKEY_SIZE 64
174
179#define QSMP_DUPLEX_MACTAG_SIZE 64
180
185#define QSMP_DUPLEX_SYMMETRIC_KEY_SIZE 64
186
191#define QSMP_DUPLEX_SCHASH_SIZE 64
192
197#define QSMP_ASYMMETRIC_KEYCHAIN_COUNT 10
198
203#define QSMP_CLIENT_PORT 31118
204
209#define QSMP_CONNECTIONS_INIT 1000
210
218#define QSMP_CONNECTIONS_MAX 50000
219
224#define QSMP_CONNECTION_MTU 1500
225
230#define QSMP_ERROR_SEQUENCE 0xFF00000000000000ULL
231
236#define QSMP_ERROR_MESSAGE_SIZE 1
237
242#define QSMP_FLAG_SIZE 1
243
248#define QSMP_HEADER_SIZE 21
249
254#define QSMP_KEEPALIVE_STRING 20
255
260#define QSMP_KEEPALIVE_TIMEOUT (120 * 1000)
261
266#define QSMP_KEYID_SIZE 16
267
272#define QSMP_MSGLEN_SIZE 4
273
278#define QSMP_NETWORK_MTU_SIZE 1500
279
284#define QSMP_NONCE_SIZE 32
285
290#define QSMP_RTOK_SIZE 32
291
296#define QSMP_SERVER_PORT 31119
297
305#define QSMP_PACKET_TIME_THRESHOLD 60
306
311#define QSMP_POLLING_INTERVAL (120 * 1000)
312
317#define QSMP_PUBKEY_DURATION_DAYS 365
318
323#define QSMP_PUBKEY_DURATION_SECONDS (QSMP_PUBKEY_DURATION_DAYS * 24 * 60 * 60)
324
329#define QSMP_PUBKEY_LINE_LENGTH 64
330
335#define QSMP_SECRET_SIZE 32
336
341#define QSMP_SEQUENCE_SIZE 8
342
347#define QSMP_SEQUENCE_TERMINATOR 0xFFFFFFFFUL
348
353#define QSMP_SRVID_SIZE 8
354
359#define QSMP_STOKEN_SIZE 64
360
365#define QSMP_TIMESTAMP_SIZE 8
366
371#define QSMP_TIMESTAMP_STRING_SIZE 20
372
377#define QSMP_MESSAGE_MAX 0x3D090000
378
379#if defined(QSMP_CONFIG_DILITHIUM_KYBER)
380
385# define qsmp_cipher_generate_keypair qsc_kyber_generate_keypair
390# define qsmp_cipher_decapsulate qsc_kyber_decapsulate
395# define qsmp_cipher_encapsulate qsc_kyber_encapsulate
400# define qsmp_signature_generate_keypair qsc_dilithium_generate_keypair
405# define qsmp_signature_sign qsc_dilithium_sign
410# define qsmp_signature_verify qsc_dilithium_verify
411
413# if defined(QSC_DILITHIUM_S1P2544) && defined(QSC_KYBER_S1P1632)
414static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "dilithium-s1_kyber-s1_sha3_rcs";
415# elif defined(QSC_DILITHIUM_S3P4016) && defined(QSC_KYBER_S3P2400)
416static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "dilithium-s3_kyber-s3_sha3_rcs";
417# elif defined(QSC_DILITHIUM_S5P4880) && defined(QSC_KYBER_S5P3168)
418static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "dilithium-s5_kyber-s5_sha3_rcs";
419# elif defined(QSC_DILITHIUM_S5P4880) && defined(QSC_KYBER_S6P3936)
420static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "dilithium-s5_kyber-s6_sha3_rcs";
421# else
422# error Invalid parameter set!
423# endif
425
430# define QSMP_ASYMMETRIC_CIPHER_TEXT_SIZE (QSC_KYBER_CIPHERTEXT_SIZE)
431
436# define QSMP_ASYMMETRIC_PRIVATE_KEY_SIZE (QSC_KYBER_PRIVATEKEY_SIZE)
437
442# define QSMP_ASYMMETRIC_PUBLIC_KEY_SIZE (QSC_KYBER_PUBLICKEY_SIZE)
443
448# define QSMP_ASYMMETRIC_SIGNING_KEY_SIZE (QSC_DILITHIUM_PRIVATEKEY_SIZE)
449
454# define QSMP_ASYMMETRIC_VERIFY_KEY_SIZE (QSC_DILITHIUM_PUBLICKEY_SIZE)
455
460# define QSMP_ASYMMETRIC_SIGNATURE_SIZE (QSC_DILITHIUM_SIGNATURE_SIZE)
461
462#elif defined(QSMP_CONFIG_DILITHIUM_MCELIECE)
467# define qsmp_cipher_generate_keypair qsc_mceliece_generate_keypair
472# define qsmp_cipher_decapsulate qsc_mceliece_decapsulate
477# define qsmp_cipher_encapsulate qsc_mceliece_encapsulate
482# define qsmp_signature_generate_keypair qsc_dilithium_generate_keypair
487# define qsmp_signature_sign qsc_dilithium_sign
492# define qsmp_signature_verify qsc_dilithium_verify
493
495# if defined(QSC_DILITHIUM_S1P2544) && defined(QSC_MCELIECE_S1N3488T64)
496static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "dilithium-s1_mceliece-s1_sha3_rcs";
497# elif defined(QSC_DILITHIUM_S3P4016) && defined(QSC_MCELIECE_S3N4608T96)
498static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "dilithium-s3_mceliece-s3_sha3_rcs";
499# elif defined(QSC_DILITHIUM_S5P4880) && defined(QSC_MCELIECE_S5N6688T128)
500static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "dilithium-s5_mceliece-s5_sha3_rcs";
501# elif defined(QSC_DILITHIUM_S5P4880) && defined(QSC_MCELIECE_S6N6960T119)
502static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "dilithium-s5_mceliece-s6_sha3_rcs";
503# elif defined(QSC_DILITHIUM_S5P4880) && defined(QSC_MCELIECE_S7N8192T128)
504static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "dilithium-s5_mceliece-s7_sha3_rcs";
505# else
506# error Invalid parameter set!
507# endif
509
514# define QSMP_ASYMMETRIC_CIPHER_TEXT_SIZE (QSC_MCELIECE_CIPHERTEXT_SIZE)
515
520# define QSMP_ASYMMETRIC_PRIVATE_KEY_SIZE (QSC_MCELIECE_PRIVATEKEY_SIZE)
521
526# define QSMP_ASYMMETRIC_PUBLIC_KEY_SIZE (QSC_MCELIECE_PUBLICKEY_SIZE)
527
532# define QSMP_ASYMMETRIC_SIGNING_KEY_SIZE (QSC_DILITHIUM_PRIVATEKEY_SIZE)
533
538# define QSMP_ASYMMETRIC_VERIFY_KEY_SIZE (QSC_DILITHIUM_PUBLICKEY_SIZE)
539
544# define QSMP_ASYMMETRIC_SIGNATURE_SIZE (QSC_DILITHIUM_SIGNATURE_SIZE)
545
546#elif defined(QSMP_CONFIG_SPHINCS_MCELIECE)
547
552# define qsmp_cipher_generate_keypair qsc_mceliece_generate_keypair
557# define qsmp_cipher_decapsulate qsc_mceliece_decapsulate
562# define qsmp_cipher_encapsulate qsc_mceliece_encapsulate
567# define qsmp_signature_generate_keypair qsc_sphincsplus_generate_keypair
572# define qsmp_signature_sign qsc_sphincsplus_sign
577# define qsmp_signature_verify qsc_sphincsplus_verify
578
580# if defined(QSC_SPHINCSPLUS_S1S128SHAKERF) && defined(QSC_MCELIECE_S1N3488T64)
581static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "sphincs-s1f_mceliece-s1_sha3_rcs";
582# elif defined(QSC_SPHINCSPLUS_S1S128SHAKERS) && defined(QSC_MCELIECE_S1N3488T64)
583static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "sphincs-s1s_mceliece-s1_sha3_rcs";
584# elif defined(QSC_SPHINCSPLUS_S3S192SHAKERF) && defined(QSC_MCELIECE_S3N4608T96)
585static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "sphincs-3f_mceliece-s3_sha3_rcs";
586# elif defined(QSC_SPHINCSPLUS_S3S192SHAKERS) && defined(QSC_MCELIECE_S3N4608T96)
587static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "sphincs-3s_mceliece-s3_sha3_rcs";
588# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERF) && defined(QSC_MCELIECE_S5N6688T128)
589static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "sphincs-s5f_mceliece-s5_sha3_rcs";
590# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERS) && defined(QSC_MCELIECE_S5N6688T128)
591static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "sphincs-s5s_mceliece-s5_sha3_rcs";
592# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERF) && defined(QSC_MCELIECE_S6N6960T119)
593static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "sphincs-s5f_mceliece-s6_sha3_rcs";
594# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERS) && defined(QSC_MCELIECE_S6N6960T119)
595static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "sphincs-s5s_mceliece-s6_sha3_rcs";
596# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERF) && defined(QSC_MCELIECE_S7N8192T128)
597static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "sphincs-s5f_mceliece-s7_sha3_rcs";
598# elif defined(QSC_SPHINCSPLUS_S5S256SHAKERS) && defined(QSC_MCELIECE_S7N8192T128)
599static const char QSMP_CONFIG_STRING[QSMP_CONFIG_SIZE] = "sphincs-s5s_mceliece-s7_sha3_rcs";
600# else
601# error Invalid parameter set!
602# endif
604
609# define QSMP_ASYMMETRIC_CIPHER_TEXT_SIZE (QSC_MCELIECE_CIPHERTEXT_SIZE)
610
615# define QSMP_ASYMMETRIC_PRIVATE_KEY_SIZE (QSC_MCELIECE_PRIVATEKEY_SIZE)
616
621# define QSMP_ASYMMETRIC_PUBLIC_KEY_SIZE (QSC_MCELIECE_PUBLICKEY_SIZE)
622
627# define QSMP_ASYMMETRIC_SIGNING_KEY_SIZE (QSC_SPHINCSPLUS_PRIVATEKEY_SIZE)
628
633# define QSMP_ASYMMETRIC_VERIFY_KEY_SIZE (QSC_SPHINCSPLUS_PUBLICKEY_SIZE)
634
639# define QSMP_ASYMMETRIC_SIGNATURE_SIZE (QSC_SPHINCSPLUS_SIGNATURE_SIZE)
640
641#else
642# error invalid parameter set!
643#endif
644
645/* public key encoding constants */
646
651#define QSMP_SIGKEY_ENCODED_SIZE (QSMP_KEYID_SIZE + QSMP_TIMESTAMP_SIZE + QSMP_CONFIG_SIZE + QSMP_ASYMMETRIC_SIGNING_KEY_SIZE + QSMP_ASYMMETRIC_VERIFY_KEY_SIZE)
652
657#define QSMP_PUBKEY_HEADER_SIZE 40
662#define QSMP_PUBKEY_VERSION_SIZE 19
667#define QSMP_PUBKEY_CONFIG_SIZE 16
672#define QSMP_PUBKEY_KEYID_SIZE 10
677#define QSMP_PUBKEY_EXPIRATION_SIZE 13
682#define QSMP_PUBKEY_FOOTER_SIZE 38
683
688static const char QSMP_PUBKEY_HEADER[QSMP_PUBKEY_HEADER_SIZE] = "------BEGIN QSMP PUBLIC KEY BLOCK------";
689
694static const char QSMP_PUBKEY_VERSION[QSMP_PUBKEY_VERSION_SIZE] = "Version: QSMP v1.2";
695
700static const char QSMP_PUBKEY_CONFIG_PREFIX[QSMP_PUBKEY_CONFIG_SIZE] = "Configuration: ";
701
706static const char QSMP_PUBKEY_KEYID_PREFIX[QSMP_PUBKEY_KEYID_SIZE] = "Host ID: ";
707
712static const char QSMP_PUBKEY_EXPIRATION_PREFIX[QSMP_PUBKEY_EXPIRATION_SIZE] = "Expiration: ";
713
718static const char QSMP_PUBKEY_FOOTER[QSMP_PUBKEY_FOOTER_SIZE] = "------END QSMP PUBLIC KEY BLOCK------";
719
720/* error code strings */
721
726#define QSMP_ERROR_STRING_DEPTH 29
731#define QSMP_ERROR_STRING_WIDTH 128
732
734static const char QSMP_ERROR_STRINGS[QSMP_ERROR_STRING_DEPTH][QSMP_ERROR_STRING_WIDTH] =
735{
736 "No error was detected",
737 "The socket accept function returned an error",
738 "The symmetric cipher had an authentication failure",
739 "The keep alive check failed",
740 "The communications channel has failed",
741 "The device could not make a connection to the remote host",
742 "The transmission failed at the KEX connection phase",
743 "The asymmetric cipher failed to decapsulate the shared secret",
744 "The decryption authentication has failed",
745 "The transmission failed at the KEX establish phase",
746 "The transmission failed at the KEX exchange phase",
747 "The public - key hash is invalid",
748 "The server has run out of socket connections",
749 "The expected input was invalid",
750 "The packet flag was unexpected",
751 "The keep alive has expired with no response",
752 "The decryption authentication has failed",
753 "The QSMP public key has expired ",
754 "The key identity is unrecognized",
755 "The ratchet operation has failed",
756 "The listener function failed to initialize",
757 "The server has run out of memory",
758 "The packet has valid time expired",
759 "The packet was received out of sequence",
760 "The random generator has failed",
761 "The receiver failed at the network layer",
762 "The transmitter failed at the network layer",
763 "The protocol string was not recognized",
764 "The expected data could not be verified",
765};
767
772#define QSMP_MESSAGE_STRING_DEPTH 22
777#define QSMP_MESSAGE_STRING_WIDTH 128
778
780static const char QSMP_MESSAGE_STRINGS[QSMP_MESSAGE_STRING_DEPTH][QSMP_MESSAGE_STRING_WIDTH] =
781{
782 "The operation completed succesfully.",
783 "The socket server accept function failed.",
784 "The listener socket listener could not connect.",
785 "The listener socket could not bind to the address.",
786 "The listener socket could not be created.",
787 "The server is connected to remote host: ",
788 "The socket receive function failed.",
789 "The server had a memory allocation failure.",
790 "The key exchange has experienced a failure.",
791 "The server has disconnected from the remote host: ",
792 "The server has disconnected the client due to an error",
793 "The server has had a socket level error: ",
794 "The server has reached the maximum number of connections",
795 "The server listener socket has failed.",
796 "The server has run out of socket connections",
797 "The message decryption has failed",
798 "The keepalive function has failed",
799 "The keepalive period has been exceeded",
800 "The connection failed or was interrupted",
801 "The function received an invalid request",
802};
804
820
848
885
916
921QSMP_EXPORT_API typedef enum qsmp_mode
922{
923 qsmp_mode_simplex = 0x00,
924 qsmp_mode_duplex = 0x01,
925} qsmp_mode;
926
931QSMP_EXPORT_API typedef struct qsmp_asymmetric_cipher_keypair
932{
933 uint8_t* prikey;
934 uint8_t* pubkey;
936
941QSMP_EXPORT_API typedef struct qsmp_asymmetric_signature_keypair
942{
943 uint8_t* sigkey;
944 uint8_t* verkey;
946
951QSMP_EXPORT_API typedef struct qsmp_network_packet
952{
953 uint8_t flag;
954 uint32_t msglen;
955 uint64_t sequence;
956 uint64_t utctime;
957 uint8_t* pmessage;
959
971
984
989QSMP_EXPORT_API typedef struct qsmp_keep_alive_state
990{
991 qsc_socket target;
992 uint64_t etime;
993 uint64_t seqctr;
994 bool recd;
996
1001QSMP_EXPORT_API typedef struct qsmp_connection_state
1002{
1004 qsc_socket target;
1005 qsc_rcs_state rxcpr;
1006 qsc_rcs_state txcpr;
1007 uint64_t rxseq;
1008 uint64_t txseq;
1009 uint32_t cid;
1014
1021
1028
1035
1042
1050QSMP_EXPORT_API void qsmp_connection_close(qsmp_connection_state* cns, qsmp_errors err, bool notify);
1051
1057QSMP_EXPORT_API void qsmp_connection_state_dispose(qsmp_connection_state* cns);
1058
1066QSMP_EXPORT_API const char* qsmp_error_to_string(qsmp_errors error);
1067
1076QSMP_EXPORT_API void qsmp_header_create(qsmp_network_packet* packetout, qsmp_flags flag, uint64_t sequence, uint32_t msglen);
1077
1090QSMP_EXPORT_API qsmp_errors qsmp_header_validate(qsmp_connection_state* cns, const qsmp_network_packet* packetin, qsmp_flags kexflag, qsmp_flags pktflag, uint64_t sequence, uint32_t msglen);
1091
1099QSMP_EXPORT_API void qsmp_generate_keypair(qsmp_client_verification_key* pubkey, qsmp_server_signature_key* prikey, const uint8_t keyid[QSMP_KEYID_SIZE]);
1100
1108QSMP_EXPORT_API const char* qsmp_get_error_description(qsmp_messages emsg);
1109
1117QSMP_EXPORT_API void qsmp_log_error(qsmp_messages emsg, qsc_socket_exceptions err, const char* msg);
1118
1124QSMP_EXPORT_API void qsmp_log_message(qsmp_messages emsg);
1125
1132QSMP_EXPORT_API void qsmp_log_write(qsmp_messages emsg, const char* msg);
1133
1139QSMP_EXPORT_API void qsmp_packet_clear(qsmp_network_packet* packet);
1140
1151QSMP_EXPORT_API qsmp_errors qsmp_packet_decrypt(qsmp_connection_state* cns, uint8_t* message, size_t* msglen, const qsmp_network_packet* packetin);
1152
1163QSMP_EXPORT_API qsmp_errors qsmp_packet_encrypt(qsmp_connection_state* cns, qsmp_network_packet* packetout, const uint8_t* message, size_t msglen);
1164
1171QSMP_EXPORT_API void qsmp_packet_error_message(qsmp_network_packet* packet, qsmp_errors error);
1172
1179QSMP_EXPORT_API void qsmp_packet_header_deserialize(const uint8_t* header, qsmp_network_packet* packet);
1180
1187QSMP_EXPORT_API void qsmp_packet_header_serialize(const qsmp_network_packet* packet, uint8_t* header);
1188
1194QSMP_EXPORT_API void qsmp_packet_set_utc_time(qsmp_network_packet* packet);
1195
1203QSMP_EXPORT_API bool qsmp_packet_time_valid(const qsmp_network_packet* packet);
1204
1213QSMP_EXPORT_API size_t qsmp_packet_to_stream(const qsmp_network_packet* packet, uint8_t* pstream);
1214
1224
1233QSMP_EXPORT_API bool qsmp_public_key_decode(qsmp_client_verification_key* pubk, const char* enck, size_t enclen);
1234
1244QSMP_EXPORT_API size_t qsmp_public_key_encode(char* enck, size_t enclen, const qsmp_client_verification_key* pubk);
1245
1251QSMP_EXPORT_API size_t qsmp_public_key_encoding_size();
1252
1259QSMP_EXPORT_API void qsmp_signature_key_deserialize(qsmp_server_signature_key* kset, const uint8_t serk[QSMP_SIGKEY_ENCODED_SIZE]);
1260
1267QSMP_EXPORT_API void qsmp_signature_key_serialize(uint8_t serk[QSMP_SIGKEY_ENCODED_SIZE], const qsmp_server_signature_key* kset);
1268
1275QSMP_EXPORT_API void qsmp_stream_to_packet(const uint8_t* pstream, qsmp_network_packet* packet);
1276
1282QSMP_EXPORT_API bool qsmp_certificate_encoding_test();
1283
1284#endif
QSMP_EXPORT_API void qsmp_packet_header_deserialize(const uint8_t *header, qsmp_network_packet *packet)
Deserialize a byte array to a packet header.
Definition qsmp.c:513
QSMP_EXPORT_API void qsmp_generate_keypair(qsmp_client_verification_key *pubkey, qsmp_server_signature_key *prikey, const uint8_t keyid[QSMP_KEYID_SIZE])
Generate a QSMP key-pair; generates the public and private asymmetric signature keys.
Definition qsmp.c:277
QSMP_EXPORT_API qsmp_errors qsmp_packet_encrypt(qsmp_connection_state *cns, qsmp_network_packet *packetout, const uint8_t *message, size_t msglen)
Encrypt a message and build an output packet.
Definition qsmp.c:461
QSMP_EXPORT_API qsmp_errors qsmp_packet_decrypt(qsmp_connection_state *cns, uint8_t *message, size_t *msglen, const qsmp_network_packet *packetin)
Decrypt a message and copy it to the message output.
Definition qsmp.c:400
QSMP_EXPORT_API size_t qsmp_packet_to_stream(const qsmp_network_packet *packet, uint8_t *pstream)
Serialize a packet to a byte array.
Definition qsmp.c:798
QSMP_EXPORT_API void qsmp_asymmetric_signature_keypair_dispose(qsmp_asymmetric_signature_keypair *keypair)
Dispose of an asymmetric signature keypair.
Definition qsmp.c:58
QSMP_EXPORT_API void qsmp_stream_to_packet(const uint8_t *pstream, qsmp_network_packet *packet)
Deserialize a byte array to a packet.
Definition qsmp.c:777
#define QSMP_ASYMMETRIC_VERIFY_KEY_SIZE
The byte size of the asymmetric signature verification-key array.
Definition qsmp.h:454
#define QSMP_ERROR_STRING_WIDTH
The width of each QSMP error string.
Definition qsmp.h:731
QSMP_EXPORT_API void qsmp_signature_key_deserialize(qsmp_server_signature_key *kset, const uint8_t serk[QSMP_SIGKEY_ENCODED_SIZE])
Decode a secret signature key structure and copy to an array.
Definition qsmp.c:743
QSMP_EXPORT_API void qsmp_asymmetric_cipher_keypair_dispose(qsmp_asymmetric_cipher_keypair *keypair)
Dispose of an asymmetric cipher keypair.
Definition qsmp.c:11
#define QSMP_MESSAGE_STRING_WIDTH
The width of each QSMP message string.
Definition qsmp.h:777
#define QSMP_PUBKEY_CONFIG_SIZE
The size of the QSMP public key configuration prefix.
Definition qsmp.h:667
QSMP_EXPORT_API void qsmp_packet_header_serialize(const qsmp_network_packet *packet, uint8_t *header)
Serialize a packet header to a byte array.
Definition qsmp.c:532
QSMP_EXPORT_API void qsmp_log_write(qsmp_messages emsg, const char *msg)
Log a message and description.
Definition qsmp.c:364
qsmp_errors
The QSMP error values.
Definition qsmp.h:854
@ qsmp_error_receive_failure
Definition qsmp.h:880
@ qsmp_error_keychain_fail
Definition qsmp.h:874
@ qsmp_error_random_failure
Definition qsmp.h:879
@ qsmp_error_connect_failure
Definition qsmp.h:861
@ qsmp_error_authentication_failure
Definition qsmp.h:857
@ qsmp_error_none
Definition qsmp.h:855
@ qsmp_error_transmit_failure
Definition qsmp.h:881
@ qsmp_error_hash_invalid
Definition qsmp.h:866
@ qsmp_error_key_expired
Definition qsmp.h:872
@ qsmp_error_channel_down
Definition qsmp.h:859
@ qsmp_error_bad_keep_alive
Definition qsmp.h:858
@ qsmp_error_connection_failure
Definition qsmp.h:860
@ qsmp_error_decryption_failure
Definition qsmp.h:863
@ qsmp_error_memory_allocation
Definition qsmp.h:876
@ qsmp_error_keepalive_timeout
Definition qsmp.h:871
@ qsmp_error_keepalive_expired
Definition qsmp.h:870
@ qsmp_error_listener_fail
Definition qsmp.h:875
@ qsmp_error_establish_failure
Definition qsmp.h:864
@ qsmp_error_accept_fail
Definition qsmp.h:856
@ qsmp_error_key_unrecognized
Definition qsmp.h:873
@ qsmp_error_exchange_failure
Definition qsmp.h:865
@ qsmp_error_decapsulation_failure
Definition qsmp.h:862
@ qsmp_error_verify_failure
Definition qsmp.h:883
@ qsmp_error_message_time_invalid
Definition qsmp.h:877
@ qsmp_error_invalid_input
Definition qsmp.h:868
@ qsmp_error_packet_unsequenced
Definition qsmp.h:878
@ qsmp_error_invalid_request
Definition qsmp.h:869
@ qsmp_error_hosts_exceeded
Definition qsmp.h:867
@ qsmp_error_unknown_protocol
Definition qsmp.h:882
QSMP_EXPORT_API qsmp_asymmetric_cipher_keypair * qsmp_asymmetric_cipher_keypair_initialize()
Initialize an asymmetric cipher keypair.
Definition qsmp.c:32
qsmp_mode
The QSMP mode enumeration.
Definition qsmp.h:922
qsmp_configuration
The asymmetric cryptographic primitive configuration.
Definition qsmp.h:810
@ qsmp_configuration_dilithium_kyber
Definition qsmp.h:813
@ qsmp_configuration_dilithium_mceliece
Definition qsmp.h:814
@ qsmp_configuration_dilithium_ntru
Definition qsmp.h:815
@ qsmp_configuration_none
Definition qsmp.h:811
@ qsmp_configuration_falcon_kyber
Definition qsmp.h:816
@ qsmp_configuration_sphincs_mceliece
Definition qsmp.h:812
@ qsmp_configuration_falcon_mceliece
Definition qsmp.h:817
@ qsmp_configuration_falcon_ntru
Definition qsmp.h:818
#define QSMP_PUBKEY_FOOTER_SIZE
The size of the QSMP public key footer.
Definition qsmp.h:682
#define QSMP_DUPLEX_SYMMETRIC_KEY_SIZE
TheDuplex 512-bit symmetric cipher key size.
Definition qsmp.h:185
QSMP_EXPORT_API bool qsmp_certificate_encoding_test()
Test the certificate encoding and decoding functions.
Definition qsmp.c:825
QSMP_EXPORT_API bool qsmp_packet_time_valid(const qsmp_network_packet *packet)
Checks the local UTC seconds time against the packet sent time for validity within the packet time th...
Definition qsmp.c:556
QSMP_EXPORT_API void qsmp_packet_set_utc_time(qsmp_network_packet *packet)
Sets the local UTC seconds time in the packet header.
Definition qsmp.c:551
qsmp_flags
The QSMP packet flags.
Definition qsmp.h:891
@ qsmp_flag_connection_terminate
Definition qsmp.h:895
@ qsmp_flag_asymmetric_ratchet_response
Definition qsmp.h:911
@ qsmp_flag_session_establish_verify
Definition qsmp.h:908
@ qsmp_flag_establish_response
Definition qsmp.h:902
@ qsmp_flag_exstart_response
Definition qsmp.h:898
@ qsmp_flag_encrypted_message
Definition qsmp.h:896
@ qsmp_flag_keep_alive_response
Definition qsmp.h:904
@ qsmp_flag_remote_terminated
Definition qsmp.h:906
@ qsmp_flag_unrecognized_protocol
Definition qsmp.h:909
@ qsmp_flag_transfer_request
Definition qsmp.h:913
@ qsmp_flag_symmetric_ratchet_request
Definition qsmp.h:912
@ qsmp_flag_exchange_request
Definition qsmp.h:899
@ qsmp_flag_exstart_request
Definition qsmp.h:897
@ qsmp_flag_connect_request
Definition qsmp.h:893
@ qsmp_flag_asymmetric_ratchet_request
Definition qsmp.h:910
@ qsmp_flag_remote_connected
Definition qsmp.h:905
@ qsmp_flag_session_established
Definition qsmp.h:907
@ qsmp_flag_none
Definition qsmp.h:892
@ qsmp_flag_keep_alive_request
Definition qsmp.h:903
@ qsmp_flag_error_condition
Definition qsmp.h:914
@ qsmp_flag_connect_response
Definition qsmp.h:894
@ qsmp_flag_exchange_response
Definition qsmp.h:900
@ qsmp_flag_establish_request
Definition qsmp.h:901
QSMP_EXPORT_API const char * qsmp_get_error_description(qsmp_messages emsg)
Get the error string description.
Definition qsmp.c:302
#define QSMP_ASYMMETRIC_SIGNING_KEY_SIZE
The byte size of the asymmetric signature signing-key array.
Definition qsmp.h:448
#define QSMP_PUBKEY_EXPIRATION_SIZE
The size of the QSMP public key expiration prefix.
Definition qsmp.h:677
QSMP_EXPORT_API bool qsmp_public_key_compare(const qsmp_client_verification_key *a, const qsmp_client_verification_key *b)
Compares two public keys for equality.
Definition qsmp.c:565
#define QSMP_PUBKEY_KEYID_SIZE
The size of the QSMP public key identifier prefix.
Definition qsmp.h:672
qsmp_messages
The logging message enumeration.
Definition qsmp.h:826
@ qsmp_messages_decryption_fail
Definition qsmp.h:842
@ qsmp_messages_disconnect
Definition qsmp.h:836
@ qsmp_messages_accept_fail
Definition qsmp.h:828
@ qsmp_messages_none
Definition qsmp.h:827
@ qsmp_messages_connection_fail
Definition qsmp.h:845
@ qsmp_messages_queue_empty
Definition qsmp.h:839
@ qsmp_messages_keepalive_fail
Definition qsmp.h:843
@ qsmp_messages_invalid_request
Definition qsmp.h:846
@ qsmp_messages_bind_fail
Definition qsmp.h:830
@ qsmp_messages_create_fail
Definition qsmp.h:831
@ qsmp_messages_sockalloc_fail
Definition qsmp.h:841
@ qsmp_messages_socket_message
Definition qsmp.h:838
@ qsmp_messages_receive_fail
Definition qsmp.h:833
@ qsmp_messages_connect_success
Definition qsmp.h:832
@ qsmp_messages_listen_fail
Definition qsmp.h:829
@ qsmp_messages_kex_fail
Definition qsmp.h:835
@ qsmp_messages_allocate_fail
Definition qsmp.h:834
@ qsmp_messages_listener_fail
Definition qsmp.h:840
@ qsmp_messages_keepalive_timeout
Definition qsmp.h:844
@ qsmp_messages_disconnect_fail
Definition qsmp.h:837
QSMP_EXPORT_API void qsmp_packet_error_message(qsmp_network_packet *packet, qsmp_errors error)
Populate a packet structure with an error message.
Definition qsmp.c:499
#define QSMP_CONFIG_SIZE
The size of the protocol configuration string.
Definition qsmp.h:131
#define QSMP_PUBKEY_HEADER_SIZE
The size of the QSMP public key header.
Definition qsmp.h:657
QSMP_EXPORT_API qsmp_errors qsmp_header_validate(qsmp_connection_state *cns, const qsmp_network_packet *packetin, qsmp_flags kexflag, qsmp_flags pktflag, uint64_t sequence, uint32_t msglen)
Validate a packet header and timestamp.
Definition qsmp.c:225
QSMP_EXPORT_API const char * qsmp_error_to_string(qsmp_errors error)
Return a pointer to a string description of an error code.
Definition qsmp.c:202
QSMP_EXPORT_API void qsmp_log_message(qsmp_messages emsg)
Log a message.
Definition qsmp.c:354
#define QSMP_MESSAGE_STRING_DEPTH
The depth of the QSMP message string array.
Definition qsmp.h:772
QSMP_EXPORT_API size_t qsmp_public_key_encoding_size()
Get the key encoding string size.
Definition qsmp.c:716
QSMP_EXPORT_API size_t qsmp_public_key_encode(char *enck, size_t enclen, const qsmp_client_verification_key *pubk)
Encode a public key structure and copy to a string.
Definition qsmp.c:639
QSMP_EXPORT_API void qsmp_log_error(qsmp_messages emsg, qsc_socket_exceptions err, const char *msg)
Log the message, socket error, and string description.
Definition qsmp.c:317
QSMP_EXPORT_API void qsmp_connection_state_dispose(qsmp_connection_state *cns)
Reset the connection state.
Definition qsmp.c:183
QSMP_EXPORT_API void qsmp_header_create(qsmp_network_packet *packetout, qsmp_flags flag, uint64_t sequence, uint32_t msglen)
Populate a packet header and set the creation time.
Definition qsmp.c:216
QSMP_EXPORT_API bool qsmp_public_key_decode(qsmp_client_verification_key *pubk, const char *enck, size_t enclen)
Decode a public key string and populate a client key structure.
Definition qsmp.c:585
QSMP_EXPORT_API qsmp_asymmetric_signature_keypair * qsmp_asymmetric_signature_keypair_initialize()
Initialize an asymmetric signature keypair.
Definition qsmp.c:79
#define QSMP_PUBKEY_VERSION_SIZE
The size of the QSMP public key version string.
Definition qsmp.h:662
QSMP_EXPORT_API void qsmp_packet_clear(qsmp_network_packet *packet)
Clear a packet's state.
Definition qsmp.c:387
#define QSMP_SIGKEY_ENCODED_SIZE
The secret signature key size.
Definition qsmp.h:651
#define QSMP_ERROR_STRING_DEPTH
The depth of the QSMP error string array.
Definition qsmp.h:726
QSMP_EXPORT_API void qsmp_connection_close(qsmp_connection_state *cns, qsmp_errors err, bool notify)
Close the network connection between hosts.
Definition qsmp.c:105
QSMP_EXPORT_API void qsmp_signature_key_serialize(uint8_t serk[QSMP_SIGKEY_ENCODED_SIZE], const qsmp_server_signature_key *kset)
Encode a secret key structure and copy to a string.
Definition qsmp.c:760
#define QSMP_KEYID_SIZE
The QSMP key identity size.
Definition qsmp.h:266
The QSMP asymmetric cipher key container.
Definition qsmp.h:932
The QSMP asymmetric signature key container.
Definition qsmp.h:942
The QSMP client key structure.
Definition qsmp.h:965
uint8_t config[QSMP_CONFIG_SIZE]
Definition qsmp.h:967
uint64_t expiration
Definition qsmp.h:966
uint8_t verkey[QSMP_ASYMMETRIC_VERIFY_KEY_SIZE]
Definition qsmp.h:969
uint8_t keyid[QSMP_KEYID_SIZE]
Definition qsmp.h:968
The QSMP socket connection state structure.
Definition qsmp.h:1002
uint64_t txseq
Definition qsmp.h:1008
qsmp_mode mode
Definition qsmp.h:1012
bool receiver
Definition qsmp.h:1011
qsc_rcs_state txcpr
Definition qsmp.h:1006
uint8_t rtcs[QSMP_DUPLEX_SYMMETRIC_KEY_SIZE]
Definition qsmp.h:1003
uint64_t rxseq
Definition qsmp.h:1007
uint32_t cid
Definition qsmp.h:1009
qsmp_flags exflag
Definition qsmp.h:1010
qsc_socket target
Definition qsmp.h:1004
qsc_rcs_state rxcpr
Definition qsmp.h:1005
The QSMP keep alive state structure.
Definition qsmp.h:990
bool recd
Definition qsmp.h:994
uint64_t seqctr
Definition qsmp.h:993
qsc_socket target
Definition qsmp.h:991
uint64_t etime
Definition qsmp.h:992
The QSMP packet structure.
Definition qsmp.h:952
uint8_t * pmessage
Definition qsmp.h:957
uint32_t msglen
Definition qsmp.h:954
uint64_t utctime
Definition qsmp.h:956
uint64_t sequence
Definition qsmp.h:955
uint8_t flag
Definition qsmp.h:953
The QSMP server key structure.
Definition qsmp.h:977
uint8_t config[QSMP_CONFIG_SIZE]
Definition qsmp.h:979
uint64_t expiration
Definition qsmp.h:978
uint8_t sigkey[QSMP_ASYMMETRIC_SIGNING_KEY_SIZE]
Definition qsmp.h:981
uint8_t verkey[QSMP_ASYMMETRIC_VERIFY_KEY_SIZE]
Definition qsmp.h:982
uint8_t keyid[QSMP_KEYID_SIZE]
Definition qsmp.h:980