SKDP: Symmetric Key Distribution Protocol 1.1.0.0 (A1)
Encrypted tunneling protocol using pre-shared keys
skdp.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 SKPD_H
41#define SKPD_H
42
43#include "common.h"
44#include "../../QSC/QSC/sha3.h"
45
83
88#define SKDP_CONFIG_SIZE 26
89
94#define SKDP_ERROR_SIZE 1
95
100#define SKDP_EXP_SIZE 8
101
106#define SKDP_HEADER_SIZE 21
107
112#define SKDP_KEEPALIVE_MESSAGE 8
113
118#define SKDP_KEEPALIVE_STRING 20
119
124#define SKDP_KEEPALIVE_TIMEOUT (300 * 1000)
125
130#define SKDP_MESSAGE_SIZE 1024
131
136#define SKDP_MESSAGE_MAX (SKDP_MESSAGE_SIZE + SKDP_HEADER_SIZE)
137
142#define SKDP_SERVER_PORT 2201
143
148#define SKDP_MID_SIZE 4
149
154#define SKDP_SID_SIZE 8
155
160#define SKDP_DID_SIZE 12
161
166#define SKDP_TID_SIZE 4
167
172#define SKDP_KID_SIZE 16
173
178#define SKDP_SEQUENCE_TERMINATOR 0xFFFFFFFF
179
186#if !defined(SKDP_PROTOCOL_SEC512)
187# if !defined(SKDP_PROTOCOL_SEC256)
188# define SKDP_PROTOCOL_SEC256
189# endif
190#endif
191
192#if defined(SKDP_PROTOCOL_SEC512)
193
194/* 512-bit security configuration definitions */
195
200# define SKDP_CPRKEY_SIZE 64
201
206# define SKDP_DDK_SIZE 64
207
212# define SKDP_DTK_SIZE 64
213
218# define SKDP_HASH_SIZE 64
219
224# define SKDP_PERMUTATION_RATE QSC_KECCAK_512_RATE
225
230# define SKDP_MACKEY_SIZE 64
231
236# define SKDP_MACTAG_SIZE 64
237
242# define SKDP_MDK_SIZE 64
243
248# define SKDP_SDK_SIZE 64
249
254# define SKDP_STH_SIZE 64
255
260# define SKDP_STK_SIZE 64
261
266# define SKDP_STOK_SIZE 64
267
272#define SKDP_EXCHANGE_MAX_MESSAGE_SIZE (SKDP_DTK_SIZE + SKDP_MACKEY_SIZE + SKDP_HEADER_SIZE)
273
277static const char SKDP_CONFIG_STRING[SKDP_CONFIG_SIZE] = "r01-skdp-rcs512-keccak512";
278
279#else
280
281/* 256-bit security configuration definitions */
282
287# define SKDP_CPRKEY_SIZE 32
288
293# define SKDP_DDK_SIZE 32
294
299# define SKDP_DTK_SIZE 32
300
305# define SKDP_HASH_SIZE 32
306
311# define SKDP_MACKEY_SIZE 32
312
317# define SKDP_MACTAG_SIZE 32
318
323# define SKDP_MDK_SIZE 32
324
329# define SKDP_PERMUTATION_RATE QSC_KECCAK_256_RATE
330
335# define SKDP_SDK_SIZE 32
336
341# define SKDP_STK_SIZE 32
342
347# define SKDP_STH_SIZE 32
348
353# define SKDP_STOK_SIZE 32
354
359#define SKDP_EXCHANGE_MAX_MESSAGE_SIZE (SKDP_KID_SIZE + SKDP_CONFIG_SIZE + SKDP_STOK_SIZE + SKDP_HEADER_SIZE)
360
364static const char SKDP_CONFIG_STRING[SKDP_CONFIG_SIZE] = "r01-skdp-rcs256-keccak256";
365
366#endif
367
372#define SKDP_KEY_DURATION_DAYS 365
373
382#define SKDP_PACKET_TIME_THRESHOLD 60
383
388#define SKDP_KEY_DURATION_SECONDS (SKDP_KEY_DURATION_DAYS * 24 * 60 * 60)
389
394#define SKDP_DEVKEY_ENCODED_SIZE (SKDP_KID_SIZE + SKDP_DDK_SIZE + SKDP_EXP_SIZE)
395
400#define SKDP_MSTKEY_ENCODED_SIZE (SKDP_KID_SIZE + SKDP_MDK_SIZE + SKDP_EXP_SIZE)
401
406#define SKDP_SRVKEY_ENCODED_SIZE (SKDP_KID_SIZE + SKDP_SDK_SIZE + SKDP_EXP_SIZE)
407
416#define SKDP_CONNECT_REQUEST_MESSAGE_SIZE (SKDP_KID_SIZE + SKDP_CONFIG_SIZE + SKDP_STOK_SIZE)
417
425#define SKDP_CONNECT_REQUEST_PACKET_SIZE (SKDP_CONNECT_REQUEST_MESSAGE_SIZE + SKDP_HEADER_SIZE)
426
431#define SKDP_EXCHANGE_REQUEST_MESSAGE_SIZE (SKDP_DTK_SIZE + SKDP_MACKEY_SIZE)
432
440#define SKDP_EXCHANGE_REQUEST_PACKET_SIZE (SKDP_EXCHANGE_REQUEST_MESSAGE_SIZE + SKDP_HEADER_SIZE)
441
446#define SKDP_ESTABLISH_REQUEST_MESSAGE_SIZE (SKDP_STH_SIZE + SKDP_MACTAG_SIZE)
447
455#define SKDP_ESTABLISH_REQUEST_PACKET_SIZE (SKDP_ESTABLISH_REQUEST_MESSAGE_SIZE + SKDP_HEADER_SIZE)
456
461#define SKDP_CONNECT_RESPONSE_MESSAGE_SIZE (SKDP_KID_SIZE + SKDP_CONFIG_SIZE + SKDP_STOK_SIZE)
462
470#define SKDP_CONNECT_RESPONSE_PACKET_SIZE (SKDP_CONNECT_RESPONSE_MESSAGE_SIZE + SKDP_HEADER_SIZE)
471
476#define SKDP_EXCHANGE_RESPONSE_MESSAGE_SIZE (SKDP_DTK_SIZE + SKDP_MACKEY_SIZE)
477
485#define SKDP_EXCHANGE_RESPONSE_PACKET_SIZE (SKDP_EXCHANGE_RESPONSE_MESSAGE_SIZE + SKDP_HEADER_SIZE)
486
491#define SKDP_ESTABLISH_RESPONSE_MESSAGE_SIZE (SKDP_HASH_SIZE + SKDP_MACTAG_SIZE)
492
500#define SKDP_ESTABLISH_RESPONSE_PACKET_SIZE (SKDP_ESTABLISH_RESPONSE_MESSAGE_SIZE + SKDP_HEADER_SIZE)
501
506#define SKDP_ESTABLISH_VERIFY_MESSAGE_SIZE (SKDP_HASH_SIZE + SKDP_MACTAG_SIZE)
507
515#define SKDP_ESTABLISH_VERIFY_PACKET_SIZE (SKDP_ESTABLISH_VERIFY_MESSAGE_SIZE + SKDP_HEADER_SIZE)
516
517/* error code strings */
518
520#define SKDP_ERROR_STRING_DEPTH 17
521#define SKDP_ERROR_STRING_WIDTH 128
522
523static const char SKDP_ERROR_STRINGS[SKDP_ERROR_STRING_DEPTH][SKDP_ERROR_STRING_WIDTH] =
524{
525 "No error was detected.",
526 "The cipher authentication has failed.",
527 "The kex authentication has failed.",
528 "The keep alive check failed.",
529 "The communications channel has failed.",
530 "The device could not make a connnection to the remote host.",
531 "The transmission failed at the kex establish phase.",
532 "The input is invalid.",
533 "The keep alive has expired with no response.",
534 "The key-id is not recognized.",
535 "The random generator experienced a failure.",
536 "The receiver failed at the network layer.",
537 "The transmitter failed at the network layer.",
538 "The protocol version is unknown.",
539 "The packet was received out of sequence.",
540 "The packet valid-time was exceeded",
541 "The connection experienced an error",
542};
544
553SKDP_EXPORT_API typedef struct skdp_master_key
554{
557 uint64_t expiration;
559
568SKDP_EXPORT_API typedef struct skdp_server_key
569{
572 uint64_t expiration;
574
583SKDP_EXPORT_API typedef struct skdp_device_key
584{
587 uint64_t expiration;
589
598SKDP_EXPORT_API typedef struct qsmp_keep_alive_state
599{
600 uint64_t etime;
601 uint64_t seqctr;
602 bool recd;
603} skdp_keep_alive_state;
604
613SKDP_EXPORT_API typedef struct skdp_network_packet
614{
615 uint8_t flag;
616 uint32_t msglen;
617 uint64_t sequence;
618 uint64_t utctime;
619 uint8_t* pmessage;
621
649
673
683SKDP_EXPORT_API void skdp_deserialize_device_key(skdp_device_key* dkey, const uint8_t input[SKDP_DEVKEY_ENCODED_SIZE]);
684
694SKDP_EXPORT_API void skdp_serialize_device_key(uint8_t output[SKDP_DEVKEY_ENCODED_SIZE], const skdp_device_key* dkey);
695
705SKDP_EXPORT_API void skdp_deserialize_master_key(skdp_master_key* mkey, const uint8_t input[SKDP_MSTKEY_ENCODED_SIZE]);
706
716SKDP_EXPORT_API void skdp_serialize_master_key(uint8_t output[SKDP_MSTKEY_ENCODED_SIZE], const skdp_master_key* mkey);
717
727SKDP_EXPORT_API void skdp_deserialize_server_key(skdp_server_key* skey, const uint8_t input[SKDP_SRVKEY_ENCODED_SIZE]);
728
738SKDP_EXPORT_API void skdp_serialize_server_key(uint8_t output[SKDP_SRVKEY_ENCODED_SIZE], const skdp_server_key* skey);
739
753SKDP_EXPORT_API bool skdp_generate_master_key(skdp_master_key* mkey, const uint8_t kid[SKDP_KID_SIZE]);
754
766SKDP_EXPORT_API void skdp_generate_server_key(skdp_server_key* skey, const skdp_master_key* mkey, const uint8_t kid[SKDP_KID_SIZE]);
767
779SKDP_EXPORT_API void skdp_generate_device_key(skdp_device_key* dkey, const skdp_server_key* skey, const uint8_t kid[SKDP_KID_SIZE]);
780
789SKDP_EXPORT_API void skdp_packet_clear(skdp_network_packet* packet);
790
801SKDP_EXPORT_API const char* skdp_error_to_string(skdp_errors error);
802
812SKDP_EXPORT_API void skdp_packet_header_deserialize(const uint8_t* header, skdp_network_packet* packet);
813
823SKDP_EXPORT_API void skdp_packet_header_serialize(const skdp_network_packet* packet, uint8_t* header);
824
833SKDP_EXPORT_API void skdp_packet_set_utc_time(skdp_network_packet* packet);
834
846SKDP_EXPORT_API bool skdp_packet_time_valid(const skdp_network_packet* packet);
847
859SKDP_EXPORT_API size_t skdp_packet_to_stream(const skdp_network_packet* packet, uint8_t* pstream);
860
870SKDP_EXPORT_API void skdp_stream_to_packet(const uint8_t* pstream, skdp_network_packet* packet);
871
872#endif
SKDP_EXPORT_API void skdp_deserialize_device_key(skdp_device_key *dkey, const uint8_t input[SKDP_DEVKEY_ENCODED_SIZE])
Deserialize a client device key.
Definition skdp.c:7
SKDP_EXPORT_API const char * skdp_error_to_string(skdp_errors error)
Return a string description of an SKDP error code.
Definition skdp.c:117
SKDP_EXPORT_API void skdp_deserialize_master_key(skdp_master_key *mkey, const uint8_t input[SKDP_MSTKEY_ENCODED_SIZE])
Deserialize a master key from a byte array.
Definition skdp.c:29
SKDP_EXPORT_API void skdp_packet_clear(skdp_network_packet *packet)
Clear a SKDP network packet.
Definition skdp.c:131
SKDP_EXPORT_API void skdp_packet_header_deserialize(const uint8_t *header, skdp_network_packet *packet)
Deserialize a byte array into a SKDP packet header.
Definition skdp.c:143
#define SKDP_KID_SIZE
The SKDP key identity size in bytes.
Definition skdp.h:172
#define SKDP_DDK_SIZE
The device derivation key size (in bytes) for 256-bit security.
Definition skdp.h:293
SKDP_EXPORT_API bool skdp_packet_time_valid(const skdp_network_packet *packet)
Check if a SKDP packet is received within the valid time threshold.
Definition skdp.c:176
SKDP_EXPORT_API void skdp_packet_set_utc_time(skdp_network_packet *packet)
Set the local UTC seconds time in a SKDP packet header.
Definition skdp.c:171
SKDP_EXPORT_API void skdp_generate_server_key(skdp_server_key *skey, const skdp_master_key *mkey, const uint8_t kid[SKDP_KID_SIZE])
Generate a server key-set.
Definition skdp.c:91
SKDP_EXPORT_API void skdp_serialize_device_key(uint8_t output[SKDP_DEVKEY_ENCODED_SIZE], const skdp_device_key *dkey)
Serialize a client device key.
Definition skdp.c:18
#define SKDP_SDK_SIZE
The server derivation key size (in bytes) for 256-bit security.
Definition skdp.h:335
#define SKDP_MDK_SIZE
The master derivation key size (in bytes) for 256-bit security.
Definition skdp.h:323
SKDP_EXPORT_API bool skdp_generate_master_key(skdp_master_key *mkey, const uint8_t kid[SKDP_KID_SIZE])
Generate a master key-set.
Definition skdp.c:73
SKDP_EXPORT_API size_t skdp_packet_to_stream(const skdp_network_packet *packet, uint8_t *pstream)
Serialize a SKDP packet into a byte array.
Definition skdp.c:185
SKDP_EXPORT_API void skdp_serialize_master_key(uint8_t output[SKDP_MSTKEY_ENCODED_SIZE], const skdp_master_key *mkey)
Serialize a master key into a byte array.
Definition skdp.c:40
skdp_errors
The SKDP error values.
Definition skdp.h:630
@ skdp_error_receive_failure
Definition skdp.h:642
@ skdp_error_transmit_failure
Definition skdp.h:643
@ skdp_error_unsequenced
Definition skdp.h:645
@ skdp_error_establish_failure
Definition skdp.h:637
@ skdp_error_random_failure
Definition skdp.h:641
@ skdp_error_packet_expired
Definition skdp.h:646
@ skdp_error_key_not_recognized
Definition skdp.h:640
@ skdp_error_none
Definition skdp.h:631
@ skdp_error_connection_failure
Definition skdp.h:636
@ skdp_error_bad_keep_alive
Definition skdp.h:634
@ skdp_error_unknown_protocol
Definition skdp.h:644
@ skdp_error_kex_auth_failure
Definition skdp.h:633
@ skdp_error_invalid_input
Definition skdp.h:638
@ skdp_error_channel_down
Definition skdp.h:635
@ skdp_error_general_failure
Definition skdp.h:647
@ skdp_error_keep_alive_expired
Definition skdp.h:639
@ skdp_error_cipher_auth_failure
Definition skdp.h:632
SKDP_EXPORT_API void skdp_generate_device_key(skdp_device_key *dkey, const skdp_server_key *skey, const uint8_t kid[SKDP_KID_SIZE])
Generate a device key-set.
Definition skdp.c:104
#define SKDP_CONFIG_SIZE
The size of the protocol configuration string.
Definition skdp.h:88
#define SKDP_DEVKEY_ENCODED_SIZE
The size (in bytes) of the encoded device key.
Definition skdp.h:394
SKDP_EXPORT_API void skdp_stream_to_packet(const uint8_t *pstream, skdp_network_packet *packet)
Deserialize a byte stream into a SKDP network packet.
Definition skdp.c:211
skdp_flags
The SKDP packet flag values.
Definition skdp.h:658
@ skdp_flag_establish_response
Definition skdp.h:667
@ skdp_flag_keepalive_request
Definition skdp.h:669
@ skdp_flag_none
Definition skdp.h:659
@ skdp_flag_connect_response
Definition skdp.h:661
@ skdp_flag_session_established
Definition skdp.h:670
@ skdp_flag_encrypted_message
Definition skdp.h:663
@ skdp_flag_establish_request
Definition skdp.h:666
@ skdp_flag_exchange_request
Definition skdp.h:664
@ skdp_flag_establish_verify
Definition skdp.h:668
@ skdp_flag_error_condition
Definition skdp.h:671
@ skdp_flag_connection_terminate
Definition skdp.h:662
@ skdp_flag_exchange_response
Definition skdp.h:665
@ skdp_flag_connect_request
Definition skdp.h:660
#define SKDP_MSTKEY_ENCODED_SIZE
The size (in bytes) of the encoded master key.
Definition skdp.h:400
SKDP_EXPORT_API void skdp_packet_header_serialize(const skdp_network_packet *packet, uint8_t *header)
Serialize a SKDP packet header into a byte array.
Definition skdp.c:157
SKDP_EXPORT_API void skdp_serialize_server_key(uint8_t output[SKDP_SRVKEY_ENCODED_SIZE], const skdp_server_key *skey)
Serialize a server key into a byte array.
Definition skdp.c:62
#define SKDP_SRVKEY_ENCODED_SIZE
The size (in bytes) of the encoded server key.
Definition skdp.h:406
SKDP_EXPORT_API void skdp_deserialize_server_key(skdp_server_key *skey, const uint8_t input[SKDP_SRVKEY_ENCODED_SIZE])
Deserialize a server key from a byte array.
Definition skdp.c:51
The SKDP keep alive state structure.
Definition skdp.h:599
bool recd
Definition skdp.h:602
uint64_t seqctr
Definition skdp.h:601
uint64_t etime
Definition skdp.h:600
The SKDP device key structure.
Definition skdp.h:584
uint8_t kid[SKDP_KID_SIZE]
Definition skdp.h:585
uint64_t expiration
Definition skdp.h:587
uint8_t ddk[SKDP_DDK_SIZE]
Definition skdp.h:586
The SKDP master key structure.
Definition skdp.h:554
uint8_t kid[SKDP_KID_SIZE]
Definition skdp.h:555
uint64_t expiration
Definition skdp.h:557
uint8_t mdk[SKDP_MDK_SIZE]
Definition skdp.h:556
The SKDP network packet structure.
Definition skdp.h:614
uint8_t * pmessage
Definition skdp.h:619
uint32_t msglen
Definition skdp.h:616
uint64_t utctime
Definition skdp.h:618
uint64_t sequence
Definition skdp.h:617
uint8_t flag
Definition skdp.h:615
The SKDP server key structure.
Definition skdp.h:569
uint8_t kid[SKDP_KID_SIZE]
Definition skdp.h:570
uint64_t expiration
Definition skdp.h:572
uint8_t sdk[SKDP_SDK_SIZE]
Definition skdp.h:571