feat(mbedtls): remove util algorithm and add them to esp8266 rom and mbedtls

This commit is contained in:
yuanjm
2020-03-02 14:46:45 +08:00
parent c4f549dbc1
commit 514bca61f4
60 changed files with 2192 additions and 2230 deletions

View File

@ -0,0 +1,138 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
#ifndef __CRC_H_
#define __CRC_H_
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/** \defgroup uart_apis, uart configuration and communication related apis
* @brief uart apis
*/
/** @addtogroup uart_apis
* @{
*/
/* Standard CRC8/16/32 algorithms. */
// CRC-8 x8+x2+x1+1 0x07
// CRC16-CCITT x16+x12+x5+1 1021 ISO HDLC, ITU X.25, V.34/V.41/V.42, PPP-FCS
// CRC32:
//G(x) = x32 +x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x1 + 1
//If your buf is not continuous, you can use the first result to be the second parameter.
/**
* @brief Crc32 value that is in little endian.
*
* @param uint32_t crc : init crc value, use 0 at the first use.
*
* @param uint8_t const *buf : buffer to start calculate crc.
*
* @param uint32_t len : buffer length in byte.
*
* @return None
*/
uint32_t crc32_le(uint32_t crc, uint8_t const *buf, uint32_t len);
/**
* @brief Crc32 value that is in big endian.
*
* @param uint32_t crc : init crc value, use 0 at the first use.
*
* @param uint8_t const *buf : buffer to start calculate crc.
*
* @param uint32_t len : buffer length in byte.
*
* @return None
*/
uint32_t crc32_be(uint32_t crc, uint8_t const *buf, uint32_t len);
/**
* @brief Crc16 value that is in little endian.
*
* @param uint16_t crc : init crc value, use 0 at the first use.
*
* @param uint8_t const *buf : buffer to start calculate crc.
*
* @param uint32_t len : buffer length in byte.
*
* @return None
*/
uint16_t crc16_le(uint16_t crc, uint8_t const *buf, uint32_t len);
/**
* @brief Crc16 value that is in big endian.
*
* @param uint16_t crc : init crc value, use 0 at the first use.
*
* @param uint8_t const *buf : buffer to start calculate crc.
*
* @param uint32_t len : buffer length in byte.
*
* @return None
*/
uint16_t crc16_be(uint16_t crc, uint8_t const *buf, uint32_t len);
/**
* @brief Crc8 value that is in little endian.
*
* @param uint8_t crc : init crc value, use 0 at the first use.
*
* @param uint8_t const *buf : buffer to start calculate crc.
*
* @param uint32_t len : buffer length in byte.
*
* @return None
*/
uint8_t crc8_le(uint8_t crc, uint8_t const *buf, uint32_t len);
/**
* @brief Crc8 value that is in big endian.
*
* @param uint32_t crc : init crc value, use 0 at the first use.
*
* @param uint8_t const *buf : buffer to start calculate crc.
*
* @param uint32_t len : buffer length in byte.
*
* @return None
*/
uint8_t crc8_be(uint8_t crc, uint8_t const *buf, uint32_t len);
/**
* @brief A crc8 algorithm used in efuse check.
*
* @param uint8_t const *p : Pointer to original data.
*
* @param uint32_t len : Data length in byte.
*
* @return uint8_t: Crc value.
*/
uint8_t esp_crc8(uint8_t const *p, uint32_t len);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,202 @@
// Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
#pragma once
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
uint32_t state[5];
uint32_t total[2];
uint8_t buffer[64];
} esp_sha1_t;
typedef struct {
uint32_t total[2];
uint32_t state[8];
uint8_t buffer[64];
int is224;
} esp_sha256_t;
typedef struct {
uint64_t total[2];
uint64_t state[8];
uint8_t buffer[128];
int is384;
} esp_sha512_t;
typedef esp_sha256_t esp_sha224_t;
typedef esp_sha512_t esp_sha384_t;
/**
* @brief initialize the SHA1 contex
*
* @param ctx SHA1 contex pointer
*
* @return 0 if success or fail
*/
int esp_sha1_init(esp_sha1_t *ctx);
/**
* @brief initialize the SHA224 contex
*
* @param ctx SHA224 contex pointer
*
* @return 0 if success or fail
*/
int esp_sha224_init(esp_sha224_t *ctx);
/**
* @brief initialize the SHA256 contex
*
* @param ctx SHA256 contex pointer
*
* @return 0 if success or fail
*/
int esp_sha256_init(esp_sha256_t *ctx);
/**
* @brief initialize the SHA384 contex
*
* @param ctx SHA384 contex pointer
*
* @return 0 if success or fail
*/
int esp_sha384_init(esp_sha384_t *ctx);
/**
* @brief initialize the SHA512 contex
*
* @param ctx SHA512 contex pointer
*
* @return 0 if success or fail
*/
int esp_sha512_init(esp_sha512_t *ctx);
/**
* @brief calculate input data for SHA1
*
* @param ctx SHA1 contex pointer
* @param src input data buffer pointer
* @param size input data bytes
*
* @return 0 if success or fail
*/
int esp_sha1_update(esp_sha1_t *ctx, const void *src, size_t size);
/**
* @brief calculate input data for SHA224
*
* @param ctx SHA224 contex pointer
* @param src input data buffer pointer
* @param size input data bytes
*
* @return 0 if success or fail
*/
int esp_sha224_update(esp_sha224_t *ctx, const void *src, size_t size);
/**
* @brief calculate input data for SHA256
*
* @param ctx SHA256 contex pointer
* @param src input data buffer pointer
* @param size input data bytes
*
* @return 0 if success or fail
*/
int esp_sha256_update(esp_sha256_t *ctx, const void *src, size_t size);
/**
* @brief calculate input data for SHA384
*
* @param ctx SHA384 contex pointer
* @param src input data buffer pointer
* @param size input data bytes
*
* @return 0 if success or fail
*/
int esp_sha384_update(esp_sha384_t *ctx, const void *src, size_t size);
/**
* @brief calculate input data for SHA512
*
* @param ctx SHA512 contex pointer
* @param src input data buffer pointer
* @param size input data bytes
*
* @return 0 if success or fail
*/
int esp_sha512_update(esp_sha512_t *ctx, const void *src, size_t size);
/**
* @brief output SHA1 calculation result
*
* @param ctx SHA1 contex pointer
* @param dest output data buffer pointer
*
* @return 0 if success or fail
*/
int esp_sha1_finish(esp_sha1_t *ctx, void *dest);
/**
* @brief output SHA224 calculation result
*
* @param ctx SHA224 contex pointer
* @param dest output data buffer pointer
*
* @return 0 if success or fail
*/
int esp_sha224_finish(esp_sha224_t *ctx, void *dest);
/**
* @brief output SHA256 calculation result
*
* @param ctx SHA256 contex pointer
* @param dest output data buffer pointer
*
* @return 0 if success or fail
*/
int esp_sha256_finish(esp_sha256_t *ctx, void *dest);
/**
* @brief output SHA384 calculation result
*
* @param ctx SHA384 contex pointer
* @param dest output data buffer pointer
*
* @return 0 if success or fail
*/
int esp_sha384_finish(esp_sha384_t *ctx, void *dest);
/**
* @brief output SHA512 calculation result
*
* @param ctx SHA512 contex pointer
* @param dest output data buffer pointer
*
* @return 0 if success or fail
*/
int esp_sha512_finish(esp_sha512_t *ctx, void *dest);
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,99 @@
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
#pragma once
#include <stdint.h>
#include <stddef.h>
/**
* @brief ESP8266's IBus(instruction bus) only can read data of word type by 4 bytes align,
* so if users' code read a bytes or half word from IBus, an load/store exception will occure.
*
* The exception function will read the word by align and transform it to be the right
* data that users' code want to read.
*
* CPU read data from IRAM or flash by IBus. And "const" type value or string will locate at flash by default.
* So users' code read "const" type data whose type is byte or half word should lead to exception,
* and the exception operation is so slow.
*
* For example:
*
* static const char s_output[] = "123456789";
*
* char get_byte(int index)
* {
* return s_output(index);
* }
*
* By above way, reading data may be not by 4 bytes align and word type.
*
* For this reason, we add the following function to read byte or half word without leading to exception. Firstly the function reads
* a word and then transform it to be the right data that users' code want to read. Users can modify above code to be following:
*
* static const char s_output[] = "123456789";
*
* char get_byte(int index)
* {
* return ESP_IBUS_GET_U8_DATA(index, s_output);
* }
*/
#ifdef USING_IBUS_FASTER_GET
#include "esp_attr.h"
#ifndef DISABLE_IBUS_INLINE_FUNC
#define IBUS_INLINE inline
#else
#define IBUS_INLINE
#endif
typedef union _ibus_data {
uint32_t u32_data[1];
uint16_t u16_data[2];
uint8_t u8_data[4];
} ibus_data_t;
#define __ESP_IBUS_GET_DATA(_type) \
static IBUS_INLINE _type __esp_ibus_get_## _type ## _data(size_t index, const void *pbuf) \
{ \
ibus_data_t data; \
const uint8_t num = index / (sizeof(uint32_t) / sizeof(_type)); \
const uint8_t off = index % (sizeof(uint32_t) / sizeof(_type)); \
const uint32_t *ptable = (const uint32_t *)pbuf; \
\
data.u32_data[0] = ptable[num]; \
\
if (sizeof(_type) == sizeof(uint8_t)) \
return data.u8_data[off]; \
else if (sizeof(_type) == sizeof(uint16_t)) \
return data.u16_data[off]; \
}
__ESP_IBUS_GET_DATA(uint8_t)
__ESP_IBUS_GET_DATA(uint16_t)
#define ESP_IBUS_ATTR WORD_ALIGNED_ATTR
#define ESP_IBUS_GET_U8_DATA(_index, _pbuf) __esp_ibus_get_uint8_t_data(_index, _pbuf)
#define ESP_IBUS_GET_U16_DATA(_index, _pbuf) __esp_ibus_get_uint16_t_data(_index, _pbuf)
#else
#define ESP_IBUS_ATTR
#define ESP_IBUS_GET_U8_DATA(_index, _pbuf) ((const uint8_t *)_pbuf)[_index]
#define ESP_IBUS_GET_U16_DATA(_index, _pbuf) ((const uint16_t *)_pbuf)[_index]
#endif

View File

@ -0,0 +1,24 @@
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
#pragma once
#include "sdkconfig.h"
#include "assert.h"
#ifdef CONFIG_UTIL_ASSERT
#define util_assert(_e) assert(_e)
#else
#define util_assert(_e)
#endif