Skip to content

Commit 8762082

Browse files
committed
2026-05-29
1 parent 3e75709 commit 8762082

8 files changed

Lines changed: 188 additions & 122 deletions

File tree

Source/Client/appclt.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,10 @@ static void qsc_socket_receive_async_callback(qsc_socket* source, const uint8_t*
143143
{
144144
/* convert the bytes to packet */
145145
pkt.pmessage = mpkt;
146-
skdp_stream_to_packet(message, *msglen, &pkt);
147-
148-
if (pkt.flag == skdp_flag_encrypted_message)
146+
if (skdp_stream_to_packet(message, *msglen, &pkt, sizeof(mpkt)) == true &&
147+
pkt.flag == skdp_flag_encrypted_message)
149148
{
150-
qerr = skdp_client_decrypt_packet(&m_skdp_client_ctx, &pkt, (uint8_t*)msgstr, msglen);
149+
qerr = skdp_client_decrypt_packet(&m_skdp_client_ctx, &pkt, (uint8_t*)msgstr, sizeof(msgstr), msglen);
151150

152151
if (qerr == skdp_error_none)
153152
{

Source/SKDP/skdp.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,25 @@ void skdp_packet_clear(skdp_network_packet* packet)
250250
}
251251
}
252252

253-
void skdp_packet_header_deserialize(const uint8_t* header, skdp_network_packet* packet)
253+
bool skdp_packet_header_deserialize(const uint8_t* header, size_t headerlen, skdp_network_packet* packet)
254254
{
255255
SKDP_ASSERT(header != NULL);
256256
SKDP_ASSERT(packet != NULL);
257257

258-
if (header != NULL && packet != NULL)
258+
bool res;
259+
260+
res = false;
261+
262+
if (header != NULL && packet != NULL && headerlen >= SKDP_HEADER_SIZE)
259263
{
260264
packet->flag = header[0U];
261265
packet->msglen = qsc_intutils_le8to32(header + sizeof(uint8_t));
262266
packet->sequence = qsc_intutils_le8to64(header + sizeof(uint8_t) + sizeof(uint32_t));
263267
packet->utctime = qsc_intutils_le8to64(header + sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint64_t));
268+
res = true;
264269
}
270+
271+
return res;
265272
}
266273

267274
void skdp_packet_header_serialize(const skdp_network_packet* packet, uint8_t* header)
@@ -339,24 +346,39 @@ size_t skdp_packet_to_stream(const skdp_network_packet* packet, uint8_t* pstream
339346
return res;
340347
}
341348

342-
void skdp_stream_to_packet(const uint8_t* pstream, size_t streamlen, skdp_network_packet* packet)
349+
bool skdp_stream_to_packet(const uint8_t* pstream, size_t streamlen, skdp_network_packet* packet, size_t message_capacity)
343350
{
344351
SKDP_ASSERT(packet != NULL);
345352
SKDP_ASSERT(pstream != NULL);
346353

347-
if (packet != NULL && pstream != NULL)
354+
bool res;
355+
356+
res = false;
357+
358+
if (packet != NULL && pstream != NULL && streamlen >= SKDP_HEADER_SIZE)
348359
{
349360
packet->flag = pstream[0U];
350361
packet->msglen = qsc_intutils_le8to32(pstream + sizeof(uint8_t));
351362
packet->sequence = qsc_intutils_le8to64(pstream + sizeof(uint8_t) + sizeof(uint32_t));
352363
packet->utctime = qsc_intutils_le8to64(pstream + sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint64_t));
353364

354-
if (packet->msglen + SKDP_HEADER_SIZE <= streamlen && packet->msglen <= SKDP_MESSAGE_MAX)
365+
if (packet->msglen <= streamlen - SKDP_HEADER_SIZE && packet->msglen <= message_capacity && packet->msglen <= SKDP_MESSAGE_MAX)
355366
{
356-
if (packet->msglen <= SKDP_MESSAGE_MAX)
367+
if (packet->msglen == 0U || packet->pmessage != NULL)
357368
{
358-
qsc_memutils_copy(packet->pmessage, pstream + sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint64_t), packet->msglen);
369+
if (packet->msglen != 0U)
370+
{
371+
qsc_memutils_copy(packet->pmessage, pstream + SKDP_HEADER_SIZE, packet->msglen);
372+
}
373+
374+
res = true;
359375
}
360376
}
377+
else
378+
{
379+
packet->msglen = 0U;
380+
}
361381
}
382+
383+
return res;
362384
}

