add new qloud-c-sdk component

This commit is contained in:
mculover666
2022-03-25 10:06:56 +08:00
parent 565cd29e94
commit a3ac2e56d8
166 changed files with 35027 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
/**
* @copyright
*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright(C) 2018 - 2021 THL A29 Limited, a Tencent company.All rights reserved.
*
* Licensed under the MIT License(the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
* @file utils_base64.h
* @brief utils base64 header file
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-05-28
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-05-28 <td>1.0 <td>fancyxu <td>first commit
* </table>
*/
#ifndef IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_BASE64_H_
#define IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_BASE64_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
/**
* @brief Encode a buffer into base64 format.
*
* @param[out] dst destination buffer
* @param[in] dlen size of the destination buffer
* @param[out] olen number of bytes written
* @param[in] src source buffer
* @param[in] slen amount of data to be encoded
* @return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL.
* *olen is always updated to reflect the amount
* of data that has (or would have) been written.
* If that length cannot be represented, then no data is
* written to the buffer and *olen is set to the maximum
* length representable as a size_t.
*
* @note Call this function with dlen = 0 to obtain the
* required buffer size in *olen
*/
int utils_base64encode(unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen);
/**
* @brief Decode a base64-formatted buffer.
*
* @param[out] dst destination buffer (can be NULL for checking size)
* @param[in] dlen size of the destination buffer
* @param[out] olen number of bytes written
* @param[in] src source buffer
* @param[in] slen amount of data to be decoded
* @return 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or
* MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is
* not correct. *olen is always updated to reflect the amount
* of data that has (or would have) been written.
*
* @note Call this function with *dst = NULL or dlen = 0 to obtain
* the required buffer size in *olen
*/
int utils_base64decode(unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen);
#ifdef __cplusplus
}
#endif
#endif // IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_BASE64_H_

View File

@@ -0,0 +1,68 @@
/**
* @copyright
*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright(C) 2018 - 2021 THL A29 Limited, a Tencent company.All rights reserved.
*
* Licensed under the MIT License(the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
* @file utils_hmac.h
* @brief header file for utils_hmac
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-05-31
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-05-31 <td>1.0 <td>fancyxu <td>first commit
* </table>
*/
#ifndef IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_HMAC_H_
#define IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_HMAC_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
#include "utils_sha1.h"
/**
* @brief Get digest of hmac-sha1.
*
* @param[in] msg message to hmac-sha1
* @param[in] msg_len message len
* @param[in] key key using in hmac-sha1
* @param[in] key_len key len
* @param[out] digest digest to calculate
* @return 0 for success
*/
int utils_hmac_sha1(const char *msg, int msg_len, const uint8_t *key, int key_len, char *digest);
/**
* @brief Get digest hex of hmac-sha1.
*
* @param[in] msg message to hmac-sha1
* @param[in] msg_len message len
* @param[in] key key using in hmac-sha1
* @param[in] key_len key len
* @param[out] digest digest to calculate
* @return 0 for success
*/
int utils_hmac_sha1_hex(const char *msg, int msg_len, const uint8_t *key, int key_len, char *digest);
#ifdef __cplusplus
}
#endif
#endif // IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_HMAC_H_

View File

