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,10 @@
file(GLOB src ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c)
set(inc ${CMAKE_CURRENT_SOURCE_DIR}/inc/)
set(src_common ${src_common} ${src} PARENT_SCOPE)
set(inc_common ${inc_common} ${inc} PARENT_SCOPE)
if( ${CONFIG_IOT_TEST} STREQUAL "ON")
file(GLOB src_unit_test ${CMAKE_CURRENT_SOURCE_DIR}/test/*.cc)
set(src_test ${src_test} ${src_unit_test} PARENT_SCOPE)
endif()

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_

View File

@@ -0,0 +1,240 @@
/**
* @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.c
* @brief base64 encdoe & decode. Reference mbedtls/base64.c.
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-05-27
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-05-27 <td>1.0 <td>fancyxu <td>first commit
* </table>
*/
#include "utils_base64.h"
static const unsigned char base64_enc_map[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
static const unsigned char base64_dec_map[128] = {
127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 62,
127, 127, 127, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 127, 127, 127, 64, 127, 127, 127, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 127, 127, 127, 127, 127, 127, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 127, 127, 127, 127, 127};
#define BASE64_SIZE_T_MAX ((size_t)-1) /* SIZE_T_MAX is not standard */
/**
* @brief Check if src buf and dst buf.
*
* @param[in] dst destination buffer
* @param[in] dlen size of the destination buffer
* @param[in] src source buffer
* @param[in] slen amount of data to be encoded
* @return 0 for valid, -1 for invalid
*/
static int _utils_base64_check_valid(unsigned char *dst, size_t dlen, const unsigned char *src, size_t slen)
{
size_t i, n;
uint32_t j, x;
/* First pass: check for validity and get output length */
for (i = n = j = 0; i < slen; i++) {
/* Skip spaces before checking for EOL */
x = 0;
while (i < slen && src[i] == ' ') {
++i;
++x;
}
/* Spaces at end of buffer are OK */
if (i == slen)
break;
if ((slen - i) >= 2 && src[i] == '\r' && src[i + 1] == '\n')
continue;
if (src[i] == '\n')
continue;
/* Space inside a line is an error */
if (x != 0)
return -1;
if (src[i] == '=' && ++j > 2)
return -1;
if (src[i] > 127 || base64_dec_map[src[i]] == 127)
return -1;
if (base64_dec_map[src[i]] < 64 && j != 0)
return -1;
n++;
}
// if no useful data, means error
if (n == 0) {
return -1;
}
/* The following expression is to calculate the following formula without
* risk of integer overflow in n:
* n = ( ( n * 6 ) + 7 ) >> 3;
*/
n = (6 * (n >> 3)) + ((6 * (n & 0x7) + 7) >> 3);
n -= j;
if (!dst || dlen < n) {
return -1;
}
return 0;
}
/**
* @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)
{
size_t i, n;
unsigned char *p;
if (slen == 0) {
*olen = 0;
return 0;
}
n = slen / 3 + (slen % 3 != 0);
if (n > (BASE64_SIZE_T_MAX - 1) / 4) {
*olen = BASE64_SIZE_T_MAX;
return -1;
}
n *= 4;
if ((dlen < n + 1) || (NULL == dst)) {
*olen = n + 1;
return -1;
}
n = (slen / 3) * 3;
int C1, C2, C3;
for (i = 0, p = dst; i < n; i += 3) {
C1 = *src++;
C2 = *src++;
C3 = *src++;
*p++ = base64_enc_map[(C1 >> 2) & 0x3F];
*p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
*p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
*p++ = base64_enc_map[C3 & 0x3F];
}
if (i < slen) {
C1 = *src++;
C2 = ((i + 1) < slen) ? *src++ : 0;
*p++ = base64_enc_map[(C1 >> 2) & 0x3F];
*p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
if ((i + 1) < slen)
*p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
else
*p++ = '=';
*p++ = '=';
}
*olen = p - dst;
*p = 0;
return 0;
}
/**
* @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)
{
size_t i, n;
uint32_t j, x;
unsigned char *p;
/* First pass: check for validity and get output length */
if (_utils_base64_check_valid(dst, dlen, src, slen)) {
*olen = 0;
return -1;
}
for (i = slen, j = 3, n = x = 0, p = dst; i > 0; i--, src++) {
if (*src == '\r' || *src == '\n' || *src == ' ')
continue;
j -= (base64_dec_map[*src] == 64);
x = (x << 6) | (base64_dec_map[*src] & 0x3F);
if (++n == 4) {
n = 0;
if (j > 0)
*p++ = (unsigned char)(x >> 16);
if (j > 1)
*p++ = (unsigned char)(x >> 8);
if (j > 2)
*p++ = (unsigned char)(x);
}
}
*olen = p - dst;
return 0;
}

View File

@@ -0,0 +1,145 @@
/**
* @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.c
* @brief Support 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
* <tr><td>2021-07-08 <td>1.1 <td>fancyxu <td>fix code standard of IotSha1Context
* </table>
*/
#include "utils_hmac.h"
/**
* @brief inner/outer padding size
*
*/
#define KEY_IO_PAD_SIZE 64
/**
* @brief sha1 digest size
*
*/
#define SHA1_DIGEST_SIZE 20
/**
* @brief Binary half byte to hex char
*
* @param[in] hb half byte
* @return hex char(0~f)
*/
static char _hb2hex(uint8_t hb)
{
hb = hb & 0xF;
return (char)(hb < 10 ? '0' + hb : hb - 10 + 'a');
}
static int _utils_hmac_sha1_process(const char *msg, int msg_len, const uint8_t *key, int key_len, unsigned char *out)
{
IotSha1Context context;
unsigned char k_ipad[KEY_IO_PAD_SIZE]; /* inner padding - key XORd with ipad */
unsigned char k_opad[KEY_IO_PAD_SIZE]; /* outer padding - key XORd with opad */
int i;
/* start out by storing key in pads */
memset(k_ipad, 0, sizeof(k_ipad));
memset(k_opad, 0, sizeof(k_opad));
memcpy(k_ipad, key, key_len);
memcpy(k_opad, key, key_len);
/* XOR key with ipad and opad values */
for (i = 0; i < KEY_IO_PAD_SIZE; i++) {
k_ipad[i] ^= 0x36;
k_opad[i] ^= 0x5c;
}
/* perform inner SHA */
utils_sha1_init(&context); /* init context for 1st pass */
utils_sha1_starts(&context); /* setup context for 1st pass */
utils_sha1_update(&context, k_ipad, KEY_IO_PAD_SIZE); /* start with inner pad */
utils_sha1_update(&context, (unsigned char *)msg, msg_len); /* then text of datagram */
utils_sha1_finish(&context, out); /* finish up 1st pass */
/* perform outer SHA */
utils_sha1_init(&context); /* init context for 2nd pass */
utils_sha1_starts(&context); /* setup context for 2nd pass */
utils_sha1_update(&context, k_opad, KEY_IO_PAD_SIZE); /* start with outer pad */
utils_sha1_update(&context, out, SHA1_DIGEST_SIZE); /* then results of 1st hash */
utils_sha1_finish(&context, out); /* finish up 2nd pass */
return 0;
}
/**
* @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)
{
unsigned char out[SHA1_DIGEST_SIZE] = {0};
if (!msg || !digest || !key) {
return -1;
}
if (key_len > KEY_IO_PAD_SIZE) {
return -1;
}
_utils_hmac_sha1_process(msg, msg_len, key, key_len, out);
for (int i = 0; i < SHA1_DIGEST_SIZE; ++i) {
digest[i * 2] = _hb2hex(out[i] >> 4);
digest[i * 2 + 1] = _hb2hex(out[i]);
}
return 0;
}
/**
* @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)
{
unsigned char out[SHA1_DIGEST_SIZE] = {0};
if ((NULL == msg) || (NULL == digest) || (NULL == key)) {
return -1;
}
if (key_len > KEY_IO_PAD_SIZE) {
return -1;
}
_utils_hmac_sha1_process(msg, msg_len, key, key_len, out);
memcpy(digest, out, SHA1_DIGEST_SIZE);
return 0;
}

View File

@@ -0,0 +1,409 @@
/**
* @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.c
* @brief
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-10-27
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-10-27 <td>1.0 <td>fancyxu <td>first commit
* </table>
*/
#include "utils_md5.h"
/**
* @brief Binary half byte to hex char
*
* @param[in] hb half byte
* @return hex char(0~f)
*/
static char _hb2hex(uint8_t hb)
{
hb = hb & 0xF;
return (char)(hb < 10 ? '0' + hb : hb - 10 + 'a');
}
/**
* @brief Lower md5sum.
*
* @param[out] md5_lower md5 sum lower
* @param[in] md5 md5 sum
*/
static void _lower(char md5_lower[33], const char md5[33])
{
int i = 0;
for (i = 0; i < 32; i++) {
md5_lower[i] = md5[i];
if (md5[i] >= 'A' && md5[i] <= 'F') {
md5_lower[i] += ('a' - 'A');
}
}
md5_lower[32] = '\0';
}
#ifdef AUTH_WITH_NO_TLS
/**
* @brief 32-bit integer manipulation macros (little endian)
*
*/
#define IOT_MD5_GET_UINT32_LE(n, b, i) \
{ \
(n) = ((uint32_t)(b)[(i)]) | ((uint32_t)(b)[(i) + 1] << 8) | ((uint32_t)(b)[(i) + 2] << 16) | \
((uint32_t)(b)[(i) + 3] << 24); \
}
#define IOT_MD5_PUT_UINT32_LE(n, b, i) \
{ \
(b)[(i)] = (uint8_t)(((n)) & 0xFF); \
(b)[(i) + 1] = (uint8_t)(((n) >> 8) & 0xFF); \
(b)[(i) + 2] = (uint8_t)(((n) >> 16) & 0xFF); \
(b)[(i) + 3] = (uint8_t)(((n) >> 24) & 0xFF); \
}
/**
* @brief Padding for md5.
*
*/
static const uint8_t sg_iot_md5_padding[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
/**
* @brief Initialize md5 context.
*
* @param[in,out] ctx MD5 context
*/
static void _utils_md5_init(IotMd5Context *ctx)
{
memset(ctx, 0, sizeof(IotMd5Context));
}
/**
* @brief Start md5 calculate.
*
* @param[in,out] ctx MD5 context
*/
static void _utils_md5_start(IotMd5Context *ctx)
{
ctx->total[0] = 0;
ctx->total[1] = 0;
ctx->state[0] = 0x67452301;
ctx->state[1] = 0xEFCDAB89;
ctx->state[2] = 0x98BADCFE;
ctx->state[3] = 0x10325476;
}
/**
* @brief Calculate md5.
*
* @param[in,out] ctx MD5 context
* @param[in] data data to calculate
*/
static void _utils_md5_process(IotMd5Context *ctx, const uint8_t data[64])
{
uint32_t X[16], A, B, C, D;
IOT_MD5_GET_UINT32_LE(X[0], data, 0);
IOT_MD5_GET_UINT32_LE(X[1], data, 4);
IOT_MD5_GET_UINT32_LE(X[2], data, 8);
IOT_MD5_GET_UINT32_LE(X[3], data, 12);
IOT_MD5_GET_UINT32_LE(X[4], data, 16);
IOT_MD5_GET_UINT32_LE(X[5], data, 20);
IOT_MD5_GET_UINT32_LE(X[6], data, 24);
IOT_MD5_GET_UINT32_LE(X[7], data, 28);
IOT_MD5_GET_UINT32_LE(X[8], data, 32);
IOT_MD5_GET_UINT32_LE(X[9], data, 36);
IOT_MD5_GET_UINT32_LE(X[10], data, 40);
IOT_MD5_GET_UINT32_LE(X[11], data, 44);
IOT_MD5_GET_UINT32_LE(X[12], data, 48);
IOT_MD5_GET_UINT32_LE(X[13], data, 52);
IOT_MD5_GET_UINT32_LE(X[14], data, 56);
IOT_MD5_GET_UINT32_LE(X[15], data, 60);
#define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
#define P(a, b, c, d, k, s, t) \
{ \
a += F(b, c, d) + X[k] + t; \
a = S(a, s) + b; \
}
A = ctx->state[0];
B = ctx->state[1];
C = ctx->state[2];
D = ctx->state[3];
#define F(x, y, z) (z ^ (x & (y ^ z)))
P(A, B, C, D, 0, 7, 0xD76AA478);
P(D, A, B, C, 1, 12, 0xE8C7B756);
P(C, D, A, B, 2, 17, 0x242070DB);
P(B, C, D, A, 3, 22, 0xC1BDCEEE);
P(A, B, C, D, 4, 7, 0xF57C0FAF);
P(D, A, B, C, 5, 12, 0x4787C62A);
P(C, D, A, B, 6, 17, 0xA8304613);
P(B, C, D, A, 7, 22, 0xFD469501);
P(A, B, C, D, 8, 7, 0x698098D8);
P(D, A, B, C, 9, 12, 0x8B44F7AF);
P(C, D, A, B, 10, 17, 0xFFFF5BB1);
P(B, C, D, A, 11, 22, 0x895CD7BE);
P(A, B, C, D, 12, 7, 0x6B901122);
P(D, A, B, C, 13, 12, 0xFD987193);
P(C, D, A, B, 14, 17, 0xA679438E);
P(B, C, D, A, 15, 22, 0x49B40821);
#undef F
#define F(x, y, z) (y ^ (z & (x ^ y)))
P(A, B, C, D, 1, 5, 0xF61E2562);
P(D, A, B, C, 6, 9, 0xC040B340);
P(C, D, A, B, 11, 14, 0x265E5A51);
P(B, C, D, A, 0, 20, 0xE9B6C7AA);
P(A, B, C, D, 5, 5, 0xD62F105D);
P(D, A, B, C, 10, 9, 0x02441453);
P(C, D, A, B, 15, 14, 0xD8A1E681);
P(B, C, D, A, 4, 20, 0xE7D3FBC8);
P(A, B, C, D, 9, 5, 0x21E1CDE6);
P(D, A, B, C, 14, 9, 0xC33707D6);
P(C, D, A, B, 3, 14, 0xF4D50D87);
P(B, C, D, A, 8, 20, 0x455A14ED);
P(A, B, C, D, 13, 5, 0xA9E3E905);
P(D, A, B, C, 2, 9, 0xFCEFA3F8);
P(C, D, A, B, 7, 14, 0x676F02D9);
P(B, C, D, A, 12, 20, 0x8D2A4C8A);
#undef F
#define F(x, y, z) (x ^ y ^ z)
P(A, B, C, D, 5, 4, 0xFFFA3942);
P(D, A, B, C, 8, 11, 0x8771F681);
P(C, D, A, B, 11, 16, 0x6D9D6122);
P(B, C, D, A, 14, 23, 0xFDE5380C);
P(A, B, C, D, 1, 4, 0xA4BEEA44);
P(D, A, B, C, 4, 11, 0x4BDECFA9);
P(C, D, A, B, 7, 16, 0xF6BB4B60);
P(B, C, D, A, 10, 23, 0xBEBFBC70);
P(A, B, C, D, 13, 4, 0x289B7EC6);
P(D, A, B, C, 0, 11, 0xEAA127FA);
P(C, D, A, B, 3, 16, 0xD4EF3085);
P(B, C, D, A, 6, 23, 0x04881D05);
P(A, B, C, D, 9, 4, 0xD9D4D039);
P(D, A, B, C, 12, 11, 0xE6DB99E5);
P(C, D, A, B, 15, 16, 0x1FA27CF8);
P(B, C, D, A, 2, 23, 0xC4AC5665);
#undef F
#define F(x, y, z) (y ^ (x | ~z))
P(A, B, C, D, 0, 6, 0xF4292244);
P(D, A, B, C, 7, 10, 0x432AFF97);
P(C, D, A, B, 14, 15, 0xAB9423A7);
P(B, C, D, A, 5, 21, 0xFC93A039);
P(A, B, C, D, 12, 6, 0x655B59C3);
P(D, A, B, C, 3, 10, 0x8F0CCC92);
P(C, D, A, B, 10, 15, 0xFFEFF47D);
P(B, C, D, A, 1, 21, 0x85845DD1);
P(A, B, C, D, 8, 6, 0x6FA87E4F);
P(D, A, B, C, 15, 10, 0xFE2CE6E0);
P(C, D, A, B, 6, 15, 0xA3014314);
P(B, C, D, A, 13, 21, 0x4E0811A1);
P(A, B, C, D, 4, 6, 0xF7537E82);
P(D, A, B, C, 11, 10, 0xBD3AF235);
P(C, D, A, B, 2, 15, 0x2AD7D2BB);
P(B, C, D, A, 9, 21, 0xEB86D391);
#undef F
ctx->state[0] += A;
ctx->state[1] += B;
ctx->state[2] += C;
ctx->state[3] += D;
}
/**
* @brief Reset MD5 context.
*
* @param[in,out] ctx MD5 context
*/
void utils_md5_reset(IotMd5Context *ctx)
{
memset(ctx, 0, sizeof(IotMd5Context));
_utils_md5_init(ctx);
_utils_md5_start(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)
{
size_t fill;
uint32_t left;
if (ilen == 0) {
return;
}
left = ctx->total[0] & 0x3F;
fill = 64 - left;
ctx->total[0] += (uint32_t)ilen;
ctx->total[0] &= 0xFFFFFFFF;
if (ctx->total[0] < (uint32_t)ilen) {
ctx->total[1]++;
}
if (left && ilen >= fill) {
memcpy((void *)(ctx->buffer + left), input, fill);
_utils_md5_process(ctx, ctx->buffer);
input += fill;
ilen -= fill;
left = 0;
}
while (ilen >= 64) {
_utils_md5_process(ctx, input);
input += 64;
ilen -= 64;
}
if (ilen > 0) {
memcpy((void *)(ctx->buffer + left), input, ilen);
}
}
/**
* @brief Finish MD5 calculation, result will store in md5sum.
*
* @param[in,out] ctx MD5 context
*/
void utils_md5_finish(IotMd5Context *ctx)
{
int i;
uint32_t last, padn;
uint32_t high, low;
uint8_t msglen[8];
uint8_t output_tmp[16];
high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
low = (ctx->total[0] << 3);
IOT_MD5_PUT_UINT32_LE(low, msglen, 0);
IOT_MD5_PUT_UINT32_LE(high, msglen, 4);
last = ctx->total[0] & 0x3F;
padn = (last < 56) ? (56 - last) : (120 - last);
utils_md5_update(ctx, sg_iot_md5_padding, padn);
utils_md5_update(ctx, msglen, 8);
IOT_MD5_PUT_UINT32_LE(ctx->state[0], output_tmp, 0);
IOT_MD5_PUT_UINT32_LE(ctx->state[1], output_tmp, 4);
IOT_MD5_PUT_UINT32_LE(ctx->state[2], output_tmp, 8);
IOT_MD5_PUT_UINT32_LE(ctx->state[3], output_tmp, 12);
for (i = 0; i < 16; ++i) {
ctx->md5sum[i * 2] = _hb2hex(output_tmp[i] >> 4);
ctx->md5sum[i * 2 + 1] = _hb2hex(output_tmp[i]);
}
ctx->md5sum[32] = '\0';
}
/**
* @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])
{
char md5sum_lower[33];
_lower(md5sum_lower, md5sum);
return strncmp(ctx->md5sum, md5sum_lower, 32);
}
#else
/**
* @brief Reset MD5 context.
*
* @param[in,out] ctx MD5 context
*/
void utils_md5_reset(IotMd5Context *ctx)
{
memset(ctx, 0, sizeof(IotMd5Context));
mbedtls_md5_init(&ctx->ctx);
mbedtls_md5_starts(&ctx->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)
{
mbedtls_md5_update(&ctx->ctx, input, ilen);
}
/**
* @brief Finish MD5 calculation, result will store in md5sum.
*
* @param[in,out] ctx MD5 context
*/
void utils_md5_finish(IotMd5Context *ctx)
{
int i;
uint8_t output_tmp[16];
mbedtls_md5_finish(&ctx->ctx, output_tmp);
for (i = 0; i < 16; ++i) {
ctx->md5sum[i * 2] = _hb2hex(output_tmp[i] >> 4);
ctx->md5sum[i * 2 + 1] = _hb2hex(output_tmp[i]);
}
ctx->md5sum[32] = '\0';
}
/**
* @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])
{
char md5sum_lower[33];
_lower(md5sum_lower, md5sum);
return strncmp(ctx->md5sum, md5sum_lower, 32);
}
#endif

View File

@@ -0,0 +1,408 @@
/**
* @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.c
* @brief SHA-1 operation, reference mbedtls
* @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>
*/
#include "utils_sha1.h"
#ifndef IOT_SHA1_GET_UINT32_BE
/**
* @brief Get 32-bit integer manipulation macros (big endian)
*
*/
#define IOT_SHA1_GET_UINT32_BE(n, b, i) \
{ \
(n) = ((uint32_t)(b)[(i)] << 24) | ((uint32_t)(b)[(i) + 1] << 16) | ((uint32_t)(b)[(i) + 2] << 8) | \
((uint32_t)(b)[(i) + 3]); \
}
#endif
#ifndef IOT_SHA1_PUT_UINT32_BE
/**
* @brief Put 32-bit integer manipulation macros (big endian)
*
*/
#define IOT_SHA1_PUT_UINT32_BE(n, b, i) \
{ \
(b)[(i)] = (unsigned char)((n) >> 24); \
(b)[(i) + 1] = (unsigned char)((n) >> 16); \
(b)[(i) + 2] = (unsigned char)((n) >> 8); \
(b)[(i) + 3] = (unsigned char)((n)); \
}
#endif
/**
* @brief Binary half byte to hex char
*
* @param[in] hb half byte
* @return hex char(0~f)
*/
static char _hb2hex(uint8_t hb)
{
hb = hb & 0xF;
return (char)(hb < 10 ? '0' + hb : hb - 10 + 'a');
}
/**
* @brief Implementation that should never be optimized out by the compiler.
*
* @param[in] v pointer to ctx
* @param[in] n sizeof ctx
*/
static void utils_sha1_zeroize(void *v, size_t n)
{
volatile unsigned char *p = v;
while (n--) {
*p++ = 0;
}
}
/**
* @brief Initialize SHA-1 context.
*
* @param[in,out] ctx SHA-1 context to be initialized
*/
void utils_sha1_init(IotSha1Context *ctx)
{
memset(ctx, 0, sizeof(IotSha1Context));
}
/**
* @brief Clear SHA-1 context.
*
* @param[in,out] ctx SHA-1 context to be cleared
*/
void utils_sha1_free(IotSha1Context *ctx)
{
if (!ctx) {
return;
}
utils_sha1_zeroize(ctx, sizeof(IotSha1Context));
}
/**
* @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)
{
*dst = *src;
}
/**
* @brief SHA-1 context setup
*
* @param[in,out] ctx context to be initialized
*/
void utils_sha1_starts(IotSha1Context *ctx)
{
ctx->total[0] = 0;
ctx->total[1] = 0;
ctx->state[0] = 0x67452301;
ctx->state[1] = 0xEFCDAB89;
ctx->state[2] = 0x98BADCFE;
ctx->state[3] = 0x10325476;
ctx->state[4] = 0xC3D2E1F0;
}
/**
* @brief SHA-1 process
*
* @param[in,out] ctx pointer to ctx
* @param[in] data data to be calculated
*/
void utils_sha1_process(IotSha1Context *ctx, const unsigned char data[64])
{
uint32_t temp, W[16], A, B, C, D, E;
IOT_SHA1_GET_UINT32_BE(W[0], data, 0);
IOT_SHA1_GET_UINT32_BE(W[1], data, 4);
IOT_SHA1_GET_UINT32_BE(W[2], data, 8);
IOT_SHA1_GET_UINT32_BE(W[3], data, 12);
IOT_SHA1_GET_UINT32_BE(W[4], data, 16);
IOT_SHA1_GET_UINT32_BE(W[5], data, 20);
IOT_SHA1_GET_UINT32_BE(W[6], data, 24);
IOT_SHA1_GET_UINT32_BE(W[7], data, 28);
IOT_SHA1_GET_UINT32_BE(W[8], data, 32);
IOT_SHA1_GET_UINT32_BE(W[9], data, 36);
IOT_SHA1_GET_UINT32_BE(W[10], data, 40);
IOT_SHA1_GET_UINT32_BE(W[11], data, 44);
IOT_SHA1_GET_UINT32_BE(W[12], data, 48);
IOT_SHA1_GET_UINT32_BE(W[13], data, 52);
IOT_SHA1_GET_UINT32_BE(W[14], data, 56);
IOT_SHA1_GET_UINT32_BE(W[15], data, 60);
#define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
#define R(t) \
(temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ W[(t - 14) & 0x0F] ^ W[t & 0x0F], (W[t & 0x0F] = S(temp, 1)))
#define P(a, b, c, d, e, x) \
{ \
e += S(a, 5) + F(b, c, d) + K + x; \
b = S(b, 30); \
}
A = ctx->state[0];
B = ctx->state[1];
C = ctx->state[2];
D = ctx->state[3];
E = ctx->state[4];
#define F(x, y, z) (z ^ (x & (y ^ z)))
#define K 0x5A827999
P(A, B, C, D, E, W[0]);
P(E, A, B, C, D, W[1]);
P(D, E, A, B, C, W[2]);
P(C, D, E, A, B, W[3]);
P(B, C, D, E, A, W[4]);
P(A, B, C, D, E, W[5]);
P(E, A, B, C, D, W[6]);
P(D, E, A, B, C, W[7]);
P(C, D, E, A, B, W[8]);
P(B, C, D, E, A, W[9]);
P(A, B, C, D, E, W[10]);
P(E, A, B, C, D, W[11]);
P(D, E, A, B, C, W[12]);
P(C, D, E, A, B, W[13]);
P(B, C, D, E, A, W[14]);
P(A, B, C, D, E, W[15]);
P(E, A, B, C, D, R(16));
P(D, E, A, B, C, R(17));
P(C, D, E, A, B, R(18));
P(B, C, D, E, A, R(19));
#undef K
#undef F
#define F(x, y, z) (x ^ y ^ z)
#define K 0x6ED9EBA1
P(A, B, C, D, E, R(20));
P(E, A, B, C, D, R(21));
P(D, E, A, B, C, R(22));
P(C, D, E, A, B, R(23));
P(B, C, D, E, A, R(24));
P(A, B, C, D, E, R(25));
P(E, A, B, C, D, R(26));
P(D, E, A, B, C, R(27));
P(C, D, E, A, B, R(28));
P(B, C, D, E, A, R(29));
P(A, B, C, D, E, R(30));
P(E, A, B, C, D, R(31));
P(D, E, A, B, C, R(32));
P(C, D, E, A, B, R(33));
P(B, C, D, E, A, R(34));
P(A, B, C, D, E, R(35));
P(E, A, B, C, D, R(36));
P(D, E, A, B, C, R(37));
P(C, D, E, A, B, R(38));
P(B, C, D, E, A, R(39));
#undef K
#undef F
#define F(x, y, z) ((x & y) | (z & (x | y)))
#define K 0x8F1BBCDC
P(A, B, C, D, E, R(40));
P(E, A, B, C, D, R(41));
P(D, E, A, B, C, R(42));
P(C, D, E, A, B, R(43));
P(B, C, D, E, A, R(44));
P(A, B, C, D, E, R(45));
P(E, A, B, C, D, R(46));
P(D, E, A, B, C, R(47));
P(C, D, E, A, B, R(48));
P(B, C, D, E, A, R(49));
P(A, B, C, D, E, R(50));
P(E, A, B, C, D, R(51));
P(D, E, A, B, C, R(52));
P(C, D, E, A, B, R(53));
P(B, C, D, E, A, R(54));
P(A, B, C, D, E, R(55));
P(E, A, B, C, D, R(56));
P(D, E, A, B, C, R(57));
P(C, D, E, A, B, R(58));
P(B, C, D, E, A, R(59));
#undef K
#undef F
#define F(x, y, z) (x ^ y ^ z)
#define K 0xCA62C1D6
P(A, B, C, D, E, R(60));
P(E, A, B, C, D, R(61));
P(D, E, A, B, C, R(62));
P(C, D, E, A, B, R(63));
P(B, C, D, E, A, R(64));
P(A, B, C, D, E, R(65));
P(E, A, B, C, D, R(66));
P(D, E, A, B, C, R(67));
P(C, D, E, A, B, R(68));
P(B, C, D, E, A, R(69));
P(A, B, C, D, E, R(70));
P(E, A, B, C, D, R(71));
P(D, E, A, B, C, R(72));
P(C, D, E, A, B, R(73));
P(B, C, D, E, A, R(74));
P(A, B, C, D, E, R(75));
P(E, A, B, C, D, R(76));
P(D, E, A, B, C, R(77));
P(C, D, E, A, B, R(78));
P(B, C, D, E, A, R(79));
#undef K
#undef F
ctx->state[0] += A;
ctx->state[1] += B;
ctx->state[2] += C;
ctx->state[3] += D;
ctx->state[4] += E;
}
/**
* @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)
{
size_t fill;
uint32_t left;
if (ilen == 0) {
return;
}
left = ctx->total[0] & 0x3F;
fill = 64 - left;
ctx->total[0] += (uint32_t)ilen;
ctx->total[0] &= 0xFFFFFFFF;
if (ctx->total[0] < (uint32_t)ilen) {
ctx->total[1]++;
}
if (left && ilen >= fill) {
memcpy((void *)(ctx->buffer + left), input, fill);
utils_sha1_process(ctx, ctx->buffer);
input += fill;
ilen -= fill;
left = 0;
}
while (ilen >= 64) {
utils_sha1_process(ctx, input);
input += 64;
ilen -= 64;
}
if (ilen > 0) {
memcpy((void *)(ctx->buffer + left), input, ilen);
}
}
static const unsigned char iot_sha1_padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
/**
* @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])
{
uint32_t last, padn;
uint32_t high, low;
unsigned char msglen[8];
high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
low = (ctx->total[0] << 3);
IOT_SHA1_PUT_UINT32_BE(high, msglen, 0);
IOT_SHA1_PUT_UINT32_BE(low, msglen, 4);
last = ctx->total[0] & 0x3F;
padn = (last < 56) ? (56 - last) : (120 - last);
utils_sha1_update(ctx, iot_sha1_padding, padn);
utils_sha1_update(ctx, msglen, 8);
IOT_SHA1_PUT_UINT32_BE(ctx->state[0], output, 0);
IOT_SHA1_PUT_UINT32_BE(ctx->state[1], output, 4);
IOT_SHA1_PUT_UINT32_BE(ctx->state[2], output, 8);
IOT_SHA1_PUT_UINT32_BE(ctx->state[3], output, 12);
IOT_SHA1_PUT_UINT32_BE(ctx->state[4], output, 16);
}
/**
* @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])
{
IotSha1Context ctx;
utils_sha1_init(&ctx);
utils_sha1_starts(&ctx);
utils_sha1_update(&ctx, input, ilen);
utils_sha1_finish(&ctx, output);
utils_sha1_free(&ctx);
}
/**
* @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])
{
unsigned char out[20 + 1] = {0};
utils_sha1(input, ilen, out);
for (int i = 0; i < 20; ++i) {
output_hex[i * 2] = _hb2hex(out[i] >> 4);
output_hex[i * 2 + 1] = _hb2hex(out[i]);
}
}

View File

@@ -0,0 +1,8 @@
Language: Cpp
BasedOnStyle: Google
ColumnLimit: 120
DerivePointerAlignment: true
PointerAlignment: Left
SortIncludes: true
IncludeBlocks: Preserve
IndentPPDirectives: AfterHash

View File

@@ -0,0 +1,179 @@
/**
* @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 test_cryptology.cc
* @brief test cryptology
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-07-07
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-07-07 <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>
*/
#include <iostream>
#include <string>
#include "gtest/gtest.h"
#include "utils_base64.h"
#include "utils_hmac.h"
#include "utils_md5.h"
#include "utils_sha1.h"
namespace cryptology_unittest {
/**
* @brief Test base64.
*
*/
TEST(CryptologyTest, base64) {
const uint8_t base64_test_dec[64] = {0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD, 0xBF, 0x17, 0xD9, 0xA2, 0xC4,
0x17, 0x1A, 0x01, 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09, 0x0C, 0xB6,
0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13, 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA,
0x31, 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38, 0x00, 0x43, 0xE9, 0x54,
0x97, 0xAF, 0x50, 0x4B, 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97};
const uint8_t base64_test_enc[] =
"JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
"swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
size_t len;
uint8_t buffer[128];
ASSERT_EQ(utils_base64encode(buffer, sizeof(buffer), &len, base64_test_dec, sizeof(base64_test_dec)), 0);
ASSERT_EQ(memcmp(base64_test_enc, buffer, len), 0);
ASSERT_EQ(utils_base64decode(buffer, sizeof(buffer), &len, base64_test_enc, sizeof(base64_test_enc) - 1), 0);
ASSERT_EQ(memcmp(base64_test_dec, buffer, len), 0);
}
/**
* @brief Test hmac.
*
*/
TEST(CryptologyTest, hmac) {
/**
* @brief HMAC-SHA1
*
*/
const char test_buf[] = "Here is a test for hmac-sha1!";
const uint8_t key[] = "0123456789123456";
const char result[] = "614b650ffefff7862c1bc7fdd9de4e472a8184c4";
char buf[128] = {0};
ASSERT_EQ(utils_hmac_sha1(test_buf, strlen(test_buf), key, sizeof(key), buf), 0);
ASSERT_EQ(memcmp(buf, result, sizeof(result)), 0);
}
/**
* @brief Test sha1.
*
*/
TEST(CryptologyTest, sha1) {
/*
* FIPS-180-1 test vectors
*/
const uint8_t sha1_test_buf[3][57] = {{"abc"}, {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}, {""}};
const int sha1_test_buflen[3] = {3, 56, 1000};
const uint8_t sha1_test_sum[3][20] = {
{
0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D,
},
{
0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1,
},
{
0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F,
},
};
int i, j, buflen, rc = 0;
uint8_t buf[1024];
uint8_t sha1sum[20];
IotSha1Context ctx;
utils_sha1_init(&ctx);
for (i = 0; i < 3; i++) {
utils_sha1_starts(&ctx);
if (i == 2) {
memset(buf, 'a', buflen = 1000);
for (j = 0; j < 1000; j++) {
utils_sha1_update(&ctx, buf, buflen);
}
} else {
utils_sha1_update(&ctx, sha1_test_buf[i], sha1_test_buflen[i]);
}
utils_sha1_finish(&ctx, sha1sum);
ASSERT_EQ(memcmp(sha1sum, sha1_test_sum[i], 20), 0);
}
utils_sha1_free(&ctx);
utils_sha1(sha1_test_buf[0], 3, sha1sum);
ASSERT_EQ(memcmp(sha1sum, sha1_test_sum[0], 20), 0);
}
/**
* @brief Test md5.
*
*/
TEST(CryptologyTest, md5) {
/*
* RFC 1321 test vectors
*/
const uint8_t md5_test_buf[7][81] = {
{""},
{"a"},
{"abc"},
{"message digest"},
{"abcdefghijklmnopqrstuvwxyz"},
{"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"},
{"12345678901234567890123456789012345678901234567890123456789012345678901234567890"},
};
const size_t md5_test_buflen[7] = {0, 1, 3, 14, 26, 62, 80};
const char md5_test_sum[7][33] = {
{"D41D8CD98F00B204E9800998ECF8427E"}, {"0CC175B9C0F1B6A831C399E269772661"}, {"900150983CD24FB0D6963F7D28E17F72"},
{"F96B697D7CB7938D525A2F31AAF161D0"}, {"C3FCD3D76192E4007DFB496CCA67E13B"}, {"D174AB98D277D9F5A5611C2C9F419D9F"},
{"57EDF4A22BE3C955AC49DA2E2107B67A"},
};
/*
* Checkup routine
*/
IotMd5Context md5_ctx;
for (int i = 0; i < 7; i++) {
utils_md5_reset(&md5_ctx);
utils_md5_update(&md5_ctx, md5_test_buf[i], md5_test_buflen[i]);
utils_md5_finish(&md5_ctx);
ASSERT_EQ(utils_md5_compare(&md5_ctx, md5_test_sum[i]), 0);
}
}
} // namespace cryptology_unittest