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,284 @@
// 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 esp_aes {
int32_t nr; /*!< The number of AES key bits */
uint32_t *rk; /*!< The AES AES key */
uint32_t buf[68]; /*!< The AES calculation cache */
} esp_aes_t;
typedef struct esp_aes_xts {
esp_aes_t crypt; /*!< The AES context to use for AES block encryption or decryption. */
esp_aes_t tweak; /*!< The AES context used for tweak computation. */
} esp_aes_xts_t;
/**
* @brief Set AES encrypt key
*
* @param aes AES contex pointer
* @param p_key AES key data buffer
* @param keybits number of AES key bits
*
* @return 0 if success or fail
*/
int esp_aes_set_encrypt_key(esp_aes_t *aes, const void *p_key, size_t keybits);
/**
* @brief Set AES decrypt key
*
* @param aes AES contex pointer
* @param p_key AES key data buffer
* @param keybits number of AES key bits
*
* @return 0 if success or fail
*/
int esp_aes_set_decrypt_key(esp_aes_t *aes, const void *key, size_t keybits);
/**
* @brief AES normal encrypt calculation
*
* @param aes AES contex pointer
* @param p_src input data buffer
* @param slen input data length by bytes
* @param p_dst output data buffer
* @param dlen output data length by bytes
*
* @return 0 if success or fail
*/
int esp_aes_encrypt(esp_aes_t *aes, const void *p_src, size_t slen, void *p_dst, size_t dlen);
/**
* @brief AES normal decrypt calculation
*
* @param aes AES contex pointer
* @param p_src input data buffer
* @param slen input data length by bytes
* @param p_dst output data buffer
* @param dlen output data length by bytes
*
* @return 0 if success or fail
*/
int esp_aes_decrypt(esp_aes_t *aes, const void *p_src, size_t slen, void *p_dst, size_t dlen);
/**
* @brief AES-ECB encrypt calculation
*
* @param aes AES contex pointer
* @param p_src input data buffer
* @param p_dst output data buffer
*
* @return 0 if success or fail
*/
static inline int esp_aes_encrypt_ecb(esp_aes_t *aes, const void *p_src, void *p_dst)
{
return esp_aes_encrypt(aes, p_src, 16, p_dst, 16);
}
/**
* @brief AES-ECB decrypt calculation
*
* @param aes AES contex pointer
* @param p_src input data buffer
* @param p_dst output data buffer
*
* @return 0 if success or fail
*/
static inline int esp_aes_decrypt_ecb(esp_aes_t *aes, const void *p_src, void *p_dst)
{
return esp_aes_decrypt(aes, p_src, 16, p_dst, 16);
}
/**
* @brief AES-CBC encrypt calculation
*
* @param aes AES contex pointer
* @param p_src input data buffer
* @param slen input data length by bytes
* @param p_dst output data buffer
* @param dlen output data length by bytes
* @param p_iv initialization vector buffer
*
* @return 0 if success or fail
*/
int esp_aes_encrypt_cbc(esp_aes_t *aes, const void *p_src, size_t slen, void *p_dst, size_t dlen, void *p_iv);
/**
* @brief AES-CBC decrypt calculation
*
* @param aes AES contex pointer
* @param p_src input data buffer
* @param slen input data length by bytes
* @param p_dst output data buffer
* @param dlen output data length by bytes
* @param p_iv initialization vector buffer
*
* @return 0 if success or fail
*/
int esp_aes_decrypt_cbc(esp_aes_t *aes, const void *p_src, size_t slen, void *p_dst, size_t dlen, void *p_iv);
/**
* @brief AES-CFB128 encrypt calculation
*
* @param aes AES contex pointer
* @param p_src input data buffer
* @param slen input data length by bytes
* @param p_dst output data buffer
* @param dlen output data length by bytes
* @param p_iv initialization vector buffer
* @param iv_off initialization vector offset
*
* @return 0 if success or fail
*/
int esp_aes_encrypt_cfb128(esp_aes_t *aes, const void *p_src, size_t slen, void *p_dst, size_t dlen, void *p_iv, size_t *iv_off);
/**
* @brief AES-CFB128 decrypt calculation
*
* @param aes AES contex pointer
* @param p_src input data buffer
* @param slen input data length by bytes
* @param p_dst output data buffer
* @param dlen output data length by bytes
* @param p_iv initialization vector buffer
* @param iv_off initialization vector offset
*
* @return 0 if success or fail
*/
int esp_aes_decrypt_cfb128(esp_aes_t *aes, const void *p_src, size_t slen, void *p_dst, size_t dlen, void *p_iv, size_t *iv_off);
/**
* @brief AES-CFB8 encrypt calculation
*
* @param aes AES contex pointer
* @param p_src input data buffer
* @param slen input data length by bytes
* @param p_dst output data buffer
* @param dlen output data length by bytes
* @param p_iv initialization vector buffer
*
* @return 0 if success or fail
*/
int esp_aes_encrypt_cfb8(esp_aes_t *aes, const void *p_src, size_t slen, void *p_dst, size_t dlen, void *p_iv);
/**
* @brief AES-CFB8 decrypt calculation
*
* @param aes AES contex pointer
* @param p_src input data buffer
* @param slen input data length by bytes
* @param p_dst output data buffer
* @param dlen output data length by bytes
* @param p_iv initialization vector buffer
*
* @return 0 if success or fail
*/
int esp_aes_decrypt_cfb8(esp_aes_t *aes, const void *p_src, size_t slen, void *p_dst, size_t dlen, void *p_iv);
/**
* @brief AES-CTR encrypt calculation
*
* @param aes AES contex pointer
* @param nc_off offset in the current stream block
* @param p_nonce_counter 128-bit nonce and counter buffer
* @param p_stream_block current stream block buffer
* @param p_src input data buffer
* @param slen input data length by bytes
* @param p_dst output data buffer
* @param dlen output data length by bytes
*
* @return 0 if success or fail
*/
int esp_aes_encrypt_ctr(esp_aes_t *aes, size_t *nc_off, void *p_nonce_counter, void *p_stream_block, const void *p_src, size_t slen, void *p_dst, size_t dlen);
/**
* @brief AES-CTR decrypt calculation
*
* @param aes AES contex pointer
* @param nc_off offset in the current stream block
* @param p_nonce_counter 128-bit nonce and counter buffer
* @param p_stream_block current stream block buffer
* @param p_src input data buffer
* @param slen input data length by bytes
* @param p_dst output data buffer
* @param dlen output data length by bytes
*
* @return 0 if success or fail
*/
static inline int esp_aes_decrypt_ctr(esp_aes_t *aes, size_t *nc_off, void *p_nonce_counter, void *p_stream_block, const void *p_src, size_t slen, void *p_dst, size_t dlen)
{
return esp_aes_encrypt_ctr(aes, nc_off, p_nonce_counter, p_stream_block, p_src, slen, p_dst, dlen);
}
/**
* @brief Set AES XTS encrypt key
*
* @param aes AES XTS contex pointer
* @param p_key AES XTS key data buffer
* @param keybits number of AES XTS key bits
*
* @return 0 if success or fail
*/
int esp_aes_xts_set_encrypt_key(esp_aes_xts_t *aes, const void *p_key, size_t keybits);
/**
* @brief Set AES XTS decrypt key
*
* @param aes AES XTS contex pointer
* @param p_key AES XTS key data buffer
* @param keybits number of AES XTS key bits
*
* @return 0 if success or fail
*/
int esp_aes_xts_set_decrypt_key(esp_aes_xts_t *aes, const void *p_key, size_t keybits);
/**
* @brief AES XTS encrypt/decrypt calculation
*
* @param aes AES contex pointer
* @param encrypt 1 : encrypt, 0 : decrypt
* @param length data unit data length by bytes
* @param p_data_unit data unit buffer
* @param p_src input data buffer
* @param p_dst output data buffer
*
* @return 0 if success or fail
*/
int esp_aes_crypt_xts(esp_aes_xts_t *aes, int encrypt, size_t length, const void *p_data_unit, const void *p_src, void *p_dst);
/**
* @brief AES OFB encrypt/decrypt calculation
*
* @param aes AES contex pointer
* @param length data length by bytes
* @param iv_off IV offset
* @param p_iv IV data buffer
* @param p_src input data buffer
* @param p_dst output data buffer
*
* @return 0 if success or fail
*/
int esp_aes_crypt_ofb(esp_aes_t *ctx, size_t length, size_t *iv_off, void *p_iv, const void *p_src, void *p_dst);
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,71 @@
// 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>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief ARC4 context structure
*/
typedef struct
{
int x; /*!< permutation index */
int y; /*!< permutation index */
unsigned char m[256]; /*!< permutation table */
}esp_arc4_context;
/**
* @brief ARC4 key schedule
*
* @param ctx ARC4 context to be setup
* @param key the secret key
* @param keylen length of the key, in bytes
*/
void esp_arc4_setup(esp_arc4_context *ctx, const uint8_t *key, uint32_t keylen);
/**
* @brief ARC4 cipher function
*
* @param ctx ARC4 context
* @param length length of the input data
* @param input buffer holding the input data
* @param output buffer for the output data
*
* @return 0 if successful
*/
int esp_arc4_encrypt(esp_arc4_context *ctx, size_t length, const uint8_t *input, uint8_t *output);
/**
* @brief ARC4 cipher function
*
* @param ctx ARC4 context
* @param length length of the input data
* @param input buffer holding the input data
* @param output buffer for the output data
*
* @return 0 if successful
* @Note When you encrypt or decrypt, must call esp_arc4_setup function to set key.
* Encrypt and decrypt will change the ctx value
*/
int esp_arc4_decrypt(esp_arc4_context *ctx, size_t length, const uint8_t *input, uint8_t *output );
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,46 @@
// 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>
/**
* @brief encode a base64-formatted buffer
*
* @param p_src input encoded data buffer pointer
* @param slen input data length by bytes
* @param p_dst output buffer pointer
* @param dlen output buffer length by bytes
*
* @return the result
* 0 : Success
* -ENOBUFS : output buffer it not enough
*/
int esp_base64_encode(const void *p_src, uint32_t slen, void *p_dst, uint32_t dlen);
/**
* @brief decode a base64-formatted buffer
*
* @param p_src input encoded data buffer pointer
* @param slen input data length by bytes
* @param p_dst output buffer pointer
* @param dlen output buffer length by bytes
*
* @return the result
* 0 : Success
* -EINVAL : input parameter invalid
* -ENOBUFS : output buffer it not enough
*/
int esp_base64_decode(const void *p_src, uint32_t slen, void *p_dst, uint32_t dlen);

View File

@ -0,0 +1,59 @@
// 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>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief MD5 context structure
*/
typedef struct MD5Context{
uint32_t total[2]; /*!< number of bytes processed */
uint32_t state[4]; /*!< intermediate digest state */
uint8_t buffer[64]; /*!< data block being processed */
} esp_md5_context_t;
/**
* @brief MD5 context setup
* @param ctx context to be initialized
* @return 0 if successful
*/
int esp_md5_init(esp_md5_context_t *ctx);
/**
* @brief MD5 process buffer
* @param ctx MD5 context
* @param input buffer holding the data
* @param ilen length of the input data
* @return 0 if successful
*/
int esp_md5_update(esp_md5_context_t *ctx, const uint8_t *input, size_t ilen);
/**
* @brief MD5 final digest
* @param ctx MD5 context
* @param output MD5 checksum result
* @return 0 if successful
*/
int esp_md5_final(esp_md5_context_t *ctx, uint8_t output[16]);
#ifdef __cplusplus
}
#endif