Source/SKDP/skdp.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,12 @@ SKDP_EXPORT_API const char* skdp_error_to_string(skdp_errors error);
852852
* This function converts a serialized byte array representing a SKDP packet header into a structured SKDP network packet.
853853
*
854854
* \param header A pointer to the input header byte array.
855+
* \param headerlen The length of the input header byte array.
855856
* \param packet A pointer to the SKDP network packet structure to populate.
857+
*
858+
* \return Returns true if the header was deserialized; otherwise, returns false.
856859
*/
857-
SKDP_EXPORT_API void skdp_packet_header_deserialize(const uint8_t* header, skdp_network_packet* packet);
860+
SKDP_EXPORT_API bool skdp_packet_header_deserialize(const uint8_t* header, size_t headerlen, skdp_network_packet* packet);
858861

859862
/**
860863
* \brief Serialize a SKDP packet header into a byte array.
@@ -912,7 +915,10 @@ SKDP_EXPORT_API size_t skdp_packet_to_stream(const skdp_network_packet* packet,
912915
* \param pstream A pointer to the input byte stream.
913916
* \param streamlen The length of the input byte stream.
914917
* \param packet A pointer to the SKDP network packet structure to populate.
918+
* \param message_capacity The size of the packet message buffer in bytes.
919+
*
920+
* \return Returns true if the stream was deserialized; otherwise, returns false.
915921
*/
916-
SKDP_EXPORT_API void skdp_stream_to_packet(const uint8_t* pstream, size_t streamlen, skdp_network_packet* packet);
922+
SKDP_EXPORT_API bool skdp_stream_to_packet(const uint8_t* pstream, size_t streamlen, skdp_network_packet* packet, size_t message_capacity);
917923

918924
#endif

