43#include "satpcommon.h"
46#include "socketclient.h"
59#define SATP_USE_RCS_ENCRYPTION
61#if defined(SATP_USE_RCS_ENCRYPTION)
63# define satp_cipher_state qsc_rcs_state
64# define satp_cipher_dispose qsc_rcs_dispose
65# define satp_cipher_initialize qsc_rcs_initialize
66# define satp_cipher_keyparams qsc_rcs_keyparams
67# define satp_cipher_set_associated qsc_rcs_set_associated
68# define satp_cipher_transform qsc_rcs_transform
71# define satp_cipher_state qsc_aes_gcm256_state
72# define satp_cipher_dispose qsc_aes_gcm256_dispose
73# define satp_cipher_initialize qsc_aes_gcm256_initialize
74# define satp_cipher_keyparams qsc_aes_keyparams
75# define satp_cipher_set_associated qsc_aes_gcm256_set_associated
76# define satp_cipher_transform qsc_aes_gcm256_transform
121#define SATP_CLIENT_PASSWORD_MAX 256U
127#define SATP_CLIENT_USERNAME_MAX 256U
133#define SATP_CONNECTIONS_INIT 1000U
139#define SATP_CONNECTIONS_MAX 50000U
145#define SATP_CONFIG_SIZE 25U
151#define SATP_ERROR_SIZE 1U
157#define SATP_EXPIRATION_SIZE 8U
163#define SATP_HASH_SIZE 32U
169#define SATP_HEADER_SIZE 21U
175#define SATP_KEEPALIVE_MESSAGE 8U
181#define SATP_KEEPALIVE_STRING 20U
187#define SATP_KEEPALIVE_TIMEOUT (300U * 1000U)
193#define SATP_KEY_TREE_COUNT 1024
198#if defined(SATP_USE_RCS_ENCRYPTION)
199# define SATP_MACTAG_SIZE 32U
201# define SATP_MACTAG_SIZE 16U
208#define SATP_MESSAGE_SIZE 1024U
214#define SATP_MESSAGE_MAX (SATP_MESSAGE_SIZE + SATP_HEADER_SIZE)
220#define SATP_CONNECTION_MTU 1500U
226#if defined(SATP_USE_RCS_ENCRYPTION)
227# define SATP_NONCE_SIZE 32U
229# define SATP_NONCE_SIZE 16U
236#define SATP_SALT_SIZE 32U
242#define SATP_SERVER_PORT 2701U
248#define SATP_SEQUENCE_TERMINATOR 0xFFFFFFFFUL
253#if defined(SATP_USE_RCS_ENCRYPTION)
254 static const char SATP_CONFIG_STRING[
SATP_CONFIG_SIZE + 1U] =
"r01-satp-rcs256-keccak256";
256 static const char SATP_CONFIG_STRING[
SATP_CONFIG_SIZE + 1U] =
"r02-satp-aes256-keccak256";
265#define SATP_KEY_DURATION_DAYS 365U
275#define SATP_PACKET_TIME_THRESHOLD 60U
281#define SATP_KEY_DURATION_SECONDS (SATP_KEY_DURATION_DAYS * 24U * 60U * 60U)
289#define SATP_BRANCH_ID_SIZE 2U
295#define SATP_DOMAIN_ID_SIZE 2U
301#define SATP_DEVICE_ID_SIZE 4U
307#define SATP_EPOCH_SIZE 2U
313#define SATP_ERROR_MESSAGE_SIZE 1U
319#define SATP_ERROR_SEQUENCE 0xFF00000000000000ULL
325#define SATP_FLAG_SIZE 1
331#define SATP_KEY_ID_SIZE 4U
337#define SATP_SERVICE_ID_SIZE 2U
343#define SATP_DID_SIZE (SATP_DOMAIN_ID_SIZE + SATP_BRANCH_ID_SIZE + SATP_EPOCH_SIZE + SATP_SERVICE_ID_SIZE + SATP_DEVICE_ID_SIZE)
349#define SATP_MID_SIZE (SATP_DOMAIN_ID_SIZE)
355#define SATP_SID_SIZE (SATP_DOMAIN_ID_SIZE + SATP_BRANCH_ID_SIZE)
361#define SATP_KID_SIZE (SATP_DID_SIZE + SATP_KEY_ID_SIZE)
369#define SATP_DKEY_SIZE 32U
375#define SATP_MKEY_SIZE 32U
381#define SATP_SKEY_SIZE 32U
387#define SATP_KTREE_SIZE (SATP_DKEY_SIZE * SATP_KEY_TREE_COUNT)
393#define SATP_DKEY_ENCODED_SIZE (SATP_KID_SIZE + SATP_SKEY_SIZE + SATP_EXPIRATION_SIZE + (SATP_DKEY_SIZE * SATP_KEY_TREE_COUNT))
399#define SATP_MKEY_ENCODED_SIZE (SATP_MKEY_SIZE + SATP_MID_SIZE + SATP_EXPIRATION_SIZE)
405#define SATP_SKEY_ENCODED_SIZE (SATP_SKEY_SIZE + SATP_SKEY_SIZE + SATP_SID_SIZE + SATP_EXPIRATION_SIZE)
411#define SATP_STOK_SIZE 32U
416#define SATP_ERROR_STRING_DEPTH 22U
417#define SATP_ERROR_STRING_WIDTH 128U
419static const char SATP_ERROR_STRINGS[SATP_ERROR_STRING_DEPTH][SATP_ERROR_STRING_WIDTH] =
421 "No error was detected",
422 "The keep alive check failed",
423 "The cipher authentication has failed",
424 "The communications channel has failed",
425 "The device could not make a connection to the remote host",
426 "The decryption authentication has failed",
427 "The device identity is unrecognized",
428 "The transmission failed at the key exchange establish phase",
429 "The input provided is invalid",
430 "The keep alive has expired with no response",
431 "The key exchange authentication has failed",
432 "The SATP public key has expired",
433 "The key identity is not recognized",
434 "The packet keep alive is invalid",
435 "The packet was received out of sequence",
436 "The random generator has failed",
437 "The receiver failed at the network layer",
438 "The transmitter failed at the network layer",
439 "The protocol string was not recognized",
440 "The packets sequence number is out of sync",
441 "The expected data could not be verified",
442 "A general failure occurred",
575} satp_keep_alive_state;
SATP_EXPORT_API satp_errors satp_packet_header_validate(const satp_network_packet *packetin, satp_flags pktflag, uint64_t sequence, uint32_t msglen)
Validate a packet header and timestamp.
Definition satp.c:541
SATP_EXPORT_API satp_errors satp_encrypt_packet(satp_connection_state *cns, const uint8_t *message, size_t msglen, satp_network_packet *packetout)
Encrypt a message into an SATP packet.
Definition satp.c:193
SATP_EXPORT_API void satp_increment_device_key(uint8_t *sdkey)
Increment the serialized key This function clears a key at the current position and increments the ki...
Definition satp.c:343
#define SATP_SID_SIZE
The server ID size in bytes.
Definition satp.h:355
SATP_EXPORT_API void satp_send_network_error(const qsc_socket *sock, satp_errors error)
Send a plaintext SATP network error message Used only during key exchange, the connection close is se...
Definition satp.c:639
SATP_EXPORT_API void satp_deserialize_device_key(satp_device_key *dkey, const uint8_t *input)
Deserialize a client device key. This function deserializes a byte array into a SATP device key struc...
Definition satp.c:233
SATP_EXPORT_API void satp_packet_clear(satp_network_packet *packet)
Clear a SATP network packet. This function resets the fields of a SATP network packet to zero,...
Definition satp.c:487
#define SATP_DKEY_SIZE
The client key size in bytes.
Definition satp.h:369
SATP_EXPORT_API bool satp_generate_master_key(satp_master_key *mkey, const uint8_t *mid)
Generate a master key-set. This function generates a new SATP master key-set. It populates the provid...
Definition satp.c:435
SATP_EXPORT_API bool satp_packet_time_valid(const satp_network_packet *packet)
Check if a SATP packet is received within the valid time threshold. This function compares the UTC ti...
Definition satp.c:595
SATP_EXPORT_API size_t satp_packet_to_stream(const satp_network_packet *packet, uint8_t *pstream)
Serialize a SATP packet into a byte array. This function converts a SATP network packet into a contig...
Definition satp.c:613
SATP_EXPORT_API void satp_packet_error_message(satp_network_packet *packet, satp_errors error)
Populate a packet structure with an error message.
Definition satp.c:358
SATP_EXPORT_API void satp_serialize_server_key(uint8_t *output, const satp_server_key *skey)
Serialize a server key into a byte array. This function serializes a SATP server key structure into a...
Definition satp.c:324
SATP_EXPORT_API void satp_serialize_device_key(uint8_t *output, const satp_device_key *dkey)
Serialize a client device key. This function serializes a SATP device key structure into a byte array...
Definition satp.c:252
SATP_EXPORT_API void satp_packet_header_serialize(const satp_network_packet *packet, uint8_t *header)
Serialize a SATP packet header into a byte array. This function converts a structured SATP network pa...
Definition satp.c:527
SATP_EXPORT_API bool satp_generate_server_key(satp_server_key *skey, const satp_master_key *mkey, const uint8_t *sid)
Generate a server key-set. This function generates a new SATP server key-set based on the provided ma...
Definition satp.c:461
SATP_EXPORT_API void satp_connection_dispose(satp_connection_state *cns)
Reset the connection state.
Definition satp.c:114
SATP_EXPORT_API void satp_generate_device_key(satp_device_key *dkey, const satp_server_key *skey, const uint8_t *did)
Generate a device key-set. This function generates a new SATP device key-set using the provided serve...
Definition satp.c:412
SATP_EXPORT_API void satp_deserialize_server_key(satp_server_key *skey, const uint8_t *input)
Deserialize a server key from a byte array. This function deserializes a byte array into a SATP serve...
Definition satp.c:305
#define SATP_KEY_TREE_COUNT
The SATP key tree count.
Definition satp.h:193
SATP_EXPORT_API void satp_stream_to_packet(const uint8_t *pstream, satp_network_packet *packet)
Deserialize a byte stream into a SATP network packet. This function converts a contiguous byte stream...
Definition satp.c:655
#define SATP_MKEY_SIZE
The master key size in bytes.
Definition satp.h:375
#define SATP_KID_SIZE
The key ID size in bytes.
Definition satp.h:361
SATP_EXPORT_API satp_errors satp_decrypt_packet(satp_connection_state *cns, const satp_network_packet *packetin, uint8_t *message, size_t *msglen)
Decrypt an SATP packet.
Definition satp.c:131
SATP_EXPORT_API void satp_connection_close(satp_connection_state *cns, satp_errors err, bool notify)
Close the network connection between hosts.
Definition satp.c:7
SATP_EXPORT_API void satp_packet_header_deserialize(const uint8_t *header, satp_network_packet *packet)
Deserialize a byte array into a SATP packet header. This function converts a serialized byte array re...
Definition satp.c:513
SATP_EXPORT_API bool satp_extract_device_key(uint8_t *dk, const uint8_t *sk, const uint8_t *kid)
Extract a device key. This function extracts a device key by hashing the server key and key identity.
Definition satp.c:386
SATP_EXPORT_API satp_errors satp_decrypt_error_message(satp_connection_state *cns, uint8_t *message)
Decrypt an error message.
Definition satp.c:49
SATP_EXPORT_API void satp_deserialize_master_key(satp_master_key *mkey, const uint8_t *input)
Deserialize a master key from a byte array. This function deserializes a byte array into a SATP maste...
Definition satp.c:271
#define SATP_SKEY_SIZE
The server key size in bytes.
Definition satp.h:381
SATP_EXPORT_API void satp_packet_header_create(satp_network_packet *packetout, satp_flags flag, uint64_t sequence, uint32_t msglen)
Populate a packet header and set the creation time.
Definition satp.c:504
satp_errors
The SATP error values. This enumeration defines the error codes returned by SATP functions.
Definition satp.h:452
@ satp_error_unknown_protocol
Definition satp.h:480
@ satp_error_random_failure
Definition satp.h:477
@ satp_error_invalid_input
Definition satp.h:467
@ satp_error_authentication_failure
Definition satp.h:455
@ satp_erroe_listen_fail
Definition satp.h:457
@ satp_error_kex_auth_failure
Definition satp.h:470
@ satp_error_general_failure
Definition satp.h:483
@ satp_error_allocation_failure
Definition satp.h:458
@ satp_error_listener_fail
Definition satp.h:473
@ satp_error_bad_keep_alive
Definition satp.h:459
@ satp_error_decryption_failure
Definition satp.h:463
@ satp_error_hosts_exceeded
Definition satp.h:466
@ satp_error_key_not_recognized
Definition satp.h:472
@ satp_error_packet_expired
Definition satp.h:475
@ satp_error_packet_unsequenced
Definition satp.h:476
@ satp_error_keep_alive_expired
Definition satp.h:469
@ satp_error_establish_failure
Definition satp.h:465
@ satp_error_key_expired
Definition satp.h:471
@ satp_error_cipher_auth_failure
Definition satp.h:460
@ satp_error_invalid_request
Definition satp.h:468
@ satp_error_connection_failure
Definition satp.h:462
@ satp_error_channel_down
Definition satp.h:461
@ satp_error_transmit_failure
Definition satp.h:479
@ satp_error_none
Definition satp.h:453
@ satp_error_authentication_success
Definition satp.h:456
@ satp_error_unsequenced
Definition satp.h:481
@ satp_error_accept_fail
Definition satp.h:454
@ satp_error_verify_failure
Definition satp.h:482
@ satp_error_device_unrecognized
Definition satp.h:464
@ satp_error_message_time_invalid
Definition satp.h:474
@ satp_error_receive_failure
Definition satp.h:478
satp_flags
The SATP packet flag values. This enumeration defines the flag values used in SATP packets to indicat...
Definition satp.h:492
@ satp_flag_connection_terminate
Definition satp.h:496
@ satp_flag_none
Definition satp.h:493
@ satp_flag_error_condition
Definition satp.h:503
@ satp_flag_connect_response
Definition satp.h:495
@ satp_flag_authentication_response
Definition satp.h:499
@ satp_flag_authentication_request
Definition satp.h:498
@ satp_flag_auth_verify
Definition satp.h:500
@ satp_flag_encrypted_message
Definition satp.h:497
@ satp_flag_session_established
Definition satp.h:502
@ satp_flag_keepalive_request
Definition satp.h:501
@ satp_flag_connect_request
Definition satp.h:494
SATP_EXPORT_API void satp_serialize_master_key(uint8_t *output, const satp_master_key *mkey)
Serialize a master key into a byte array. This function serializes a SATP master key structure into a...
Definition satp.c:288
SATP_EXPORT_API const char * satp_error_to_string(satp_errors error)
Return a string description of an SATP error code. This function returns a human-readable string corr...
Definition satp.c:372
#define SATP_MID_SIZE
The master key identity size in bytes.
Definition satp.h:349
#define SATP_CONFIG_SIZE
The size of the protocol configuration string.
Definition satp.h:145
SATP_EXPORT_API void satp_packet_set_utc_time(satp_network_packet *packet)
Set the local UTC seconds time in a SATP packet header. This function updates the SATP packet header ...
Definition satp.c:585
The SATP keep alive state structure. This structure tracks the state of keep alive messages within SA...
Definition satp.h:571
bool recd
Definition satp.h:574
uint64_t seqctr
Definition satp.h:573
uint64_t etime
Definition satp.h:572
The SATP socket connection state structure.
Definition satp.h:511
uint64_t txseq
Definition satp.h:516
bool receiver
Definition satp.h:519
qsc_rcs_state txcpr
Definition satp.h:514
uint64_t rxseq
Definition satp.h:515
uint32_t cid
Definition satp.h:517
qsc_socket target
Definition satp.h:512
qsc_rcs_state rxcpr
Definition satp.h:513
satp_flags exflag
Definition satp.h:518
The SATP device key structure. This structure represents the SATP device key, which is derived from t...
Definition satp.h:529
QSC_SIMD_ALIGN uint8_t ktree[SATP_DKEY_SIZE *SATP_KEY_TREE_COUNT]
Definition satp.h:530
QSC_SIMD_ALIGN uint8_t kid[SATP_KID_SIZE]
Definition satp.h:531
uint64_t expiration
Definition satp.h:533
QSC_SIMD_ALIGN uint8_t stc[SATP_SKEY_SIZE]
Definition satp.h:532
uint8_t * spass
Definition satp.h:534
The SATP master key structure. This structure holds the SATP master key information,...
Definition satp.h:544
QSC_SIMD_ALIGN uint8_t mdk[SATP_MKEY_SIZE]
Definition satp.h:545
uint64_t expiration
Definition satp.h:547
QSC_SIMD_ALIGN uint8_t mid[SATP_MID_SIZE]
Definition satp.h:546
The SATP network packet structure. This structure defines the format of a SATP network packet....
Definition satp.h:584
uint8_t * pmessage
Definition satp.h:589
uint32_t msglen
Definition satp.h:586
uint64_t utctime
Definition satp.h:588
uint64_t sequence
Definition satp.h:587
uint8_t flag
Definition satp.h:585
The SATP server key structure. This structure represents the SATP server key, which is derived from t...
Definition satp.h:557
uint64_t expiration
Definition satp.h:561
QSC_SIMD_ALIGN uint8_t stc[SATP_SKEY_SIZE]
Definition satp.h:560
QSC_SIMD_ALIGN uint8_t sid[SATP_SID_SIZE]
Definition satp.h:559
QSC_SIMD_ALIGN uint8_t sdk[SATP_SKEY_SIZE]
Definition satp.h:558