QSC Post Quantum Cryptographic Library 1.0.0.6c (A6)
A post quantum secure library written in Ansi C
 
Loading...
Searching...
No Matches
encoding.h
Go to the documentation of this file.
1/*
2 * 2025 Quantum Resistant Cryptographic Solutions Corporation
3 * All Rights Reserved.
4 *
5 * NOTICE: This software and all accompanying materials are the exclusive
6 * property of Quantum Resistant Cryptographic Solutions Corporation (QRCS).
7 * The intellectual and technical concepts contained within this implementation
8 * are proprietary to QRCS and its authorized licensors and are protected under
9 * applicable U.S. and international copyright, patent, and trade secret laws.
10 *
11 * CRYPTOGRAPHIC STANDARDS:
12 * - This software includes implementations of cryptographic algorithms such as
13 * SHA3, AES, and others. These algorithms are public domain or standardized
14 * by organizations such as NIST and are NOT the property of QRCS.
15 * - However, all source code, optimizations, and implementations in this library
16 * are original works of QRCS and are protected under this license.
17 *
18 * RESTRICTIONS:
19 * - Redistribution, modification, or unauthorized distribution of this software,
20 * in whole or in part, is strictly prohibited.
21 * - This software is provided for non-commercial, educational, and research
22 * purposes only. Commercial use in any form is expressly forbidden.
23 * - Licensing and authorized distribution are solely at the discretion of QRCS.
24 * - Any use of this software implies acceptance of these restrictions.
25 *
26 * DISCLAIMER:
27 * This software is provided "as is," without warranty of any kind, express or
28 * implied, including but not limited to warranties of merchantability or fitness
29 * for a particular purpose. QRCS disclaims all liability for any direct, indirect,
30 * incidental, or consequential damages resulting from the use or misuse of this software.
31 *
32 * FULL LICENSE:
33 * This software is subject to the **Quantum Resistant Cryptographic Solutions
34 * Proprietary License (QRCS-PL)**. The complete license terms are included
35 * in the LICENSE.txt file distributed with this software.
36 *
37 * Written by: John G. Underhill
38 * Contact: john.underhill\protonmail.com
39 */
40
41#ifndef QSC_ENCODING_H
42#define QSC_ENCODING_H
43
44#include "common.h"
45
46QSC_CPLUSPLUS_ENABLED_START
47
106
115#define QSC_ENCODING_BER_CLASS_UNIVERSAL 0x00U
116
126#define QSC_ENCODING_BER_CLASS_APPLICATION 0x40U
127
137#define QSC_ENCODING_BER_CLASS_CONTEXT_SPECIFIC 0x80U
138
147#define QSC_ENCODING_BER_CLASS_PRIVATE 0xC0U
148
155#define QSC_BER_ENCODING_INDEFINITE_LENGTH ((size_t)-1)
156
196
223
234QSC_EXPORT_API bool qsc_encoding_base64_decode(uint8_t* output, size_t otplen, const char* input, size_t inlen);
235
244QSC_EXPORT_API size_t qsc_encoding_base64_decoded_size(const char* input, size_t length);
245
254QSC_EXPORT_API void qsc_encoding_base64_encode(char* output, size_t otplen, const uint8_t* input, size_t inplen);
255
264
273
287QSC_EXPORT_API qsc_encoding_ber_element* qsc_encoding_ber_decode_element(const uint8_t* buffer, size_t buflen, size_t* consumed);
288
303QSC_EXPORT_API size_t qsc_encoding_ber_decode_length(const uint8_t* buffer, size_t buflen, size_t* length, bool* indef);
304
320QSC_EXPORT_API size_t qsc_encoding_ber_decode_tag(const uint8_t* buffer, size_t buflen, uint8_t* tagclass, bool* construct, uint32_t* tagnum);
321
336QSC_EXPORT_API size_t qsc_encoding_ber_encode_element(qsc_encoding_ber_element* element, uint8_t* buffer, size_t buflen);
337
352QSC_EXPORT_API size_t qsc_encoding_ber_encode_length(size_t length, uint8_t* buffer, size_t buflen);
353
370QSC_EXPORT_API size_t qsc_encoding_ber_encode_tag(uint8_t tagclass, bool construct, uint32_t tagnum, uint8_t* buffer, size_t buflen);
371
378
393QSC_EXPORT_API qsc_encoding_ber_element* qsc_encoding_der_decode_element(const uint8_t* buffer, size_t buflen, size_t* consumed);
394
413QSC_EXPORT_API size_t qsc_encoding_der_encode_element(qsc_encoding_ber_element* element, uint8_t* buffer, size_t buflen);
414
426QSC_EXPORT_API bool qsc_encoding_hex_decode(const char* input, size_t inplen, uint8_t* output, size_t otplen, size_t* declen);
427
438QSC_EXPORT_API bool qsc_encoding_hex_encode(const uint8_t* input, size_t inplen, char* output, size_t otplen);
439
450QSC_EXPORT_API bool qsc_encoding_pem_decode(const char* input, uint8_t* output, size_t otplen, size_t* declen);
451
463QSC_EXPORT_API bool qsc_encoding_pem_encode(const char* label, char* output, size_t otplen, const uint8_t* data, size_t datalen);
464
465QSC_CPLUSPLUS_ENABLED_END
466
467#endif
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 common.h:520
QSC_EXPORT_API qsc_encoding_ber_element * qsc_encoding_ber_decode_element(const uint8_t *buffer, size_t buflen, size_t *consumed)
Decodes a BER element from encoded data.
Definition encoding.c:6
QSC_EXPORT_API size_t qsc_encoding_base64_decoded_size(const char *input, size_t length)
Gets the expected size of an array required for Base64 decoding.
Definition encoding.c:740
QSC_EXPORT_API size_t qsc_encoding_der_encode_element(qsc_encoding_ber_element *element, uint8_t *buffer, size_t buflen)
Encodes an ASN.1 element using DER (Distinguished Encoding Rules).
Definition encoding.c:892
QSC_EXPORT_API size_t qsc_encoding_ber_encode_element(qsc_encoding_ber_element *element, uint8_t *buffer, size_t buflen)
Encodes a complete BER element.
Definition encoding.c:374
QSC_EXPORT_API bool qsc_encoding_pem_encode(const char *label, char *output, size_t otplen, const uint8_t *data, size_t datalen)
Encodes binary data in PEM format.
Definition encoding.c:1217
QSC_EXPORT_API size_t qsc_encoding_ber_decode_length(const uint8_t *buffer, size_t buflen, size_t *length, bool *indef)
Decodes a BER-encoded length value.
Definition encoding.c:257
QSC_EXPORT_API size_t qsc_encoding_ber_encode_length(size_t length, uint8_t *buffer, size_t buflen)
Encodes a length value into BER format.
Definition encoding.c:510
QSC_EXPORT_API bool qsc_encoding_hex_encode(const uint8_t *input, size_t inplen, char *output, size_t otplen)
Encodes binary data into a hexadecimal string.
Definition encoding.c:1061
QSC_EXPORT_API void encoding_ber_free_element(qsc_encoding_ber_element *element)
Free a BER element from the array.
Definition encoding.c:643
QSC_EXPORT_API bool qsc_encoding_base64_decode(uint8_t *output, size_t otplen, const char *input, size_t inlen)
Decodes a Base64 string to a byte array.
Definition encoding.c:667
QSC_EXPORT_API bool qsc_encoding_base64_is_valid_char(char value)
Tests if an encoded character is a valid Base64 encoding.
Definition encoding.c:836
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)
Decodes an ASN.1 tag from BER-encoded data.
Definition encoding.c:319
QSC_EXPORT_API size_t qsc_encoding_base64_encoded_size(size_t length)
Gets the expected size of a character array required for Base64 encoding.
Definition encoding.c:817
qsc_encoding_ber_asn1_tag_t
Enumeration of the standard Universal ASN.1 tag numbers used in BER.
Definition encoding.h:164
@ BER_ASN1_UTCTIME
Definition encoding.h:187
@ BER_ASN1_SEQUENCE
Definition encoding.h:180
@ BER_ASN1_INTEGER
Definition encoding.h:167
@ BER_ASN1_GENERAL_STRING
Definition encoding.h:191
@ BER_ASN1_CHARACTER_STRING
Definition encoding.h:193
@ BER_ASN1_OBJECT_DESCRIPTOR
Definition encoding.h:172
@ BER_ASN1_VIDEOTEX_STRING
Definition encoding.h:185
@ BER_ASN1_UTF8_STRING
Definition encoding.h:177
@ BER_ASN1_PRINTABLE_STRING
Definition encoding.h:183
@ BER_ASN1_BIT_STRING
Definition encoding.h:168
@ BER_ASN1_T61_STRING
Definition encoding.h:184
@ BER_ASN1_NULL
Definition encoding.h:170
@ BER_ASN1_EOC
Definition encoding.h:165
@ BER_ASN1_OCTET_STRING
Definition encoding.h:169
@ BER_ASN1_BMP_STRING
Definition encoding.h:194
@ BER_ASN1_GENERALIZEDTIME
Definition encoding.h:188
@ BER_ASN1_OBJECT_IDENTIFIER
Definition encoding.h:171
@ BER_ASN1_VISIBLE_STRING
Definition encoding.h:190
@ BER_ASN1_EMBEDDED_PDV
Definition encoding.h:176
@ BER_ASN1_SET
Definition encoding.h:181
@ BER_ASN1_IA5_STRING
Definition encoding.h:186
@ BER_ASN1_UNIVERSAL_STRING
Definition encoding.h:192
@ BER_ASN1_REAL
Definition encoding.h:174
@ BER_ASN1_ENUMERATED
Definition encoding.h:175
@ BER_ASN1_GRAPHIC_STRING
Definition encoding.h:189
@ BER_ASN1_NUMERIC_STRING
Definition encoding.h:182
@ BER_ASN1_BOOLEAN
Definition encoding.h:166
@ BER_ASN1_RELATIVE_OID
Definition encoding.h:178
@ BER_ASN1_EXTERNAL
Definition encoding.h:173
QSC_EXPORT_API void qsc_encoding_base64_encode(char *output, size_t otplen, const uint8_t *input, size_t inplen)
Encodes a byte array to a Base64 string.
Definition encoding.c:772
QSC_EXPORT_API bool qsc_encoding_hex_decode(const char *input, size_t inplen, uint8_t *output, size_t otplen, size_t *declen)
Decodes a hexadecimal string into binary data.
Definition encoding.c:988
QSC_EXPORT_API size_t qsc_encoding_ber_encode_tag(uint8_t tagclass, bool construct, uint32_t tagnum, uint8_t *buffer, size_t buflen)
Encodes an ASN.1 tag into BER format.
Definition encoding.c:570
QSC_EXPORT_API qsc_encoding_ber_element * qsc_encoding_der_decode_element(const uint8_t *buffer, size_t buflen, size_t *consumed)
Decodes an ASN.1 element encoded in DER format.
Definition encoding.c:864
QSC_EXPORT_API bool qsc_encoding_pem_decode(const char *input, uint8_t *output, size_t otplen, size_t *declen)
Decodes a PEM-formatted string into binary data.
Definition encoding.c:1089
Represents a BER (Basic Encoding Rules) element.
Definition encoding.h:213
size_t ccount
Definition encoding.h:221
uint8_t tagclass
Definition encoding.h:214
uint32_t tagnumber
Definition encoding.h:216
bool constructed
Definition encoding.h:215
uint8_t * value
Definition encoding.h:219
struct qsc_encoding_ber_element ** children
Definition encoding.h:220
bool indefinite
Definition encoding.h:217
size_t length
Definition encoding.h:218