Source/SKDP/skdpclient.c

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static skdp_errors client_establish_request(skdp_client_state* ctx, const skdp_n
182182
if (skdp_packet_time_valid(packetin) == true)
183183
{
184184
/* generate the encryption and mac keys */
185-
qsc_cshake_initialize(&kctx, SKDP_PERMUTATION_RATE, ctx->ddk, SKDP_STK_SIZE, NULL, 0U, ctx->ssh, SKDP_STH_SIZE);
185+
qsc_cshake_initialize(&kctx, SKDP_PERMUTATION_RATE, ctx->ddk, SKDP_DDK_SIZE, NULL, 0U, ctx->ssh, SKDP_STH_SIZE);
186186
qsc_cshake_squeezeblocks(&kctx, SKDP_PERMUTATION_RATE, prnd, RNDBLK);
187187

188188
/* mac the encrypted token key */
@@ -266,37 +266,46 @@ static skdp_errors client_establish_verify(skdp_client_state* ctx, const skdp_ne
266266

267267
err = skdp_error_none;
268268

269-
/* serialize the packet header and add it to associated data */
270-
skdp_packet_header_serialize(packetin, hdr);
271-
skdp_cipher_set_associated(&ctx->rxcpr, hdr, SKDP_HEADER_SIZE);
272-
273-
/* authenticate and decrypt the cipher-text */
274-
if (skdp_cipher_transform(&ctx->rxcpr, msg, packetin->pmessage, packetin->msglen - SKDP_MACTAG_SIZE) == true)
269+
if (packetin->flag == skdp_flag_establish_response &&
270+
packetin->msglen == SKDP_ESTABLISH_RESPONSE_MESSAGE_SIZE)
275271
{
276-
qsc_keccak_state kctx = { 0 };
277-
uint8_t vhash[SKDP_HASH_SIZE] = { 0U };
272+
/* serialize the packet header and add it to associated data */
273+
skdp_packet_header_serialize(packetin, hdr);
274+
skdp_cipher_set_associated(&ctx->rxcpr, hdr, SKDP_HEADER_SIZE);
278275

279-
/* hash the stored random verification-token */
280-
qsc_sha3_initialize(&kctx);
281-
qsc_sha3_update(&kctx, SKDP_PERMUTATION_RATE, ctx->dsh, SKDP_STH_SIZE);
282-
qsc_sha3_finalize(&kctx, SKDP_PERMUTATION_RATE, vhash);
276+
/* authenticate and decrypt the cipher-text */
277+
if (skdp_cipher_transform(&ctx->rxcpr, msg, packetin->pmessage, packetin->msglen - SKDP_MACTAG_SIZE) == true)
278+
{
279+
qsc_keccak_state kctx = { 0 };
280+
uint8_t vhash[SKDP_HASH_SIZE] = { 0U };
283281

284-
qsc_memutils_secure_erase(&kctx, sizeof(qsc_keccak_state));
282+
/* hash the stored random verification-token */
283+
qsc_sha3_initialize(&kctx);
284+
qsc_sha3_update(&kctx, SKDP_PERMUTATION_RATE, ctx->dsh, SKDP_STH_SIZE);
285+
qsc_sha3_finalize(&kctx, SKDP_PERMUTATION_RATE, vhash);
285286

286-
if (qsc_intutils_verify(vhash, msg, SKDP_HASH_SIZE) == 0)
287-
{
288-
ctx->exflag = skdp_flag_session_established;
287+
qsc_memutils_secure_erase(&kctx, sizeof(qsc_keccak_state));
288+
289+
if (qsc_intutils_verify(vhash, msg, SKDP_HASH_SIZE) == 0)
290+
{
291+
ctx->exflag = skdp_flag_session_established;
292+
}
293+
else
294+
{
295+
ctx->exflag = skdp_flag_none;
296+
err = skdp_error_establish_failure;
297+
}
289298
}
290299
else
291300
{
292301
ctx->exflag = skdp_flag_none;
293-
err = skdp_error_establish_failure;
302+
err = skdp_error_cipher_auth_failure;
294303
}
295304
}
296305
else
297306
{
298307
ctx->exflag = skdp_flag_none;
299-
err = skdp_error_cipher_auth_failure;
308+
err = skdp_error_invalid_input;
300309
}
301310

302311
return err;
@@ -333,7 +342,7 @@ static skdp_errors client_key_exchange(skdp_client_state* ctx, qsc_socket* sock)
333342
if (rlen == SKDP_CONNECT_RESPONSE_PACKET_SIZE)
334343
{
335344
/* convert server response to packet */
336-
skdp_packet_header_deserialize(mresp, &resp);
345+
skdp_packet_header_deserialize(mresp, SKDP_HEADER_SIZE, &resp);
337346
resp.pmessage = mresp + SKDP_HEADER_SIZE;
338347

339348
if (resp.sequence == ctx->rxseq)
@@ -397,7 +406,7 @@ static skdp_errors client_key_exchange(skdp_client_state* ctx, qsc_socket* sock)
397406

398407
if (rlen == SKDP_EXCHANGE_RESPONSE_PACKET_SIZE)
399408
{
400-
skdp_packet_header_deserialize(mresp, &resp);
409+
skdp_packet_header_deserialize(mresp, SKDP_HEADER_SIZE, &resp);
401410
resp.pmessage = mresp + SKDP_HEADER_SIZE;
402411

403412
if (resp.sequence == ctx->rxseq)
@@ -460,7 +469,7 @@ static skdp_errors client_key_exchange(skdp_client_state* ctx, qsc_socket* sock)
460469

461470
if (rlen == SKDP_ESTABLISH_RESPONSE_PACKET_SIZE)
462471
{
463-
skdp_packet_header_deserialize(mresp, &resp);
472+
skdp_packet_header_deserialize(mresp, SKDP_HEADER_SIZE, &resp);
464473
resp.pmessage = mresp + SKDP_HEADER_SIZE;
465474

466475
if (resp.sequence == ctx->rxseq)
@@ -654,7 +663,7 @@ void skdp_client_connection_close(skdp_client_state* ctx, qsc_socket* sock, skdp
654663
client_dispose(ctx);
655664
}
656665

657-
skdp_errors skdp_client_decrypt_packet(skdp_client_state* ctx, const skdp_network_packet* packetin, uint8_t* message, size_t* msglen)
666+
skdp_errors skdp_client_decrypt_packet(skdp_client_state* ctx, const skdp_network_packet* packetin, uint8_t* message, size_t message_capacity, size_t* msglen)
658667
{
659668
SKDP_ASSERT(ctx != NULL);
660669
SKDP_ASSERT(message != NULL);
@@ -675,22 +684,26 @@ skdp_errors skdp_client_decrypt_packet(skdp_client_state* ctx, const skdp_networ
675684
/* change 1.1 anti-replay; verify the packet time */
676685
if (skdp_packet_time_valid(packetin) == true)
677686
{
678-
/* serialize the header and add it to the ciphers associated data */
679-
skdp_packet_header_serialize(packetin, hdr);
680-
skdp_cipher_set_associated(&ctx->rxcpr, hdr, SKDP_HEADER_SIZE);
681-
682-
if (packetin->msglen >= SKDP_MACTAG_SIZE)
687+
if (packetin->flag == skdp_flag_encrypted_message &&
688+
packetin->msglen >= SKDP_MACTAG_SIZE &&
689+
packetin->msglen <= SKDP_MESSAGE_SIZE + SKDP_MACTAG_SIZE &&
690+
packetin->msglen - SKDP_MACTAG_SIZE <= message_capacity)
683691
{
692+
/* serialize the header and add it to the ciphers associated data */
693+
skdp_packet_header_serialize(packetin, hdr);
694+
skdp_cipher_set_associated(&ctx->rxcpr, hdr, SKDP_HEADER_SIZE);
695+
684696
*msglen = packetin->msglen - SKDP_MACTAG_SIZE;
685-
ctx->rxseq += 1;
686697

687698
/* authenticate then decrypt the data */
688699
if (skdp_cipher_transform(&ctx->rxcpr, message, packetin->pmessage, *msglen) == true)
689700
{
701+
ctx->rxseq += 1U;
690702
err = skdp_error_none;
691703
}
692704
else
693705
{
706+
ctx->exflag = skdp_flag_none;
694707
err = skdp_error_cipher_auth_failure;
695708
}
696709
}
@@ -716,7 +729,7 @@ skdp_errors skdp_client_decrypt_packet(skdp_client_state* ctx, const skdp_networ
716729
}
717730
}
718731

719-
if (err != skdp_error_none)
732+
if (msglen != NULL && err != skdp_error_none)
720733
{
721734
*msglen = 0;
722735
}
@@ -738,22 +751,29 @@ skdp_errors skdp_client_encrypt_packet(skdp_client_state* ctx, const uint8_t* me
738751
{
739752
if (ctx->exflag == skdp_flag_session_established)
740753
{
741-
uint8_t hdr[SKDP_HEADER_SIZE] = { 0U };
754+
if (msglen <= SKDP_MESSAGE_SIZE)
755+
{
756+
uint8_t hdr[SKDP_HEADER_SIZE] = { 0U };
742757

743-
/* assemble the encryption packet */
744-
ctx->txseq += 1;
745-
packetout->flag = skdp_flag_encrypted_message;
746-
packetout->msglen = (uint32_t)msglen + SKDP_MACTAG_SIZE;
747-
packetout->sequence = ctx->txseq;
748-
/* change 1.1 anti-replay; set the packet utc time field */
749-
skdp_packet_set_utc_time(packetout);
750-
/* serialize the header and add it to the ciphers associated data */
751-
skdp_packet_header_serialize(packetout, hdr);
752-
skdp_cipher_set_associated(&ctx->txcpr, hdr, SKDP_HEADER_SIZE);
753-
/* encrypt the message */
754-
skdp_cipher_transform(&ctx->txcpr, packetout->pmessage, message, msglen);
755-
756-
err = skdp_error_none;
758+
/* assemble the encryption packet */
759+
ctx->txseq += 1U;
760+
packetout->flag = skdp_flag_encrypted_message;
761+
packetout->msglen = (uint32_t)msglen + SKDP_MACTAG_SIZE;
762+
packetout->sequence = ctx->txseq;
763+
/* change 1.1 anti-replay; set the packet utc time field */
764+
skdp_packet_set_utc_time(packetout);
765+
/* serialize the header and add it to the ciphers associated data */
766+
skdp_packet_header_serialize(packetout, hdr);
767+
skdp_cipher_set_associated(&ctx->txcpr, hdr, SKDP_HEADER_SIZE);
768+
/* encrypt the message */
769+
skdp_cipher_transform(&ctx->txcpr, packetout->pmessage, message, msglen);
770+
771+
err = skdp_error_none;
772+
}
773+
else
774+
{
775+
err = skdp_error_invalid_input;
776+
}
757777
}
758778
else
759779
{

Source/SKDP/skdpclient.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,12 @@ SKDP_EXPORT_API void skdp_client_connection_close(skdp_client_state* ctx, qsc_so
190190
* \param ctx A pointer to the SKDP client state structure.
191191
* \param packetin [const] A pointer to the input SKDP network packet.
192192
* \param message The output buffer where the decrypted message will be stored.
193+
* \param message_capacity The size of the output message buffer in bytes.
193194
* \param msglen A pointer to a variable that receives the length of the decrypted message.
194195
*
195196
* \return Returns a value of type \c skdp_errors indicating the result of the decryption operation.
196197
*/
197-
SKDP_EXPORT_API skdp_errors skdp_client_decrypt_packet(skdp_client_state* ctx, const skdp_network_packet* packetin, uint8_t* message, size_t* msglen);
198+
SKDP_EXPORT_API skdp_errors skdp_client_decrypt_packet(skdp_client_state* ctx, const skdp_network_packet* packetin, uint8_t* message, size_t message_capacity, size_t* msglen);
198199

199200
/*!
200201
* \brief Encrypt a message into an SKDP packet.
@@ -207,7 +208,7 @@ SKDP_EXPORT_API skdp_errors skdp_client_decrypt_packet(skdp_client_state* ctx, c
207208
*
208209
* \param ctx A pointer to the SKDP client state structure.
209210
* \param message [const] The plaintext message to be encrypted.
210-
* \param msglen The length of the plaintext message in bytes.
211+
* \param msglen The length of the plaintext message in bytes. The length must not exceed \c SKDP_MESSAGE_SIZE.
211212
* \param packetout A pointer to the output SKDP network packet structure.
212213
*
213214
* \return Returns a value of type \c skdp_errors indicating the success or failure of the encryption process.

0 commit comments

Comments
 (0)