44#include "../../QSC/QSC/dilithium.h"
45#include "../../QSC/QSC/kyber.h"
46#include "../../QSC/QSC/rcs.h"
47#include "../../QSC/QSC/sha3.h"
48#include "../../QSC/QSC/socketbase.h"
93#define PQS_CONFIG_SIZE 48
120#if defined(QSC_DILITHIUM_S1P2544)
121# if defined(QSC_KYBER_S1P1632)
122static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s1_kyber-s1_sha3_rcs";
123# elif defined(QSC_KYBER_S3P2400)
124static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s1_kyber-s3_sha3_rcs";
125# elif defined(QSC_KYBER_S5P3168)
126static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s1_kyber-s5_sha3_rcs";
127# elif defined(QSC_KYBER_S6P3936)
128static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s1_kyber-s6_sha3_rcs";
130# error Invalid parameter set!
132#elif defined(QSC_DILITHIUM_S3P4016)
133# if defined(QSC_KYBER_S1P1632)
134static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s3_kyber-s1_sha3_rcs";
135# elif defined(QSC_KYBER_S3P2400)
136static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s3_kyber-s3_sha3_rcs";
137# elif defined(QSC_KYBER_S5P3168)
138static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s3_kyber-s5_sha3_rcs";
139# elif defined(QSC_KYBER_S6P3936)
140static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s3_kyber-s6_sha3_rcs";
142# error Invalid parameter set!
144#elif defined(QSC_DILITHIUM_S5P4880)
145# if defined(QSC_KYBER_S1P1632)
146static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s5_kyber-s1_sha3_rcs";
147# elif defined(QSC_KYBER_S3P2400)
148static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s5_kyber-s3_sha3_rcs";
149# elif defined(QSC_KYBER_S5P3168)
150static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s5_kyber-s5_sha3_rcs";
151# elif defined(QSC_KYBER_S6P3936)
152static const char PQS_CONFIG_STRING[
PQS_CONFIG_SIZE] =
"dilithium-s5_kyber-s6_sha3_rcs";
154# error Invalid parameter set!
157# error Invalid parameter set!
167#define PQS_ASYMMETRIC_CIPHER_TEXT_SIZE (QSC_KYBER_CIPHERTEXT_SIZE)
175#define PQS_ASYMMETRIC_PRIVATE_KEY_SIZE (QSC_KYBER_PRIVATEKEY_SIZE)
183#define PQS_ASYMMETRIC_PUBLIC_KEY_SIZE (QSC_KYBER_PUBLICKEY_SIZE)
191#define PQS_ASYMMETRIC_SIGNING_KEY_SIZE (QSC_DILITHIUM_PRIVATEKEY_SIZE)
199#define PQS_ASYMMETRIC_VERIFY_KEY_SIZE (QSC_DILITHIUM_PUBLICKEY_SIZE)
207#define PQS_ASYMMETRIC_SIGNATURE_SIZE (QSC_DILITHIUM_SIGNATURE_SIZE)
215#if defined(QSC_DILITHIUM_S1P2544)
216# define PQS_PUBKEY_ENCODING_SIZE 1752
217#elif defined(QSC_DILITHIUM_S3P4016)
218# define PQS_PUBKEY_ENCODING_SIZE 2604
219#elif defined(QSC_DILITHIUM_S5P4880)
220# define PQS_PUBKEY_ENCODING_SIZE 3456
222# error invalid dilithium parameter!
231#if defined(QSC_DILITHIUM_S1P2544)
232# define PQS_PUBKEY_STRING_SIZE 2014
233#elif defined(QSC_DILITHIUM_S3P4016)
234# define PQS_PUBKEY_STRING_SIZE 2879
235#elif defined(QSC_DILITHIUM_S5P4880)
236# define PQS_PUBKEY_STRING_SIZE 3745
238# error invalid dilithium parameter!
245#define PQS_CLIENT_PORT 33118
253#define PQS_CONNECTIONS_INIT 1000
262#define PQS_CONNECTIONS_MAX 50000
268#define PQS_CONNECTION_MTU 1500
274#define PQS_ERROR_SEQUENCE 0xFF00000000000000ULL
280#define PQS_ERROR_MESSAGE_SIZE 1
286#define PQS_FLAG_SIZE 1
292#define PQS_HASH_SIZE 32
298#define PQS_HEADER_SIZE 21
306#define PQS_KEEPALIVE_TIMEOUT (120 * 1000)
312#define PQS_KEYID_SIZE 16
318#define PQS_MACTAG_SIZE 32
326#define PQS_MESSAGE_MAX 0x3D090000
332#define PQS_MSGLEN_SIZE 4
338#define PQS_NONCE_SIZE 32
344#define PQS_NETWORK_BUFFER_SIZE 1280
353#define PQS_PACKET_TIME_THRESHOLD 60
359#define PQS_PUBKEY_DURATION_DAYS 365
367#define PQS_PUBKEY_DURATION_SECONDS (PQS_PUBKEY_DURATION_DAYS * 24 * 60 * 60)
373#define PQS_PUBKEY_LINE_LENGTH 64
379#define PQS_SCHASH_SIZE 32
385#define PQS_SECRET_SIZE 32
391#define PQS_SEQUENCE_SIZE 8
397#define PQS_SEQUENCE_TERMINATOR 0xFFFFFFFFUL
405#define PQS_SERVER_LISTEN_BACKLOG 0
411#define PQS_SERVER_PORT 3119
417#define PQS_SYMMETRIC_KEY_SIZE 32
423#define PQS_TIMESTAMP_SIZE 8
432#define PQS_SIGKEY_ENCODED_SIZE (PQS_KEYID_SIZE + PQS_TIMESTAMP_SIZE + PQS_CONFIG_SIZE + \
433 PQS_HASH_SIZE + PQS_ASYMMETRIC_SIGNING_KEY_SIZE + PQS_ASYMMETRIC_VERIFY_KEY_SIZE)
443#define pqs_cipher_generate_keypair qsc_kyber_generate_keypair
448#define pqs_cipher_decapsulate qsc_kyber_decapsulate
453#define pqs_cipher_encapsulate qsc_kyber_encapsulate
458#define pqs_signature_generate_keypair qsc_dilithium_generate_keypair
463#define pqs_signature_sign qsc_dilithium_sign
468#define pqs_signature_verify qsc_dilithium_verify
475static const char PQS_PUBKEY_HEADER[] =
"------BEGIN PQS PUBLIC KEY BLOCK------";
476static const char PQS_PUBKEY_VERSION[] =
"Version: PQS v1.0";
477static const char PQS_PUBKEY_CONFIG_PREFIX[] =
"Configuration: ";
478static const char PQS_PUBKEY_KEYID_PREFIX[] =
"Host ID: ";
479static const char PQS_PUBKEY_EXPIRATION_PREFIX[] =
"Expiration: ";
480static const char PQS_PUBKEY_FOOTER[] =
"------END PQS PUBLIC KEY BLOCK------";
488#define PQS_ERROR_STRING_DEPTH 32
489#define PQS_ERROR_STRING_WIDTH 128
490#define PQS_MESSAGE_STRING_DEPTH 21
491#define PQS_MESSAGE_STRING_WIDTH 128
498static const char PQS_MESSAGE_STRINGS[PQS_ERROR_STRING_DEPTH][PQS_ERROR_STRING_WIDTH] =
500 "The operation completed succesfully.",
501 "The socket server accept function failed.",
502 "The listener socket listener could not connect.",
503 "The listener socket could not bind to the address.",
504 "The listener socket could not be created.",
505 "The server is connected to remote host - ",
506 "The socket receive function failed - ",
507 "The server had a memory allocation failure.",
508 "The key exchange has experienced a failure.",
509 "The server has disconnected from the remote host - ",
510 "The server has disconnected the client due to an error - ",
511 "The server has had a socket level error.",
512 "The server has reached the maximum number of connections",
513 "The server listener socket has failed.",
514 "The server has run out of socket connections",
515 "The message decryption has failed - ",
516 "The keepalive function has failed - ",
517 "The keepalive period has been exceeded",
518 "The connection failed or was interrupted - ",
519 "The function received an invalid request - ",
520 "The remote host is busy and refused the connection - "
523static const char PQS_ERROR_STRINGS[PQS_ERROR_STRING_DEPTH][PQS_ERROR_STRING_WIDTH] =
525 "No error was detected",
526 "The socket accept function returned an error",
527 "The symmetric cipher had an authentication failure",
528 "The keep alive check failed",
529 "The communications channel has failed",
530 "The device could not make a connection to the remote host",
531 "The transmission failed at the KEX connection phase",
532 "The asymmetric cipher failed to decapsulate the shared secret",
533 "The decryption authentication has failed",
534 "The transmission failed at the KEX establish phase",
535 "The transmission failed at the KEX exchange phase",
536 "The public - key hash is invalid",
537 "The server has run out of socket connections",
538 "The expected input was invalid",
539 "The packet flag was unexpected",
540 "The keep alive has expired with no response",
541 "The decryption authentication has failed",
542 "The PQS public key has expired ",
543 "The key identity is unrecognized",
544 "The ratchet operation has failed",
545 "The listener function failed to initialize",
546 "The server has run out of memory",
547 "The packet was received out of sequence",
548 "The random generator has failed",
549 "The receiver failed at the network layer",
550 "The transmitter failed at the network layer",
551 "The protocol string was not recognized",
552 "The expected data could not be verified",
553 "The client received an authentication failure response",
554 "The client received an authentication success response",
555 "The packet valid time has expired",
556 "The connection was refused by the remote server"
pqs_flags
Enumeration of packet flags used in the PQS protocol.
Definition pqs.h:629
@ pqs_flag_keep_alive_response
Definition pqs.h:642
@ pqs_flag_symmetric_ratchet_request
Definition pqs.h:650
@ pqs_flag_transfer_request
Definition pqs.h:651
@ pqs_flag_exstart_request
Definition pqs.h:635
@ pqs_flag_connection_terminate
Definition pqs.h:633
@ pqs_flag_session_established
Definition pqs.h:645
@ pqs_flag_remote_connected
Definition pqs.h:643
@ pqs_flag_remote_terminated
Definition pqs.h:644
@ pqs_flag_encrypted_message
Definition pqs.h:634
@ pqs_flag_none
Definition pqs.h:630
@ pqs_flag_exstart_response
Definition pqs.h:636
@ pqs_flag_connect_response
Definition pqs.h:632
@ pqs_flag_exchange_request
Definition pqs.h:637
@ pqs_flag_error_condition
Definition pqs.h:652
@ pqs_flag_establish_request
Definition pqs.h:639
@ pqs_flag_session_establish_verify
Definition pqs.h:646
@ pqs_flag_asymmetric_ratchet_request
Definition pqs.h:648
@ pqs_flag_asymmetric_ratchet_response
Definition pqs.h:649
@ pqs_flag_keep_alive_request
Definition pqs.h:641
@ pqs_flag_establish_response
Definition pqs.h:640
@ pqs_flag_connect_request
Definition pqs.h:631
@ pqs_flag_exchange_response
Definition pqs.h:638
@ pqs_flag_unrecognized_protocol
Definition pqs.h:647
#define PQS_ASYMMETRIC_SIGNING_KEY_SIZE
The size in bytes of the asymmetric signature signing-key array.
Definition pqs.h:191
PQS_EXPORT_API void pqs_public_key_hash(uint8_t *hash, const pqs_client_verification_key *pubk)
Computes a hash of a public key structure.
Definition pqs.c:582
PQS_EXPORT_API void pqs_packet_header_serialize(const pqs_network_packet *packet, uint8_t *header)
Serializes a packet header into a byte array.
Definition pqs.c:368
#define PQS_ASYMMETRIC_PRIVATE_KEY_SIZE
The size in bytes of the asymmetric cipher private-key array.
Definition pqs.h:175
pqs_errors
Enumeration of error codes returned by PQS functions.
Definition pqs.h:586
@ pqs_error_establish_failure
Definition pqs.h:596
@ pqs_error_memory_allocation
Definition pqs.h:608
@ pqs_error_connection_failure
Definition pqs.h:592
@ pqs_error_authentication_failure
Definition pqs.h:589
@ pqs_error_packet_unsequenced
Definition pqs.h:609
@ pqs_error_invalid_input
Definition pqs.h:600
@ pqs_error_listener_fail
Definition pqs.h:607
@ pqs_error_channel_down
Definition pqs.h:591
@ pqs_error_verify_failure
Definition pqs.h:614
@ pqs_error_key_unrecognized
Definition pqs.h:605
@ pqs_error_decryption_failure
Definition pqs.h:595
@ pqs_error_invalid_request
Definition pqs.h:601
@ pqs_error_bad_keep_alive
Definition pqs.h:590
@ pqs_error_keepalive_expired
Definition pqs.h:602
@ pqs_error_none
Definition pqs.h:587
@ pqs_error_random_failure
Definition pqs.h:610
@ pqs_error_login_failure
Definition pqs.h:615
@ pqs_error_login_success
Definition pqs.h:616
@ pqs_error_keychain_fail
Definition pqs.h:606
@ pqs_error_hosts_exceeded
Definition pqs.h:599
@ pqs_error_accept_fail
Definition pqs.h:588
@ pqs_error_exchange_failure
Definition pqs.h:597
@ pqs_error_hash_invalid
Definition pqs.h:598
@ pqs_error_keepalive_timeout
Definition pqs.h:603
@ pqs_error_connection_refused
Definition pqs.h:618
@ pqs_error_connect_failure
Definition pqs.h:593
@ pqs_error_receive_failure
Definition pqs.h:611
@ pqs_error_unknown_protocol
Definition pqs.h:613
@ pqs_error_transmit_failure
Definition pqs.h:612
@ pqs_error_key_expired
Definition pqs.h:604
@ pqs_error_message_time_invalid
Definition pqs.h:617
@ pqs_error_decapsulation_failure
Definition pqs.h:594
#define PQS_SIGKEY_ENCODED_SIZE
The size in bytes of the encoded secret signature key structure.
Definition pqs.h:432
PQS_EXPORT_API pqs_errors pqs_packet_decrypt(pqs_connection_state *cns, uint8_t *message, size_t *msglen, const pqs_network_packet *packetin)
Decrypts an incoming packet's payload.
Definition pqs.c:230
PQS_EXPORT_API void pqs_generate_keypair(pqs_client_verification_key *pubkey, pqs_server_signature_key *prikey, const uint8_t keyid[PQS_KEYID_SIZE])
Generates a PQS key pair.
Definition pqs.c:112
PQS_EXPORT_API size_t pqs_packet_to_stream(const pqs_network_packet *packet, uint8_t *pstream)
Serializes a full packet (header and payload) into a byte stream.
Definition pqs.c:453
PQS_EXPORT_API void pqs_signature_key_serialize(uint8_t serk[PQS_SIGKEY_ENCODED_SIZE], const pqs_server_signature_key *kset)
Serializes a server secret signature key structure into an encoded array.
Definition pqs.c:616
#define PQS_ASYMMETRIC_PUBLIC_KEY_SIZE
The size in bytes of the asymmetric cipher public-key array.
Definition pqs.h:183
PQS_EXPORT_API void pqs_log_write(pqs_messages emsg, const char *msg)
Logs a message with an accompanying description.
Definition pqs.c:195
PQS_EXPORT_API void pqs_public_key_encode(char enck[PQS_PUBKEY_STRING_SIZE], const pqs_client_verification_key *pubkey)
Encodes a client public key structure into a printable string.
Definition pqs.c:515
PQS_EXPORT_API void pqs_packet_error_message(pqs_network_packet *packet, pqs_errors error)
Populates a packet structure with an error message.
Definition pqs.c:327
#define PQS_CONFIG_SIZE
The size in bytes of the protocol configuration string.
Definition pqs.h:93
#define PQS_HASH_SIZE
The output size in bytes of the Simplex 256-bit hash function.
Definition pqs.h:292
PQS_EXPORT_API void pqs_packet_header_create(pqs_network_packet *packetout, pqs_flags flag, uint64_t sequence, uint32_t msglen)
Creates and populates a packet header.
Definition pqs.c:340
pqs_client_commands
Enumeration of client commands in the PQS protocol.
Definition pqs.h:571
@ pqs_client_command_none
Definition pqs.h:572
@ pqs_client_command_cprint
Definition pqs.h:573
@ pqs_client_command_execute
Definition pqs.h:574
@ pqs_client_command_quit
Definition pqs.h:575
PQS_EXPORT_API void pqs_packet_clear(pqs_network_packet *packet)
Clears the state of a network packet.
Definition pqs.c:218
pqs_messages
Enumeration of logging and status messages used by PQS.
Definition pqs.h:663
@ pqs_messages_socket_message
Definition pqs.h:675
@ pqs_messages_keepalive_fail
Definition pqs.h:680
@ pqs_messages_listen_fail
Definition pqs.h:666
@ pqs_messages_kex_fail
Definition pqs.h:672
@ pqs_messages_queue_empty
Definition pqs.h:676
@ pqs_messages_disconnect_fail
Definition pqs.h:674
@ pqs_messages_connection_refused
Definition pqs.h:684
@ pqs_messages_accept_fail
Definition pqs.h:665
@ pqs_messages_disconnect
Definition pqs.h:673
@ pqs_messages_sockalloc_fail
Definition pqs.h:678
@ pqs_messages_receive_fail
Definition pqs.h:670
@ pqs_messages_bind_fail
Definition pqs.h:667
@ pqs_messages_create_fail
Definition pqs.h:668
@ pqs_messages_decryption_fail
Definition pqs.h:679
@ pqs_messages_keepalive_timeout
Definition pqs.h:681
@ pqs_messages_invalid_request
Definition pqs.h:683
@ pqs_messages_connect_success
Definition pqs.h:669
@ pqs_messages_none
Definition pqs.h:664
@ pqs_messages_listener_fail
Definition pqs.h:677
@ pqs_messages_allocate_fail
Definition pqs.h:671
@ pqs_messages_connection_fail
Definition pqs.h:682
PQS_EXPORT_API pqs_errors pqs_header_validate(pqs_connection_state *cns, const pqs_network_packet *packetin, pqs_flags kexflag, pqs_flags pktflag, uint64_t sequence, uint32_t msglen)
Validates a packet header and its associated timestamp.
Definition pqs.c:387
PQS_EXPORT_API bool pqs_packet_time_validate(const pqs_network_packet *packet)
Validates the timestamp of a packet against the local UTC time.
Definition pqs.c:444
PQS_EXPORT_API bool pqs_public_key_decode(pqs_client_verification_key *pubk, const char enck[PQS_PUBKEY_STRING_SIZE])
Decodes an encoded public key string into a client verification key structure.
Definition pqs.c:480
#define PQS_ASYMMETRIC_VERIFY_KEY_SIZE
The size in bytes of the asymmetric signature verification-key array.
Definition pqs.h:199
PQS_EXPORT_API void pqs_connection_state_dispose(pqs_connection_state *cns)
Resets and disposes of the connection state.
Definition pqs.c:67
PQS_EXPORT_API const char * pqs_error_description(pqs_messages emsg)
Retrieves the description string for a given message enumeration.
Definition pqs.c:83
PQS_EXPORT_API void pqs_log_error(pqs_messages emsg, qsc_socket_exceptions err, const char *msg)
Logs an error message along with socket error details.
Definition pqs.c:137
PQS_EXPORT_API void pqs_packet_time_set(pqs_network_packet *packet)
Sets the packet's UTC timestamp to the current time.
Definition pqs.c:439
PQS_EXPORT_API const char * pqs_error_to_string(pqs_errors error)
Converts an error code to its corresponding string description.
Definition pqs.c:98
PQS_EXPORT_API void pqs_signature_key_deserialize(pqs_server_signature_key *kset, const uint8_t serk[PQS_SIGKEY_ENCODED_SIZE])
Deserializes an encoded secret signature key into a server signature key structure.
Definition pqs.c:597
PQS_EXPORT_API void pqs_packet_header_deserialize(const uint8_t *header, pqs_network_packet *packet)
Deserializes a byte array into a packet header.
Definition pqs.c:349
PQS_EXPORT_API void pqs_stream_to_packet(const uint8_t *pstream, pqs_network_packet *packet)
Deserializes a byte stream into a network packet structure.
Definition pqs.c:635
#define PQS_KEYID_SIZE
The size in bytes of a PQS key identity.
Definition pqs.h:312
PQS_EXPORT_API void pqs_connection_close(pqs_connection_state *cns, pqs_errors err, bool notify)
Closes the network connection between hosts.
Definition pqs.c:11
PQS_EXPORT_API void pqs_log_message(pqs_messages emsg)
Logs a message based on the provided message enumeration.
Definition pqs.c:185
PQS_EXPORT_API pqs_errors pqs_packet_encrypt(pqs_connection_state *cns, pqs_network_packet *packetout, const uint8_t *message, size_t msglen)
Encrypts a message and constructs an output packet.
Definition pqs.c:289
#define PQS_PUBKEY_STRING_SIZE
The size in bytes of the serialized PQS client-key structure.
Definition pqs.h:236
Container for an asymmetric cipher key pair.
Definition pqs.h:699
uint8_t prikey[PQS_ASYMMETRIC_PRIVATE_KEY_SIZE]
Definition pqs.h:700
uint8_t pubkey[PQS_ASYMMETRIC_PUBLIC_KEY_SIZE]
Definition pqs.h:701
Container for an asymmetric signature key pair.
Definition pqs.h:712
uint8_t verkey[PQS_ASYMMETRIC_VERIFY_KEY_SIZE]
Definition pqs.h:714
uint8_t sigkey[PQS_ASYMMETRIC_SIGNING_KEY_SIZE]
Definition pqs.h:713
Structure holding a PQS client public key.
Definition pqs.h:742
uint8_t config[PQS_CONFIG_SIZE]
Definition pqs.h:744
uint64_t expiration
Definition pqs.h:743
uint8_t verkey[PQS_ASYMMETRIC_VERIFY_KEY_SIZE]
Definition pqs.h:746
uint8_t keyid[PQS_KEYID_SIZE]
Definition pqs.h:745
Maintains the state for a PQS socket connection.
Definition pqs.h:790
uint64_t txseq
Definition pqs.h:795
pqs_flags exflag
Definition pqs.h:797
bool receiver
Definition pqs.h:798
qsc_rcs_state txcpr
Definition pqs.h:793
uint64_t rxseq
Definition pqs.h:794
uint32_t cid
Definition pqs.h:796
qsc_socket target
Definition pqs.h:791
qsc_rcs_state rxcpr
Definition pqs.h:792
Maintains the state for a keep-alive mechanism.
Definition pqs.h:774
bool recd
Definition pqs.h:778
uint64_t seqctr
Definition pqs.h:777
qsc_socket target
Definition pqs.h:775
uint64_t etime
Definition pqs.h:776
Represents a network packet in the PQS protocol.
Definition pqs.h:725
uint8_t * pmessage
Definition pqs.h:730
uint32_t msglen
Definition pqs.h:727
uint64_t utctime
Definition pqs.h:729
uint64_t sequence
Definition pqs.h:728
uint8_t flag
Definition pqs.h:726
Structure holding a PQS server secret signature key.
Definition pqs.h:757
uint8_t config[PQS_CONFIG_SIZE]
Definition pqs.h:759
uint64_t expiration
Definition pqs.h:758
uint8_t verkey[PQS_ASYMMETRIC_VERIFY_KEY_SIZE]
Definition pqs.h:762
uint8_t rkhash[PQS_HASH_SIZE]
Definition pqs.h:763
uint8_t keyid[PQS_KEYID_SIZE]
Definition pqs.h:760
uint8_t sigkey[PQS_ASYMMETRIC_SIGNING_KEY_SIZE]
Definition pqs.h:761