QSC Post Quantum Cryptographic Library 1.1.0.2 (B2)
A post quantum secure library written in Ansi C
Loading...
Searching...
No Matches
encoding.h
Go to the documentation of this file.
1/* 2020-2026 Quantum Resistant Cryptographic Solutions Corporation
2 * All Rights Reserved.
3 *
4 * NOTICE:
5 * This software and all accompanying materials are the exclusive property of
6 * Quantum Resistant Cryptographic Solutions Corporation (QRCS). The intellectual
7 * and technical concepts contained herein are proprietary to QRCS and are
8 * protected under applicable Canadian, U.S., and international copyright,
9 * patent, and trade secret laws.
10 *
11 * CRYPTOGRAPHIC ALGORITHMS AND IMPLEMENTATIONS:
12 * - This software includes implementations of cryptographic primitives and
13 * algorithms that are standardized or in the public domain, such as AES
14 * and SHA-3, which are not proprietary to QRCS.
15 * - This software also includes cryptographic primitives, constructions, and
16 * algorithms designed by QRCS, including but not limited to RCS, SCB, CSX, QMAC, and
17 * related components, which are proprietary to QRCS.
18 * - All source code, implementations, protocol compositions, optimizations,
19 * parameter selections, and engineering work contained in this software are
20 * original works of QRCS and are protected under this license.
21 *
22 * LICENSE AND USE RESTRICTIONS:
23 * - This software is licensed under the Quantum Resistant Cryptographic Solutions
24 * Public Research and Evaluation License (QRCS-PREL), 2025-2026.
25 * - Permission is granted solely for non-commercial evaluation, academic research,
26 * cryptographic analysis, interoperability testing, and feasibility assessment.
27 * - Commercial use, production deployment, commercial redistribution, or
28 * integration into products or services is strictly prohibited without a
29 * separate written license agreement executed with QRCS.
30 * - Licensing and authorized distribution are solely at the discretion of QRCS.
31 *
32 * EXPERIMENTAL CRYPTOGRAPHY NOTICE:
33 * Portions of this software may include experimental, novel, or evolving
34 * cryptographic designs. Use of this software is entirely at the user's risk.
35 *
36 * DISCLAIMER:
37 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38 * IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS
39 * FOR A PARTICULAR PURPOSE, SECURITY, OR NON-INFRINGEMENT. QRCS DISCLAIMS ALL
40 * LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
41 * ARISING FROM THE USE OR MISUSE OF THIS SOFTWARE.
42 *
43 * FULL LICENSE:
44 * This software is subject to the Quantum Resistant Cryptographic Solutions
45 * Public Research and Evaluation License (QRCS-PREL), 2025-2026. The complete license terms
46 * are provided in the accompanying LICENSE file or at https://www.qrcscorp.ca.
47 *
48 * Written by: John G. Underhill
49 * Contact: contact@qrcscorp.ca
50 */
51
52#ifndef QSC_ENCODING_H
53#define QSC_ENCODING_H
54
55#include "qsccommon.h"
56
57QSC_CPLUSPLUS_ENABLED_START
58
176
177/* ========================================================================== */
178/* BER Tag-Class Constants */
179/* ========================================================================== */
180
189#define QSC_ENCODING_BER_CLASS_UNIVERSAL 0x00U
190
199#define QSC_ENCODING_BER_CLASS_APPLICATION 0x40U
200
209#define QSC_ENCODING_BER_CLASS_CONTEXT_SPECIFIC 0x80U
210
218#define QSC_ENCODING_BER_CLASS_PRIVATE 0xC0U
219
230#define QSC_BER_ENCODING_INDEFINITE_LENGTH ((size_t)-1)
231
232/* ========================================================================== */
233/* Universal ASN.1 Tag-Number Enumeration */
234/* ========================================================================== */
235
278
279/* ========================================================================== */
280/* BER Element Structure */
281/* ========================================================================== */
282
334
335/* ========================================================================== */
336/* Base64 Functions */
337/* ========================================================================== */
338
357QSC_EXPORT_API bool qsc_encoding_base64_decode(uint8_t* output, size_t otplen, const char* input, size_t inplen);
358
372QSC_EXPORT_API size_t qsc_encoding_base64_decoded_size(const char* input, size_t length);
373
390QSC_EXPORT_API bool qsc_encoding_base64_encode(char* output, size_t otplen, const uint8_t* input, size_t inplen);
391
404
415
416/* ========================================================================== */
417/* BER Decode Functions */
418/* ========================================================================== */
419
443QSC_EXPORT_API qsc_encoding_ber_element* qsc_encoding_ber_decode_element(const uint8_t* buffer, size_t buflen, size_t* consumed);
444
464QSC_EXPORT_API size_t qsc_encoding_ber_decode_length(const uint8_t* buffer, size_t buflen, size_t* length, bool* indef);
465
482QSC_EXPORT_API size_t qsc_encoding_ber_decode_tag(const uint8_t* buffer, size_t buflen, uint8_t* tagclass, bool* construct, uint32_t* tagnum);
483
484/* ========================================================================== */
485/* BER Encode Functions */
486/* ========================================================================== */
487
512QSC_EXPORT_API size_t qsc_encoding_ber_encode_element(qsc_encoding_ber_element* element, uint8_t* buffer, size_t buflen);
513
531QSC_EXPORT_API size_t qsc_encoding_ber_encode_length(size_t length, uint8_t* buffer, size_t buflen);
532
553QSC_EXPORT_API size_t qsc_encoding_ber_encode_tag(uint8_t tagclass, bool construct, uint32_t tagnum, uint8_t* buffer, size_t buflen);
554
566
567/* ========================================================================== */
568/* DER Functions */
569/* ========================================================================== */
570
588QSC_EXPORT_API qsc_encoding_ber_element* qsc_encoding_der_decode_element(const uint8_t* buffer, size_t buflen, size_t* consumed);
589
608QSC_EXPORT_API size_t qsc_encoding_der_encode_element(qsc_encoding_ber_element* element, uint8_t* buffer, size_t buflen);
609
610/* ========================================================================== */
611/* Hexadecimal Functions */
612/* ========================================================================== */
613
630QSC_EXPORT_API bool qsc_encoding_hex_decode(const char* input, size_t inplen, uint8_t* output, size_t otplen, size_t* declen);
631
645QSC_EXPORT_API bool qsc_encoding_hex_encode(const uint8_t* input, size_t inplen, char* output, size_t otplen);
646
647/* ========================================================================== */
648/* PEM Functions */
649/* ========================================================================== */
650
674QSC_EXPORT_API bool qsc_encoding_pem_decode(const char* input, size_t inplen, uint8_t* output, size_t otplen, size_t* declen);
675
696QSC_EXPORT_API bool qsc_encoding_pem_encode(const char* label, char* output, size_t otplen, const uint8_t* data, size_t datalen);
697
698/* ========================================================================== */
699/* Debug Self-Test Function */
700/* ========================================================================== */
701
702#if defined(QSC_DEBUG_MODE)
712QSC_EXPORT_API bool qsc_encoding_tests(void);
713#endif
714
715QSC_CPLUSPLUS_ENABLED_END
716
717#endif
QSC_EXPORT_API qsc_encoding_ber_element * qsc_encoding_ber_decode_element(const uint8_t *buffer, size_t buflen, size_t *consumed)
Decode a single BER element from an octet buffer.
Definition encoding.c:1243
QSC_EXPORT_API size_t qsc_encoding_base64_decoded_size(const char *input, size_t length)
Compute the byte count required to hold the decoded form of a Base64 string.
Definition encoding.c:378
QSC_EXPORT_API size_t qsc_encoding_der_encode_element(qsc_encoding_ber_element *element, uint8_t *buffer, size_t buflen)
Encode an ASN.1 element tree using DER.
Definition encoding.c:1351
QSC_EXPORT_API size_t qsc_encoding_ber_encode_element(qsc_encoding_ber_element *element, uint8_t *buffer, size_t buflen)
Encode a complete BER element (tag + length + value) to an octet buffer.
Definition encoding.c:1128
QSC_EXPORT_API bool qsc_encoding_pem_encode(const char *label, char *output, size_t otplen, const uint8_t *data, size_t datalen)
Encode binary data in PEM format.
Definition encoding.c:1740
QSC_EXPORT_API size_t qsc_encoding_ber_decode_length(const uint8_t *buffer, size_t buflen, size_t *length, bool *indef)
Decode a BER length field from an octet buffer.
Definition encoding.c:776
QSC_EXPORT_API bool qsc_encoding_pem_decode(const char *input, size_t inplen, uint8_t *output, size_t otplen, size_t *declen)
Decode a PEM-formatted string to binary data.
Definition encoding.c:1624
QSC_EXPORT_API size_t qsc_encoding_ber_encode_length(size_t length, uint8_t *buffer, size_t buflen)
Encode a length value to BER form.
Definition encoding.c:649
QSC_EXPORT_API bool qsc_encoding_hex_encode(const uint8_t *input, size_t inplen, char *output, size_t otplen)
Encode binary data to an upper-case hexadecimal string.
Definition encoding.c:1597
QSC_EXPORT_API bool qsc_encoding_base64_is_valid_char(char value)
Test whether a character belongs to the Base64 alphabet.
Definition encoding.c:336
QSC_EXPORT_API size_t qsc_encoding_ber_decode_tag(const uint8_t *buffer, size_t buflen, uint8_t *tagclass, bool *construct, uint32_t *tagnum)
Decode a BER tag field from an octet buffer.
Definition encoding.c:707
QSC_EXPORT_API void qsc_encoding_ber_free_element(qsc_encoding_ber_element *element)
Recursively free a decoded BER element tree.
Definition encoding.c:845
QSC_EXPORT_API size_t qsc_encoding_base64_encoded_size(size_t length)
Compute the character count required to hold the Base64 encoding of length input bytes,...
Definition encoding.c:355
qsc_encoding_ber_asn1_tag_t
Universal ASN.1 tag numbers as defined in X.680 8.6 and X.690 Table 1.
Definition encoding.h:246
@ BER_ASN1_UTCTIME
Definition encoding.h:269
@ BER_ASN1_SEQUENCE
Definition encoding.h:262
@ BER_ASN1_INTEGER
Definition encoding.h:249
@ BER_ASN1_GENERAL_STRING
Definition encoding.h:273
@ BER_ASN1_CHARACTER_STRING
Definition encoding.h:275
@ BER_ASN1_OBJECT_DESCRIPTOR
Definition encoding.h:254
@ BER_ASN1_VIDEOTEX_STRING
Definition encoding.h:267
@ BER_ASN1_UTF8_STRING
Definition encoding.h:259
@ BER_ASN1_PRINTABLE_STRING
Definition encoding.h:265
@ BER_ASN1_BIT_STRING
Definition encoding.h:250
@ BER_ASN1_T61_STRING
Definition encoding.h:266
@ BER_ASN1_NULL
Definition encoding.h:252
@ BER_ASN1_EOC
Definition encoding.h:247
@ BER_ASN1_OCTET_STRING
Definition encoding.h:251
@ BER_ASN1_BMP_STRING
Definition encoding.h:276
@ BER_ASN1_GENERALIZEDTIME
Definition encoding.h:270
@ BER_ASN1_OBJECT_IDENTIFIER
Definition encoding.h:253
@ BER_ASN1_VISIBLE_STRING
Definition encoding.h:272
@ BER_ASN1_EMBEDDED_PDV
Definition encoding.h:258
@ BER_ASN1_SET
Definition encoding.h:263
@ BER_ASN1_IA5_STRING
Definition encoding.h:268
@ BER_ASN1_UNIVERSAL_STRING
Definition encoding.h:274
@ BER_ASN1_REAL
Definition encoding.h:256
@ BER_ASN1_ENUMERATED
Definition encoding.h:257
@ BER_ASN1_GRAPHIC_STRING
Definition encoding.h:271
@ BER_ASN1_NUMERIC_STRING
Definition encoding.h:264
@ BER_ASN1_BOOLEAN
Definition encoding.h:248
@ BER_ASN1_RELATIVE_OID
Definition encoding.h:260
@ BER_ASN1_EXTERNAL
Definition encoding.h:255
QSC_EXPORT_API bool qsc_encoding_base64_encode(char *output, size_t otplen, const uint8_t *input, size_t inplen)
Encode a byte array to a Base64 string.
Definition encoding.c:411
QSC_EXPORT_API bool qsc_encoding_hex_decode(const char *input, size_t inplen, uint8_t *output, size_t otplen, size_t *declen)
Decode a hexadecimal string to binary data.
Definition encoding.c:1494
QSC_EXPORT_API size_t qsc_encoding_ber_encode_tag(uint8_t tagclass, bool construct, uint32_t tagnum, uint8_t *buffer, size_t buflen)
Encode an ASN.1 tag to BER form.
Definition encoding.c:568
QSC_EXPORT_API qsc_encoding_ber_element * qsc_encoding_der_decode_element(const uint8_t *buffer, size_t buflen, size_t *consumed)
Decode a single DER element from an octet buffer.
Definition encoding.c:1316
QSC_EXPORT_API bool qsc_encoding_base64_decode(uint8_t *output, size_t otplen, const char *input, size_t inplen)
Decode a Base64-encoded string to a byte array.
Definition encoding.c:452
Contains common definitions for the Quantum Secure Cryptographic (QSC) library.
#define QSC_EXPORT_API
API export macro for Microsoft compilers when importing from a DLL.
Definition qsccommon.h:605
In-memory representation of a single BER/DER ASN.1 TLV element.
Definition encoding.h:311
size_t ccount
Definition encoding.h:332
uint8_t tagclass
Definition encoding.h:312
uint32_t tagnumber
Definition encoding.h:318
bool constructed
Definition encoding.h:316
uint8_t * value
Definition encoding.h:325
struct qsc_encoding_ber_element ** children
Definition encoding.h:329
bool indefinite
Definition encoding.h:320
size_t length
Definition encoding.h:322