From 87a61b2bac2dcdda2c691ca9fe5475f4d81c7838 Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Fri, 4 Oct 2024 10:33:45 -0300 Subject: [PATCH 01/14] test: adjusts on ciot_decoder tests --- tests/CMakeLists.txt | 4 ++++ tests/ciot_custom_config.h | 2 +- tests/common/ciot_crypt_test.c | 0 tests/common/ciot_decoder_s_test.c | 33 +++++++++++++-------------- tests/common/ciot_decoder_slip_test.c | 33 +++++++++++++-------------- 5 files changed, 37 insertions(+), 35 deletions(-) create mode 100644 tests/common/ciot_crypt_test.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d800d558..4fe8c100 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,6 +15,8 @@ FILE(GLOB_RECURSE COMMON_TESTS common/*) include_directories( ${CIOT_INCLUDE_DIRS} + ${CIOT_MBED_INCLUDE_DIRS} + ${CIOT_PB_INCLUDE_DIRS} unity/src . ) @@ -23,6 +25,8 @@ add_executable(${PROJECT_NAME} ${CIOT_SRCS} ${CIOT_MG_SRCS} ${CIOT_WIN_SRCS} + ${CIOT_PB_V1_SRCS} + ${CIOT_MBED_SRCS} ${COMMON_TESTS} unity/src/unity.c main.c diff --git a/tests/ciot_custom_config.h b/tests/ciot_custom_config.h index 36aeac5c..44254531 100644 --- a/tests/ciot_custom_config.h +++ b/tests/ciot_custom_config.h @@ -14,7 +14,7 @@ #include "ciot_log.h" -#define CIOT_CONFIG_LOG_LEVEL CIOT_LOG_LEVEL_NONE +#define CIOT_CONFIG_LOG_LEVEL CIOT__LOG_LEVEL__LOG_LEVEL_NONE #define CIOT_CONFIG_HARDWARE_NAME "CIoT Tests" #define CIOT_CONFIG_APP_VER 0, 1, 0 diff --git a/tests/common/ciot_crypt_test.c b/tests/common/ciot_crypt_test.c new file mode 100644 index 00000000..e69de29b diff --git a/tests/common/ciot_decoder_s_test.c b/tests/common/ciot_decoder_s_test.c index 82d91197..e47d25d6 100644 --- a/tests/common/ciot_decoder_s_test.c +++ b/tests/common/ciot_decoder_s_test.c @@ -14,14 +14,14 @@ #include "unity.h" #include "ciot_decoder_s.h" -static ciot_iface_t iface_mock; +static ciot_iface_t iface; static uint8_t encode_result[64]; static int encode_result_size; void test_ciot_decoder_s_decode_buffer_null() { - ciot_decoder_t decoder = ciot_decoder_s_new(NULL, 0); - ciot_err_t err = decoder->decode(decoder, 0); + iface.decoder = ciot_decoder_s_new(NULL, 0); + ciot_err_t err = iface.decoder->decode(&iface, 0); TEST_ASSERT(err == CIOT_ERR__NULL_ARG); } @@ -30,15 +30,15 @@ void test_ciot_decoder_s_decode_overflow() ciot_err_t err; uint8_t buf[4]; uint8_t data[] = { '{', 0x05, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, '}' }; - ciot_decoder_t decoder = ciot_decoder_s_new(buf, sizeof(buf)); + iface.decoder = ciot_decoder_s_new(buf, sizeof(buf)); for (size_t i = 0; i < sizeof(data); i++) { - err = decoder->decode(decoder, data[i]); + err = iface.decoder->decode(&iface, data[i]); if(err != CIOT_ERR__OK) break; } - TEST_ASSERT(decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); + TEST_ASSERT(iface.decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); TEST_ASSERT(err == CIOT_ERR__OVERFLOW); } @@ -47,15 +47,15 @@ void test_ciot_decoder_s_decode_protocol_violation() ciot_err_t err; uint8_t buf[32]; uint8_t data[] = { '{', 0x05, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, '}' }; - ciot_decoder_t decoder = ciot_decoder_s_new(buf, sizeof(buf)); + iface.decoder = ciot_decoder_s_new(buf, sizeof(buf)); for (size_t i = 0; i < sizeof(data); i++) { - err = decoder->decode(decoder, data[i]); + err = iface.decoder->decode(&iface, data[i]); if(err != CIOT_ERR__OK) break; } - TEST_ASSERT(decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); + TEST_ASSERT(iface.decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); TEST_ASSERT(err == CIOT_ERR__PROTOCOL_VIOLATION); } @@ -65,11 +65,11 @@ void test_ciot_decoder_s_decode_ok() uint8_t buf[32] = { 0 }; uint8_t data[] = { '{', 0x05, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, '}' }; uint8_t expected[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; - ciot_decoder_t decoder = ciot_decoder_s_new(buf, sizeof(buf)); + iface.decoder = ciot_decoder_s_new(buf, sizeof(buf)); for (size_t i = 0; i < sizeof(data); i++) { - err = decoder->decode(decoder, data[i]); + err = iface.decoder->decode(&iface, data[i]); if(err != CIOT_ERR__OK) break; } @@ -80,8 +80,8 @@ void test_ciot_decoder_s_decode_ok() void test_ciot_decoder_s_send_null_iface() { uint8_t data[32]; - ciot_decoder_t decoder = ciot_decoder_s_new(NULL, 0); - ciot_err_t err = decoder->send(decoder, data, 0); + iface.decoder = ciot_decoder_s_new(NULL, 0); + ciot_err_t err = iface.decoder->send(NULL, data, 0); TEST_ASSERT(err == CIOT_ERR__NULL_ARG); } @@ -90,9 +90,8 @@ void test_ciot_decoder_s_send_ok() uint8_t buf[16]; uint8_t data[] = { 0x01, 0x02, 0x03, 0x04, 0x05 }; uint8_t expected[] = { '{', 0x05, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, '}' }; - ciot_decoder_t decoder = ciot_decoder_s_new(buf, sizeof(buf)); - decoder->iface = &iface_mock; - ciot_err_t err = decoder->send(decoder, data, sizeof(data)); + iface.decoder = ciot_decoder_s_new(buf, sizeof(buf)); + ciot_err_t err = iface.decoder->send(&iface, data, sizeof(data)); TEST_ASSERT_EQUAL(err, CIOT_ERR__OK); TEST_ASSERT_EQUAL_MEMORY(expected, encode_result, encode_result_size); } @@ -107,7 +106,7 @@ static ciot_err_t iface_send(ciot_iface_t *iface, uint8_t *bytes, int size) void test_ciot_decoder_s() { - iface_mock.send_data = iface_send; + iface.send_data = iface_send; RUN_TEST(test_ciot_decoder_s_decode_buffer_null); RUN_TEST(test_ciot_decoder_s_decode_overflow); diff --git a/tests/common/ciot_decoder_slip_test.c b/tests/common/ciot_decoder_slip_test.c index 2d5ebf59..cb7fbfa4 100644 --- a/tests/common/ciot_decoder_slip_test.c +++ b/tests/common/ciot_decoder_slip_test.c @@ -14,14 +14,14 @@ #include "unity.h" #include "ciot_decoder_slip.h" -static ciot_iface_t iface_mock; +static ciot_iface_t iface; static uint8_t encode_result[64]; static int encode_result_size; void test_ciot_decoder_slip_decode_buffer_null() { - ciot_decoder_t decoder = ciot_decoder_slip_new(NULL, 0); - ciot_err_t err = decoder->decode(decoder, 0); + iface.decoder = ciot_decoder_slip_new(NULL, 0); + ciot_err_t err = iface.decoder->decode(&iface, 0); TEST_ASSERT(err == CIOT_ERR__NULL_ARG); } @@ -30,16 +30,16 @@ void test_ciot_decoder_slip_decode_no_memory() ciot_err_t err; uint8_t buf[4]; uint8_t data[] = {1, 2, 3, 4, 5}; - ciot_decoder_t decoder = ciot_decoder_slip_new(buf, sizeof(buf)); + iface.decoder = ciot_decoder_slip_new(buf, sizeof(buf)); for (size_t i = 0; i < sizeof(data); i++) { - err = decoder->decode(decoder, data[i]); + err = iface.decoder->decode(&iface, data[i]); if (err != CIOT_ERR__OK) break; } - TEST_ASSERT(decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); + TEST_ASSERT(iface.decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); TEST_ASSERT(err == CIOT_ERR__OVERFLOW); } @@ -48,16 +48,16 @@ void test_ciot_decoder_slip_decode_protocol_violation() ciot_err_t err; uint8_t buf[32]; uint8_t data[] = {1, 0xdb, 0xff}; - ciot_decoder_t decoder = ciot_decoder_slip_new(buf, sizeof(buf)); + iface.decoder = ciot_decoder_slip_new(buf, sizeof(buf)); for (size_t i = 0; i < sizeof(data); i++) { - err = decoder->decode(decoder, data[i]); + err = iface.decoder->decode(&iface, data[i]); if (err != CIOT_ERR__OK) break; } - TEST_ASSERT(decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); + TEST_ASSERT(iface.decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); TEST_ASSERT(err == CIOT_ERR__PROTOCOL_VIOLATION); } @@ -67,11 +67,11 @@ void test_ciot_decoder_slip_decode_ok() uint8_t buf[32] = {0}; uint8_t data[] = {0x01, 0xdb, 0xdc, 0xdb, 0xdc, 0x02, 0xdb, 0xdd, 0x03, 0xdc, 0x04, 0xdd, 0x05, 0xc0}; uint8_t expected[] = {0x01, 0xc0, 0xc0, 0x02, 0xdb, 0x03, 0xdc, 0x04, 0xdd, 0x05}; - ciot_decoder_t decoder = ciot_decoder_slip_new(buf, sizeof(buf)); + iface.decoder = ciot_decoder_slip_new(buf, sizeof(buf)); for (size_t i = 0; i < sizeof(data); i++) { - err = decoder->decode(decoder, data[i]); + err = iface.decoder->decode(&iface, data[i]); if (err != CIOT_ERR__OK) break; } @@ -83,8 +83,8 @@ void test_ciot_decoder_slip_decode_ok() void test_ciot_decoder_slip_send_null_iface() { uint8_t data[32]; - ciot_decoder_t decoder = ciot_decoder_slip_new(NULL, 0); - ciot_err_t err = decoder->send(decoder, data, 0); + iface.decoder = ciot_decoder_slip_new(NULL, 0); + ciot_err_t err = iface.decoder->send(NULL, data, 0); TEST_ASSERT(err == CIOT_ERR__NULL_ARG); } @@ -93,9 +93,8 @@ void test_ciot_decoder_slip_send_ok() uint8_t buf[16]; uint8_t data[] = {0x01, 0xc0, 0xc0, 0x02, 0xdb, 0x03, 0xdc, 0x04, 0xdd, 0x05}; uint8_t expected[] = {0x01, 0xdb, 0xdc, 0xdb, 0xdc, 0x02, 0xdb, 0xdd, 0x03, 0xdc, 0x04, 0xdd, 0x05, 0xc0}; - ciot_decoder_t decoder = ciot_decoder_slip_new(buf, sizeof(buf)); - decoder->iface = &iface_mock; - ciot_err_t err = decoder->send(decoder, data, sizeof(data)); + iface.decoder = ciot_decoder_slip_new(buf, sizeof(buf)); + ciot_err_t err = iface.decoder->send(&iface, data, sizeof(data)); TEST_ASSERT_EQUAL(err, CIOT_ERR__OK); TEST_ASSERT_EQUAL_MEMORY(expected, encode_result, encode_result_size); } @@ -110,7 +109,7 @@ static ciot_err_t iface_send(ciot_iface_t *iface, uint8_t *bytes, int size) void test_ciot_decoder_slip() { - iface_mock.send_data = iface_send; + iface.send_data = iface_send; RUN_TEST(test_ciot_decoder_slip_decode_buffer_null); RUN_TEST(test_ciot_decoder_slip_decode_no_memory); From c612f4108f9264a87c5fb0bf09a35505562f3b02 Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:30:08 -0300 Subject: [PATCH 02/14] feat: added mbedtls library --- libs/crypt/aes.c | 2271 ++++++++++++++++++ libs/crypt/base64.c | 364 +++ libs/crypt/mbedtls/aes.h | 701 ++++++ libs/crypt/mbedtls/base64.h | 125 + libs/crypt/mbedtls/config.h | 3444 ++++++++++++++++++++++++++++ libs/crypt/mbedtls/platform.h | 394 ++++ libs/crypt/mbedtls/platform_time.h | 107 + libs/crypt/mbedtls/platform_util.h | 221 ++ libs/crypt/mbedtls/threading.h | 153 ++ libs/crypt/platform.c | 373 +++ libs/crypt/platform_util.c | 164 ++ libs/crypt/threading.c | 218 ++ 12 files changed, 8535 insertions(+) create mode 100644 libs/crypt/aes.c create mode 100644 libs/crypt/base64.c create mode 100644 libs/crypt/mbedtls/aes.h create mode 100644 libs/crypt/mbedtls/base64.h create mode 100644 libs/crypt/mbedtls/config.h create mode 100644 libs/crypt/mbedtls/platform.h create mode 100644 libs/crypt/mbedtls/platform_time.h create mode 100644 libs/crypt/mbedtls/platform_util.h create mode 100644 libs/crypt/mbedtls/threading.h create mode 100644 libs/crypt/platform.c create mode 100644 libs/crypt/platform_util.c create mode 100644 libs/crypt/threading.c diff --git a/libs/crypt/aes.c b/libs/crypt/aes.c new file mode 100644 index 00000000..af19a384 --- /dev/null +++ b/libs/crypt/aes.c @@ -0,0 +1,2271 @@ +/* + * FIPS-197 compliant AES implementation + * + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ +/* + * The AES block cipher was designed by Vincent Rijmen and Joan Daemen. + * + * http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf + * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf + */ + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#if defined(MBEDTLS_AES_C) + +#include + +#include "mbedtls/aes.h" +#include "mbedtls/platform.h" +#include "mbedtls/platform_util.h" +#if defined(MBEDTLS_PADLOCK_C) +#include "mbedtls/padlock.h" +#endif +#if defined(MBEDTLS_AESNI_C) +#include "mbedtls/aesni.h" +#endif + +#if defined(MBEDTLS_SELF_TEST) +#if defined(MBEDTLS_PLATFORM_C) +#include "mbedtls/platform.h" +#else +#include +#define mbedtls_printf printf +#endif /* MBEDTLS_PLATFORM_C */ +#endif /* MBEDTLS_SELF_TEST */ + +#if !defined(MBEDTLS_AES_ALT) + +/* Parameter validation macros based on platform_util.h */ +#define AES_VALIDATE_RET( cond ) \ + MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_AES_BAD_INPUT_DATA ) +#define AES_VALIDATE( cond ) \ + MBEDTLS_INTERNAL_VALIDATE( cond ) + +/* + * 32-bit integer manipulation macros (little endian) + */ +#ifndef GET_UINT32_LE +#define 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 ); \ +} +#endif + +#ifndef PUT_UINT32_LE +#define PUT_UINT32_LE(n,b,i) \ +{ \ + (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ + (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ + (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ + (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ +} +#endif + +#if defined(MBEDTLS_PADLOCK_C) && \ + ( defined(MBEDTLS_HAVE_X86) || defined(MBEDTLS_PADLOCK_ALIGN16) ) +static int aes_padlock_ace = -1; +#endif + +#if defined(MBEDTLS_AES_ROM_TABLES) +/* + * Forward S-box + */ +static const unsigned char FSb[256] = +{ + 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, + 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, + 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, + 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, + 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, + 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, + 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, + 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75, + 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, + 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, + 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, + 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, + 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, + 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8, + 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, + 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, + 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, + 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, + 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, + 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB, + 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, + 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, + 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, + 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, + 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, + 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A, + 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, + 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, + 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, + 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, + 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, + 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16 +}; + +/* + * Forward tables + */ +#define FT \ +\ + V(A5,63,63,C6), V(84,7C,7C,F8), V(99,77,77,EE), V(8D,7B,7B,F6), \ + V(0D,F2,F2,FF), V(BD,6B,6B,D6), V(B1,6F,6F,DE), V(54,C5,C5,91), \ + V(50,30,30,60), V(03,01,01,02), V(A9,67,67,CE), V(7D,2B,2B,56), \ + V(19,FE,FE,E7), V(62,D7,D7,B5), V(E6,AB,AB,4D), V(9A,76,76,EC), \ + V(45,CA,CA,8F), V(9D,82,82,1F), V(40,C9,C9,89), V(87,7D,7D,FA), \ + V(15,FA,FA,EF), V(EB,59,59,B2), V(C9,47,47,8E), V(0B,F0,F0,FB), \ + V(EC,AD,AD,41), V(67,D4,D4,B3), V(FD,A2,A2,5F), V(EA,AF,AF,45), \ + V(BF,9C,9C,23), V(F7,A4,A4,53), V(96,72,72,E4), V(5B,C0,C0,9B), \ + V(C2,B7,B7,75), V(1C,FD,FD,E1), V(AE,93,93,3D), V(6A,26,26,4C), \ + V(5A,36,36,6C), V(41,3F,3F,7E), V(02,F7,F7,F5), V(4F,CC,CC,83), \ + V(5C,34,34,68), V(F4,A5,A5,51), V(34,E5,E5,D1), V(08,F1,F1,F9), \ + V(93,71,71,E2), V(73,D8,D8,AB), V(53,31,31,62), V(3F,15,15,2A), \ + V(0C,04,04,08), V(52,C7,C7,95), V(65,23,23,46), V(5E,C3,C3,9D), \ + V(28,18,18,30), V(A1,96,96,37), V(0F,05,05,0A), V(B5,9A,9A,2F), \ + V(09,07,07,0E), V(36,12,12,24), V(9B,80,80,1B), V(3D,E2,E2,DF), \ + V(26,EB,EB,CD), V(69,27,27,4E), V(CD,B2,B2,7F), V(9F,75,75,EA), \ + V(1B,09,09,12), V(9E,83,83,1D), V(74,2C,2C,58), V(2E,1A,1A,34), \ + V(2D,1B,1B,36), V(B2,6E,6E,DC), V(EE,5A,5A,B4), V(FB,A0,A0,5B), \ + V(F6,52,52,A4), V(4D,3B,3B,76), V(61,D6,D6,B7), V(CE,B3,B3,7D), \ + V(7B,29,29,52), V(3E,E3,E3,DD), V(71,2F,2F,5E), V(97,84,84,13), \ + V(F5,53,53,A6), V(68,D1,D1,B9), V(00,00,00,00), V(2C,ED,ED,C1), \ + V(60,20,20,40), V(1F,FC,FC,E3), V(C8,B1,B1,79), V(ED,5B,5B,B6), \ + V(BE,6A,6A,D4), V(46,CB,CB,8D), V(D9,BE,BE,67), V(4B,39,39,72), \ + V(DE,4A,4A,94), V(D4,4C,4C,98), V(E8,58,58,B0), V(4A,CF,CF,85), \ + V(6B,D0,D0,BB), V(2A,EF,EF,C5), V(E5,AA,AA,4F), V(16,FB,FB,ED), \ + V(C5,43,43,86), V(D7,4D,4D,9A), V(55,33,33,66), V(94,85,85,11), \ + V(CF,45,45,8A), V(10,F9,F9,E9), V(06,02,02,04), V(81,7F,7F,FE), \ + V(F0,50,50,A0), V(44,3C,3C,78), V(BA,9F,9F,25), V(E3,A8,A8,4B), \ + V(F3,51,51,A2), V(FE,A3,A3,5D), V(C0,40,40,80), V(8A,8F,8F,05), \ + V(AD,92,92,3F), V(BC,9D,9D,21), V(48,38,38,70), V(04,F5,F5,F1), \ + V(DF,BC,BC,63), V(C1,B6,B6,77), V(75,DA,DA,AF), V(63,21,21,42), \ + V(30,10,10,20), V(1A,FF,FF,E5), V(0E,F3,F3,FD), V(6D,D2,D2,BF), \ + V(4C,CD,CD,81), V(14,0C,0C,18), V(35,13,13,26), V(2F,EC,EC,C3), \ + V(E1,5F,5F,BE), V(A2,97,97,35), V(CC,44,44,88), V(39,17,17,2E), \ + V(57,C4,C4,93), V(F2,A7,A7,55), V(82,7E,7E,FC), V(47,3D,3D,7A), \ + V(AC,64,64,C8), V(E7,5D,5D,BA), V(2B,19,19,32), V(95,73,73,E6), \ + V(A0,60,60,C0), V(98,81,81,19), V(D1,4F,4F,9E), V(7F,DC,DC,A3), \ + V(66,22,22,44), V(7E,2A,2A,54), V(AB,90,90,3B), V(83,88,88,0B), \ + V(CA,46,46,8C), V(29,EE,EE,C7), V(D3,B8,B8,6B), V(3C,14,14,28), \ + V(79,DE,DE,A7), V(E2,5E,5E,BC), V(1D,0B,0B,16), V(76,DB,DB,AD), \ + V(3B,E0,E0,DB), V(56,32,32,64), V(4E,3A,3A,74), V(1E,0A,0A,14), \ + V(DB,49,49,92), V(0A,06,06,0C), V(6C,24,24,48), V(E4,5C,5C,B8), \ + V(5D,C2,C2,9F), V(6E,D3,D3,BD), V(EF,AC,AC,43), V(A6,62,62,C4), \ + V(A8,91,91,39), V(A4,95,95,31), V(37,E4,E4,D3), V(8B,79,79,F2), \ + V(32,E7,E7,D5), V(43,C8,C8,8B), V(59,37,37,6E), V(B7,6D,6D,DA), \ + V(8C,8D,8D,01), V(64,D5,D5,B1), V(D2,4E,4E,9C), V(E0,A9,A9,49), \ + V(B4,6C,6C,D8), V(FA,56,56,AC), V(07,F4,F4,F3), V(25,EA,EA,CF), \ + V(AF,65,65,CA), V(8E,7A,7A,F4), V(E9,AE,AE,47), V(18,08,08,10), \ + V(D5,BA,BA,6F), V(88,78,78,F0), V(6F,25,25,4A), V(72,2E,2E,5C), \ + V(24,1C,1C,38), V(F1,A6,A6,57), V(C7,B4,B4,73), V(51,C6,C6,97), \ + V(23,E8,E8,CB), V(7C,DD,DD,A1), V(9C,74,74,E8), V(21,1F,1F,3E), \ + V(DD,4B,4B,96), V(DC,BD,BD,61), V(86,8B,8B,0D), V(85,8A,8A,0F), \ + V(90,70,70,E0), V(42,3E,3E,7C), V(C4,B5,B5,71), V(AA,66,66,CC), \ + V(D8,48,48,90), V(05,03,03,06), V(01,F6,F6,F7), V(12,0E,0E,1C), \ + V(A3,61,61,C2), V(5F,35,35,6A), V(F9,57,57,AE), V(D0,B9,B9,69), \ + V(91,86,86,17), V(58,C1,C1,99), V(27,1D,1D,3A), V(B9,9E,9E,27), \ + V(38,E1,E1,D9), V(13,F8,F8,EB), V(B3,98,98,2B), V(33,11,11,22), \ + V(BB,69,69,D2), V(70,D9,D9,A9), V(89,8E,8E,07), V(A7,94,94,33), \ + V(B6,9B,9B,2D), V(22,1E,1E,3C), V(92,87,87,15), V(20,E9,E9,C9), \ + V(49,CE,CE,87), V(FF,55,55,AA), V(78,28,28,50), V(7A,DF,DF,A5), \ + V(8F,8C,8C,03), V(F8,A1,A1,59), V(80,89,89,09), V(17,0D,0D,1A), \ + V(DA,BF,BF,65), V(31,E6,E6,D7), V(C6,42,42,84), V(B8,68,68,D0), \ + V(C3,41,41,82), V(B0,99,99,29), V(77,2D,2D,5A), V(11,0F,0F,1E), \ + V(CB,B0,B0,7B), V(FC,54,54,A8), V(D6,BB,BB,6D), V(3A,16,16,2C) + +#define V(a,b,c,d) 0x##a##b##c##d +static const uint32_t FT0[256] = { FT }; +#undef V + +#if !defined(MBEDTLS_AES_FEWER_TABLES) + +#define V(a,b,c,d) 0x##b##c##d##a +static const uint32_t FT1[256] = { FT }; +#undef V + +#define V(a,b,c,d) 0x##c##d##a##b +static const uint32_t FT2[256] = { FT }; +#undef V + +#define V(a,b,c,d) 0x##d##a##b##c +static const uint32_t FT3[256] = { FT }; +#undef V + +#endif /* !MBEDTLS_AES_FEWER_TABLES */ + +#undef FT + +/* + * Reverse S-box + */ +static const unsigned char RSb[256] = +{ + 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, + 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB, + 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, + 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, + 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, + 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E, + 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, + 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25, + 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, + 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, + 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, + 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84, + 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, + 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06, + 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, + 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, + 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, + 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73, + 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, + 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E, + 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, + 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, + 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, + 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4, + 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, + 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F, + 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, + 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, + 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, + 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61, + 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, + 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D +}; + +/* + * Reverse tables + */ +#define RT \ +\ + V(50,A7,F4,51), V(53,65,41,7E), V(C3,A4,17,1A), V(96,5E,27,3A), \ + V(CB,6B,AB,3B), V(F1,45,9D,1F), V(AB,58,FA,AC), V(93,03,E3,4B), \ + V(55,FA,30,20), V(F6,6D,76,AD), V(91,76,CC,88), V(25,4C,02,F5), \ + V(FC,D7,E5,4F), V(D7,CB,2A,C5), V(80,44,35,26), V(8F,A3,62,B5), \ + V(49,5A,B1,DE), V(67,1B,BA,25), V(98,0E,EA,45), V(E1,C0,FE,5D), \ + V(02,75,2F,C3), V(12,F0,4C,81), V(A3,97,46,8D), V(C6,F9,D3,6B), \ + V(E7,5F,8F,03), V(95,9C,92,15), V(EB,7A,6D,BF), V(DA,59,52,95), \ + V(2D,83,BE,D4), V(D3,21,74,58), V(29,69,E0,49), V(44,C8,C9,8E), \ + V(6A,89,C2,75), V(78,79,8E,F4), V(6B,3E,58,99), V(DD,71,B9,27), \ + V(B6,4F,E1,BE), V(17,AD,88,F0), V(66,AC,20,C9), V(B4,3A,CE,7D), \ + V(18,4A,DF,63), V(82,31,1A,E5), V(60,33,51,97), V(45,7F,53,62), \ + V(E0,77,64,B1), V(84,AE,6B,BB), V(1C,A0,81,FE), V(94,2B,08,F9), \ + V(58,68,48,70), V(19,FD,45,8F), V(87,6C,DE,94), V(B7,F8,7B,52), \ + V(23,D3,73,AB), V(E2,02,4B,72), V(57,8F,1F,E3), V(2A,AB,55,66), \ + V(07,28,EB,B2), V(03,C2,B5,2F), V(9A,7B,C5,86), V(A5,08,37,D3), \ + V(F2,87,28,30), V(B2,A5,BF,23), V(BA,6A,03,02), V(5C,82,16,ED), \ + V(2B,1C,CF,8A), V(92,B4,79,A7), V(F0,F2,07,F3), V(A1,E2,69,4E), \ + V(CD,F4,DA,65), V(D5,BE,05,06), V(1F,62,34,D1), V(8A,FE,A6,C4), \ + V(9D,53,2E,34), V(A0,55,F3,A2), V(32,E1,8A,05), V(75,EB,F6,A4), \ + V(39,EC,83,0B), V(AA,EF,60,40), V(06,9F,71,5E), V(51,10,6E,BD), \ + V(F9,8A,21,3E), V(3D,06,DD,96), V(AE,05,3E,DD), V(46,BD,E6,4D), \ + V(B5,8D,54,91), V(05,5D,C4,71), V(6F,D4,06,04), V(FF,15,50,60), \ + V(24,FB,98,19), V(97,E9,BD,D6), V(CC,43,40,89), V(77,9E,D9,67), \ + V(BD,42,E8,B0), V(88,8B,89,07), V(38,5B,19,E7), V(DB,EE,C8,79), \ + V(47,0A,7C,A1), V(E9,0F,42,7C), V(C9,1E,84,F8), V(00,00,00,00), \ + V(83,86,80,09), V(48,ED,2B,32), V(AC,70,11,1E), V(4E,72,5A,6C), \ + V(FB,FF,0E,FD), V(56,38,85,0F), V(1E,D5,AE,3D), V(27,39,2D,36), \ + V(64,D9,0F,0A), V(21,A6,5C,68), V(D1,54,5B,9B), V(3A,2E,36,24), \ + V(B1,67,0A,0C), V(0F,E7,57,93), V(D2,96,EE,B4), V(9E,91,9B,1B), \ + V(4F,C5,C0,80), V(A2,20,DC,61), V(69,4B,77,5A), V(16,1A,12,1C), \ + V(0A,BA,93,E2), V(E5,2A,A0,C0), V(43,E0,22,3C), V(1D,17,1B,12), \ + V(0B,0D,09,0E), V(AD,C7,8B,F2), V(B9,A8,B6,2D), V(C8,A9,1E,14), \ + V(85,19,F1,57), V(4C,07,75,AF), V(BB,DD,99,EE), V(FD,60,7F,A3), \ + V(9F,26,01,F7), V(BC,F5,72,5C), V(C5,3B,66,44), V(34,7E,FB,5B), \ + V(76,29,43,8B), V(DC,C6,23,CB), V(68,FC,ED,B6), V(63,F1,E4,B8), \ + V(CA,DC,31,D7), V(10,85,63,42), V(40,22,97,13), V(20,11,C6,84), \ + V(7D,24,4A,85), V(F8,3D,BB,D2), V(11,32,F9,AE), V(6D,A1,29,C7), \ + V(4B,2F,9E,1D), V(F3,30,B2,DC), V(EC,52,86,0D), V(D0,E3,C1,77), \ + V(6C,16,B3,2B), V(99,B9,70,A9), V(FA,48,94,11), V(22,64,E9,47), \ + V(C4,8C,FC,A8), V(1A,3F,F0,A0), V(D8,2C,7D,56), V(EF,90,33,22), \ + V(C7,4E,49,87), V(C1,D1,38,D9), V(FE,A2,CA,8C), V(36,0B,D4,98), \ + V(CF,81,F5,A6), V(28,DE,7A,A5), V(26,8E,B7,DA), V(A4,BF,AD,3F), \ + V(E4,9D,3A,2C), V(0D,92,78,50), V(9B,CC,5F,6A), V(62,46,7E,54), \ + V(C2,13,8D,F6), V(E8,B8,D8,90), V(5E,F7,39,2E), V(F5,AF,C3,82), \ + V(BE,80,5D,9F), V(7C,93,D0,69), V(A9,2D,D5,6F), V(B3,12,25,CF), \ + V(3B,99,AC,C8), V(A7,7D,18,10), V(6E,63,9C,E8), V(7B,BB,3B,DB), \ + V(09,78,26,CD), V(F4,18,59,6E), V(01,B7,9A,EC), V(A8,9A,4F,83), \ + V(65,6E,95,E6), V(7E,E6,FF,AA), V(08,CF,BC,21), V(E6,E8,15,EF), \ + V(D9,9B,E7,BA), V(CE,36,6F,4A), V(D4,09,9F,EA), V(D6,7C,B0,29), \ + V(AF,B2,A4,31), V(31,23,3F,2A), V(30,94,A5,C6), V(C0,66,A2,35), \ + V(37,BC,4E,74), V(A6,CA,82,FC), V(B0,D0,90,E0), V(15,D8,A7,33), \ + V(4A,98,04,F1), V(F7,DA,EC,41), V(0E,50,CD,7F), V(2F,F6,91,17), \ + V(8D,D6,4D,76), V(4D,B0,EF,43), V(54,4D,AA,CC), V(DF,04,96,E4), \ + V(E3,B5,D1,9E), V(1B,88,6A,4C), V(B8,1F,2C,C1), V(7F,51,65,46), \ + V(04,EA,5E,9D), V(5D,35,8C,01), V(73,74,87,FA), V(2E,41,0B,FB), \ + V(5A,1D,67,B3), V(52,D2,DB,92), V(33,56,10,E9), V(13,47,D6,6D), \ + V(8C,61,D7,9A), V(7A,0C,A1,37), V(8E,14,F8,59), V(89,3C,13,EB), \ + V(EE,27,A9,CE), V(35,C9,61,B7), V(ED,E5,1C,E1), V(3C,B1,47,7A), \ + V(59,DF,D2,9C), V(3F,73,F2,55), V(79,CE,14,18), V(BF,37,C7,73), \ + V(EA,CD,F7,53), V(5B,AA,FD,5F), V(14,6F,3D,DF), V(86,DB,44,78), \ + V(81,F3,AF,CA), V(3E,C4,68,B9), V(2C,34,24,38), V(5F,40,A3,C2), \ + V(72,C3,1D,16), V(0C,25,E2,BC), V(8B,49,3C,28), V(41,95,0D,FF), \ + V(71,01,A8,39), V(DE,B3,0C,08), V(9C,E4,B4,D8), V(90,C1,56,64), \ + V(61,84,CB,7B), V(70,B6,32,D5), V(74,5C,6C,48), V(42,57,B8,D0) + +#define V(a,b,c,d) 0x##a##b##c##d +static const uint32_t RT0[256] = { RT }; +#undef V + +#if !defined(MBEDTLS_AES_FEWER_TABLES) + +#define V(a,b,c,d) 0x##b##c##d##a +static const uint32_t RT1[256] = { RT }; +#undef V + +#define V(a,b,c,d) 0x##c##d##a##b +static const uint32_t RT2[256] = { RT }; +#undef V + +#define V(a,b,c,d) 0x##d##a##b##c +static const uint32_t RT3[256] = { RT }; +#undef V + +#endif /* !MBEDTLS_AES_FEWER_TABLES */ + +#undef RT + +/* + * Round constants + */ +static const uint32_t RCON[10] = +{ + 0x00000001, 0x00000002, 0x00000004, 0x00000008, + 0x00000010, 0x00000020, 0x00000040, 0x00000080, + 0x0000001B, 0x00000036 +}; + +#else /* MBEDTLS_AES_ROM_TABLES */ + +/* + * Forward S-box & tables + */ +static unsigned char FSb[256]; +static uint32_t FT0[256]; +#if !defined(MBEDTLS_AES_FEWER_TABLES) +static uint32_t FT1[256]; +static uint32_t FT2[256]; +static uint32_t FT3[256]; +#endif /* !MBEDTLS_AES_FEWER_TABLES */ + +/* + * Reverse S-box & tables + */ +static unsigned char RSb[256]; +static uint32_t RT0[256]; +#if !defined(MBEDTLS_AES_FEWER_TABLES) +static uint32_t RT1[256]; +static uint32_t RT2[256]; +static uint32_t RT3[256]; +#endif /* !MBEDTLS_AES_FEWER_TABLES */ + +/* + * Round constants + */ +static uint32_t RCON[10]; + +/* + * Tables generation code + */ +#define ROTL8(x) ( ( (x) << 8 ) & 0xFFFFFFFF ) | ( (x) >> 24 ) +#define XTIME(x) ( ( (x) << 1 ) ^ ( ( (x) & 0x80 ) ? 0x1B : 0x00 ) ) +#define MUL(x,y) ( ( (x) && (y) ) ? pow[(log[(x)]+log[(y)]) % 255] : 0 ) + +static int aes_init_done = 0; + +static void aes_gen_tables( void ) +{ + int i, x, y, z; + int pow[256]; + int log[256]; + + /* + * compute pow and log tables over GF(2^8) + */ + for( i = 0, x = 1; i < 256; i++ ) + { + pow[i] = x; + log[x] = i; + x = ( x ^ XTIME( x ) ) & 0xFF; + } + + /* + * calculate the round constants + */ + for( i = 0, x = 1; i < 10; i++ ) + { + RCON[i] = (uint32_t) x; + x = XTIME( x ) & 0xFF; + } + + /* + * generate the forward and reverse S-boxes + */ + FSb[0x00] = 0x63; + RSb[0x63] = 0x00; + + for( i = 1; i < 256; i++ ) + { + x = pow[255 - log[i]]; + + y = x; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF; + x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF; + x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF; + x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF; + x ^= y ^ 0x63; + + FSb[i] = (unsigned char) x; + RSb[x] = (unsigned char) i; + } + + /* + * generate the forward and reverse tables + */ + for( i = 0; i < 256; i++ ) + { + x = FSb[i]; + y = XTIME( x ) & 0xFF; + z = ( y ^ x ) & 0xFF; + + FT0[i] = ( (uint32_t) y ) ^ + ( (uint32_t) x << 8 ) ^ + ( (uint32_t) x << 16 ) ^ + ( (uint32_t) z << 24 ); + +#if !defined(MBEDTLS_AES_FEWER_TABLES) + FT1[i] = ROTL8( FT0[i] ); + FT2[i] = ROTL8( FT1[i] ); + FT3[i] = ROTL8( FT2[i] ); +#endif /* !MBEDTLS_AES_FEWER_TABLES */ + + x = RSb[i]; + + RT0[i] = ( (uint32_t) MUL( 0x0E, x ) ) ^ + ( (uint32_t) MUL( 0x09, x ) << 8 ) ^ + ( (uint32_t) MUL( 0x0D, x ) << 16 ) ^ + ( (uint32_t) MUL( 0x0B, x ) << 24 ); + +#if !defined(MBEDTLS_AES_FEWER_TABLES) + RT1[i] = ROTL8( RT0[i] ); + RT2[i] = ROTL8( RT1[i] ); + RT3[i] = ROTL8( RT2[i] ); +#endif /* !MBEDTLS_AES_FEWER_TABLES */ + } +} + +#undef ROTL8 + +#endif /* MBEDTLS_AES_ROM_TABLES */ + +#if defined(MBEDTLS_AES_FEWER_TABLES) + +#define ROTL8(x) ( (uint32_t)( ( x ) << 8 ) + (uint32_t)( ( x ) >> 24 ) ) +#define ROTL16(x) ( (uint32_t)( ( x ) << 16 ) + (uint32_t)( ( x ) >> 16 ) ) +#define ROTL24(x) ( (uint32_t)( ( x ) << 24 ) + (uint32_t)( ( x ) >> 8 ) ) + +#define AES_RT0(idx) RT0[idx] +#define AES_RT1(idx) ROTL8( RT0[idx] ) +#define AES_RT2(idx) ROTL16( RT0[idx] ) +#define AES_RT3(idx) ROTL24( RT0[idx] ) + +#define AES_FT0(idx) FT0[idx] +#define AES_FT1(idx) ROTL8( FT0[idx] ) +#define AES_FT2(idx) ROTL16( FT0[idx] ) +#define AES_FT3(idx) ROTL24( FT0[idx] ) + +#else /* MBEDTLS_AES_FEWER_TABLES */ + +#define AES_RT0(idx) RT0[idx] +#define AES_RT1(idx) RT1[idx] +#define AES_RT2(idx) RT2[idx] +#define AES_RT3(idx) RT3[idx] + +#define AES_FT0(idx) FT0[idx] +#define AES_FT1(idx) FT1[idx] +#define AES_FT2(idx) FT2[idx] +#define AES_FT3(idx) FT3[idx] + +#endif /* MBEDTLS_AES_FEWER_TABLES */ + +void mbedtls_aes_init( mbedtls_aes_context *ctx ) +{ + AES_VALIDATE( ctx != NULL ); + + memset( ctx, 0, sizeof( mbedtls_aes_context ) ); +} + +void mbedtls_aes_free( mbedtls_aes_context *ctx ) +{ + if( ctx == NULL ) + return; + + mbedtls_platform_zeroize( ctx, sizeof( mbedtls_aes_context ) ); +} + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx ) +{ + AES_VALIDATE( ctx != NULL ); + + mbedtls_aes_init( &ctx->crypt ); + mbedtls_aes_init( &ctx->tweak ); +} + +void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx ) +{ + if( ctx == NULL ) + return; + + mbedtls_aes_free( &ctx->crypt ); + mbedtls_aes_free( &ctx->tweak ); +} +#endif /* MBEDTLS_CIPHER_MODE_XTS */ + +/* + * AES key schedule (encryption) + */ +#if !defined(MBEDTLS_AES_SETKEY_ENC_ALT) +int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, + unsigned int keybits ) +{ + unsigned int i; + uint32_t *RK; + + AES_VALIDATE_RET( ctx != NULL ); + AES_VALIDATE_RET( key != NULL ); + + switch( keybits ) + { + case 128: ctx->nr = 10; break; + case 192: ctx->nr = 12; break; + case 256: ctx->nr = 14; break; + default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH ); + } + +#if !defined(MBEDTLS_AES_ROM_TABLES) + if( aes_init_done == 0 ) + { + aes_gen_tables(); + aes_init_done = 1; + } +#endif + +#if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_PADLOCK_ALIGN16) + if( aes_padlock_ace == -1 ) + aes_padlock_ace = mbedtls_padlock_has_support( MBEDTLS_PADLOCK_ACE ); + + if( aes_padlock_ace ) + ctx->rk = RK = MBEDTLS_PADLOCK_ALIGN16( ctx->buf ); + else +#endif + ctx->rk = RK = ctx->buf; + +#if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64) + if( mbedtls_aesni_has_support( MBEDTLS_AESNI_AES ) ) + return( mbedtls_aesni_setkey_enc( (unsigned char *) ctx->rk, key, keybits ) ); +#endif + + for( i = 0; i < ( keybits >> 5 ); i++ ) + { + GET_UINT32_LE( RK[i], key, i << 2 ); + } + + switch( ctx->nr ) + { + case 10: + + for( i = 0; i < 10; i++, RK += 4 ) + { + RK[4] = RK[0] ^ RCON[i] ^ + ( (uint32_t) FSb[ ( RK[3] >> 8 ) & 0xFF ] ) ^ + ( (uint32_t) FSb[ ( RK[3] >> 16 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) FSb[ ( RK[3] >> 24 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) FSb[ ( RK[3] ) & 0xFF ] << 24 ); + + RK[5] = RK[1] ^ RK[4]; + RK[6] = RK[2] ^ RK[5]; + RK[7] = RK[3] ^ RK[6]; + } + break; + + case 12: + + for( i = 0; i < 8; i++, RK += 6 ) + { + RK[6] = RK[0] ^ RCON[i] ^ + ( (uint32_t) FSb[ ( RK[5] >> 8 ) & 0xFF ] ) ^ + ( (uint32_t) FSb[ ( RK[5] >> 16 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) FSb[ ( RK[5] >> 24 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) FSb[ ( RK[5] ) & 0xFF ] << 24 ); + + RK[7] = RK[1] ^ RK[6]; + RK[8] = RK[2] ^ RK[7]; + RK[9] = RK[3] ^ RK[8]; + RK[10] = RK[4] ^ RK[9]; + RK[11] = RK[5] ^ RK[10]; + } + break; + + case 14: + + for( i = 0; i < 7; i++, RK += 8 ) + { + RK[8] = RK[0] ^ RCON[i] ^ + ( (uint32_t) FSb[ ( RK[7] >> 8 ) & 0xFF ] ) ^ + ( (uint32_t) FSb[ ( RK[7] >> 16 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) FSb[ ( RK[7] >> 24 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) FSb[ ( RK[7] ) & 0xFF ] << 24 ); + + RK[9] = RK[1] ^ RK[8]; + RK[10] = RK[2] ^ RK[9]; + RK[11] = RK[3] ^ RK[10]; + + RK[12] = RK[4] ^ + ( (uint32_t) FSb[ ( RK[11] ) & 0xFF ] ) ^ + ( (uint32_t) FSb[ ( RK[11] >> 8 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) FSb[ ( RK[11] >> 16 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) FSb[ ( RK[11] >> 24 ) & 0xFF ] << 24 ); + + RK[13] = RK[5] ^ RK[12]; + RK[14] = RK[6] ^ RK[13]; + RK[15] = RK[7] ^ RK[14]; + } + break; + } + + return( 0 ); +} +#endif /* !MBEDTLS_AES_SETKEY_ENC_ALT */ + +/* + * AES key schedule (decryption) + */ +#if !defined(MBEDTLS_AES_SETKEY_DEC_ALT) +int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, + unsigned int keybits ) +{ + int i, j, ret; + mbedtls_aes_context cty; + uint32_t *RK; + uint32_t *SK; + + AES_VALIDATE_RET( ctx != NULL ); + AES_VALIDATE_RET( key != NULL ); + + mbedtls_aes_init( &cty ); + +#if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_PADLOCK_ALIGN16) + if( aes_padlock_ace == -1 ) + aes_padlock_ace = mbedtls_padlock_has_support( MBEDTLS_PADLOCK_ACE ); + + if( aes_padlock_ace ) + ctx->rk = RK = MBEDTLS_PADLOCK_ALIGN16( ctx->buf ); + else +#endif + ctx->rk = RK = ctx->buf; + + /* Also checks keybits */ + if( ( ret = mbedtls_aes_setkey_enc( &cty, key, keybits ) ) != 0 ) + goto exit; + + ctx->nr = cty.nr; + +#if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64) + if( mbedtls_aesni_has_support( MBEDTLS_AESNI_AES ) ) + { + mbedtls_aesni_inverse_key( (unsigned char *) ctx->rk, + (const unsigned char *) cty.rk, ctx->nr ); + goto exit; + } +#endif + + SK = cty.rk + cty.nr * 4; + + *RK++ = *SK++; + *RK++ = *SK++; + *RK++ = *SK++; + *RK++ = *SK++; + + for( i = ctx->nr - 1, SK -= 8; i > 0; i--, SK -= 8 ) + { + for( j = 0; j < 4; j++, SK++ ) + { + *RK++ = AES_RT0( FSb[ ( *SK ) & 0xFF ] ) ^ + AES_RT1( FSb[ ( *SK >> 8 ) & 0xFF ] ) ^ + AES_RT2( FSb[ ( *SK >> 16 ) & 0xFF ] ) ^ + AES_RT3( FSb[ ( *SK >> 24 ) & 0xFF ] ); + } + } + + *RK++ = *SK++; + *RK++ = *SK++; + *RK++ = *SK++; + *RK++ = *SK++; + +exit: + mbedtls_aes_free( &cty ); + + return( ret ); +} +#endif /* !MBEDTLS_AES_SETKEY_DEC_ALT */ + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +static int mbedtls_aes_xts_decode_keys( const unsigned char *key, + unsigned int keybits, + const unsigned char **key1, + unsigned int *key1bits, + const unsigned char **key2, + unsigned int *key2bits ) +{ + const unsigned int half_keybits = keybits / 2; + const unsigned int half_keybytes = half_keybits / 8; + + switch( keybits ) + { + case 256: break; + case 512: break; + default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH ); + } + + *key1bits = half_keybits; + *key2bits = half_keybits; + *key1 = &key[0]; + *key2 = &key[half_keybytes]; + + return 0; +} + +int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx, + const unsigned char *key, + unsigned int keybits) +{ + int ret; + const unsigned char *key1, *key2; + unsigned int key1bits, key2bits; + + AES_VALIDATE_RET( ctx != NULL ); + AES_VALIDATE_RET( key != NULL ); + + ret = mbedtls_aes_xts_decode_keys( key, keybits, &key1, &key1bits, + &key2, &key2bits ); + if( ret != 0 ) + return( ret ); + + /* Set the tweak key. Always set tweak key for the encryption mode. */ + ret = mbedtls_aes_setkey_enc( &ctx->tweak, key2, key2bits ); + if( ret != 0 ) + return( ret ); + + /* Set crypt key for encryption. */ + return mbedtls_aes_setkey_enc( &ctx->crypt, key1, key1bits ); +} + +int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx, + const unsigned char *key, + unsigned int keybits) +{ + int ret; + const unsigned char *key1, *key2; + unsigned int key1bits, key2bits; + + AES_VALIDATE_RET( ctx != NULL ); + AES_VALIDATE_RET( key != NULL ); + + ret = mbedtls_aes_xts_decode_keys( key, keybits, &key1, &key1bits, + &key2, &key2bits ); + if( ret != 0 ) + return( ret ); + + /* Set the tweak key. Always set tweak key for encryption. */ + ret = mbedtls_aes_setkey_enc( &ctx->tweak, key2, key2bits ); + if( ret != 0 ) + return( ret ); + + /* Set crypt key for decryption. */ + return mbedtls_aes_setkey_dec( &ctx->crypt, key1, key1bits ); +} +#endif /* MBEDTLS_CIPHER_MODE_XTS */ + +#define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ + do \ + { \ + (X0) = *RK++ ^ AES_FT0( ( (Y0) ) & 0xFF ) ^ \ + AES_FT1( ( (Y1) >> 8 ) & 0xFF ) ^ \ + AES_FT2( ( (Y2) >> 16 ) & 0xFF ) ^ \ + AES_FT3( ( (Y3) >> 24 ) & 0xFF ); \ + \ + (X1) = *RK++ ^ AES_FT0( ( (Y1) ) & 0xFF ) ^ \ + AES_FT1( ( (Y2) >> 8 ) & 0xFF ) ^ \ + AES_FT2( ( (Y3) >> 16 ) & 0xFF ) ^ \ + AES_FT3( ( (Y0) >> 24 ) & 0xFF ); \ + \ + (X2) = *RK++ ^ AES_FT0( ( (Y2) ) & 0xFF ) ^ \ + AES_FT1( ( (Y3) >> 8 ) & 0xFF ) ^ \ + AES_FT2( ( (Y0) >> 16 ) & 0xFF ) ^ \ + AES_FT3( ( (Y1) >> 24 ) & 0xFF ); \ + \ + (X3) = *RK++ ^ AES_FT0( ( (Y3) ) & 0xFF ) ^ \ + AES_FT1( ( (Y0) >> 8 ) & 0xFF ) ^ \ + AES_FT2( ( (Y1) >> 16 ) & 0xFF ) ^ \ + AES_FT3( ( (Y2) >> 24 ) & 0xFF ); \ + } while( 0 ) + +#define AES_RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ + do \ + { \ + (X0) = *RK++ ^ AES_RT0( ( (Y0) ) & 0xFF ) ^ \ + AES_RT1( ( (Y3) >> 8 ) & 0xFF ) ^ \ + AES_RT2( ( (Y2) >> 16 ) & 0xFF ) ^ \ + AES_RT3( ( (Y1) >> 24 ) & 0xFF ); \ + \ + (X1) = *RK++ ^ AES_RT0( ( (Y1) ) & 0xFF ) ^ \ + AES_RT1( ( (Y0) >> 8 ) & 0xFF ) ^ \ + AES_RT2( ( (Y3) >> 16 ) & 0xFF ) ^ \ + AES_RT3( ( (Y2) >> 24 ) & 0xFF ); \ + \ + (X2) = *RK++ ^ AES_RT0( ( (Y2) ) & 0xFF ) ^ \ + AES_RT1( ( (Y1) >> 8 ) & 0xFF ) ^ \ + AES_RT2( ( (Y0) >> 16 ) & 0xFF ) ^ \ + AES_RT3( ( (Y3) >> 24 ) & 0xFF ); \ + \ + (X3) = *RK++ ^ AES_RT0( ( (Y3) ) & 0xFF ) ^ \ + AES_RT1( ( (Y2) >> 8 ) & 0xFF ) ^ \ + AES_RT2( ( (Y1) >> 16 ) & 0xFF ) ^ \ + AES_RT3( ( (Y0) >> 24 ) & 0xFF ); \ + } while( 0 ) + +/* + * AES-ECB block encryption + */ +#if !defined(MBEDTLS_AES_ENCRYPT_ALT) +int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ) +{ + int i; + uint32_t *RK = ctx->rk; + struct + { + uint32_t X[4]; + uint32_t Y[4]; + } t; + + GET_UINT32_LE( t.X[0], input, 0 ); t.X[0] ^= *RK++; + GET_UINT32_LE( t.X[1], input, 4 ); t.X[1] ^= *RK++; + GET_UINT32_LE( t.X[2], input, 8 ); t.X[2] ^= *RK++; + GET_UINT32_LE( t.X[3], input, 12 ); t.X[3] ^= *RK++; + + for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- ) + { + AES_FROUND( t.Y[0], t.Y[1], t.Y[2], t.Y[3], t.X[0], t.X[1], t.X[2], t.X[3] ); + AES_FROUND( t.X[0], t.X[1], t.X[2], t.X[3], t.Y[0], t.Y[1], t.Y[2], t.Y[3] ); + } + + AES_FROUND( t.Y[0], t.Y[1], t.Y[2], t.Y[3], t.X[0], t.X[1], t.X[2], t.X[3] ); + + t.X[0] = *RK++ ^ \ + ( (uint32_t) FSb[ ( t.Y[0] ) & 0xFF ] ) ^ + ( (uint32_t) FSb[ ( t.Y[1] >> 8 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) FSb[ ( t.Y[2] >> 16 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) FSb[ ( t.Y[3] >> 24 ) & 0xFF ] << 24 ); + + t.X[1] = *RK++ ^ \ + ( (uint32_t) FSb[ ( t.Y[1] ) & 0xFF ] ) ^ + ( (uint32_t) FSb[ ( t.Y[2] >> 8 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) FSb[ ( t.Y[3] >> 16 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) FSb[ ( t.Y[0] >> 24 ) & 0xFF ] << 24 ); + + t.X[2] = *RK++ ^ \ + ( (uint32_t) FSb[ ( t.Y[2] ) & 0xFF ] ) ^ + ( (uint32_t) FSb[ ( t.Y[3] >> 8 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) FSb[ ( t.Y[0] >> 16 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) FSb[ ( t.Y[1] >> 24 ) & 0xFF ] << 24 ); + + t.X[3] = *RK++ ^ \ + ( (uint32_t) FSb[ ( t.Y[3] ) & 0xFF ] ) ^ + ( (uint32_t) FSb[ ( t.Y[0] >> 8 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) FSb[ ( t.Y[1] >> 16 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) FSb[ ( t.Y[2] >> 24 ) & 0xFF ] << 24 ); + + PUT_UINT32_LE( t.X[0], output, 0 ); + PUT_UINT32_LE( t.X[1], output, 4 ); + PUT_UINT32_LE( t.X[2], output, 8 ); + PUT_UINT32_LE( t.X[3], output, 12 ); + + mbedtls_platform_zeroize( &t, sizeof( t ) ); + + return( 0 ); +} +#endif /* !MBEDTLS_AES_ENCRYPT_ALT */ + +#if !defined(MBEDTLS_DEPRECATED_REMOVED) +void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ) +{ + mbedtls_internal_aes_encrypt( ctx, input, output ); +} +#endif /* !MBEDTLS_DEPRECATED_REMOVED */ + +/* + * AES-ECB block decryption + */ +#if !defined(MBEDTLS_AES_DECRYPT_ALT) +int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ) +{ + int i; + uint32_t *RK = ctx->rk; + struct + { + uint32_t X[4]; + uint32_t Y[4]; + } t; + + GET_UINT32_LE( t.X[0], input, 0 ); t.X[0] ^= *RK++; + GET_UINT32_LE( t.X[1], input, 4 ); t.X[1] ^= *RK++; + GET_UINT32_LE( t.X[2], input, 8 ); t.X[2] ^= *RK++; + GET_UINT32_LE( t.X[3], input, 12 ); t.X[3] ^= *RK++; + + for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- ) + { + AES_RROUND( t.Y[0], t.Y[1], t.Y[2], t.Y[3], t.X[0], t.X[1], t.X[2], t.X[3] ); + AES_RROUND( t.X[0], t.X[1], t.X[2], t.X[3], t.Y[0], t.Y[1], t.Y[2], t.Y[3] ); + } + + AES_RROUND( t.Y[0], t.Y[1], t.Y[2], t.Y[3], t.X[0], t.X[1], t.X[2], t.X[3] ); + + t.X[0] = *RK++ ^ \ + ( (uint32_t) RSb[ ( t.Y[0] ) & 0xFF ] ) ^ + ( (uint32_t) RSb[ ( t.Y[3] >> 8 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) RSb[ ( t.Y[2] >> 16 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) RSb[ ( t.Y[1] >> 24 ) & 0xFF ] << 24 ); + + t.X[1] = *RK++ ^ \ + ( (uint32_t) RSb[ ( t.Y[1] ) & 0xFF ] ) ^ + ( (uint32_t) RSb[ ( t.Y[0] >> 8 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) RSb[ ( t.Y[3] >> 16 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) RSb[ ( t.Y[2] >> 24 ) & 0xFF ] << 24 ); + + t.X[2] = *RK++ ^ \ + ( (uint32_t) RSb[ ( t.Y[2] ) & 0xFF ] ) ^ + ( (uint32_t) RSb[ ( t.Y[1] >> 8 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) RSb[ ( t.Y[0] >> 16 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) RSb[ ( t.Y[3] >> 24 ) & 0xFF ] << 24 ); + + t.X[3] = *RK++ ^ \ + ( (uint32_t) RSb[ ( t.Y[3] ) & 0xFF ] ) ^ + ( (uint32_t) RSb[ ( t.Y[2] >> 8 ) & 0xFF ] << 8 ) ^ + ( (uint32_t) RSb[ ( t.Y[1] >> 16 ) & 0xFF ] << 16 ) ^ + ( (uint32_t) RSb[ ( t.Y[0] >> 24 ) & 0xFF ] << 24 ); + + PUT_UINT32_LE( t.X[0], output, 0 ); + PUT_UINT32_LE( t.X[1], output, 4 ); + PUT_UINT32_LE( t.X[2], output, 8 ); + PUT_UINT32_LE( t.X[3], output, 12 ); + + mbedtls_platform_zeroize( &t, sizeof( t ) ); + + return( 0 ); +} +#endif /* !MBEDTLS_AES_DECRYPT_ALT */ + +#if !defined(MBEDTLS_DEPRECATED_REMOVED) +void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ) +{ + mbedtls_internal_aes_decrypt( ctx, input, output ); +} +#endif /* !MBEDTLS_DEPRECATED_REMOVED */ + +/* + * AES-ECB block encryption/decryption + */ +int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, + int mode, + const unsigned char input[16], + unsigned char output[16] ) +{ + AES_VALIDATE_RET( ctx != NULL ); + AES_VALIDATE_RET( input != NULL ); + AES_VALIDATE_RET( output != NULL ); + AES_VALIDATE_RET( mode == MBEDTLS_AES_ENCRYPT || + mode == MBEDTLS_AES_DECRYPT ); + +#if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64) + if( mbedtls_aesni_has_support( MBEDTLS_AESNI_AES ) ) + return( mbedtls_aesni_crypt_ecb( ctx, mode, input, output ) ); +#endif + +#if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_HAVE_X86) + if( aes_padlock_ace ) + { + if( mbedtls_padlock_xcryptecb( ctx, mode, input, output ) == 0 ) + return( 0 ); + + // If padlock data misaligned, we just fall back to + // unaccelerated mode + // + } +#endif + + if( mode == MBEDTLS_AES_ENCRYPT ) + return( mbedtls_internal_aes_encrypt( ctx, input, output ) ); + else + return( mbedtls_internal_aes_decrypt( ctx, input, output ) ); +} + +#if defined(MBEDTLS_CIPHER_MODE_CBC) +/* + * AES-CBC buffer encryption/decryption + */ +int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, + int mode, + size_t length, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ) +{ + int i; + int ret; + unsigned char temp[16]; + + AES_VALIDATE_RET( ctx != NULL ); + AES_VALIDATE_RET( mode == MBEDTLS_AES_ENCRYPT || + mode == MBEDTLS_AES_DECRYPT ); + AES_VALIDATE_RET( iv != NULL ); + AES_VALIDATE_RET( input != NULL ); + AES_VALIDATE_RET( output != NULL ); + + if( length % 16 ) + return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); + +#if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_HAVE_X86) + if( aes_padlock_ace ) + { + if( mbedtls_padlock_xcryptcbc( ctx, mode, length, iv, input, output ) == 0 ) + return( 0 ); + + // If padlock data misaligned, we just fall back to + // unaccelerated mode + // + } +#endif + + if( mode == MBEDTLS_AES_DECRYPT ) + { + while( length > 0 ) + { + memcpy( temp, input, 16 ); + ret = mbedtls_aes_crypt_ecb( ctx, mode, input, output ); + if( ret != 0 ) + goto exit; + + for( i = 0; i < 16; i++ ) + output[i] = (unsigned char)( output[i] ^ iv[i] ); + + memcpy( iv, temp, 16 ); + + input += 16; + output += 16; + length -= 16; + } + } + else + { + while( length > 0 ) + { + for( i = 0; i < 16; i++ ) + output[i] = (unsigned char)( input[i] ^ iv[i] ); + + ret = mbedtls_aes_crypt_ecb( ctx, mode, output, output ); + if( ret != 0 ) + goto exit; + memcpy( iv, output, 16 ); + + input += 16; + output += 16; + length -= 16; + } + } + ret = 0; + +exit: + return( ret ); +} +#endif /* MBEDTLS_CIPHER_MODE_CBC */ + +#if defined(MBEDTLS_CIPHER_MODE_XTS) + +/* Endianess with 64 bits values */ +#ifndef GET_UINT64_LE +#define GET_UINT64_LE(n,b,i) \ +{ \ + (n) = ( (uint64_t) (b)[(i) + 7] << 56 ) \ + | ( (uint64_t) (b)[(i) + 6] << 48 ) \ + | ( (uint64_t) (b)[(i) + 5] << 40 ) \ + | ( (uint64_t) (b)[(i) + 4] << 32 ) \ + | ( (uint64_t) (b)[(i) + 3] << 24 ) \ + | ( (uint64_t) (b)[(i) + 2] << 16 ) \ + | ( (uint64_t) (b)[(i) + 1] << 8 ) \ + | ( (uint64_t) (b)[(i) ] ); \ +} +#endif + +#ifndef PUT_UINT64_LE +#define PUT_UINT64_LE(n,b,i) \ +{ \ + (b)[(i) + 7] = (unsigned char) ( (n) >> 56 ); \ + (b)[(i) + 6] = (unsigned char) ( (n) >> 48 ); \ + (b)[(i) + 5] = (unsigned char) ( (n) >> 40 ); \ + (b)[(i) + 4] = (unsigned char) ( (n) >> 32 ); \ + (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \ + (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \ + (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \ + (b)[(i) ] = (unsigned char) ( (n) ); \ +} +#endif + +typedef unsigned char mbedtls_be128[16]; + +/* + * GF(2^128) multiplication function + * + * This function multiplies a field element by x in the polynomial field + * representation. It uses 64-bit word operations to gain speed but compensates + * for machine endianess and hence works correctly on both big and little + * endian machines. + */ +static void mbedtls_gf128mul_x_ble( unsigned char r[16], + const unsigned char x[16] ) +{ + uint64_t a, b, ra, rb; + + GET_UINT64_LE( a, x, 0 ); + GET_UINT64_LE( b, x, 8 ); + + ra = ( a << 1 ) ^ 0x0087 >> ( 8 - ( ( b >> 63 ) << 3 ) ); + rb = ( a >> 63 ) | ( b << 1 ); + + PUT_UINT64_LE( ra, r, 0 ); + PUT_UINT64_LE( rb, r, 8 ); +} + +/* + * AES-XTS buffer encryption/decryption + */ +int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx, + int mode, + size_t length, + const unsigned char data_unit[16], + const unsigned char *input, + unsigned char *output ) +{ + int ret; + size_t blocks = length / 16; + size_t leftover = length % 16; + unsigned char tweak[16]; + unsigned char prev_tweak[16]; + unsigned char tmp[16]; + + AES_VALIDATE_RET( ctx != NULL ); + AES_VALIDATE_RET( mode == MBEDTLS_AES_ENCRYPT || + mode == MBEDTLS_AES_DECRYPT ); + AES_VALIDATE_RET( data_unit != NULL ); + AES_VALIDATE_RET( input != NULL ); + AES_VALIDATE_RET( output != NULL ); + + /* Data units must be at least 16 bytes long. */ + if( length < 16 ) + return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH; + + /* NIST SP 800-38E disallows data units larger than 2**20 blocks. */ + if( length > ( 1 << 20 ) * 16 ) + return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH; + + /* Compute the tweak. */ + ret = mbedtls_aes_crypt_ecb( &ctx->tweak, MBEDTLS_AES_ENCRYPT, + data_unit, tweak ); + if( ret != 0 ) + return( ret ); + + while( blocks-- ) + { + size_t i; + + if( leftover && ( mode == MBEDTLS_AES_DECRYPT ) && blocks == 0 ) + { + /* We are on the last block in a decrypt operation that has + * leftover bytes, so we need to use the next tweak for this block, + * and this tweak for the lefover bytes. Save the current tweak for + * the leftovers and then update the current tweak for use on this, + * the last full block. */ + memcpy( prev_tweak, tweak, sizeof( tweak ) ); + mbedtls_gf128mul_x_ble( tweak, tweak ); + } + + for( i = 0; i < 16; i++ ) + tmp[i] = input[i] ^ tweak[i]; + + ret = mbedtls_aes_crypt_ecb( &ctx->crypt, mode, tmp, tmp ); + if( ret != 0 ) + return( ret ); + + for( i = 0; i < 16; i++ ) + output[i] = tmp[i] ^ tweak[i]; + + /* Update the tweak for the next block. */ + mbedtls_gf128mul_x_ble( tweak, tweak ); + + output += 16; + input += 16; + } + + if( leftover ) + { + /* If we are on the leftover bytes in a decrypt operation, we need to + * use the previous tweak for these bytes (as saved in prev_tweak). */ + unsigned char *t = mode == MBEDTLS_AES_DECRYPT ? prev_tweak : tweak; + + /* We are now on the final part of the data unit, which doesn't divide + * evenly by 16. It's time for ciphertext stealing. */ + size_t i; + unsigned char *prev_output = output - 16; + + /* Copy ciphertext bytes from the previous block to our output for each + * byte of cyphertext we won't steal. At the same time, copy the + * remainder of the input for this final round (since the loop bounds + * are the same). */ + for( i = 0; i < leftover; i++ ) + { + output[i] = prev_output[i]; + tmp[i] = input[i] ^ t[i]; + } + + /* Copy ciphertext bytes from the previous block for input in this + * round. */ + for( ; i < 16; i++ ) + tmp[i] = prev_output[i] ^ t[i]; + + ret = mbedtls_aes_crypt_ecb( &ctx->crypt, mode, tmp, tmp ); + if( ret != 0 ) + return ret; + + /* Write the result back to the previous block, overriding the previous + * output we copied. */ + for( i = 0; i < 16; i++ ) + prev_output[i] = tmp[i] ^ t[i]; + } + + return( 0 ); +} +#endif /* MBEDTLS_CIPHER_MODE_XTS */ + +#if defined(MBEDTLS_CIPHER_MODE_CFB) +/* + * AES-CFB128 buffer encryption/decryption + */ +int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, + int mode, + size_t length, + size_t *iv_off, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ) +{ + int c; + int ret; + size_t n; + + AES_VALIDATE_RET( ctx != NULL ); + AES_VALIDATE_RET( mode == MBEDTLS_AES_ENCRYPT || + mode == MBEDTLS_AES_DECRYPT ); + AES_VALIDATE_RET( iv_off != NULL ); + AES_VALIDATE_RET( iv != NULL ); + AES_VALIDATE_RET( input != NULL ); + AES_VALIDATE_RET( output != NULL ); + + n = *iv_off; + + if( n > 15 ) + return( MBEDTLS_ERR_AES_BAD_INPUT_DATA ); + + if( mode == MBEDTLS_AES_DECRYPT ) + { + while( length-- ) + { + if( n == 0 ) + { + ret = mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ); + if( ret != 0 ) + goto exit; + } + + c = *input++; + *output++ = (unsigned char)( c ^ iv[n] ); + iv[n] = (unsigned char) c; + + n = ( n + 1 ) & 0x0F; + } + } + else + { + while( length-- ) + { + if( n == 0 ) + { + ret = mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ); + if( ret != 0 ) + goto exit; + } + + iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ ); + + n = ( n + 1 ) & 0x0F; + } + } + + *iv_off = n; + ret = 0; + +exit: + return( ret ); +} + +/* + * AES-CFB8 buffer encryption/decryption + */ +int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, + int mode, + size_t length, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ) +{ + int ret; + unsigned char c; + unsigned char ov[17]; + + AES_VALIDATE_RET( ctx != NULL ); + AES_VALIDATE_RET( mode == MBEDTLS_AES_ENCRYPT || + mode == MBEDTLS_AES_DECRYPT ); + AES_VALIDATE_RET( iv != NULL ); + AES_VALIDATE_RET( input != NULL ); + AES_VALIDATE_RET( output != NULL ); + while( length-- ) + { + memcpy( ov, iv, 16 ); + ret = mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ); + if( ret != 0 ) + goto exit; + + if( mode == MBEDTLS_AES_DECRYPT ) + ov[16] = *input; + + c = *output++ = (unsigned char)( iv[0] ^ *input++ ); + + if( mode == MBEDTLS_AES_ENCRYPT ) + ov[16] = c; + + memcpy( iv, ov + 1, 16 ); + } + ret = 0; + +exit: + return( ret ); +} +#endif /* MBEDTLS_CIPHER_MODE_CFB */ + +#if defined(MBEDTLS_CIPHER_MODE_OFB) +/* + * AES-OFB (Output Feedback Mode) buffer encryption/decryption + */ +int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx, + size_t length, + size_t *iv_off, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ) +{ + int ret = 0; + size_t n; + + AES_VALIDATE_RET( ctx != NULL ); + AES_VALIDATE_RET( iv_off != NULL ); + AES_VALIDATE_RET( iv != NULL ); + AES_VALIDATE_RET( input != NULL ); + AES_VALIDATE_RET( output != NULL ); + + n = *iv_off; + + if( n > 15 ) + return( MBEDTLS_ERR_AES_BAD_INPUT_DATA ); + + while( length-- ) + { + if( n == 0 ) + { + ret = mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ); + if( ret != 0 ) + goto exit; + } + *output++ = *input++ ^ iv[n]; + + n = ( n + 1 ) & 0x0F; + } + + *iv_off = n; + +exit: + return( ret ); +} +#endif /* MBEDTLS_CIPHER_MODE_OFB */ + +#if defined(MBEDTLS_CIPHER_MODE_CTR) +/* + * AES-CTR buffer encryption/decryption + */ +int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, + size_t length, + size_t *nc_off, + unsigned char nonce_counter[16], + unsigned char stream_block[16], + const unsigned char *input, + unsigned char *output ) +{ + int c, i; + int ret; + size_t n; + + AES_VALIDATE_RET( ctx != NULL ); + AES_VALIDATE_RET( nc_off != NULL ); + AES_VALIDATE_RET( nonce_counter != NULL ); + AES_VALIDATE_RET( stream_block != NULL ); + AES_VALIDATE_RET( input != NULL ); + AES_VALIDATE_RET( output != NULL ); + + n = *nc_off; + + if ( n > 0x0F ) + return( MBEDTLS_ERR_AES_BAD_INPUT_DATA ); + + while( length-- ) + { + if( n == 0 ) { + ret = mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block ); + if( ret != 0 ) + goto exit; + + for( i = 16; i > 0; i-- ) + if( ++nonce_counter[i - 1] != 0 ) + break; + } + c = *input++; + *output++ = (unsigned char)( c ^ stream_block[n] ); + + n = ( n + 1 ) & 0x0F; + } + + *nc_off = n; + ret = 0; + +exit: + return( ret ); +} +#endif /* MBEDTLS_CIPHER_MODE_CTR */ + +#endif /* !MBEDTLS_AES_ALT */ + +#if defined(MBEDTLS_SELF_TEST) +/* + * AES test vectors from: + * + * http://csrc.nist.gov/archive/aes/rijndael/rijndael-vals.zip + */ +static const unsigned char aes_test_ecb_dec[3][16] = +{ + { 0x44, 0x41, 0x6A, 0xC2, 0xD1, 0xF5, 0x3C, 0x58, + 0x33, 0x03, 0x91, 0x7E, 0x6B, 0xE9, 0xEB, 0xE0 }, + { 0x48, 0xE3, 0x1E, 0x9E, 0x25, 0x67, 0x18, 0xF2, + 0x92, 0x29, 0x31, 0x9C, 0x19, 0xF1, 0x5B, 0xA4 }, + { 0x05, 0x8C, 0xCF, 0xFD, 0xBB, 0xCB, 0x38, 0x2D, + 0x1F, 0x6F, 0x56, 0x58, 0x5D, 0x8A, 0x4A, 0xDE } +}; + +static const unsigned char aes_test_ecb_enc[3][16] = +{ + { 0xC3, 0x4C, 0x05, 0x2C, 0xC0, 0xDA, 0x8D, 0x73, + 0x45, 0x1A, 0xFE, 0x5F, 0x03, 0xBE, 0x29, 0x7F }, + { 0xF3, 0xF6, 0x75, 0x2A, 0xE8, 0xD7, 0x83, 0x11, + 0x38, 0xF0, 0x41, 0x56, 0x06, 0x31, 0xB1, 0x14 }, + { 0x8B, 0x79, 0xEE, 0xCC, 0x93, 0xA0, 0xEE, 0x5D, + 0xFF, 0x30, 0xB4, 0xEA, 0x21, 0x63, 0x6D, 0xA4 } +}; + +#if defined(MBEDTLS_CIPHER_MODE_CBC) +static const unsigned char aes_test_cbc_dec[3][16] = +{ + { 0xFA, 0xCA, 0x37, 0xE0, 0xB0, 0xC8, 0x53, 0x73, + 0xDF, 0x70, 0x6E, 0x73, 0xF7, 0xC9, 0xAF, 0x86 }, + { 0x5D, 0xF6, 0x78, 0xDD, 0x17, 0xBA, 0x4E, 0x75, + 0xB6, 0x17, 0x68, 0xC6, 0xAD, 0xEF, 0x7C, 0x7B }, + { 0x48, 0x04, 0xE1, 0x81, 0x8F, 0xE6, 0x29, 0x75, + 0x19, 0xA3, 0xE8, 0x8C, 0x57, 0x31, 0x04, 0x13 } +}; + +static const unsigned char aes_test_cbc_enc[3][16] = +{ + { 0x8A, 0x05, 0xFC, 0x5E, 0x09, 0x5A, 0xF4, 0x84, + 0x8A, 0x08, 0xD3, 0x28, 0xD3, 0x68, 0x8E, 0x3D }, + { 0x7B, 0xD9, 0x66, 0xD5, 0x3A, 0xD8, 0xC1, 0xBB, + 0x85, 0xD2, 0xAD, 0xFA, 0xE8, 0x7B, 0xB1, 0x04 }, + { 0xFE, 0x3C, 0x53, 0x65, 0x3E, 0x2F, 0x45, 0xB5, + 0x6F, 0xCD, 0x88, 0xB2, 0xCC, 0x89, 0x8F, 0xF0 } +}; +#endif /* MBEDTLS_CIPHER_MODE_CBC */ + +#if defined(MBEDTLS_CIPHER_MODE_CFB) +/* + * AES-CFB128 test vectors from: + * + * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf + */ +static const unsigned char aes_test_cfb128_key[3][32] = +{ + { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, + 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }, + { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52, + 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5, + 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B }, + { 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE, + 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81, + 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7, + 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4 } +}; + +static const unsigned char aes_test_cfb128_iv[16] = +{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F +}; + +static const unsigned char aes_test_cfb128_pt[64] = +{ + 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, + 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A, + 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C, + 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51, + 0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11, + 0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF, + 0xF6, 0x9F, 0x24, 0x45, 0xDF, 0x4F, 0x9B, 0x17, + 0xAD, 0x2B, 0x41, 0x7B, 0xE6, 0x6C, 0x37, 0x10 +}; + +static const unsigned char aes_test_cfb128_ct[3][64] = +{ + { 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, + 0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A, + 0xC8, 0xA6, 0x45, 0x37, 0xA0, 0xB3, 0xA9, 0x3F, + 0xCD, 0xE3, 0xCD, 0xAD, 0x9F, 0x1C, 0xE5, 0x8B, + 0x26, 0x75, 0x1F, 0x67, 0xA3, 0xCB, 0xB1, 0x40, + 0xB1, 0x80, 0x8C, 0xF1, 0x87, 0xA4, 0xF4, 0xDF, + 0xC0, 0x4B, 0x05, 0x35, 0x7C, 0x5D, 0x1C, 0x0E, + 0xEA, 0xC4, 0xC6, 0x6F, 0x9F, 0xF7, 0xF2, 0xE6 }, + { 0xCD, 0xC8, 0x0D, 0x6F, 0xDD, 0xF1, 0x8C, 0xAB, + 0x34, 0xC2, 0x59, 0x09, 0xC9, 0x9A, 0x41, 0x74, + 0x67, 0xCE, 0x7F, 0x7F, 0x81, 0x17, 0x36, 0x21, + 0x96, 0x1A, 0x2B, 0x70, 0x17, 0x1D, 0x3D, 0x7A, + 0x2E, 0x1E, 0x8A, 0x1D, 0xD5, 0x9B, 0x88, 0xB1, + 0xC8, 0xE6, 0x0F, 0xED, 0x1E, 0xFA, 0xC4, 0xC9, + 0xC0, 0x5F, 0x9F, 0x9C, 0xA9, 0x83, 0x4F, 0xA0, + 0x42, 0xAE, 0x8F, 0xBA, 0x58, 0x4B, 0x09, 0xFF }, + { 0xDC, 0x7E, 0x84, 0xBF, 0xDA, 0x79, 0x16, 0x4B, + 0x7E, 0xCD, 0x84, 0x86, 0x98, 0x5D, 0x38, 0x60, + 0x39, 0xFF, 0xED, 0x14, 0x3B, 0x28, 0xB1, 0xC8, + 0x32, 0x11, 0x3C, 0x63, 0x31, 0xE5, 0x40, 0x7B, + 0xDF, 0x10, 0x13, 0x24, 0x15, 0xE5, 0x4B, 0x92, + 0xA1, 0x3E, 0xD0, 0xA8, 0x26, 0x7A, 0xE2, 0xF9, + 0x75, 0xA3, 0x85, 0x74, 0x1A, 0xB9, 0xCE, 0xF8, + 0x20, 0x31, 0x62, 0x3D, 0x55, 0xB1, 0xE4, 0x71 } +}; +#endif /* MBEDTLS_CIPHER_MODE_CFB */ + +#if defined(MBEDTLS_CIPHER_MODE_OFB) +/* + * AES-OFB test vectors from: + * + * https://csrc.nist.gov/publications/detail/sp/800-38a/final + */ +static const unsigned char aes_test_ofb_key[3][32] = +{ + { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, + 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }, + { 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52, + 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5, + 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B }, + { 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE, + 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81, + 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7, + 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4 } +}; + +static const unsigned char aes_test_ofb_iv[16] = +{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F +}; + +static const unsigned char aes_test_ofb_pt[64] = +{ + 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, + 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A, + 0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C, + 0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51, + 0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11, + 0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF, + 0xF6, 0x9F, 0x24, 0x45, 0xDF, 0x4F, 0x9B, 0x17, + 0xAD, 0x2B, 0x41, 0x7B, 0xE6, 0x6C, 0x37, 0x10 +}; + +static const unsigned char aes_test_ofb_ct[3][64] = +{ + { 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, + 0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A, + 0x77, 0x89, 0x50, 0x8d, 0x16, 0x91, 0x8f, 0x03, + 0xf5, 0x3c, 0x52, 0xda, 0xc5, 0x4e, 0xd8, 0x25, + 0x97, 0x40, 0x05, 0x1e, 0x9c, 0x5f, 0xec, 0xf6, + 0x43, 0x44, 0xf7, 0xa8, 0x22, 0x60, 0xed, 0xcc, + 0x30, 0x4c, 0x65, 0x28, 0xf6, 0x59, 0xc7, 0x78, + 0x66, 0xa5, 0x10, 0xd9, 0xc1, 0xd6, 0xae, 0x5e }, + { 0xCD, 0xC8, 0x0D, 0x6F, 0xDD, 0xF1, 0x8C, 0xAB, + 0x34, 0xC2, 0x59, 0x09, 0xC9, 0x9A, 0x41, 0x74, + 0xfc, 0xc2, 0x8b, 0x8d, 0x4c, 0x63, 0x83, 0x7c, + 0x09, 0xe8, 0x17, 0x00, 0xc1, 0x10, 0x04, 0x01, + 0x8d, 0x9a, 0x9a, 0xea, 0xc0, 0xf6, 0x59, 0x6f, + 0x55, 0x9c, 0x6d, 0x4d, 0xaf, 0x59, 0xa5, 0xf2, + 0x6d, 0x9f, 0x20, 0x08, 0x57, 0xca, 0x6c, 0x3e, + 0x9c, 0xac, 0x52, 0x4b, 0xd9, 0xac, 0xc9, 0x2a }, + { 0xDC, 0x7E, 0x84, 0xBF, 0xDA, 0x79, 0x16, 0x4B, + 0x7E, 0xCD, 0x84, 0x86, 0x98, 0x5D, 0x38, 0x60, + 0x4f, 0xeb, 0xdc, 0x67, 0x40, 0xd2, 0x0b, 0x3a, + 0xc8, 0x8f, 0x6a, 0xd8, 0x2a, 0x4f, 0xb0, 0x8d, + 0x71, 0xab, 0x47, 0xa0, 0x86, 0xe8, 0x6e, 0xed, + 0xf3, 0x9d, 0x1c, 0x5b, 0xba, 0x97, 0xc4, 0x08, + 0x01, 0x26, 0x14, 0x1d, 0x67, 0xf3, 0x7b, 0xe8, + 0x53, 0x8f, 0x5a, 0x8b, 0xe7, 0x40, 0xe4, 0x84 } +}; +#endif /* MBEDTLS_CIPHER_MODE_OFB */ + +#if defined(MBEDTLS_CIPHER_MODE_CTR) +/* + * AES-CTR test vectors from: + * + * http://www.faqs.org/rfcs/rfc3686.html + */ + +static const unsigned char aes_test_ctr_key[3][16] = +{ + { 0xAE, 0x68, 0x52, 0xF8, 0x12, 0x10, 0x67, 0xCC, + 0x4B, 0xF7, 0xA5, 0x76, 0x55, 0x77, 0xF3, 0x9E }, + { 0x7E, 0x24, 0x06, 0x78, 0x17, 0xFA, 0xE0, 0xD7, + 0x43, 0xD6, 0xCE, 0x1F, 0x32, 0x53, 0x91, 0x63 }, + { 0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8, + 0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC } +}; + +static const unsigned char aes_test_ctr_nonce_counter[3][16] = +{ + { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, + { 0x00, 0x6C, 0xB6, 0xDB, 0xC0, 0x54, 0x3B, 0x59, + 0xDA, 0x48, 0xD9, 0x0B, 0x00, 0x00, 0x00, 0x01 }, + { 0x00, 0xE0, 0x01, 0x7B, 0x27, 0x77, 0x7F, 0x3F, + 0x4A, 0x17, 0x86, 0xF0, 0x00, 0x00, 0x00, 0x01 } +}; + +static const unsigned char aes_test_ctr_pt[3][48] = +{ + { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62, + 0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 }, + + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F }, + + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, + 0x20, 0x21, 0x22, 0x23 } +}; + +static const unsigned char aes_test_ctr_ct[3][48] = +{ + { 0xE4, 0x09, 0x5D, 0x4F, 0xB7, 0xA7, 0xB3, 0x79, + 0x2D, 0x61, 0x75, 0xA3, 0x26, 0x13, 0x11, 0xB8 }, + { 0x51, 0x04, 0xA1, 0x06, 0x16, 0x8A, 0x72, 0xD9, + 0x79, 0x0D, 0x41, 0xEE, 0x8E, 0xDA, 0xD3, 0x88, + 0xEB, 0x2E, 0x1E, 0xFC, 0x46, 0xDA, 0x57, 0xC8, + 0xFC, 0xE6, 0x30, 0xDF, 0x91, 0x41, 0xBE, 0x28 }, + { 0xC1, 0xCF, 0x48, 0xA8, 0x9F, 0x2F, 0xFD, 0xD9, + 0xCF, 0x46, 0x52, 0xE9, 0xEF, 0xDB, 0x72, 0xD7, + 0x45, 0x40, 0xA4, 0x2B, 0xDE, 0x6D, 0x78, 0x36, + 0xD5, 0x9A, 0x5C, 0xEA, 0xAE, 0xF3, 0x10, 0x53, + 0x25, 0xB2, 0x07, 0x2F } +}; + +static const int aes_test_ctr_len[3] = + { 16, 32, 36 }; +#endif /* MBEDTLS_CIPHER_MODE_CTR */ + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +/* + * AES-XTS test vectors from: + * + * IEEE P1619/D16 Annex B + * https://web.archive.org/web/20150629024421/http://grouper.ieee.org/groups/1619/email/pdf00086.pdf + * (Archived from original at http://grouper.ieee.org/groups/1619/email/pdf00086.pdf) + */ +static const unsigned char aes_test_xts_key[][32] = +{ + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 }, + { 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, + 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0, + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 }, +}; + +static const unsigned char aes_test_xts_pt32[][32] = +{ + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, + 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, + 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, + 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44 }, + { 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, + 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, + 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, + 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44 }, +}; + +static const unsigned char aes_test_xts_ct32[][32] = +{ + { 0x91, 0x7c, 0xf6, 0x9e, 0xbd, 0x68, 0xb2, 0xec, + 0x9b, 0x9f, 0xe9, 0xa3, 0xea, 0xdd, 0xa6, 0x92, + 0xcd, 0x43, 0xd2, 0xf5, 0x95, 0x98, 0xed, 0x85, + 0x8c, 0x02, 0xc2, 0x65, 0x2f, 0xbf, 0x92, 0x2e }, + { 0xc4, 0x54, 0x18, 0x5e, 0x6a, 0x16, 0x93, 0x6e, + 0x39, 0x33, 0x40, 0x38, 0xac, 0xef, 0x83, 0x8b, + 0xfb, 0x18, 0x6f, 0xff, 0x74, 0x80, 0xad, 0xc4, + 0x28, 0x93, 0x82, 0xec, 0xd6, 0xd3, 0x94, 0xf0 }, + { 0xaf, 0x85, 0x33, 0x6b, 0x59, 0x7a, 0xfc, 0x1a, + 0x90, 0x0b, 0x2e, 0xb2, 0x1e, 0xc9, 0x49, 0xd2, + 0x92, 0xdf, 0x4c, 0x04, 0x7e, 0x0b, 0x21, 0x53, + 0x21, 0x86, 0xa5, 0x97, 0x1a, 0x22, 0x7a, 0x89 }, +}; + +static const unsigned char aes_test_xts_data_unit[][16] = +{ + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x33, 0x33, 0x33, 0x33, 0x33, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x33, 0x33, 0x33, 0x33, 0x33, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, +}; + +#endif /* MBEDTLS_CIPHER_MODE_XTS */ + +/* + * Checkup routine + */ +int mbedtls_aes_self_test( int verbose ) +{ + int ret = 0, i, j, u, mode; + unsigned int keybits; + unsigned char key[32]; + unsigned char buf[64]; + const unsigned char *aes_tests; +#if defined(MBEDTLS_CIPHER_MODE_CBC) || defined(MBEDTLS_CIPHER_MODE_CFB) + unsigned char iv[16]; +#endif +#if defined(MBEDTLS_CIPHER_MODE_CBC) + unsigned char prv[16]; +#endif +#if defined(MBEDTLS_CIPHER_MODE_CTR) || defined(MBEDTLS_CIPHER_MODE_CFB) || \ + defined(MBEDTLS_CIPHER_MODE_OFB) + size_t offset; +#endif +#if defined(MBEDTLS_CIPHER_MODE_CTR) || defined(MBEDTLS_CIPHER_MODE_XTS) + int len; +#endif +#if defined(MBEDTLS_CIPHER_MODE_CTR) + unsigned char nonce_counter[16]; + unsigned char stream_block[16]; +#endif + mbedtls_aes_context ctx; + + memset( key, 0, 32 ); + mbedtls_aes_init( &ctx ); + + /* + * ECB mode + */ + for( i = 0; i < 6; i++ ) + { + u = i >> 1; + keybits = 128 + u * 64; + mode = i & 1; + + if( verbose != 0 ) + mbedtls_printf( " AES-ECB-%3d (%s): ", keybits, + ( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" ); + + memset( buf, 0, 16 ); + + if( mode == MBEDTLS_AES_DECRYPT ) + { + ret = mbedtls_aes_setkey_dec( &ctx, key, keybits ); + aes_tests = aes_test_ecb_dec[u]; + } + else + { + ret = mbedtls_aes_setkey_enc( &ctx, key, keybits ); + aes_tests = aes_test_ecb_enc[u]; + } + + /* + * AES-192 is an optional feature that may be unavailable when + * there is an alternative underlying implementation i.e. when + * MBEDTLS_AES_ALT is defined. + */ + if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED && keybits == 192 ) + { + mbedtls_printf( "skipped\n" ); + continue; + } + else if( ret != 0 ) + { + goto exit; + } + + for( j = 0; j < 10000; j++ ) + { + ret = mbedtls_aes_crypt_ecb( &ctx, mode, buf, buf ); + if( ret != 0 ) + goto exit; + } + + if( memcmp( buf, aes_tests, 16 ) != 0 ) + { + ret = 1; + goto exit; + } + + if( verbose != 0 ) + mbedtls_printf( "passed\n" ); + } + + if( verbose != 0 ) + mbedtls_printf( "\n" ); + +#if defined(MBEDTLS_CIPHER_MODE_CBC) + /* + * CBC mode + */ + for( i = 0; i < 6; i++ ) + { + u = i >> 1; + keybits = 128 + u * 64; + mode = i & 1; + + if( verbose != 0 ) + mbedtls_printf( " AES-CBC-%3d (%s): ", keybits, + ( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" ); + + memset( iv , 0, 16 ); + memset( prv, 0, 16 ); + memset( buf, 0, 16 ); + + if( mode == MBEDTLS_AES_DECRYPT ) + { + ret = mbedtls_aes_setkey_dec( &ctx, key, keybits ); + aes_tests = aes_test_cbc_dec[u]; + } + else + { + ret = mbedtls_aes_setkey_enc( &ctx, key, keybits ); + aes_tests = aes_test_cbc_enc[u]; + } + + /* + * AES-192 is an optional feature that may be unavailable when + * there is an alternative underlying implementation i.e. when + * MBEDTLS_AES_ALT is defined. + */ + if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED && keybits == 192 ) + { + mbedtls_printf( "skipped\n" ); + continue; + } + else if( ret != 0 ) + { + goto exit; + } + + for( j = 0; j < 10000; j++ ) + { + if( mode == MBEDTLS_AES_ENCRYPT ) + { + unsigned char tmp[16]; + + memcpy( tmp, prv, 16 ); + memcpy( prv, buf, 16 ); + memcpy( buf, tmp, 16 ); + } + + ret = mbedtls_aes_crypt_cbc( &ctx, mode, 16, iv, buf, buf ); + if( ret != 0 ) + goto exit; + + } + + if( memcmp( buf, aes_tests, 16 ) != 0 ) + { + ret = 1; + goto exit; + } + + if( verbose != 0 ) + mbedtls_printf( "passed\n" ); + } + + if( verbose != 0 ) + mbedtls_printf( "\n" ); +#endif /* MBEDTLS_CIPHER_MODE_CBC */ + +#if defined(MBEDTLS_CIPHER_MODE_CFB) + /* + * CFB128 mode + */ + for( i = 0; i < 6; i++ ) + { + u = i >> 1; + keybits = 128 + u * 64; + mode = i & 1; + + if( verbose != 0 ) + mbedtls_printf( " AES-CFB128-%3d (%s): ", keybits, + ( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" ); + + memcpy( iv, aes_test_cfb128_iv, 16 ); + memcpy( key, aes_test_cfb128_key[u], keybits / 8 ); + + offset = 0; + ret = mbedtls_aes_setkey_enc( &ctx, key, keybits ); + /* + * AES-192 is an optional feature that may be unavailable when + * there is an alternative underlying implementation i.e. when + * MBEDTLS_AES_ALT is defined. + */ + if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED && keybits == 192 ) + { + mbedtls_printf( "skipped\n" ); + continue; + } + else if( ret != 0 ) + { + goto exit; + } + + if( mode == MBEDTLS_AES_DECRYPT ) + { + memcpy( buf, aes_test_cfb128_ct[u], 64 ); + aes_tests = aes_test_cfb128_pt; + } + else + { + memcpy( buf, aes_test_cfb128_pt, 64 ); + aes_tests = aes_test_cfb128_ct[u]; + } + + ret = mbedtls_aes_crypt_cfb128( &ctx, mode, 64, &offset, iv, buf, buf ); + if( ret != 0 ) + goto exit; + + if( memcmp( buf, aes_tests, 64 ) != 0 ) + { + ret = 1; + goto exit; + } + + if( verbose != 0 ) + mbedtls_printf( "passed\n" ); + } + + if( verbose != 0 ) + mbedtls_printf( "\n" ); +#endif /* MBEDTLS_CIPHER_MODE_CFB */ + +#if defined(MBEDTLS_CIPHER_MODE_OFB) + /* + * OFB mode + */ + for( i = 0; i < 6; i++ ) + { + u = i >> 1; + keybits = 128 + u * 64; + mode = i & 1; + + if( verbose != 0 ) + mbedtls_printf( " AES-OFB-%3d (%s): ", keybits, + ( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" ); + + memcpy( iv, aes_test_ofb_iv, 16 ); + memcpy( key, aes_test_ofb_key[u], keybits / 8 ); + + offset = 0; + ret = mbedtls_aes_setkey_enc( &ctx, key, keybits ); + /* + * AES-192 is an optional feature that may be unavailable when + * there is an alternative underlying implementation i.e. when + * MBEDTLS_AES_ALT is defined. + */ + if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED && keybits == 192 ) + { + mbedtls_printf( "skipped\n" ); + continue; + } + else if( ret != 0 ) + { + goto exit; + } + + if( mode == MBEDTLS_AES_DECRYPT ) + { + memcpy( buf, aes_test_ofb_ct[u], 64 ); + aes_tests = aes_test_ofb_pt; + } + else + { + memcpy( buf, aes_test_ofb_pt, 64 ); + aes_tests = aes_test_ofb_ct[u]; + } + + ret = mbedtls_aes_crypt_ofb( &ctx, 64, &offset, iv, buf, buf ); + if( ret != 0 ) + goto exit; + + if( memcmp( buf, aes_tests, 64 ) != 0 ) + { + ret = 1; + goto exit; + } + + if( verbose != 0 ) + mbedtls_printf( "passed\n" ); + } + + if( verbose != 0 ) + mbedtls_printf( "\n" ); +#endif /* MBEDTLS_CIPHER_MODE_OFB */ + +#if defined(MBEDTLS_CIPHER_MODE_CTR) + /* + * CTR mode + */ + for( i = 0; i < 6; i++ ) + { + u = i >> 1; + mode = i & 1; + + if( verbose != 0 ) + mbedtls_printf( " AES-CTR-128 (%s): ", + ( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" ); + + memcpy( nonce_counter, aes_test_ctr_nonce_counter[u], 16 ); + memcpy( key, aes_test_ctr_key[u], 16 ); + + offset = 0; + if( ( ret = mbedtls_aes_setkey_enc( &ctx, key, 128 ) ) != 0 ) + goto exit; + + len = aes_test_ctr_len[u]; + + if( mode == MBEDTLS_AES_DECRYPT ) + { + memcpy( buf, aes_test_ctr_ct[u], len ); + aes_tests = aes_test_ctr_pt[u]; + } + else + { + memcpy( buf, aes_test_ctr_pt[u], len ); + aes_tests = aes_test_ctr_ct[u]; + } + + ret = mbedtls_aes_crypt_ctr( &ctx, len, &offset, nonce_counter, + stream_block, buf, buf ); + if( ret != 0 ) + goto exit; + + if( memcmp( buf, aes_tests, len ) != 0 ) + { + ret = 1; + goto exit; + } + + if( verbose != 0 ) + mbedtls_printf( "passed\n" ); + } + + if( verbose != 0 ) + mbedtls_printf( "\n" ); +#endif /* MBEDTLS_CIPHER_MODE_CTR */ + +#if defined(MBEDTLS_CIPHER_MODE_XTS) + { + static const int num_tests = + sizeof(aes_test_xts_key) / sizeof(*aes_test_xts_key); + mbedtls_aes_xts_context ctx_xts; + + /* + * XTS mode + */ + mbedtls_aes_xts_init( &ctx_xts ); + + for( i = 0; i < num_tests << 1; i++ ) + { + const unsigned char *data_unit; + u = i >> 1; + mode = i & 1; + + if( verbose != 0 ) + mbedtls_printf( " AES-XTS-128 (%s): ", + ( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" ); + + memset( key, 0, sizeof( key ) ); + memcpy( key, aes_test_xts_key[u], 32 ); + data_unit = aes_test_xts_data_unit[u]; + + len = sizeof( *aes_test_xts_ct32 ); + + if( mode == MBEDTLS_AES_DECRYPT ) + { + ret = mbedtls_aes_xts_setkey_dec( &ctx_xts, key, 256 ); + if( ret != 0) + goto exit; + memcpy( buf, aes_test_xts_ct32[u], len ); + aes_tests = aes_test_xts_pt32[u]; + } + else + { + ret = mbedtls_aes_xts_setkey_enc( &ctx_xts, key, 256 ); + if( ret != 0) + goto exit; + memcpy( buf, aes_test_xts_pt32[u], len ); + aes_tests = aes_test_xts_ct32[u]; + } + + + ret = mbedtls_aes_crypt_xts( &ctx_xts, mode, len, data_unit, + buf, buf ); + if( ret != 0 ) + goto exit; + + if( memcmp( buf, aes_tests, len ) != 0 ) + { + ret = 1; + goto exit; + } + + if( verbose != 0 ) + mbedtls_printf( "passed\n" ); + } + + if( verbose != 0 ) + mbedtls_printf( "\n" ); + + mbedtls_aes_xts_free( &ctx_xts ); + } +#endif /* MBEDTLS_CIPHER_MODE_XTS */ + + ret = 0; + +exit: + if( ret != 0 && verbose != 0 ) + mbedtls_printf( "failed\n" ); + + mbedtls_aes_free( &ctx ); + + return( ret ); +} + +#endif /* MBEDTLS_SELF_TEST */ + +#endif /* MBEDTLS_AES_C */ diff --git a/libs/crypt/base64.c b/libs/crypt/base64.c new file mode 100644 index 00000000..b1bd330d --- /dev/null +++ b/libs/crypt/base64.c @@ -0,0 +1,364 @@ +/* + * RFC 1521 base64 encoding/decoding + * + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#if defined(MBEDTLS_BASE64_C) + +#include "mbedtls/base64.h" + +#include + +#if defined(MBEDTLS_SELF_TEST) +#include +#if defined(MBEDTLS_PLATFORM_C) +#include "mbedtls/platform.h" +#else +#include +#define mbedtls_printf printf +#endif /* MBEDTLS_PLATFORM_C */ +#endif /* MBEDTLS_SELF_TEST */ + +#define BASE64_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */ + +/* Return 0xff if low <= c <= high, 0 otherwise. + * + * Constant flow with respect to c. + */ +static unsigned char mask_of_range( unsigned char low, unsigned char high, + unsigned char c ) +{ + /* low_mask is: 0 if low <= c, 0x...ff if low > c */ + unsigned low_mask = ( (unsigned) c - low ) >> 8; + /* high_mask is: 0 if c <= high, 0x...ff if c > high */ + unsigned high_mask = ( (unsigned) high - c ) >> 8; + return( ~( low_mask | high_mask ) & 0xff ); +} + +/* Given a value in the range 0..63, return the corresponding Base64 digit. + * The implementation assumes that letters are consecutive (e.g. ASCII + * but not EBCDIC). + */ +static unsigned char enc_char( unsigned char val ) +{ + unsigned char digit = 0; + /* For each range of values, if val is in that range, mask digit with + * the corresponding value. Since val can only be in a single range, + * only at most one masking will change digit. */ + digit |= mask_of_range( 0, 25, val ) & ( 'A' + val ); + digit |= mask_of_range( 26, 51, val ) & ( 'a' + val - 26 ); + digit |= mask_of_range( 52, 61, val ) & ( '0' + val - 52 ); + digit |= mask_of_range( 62, 62, val ) & '+'; + digit |= mask_of_range( 63, 63, val ) & '/'; + return( digit ); +} + +/* + * Encode a buffer into base64 format + */ +int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen, + const unsigned char *src, size_t slen ) +{ + size_t i, n; + int C1, C2, C3; + 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( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); + } + + n *= 4; + + if( ( dlen < n + 1 ) || ( NULL == dst ) ) + { + *olen = n + 1; + return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); + } + + n = ( slen / 3 ) * 3; + + for( i = 0, p = dst; i < n; i += 3 ) + { + C1 = *src++; + C2 = *src++; + C3 = *src++; + + *p++ = enc_char( ( C1 >> 2 ) & 0x3F ); + *p++ = enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F ); + *p++ = enc_char( ( ( ( C2 & 15 ) << 2 ) + ( C3 >> 6 ) ) & 0x3F ); + *p++ = enc_char( C3 & 0x3F ); + } + + if( i < slen ) + { + C1 = *src++; + C2 = ( ( i + 1 ) < slen ) ? *src++ : 0; + + *p++ = enc_char( ( C1 >> 2 ) & 0x3F ); + *p++ = enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F ); + + if( ( i + 1 ) < slen ) + *p++ = enc_char( ( ( C2 & 15 ) << 2 ) & 0x3F ); + else *p++ = '='; + + *p++ = '='; + } + + *olen = p - dst; + *p = 0; + + return( 0 ); +} + +/* Given a Base64 digit, return its value. + * If c is not a Base64 digit ('A'..'Z', 'a'..'z', '0'..'9', '+' or '/'), + * return -1. + * + * The implementation assumes that letters are consecutive (e.g. ASCII + * but not EBCDIC). + * + * The implementation is constant-flow (no branch or memory access depending + * on the value of c) unless the compiler inlines and optimizes a specific + * access. + */ +static signed char dec_value( unsigned char c ) +{ + unsigned char val = 0; + /* For each range of digits, if c is in that range, mask val with + * the corresponding value. Since c can only be in a single range, + * only at most one masking will change val. Set val to one plus + * the desired value so that it stays 0 if c is in none of the ranges. */ + val |= mask_of_range( 'A', 'Z', c ) & ( c - 'A' + 0 + 1 ); + val |= mask_of_range( 'a', 'z', c ) & ( c - 'a' + 26 + 1 ); + val |= mask_of_range( '0', '9', c ) & ( c - '0' + 52 + 1 ); + val |= mask_of_range( '+', '+', c ) & ( c - '+' + 62 + 1 ); + val |= mask_of_range( '/', '/', c ) & ( c - '/' + 63 + 1 ); + /* At this point, val is 0 if c is an invalid digit and v+1 if c is + * a digit with the value v. */ + return( val - 1 ); +} + +/* + * Decode a base64-formatted buffer + */ +int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, + const unsigned char *src, size_t slen ) +{ + size_t i; /* index in source */ + size_t n; /* number of digits or trailing = in source */ + uint32_t x; /* value accumulator */ + unsigned accumulated_digits = 0; + unsigned equals = 0; + int spaces_present = 0; + unsigned char *p; + + /* First pass: check for validity and get output length */ + for( i = n = 0; i < slen; i++ ) + { + /* Skip spaces before checking for EOL */ + spaces_present = 0; + while( i < slen && src[i] == ' ' ) + { + ++i; + spaces_present = 1; + } + + /* 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( spaces_present ) + return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); + + if( src[i] > 127 ) + return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); + + if( src[i] == '=' ) + { + if( ++equals > 2 ) + return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); + } + else + { + if( equals != 0 ) + return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); + if( dec_value( src[i] ) < 0 ) + return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); + } + n++; + } + + if( n == 0 ) + { + *olen = 0; + return( 0 ); + } + + /* 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 -= equals; + + if( dst == NULL || dlen < n ) + { + *olen = n; + return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); + } + + equals = 0; + for( x = 0, p = dst; i > 0; i--, src++ ) + { + if( *src == '\r' || *src == '\n' || *src == ' ' ) + continue; + + x = x << 6; + if( *src == '=' ) + ++equals; + else + x |= dec_value( *src ); + + if( ++accumulated_digits == 4 ) + { + accumulated_digits = 0; + *p++ = (unsigned char)( x >> 16 ); + if( equals <= 1 ) *p++ = (unsigned char)( x >> 8 ); + if( equals <= 0 ) *p++ = (unsigned char)( x ); + } + } + + *olen = p - dst; + + return( 0 ); +} + +#if defined(MBEDTLS_SELF_TEST) + +static const unsigned char 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 +}; + +static const unsigned char base64_test_enc[] = + "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK" + "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw=="; + +/* + * Checkup routine + */ +int mbedtls_base64_self_test( int verbose ) +{ + size_t len; + const unsigned char *src; + unsigned char buffer[128]; + + if( verbose != 0 ) + mbedtls_printf( " Base64 encoding test: " ); + + src = base64_test_dec; + + if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 || + memcmp( base64_test_enc, buffer, 88 ) != 0 ) + { + if( verbose != 0 ) + mbedtls_printf( "failed\n" ); + + return( 1 ); + } + + if( verbose != 0 ) + mbedtls_printf( "passed\n Base64 decoding test: " ); + + src = base64_test_enc; + + if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 || + memcmp( base64_test_dec, buffer, 64 ) != 0 ) + { + if( verbose != 0 ) + mbedtls_printf( "failed\n" ); + + return( 1 ); + } + + if( verbose != 0 ) + mbedtls_printf( "passed\n\n" ); + + return( 0 ); +} + +#endif /* MBEDTLS_SELF_TEST */ + +#endif /* MBEDTLS_BASE64_C */ diff --git a/libs/crypt/mbedtls/aes.h b/libs/crypt/mbedtls/aes.h new file mode 100644 index 00000000..dc5ae199 --- /dev/null +++ b/libs/crypt/mbedtls/aes.h @@ -0,0 +1,701 @@ +/** + * \file aes.h + * + * \brief This file contains AES definitions and functions. + * + * The Advanced Encryption Standard (AES) specifies a FIPS-approved + * cryptographic algorithm that can be used to protect electronic + * data. + * + * The AES algorithm is a symmetric block cipher that can + * encrypt and decrypt information. For more information, see + * FIPS Publication 197: Advanced Encryption Standard and + * ISO/IEC 18033-2:2006: Information technology -- Security + * techniques -- Encryption algorithms -- Part 2: Asymmetric + * ciphers. + * + * The AES-XTS block mode is standardized by NIST SP 800-38E + * + * and described in detail by IEEE P1619 + * . + */ + +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ + +#ifndef MBEDTLS_AES_H +#define MBEDTLS_AES_H + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#include +#include + +/* padlock.c and aesni.c rely on these values! */ +#define MBEDTLS_AES_ENCRYPT 1 /**< AES encryption. */ +#define MBEDTLS_AES_DECRYPT 0 /**< AES decryption. */ + +/* Error codes in range 0x0020-0x0022 */ +/** Invalid key length. */ +#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 +/** Invalid data input length. */ +#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 + +/* Error codes in range 0x0021-0x0025 */ +/** Invalid input data. */ +#define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021 + +/* MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE is deprecated and should not be used. */ +/** Feature not available. For example, an unsupported AES key size. */ +#define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023 + +/* MBEDTLS_ERR_AES_HW_ACCEL_FAILED is deprecated and should not be used. */ +/** AES hardware accelerator failed. */ +#define MBEDTLS_ERR_AES_HW_ACCEL_FAILED -0x0025 + +#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ + !defined(inline) && !defined(__cplusplus) +#define inline __inline +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#if !defined(MBEDTLS_AES_ALT) +// Regular implementation +// + +/** + * \brief The AES context-type definition. + */ +typedef struct mbedtls_aes_context +{ + int nr; /*!< The number of rounds. */ + uint32_t *rk; /*!< AES round keys. */ + uint32_t buf[68]; /*!< Unaligned data buffer. This buffer can + hold 32 extra Bytes, which can be used for + one of the following purposes: +
  • Alignment if VIA padlock is + used.
  • +
  • Simplifying key expansion in the 256-bit + case by generating an extra round key. +
*/ +} +mbedtls_aes_context; + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +/** + * \brief The AES XTS context-type definition. + */ +typedef struct mbedtls_aes_xts_context +{ + mbedtls_aes_context crypt; /*!< The AES context to use for AES block + encryption or decryption. */ + mbedtls_aes_context tweak; /*!< The AES context used for tweak + computation. */ +} mbedtls_aes_xts_context; +#endif /* MBEDTLS_CIPHER_MODE_XTS */ + +#else /* MBEDTLS_AES_ALT */ +#include "aes_alt.h" +#endif /* MBEDTLS_AES_ALT */ + +/** + * \brief This function initializes the specified AES context. + * + * It must be the first API called before using + * the context. + * + * \param ctx The AES context to initialize. This must not be \c NULL. + */ +void mbedtls_aes_init( mbedtls_aes_context *ctx ); + +/** + * \brief This function releases and clears the specified AES context. + * + * \param ctx The AES context to clear. + * If this is \c NULL, this function does nothing. + * Otherwise, the context must have been at least initialized. + */ +void mbedtls_aes_free( mbedtls_aes_context *ctx ); + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +/** + * \brief This function initializes the specified AES XTS context. + * + * It must be the first API called before using + * the context. + * + * \param ctx The AES XTS context to initialize. This must not be \c NULL. + */ +void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx ); + +/** + * \brief This function releases and clears the specified AES XTS context. + * + * \param ctx The AES XTS context to clear. + * If this is \c NULL, this function does nothing. + * Otherwise, the context must have been at least initialized. + */ +void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx ); +#endif /* MBEDTLS_CIPHER_MODE_XTS */ + +/** + * \brief This function sets the encryption key. + * + * \param ctx The AES context to which the key should be bound. + * It must be initialized. + * \param key The encryption key. + * This must be a readable buffer of size \p keybits bits. + * \param keybits The size of data passed in bits. Valid options are: + *
  • 128 bits
  • + *
  • 192 bits
  • + *
  • 256 bits
+ * + * \return \c 0 on success. + * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. + */ +int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, + unsigned int keybits ); + +/** + * \brief This function sets the decryption key. + * + * \param ctx The AES context to which the key should be bound. + * It must be initialized. + * \param key The decryption key. + * This must be a readable buffer of size \p keybits bits. + * \param keybits The size of data passed. Valid options are: + *
  • 128 bits
  • + *
  • 192 bits
  • + *
  • 256 bits
+ * + * \return \c 0 on success. + * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. + */ +int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, + unsigned int keybits ); + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +/** + * \brief This function prepares an XTS context for encryption and + * sets the encryption key. + * + * \param ctx The AES XTS context to which the key should be bound. + * It must be initialized. + * \param key The encryption key. This is comprised of the XTS key1 + * concatenated with the XTS key2. + * This must be a readable buffer of size \p keybits bits. + * \param keybits The size of \p key passed in bits. Valid options are: + *
  • 256 bits (each of key1 and key2 is a 128-bit key)
  • + *
  • 512 bits (each of key1 and key2 is a 256-bit key)
+ * + * \return \c 0 on success. + * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. + */ +int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx, + const unsigned char *key, + unsigned int keybits ); + +/** + * \brief This function prepares an XTS context for decryption and + * sets the decryption key. + * + * \param ctx The AES XTS context to which the key should be bound. + * It must be initialized. + * \param key The decryption key. This is comprised of the XTS key1 + * concatenated with the XTS key2. + * This must be a readable buffer of size \p keybits bits. + * \param keybits The size of \p key passed in bits. Valid options are: + *
  • 256 bits (each of key1 and key2 is a 128-bit key)
  • + *
  • 512 bits (each of key1 and key2 is a 256-bit key)
+ * + * \return \c 0 on success. + * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure. + */ +int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx, + const unsigned char *key, + unsigned int keybits ); +#endif /* MBEDTLS_CIPHER_MODE_XTS */ + +/** + * \brief This function performs an AES single-block encryption or + * decryption operation. + * + * It performs the operation defined in the \p mode parameter + * (encrypt or decrypt), on the input data buffer defined in + * the \p input parameter. + * + * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or + * mbedtls_aes_setkey_dec() must be called before the first + * call to this API with the same context. + * + * \param ctx The AES context to use for encryption or decryption. + * It must be initialized and bound to a key. + * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or + * #MBEDTLS_AES_DECRYPT. + * \param input The buffer holding the input data. + * It must be readable and at least \c 16 Bytes long. + * \param output The buffer where the output data will be written. + * It must be writeable and at least \c 16 Bytes long. + + * \return \c 0 on success. + */ +int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, + int mode, + const unsigned char input[16], + unsigned char output[16] ); + +#if defined(MBEDTLS_CIPHER_MODE_CBC) +/** + * \brief This function performs an AES-CBC encryption or decryption operation + * on full blocks. + * + * It performs the operation defined in the \p mode + * parameter (encrypt/decrypt), on the input data buffer defined in + * the \p input parameter. + * + * It can be called as many times as needed, until all the input + * data is processed. mbedtls_aes_init(), and either + * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called + * before the first call to this API with the same context. + * + * \note This function operates on full blocks, that is, the input size + * must be a multiple of the AES block size of \c 16 Bytes. + * + * \note Upon exit, the content of the IV is updated so that you can + * call the same function again on the next + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If you need to retain the contents of the IV, you should + * either save it manually or use the cipher module instead. + * + * + * \param ctx The AES context to use for encryption or decryption. + * It must be initialized and bound to a key. + * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or + * #MBEDTLS_AES_DECRYPT. + * \param length The length of the input data in Bytes. This must be a + * multiple of the block size (\c 16 Bytes). + * \param iv Initialization vector (updated after use). + * It must be a readable and writeable buffer of \c 16 Bytes. + * \param input The buffer holding the input data. + * It must be readable and of size \p length Bytes. + * \param output The buffer holding the output data. + * It must be writeable and of size \p length Bytes. + * + * \return \c 0 on success. + * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH + * on failure. + */ +int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, + int mode, + size_t length, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ); +#endif /* MBEDTLS_CIPHER_MODE_CBC */ + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +/** + * \brief This function performs an AES-XTS encryption or decryption + * operation for an entire XTS data unit. + * + * AES-XTS encrypts or decrypts blocks based on their location as + * defined by a data unit number. The data unit number must be + * provided by \p data_unit. + * + * NIST SP 800-38E limits the maximum size of a data unit to 2^20 + * AES blocks. If the data unit is larger than this, this function + * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH. + * + * \param ctx The AES XTS context to use for AES XTS operations. + * It must be initialized and bound to a key. + * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or + * #MBEDTLS_AES_DECRYPT. + * \param length The length of a data unit in Bytes. This can be any + * length between 16 bytes and 2^24 bytes inclusive + * (between 1 and 2^20 block cipher blocks). + * \param data_unit The address of the data unit encoded as an array of 16 + * bytes in little-endian format. For disk encryption, this + * is typically the index of the block device sector that + * contains the data. + * \param input The buffer holding the input data (which is an entire + * data unit). This function reads \p length Bytes from \p + * input. + * \param output The buffer holding the output data (which is an entire + * data unit). This function writes \p length Bytes to \p + * output. + * + * \return \c 0 on success. + * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is + * smaller than an AES block in size (16 Bytes) or if \p + * length is larger than 2^20 blocks (16 MiB). + */ +int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx, + int mode, + size_t length, + const unsigned char data_unit[16], + const unsigned char *input, + unsigned char *output ); +#endif /* MBEDTLS_CIPHER_MODE_XTS */ + +#if defined(MBEDTLS_CIPHER_MODE_CFB) +/** + * \brief This function performs an AES-CFB128 encryption or decryption + * operation. + * + * It performs the operation defined in the \p mode + * parameter (encrypt or decrypt), on the input data buffer + * defined in the \p input parameter. + * + * For CFB, you must set up the context with mbedtls_aes_setkey_enc(), + * regardless of whether you are performing an encryption or decryption + * operation, that is, regardless of the \p mode parameter. This is + * because CFB mode uses the same key schedule for encryption and + * decryption. + * + * \note Upon exit, the content of the IV is updated so that you can + * call the same function again on the next + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If you need to retain the contents of the + * IV, you must either save it manually or use the cipher + * module instead. + * + * + * \param ctx The AES context to use for encryption or decryption. + * It must be initialized and bound to a key. + * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or + * #MBEDTLS_AES_DECRYPT. + * \param length The length of the input data in Bytes. + * \param iv_off The offset in IV (updated after use). + * It must point to a valid \c size_t. + * \param iv The initialization vector (updated after use). + * It must be a readable and writeable buffer of \c 16 Bytes. + * \param input The buffer holding the input data. + * It must be readable and of size \p length Bytes. + * \param output The buffer holding the output data. + * It must be writeable and of size \p length Bytes. + * + * \return \c 0 on success. + */ +int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, + int mode, + size_t length, + size_t *iv_off, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ); + +/** + * \brief This function performs an AES-CFB8 encryption or decryption + * operation. + * + * It performs the operation defined in the \p mode + * parameter (encrypt/decrypt), on the input data buffer defined + * in the \p input parameter. + * + * Due to the nature of CFB, you must use the same key schedule for + * both encryption and decryption operations. Therefore, you must + * use the context initialized with mbedtls_aes_setkey_enc() for + * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT. + * + * \note Upon exit, the content of the IV is updated so that you can + * call the same function again on the next + * block(s) of data and get the same result as if it was + * encrypted in one call. This allows a "streaming" usage. + * If you need to retain the contents of the + * IV, you should either save it manually or use the cipher + * module instead. + * + * + * \param ctx The AES context to use for encryption or decryption. + * It must be initialized and bound to a key. + * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or + * #MBEDTLS_AES_DECRYPT + * \param length The length of the input data. + * \param iv The initialization vector (updated after use). + * It must be a readable and writeable buffer of \c 16 Bytes. + * \param input The buffer holding the input data. + * It must be readable and of size \p length Bytes. + * \param output The buffer holding the output data. + * It must be writeable and of size \p length Bytes. + * + * \return \c 0 on success. + */ +int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, + int mode, + size_t length, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ); +#endif /*MBEDTLS_CIPHER_MODE_CFB */ + +#if defined(MBEDTLS_CIPHER_MODE_OFB) +/** + * \brief This function performs an AES-OFB (Output Feedback Mode) + * encryption or decryption operation. + * + * For OFB, you must set up the context with + * mbedtls_aes_setkey_enc(), regardless of whether you are + * performing an encryption or decryption operation. This is + * because OFB mode uses the same key schedule for encryption and + * decryption. + * + * The OFB operation is identical for encryption or decryption, + * therefore no operation mode needs to be specified. + * + * \note Upon exit, the content of iv, the Initialisation Vector, is + * updated so that you can call the same function again on the next + * block(s) of data and get the same result as if it was encrypted + * in one call. This allows a "streaming" usage, by initialising + * iv_off to 0 before the first call, and preserving its value + * between calls. + * + * For non-streaming use, the iv should be initialised on each call + * to a unique value, and iv_off set to 0 on each call. + * + * If you need to retain the contents of the initialisation vector, + * you must either save it manually or use the cipher module + * instead. + * + * \warning For the OFB mode, the initialisation vector must be unique + * every encryption operation. Reuse of an initialisation vector + * will compromise security. + * + * \param ctx The AES context to use for encryption or decryption. + * It must be initialized and bound to a key. + * \param length The length of the input data. + * \param iv_off The offset in IV (updated after use). + * It must point to a valid \c size_t. + * \param iv The initialization vector (updated after use). + * It must be a readable and writeable buffer of \c 16 Bytes. + * \param input The buffer holding the input data. + * It must be readable and of size \p length Bytes. + * \param output The buffer holding the output data. + * It must be writeable and of size \p length Bytes. + * + * \return \c 0 on success. + */ +int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx, + size_t length, + size_t *iv_off, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ); + +#endif /* MBEDTLS_CIPHER_MODE_OFB */ + +#if defined(MBEDTLS_CIPHER_MODE_CTR) +/** + * \brief This function performs an AES-CTR encryption or decryption + * operation. + * + * Due to the nature of CTR, you must use the same key schedule + * for both encryption and decryption operations. Therefore, you + * must use the context initialized with mbedtls_aes_setkey_enc() + * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT. + * + * \warning You must never reuse a nonce value with the same key. Doing so + * would void the encryption for the two messages encrypted with + * the same nonce and key. + * + * There are two common strategies for managing nonces with CTR: + * + * 1. You can handle everything as a single message processed over + * successive calls to this function. In that case, you want to + * set \p nonce_counter and \p nc_off to 0 for the first call, and + * then preserve the values of \p nonce_counter, \p nc_off and \p + * stream_block across calls to this function as they will be + * updated by this function. + * + * With this strategy, you must not encrypt more than 2**128 + * blocks of data with the same key. + * + * 2. You can encrypt separate messages by dividing the \p + * nonce_counter buffer in two areas: the first one used for a + * per-message nonce, handled by yourself, and the second one + * updated by this function internally. + * + * For example, you might reserve the first 12 bytes for the + * per-message nonce, and the last 4 bytes for internal use. In that + * case, before calling this function on a new message you need to + * set the first 12 bytes of \p nonce_counter to your chosen nonce + * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p + * stream_block to be ignored). That way, you can encrypt at most + * 2**96 messages of up to 2**32 blocks each with the same key. + * + * The per-message nonce (or information sufficient to reconstruct + * it) needs to be communicated with the ciphertext and must be unique. + * The recommended way to ensure uniqueness is to use a message + * counter. An alternative is to generate random nonces, but this + * limits the number of messages that can be securely encrypted: + * for example, with 96-bit random nonces, you should not encrypt + * more than 2**32 messages with the same key. + * + * Note that for both stategies, sizes are measured in blocks and + * that an AES block is 16 bytes. + * + * \warning Upon return, \p stream_block contains sensitive data. Its + * content must not be written to insecure storage and should be + * securely discarded as soon as it's no longer needed. + * + * \param ctx The AES context to use for encryption or decryption. + * It must be initialized and bound to a key. + * \param length The length of the input data. + * \param nc_off The offset in the current \p stream_block, for + * resuming within the current cipher stream. The + * offset pointer should be 0 at the start of a stream. + * It must point to a valid \c size_t. + * \param nonce_counter The 128-bit nonce and counter. + * It must be a readable-writeable buffer of \c 16 Bytes. + * \param stream_block The saved stream block for resuming. This is + * overwritten by the function. + * It must be a readable-writeable buffer of \c 16 Bytes. + * \param input The buffer holding the input data. + * It must be readable and of size \p length Bytes. + * \param output The buffer holding the output data. + * It must be writeable and of size \p length Bytes. + * + * \return \c 0 on success. + */ +int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, + size_t length, + size_t *nc_off, + unsigned char nonce_counter[16], + unsigned char stream_block[16], + const unsigned char *input, + unsigned char *output ); +#endif /* MBEDTLS_CIPHER_MODE_CTR */ + +/** + * \brief Internal AES block encryption function. This is only + * exposed to allow overriding it using + * \c MBEDTLS_AES_ENCRYPT_ALT. + * + * \param ctx The AES context to use for encryption. + * \param input The plaintext block. + * \param output The output (ciphertext) block. + * + * \return \c 0 on success. + */ +int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ); + +/** + * \brief Internal AES block decryption function. This is only + * exposed to allow overriding it using see + * \c MBEDTLS_AES_DECRYPT_ALT. + * + * \param ctx The AES context to use for decryption. + * \param input The ciphertext block. + * \param output The output (plaintext) block. + * + * \return \c 0 on success. + */ +int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ); + +#if !defined(MBEDTLS_DEPRECATED_REMOVED) +#if defined(MBEDTLS_DEPRECATED_WARNING) +#define MBEDTLS_DEPRECATED __attribute__((deprecated)) +#else +#define MBEDTLS_DEPRECATED +#endif +/** + * \brief Deprecated internal AES block encryption function + * without return value. + * + * \deprecated Superseded by mbedtls_internal_aes_encrypt() + * + * \param ctx The AES context to use for encryption. + * \param input Plaintext block. + * \param output Output (ciphertext) block. + */ +MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ); + +/** + * \brief Deprecated internal AES block decryption function + * without return value. + * + * \deprecated Superseded by mbedtls_internal_aes_decrypt() + * + * \param ctx The AES context to use for decryption. + * \param input Ciphertext block. + * \param output Output (plaintext) block. + */ +MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ); + +#undef MBEDTLS_DEPRECATED +#endif /* !MBEDTLS_DEPRECATED_REMOVED */ + + +#if defined(MBEDTLS_SELF_TEST) +/** + * \brief Checkup routine. + * + * \return \c 0 on success. + * \return \c 1 on failure. + */ +int mbedtls_aes_self_test( int verbose ); + +#endif /* MBEDTLS_SELF_TEST */ + +#ifdef __cplusplus +} +#endif + +#endif /* aes.h */ diff --git a/libs/crypt/mbedtls/base64.h b/libs/crypt/mbedtls/base64.h new file mode 100644 index 00000000..eaada6e9 --- /dev/null +++ b/libs/crypt/mbedtls/base64.h @@ -0,0 +1,125 @@ +/** + * \file base64.h + * + * \brief RFC 1521 base64 encoding/decoding + */ +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ +#ifndef MBEDTLS_BASE64_H +#define MBEDTLS_BASE64_H + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#include + +/** Output buffer too small. */ +#define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A +/** Invalid character in input. */ +#define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief Encode a buffer into base64 format + * + * \param dst destination buffer + * \param dlen size of the destination buffer + * \param olen number of bytes written + * \param src source buffer + * \param 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 mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen, + const unsigned char *src, size_t slen ); + +/** + * \brief Decode a base64-formatted buffer + * + * \param dst destination buffer (can be NULL for checking size) + * \param dlen size of the destination buffer + * \param olen number of bytes written + * \param src source buffer + * \param 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 mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, + const unsigned char *src, size_t slen ); + +#if defined(MBEDTLS_SELF_TEST) +/** + * \brief Checkup routine + * + * \return 0 if successful, or 1 if the test failed + */ +int mbedtls_base64_self_test( int verbose ); + +#endif /* MBEDTLS_SELF_TEST */ + +#ifdef __cplusplus +} +#endif + +#endif /* base64.h */ diff --git a/libs/crypt/mbedtls/config.h b/libs/crypt/mbedtls/config.h new file mode 100644 index 00000000..39aa5058 --- /dev/null +++ b/libs/crypt/mbedtls/config.h @@ -0,0 +1,3444 @@ +/** + * \file config.h + * + * \brief Configuration options (set of defines) + * + * This set of compile-time options may be used to enable + * or disable features selectively, and reduce the global + * memory footprint. + */ +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ + +#ifndef MBEDTLS_CONFIG_H +#define MBEDTLS_CONFIG_H + +#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE) +#define _CRT_SECURE_NO_DEPRECATE 1 +#endif + +/** + * \name SECTION: System support + * + * This section sets system specific settings. + * \{ + */ + +/** + * \def MBEDTLS_HAVE_ASM + * + * The compiler has support for asm(). + * + * Requires support for asm() in compiler. + * + * Used in: + * library/aria.c + * library/timing.c + * include/mbedtls/bn_mul.h + * + * Required by: + * MBEDTLS_AESNI_C + * MBEDTLS_PADLOCK_C + * + * Comment to disable the use of assembly code. + */ +#define MBEDTLS_HAVE_ASM + +/** + * \def MBEDTLS_NO_UDBL_DIVISION + * + * The platform lacks support for double-width integer division (64-bit + * division on a 32-bit platform, 128-bit division on a 64-bit platform). + * + * Used in: + * include/mbedtls/bignum.h + * library/bignum.c + * + * The bignum code uses double-width division to speed up some operations. + * Double-width division is often implemented in software that needs to + * be linked with the program. The presence of a double-width integer + * type is usually detected automatically through preprocessor macros, + * but the automatic detection cannot know whether the code needs to + * and can be linked with an implementation of division for that type. + * By default division is assumed to be usable if the type is present. + * Uncomment this option to prevent the use of double-width division. + * + * Note that division for the native integer type is always required. + * Furthermore, a 64-bit type is always required even on a 32-bit + * platform, but it need not support multiplication or division. In some + * cases it is also desirable to disable some double-width operations. For + * example, if double-width division is implemented in software, disabling + * it can reduce code size in some embedded targets. + */ +//#define MBEDTLS_NO_UDBL_DIVISION + +/** + * \def MBEDTLS_NO_64BIT_MULTIPLICATION + * + * The platform lacks support for 32x32 -> 64-bit multiplication. + * + * Used in: + * library/poly1305.c + * + * Some parts of the library may use multiplication of two unsigned 32-bit + * operands with a 64-bit result in order to speed up computations. On some + * platforms, this is not available in hardware and has to be implemented in + * software, usually in a library provided by the toolchain. + * + * Sometimes it is not desirable to have to link to that library. This option + * removes the dependency of that library on platforms that lack a hardware + * 64-bit multiplier by embedding a software implementation in Mbed TLS. + * + * Note that depending on the compiler, this may decrease performance compared + * to using the library function provided by the toolchain. + */ +//#define MBEDTLS_NO_64BIT_MULTIPLICATION + +/** + * \def MBEDTLS_HAVE_SSE2 + * + * CPU supports SSE2 instruction set. + * + * Uncomment if the CPU supports SSE2 (IA-32 specific). + */ +//#define MBEDTLS_HAVE_SSE2 + +/** + * \def MBEDTLS_HAVE_TIME + * + * System has time.h and time(). + * The time does not need to be correct, only time differences are used, + * by contrast with MBEDTLS_HAVE_TIME_DATE + * + * Defining MBEDTLS_HAVE_TIME allows you to specify MBEDTLS_PLATFORM_TIME_ALT, + * MBEDTLS_PLATFORM_TIME_MACRO, MBEDTLS_PLATFORM_TIME_TYPE_MACRO and + * MBEDTLS_PLATFORM_STD_TIME. + * + * Comment if your system does not support time functions + */ +#define MBEDTLS_HAVE_TIME + +/** + * \def MBEDTLS_HAVE_TIME_DATE + * + * System has time.h, time(), and an implementation for + * mbedtls_platform_gmtime_r() (see below). + * The time needs to be correct (not necessarily very accurate, but at least + * the date should be correct). This is used to verify the validity period of + * X.509 certificates. + * + * Comment if your system does not have a correct clock. + * + * \note mbedtls_platform_gmtime_r() is an abstraction in platform_util.h that + * behaves similarly to the gmtime_r() function from the C standard. Refer to + * the documentation for mbedtls_platform_gmtime_r() for more information. + * + * \note It is possible to configure an implementation for + * mbedtls_platform_gmtime_r() at compile-time by using the macro + * MBEDTLS_PLATFORM_GMTIME_R_ALT. + */ +#define MBEDTLS_HAVE_TIME_DATE + +/** + * \def MBEDTLS_PLATFORM_MEMORY + * + * Enable the memory allocation layer. + * + * By default mbed TLS uses the system-provided calloc() and free(). + * This allows different allocators (self-implemented or provided) to be + * provided to the platform abstraction layer. + * + * Enabling MBEDTLS_PLATFORM_MEMORY without the + * MBEDTLS_PLATFORM_{FREE,CALLOC}_MACROs will provide + * "mbedtls_platform_set_calloc_free()" allowing you to set an alternative calloc() and + * free() function pointer at runtime. + * + * Enabling MBEDTLS_PLATFORM_MEMORY and specifying + * MBEDTLS_PLATFORM_{CALLOC,FREE}_MACROs will allow you to specify the + * alternate function at compile time. + * + * Requires: MBEDTLS_PLATFORM_C + * + * Enable this layer to allow use of alternative memory allocators. + */ +//#define MBEDTLS_PLATFORM_MEMORY + +/** + * \def MBEDTLS_PLATFORM_NO_STD_FUNCTIONS + * + * Do not assign standard functions in the platform layer (e.g. calloc() to + * MBEDTLS_PLATFORM_STD_CALLOC and printf() to MBEDTLS_PLATFORM_STD_PRINTF) + * + * This makes sure there are no linking errors on platforms that do not support + * these functions. You will HAVE to provide alternatives, either at runtime + * via the platform_set_xxx() functions or at compile time by setting + * the MBEDTLS_PLATFORM_STD_XXX defines, or enabling a + * MBEDTLS_PLATFORM_XXX_MACRO. + * + * Requires: MBEDTLS_PLATFORM_C + * + * Uncomment to prevent default assignment of standard functions in the + * platform layer. + */ +//#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS + +/** + * \def MBEDTLS_PLATFORM_EXIT_ALT + * + * MBEDTLS_PLATFORM_XXX_ALT: Uncomment a macro to let mbed TLS support the + * function in the platform abstraction layer. + * + * Example: In case you uncomment MBEDTLS_PLATFORM_PRINTF_ALT, mbed TLS will + * provide a function "mbedtls_platform_set_printf()" that allows you to set an + * alternative printf function pointer. + * + * All these define require MBEDTLS_PLATFORM_C to be defined! + * + * \note MBEDTLS_PLATFORM_SNPRINTF_ALT is required on Windows; + * it will be enabled automatically by check_config.h + * + * \warning MBEDTLS_PLATFORM_XXX_ALT cannot be defined at the same time as + * MBEDTLS_PLATFORM_XXX_MACRO! + * + * Requires: MBEDTLS_PLATFORM_TIME_ALT requires MBEDTLS_HAVE_TIME + * + * Uncomment a macro to enable alternate implementation of specific base + * platform function + */ +//#define MBEDTLS_PLATFORM_EXIT_ALT +//#define MBEDTLS_PLATFORM_TIME_ALT +//#define MBEDTLS_PLATFORM_FPRINTF_ALT +//#define MBEDTLS_PLATFORM_PRINTF_ALT +//#define MBEDTLS_PLATFORM_SNPRINTF_ALT +//#define MBEDTLS_PLATFORM_NV_SEED_ALT +//#define MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT + +/** + * \def MBEDTLS_DEPRECATED_WARNING + * + * Mark deprecated functions so that they generate a warning if used. + * Functions deprecated in one version will usually be removed in the next + * version. You can enable this to help you prepare the transition to a new + * major version by making sure your code is not using these functions. + * + * This only works with GCC and Clang. With other compilers, you may want to + * use MBEDTLS_DEPRECATED_REMOVED + * + * Uncomment to get warnings on using deprecated functions. + */ +//#define MBEDTLS_DEPRECATED_WARNING + +/** + * \def MBEDTLS_DEPRECATED_REMOVED + * + * Remove deprecated functions so that they generate an error if used. + * Functions deprecated in one version will usually be removed in the next + * version. You can enable this to help you prepare the transition to a new + * major version by making sure your code is not using these functions. + * + * Uncomment to get errors on using deprecated functions. + */ +//#define MBEDTLS_DEPRECATED_REMOVED + +/** + * \def MBEDTLS_CHECK_PARAMS + * + * This configuration option controls whether the library validates more of + * the parameters passed to it. + * + * When this flag is not defined, the library only attempts to validate an + * input parameter if: (1) they may come from the outside world (such as the + * network, the filesystem, etc.) or (2) not validating them could result in + * internal memory errors such as overflowing a buffer controlled by the + * library. On the other hand, it doesn't attempt to validate parameters whose + * values are fully controlled by the application (such as pointers). + * + * When this flag is defined, the library additionally attempts to validate + * parameters that are fully controlled by the application, and should always + * be valid if the application code is fully correct and trusted. + * + * For example, when a function accepts as input a pointer to a buffer that may + * contain untrusted data, and its documentation mentions that this pointer + * must not be NULL: + * - The pointer is checked to be non-NULL only if this option is enabled. + * - The content of the buffer is always validated. + * + * When this flag is defined, if a library function receives a parameter that + * is invalid: + * 1. The function will invoke the macro MBEDTLS_PARAM_FAILED(). + * 2. If MBEDTLS_PARAM_FAILED() did not terminate the program, the function + * will immediately return. If the function returns an Mbed TLS error code, + * the error code in this case is MBEDTLS_ERR_xxx_BAD_INPUT_DATA. + * + * When defining this flag, you also need to arrange a definition for + * MBEDTLS_PARAM_FAILED(). You can do this by any of the following methods: + * - By default, the library defines MBEDTLS_PARAM_FAILED() to call a + * function mbedtls_param_failed(), but the library does not define this + * function. If you do not make any other arrangements, you must provide + * the function mbedtls_param_failed() in your application. + * See `platform_util.h` for its prototype. + * - If you enable the macro #MBEDTLS_CHECK_PARAMS_ASSERT, then the + * library defines #MBEDTLS_PARAM_FAILED(\c cond) to be `assert(cond)`. + * You can still supply an alternative definition of + * MBEDTLS_PARAM_FAILED(), which may call `assert`. + * - If you define a macro MBEDTLS_PARAM_FAILED() before including `config.h` + * or you uncomment the definition of MBEDTLS_PARAM_FAILED() in `config.h`, + * the library will call the macro that you defined and will not supply + * its own version. Note that if MBEDTLS_PARAM_FAILED() calls `assert`, + * you need to enable #MBEDTLS_CHECK_PARAMS_ASSERT so that library source + * files include ``. + * + * Uncomment to enable validation of application-controlled parameters. + */ +//#define MBEDTLS_CHECK_PARAMS + +/** + * \def MBEDTLS_CHECK_PARAMS_ASSERT + * + * Allow MBEDTLS_PARAM_FAILED() to call `assert`, and make it default to + * `assert`. This macro is only used if #MBEDTLS_CHECK_PARAMS is defined. + * + * If this macro is not defined, then MBEDTLS_PARAM_FAILED() defaults to + * calling a function mbedtls_param_failed(). See the documentation of + * #MBEDTLS_CHECK_PARAMS for details. + * + * Uncomment to allow MBEDTLS_PARAM_FAILED() to call `assert`. + */ +//#define MBEDTLS_CHECK_PARAMS_ASSERT + +/* \} name SECTION: System support */ + +/** + * \name SECTION: mbed TLS feature support + * + * This section sets support for features that are or are not needed + * within the modules that are enabled. + * \{ + */ + +/** + * \def MBEDTLS_TIMING_ALT + * + * Uncomment to provide your own alternate implementation for mbedtls_timing_hardclock(), + * mbedtls_timing_get_timer(), mbedtls_set_alarm(), mbedtls_set/get_delay() + * + * Only works if you have MBEDTLS_TIMING_C enabled. + * + * You will need to provide a header "timing_alt.h" and an implementation at + * compile time. + */ +//#define MBEDTLS_TIMING_ALT + +/** + * \def MBEDTLS_AES_ALT + * + * MBEDTLS__MODULE_NAME__ALT: Uncomment a macro to let mbed TLS use your + * alternate core implementation of a symmetric crypto, an arithmetic or hash + * module (e.g. platform specific assembly optimized implementations). Keep + * in mind that the function prototypes should remain the same. + * + * This replaces the whole module. If you only want to replace one of the + * functions, use one of the MBEDTLS__FUNCTION_NAME__ALT flags. + * + * Example: In case you uncomment MBEDTLS_AES_ALT, mbed TLS will no longer + * provide the "struct mbedtls_aes_context" definition and omit the base + * function declarations and implementations. "aes_alt.h" will be included from + * "aes.h" to include the new function definitions. + * + * Uncomment a macro to enable alternate implementation of the corresponding + * module. + * + * \warning MD2, MD4, MD5, ARC4, DES and SHA-1 are considered weak and their + * use constitutes a security risk. If possible, we recommend + * avoiding dependencies on them, and considering stronger message + * digests and ciphers instead. + * + */ +//#define MBEDTLS_AES_ALT +//#define MBEDTLS_ARC4_ALT +//#define MBEDTLS_ARIA_ALT +//#define MBEDTLS_BLOWFISH_ALT +//#define MBEDTLS_CAMELLIA_ALT +//#define MBEDTLS_CCM_ALT +//#define MBEDTLS_CHACHA20_ALT +//#define MBEDTLS_CHACHAPOLY_ALT +//#define MBEDTLS_CMAC_ALT +//#define MBEDTLS_DES_ALT +//#define MBEDTLS_DHM_ALT +//#define MBEDTLS_ECJPAKE_ALT +//#define MBEDTLS_GCM_ALT +//#define MBEDTLS_NIST_KW_ALT +//#define MBEDTLS_MD2_ALT +//#define MBEDTLS_MD4_ALT +//#define MBEDTLS_MD5_ALT +//#define MBEDTLS_POLY1305_ALT +//#define MBEDTLS_RIPEMD160_ALT +//#define MBEDTLS_RSA_ALT +//#define MBEDTLS_SHA1_ALT +//#define MBEDTLS_SHA256_ALT +//#define MBEDTLS_SHA512_ALT +//#define MBEDTLS_XTEA_ALT + +/* + * When replacing the elliptic curve module, pleace consider, that it is + * implemented with two .c files: + * - ecp.c + * - ecp_curves.c + * You can replace them very much like all the other MBEDTLS__MODULE_NAME__ALT + * macros as described above. The only difference is that you have to make sure + * that you provide functionality for both .c files. + */ +//#define MBEDTLS_ECP_ALT + +/** + * \def MBEDTLS_MD2_PROCESS_ALT + * + * MBEDTLS__FUNCTION_NAME__ALT: Uncomment a macro to let mbed TLS use you + * alternate core implementation of symmetric crypto or hash function. Keep in + * mind that function prototypes should remain the same. + * + * This replaces only one function. The header file from mbed TLS is still + * used, in contrast to the MBEDTLS__MODULE_NAME__ALT flags. + * + * Example: In case you uncomment MBEDTLS_SHA256_PROCESS_ALT, mbed TLS will + * no longer provide the mbedtls_sha1_process() function, but it will still provide + * the other function (using your mbedtls_sha1_process() function) and the definition + * of mbedtls_sha1_context, so your implementation of mbedtls_sha1_process must be compatible + * with this definition. + * + * \note Because of a signature change, the core AES encryption and decryption routines are + * currently named mbedtls_aes_internal_encrypt and mbedtls_aes_internal_decrypt, + * respectively. When setting up alternative implementations, these functions should + * be overridden, but the wrapper functions mbedtls_aes_decrypt and mbedtls_aes_encrypt + * must stay untouched. + * + * \note If you use the AES_xxx_ALT macros, then it is recommended to also set + * MBEDTLS_AES_ROM_TABLES in order to help the linker garbage-collect the AES + * tables. + * + * Uncomment a macro to enable alternate implementation of the corresponding + * function. + * + * \warning MD2, MD4, MD5, DES and SHA-1 are considered weak and their use + * constitutes a security risk. If possible, we recommend avoiding + * dependencies on them, and considering stronger message digests + * and ciphers instead. + * + * \warning If both MBEDTLS_ECDSA_SIGN_ALT and MBEDTLS_ECDSA_DETERMINISTIC are + * enabled, then the deterministic ECDH signature functions pass the + * the static HMAC-DRBG as RNG to mbedtls_ecdsa_sign(). Therefore + * alternative implementations should use the RNG only for generating + * the ephemeral key and nothing else. If this is not possible, then + * MBEDTLS_ECDSA_DETERMINISTIC should be disabled and an alternative + * implementation should be provided for mbedtls_ecdsa_sign_det_ext() + * (and for mbedtls_ecdsa_sign_det() too if backward compatibility is + * desirable). + * + */ +//#define MBEDTLS_MD2_PROCESS_ALT +//#define MBEDTLS_MD4_PROCESS_ALT +//#define MBEDTLS_MD5_PROCESS_ALT +//#define MBEDTLS_RIPEMD160_PROCESS_ALT +//#define MBEDTLS_SHA1_PROCESS_ALT +//#define MBEDTLS_SHA256_PROCESS_ALT +//#define MBEDTLS_SHA512_PROCESS_ALT +//#define MBEDTLS_DES_SETKEY_ALT +//#define MBEDTLS_DES_CRYPT_ECB_ALT +//#define MBEDTLS_DES3_CRYPT_ECB_ALT +//#define MBEDTLS_AES_SETKEY_ENC_ALT +//#define MBEDTLS_AES_SETKEY_DEC_ALT +//#define MBEDTLS_AES_ENCRYPT_ALT +//#define MBEDTLS_AES_DECRYPT_ALT +//#define MBEDTLS_ECDH_GEN_PUBLIC_ALT +//#define MBEDTLS_ECDH_COMPUTE_SHARED_ALT +//#define MBEDTLS_ECDSA_VERIFY_ALT +//#define MBEDTLS_ECDSA_SIGN_ALT +//#define MBEDTLS_ECDSA_GENKEY_ALT + +/** + * \def MBEDTLS_ECP_INTERNAL_ALT + * + * Expose a part of the internal interface of the Elliptic Curve Point module. + * + * MBEDTLS_ECP__FUNCTION_NAME__ALT: Uncomment a macro to let mbed TLS use your + * alternative core implementation of elliptic curve arithmetic. Keep in mind + * that function prototypes should remain the same. + * + * This partially replaces one function. The header file from mbed TLS is still + * used, in contrast to the MBEDTLS_ECP_ALT flag. The original implementation + * is still present and it is used for group structures not supported by the + * alternative. + * + * Any of these options become available by defining MBEDTLS_ECP_INTERNAL_ALT + * and implementing the following functions: + * unsigned char mbedtls_internal_ecp_grp_capable( + * const mbedtls_ecp_group *grp ) + * int mbedtls_internal_ecp_init( const mbedtls_ecp_group *grp ) + * void mbedtls_internal_ecp_free( const mbedtls_ecp_group *grp ) + * The mbedtls_internal_ecp_grp_capable function should return 1 if the + * replacement functions implement arithmetic for the given group and 0 + * otherwise. + * The functions mbedtls_internal_ecp_init and mbedtls_internal_ecp_free are + * called before and after each point operation and provide an opportunity to + * implement optimized set up and tear down instructions. + * + * Example: In case you uncomment MBEDTLS_ECP_INTERNAL_ALT and + * MBEDTLS_ECP_DOUBLE_JAC_ALT, mbed TLS will still provide the ecp_double_jac + * function, but will use your mbedtls_internal_ecp_double_jac if the group is + * supported (your mbedtls_internal_ecp_grp_capable function returns 1 when + * receives it as an argument). If the group is not supported then the original + * implementation is used. The other functions and the definition of + * mbedtls_ecp_group and mbedtls_ecp_point will not change, so your + * implementation of mbedtls_internal_ecp_double_jac and + * mbedtls_internal_ecp_grp_capable must be compatible with this definition. + * + * Uncomment a macro to enable alternate implementation of the corresponding + * function. + */ +/* Required for all the functions in this section */ +//#define MBEDTLS_ECP_INTERNAL_ALT +/* Support for Weierstrass curves with Jacobi representation */ +//#define MBEDTLS_ECP_RANDOMIZE_JAC_ALT +//#define MBEDTLS_ECP_ADD_MIXED_ALT +//#define MBEDTLS_ECP_DOUBLE_JAC_ALT +//#define MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT +//#define MBEDTLS_ECP_NORMALIZE_JAC_ALT +/* Support for curves with Montgomery arithmetic */ +//#define MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT +//#define MBEDTLS_ECP_RANDOMIZE_MXZ_ALT +//#define MBEDTLS_ECP_NORMALIZE_MXZ_ALT + +/** + * \def MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN + * + * Enable testing of the constant-flow nature of some sensitive functions with + * clang's MemorySanitizer. This causes some existing tests to also test + * this non-functional property of the code under test. + * + * This setting requires compiling with clang -fsanitize=memory. The test + * suites can then be run normally. + * + * \warning This macro is only used for extended testing; it is not considered + * part of the library's API, so it may change or disappear at any time. + * + * Uncomment to enable testing of the constant-flow nature of selected code. + */ +//#define MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN + +/** + * \def MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND + * + * Enable testing of the constant-flow nature of some sensitive functions with + * valgrind's memcheck tool. This causes some existing tests to also test + * this non-functional property of the code under test. + * + * This setting requires valgrind headers for building, and is only useful for + * testing if the tests suites are run with valgrind's memcheck. This can be + * done for an individual test suite with 'valgrind ./test_suite_xxx', or when + * using CMake, this can be done for all test suites with 'make memcheck'. + * + * \warning This macro is only used for extended testing; it is not considered + * part of the library's API, so it may change or disappear at any time. + * + * Uncomment to enable testing of the constant-flow nature of selected code. + */ +//#define MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND + +/** + * \def MBEDTLS_TEST_NULL_ENTROPY + * + * Enables testing and use of mbed TLS without any configured entropy sources. + * This permits use of the library on platforms before an entropy source has + * been integrated (see for example the MBEDTLS_ENTROPY_HARDWARE_ALT or the + * MBEDTLS_ENTROPY_NV_SEED switches). + * + * WARNING! This switch MUST be disabled in production builds, and is suitable + * only for development. + * Enabling the switch negates any security provided by the library. + * + * Requires MBEDTLS_ENTROPY_C, MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES + * + */ +//#define MBEDTLS_TEST_NULL_ENTROPY + +/** + * \def MBEDTLS_ENTROPY_HARDWARE_ALT + * + * Uncomment this macro to let mbed TLS use your own implementation of a + * hardware entropy collector. + * + * Your function must be called \c mbedtls_hardware_poll(), have the same + * prototype as declared in entropy_poll.h, and accept NULL as first argument. + * + * Uncomment to use your own hardware entropy collector. + */ +//#define MBEDTLS_ENTROPY_HARDWARE_ALT + +/** + * \def MBEDTLS_AES_ROM_TABLES + * + * Use precomputed AES tables stored in ROM. + * + * Uncomment this macro to use precomputed AES tables stored in ROM. + * Comment this macro to generate AES tables in RAM at runtime. + * + * Tradeoff: Using precomputed ROM tables reduces RAM usage by ~8kb + * (or ~2kb if \c MBEDTLS_AES_FEWER_TABLES is used) and reduces the + * initialization time before the first AES operation can be performed. + * It comes at the cost of additional ~8kb ROM use (resp. ~2kb if \c + * MBEDTLS_AES_FEWER_TABLES below is used), and potentially degraded + * performance if ROM access is slower than RAM access. + * + * This option is independent of \c MBEDTLS_AES_FEWER_TABLES. + * + */ +//#define MBEDTLS_AES_ROM_TABLES + +/** + * \def MBEDTLS_AES_FEWER_TABLES + * + * Use less ROM/RAM for AES tables. + * + * Uncommenting this macro omits 75% of the AES tables from + * ROM / RAM (depending on the value of \c MBEDTLS_AES_ROM_TABLES) + * by computing their values on the fly during operations + * (the tables are entry-wise rotations of one another). + * + * Tradeoff: Uncommenting this reduces the RAM / ROM footprint + * by ~6kb but at the cost of more arithmetic operations during + * runtime. Specifically, one has to compare 4 accesses within + * different tables to 4 accesses with additional arithmetic + * operations within the same table. The performance gain/loss + * depends on the system and memory details. + * + * This option is independent of \c MBEDTLS_AES_ROM_TABLES. + * + */ +//#define MBEDTLS_AES_FEWER_TABLES + +/** + * \def MBEDTLS_CAMELLIA_SMALL_MEMORY + * + * Use less ROM for the Camellia implementation (saves about 768 bytes). + * + * Uncomment this macro to use less memory for Camellia. + */ +//#define MBEDTLS_CAMELLIA_SMALL_MEMORY + +/** + * \def MBEDTLS_CIPHER_MODE_CBC + * + * Enable Cipher Block Chaining mode (CBC) for symmetric ciphers. + */ +#define MBEDTLS_CIPHER_MODE_CBC + +/** + * \def MBEDTLS_CIPHER_MODE_CFB + * + * Enable Cipher Feedback mode (CFB) for symmetric ciphers. + */ +#define MBEDTLS_CIPHER_MODE_CFB + +/** + * \def MBEDTLS_CIPHER_MODE_CTR + * + * Enable Counter Block Cipher mode (CTR) for symmetric ciphers. + */ +#define MBEDTLS_CIPHER_MODE_CTR + +/** + * \def MBEDTLS_CIPHER_MODE_OFB + * + * Enable Output Feedback mode (OFB) for symmetric ciphers. + */ +#define MBEDTLS_CIPHER_MODE_OFB + +/** + * \def MBEDTLS_CIPHER_MODE_XTS + * + * Enable Xor-encrypt-xor with ciphertext stealing mode (XTS) for AES. + */ +#define MBEDTLS_CIPHER_MODE_XTS + +/** + * \def MBEDTLS_CIPHER_NULL_CIPHER + * + * Enable NULL cipher. + * Warning: Only do so when you know what you are doing. This allows for + * encryption or channels without any security! + * + * Requires MBEDTLS_ENABLE_WEAK_CIPHERSUITES as well to enable + * the following ciphersuites: + * MBEDTLS_TLS_ECDH_ECDSA_WITH_NULL_SHA + * MBEDTLS_TLS_ECDH_RSA_WITH_NULL_SHA + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_NULL_SHA + * MBEDTLS_TLS_ECDHE_RSA_WITH_NULL_SHA + * MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA384 + * MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA256 + * MBEDTLS_TLS_ECDHE_PSK_WITH_NULL_SHA + * MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA384 + * MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA256 + * MBEDTLS_TLS_DHE_PSK_WITH_NULL_SHA + * MBEDTLS_TLS_RSA_WITH_NULL_SHA256 + * MBEDTLS_TLS_RSA_WITH_NULL_SHA + * MBEDTLS_TLS_RSA_WITH_NULL_MD5 + * MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA384 + * MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA256 + * MBEDTLS_TLS_RSA_PSK_WITH_NULL_SHA + * MBEDTLS_TLS_PSK_WITH_NULL_SHA384 + * MBEDTLS_TLS_PSK_WITH_NULL_SHA256 + * MBEDTLS_TLS_PSK_WITH_NULL_SHA + * + * Uncomment this macro to enable the NULL cipher and ciphersuites + */ +//#define MBEDTLS_CIPHER_NULL_CIPHER + +/** + * \def MBEDTLS_CIPHER_PADDING_PKCS7 + * + * MBEDTLS_CIPHER_PADDING_XXX: Uncomment or comment macros to add support for + * specific padding modes in the cipher layer with cipher modes that support + * padding (e.g. CBC) + * + * If you disable all padding modes, only full blocks can be used with CBC. + * + * Enable padding modes in the cipher layer. + */ +#define MBEDTLS_CIPHER_PADDING_PKCS7 +#define MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS +#define MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN +#define MBEDTLS_CIPHER_PADDING_ZEROS + +/** \def MBEDTLS_CTR_DRBG_USE_128_BIT_KEY + * + * Uncomment this macro to use a 128-bit key in the CTR_DRBG module. + * By default, CTR_DRBG uses a 256-bit key. + */ +//#define MBEDTLS_CTR_DRBG_USE_128_BIT_KEY + +/** + * \def MBEDTLS_ENABLE_WEAK_CIPHERSUITES + * + * Enable weak ciphersuites in SSL / TLS. + * Warning: Only do so when you know what you are doing. This allows for + * channels with virtually no security at all! + * + * This enables the following ciphersuites: + * MBEDTLS_TLS_RSA_WITH_DES_CBC_SHA + * MBEDTLS_TLS_DHE_RSA_WITH_DES_CBC_SHA + * + * Uncomment this macro to enable weak ciphersuites + * + * \warning DES is considered a weak cipher and its use constitutes a + * security risk. We recommend considering stronger ciphers instead. + */ +//#define MBEDTLS_ENABLE_WEAK_CIPHERSUITES + +/** + * \def MBEDTLS_REMOVE_ARC4_CIPHERSUITES + * + * Remove RC4 ciphersuites by default in SSL / TLS. + * This flag removes the ciphersuites based on RC4 from the default list as + * returned by mbedtls_ssl_list_ciphersuites(). However, it is still possible to + * enable (some of) them with mbedtls_ssl_conf_ciphersuites() by including them + * explicitly. + * + * Uncomment this macro to remove RC4 ciphersuites by default. + */ +#define MBEDTLS_REMOVE_ARC4_CIPHERSUITES + +/** + * \def MBEDTLS_REMOVE_3DES_CIPHERSUITES + * + * Remove 3DES ciphersuites by default in SSL / TLS. + * This flag removes the ciphersuites based on 3DES from the default list as + * returned by mbedtls_ssl_list_ciphersuites(). However, it is still possible + * to enable (some of) them with mbedtls_ssl_conf_ciphersuites() by including + * them explicitly. + * + * A man-in-the-browser attacker can recover authentication tokens sent through + * a TLS connection using a 3DES based cipher suite (see "On the Practical + * (In-)Security of 64-bit Block Ciphers" by Karthikeyan Bhargavan and Gaëtan + * Leurent, see https://sweet32.info/SWEET32_CCS16.pdf). If this attack falls + * in your threat model or you are unsure, then you should keep this option + * enabled to remove 3DES based cipher suites. + * + * Comment this macro to keep 3DES in the default ciphersuite list. + */ +#define MBEDTLS_REMOVE_3DES_CIPHERSUITES + +/** + * \def MBEDTLS_ECP_DP_SECP192R1_ENABLED + * + * MBEDTLS_ECP_XXXX_ENABLED: Enables specific curves within the Elliptic Curve + * module. By default all supported curves are enabled. + * + * Comment macros to disable the curve and functions for it + */ +#define MBEDTLS_ECP_DP_SECP192R1_ENABLED +#define MBEDTLS_ECP_DP_SECP224R1_ENABLED +#define MBEDTLS_ECP_DP_SECP256R1_ENABLED +#define MBEDTLS_ECP_DP_SECP384R1_ENABLED +#define MBEDTLS_ECP_DP_SECP521R1_ENABLED +#define MBEDTLS_ECP_DP_SECP192K1_ENABLED +#define MBEDTLS_ECP_DP_SECP224K1_ENABLED +#define MBEDTLS_ECP_DP_SECP256K1_ENABLED +#define MBEDTLS_ECP_DP_BP256R1_ENABLED +#define MBEDTLS_ECP_DP_BP384R1_ENABLED +#define MBEDTLS_ECP_DP_BP512R1_ENABLED +#define MBEDTLS_ECP_DP_CURVE25519_ENABLED +#define MBEDTLS_ECP_DP_CURVE448_ENABLED + +/** + * \def MBEDTLS_ECP_NIST_OPTIM + * + * Enable specific 'modulo p' routines for each NIST prime. + * Depending on the prime and architecture, makes operations 4 to 8 times + * faster on the corresponding curve. + * + * Comment this macro to disable NIST curves optimisation. + */ +#define MBEDTLS_ECP_NIST_OPTIM + +/** + * \def MBEDTLS_ECP_NO_INTERNAL_RNG + * + * When this option is disabled, mbedtls_ecp_mul() will make use of an + * internal RNG when called with a NULL \c f_rng argument, in order to protect + * against some side-channel attacks. + * + * This protection introduces a dependency of the ECP module on one of the + * DRBG or SHA modules (HMAC-DRBG, CTR-DRBG, SHA-512 or SHA-256.) For very + * constrained applications that don't require this protection (for example, + * because you're only doing signature verification, so not manipulating any + * secret, or because local/physical side-channel attacks are outside your + * threat model), it might be desirable to get rid of that dependency. + * + * \warning Enabling this option makes some uses of ECP vulnerable to some + * side-channel attacks. Only enable it if you know that's not a problem for + * your use case. + * + * Uncomment this macro to disable some counter-measures in ECP. + */ +//#define MBEDTLS_ECP_NO_INTERNAL_RNG + +/** + * \def MBEDTLS_ECP_RESTARTABLE + * + * Enable "non-blocking" ECC operations that can return early and be resumed. + * + * This allows various functions to pause by returning + * #MBEDTLS_ERR_ECP_IN_PROGRESS (or, for functions in the SSL module, + * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) and then be called later again in + * order to further progress and eventually complete their operation. This is + * controlled through mbedtls_ecp_set_max_ops() which limits the maximum + * number of ECC operations a function may perform before pausing; see + * mbedtls_ecp_set_max_ops() for more information. + * + * This is useful in non-threaded environments if you want to avoid blocking + * for too long on ECC (and, hence, X.509 or SSL/TLS) operations. + * + * Uncomment this macro to enable restartable ECC computations. + * + * \note This option only works with the default software implementation of + * elliptic curve functionality. It is incompatible with + * MBEDTLS_ECP_ALT, MBEDTLS_ECDH_XXX_ALT and MBEDTLS_ECDSA_XXX_ALT. + */ +//#define MBEDTLS_ECP_RESTARTABLE + +/** + * \def MBEDTLS_ECDSA_DETERMINISTIC + * + * Enable deterministic ECDSA (RFC 6979). + * Standard ECDSA is "fragile" in the sense that lack of entropy when signing + * may result in a compromise of the long-term signing key. This is avoided by + * the deterministic variant. + * + * Requires: MBEDTLS_HMAC_DRBG_C + * + * Comment this macro to disable deterministic ECDSA. + */ +#define MBEDTLS_ECDSA_DETERMINISTIC + +/** + * \def MBEDTLS_KEY_EXCHANGE_PSK_ENABLED + * + * Enable the PSK based ciphersuite modes in SSL / TLS. + * + * This enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_PSK_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_PSK_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_PSK_WITH_RC4_128_SHA + */ +#define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED + +/** + * \def MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED + * + * Enable the DHE-PSK based ciphersuite modes in SSL / TLS. + * + * Requires: MBEDTLS_DHM_C + * + * This enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_DHE_PSK_WITH_RC4_128_SHA + * + * \warning Using DHE constitutes a security risk as it + * is not possible to validate custom DH parameters. + * If possible, it is recommended users should consider + * preferring other methods of key exchange. + * See dhm.h for more details. + * + */ +#define MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED + +/** + * \def MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED + * + * Enable the ECDHE-PSK based ciphersuite modes in SSL / TLS. + * + * Requires: MBEDTLS_ECDH_C + * + * This enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_ECDHE_PSK_WITH_RC4_128_SHA + */ +#define MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED + +/** + * \def MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED + * + * Enable the RSA-PSK based ciphersuite modes in SSL / TLS. + * + * Requires: MBEDTLS_RSA_C, MBEDTLS_PKCS1_V15, + * MBEDTLS_X509_CRT_PARSE_C + * + * This enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_RSA_PSK_WITH_RC4_128_SHA + */ +#define MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED + +/** + * \def MBEDTLS_KEY_EXCHANGE_RSA_ENABLED + * + * Enable the RSA-only based ciphersuite modes in SSL / TLS. + * + * Requires: MBEDTLS_RSA_C, MBEDTLS_PKCS1_V15, + * MBEDTLS_X509_CRT_PARSE_C + * + * This enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256 + * MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA + * MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA + * MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_RSA_WITH_RC4_128_SHA + * MBEDTLS_TLS_RSA_WITH_RC4_128_MD5 + */ +#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED + +/** + * \def MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED + * + * Enable the DHE-RSA based ciphersuite modes in SSL / TLS. + * + * Requires: MBEDTLS_DHM_C, MBEDTLS_RSA_C, MBEDTLS_PKCS1_V15, + * MBEDTLS_X509_CRT_PARSE_C + * + * This enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA + * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA + * MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA + * + * \warning Using DHE constitutes a security risk as it + * is not possible to validate custom DH parameters. + * If possible, it is recommended users should consider + * preferring other methods of key exchange. + * See dhm.h for more details. + * + */ +#define MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED + +/** + * \def MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED + * + * Enable the ECDHE-RSA based ciphersuite modes in SSL / TLS. + * + * Requires: MBEDTLS_ECDH_C, MBEDTLS_RSA_C, MBEDTLS_PKCS1_V15, + * MBEDTLS_X509_CRT_PARSE_C + * + * This enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_ECDHE_RSA_WITH_RC4_128_SHA + */ +#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED + +/** + * \def MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED + * + * Enable the ECDHE-ECDSA based ciphersuite modes in SSL / TLS. + * + * Requires: MBEDTLS_ECDH_C, MBEDTLS_ECDSA_C, MBEDTLS_X509_CRT_PARSE_C, + * + * This enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA + */ +#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED + +/** + * \def MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED + * + * Enable the ECDH-ECDSA based ciphersuite modes in SSL / TLS. + * + * Requires: MBEDTLS_ECDH_C, MBEDTLS_X509_CRT_PARSE_C + * + * This enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_ECDH_ECDSA_WITH_RC4_128_SHA + * MBEDTLS_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 + */ +#define MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED + +/** + * \def MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED + * + * Enable the ECDH-RSA based ciphersuite modes in SSL / TLS. + * + * Requires: MBEDTLS_ECDH_C, MBEDTLS_X509_CRT_PARSE_C + * + * This enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_ECDH_RSA_WITH_RC4_128_SHA + * MBEDTLS_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384 + */ +#define MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED + +/** + * \def MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED + * + * Enable the ECJPAKE based ciphersuite modes in SSL / TLS. + * + * \warning This is currently experimental. EC J-PAKE support is based on the + * Thread v1.0.0 specification; incompatible changes to the specification + * might still happen. For this reason, this is disabled by default. + * + * Requires: MBEDTLS_ECJPAKE_C + * MBEDTLS_SHA256_C + * MBEDTLS_ECP_DP_SECP256R1_ENABLED + * + * This enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8 + */ +//#define MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED + +/** + * \def MBEDTLS_PK_PARSE_EC_EXTENDED + * + * Enhance support for reading EC keys using variants of SEC1 not allowed by + * RFC 5915 and RFC 5480. + * + * Currently this means parsing the SpecifiedECDomain choice of EC + * parameters (only known groups are supported, not arbitrary domains, to + * avoid validation issues). + * + * Disable if you only need to support RFC 5915 + 5480 key formats. + */ +#define MBEDTLS_PK_PARSE_EC_EXTENDED + +/** + * \def MBEDTLS_ERROR_STRERROR_DUMMY + * + * Enable a dummy error function to make use of mbedtls_strerror() in + * third party libraries easier when MBEDTLS_ERROR_C is disabled + * (no effect when MBEDTLS_ERROR_C is enabled). + * + * You can safely disable this if MBEDTLS_ERROR_C is enabled, or if you're + * not using mbedtls_strerror() or error_strerror() in your application. + * + * Disable if you run into name conflicts and want to really remove the + * mbedtls_strerror() + */ +#define MBEDTLS_ERROR_STRERROR_DUMMY + +/** + * \def MBEDTLS_GENPRIME + * + * Enable the prime-number generation code. + * + * Requires: MBEDTLS_BIGNUM_C + */ +#define MBEDTLS_GENPRIME + +/** + * \def MBEDTLS_FS_IO + * + * Enable functions that use the filesystem. + */ +#define MBEDTLS_FS_IO + +/** + * \def MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES + * + * Do not add default entropy sources. These are the platform specific, + * mbedtls_timing_hardclock and HAVEGE based poll functions. + * + * This is useful to have more control over the added entropy sources in an + * application. + * + * Uncomment this macro to prevent loading of default entropy functions. + */ +//#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES + +/** + * \def MBEDTLS_NO_PLATFORM_ENTROPY + * + * Do not use built-in platform entropy functions. + * This is useful if your platform does not support + * standards like the /dev/urandom or Windows CryptoAPI. + * + * Uncomment this macro to disable the built-in platform entropy functions. + */ +//#define MBEDTLS_NO_PLATFORM_ENTROPY + +/** + * \def MBEDTLS_ENTROPY_FORCE_SHA256 + * + * Force the entropy accumulator to use a SHA-256 accumulator instead of the + * default SHA-512 based one (if both are available). + * + * Requires: MBEDTLS_SHA256_C + * + * On 32-bit systems SHA-256 can be much faster than SHA-512. Use this option + * if you have performance concerns. + * + * This option is only useful if both MBEDTLS_SHA256_C and + * MBEDTLS_SHA512_C are defined. Otherwise the available hash module is used. + */ +//#define MBEDTLS_ENTROPY_FORCE_SHA256 + +/** + * \def MBEDTLS_ENTROPY_NV_SEED + * + * Enable the non-volatile (NV) seed file-based entropy source. + * (Also enables the NV seed read/write functions in the platform layer) + * + * This is crucial (if not required) on systems that do not have a + * cryptographic entropy source (in hardware or kernel) available. + * + * Requires: MBEDTLS_ENTROPY_C, MBEDTLS_PLATFORM_C + * + * \note The read/write functions that are used by the entropy source are + * determined in the platform layer, and can be modified at runtime and/or + * compile-time depending on the flags (MBEDTLS_PLATFORM_NV_SEED_*) used. + * + * \note If you use the default implementation functions that read a seedfile + * with regular fopen(), please make sure you make a seedfile with the + * proper name (defined in MBEDTLS_PLATFORM_STD_NV_SEED_FILE) and at + * least MBEDTLS_ENTROPY_BLOCK_SIZE bytes in size that can be read from + * and written to or you will get an entropy source error! The default + * implementation will only use the first MBEDTLS_ENTROPY_BLOCK_SIZE + * bytes from the file. + * + * \note The entropy collector will write to the seed file before entropy is + * given to an external source, to update it. + */ +//#define MBEDTLS_ENTROPY_NV_SEED + +/** + * \def MBEDTLS_MEMORY_DEBUG + * + * Enable debugging of buffer allocator memory issues. Automatically prints + * (to stderr) all (fatal) messages on memory allocation issues. Enables + * function for 'debug output' of allocated memory. + * + * Requires: MBEDTLS_MEMORY_BUFFER_ALLOC_C + * + * Uncomment this macro to let the buffer allocator print out error messages. + */ +//#define MBEDTLS_MEMORY_DEBUG + +/** + * \def MBEDTLS_MEMORY_BACKTRACE + * + * Include backtrace information with each allocated block. + * + * Requires: MBEDTLS_MEMORY_BUFFER_ALLOC_C + * GLIBC-compatible backtrace() an backtrace_symbols() support + * + * Uncomment this macro to include backtrace information + */ +//#define MBEDTLS_MEMORY_BACKTRACE + +/** + * \def MBEDTLS_PK_RSA_ALT_SUPPORT + * + * Support external private RSA keys (eg from a HSM) in the PK layer. + * + * Comment this macro to disable support for external private RSA keys. + */ +#define MBEDTLS_PK_RSA_ALT_SUPPORT + +/** + * \def MBEDTLS_PKCS1_V15 + * + * Enable support for PKCS#1 v1.5 encoding. + * + * Requires: MBEDTLS_RSA_C + * + * This enables support for PKCS#1 v1.5 operations. + */ +#define MBEDTLS_PKCS1_V15 + +/** + * \def MBEDTLS_PKCS1_V21 + * + * Enable support for PKCS#1 v2.1 encoding. + * + * Requires: MBEDTLS_MD_C, MBEDTLS_RSA_C + * + * This enables support for RSAES-OAEP and RSASSA-PSS operations. + */ +#define MBEDTLS_PKCS1_V21 + +/** + * \def MBEDTLS_RSA_NO_CRT + * + * Do not use the Chinese Remainder Theorem + * for the RSA private operation. + * + * Uncomment this macro to disable the use of CRT in RSA. + * + */ +//#define MBEDTLS_RSA_NO_CRT + +/** + * \def MBEDTLS_SELF_TEST + * + * Enable the checkup functions (*_self_test). + */ +// #define MBEDTLS_SELF_TEST + +/** + * \def MBEDTLS_SHA256_SMALLER + * + * Enable an implementation of SHA-256 that has lower ROM footprint but also + * lower performance. + * + * The default implementation is meant to be a reasonnable compromise between + * performance and size. This version optimizes more aggressively for size at + * the expense of performance. Eg on Cortex-M4 it reduces the size of + * mbedtls_sha256_process() from ~2KB to ~0.5KB for a performance hit of about + * 30%. + * + * Uncomment to enable the smaller implementation of SHA256. + */ +//#define MBEDTLS_SHA256_SMALLER + +/** + * \def MBEDTLS_SSL_ALL_ALERT_MESSAGES + * + * Enable sending of alert messages in case of encountered errors as per RFC. + * If you choose not to send the alert messages, mbed TLS can still communicate + * with other servers, only debugging of failures is harder. + * + * The advantage of not sending alert messages, is that no information is given + * about reasons for failures thus preventing adversaries of gaining intel. + * + * Enable sending of all alert messages + */ +#define MBEDTLS_SSL_ALL_ALERT_MESSAGES + +/** + * \def MBEDTLS_SSL_ASYNC_PRIVATE + * + * Enable asynchronous external private key operations in SSL. This allows + * you to configure an SSL connection to call an external cryptographic + * module to perform private key operations instead of performing the + * operation inside the library. + * + */ +//#define MBEDTLS_SSL_ASYNC_PRIVATE + +/** + * \def MBEDTLS_SSL_DEBUG_ALL + * + * Enable the debug messages in SSL module for all issues. + * Debug messages have been disabled in some places to prevent timing + * attacks due to (unbalanced) debugging function calls. + * + * If you need all error reporting you should enable this during debugging, + * but remove this for production servers that should log as well. + * + * Uncomment this macro to report all debug messages on errors introducing + * a timing side-channel. + * + */ +//#define MBEDTLS_SSL_DEBUG_ALL + +/** \def MBEDTLS_SSL_ENCRYPT_THEN_MAC + * + * Enable support for Encrypt-then-MAC, RFC 7366. + * + * This allows peers that both support it to use a more robust protection for + * ciphersuites using CBC, providing deep resistance against timing attacks + * on the padding or underlying cipher. + * + * This only affects CBC ciphersuites, and is useless if none is defined. + * + * Requires: MBEDTLS_SSL_PROTO_TLS1 or + * MBEDTLS_SSL_PROTO_TLS1_1 or + * MBEDTLS_SSL_PROTO_TLS1_2 + * + * Comment this macro to disable support for Encrypt-then-MAC + */ +#define MBEDTLS_SSL_ENCRYPT_THEN_MAC + +/** \def MBEDTLS_SSL_EXTENDED_MASTER_SECRET + * + * Enable support for Extended Master Secret, aka Session Hash + * (draft-ietf-tls-session-hash-02). + * + * This was introduced as "the proper fix" to the Triple Handshake familiy of + * attacks, but it is recommended to always use it (even if you disable + * renegotiation), since it actually fixes a more fundamental issue in the + * original SSL/TLS design, and has implications beyond Triple Handshake. + * + * Requires: MBEDTLS_SSL_PROTO_TLS1 or + * MBEDTLS_SSL_PROTO_TLS1_1 or + * MBEDTLS_SSL_PROTO_TLS1_2 + * + * Comment this macro to disable support for Extended Master Secret. + */ +#define MBEDTLS_SSL_EXTENDED_MASTER_SECRET + +/** + * \def MBEDTLS_SSL_FALLBACK_SCSV + * + * Enable support for FALLBACK_SCSV (draft-ietf-tls-downgrade-scsv-00). + * + * For servers, it is recommended to always enable this, unless you support + * only one version of TLS, or know for sure that none of your clients + * implements a fallback strategy. + * + * For clients, you only need this if you're using a fallback strategy, which + * is not recommended in the first place, unless you absolutely need it to + * interoperate with buggy (version-intolerant) servers. + * + * Comment this macro to disable support for FALLBACK_SCSV + */ +#define MBEDTLS_SSL_FALLBACK_SCSV + +/** + * \def MBEDTLS_SSL_HW_RECORD_ACCEL + * + * Enable hooking functions in SSL module for hardware acceleration of + * individual records. + * + * Uncomment this macro to enable hooking functions. + */ +//#define MBEDTLS_SSL_HW_RECORD_ACCEL + +/** + * \def MBEDTLS_SSL_CBC_RECORD_SPLITTING + * + * Enable 1/n-1 record splitting for CBC mode in SSLv3 and TLS 1.0. + * + * This is a countermeasure to the BEAST attack, which also minimizes the risk + * of interoperability issues compared to sending 0-length records. + * + * Comment this macro to disable 1/n-1 record splitting. + */ +#define MBEDTLS_SSL_CBC_RECORD_SPLITTING + +/** + * \def MBEDTLS_SSL_RENEGOTIATION + * + * Enable support for TLS renegotiation. + * + * The two main uses of renegotiation are (1) refresh keys on long-lived + * connections and (2) client authentication after the initial handshake. + * If you don't need renegotiation, it's probably better to disable it, since + * it has been associated with security issues in the past and is easy to + * misuse/misunderstand. + * + * Comment this to disable support for renegotiation. + * + * \note Even if this option is disabled, both client and server are aware + * of the Renegotiation Indication Extension (RFC 5746) used to + * prevent the SSL renegotiation attack (see RFC 5746 Sect. 1). + * (See \c mbedtls_ssl_conf_legacy_renegotiation for the + * configuration of this extension). + * + */ +#define MBEDTLS_SSL_RENEGOTIATION + +/** + * \def MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO + * + * Enable support for receiving and parsing SSLv2 Client Hello messages for the + * SSL Server module (MBEDTLS_SSL_SRV_C). + * + * Uncomment this macro to enable support for SSLv2 Client Hello messages. + */ +//#define MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO + +/** + * \def MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE + * + * Pick the ciphersuite according to the client's preferences rather than ours + * in the SSL Server module (MBEDTLS_SSL_SRV_C). + * + * Uncomment this macro to respect client's ciphersuite order + */ +//#define MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE + +/** + * \def MBEDTLS_SSL_MAX_FRAGMENT_LENGTH + * + * Enable support for RFC 6066 max_fragment_length extension in SSL. + * + * Comment this macro to disable support for the max_fragment_length extension + */ +#define MBEDTLS_SSL_MAX_FRAGMENT_LENGTH + +/** + * \def MBEDTLS_SSL_PROTO_SSL3 + * + * Enable support for SSL 3.0. + * + * Requires: MBEDTLS_MD5_C + * MBEDTLS_SHA1_C + * + * Comment this macro to disable support for SSL 3.0 + */ +//#define MBEDTLS_SSL_PROTO_SSL3 + +/** + * \def MBEDTLS_SSL_PROTO_TLS1 + * + * Enable support for TLS 1.0. + * + * Requires: MBEDTLS_MD5_C + * MBEDTLS_SHA1_C + * + * Comment this macro to disable support for TLS 1.0 + */ +#define MBEDTLS_SSL_PROTO_TLS1 + +/** + * \def MBEDTLS_SSL_PROTO_TLS1_1 + * + * Enable support for TLS 1.1 (and DTLS 1.0 if DTLS is enabled). + * + * Requires: MBEDTLS_MD5_C + * MBEDTLS_SHA1_C + * + * Comment this macro to disable support for TLS 1.1 / DTLS 1.0 + */ +#define MBEDTLS_SSL_PROTO_TLS1_1 + +/** + * \def MBEDTLS_SSL_PROTO_TLS1_2 + * + * Enable support for TLS 1.2 (and DTLS 1.2 if DTLS is enabled). + * + * Requires: MBEDTLS_SHA1_C or MBEDTLS_SHA256_C or MBEDTLS_SHA512_C + * (Depends on ciphersuites) + * + * Comment this macro to disable support for TLS 1.2 / DTLS 1.2 + */ +#define MBEDTLS_SSL_PROTO_TLS1_2 + +/** + * \def MBEDTLS_SSL_PROTO_DTLS + * + * Enable support for DTLS (all available versions). + * + * Enable this and MBEDTLS_SSL_PROTO_TLS1_1 to enable DTLS 1.0, + * and/or this and MBEDTLS_SSL_PROTO_TLS1_2 to enable DTLS 1.2. + * + * Requires: MBEDTLS_SSL_PROTO_TLS1_1 + * or MBEDTLS_SSL_PROTO_TLS1_2 + * + * Comment this macro to disable support for DTLS + */ +#define MBEDTLS_SSL_PROTO_DTLS + +/** + * \def MBEDTLS_SSL_ALPN + * + * Enable support for RFC 7301 Application Layer Protocol Negotiation. + * + * Comment this macro to disable support for ALPN. + */ +#define MBEDTLS_SSL_ALPN + +/** + * \def MBEDTLS_SSL_DTLS_ANTI_REPLAY + * + * Enable support for the anti-replay mechanism in DTLS. + * + * Requires: MBEDTLS_SSL_TLS_C + * MBEDTLS_SSL_PROTO_DTLS + * + * \warning Disabling this is often a security risk! + * See mbedtls_ssl_conf_dtls_anti_replay() for details. + * + * Comment this to disable anti-replay in DTLS. + */ +#define MBEDTLS_SSL_DTLS_ANTI_REPLAY + +/** + * \def MBEDTLS_SSL_DTLS_HELLO_VERIFY + * + * Enable support for HelloVerifyRequest on DTLS servers. + * + * This feature is highly recommended to prevent DTLS servers being used as + * amplifiers in DoS attacks against other hosts. It should always be enabled + * unless you know for sure amplification cannot be a problem in the + * environment in which your server operates. + * + * \warning Disabling this can ba a security risk! (see above) + * + * Requires: MBEDTLS_SSL_PROTO_DTLS + * + * Comment this to disable support for HelloVerifyRequest. + */ +#define MBEDTLS_SSL_DTLS_HELLO_VERIFY + +/** + * \def MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE + * + * Enable server-side support for clients that reconnect from the same port. + * + * Some clients unexpectedly close the connection and try to reconnect using the + * same source port. This needs special support from the server to handle the + * new connection securely, as described in section 4.2.8 of RFC 6347. This + * flag enables that support. + * + * Requires: MBEDTLS_SSL_DTLS_HELLO_VERIFY + * + * Comment this to disable support for clients reusing the source port. + */ +#define MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE + +/** + * \def MBEDTLS_SSL_DTLS_BADMAC_LIMIT + * + * Enable support for a limit of records with bad MAC. + * + * See mbedtls_ssl_conf_dtls_badmac_limit(). + * + * Requires: MBEDTLS_SSL_PROTO_DTLS + */ +#define MBEDTLS_SSL_DTLS_BADMAC_LIMIT + +/** + * \def MBEDTLS_SSL_SESSION_TICKETS + * + * Enable support for RFC 5077 session tickets in SSL. + * Client-side, provides full support for session tickets (maintenance of a + * session store remains the responsibility of the application, though). + * Server-side, you also need to provide callbacks for writing and parsing + * tickets, including authenticated encryption and key management. Example + * callbacks are provided by MBEDTLS_SSL_TICKET_C. + * + * Comment this macro to disable support for SSL session tickets + */ +#define MBEDTLS_SSL_SESSION_TICKETS + +/** + * \def MBEDTLS_SSL_EXPORT_KEYS + * + * Enable support for exporting key block and master secret. + * This is required for certain users of TLS, e.g. EAP-TLS. + * + * Comment this macro to disable support for key export + */ +#define MBEDTLS_SSL_EXPORT_KEYS + +/** + * \def MBEDTLS_SSL_SERVER_NAME_INDICATION + * + * Enable support for RFC 6066 server name indication (SNI) in SSL. + * + * Requires: MBEDTLS_X509_CRT_PARSE_C + * + * Comment this macro to disable support for server name indication in SSL + */ +#define MBEDTLS_SSL_SERVER_NAME_INDICATION + +/** + * \def MBEDTLS_SSL_TRUNCATED_HMAC + * + * Enable support for RFC 6066 truncated HMAC in SSL. + * + * Comment this macro to disable support for truncated HMAC in SSL + */ +#define MBEDTLS_SSL_TRUNCATED_HMAC + +/** + * \def MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT + * + * Fallback to old (pre-2.7), non-conforming implementation of the truncated + * HMAC extension which also truncates the HMAC key. Note that this option is + * only meant for a transitory upgrade period and is likely to be removed in + * a future version of the library. + * + * \warning The old implementation is non-compliant and has a security weakness + * (2^80 brute force attack on the HMAC key used for a single, + * uninterrupted connection). This should only be enabled temporarily + * when (1) the use of truncated HMAC is essential in order to save + * bandwidth, and (2) the peer is an Mbed TLS stack that doesn't use + * the fixed implementation yet (pre-2.7). + * + * \deprecated This option is deprecated and will likely be removed in a + * future version of Mbed TLS. + * + * Uncomment to fallback to old, non-compliant truncated HMAC implementation. + * + * Requires: MBEDTLS_SSL_TRUNCATED_HMAC + */ +//#define MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT + +/** + * \def MBEDTLS_TEST_HOOKS + * + * Enable features for invasive testing such as introspection functions and + * hooks for fault injection. This enables additional unit tests. + * + * Merely enabling this feature should not change the behavior of the product. + * It only adds new code, and new branching points where the default behavior + * is the same as when this feature is disabled. + * However, this feature increases the attack surface: there is an added + * risk of vulnerabilities, and more gadgets that can make exploits easier. + * Therefore this feature must never be enabled in production. + * + * Uncomment to enable invasive tests. + */ +//#define MBEDTLS_TEST_HOOKS + +/** + * \def MBEDTLS_THREADING_ALT + * + * Provide your own alternate threading implementation. + * + * Requires: MBEDTLS_THREADING_C + * + * Uncomment this to allow your own alternate threading implementation. + */ +//#define MBEDTLS_THREADING_ALT + +/** + * \def MBEDTLS_THREADING_PTHREAD + * + * Enable the pthread wrapper layer for the threading layer. + * + * Requires: MBEDTLS_THREADING_C + * + * Uncomment this to enable pthread mutexes. + */ +//#define MBEDTLS_THREADING_PTHREAD + +/** + * \def MBEDTLS_VERSION_FEATURES + * + * Allow run-time checking of compile-time enabled features. Thus allowing users + * to check at run-time if the library is for instance compiled with threading + * support via mbedtls_version_check_feature(). + * + * Requires: MBEDTLS_VERSION_C + * + * Comment this to disable run-time checking and save ROM space + */ +#define MBEDTLS_VERSION_FEATURES + +/** + * \def MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3 + * + * If set, the X509 parser will not break-off when parsing an X509 certificate + * and encountering an extension in a v1 or v2 certificate. + * + * Uncomment to prevent an error. + */ +//#define MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3 + +/** + * \def MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION + * + * If set, the X509 parser will not break-off when parsing an X509 certificate + * and encountering an unknown critical extension. + * + * \warning Depending on your PKI use, enabling this can be a security risk! + * + * Uncomment to prevent an error. + */ +//#define MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION + +/** + * \def MBEDTLS_X509_CHECK_KEY_USAGE + * + * Enable verification of the keyUsage extension (CA and leaf certificates). + * + * Disabling this avoids problems with mis-issued and/or misused + * (intermediate) CA and leaf certificates. + * + * \warning Depending on your PKI use, disabling this can be a security risk! + * + * Comment to skip keyUsage checking for both CA and leaf certificates. + */ +#define MBEDTLS_X509_CHECK_KEY_USAGE + +/** + * \def MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE + * + * Enable verification of the extendedKeyUsage extension (leaf certificates). + * + * Disabling this avoids problems with mis-issued and/or misused certificates. + * + * \warning Depending on your PKI use, disabling this can be a security risk! + * + * Comment to skip extendedKeyUsage checking for certificates. + */ +#define MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE + +/** + * \def MBEDTLS_X509_RSASSA_PSS_SUPPORT + * + * Enable parsing and verification of X.509 certificates, CRLs and CSRS + * signed with RSASSA-PSS (aka PKCS#1 v2.1). + * + * Comment this macro to disallow using RSASSA-PSS in certificates. + */ +#define MBEDTLS_X509_RSASSA_PSS_SUPPORT + +/** + * \def MBEDTLS_ZLIB_SUPPORT + * + * If set, the SSL/TLS module uses ZLIB to support compression and + * decompression of packet data. + * + * \warning TLS-level compression MAY REDUCE SECURITY! See for example the + * CRIME attack. Before enabling this option, you should examine with care if + * CRIME or similar exploits may be applicable to your use case. + * + * \note Currently compression can't be used with DTLS. + * + * \deprecated This feature is deprecated and will be removed + * in the next major revision of the library. + * + * Used in: library/ssl_tls.c + * library/ssl_cli.c + * library/ssl_srv.c + * + * This feature requires zlib library and headers to be present. + * + * Uncomment to enable use of ZLIB + */ +//#define MBEDTLS_ZLIB_SUPPORT +/* \} name SECTION: mbed TLS feature support */ + +/** + * \name SECTION: mbed TLS modules + * + * This section enables or disables entire modules in mbed TLS + * \{ + */ + +/** + * \def MBEDTLS_AESNI_C + * + * Enable AES-NI support on x86-64. + * + * Module: library/aesni.c + * Caller: library/aes.c + * + * Requires: MBEDTLS_HAVE_ASM + * + * This modules adds support for the AES-NI instructions on x86-64 + */ +// #define MBEDTLS_AESNI_C + +/** + * \def MBEDTLS_AES_C + * + * Enable the AES block cipher. + * + * Module: library/aes.c + * Caller: library/cipher.c + * library/pem.c + * library/ctr_drbg.c + * + * This module enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_DHE_RSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_DHE_PSK_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_DHE_PSK_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256 + * MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_RSA_PSK_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_RSA_PSK_WITH_AES_128_CBC_SHA + * MBEDTLS_TLS_PSK_WITH_AES_256_GCM_SHA384 + * MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA384 + * MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA + * MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256 + * MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256 + * MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA + * + * PEM_PARSE uses AES for decrypting encrypted keys. + */ +#define MBEDTLS_AES_C + +/** + * \def MBEDTLS_ARC4_C + * + * Enable the ARCFOUR stream cipher. + * + * Module: library/arc4.c + * Caller: library/cipher.c + * + * This module enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_ECDH_ECDSA_WITH_RC4_128_SHA + * MBEDTLS_TLS_ECDH_RSA_WITH_RC4_128_SHA + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA + * MBEDTLS_TLS_ECDHE_RSA_WITH_RC4_128_SHA + * MBEDTLS_TLS_ECDHE_PSK_WITH_RC4_128_SHA + * MBEDTLS_TLS_DHE_PSK_WITH_RC4_128_SHA + * MBEDTLS_TLS_RSA_WITH_RC4_128_SHA + * MBEDTLS_TLS_RSA_WITH_RC4_128_MD5 + * MBEDTLS_TLS_RSA_PSK_WITH_RC4_128_SHA + * MBEDTLS_TLS_PSK_WITH_RC4_128_SHA + * + * \warning ARC4 is considered a weak cipher and its use constitutes a + * security risk. If possible, we recommend avoidng dependencies on + * it, and considering stronger ciphers instead. + * + */ +#define MBEDTLS_ARC4_C + +/** + * \def MBEDTLS_ASN1_PARSE_C + * + * Enable the generic ASN1 parser. + * + * Module: library/asn1.c + * Caller: library/x509.c + * library/dhm.c + * library/pkcs12.c + * library/pkcs5.c + * library/pkparse.c + */ +#define MBEDTLS_ASN1_PARSE_C + +/** + * \def MBEDTLS_ASN1_WRITE_C + * + * Enable the generic ASN1 writer. + * + * Module: library/asn1write.c + * Caller: library/ecdsa.c + * library/pkwrite.c + * library/x509_create.c + * library/x509write_crt.c + * library/x509write_csr.c + */ +#define MBEDTLS_ASN1_WRITE_C + +/** + * \def MBEDTLS_BASE64_C + * + * Enable the Base64 module. + * + * Module: library/base64.c + * Caller: library/pem.c + * + * This module is required for PEM support (required by X.509). + */ +#define MBEDTLS_BASE64_C + +/** + * \def MBEDTLS_BIGNUM_C + * + * Enable the multi-precision integer library. + * + * Module: library/bignum.c + * Caller: library/dhm.c + * library/ecp.c + * library/ecdsa.c + * library/rsa.c + * library/rsa_internal.c + * library/ssl_tls.c + * + * This module is required for RSA, DHM and ECC (ECDH, ECDSA) support. + */ +#define MBEDTLS_BIGNUM_C + +/** + * \def MBEDTLS_BLOWFISH_C + * + * Enable the Blowfish block cipher. + * + * Module: library/blowfish.c + */ +#define MBEDTLS_BLOWFISH_C + +/** + * \def MBEDTLS_CAMELLIA_C + * + * Enable the Camellia block cipher. + * + * Module: library/camellia.c + * Caller: library/cipher.c + * + * This module enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA + * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA + * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256 + * MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384 + * MBEDTLS_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384 + * MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256 + * MBEDTLS_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256 + */ +#define MBEDTLS_CAMELLIA_C + +/** + * \def MBEDTLS_ARIA_C + * + * Enable the ARIA block cipher. + * + * Module: library/aria.c + * Caller: library/cipher.c + * + * This module enables the following ciphersuites (if other requisites are + * enabled as well): + * + * MBEDTLS_TLS_RSA_WITH_ARIA_128_CBC_SHA256 + * MBEDTLS_TLS_RSA_WITH_ARIA_256_CBC_SHA384 + * MBEDTLS_TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384 + * MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384 + * MBEDTLS_TLS_RSA_WITH_ARIA_128_GCM_SHA256 + * MBEDTLS_TLS_RSA_WITH_ARIA_256_GCM_SHA384 + * MBEDTLS_TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256 + * MBEDTLS_TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256 + * MBEDTLS_TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384 + * MBEDTLS_TLS_PSK_WITH_ARIA_128_CBC_SHA256 + * MBEDTLS_TLS_PSK_WITH_ARIA_256_CBC_SHA384 + * MBEDTLS_TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256 + * MBEDTLS_TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384 + * MBEDTLS_TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256 + * MBEDTLS_TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384 + * MBEDTLS_TLS_PSK_WITH_ARIA_128_GCM_SHA256 + * MBEDTLS_TLS_PSK_WITH_ARIA_256_GCM_SHA384 + * MBEDTLS_TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256 + * MBEDTLS_TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384 + * MBEDTLS_TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256 + * MBEDTLS_TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384 + * MBEDTLS_TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256 + * MBEDTLS_TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384 + */ +//#define MBEDTLS_ARIA_C + +/** + * \def MBEDTLS_CCM_C + * + * Enable the Counter with CBC-MAC (CCM) mode for 128-bit block cipher. + * + * Module: library/ccm.c + * + * Requires: MBEDTLS_AES_C or MBEDTLS_CAMELLIA_C + * + * This module enables the AES-CCM ciphersuites, if other requisites are + * enabled as well. + */ +#define MBEDTLS_CCM_C + +/** + * \def MBEDTLS_CERTS_C + * + * Enable the test certificates. + * + * Module: library/certs.c + * Caller: + * + * This module is used for testing (ssl_client/server). + */ +#define MBEDTLS_CERTS_C + +/** + * \def MBEDTLS_CHACHA20_C + * + * Enable the ChaCha20 stream cipher. + * + * Module: library/chacha20.c + */ +#define MBEDTLS_CHACHA20_C + +/** + * \def MBEDTLS_CHACHAPOLY_C + * + * Enable the ChaCha20-Poly1305 AEAD algorithm. + * + * Module: library/chachapoly.c + * + * This module requires: MBEDTLS_CHACHA20_C, MBEDTLS_POLY1305_C + */ +#define MBEDTLS_CHACHAPOLY_C + +/** + * \def MBEDTLS_CIPHER_C + * + * Enable the generic cipher layer. + * + * Module: library/cipher.c + * Caller: library/ssl_tls.c + * + * Uncomment to enable generic cipher wrappers. + */ +#define MBEDTLS_CIPHER_C + +/** + * \def MBEDTLS_CMAC_C + * + * Enable the CMAC (Cipher-based Message Authentication Code) mode for block + * ciphers. + * + * Module: library/cmac.c + * + * Requires: MBEDTLS_AES_C or MBEDTLS_DES_C + * + */ +//#define MBEDTLS_CMAC_C + +/** + * \def MBEDTLS_CTR_DRBG_C + * + * Enable the CTR_DRBG AES-based random generator. + * The CTR_DRBG generator uses AES-256 by default. + * To use AES-128 instead, enable \c MBEDTLS_CTR_DRBG_USE_128_BIT_KEY above. + * + * \note To achieve a 256-bit security strength with CTR_DRBG, + * you must use AES-256 *and* use sufficient entropy. + * See ctr_drbg.h for more details. + * + * Module: library/ctr_drbg.c + * Caller: + * + * Requires: MBEDTLS_AES_C + * + * This module provides the CTR_DRBG AES random number generator. + */ +#define MBEDTLS_CTR_DRBG_C + +/** + * \def MBEDTLS_DEBUG_C + * + * Enable the debug functions. + * + * Module: library/debug.c + * Caller: library/ssl_cli.c + * library/ssl_srv.c + * library/ssl_tls.c + * + * This module provides debugging functions. + */ +#define MBEDTLS_DEBUG_C + +/** + * \def MBEDTLS_DES_C + * + * Enable the DES block cipher. + * + * Module: library/des.c + * Caller: library/pem.c + * library/cipher.c + * + * This module enables the following ciphersuites (if other requisites are + * enabled as well): + * MBEDTLS_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA + * MBEDTLS_TLS_PSK_WITH_3DES_EDE_CBC_SHA + * + * PEM_PARSE uses DES/3DES for decrypting encrypted keys. + * + * \warning DES is considered a weak cipher and its use constitutes a + * security risk. We recommend considering stronger ciphers instead. + */ +#define MBEDTLS_DES_C + +/** + * \def MBEDTLS_DHM_C + * + * Enable the Diffie-Hellman-Merkle module. + * + * Module: library/dhm.c + * Caller: library/ssl_cli.c + * library/ssl_srv.c + * + * This module is used by the following key exchanges: + * DHE-RSA, DHE-PSK + * + * \warning Using DHE constitutes a security risk as it + * is not possible to validate custom DH parameters. + * If possible, it is recommended users should consider + * preferring other methods of key exchange. + * See dhm.h for more details. + * + */ +#define MBEDTLS_DHM_C + +/** + * \def MBEDTLS_ECDH_C + * + * Enable the elliptic curve Diffie-Hellman library. + * + * Module: library/ecdh.c + * Caller: library/ssl_cli.c + * library/ssl_srv.c + * + * This module is used by the following key exchanges: + * ECDHE-ECDSA, ECDHE-RSA, DHE-PSK + * + * Requires: MBEDTLS_ECP_C + */ +#define MBEDTLS_ECDH_C + +/** + * \def MBEDTLS_ECDSA_C + * + * Enable the elliptic curve DSA library. + * + * Module: library/ecdsa.c + * Caller: + * + * This module is used by the following key exchanges: + * ECDHE-ECDSA + * + * Requires: MBEDTLS_ECP_C, MBEDTLS_ASN1_WRITE_C, MBEDTLS_ASN1_PARSE_C + */ +#define MBEDTLS_ECDSA_C + +/** + * \def MBEDTLS_ECJPAKE_C + * + * Enable the elliptic curve J-PAKE library. + * + * \warning This is currently experimental. EC J-PAKE support is based on the + * Thread v1.0.0 specification; incompatible changes to the specification + * might still happen. For this reason, this is disabled by default. + * + * Module: library/ecjpake.c + * Caller: + * + * This module is used by the following key exchanges: + * ECJPAKE + * + * Requires: MBEDTLS_ECP_C, MBEDTLS_MD_C + */ +//#define MBEDTLS_ECJPAKE_C + +/** + * \def MBEDTLS_ECP_C + * + * Enable the elliptic curve over GF(p) library. + * + * Module: library/ecp.c + * Caller: library/ecdh.c + * library/ecdsa.c + * library/ecjpake.c + * + * Requires: MBEDTLS_BIGNUM_C and at least one MBEDTLS_ECP_DP_XXX_ENABLED + */ +#define MBEDTLS_ECP_C + +/** + * \def MBEDTLS_ENTROPY_C + * + * Enable the platform-specific entropy code. + * + * Module: library/entropy.c + * Caller: + * + * Requires: MBEDTLS_SHA512_C or MBEDTLS_SHA256_C + * + * This module provides a generic entropy pool + */ +#define MBEDTLS_ENTROPY_C + +/** + * \def MBEDTLS_ERROR_C + * + * Enable error code to error string conversion. + * + * Module: library/error.c + * Caller: + * + * This module enables mbedtls_strerror(). + */ +#define MBEDTLS_ERROR_C + +/** + * \def MBEDTLS_GCM_C + * + * Enable the Galois/Counter Mode (GCM) for AES. + * + * Module: library/gcm.c + * + * Requires: MBEDTLS_AES_C or MBEDTLS_CAMELLIA_C + * + * This module enables the AES-GCM and CAMELLIA-GCM ciphersuites, if other + * requisites are enabled as well. + */ +#define MBEDTLS_GCM_C + +/** + * \def MBEDTLS_HAVEGE_C + * + * Enable the HAVEGE random generator. + * + * Warning: the HAVEGE random generator is not suitable for virtualized + * environments + * + * Warning: the HAVEGE random generator is dependent on timing and specific + * processor traits. It is therefore not advised to use HAVEGE as + * your applications primary random generator or primary entropy pool + * input. As a secondary input to your entropy pool, it IS able add + * the (limited) extra entropy it provides. + * + * Module: library/havege.c + * Caller: + * + * Requires: MBEDTLS_TIMING_C + * + * Uncomment to enable the HAVEGE random generator. + */ +//#define MBEDTLS_HAVEGE_C + +/** + * \def MBEDTLS_HKDF_C + * + * Enable the HKDF algorithm (RFC 5869). + * + * Module: library/hkdf.c + * Caller: + * + * Requires: MBEDTLS_MD_C + * + * This module adds support for the Hashed Message Authentication Code + * (HMAC)-based key derivation function (HKDF). + */ +#define MBEDTLS_HKDF_C + +/** + * \def MBEDTLS_HMAC_DRBG_C + * + * Enable the HMAC_DRBG random generator. + * + * Module: library/hmac_drbg.c + * Caller: + * + * Requires: MBEDTLS_MD_C + * + * Uncomment to enable the HMAC_DRBG random number geerator. + */ +#define MBEDTLS_HMAC_DRBG_C + +/** + * \def MBEDTLS_NIST_KW_C + * + * Enable the Key Wrapping mode for 128-bit block ciphers, + * as defined in NIST SP 800-38F. Only KW and KWP modes + * are supported. At the moment, only AES is approved by NIST. + * + * Module: library/nist_kw.c + * + * Requires: MBEDTLS_AES_C and MBEDTLS_CIPHER_C + */ +//#define MBEDTLS_NIST_KW_C + +/** + * \def MBEDTLS_MD_C + * + * Enable the generic message digest layer. + * + * Module: library/md.c + * Caller: + * + * Uncomment to enable generic message digest wrappers. + */ +#define MBEDTLS_MD_C + +/** + * \def MBEDTLS_MD2_C + * + * Enable the MD2 hash algorithm. + * + * Module: library/md2.c + * Caller: + * + * Uncomment to enable support for (rare) MD2-signed X.509 certs. + * + * \warning MD2 is considered a weak message digest and its use constitutes a + * security risk. If possible, we recommend avoiding dependencies on + * it, and considering stronger message digests instead. + * + */ +//#define MBEDTLS_MD2_C + +/** + * \def MBEDTLS_MD4_C + * + * Enable the MD4 hash algorithm. + * + * Module: library/md4.c + * Caller: + * + * Uncomment to enable support for (rare) MD4-signed X.509 certs. + * + * \warning MD4 is considered a weak message digest and its use constitutes a + * security risk. If possible, we recommend avoiding dependencies on + * it, and considering stronger message digests instead. + * + */ +//#define MBEDTLS_MD4_C + +/** + * \def MBEDTLS_MD5_C + * + * Enable the MD5 hash algorithm. + * + * Module: library/md5.c + * Caller: library/md.c + * library/pem.c + * library/ssl_tls.c + * + * This module is required for SSL/TLS up to version 1.1, and for TLS 1.2 + * depending on the handshake parameters. Further, it is used for checking + * MD5-signed certificates, and for PBKDF1 when decrypting PEM-encoded + * encrypted keys. + * + * \warning MD5 is considered a weak message digest and its use constitutes a + * security risk. If possible, we recommend avoiding dependencies on + * it, and considering stronger message digests instead. + * + */ +#define MBEDTLS_MD5_C + +/** + * \def MBEDTLS_MEMORY_BUFFER_ALLOC_C + * + * Enable the buffer allocator implementation that makes use of a (stack) + * based buffer to 'allocate' dynamic memory. (replaces calloc() and free() + * calls) + * + * Module: library/memory_buffer_alloc.c + * + * Requires: MBEDTLS_PLATFORM_C + * MBEDTLS_PLATFORM_MEMORY (to use it within mbed TLS) + * + * Enable this module to enable the buffer memory allocator. + */ +//#define MBEDTLS_MEMORY_BUFFER_ALLOC_C + +/** + * \def MBEDTLS_NET_C + * + * Enable the TCP and UDP over IPv6/IPv4 networking routines. + * + * \note This module only works on POSIX/Unix (including Linux, BSD and OS X) + * and Windows. For other platforms, you'll want to disable it, and write your + * own networking callbacks to be passed to \c mbedtls_ssl_set_bio(). + * + * \note See also our Knowledge Base article about porting to a new + * environment: + * https://tls.mbed.org/kb/how-to/how-do-i-port-mbed-tls-to-a-new-environment-OS + * + * Module: library/net_sockets.c + * + * This module provides networking routines. + */ +#define MBEDTLS_NET_C + +/** + * \def MBEDTLS_OID_C + * + * Enable the OID database. + * + * Module: library/oid.c + * Caller: library/asn1write.c + * library/pkcs5.c + * library/pkparse.c + * library/pkwrite.c + * library/rsa.c + * library/x509.c + * library/x509_create.c + * library/x509_crl.c + * library/x509_crt.c + * library/x509_csr.c + * library/x509write_crt.c + * library/x509write_csr.c + * + * This modules translates between OIDs and internal values. + */ +#define MBEDTLS_OID_C + +/** + * \def MBEDTLS_PADLOCK_C + * + * Enable VIA Padlock support on x86. + * + * Module: library/padlock.c + * Caller: library/aes.c + * + * Requires: MBEDTLS_HAVE_ASM + * + * This modules adds support for the VIA PadLock on x86. + */ +// #define MBEDTLS_PADLOCK_C + +/** + * \def MBEDTLS_PEM_PARSE_C + * + * Enable PEM decoding / parsing. + * + * Module: library/pem.c + * Caller: library/dhm.c + * library/pkparse.c + * library/x509_crl.c + * library/x509_crt.c + * library/x509_csr.c + * + * Requires: MBEDTLS_BASE64_C + * + * This modules adds support for decoding / parsing PEM files. + */ +#define MBEDTLS_PEM_PARSE_C + +/** + * \def MBEDTLS_PEM_WRITE_C + * + * Enable PEM encoding / writing. + * + * Module: library/pem.c + * Caller: library/pkwrite.c + * library/x509write_crt.c + * library/x509write_csr.c + * + * Requires: MBEDTLS_BASE64_C + * + * This modules adds support for encoding / writing PEM files. + */ +#define MBEDTLS_PEM_WRITE_C + +/** + * \def MBEDTLS_PK_C + * + * Enable the generic public (asymetric) key layer. + * + * Module: library/pk.c + * Caller: library/ssl_tls.c + * library/ssl_cli.c + * library/ssl_srv.c + * + * Requires: MBEDTLS_RSA_C or MBEDTLS_ECP_C + * + * Uncomment to enable generic public key wrappers. + */ +#define MBEDTLS_PK_C + +/** + * \def MBEDTLS_PK_PARSE_C + * + * Enable the generic public (asymetric) key parser. + * + * Module: library/pkparse.c + * Caller: library/x509_crt.c + * library/x509_csr.c + * + * Requires: MBEDTLS_PK_C + * + * Uncomment to enable generic public key parse functions. + */ +#define MBEDTLS_PK_PARSE_C + +/** + * \def MBEDTLS_PK_WRITE_C + * + * Enable the generic public (asymetric) key writer. + * + * Module: library/pkwrite.c + * Caller: library/x509write.c + * + * Requires: MBEDTLS_PK_C + * + * Uncomment to enable generic public key write functions. + */ +#define MBEDTLS_PK_WRITE_C + +/** + * \def MBEDTLS_PKCS5_C + * + * Enable PKCS#5 functions. + * + * Module: library/pkcs5.c + * + * Requires: MBEDTLS_MD_C + * + * This module adds support for the PKCS#5 functions. + */ +#define MBEDTLS_PKCS5_C + +/** + * \def MBEDTLS_PKCS11_C + * + * Enable wrapper for PKCS#11 smartcard support. + * + * Module: library/pkcs11.c + * Caller: library/pk.c + * + * Requires: MBEDTLS_PK_C + * + * This module enables SSL/TLS PKCS #11 smartcard support. + * Requires the presence of the PKCS#11 helper library (libpkcs11-helper) + */ +//#define MBEDTLS_PKCS11_C + +/** + * \def MBEDTLS_PKCS12_C + * + * Enable PKCS#12 PBE functions. + * Adds algorithms for parsing PKCS#8 encrypted private keys + * + * Module: library/pkcs12.c + * Caller: library/pkparse.c + * + * Requires: MBEDTLS_ASN1_PARSE_C, MBEDTLS_CIPHER_C, MBEDTLS_MD_C + * Can use: MBEDTLS_ARC4_C + * + * This module enables PKCS#12 functions. + */ +#define MBEDTLS_PKCS12_C + +/** + * \def MBEDTLS_PLATFORM_C + * + * Enable the platform abstraction layer that allows you to re-assign + * functions like calloc(), free(), snprintf(), printf(), fprintf(), exit(). + * + * Enabling MBEDTLS_PLATFORM_C enables to use of MBEDTLS_PLATFORM_XXX_ALT + * or MBEDTLS_PLATFORM_XXX_MACRO directives, allowing the functions mentioned + * above to be specified at runtime or compile time respectively. + * + * \note This abstraction layer must be enabled on Windows (including MSYS2) + * as other module rely on it for a fixed snprintf implementation. + * + * Module: library/platform.c + * Caller: Most other .c files + * + * This module enables abstraction of common (libc) functions. + */ +#define MBEDTLS_PLATFORM_C + +/** + * \def MBEDTLS_POLY1305_C + * + * Enable the Poly1305 MAC algorithm. + * + * Module: library/poly1305.c + * Caller: library/chachapoly.c + */ +#define MBEDTLS_POLY1305_C + +/** + * \def MBEDTLS_RIPEMD160_C + * + * Enable the RIPEMD-160 hash algorithm. + * + * Module: library/ripemd160.c + * Caller: library/md.c + * + */ +#define MBEDTLS_RIPEMD160_C + +/** + * \def MBEDTLS_RSA_C + * + * Enable the RSA public-key cryptosystem. + * + * Module: library/rsa.c + * library/rsa_internal.c + * Caller: library/ssl_cli.c + * library/ssl_srv.c + * library/ssl_tls.c + * library/x509.c + * + * This module is used by the following key exchanges: + * RSA, DHE-RSA, ECDHE-RSA, RSA-PSK + * + * Requires: MBEDTLS_BIGNUM_C, MBEDTLS_OID_C + */ +#define MBEDTLS_RSA_C + +/** + * \def MBEDTLS_SHA1_C + * + * Enable the SHA1 cryptographic hash algorithm. + * + * Module: library/sha1.c + * Caller: library/md.c + * library/ssl_cli.c + * library/ssl_srv.c + * library/ssl_tls.c + * library/x509write_crt.c + * + * This module is required for SSL/TLS up to version 1.1, for TLS 1.2 + * depending on the handshake parameters, and for SHA1-signed certificates. + * + * \warning SHA-1 is considered a weak message digest and its use constitutes + * a security risk. If possible, we recommend avoiding dependencies + * on it, and considering stronger message digests instead. + * + */ +#define MBEDTLS_SHA1_C + +/** + * \def MBEDTLS_SHA256_C + * + * Enable the SHA-224 and SHA-256 cryptographic hash algorithms. + * + * Module: library/sha256.c + * Caller: library/entropy.c + * library/md.c + * library/ssl_cli.c + * library/ssl_srv.c + * library/ssl_tls.c + * + * This module adds support for SHA-224 and SHA-256. + * This module is required for the SSL/TLS 1.2 PRF function. + */ +#define MBEDTLS_SHA256_C + +/** + * \def MBEDTLS_SHA512_C + * + * Enable the SHA-384 and SHA-512 cryptographic hash algorithms. + * + * Module: library/sha512.c + * Caller: library/entropy.c + * library/md.c + * library/ssl_cli.c + * library/ssl_srv.c + * + * This module adds support for SHA-384 and SHA-512. + */ +#define MBEDTLS_SHA512_C + +/** + * \def MBEDTLS_SSL_CACHE_C + * + * Enable simple SSL cache implementation. + * + * Module: library/ssl_cache.c + * Caller: + * + * Requires: MBEDTLS_SSL_CACHE_C + */ +#define MBEDTLS_SSL_CACHE_C + +/** + * \def MBEDTLS_SSL_COOKIE_C + * + * Enable basic implementation of DTLS cookies for hello verification. + * + * Module: library/ssl_cookie.c + * Caller: + */ +#define MBEDTLS_SSL_COOKIE_C + +/** + * \def MBEDTLS_SSL_TICKET_C + * + * Enable an implementation of TLS server-side callbacks for session tickets. + * + * Module: library/ssl_ticket.c + * Caller: + * + * Requires: MBEDTLS_CIPHER_C + */ +#define MBEDTLS_SSL_TICKET_C + +/** + * \def MBEDTLS_SSL_CLI_C + * + * Enable the SSL/TLS client code. + * + * Module: library/ssl_cli.c + * Caller: + * + * Requires: MBEDTLS_SSL_TLS_C + * + * This module is required for SSL/TLS client support. + */ +#define MBEDTLS_SSL_CLI_C + +/** + * \def MBEDTLS_SSL_SRV_C + * + * Enable the SSL/TLS server code. + * + * Module: library/ssl_srv.c + * Caller: + * + * Requires: MBEDTLS_SSL_TLS_C + * + * This module is required for SSL/TLS server support. + */ +#define MBEDTLS_SSL_SRV_C + +/** + * \def MBEDTLS_SSL_TLS_C + * + * Enable the generic SSL/TLS code. + * + * Module: library/ssl_tls.c + * Caller: library/ssl_cli.c + * library/ssl_srv.c + * + * Requires: MBEDTLS_CIPHER_C, MBEDTLS_MD_C + * and at least one of the MBEDTLS_SSL_PROTO_XXX defines + * + * This module is required for SSL/TLS. + */ +#define MBEDTLS_SSL_TLS_C + +/** + * \def MBEDTLS_THREADING_C + * + * Enable the threading abstraction layer. + * By default mbed TLS assumes it is used in a non-threaded environment or that + * contexts are not shared between threads. If you do intend to use contexts + * between threads, you will need to enable this layer to prevent race + * conditions. See also our Knowledge Base article about threading: + * https://tls.mbed.org/kb/development/thread-safety-and-multi-threading + * + * Module: library/threading.c + * + * This allows different threading implementations (self-implemented or + * provided). + * + * You will have to enable either MBEDTLS_THREADING_ALT or + * MBEDTLS_THREADING_PTHREAD. + * + * Enable this layer to allow use of mutexes within mbed TLS + */ +//#define MBEDTLS_THREADING_C + +/** + * \def MBEDTLS_TIMING_C + * + * Enable the semi-portable timing interface. + * + * \note The provided implementation only works on POSIX/Unix (including Linux, + * BSD and OS X) and Windows. On other platforms, you can either disable that + * module and provide your own implementations of the callbacks needed by + * \c mbedtls_ssl_set_timer_cb() for DTLS, or leave it enabled and provide + * your own implementation of the whole module by setting + * \c MBEDTLS_TIMING_ALT in the current file. + * + * \note See also our Knowledge Base article about porting to a new + * environment: + * https://tls.mbed.org/kb/how-to/how-do-i-port-mbed-tls-to-a-new-environment-OS + * + * Module: library/timing.c + * Caller: library/havege.c + * + * This module is used by the HAVEGE random number generator. + */ +#define MBEDTLS_TIMING_C + +/** + * \def MBEDTLS_VERSION_C + * + * Enable run-time version information. + * + * Module: library/version.c + * + * This module provides run-time version information. + */ +#define MBEDTLS_VERSION_C + +/** + * \def MBEDTLS_X509_USE_C + * + * Enable X.509 core for using certificates. + * + * Module: library/x509.c + * Caller: library/x509_crl.c + * library/x509_crt.c + * library/x509_csr.c + * + * Requires: MBEDTLS_ASN1_PARSE_C, MBEDTLS_BIGNUM_C, MBEDTLS_OID_C, + * MBEDTLS_PK_PARSE_C + * + * This module is required for the X.509 parsing modules. + */ +#define MBEDTLS_X509_USE_C + +/** + * \def MBEDTLS_X509_CRT_PARSE_C + * + * Enable X.509 certificate parsing. + * + * Module: library/x509_crt.c + * Caller: library/ssl_cli.c + * library/ssl_srv.c + * library/ssl_tls.c + * + * Requires: MBEDTLS_X509_USE_C + * + * This module is required for X.509 certificate parsing. + */ +#define MBEDTLS_X509_CRT_PARSE_C + +/** + * \def MBEDTLS_X509_CRL_PARSE_C + * + * Enable X.509 CRL parsing. + * + * Module: library/x509_crl.c + * Caller: library/x509_crt.c + * + * Requires: MBEDTLS_X509_USE_C + * + * This module is required for X.509 CRL parsing. + */ +#define MBEDTLS_X509_CRL_PARSE_C + +/** + * \def MBEDTLS_X509_CSR_PARSE_C + * + * Enable X.509 Certificate Signing Request (CSR) parsing. + * + * Module: library/x509_csr.c + * Caller: library/x509_crt_write.c + * + * Requires: MBEDTLS_X509_USE_C + * + * This module is used for reading X.509 certificate request. + */ +#define MBEDTLS_X509_CSR_PARSE_C + +/** + * \def MBEDTLS_X509_CREATE_C + * + * Enable X.509 core for creating certificates. + * + * Module: library/x509_create.c + * + * Requires: MBEDTLS_BIGNUM_C, MBEDTLS_OID_C, MBEDTLS_PK_WRITE_C + * + * This module is the basis for creating X.509 certificates and CSRs. + */ +#define MBEDTLS_X509_CREATE_C + +/** + * \def MBEDTLS_X509_CRT_WRITE_C + * + * Enable creating X.509 certificates. + * + * Module: library/x509_crt_write.c + * + * Requires: MBEDTLS_X509_CREATE_C + * + * This module is required for X.509 certificate creation. + */ +#define MBEDTLS_X509_CRT_WRITE_C + +/** + * \def MBEDTLS_X509_CSR_WRITE_C + * + * Enable creating X.509 Certificate Signing Requests (CSR). + * + * Module: library/x509_csr_write.c + * + * Requires: MBEDTLS_X509_CREATE_C + * + * This module is required for X.509 certificate request writing. + */ +#define MBEDTLS_X509_CSR_WRITE_C + +/** + * \def MBEDTLS_XTEA_C + * + * Enable the XTEA block cipher. + * + * Module: library/xtea.c + * Caller: + */ +#define MBEDTLS_XTEA_C + +/* \} name SECTION: mbed TLS modules */ + +/** + * \name SECTION: Module configuration options + * + * This section allows for the setting of module specific sizes and + * configuration options. The default values are already present in the + * relevant header files and should suffice for the regular use cases. + * + * Our advice is to enable options and change their values here + * only if you have a good reason and know the consequences. + * + * Please check the respective header file for documentation on these + * parameters (to prevent duplicate documentation). + * \{ + */ + +/* MPI / BIGNUM options */ +//#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum window size used. */ +//#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */ + +/* CTR_DRBG options */ +//#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */ +//#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */ +//#define MBEDTLS_CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */ +//#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */ +//#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */ + +/* HMAC_DRBG options */ +//#define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */ +//#define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */ +//#define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */ +//#define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */ + +/* ECP options */ +//#define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */ +//#define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */ +//#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */ + +/* Entropy options */ +//#define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ +//#define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ +//#define MBEDTLS_ENTROPY_MIN_HARDWARE 32 /**< Default minimum number of bytes required for the hardware entropy source mbedtls_hardware_poll() before entropy is released */ + +/* Memory buffer allocator options */ +//#define MBEDTLS_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */ + +/* Platform options */ +//#define MBEDTLS_PLATFORM_STD_MEM_HDR /**< Header to include if MBEDTLS_PLATFORM_NO_STD_FUNCTIONS is defined. Don't define if no header is needed. */ +//#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use, can be undefined */ +//#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use, can be undefined */ +//#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use, can be undefined */ +//#define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use, can be undefined. MBEDTLS_HAVE_TIME must be enabled */ +//#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */ +//#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use, can be undefined */ +/* Note: your snprintf must correctly zero-terminate the buffer! */ +//#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use, can be undefined */ +//#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS 0 /**< Default exit value to use, can be undefined */ +//#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE 1 /**< Default exit value to use, can be undefined */ +//#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read /**< Default nv_seed_read function to use, can be undefined */ +//#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write /**< Default nv_seed_write function to use, can be undefined */ +//#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile" /**< Seed file to read/write with default implementation */ + +/* To Use Function Macros MBEDTLS_PLATFORM_C must be enabled */ +/* MBEDTLS_PLATFORM_XXX_MACRO and MBEDTLS_PLATFORM_XXX_ALT cannot both be defined */ +//#define MBEDTLS_PLATFORM_CALLOC_MACRO calloc /**< Default allocator macro to use, can be undefined */ +//#define MBEDTLS_PLATFORM_FREE_MACRO free /**< Default free macro to use, can be undefined */ +//#define MBEDTLS_PLATFORM_EXIT_MACRO exit /**< Default exit macro to use, can be undefined */ +//#define MBEDTLS_PLATFORM_TIME_MACRO time /**< Default time macro to use, can be undefined. MBEDTLS_HAVE_TIME must be enabled */ +//#define MBEDTLS_PLATFORM_TIME_TYPE_MACRO time_t /**< Default time macro to use, can be undefined. MBEDTLS_HAVE_TIME must be enabled */ +//#define MBEDTLS_PLATFORM_FPRINTF_MACRO fprintf /**< Default fprintf macro to use, can be undefined */ +//#define MBEDTLS_PLATFORM_PRINTF_MACRO printf /**< Default printf macro to use, can be undefined */ +/* Note: your snprintf must correctly zero-terminate the buffer! */ +//#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf /**< Default snprintf macro to use, can be undefined */ +//#define MBEDTLS_PLATFORM_NV_SEED_READ_MACRO mbedtls_platform_std_nv_seed_read /**< Default nv_seed_read function to use, can be undefined */ +//#define MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO mbedtls_platform_std_nv_seed_write /**< Default nv_seed_write function to use, can be undefined */ + +/** + * \brief This macro is invoked by the library when an invalid parameter + * is detected that is only checked with #MBEDTLS_CHECK_PARAMS + * (see the documentation of that option for context). + * + * When you leave this undefined here, the library provides + * a default definition. If the macro #MBEDTLS_CHECK_PARAMS_ASSERT + * is defined, the default definition is `assert(cond)`, + * otherwise the default definition calls a function + * mbedtls_param_failed(). This function is declared in + * `platform_util.h` for the benefit of the library, but + * you need to define in your application. + * + * When you define this here, this replaces the default + * definition in platform_util.h (which no longer declares the + * function mbedtls_param_failed()) and it is your responsibility + * to make sure this macro expands to something suitable (in + * particular, that all the necessary declarations are visible + * from within the library - you can ensure that by providing + * them in this file next to the macro definition). + * If you define this macro to call `assert`, also define + * #MBEDTLS_CHECK_PARAMS_ASSERT so that library source files + * include ``. + * + * Note that you may define this macro to expand to nothing, in + * which case you don't have to worry about declarations or + * definitions. However, you will then be notified about invalid + * parameters only in non-void functions, and void function will + * just silently return early on invalid parameters, which + * partially negates the benefits of enabling + * #MBEDTLS_CHECK_PARAMS in the first place, so is discouraged. + * + * \param cond The expression that should evaluate to true, but doesn't. + */ +//#define MBEDTLS_PARAM_FAILED( cond ) assert( cond ) + +/* SSL Cache options */ +//#define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */ +//#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /**< Maximum entries in cache */ + +/* SSL options */ + +/** \def MBEDTLS_SSL_MAX_CONTENT_LEN + * + * Maximum length (in bytes) of incoming and outgoing plaintext fragments. + * + * This determines the size of both the incoming and outgoing TLS I/O buffers + * in such a way that both are capable of holding the specified amount of + * plaintext data, regardless of the protection mechanism used. + * + * To configure incoming and outgoing I/O buffers separately, use + * #MBEDTLS_SSL_IN_CONTENT_LEN and #MBEDTLS_SSL_OUT_CONTENT_LEN, + * which overwrite the value set by this option. + * + * \note When using a value less than the default of 16KB on the client, it is + * recommended to use the Maximum Fragment Length (MFL) extension to + * inform the server about this limitation. On the server, there + * is no supported, standardized way of informing the client about + * restriction on the maximum size of incoming messages, and unless + * the limitation has been communicated by other means, it is recommended + * to only change the outgoing buffer size #MBEDTLS_SSL_OUT_CONTENT_LEN + * while keeping the default value of 16KB for the incoming buffer. + * + * Uncomment to set the maximum plaintext size of both + * incoming and outgoing I/O buffers. + */ +//#define MBEDTLS_SSL_MAX_CONTENT_LEN 16384 + +/** \def MBEDTLS_SSL_IN_CONTENT_LEN + * + * Maximum length (in bytes) of incoming plaintext fragments. + * + * This determines the size of the incoming TLS I/O buffer in such a way + * that it is capable of holding the specified amount of plaintext data, + * regardless of the protection mechanism used. + * + * If this option is undefined, it inherits its value from + * #MBEDTLS_SSL_MAX_CONTENT_LEN. + * + * \note When using a value less than the default of 16KB on the client, it is + * recommended to use the Maximum Fragment Length (MFL) extension to + * inform the server about this limitation. On the server, there + * is no supported, standardized way of informing the client about + * restriction on the maximum size of incoming messages, and unless + * the limitation has been communicated by other means, it is recommended + * to only change the outgoing buffer size #MBEDTLS_SSL_OUT_CONTENT_LEN + * while keeping the default value of 16KB for the incoming buffer. + * + * Uncomment to set the maximum plaintext size of the incoming I/O buffer + * independently of the outgoing I/O buffer. + */ +//#define MBEDTLS_SSL_IN_CONTENT_LEN 16384 + +/** \def MBEDTLS_SSL_OUT_CONTENT_LEN + * + * Maximum length (in bytes) of outgoing plaintext fragments. + * + * This determines the size of the outgoing TLS I/O buffer in such a way + * that it is capable of holding the specified amount of plaintext data, + * regardless of the protection mechanism used. + * + * If this option undefined, it inherits its value from + * #MBEDTLS_SSL_MAX_CONTENT_LEN. + * + * It is possible to save RAM by setting a smaller outward buffer, while keeping + * the default inward 16384 byte buffer to conform to the TLS specification. + * + * The minimum required outward buffer size is determined by the handshake + * protocol's usage. Handshaking will fail if the outward buffer is too small. + * The specific size requirement depends on the configured ciphers and any + * certificate data which is sent during the handshake. + * + * Uncomment to set the maximum plaintext size of the outgoing I/O buffer + * independently of the incoming I/O buffer. + */ +//#define MBEDTLS_SSL_OUT_CONTENT_LEN 16384 + +/** \def MBEDTLS_SSL_DTLS_MAX_BUFFERING + * + * Maximum number of heap-allocated bytes for the purpose of + * DTLS handshake message reassembly and future message buffering. + * + * This should be at least 9/8 * MBEDTLSSL_IN_CONTENT_LEN + * to account for a reassembled handshake message of maximum size, + * together with its reassembly bitmap. + * + * A value of 2 * MBEDTLS_SSL_IN_CONTENT_LEN (32768 by default) + * should be sufficient for all practical situations as it allows + * to reassembly a large handshake message (such as a certificate) + * while buffering multiple smaller handshake messages. + * + */ +//#define MBEDTLS_SSL_DTLS_MAX_BUFFERING 32768 + +//#define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */ +//#define MBEDTLS_PSK_MAX_LEN 32 /**< Max size of TLS pre-shared keys, in bytes (default 256 bits) */ +//#define MBEDTLS_SSL_COOKIE_TIMEOUT 60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */ + +/** + * Complete list of ciphersuites to use, in order of preference. + * + * \warning No dependency checking is done on that field! This option can only + * be used to restrict the set of available ciphersuites. It is your + * responsibility to make sure the needed modules are active. + * + * Use this to save a few hundred bytes of ROM (default ordering of all + * available ciphersuites) and a few to a few hundred bytes of RAM. + * + * The value below is only an example, not the default. + */ +//#define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 + +/* X509 options */ +//#define MBEDTLS_X509_MAX_INTERMEDIATE_CA 8 /**< Maximum number of intermediate CAs in a verification chain. */ +//#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512 /**< Maximum length of a path/filename string in bytes including the null terminator character ('\0'). */ + +/** + * Allow SHA-1 in the default TLS configuration for certificate signing. + * Without this build-time option, SHA-1 support must be activated explicitly + * through mbedtls_ssl_conf_cert_profile. Turning on this option is not + * recommended because of it is possible to generate SHA-1 collisions, however + * this may be safe for legacy infrastructure where additional controls apply. + * + * \warning SHA-1 is considered a weak message digest and its use constitutes + * a security risk. If possible, we recommend avoiding dependencies + * on it, and considering stronger message digests instead. + * + */ +// #define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES + +/** + * Allow SHA-1 in the default TLS configuration for TLS 1.2 handshake + * signature and ciphersuite selection. Without this build-time option, SHA-1 + * support must be activated explicitly through mbedtls_ssl_conf_sig_hashes. + * The use of SHA-1 in TLS <= 1.1 and in HMAC-SHA-1 is always allowed by + * default. At the time of writing, there is no practical attack on the use + * of SHA-1 in handshake signatures, hence this option is turned on by default + * to preserve compatibility with existing peers, but the general + * warning applies nonetheless: + * + * \warning SHA-1 is considered a weak message digest and its use constitutes + * a security risk. If possible, we recommend avoiding dependencies + * on it, and considering stronger message digests instead. + * + */ +#define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE + +/** + * Uncomment the macro to let mbed TLS use your alternate implementation of + * mbedtls_platform_zeroize(). This replaces the default implementation in + * platform_util.c. + * + * mbedtls_platform_zeroize() is a widely used function across the library to + * zero a block of memory. The implementation is expected to be secure in the + * sense that it has been written to prevent the compiler from removing calls + * to mbedtls_platform_zeroize() as part of redundant code elimination + * optimizations. However, it is difficult to guarantee that calls to + * mbedtls_platform_zeroize() will not be optimized by the compiler as older + * versions of the C language standards do not provide a secure implementation + * of memset(). Therefore, MBEDTLS_PLATFORM_ZEROIZE_ALT enables users to + * configure their own implementation of mbedtls_platform_zeroize(), for + * example by using directives specific to their compiler, features from newer + * C standards (e.g using memset_s() in C11) or calling a secure memset() from + * their system (e.g explicit_bzero() in BSD). + */ +//#define MBEDTLS_PLATFORM_ZEROIZE_ALT + +/** + * Uncomment the macro to let Mbed TLS use your alternate implementation of + * mbedtls_platform_gmtime_r(). This replaces the default implementation in + * platform_util.c. + * + * gmtime() is not a thread-safe function as defined in the C standard. The + * library will try to use safer implementations of this function, such as + * gmtime_r() when available. However, if Mbed TLS cannot identify the target + * system, the implementation of mbedtls_platform_gmtime_r() will default to + * using the standard gmtime(). In this case, calls from the library to + * gmtime() will be guarded by the global mutex mbedtls_threading_gmtime_mutex + * if MBEDTLS_THREADING_C is enabled. We recommend that calls from outside the + * library are also guarded with this mutex to avoid race conditions. However, + * if the macro MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, Mbed TLS will + * unconditionally use the implementation for mbedtls_platform_gmtime_r() + * supplied at compile time. + */ +//#define MBEDTLS_PLATFORM_GMTIME_R_ALT + +/* \} name SECTION: Customisation configuration options */ + +/* Target and application specific configurations + * + * Allow user to override any previous default. + * + */ +#if defined(MBEDTLS_USER_CONFIG_FILE) +#include MBEDTLS_USER_CONFIG_FILE +#endif + +// #include "check_config.h" + +#endif /* MBEDTLS_CONFIG_H */ diff --git a/libs/crypt/mbedtls/platform.h b/libs/crypt/mbedtls/platform.h new file mode 100644 index 00000000..30e76122 --- /dev/null +++ b/libs/crypt/mbedtls/platform.h @@ -0,0 +1,394 @@ +/** + * \file platform.h + * + * \brief This file contains the definitions and functions of the + * Mbed TLS platform abstraction layer. + * + * The platform abstraction layer removes the need for the library + * to directly link to standard C library functions or operating + * system services, making the library easier to port and embed. + * Application developers and users of the library can provide their own + * implementations of these functions, or implementations specific to + * their platform, which can be statically linked to the library or + * dynamically configured at runtime. + */ +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ +#ifndef MBEDTLS_PLATFORM_H +#define MBEDTLS_PLATFORM_H + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +// #if defined(MBEDTLS_HAVE_TIME) +// #include "platform_time.h" +// #endif + +/** Hardware accelerator failed */ +#define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070 +/** The requested feature is not supported by the platform */ +#define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072 + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \name SECTION: Module settings + * + * The configuration options you can set for this module are in this section. + * Either change them in config.h or define them on the compiler command line. + * \{ + */ + +#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) +#include +#include +#include +#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) +#if defined(_WIN32) +#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< The default \c snprintf function to use. */ +#else +#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */ +#endif +#endif +#if !defined(MBEDTLS_PLATFORM_STD_PRINTF) +#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */ +#endif +#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) +#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */ +#endif +#if !defined(MBEDTLS_PLATFORM_STD_CALLOC) +#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */ +#endif +#if !defined(MBEDTLS_PLATFORM_STD_FREE) +#define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */ +#endif +#if !defined(MBEDTLS_PLATFORM_STD_EXIT) +#define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */ +#endif +#if !defined(MBEDTLS_PLATFORM_STD_TIME) +#define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */ +#endif +#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS) +#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */ +#endif +#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE) +#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */ +#endif +#if defined(MBEDTLS_FS_IO) +#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) +#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read +#endif +#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) +#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write +#endif +#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE) +#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile" +#endif +#endif /* MBEDTLS_FS_IO */ +#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ +#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR) +#include MBEDTLS_PLATFORM_STD_MEM_HDR +#endif +#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ + + +/* \} name SECTION: Module settings */ + +/* + * The function pointers for calloc and free. + */ +#if defined(MBEDTLS_PLATFORM_MEMORY) +#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \ + defined(MBEDTLS_PLATFORM_CALLOC_MACRO) +#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO +#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO +#else +/* For size_t */ +#include +extern void *mbedtls_calloc( size_t n, size_t size ); +extern void mbedtls_free( void *ptr ); + +/** + * \brief This function dynamically sets the memory-management + * functions used by the library, during runtime. + * + * \param calloc_func The \c calloc function implementation. + * \param free_func The \c free function implementation. + * + * \return \c 0. + */ +int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), + void (*free_func)( void * ) ); +#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */ +#else /* !MBEDTLS_PLATFORM_MEMORY */ +#define mbedtls_free free +#define mbedtls_calloc calloc +#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */ + +/* + * The function pointers for fprintf + */ +#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) +/* We need FILE * */ +#include +extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... ); + +/** + * \brief This function dynamically configures the fprintf + * function that is called when the + * mbedtls_fprintf() function is invoked by the library. + * + * \param fprintf_func The \c fprintf function implementation. + * + * \return \c 0. + */ +int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *, + ... ) ); +#else +#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO) +#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO +#else +#define mbedtls_fprintf fprintf +#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */ +#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */ + +/* + * The function pointers for printf + */ +#if defined(MBEDTLS_PLATFORM_PRINTF_ALT) +extern int (*mbedtls_printf)( const char *format, ... ); + +/** + * \brief This function dynamically configures the snprintf + * function that is called when the mbedtls_snprintf() + * function is invoked by the library. + * + * \param printf_func The \c printf function implementation. + * + * \return \c 0 on success. + */ +int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) ); +#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */ +#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO) +#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO +#else +#define mbedtls_printf printf +#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */ +#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */ + +/* + * The function pointers for snprintf + * + * The snprintf implementation should conform to C99: + * - it *must* always correctly zero-terminate the buffer + * (except when n == 0, then it must leave the buffer untouched) + * - however it is acceptable to return -1 instead of the required length when + * the destination buffer is too short. + */ +#if defined(_WIN32) +/* For Windows (inc. MSYS2), we provide our own fixed implementation */ +int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ); +#endif + +#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) +extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... ); + +/** + * \brief This function allows configuring a custom + * \c snprintf function pointer. + * + * \param snprintf_func The \c snprintf function implementation. + * + * \return \c 0 on success. + */ +int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, + const char * format, ... ) ); +#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ +#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO) +#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO +#else +#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF +#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */ +#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ + +/* + * The function pointers for exit + */ +#if defined(MBEDTLS_PLATFORM_EXIT_ALT) +extern void (*mbedtls_exit)( int status ); + +/** + * \brief This function dynamically configures the exit + * function that is called when the mbedtls_exit() + * function is invoked by the library. + * + * \param exit_func The \c exit function implementation. + * + * \return \c 0 on success. + */ +int mbedtls_platform_set_exit( void (*exit_func)( int status ) ); +#else +#if defined(MBEDTLS_PLATFORM_EXIT_MACRO) +#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO +#else +#define mbedtls_exit exit +#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */ +#endif /* MBEDTLS_PLATFORM_EXIT_ALT */ + +/* + * The default exit values + */ +#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS) +#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS +#else +#define MBEDTLS_EXIT_SUCCESS 0 +#endif +#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE) +#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE +#else +#define MBEDTLS_EXIT_FAILURE 1 +#endif + +/* + * The function pointers for reading from and writing a seed file to + * Non-Volatile storage (NV) in a platform-independent way + * + * Only enabled when the NV seed entropy source is enabled + */ +#if defined(MBEDTLS_ENTROPY_NV_SEED) +#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO) +/* Internal standard platform definitions */ +int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len ); +int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len ); +#endif + +#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT) +extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ); +extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ); + +/** + * \brief This function allows configuring custom seed file writing and + * reading functions. + * + * \param nv_seed_read_func The seed reading function implementation. + * \param nv_seed_write_func The seed writing function implementation. + * + * \return \c 0 on success. + */ +int mbedtls_platform_set_nv_seed( + int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ), + int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) + ); +#else +#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \ + defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO) +#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO +#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO +#else +#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read +#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write +#endif +#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */ +#endif /* MBEDTLS_ENTROPY_NV_SEED */ + +#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) + +/** + * \brief The platform context structure. + * + * \note This structure may be used to assist platform-specific + * setup or teardown operations. + */ +typedef struct mbedtls_platform_context +{ + char dummy; /**< A placeholder member, as empty structs are not portable. */ +} +mbedtls_platform_context; + +#else +#include "platform_alt.h" +#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */ + +/** + * \brief This function performs any platform-specific initialization + * operations. + * + * \note This function should be called before any other library functions. + * + * Its implementation is platform-specific, and unless + * platform-specific code is provided, it does nothing. + * + * \note The usage and necessity of this function is dependent on the platform. + * + * \param ctx The platform context. + * + * \return \c 0 on success. + */ +int mbedtls_platform_setup( mbedtls_platform_context *ctx ); +/** + * \brief This function performs any platform teardown operations. + * + * \note This function should be called after every other Mbed TLS module + * has been correctly freed using the appropriate free function. + * + * Its implementation is platform-specific, and unless + * platform-specific code is provided, it does nothing. + * + * \note The usage and necessity of this function is dependent on the platform. + * + * \param ctx The platform context. + * + */ +void mbedtls_platform_teardown( mbedtls_platform_context *ctx ); + +#ifdef __cplusplus +} +#endif + +#endif /* platform.h */ diff --git a/libs/crypt/mbedtls/platform_time.h b/libs/crypt/mbedtls/platform_time.h new file mode 100644 index 00000000..e132f6a6 --- /dev/null +++ b/libs/crypt/mbedtls/platform_time.h @@ -0,0 +1,107 @@ +/** + * \file platform_time.h + * + * \brief mbed TLS Platform time abstraction + */ +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ +#ifndef MBEDTLS_PLATFORM_TIME_H +#define MBEDTLS_PLATFORM_TIME_H + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \name SECTION: Module settings + * + * The configuration options you can set for this module are in this section. + * Either change them in config.h or define them on the compiler command line. + * \{ + */ + +/* + * The time_t datatype + */ +#if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO) +typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t; +#else +/* For time_t */ +#include +typedef time_t mbedtls_time_t; +#endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */ + +/* + * The function pointers for time + */ +#if defined(MBEDTLS_PLATFORM_TIME_ALT) +extern mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* time ); + +/** + * \brief Set your own time function pointer + * + * \param time_func the time function implementation + * + * \return 0 + */ +int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* time ) ); +#else +#if defined(MBEDTLS_PLATFORM_TIME_MACRO) +#define mbedtls_time MBEDTLS_PLATFORM_TIME_MACRO +#else +#define mbedtls_time time +#endif /* MBEDTLS_PLATFORM_TIME_MACRO */ +#endif /* MBEDTLS_PLATFORM_TIME_ALT */ + +#ifdef __cplusplus +} +#endif + +#endif /* platform_time.h */ diff --git a/libs/crypt/mbedtls/platform_util.h b/libs/crypt/mbedtls/platform_util.h new file mode 100644 index 00000000..426afaf0 --- /dev/null +++ b/libs/crypt/mbedtls/platform_util.h @@ -0,0 +1,221 @@ +/** + * \file platform_util.h + * + * \brief Common and shared functions used by multiple modules in the Mbed TLS + * library. + */ +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ +#ifndef MBEDTLS_PLATFORM_UTIL_H +#define MBEDTLS_PLATFORM_UTIL_H + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#include +#if defined(MBEDTLS_HAVE_TIME_DATE) +#include "platform_time.h" +#include +#endif /* MBEDTLS_HAVE_TIME_DATE */ + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(MBEDTLS_CHECK_PARAMS) + +#if defined(MBEDTLS_CHECK_PARAMS_ASSERT) +/* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert + * (which is what our config.h suggests). */ +#include +#endif /* MBEDTLS_CHECK_PARAMS_ASSERT */ + +#if defined(MBEDTLS_PARAM_FAILED) +/** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h. + * + * This flag can be used to check whether it is safe to assume that + * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed(). + */ +#define MBEDTLS_PARAM_FAILED_ALT + +#elif defined(MBEDTLS_CHECK_PARAMS_ASSERT) +#define MBEDTLS_PARAM_FAILED( cond ) assert( cond ) +#define MBEDTLS_PARAM_FAILED_ALT + +#else /* MBEDTLS_PARAM_FAILED */ +#define MBEDTLS_PARAM_FAILED( cond ) \ + mbedtls_param_failed( #cond, __FILE__, __LINE__ ) + +/** + * \brief User supplied callback function for parameter validation failure. + * See #MBEDTLS_CHECK_PARAMS for context. + * + * This function will be called unless an alternative treatement + * is defined through the #MBEDTLS_PARAM_FAILED macro. + * + * This function can return, and the operation will be aborted, or + * alternatively, through use of setjmp()/longjmp() can resume + * execution in the application code. + * + * \param failure_condition The assertion that didn't hold. + * \param file The file where the assertion failed. + * \param line The line in the file where the assertion failed. + */ +void mbedtls_param_failed( const char *failure_condition, + const char *file, + int line ); +#endif /* MBEDTLS_PARAM_FAILED */ + +/* Internal macro meant to be called only from within the library. */ +#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \ + do { \ + if( !(cond) ) \ + { \ + MBEDTLS_PARAM_FAILED( cond ); \ + return( ret ); \ + } \ + } while( 0 ) + +/* Internal macro meant to be called only from within the library. */ +#define MBEDTLS_INTERNAL_VALIDATE( cond ) \ + do { \ + if( !(cond) ) \ + { \ + MBEDTLS_PARAM_FAILED( cond ); \ + return; \ + } \ + } while( 0 ) + +#else /* MBEDTLS_CHECK_PARAMS */ + +/* Internal macros meant to be called only from within the library. */ +#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 ) +#define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 ) + +#endif /* MBEDTLS_CHECK_PARAMS */ + +/* Internal helper macros for deprecating API constants. */ +#if !defined(MBEDTLS_DEPRECATED_REMOVED) +#if defined(MBEDTLS_DEPRECATED_WARNING) +/* Deliberately don't (yet) export MBEDTLS_DEPRECATED here + * to avoid conflict with other headers which define and use + * it, too. We might want to move all these definitions here at + * some point for uniformity. */ +#define MBEDTLS_DEPRECATED __attribute__((deprecated)) +MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t; +#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \ + ( (mbedtls_deprecated_string_constant_t) ( VAL ) ) +MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t; +#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \ + ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) ) +#undef MBEDTLS_DEPRECATED +#else /* MBEDTLS_DEPRECATED_WARNING */ +#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL +#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL +#endif /* MBEDTLS_DEPRECATED_WARNING */ +#endif /* MBEDTLS_DEPRECATED_REMOVED */ + +/** + * \brief Securely zeroize a buffer + * + * The function is meant to wipe the data contained in a buffer so + * that it can no longer be recovered even if the program memory + * is later compromised. Call this function on sensitive data + * stored on the stack before returning from a function, and on + * sensitive data stored on the heap before freeing the heap + * object. + * + * It is extremely difficult to guarantee that calls to + * mbedtls_platform_zeroize() are not removed by aggressive + * compiler optimizations in a portable way. For this reason, Mbed + * TLS provides the configuration option + * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure + * mbedtls_platform_zeroize() to use a suitable implementation for + * their platform and needs + * + * \param buf Buffer to be zeroized + * \param len Length of the buffer in bytes + * + */ +void mbedtls_platform_zeroize( void *buf, size_t len ); + +#if defined(MBEDTLS_HAVE_TIME_DATE) +/** + * \brief Platform-specific implementation of gmtime_r() + * + * The function is a thread-safe abstraction that behaves + * similarly to the gmtime_r() function from Unix/POSIX. + * + * Mbed TLS will try to identify the underlying platform and + * make use of an appropriate underlying implementation (e.g. + * gmtime_r() for POSIX and gmtime_s() for Windows). If this is + * not possible, then gmtime() will be used. In this case, calls + * from the library to gmtime() will be guarded by the mutex + * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is + * enabled. It is recommended that calls from outside the library + * are also guarded by this mutex. + * + * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will + * unconditionally use the alternative implementation for + * mbedtls_platform_gmtime_r() supplied by the user at compile time. + * + * \param tt Pointer to an object containing time (in seconds) since the + * epoch to be converted + * \param tm_buf Pointer to an object where the results will be stored + * + * \return Pointer to an object of type struct tm on success, otherwise + * NULL + */ +struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, + struct tm *tm_buf ); +#endif /* MBEDTLS_HAVE_TIME_DATE */ + +#ifdef __cplusplus +} +#endif + +#endif /* MBEDTLS_PLATFORM_UTIL_H */ diff --git a/libs/crypt/mbedtls/threading.h b/libs/crypt/mbedtls/threading.h new file mode 100644 index 00000000..2cf07167 --- /dev/null +++ b/libs/crypt/mbedtls/threading.h @@ -0,0 +1,153 @@ +/** + * \file threading.h + * + * \brief Threading abstraction layer + */ +/* + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ +#ifndef MBEDTLS_THREADING_H +#define MBEDTLS_THREADING_H + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE is deprecated and should not be + * used. */ +/** The selected feature is not available. */ +#define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A + +/** Bad input parameters to function. */ +#define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA -0x001C +/** Locking / unlocking / free failed with error code. */ +#define MBEDTLS_ERR_THREADING_MUTEX_ERROR -0x001E + +#if defined(MBEDTLS_THREADING_PTHREAD) +#include +typedef struct mbedtls_threading_mutex_t +{ + pthread_mutex_t mutex; + /* is_valid is 0 after a failed init or a free, and nonzero after a + * successful init. This field is not considered part of the public + * API of Mbed TLS and may change without notice. */ + char is_valid; +} mbedtls_threading_mutex_t; +#endif + +#if defined(MBEDTLS_THREADING_ALT) +/* You should define the mbedtls_threading_mutex_t type in your header */ +#include "threading_alt.h" + +/** + * \brief Set your alternate threading implementation function + * pointers and initialize global mutexes. If used, this + * function must be called once in the main thread before any + * other mbed TLS function is called, and + * mbedtls_threading_free_alt() must be called once in the main + * thread after all other mbed TLS functions. + * + * \note mutex_init() and mutex_free() don't return a status code. + * If mutex_init() fails, it should leave its argument (the + * mutex) in a state such that mutex_lock() will fail when + * called with this argument. + * + * \param mutex_init the init function implementation + * \param mutex_free the free function implementation + * \param mutex_lock the lock function implementation + * \param mutex_unlock the unlock function implementation + */ +void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ), + void (*mutex_free)( mbedtls_threading_mutex_t * ), + int (*mutex_lock)( mbedtls_threading_mutex_t * ), + int (*mutex_unlock)( mbedtls_threading_mutex_t * ) ); + +/** + * \brief Free global mutexes. + */ +void mbedtls_threading_free_alt( void ); +#endif /* MBEDTLS_THREADING_ALT */ + +#if defined(MBEDTLS_THREADING_C) +/* + * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock + * + * All these functions are expected to work or the result will be undefined. + */ +extern void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t *mutex ); +extern void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t *mutex ); +extern int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t *mutex ); +extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex ); + +/* + * Global mutexes + */ +#if defined(MBEDTLS_FS_IO) +extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex; +#endif + +#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT) +/* This mutex may or may not be used in the default definition of + * mbedtls_platform_gmtime_r(), but in order to determine that, + * we need to check POSIX features, hence modify _POSIX_C_SOURCE. + * With the current approach, this declaration is orphaned, lacking + * an accompanying definition, in case mbedtls_platform_gmtime_r() + * doesn't need it, but that's not a problem. */ +extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex; +#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */ + +#endif /* MBEDTLS_THREADING_C */ + +#ifdef __cplusplus +} +#endif + +#endif /* threading.h */ diff --git a/libs/crypt/platform.c b/libs/crypt/platform.c new file mode 100644 index 00000000..c4c3fd33 --- /dev/null +++ b/libs/crypt/platform.c @@ -0,0 +1,373 @@ +/* + * Platform abstraction layer + * + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#if defined(MBEDTLS_PLATFORM_C) + +#include "mbedtls/platform.h" +#include "mbedtls/platform_util.h" + +/* The compile time configuration of memory allocation via the macros + * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime + * configuration via mbedtls_platform_set_calloc_free(). So, omit everything + * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */ +#if defined(MBEDTLS_PLATFORM_MEMORY) && \ + !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \ + defined(MBEDTLS_PLATFORM_FREE_MACRO) ) + +#if !defined(MBEDTLS_PLATFORM_STD_CALLOC) +static void *platform_calloc_uninit( size_t n, size_t size ) +{ + ((void) n); + ((void) size); + return( NULL ); +} + +#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit +#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */ + +#if !defined(MBEDTLS_PLATFORM_STD_FREE) +static void platform_free_uninit( void *ptr ) +{ + ((void) ptr); +} + +#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit +#endif /* !MBEDTLS_PLATFORM_STD_FREE */ + +static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC; +static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE; + +void * mbedtls_calloc( size_t nmemb, size_t size ) +{ + return (*mbedtls_calloc_func)( nmemb, size ); +} + +void mbedtls_free( void * ptr ) +{ + (*mbedtls_free_func)( ptr ); +} + +int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), + void (*free_func)( void * ) ) +{ + mbedtls_calloc_func = calloc_func; + mbedtls_free_func = free_func; + return( 0 ); +} +#endif /* MBEDTLS_PLATFORM_MEMORY && + !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && + defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */ + +#if defined(_WIN32) +#include +int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ) +{ + int ret; + va_list argp; + + /* Avoid calling the invalid parameter handler by checking ourselves */ + if( s == NULL || n == 0 || fmt == NULL ) + return( -1 ); + + va_start( argp, fmt ); +#if defined(_TRUNCATE) && !defined(__MINGW32__) + ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); +#else + ret = _vsnprintf( s, n, fmt, argp ); + if( ret < 0 || (size_t) ret == n ) + { + s[n-1] = '\0'; + ret = -1; + } +#endif + va_end( argp ); + + return( ret ); +} +#endif + +#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) +#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static int platform_snprintf_uninit( char * s, size_t n, + const char * format, ... ) +{ + ((void) s); + ((void) n); + ((void) format); + return( 0 ); +} + +#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit +#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */ + +int (*mbedtls_snprintf)( char * s, size_t n, + const char * format, + ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF; + +int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, + const char * format, + ... ) ) +{ + mbedtls_snprintf = snprintf_func; + return( 0 ); +} +#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ + +#if defined(MBEDTLS_PLATFORM_PRINTF_ALT) +#if !defined(MBEDTLS_PLATFORM_STD_PRINTF) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static int platform_printf_uninit( const char *format, ... ) +{ + ((void) format); + return( 0 ); +} + +#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit +#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */ + +int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF; + +int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) ) +{ + mbedtls_printf = printf_func; + return( 0 ); +} +#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */ + +#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) +#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static int platform_fprintf_uninit( FILE *stream, const char *format, ... ) +{ + ((void) stream); + ((void) format); + return( 0 ); +} + +#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit +#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */ + +int (*mbedtls_fprintf)( FILE *, const char *, ... ) = + MBEDTLS_PLATFORM_STD_FPRINTF; + +int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) +{ + mbedtls_fprintf = fprintf_func; + return( 0 ); +} +#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */ + +#if defined(MBEDTLS_PLATFORM_EXIT_ALT) +#if !defined(MBEDTLS_PLATFORM_STD_EXIT) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static void platform_exit_uninit( int status ) +{ + ((void) status); +} + +#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit +#endif /* !MBEDTLS_PLATFORM_STD_EXIT */ + +void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT; + +int mbedtls_platform_set_exit( void (*exit_func)( int status ) ) +{ + mbedtls_exit = exit_func; + return( 0 ); +} +#endif /* MBEDTLS_PLATFORM_EXIT_ALT */ + +#if defined(MBEDTLS_HAVE_TIME) + +#if defined(MBEDTLS_PLATFORM_TIME_ALT) +#if !defined(MBEDTLS_PLATFORM_STD_TIME) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer ) +{ + ((void) timer); + return( 0 ); +} + +#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit +#endif /* !MBEDTLS_PLATFORM_STD_TIME */ + +mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME; + +int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) ) +{ + mbedtls_time = time_func; + return( 0 ); +} +#endif /* MBEDTLS_PLATFORM_TIME_ALT */ + +#endif /* MBEDTLS_HAVE_TIME */ + +#if defined(MBEDTLS_ENTROPY_NV_SEED) +#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO) +/* Default implementations for the platform independent seed functions use + * standard libc file functions to read from and write to a pre-defined filename + */ +int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len ) +{ + FILE *file; + size_t n; + + if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL ) + return( -1 ); + + if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len ) + { + fclose( file ); + mbedtls_platform_zeroize( buf, buf_len ); + return( -1 ); + } + + fclose( file ); + return( (int)n ); +} + +int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len ) +{ + FILE *file; + size_t n; + + if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL ) + return -1; + + if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len ) + { + fclose( file ); + return -1; + } + + fclose( file ); + return( (int)n ); +} +#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ + +#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT) +#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len ) +{ + ((void) buf); + ((void) buf_len); + return( -1 ); +} + +#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit +#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */ + +#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len ) +{ + ((void) buf); + ((void) buf_len); + return( -1 ); +} + +#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit +#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */ + +int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) = + MBEDTLS_PLATFORM_STD_NV_SEED_READ; +int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) = + MBEDTLS_PLATFORM_STD_NV_SEED_WRITE; + +int mbedtls_platform_set_nv_seed( + int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ), + int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) ) +{ + mbedtls_nv_seed_read = nv_seed_read_func; + mbedtls_nv_seed_write = nv_seed_write_func; + return( 0 ); +} +#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */ +#endif /* MBEDTLS_ENTROPY_NV_SEED */ + +#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) +/* + * Placeholder platform setup that does nothing by default + */ +int mbedtls_platform_setup( mbedtls_platform_context *ctx ) +{ + (void)ctx; + + return( 0 ); +} + +/* + * Placeholder platform teardown that does nothing by default + */ +void mbedtls_platform_teardown( mbedtls_platform_context *ctx ) +{ + (void)ctx; +} +#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */ + +#endif /* MBEDTLS_PLATFORM_C */ diff --git a/libs/crypt/platform_util.c b/libs/crypt/platform_util.c new file mode 100644 index 00000000..c8cd52d5 --- /dev/null +++ b/libs/crypt/platform_util.c @@ -0,0 +1,164 @@ +/* + * Common and shared functions used by multiple modules in the Mbed TLS + * library. + * + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ + +/* + * Ensure gmtime_r is available even with -std=c99; must be defined before + * config.h, which pulls in glibc's features.h. Harmless on other platforms. + */ +#if !defined(_POSIX_C_SOURCE) +#define _POSIX_C_SOURCE 200112L +#endif + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#include "mbedtls/platform_util.h" +#include "mbedtls/platform.h" +#include "mbedtls/threading.h" + +#include +#include + +#if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT) +/* + * This implementation should never be optimized out by the compiler + * + * This implementation for mbedtls_platform_zeroize() was inspired from Colin + * Percival's blog article at: + * + * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html + * + * It uses a volatile function pointer to the standard memset(). Because the + * pointer is volatile the compiler expects it to change at + * any time and will not optimize out the call that could potentially perform + * other operations on the input buffer instead of just setting it to 0. + * Nevertheless, as pointed out by davidtgoldblatt on Hacker News + * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for + * details), optimizations of the following form are still possible: + * + * if( memset_func != memset ) + * memset_func( buf, 0, len ); + * + * Note that it is extremely difficult to guarantee that + * mbedtls_platform_zeroize() will not be optimized out by aggressive compilers + * in a portable way. For this reason, Mbed TLS also provides the configuration + * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure + * mbedtls_platform_zeroize() to use a suitable implementation for their + * platform and needs. + */ +static void * (* const volatile memset_func)( void *, int, size_t ) = memset; + +void mbedtls_platform_zeroize( void *buf, size_t len ) +{ + MBEDTLS_INTERNAL_VALIDATE( len == 0 || buf != NULL ); + + if( len > 0 ) + memset_func( buf, 0, len ); +} +#endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */ + +#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT) +#include +#if !defined(_WIN32) && (defined(unix) || \ + defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \ + defined(__MACH__))) +#include +#endif /* !_WIN32 && (unix || __unix || __unix__ || + * (__APPLE__ && __MACH__)) */ + +#if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \ + ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \ + _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) +/* + * This is a convenience shorthand macro to avoid checking the long + * preprocessor conditions above. Ideally, we could expose this macro in + * platform_util.h and simply use it in platform_util.c, threading.c and + * threading.h. However, this macro is not part of the Mbed TLS public API, so + * we keep it private by only defining it in this file + */ +#if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) +#define PLATFORM_UTIL_USE_GMTIME +#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */ + +#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \ + ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \ + _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */ + +struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, + struct tm *tm_buf ) +{ +#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) + return( ( gmtime_s( tm_buf, tt ) == 0 ) ? tm_buf : NULL ); +#elif !defined(PLATFORM_UTIL_USE_GMTIME) + return( gmtime_r( tt, tm_buf ) ); +#else + struct tm *lt; + +#if defined(MBEDTLS_THREADING_C) + if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 ) + return( NULL ); +#endif /* MBEDTLS_THREADING_C */ + + lt = gmtime( tt ); + + if( lt != NULL ) + { + memcpy( tm_buf, lt, sizeof( struct tm ) ); + } + +#if defined(MBEDTLS_THREADING_C) + if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 ) + return( NULL ); +#endif /* MBEDTLS_THREADING_C */ + + return( ( lt == NULL ) ? NULL : tm_buf ); +#endif /* _WIN32 && !EFIX64 && !EFI32 */ +} +#endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */ diff --git a/libs/crypt/threading.c b/libs/crypt/threading.c new file mode 100644 index 00000000..0dc5488c --- /dev/null +++ b/libs/crypt/threading.c @@ -0,0 +1,218 @@ +/* + * Threading abstraction layer + * + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + * + * This file is provided under the Apache License 2.0, or the + * GNU General Public License v2.0 or later. + * + * ********** + * Apache License 2.0: + * + * 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. + * + * ********** + * + * ********** + * GNU General Public License v2.0 or later: + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * ********** + */ + +/* + * Ensure gmtime_r is available even with -std=c99; must be defined before + * config.h, which pulls in glibc's features.h. Harmless on other platforms. + */ +#if !defined(_POSIX_C_SOURCE) +#define _POSIX_C_SOURCE 200112L +#endif + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#if defined(MBEDTLS_THREADING_C) + +#include "mbedtls/threading.h" + +#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT) + +#if !defined(_WIN32) && (defined(unix) || \ + defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \ + defined(__MACH__))) +#include +#endif /* !_WIN32 && (unix || __unix || __unix__ || + * (__APPLE__ && __MACH__)) */ + +#if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \ + ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \ + _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) +/* + * This is a convenience shorthand macro to avoid checking the long + * preprocessor conditions above. Ideally, we could expose this macro in + * platform_util.h and simply use it in platform_util.c, threading.c and + * threading.h. However, this macro is not part of the Mbed TLS public API, so + * we keep it private by only defining it in this file + */ + +#if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) +#define THREADING_USE_GMTIME +#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */ + +#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \ + ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \ + _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */ + +#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */ + +#if defined(MBEDTLS_THREADING_PTHREAD) +static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex ) +{ + if( mutex == NULL ) + return; + + /* A nonzero value of is_valid indicates a successfully initialized + * mutex. This is a workaround for not being able to return an error + * code for this function. The lock/unlock functions return an error + * if is_valid is nonzero. The Mbed TLS unit test code uses this field + * to distinguish more states of the mutex; see helpers.function for + * details. */ + mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0; +} + +static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex ) +{ + if( mutex == NULL || !mutex->is_valid ) + return; + + (void) pthread_mutex_destroy( &mutex->mutex ); + mutex->is_valid = 0; +} + +static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex ) +{ + if( mutex == NULL || ! mutex->is_valid ) + return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); + + if( pthread_mutex_lock( &mutex->mutex ) != 0 ) + return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); + + return( 0 ); +} + +static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex ) +{ + if( mutex == NULL || ! mutex->is_valid ) + return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); + + if( pthread_mutex_unlock( &mutex->mutex ) != 0 ) + return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); + + return( 0 ); +} + +void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread; +void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread; +int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread; +int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread; + +/* + * With phtreads we can statically initialize mutexes + */ +#define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 } + +#endif /* MBEDTLS_THREADING_PTHREAD */ + +#if defined(MBEDTLS_THREADING_ALT) +static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex ) +{ + ((void) mutex ); + return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); +} +static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex ) +{ + ((void) mutex ); + return; +} + +void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; +void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; +int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; +int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; + +/* + * Set functions pointers and initialize global mutexes + */ +void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ), + void (*mutex_free)( mbedtls_threading_mutex_t * ), + int (*mutex_lock)( mbedtls_threading_mutex_t * ), + int (*mutex_unlock)( mbedtls_threading_mutex_t * ) ) +{ + mbedtls_mutex_init = mutex_init; + mbedtls_mutex_free = mutex_free; + mbedtls_mutex_lock = mutex_lock; + mbedtls_mutex_unlock = mutex_unlock; + +#if defined(MBEDTLS_FS_IO) + mbedtls_mutex_init( &mbedtls_threading_readdir_mutex ); +#endif +#if defined(THREADING_USE_GMTIME) + mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex ); +#endif +} + +/* + * Free global mutexes + */ +void mbedtls_threading_free_alt( void ) +{ +#if defined(MBEDTLS_FS_IO) + mbedtls_mutex_free( &mbedtls_threading_readdir_mutex ); +#endif +#if defined(THREADING_USE_GMTIME) + mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex ); +#endif +} +#endif /* MBEDTLS_THREADING_ALT */ + +/* + * Define global mutexes + */ +#ifndef MUTEX_INIT +#define MUTEX_INIT +#endif +#if defined(MBEDTLS_FS_IO) +mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; +#endif +#if defined(THREADING_USE_GMTIME) +mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT; +#endif + +#endif /* MBEDTLS_THREADING_C */ From 674be9e6faccf7259c772dbce69729770b593fcb Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:30:25 -0300 Subject: [PATCH 03/14] chore: added mbedtls lib to Common.cmake --- common/Common.cmake | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/common/Common.cmake b/common/Common.cmake index 14f8d69b..523d0cb3 100644 --- a/common/Common.cmake +++ b/common/Common.cmake @@ -10,10 +10,15 @@ set(CIOT_PB_INCLUDE_DIRS ${CIOT_DIR}/src/proto/ ) +set(CIOT_MBED_INCLUDE_DIRS + ${CIOT_DIR}/libs/crypt +) + FILE(GLOB_RECURSE CIOT_COMMON_SRCS ${CIOT_DIR}/src/common/*.c) FILE(GLOB_RECURSE CIOT_CORE_SRCS ${CIOT_DIR}/src/core/*.c) FILE(GLOB_RECURSE CIOT_PB_C_SRCS ${CIOT_DIR}/src/proto/protobuf-c/protobuf-c/*.c) FILE(GLOB_RECURSE CIOT_PB_V1_SRCS ${CIOT_DIR}/src/proto/ciot/proto/v1/*.c) +FILE(GLOB_RECURSE CIOT_MBED_SRCS ${CIOT_DIR}/libs/crypt/*.c) list(APPEND CIOT_SRCS ${CIOT_COMMON_SRCS}) list(APPEND CIOT_SRCS ${CIOT_CORE_SRCS}) From 56fd1cd482823e928d99bacb8bc65e9275a966a2 Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:30:45 -0300 Subject: [PATCH 04/14] feat: updated ciot_custom_config.h from examples --- examples/http_server/ciot_custom_config.h | 2 +- examples/mqtt_client/ciot_custom_config.h | 2 +- examples/nrf_ble_module/ciot_custom_config.h | 2 +- examples/nrf_dfu_master/ciot_custom_config.h | 2 +- examples/uart/ciot_custom_config.h | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/http_server/ciot_custom_config.h b/examples/http_server/ciot_custom_config.h index e1d362bc..0d3164e3 100644 --- a/examples/http_server/ciot_custom_config.h +++ b/examples/http_server/ciot_custom_config.h @@ -12,7 +12,7 @@ #ifndef __CIOT_CUSTOM_CONFIG__H__ #define __CIOT_CUSTOM_CONFIG__H__ -#define CIOT_CONFIG_LOG_LEVEL CIOT_LOG_LEVEL_INFO +#define CIOT_CONFIG_LOG_LEVEL CIOT__LOG_LEVEL__LOG_LEVEL_INFO #define CIOT_CONFIG_APP_VER 0, 1, 0 #define CIOT_CONFIG_HARDWARE_NAME "CIoT HTTP Server" diff --git a/examples/mqtt_client/ciot_custom_config.h b/examples/mqtt_client/ciot_custom_config.h index 8e3addaa..0a160627 100644 --- a/examples/mqtt_client/ciot_custom_config.h +++ b/examples/mqtt_client/ciot_custom_config.h @@ -12,7 +12,7 @@ #ifndef __CIOT_CUSTOM_CONFIG__H__ #define __CIOT_CUSTOM_CONFIG__H__ -#define CIOT_CONFIG_LOG_LEVEL CIOT_LOG_LEVEL_INFO +#define CIOT_CONFIG_LOG_LEVEL CIOT__LOG_LEVEL__LOG_LEVEL_INFO #define CIOT_CONFIG_APP_VER 0, 1, 0 #define CIOT_CONFIG_HARDWARE_NAME "CIoT HTTP Server" diff --git a/examples/nrf_ble_module/ciot_custom_config.h b/examples/nrf_ble_module/ciot_custom_config.h index 28e00ef3..9ff565f2 100644 --- a/examples/nrf_ble_module/ciot_custom_config.h +++ b/examples/nrf_ble_module/ciot_custom_config.h @@ -14,7 +14,7 @@ #include "ciot_log.h" -#define CIOT_CONFIG_LOG_LEVEL CIOT_LOG_LEVEL_NONE +#define CIOT_CONFIG_LOG_LEVEL CIOT__LOG_LEVEL__LOG_LEVEL_INFO #define CIOT_CONFIG_HARDWARE_NAME "CIoT NRF Ble Module" #define CIOT_CONFIG_APP_VER 0, 1, 0 diff --git a/examples/nrf_dfu_master/ciot_custom_config.h b/examples/nrf_dfu_master/ciot_custom_config.h index 755c4182..42161d14 100644 --- a/examples/nrf_dfu_master/ciot_custom_config.h +++ b/examples/nrf_dfu_master/ciot_custom_config.h @@ -12,7 +12,7 @@ #ifndef __CIOT_CUSTOM_CONFIG__H__ #define __CIOT_CUSTOM_CONFIG__H__ -#define CIOT_CONFIG_LOG_LEVEL CIOT_LOG_LEVEL_INFO +#define CIOT_CONFIG_LOG_LEVEL CIOT__LOG_LEVEL__LOG_LEVEL_INFO #define CIOT_CONFIG_APP_VER 0, 1, 0 #define CIOT_CONFIG_HARDWARE_NAME "CIoT HTTP Server" diff --git a/examples/uart/ciot_custom_config.h b/examples/uart/ciot_custom_config.h index 755c4182..42161d14 100644 --- a/examples/uart/ciot_custom_config.h +++ b/examples/uart/ciot_custom_config.h @@ -12,7 +12,7 @@ #ifndef __CIOT_CUSTOM_CONFIG__H__ #define __CIOT_CUSTOM_CONFIG__H__ -#define CIOT_CONFIG_LOG_LEVEL CIOT_LOG_LEVEL_INFO +#define CIOT_CONFIG_LOG_LEVEL CIOT__LOG_LEVEL__LOG_LEVEL_INFO #define CIOT_CONFIG_APP_VER 0, 1, 0 #define CIOT_CONFIG_HARDWARE_NAME "CIoT HTTP Server" From 123c1e8e0c5748cc33f12ce0687923a71d1e8c79 Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:31:02 -0300 Subject: [PATCH 05/14] feat: generate update proto files --- src/proto/ciot/proto/v1/errors.pb-c.c | 12 +++++++----- src/proto/ciot/proto/v1/errors.pb-c.h | 6 +++++- src/proto/ciot/proto/v1/gpio.pb-c.h | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/proto/ciot/proto/v1/errors.pb-c.c b/src/proto/ciot/proto/v1/errors.pb-c.c index 3b924f55..40303943 100644 --- a/src/proto/ciot/proto/v1/errors.pb-c.c +++ b/src/proto/ciot/proto/v1/errors.pb-c.c @@ -7,7 +7,7 @@ #endif #include "ciot/proto/v1/errors.pb-c.h" -static const ProtobufCEnumValue ciot_err__enum_values_by_number[32] = +static const ProtobufCEnumValue ciot_err__enum_values_by_number[33] = { { "OK", "CIOT_ERR__OK", 0 }, { "NULL_ARG", "CIOT_ERR__NULL_ARG", 1 }, @@ -41,11 +41,12 @@ static const ProtobufCEnumValue ciot_err__enum_values_by_number[32] = { "DISABLED", "CIOT_ERR__DISABLED", 29 }, { "FAIL", "CIOT_ERR__FAIL", 30 }, { "INVALID_INDEX", "CIOT_ERR__INVALID_INDEX", 31 }, + { "INVALID_KEY_SIZE", "CIOT_ERR__INVALID_KEY_SIZE", 32 }, }; static const ProtobufCIntRange ciot_err__value_ranges[] = { -{0, 0},{0, 32} +{0, 0},{0, 33} }; -static const ProtobufCEnumValueIndex ciot_err__enum_values_by_name[32] = +static const ProtobufCEnumValueIndex ciot_err__enum_values_by_name[33] = { { "BUSY", 7 }, { "CHECKSUM", 25 }, @@ -62,6 +63,7 @@ static const ProtobufCEnumValueIndex ciot_err__enum_values_by_name[32] = { "INVALID_HEADER", 28 }, { "INVALID_ID", 2 }, { "INVALID_INDEX", 31 }, + { "INVALID_KEY_SIZE", 32 }, { "INVALID_SIZE", 13 }, { "INVALID_STATE", 8 }, { "INVALID_TYPE", 3 }, @@ -87,9 +89,9 @@ const ProtobufCEnumDescriptor ciot_err__descriptor = "CiotErr", "CiotErr", "", - 32, + 33, ciot_err__enum_values_by_number, - 32, + 33, ciot_err__enum_values_by_name, 1, ciot_err__value_ranges, diff --git a/src/proto/ciot/proto/v1/errors.pb-c.h b/src/proto/ciot/proto/v1/errors.pb-c.h index a6dc82fc..da4efaa6 100644 --- a/src/proto/ciot/proto/v1/errors.pb-c.h +++ b/src/proto/ciot/proto/v1/errors.pb-c.h @@ -150,7 +150,11 @@ typedef enum _CiotErr { /* * Invalid index */ - CIOT_ERR__INVALID_INDEX = 31 + CIOT_ERR__INVALID_INDEX = 31, + /* + * Invalid key size + */ + CIOT_ERR__INVALID_KEY_SIZE = 32 PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(CIOT_ERR) } CiotErr; diff --git a/src/proto/ciot/proto/v1/gpio.pb-c.h b/src/proto/ciot/proto/v1/gpio.pb-c.h index ddf159bf..ce6505f3 100644 --- a/src/proto/ciot/proto/v1/gpio.pb-c.h +++ b/src/proto/ciot/proto/v1/gpio.pb-c.h @@ -142,7 +142,7 @@ struct _Ciot__GpioCfg */ ProtobufCBinaryData pull; /* - * Interval between gpio blinks + * Interval between gpio blinks in miliseconds */ uint32_t blink_interval; }; From 133c800cfff0d5f03fee120f1543240629a0484e Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:31:15 -0300 Subject: [PATCH 06/14] refact: removed commented item --- include/ciot_iface.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/ciot_iface.h b/include/ciot_iface.h index d7ec1e32..d21fca9f 100644 --- a/include/ciot_iface.h +++ b/include/ciot_iface.h @@ -61,7 +61,6 @@ typedef struct ciot_iface_req_status { ciot_iface_req_state_t state; ciot_msg_type_t type; ciot_iface_info_t iface; - // uint8_t iface; } ciot_iface_req_status_t; typedef struct ciot_iface ciot_iface_t; From 6cb908a41d5e6c81259c7fd4f0244d240e43936c Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:31:41 -0300 Subject: [PATCH 07/14] feat: implement ciot_crypt module --- include/ciot_crypt.h | 32 +++++++++ src/common/ciot_crypt.c | 147 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 include/ciot_crypt.h create mode 100644 src/common/ciot_crypt.c diff --git a/include/ciot_crypt.h b/include/ciot_crypt.h new file mode 100644 index 00000000..4e6295d4 --- /dev/null +++ b/include/ciot_crypt.h @@ -0,0 +1,32 @@ +/** + * @file ciot_crypt.h + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-10-04 + * + * @copyright Copyright (c) 2024 + * + */ + +#ifndef __CIOT_CRYPT__H__ +#define __CIOT_CRYPT__H__ + +#include +#include "ciot_err.h" + +typedef struct ciot_crypt_key +{ + const uint8_t *data; + int size; +} ciot_crypt_key_t; + +typedef struct ciot_crypt +{ + ciot_crypt_key_t key; +} ciot_crypt_t; + +ciot_err_t ciot_crypt_enc(ciot_crypt_t *crypt, char *data, char *out, int size); +ciot_err_t ciot_crypt_dec(ciot_crypt_t *crypt, char *data, char *out, int size); + +#endif //!__CIOT_CRYPT__H__ \ No newline at end of file diff --git a/src/common/ciot_crypt.c b/src/common/ciot_crypt.c new file mode 100644 index 00000000..97a32c8c --- /dev/null +++ b/src/common/ciot_crypt.c @@ -0,0 +1,147 @@ +/** + * @file ciot_crypt.c + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-10-04 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +#include +#include + +#include "mbedtls/aes.h" +#include "mbedtls/base64.h" +#include "ciot_crypt.h" + +static ciot_err_t ciot_crypt_enc_word(char *data, ciot_crypt_key_t *key, char *out); +static ciot_err_t ciot_crypt_dec_word(char *data, ciot_crypt_key_t *key, char *out); + +static const char *TAG = "ciot_crypt"; + +ciot_err_t ciot_crypt_enc(ciot_crypt_t *crypt, char *data, char *out, int size) +{ + CIOT_ERR_NULL_CHECK(crypt); + CIOT_ERR_NULL_CHECK(data); + CIOT_ERR_NULL_CHECK(out); + + int err = 0; + size_t len = strlen(data); + size_t block_size = 16 * ((len / 16) + 1); + unsigned char pad[block_size]; + unsigned char buf[block_size]; + + sprintf(pad, "%s", data); + + for (size_t i = len; i < block_size; i++) + { + pad[i] = (block_size - len); + } + + for (size_t i = 0; i < block_size; i+=16) + { + err = ciot_crypt_enc_word(&pad[i], &crypt->key, &buf[i]); + if(err) + { + return err; + } + } + + err = mbedtls_base64_encode(out, size, &len, buf, block_size); + if(err) + { + return CIOT_ERR__INVALID_SIZE; + } + + return CIOT_ERR__OK; +} + +ciot_err_t ciot_crypt_dec(ciot_crypt_t *crypt, char *data, char *out, int size) +{ + CIOT_ERR_NULL_CHECK(crypt); + CIOT_ERR_NULL_CHECK(data); + CIOT_ERR_NULL_CHECK(out); + + int err = 0; + size_t len = strlen(data); + size_t decoded_size = 0; + unsigned char buf[len]; + + err = mbedtls_base64_decode(buf, len, &decoded_size, data, len); + if(err) + { + return CIOT_ERR__INVALID_SIZE; + } + + if(decoded_size >= size) + { + return CIOT_ERR__INVALID_SIZE; + } + + for (size_t i = 0; i < decoded_size; i+=16) + { + err = ciot_crypt_dec_word(&buf[i], &crypt->key, out); + if(err) + { + return err; + } + } + + int decrypted_size = decoded_size - out[decoded_size - 1]; + out[decrypted_size] = '\0'; + return CIOT_ERR__OK; +} + +static ciot_err_t ciot_crypt_enc_word(char *data, ciot_crypt_key_t *key, char *out) +{ + mbedtls_aes_context aes; + mbedtls_aes_init(&aes); + + int err = mbedtls_aes_setkey_enc(&aes, (const unsigned char *)key->data, key->size * 8); + if(err) + { + CIOT_LOGE(TAG, "setkey enc mbedtls error: %d", err); + mbedtls_aes_free(&aes); + return CIOT_ERR__INVALID_KEY_SIZE; + } + + err = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, (const unsigned char *)data, (unsigned char *)out); + if(err) + { + CIOT_LOGE(TAG, "encrypt mbedtls error: %d", err); + mbedtls_aes_free(&aes); + return CIOT_ERR__FAIL; + } + + mbedtls_aes_free(&aes); + + return CIOT_ERR__OK; +} + +static ciot_err_t ciot_crypt_dec_word(char *data, ciot_crypt_key_t *key, char *out) +{ + mbedtls_aes_context aes; + mbedtls_aes_init(&aes); + + int err = mbedtls_aes_setkey_dec(&aes, key->data, key->size * 8); + if(err) + { + CIOT_LOGE(TAG, "setkey dec mbedtls error: %d", err); + mbedtls_aes_free(&aes); + return CIOT_ERR__INVALID_KEY_SIZE; + } + + err = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, (const unsigned char*)data, (unsigned char *)out); + if(err) + { + CIOT_LOGE(TAG, "crypt ecb mbedtls error: %d", err); + mbedtls_aes_free(&aes); + return CIOT_ERR__INVALID_KEY_SIZE; + } + + mbedtls_aes_free(&aes); + return CIOT_ERR__OK; +} From 254cb0b360389ed6a9e76178887efa177dbccd9a Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:31:53 -0300 Subject: [PATCH 08/14] test: implement ciot_crypt module tests --- tests/common/ciot_crypt_test.c | 163 +++++++++++++++++++++++++++++++++ tests/main.c | 2 + 2 files changed, 165 insertions(+) diff --git a/tests/common/ciot_crypt_test.c b/tests/common/ciot_crypt_test.c index e69de29b..f790e5c5 100644 --- a/tests/common/ciot_crypt_test.c +++ b/tests/common/ciot_crypt_test.c @@ -0,0 +1,163 @@ +/** + * @file ciot_crypt_test.c + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-10-04 + * + * @copyright Copyright (c) 2024 + * + */ + +#include + +#include "unity.h" +#include "ciot_crypt.h" + +#define CIOT_CRYPT_INVALID_KEY (uint8_t*) "12345678" +#define CIOT_CRYPT_INVALID_KEY_SIZE 8 + +#define CIOT_CRYPT_VALID_KEY (uint8_t*) "1234567891234567" +#define CIOT_CRYPT_VALID_KEY_SIZE 16 + +#define CIOT_CRYPT_DATA_DECRYPTED "data_test" +#define CIOT_CRYPT_DATA_ENCRYPTED "v6PcItCKYZersU+bnAo1CA==" + +void test_ciot_crypt_enc_null_crypt(void) +{ + char *data = CIOT_CRYPT_DATA_DECRYPTED; + char out[32]; + ciot_err_t err = ciot_crypt_enc(NULL, data, out, sizeof(out)); + TEST_ASSERT(err == CIOT_ERR__NULL_ARG); +} + +void test_ciot_crypt_enc_null_data(void) +{ + char out[32]; + ciot_crypt_t crypt = { 0 }; + ciot_err_t err = ciot_crypt_enc(&crypt, NULL, out, sizeof(out)); + TEST_ASSERT(err == CIOT_ERR__NULL_ARG); +} + +void test_ciot_crypt_enc_null_buffer(void) +{ + char *data = CIOT_CRYPT_DATA_DECRYPTED; + ciot_crypt_t crypt = { 0 }; + ciot_err_t err = ciot_crypt_enc(&crypt, data, NULL, 0); + TEST_ASSERT(err == CIOT_ERR__NULL_ARG); +} + +void test_ciot_crypt_enc_invalid_key_size(void) +{ + char out[32]; + char *data = CIOT_CRYPT_DATA_DECRYPTED; + ciot_crypt_t crypt = { + .key.data = CIOT_CRYPT_INVALID_KEY, + .key.size = CIOT_CRYPT_INVALID_KEY_SIZE, + }; + ciot_err_t err = ciot_crypt_enc(&crypt, data, out, sizeof(out)); + TEST_ASSERT(err == CIOT_ERR__INVALID_KEY_SIZE); +} + +void test_ciot_crypt_enc_invalid_size(void) +{ + char out[5]; + char *data = CIOT_CRYPT_DATA_DECRYPTED; + ciot_crypt_t crypt = { + .key.data = CIOT_CRYPT_VALID_KEY, + .key.size = CIOT_CRYPT_VALID_KEY_SIZE, + }; + ciot_err_t err = ciot_crypt_enc(&crypt, data, out, sizeof(out)); + TEST_ASSERT(err == CIOT_ERR__INVALID_SIZE); +} + +void test_ciot_crypt_enc_ok(void) +{ + char out[32]; + char *data = CIOT_CRYPT_DATA_DECRYPTED; + ciot_crypt_t crypt = { + .key.data = CIOT_CRYPT_VALID_KEY, + .key.size = CIOT_CRYPT_VALID_KEY_SIZE, + }; + ciot_err_t err = ciot_crypt_enc(&crypt, data, out, sizeof(out)); + TEST_ASSERT(err == CIOT_ERR__OK); + TEST_ASSERT(strcmp(out, CIOT_CRYPT_DATA_ENCRYPTED) == 0); +} + +void test_ciot_crypt_dec_null_crypt(void) +{ + char *data = CIOT_CRYPT_DATA_ENCRYPTED; + char out[32]; + ciot_err_t err = ciot_crypt_dec(NULL, data, out, sizeof(out)); + TEST_ASSERT(err == CIOT_ERR__NULL_ARG); +} + +void test_ciot_crypt_dec_null_data(void) +{ + char out[32]; + ciot_crypt_t crypt = { 0 }; + ciot_err_t err = ciot_crypt_dec(&crypt, NULL, out, sizeof(out)); + TEST_ASSERT(err == CIOT_ERR__NULL_ARG); +} + +void test_ciot_crypt_dec_null_buffer(void) +{ + char *data = CIOT_CRYPT_DATA_ENCRYPTED; + ciot_crypt_t crypt = { 0 }; + ciot_err_t err = ciot_crypt_dec(&crypt, data, NULL, 0); + TEST_ASSERT(err == CIOT_ERR__NULL_ARG); +} + +void test_ciot_crypt_dec_invalid_key_size(void) +{ + char out[32]; + char *data = CIOT_CRYPT_DATA_ENCRYPTED; + ciot_crypt_t crypt = { + .key.data = CIOT_CRYPT_INVALID_KEY, + .key.size = CIOT_CRYPT_INVALID_KEY_SIZE, + }; + ciot_err_t err = ciot_crypt_dec(&crypt, data, out, sizeof(out)); + TEST_ASSERT(err == CIOT_ERR__INVALID_KEY_SIZE); +} + +void test_ciot_crypt_dec_invalid_size(void) +{ + char out[5]; + char *data = CIOT_CRYPT_DATA_ENCRYPTED; + ciot_crypt_t crypt = { + .key.data = CIOT_CRYPT_VALID_KEY, + .key.size = CIOT_CRYPT_VALID_KEY_SIZE, + }; + ciot_err_t err = ciot_crypt_dec(&crypt, data, out, sizeof(out)); + TEST_ASSERT(err == CIOT_ERR__INVALID_SIZE); +} + +void test_ciot_crypt_dec_ok(void) +{ + char out[32]; + char *data = CIOT_CRYPT_DATA_ENCRYPTED; + ciot_crypt_t crypt = { + .key.data = CIOT_CRYPT_VALID_KEY, + .key.size = CIOT_CRYPT_VALID_KEY_SIZE, + }; + ciot_err_t err = ciot_crypt_dec(&crypt, data, out, sizeof(out)); + TEST_ASSERT(err == CIOT_ERR__OK); + TEST_ASSERT(strcmp(out, CIOT_CRYPT_DATA_DECRYPTED) == 0); +} + +void test_ciot_crypt(void) +{ + RUN_TEST(test_ciot_crypt_enc_null_crypt); + RUN_TEST(test_ciot_crypt_enc_null_data); + RUN_TEST(test_ciot_crypt_enc_null_buffer); + RUN_TEST(test_ciot_crypt_enc_invalid_key_size); + RUN_TEST(test_ciot_crypt_enc_invalid_size); + RUN_TEST(test_ciot_crypt_enc_ok); + + RUN_TEST(test_ciot_crypt_dec_null_crypt); + RUN_TEST(test_ciot_crypt_dec_null_data); + RUN_TEST(test_ciot_crypt_dec_null_buffer); + RUN_TEST(test_ciot_crypt_dec_invalid_key_size); + RUN_TEST(test_ciot_crypt_dec_invalid_size); + RUN_TEST(test_ciot_crypt_dec_ok); +} diff --git a/tests/main.c b/tests/main.c index 239b826d..6bdee5e3 100644 --- a/tests/main.c +++ b/tests/main.c @@ -15,6 +15,7 @@ void test_ciot_decoder_s(); void test_ciot_decoder_slip(); +void test_ciot_crypt(); void setUp(void) { // set stuff up here @@ -29,5 +30,6 @@ int main(void) UNITY_BEGIN(); test_ciot_decoder_s(); test_ciot_decoder_slip(); + test_ciot_crypt(); return UNITY_END(); } From f114350d50b5b22b7d28b0f5cc95fb1f209f8577 Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Sat, 5 Oct 2024 07:42:38 -0300 Subject: [PATCH 09/14] feat: add config --- include/ciot_config.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/ciot_config.h b/include/ciot_config.h index 72af4150..9dc35ded 100644 --- a/include/ciot_config.h +++ b/include/ciot_config.h @@ -33,5 +33,6 @@ #define CIOT_CONFIG_FEATURE_MQTT_CLIENT 0 #define CIOT_CONFIG_FEATURE_TIMER 0 #define CIOT_CONFIG_FEATURE_SERIALIZER 0 +#define CIOT_CONFIG_FEATURE_CRYPT 0 #endif \ No newline at end of file From c524273031d0c7ae605dce2177adef76d4f9aef9 Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Sat, 5 Oct 2024 07:42:57 -0300 Subject: [PATCH 10/14] feat: add macro to enable/disable encryption module --- src/common/ciot_crypt.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/common/ciot_crypt.c b/src/common/ciot_crypt.c index 97a32c8c..5973b1b9 100644 --- a/src/common/ciot_crypt.c +++ b/src/common/ciot_crypt.c @@ -9,13 +9,17 @@ * */ +#include "ciot_config.h" +#include "ciot_crypt.h" + +#if CIOT_CONFIG_FEATURE_CRYPT == 1 + #include #include #include #include "mbedtls/aes.h" #include "mbedtls/base64.h" -#include "ciot_crypt.h" static ciot_err_t ciot_crypt_enc_word(char *data, ciot_crypt_key_t *key, char *out); static ciot_err_t ciot_crypt_dec_word(char *data, ciot_crypt_key_t *key, char *out); @@ -33,6 +37,8 @@ ciot_err_t ciot_crypt_enc(ciot_crypt_t *crypt, char *data, char *out, int size) size_t block_size = 16 * ((len / 16) + 1); unsigned char pad[block_size]; unsigned char buf[block_size]; + memset(pad, 0, block_size); + memset(buf, 0, block_size); sprintf(pad, "%s", data); @@ -69,6 +75,7 @@ ciot_err_t ciot_crypt_dec(ciot_crypt_t *crypt, char *data, char *out, int size) size_t len = strlen(data); size_t decoded_size = 0; unsigned char buf[len]; + memset(buf, 0, len); err = mbedtls_base64_decode(buf, len, &decoded_size, data, len); if(err) @@ -83,7 +90,7 @@ ciot_err_t ciot_crypt_dec(ciot_crypt_t *crypt, char *data, char *out, int size) for (size_t i = 0; i < decoded_size; i+=16) { - err = ciot_crypt_dec_word(&buf[i], &crypt->key, out); + err = ciot_crypt_dec_word(&buf[i], &crypt->key, &out[i]); if(err) { return err; @@ -145,3 +152,17 @@ static ciot_err_t ciot_crypt_dec_word(char *data, ciot_crypt_key_t *key, char *o mbedtls_aes_free(&aes); return CIOT_ERR__OK; } + +#else + +ciot_err_t ciot_crypt_enc(ciot_crypt_t *crypt, char *data, char *out, int size) +{ + return CIOT_ERR__DISABLED; +} + +ciot_err_t ciot_crypt_dec(ciot_crypt_t *crypt, char *data, char *out, int size) +{ + return CIOT_ERR__DISABLED; +} + +#endif From ddb5a9baad83ad6ba42c0512c7f29356ed1848b2 Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Sat, 5 Oct 2024 07:43:18 -0300 Subject: [PATCH 11/14] feat: check if mqtt is connected before publish data --- src/mg/ciot_mqtt_client.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mg/ciot_mqtt_client.c b/src/mg/ciot_mqtt_client.c index 1be94f1b..53bbf0c9 100644 --- a/src/mg/ciot_mqtt_client.c +++ b/src/mg/ciot_mqtt_client.c @@ -105,6 +105,7 @@ ciot_err_t ciot_mqtt_client_pub(ciot_mqtt_client_t self, char *topic, uint8_t *d CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(topic); CIOT_ERR_NULL_CHECK(self->connection); + CIOT_ERR_VALUE_CHECK(self->base.status.state, CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTED, CIOT_ERR__INVALID_STATE); CIOT_ERR_EMPTY_STRING_CHECK(topic); struct mg_mqtt_opts opts = {0}; struct mg_str msg = {0}; From 7e04673811a85d19f27eb30fafdc80ca2e5f8483 Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Sat, 5 Oct 2024 07:43:36 -0300 Subject: [PATCH 12/14] test: add config --- tests/ciot_custom_config.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ciot_custom_config.h b/tests/ciot_custom_config.h index 44254531..6c8c00e9 100644 --- a/tests/ciot_custom_config.h +++ b/tests/ciot_custom_config.h @@ -34,5 +34,6 @@ #define CIOT_CONFIG_FEATURE_MQTT_CLIENT 0 #define CIOT_CONFIG_FEATURE_TIMER 0 #define CIOT_CONFIG_FEATURE_SERIALIZER 0 +#define CIOT_CONFIG_FEATURE_CRYPT 1 #endif //!__CIOT_CUSTOM_CONFIG__H__ From 103357fce87382b0db176d47b39d93cbc04b1de0 Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Sat, 5 Oct 2024 07:43:47 -0300 Subject: [PATCH 13/14] test: improve encryption tests --- tests/common/ciot_crypt_test.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/common/ciot_crypt_test.c b/tests/common/ciot_crypt_test.c index f790e5c5..db773139 100644 --- a/tests/common/ciot_crypt_test.c +++ b/tests/common/ciot_crypt_test.c @@ -17,23 +17,23 @@ #define CIOT_CRYPT_INVALID_KEY (uint8_t*) "12345678" #define CIOT_CRYPT_INVALID_KEY_SIZE 8 -#define CIOT_CRYPT_VALID_KEY (uint8_t*) "1234567891234567" +#define CIOT_CRYPT_VALID_KEY (uint8_t*) "1234567890123456" #define CIOT_CRYPT_VALID_KEY_SIZE 16 -#define CIOT_CRYPT_DATA_DECRYPTED "data_test" -#define CIOT_CRYPT_DATA_ENCRYPTED "v6PcItCKYZersU+bnAo1CA==" +#define CIOT_CRYPT_DATA_DECRYPTED "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat" +#define CIOT_CRYPT_DATA_ENCRYPTED "YFGMkV1Cyz4ipXOXE6jw27qv4BCWYnJdwNnRIFVpJKe+k9CMC/j1KLPb801/eo9Hieb5D8Y6Npng08DbEU7wpYRYpB6wBn2NTql3qIkhYYvQzsPi9rY3gZtMBpe+5MV83d71wUEgGVnsu224gvCmOG0ATRhTu5ltNq6AICqw+fyScYexCTVP2aEDNIp+13WcD1LSkNOO6xWyOlSH4xhbsJGDLMq379g3IEoDS9CltUrO+vDlJ4EcNy72j6frD4KElPVgk9SgAbtqvyZ7vcwV3K5qK8GdpEVi7hpCQ9pQXskUL2oAnyR6U/A64xgu9G87" void test_ciot_crypt_enc_null_crypt(void) { char *data = CIOT_CRYPT_DATA_DECRYPTED; - char out[32]; + char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; ciot_err_t err = ciot_crypt_enc(NULL, data, out, sizeof(out)); TEST_ASSERT(err == CIOT_ERR__NULL_ARG); } void test_ciot_crypt_enc_null_data(void) { - char out[32]; + char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; ciot_crypt_t crypt = { 0 }; ciot_err_t err = ciot_crypt_enc(&crypt, NULL, out, sizeof(out)); TEST_ASSERT(err == CIOT_ERR__NULL_ARG); @@ -49,7 +49,7 @@ void test_ciot_crypt_enc_null_buffer(void) void test_ciot_crypt_enc_invalid_key_size(void) { - char out[32]; + char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; char *data = CIOT_CRYPT_DATA_DECRYPTED; ciot_crypt_t crypt = { .key.data = CIOT_CRYPT_INVALID_KEY, @@ -73,7 +73,7 @@ void test_ciot_crypt_enc_invalid_size(void) void test_ciot_crypt_enc_ok(void) { - char out[32]; + char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; char *data = CIOT_CRYPT_DATA_DECRYPTED; ciot_crypt_t crypt = { .key.data = CIOT_CRYPT_VALID_KEY, @@ -87,14 +87,14 @@ void test_ciot_crypt_enc_ok(void) void test_ciot_crypt_dec_null_crypt(void) { char *data = CIOT_CRYPT_DATA_ENCRYPTED; - char out[32]; + char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; ciot_err_t err = ciot_crypt_dec(NULL, data, out, sizeof(out)); TEST_ASSERT(err == CIOT_ERR__NULL_ARG); } void test_ciot_crypt_dec_null_data(void) { - char out[32]; + char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; ciot_crypt_t crypt = { 0 }; ciot_err_t err = ciot_crypt_dec(&crypt, NULL, out, sizeof(out)); TEST_ASSERT(err == CIOT_ERR__NULL_ARG); @@ -110,7 +110,7 @@ void test_ciot_crypt_dec_null_buffer(void) void test_ciot_crypt_dec_invalid_key_size(void) { - char out[32]; + char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; char *data = CIOT_CRYPT_DATA_ENCRYPTED; ciot_crypt_t crypt = { .key.data = CIOT_CRYPT_INVALID_KEY, @@ -134,7 +134,7 @@ void test_ciot_crypt_dec_invalid_size(void) void test_ciot_crypt_dec_ok(void) { - char out[32]; + char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; char *data = CIOT_CRYPT_DATA_ENCRYPTED; ciot_crypt_t crypt = { .key.data = CIOT_CRYPT_VALID_KEY, From f5b41ce42f10c34769e299a25c2043d0fe1f8167 Mon Sep 17 00:00:00 2001 From: wesley-santos <53940899+wjsan@users.noreply.github.com> Date: Tue, 22 Oct 2024 11:02:46 -0300 Subject: [PATCH 14/14] refact: CIOT_ERR to CIOT__ERR --- .vscode/c_cpp_properties.json | 2 +- ciot_ext.tag | 54 +- examples/cli/ciot_cli.c | 5 +- examples/cli/ciot_cli_conn.c | 8 +- examples/cli/ciot_cli_ota.c | 2 +- examples/tcp_client/main.c | 2 +- include/ciot_err.h | 28 +- include/ciot_storage_auto.h | 19 + include/ciot_storage_fat.h | 19 + include/ciot_storage_fs.h | 3 + include/ciot_storage_spiffs.h | 19 + proto | 2 +- src/common/ciot_base.c | 35 +- src/common/ciot_ble_adv_base.c | 12 +- src/common/ciot_ble_base.c | 20 +- src/common/ciot_ble_scn_base.c | 20 +- src/common/ciot_crypt.c | 36 +- src/common/ciot_decoder_s.c | 8 +- src/common/ciot_decoder_slip.c | 8 +- src/common/ciot_dfu_nrf.c | 68 +-- src/common/ciot_eth_base.c | 15 +- src/common/ciot_gpio_base.c | 22 +- src/common/ciot_http_client_base.c | 12 +- src/common/ciot_http_server_base.c | 12 +- src/common/ciot_model_base.c | 16 +- src/common/ciot_mqtt_client_base.c | 18 +- src/common/ciot_ntp_base.c | 18 +- src/common/ciot_ota_base.c | 18 +- src/common/ciot_socket_base.c | 14 +- src/common/ciot_storage.c | 36 +- src/common/ciot_storage_fs.c | 22 +- src/common/ciot_str.c | 2 +- src/common/ciot_sys_base.c | 20 +- src/common/ciot_tcp_base.c | 18 +- src/common/ciot_timer_base.c | 2 +- src/common/ciot_uart_base.c | 12 +- src/common/ciot_usb_base.c | 20 +- src/common/ciot_viface.c | 4 +- src/common/ciot_viface_base.c | 16 +- src/common/ciot_wifi_base.c | 18 +- src/core/ciot.c | 44 +- src/core/ciot_err.c | 4 +- src/core/ciot_iface.c | 40 +- src/core/ciot_model.c | 4 +- src/core/ciot_msg.c | 6 +- src/esp32/ciot_eth.c | 28 +- src/esp32/ciot_gpio.c | 2 +- src/esp32/ciot_http_server.c | 75 ++- src/esp32/ciot_mqtt_client.c | 10 +- src/esp32/ciot_ntp.c | 4 +- src/esp32/ciot_ota.c | 10 +- src/esp32/ciot_storage_auto.c | 39 ++ src/esp32/ciot_storage_fat.c | 62 +++ src/esp32/ciot_storage_nvs.c | 12 +- src/esp32/ciot_storage_spiffs.c | 61 +++ src/esp32/ciot_sys.c | 14 +- src/esp32/ciot_tcp.c | 174 +++++-- src/esp32/ciot_uart.c | 10 +- src/esp32/ciot_wifi.c | 35 +- src/mg/ciot_http_client.c | 14 +- src/mg/ciot_http_server.c | 8 +- src/mg/ciot_mqtt_client.c | 10 +- src/mg/ciot_socket.c | 14 +- src/mg/ciot_sys.c | 14 +- src/nrf/ciot_ble.c | 10 +- src/nrf/ciot_ble_adv.c | 16 +- src/nrf/ciot_ble_scn.c | 25 +- src/nrf/ciot_gpio.c | 36 +- src/nrf/ciot_sys.c | 8 +- src/nrf/ciot_uart.c | 14 +- src/nrf/ciot_usb.c | 12 +- src/proto/ciot/proto/v1/ble_scn.pb-c.c | 56 +- src/proto/ciot/proto/v1/ble_scn.pb-c.h | 18 +- src/proto/ciot/proto/v1/dfu.pb-c.c | 2 +- src/proto/ciot/proto/v1/dfu.pb-c.h | 4 +- src/proto/ciot/proto/v1/errors.pb-c.c | 88 ++-- src/proto/ciot/proto/v1/errors.pb-c.h | 74 +-- src/proto/ciot/proto/v1/msg.pb-c.c | 2 +- src/proto/ciot/proto/v1/msg.pb-c.h | 4 +- src/proto/ciot/proto/v1/tcp.pb-c.c | 67 ++- src/proto/ciot/proto/v1/tcp.pb-c.h | 18 +- src/proto/ciot/proto/v1/wifi.pb-c.h | 2 +- src/proto/ciot/proto/v2/msg.pb-c.c | 683 +++++++++++++++++++++++++ src/proto/ciot/proto/v2/msg.pb-c.h | 381 ++++++++++++++ src/win/ciot_ble.c | 6 +- src/win/ciot_ble_adv.c | 6 +- src/win/ciot_ble_scn.c | 4 +- src/win/ciot_eth.c | 4 +- src/win/ciot_gpio.c | 2 +- src/win/ciot_ntp.c | 4 +- src/win/ciot_ota.c | 6 +- src/win/ciot_tcp.c | 6 +- src/win/ciot_uart.c | 24 +- src/win/ciot_wifi.c | 12 +- tests/common/ciot_crypt_test.c | 24 +- tests/common/ciot_decoder_s_test.c | 20 +- tests/common/ciot_decoder_slip_test.c | 20 +- 97 files changed, 2308 insertions(+), 729 deletions(-) create mode 100644 include/ciot_storage_auto.h create mode 100644 include/ciot_storage_fat.h create mode 100644 include/ciot_storage_spiffs.h create mode 100644 src/esp32/ciot_storage_auto.c create mode 100644 src/esp32/ciot_storage_fat.c create mode 100644 src/esp32/ciot_storage_spiffs.c create mode 100644 src/proto/ciot/proto/v2/msg.pb-c.c create mode 100644 src/proto/ciot/proto/v2/msg.pb-c.h diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 26037ca5..4468a616 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -1,6 +1,6 @@ { "env": { - "IDF_COMPILER": "C:\\Users\\intec\\.espressif\\tools\\xtensa-esp-elf\\esp-13.2.0_20230928\\xtensa-esp-elf\\bin\\xtensa-esp32-elf-gcc.exe" + "IDF_COMPILER": "C:\\Users\\BINAR\\.espressif\\tools\\xtensa-esp-elf\\esp-13.2.0_20240530\\xtensa-esp-elf\\bin\\xtensa-esp32-elf-gcc.exe" }, "configurations": [ { diff --git a/ciot_ext.tag b/ciot_ext.tag index 21377d7b..1e206b35 100644 --- a/ciot_ext.tag +++ b/ciot_ext.tag @@ -8593,7 +8593,7 @@ const ProtobufCEnumDescriptor - ciot__ciot_err__descriptor + ciot__ciot__err__descriptor errors_8pb-c_8c.html abfa771d06cfc5550e0683759baec4d94 @@ -8618,158 +8618,158 @@ - CIOT__CIOT_ERR__CIOT_OK + CIOT__CIOT__ERR__CIOT_OK errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea7294c3a453eac537a9ff371894537b64 - CIOT__CIOT_ERR__CIOT_ERR_NULL_ARG + CIOT__CIOT__ERR__CIOT_ERR_NULL_ARG errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7deaa9b0cc3fe841fa6dcbae5ce94d8e5da2 - CIOT__CIOT_ERR__CIOT_ERR_INVALID_ID + CIOT__CIOT__ERR__CIOT_ERR_INVALID_ID errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea8a8e57afe3f9a5dce3b93d29d10fa202 - CIOT__CIOT_ERR__CIOT_ERR_INVALID_TYPE + CIOT__CIOT__ERR__CIOT_ERR_INVALID_TYPE errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7deafbdfdfd8b2f5ab61c54fbe1370013c0c - CIOT__CIOT_ERR__CIOT_ERR_OVERFLOW + CIOT__CIOT__ERR__CIOT_ERR_OVERFLOW errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7deaedb6fc1b6d985accf9cc42edd6463410 - CIOT__CIOT_ERR__CIOT_ERR_NOT_IMPLEMENTED + CIOT__CIOT__ERR__CIOT_ERR_NOT_IMPLEMENTED errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7deaec7398a368e2293053c98bca4356c0e6 - CIOT__CIOT_ERR__CIOT_ERR_NOT_SUPPORTED + CIOT__CIOT__ERR__CIOT_ERR_NOT_SUPPORTED errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea6a4165ef2682069543616351ffce0a9b - CIOT__CIOT_ERR__CIOT_ERR_BUSY + CIOT__CIOT__ERR__CIOT_ERR_BUSY errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea2a4579447082d796a436c2c78b776252 - CIOT__CIOT_ERR__CIOT_ERR_INVALID_STATE + CIOT__CIOT__ERR__CIOT_ERR_INVALID_STATE errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7deaa4b52322af2548f57a264e332efe8d03 - CIOT__CIOT_ERR__CIOT_ERR_SERIALIZATION + CIOT__CIOT__ERR__CIOT_ERR_SERIALIZATION errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea4802783865a263f80f5c489e27912f70 - CIOT__CIOT_ERR__CIOT_ERR_DESERIALIZATION + CIOT__CIOT__ERR__CIOT_ERR_DESERIALIZATION errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7deacf22ef4d12595d78bea7eb714f1f4649 - CIOT__CIOT_ERR__CIOT_ERR_SEND_DATA + CIOT__CIOT__ERR__CIOT_ERR_SEND_DATA errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea857ea48cafe0270a008b3451516db7b6 - CIOT__CIOT_ERR__CIOT_ERR_RECV_DATA + CIOT__CIOT__ERR__CIOT_ERR_RECV_DATA errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7deabc5eb6ae3edd4b27be0f0d74802a61aa - CIOT__CIOT_ERR__CIOT_ERR_INVALID_SIZE + CIOT__CIOT__ERR__CIOT_ERR_INVALID_SIZE errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7deaeba2719688998ddf2876c145dcaedd31 - CIOT__CIOT_ERR__CIOT_ERR_CLOSED + CIOT__CIOT__ERR__CIOT_ERR_CLOSED errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7deaca2d4a4a40eaaf1738d93a94b75926a2 - CIOT__CIOT_ERR__CIOT_ERR_NOT_FOUND + CIOT__CIOT__ERR__CIOT_ERR_NOT_FOUND errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea82cc6d4f90cf3af0761080ce27fa5e03 - CIOT__CIOT_ERR__CIOT_ERR_VALIDATION_FAILED + CIOT__CIOT__ERR__CIOT_ERR_VALIDATION_FAILED errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea05cdc8fb5aebd0d9e6b46bd569e0fa58 - CIOT__CIOT_ERR__CIOT_ERR_CONNECTION + CIOT__CIOT__ERR__CIOT_ERR_CONNECTION errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea537f8f5b9337a62e725d2bb5db4ab9d6 - CIOT__CIOT_ERR__CIOT_ERR_DISCONNECTION + CIOT__CIOT__ERR__CIOT_ERR_DISCONNECTION errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea282dd6c1d32ccaa5413a312b9ab8d0ef - CIOT__CIOT_ERR__CIOT_ERR_EXCEPTION + CIOT__CIOT__ERR__CIOT_ERR_EXCEPTION errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7deaf9d0cb8c8c0df17d8e549f2b85a43ac4 - CIOT__CIOT_ERR__CIOT_ERR_TERMINATOR_MISSING + CIOT__CIOT__ERR__CIOT_ERR_TERMINATOR_MISSING errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea5d77a625d646b85d0bb853eb2b0e6965 - CIOT__CIOT_ERR__CIOT_ERR_INVALID_ARG + CIOT__CIOT__ERR__CIOT_ERR_INVALID_ARG errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7deaa843ed8fe22087d8713e2ac76b22e8f9 - CIOT__CIOT_ERR__CIOT_ERR_NO_MEMORY + CIOT__CIOT__ERR__CIOT_ERR_NO_MEMORY errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea552542b71379e9f4872c979506e84ae5 - CIOT__CIOT_ERR__CIOT_ERR_TIMEOUT + CIOT__CIOT__ERR__CIOT_ERR_TIMEOUT errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea56d156577c434fdc956311dc244e54bc - CIOT__CIOT_ERR__CIOT_FAIL + CIOT__CIOT__ERR__CIOT_FAIL errors_8pb-c_8h.html a9ed00d4c46bfaed408ceb78e9f6ac7dea1d6b6afa2c2854a9903371653b4b9dfe const ProtobufCEnumDescriptor - ciot__ciot_err__descriptor + ciot__ciot__err__descriptor errors_8pb-c_8h.html abfa771d06cfc5550e0683759baec4d94 diff --git a/examples/cli/ciot_cli.c b/examples/cli/ciot_cli.c index 3f74633b..7abe80fb 100644 --- a/examples/cli/ciot_cli.c +++ b/examples/cli/ciot_cli.c @@ -27,9 +27,8 @@ static const char *TAG = "ciot_cli"; static const char *default_argv[] = { "ciot_cli", - "save", - "5", - "9" + "set-dfu", + "1", }; static bool run_cmd = false; diff --git a/examples/cli/ciot_cli_conn.c b/examples/cli/ciot_cli_conn.c index e4874987..c0708268 100644 --- a/examples/cli/ciot_cli_conn.c +++ b/examples/cli/ciot_cli_conn.c @@ -54,7 +54,7 @@ ciot_err_t ciot_cli_conn_start() if (type == CIOT__IFACE_TYPE__IFACE_TYPE_UNKNOWN || conn_data == NULL) { printf("ERROR: connection not configured.\n"); - exit(CIOT_ERR__CONNECTION); + exit(CIOT__ERR__CONNECTION); } if (type == CIOT__IFACE_TYPE__IFACE_TYPE_UART && conn_data && conn_data->uart) @@ -75,7 +75,7 @@ ciot_err_t ciot_cli_conn_start() if (conn == NULL) { printf("selected connection (%s) is not configured\n", ciot__iface_type__descriptor.values[type].name); - exit(CIOT_ERR__CONNECTION); + exit(CIOT__ERR__CONNECTION); } cfgs[1] = conn_data; @@ -110,12 +110,14 @@ int ciot_cli_conn(int argc, char const *argv[]) ciot_err_t ciot_cli_conn_send_msg(ciot_msg_t *msg) { + ciot_msg_print(&msg->base); return ciot_iface_send_req(conn, msg); } ciot_err_t ciot_cli_conn_send_req(ciot_msg_t *msg, ciot_iface_event_handler_fn *req_handler_fn) { req_handler = req_handler_fn; + ciot_msg_print(&msg->base); return ciot_iface_send_req(conn, msg); } @@ -331,5 +333,5 @@ static ciot_err_t ciot_cli_event_handler(ciot_iface_t *sender, ciot_iface_event_ CIOT_LOGI(TAG, "Other event type: %d", event->type); break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/examples/cli/ciot_cli_ota.c b/examples/cli/ciot_cli_ota.c index 7405d89a..24b0d2e0 100644 --- a/examples/cli/ciot_cli_ota.c +++ b/examples/cli/ciot_cli_ota.c @@ -155,5 +155,5 @@ static ciot_err_t ciot_cli_ota_event_handler(ciot_iface_t *iface, ciot_iface_eve CIOT_LOGI(TAG, "event received: %d", event->type); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/examples/tcp_client/main.c b/examples/tcp_client/main.c index aea7f906..16f905a9 100644 --- a/examples/tcp_client/main.c +++ b/examples/tcp_client/main.c @@ -97,5 +97,5 @@ static ciot_err_t app_event_handler(ciot_iface_t *sender, ciot_iface_event_t *ev CIOT_LOGI(TAG, "Firwmare Version: %d.%d.%d", info->app_ver.data[0], info->app_ver.data[1], info->app_ver.data[2]); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/include/ciot_err.h b/include/ciot_err.h index 381c3749..e2402752 100644 --- a/include/ciot_err.h +++ b/include/ciot_err.h @@ -9,34 +9,34 @@ * */ -#ifndef __CIOT_ERR__H__ -#define __CIOT_ERR__H__ +#ifndef __CIOT__ERR__H__ +#define __CIOT__ERR__H__ #include "ciot/proto/v1/errors.pb-c.h" #include "ciot_log.h" -typedef CiotErr ciot_err_t; +typedef Ciot__Err ciot_err_t; #define CIOT_ERR_LOG(TAG, x) CIOT_LOGE(TAG, "%s:%d %s", __FILE__, __LINE__, ciot_err_to_message(x)) #define CIOT_ERR_NULL_CHECK(x) \ if (x == NULL) \ { \ - CIOT_ERR_LOG("ciot_err", CIOT_ERR__NULL_ARG); \ - return CIOT_ERR__NULL_ARG; \ + CIOT_ERR_LOG("ciot_err", CIOT__ERR__NULL_ARG); \ + return CIOT__ERR__NULL_ARG; \ } #define CIOT_ERR_INDEX_CHECK(index, min, max) \ if (index < min) \ - return CIOT_ERR__INVALID_INDEX; \ + return CIOT__ERR__INVALID_INDEX; \ if (index > max) \ - return CIOT_ERR__INVALID_INDEX; + return CIOT__ERR__INVALID_INDEX; #define CIOT_ERR_STATE_CHECK(current, expected) \ if (current != expected) \ { \ - CIOT_ERR_LOG("ciot_err", CIOT_ERR__INVALID_STATE); \ - return CIOT_ERR__INVALID_STATE; \ + CIOT_ERR_LOG("ciot_err", CIOT__ERR__INVALID_STATE); \ + return CIOT__ERR__INVALID_STATE; \ } #define CIOT_ERR_VALUE_CHECK(current, expected, err) \ @@ -49,15 +49,15 @@ typedef CiotErr ciot_err_t; #define CIOT_ERR_EMPTY_STRING_CHECK(x) \ if (x[0] == '\0') \ { \ - /*CIOT_ERR_LOG("ciot_err", CIOT_ERR__INVALID_ARG);*/ \ - return CIOT_ERR__INVALID_ARG; \ + /*CIOT_ERR_LOG("ciot_err", CIOT__ERR__INVALID_ARG);*/ \ + return CIOT__ERR__INVALID_ARG; \ } #define CIOT_ERR_RETURN(x) \ do \ { \ ciot_err_t ciot_err = x; \ - if (ciot_err != CIOT_ERR__OK) \ + if (ciot_err != CIOT__ERR__OK) \ { \ CIOT_ERR_LOG("ciot_err", ciot_err); \ return ciot_err; \ @@ -68,7 +68,7 @@ typedef CiotErr ciot_err_t; do \ { \ ciot_err_t ciot_err = x; \ - if (ciot_err != CIOT_ERR__OK) \ + if (ciot_err != CIOT__ERR__OK) \ { \ CIOT_ERR_LOG(TAG, ciot_err); \ } \ @@ -76,4 +76,4 @@ typedef CiotErr ciot_err_t; const char *ciot_err_to_message(ciot_err_t err); -#endif //!__CIOT_ERR__H__ +#endif //!__CIOT__ERR__H__ diff --git a/include/ciot_storage_auto.h b/include/ciot_storage_auto.h new file mode 100644 index 00000000..1292ec01 --- /dev/null +++ b/include/ciot_storage_auto.h @@ -0,0 +1,19 @@ +/** + * @file ciot_storage_auto.h + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-10-17 + * + * @copyright Copyright (c) 2024 + * + */ + +#ifndef __CIOT_STORAGE_AUTO__H__ +#define __CIOT_STORAGE_AUTO__H__ + +#include "ciot_storage.h" + +ciot_storage_t ciot_storage_auto_new(void); + +#endif //!__CIOT_STORAGE_AUTO__H__ \ No newline at end of file diff --git a/include/ciot_storage_fat.h b/include/ciot_storage_fat.h new file mode 100644 index 00000000..fa74a1a3 --- /dev/null +++ b/include/ciot_storage_fat.h @@ -0,0 +1,19 @@ +/** + * @file ciot_storage_fat.h + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-07-04 + * + * @copyright Copyright (c) 2024 + * + */ + +#ifndef __CIOT_STORAGE_FAT__H__ +#define __CIOT_STORAGE_FAT__H__ + +#include "ciot_storage.h" + +ciot_storage_t ciot_storage_fat_new(void); + +#endif //!__CIOT_STORAGE_FAT__H__ diff --git a/include/ciot_storage_fs.h b/include/ciot_storage_fs.h index 011b670a..52ec416c 100644 --- a/include/ciot_storage_fs.h +++ b/include/ciot_storage_fs.h @@ -15,5 +15,8 @@ #include "ciot_storage.h" ciot_storage_t ciot_storage_fs_new(void); +ciot_err_t ciot_storage_fs_delete(char *path); +ciot_err_t ciot_storage_fs_write_bytes(char *path, uint8_t *bytes, int size); +ciot_err_t ciot_storage_fs_read_bytes(char *path, uint8_t *bytes, int *size); #endif //!__CIOT_STORAGE_FS__H__ diff --git a/include/ciot_storage_spiffs.h b/include/ciot_storage_spiffs.h new file mode 100644 index 00000000..19112e78 --- /dev/null +++ b/include/ciot_storage_spiffs.h @@ -0,0 +1,19 @@ +/** + * @file ciot_storage_spiffs.h + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-07-04 + * + * @copyright Copyright (c) 2024 + * + */ + +#ifndef __CIOT_STORAGE_SPIFFS__H__ +#define __CIOT_STORAGE_SPIFFS__H__ + +#include "ciot_storage.h" + +ciot_storage_t ciot_storage_spiffs_new(void); + +#endif //!__CIOT_STORAGE_SPIFFS__H__ diff --git a/proto b/proto index 76fb2c98..09276f2a 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit 76fb2c98fbadc6ac228a73fe6af9f7cf1e35ef3e +Subproject commit 09276f2a167e5c62a52c38152360692253179a2a diff --git a/src/common/ciot_base.c b/src/common/ciot_base.c index 9be6c176..7c7f2f18 100644 --- a/src/common/ciot_base.c +++ b/src/common/ciot_base.c @@ -39,7 +39,7 @@ ciot_err_t ciot_init(ciot_t self) self->info.version.data = ver; self->info.version.len = sizeof(ver); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -58,7 +58,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -88,12 +88,12 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.ciot = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_process_req(ciot_t self, ciot_msg_t *msg) @@ -114,11 +114,11 @@ ciot_err_t ciot_process_req(ciot_t self, ciot_msg_t *msg) self->iface.req_status.state = CIOT_IFACE_REQ_STATE_IDLE; return ciot_delete_cfg(self, req->iface); case CIOT__CIOT_REQ_TYPE__CIOT_REQ_TYPE_PROXY_MSG: - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; default: - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } ciot_err_t ciot_get_cfg(ciot_t self, ciot_cfg_t *cfg) @@ -126,7 +126,7 @@ ciot_err_t ciot_get_cfg(ciot_t self, ciot_cfg_t *cfg) CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); *cfg = self->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_get_status(ciot_t self, ciot_status_t *status) @@ -134,7 +134,7 @@ ciot_err_t ciot_get_status(ciot_t self, ciot_status_t *status) CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(status); *status = self->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_get_info(ciot_t self, ciot_info_t *info) @@ -142,14 +142,14 @@ ciot_err_t ciot_get_info(ciot_t self, ciot_info_t *info) CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(info); *info = self->info; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_delete_cfg(ciot_t self, ciot_iface_info_t *iface) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(self->storage); - CIOT_ERR_VALUE_CHECK(iface->type, self->ifaces.list[iface->id]->info.type, CIOT_ERR__INVALID_TYPE); + CIOT_ERR_VALUE_CHECK(iface->type, self->ifaces.list[iface->id]->info.type, CIOT__ERR__INVALID_TYPE); char filename[16]; sprintf(filename, CIOT_IFACE_CFG_FILENAME, (int)iface->id); return self->storage->delete(filename); @@ -175,7 +175,7 @@ ciot_err_t ciot_create_cfg(ciot_t self, ciot_iface_info_t *iface, ciot_msg_data_ { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(self->storage); - CIOT_ERR_VALUE_CHECK(iface->type, self->ifaces.list[iface->id]->info.type, CIOT_ERR__INVALID_TYPE); + CIOT_ERR_VALUE_CHECK(iface->type, self->ifaces.list[iface->id]->info.type, CIOT__ERR__INVALID_TYPE); char filename[16]; sprintf(filename, CIOT_IFACE_CFG_FILENAME, (int)iface->id); cfg->ciot = NULL; @@ -223,11 +223,18 @@ ciot_err_t ciot_get_ifaces_info(ciot_t self, ProtobufCBinaryData *ifaces_info) for (size_t i = 0; i < self->ifaces.count; i++) { - iface_types[i] = self->ifaces.list[i]->info.type; + if(self->ifaces.list[i] != NULL) + { + iface_types[i] = self->ifaces.list[i]->info.type; + } + else + { + iface_types[i] = 255; + } } ifaces_info->data = iface_types; ifaces_info->len = self->ifaces.count; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } \ No newline at end of file diff --git a/src/common/ciot_ble_adv_base.c b/src/common/ciot_ble_adv_base.c index 5790648d..5d178756 100644 --- a/src/common/ciot_ble_adv_base.c +++ b/src/common/ciot_ble_adv_base.c @@ -34,7 +34,7 @@ ciot_err_t ciot_ble_adv_init(ciot_ble_adv_t self) base->iface.send_data = ciot_iface_send_data; base->iface.info.type = CIOT__IFACE_TYPE__IFACE_TYPE_BLE_ADV; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -53,7 +53,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -81,7 +81,7 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.ble_adv = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) @@ -92,7 +92,7 @@ static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int s ciot_err_t ciot_ble_adv_process_req(ciot_ble_adv_t self, ciot_ble_adv_req_t *req) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_ble_adv_get_cfg(ciot_ble_adv_t self, ciot_ble_adv_cfg_t *cfg) @@ -101,7 +101,7 @@ ciot_err_t ciot_ble_adv_get_cfg(ciot_ble_adv_t self, ciot_ble_adv_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_ble_adv_base_t *base = (ciot_ble_adv_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ble_adv_get_status(ciot_ble_adv_t self, ciot_ble_adv_status_t *status) @@ -110,5 +110,5 @@ ciot_err_t ciot_ble_adv_get_status(ciot_ble_adv_t self, ciot_ble_adv_status_t *s CIOT_ERR_NULL_CHECK(status); ciot_ble_adv_base_t *base = (ciot_ble_adv_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_ble_base.c b/src/common/ciot_ble_base.c index 8540aaa9..29f90af3 100644 --- a/src/common/ciot_ble_base.c +++ b/src/common/ciot_ble_base.c @@ -41,7 +41,7 @@ ciot_err_t ciot_ble_init(ciot_ble_t self) base->info.hw_mac.data = base->macs.hw; base->info.hw_mac.len = sizeof(base->macs.hw); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -60,7 +60,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -90,12 +90,12 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.ble = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_ble_process_req(ciot_ble_t self, ciot_ble_req_t *req) @@ -111,11 +111,11 @@ ciot_err_t ciot_ble_process_req(ciot_ble_t self, ciot_ble_req_t *req) { base->iface.req_status.state = CIOT_IFACE_REQ_STATE_IDLE; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; default: break; } - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } ciot_err_t ciot_ble_get_cfg(ciot_ble_t self, ciot_ble_cfg_t *cfg) @@ -124,7 +124,7 @@ ciot_err_t ciot_ble_get_cfg(ciot_ble_t self, ciot_ble_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_ble_base_t *base = (ciot_ble_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ble_get_status(ciot_ble_t self, ciot_ble_status_t *status) @@ -133,7 +133,7 @@ ciot_err_t ciot_ble_get_status(ciot_ble_t self, ciot_ble_status_t *status) CIOT_ERR_NULL_CHECK(status); ciot_ble_base_t *base = (ciot_ble_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ble_get_info(ciot_ble_t self, ciot_ble_info_t *info) @@ -142,7 +142,7 @@ ciot_err_t ciot_ble_get_info(ciot_ble_t self, ciot_ble_info_t *info) CIOT_ERR_NULL_CHECK(info); ciot_ble_base_t *base = (ciot_ble_base_t*)self; *info = base->info; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } bool ciot_ble_mac_is_valid(uint8_t mac[6]) @@ -163,5 +163,5 @@ ciot_err_t ciot_ble_set_ifaces(ciot_ble_t self, ciot_ble_ifaces_t *ifaces) CIOT_ERR_NULL_CHECK(ifaces); ciot_ble_base_t *base = (ciot_ble_base_t*)self; base->ifaces = *ifaces; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } \ No newline at end of file diff --git a/src/common/ciot_ble_scn_base.c b/src/common/ciot_ble_scn_base.c index 1e5bb23e..f69f9f0e 100644 --- a/src/common/ciot_ble_scn_base.c +++ b/src/common/ciot_ble_scn_base.c @@ -46,7 +46,7 @@ ciot_err_t ciot_ble_scn_init(ciot_ble_scn_t self) ciot_ble_scn_base_init_fifo(&base->adv_fifo); #endif - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -65,7 +65,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -93,19 +93,19 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.ble_scn = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_ble_scn_process_req(ciot_ble_scn_t self, ciot_ble_scn_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_ble_scn_get_cfg(ciot_ble_scn_t self, ciot_ble_scn_cfg_t *cfg) @@ -114,7 +114,7 @@ ciot_err_t ciot_ble_scn_get_cfg(ciot_ble_scn_t self, ciot_ble_scn_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_ble_scn_base_t *base = (ciot_ble_scn_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ble_scn_get_status(ciot_ble_scn_t self, ciot_ble_scn_status_t *status) @@ -123,7 +123,7 @@ ciot_err_t ciot_ble_scn_get_status(ciot_ble_scn_t self, ciot_ble_scn_status_t *s CIOT_ERR_NULL_CHECK(status); ciot_ble_scn_base_t *base = (ciot_ble_scn_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ble_scn_base_task(ciot_ble_scn_t self) @@ -150,7 +150,7 @@ ciot_err_t ciot_ble_scn_base_task(ciot_ble_scn_t self) } } #endif - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ble_scn_set_filter(ciot_ble_scn_t self, ciot_ble_scn_filter_fn *filter, void *args) @@ -160,7 +160,7 @@ ciot_err_t ciot_ble_scn_set_filter(ciot_ble_scn_t self, ciot_ble_scn_filter_fn * ciot_ble_scn_base_t *base = (ciot_ble_scn_base_t*)self; base->filter.handler = filter; base->filter.args = args; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } #if CIOT_CONFIG_BLE_SCN_ADV_FIFO_SIZE @@ -174,6 +174,6 @@ static ciot_err_t ciot_ble_scn_base_init_fifo(ciot_ble_scn_adv_fifo_t *adv_fifo) adv_fifo->list[i].info->mac.data = adv_fifo->data.macs[i]; adv_fifo->list[i].payload.data = adv_fifo->data.advs[i]; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } #endif \ No newline at end of file diff --git a/src/common/ciot_crypt.c b/src/common/ciot_crypt.c index 5973b1b9..cc32efdb 100644 --- a/src/common/ciot_crypt.c +++ b/src/common/ciot_crypt.c @@ -35,8 +35,8 @@ ciot_err_t ciot_crypt_enc(ciot_crypt_t *crypt, char *data, char *out, int size) int err = 0; size_t len = strlen(data); size_t block_size = 16 * ((len / 16) + 1); - unsigned char pad[block_size]; - unsigned char buf[block_size]; + char pad[block_size]; + char buf[block_size]; memset(pad, 0, block_size); memset(buf, 0, block_size); @@ -56,13 +56,13 @@ ciot_err_t ciot_crypt_enc(ciot_crypt_t *crypt, char *data, char *out, int size) } } - err = mbedtls_base64_encode(out, size, &len, buf, block_size); + err = mbedtls_base64_encode((unsigned char*)out, size, &len, (unsigned char*)buf, block_size); if(err) { - return CIOT_ERR__INVALID_SIZE; + return CIOT__ERR__INVALID_SIZE; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_crypt_dec(ciot_crypt_t *crypt, char *data, char *out, int size) @@ -74,18 +74,18 @@ ciot_err_t ciot_crypt_dec(ciot_crypt_t *crypt, char *data, char *out, int size) int err = 0; size_t len = strlen(data); size_t decoded_size = 0; - unsigned char buf[len]; + char buf[len]; memset(buf, 0, len); - err = mbedtls_base64_decode(buf, len, &decoded_size, data, len); + err = mbedtls_base64_decode((unsigned char*)buf, len, &decoded_size, (unsigned char*)data, len); if(err) { - return CIOT_ERR__INVALID_SIZE; + return CIOT__ERR__INVALID_SIZE; } if(decoded_size >= size) { - return CIOT_ERR__INVALID_SIZE; + return CIOT__ERR__INVALID_SIZE; } for (size_t i = 0; i < decoded_size; i+=16) @@ -99,7 +99,7 @@ ciot_err_t ciot_crypt_dec(ciot_crypt_t *crypt, char *data, char *out, int size) int decrypted_size = decoded_size - out[decoded_size - 1]; out[decrypted_size] = '\0'; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_crypt_enc_word(char *data, ciot_crypt_key_t *key, char *out) @@ -112,7 +112,7 @@ static ciot_err_t ciot_crypt_enc_word(char *data, ciot_crypt_key_t *key, char *o { CIOT_LOGE(TAG, "setkey enc mbedtls error: %d", err); mbedtls_aes_free(&aes); - return CIOT_ERR__INVALID_KEY_SIZE; + return CIOT__ERR__INVALID_KEY_SIZE; } err = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, (const unsigned char *)data, (unsigned char *)out); @@ -120,12 +120,12 @@ static ciot_err_t ciot_crypt_enc_word(char *data, ciot_crypt_key_t *key, char *o { CIOT_LOGE(TAG, "encrypt mbedtls error: %d", err); mbedtls_aes_free(&aes); - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } mbedtls_aes_free(&aes); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_crypt_dec_word(char *data, ciot_crypt_key_t *key, char *out) @@ -138,7 +138,7 @@ static ciot_err_t ciot_crypt_dec_word(char *data, ciot_crypt_key_t *key, char *o { CIOT_LOGE(TAG, "setkey dec mbedtls error: %d", err); mbedtls_aes_free(&aes); - return CIOT_ERR__INVALID_KEY_SIZE; + return CIOT__ERR__INVALID_KEY_SIZE; } err = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, (const unsigned char*)data, (unsigned char *)out); @@ -146,23 +146,23 @@ static ciot_err_t ciot_crypt_dec_word(char *data, ciot_crypt_key_t *key, char *o { CIOT_LOGE(TAG, "crypt ecb mbedtls error: %d", err); mbedtls_aes_free(&aes); - return CIOT_ERR__INVALID_KEY_SIZE; + return CIOT__ERR__INVALID_KEY_SIZE; } mbedtls_aes_free(&aes); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } #else ciot_err_t ciot_crypt_enc(ciot_crypt_t *crypt, char *data, char *out, int size) { - return CIOT_ERR__DISABLED; + return CIOT__ERR__DISABLED; } ciot_err_t ciot_crypt_dec(ciot_crypt_t *crypt, char *data, char *out, int size) { - return CIOT_ERR__DISABLED; + return CIOT__ERR__DISABLED; } #endif diff --git a/src/common/ciot_decoder_s.c b/src/common/ciot_decoder_s.c index 78e93434..6480cfb3 100644 --- a/src/common/ciot_decoder_s.c +++ b/src/common/ciot_decoder_s.c @@ -72,7 +72,7 @@ static ciot_err_t ciot_decoder_s_decode(ciot_iface_t *iface, uint8_t byte) self->idx = 0; self->state = CIOT_DECODER_S_STATE_WAIT_START_CH; base->state = CIOT_IFACE_DECODER_STATE_ERROR; - return CIOT_ERR__OVERFLOW; + return CIOT__ERR__OVERFLOW; } switch (self->state) @@ -110,7 +110,7 @@ static ciot_err_t ciot_decoder_s_decode(ciot_iface_t *iface, uint8_t byte) self->idx = 0; self->state = CIOT_DECODER_S_STATE_WAIT_START_CH; base->state = CIOT_IFACE_DECODER_STATE_ERROR; - return CIOT_ERR__PROTOCOL_VIOLATION; + return CIOT__ERR__PROTOCOL_VIOLATION; } default: break; @@ -118,7 +118,7 @@ static ciot_err_t ciot_decoder_s_decode(ciot_iface_t *iface, uint8_t byte) base->state = CIOT_IFACE_DECODER_STATE_DECODING; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_decoder_s_send(ciot_iface_t *iface, uint8_t *data, int size) @@ -129,5 +129,5 @@ static ciot_err_t ciot_decoder_s_send(ciot_iface_t *iface, uint8_t *data, int si iface->send_data(iface, (uint8_t*)&size, 2); iface->send_data(iface, data, size); iface->send_data(iface, &end_ch, 1); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_decoder_slip.c b/src/common/ciot_decoder_slip.c index a173aa07..b1e5bdbf 100644 --- a/src/common/ciot_decoder_slip.c +++ b/src/common/ciot_decoder_slip.c @@ -75,7 +75,7 @@ static ciot_err_t ciot_decoder_slip_decode(ciot_iface_t *iface, uint8_t byte) CIOT_LOGE(TAG, "Overflow"); self->idx = 0; base->state = CIOT_IFACE_DECODER_STATE_ERROR; - return CIOT_ERR__OVERFLOW; + return CIOT__ERR__OVERFLOW; } switch (self->state) @@ -115,7 +115,7 @@ static ciot_err_t ciot_decoder_slip_decode(ciot_iface_t *iface, uint8_t byte) default: self->state = CIOT_DECODER_SLIP_STATE_CLEARING_INVALID_PACKET; base->state = CIOT_IFACE_DECODER_STATE_ERROR; - return CIOT_ERR__PROTOCOL_VIOLATION; + return CIOT__ERR__PROTOCOL_VIOLATION; } break; case CIOT_DECODER_SLIP_STATE_CLEARING_INVALID_PACKET: @@ -131,7 +131,7 @@ static ciot_err_t ciot_decoder_slip_decode(ciot_iface_t *iface, uint8_t byte) base->state = CIOT_IFACE_DECODER_STATE_DECODING; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_decoder_slip_send(ciot_iface_t *iface, uint8_t *data, int size) @@ -159,5 +159,5 @@ static ciot_err_t ciot_decoder_slip_send(ciot_iface_t *iface, uint8_t *data, int iface->send_data(iface, &slip_byte_end, 1); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_dfu_nrf.c b/src/common/ciot_dfu_nrf.c index 77d6e97b..9ef62efd 100644 --- a/src/common/ciot_dfu_nrf.c +++ b/src/common/ciot_dfu_nrf.c @@ -88,7 +88,7 @@ ciot_err_t ciot_dfu_nrf_start(ciot_dfu_nrf_t self, ciot_dfu_cfg_t *cfg) if (cfg->type != self->cfg.dfu.type) { - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } self->state = CIOT_DFU_NRF_STATE_IDLE; @@ -120,7 +120,7 @@ ciot_err_t ciot_dfu_nrf_stop(ciot_dfu_nrf_t self) }; self->cfg.iface->process_req(self->cfg.iface, &msg); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_dfu_nrf_init(ciot_dfu_nrf_t self) @@ -138,7 +138,7 @@ ciot_err_t ciot_dfu_nrf_init(ciot_dfu_nrf_t self) base->iface.send_data = ciot_iface_send_data; base->iface.info.type = CIOT__IFACE_TYPE__IFACE_TYPE_DFU; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -157,7 +157,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -185,7 +185,7 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.dfu = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) @@ -201,7 +201,7 @@ ciot_err_t ciot_dfu_nrf_process_req(ciot_dfu_nrf_t self, ciot_dfu_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_dfu_nrf_get_cfg(ciot_dfu_nrf_t self, ciot_dfu_cfg_t *cfg) @@ -210,7 +210,7 @@ ciot_err_t ciot_dfu_nrf_get_cfg(ciot_dfu_nrf_t self, ciot_dfu_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_dfu_t *base = (ciot_dfu_t *)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_dfu_nrf_get_status(ciot_dfu_nrf_t self, ciot_dfu_status_t *status) @@ -219,7 +219,7 @@ ciot_err_t ciot_dfu_nrf_get_status(ciot_dfu_nrf_t self, ciot_dfu_status_t *statu CIOT_ERR_NULL_CHECK(status); ciot_dfu_t *base = (ciot_dfu_t *)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } // ciot_err_t ciot_dfu_nrf_get_info(ciot_dfu_nrf_t self, ciot_dfu_info_t *info) @@ -228,7 +228,7 @@ ciot_err_t ciot_dfu_nrf_get_status(ciot_dfu_nrf_t self, ciot_dfu_status_t *statu // CIOT_ERR_NULL_CHECK(info); // ciot_dfu_t *base = (ciot_dfu_t*)self; // *info = base->info; -// return CIOT_ERR__OK; +// return CIOT__ERR__OK; // } ciot_err_t ciot_dfu_nrf_task(ciot_dfu_nrf_t self) @@ -237,11 +237,11 @@ ciot_err_t ciot_dfu_nrf_task(ciot_dfu_nrf_t self) if (self->state == CIOT_DFU_NRF_STATE_IDLE || self->state == CIOT_DFU_NRF_STATE_COMPLETED) { - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t err = ciot_dfu_nrf_write(self); - if (err != CIOT_ERR__OK) + if (err != CIOT__ERR__OK) { self->base.status.error = err; self->state = CIOT_DFU_NRF_STATE_ERROR; @@ -253,7 +253,7 @@ ciot_err_t ciot_dfu_nrf_task(ciot_dfu_nrf_t self) if(ciot_timer_compare(&self->timer, 5)) { CIOT_LOGE(TAG, "TIMEOUT"); - err = CIOT_ERR__TIMEOUT; + err = CIOT__ERR__TIMEOUT; self->base.status.error = err; self->state = CIOT_DFU_NRF_STATE_ERROR; ciot_dfu_nrf_set_state(self, CIOT__DFU_STATE__DFU_STATE_ERROR); @@ -275,13 +275,13 @@ ciot_err_t ciot_dfu_nrf_read_file(ciot_dfu_nrf_packet_t *object, const char *nam if (file == NULL) { CIOT_LOGE(TAG, "Error opening file %s", name); - return CIOT_ERR__NOT_FOUND; + return CIOT__ERR__NOT_FOUND; } if (object == NULL || object->data != NULL) { CIOT_LOGE(TAG, "Invalid arg"); - return CIOT_ERR__INVALID_ARG; + return CIOT__ERR__INVALID_ARG; } fseek(file, 0, SEEK_END); @@ -293,14 +293,14 @@ ciot_err_t ciot_dfu_nrf_read_file(ciot_dfu_nrf_packet_t *object, const char *nam if (object->data == NULL) { CIOT_LOGE(TAG, "Error allocating memory"); - return CIOT_ERR__NO_MEMORY; + return CIOT__ERR__NO_MEMORY; } size_t elements_read = fread(object->data, sizeof(uint8_t), file_size, file); if (elements_read != file_size) { CIOT_LOGE(TAG, "Error reading file"); - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } return 0; @@ -318,7 +318,7 @@ ciot_err_t ciot_dfu_nrf_start_bootloader(ciot_dfu_nrf_t self, ciot_iface_t *ifac msg->data->sys = &sys_data; ciot_err_t err = ciot_iface_send_req(iface, msg); - if (err != CIOT_ERR__OK) + if (err != CIOT__ERR__OK) { self->base.status.error = err; self->state = CIOT_DFU_NRF_STATE_ERROR; @@ -386,7 +386,7 @@ static ciot_err_t ciot_dfu_nrf_write(ciot_dfu_nrf_t self) } ciot_err_t err = ciot_iface_send_data(&base->iface, &self->object.packet->data[self->data_transferred], bytes_to_write); - if (err != CIOT_ERR__OK) + if (err != CIOT__ERR__OK) { CIOT_LOGE(TAG, "Error sending data (0x%x)", err); self->base.status.error = err; @@ -426,7 +426,7 @@ static ciot_err_t ciot_dfu_nrf_write(ciot_dfu_nrf_t self) return ciot_iface_send_data(&base->iface, self->data, 1); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_dfu_nrf_process_data(ciot_dfu_nrf_t self, uint8_t *data, int len) @@ -452,7 +452,7 @@ static ciot_err_t ciot_dfu_nrf_process_data(ciot_dfu_nrf_t self, uint8_t *data, if (data[0] != CIOT_DFU_NRF_OP_RESPONSE) { CIOT_LOGE(TAG, "Invalid msg"); - return CIOT_ERR__VALIDATION_FAILED; + return CIOT__ERR__VALIDATION_FAILED; } // Ping Response Success [x60 x09 x01] or Opcode not supported [x60 x09 x03] @@ -464,7 +464,7 @@ static ciot_err_t ciot_dfu_nrf_process_data(ciot_dfu_nrf_t self, uint8_t *data, CIOT_LOGI(TAG, "Ping response sucess"); self->state = CIOT_DFU_NRF_STATE_CREATE_OBJECT; ciot_dfu_nrf_set_state(self, CIOT__DFU_STATE__DFU_STATE_IN_PROGRESS); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } if (self->state == CIOT_DFU_NRF_STATE_WAITING_PING_RESP && @@ -475,7 +475,7 @@ static ciot_err_t ciot_dfu_nrf_process_data(ciot_dfu_nrf_t self, uint8_t *data, CIOT_LOGI(TAG, "First ping refused. Trying again..."); self->state = CIOT_DFU_NRF_STATE_SEND_PING; self->ping_refused = true; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } // Response Create Success [x60 x01 x01] @@ -486,7 +486,7 @@ static ciot_err_t ciot_dfu_nrf_process_data(ciot_dfu_nrf_t self, uint8_t *data, { CIOT_LOGI(TAG, "Response create success"); self->state = CIOT_DFU_NRF_STATE_WRITE_DFU_PACKAGE; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } // Response Calculate CRC Sucess: offset CRC32 [x60 x03 x01 XXXXXXXX XXXXXXXX] @@ -506,7 +506,7 @@ static ciot_err_t ciot_dfu_nrf_process_data(ciot_dfu_nrf_t self, uint8_t *data, CIOT_LOGI(TAG, "CRC error: expected = %lu, received = %lu", self->crc.expected, self->crc.received); self->state = CIOT_DFU_NRF_STATE_ERROR; ciot_dfu_nrf_set_state(self, CIOT__DFU_STATE__DFU_STATE_ERROR); - return CIOT_ERR__CHECKSUM; + return CIOT__ERR__CHECKSUM; } if (self->object.remaining == 0) { @@ -518,7 +518,7 @@ static ciot_err_t ciot_dfu_nrf_process_data(ciot_dfu_nrf_t self, uint8_t *data, CIOT_LOGI(TAG, "Response PRN Sucess: offset CRC32"); self->state = CIOT_DFU_NRF_STATE_WRITE_DFU_PACKAGE; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } // Response Execute Sucess [x60 x04 x01] @@ -537,7 +537,7 @@ static ciot_err_t ciot_dfu_nrf_process_data(ciot_dfu_nrf_t self, uint8_t *data, self->data_transferred = 0; self->crc.expected = 0; self->state = CIOT_DFU_NRF_STATE_CREATE_OBJECT; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } if (self->object.packet->type == CIOT_DFU_NRF_PACKET_TYPE_APP_IMAGE) { @@ -545,21 +545,21 @@ static ciot_err_t ciot_dfu_nrf_process_data(ciot_dfu_nrf_t self, uint8_t *data, self->state = CIOT_DFU_NRF_STATE_COMPLETED; ciot_dfu_nrf_set_state(self, CIOT__DFU_STATE__DFU_STATE_COMPLETED); ciot_dfu_nrf_stop(self); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } } else { CIOT_LOGI(TAG, "Object write completed"); self->state = CIOT_DFU_NRF_STATE_CREATE_OBJECT; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } } // DFU completed... nothing to do... if (self->state == CIOT_DFU_NRF_STATE_COMPLETED) { - return CIOT_ERR__OK; + return CIOT__ERR__OK; } if (data[2] != CIOT_DFU_NRF_RES_CODE_SUCCESS) @@ -577,10 +577,10 @@ static ciot_err_t ciot_dfu_nrf_process_data(ciot_dfu_nrf_t self, uint8_t *data, } self->state = CIOT_DFU_NRF_STATE_ERROR; - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_dfu_nrf_set_state(ciot_dfu_nrf_t self, ciot_dfu_state_t state) @@ -599,7 +599,7 @@ static ciot_err_t ciot_dfu_nrf_set_state(ciot_dfu_nrf_t self, ciot_dfu_state_t s iface_event.msg = msg; return base->iface.event_handler(&base->iface, &iface_event, base->iface.event_args); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_dfu_nrf_event_handler(ciot_iface_t *sender, ciot_iface_event_t *event, void *args) @@ -618,7 +618,7 @@ static ciot_err_t ciot_dfu_nrf_event_handler(ciot_iface_t *sender, ciot_iface_ev if (event->type == CIOT_IFACE_EVENT_DATA) { ciot_err_t err = ciot_dfu_nrf_process_data(self, event->data, event->size); - if (err != CIOT_ERR__OK) + if (err != CIOT__ERR__OK) { self->base.status.error = err; self->state = CIOT_DFU_NRF_STATE_ERROR; @@ -626,5 +626,5 @@ static ciot_err_t ciot_dfu_nrf_event_handler(ciot_iface_t *sender, ciot_iface_ev } } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_eth_base.c b/src/common/ciot_eth_base.c index 6cb2b621..6f91c66e 100644 --- a/src/common/ciot_eth_base.c +++ b/src/common/ciot_eth_base.c @@ -25,12 +25,15 @@ ciot_err_t ciot_eth_init(ciot_eth_t self) { ciot_eth_base_t *base = (ciot_eth_base_t*)self; + ciot_iface_init(&base->iface); + + base->iface.ptr = self; base->iface.process_req = ciot_iface_process_req; base->iface.get_data = ciot_iface_get_data; base->iface.send_data = ciot_iface_send_data; base->iface.info.type = CIOT__IFACE_TYPE__IFACE_TYPE_ETH; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -49,7 +52,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -80,19 +83,19 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.tcp = &base->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_eth_process_req(ciot_eth_t self, ciot_tcp_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_eth_get_cfg(ciot_eth_t self, ciot_tcp_cfg_t *cfg) @@ -126,5 +129,5 @@ ciot_err_t ciot_eth_get_mac(ciot_eth_t self, uint8_t mac[6]) ciot_eth_base_t *base = (ciot_eth_base_t*)self; ciot_tcp_base_t *tcp = (ciot_tcp_base_t*)base->tcp; memcpy(mac, tcp->info.mac.data, 6); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_gpio_base.c b/src/common/ciot_gpio_base.c index 14866f21..45ab60ff 100644 --- a/src/common/ciot_gpio_base.c +++ b/src/common/ciot_gpio_base.c @@ -53,7 +53,7 @@ ciot_err_t ciot_gpio_init(ciot_gpio_t self, int count) base->cfg.pull.len = count; base->status.states.len = count; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_gpio_task(ciot_gpio_t self) @@ -99,7 +99,7 @@ ciot_err_t ciot_gpio_task(ciot_gpio_t self) base->timer = 0; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -118,7 +118,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -144,32 +144,32 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.gpio = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_gpio_process_req(ciot_gpio_t self, ciot_gpio_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_gpio_set_cfg(ciot_gpio_t self, ciot_gpio_cfg_t *cfg) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); - CIOT_ERR_VALUE_CHECK(cfg->pins.len, cfg->pins.len, CIOT_ERR__INVALID_ARG); + CIOT_ERR_VALUE_CHECK(cfg->pins.len, cfg->pins.len, CIOT__ERR__INVALID_ARG); ciot_gpio_base_t *base = (ciot_gpio_base_t*)self; memcpy(base->cfg.pins.data, cfg->pins.data, cfg->pins.len); memcpy(base->cfg.modes.data, cfg->modes.data, cfg->pins.len); memcpy(base->cfg.pull.data, cfg->pull.data, cfg->pins.len); base->cfg.blink_interval = cfg->blink_interval; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_gpio_get_cfg(ciot_gpio_t self, ciot_gpio_cfg_t *cfg) @@ -178,7 +178,7 @@ ciot_err_t ciot_gpio_get_cfg(ciot_gpio_t self, ciot_gpio_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_gpio_base_t *base = (ciot_gpio_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_gpio_get_status(ciot_gpio_t self, ciot_gpio_status_t *status) @@ -187,7 +187,7 @@ ciot_err_t ciot_gpio_get_status(ciot_gpio_t self, ciot_gpio_status_t *status) CIOT_ERR_NULL_CHECK(status); ciot_gpio_base_t *base = (ciot_gpio_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_gpio_set_state(ciot_gpio_t self, uint16_t id, ciot_gpio_state_t state) @@ -214,7 +214,7 @@ ciot_err_t ciot_gpio_set_state(ciot_gpio_t self, uint16_t id, ciot_gpio_state_t base->status.states.data[id] = state; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_gpio_state_t ciot_gpio_get_state(ciot_gpio_t self, uint16_t id) diff --git a/src/common/ciot_http_client_base.c b/src/common/ciot_http_client_base.c index 180bcd7d..e5a073f5 100644 --- a/src/common/ciot_http_client_base.c +++ b/src/common/ciot_http_client_base.c @@ -34,7 +34,7 @@ ciot_err_t ciot_http_client_init(ciot_http_client_t self) base->iface.send_data = ciot_iface_send_data; base->iface.info.type = CIOT__IFACE_TYPE__IFACE_TYPE_HTTP_CLIENT; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -53,7 +53,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -81,7 +81,7 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.http_client = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) @@ -96,7 +96,7 @@ ciot_err_t ciot_http_client_process_req(ciot_http_client_t self, ciot_http_clien { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_http_client_get_cfg(ciot_http_client_t self, ciot_http_client_cfg_t *cfg) @@ -105,7 +105,7 @@ ciot_err_t ciot_http_client_get_cfg(ciot_http_client_t self, ciot_http_client_cf CIOT_ERR_NULL_CHECK(cfg); ciot_http_client_base_t *base = (ciot_http_client_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_http_client_get_status(ciot_http_client_t self, ciot_http_client_status_t *status) @@ -114,5 +114,5 @@ ciot_err_t ciot_http_client_get_status(ciot_http_client_t self, ciot_http_client CIOT_ERR_NULL_CHECK(status); ciot_http_client_base_t *base = (ciot_http_client_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_http_server_base.c b/src/common/ciot_http_server_base.c index 5c547f3c..186411e5 100644 --- a/src/common/ciot_http_server_base.c +++ b/src/common/ciot_http_server_base.c @@ -34,7 +34,7 @@ ciot_err_t ciot_http_server_init(ciot_http_server_t self) base->iface.send_data = ciot_iface_send_data; base->iface.info.type = CIOT__IFACE_TYPE__IFACE_TYPE_HTTP_SERVER; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -53,7 +53,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -81,7 +81,7 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.http_server = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) @@ -96,7 +96,7 @@ ciot_err_t ciot_http_server_process_req(ciot_http_server_t self, ciot_http_serve { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_http_server_get_cfg(ciot_http_server_t self, ciot_http_server_cfg_t *cfg) @@ -105,7 +105,7 @@ ciot_err_t ciot_http_server_get_cfg(ciot_http_server_t self, ciot_http_server_cf CIOT_ERR_NULL_CHECK(cfg); ciot_http_server_base_t *base = (ciot_http_server_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_http_server_get_status(ciot_http_server_t self, ciot_http_server_status_t *status) @@ -114,5 +114,5 @@ ciot_err_t ciot_http_server_get_status(ciot_http_server_t self, ciot_http_server CIOT_ERR_NULL_CHECK(status); ciot_http_server_base_t *base = (ciot_http_server_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_model_base.c b/src/common/ciot_model_base.c index 4d323332..f09bf1e7 100644 --- a/src/common/ciot_model_base.c +++ b/src/common/ciot_model_base.c @@ -35,7 +35,7 @@ ciot_err_t ciot_model_init(ciot_model_t self) base->iface.send_data = ciot_iface_send_data; base->iface.info.type = CIOT__IFACE_TYPE__IFACE_TYPE_CUSTOM; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -54,7 +54,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -84,19 +84,19 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.model = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_model_process_req(ciot_model_t self, ciot_model_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_model_get_cfg(ciot_model_t self, ciot_model_cfg_t *cfg) @@ -105,7 +105,7 @@ ciot_err_t ciot_model_get_cfg(ciot_model_t self, ciot_model_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_model_base_t *base = (ciot_model_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_model_get_status(ciot_model_t self, ciot_model_status_t *status) @@ -114,7 +114,7 @@ ciot_err_t ciot_model_get_status(ciot_model_t self, ciot_model_status_t *status) CIOT_ERR_NULL_CHECK(status); ciot_model_base_t *base = (ciot_model_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_model_get_info(ciot_model_t self, ciot_model_info_t *info) @@ -123,5 +123,5 @@ ciot_err_t ciot_model_get_info(ciot_model_t self, ciot_model_info_t *info) CIOT_ERR_NULL_CHECK(info); ciot_model_base_t *base = (ciot_model_base_t*)self; *info = base->info; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_mqtt_client_base.c b/src/common/ciot_mqtt_client_base.c index 8da0aa57..586c6c9e 100644 --- a/src/common/ciot_mqtt_client_base.c +++ b/src/common/ciot_mqtt_client_base.c @@ -49,7 +49,7 @@ ciot_err_t ciot_mqtt_client_init(ciot_mqtt_client_t self) base->cfg.topics->sub = base->topic_sub; base->topic_len = 0; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -68,7 +68,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -97,7 +97,7 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.mqtt_client = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) @@ -107,14 +107,14 @@ static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int s CIOT_ERR_NULL_CHECK(iface); CIOT_ERR_NULL_CHECK(data); ciot_mqtt_client_pub(self, base->cfg.topics->pub, data, size, base->cfg.qos); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_mqtt_client_process_req(ciot_mqtt_client_t self, ciot_mqtt_client_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_mqtt_client_get_cfg(ciot_mqtt_client_t self, ciot_mqtt_client_cfg_t *cfg) @@ -125,7 +125,7 @@ ciot_err_t ciot_mqtt_client_get_cfg(ciot_mqtt_client_t self, ciot_mqtt_client_cf CIOT_ERR_INDEX_CHECK(base->topic_len, 0, sizeof(base->topic_pub)); base->topic_pub[base->topic_len] = '\0'; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_mqtt_client_get_status(ciot_mqtt_client_t self, ciot_mqtt_client_status_t *status) @@ -134,7 +134,7 @@ ciot_err_t ciot_mqtt_client_get_status(ciot_mqtt_client_t self, ciot_mqtt_client CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(status); *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_mqtt_client_send(ciot_mqtt_client_t self, uint8_t *data, int size) @@ -160,7 +160,7 @@ ciot_err_t ciot_mqtt_client_set_subtopic(ciot_mqtt_client_t self, char *subtopic CIOT_ERR_NULL_CHECK(self); CIOT_ERR_INDEX_CHECK(base->topic_len, 0, sizeof(base->topic_pub)); memcpy(&base->topic_pub[base->topic_len], subtopic, len); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_mqtt_client_update_data_rate(ciot_mqtt_client_t self, int bytes_sended) @@ -176,5 +176,5 @@ ciot_err_t ciot_mqtt_client_update_data_rate(ciot_mqtt_client_t self, int bytes_ base->data_rate_aux = 0; } } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_ntp_base.c b/src/common/ciot_ntp_base.c index d1fec568..68b4be86 100644 --- a/src/common/ciot_ntp_base.c +++ b/src/common/ciot_ntp_base.c @@ -41,7 +41,7 @@ ciot_err_t ciot_ntp_init(ciot_ntp_t self) base->cfg.server2 = base->server2; base->cfg.server3 = base->server3; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -60,7 +60,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -90,19 +90,19 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.ntp = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_ntp_process_req(ciot_ntp_t self, ciot_ntp_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_ntp_get_cfg(ciot_ntp_t self, ciot_ntp_cfg_t *cfg) @@ -111,7 +111,7 @@ ciot_err_t ciot_ntp_get_cfg(ciot_ntp_t self, ciot_ntp_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_ntp_base_t *base = (ciot_ntp_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ntp_set_cfg(ciot_ntp_t self, ciot_ntp_cfg_t *cfg) @@ -127,7 +127,7 @@ ciot_err_t ciot_ntp_set_cfg(ciot_ntp_t self, ciot_ntp_cfg_t *cfg) ciot_strncpy(base->server1, cfg->server1, CIOT_CONFIG_NTP_SERVER_URL_LEN); ciot_strncpy(base->server2, cfg->server2, CIOT_CONFIG_NTP_SERVER_URL_LEN); ciot_strncpy(base->server3, cfg->server3, CIOT_CONFIG_NTP_SERVER_URL_LEN); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ntp_get_status(ciot_ntp_t self, ciot_ntp_status_t *status) @@ -136,7 +136,7 @@ ciot_err_t ciot_ntp_get_status(ciot_ntp_t self, ciot_ntp_status_t *status) CIOT_ERR_NULL_CHECK(status); ciot_ntp_base_t *base = (ciot_ntp_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } // ciot_err_t ciot_ntp_get_info(ciot_ntp_t self, ciot_ntp_info_t *info) @@ -145,5 +145,5 @@ ciot_err_t ciot_ntp_get_status(ciot_ntp_t self, ciot_ntp_status_t *status) // CIOT_ERR_NULL_CHECK(info); // ciot_ntp_base_t *base = (ciot_ntp_base_t*)self; // *info = base->info; -// return CIOT_ERR__OK; +// return CIOT__ERR__OK; // } diff --git a/src/common/ciot_ota_base.c b/src/common/ciot_ota_base.c index e9571830..c32572f3 100644 --- a/src/common/ciot_ota_base.c +++ b/src/common/ciot_ota_base.c @@ -37,7 +37,7 @@ ciot_err_t ciot_ota_init(ciot_ota_t self) base->cfg.url = base->url; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -56,7 +56,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -86,12 +86,12 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.ota = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_ota_process_req(ciot_ota_t self, ciot_ota_req_t *req) @@ -104,10 +104,10 @@ ciot_err_t ciot_ota_process_req(ciot_ota_t self, ciot_ota_req_t *req) case CIOT__OTA_REQ_TYPE__OTA_REQ_TYPE_ROLLBACK: return ciot_ota_rollback(self); default: - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ota_get_cfg(ciot_ota_t self, ciot_ota_cfg_t *cfg) @@ -116,7 +116,7 @@ ciot_err_t ciot_ota_get_cfg(ciot_ota_t self, ciot_ota_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_ota_base_t *base = (ciot_ota_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ota_get_status(ciot_ota_t self, ciot_ota_status_t *status) @@ -125,7 +125,7 @@ ciot_err_t ciot_ota_get_status(ciot_ota_t self, ciot_ota_status_t *status) CIOT_ERR_NULL_CHECK(status); ciot_ota_base_t *base = (ciot_ota_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } // ciot_err_t ciot_ota_get_info(ciot_ota_t self, ciot_ota_info_t *info) @@ -134,5 +134,5 @@ ciot_err_t ciot_ota_get_status(ciot_ota_t self, ciot_ota_status_t *status) // CIOT_ERR_NULL_CHECK(info); // ciot_ota_base_t *base = (ciot_ota_base_t*)self; // *info = base->info; -// return CIOT_ERR__OK; +// return CIOT__ERR__OK; // } diff --git a/src/common/ciot_socket_base.c b/src/common/ciot_socket_base.c index edefa8fa..94cd406d 100644 --- a/src/common/ciot_socket_base.c +++ b/src/common/ciot_socket_base.c @@ -35,7 +35,7 @@ ciot_err_t ciot_socket_init(ciot_socket_t self) base->iface.send_data = ciot_iface_send_data; base->iface.info.type = CIOT__IFACE_TYPE__IFACE_TYPE_SOCKET; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -54,7 +54,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -84,7 +84,7 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.socket = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) @@ -99,7 +99,7 @@ ciot_err_t ciot_socket_process_req(ciot_socket_t self, ciot_socket_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_socket_get_cfg(ciot_socket_t self, ciot_socket_cfg_t *cfg) @@ -108,7 +108,7 @@ ciot_err_t ciot_socket_get_cfg(ciot_socket_t self, ciot_socket_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_socket_base_t *base = (ciot_socket_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_socket_get_status(ciot_socket_t self, ciot_socket_status_t *status) @@ -117,7 +117,7 @@ ciot_err_t ciot_socket_get_status(ciot_socket_t self, ciot_socket_status_t *stat CIOT_ERR_NULL_CHECK(status); ciot_socket_base_t *base = (ciot_socket_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_socket_get_info(ciot_socket_t self, ciot_socket_info_t *info) @@ -126,5 +126,5 @@ ciot_err_t ciot_socket_get_info(ciot_socket_t self, ciot_socket_info_t *info) CIOT_ERR_NULL_CHECK(info); ciot_socket_base_t *base = (ciot_socket_base_t*)self; *info = base->info; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_storage.c b/src/common/ciot_storage.c index 9a37ad58..f7908d01 100644 --- a/src/common/ciot_storage.c +++ b/src/common/ciot_storage.c @@ -45,21 +45,33 @@ ciot_msg_data_t *ciot_storage_get_data(ciot_storage_t self, char *path) { if(self == NULL) return NULL; + CIOT_LOGI(TAG, "Get data from %s", path); + int size = 0; self->read_bytes(path, NULL, &size); - uint8_t buf[size]; - self->read_bytes(path, buf, &size); - - ciot__msg__free_unpacked(msg, NULL); - if(self->serializer) + if(size > 0) { - msg = self->serializer->from_bytes(buf, size); - } - else - { - msg = ciot__msg__unpack(NULL, size, buf); - } + uint8_t buf[size]; + self->read_bytes(path, buf, &size); + + ciot__msg__free_unpacked(msg, NULL); + if(self->serializer) + { + msg = self->serializer->from_bytes(buf, size); + } + else + { + msg = ciot__msg__unpack(NULL, size, buf); + } - return msg->data; + if(msg == NULL) + { + CIOT_LOGE(TAG, "Deserialization error."); + return NULL; + } + + return msg->data; + } + return NULL; } diff --git a/src/common/ciot_storage_fs.c b/src/common/ciot_storage_fs.c index 375bab9d..f59d06d9 100644 --- a/src/common/ciot_storage_fs.c +++ b/src/common/ciot_storage_fs.c @@ -20,10 +20,6 @@ struct ciot_storage_fs typedef struct ciot_storage_fs *ciot_storage_fs_t; -static ciot_err_t ciot_storage_fs_delete(char *path); -static ciot_err_t ciot_storage_fs_write_bytes(char *path, uint8_t *bytes, int size); -static ciot_err_t ciot_storage_fs_read_bytes(char *path, uint8_t *bytes, int *size); - ciot_storage_t ciot_storage_fs_new(void) { ciot_storage_fs_t self = calloc(1, sizeof(struct ciot_storage_fs)); @@ -34,26 +30,26 @@ ciot_storage_t ciot_storage_fs_new(void) return &self->base; } -static ciot_err_t ciot_storage_fs_write_bytes(char *path, uint8_t *bytes, int size) +ciot_err_t ciot_storage_fs_write_bytes(char *path, uint8_t *bytes, int size) { FILE *f = fopen(path, "wb"); if(f != NULL) { fwrite(bytes, size, 1, f); fclose(f); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } -static ciot_err_t ciot_storage_fs_read_bytes(char *path, uint8_t *bytes, int *size) +ciot_err_t ciot_storage_fs_read_bytes(char *path, uint8_t *bytes, int *size) { FILE *f = fopen(path, "rb"); if(f == NULL) { *size = 0; - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } if(bytes == NULL) @@ -61,7 +57,7 @@ static ciot_err_t ciot_storage_fs_read_bytes(char *path, uint8_t *bytes, int *si fseek(f, 0, SEEK_END); *size = ftell(f); fclose(f); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } int idx = 0; @@ -74,11 +70,11 @@ static ciot_err_t ciot_storage_fs_read_bytes(char *path, uint8_t *bytes, int *si } } fclose(f); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } -static ciot_err_t ciot_storage_fs_delete(char *path) +ciot_err_t ciot_storage_fs_delete(char *path) { remove(path); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_str.c b/src/common/ciot_str.c index 8ad21d01..26a223a5 100644 --- a/src/common/ciot_str.c +++ b/src/common/ciot_str.c @@ -17,6 +17,6 @@ ciot_err_t ciot_strncpy(char *destiny, char *source, int size) CIOT_ERR_NULL_CHECK(destiny); CIOT_ERR_EMPTY_STRING_CHECK(source); strncpy(destiny, source, size); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_sys_base.c b/src/common/ciot_sys_base.c index 0aff6a27..bb27f9f4 100644 --- a/src/common/ciot_sys_base.c +++ b/src/common/ciot_sys_base.c @@ -43,7 +43,7 @@ ciot_err_t ciot_sys_init(ciot_sys_t self) base->info.features->hw = &base->hw; base->info.features->sw = &base->sw; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -59,7 +59,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) case CIOT__MSG_TYPE__MSG_TYPE_REQUEST: return ciot_sys_process_req(self, req->data->sys->request); default: - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } } @@ -91,12 +91,12 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.sys = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_sys_process_req(ciot_sys_t self, ciot_sys_req_t *req) @@ -114,7 +114,7 @@ ciot_err_t ciot_sys_process_req(ciot_sys_t self, ciot_sys_req_t *req) break; } - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_sys_get_cfg(ciot_sys_t self, ciot_sys_cfg_t *cfg) @@ -123,7 +123,7 @@ ciot_err_t ciot_sys_get_cfg(ciot_sys_t self, ciot_sys_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_sys_base_t *base = (ciot_sys_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_get_status(ciot_sys_t self, ciot_sys_status_t *status) @@ -132,7 +132,7 @@ ciot_err_t ciot_sys_get_status(ciot_sys_t self, ciot_sys_status_t *status) CIOT_ERR_NULL_CHECK(status); ciot_sys_base_t *base = (ciot_sys_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_get_info(ciot_sys_t self, ciot_sys_info_t *info) @@ -148,7 +148,7 @@ ciot_err_t ciot_sys_get_info(ciot_sys_t self, ciot_sys_info_t *info) ciot_sys_get_hw(&info->hardware); if(info->features) ciot_sys_get_features(info->features); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_sys_get_hw(ciot_sys_hw_t *hw) @@ -171,7 +171,7 @@ static ciot_err_t ciot_sys_get_hw(ciot_sys_hw_t *hw) #else *hw = CIOT__SYS_HW__SYS_HW_UNKNOWN; #endif - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_sys_get_features(ciot_sys_features_t *features) @@ -196,5 +196,5 @@ static ciot_err_t ciot_sys_get_features(ciot_sys_features_t *features) features->sw->mqtt_client = CIOT_CONFIG_FEATURE_MQTT_CLIENT; features->sw->timer = CIOT_CONFIG_FEATURE_TIMER; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_tcp_base.c b/src/common/ciot_tcp_base.c index 58e104c3..7b32541e 100644 --- a/src/common/ciot_tcp_base.c +++ b/src/common/ciot_tcp_base.c @@ -47,7 +47,7 @@ ciot_err_t ciot_tcp_init(ciot_tcp_t self) base->info.mac.data = base->info_mac; base->info.mac.len = sizeof(base->info_mac); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } // static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -66,7 +66,7 @@ ciot_err_t ciot_tcp_init(ciot_tcp_t self) // break; // } -// return CIOT_ERR__OK; +// return CIOT__ERR__OK; // } // static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -96,19 +96,19 @@ ciot_err_t ciot_tcp_init(ciot_tcp_t self) // self->iface.data.tcp = &self->data; // msg->data = &self->iface.data; -// return CIOT_ERR__OK; +// return CIOT__ERR__OK; // } // static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) // { -// return CIOT_ERR__NOT_IMPLEMENTED; +// return CIOT__ERR__NOT_IMPLEMENTED; // } // ciot_err_t ciot_tcp_process_req(ciot_tcp_t self, ciot_tcp_req_t *req) // { // CIOT_ERR_NULL_CHECK(self); // CIOT_ERR_NULL_CHECK(req); -// return CIOT_ERR__NOT_IMPLEMENTED; +// return CIOT__ERR__NOT_IMPLEMENTED; // } ciot_err_t ciot_tcp_get_cfg(ciot_tcp_t self, ciot_tcp_cfg_t *cfg) @@ -117,7 +117,7 @@ ciot_err_t ciot_tcp_get_cfg(ciot_tcp_t self, ciot_tcp_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_tcp_base_t *base = (ciot_tcp_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_tcp_set_cfg(ciot_tcp_t self, ciot_tcp_cfg_t *cfg) @@ -130,7 +130,7 @@ ciot_err_t ciot_tcp_set_cfg(ciot_tcp_t self, ciot_tcp_cfg_t *cfg) if(cfg->gateway.data) memcpy(base->cfg_gateway, cfg->gateway.data, sizeof(base->cfg_gateway)); if(cfg->mask.data) memcpy(base->cfg_mask, cfg->mask.data, sizeof(base->cfg_mask)); if(cfg->dns.data) memcpy(base->cfg_dns, cfg->dns.data, sizeof(base->cfg_dns)); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_tcp_get_status(ciot_tcp_t self, ciot_tcp_status_t *status) @@ -139,7 +139,7 @@ ciot_err_t ciot_tcp_get_status(ciot_tcp_t self, ciot_tcp_status_t *status) CIOT_ERR_NULL_CHECK(status); ciot_tcp_base_t *base = (ciot_tcp_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_tcp_get_info(ciot_tcp_t self, ciot_tcp_info_t *info) @@ -148,5 +148,5 @@ ciot_err_t ciot_tcp_get_info(ciot_tcp_t self, ciot_tcp_info_t *info) CIOT_ERR_NULL_CHECK(info); ciot_tcp_base_t *base = (ciot_tcp_base_t*)self; *info = base->info; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_timer_base.c b/src/common/ciot_timer_base.c index 68baa206..ef3d5d4e 100644 --- a/src/common/ciot_timer_base.c +++ b/src/common/ciot_timer_base.c @@ -25,5 +25,5 @@ ciot_err_t ciot_timer_init(uint64_t *timer, uint16_t interval) { CIOT_ERR_NULL_CHECK(timer); *timer = ciot_timer_now() + interval; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_uart_base.c b/src/common/ciot_uart_base.c index 4f61860b..2fd303e0 100644 --- a/src/common/ciot_uart_base.c +++ b/src/common/ciot_uart_base.c @@ -34,7 +34,7 @@ ciot_err_t ciot_uart_init(ciot_uart_t self) base->iface.send_data = ciot_iface_send_data; base->iface.info.type = CIOT__IFACE_TYPE__IFACE_TYPE_UART; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -53,7 +53,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -81,7 +81,7 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.uart = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) @@ -94,7 +94,7 @@ ciot_err_t ciot_uart_process_req(ciot_uart_t self, ciot_uart_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_uart_get_cfg(ciot_uart_t self, ciot_uart_cfg_t *cfg) @@ -103,7 +103,7 @@ ciot_err_t ciot_uart_get_cfg(ciot_uart_t self, ciot_uart_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_uart_base_t *base = (ciot_uart_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_uart_get_status(ciot_uart_t self, ciot_uart_status_t *status) @@ -112,5 +112,5 @@ ciot_err_t ciot_uart_get_status(ciot_uart_t self, ciot_uart_status_t *status) CIOT_ERR_NULL_CHECK(status); ciot_uart_base_t *base = (ciot_uart_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_usb_base.c b/src/common/ciot_usb_base.c index bf9b1222..00a25598 100644 --- a/src/common/ciot_usb_base.c +++ b/src/common/ciot_usb_base.c @@ -31,22 +31,22 @@ ciot_usb_t ciot_usb_new(void *handle) ciot_err_t ciot_usb_start(ciot_usb_t self, ciot_usb_cfg_t *cfg) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_usb_stop(ciot_usb_t self) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_usb_task(ciot_usb_t self) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_usb_send_bytes(ciot_iface_t *iface, uint8_t *bytes, int size) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } #endif @@ -71,7 +71,7 @@ ciot_err_t ciot_usb_init(ciot_usb_t self) base->iface.send_data = ciot_iface_send_data; base->iface.info.type = CIOT__IFACE_TYPE__IFACE_TYPE_USB; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -90,7 +90,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -120,7 +120,7 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.usb = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) @@ -132,7 +132,7 @@ ciot_err_t ciot_usb_process_req(ciot_usb_t self, ciot_usb_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_usb_get_cfg(ciot_usb_t self, ciot_usb_cfg_t *cfg) @@ -141,7 +141,7 @@ ciot_err_t ciot_usb_get_cfg(ciot_usb_t self, ciot_usb_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_usb_base_t *base = (ciot_usb_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_usb_get_status(ciot_usb_t self, ciot_usb_status_t *status) @@ -150,5 +150,5 @@ ciot_err_t ciot_usb_get_status(ciot_usb_t self, ciot_usb_status_t *status) CIOT_ERR_NULL_CHECK(status); ciot_usb_base_t *base = (ciot_usb_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_viface.c b/src/common/ciot_viface.c index 2a5cf3c6..b5dafc77 100644 --- a/src/common/ciot_viface.c +++ b/src/common/ciot_viface.c @@ -31,11 +31,11 @@ ciot_err_t ciot_viface_start(ciot_viface_t self, ciot_viface_cfg_t *cfg) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_viface_stop(ciot_viface_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_viface_base.c b/src/common/ciot_viface_base.c index 0a86419e..9ac1bd39 100644 --- a/src/common/ciot_viface_base.c +++ b/src/common/ciot_viface_base.c @@ -32,7 +32,7 @@ ciot_err_t ciot_viface_init(ciot_viface_t self, ciot_iface_type_t type) base->iface.send_data = ciot_iface_send_data; base->iface.info.type = type; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -51,7 +51,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -81,7 +81,7 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) // self->iface.data.viface = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) @@ -93,14 +93,14 @@ static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int s printf("%02X", u8ptr[i]); } printf("\n"); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_viface_process_req(ciot_viface_t self, ciot_viface_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_viface_get_cfg(ciot_viface_t self, ciot_viface_cfg_t *cfg) @@ -109,7 +109,7 @@ ciot_err_t ciot_viface_get_cfg(ciot_viface_t self, ciot_viface_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_viface_base_t *base = (ciot_viface_base_t *)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_viface_get_status(ciot_viface_t self, ciot_viface_status_t *status) @@ -118,7 +118,7 @@ ciot_err_t ciot_viface_get_status(ciot_viface_t self, ciot_viface_status_t *stat CIOT_ERR_NULL_CHECK(status); ciot_viface_base_t *base = (ciot_viface_base_t *)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_viface_get_info(ciot_viface_t self, ciot_viface_info_t *info) @@ -127,5 +127,5 @@ ciot_err_t ciot_viface_get_info(ciot_viface_t self, ciot_viface_info_t *info) CIOT_ERR_NULL_CHECK(info); ciot_viface_base_t *base = (ciot_viface_base_t *)self; *info = base->info; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/common/ciot_wifi_base.c b/src/common/ciot_wifi_base.c index c48a4fe1..3c5576fb 100644 --- a/src/common/ciot_wifi_base.c +++ b/src/common/ciot_wifi_base.c @@ -58,7 +58,7 @@ ciot_err_t ciot_wifi_init(ciot_wifi_t self) base->status.tcp = &tcp->status; base->info.tcp = &tcp->info; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) @@ -77,7 +77,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -107,19 +107,19 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) self->iface.data.wifi = &self->data; msg->data = &self->iface.data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_send_data(ciot_iface_t *iface, uint8_t *data, int size) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_wifi_process_req(ciot_wifi_t self, ciot_wifi_req_t *req) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(req); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_wifi_get_cfg(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) @@ -128,7 +128,7 @@ ciot_err_t ciot_wifi_get_cfg(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) CIOT_ERR_NULL_CHECK(cfg); ciot_wifi_base_t *base = (ciot_wifi_base_t*)self; *cfg = base->cfg; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_wifi_get_status(ciot_wifi_t self, ciot_wifi_status_t *status) @@ -137,7 +137,7 @@ ciot_err_t ciot_wifi_get_status(ciot_wifi_t self, ciot_wifi_status_t *status) CIOT_ERR_NULL_CHECK(status); ciot_wifi_base_t *base = (ciot_wifi_base_t*)self; *status = base->status; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_wifi_get_info(ciot_wifi_t self, ciot_wifi_info_t *info) @@ -146,7 +146,7 @@ ciot_err_t ciot_wifi_get_info(ciot_wifi_t self, ciot_wifi_info_t *info) CIOT_ERR_NULL_CHECK(info); ciot_wifi_base_t *base = (ciot_wifi_base_t*)self; *info = base->info; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_wifi_get_mac(ciot_wifi_t self, uint8_t mac[6]) @@ -155,7 +155,7 @@ ciot_err_t ciot_wifi_get_mac(ciot_wifi_t self, uint8_t mac[6]) CIOT_ERR_NULL_CHECK(mac); ciot_wifi_base_t *base = (ciot_wifi_base_t*)self; memcpy(mac, base->info.tcp->mac.data, 6); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_wifi_toggle(ciot_wifi_t self) diff --git a/src/core/ciot.c b/src/core/ciot.c index dfaa5a38..023a20ca 100644 --- a/src/core/ciot.c +++ b/src/core/ciot.c @@ -52,7 +52,7 @@ ciot_err_t ciot_start(ciot_t self, ciot_cfg_t *cfg) self->status.state != CIOT__CIOT_STATE__CIOT_STATE_STARTED) { CIOT_LOGE(TAG, "ciot core incorrect state"); - return CIOT_ERR__INVALID_STATE; + return CIOT__ERR__INVALID_STATE; } self->ifaces.cfgs = cfg->cfgs; self->storage = cfg->storage; @@ -62,7 +62,7 @@ ciot_err_t ciot_start(ciot_t self, ciot_cfg_t *cfg) self->starting.waiting_result = false; self->status.state = CIOT__CIOT_STATE__CIOT_STATE_STARTING; CIOT_LOGI(TAG, "CIOT_STATE_STARTING"); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_stop(ciot_t self) @@ -70,7 +70,7 @@ ciot_err_t ciot_stop(ciot_t self) CIOT_ERR_NULL_CHECK(self); self->status.state = CIOT__CIOT_STATE__CIOT_STATE_IDLE; CIOT_LOGV(TAG, "CIOT_STATE_IDLE"); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_task(ciot_t self) @@ -93,7 +93,7 @@ ciot_err_t ciot_task(ciot_t self) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_starting_task(ciot_t self) @@ -103,16 +103,16 @@ static ciot_err_t ciot_starting_task(ciot_t self) if (self->ifaces.count == 1) { - return CIOT_ERR__OK; + return CIOT__ERR__OK; } if (self->starting.waiting_result) { if (self->recv.sender != NULL && - self->recv.event.type == CIOT_IFACE_EVENT_STARTED && + ciot_iface_event_is_ack(self->recv.event.type) && self->starting.iface_id == self->recv.sender->info.id) { - CIOT_LOGI(TAG, "Interface [%lu]:%s Started", self->recv.sender->info.id, ciot_iface_to_str(self->recv.sender)); + CIOT_LOGI(TAG, "Interface [%lu]:%s evt type %d received", self->recv.sender->info.id, ciot_iface_to_str(self->recv.sender), self->recv.event.type); if(self->iface.event_handler != NULL) { self->iface.event_handler(self->recv.sender, &self->recv.event, self->iface.event_args); @@ -157,7 +157,7 @@ static ciot_err_t ciot_starting_task(ciot_t self) if(cfg != NULL) { ciot_err_t err = ciot_start_iface(self, iface, cfg); - if (err == CIOT_ERR__OK) + if (err == CIOT__ERR__OK) { ciot_timer_init(&self->starting.timer, CIOT_IFACE_START_TIMEOUT_SECS); self->starting.waiting_result = true; @@ -184,7 +184,7 @@ static ciot_err_t ciot_starting_task(ciot_t self) ciot_iface_send_event(&self->iface, &event); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_busy_task(ciot_t self) @@ -195,7 +195,7 @@ static ciot_err_t ciot_busy_task(ciot_t self) { CIOT_LOGE(TAG, "Sender is null"); self->status.state = CIOT__CIOT_STATE__CIOT_STATE_STARTED; - return CIOT_ERR__NULL_ARG; + return CIOT__ERR__NULL_ARG; } ciot_iface_event_t *event = &self->recv.event; @@ -248,14 +248,14 @@ static ciot_err_t ciot_busy_task(ciot_t self) { CIOT_LOGE(TAG, "Error. %d iface is null.", id); event->type = CIOT_IFACE_EVENT_ERROR; - ciot_iface_send_error(sender, CIOT__IFACE_TYPE__IFACE_TYPE_UNKNOWN, id, event->msg, CIOT_ERR__NULL_ARG); + ciot_iface_send_error(sender, CIOT__IFACE_TYPE__IFACE_TYPE_UNKNOWN, id, event->msg, CIOT__ERR__NULL_ARG); } } // else if(event->msg->type != CIOT__MSG_TYPE__MSG_TYPE_CUSTOM) // { // CIOT_LOGE(TAG, "Error msg type %s from iface %d.", ciot__msg_type__descriptor.values[event->msg->type].name, id); // event->type = CIOT_IFACE_EVENT_ERROR; - // ciot_iface_send_error(sender, CIOT__IFACE_TYPE__IFACE_TYPE_UNKNOWN, id, event->msg, CIOT_ERR__INVALID_HEADER); + // ciot_iface_send_error(sender, CIOT__IFACE_TYPE__IFACE_TYPE_UNKNOWN, id, event->msg, CIOT__ERR__INVALID_HEADER); // } } @@ -269,7 +269,7 @@ static ciot_err_t ciot_busy_task(ciot_t self) CIOT_LOGI(TAG, "ciot done"); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_start_iface(ciot_t self, ciot_iface_t *iface, ciot_msg_data_t *cfg) @@ -286,7 +286,7 @@ static ciot_err_t ciot_start_iface(ciot_t self, ciot_iface_t *iface, ciot_msg_da static ciot_err_t ciot_set_iface_list(ciot_t self, ciot_iface_t *ifaces[], int count) { - ciot_err_t ret = CIOT_ERR__OK; + ciot_err_t ret = CIOT__ERR__OK; self->ifaces.list = ifaces; self->ifaces.count = count; @@ -300,7 +300,7 @@ static ciot_err_t ciot_set_iface_list(ciot_t self, ciot_iface_t *ifaces[], int c if(iface->info.id != self->iface.info.id) { ciot_err_t err = ciot_iface_set_event_handler(iface, ciot_iface_event_handler, self); - if (err != CIOT_ERR__OK) + if (err != CIOT__ERR__OK) { CIOT_LOGE(TAG, "Interface id:%d type%d register event error: %d", i, iface->info.type, err); ret = err; @@ -332,10 +332,10 @@ static ciot_err_t ciot_bytes_received(ciot_t self, ciot_iface_t *sender, uint8_t if(self->recv.event.msg == NULL) { CIOT_LOGE(TAG, "msg unpack error"); - return CIOT_ERR__DESERIALIZATION; + return CIOT__ERR__DESERIALIZATION; } } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_event_handler(ciot_iface_t *sender, ciot_iface_event_t *event, void *event_args) @@ -355,7 +355,7 @@ static ciot_err_t ciot_iface_event_handler(ciot_iface_t *sender, ciot_iface_even if (self->status.state == CIOT__CIOT_STATE__CIOT_STATE_BUSY) { CIOT_LOGE(TAG, "ciot busy. %s(%lu) evt type:%d ignored", ciot_iface_to_str(sender), sender->info.id, event->type); - return CIOT_ERR__BUSY; + return CIOT__ERR__BUSY; } self->recv.event.type = event->type; @@ -366,7 +366,7 @@ static ciot_err_t ciot_iface_event_handler(ciot_iface_t *sender, ciot_iface_even if (self->status.state != CIOT__CIOT_STATE__CIOT_STATE_STARTED) { CIOT_LOGE(TAG, "ciot core is not started"); - return CIOT_ERR__BUSY; + return CIOT__ERR__BUSY; } CIOT_ERR_RETURN(ciot_bytes_received(self, sender, event->data, event->size)); @@ -377,7 +377,7 @@ static ciot_err_t ciot_iface_event_handler(ciot_iface_t *sender, ciot_iface_even if(log->level == CIOT__LOG_LEVEL__LOG_LEVEL_INFO) CIOT_LOGI(TAG, "[%s] %s", log->tag, log->message); if(log->level == CIOT__LOG_LEVEL__LOG_LEVEL_WARNING) CIOT_LOGW(TAG, "[%s] %s", log->tag, log->message); if(log->level == CIOT__LOG_LEVEL__LOG_LEVEL_ERROR) CIOT_LOGE(TAG, "[%s] %s", log->tag, log->message); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } if(self->recv.event.msg->type == CIOT__MSG_TYPE__MSG_TYPE_CUSTOM && self->iface.event_handler != NULL) @@ -400,7 +400,7 @@ static ciot_err_t ciot_iface_event_handler(ciot_iface_t *sender, ciot_iface_even self->status.state = CIOT__CIOT_STATE__CIOT_STATE_BUSY; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_free_recv(ciot_t self) @@ -411,6 +411,6 @@ static ciot_err_t ciot_free_recv(ciot_t self) self->recv.serialized = false; self->recv.event.msg = NULL; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/core/ciot_err.c b/src/core/ciot_err.c index 5feb324d..47ddfad3 100644 --- a/src/core/ciot_err.c +++ b/src/core/ciot_err.c @@ -17,9 +17,9 @@ const char *ciot_err_to_message(ciot_err_t err) { - if (err >= 0 && err <= CIOT_ERR__FAIL) + if (err >= 0 && err <= CIOT__ERR__FAIL) { - return ciot_err__descriptor.values[err].name; + return ciot__err__descriptor.values[err].name; } else { diff --git a/src/core/ciot_iface.c b/src/core/ciot_iface.c index 0eea137d..4d7a2c0e 100644 --- a/src/core/ciot_iface.c +++ b/src/core/ciot_iface.c @@ -27,7 +27,7 @@ ciot_err_t ciot_iface_init(ciot_iface_t *self) CIOT_ERR_NULL_CHECK(self); ciot__msg_data__init(&self->data); ciot__iface_info__init(&self->info); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_iface_send_msg(ciot_iface_t *self, ciot_msg_t *msg) @@ -38,7 +38,7 @@ ciot_err_t ciot_iface_send_msg(ciot_iface_t *self, ciot_msg_t *msg) if (self->req_status.state != CIOT_IFACE_REQ_STATE_IDLE) { CIOT_LOGE(TAG, "Iface %s (%lu) is busy", ciot_iface_to_str(self), self->info.id); - return CIOT_ERR__BUSY; + return CIOT__ERR__BUSY; } else { @@ -58,7 +58,7 @@ ciot_err_t ciot_iface_send_rsp(ciot_iface_t *self, ciot_msg_t *rsp) if (self->req_status.state != CIOT_IFACE_REQ_STATE_IDLE) { CIOT_LOGE(TAG, "Iface %s (%lu) is busy", ciot_iface_to_str(self), self->info.id); - return CIOT_ERR__BUSY; + return CIOT__ERR__BUSY; } else { @@ -85,7 +85,7 @@ ciot_err_t ciot_iface_send_req(ciot_iface_t *self, ciot_msg_t *req) // CIOT_LOG_MSG_P("ciot", CIOT_LOGV, "TX REQ <- ", self, req); ciot_err_t err = ciot_iface_send(self, req); - if(err != CIOT_ERR__OK) + if(err != CIOT__ERR__OK) { self->req_status.state = CIOT_IFACE_REQ_STATE_IDLE; } @@ -106,7 +106,7 @@ ciot_err_t ciot_iface_send_error(ciot_iface_t *self, ciot_iface_type_t iface_typ error.msg->type = msg->type; error.iface->type = iface_type; error.iface->id = iface_id; - error.code = CIOT_ERR__NULL_ARG; + error.code = CIOT__ERR__NULL_ARG; msg->type = CIOT__MSG_TYPE__MSG_TYPE_ERROR; msg->data->error = &error; self->req_status.state = CIOT_IFACE_REQ_STATE_IDLE; @@ -137,7 +137,7 @@ ciot_err_t ciot_iface_process_msg(ciot_iface_t *self, ciot_msg_t *msg, ciot_ifac CIOT_ERR_NULL_CHECK(msg); ciot_iface_t *sender_iface = (ciot_iface_t*)sender; - ciot_err_t err = CIOT_ERR__OK; + ciot_err_t err = CIOT__ERR__OK; ciot_msg_data_t *msg_data = msg->data; CIOT_LOGD(TAG, "%p", msg_data); @@ -146,24 +146,24 @@ ciot_err_t ciot_iface_process_msg(ciot_iface_t *self, ciot_msg_t *msg, ciot_ifac { if (msg->iface->id != self->info.id) { - err = CIOT_ERR__INVALID_ID; + err = CIOT__ERR__INVALID_ID; } if (msg->iface->type != self->info.type) { - err = CIOT_ERR__INVALID_TYPE; + err = CIOT__ERR__INVALID_TYPE; } } else { - err = CIOT_ERR__NULL_ARG; + err = CIOT__ERR__NULL_ARG; } - if(err == CIOT_ERR__OK) + if(err == CIOT__ERR__OK) { switch (msg->type) { case CIOT__MSG_TYPE__MSG_TYPE_UNKNOWN: - err = CIOT_ERR__INVALID_TYPE; + err = CIOT__ERR__INVALID_TYPE; break; case CIOT__MSG_TYPE__MSG_TYPE_START: case CIOT__MSG_TYPE__MSG_TYPE_STOP: @@ -183,7 +183,7 @@ ciot_err_t ciot_iface_process_msg(ciot_iface_t *self, ciot_msg_t *msg, ciot_ifac err = ciot_iface_process_request_result(self, sender_iface, msg, err); break; case CIOT__MSG_TYPE__MSG_TYPE_EVENT: - err = CIOT_ERR__NOT_IMPLEMENTED; + err = CIOT__ERR__NOT_IMPLEMENTED; break; case CIOT__MSG_TYPE__MSG_TYPE_ERROR: CIOT_LOG_MSG_P(TAG, CIOT_LOGE, "RX ERR <- ", sender_iface, msg); @@ -193,7 +193,7 @@ ciot_err_t ciot_iface_process_msg(ciot_iface_t *self, ciot_msg_t *msg, ciot_ifac } } - if(err != CIOT_ERR__OK) + if(err != CIOT__ERR__OK) { ciot_msg_error_t error = { .base = PROTOBUF_C_MESSAGE_INIT(&ciot__msg_error__descriptor), @@ -221,12 +221,12 @@ ciot_err_t ciot_iface_process_data(ciot_iface_t *self, uint8_t *data, int size) for (size_t i = 0; i < size; i++) { ciot_err_t err = self->decoder->decode(self, data[i]); - if(err != CIOT_ERR__OK) + if(err != CIOT__ERR__OK) { return err; } } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } else { @@ -247,7 +247,7 @@ ciot_err_t ciot_iface_register_req(ciot_iface_t *self, ciot_iface_info_t *iface, self->req_status.type = req->type; self->req_status.iface.id = iface->id; self->req_status.iface.type = iface->type; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_iface_set_event_handler(ciot_iface_t *self, ciot_iface_event_handler_fn event_handler, void *event_args) @@ -255,21 +255,21 @@ ciot_err_t ciot_iface_set_event_handler(ciot_iface_t *self, ciot_iface_event_han CIOT_ERR_NULL_CHECK(self); self->event_handler = event_handler; self->event_args = event_args; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_iface_set_serializer(ciot_iface_t *self, ciot_serializer_t serializer) { CIOT_ERR_NULL_CHECK(self); self->serializer = serializer; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_iface_set_decoder(ciot_iface_t *self, ciot_iface_decoder_t decoder) { CIOT_ERR_NULL_CHECK(self); self->decoder = decoder; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_iface_get_msg_id(void) @@ -363,7 +363,7 @@ static ciot_err_t ciot_iface_send(ciot_iface_t *self, ciot_msg_t *msg) static ciot_err_t ciot_iface_process_request_result(ciot_iface_t *self, ciot_iface_t *sender, ciot_msg_t *msg, ciot_err_t err) { - if(self->req_status.state == CIOT_IFACE_REQ_STATE_IDLE && err == CIOT_ERR__OK) + if(self->req_status.state == CIOT_IFACE_REQ_STATE_IDLE && err == CIOT__ERR__OK) { return ciot_iface_send_rsp(sender, msg); } diff --git a/src/core/ciot_model.c b/src/core/ciot_model.c index dffa545b..edc1819b 100644 --- a/src/core/ciot_model.c +++ b/src/core/ciot_model.c @@ -31,11 +31,11 @@ ciot_err_t ciot_model_start(ciot_model_t self, ciot_model_cfg_t *cfg) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_model_stop(ciot_model_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } diff --git a/src/core/ciot_msg.c b/src/core/ciot_msg.c index 941540b9..f46e00af 100644 --- a/src/core/ciot_msg.c +++ b/src/core/ciot_msg.c @@ -25,7 +25,7 @@ ciot_err_t ciot_msg_init(ciot_msg_t *msg) ciot__msg_data__init(&msg_data); msg->iface = &iface_info; msg->data = &msg_data; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_msg_t* ciot_msg_get(ciot_msg_type_t type, ciot_iface_t *iface) @@ -141,7 +141,9 @@ int ciot_msg_to_json(const ProtobufCMessage *message, char *json) { const ProtobufCEnumDescriptor *enum_descr = field_desc->descriptor; int value = *(int *)field; - idx += sprintf(&json[idx], "\"%s\",\n", enum_descr->values[value].name); + idx += value < enum_descr->n_values + ? sprintf(&json[idx], "\"%s\",\n", enum_descr->values[value].name) + : sprintf(&json[idx], "%d,\n", value); break; } case PROTOBUF_C_TYPE_BYTES: diff --git a/src/esp32/ciot_eth.c b/src/esp32/ciot_eth.c index 2e64ecde..5e9323fc 100644 --- a/src/esp32/ciot_eth.c +++ b/src/esp32/ciot_eth.c @@ -45,6 +45,11 @@ ciot_err_t ciot_eth_start(ciot_eth_t self, ciot_tcp_cfg_t *cfg) ciot_tcp_base_t *tcp = (ciot_tcp_base_t*)self->base.tcp; + if(cfg->disabled) + { + return ciot_eth_stop(self); + } + if(!self->hw_init) { ESP_ERROR_CHECK(ciot_eth_hw_init(self)); @@ -55,14 +60,25 @@ ciot_err_t ciot_eth_start(ciot_eth_t self, ciot_tcp_cfg_t *cfg) CIOT_ERR_RETURN(esp_eth_stop(self->eth)); } + CIOT_ERR_RETURN(ciot_tcp_set_cfg(self->base.tcp, cfg)); + CIOT_ERR_RETURN(ciot_tcp_start(self->base.tcp)); CIOT_ERR_RETURN(esp_eth_start(self->eth)); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_eth_stop(ciot_eth_t self) { - return CIOT_ERR__NOT_IMPLEMENTED; + ciot_tcp_base_t *tcp = (ciot_tcp_base_t*)self->base.tcp; + if(tcp->status.state == CIOT__TCP_STATE__TCP_STATE_STOPPED) + { + return ciot_iface_send_event_type(&self->base.iface, CIOT_IFACE_EVENT_STOPPED); + } + if(tcp->status.state == CIOT__TCP_STATE__TCP_STATE_STARTED) + { + CIOT_ERR_RETURN(esp_eth_stop(self->eth)); + } + return CIOT__ERR__OK; } static esp_err_t ciot_eth_hw_init(ciot_eth_t self) @@ -90,7 +106,7 @@ static esp_err_t ciot_eth_hw_init(ciot_eth_t self) CIOT_ERR_RETURN(esp_eth_driver_install(ð_conf, &self->eth)); CIOT_ERR_RETURN(esp_netif_attach((esp_netif_t*)ciot_tcp_get_netif(self->base.tcp), esp_eth_new_netif_glue(self->eth))); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } // static ciot_err_t ciot_eth_tcp_event_handler(ciot_iface_t *sender, ciot_iface_event_t *iface_event, void *args) @@ -102,22 +118,22 @@ static esp_err_t ciot_eth_hw_init(ciot_eth_t self) static void ciot_eth_event_handler(void *handler_args, esp_event_base_t event_base, int32_t event_id, void *event_data) { - ciot_eth_t self = (ciot_eth_t)event_data; + ciot_eth_t self = (ciot_eth_t)handler_args; ciot_eth_base_t *base = &self->base; ciot_tcp_base_t *tcp = (ciot_tcp_base_t*)base->tcp; switch ((eth_event_t)event_id) { case ETHERNET_EVENT_START: - ESP_LOGI(TAG, "ETHERNET_EVENT_START"); + ESP_LOGI(TAG, "ETHERNET_EVENT_START %s", ciot__tcp_dhcp_cfg__descriptor.values[tcp->cfg.dhcp].name); CIOT_ERR_PRINT(TAG, esp_read_mac(tcp->info.mac.data, ESP_MAC_ETH)); + ciot_iface_send_event_type(&base->iface, CIOT_IFACE_EVENT_STARTED); break; case ETHERNET_EVENT_STOP: ESP_LOGI(TAG, "ETHERNET_EVENT_STOP"); break; case ETHERNET_EVENT_CONNECTED: ESP_LOGI(TAG, "ETHERNET_EVENT_CONNECTED"); - CIOT_ERR_PRINT(TAG, ciot_tcp_start(base->tcp)); break; case ETHERNET_EVENT_DISCONNECTED: ESP_LOGI(TAG, "ETHERNET_EVENT_DISCONNECTED"); diff --git a/src/esp32/ciot_gpio.c b/src/esp32/ciot_gpio.c index 9419c158..07c74382 100644 --- a/src/esp32/ciot_gpio.c +++ b/src/esp32/ciot_gpio.c @@ -58,5 +58,5 @@ ciot_err_t ciot_gpio_start(ciot_gpio_t self, ciot_gpio_cfg_t *cfg) ciot_err_t ciot_gpio_stop(ciot_gpio_t self) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } diff --git a/src/esp32/ciot_http_server.c b/src/esp32/ciot_http_server.c index 1f8ae442..902707cb 100644 --- a/src/esp32/ciot_http_server.c +++ b/src/esp32/ciot_http_server.c @@ -25,6 +25,8 @@ static const char *TAG = "ciot_http_server"; static ciot_err_t ciot_https_register_routes(ciot_http_server_t self); static esp_err_t ciot_post_handler(httpd_req_t *req); +static esp_err_t ciot_file_handler(httpd_req_t *req); +static const char *get_mime_type(const char *filename); ciot_http_server_t ciot_http_server_new(void *handle) { @@ -79,19 +81,36 @@ ciot_err_t ciot_http_server_send_bytes(ciot_http_server_t self, uint8_t *data, i httpd_resp_set_type(self->req, HTTPD_TYPE_OCTET); httpd_resp_send(self->req, (const char*)data, size); httpd_req_async_handler_complete(self->req); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_https_register_routes(ciot_http_server_t self) { CIOT_LOGI(TAG, "Registering route: %s", self->base.route); - httpd_uri_t ciot_post = { + httpd_uri_t post_uri = { .uri = self->base.route, .handler = ciot_post_handler, .method = HTTP_POST, .user_ctx = self, }; - return httpd_register_uri_handler(self->handle, &ciot_post); + esp_err_t err = httpd_register_uri_handler(self->handle, &post_uri); + if(err) { + CIOT_LOGE(TAG, "Register uri error: %s", esp_err_to_name(err)); + return CIOT__ERR__FAIL; + } + + httpd_uri_t file_uri = { + .uri = "/*", // Captura todas as URIs + .method = HTTP_GET, + .handler = ciot_file_handler, + .user_ctx = NULL + }; + err = httpd_register_uri_handler(self->handle, &file_uri); + if(err) { + CIOT_LOGE(TAG, "Register uri error: %s", esp_err_to_name(err)); + return CIOT__ERR__FAIL; + } + return CIOT__ERR__OK; } static esp_err_t ciot_post_handler(httpd_req_t *req) @@ -103,7 +122,7 @@ static esp_err_t ciot_post_handler(httpd_req_t *req) if (self == NULL) { CIOT_LOGE(TAG, "Null context"); - return CIOT_ERR__NULL_ARG; + return CIOT__ERR__NULL_ARG; } uint8_t *buf = calloc(1, req->content_len); @@ -119,5 +138,51 @@ static esp_err_t ciot_post_handler(httpd_req_t *req) free(buf); - return CIOT_ERR__OK; + return CIOT__ERR__OK; +} + +static esp_err_t ciot_file_handler(httpd_req_t *req) +{ + char filepath[64]; + snprintf(filepath, sizeof(filepath), "/fs%.*s", (int)(sizeof(filepath) - 4), req->uri); + + // Verificar se a URI é "/", servir "index.html" + if (strcmp(req->uri, "/") == 0) { + strcpy(filepath, "/fs/index.html"); + } + + // Abrir o arquivo solicitado + FILE* file = fopen(filepath, "r"); + if (!file) { + ESP_LOGE(TAG, "Failed to open file: %s", filepath); + httpd_resp_send_404(req); + return ESP_FAIL; + } + + // Configurar o cabeçalho com o tipo de conteúdo adequado + httpd_resp_set_type(req, get_mime_type(filepath)); + + // Ler o arquivo e enviar seu conteúdo + char line[128]; + while (fgets(line, sizeof(line), file)) { + httpd_resp_sendstr_chunk(req, line); + } + + // Enviar a resposta final + httpd_resp_sendstr_chunk(req, NULL); + + fclose(file); + return ESP_OK; +} + +static const char *get_mime_type(const char *filename) +{ + if (strstr(filename, ".html")) return "text/html"; + if (strstr(filename, ".css")) return "text/css"; + if (strstr(filename, ".js")) return "application/javascript"; + if (strstr(filename, ".png")) return "image/png"; + if (strstr(filename, ".jpg")) return "image/jpeg"; + if (strstr(filename, ".ico")) return "image/x-icon"; + if (strstr(filename, ".svg")) return "image/svg+xml"; + return "text/plain"; } diff --git a/src/esp32/ciot_mqtt_client.c b/src/esp32/ciot_mqtt_client.c index 7b1d9ad0..ed9b082c 100644 --- a/src/esp32/ciot_mqtt_client.c +++ b/src/esp32/ciot_mqtt_client.c @@ -83,7 +83,7 @@ ciot_err_t ciot_mqtt_client_start(ciot_mqtt_client_t self, ciot_mqtt_client_cfg_ esp_mqtt_client_register_event(self->handle, ESP_EVENT_ANY_ID, ciot_mqtt_event_handler, self); esp_err_t err = esp_mqtt_client_start(self->handle); - return err == ESP_OK ? CIOT_ERR__OK : CIOT_ERR__FAIL; + return err == ESP_OK ? CIOT__ERR__OK : CIOT__ERR__FAIL; } ciot_err_t ciot_mqtt_client_stop(ciot_mqtt_client_t self) @@ -91,7 +91,7 @@ ciot_err_t ciot_mqtt_client_stop(ciot_mqtt_client_t self) CIOT_ERR_NULL_CHECK(self); self->base.status.state = CIOT__MQTT_CLIENT_STATE__MQTT_STATE_DISCONNECTING; esp_err_t err = esp_mqtt_client_stop(self->handle); - return err == ESP_OK ? CIOT_ERR__OK : CIOT_ERR__FAIL; + return err == ESP_OK ? CIOT__ERR__OK : CIOT__ERR__FAIL; } ciot_err_t ciot_mqtt_client_sub(ciot_mqtt_client_t self, char *topic, int qos) @@ -100,13 +100,15 @@ ciot_err_t ciot_mqtt_client_sub(ciot_mqtt_client_t self, char *topic, int qos) CIOT_ERR_NULL_CHECK(topic); CIOT_ERR_EMPTY_STRING_CHECK(topic); esp_err_t err = esp_mqtt_client_subscribe(self->handle, topic, qos); - return err == ESP_OK ? CIOT_ERR__OK : CIOT_ERR__FAIL; + return err == ESP_OK ? CIOT__ERR__OK : CIOT__ERR__FAIL; } ciot_err_t ciot_mqtt_client_pub(ciot_mqtt_client_t self, char *topic, uint8_t *data, int size, int qos) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(topic); + CIOT_ERR_NULL_CHECK(self->handle); + CIOT_ERR_VALUE_CHECK(self->base.status.state, CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTED, CIOT__ERR__INVALID_STATE); CIOT_ERR_EMPTY_STRING_CHECK(topic); int err = 0; if(qos == 0) @@ -119,7 +121,7 @@ ciot_err_t ciot_mqtt_client_pub(ciot_mqtt_client_t self, char *topic, uint8_t *d err = esp_mqtt_client_enqueue(self->handle, topic, (char*)data, size, qos, false, false); if(err == 0) ciot_mqtt_client_update_data_rate(self, size); } - return err <= 0 ? CIOT_ERR__OK : CIOT_ERR__FAIL; + return err <= 0 ? CIOT__ERR__OK : CIOT__ERR__FAIL; } static void ciot_mqtt_event_handler(void *handler_args, esp_event_base_t event_base, int32_t event_id, void *event_data) diff --git a/src/esp32/ciot_ntp.c b/src/esp32/ciot_ntp.c index 4bd0f66c..6c59341e 100644 --- a/src/esp32/ciot_ntp.c +++ b/src/esp32/ciot_ntp.c @@ -69,14 +69,14 @@ ciot_err_t ciot_ntp_start(ciot_ntp_t self, ciot_ntp_cfg_t *cfg) tzset(); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ntp_stop(ciot_ntp_t self) { CIOT_ERR_NULL_CHECK(self); esp_sntp_stop(); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static void ciot_ntp_sync_notification_cb(struct timeval *tv) diff --git a/src/esp32/ciot_ota.c b/src/esp32/ciot_ota.c index 04804fc1..07217687 100644 --- a/src/esp32/ciot_ota.c +++ b/src/esp32/ciot_ota.c @@ -63,18 +63,18 @@ ciot_err_t ciot_ota_start(ciot_ota_t self, ciot_ota_cfg_t *cfg) #ifndef CONFIG_ESP_HTTPS_OTA_DECRYPT_CB if (base->cfg.encrypted) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } #endif // CONFIG_ESP_HTTPS_OTA_DECRYPT_CB if(base->status.state != CIOT__OTA_STATE__OTA_STATE_IDLE) { - return CIOT_ERR__INVALID_STATE; + return CIOT__ERR__INVALID_STATE; } self->buffer = malloc(CIOT_CONFIG_OTA_BUF_SIZE); base->status.state = CIOT__OTA_STATE__OTA_STATE_INIT; - base->status.error = CIOT_ERR__OK; + base->status.error = CIOT__ERR__OK; base->cfg = *cfg; base->cfg.url = base->url; ciot_strncpy(base->url, cfg->url, CIOT_CONFIG_OTA_URL_LEN); @@ -99,7 +99,7 @@ ciot_err_t ciot_ota_stop(ciot_ota_t self) CIOT_ERR_NULL_CHECK(self); vTaskDelete(self->task); free(self); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ota_rollback(ciot_ota_t self) @@ -110,7 +110,7 @@ ciot_err_t ciot_ota_rollback(ciot_ota_t self) } else { - return CIOT_ERR__IMPOSSIBLE_OP; + return CIOT__ERR__IMPOSSIBLE_OP; } } diff --git a/src/esp32/ciot_storage_auto.c b/src/esp32/ciot_storage_auto.c new file mode 100644 index 00000000..95deda66 --- /dev/null +++ b/src/esp32/ciot_storage_auto.c @@ -0,0 +1,39 @@ +/** + * @file ciot_storage_auto.c + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-10-17 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +#include "esp_vfs.h" +#include "esp_vfs_fat.h" +#include "esp_spiffs.h" +#include "esp_partition.h" + +#include "ciot_storage.h" +#include "ciot_storage_fs.h" +#include "ciot_storage_fat.h" +#include "ciot_storage_spiffs.c" + +typedef struct ciot_storage_auto *ciot_storage_auto_t; + +ciot_storage_t ciot_storage_auto_new(void) +{ + esp_partition_iterator_t iterator = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, FS_PARTITION_LABLE); + const esp_partition_t* partition = esp_partition_get(iterator); + + switch (partition->subtype) + { + case ESP_PARTITION_SUBTYPE_DATA_FAT: + return ciot_storage_fat_new(); + case ESP_PARTITION_SUBTYPE_DATA_SPIFFS: + return ciot_storage_spiffs_new(); + default: + return NULL; + } +} diff --git a/src/esp32/ciot_storage_fat.c b/src/esp32/ciot_storage_fat.c new file mode 100644 index 00000000..3ce424b1 --- /dev/null +++ b/src/esp32/ciot_storage_fat.c @@ -0,0 +1,62 @@ +/** + * @file ciot_storage_fat.c + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-10-17 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +#include "ciot_storage.h" +#include "ciot_storage_fs.h" +#include "esp_vfs.h" +#include "esp_vfs_fat.h" + +#define FS_BASE_PATH "/fs" +#define FS_PARTITION_LABLE "storage" + +#ifndef CONFIG_WL_SECTOR_SIZE +#define CONFIG_WL_SECTOR_SIZE 512 +#endif //! CONFIG_WL_SECTOR_SIZE + +struct ciot_storage_fat +{ + struct ciot_storage base; + wl_handle_t handle; +}; + +typedef struct ciot_storage_fat *ciot_storage_fat_t; + +static const char *TAG = "ciot_storage_fat"; + +static ciot_err_t ciot_storage_fat_init(ciot_storage_fat_t self); + +ciot_storage_t ciot_storage_fat_new(void) +{ + ciot_storage_fat_t self = calloc(1, sizeof(struct ciot_storage_fat)); + ciot_storage_t base = &self->base; + ciot_storage_fat_init(self); + base->delete = ciot_storage_fs_delete; + base->write_bytes = ciot_storage_fs_write_bytes; + base->read_bytes = ciot_storage_fs_read_bytes; + return &self->base; +} + +static ciot_err_t ciot_storage_fat_init(ciot_storage_fat_t self) +{ + esp_vfs_fat_mount_config_t cfg = { + .max_files = 4, + .format_if_mount_failed = true, + .allocation_unit_size = CONFIG_WL_SECTOR_SIZE, + }; + esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(FS_BASE_PATH, FS_PARTITION_LABLE, &cfg, &self->handle); + if(err) + { + CIOT_LOGE(TAG, "Error mounting fat: %s", esp_err_to_name(err)); + return CIOT__ERR__FAIL; + } + return CIOT__ERR__OK; +} diff --git a/src/esp32/ciot_storage_nvs.c b/src/esp32/ciot_storage_nvs.c index 6312ffa4..a76694c6 100644 --- a/src/esp32/ciot_storage_nvs.c +++ b/src/esp32/ciot_storage_nvs.c @@ -75,26 +75,26 @@ static ciot_err_t ciot_storage_nvs_read_bytes(char *path, uint8_t *bytes, int *s ciot_err_t err = nvs_open(CIOT_STORAGE_NVS_NS, NVS_READONLY, &handle); if(err == ESP_ERR_NVS_NOT_FOUND) { - return CIOT_ERR__NOT_FOUND; + return CIOT__ERR__NOT_FOUND; } else if(err != ESP_OK) { CIOT_LOGE(TAG, "nvs get blob error: %s", esp_err_to_name(err)); - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } err = nvs_get_blob(handle, path, bytes, (size_t*)size); if(err == ESP_ERR_NVS_NOT_FOUND) { - return CIOT_ERR__NOT_FOUND; + return CIOT__ERR__NOT_FOUND; } else if(err != ESP_OK) { CIOT_LOGE(TAG, "nvs get blob error: %s", esp_err_to_name(err)); - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_storage_nvs_init(void) @@ -113,6 +113,6 @@ static ciot_err_t ciot_storage_nvs_init(void) } else { - return CIOT_ERR__OK; + return CIOT__ERR__OK; } } diff --git a/src/esp32/ciot_storage_spiffs.c b/src/esp32/ciot_storage_spiffs.c new file mode 100644 index 00000000..385f4438 --- /dev/null +++ b/src/esp32/ciot_storage_spiffs.c @@ -0,0 +1,61 @@ +/** + * @file ciot_storage_spiffs.c + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-10-17 + * + * @copyright Copyright (c) 2024 + * + */ + +#include +#include "ciot_storage.h" +#include "ciot_storage_fs.h" +#include "esp_spiffs.h" + +#define FS_BASE_PATH "/fs" +#define FS_PARTITION_LABLE "storage" + +#ifndef CONFIG_WL_SECTOR_SIZE +#define CONFIG_WL_SECTOR_SIZE 512 +#endif //! CONFIG_WL_SECTOR_SIZE + +struct ciot_storage_spiffs +{ + struct ciot_storage base; +}; + +typedef struct ciot_storage_spiffs *ciot_storage_spiffs_t; + +static const char *TAG = "ciot_storage_spiffs"; + +static ciot_err_t ciot_storage_spiffs_init(ciot_storage_spiffs_t self); + +ciot_storage_t ciot_storage_spiffs_new(void) +{ + ciot_storage_spiffs_t self = calloc(1, sizeof(struct ciot_storage_spiffs)); + ciot_storage_t base = &self->base; + ciot_storage_spiffs_init(self); + base->delete = ciot_storage_fs_delete; + base->write_bytes = ciot_storage_fs_write_bytes; + base->read_bytes = ciot_storage_fs_read_bytes; + return &self->base; +} + +static ciot_err_t ciot_storage_spiffs_init(ciot_storage_spiffs_t self) +{ + esp_vfs_spiffs_conf_t cfg = { + .base_path = FS_BASE_PATH, + .partition_label = FS_PARTITION_LABLE, + .max_files = 4, + .format_if_mount_failed = true, + }; + esp_err_t err = esp_vfs_spiffs_register(&cfg); + if(err) + { + CIOT_LOGE(TAG, "Error mounting spiffs: %s", esp_err_to_name(err)); + return CIOT__ERR__FAIL; + } + return CIOT__ERR__OK; +} diff --git a/src/esp32/ciot_sys.c b/src/esp32/ciot_sys.c index 4d45373b..fd10fb3e 100644 --- a/src/esp32/ciot_sys.c +++ b/src/esp32/ciot_sys.c @@ -55,13 +55,13 @@ ciot_err_t ciot_sys_start(ciot_sys_t self, ciot_sys_cfg_t *cfg) event.msg = ciot_msg_get(CIOT__MSG_TYPE__MSG_TYPE_STATUS, &self->base.iface); ciot_iface_send_event(&base->iface, &event); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_stop(ciot_sys_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_sys_task(ciot_sys_t self) @@ -70,29 +70,29 @@ ciot_err_t ciot_sys_task(ciot_sys_t self) base->status.free_memory = esp_get_free_heap_size(); base->status.lifetime = ciot_timer_now() - self->init_time; xEventGroupWaitBits(self->event_group, CIOT_SYS_EVT_BIT_POOLING, pdTRUE, pdTRUE, pdMS_TO_TICKS(100)); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_set_event_bits(ciot_sys_t self, int event_bits) { CIOT_ERR_NULL_CHECK(self); xEventGroupSetBits(self->event_group, event_bits); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_sleep(long ms) { vTaskDelay(pdMS_TO_TICKS(ms)); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_restart(void) { esp_restart(); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_init_dfu(void) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } diff --git a/src/esp32/ciot_tcp.c b/src/esp32/ciot_tcp.c index 5fb307d8..c176304e 100644 --- a/src/esp32/ciot_tcp.c +++ b/src/esp32/ciot_tcp.c @@ -1,12 +1,12 @@ /** * @file ciot_tcp.c * @author your name (you@domain.com) - * @brief + * @brief * @version 0.1 * @date 2024-06-07 - * + * * @copyright Copyright (c) 2024 - * + * */ #include @@ -19,13 +19,30 @@ #include "ciot_msg.h" #include "ciot_err.h" +#define CIOT_ESP_NETIF_INHERENT_DEFAULT_ETH() \ + { \ + .get_ip_event = IP_EVENT_ETH_GOT_IP, \ + .lost_ip_event = 0, \ + .flags = ESP_NETIF_DHCP_SERVER | ESP_NETIF_FLAG_AUTOUP, \ + .ip_info = (esp_netif_ip_info_t *)&ciot_eth_ap_ip, \ + .if_key = "ETHDHCPS", \ + .if_desc = "eth", \ + .route_prio = 50} + +#define CIOT_NETIF_DEFAULT_ETH() \ + { \ + .base = &_g_ciot_esp_netif_inherent_eth_config, \ + .driver = NULL, \ + .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH, \ + } + struct ciot_tcp { ciot_tcp_base_t base; esp_netif_t *netif; }; -static ciot_err_t ciot_tcp_set_dhcp_cfg(ciot_tcp_t self, ciot_tcp_dhcp_cfg_t dhcp); +// static ciot_err_t ciot_tcp_set_dhcp_cfg(ciot_tcp_t self, ciot_tcp_dhcp_cfg_t dhcp); static ciot_err_t ciot_tcp_set_ip_cfg(ciot_tcp_t self, ciot_tcp_cfg_t *cfg); static void ciot_tcp_sta_event_handler(void *handler_args, esp_event_base_t event_base, int32_t event_id, void *event_data); static void ciot_tcp_ap_event_handler(void *handler_args, esp_event_base_t event_base, int32_t event_id, void *event_data); @@ -35,11 +52,19 @@ static void ciot_tcp_event_handler(void *handler_args, esp_event_base_t event_ba static bool tcp_init = false; static const char *TAG = "ciot_tcp"; +const esp_netif_ip_info_t ciot_eth_ap_ip = { + .ip = {.addr = PP_HTONL(LWIP_MAKEU32(192, 168, 1, 1))}, + .gw = {.addr = PP_HTONL(LWIP_MAKEU32(192, 168, 1, 1))}, + .netmask = {.addr = PP_HTONL(LWIP_MAKEU32(255, 255, 255, 0))}, +}; + +const esp_netif_inherent_config_t _g_ciot_esp_netif_inherent_eth_config = CIOT_ESP_NETIF_INHERENT_DEFAULT_ETH(); + ciot_tcp_t ciot_tcp_new(ciot_iface_t *iface, ciot_tcp_type_t type) { ciot_tcp_t self = calloc(1, sizeof(struct ciot_tcp)); ciot_tcp_init(self); - if(!tcp_init) + if (!tcp_init) { ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); @@ -54,10 +79,10 @@ ciot_err_t ciot_tcp_init_netif(ciot_tcp_t self) { CIOT_ERR_NULL_CHECK(self); - if(self->netif != NULL) + if (self->netif != NULL) { CIOT_LOGW(TAG, "Netif already created"); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } switch (self->base.type) @@ -85,30 +110,17 @@ ciot_err_t ciot_tcp_init_netif(ciot_tcp_t self) break; } default: - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } - return self->netif != NULL ? CIOT_ERR__OK : CIOT_ERR__FAIL; + return self->netif != NULL ? CIOT__ERR__OK : CIOT__ERR__FAIL; } ciot_err_t ciot_tcp_start(ciot_tcp_t self) { CIOT_ERR_NULL_CHECK(self); ciot_tcp_base_t *base = &self->base; - CIOT_ERR_RETURN(ciot_tcp_set_dhcp_cfg(self, base->cfg.dhcp)); - CIOT_ERR_RETURN(ciot_tcp_set_ip_cfg(self, &base->cfg)); - base->status.state = CIOT__TCP_STATE__TCP_STATE_STARTED; - return CIOT_ERR__OK; -} - -ciot_err_t ciot_tcp_stop(ciot_tcp_t self) -{ - CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__OK; -} - -static ciot_err_t ciot_tcp_set_dhcp_cfg(ciot_tcp_t self, ciot_tcp_dhcp_cfg_t dhcp) -{ + CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(self->netif); @@ -117,21 +129,21 @@ static ciot_err_t ciot_tcp_set_dhcp_cfg(ciot_tcp_t self, ciot_tcp_dhcp_cfg_t dhc esp_netif_dhcp_status_t dhcps = ESP_NETIF_DHCP_STOPPED; esp_netif_flags_t flags = esp_netif_get_flags(netif); - if(flags & ESP_NETIF_DHCP_CLIENT) + if (flags & ESP_NETIF_DHCP_CLIENT) { CIOT_ERR_PRINT(TAG, esp_netif_dhcpc_get_status(netif, &dhcpc)); } - if(flags & ESP_NETIF_DHCP_SERVER) + if (flags & ESP_NETIF_DHCP_SERVER) { CIOT_ERR_PRINT(TAG, esp_netif_dhcps_get_status(netif, &dhcps)); } - switch (dhcp) + switch (self->base.cfg.dhcp) { case CIOT__TCP_DHCP_CFG__TCP_DHCP_CFG_NO_CHANGE: ESP_LOGI(TAG, "CIOT_TCP_DHCP_CFG_NO_CHANGE"); - return CIOT_ERR__OK; + return CIOT__ERR__OK; case CIOT__TCP_DHCP_CFG__TCP_DHCP_CFG_CLIENT: ESP_LOGI(TAG, "CIOT_TCP_DHCP_CFG_CLIENT"); if (dhcps != ESP_NETIF_DHCP_STOPPED) @@ -154,7 +166,8 @@ static ciot_err_t ciot_tcp_set_dhcp_cfg(ciot_tcp_t self, ciot_tcp_dhcp_cfg_t dhc } if (dhcps != ESP_NETIF_DHCP_STARTED) { - CIOT_LOGI(TAG, "Stopping dhcp server"); + CIOT_LOGI(TAG, "Starting dhcp server"); + CIOT_ERR_RETURN(ciot_tcp_set_ip_cfg(self, &self->base.cfg)); CIOT_ERR_RETURN(esp_netif_dhcps_start(netif)); } break; @@ -170,23 +183,103 @@ static ciot_err_t ciot_tcp_set_dhcp_cfg(ciot_tcp_t self, ciot_tcp_dhcp_cfg_t dhc CIOT_LOGI(TAG, "Stopping dhcp server"); CIOT_ERR_RETURN(esp_netif_dhcps_stop(netif)); } + CIOT_ERR_RETURN(ciot_tcp_set_ip_cfg(self, &self->base.cfg)); break; default: - return CIOT_ERR__INVALID_ARG; + return CIOT__ERR__INVALID_ARG; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; + + base->status.state = CIOT__TCP_STATE__TCP_STATE_STARTED; + return CIOT__ERR__OK; +} + +ciot_err_t ciot_tcp_stop(ciot_tcp_t self) +{ + CIOT_ERR_NULL_CHECK(self); + return CIOT__ERR__OK; } +// static ciot_err_t ciot_tcp_set_dhcp_cfg(ciot_tcp_t self, ciot_tcp_dhcp_cfg_t dhcp) +// { +// CIOT_ERR_NULL_CHECK(self); +// CIOT_ERR_NULL_CHECK(self->netif); + +// esp_netif_t *netif = self->netif; +// esp_netif_dhcp_status_t dhcpc = ESP_NETIF_DHCP_STOPPED; +// esp_netif_dhcp_status_t dhcps = ESP_NETIF_DHCP_STOPPED; +// esp_netif_flags_t flags = esp_netif_get_flags(netif); + +// if (flags & ESP_NETIF_DHCP_CLIENT) +// { +// CIOT_ERR_PRINT(TAG, esp_netif_dhcpc_get_status(netif, &dhcpc)); +// } + +// if (flags & ESP_NETIF_DHCP_SERVER) +// { +// CIOT_ERR_PRINT(TAG, esp_netif_dhcps_get_status(netif, &dhcps)); +// } + +// switch (dhcp) +// { +// case CIOT__TCP_DHCP_CFG__TCP_DHCP_CFG_NO_CHANGE: +// ESP_LOGI(TAG, "CIOT_TCP_DHCP_CFG_NO_CHANGE"); +// return CIOT__ERR__OK; +// case CIOT__TCP_DHCP_CFG__TCP_DHCP_CFG_CLIENT: +// ESP_LOGI(TAG, "CIOT_TCP_DHCP_CFG_CLIENT"); +// if (dhcps != ESP_NETIF_DHCP_STOPPED) +// { +// CIOT_LOGI(TAG, "Stopping dhcp server"); +// CIOT_ERR_RETURN(esp_netif_dhcps_stop(netif)); +// } +// if (dhcpc != ESP_NETIF_DHCP_STARTED) +// { +// CIOT_LOGI(TAG, "Starting dhcp client"); +// CIOT_ERR_RETURN(esp_netif_dhcpc_start(netif)); +// } +// break; +// case CIOT__TCP_DHCP_CFG__TCP_DHCP_CFG_SERVER: +// ESP_LOGI(TAG, "CIOT_TCP_DHCP_CFG_SERVER"); +// if (dhcpc != ESP_NETIF_DHCP_STOPPED) +// { +// CIOT_LOGI(TAG, "Stopping dhcp client"); +// CIOT_ERR_RETURN(esp_netif_dhcpc_stop(netif)); +// } +// if (dhcps != ESP_NETIF_DHCP_STARTED) +// { +// CIOT_LOGI(TAG, "Starting dhcp server"); +// CIOT_ERR_RETURN(esp_netif_dhcps_start(netif)); +// } +// break; +// case CIOT__TCP_DHCP_CFG__TCP_DHCP_CFG_DISABLED: +// ESP_LOGI(TAG, "CIOT_TCP_DHCP_CFG_DISABLED"); +// if (dhcpc != ESP_NETIF_DHCP_STOPPED) +// { +// CIOT_LOGI(TAG, "Stopping dhcp client"); +// CIOT_ERR_RETURN(esp_netif_dhcpc_stop(netif)); +// } +// if (dhcps != ESP_NETIF_DHCP_STOPPED) +// { +// CIOT_LOGI(TAG, "Stopping dhcp server"); +// CIOT_ERR_RETURN(esp_netif_dhcps_stop(netif)); +// } +// break; +// default: +// return CIOT__ERR__INVALID_ARG; +// } +// return CIOT__ERR__OK; +// } + static ciot_err_t ciot_tcp_set_ip_cfg(ciot_tcp_t self, ciot_tcp_cfg_t *cfg) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); CIOT_ERR_NULL_CHECK(self->netif); - - if (cfg->dhcp == CIOT__TCP_DHCP_CFG__TCP_DHCP_CFG_DISABLED) + if (cfg->dhcp == CIOT__TCP_DHCP_CFG__TCP_DHCP_CFG_DISABLED || + cfg->dhcp == CIOT__TCP_DHCP_CFG__TCP_DHCP_CFG_SERVER) { - ESP_LOGI(TAG, "cfg:cfg: cfg:%d.%d.%d.%d gw:%d.%d.%d.%d mask:%d.%d.%d.%d dns:%d.%d.%d.%d", + ESP_LOGI(TAG, "cfg: ip:%d.%d.%d.%d gw:%d.%d.%d.%d mask:%d.%d.%d.%d dns:%d.%d.%d.%d", cfg->ip.data[0], cfg->ip.data[1], cfg->ip.data[2], cfg->ip.data[3], cfg->gateway.data[0], cfg->gateway.data[1], cfg->gateway.data[2], cfg->gateway.data[3], cfg->mask.data[0], cfg->mask.data[1], cfg->mask.data[2], cfg->mask.data[3], @@ -221,12 +314,12 @@ static ciot_err_t ciot_tcp_set_ip_cfg(ciot_tcp_t self, ciot_tcp_cfg_t *cfg) info->ip.data[2] = ip4_addr3(&ip_info.ip); info->ip.data[3] = ip4_addr4(&ip_info.ip); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static void ciot_tcp_sta_event_handler(void *handler_args, esp_event_base_t event_base, int32_t event_id, void *event_data) { - if(event_id == IP_EVENT_STA_GOT_IP || event_id == IP_EVENT_STA_LOST_IP) + if (event_id == IP_EVENT_STA_GOT_IP || event_id == IP_EVENT_STA_LOST_IP) { ciot_tcp_event_handler(handler_args, event_base, event_id, event_data); } @@ -234,7 +327,7 @@ static void ciot_tcp_sta_event_handler(void *handler_args, esp_event_base_t even static void ciot_tcp_ap_event_handler(void *handler_args, esp_event_base_t event_base, int32_t event_id, void *event_data) { - if(event_id == IP_EVENT_AP_STAIPASSIGNED) + if (event_id == IP_EVENT_AP_STAIPASSIGNED) { ciot_tcp_event_handler(handler_args, event_base, event_id, event_data); } @@ -242,7 +335,7 @@ static void ciot_tcp_ap_event_handler(void *handler_args, esp_event_base_t event static void ciot_tcp_eth_event_handler(void *handler_args, esp_event_base_t event_base, int32_t event_id, void *event_data) { - if(event_id == IP_EVENT_ETH_GOT_IP || event_id == IP_EVENT_ETH_LOST_IP) + if (event_id == IP_EVENT_ETH_GOT_IP || event_id == IP_EVENT_ETH_LOST_IP) { ciot_tcp_event_handler(handler_args, event_base, event_id, event_data); } @@ -260,7 +353,7 @@ static void ciot_tcp_event_handler(void *handler_args, esp_event_base_t event_ba { CIOT_LOGI(TAG, "IP_EVENT_GOT_IP"); - ip_event_got_ip_t *ip_event = (ip_event_got_ip_t*)event_data; + ip_event_got_ip_t *ip_event = (ip_event_got_ip_t *)event_data; esp_netif_dhcp_status_t dhcpc = ESP_NETIF_DHCP_STOPPED; esp_netif_dhcp_status_t dhcps = ESP_NETIF_DHCP_STOPPED; @@ -306,11 +399,12 @@ ciot_err_t ciot_tcp_get_ip(ciot_tcp_t self, uint8_t *ip) esp_netif_ip_info_t ip_info; esp_netif_get_ip_info(self->netif, &ip_info); memcpy(ip, &ip_info.ip.addr, sizeof(ip_info.ip.addr)); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } void *ciot_tcp_get_netif(ciot_tcp_t self) { - if(self == NULL) return NULL; + if (self == NULL) + return NULL; return self->netif; } diff --git a/src/esp32/ciot_uart.c b/src/esp32/ciot_uart.c index 47c3bd3f..dc0490bd 100644 --- a/src/esp32/ciot_uart.c +++ b/src/esp32/ciot_uart.c @@ -98,7 +98,7 @@ ciot_err_t ciot_uart_start(ciot_uart_t self, ciot_uart_cfg_t *cfg) event.msg = ciot_msg_get(CIOT__MSG_TYPE__MSG_TYPE_STATUS, &base->iface); ciot_iface_send_event(&base->iface, &event); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_uart_stop(ciot_uart_t self) @@ -106,7 +106,7 @@ ciot_err_t ciot_uart_stop(ciot_uart_t self) CIOT_ERR_NULL_CHECK(self); uart_driver_delete(self->base.cfg.num); vTaskDelete(self->task); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_uart_send_bytes(ciot_uart_t self, uint8_t *bytes, int size) @@ -114,13 +114,13 @@ ciot_err_t ciot_uart_send_bytes(ciot_uart_t self, uint8_t *bytes, int size) CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(bytes); uart_write_bytes(self->base.cfg.num, bytes, size); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_uart_task(ciot_uart_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static void ciot_uart0_task(void *args) @@ -181,7 +181,7 @@ static void ciot_uart_event_handler(ciot_uart_t self, uart_event_t *event) // uint8_t data[event->size]; // uart_read_bytes(base->cfg.num, data, event->size, portMAX_DELAY); // ciot_err_t err = ciot_iface_process_data(&base->iface, data, event->size); - // if(err != CIOT_ERR__OK) + // if(err != CIOT__ERR__OK) // { // CIOT_LOGI(TAG, "Process data error %s", ciot_err_to_message(err)); // } diff --git a/src/esp32/ciot_wifi.c b/src/esp32/ciot_wifi.c index 80f399ef..21fcd364 100644 --- a/src/esp32/ciot_wifi.c +++ b/src/esp32/ciot_wifi.c @@ -23,6 +23,7 @@ struct ciot_wifi { ciot_wifi_base_t base; + bool reconecting; }; static ciot_err_t ciot_wifi_start_sta(ciot_wifi_t self, ciot_wifi_cfg_t *cfg); @@ -100,7 +101,7 @@ ciot_err_t ciot_wifi_start(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) case CIOT__WIFI_TYPE__WIFI_TYPE_AP: return ciot_wifi_start_ap(self, cfg); default: - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } } @@ -130,7 +131,7 @@ static ciot_err_t ciot_wifi_start_sta(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) CIOT_ERR_PRINT(TAG, esp_wifi_connect()); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_wifi_start_ap(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) @@ -145,15 +146,20 @@ static ciot_err_t ciot_wifi_start_ap(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &conf)); ESP_ERROR_CHECK(esp_wifi_start()); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_wifi_stop(ciot_wifi_t self) { - CIOT_LOGI(TAG, "Stopping WiFi"); + CIOT_LOGI(TAG, "Stopping WiFi %s", ciot__tcp_state__descriptor.values[self->base.status.tcp->state].name); self->base.cfg.disabled = true; + if(self->base.status.tcp->state == CIOT__TCP_STATE__TCP_STATE_STOPPED) + { + ciot_iface_send_event_type(&self->base.iface, CIOT_IFACE_EVENT_STOPPED); + } + switch (self->base.cfg.type) { case CIOT__WIFI_TYPE__WIFI_TYPE_STA: @@ -163,20 +169,20 @@ ciot_err_t ciot_wifi_stop(ciot_wifi_t self) CIOT_LOGI(TAG, "Stopping access point"); return esp_wifi_set_mode(WIFI_MODE_STA); default: - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_wifi_task(ciot_wifi_t self) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_wifi_send_bytes(ciot_iface_t *iface, uint8_t *bytes, int size) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } static ciot_err_t ciot_wifi_set_cfg(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) @@ -191,7 +197,7 @@ static ciot_err_t ciot_wifi_set_cfg(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) if (self->base.cfg.type != cfg->type) { CIOT_LOGE(TAG, "Invalid wifi type: %s. Expected: %s", ciot__wifi_type__descriptor.values[cfg->type].name, ciot__wifi_type__descriptor.values[self->base.cfg.type].name); - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } base->cfg.disabled = cfg->disabled; @@ -205,7 +211,7 @@ static ciot_err_t ciot_wifi_set_cfg(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) CIOT_ERR_RETURN(ciot_tcp_set_cfg(base->tcp, cfg->tcp)); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static void ciot_wifi_ap_event_handler(void *handler_args, esp_event_base_t event_base, int32_t event_id, void *event_data) @@ -297,6 +303,7 @@ static void ciot_wifi_sta_event_handler(void *handler_args, esp_event_base_t eve base->info.ap->authmode = data->authmode; memcpy(base->info.ap->bssid.data, data->bssid, sizeof(data->bssid)); memcpy(base->info.ap->ssid, data->ssid, sizeof(data->ssid)); + self->reconecting = false; CIOT_ERR_PRINT(TAG, ciot_tcp_start(base->tcp)); break; } @@ -304,12 +311,18 @@ static void ciot_wifi_sta_event_handler(void *handler_args, esp_event_base_t eve { CIOT_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); wifi_event_sta_disconnected_t *data = (wifi_event_sta_disconnected_t *)event_data; - tcp->status.state = CIOT__TCP_STATE__TCP_STATE_DISCONNECTED; base->status.disconnect_reason = data->reason; base->info.ap->authmode = 0; memset(base->info.ap->bssid.data, 0, sizeof(base->info.ap->bssid)); memset(base->info.ap->ssid, 0, sizeof(base->info.ap->ssid)); ciot_iface_send_event_type(&self->base.iface, CIOT_IFACE_EVENT_STOPPED); + if(base->status.tcp->state == CIOT__TCP_STATE__TCP_STATE_CONNECTED || self->reconecting) + { + CIOT_LOGI(TAG, "Connection losted. Connecting again..."); + esp_wifi_connect(); + self->reconecting = true; + } + tcp->status.state = CIOT__TCP_STATE__TCP_STATE_DISCONNECTED; break; } default: diff --git a/src/mg/ciot_http_client.c b/src/mg/ciot_http_client.c index a7aebaab..283bb488 100644 --- a/src/mg/ciot_http_client.c +++ b/src/mg/ciot_http_client.c @@ -52,7 +52,7 @@ static ciot_err_t ciot_iface_process_req(ciot_iface_t *iface, ciot_msg_t *req) break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) @@ -68,10 +68,10 @@ static ciot_err_t ciot_iface_get_data(ciot_iface_t *iface, ciot_msg_t *msg) msg->data->http_client->status = &self->base.status; break; default: - return CIOT_ERR__NOT_FOUND; + return CIOT__ERR__NOT_FOUND; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_http_client_send_bytes(ciot_http_client_t self, uint8_t *data, int size) @@ -92,11 +92,11 @@ ciot_err_t ciot_http_client_send_bytes(ciot_http_client_t self, uint8_t *data, i self->base.send.data = calloc(1, size); memcpy(self->base.send.data, data, size); mg_http_connect(self->mgr, self->base.cfg.url, ciot_http_client_event_handler, self); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } else { - return CIOT_ERR__BUSY; + return CIOT__ERR__BUSY; } } @@ -111,13 +111,13 @@ ciot_err_t ciot_http_client_start(ciot_http_client_t self, ciot_http_client_cfg_ ciot_iface_send_event_type(&self->base.iface, CIOT_IFACE_EVENT_STARTED); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_http_client_stop(ciot_http_client_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static void ciot_http_client_event_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data) diff --git a/src/mg/ciot_http_server.c b/src/mg/ciot_http_server.c index b73fe9a9..bdde46c7 100644 --- a/src/mg/ciot_http_server.c +++ b/src/mg/ciot_http_server.c @@ -59,12 +59,12 @@ ciot_err_t ciot_http_server_start(ciot_http_server_t self, ciot_http_server_cfg_ if (self->conn_rx == NULL) { base->status.state = CIOT__HTTP_SERVER_STATE__HTTP_SERVER_STATE_ERROR; - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } else { - return CIOT_ERR__OK; + return CIOT__ERR__OK; } } @@ -73,7 +73,7 @@ ciot_err_t ciot_http_server_stop(ciot_http_server_t self) CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(self->conn_rx); mg_close_conn(self->conn_rx); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_http_server_send_bytes(ciot_http_server_t self, uint8_t *data, int size) @@ -84,7 +84,7 @@ ciot_err_t ciot_http_server_send_bytes(ciot_http_server_t self, uint8_t *data, i "Content-Length: %d\r\n\r\n", size); mg_send(self->conn_tx, data, size); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static bool check_method(struct mg_http_message *hm, const char* method) diff --git a/src/mg/ciot_mqtt_client.c b/src/mg/ciot_mqtt_client.c index 53bbf0c9..7b22368b 100644 --- a/src/mg/ciot_mqtt_client.c +++ b/src/mg/ciot_mqtt_client.c @@ -78,13 +78,13 @@ ciot_err_t ciot_mqtt_client_start(ciot_mqtt_client_t self, ciot_mqtt_client_cfg_ self->connection = mg_mqtt_connect(self->mgr, base->url, &opts, ciot_mqtt_client_event_handler, self); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_mqtt_client_stop(ciot_mqtt_client_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_mqtt_client_sub(ciot_mqtt_client_t self, char *topic, int qos) @@ -97,7 +97,7 @@ ciot_err_t ciot_mqtt_client_sub(ciot_mqtt_client_t self, char *topic, int qos) opts.topic = mg_str(topic); opts.qos = qos; mg_mqtt_sub(self->connection, &opts); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_mqtt_client_pub(ciot_mqtt_client_t self, char *topic, uint8_t *data, int size, int qos) @@ -105,7 +105,7 @@ ciot_err_t ciot_mqtt_client_pub(ciot_mqtt_client_t self, char *topic, uint8_t *d CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(topic); CIOT_ERR_NULL_CHECK(self->connection); - CIOT_ERR_VALUE_CHECK(self->base.status.state, CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTED, CIOT_ERR__INVALID_STATE); + CIOT_ERR_VALUE_CHECK(self->base.status.state, CIOT__MQTT_CLIENT_STATE__MQTT_STATE_CONNECTED, CIOT__ERR__INVALID_STATE); CIOT_ERR_EMPTY_STRING_CHECK(topic); struct mg_mqtt_opts opts = {0}; struct mg_str msg = {0}; @@ -117,7 +117,7 @@ ciot_err_t ciot_mqtt_client_pub(ciot_mqtt_client_t self, char *topic, uint8_t *d opts.retain = false; mg_mqtt_pub(self->connection, &opts); ciot_mqtt_client_update_data_rate(self, size); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static void ciot_mqtt_client_event_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data) diff --git a/src/mg/ciot_socket.c b/src/mg/ciot_socket.c index fe5bac1a..7e5e6f8e 100644 --- a/src/mg/ciot_socket.c +++ b/src/mg/ciot_socket.c @@ -62,13 +62,13 @@ ciot_err_t ciot_socket_start(ciot_socket_t self, ciot_socket_cfg_t *cfg) return ciot_socket_client_start(self); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_socket_stop(ciot_socket_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_socket_send_bytes(ciot_socket_t self, uint8_t *data, int size) @@ -77,7 +77,7 @@ ciot_err_t ciot_socket_send_bytes(ciot_socket_t self, uint8_t *data, int size) struct mg_connection *conn = self->resp_conn ? self->resp_conn : self->conn; mg_send(conn, data, size); self->resp_conn = NULL; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_socket_server_start(ciot_socket_t self) @@ -88,9 +88,9 @@ static ciot_err_t ciot_socket_server_start(ciot_socket_t self) if(self->conn == NULL) { base->status.state = CIOT__SOCKET_STATE__SOCKET_STATE_ERROR; - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static ciot_err_t ciot_socket_client_start(ciot_socket_t self) @@ -101,9 +101,9 @@ static ciot_err_t ciot_socket_client_start(ciot_socket_t self) if(self->conn == NULL) { base->status.state = CIOT__SOCKET_STATE__SOCKET_STATE_ERROR; - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } static void ciot_socket_event_handler(struct mg_connection *c, int ev, void *ev_data, void *fn_data) diff --git a/src/mg/ciot_sys.c b/src/mg/ciot_sys.c index fc2a1a8e..310c4c18 100644 --- a/src/mg/ciot_sys.c +++ b/src/mg/ciot_sys.c @@ -53,7 +53,7 @@ ciot_err_t ciot_sys_stop(ciot_sys_t self) { CIOT_ERR_NULL_CHECK(self); exit(0); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_task(ciot_sys_t self) @@ -61,12 +61,12 @@ ciot_err_t ciot_sys_task(ciot_sys_t self) CIOT_ERR_NULL_CHECK(self); sys->base.status.free_memory = ciot_sys_get_free_ram(); sys->base.status.lifetime = ciot_timer_now() - sys->init_time; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_set_event_bits(ciot_sys_t self, int event_bits) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_sys_sleep(long ms) @@ -74,9 +74,9 @@ ciot_err_t ciot_sys_sleep(long ms) #if MG_ARCH == MG_ARCH_WIN32 Sleep(ms); #else - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; #endif - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_restart(void) @@ -89,12 +89,12 @@ ciot_err_t ciot_sys_restart(void) exit(0); CIOT_LOGE(TAG, "Restart error"); #endif - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_sys_init_dfu(void) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } #if defined(CIOT_TARGET_ESP8266) diff --git a/src/nrf/ciot_ble.c b/src/nrf/ciot_ble.c index a9fc72d9..a9d27b44 100644 --- a/src/nrf/ciot_ble.c +++ b/src/nrf/ciot_ble.c @@ -80,18 +80,18 @@ ciot_err_t ciot_ble_start(ciot_ble_t self, ciot_ble_cfg_t *cfg) iface_event.msg = ciot_msg_get(CIOT__MSG_TYPE__MSG_TYPE_STATUS, &base->iface); ciot_iface_send_event(&base->iface, &iface_event); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ble_stop(ciot_ble_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_ble_task(ciot_ble_t self) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_ble_set_mac(ciot_ble_t self, uint8_t mac[6]) @@ -103,7 +103,7 @@ ciot_err_t ciot_ble_set_mac(ciot_ble_t self, uint8_t mac[6]) if (ciot_ble_mac_is_valid(mac) == false) { - return CIOT_ERR__INVALID_ARG; + return CIOT__ERR__INVALID_ARG; } uint32_t err = 0; @@ -156,7 +156,7 @@ ciot_err_t ciot_ble_get_mac(ciot_ble_t self, ciot_ble_mac_type_t type, uint8_t m break; } default: - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } return err; diff --git a/src/nrf/ciot_ble_adv.c b/src/nrf/ciot_ble_adv.c index fd8c9d8c..e704db20 100644 --- a/src/nrf/ciot_ble_adv.c +++ b/src/nrf/ciot_ble_adv.c @@ -75,7 +75,7 @@ ciot_err_t ciot_ble_adv_start(ciot_ble_adv_t self, ciot_ble_adv_cfg_t *cfg) uint32_t err_code = sd_ble_gap_tx_power_set(cfg->tx_power); if (err_code) { - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } #else @@ -88,19 +88,19 @@ ciot_err_t ciot_ble_adv_start(ciot_ble_adv_t self, ciot_ble_adv_cfg_t *cfg) uint32_t err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_str, &m_adv_params); if (err_code) { - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, m_adv_handle, cfg->tx_power); if (err_code) { - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_SCAN_INIT, 0, cfg->tx_power); if (err_code) { - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } #endif @@ -115,7 +115,7 @@ ciot_err_t ciot_ble_adv_start(ciot_ble_adv_t self, ciot_ble_adv_cfg_t *cfg) ciot_err_t ciot_ble_adv_stop(ciot_ble_adv_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_ble_adv_send_data(ciot_ble_adv_t self, uint8_t *data, int size) @@ -137,10 +137,10 @@ ciot_err_t ciot_ble_adv_send_data(ciot_ble_adv_t self, uint8_t *data, int size) if (self->base.status.err_code == NRF_SUCCESS) { self->base.status.state = CIOT__BLE_ADV_STATE__BLE_ADV_STATE_SENDING; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } ciot_err_t ciot_ble_adv_handle_event(ciot_ble_adv_t self, void *event, void *event_args) @@ -158,5 +158,5 @@ ciot_err_t ciot_ble_adv_handle_event(ciot_ble_adv_t self, void *event, void *eve break; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/nrf/ciot_ble_scn.c b/src/nrf/ciot_ble_scn.c index 941bcd1b..4829e301 100644 --- a/src/nrf/ciot_ble_scn.c +++ b/src/nrf/ciot_ble_scn.c @@ -39,7 +39,7 @@ struct ciot_ble_scn static void ciot_ble_scn_copy_mac(uint8_t destiny[6], uint8_t source[6], bool reverse); static void ciot_ble_scn_handle_adv_report(ciot_ble_scn_t self, ciot_ble_scn_adv_t *adv); -static const char *TAG = "hg_ble_scn"; +// static const char *TAG = "hg_ble_scn"; ciot_ble_scn_t ciot_ble_scn_new(void *handle) { @@ -61,12 +61,12 @@ ciot_err_t ciot_ble_scn_start(ciot_ble_scn_t self, ciot_ble_scn_cfg_t *cfg) if (base->cfg.active && base->status.state == CIOT__BLE_SCN_STATE__BLE_SCN_STATE_ACTIVE) { - return CIOT_ERR__OK; + return CIOT__ERR__OK; } if (!base->cfg.active && base->status.state == CIOT__BLE_SCN_STATE__BLE_SCN_STATE_PASSIVE) { - return CIOT_ERR__OK; + return CIOT__ERR__OK; } uint32_t err = sd_ble_gap_scan_stop(); @@ -113,13 +113,13 @@ ciot_err_t ciot_ble_scn_stop(ciot_ble_scn_t self) iface_event.msg = ciot_msg_get(CIOT__MSG_TYPE__MSG_TYPE_STATUS, &base->iface); ciot_iface_send_event(&base->iface, &iface_event); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ble_scn_task(ciot_ble_scn_t self) { ciot_ble_scn_base_task(self); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_ble_scn_handle_event(ciot_ble_scn_t self, void *event, void *event_args) @@ -131,7 +131,6 @@ ciot_err_t ciot_ble_scn_handle_event(ciot_ble_scn_t self, void *event, void *eve const ble_evt_t *ev = event; uint8_t mac[6]; - switch (ev->header.evt_id) { case BLE_GAP_EVT_ADV_REPORT: @@ -139,6 +138,7 @@ ciot_err_t ciot_ble_scn_handle_event(ciot_ble_scn_t self, void *event, void *eve base->recv.info->mac.data = mac; base->recv.info->mac.len = sizeof(mac); base->recv.info->rssi = ev->evt.gap_evt.params.adv_report.rssi; + base->status.advs_count++; #if NRF_SD_BLE_API_VERSION == 2 || NRF_SD_BLE_API_VERSION == 3 base->recv.adv.data = (ciot_iface_event_data_u *)ev->evt.gap_evt.params.adv_report.data; base->recv.adv.len = ev->evt.gap_evt.params.adv_report.dlen; @@ -151,15 +151,18 @@ ciot_err_t ciot_ble_scn_handle_event(ciot_ble_scn_t self, void *event, void *eve { ciot_ble_scn_handle_adv_report(self, &base->recv); } - break; - case BLE_GAP_EVT_TIMEOUT: - CIOT_LOGI(TAG, "%d", ev->evt.gap_evt.params.timeout.src); + else + { + base->status.advs_filtered++; + } break; default: break; } - return CIOT_ERR__NOT_IMPLEMENTED; + base->status.last_event = ev->header.evt_id; + + return CIOT__ERR__OK; } static void ciot_ble_scn_copy_mac(uint8_t destiny[6], uint8_t source[6], bool reverse) @@ -195,7 +198,7 @@ static void ciot_ble_scn_handle_adv_report(ciot_ble_scn_t self, ciot_ble_scn_adv } else { - base->status.err_code = CIOT_ERR__DATA_LOSS; + base->status.err_code = CIOT__ERR__DATA_LOSS; } #else ciot_iface_event_t event = {0}; diff --git a/src/nrf/ciot_gpio.c b/src/nrf/ciot_gpio.c index 05f9a204..132dc5f6 100644 --- a/src/nrf/ciot_gpio.c +++ b/src/nrf/ciot_gpio.c @@ -1,12 +1,12 @@ /** * @file ciot_gpio.c * @author your name (you@domain.com) - * @brief + * @brief * @version 0.1 * @date 2024-06-07 - * + * * @copyright Copyright (c) 2024 - * + * */ #include @@ -15,15 +15,20 @@ // static const char *TAG = "ciot_gpio"; +static void set_bit(int bit_position, int value); +static int get_bit(int bit_position); +static uint64_t states_mask; struct ciot_gpio { ciot_gpio_base_t base; }; -ciot_gpio_t ciot_gpio_new(void *handle) +ciot_gpio_t ciot_gpio_new(void *handle, int count) { ciot_gpio_t self = calloc(1, sizeof(struct ciot_gpio)); - ciot_gpio_init(self); + ciot_gpio_init(self, count); + self->base.set_state = (ciot_hal_gpio_set_state_fn*)set_bit; + self->base.get_state = get_bit; return self; } @@ -31,11 +36,28 @@ ciot_err_t ciot_gpio_start(ciot_gpio_t self, ciot_gpio_cfg_t *cfg) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); - return CIOT_ERR__NOT_IMPLEMENTED; + return ciot_gpio_set_cfg(self, cfg); } ciot_err_t ciot_gpio_stop(ciot_gpio_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_SUPPORTED; +} + +static void set_bit(int bit_position, int value) +{ + if (value) + { + states_mask |= (1ULL << bit_position); + } + else + { + states_mask &= ~(1ULL << bit_position); + } +} + +static int get_bit(int bit_position) +{ + return (states_mask >> bit_position) & 1ULL; } diff --git a/src/nrf/ciot_sys.c b/src/nrf/ciot_sys.c index 9d4905d9..fedc90ce 100644 --- a/src/nrf/ciot_sys.c +++ b/src/nrf/ciot_sys.c @@ -37,13 +37,13 @@ ciot_err_t ciot_sys_start(ciot_sys_t self, ciot_sys_cfg_t *cfg) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_sys_stop(ciot_sys_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_sys_task(ciot_sys_t self) @@ -58,13 +58,13 @@ ciot_err_t ciot_sys_task(ciot_sys_t self) nrf_pwr_mgmt_run(); #endif } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_restart(void) { sd_nvic_SystemReset(); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_sys_init_dfu(void) diff --git a/src/nrf/ciot_uart.c b/src/nrf/ciot_uart.c index 5d87bf62..f44da79c 100644 --- a/src/nrf/ciot_uart.c +++ b/src/nrf/ciot_uart.c @@ -87,7 +87,7 @@ ciot_err_t ciot_uart_start(ciot_uart_t self, ciot_uart_cfg_t *cfg) } #endif default: - return CIOT_ERR__INVALID_ARG; + return CIOT__ERR__INVALID_ARG; } err_code = nrf_drv_uart_init(&self->handle, &config, ciot_uart_event_handler); @@ -105,7 +105,7 @@ ciot_err_t ciot_uart_start(ciot_uart_t self, ciot_uart_cfg_t *cfg) base->status.state = CIOT__UART_STATE__UART_STATE_STARTED; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_uart_stop(ciot_uart_t self) @@ -113,7 +113,7 @@ ciot_err_t ciot_uart_stop(ciot_uart_t self) CIOT_ERR_NULL_CHECK(self); nrf_drv_uart_uninit(&self->handle); self->base.status.state = CIOT__UART_STATE__UART_STATE_CLOSED; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_uart_send_bytes(ciot_uart_t self, uint8_t *bytes, int size) @@ -125,8 +125,8 @@ ciot_err_t ciot_uart_send_bytes(ciot_uart_t self, uint8_t *bytes, int size) uint32_t len = 0; app_fifo_write(&self->fifo.tx, NULL, &len); - err_code = len < size ? CIOT__UART_ERROR__UART_ERR_FIFO_OVERFLOW : CIOT_ERR__OK; - if(err_code == CIOT_ERR__OK) + err_code = len < size ? CIOT__UART_ERROR__UART_ERR_FIFO_OVERFLOW : CIOT__ERR__OK; + if(err_code == CIOT__ERR__OK) { len = size; err_code = app_fifo_write(&self->fifo.tx, bytes, &len); @@ -148,7 +148,7 @@ ciot_err_t ciot_uart_send_bytes(ciot_uart_t self, uint8_t *bytes, int size) ciot_err_t ciot_uart_task(ciot_uart_t self) { - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } static void ciot_uart_event_handler(nrf_drv_uart_event_t *event, void *args) @@ -171,7 +171,7 @@ static void ciot_uart_event_handler(nrf_drv_uart_event_t *event, void *args) break; } ciot_err_t err = ciot_iface_process_data(&self->base.iface, event->data.rxtx.p_data, event->data.rxtx.bytes); - if(err != CIOT_ERR__OK) + if(err != CIOT__ERR__OK) { base->status.error = err; } diff --git a/src/nrf/ciot_usb.c b/src/nrf/ciot_usb.c index c53e2a84..2214cded 100644 --- a/src/nrf/ciot_usb.c +++ b/src/nrf/ciot_usb.c @@ -124,20 +124,20 @@ ciot_err_t ciot_usb_start(ciot_usb_t self, ciot_usb_cfg_t *cfg) event.msg = ciot_msg_get(CIOT__MSG_TYPE__MSG_TYPE_STATUS, &self->base.iface); ciot_iface_send_event(&self->base.iface, &event); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_usb_stop(ciot_usb_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_usb_task(ciot_usb_t self) { CIOT_ERR_NULL_CHECK(self); app_usbd_event_queue_process(); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_usb_send_bytes(ciot_iface_t *iface, uint8_t *bytes, int size) @@ -151,12 +151,12 @@ ciot_err_t ciot_usb_send_bytes(ciot_iface_t *iface, uint8_t *bytes, int size) if(self->base.status.state != CIOT__USB_STATE__USB_STATE_STARTED) { - return CIOT_ERR__INVALID_STATE; + return CIOT__ERR__INVALID_STATE; } err_code = app_fifo_write(&self->fifo.tx, NULL, &len); - err_code = len < size ? CIOT_ERR__OVERFLOW : CIOT_ERR__OK; - if(err_code == CIOT_ERR__OK) + err_code = len < size ? CIOT__ERR__OVERFLOW : CIOT__ERR__OK; + if(err_code == CIOT__ERR__OK) { len = size; err_code = app_fifo_write(&self->fifo.tx, bytes, &len); diff --git a/src/proto/ciot/proto/v1/ble_scn.pb-c.c b/src/proto/ciot/proto/v1/ble_scn.pb-c.c index abacf7de..03d68a6f 100644 --- a/src/proto/ciot/proto/v1/ble_scn.pb-c.c +++ b/src/proto/ciot/proto/v1/ble_scn.pb-c.c @@ -469,7 +469,7 @@ const ProtobufCMessageDescriptor ciot__ble_scn_adv__descriptor = (ProtobufCMessageInit) ciot__ble_scn_adv__init, NULL,NULL,NULL /* reserved[123] */ }; -static const ProtobufCFieldDescriptor ciot__ble_scn_status__field_descriptors[5] = +static const ProtobufCFieldDescriptor ciot__ble_scn_status__field_descriptors[7] = { { "state", @@ -484,35 +484,59 @@ static const ProtobufCFieldDescriptor ciot__ble_scn_status__field_descriptors[5] 0,NULL,NULL /* reserved1,reserved2, etc */ }, { - "adv_info", + "err_code", 2, PROTOBUF_C_LABEL_NONE, - PROTOBUF_C_TYPE_MESSAGE, + PROTOBUF_C_TYPE_INT32, 0, /* quantifier_offset */ - offsetof(Ciot__BleScnStatus, adv_info), - &ciot__ble_scn_adv_info__descriptor, + offsetof(Ciot__BleScnStatus, err_code), + NULL, NULL, 0, /* flags */ 0,NULL,NULL /* reserved1,reserved2, etc */ }, { - "err_code", + "advs_count", 3, PROTOBUF_C_LABEL_NONE, PROTOBUF_C_TYPE_INT32, 0, /* quantifier_offset */ - offsetof(Ciot__BleScnStatus, err_code), + offsetof(Ciot__BleScnStatus, advs_count), NULL, NULL, 0, /* flags */ 0,NULL,NULL /* reserved1,reserved2, etc */ }, { - "fifo_len", + "advs_filtered", 4, PROTOBUF_C_LABEL_NONE, PROTOBUF_C_TYPE_INT32, 0, /* quantifier_offset */ + offsetof(Ciot__BleScnStatus, advs_filtered), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "last_event", + 5, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_INT32, + 0, /* quantifier_offset */ + offsetof(Ciot__BleScnStatus, last_event), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "fifo_len", + 6, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_INT32, + 0, /* quantifier_offset */ offsetof(Ciot__BleScnStatus, fifo_len), NULL, NULL, @@ -521,7 +545,7 @@ static const ProtobufCFieldDescriptor ciot__ble_scn_status__field_descriptors[5] }, { "fifo_max", - 5, + 7, PROTOBUF_C_LABEL_NONE, PROTOBUF_C_TYPE_INT32, 0, /* quantifier_offset */ @@ -533,16 +557,18 @@ static const ProtobufCFieldDescriptor ciot__ble_scn_status__field_descriptors[5] }, }; static const unsigned ciot__ble_scn_status__field_indices_by_name[] = { - 1, /* field[1] = adv_info */ - 2, /* field[2] = err_code */ - 3, /* field[3] = fifo_len */ - 4, /* field[4] = fifo_max */ + 2, /* field[2] = advs_count */ + 3, /* field[3] = advs_filtered */ + 1, /* field[1] = err_code */ + 5, /* field[5] = fifo_len */ + 6, /* field[6] = fifo_max */ + 4, /* field[4] = last_event */ 0, /* field[0] = state */ }; static const ProtobufCIntRange ciot__ble_scn_status__number_ranges[1 + 1] = { { 1, 0 }, - { 0, 5 } + { 0, 7 } }; const ProtobufCMessageDescriptor ciot__ble_scn_status__descriptor = { @@ -552,7 +578,7 @@ const ProtobufCMessageDescriptor ciot__ble_scn_status__descriptor = "Ciot__BleScnStatus", "Ciot", sizeof(Ciot__BleScnStatus), - 5, + 7, ciot__ble_scn_status__field_descriptors, ciot__ble_scn_status__field_indices_by_name, 1, ciot__ble_scn_status__number_ranges, diff --git a/src/proto/ciot/proto/v1/ble_scn.pb-c.h b/src/proto/ciot/proto/v1/ble_scn.pb-c.h index ec09f166..969bfefb 100644 --- a/src/proto/ciot/proto/v1/ble_scn.pb-c.h +++ b/src/proto/ciot/proto/v1/ble_scn.pb-c.h @@ -140,14 +140,22 @@ struct _Ciot__BleScnStatus * Current state */ Ciot__BleScnState state; - /* - * Last advertisement information - */ - Ciot__BleScnAdvInfo *adv_info; /* * Current error code */ int32_t err_code; + /* + * ADVs counter + */ + int32_t advs_count; + /* + * ADVs filtered + */ + int32_t advs_filtered; + /* + * Last event received + */ + int32_t last_event; /* * Current fifo lenght */ @@ -159,7 +167,7 @@ struct _Ciot__BleScnStatus }; #define CIOT__BLE_SCN_STATUS__INIT \ { PROTOBUF_C_MESSAGE_INIT (&ciot__ble_scn_status__descriptor) \ - , CIOT__BLE_SCN_STATE__BLE_SCN_STATE_IDLE, NULL, 0, 0, 0 } + , CIOT__BLE_SCN_STATE__BLE_SCN_STATE_IDLE, 0, 0, 0, 0, 0, 0 } /* diff --git a/src/proto/ciot/proto/v1/dfu.pb-c.c b/src/proto/ciot/proto/v1/dfu.pb-c.c index 2fe0b86c..11a10739 100644 --- a/src/proto/ciot/proto/v1/dfu.pb-c.c +++ b/src/proto/ciot/proto/v1/dfu.pb-c.c @@ -258,7 +258,7 @@ static const ProtobufCFieldDescriptor ciot__dfu_status__field_descriptors[5] = PROTOBUF_C_TYPE_ENUM, 0, /* quantifier_offset */ offsetof(Ciot__DfuStatus, error), - &ciot_err__descriptor, + &ciot__err__descriptor, NULL, 0, /* flags */ 0,NULL,NULL /* reserved1,reserved2, etc */ diff --git a/src/proto/ciot/proto/v1/dfu.pb-c.h b/src/proto/ciot/proto/v1/dfu.pb-c.h index 07516457..f838e546 100644 --- a/src/proto/ciot/proto/v1/dfu.pb-c.h +++ b/src/proto/ciot/proto/v1/dfu.pb-c.h @@ -119,7 +119,7 @@ struct _Ciot__DfuStatus /* * Dfu error code */ - CiotErr error; + Ciot__Err error; /* * Dfu image size */ @@ -131,7 +131,7 @@ struct _Ciot__DfuStatus }; #define CIOT__DFU_STATUS__INIT \ { PROTOBUF_C_MESSAGE_INIT (&ciot__dfu_status__descriptor) \ - , CIOT__DFU_STATE__DFU_STATE_IDLE, 0, CIOT_ERR__OK, 0, 0 } + , CIOT__DFU_STATE__DFU_STATE_IDLE, 0, CIOT__ERR__OK, 0, 0 } /* diff --git a/src/proto/ciot/proto/v1/errors.pb-c.c b/src/proto/ciot/proto/v1/errors.pb-c.c index 40303943..8d281094 100644 --- a/src/proto/ciot/proto/v1/errors.pb-c.c +++ b/src/proto/ciot/proto/v1/errors.pb-c.c @@ -7,46 +7,46 @@ #endif #include "ciot/proto/v1/errors.pb-c.h" -static const ProtobufCEnumValue ciot_err__enum_values_by_number[33] = +static const ProtobufCEnumValue ciot__err__enum_values_by_number[33] = { - { "OK", "CIOT_ERR__OK", 0 }, - { "NULL_ARG", "CIOT_ERR__NULL_ARG", 1 }, - { "INVALID_ID", "CIOT_ERR__INVALID_ID", 2 }, - { "INVALID_TYPE", "CIOT_ERR__INVALID_TYPE", 3 }, - { "OVERFLOW", "CIOT_ERR__OVERFLOW", 4 }, - { "NOT_IMPLEMENTED", "CIOT_ERR__NOT_IMPLEMENTED", 5 }, - { "NOT_SUPPORTED", "CIOT_ERR__NOT_SUPPORTED", 6 }, - { "BUSY", "CIOT_ERR__BUSY", 7 }, - { "INVALID_STATE", "CIOT_ERR__INVALID_STATE", 8 }, - { "SERIALIZATION", "CIOT_ERR__SERIALIZATION", 9 }, - { "DESERIALIZATION", "CIOT_ERR__DESERIALIZATION", 10 }, - { "SEND_DATA", "CIOT_ERR__SEND_DATA", 11 }, - { "RECV_DATA", "CIOT_ERR__RECV_DATA", 12 }, - { "INVALID_SIZE", "CIOT_ERR__INVALID_SIZE", 13 }, - { "CLOSED", "CIOT_ERR__CLOSED", 14 }, - { "NOT_FOUND", "CIOT_ERR__NOT_FOUND", 15 }, - { "VALIDATION_FAILED", "CIOT_ERR__VALIDATION_FAILED", 16 }, - { "CONNECTION", "CIOT_ERR__CONNECTION", 17 }, - { "DISCONNECTION", "CIOT_ERR__DISCONNECTION", 18 }, - { "EXCEPTION", "CIOT_ERR__EXCEPTION", 19 }, - { "INVALID_ARG", "CIOT_ERR__INVALID_ARG", 20 }, - { "NO_MEMORY", "CIOT_ERR__NO_MEMORY", 21 }, - { "TIMEOUT", "CIOT_ERR__TIMEOUT", 22 }, - { "MONGOOSE", "CIOT_ERR__MONGOOSE", 23 }, - { "IMPOSSIBLE_OP", "CIOT_ERR__IMPOSSIBLE_OP", 24 }, - { "CHECKSUM", "CIOT_ERR__CHECKSUM", 25 }, - { "PROTOCOL_VIOLATION", "CIOT_ERR__PROTOCOL_VIOLATION", 26 }, - { "DATA_LOSS", "CIOT_ERR__DATA_LOSS", 27 }, - { "INVALID_HEADER", "CIOT_ERR__INVALID_HEADER", 28 }, - { "DISABLED", "CIOT_ERR__DISABLED", 29 }, - { "FAIL", "CIOT_ERR__FAIL", 30 }, - { "INVALID_INDEX", "CIOT_ERR__INVALID_INDEX", 31 }, - { "INVALID_KEY_SIZE", "CIOT_ERR__INVALID_KEY_SIZE", 32 }, + { "OK", "CIOT__ERR__OK", 0 }, + { "NULL_ARG", "CIOT__ERR__NULL_ARG", 1 }, + { "INVALID_ID", "CIOT__ERR__INVALID_ID", 2 }, + { "INVALID_TYPE", "CIOT__ERR__INVALID_TYPE", 3 }, + { "OVERFLOW", "CIOT__ERR__OVERFLOW", 4 }, + { "NOT_IMPLEMENTED", "CIOT__ERR__NOT_IMPLEMENTED", 5 }, + { "NOT_SUPPORTED", "CIOT__ERR__NOT_SUPPORTED", 6 }, + { "BUSY", "CIOT__ERR__BUSY", 7 }, + { "INVALID_STATE", "CIOT__ERR__INVALID_STATE", 8 }, + { "SERIALIZATION", "CIOT__ERR__SERIALIZATION", 9 }, + { "DESERIALIZATION", "CIOT__ERR__DESERIALIZATION", 10 }, + { "SEND_DATA", "CIOT__ERR__SEND_DATA", 11 }, + { "RECV_DATA", "CIOT__ERR__RECV_DATA", 12 }, + { "INVALID_SIZE", "CIOT__ERR__INVALID_SIZE", 13 }, + { "CLOSED", "CIOT__ERR__CLOSED", 14 }, + { "NOT_FOUND", "CIOT__ERR__NOT_FOUND", 15 }, + { "VALIDATION_FAILED", "CIOT__ERR__VALIDATION_FAILED", 16 }, + { "CONNECTION", "CIOT__ERR__CONNECTION", 17 }, + { "DISCONNECTION", "CIOT__ERR__DISCONNECTION", 18 }, + { "EXCEPTION", "CIOT__ERR__EXCEPTION", 19 }, + { "INVALID_ARG", "CIOT__ERR__INVALID_ARG", 20 }, + { "NO_MEMORY", "CIOT__ERR__NO_MEMORY", 21 }, + { "TIMEOUT", "CIOT__ERR__TIMEOUT", 22 }, + { "MONGOOSE", "CIOT__ERR__MONGOOSE", 23 }, + { "IMPOSSIBLE_OP", "CIOT__ERR__IMPOSSIBLE_OP", 24 }, + { "CHECKSUM", "CIOT__ERR__CHECKSUM", 25 }, + { "PROTOCOL_VIOLATION", "CIOT__ERR__PROTOCOL_VIOLATION", 26 }, + { "DATA_LOSS", "CIOT__ERR__DATA_LOSS", 27 }, + { "INVALID_HEADER", "CIOT__ERR__INVALID_HEADER", 28 }, + { "DISABLED", "CIOT__ERR__DISABLED", 29 }, + { "FAIL", "CIOT__ERR__FAIL", 30 }, + { "INVALID_INDEX", "CIOT__ERR__INVALID_INDEX", 31 }, + { "INVALID_KEY_SIZE", "CIOT__ERR__INVALID_KEY_SIZE", 32 }, }; -static const ProtobufCIntRange ciot_err__value_ranges[] = { +static const ProtobufCIntRange ciot__err__value_ranges[] = { {0, 0},{0, 33} }; -static const ProtobufCEnumValueIndex ciot_err__enum_values_by_name[33] = +static const ProtobufCEnumValueIndex ciot__err__enum_values_by_name[33] = { { "BUSY", 7 }, { "CHECKSUM", 25 }, @@ -82,18 +82,18 @@ static const ProtobufCEnumValueIndex ciot_err__enum_values_by_name[33] = { "TIMEOUT", 22 }, { "VALIDATION_FAILED", 16 }, }; -const ProtobufCEnumDescriptor ciot_err__descriptor = +const ProtobufCEnumDescriptor ciot__err__descriptor = { PROTOBUF_C__ENUM_DESCRIPTOR_MAGIC, - "CiotErr", - "CiotErr", - "CiotErr", - "", + "Ciot.Err", + "Err", + "Ciot__Err", + "Ciot", 33, - ciot_err__enum_values_by_number, + ciot__err__enum_values_by_number, 33, - ciot_err__enum_values_by_name, + ciot__err__enum_values_by_name, 1, - ciot_err__value_ranges, + ciot__err__value_ranges, NULL,NULL,NULL,NULL /* reserved[1234] */ }; diff --git a/src/proto/ciot/proto/v1/errors.pb-c.h b/src/proto/ciot/proto/v1/errors.pb-c.h index da4efaa6..58c64d53 100644 --- a/src/proto/ciot/proto/v1/errors.pb-c.h +++ b/src/proto/ciot/proto/v1/errors.pb-c.h @@ -22,141 +22,141 @@ PROTOBUF_C__BEGIN_DECLS /* * Error codes */ -typedef enum _CiotErr { +typedef enum _Ciot__Err { /* * No error */ - CIOT_ERR__OK = 0, + CIOT__ERR__OK = 0, /* * Null argunment */ - CIOT_ERR__NULL_ARG = 1, + CIOT__ERR__NULL_ARG = 1, /* * Invalid ID */ - CIOT_ERR__INVALID_ID = 2, + CIOT__ERR__INVALID_ID = 2, /* * Invalid Type */ - CIOT_ERR__INVALID_TYPE = 3, + CIOT__ERR__INVALID_TYPE = 3, /* * Memory overflow */ - CIOT_ERR__OVERFLOW = 4, + CIOT__ERR__OVERFLOW = 4, /* * Not implemented */ - CIOT_ERR__NOT_IMPLEMENTED = 5, + CIOT__ERR__NOT_IMPLEMENTED = 5, /* * Not supported */ - CIOT_ERR__NOT_SUPPORTED = 6, + CIOT__ERR__NOT_SUPPORTED = 6, /* * Busy */ - CIOT_ERR__BUSY = 7, + CIOT__ERR__BUSY = 7, /* * Invalid state */ - CIOT_ERR__INVALID_STATE = 8, + CIOT__ERR__INVALID_STATE = 8, /* * Serialization error */ - CIOT_ERR__SERIALIZATION = 9, + CIOT__ERR__SERIALIZATION = 9, /* * Deserialization error */ - CIOT_ERR__DESERIALIZATION = 10, + CIOT__ERR__DESERIALIZATION = 10, /* * Error sending data */ - CIOT_ERR__SEND_DATA = 11, + CIOT__ERR__SEND_DATA = 11, /* * Error on data receive */ - CIOT_ERR__RECV_DATA = 12, + CIOT__ERR__RECV_DATA = 12, /* * Invalid size */ - CIOT_ERR__INVALID_SIZE = 13, + CIOT__ERR__INVALID_SIZE = 13, /* * Closed */ - CIOT_ERR__CLOSED = 14, + CIOT__ERR__CLOSED = 14, /* * Resource not found */ - CIOT_ERR__NOT_FOUND = 15, + CIOT__ERR__NOT_FOUND = 15, /* * Validation failed */ - CIOT_ERR__VALIDATION_FAILED = 16, + CIOT__ERR__VALIDATION_FAILED = 16, /* * Connection failed */ - CIOT_ERR__CONNECTION = 17, + CIOT__ERR__CONNECTION = 17, /* * Disconnection failed */ - CIOT_ERR__DISCONNECTION = 18, + CIOT__ERR__DISCONNECTION = 18, /* * Unknown exception occours */ - CIOT_ERR__EXCEPTION = 19, + CIOT__ERR__EXCEPTION = 19, /* * Invalid argument detected */ - CIOT_ERR__INVALID_ARG = 20, + CIOT__ERR__INVALID_ARG = 20, /* * Insuficient memory */ - CIOT_ERR__NO_MEMORY = 21, + CIOT__ERR__NO_MEMORY = 21, /* * Timeout */ - CIOT_ERR__TIMEOUT = 22, + CIOT__ERR__TIMEOUT = 22, /* * Internal error on mongoose framework */ - CIOT_ERR__MONGOOSE = 23, + CIOT__ERR__MONGOOSE = 23, /* * Requested operation is impossible */ - CIOT_ERR__IMPOSSIBLE_OP = 24, + CIOT__ERR__IMPOSSIBLE_OP = 24, /* * Checksum validation error */ - CIOT_ERR__CHECKSUM = 25, + CIOT__ERR__CHECKSUM = 25, /* * Protocol validation error */ - CIOT_ERR__PROTOCOL_VIOLATION = 26, + CIOT__ERR__PROTOCOL_VIOLATION = 26, /* * Data loss */ - CIOT_ERR__DATA_LOSS = 27, + CIOT__ERR__DATA_LOSS = 27, /* * Invalid message header */ - CIOT_ERR__INVALID_HEADER = 28, + CIOT__ERR__INVALID_HEADER = 28, /* * Feature disabled */ - CIOT_ERR__DISABLED = 29, + CIOT__ERR__DISABLED = 29, /* * Operation failed */ - CIOT_ERR__FAIL = 30, + CIOT__ERR__FAIL = 30, /* * Invalid index */ - CIOT_ERR__INVALID_INDEX = 31, + CIOT__ERR__INVALID_INDEX = 31, /* * Invalid key size */ - CIOT_ERR__INVALID_KEY_SIZE = 32 - PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(CIOT_ERR) -} CiotErr; + CIOT__ERR__INVALID_KEY_SIZE = 32 + PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(CIOT__ERR) +} Ciot__Err; /* --- messages --- */ @@ -168,7 +168,7 @@ typedef enum _CiotErr { /* --- descriptors --- */ -extern const ProtobufCEnumDescriptor ciot_err__descriptor; +extern const ProtobufCEnumDescriptor ciot__err__descriptor; PROTOBUF_C__END_DECLS diff --git a/src/proto/ciot/proto/v1/msg.pb-c.c b/src/proto/ciot/proto/v1/msg.pb-c.c index f541535c..065065aa 100644 --- a/src/proto/ciot/proto/v1/msg.pb-c.c +++ b/src/proto/ciot/proto/v1/msg.pb-c.c @@ -220,7 +220,7 @@ static const ProtobufCFieldDescriptor ciot__msg_error__field_descriptors[3] = PROTOBUF_C_TYPE_ENUM, 0, /* quantifier_offset */ offsetof(Ciot__MsgError, code), - &ciot_err__descriptor, + &ciot__err__descriptor, NULL, 0, /* flags */ 0,NULL,NULL /* reserved1,reserved2, etc */ diff --git a/src/proto/ciot/proto/v1/msg.pb-c.h b/src/proto/ciot/proto/v1/msg.pb-c.h index 24a2f95d..9417ebe7 100644 --- a/src/proto/ciot/proto/v1/msg.pb-c.h +++ b/src/proto/ciot/proto/v1/msg.pb-c.h @@ -119,11 +119,11 @@ struct _Ciot__MsgError /* * Error code. */ - CiotErr code; + Ciot__Err code; }; #define CIOT__MSG_ERROR__INIT \ { PROTOBUF_C_MESSAGE_INIT (&ciot__msg_error__descriptor) \ - , NULL, NULL, CIOT_ERR__OK } + , NULL, NULL, CIOT__ERR__OK } /* diff --git a/src/proto/ciot/proto/v1/tcp.pb-c.c b/src/proto/ciot/proto/v1/tcp.pb-c.c index c0f0acd3..064b84d7 100644 --- a/src/proto/ciot/proto/v1/tcp.pb-c.c +++ b/src/proto/ciot/proto/v1/tcp.pb-c.c @@ -277,12 +277,24 @@ void ciot__tcp_data__free_unpacked assert(message->base.descriptor == &ciot__tcp_data__descriptor); protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); } -static const ProtobufCFieldDescriptor ciot__tcp_cfg__field_descriptors[5] = +static const ProtobufCFieldDescriptor ciot__tcp_cfg__field_descriptors[6] = { { - "dhcp", + "disabled", 1, PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_BOOL, + 0, /* quantifier_offset */ + offsetof(Ciot__TcpCfg, disabled), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "dhcp", + 2, + PROTOBUF_C_LABEL_NONE, PROTOBUF_C_TYPE_ENUM, 0, /* quantifier_offset */ offsetof(Ciot__TcpCfg, dhcp), @@ -293,7 +305,7 @@ static const ProtobufCFieldDescriptor ciot__tcp_cfg__field_descriptors[5] = }, { "ip", - 2, + 3, PROTOBUF_C_LABEL_NONE, PROTOBUF_C_TYPE_BYTES, 0, /* quantifier_offset */ @@ -305,7 +317,7 @@ static const ProtobufCFieldDescriptor ciot__tcp_cfg__field_descriptors[5] = }, { "gateway", - 3, + 4, PROTOBUF_C_LABEL_NONE, PROTOBUF_C_TYPE_BYTES, 0, /* quantifier_offset */ @@ -317,7 +329,7 @@ static const ProtobufCFieldDescriptor ciot__tcp_cfg__field_descriptors[5] = }, { "mask", - 4, + 5, PROTOBUF_C_LABEL_NONE, PROTOBUF_C_TYPE_BYTES, 0, /* quantifier_offset */ @@ -329,7 +341,7 @@ static const ProtobufCFieldDescriptor ciot__tcp_cfg__field_descriptors[5] = }, { "dns", - 5, + 6, PROTOBUF_C_LABEL_NONE, PROTOBUF_C_TYPE_BYTES, 0, /* quantifier_offset */ @@ -341,16 +353,17 @@ static const ProtobufCFieldDescriptor ciot__tcp_cfg__field_descriptors[5] = }, }; static const unsigned ciot__tcp_cfg__field_indices_by_name[] = { - 0, /* field[0] = dhcp */ - 4, /* field[4] = dns */ - 2, /* field[2] = gateway */ - 1, /* field[1] = ip */ - 3, /* field[3] = mask */ + 1, /* field[1] = dhcp */ + 0, /* field[0] = disabled */ + 5, /* field[5] = dns */ + 3, /* field[3] = gateway */ + 2, /* field[2] = ip */ + 4, /* field[4] = mask */ }; static const ProtobufCIntRange ciot__tcp_cfg__number_ranges[1 + 1] = { { 1, 0 }, - { 0, 5 } + { 0, 6 } }; const ProtobufCMessageDescriptor ciot__tcp_cfg__descriptor = { @@ -360,7 +373,7 @@ const ProtobufCMessageDescriptor ciot__tcp_cfg__descriptor = "Ciot__TcpCfg", "Ciot", sizeof(Ciot__TcpCfg), - 5, + 6, ciot__tcp_cfg__field_descriptors, ciot__tcp_cfg__field_indices_by_name, 1, ciot__tcp_cfg__number_ranges, @@ -648,24 +661,26 @@ const ProtobufCMessageDescriptor ciot__tcp_data__descriptor = (ProtobufCMessageInit) ciot__tcp_data__init, NULL,NULL,NULL /* reserved[123] */ }; -static const ProtobufCEnumValue ciot__tcp_state__enum_values_by_number[6] = +static const ProtobufCEnumValue ciot__tcp_state__enum_values_by_number[7] = { { "TCP_STATE_STOPPED", "CIOT__TCP_STATE__TCP_STATE_STOPPED", 0 }, { "TCP_STATE_STARTED", "CIOT__TCP_STATE__TCP_STATE_STARTED", 1 }, - { "TCP_STATE_DISCONNECTED", "CIOT__TCP_STATE__TCP_STATE_DISCONNECTED", 2 }, - { "TCP_STATE_CONNECTING", "CIOT__TCP_STATE__TCP_STATE_CONNECTING", 3 }, - { "TCP_STATE_CONNECTED", "CIOT__TCP_STATE__TCP_STATE_CONNECTED", 4 }, - { "TCP_STATE_ERROR", "CIOT__TCP_STATE__TCP_STATE_ERROR", 5 }, + { "TCP_STATE_DISCONNECTING", "CIOT__TCP_STATE__TCP_STATE_DISCONNECTING", 2 }, + { "TCP_STATE_DISCONNECTED", "CIOT__TCP_STATE__TCP_STATE_DISCONNECTED", 3 }, + { "TCP_STATE_CONNECTING", "CIOT__TCP_STATE__TCP_STATE_CONNECTING", 4 }, + { "TCP_STATE_CONNECTED", "CIOT__TCP_STATE__TCP_STATE_CONNECTED", 5 }, + { "TCP_STATE_ERROR", "CIOT__TCP_STATE__TCP_STATE_ERROR", 6 }, }; static const ProtobufCIntRange ciot__tcp_state__value_ranges[] = { -{0, 0},{0, 6} +{0, 0},{0, 7} }; -static const ProtobufCEnumValueIndex ciot__tcp_state__enum_values_by_name[6] = +static const ProtobufCEnumValueIndex ciot__tcp_state__enum_values_by_name[7] = { - { "TCP_STATE_CONNECTED", 4 }, - { "TCP_STATE_CONNECTING", 3 }, - { "TCP_STATE_DISCONNECTED", 2 }, - { "TCP_STATE_ERROR", 5 }, + { "TCP_STATE_CONNECTED", 5 }, + { "TCP_STATE_CONNECTING", 4 }, + { "TCP_STATE_DISCONNECTED", 3 }, + { "TCP_STATE_DISCONNECTING", 2 }, + { "TCP_STATE_ERROR", 6 }, { "TCP_STATE_STARTED", 1 }, { "TCP_STATE_STOPPED", 0 }, }; @@ -676,9 +691,9 @@ const ProtobufCEnumDescriptor ciot__tcp_state__descriptor = "TcpState", "Ciot__TcpState", "Ciot", - 6, + 7, ciot__tcp_state__enum_values_by_number, - 6, + 7, ciot__tcp_state__enum_values_by_name, 1, ciot__tcp_state__value_ranges, diff --git a/src/proto/ciot/proto/v1/tcp.pb-c.h b/src/proto/ciot/proto/v1/tcp.pb-c.h index 5805a1da..a60c3f6b 100644 --- a/src/proto/ciot/proto/v1/tcp.pb-c.h +++ b/src/proto/ciot/proto/v1/tcp.pb-c.h @@ -37,22 +37,26 @@ typedef enum _Ciot__TcpState { * TCP module is started. */ CIOT__TCP_STATE__TCP_STATE_STARTED = 1, + /* + * TCP module is disconnecting. + */ + CIOT__TCP_STATE__TCP_STATE_DISCONNECTING = 2, /* * TCP module is disconnected. */ - CIOT__TCP_STATE__TCP_STATE_DISCONNECTED = 2, + CIOT__TCP_STATE__TCP_STATE_DISCONNECTED = 3, /* * TCP module is connecting. */ - CIOT__TCP_STATE__TCP_STATE_CONNECTING = 3, + CIOT__TCP_STATE__TCP_STATE_CONNECTING = 4, /* * TCP module is connected. */ - CIOT__TCP_STATE__TCP_STATE_CONNECTED = 4, + CIOT__TCP_STATE__TCP_STATE_CONNECTED = 5, /* * TCP module encountered an error. */ - CIOT__TCP_STATE__TCP_STATE_ERROR = 5 + CIOT__TCP_STATE__TCP_STATE_ERROR = 6 PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(CIOT__TCP_STATE) } Ciot__TcpState; /* @@ -115,6 +119,10 @@ typedef enum _Ciot__TcpReqType { struct _Ciot__TcpCfg { ProtobufCMessage base; + /* + * Disable TCP interface + */ + protobuf_c_boolean disabled; /* * DHCP configuration. */ @@ -138,7 +146,7 @@ struct _Ciot__TcpCfg }; #define CIOT__TCP_CFG__INIT \ { PROTOBUF_C_MESSAGE_INIT (&ciot__tcp_cfg__descriptor) \ - , CIOT__TCP_DHCP_CFG__TCP_DHCP_CFG_NO_CHANGE, {0,NULL}, {0,NULL}, {0,NULL}, {0,NULL} } + , 0, CIOT__TCP_DHCP_CFG__TCP_DHCP_CFG_NO_CHANGE, {0,NULL}, {0,NULL}, {0,NULL}, {0,NULL} } /* diff --git a/src/proto/ciot/proto/v1/wifi.pb-c.h b/src/proto/ciot/proto/v1/wifi.pb-c.h index 96debf71..2fa97d44 100644 --- a/src/proto/ciot/proto/v1/wifi.pb-c.h +++ b/src/proto/ciot/proto/v1/wifi.pb-c.h @@ -125,7 +125,7 @@ struct _Ciot__WifiCfg { ProtobufCMessage base; /* - * Disable wifi interface + * Disable Wi-Fi interface */ protobuf_c_boolean disabled; /* diff --git a/src/proto/ciot/proto/v2/msg.pb-c.c b/src/proto/ciot/proto/v2/msg.pb-c.c new file mode 100644 index 00000000..3642f7a9 --- /dev/null +++ b/src/proto/ciot/proto/v2/msg.pb-c.c @@ -0,0 +1,683 @@ +/* Generated by the protocol buffer compiler. DO NOT EDIT! */ +/* Generated from: ciot/proto/v2/msg.proto */ + +/* Do not generate deprecated warnings for self */ +#ifndef PROTOBUF_C__NO_DEPRECATED +#define PROTOBUF_C__NO_DEPRECATED +#endif + +#include "ciot/proto/v2/msg.pb-c.h" +void ciot__msg_error__init + (Ciot__MsgError *message) +{ + static const Ciot__MsgError init_value = CIOT__MSG_ERROR__INIT; + *message = init_value; +} +size_t ciot__msg_error__get_packed_size + (const Ciot__MsgError *message) +{ + assert(message->base.descriptor == &ciot__msg_error__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t ciot__msg_error__pack + (const Ciot__MsgError *message, + uint8_t *out) +{ + assert(message->base.descriptor == &ciot__msg_error__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t ciot__msg_error__pack_to_buffer + (const Ciot__MsgError *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &ciot__msg_error__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +Ciot__MsgError * + ciot__msg_error__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (Ciot__MsgError *) + protobuf_c_message_unpack (&ciot__msg_error__descriptor, + allocator, len, data); +} +void ciot__msg_error__free_unpacked + (Ciot__MsgError *message, + ProtobufCAllocator *allocator) +{ + if(!message) + return; + assert(message->base.descriptor == &ciot__msg_error__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +void ciot__msg_header__init + (Ciot__MsgHeader *message) +{ + static const Ciot__MsgHeader init_value = CIOT__MSG_HEADER__INIT; + *message = init_value; +} +size_t ciot__msg_header__get_packed_size + (const Ciot__MsgHeader *message) +{ + assert(message->base.descriptor == &ciot__msg_header__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t ciot__msg_header__pack + (const Ciot__MsgHeader *message, + uint8_t *out) +{ + assert(message->base.descriptor == &ciot__msg_header__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t ciot__msg_header__pack_to_buffer + (const Ciot__MsgHeader *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &ciot__msg_header__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +Ciot__MsgHeader * + ciot__msg_header__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (Ciot__MsgHeader *) + protobuf_c_message_unpack (&ciot__msg_header__descriptor, + allocator, len, data); +} +void ciot__msg_header__free_unpacked + (Ciot__MsgHeader *message, + ProtobufCAllocator *allocator) +{ + if(!message) + return; + assert(message->base.descriptor == &ciot__msg_header__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +void ciot__msg__init + (Ciot__Msg *message) +{ + static const Ciot__Msg init_value = CIOT__MSG__INIT; + *message = init_value; +} +size_t ciot__msg__get_packed_size + (const Ciot__Msg *message) +{ + assert(message->base.descriptor == &ciot__msg__descriptor); + return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message)); +} +size_t ciot__msg__pack + (const Ciot__Msg *message, + uint8_t *out) +{ + assert(message->base.descriptor == &ciot__msg__descriptor); + return protobuf_c_message_pack ((const ProtobufCMessage*)message, out); +} +size_t ciot__msg__pack_to_buffer + (const Ciot__Msg *message, + ProtobufCBuffer *buffer) +{ + assert(message->base.descriptor == &ciot__msg__descriptor); + return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer); +} +Ciot__Msg * + ciot__msg__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data) +{ + return (Ciot__Msg *) + protobuf_c_message_unpack (&ciot__msg__descriptor, + allocator, len, data); +} +void ciot__msg__free_unpacked + (Ciot__Msg *message, + ProtobufCAllocator *allocator) +{ + if(!message) + return; + assert(message->base.descriptor == &ciot__msg__descriptor); + protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); +} +static const ProtobufCFieldDescriptor ciot__msg_error__field_descriptors[3] = +{ + { + "msg", + 1, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + 0, /* quantifier_offset */ + offsetof(Ciot__MsgError, msg), + &ciot__msg_header__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "iface", + 2, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + 0, /* quantifier_offset */ + offsetof(Ciot__MsgError, iface), + &ciot__iface_info__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "code", + 3, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_ENUM, + 0, /* quantifier_offset */ + offsetof(Ciot__MsgError, code), + &ciot__err__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned ciot__msg_error__field_indices_by_name[] = { + 2, /* field[2] = code */ + 1, /* field[1] = iface */ + 0, /* field[0] = msg */ +}; +static const ProtobufCIntRange ciot__msg_error__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 3 } +}; +const ProtobufCMessageDescriptor ciot__msg_error__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "Ciot.MsgError", + "MsgError", + "Ciot__MsgError", + "Ciot", + sizeof(Ciot__MsgError), + 3, + ciot__msg_error__field_descriptors, + ciot__msg_error__field_indices_by_name, + 1, ciot__msg_error__number_ranges, + (ProtobufCMessageInit) ciot__msg_error__init, + NULL,NULL,NULL /* reserved[123] */ +}; +static const ProtobufCFieldDescriptor ciot__msg_header__field_descriptors[3] = +{ + { + "id", + 1, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(Ciot__MsgHeader, id), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "type", + 2, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_ENUM, + 0, /* quantifier_offset */ + offsetof(Ciot__MsgHeader, type), + &ciot__msg_type__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "iface", + 3, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + 0, /* quantifier_offset */ + offsetof(Ciot__MsgHeader, iface), + &ciot__iface_info__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned ciot__msg_header__field_indices_by_name[] = { + 0, /* field[0] = id */ + 2, /* field[2] = iface */ + 1, /* field[1] = type */ +}; +static const ProtobufCIntRange ciot__msg_header__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 3 } +}; +const ProtobufCMessageDescriptor ciot__msg_header__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "Ciot.MsgHeader", + "MsgHeader", + "Ciot__MsgHeader", + "Ciot", + sizeof(Ciot__MsgHeader), + 3, + ciot__msg_header__field_descriptors, + ciot__msg_header__field_indices_by_name, + 1, ciot__msg_header__number_ranges, + (ProtobufCMessageInit) ciot__msg_header__init, + NULL,NULL,NULL /* reserved[123] */ +}; +static const ProtobufCFieldDescriptor ciot__msg__field_descriptors[26] = +{ + { + "id", + 1, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_UINT32, + 0, /* quantifier_offset */ + offsetof(Ciot__Msg, id), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "type", + 2, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_ENUM, + 0, /* quantifier_offset */ + offsetof(Ciot__Msg, type), + &ciot__msg_type__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "iface", + 3, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + 0, /* quantifier_offset */ + offsetof(Ciot__Msg, iface), + &ciot__iface_info__descriptor, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "error", + 4, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, error), + &ciot__msg_error__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "payload", + 5, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_BYTES, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, payload), + NULL, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "model", + 6, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, model), + &ciot__model_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "log", + 7, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, log), + &ciot__log__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "ciot", + 8, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, ciot), + &ciot__ciot_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "storage", + 9, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, storage), + &ciot__storage_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "sys", + 10, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, sys), + &ciot__sys_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "uart", + 11, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, uart), + &ciot__uart_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "usb", + 12, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, usb), + &ciot__usb_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "tcp", + 13, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, tcp), + &ciot__tcp_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "eth", + 14, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, eth), + &ciot__tcp_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "wifi", + 15, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, wifi), + &ciot__wifi_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "ble", + 16, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, ble), + &ciot__ble_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "ble_scn", + 17, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, ble_scn), + &ciot__ble_scn_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "ble_adv", + 18, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, ble_adv), + &ciot__ble_adv_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "ntp", + 19, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, ntp), + &ciot__ntp_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "ota", + 20, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, ota), + &ciot__ota_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "dfu", + 21, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, dfu), + &ciot__dfu_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "http_server", + 22, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, http_server), + &ciot__http_server_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "http_client", + 23, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, http_client), + &ciot__http_client_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "mqtt_client", + 24, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, mqtt_client), + &ciot__mqtt_client_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "socket", + 25, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, socket), + &ciot__socket_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "gpio", + 26, + PROTOBUF_C_LABEL_NONE, + PROTOBUF_C_TYPE_MESSAGE, + offsetof(Ciot__Msg, data_case), + offsetof(Ciot__Msg, gpio), + &ciot__gpio_data__descriptor, + NULL, + 0 | PROTOBUF_C_FIELD_FLAG_ONEOF, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, +}; +static const unsigned ciot__msg__field_indices_by_name[] = { + 15, /* field[15] = ble */ + 17, /* field[17] = ble_adv */ + 16, /* field[16] = ble_scn */ + 7, /* field[7] = ciot */ + 20, /* field[20] = dfu */ + 3, /* field[3] = error */ + 13, /* field[13] = eth */ + 25, /* field[25] = gpio */ + 22, /* field[22] = http_client */ + 21, /* field[21] = http_server */ + 0, /* field[0] = id */ + 2, /* field[2] = iface */ + 6, /* field[6] = log */ + 5, /* field[5] = model */ + 23, /* field[23] = mqtt_client */ + 18, /* field[18] = ntp */ + 19, /* field[19] = ota */ + 4, /* field[4] = payload */ + 24, /* field[24] = socket */ + 8, /* field[8] = storage */ + 9, /* field[9] = sys */ + 12, /* field[12] = tcp */ + 1, /* field[1] = type */ + 10, /* field[10] = uart */ + 11, /* field[11] = usb */ + 14, /* field[14] = wifi */ +}; +static const ProtobufCIntRange ciot__msg__number_ranges[1 + 1] = +{ + { 1, 0 }, + { 0, 26 } +}; +const ProtobufCMessageDescriptor ciot__msg__descriptor = +{ + PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC, + "Ciot.Msg", + "Msg", + "Ciot__Msg", + "Ciot", + sizeof(Ciot__Msg), + 26, + ciot__msg__field_descriptors, + ciot__msg__field_indices_by_name, + 1, ciot__msg__number_ranges, + (ProtobufCMessageInit) ciot__msg__init, + NULL,NULL,NULL /* reserved[123] */ +}; +static const ProtobufCEnumValue ciot__msg_type__enum_values_by_number[12] = +{ + { "MSG_TYPE_UNKNOWN", "CIOT__MSG_TYPE__MSG_TYPE_UNKNOWN", 0 }, + { "MSG_TYPE_CUSTOM", "CIOT__MSG_TYPE__MSG_TYPE_CUSTOM", 1 }, + { "MSG_TYPE_START", "CIOT__MSG_TYPE__MSG_TYPE_START", 2 }, + { "MSG_TYPE_STOP", "CIOT__MSG_TYPE__MSG_TYPE_STOP", 3 }, + { "MSG_TYPE_CONFIG", "CIOT__MSG_TYPE__MSG_TYPE_CONFIG", 4 }, + { "MSG_TYPE_INFO", "CIOT__MSG_TYPE__MSG_TYPE_INFO", 5 }, + { "MSG_TYPE_STATUS", "CIOT__MSG_TYPE__MSG_TYPE_STATUS", 6 }, + { "MSG_TYPE_SEND_PAYLOAD", "CIOT__MSG_TYPE__MSG_TYPE_SEND_PAYLOAD", 7 }, + { "MSG_TYPE_REQUEST", "CIOT__MSG_TYPE__MSG_TYPE_REQUEST", 8 }, + { "MSG_TYPE_ERROR", "CIOT__MSG_TYPE__MSG_TYPE_ERROR", 9 }, + { "MSG_TYPE_EVENT", "CIOT__MSG_TYPE__MSG_TYPE_EVENT", 10 }, + { "MSG_TYPE_LOG", "CIOT__MSG_TYPE__MSG_TYPE_LOG", 11 }, +}; +static const ProtobufCIntRange ciot__msg_type__value_ranges[] = { +{0, 0},{0, 12} +}; +static const ProtobufCEnumValueIndex ciot__msg_type__enum_values_by_name[12] = +{ + { "MSG_TYPE_CONFIG", 4 }, + { "MSG_TYPE_CUSTOM", 1 }, + { "MSG_TYPE_ERROR", 9 }, + { "MSG_TYPE_EVENT", 10 }, + { "MSG_TYPE_INFO", 5 }, + { "MSG_TYPE_LOG", 11 }, + { "MSG_TYPE_REQUEST", 8 }, + { "MSG_TYPE_SEND_PAYLOAD", 7 }, + { "MSG_TYPE_START", 2 }, + { "MSG_TYPE_STATUS", 6 }, + { "MSG_TYPE_STOP", 3 }, + { "MSG_TYPE_UNKNOWN", 0 }, +}; +const ProtobufCEnumDescriptor ciot__msg_type__descriptor = +{ + PROTOBUF_C__ENUM_DESCRIPTOR_MAGIC, + "Ciot.MsgType", + "MsgType", + "Ciot__MsgType", + "Ciot", + 12, + ciot__msg_type__enum_values_by_number, + 12, + ciot__msg_type__enum_values_by_name, + 1, + ciot__msg_type__value_ranges, + NULL,NULL,NULL,NULL /* reserved[1234] */ +}; diff --git a/src/proto/ciot/proto/v2/msg.pb-c.h b/src/proto/ciot/proto/v2/msg.pb-c.h new file mode 100644 index 00000000..da1a15b4 --- /dev/null +++ b/src/proto/ciot/proto/v2/msg.pb-c.h @@ -0,0 +1,381 @@ +/* Generated by the protocol buffer compiler. DO NOT EDIT! */ +/* Generated from: ciot/proto/v2/msg.proto */ + +#ifndef PROTOBUF_C_ciot_2fproto_2fv2_2fmsg_2eproto__INCLUDED +#define PROTOBUF_C_ciot_2fproto_2fv2_2fmsg_2eproto__INCLUDED + +#include + +PROTOBUF_C__BEGIN_DECLS + +#if PROTOBUF_C_VERSION_NUMBER < 1003000 +# error This file was generated by a newer version of protoc-c which is incompatible with your libprotobuf-c headers. Please update your headers. +#elif 1003003 < PROTOBUF_C_MIN_COMPILER_VERSION +# error This file was generated by an older version of protoc-c which is incompatible with your libprotobuf-c headers. Please regenerate this file with a newer version of protoc-c. +#endif + +#include "ciot/proto/v1/iface.pb-c.h" +#include "ciot/proto/v1/ble_adv.pb-c.h" +#include "ciot/proto/v1/ble_scn.pb-c.h" +#include "ciot/proto/v1/ble.pb-c.h" +#include "ciot/proto/v1/ciot.pb-c.h" +#include "ciot/proto/v1/dfu.pb-c.h" +#include "ciot/proto/v1/errors.pb-c.h" +#include "ciot/proto/v1/gpio.pb-c.h" +#include "ciot/proto/v1/http_client.pb-c.h" +#include "ciot/proto/v1/http_server.pb-c.h" +#include "ciot/proto/v1/logger.pb-c.h" +#include "ciot/proto/v1/model.pb-c.h" +#include "ciot/proto/v1/mqtt_client.pb-c.h" +#include "ciot/proto/v1/ntp.pb-c.h" +#include "ciot/proto/v1/ota.pb-c.h" +#include "ciot/proto/v1/storage.pb-c.h" +#include "ciot/proto/v1/socket.pb-c.h" +#include "ciot/proto/v1/sys.pb-c.h" +#include "ciot/proto/v1/tcp.pb-c.h" +#include "ciot/proto/v1/uart.pb-c.h" +#include "ciot/proto/v1/usb.pb-c.h" +#include "ciot/proto/v1/wifi.pb-c.h" + +typedef struct _Ciot__MsgError Ciot__MsgError; +typedef struct _Ciot__MsgHeader Ciot__MsgHeader; +typedef struct _Ciot__Msg Ciot__Msg; + + +/* --- enums --- */ + +/* + * Enum representing different types of messages. + */ +typedef enum _Ciot__MsgType { + /* + * Unknown message type. + */ + CIOT__MSG_TYPE__MSG_TYPE_UNKNOWN = 0, + /* + * Custom message. + */ + CIOT__MSG_TYPE__MSG_TYPE_CUSTOM = 1, + /* + * Start message. + */ + CIOT__MSG_TYPE__MSG_TYPE_START = 2, + /* + * Stop message. + */ + CIOT__MSG_TYPE__MSG_TYPE_STOP = 3, + /* + * configuration message. + */ + CIOT__MSG_TYPE__MSG_TYPE_CONFIG = 4, + /* + * information message. + */ + CIOT__MSG_TYPE__MSG_TYPE_INFO = 5, + /* + * status message. + */ + CIOT__MSG_TYPE__MSG_TYPE_STATUS = 6, + /* + * Send payload message. + */ + CIOT__MSG_TYPE__MSG_TYPE_SEND_PAYLOAD = 7, + /* + * Request message. + */ + CIOT__MSG_TYPE__MSG_TYPE_REQUEST = 8, + /* + * Error message. + */ + CIOT__MSG_TYPE__MSG_TYPE_ERROR = 9, + /* + * Event message. + */ + CIOT__MSG_TYPE__MSG_TYPE_EVENT = 10, + /* + * Log message. + */ + CIOT__MSG_TYPE__MSG_TYPE_LOG = 11 + PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(CIOT__MSG_TYPE) +} Ciot__MsgType; + +/* --- messages --- */ + +/* + * Message representing error details for a message. + */ +struct _Ciot__MsgError +{ + ProtobufCMessage base; + /* + * Type of the message. + */ + Ciot__MsgHeader *msg; + /* + * Interface information. + */ + Ciot__IfaceInfo *iface; + /* + * Error code. + */ + Ciot__Err code; +}; +#define CIOT__MSG_ERROR__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&ciot__msg_error__descriptor) \ + , NULL, NULL, CIOT__ERR__OK } + + +/* + * Message representing the header of a message. + */ +struct _Ciot__MsgHeader +{ + ProtobufCMessage base; + /* + * Message ID. + */ + uint32_t id; + /* + * Message type. + */ + Ciot__MsgType type; + /* + * Interface information. + */ + Ciot__IfaceInfo *iface; +}; +#define CIOT__MSG_HEADER__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&ciot__msg_header__descriptor) \ + , 0, CIOT__MSG_TYPE__MSG_TYPE_UNKNOWN, NULL } + + +typedef enum { + CIOT__MSG__DATA__NOT_SET = 0, + CIOT__MSG__DATA_ERROR = 4, + CIOT__MSG__DATA_PAYLOAD = 5, + CIOT__MSG__DATA_MODEL = 6, + CIOT__MSG__DATA_LOG = 7, + CIOT__MSG__DATA_CIOT = 8, + CIOT__MSG__DATA_STORAGE = 9, + CIOT__MSG__DATA_SYS = 10, + CIOT__MSG__DATA_UART = 11, + CIOT__MSG__DATA_USB = 12, + CIOT__MSG__DATA_TCP = 13, + CIOT__MSG__DATA_ETH = 14, + CIOT__MSG__DATA_WIFI = 15, + CIOT__MSG__DATA_BLE = 16, + CIOT__MSG__DATA_BLE_SCN = 17, + CIOT__MSG__DATA_BLE_ADV = 18, + CIOT__MSG__DATA_NTP = 19, + CIOT__MSG__DATA_OTA = 20, + CIOT__MSG__DATA_DFU = 21, + CIOT__MSG__DATA_HTTP_SERVER = 22, + CIOT__MSG__DATA_HTTP_CLIENT = 23, + CIOT__MSG__DATA_MQTT_CLIENT = 24, + CIOT__MSG__DATA_SOCKET = 25, + CIOT__MSG__DATA_GPIO = 26 + PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(CIOT__MSG__DATA) +} Ciot__Msg__DataCase; + +/* + * Represents an CioT message + */ +struct _Ciot__Msg +{ + ProtobufCMessage base; + /* + * Message ID. + */ + uint32_t id; + /* + * Message type. + */ + Ciot__MsgType type; + /* + * Interface information. + */ + Ciot__IfaceInfo *iface; + Ciot__Msg__DataCase data_case; + union { + /* + * Error data. + */ + Ciot__MsgError *error; + /* + * Payload data. + */ + ProtobufCBinaryData payload; + /* + * Model data. + */ + Ciot__ModelData *model; + /* + * Log data. + */ + Ciot__Log *log; + /* + * CIOT data. + */ + Ciot__CiotData *ciot; + /* + * Storage data. + */ + Ciot__StorageData *storage; + /* + * System data. + */ + Ciot__SysData *sys; + /* + * UART data. + */ + Ciot__UartData *uart; + /* + * USB data. + */ + Ciot__UsbData *usb; + /* + * TCP data. + */ + Ciot__TcpData *tcp; + /* + * Ethernet data. + */ + Ciot__TcpData *eth; + /* + * WiFi data. + */ + Ciot__WifiData *wifi; + /* + * BLE data. + */ + Ciot__BleData *ble; + /* + * BLE SCN data. + */ + Ciot__BleScnData *ble_scn; + /* + * BLE ADV data. + */ + Ciot__BleAdvData *ble_adv; + /* + * NTP data. + */ + Ciot__NtpData *ntp; + /* + * OTA data. + */ + Ciot__OtaData *ota; + /* + * DFU data. + */ + Ciot__DfuData *dfu; + /* + * HTTP Server data. + */ + Ciot__HttpServerData *http_server; + /* + * HTTP Client data. + */ + Ciot__HttpClientData *http_client; + /* + * MQTT Client data. + */ + Ciot__MqttClientData *mqtt_client; + /* + * Socket data. + */ + Ciot__SocketData *socket; + /* + * GPIO data. + */ + Ciot__GpioData *gpio; + }; +}; +#define CIOT__MSG__INIT \ + { PROTOBUF_C_MESSAGE_INIT (&ciot__msg__descriptor) \ + , 0, CIOT__MSG_TYPE__MSG_TYPE_UNKNOWN, NULL, CIOT__MSG__DATA__NOT_SET, {0} } + + +/* Ciot__MsgError methods */ +void ciot__msg_error__init + (Ciot__MsgError *message); +size_t ciot__msg_error__get_packed_size + (const Ciot__MsgError *message); +size_t ciot__msg_error__pack + (const Ciot__MsgError *message, + uint8_t *out); +size_t ciot__msg_error__pack_to_buffer + (const Ciot__MsgError *message, + ProtobufCBuffer *buffer); +Ciot__MsgError * + ciot__msg_error__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void ciot__msg_error__free_unpacked + (Ciot__MsgError *message, + ProtobufCAllocator *allocator); +/* Ciot__MsgHeader methods */ +void ciot__msg_header__init + (Ciot__MsgHeader *message); +size_t ciot__msg_header__get_packed_size + (const Ciot__MsgHeader *message); +size_t ciot__msg_header__pack + (const Ciot__MsgHeader *message, + uint8_t *out); +size_t ciot__msg_header__pack_to_buffer + (const Ciot__MsgHeader *message, + ProtobufCBuffer *buffer); +Ciot__MsgHeader * + ciot__msg_header__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void ciot__msg_header__free_unpacked + (Ciot__MsgHeader *message, + ProtobufCAllocator *allocator); +/* Ciot__Msg methods */ +void ciot__msg__init + (Ciot__Msg *message); +size_t ciot__msg__get_packed_size + (const Ciot__Msg *message); +size_t ciot__msg__pack + (const Ciot__Msg *message, + uint8_t *out); +size_t ciot__msg__pack_to_buffer + (const Ciot__Msg *message, + ProtobufCBuffer *buffer); +Ciot__Msg * + ciot__msg__unpack + (ProtobufCAllocator *allocator, + size_t len, + const uint8_t *data); +void ciot__msg__free_unpacked + (Ciot__Msg *message, + ProtobufCAllocator *allocator); +/* --- per-message closures --- */ + +typedef void (*Ciot__MsgError_Closure) + (const Ciot__MsgError *message, + void *closure_data); +typedef void (*Ciot__MsgHeader_Closure) + (const Ciot__MsgHeader *message, + void *closure_data); +typedef void (*Ciot__Msg_Closure) + (const Ciot__Msg *message, + void *closure_data); + +/* --- services --- */ + + +/* --- descriptors --- */ + +extern const ProtobufCEnumDescriptor ciot__msg_type__descriptor; +extern const ProtobufCMessageDescriptor ciot__msg_error__descriptor; +extern const ProtobufCMessageDescriptor ciot__msg_header__descriptor; +extern const ProtobufCMessageDescriptor ciot__msg__descriptor; + +PROTOBUF_C__END_DECLS + + +#endif /* PROTOBUF_C_ciot_2fproto_2fv2_2fmsg_2eproto__INCLUDED */ diff --git a/src/win/ciot_ble.c b/src/win/ciot_ble.c index 8c9ed119..7619d71f 100644 --- a/src/win/ciot_ble.c +++ b/src/win/ciot_ble.c @@ -31,16 +31,16 @@ ciot_err_t ciot_ble_start(ciot_ble_t self, ciot_ble_cfg_t *cfg) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_ble_stop(ciot_ble_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_ble_set_mac(ciot_ble_t self, uint8_t mac[6]) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } diff --git a/src/win/ciot_ble_adv.c b/src/win/ciot_ble_adv.c index aa0a6be6..36c36080 100644 --- a/src/win/ciot_ble_adv.c +++ b/src/win/ciot_ble_adv.c @@ -31,16 +31,16 @@ ciot_err_t ciot_ble_adv_start(ciot_ble_adv_t self, ciot_ble_adv_cfg_t *cfg) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_ble_adv_stop(ciot_ble_adv_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_ble_adv_send_data(ciot_ble_adv_t self, uint8_t *data, int size) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } diff --git a/src/win/ciot_ble_scn.c b/src/win/ciot_ble_scn.c index 082f6fdf..aa5118b7 100644 --- a/src/win/ciot_ble_scn.c +++ b/src/win/ciot_ble_scn.c @@ -31,11 +31,11 @@ ciot_err_t ciot_ble_scn_start(ciot_ble_scn_t self, ciot_ble_scn_cfg_t *cfg) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_ble_scn_stop(ciot_ble_scn_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } diff --git a/src/win/ciot_eth.c b/src/win/ciot_eth.c index e8d95976..91271b86 100644 --- a/src/win/ciot_eth.c +++ b/src/win/ciot_eth.c @@ -31,10 +31,10 @@ ciot_eth_t ciot_eth_new(void *handle) ciot_err_t ciot_eth_start(ciot_eth_t self, ciot_tcp_cfg_t *cfg) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_eth_stop(ciot_eth_t self) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } diff --git a/src/win/ciot_gpio.c b/src/win/ciot_gpio.c index 3975d347..132dc5f6 100644 --- a/src/win/ciot_gpio.c +++ b/src/win/ciot_gpio.c @@ -42,7 +42,7 @@ ciot_err_t ciot_gpio_start(ciot_gpio_t self, ciot_gpio_cfg_t *cfg) ciot_err_t ciot_gpio_stop(ciot_gpio_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } static void set_bit(int bit_position, int value) diff --git a/src/win/ciot_ntp.c b/src/win/ciot_ntp.c index 41ab7ec1..bd45ce6e 100644 --- a/src/win/ciot_ntp.c +++ b/src/win/ciot_ntp.c @@ -33,11 +33,11 @@ ciot_err_t ciot_ntp_start(ciot_ntp_t self, ciot_ntp_cfg_t *cfg) CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } ciot_err_t ciot_ntp_stop(ciot_ntp_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_IMPLEMENTED; + return CIOT__ERR__NOT_IMPLEMENTED; } diff --git a/src/win/ciot_ota.c b/src/win/ciot_ota.c index 83f0ba61..6e823764 100644 --- a/src/win/ciot_ota.c +++ b/src/win/ciot_ota.c @@ -29,16 +29,16 @@ ciot_err_t ciot_ota_start(ciot_ota_t self, ciot_ota_cfg_t *cfg) { CIOT_ERR_NULL_CHECK(self); CIOT_ERR_NULL_CHECK(cfg); - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_ota_stop(ciot_ota_t self) { CIOT_ERR_NULL_CHECK(self); - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_ota_rollback(ciot_ota_t self) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } diff --git a/src/win/ciot_tcp.c b/src/win/ciot_tcp.c index f534acb1..82aefd50 100644 --- a/src/win/ciot_tcp.c +++ b/src/win/ciot_tcp.c @@ -40,15 +40,15 @@ ciot_tcp_t ciot_tcp_new(ciot_iface_t *iface, ciot_tcp_type_t type) ciot_err_t ciot_tcp_init_netif(ciot_tcp_t self) { - return CIOT_ERR__OK;; + return CIOT__ERR__OK;; } ciot_err_t ciot_tcp_start(ciot_tcp_t self) { - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_tcp_stop(ciot_tcp_t self) { - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/src/win/ciot_uart.c b/src/win/ciot_uart.c index 19cbd979..7bb4e352 100644 --- a/src/win/ciot_uart.c +++ b/src/win/ciot_uart.c @@ -56,7 +56,7 @@ ciot_err_t ciot_uart_start(ciot_uart_t self, ciot_uart_cfg_t *cfg) base->status.state = CIOT__UART_STATE__UART_STATE_INTERNAL_ERROR; base->status.error = CIOT__UART_ERROR__UART_ERR_OPEN; ciot_iface_send_event_type(&base->iface, CIOT_IFACE_EVENT_STOPPED); - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } self->params.DCBlength = sizeof(self->params); @@ -66,7 +66,7 @@ ciot_err_t ciot_uart_start(ciot_uart_t self, ciot_uart_cfg_t *cfg) base->status.state = CIOT__UART_STATE__UART_STATE_INTERNAL_ERROR; base->status.error = CIOT__UART_ERROR__UART_ERR_OPEN; ciot_iface_send_event_type(&base->iface, CIOT_IFACE_EVENT_STOPPED); - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } self->params.BaudRate = base->cfg.baud_rate; @@ -80,7 +80,7 @@ ciot_err_t ciot_uart_start(ciot_uart_t self, ciot_uart_cfg_t *cfg) base->status.state = CIOT__UART_STATE__UART_STATE_INTERNAL_ERROR; base->status.error = CIOT__UART_ERROR__UART_ERR_OPEN; ciot_iface_send_event_type(&base->iface, CIOT_IFACE_EVENT_STOPPED); - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } self->timeouts.ReadIntervalTimeout = 50; @@ -94,10 +94,10 @@ ciot_err_t ciot_uart_start(ciot_uart_t self, ciot_uart_cfg_t *cfg) base->status.state = CIOT__UART_STATE__UART_STATE_INTERNAL_ERROR; base->status.error = CIOT__UART_ERROR__UART_ERR_OPEN; ciot_iface_send_event_type(&base->iface, CIOT_IFACE_EVENT_STOPPED); - return CIOT_ERR__FAIL; + return CIOT__ERR__FAIL; } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_uart_stop(ciot_uart_t self) @@ -105,7 +105,7 @@ ciot_err_t ciot_uart_stop(ciot_uart_t self) CIOT_ERR_NULL_CHECK(self); CloseHandle(self->handle); self->base.status.state = CIOT__UART_STATE__UART_STATE_CLOSED; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_uart_task(ciot_uart_t self) @@ -121,7 +121,7 @@ ciot_err_t ciot_uart_task(ciot_uart_t self) ciot_uart_process_status(self, &status); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } ciot_err_t ciot_uart_send_bytes(ciot_uart_t self, uint8_t *bytes, int size) @@ -134,12 +134,12 @@ ciot_err_t ciot_uart_send_bytes(ciot_uart_t self, uint8_t *bytes, int size) if(base->status.state == CIOT__UART_STATE__UART_STATE_STARTED) { WriteFile(self->handle, bytes, size, &self->bytes_written, NULL); - return CIOT_ERR__OK; + return CIOT__ERR__OK; } else { CIOT_LOGE(TAG, "Port COM%d is closed", base->cfg.num); - return CIOT_ERR__INVALID_STATE; + return CIOT__ERR__INVALID_STATE; } } @@ -174,7 +174,7 @@ static ciot_err_t ciot_uart_process_status(ciot_uart_t self, COMSTAT *status) if(base->status.state != CIOT__UART_STATE__UART_STATE_STARTED) { - return CIOT_ERR__INVALID_STATE; + return CIOT__ERR__INVALID_STATE; } if(status->cbInQue > 0) @@ -185,7 +185,7 @@ static ciot_err_t ciot_uart_process_status(ciot_uart_t self, COMSTAT *status) if(ReadFile(self->handle, &byte, 1, &self->bytes_read, NULL)) { ciot_err_t err = ciot_iface_process_data(&base->iface, &byte, 1); - if(err != CIOT_ERR__OK) + if(err != CIOT__ERR__OK) { base->status.error = err; } @@ -194,5 +194,5 @@ static ciot_err_t ciot_uart_process_status(ciot_uart_t self, COMSTAT *status) } } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } \ No newline at end of file diff --git a/src/win/ciot_wifi.c b/src/win/ciot_wifi.c index 7af16eb8..91443046 100644 --- a/src/win/ciot_wifi.c +++ b/src/win/ciot_wifi.c @@ -68,22 +68,22 @@ ciot_err_t ciot_wifi_start(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) CIOT_ERR_RETURN(ciot_wifi_set_cfg(self, cfg)); - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_wifi_stop(ciot_wifi_t self) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_wifi_task(ciot_wifi_t self) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } ciot_err_t ciot_wifi_send_bytes(ciot_iface_t *iface, uint8_t *bytes, int size) { - return CIOT_ERR__NOT_SUPPORTED; + return CIOT__ERR__NOT_SUPPORTED; } static ciot_err_t ciot_wifi_set_cfg(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) @@ -98,7 +98,7 @@ static ciot_err_t ciot_wifi_set_cfg(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) if (self->base.cfg.type != cfg->type) { - return CIOT_ERR__INVALID_TYPE; + return CIOT__ERR__INVALID_TYPE; } CIOT_ERR_RETURN(ciot_tcp_init_netif(base->tcp)); @@ -113,5 +113,5 @@ static ciot_err_t ciot_wifi_set_cfg(ciot_wifi_t self, ciot_wifi_cfg_t *cfg) CIOT_ERR_RETURN(ciot_tcp_set_cfg(base->tcp, cfg->tcp)); } - return CIOT_ERR__OK; + return CIOT__ERR__OK; } diff --git a/tests/common/ciot_crypt_test.c b/tests/common/ciot_crypt_test.c index db773139..fec1be2a 100644 --- a/tests/common/ciot_crypt_test.c +++ b/tests/common/ciot_crypt_test.c @@ -28,7 +28,7 @@ void test_ciot_crypt_enc_null_crypt(void) char *data = CIOT_CRYPT_DATA_DECRYPTED; char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; ciot_err_t err = ciot_crypt_enc(NULL, data, out, sizeof(out)); - TEST_ASSERT(err == CIOT_ERR__NULL_ARG); + TEST_ASSERT(err == CIOT__ERR__NULL_ARG); } void test_ciot_crypt_enc_null_data(void) @@ -36,7 +36,7 @@ void test_ciot_crypt_enc_null_data(void) char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; ciot_crypt_t crypt = { 0 }; ciot_err_t err = ciot_crypt_enc(&crypt, NULL, out, sizeof(out)); - TEST_ASSERT(err == CIOT_ERR__NULL_ARG); + TEST_ASSERT(err == CIOT__ERR__NULL_ARG); } void test_ciot_crypt_enc_null_buffer(void) @@ -44,7 +44,7 @@ void test_ciot_crypt_enc_null_buffer(void) char *data = CIOT_CRYPT_DATA_DECRYPTED; ciot_crypt_t crypt = { 0 }; ciot_err_t err = ciot_crypt_enc(&crypt, data, NULL, 0); - TEST_ASSERT(err == CIOT_ERR__NULL_ARG); + TEST_ASSERT(err == CIOT__ERR__NULL_ARG); } void test_ciot_crypt_enc_invalid_key_size(void) @@ -56,7 +56,7 @@ void test_ciot_crypt_enc_invalid_key_size(void) .key.size = CIOT_CRYPT_INVALID_KEY_SIZE, }; ciot_err_t err = ciot_crypt_enc(&crypt, data, out, sizeof(out)); - TEST_ASSERT(err == CIOT_ERR__INVALID_KEY_SIZE); + TEST_ASSERT(err == CIOT__ERR__INVALID_KEY_SIZE); } void test_ciot_crypt_enc_invalid_size(void) @@ -68,7 +68,7 @@ void test_ciot_crypt_enc_invalid_size(void) .key.size = CIOT_CRYPT_VALID_KEY_SIZE, }; ciot_err_t err = ciot_crypt_enc(&crypt, data, out, sizeof(out)); - TEST_ASSERT(err == CIOT_ERR__INVALID_SIZE); + TEST_ASSERT(err == CIOT__ERR__INVALID_SIZE); } void test_ciot_crypt_enc_ok(void) @@ -80,7 +80,7 @@ void test_ciot_crypt_enc_ok(void) .key.size = CIOT_CRYPT_VALID_KEY_SIZE, }; ciot_err_t err = ciot_crypt_enc(&crypt, data, out, sizeof(out)); - TEST_ASSERT(err == CIOT_ERR__OK); + TEST_ASSERT(err == CIOT__ERR__OK); TEST_ASSERT(strcmp(out, CIOT_CRYPT_DATA_ENCRYPTED) == 0); } @@ -89,7 +89,7 @@ void test_ciot_crypt_dec_null_crypt(void) char *data = CIOT_CRYPT_DATA_ENCRYPTED; char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; ciot_err_t err = ciot_crypt_dec(NULL, data, out, sizeof(out)); - TEST_ASSERT(err == CIOT_ERR__NULL_ARG); + TEST_ASSERT(err == CIOT__ERR__NULL_ARG); } void test_ciot_crypt_dec_null_data(void) @@ -97,7 +97,7 @@ void test_ciot_crypt_dec_null_data(void) char out[sizeof(CIOT_CRYPT_DATA_ENCRYPTED)]; ciot_crypt_t crypt = { 0 }; ciot_err_t err = ciot_crypt_dec(&crypt, NULL, out, sizeof(out)); - TEST_ASSERT(err == CIOT_ERR__NULL_ARG); + TEST_ASSERT(err == CIOT__ERR__NULL_ARG); } void test_ciot_crypt_dec_null_buffer(void) @@ -105,7 +105,7 @@ void test_ciot_crypt_dec_null_buffer(void) char *data = CIOT_CRYPT_DATA_ENCRYPTED; ciot_crypt_t crypt = { 0 }; ciot_err_t err = ciot_crypt_dec(&crypt, data, NULL, 0); - TEST_ASSERT(err == CIOT_ERR__NULL_ARG); + TEST_ASSERT(err == CIOT__ERR__NULL_ARG); } void test_ciot_crypt_dec_invalid_key_size(void) @@ -117,7 +117,7 @@ void test_ciot_crypt_dec_invalid_key_size(void) .key.size = CIOT_CRYPT_INVALID_KEY_SIZE, }; ciot_err_t err = ciot_crypt_dec(&crypt, data, out, sizeof(out)); - TEST_ASSERT(err == CIOT_ERR__INVALID_KEY_SIZE); + TEST_ASSERT(err == CIOT__ERR__INVALID_KEY_SIZE); } void test_ciot_crypt_dec_invalid_size(void) @@ -129,7 +129,7 @@ void test_ciot_crypt_dec_invalid_size(void) .key.size = CIOT_CRYPT_VALID_KEY_SIZE, }; ciot_err_t err = ciot_crypt_dec(&crypt, data, out, sizeof(out)); - TEST_ASSERT(err == CIOT_ERR__INVALID_SIZE); + TEST_ASSERT(err == CIOT__ERR__INVALID_SIZE); } void test_ciot_crypt_dec_ok(void) @@ -141,7 +141,7 @@ void test_ciot_crypt_dec_ok(void) .key.size = CIOT_CRYPT_VALID_KEY_SIZE, }; ciot_err_t err = ciot_crypt_dec(&crypt, data, out, sizeof(out)); - TEST_ASSERT(err == CIOT_ERR__OK); + TEST_ASSERT(err == CIOT__ERR__OK); TEST_ASSERT(strcmp(out, CIOT_CRYPT_DATA_DECRYPTED) == 0); } diff --git a/tests/common/ciot_decoder_s_test.c b/tests/common/ciot_decoder_s_test.c index e47d25d6..9c7a2478 100644 --- a/tests/common/ciot_decoder_s_test.c +++ b/tests/common/ciot_decoder_s_test.c @@ -22,7 +22,7 @@ void test_ciot_decoder_s_decode_buffer_null() { iface.decoder = ciot_decoder_s_new(NULL, 0); ciot_err_t err = iface.decoder->decode(&iface, 0); - TEST_ASSERT(err == CIOT_ERR__NULL_ARG); + TEST_ASSERT(err == CIOT__ERR__NULL_ARG); } void test_ciot_decoder_s_decode_overflow() @@ -35,11 +35,11 @@ void test_ciot_decoder_s_decode_overflow() for (size_t i = 0; i < sizeof(data); i++) { err = iface.decoder->decode(&iface, data[i]); - if(err != CIOT_ERR__OK) break; + if(err != CIOT__ERR__OK) break; } TEST_ASSERT(iface.decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); - TEST_ASSERT(err == CIOT_ERR__OVERFLOW); + TEST_ASSERT(err == CIOT__ERR__OVERFLOW); } void test_ciot_decoder_s_decode_protocol_violation() @@ -52,11 +52,11 @@ void test_ciot_decoder_s_decode_protocol_violation() for (size_t i = 0; i < sizeof(data); i++) { err = iface.decoder->decode(&iface, data[i]); - if(err != CIOT_ERR__OK) break; + if(err != CIOT__ERR__OK) break; } TEST_ASSERT(iface.decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); - TEST_ASSERT(err == CIOT_ERR__PROTOCOL_VIOLATION); + TEST_ASSERT(err == CIOT__ERR__PROTOCOL_VIOLATION); } void test_ciot_decoder_s_decode_ok() @@ -70,10 +70,10 @@ void test_ciot_decoder_s_decode_ok() for (size_t i = 0; i < sizeof(data); i++) { err = iface.decoder->decode(&iface, data[i]); - if(err != CIOT_ERR__OK) break; + if(err != CIOT__ERR__OK) break; } - TEST_ASSERT_EQUAL(err, CIOT_ERR__OK); + TEST_ASSERT_EQUAL(err, CIOT__ERR__OK); TEST_ASSERT_EQUAL_MEMORY(expected, buf, sizeof(expected)); } @@ -82,7 +82,7 @@ void test_ciot_decoder_s_send_null_iface() uint8_t data[32]; iface.decoder = ciot_decoder_s_new(NULL, 0); ciot_err_t err = iface.decoder->send(NULL, data, 0); - TEST_ASSERT(err == CIOT_ERR__NULL_ARG); + TEST_ASSERT(err == CIOT__ERR__NULL_ARG); } void test_ciot_decoder_s_send_ok() @@ -92,7 +92,7 @@ void test_ciot_decoder_s_send_ok() uint8_t expected[] = { '{', 0x05, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, '}' }; iface.decoder = ciot_decoder_s_new(buf, sizeof(buf)); ciot_err_t err = iface.decoder->send(&iface, data, sizeof(data)); - TEST_ASSERT_EQUAL(err, CIOT_ERR__OK); + TEST_ASSERT_EQUAL(err, CIOT__ERR__OK); TEST_ASSERT_EQUAL_MEMORY(expected, encode_result, encode_result_size); } @@ -101,7 +101,7 @@ static ciot_err_t iface_send(ciot_iface_t *iface, uint8_t *bytes, int size) int idx = encode_result_size; memcpy(&encode_result[idx], bytes, size); encode_result_size += size; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } void test_ciot_decoder_s() diff --git a/tests/common/ciot_decoder_slip_test.c b/tests/common/ciot_decoder_slip_test.c index cb7fbfa4..48e673c0 100644 --- a/tests/common/ciot_decoder_slip_test.c +++ b/tests/common/ciot_decoder_slip_test.c @@ -22,7 +22,7 @@ void test_ciot_decoder_slip_decode_buffer_null() { iface.decoder = ciot_decoder_slip_new(NULL, 0); ciot_err_t err = iface.decoder->decode(&iface, 0); - TEST_ASSERT(err == CIOT_ERR__NULL_ARG); + TEST_ASSERT(err == CIOT__ERR__NULL_ARG); } void test_ciot_decoder_slip_decode_no_memory() @@ -35,12 +35,12 @@ void test_ciot_decoder_slip_decode_no_memory() for (size_t i = 0; i < sizeof(data); i++) { err = iface.decoder->decode(&iface, data[i]); - if (err != CIOT_ERR__OK) + if (err != CIOT__ERR__OK) break; } TEST_ASSERT(iface.decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); - TEST_ASSERT(err == CIOT_ERR__OVERFLOW); + TEST_ASSERT(err == CIOT__ERR__OVERFLOW); } void test_ciot_decoder_slip_decode_protocol_violation() @@ -53,12 +53,12 @@ void test_ciot_decoder_slip_decode_protocol_violation() for (size_t i = 0; i < sizeof(data); i++) { err = iface.decoder->decode(&iface, data[i]); - if (err != CIOT_ERR__OK) + if (err != CIOT__ERR__OK) break; } TEST_ASSERT(iface.decoder->state == CIOT_IFACE_DECODER_STATE_ERROR); - TEST_ASSERT(err == CIOT_ERR__PROTOCOL_VIOLATION); + TEST_ASSERT(err == CIOT__ERR__PROTOCOL_VIOLATION); } void test_ciot_decoder_slip_decode_ok() @@ -72,11 +72,11 @@ void test_ciot_decoder_slip_decode_ok() for (size_t i = 0; i < sizeof(data); i++) { err = iface.decoder->decode(&iface, data[i]); - if (err != CIOT_ERR__OK) + if (err != CIOT__ERR__OK) break; } - TEST_ASSERT_EQUAL(err, CIOT_ERR__OK); + TEST_ASSERT_EQUAL(err, CIOT__ERR__OK); TEST_ASSERT_EQUAL_MEMORY(expected, buf, sizeof(expected)); } @@ -85,7 +85,7 @@ void test_ciot_decoder_slip_send_null_iface() uint8_t data[32]; iface.decoder = ciot_decoder_slip_new(NULL, 0); ciot_err_t err = iface.decoder->send(NULL, data, 0); - TEST_ASSERT(err == CIOT_ERR__NULL_ARG); + TEST_ASSERT(err == CIOT__ERR__NULL_ARG); } void test_ciot_decoder_slip_send_ok() @@ -95,7 +95,7 @@ void test_ciot_decoder_slip_send_ok() uint8_t expected[] = {0x01, 0xdb, 0xdc, 0xdb, 0xdc, 0x02, 0xdb, 0xdd, 0x03, 0xdc, 0x04, 0xdd, 0x05, 0xc0}; iface.decoder = ciot_decoder_slip_new(buf, sizeof(buf)); ciot_err_t err = iface.decoder->send(&iface, data, sizeof(data)); - TEST_ASSERT_EQUAL(err, CIOT_ERR__OK); + TEST_ASSERT_EQUAL(err, CIOT__ERR__OK); TEST_ASSERT_EQUAL_MEMORY(expected, encode_result, encode_result_size); } @@ -104,7 +104,7 @@ static ciot_err_t iface_send(ciot_iface_t *iface, uint8_t *bytes, int size) int idx = encode_result_size; memcpy(&encode_result[idx], bytes, size); encode_result_size += size; - return CIOT_ERR__OK; + return CIOT__ERR__OK; } void test_ciot_decoder_slip()