1 /* 2 * This file is part of the openHiTLS project. 3 * 4 * openHiTLS is licensed under the Mulan PSL v2. 5 * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 * You may obtain a copy of Mulan PSL v2 at: 7 * 8 * http://license.coscl.org.cn/MulanPSL2 9 * 10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 * See the Mulan PSL v2 for more details. 14 */ 15 16 #ifndef BSL_BASE64_H 17 #define BSL_BASE64_H 18 19 #include <stdint.h> 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 #define HITLS_BASE64_CTX_BUF_LENGTH 80 26 #define HITLS_BASE64_CTX_LENGTH 48 27 /* Input length (len) divided by 3, rounded up, multiplied by 4, then add the number of newline characters,and 28 add the length of the context buffer */ 29 #define HITLS_BASE64_ENCODE_LENGTH(len) \ 30 ((((len) + 2) / 3 * 4) + ((len) / HITLS_BASE64_CTX_LENGTH + 1) * 2 + HITLS_BASE64_CTX_BUF_LENGTH) 31 #define HITLS_BASE64_DECODE_LENGTH(len) (((len) + 3) / 4 * 3 + HITLS_BASE64_CTX_BUF_LENGTH) 32 33 /* 34 * When writing, it makes all the data written on one line without a newline character at the end; 35 * When reading, it expects all data to be on one line (regardless of whether there is a trailing newline character) 36 */ 37 #define BSL_BASE64_FLAGS_NO_NEWLINE 0x01 38 39 typedef struct BASE64_ControlBlock BSL_Base64Ctx; 40 41 /** 42 * @ingroup bsl_base64 43 * @brief Encode the specified buffer into the base64 format. 44 * @par Description: The function converts the DER code to base64 format, 45 * In the case of the encoding is successful, The user needs to release the dstBuf memory after the dstBuf is 46 * used up. The user needs to allocate space dstBuf in advance. dstBufLen indicates the length of the allocate space. 47 * @attention None 48 * @param srcBuf [IN] Passed buff buffer. 49 * @param srcBufLen [IN] Input buff buffer length. 50 * @param dstBuf [OUT] Output buff buffer. 51 * @param dstBufLen [OUT] Number of encoded bytes excluding the terminator (a multiple of 4) 52 * @return Error code 53 */ 54 int32_t BSL_BASE64_Encode(const uint8_t *srcBuf, const uint32_t srcBufLen, char *dstBuf, uint32_t *dstBufLen); 55 56 /** 57 * @ingroup bsl_base64 58 * @brief Decode the specified buffer into the DER format. 59 * @par Description: This function converts the specified base64 format into the DER format, In the case of the 60 * decoding is successful, the user needs to release the dstBuf memory after the dstBuf is used up. 61 * @attention None 62 * @param srcBuf [IN] Passed buff buffer. 63 * @param srcBufLen [IN] Input buff buffer length. 64 * @param dstBufLen [OUT] Encoding length obtained after decoding. 65 * @param dstBuf [OUT] Encoding string obtained after decoding. 66 * @retval when success , return BSL_SUCCESS; Otherwise, return error code 67 */ 68 int32_t BSL_BASE64_Decode(const char *srcBuf, const uint32_t srcBufLen, uint8_t *dstBuf, uint32_t *dstBufLen); 69 70 /** 71 * @ingroup bsl_base64 72 * @brief Generate Stream Encoding Context. 73 * @par Description: generate BSL_Base64Ctx. 74 * @param ctx [IN] Base64 context 75 * @retval void 76 */ 77 BSL_Base64Ctx *BSL_BASE64_CtxNew(void); 78 79 /** 80 * @ingroup bsl_base64 81 * @brief Release the stream encoding context. 82 * @par Description: release BSL_Base64Ctx. 83 * @param ctx [IN] Base64 context 84 * @retval void 85 */ 86 void BSL_BASE64_CtxFree(BSL_Base64Ctx *ctx); 87 88 /** 89 * @ingroup bsl_base64 90 * @brief Clear stream encoding context. 91 * @par Description: clear BSL_Base64Ctx. 92 * @param ctx [IN] Base64 context 93 * @retval void 94 */ 95 void BSL_BASE64_CtxClear(BSL_Base64Ctx *ctx); 96 97 /** 98 * @ingroup bsl_base64 99 * @brief Initialize stream encoding. 100 * @par Description: initialize the context. 101 * @param ctx [IN] Base64 context 102 * @retval In the case of success, return BSL_SUCCESS; Otherwise, returned error code. 103 */ 104 int32_t BSL_BASE64_EncodeInit(BSL_Base64Ctx *ctx); 105 106 /** 107 * @ingroup bsl_base64 108 * @brief Encodes a specified buffer into the Base64 format. 109 * @par Description: If the length of the data to be encoded is less than one line or one block, 110 * the data is stored in encData of the context for the next input by user. Until one block is 111 * satisfied or we encountered the last line. 112 * @param srcBuf [IN] Passed buff buffer. 113 * @param srcBufLen [IN] Input buff buffer length. 114 * @param dstBuf [OUT] String obtained after encoding. 115 * @param dstBufLen [OUT] Length obtained after encoding. 116 * @retval In the case of success, return BSL_SUCCESS. Otherwise, returned error code. 117 */ 118 int32_t BSL_BASE64_EncodeUpdate(BSL_Base64Ctx *ctx, const uint8_t *srcBuf, uint32_t srcBufLen, 119 char *dstBuf, uint32_t *dstBufLen); 120 121 /** 122 * @ingroup bsl_base64 123 * @brief Encode the specified buffer into the Base64 format. 124 * @par Description: Encode the remaining characters stored in the context buffer. 125 * @param dstBufLen [OUT] Length obtained after encoding. 126 * @param dstBuf [OUT] String obtained after encoding. 127 * @retval In the case of success, return BSL_SUCCESS. Otherwise, returned error code. 128 */ 129 int32_t BSL_BASE64_EncodeFinal(BSL_Base64Ctx *ctx, char *dstBuf, uint32_t *dstBufLen); 130 131 /** 132 * @ingroup bsl_base64 133 * @brief Initialize stream decoding. 134 * @par Description: Initialize the context. 135 * @param ctx [IN] Base64 context 136 * @retval In the case of success, return BSL_SUCCESS. Otherwise, returned error code. 137 */ 138 int32_t BSL_BASE64_DecodeInit(BSL_Base64Ctx *ctx); 139 140 /** 141 * @ingroup bsl_base64 142 * @brief Decode the specified buffer into the DER format. 143 * @par Description: Block decoding is performed for each full block, 144 * Otherwise, the padding is less one block are placed in the context for decodeFinal processing. 145 * @param srcBuf [IN] Passed buff buffer. 146 * @param srcBufLen [IN] Input buff buffer length. 147 * @param dstBuf [OUT] String obtained after decoding. 148 * @param dstBufLen [OUT] Length obtained after decoding. 149 * @retval In the case of success, return BSL_SUCCESS; Otherwise, returned error code. 150 */ 151 int32_t BSL_BASE64_DecodeUpdate(BSL_Base64Ctx *ctx, const char *srcBuf, const uint32_t srcBufLen, 152 uint8_t *dstBuf, uint32_t *dstBufLen); 153 154 /** 155 * @ingroup bsl_base64 156 * @brief Decode the specified buffer into the DER format. 157 * @par Description: Decode the remaining characters stored in the context buffer. 158 * @param dstBufLen [OUT] Length obtained after decoding. 159 * @param dstBuf [OUT] String obtained after decoding. 160 * @retval In the case of success, return BSL_SUCCESS. Otherwise, returned error code. 161 */ 162 int32_t BSL_BASE64_DecodeFinal(BSL_Base64Ctx *ctx, uint8_t *dstBuf, uint32_t *dstBufLen); 163 164 /** 165 * @ingroup bsl_base64 166 * @brief Set the flag 167 * @par Description: sets the context flags 168 * @param ctx [IN] Input context. 169 * @param flags [IN] Flags to be set. 170 * @retval In the case of success, return BSL_SUCCESS; Otherwise, returned error code. 171 */ 172 int32_t BSL_BASE64_SetFlags(BSL_Base64Ctx *ctx, uint32_t flags); 173 174 #ifdef __cplusplus 175 } 176 #endif 177 178 #endif 179