@@ -0,0 +1,94 @@
/**
* @copyright
*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright(C) 2018 - 2021 THL A29 Limited, a Tencent company.All rights reserved.
*
* Licensed under the MIT License(the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
* @file utils_md5.h
* @brief
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-10-20
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-10-20 <td>1.0 <td>fancyxu <td>first commit
* </table>
*/
#ifndef IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_MD5_H_
#define IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_MD5_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "qcloud_iot_config.h"
#ifdef AUTH_WITH_NO_TLS
typedef struct {
uint32_t total[2]; /**< number of bytes processed */
uint32_t state[4]; /**< intermediate digest state */
uint8_t buffer[64]; /**< data block being processed */
char md5sum[33]; /**< md5sum result in hex string */
} IotMd5Context;
#else
#include "mbedtls/md5.h"
typedef struct {
mbedtls_md5_context ctx;
char md5sum[33]; /**< md5sum result in hex string */
} IotMd5Context;
#endif
/**
* @brief Reset MD5 context.
*
* @param[in,out] ctx MD5 context
*/
void utils_md5_reset(IotMd5Context *ctx);
/**
* @brief MD5 update.
*
* @param[in,out] ctx MD5 context
* @param[in] input input data
* @param[in] ilen data length
*/
void utils_md5_update(IotMd5Context *ctx, const uint8_t *input, size_t ilen);
/**
* @brief Finish MD5 calculation, result will store in md5sum.
*
* @param[in,out] ctx MD5 context
*/
void utils_md5_finish(IotMd5Context *ctx);
/**
* @brief Compare md5sum with context.
*
* @param[in,out] ctx MD5 context
* @param[in] md5sum md5sum to compare
* @return 0 for the same
*/
int utils_md5_compare(IotMd5Context *ctx, const char md5sum[33]);
#ifdef __cplusplus
}
#endif
#endif // IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_MD5_H_

View File

@@ -0,0 +1,119 @@
/**
* @copyright
*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright(C) 2018 - 2021 THL A29 Limited, a Tencent company.All rights reserved.
*
* Licensed under the MIT License(the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
* @file utils_sha1.h
* @brief header file for utils-sha1
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-05-31
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-05-31 <td>1.0 <td>fancyxu <td>first commit
* <tr><td>2021-07-08 <td>1.1 <td>fancyxu <td>fix code standard of IotSha1Context
* </table>
*/
#ifndef IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_SHA1_H_
#define IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_SHA1_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/**
* @brief SHA-1 context structure.
*
*/
typedef struct {
uint32_t total[2]; /**< number of bytes processed */
uint32_t state[5]; /**< intermediate digest state */
unsigned char buffer[64]; /**< data block being processed */
} IotSha1Context;
/**
* @brief Initialize SHA-1 context.
*
* @param[in,out] ctx SHA-1 context to be initialized
*/
void utils_sha1_init(IotSha1Context *ctx);
/**
* @brief Clear SHA-1 context.
*
* @param[in,out] ctx SHA-1 context to be cleared
*/
void utils_sha1_free(IotSha1Context *ctx);
/**
* @brief Clone (the state of) a SHA-1 context.
*
* @param[out] dst The destination context
* @param[in] src The context to be cloned
*/
void utils_sha1_clone(IotSha1Context *dst, const IotSha1Context *src);
/**
* @brief SHA-1 context setup
*
* @param[in,out] ctx context to be initialized
*/
void utils_sha1_starts(IotSha1Context *ctx);
/**
* @brief SHA-1 process buffer.
*
* @param[in,out] ctx SHA-1 context
* @param[in] input buffer holding the data
* @param[in] ilen length of the input data
*/
void utils_sha1_update(IotSha1Context *ctx, const unsigned char *input, size_t ilen);
/**
* @brief SHA-1 final digest
*
* @param[in,out] ctx SHA-1 context
* @param[out] output SHA-1 checksum result
*/
void utils_sha1_finish(IotSha1Context *ctx, unsigned char output[20]);
/**
* @brief Output = SHA-1( input buffer )
*
* @param[in] input buffer holding the data
* @param[in] ilen length of the input data
* @param[out] output SHA-1 checksum result
*/
void utils_sha1(const unsigned char *input, size_t ilen, unsigned char output[20]);
/**
* @brief Output = SHA-1( input buffer )
*
* @param[in] input buffer holding the data
* @param[in] ilen length of the input data
* @param[out] output SHA-1 checksum result hex
*/
void utils_sha1_hex(const unsigned char *input, size_t ilen, unsigned char output_hex[40]);
#ifdef __cplusplus
}
#endif
#endif // IOT_HUB_DEVICE_C_SDK_COMMON_CRYPTOLOGY_INC_UTILS_SHA1_H_