Skip to content

Commit

Permalink
chore: cleanup platform management
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierChanth committed Dec 5, 2024
1 parent e1c7e31 commit 90029c7
Show file tree
Hide file tree
Showing 70 changed files with 357 additions and 263 deletions.
1 change: 0 additions & 1 deletion cmake/find_mbedtls.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# Configurable variables
option(ENABLE_TESTING "Enable MbedTLS tests" OFF)
option(ATCHOPS_MBEDTLS_VERSION_2 "Use v2 of mbedtls" OFF)
set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE OPT_IN) # only try find_package if FIND_PACKAGE_ARGS is set

message(STATUS "[MbedTLS] fetching package...")
Expand Down
2 changes: 2 additions & 0 deletions packages/atchops/include/atchops/aes.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#ifndef ATCHOPS_AES_H
#define ATCHOPS_AES_H

#ifdef __cplusplus
extern "C" {
#endif

#include <atchops/platform.h> // IWYU pragma: keep
#include <stddef.h>

enum atchops_aes_size {
Expand Down
1 change: 1 addition & 0 deletions packages/atchops/include/atchops/aes_ctr.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern "C" {
#endif

#include "atchops/aes.h"
#include <atchops/platform.h> // IWYU pragma: keep
#include <stddef.h>

/**
Expand Down
1 change: 1 addition & 0 deletions packages/atchops/include/atchops/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern "C" {
#endif

#include <atchops/platform.h> // IWYU pragma: keep
#include <stddef.h>

/**
Expand Down
1 change: 1 addition & 0 deletions packages/atchops/include/atchops/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern "C" {
#endif

#include <atchops/platform.h> // IWYU pragma: keep
#define ATCHOPS_RNG_PERSONALIZATION "@atchops12345"

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions packages/atchops/include/atchops/hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifdef __cplusplus
extern "C" {
#endif
#include <atchops/platform.h> // IWYU pragma: keep
#include <stddef.h>

/**
Expand Down
1 change: 1 addition & 0 deletions packages/atchops/include/atchops/iv.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern "C" {
#endif

#include <atchops/platform.h> // IWYU pragma: keep
#include <stddef.h>

#define ATCHOPS_IV_BUFFER_SIZE 16 // non-base64 ivs are always 16 bytes long
Expand Down
6 changes: 2 additions & 4 deletions packages/atchops/include/atchops/mbedtls.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
extern "C" {
#endif

#ifdef TARGET_PORTENTA_H7
#define ATCHOPS_MBEDTLS_VERSION_2
#endif

#include <atchops/platform.h> // IWYU pragma: keep
#include <mbedtls/aes.h> // IWYU pragma: export
#include <mbedtls/asn1.h> // IWYU pragma: export
#include <mbedtls/base64.h> // IWYU pragma: export
#include <mbedtls/ctr_drbg.h> // IWYU pragma: export
#include <mbedtls/entropy.h> // IWYU pragma: export
#include <mbedtls/error.h> // IWYU pragma: export
#include <mbedtls/md.h> // IWYU pragma: export
#include <mbedtls/md5.h> // IWYU pragma: export
#include <mbedtls/net_sockets.h> // IWYU pragma: export
Expand Down
26 changes: 26 additions & 0 deletions packages/atchops/include/atchops/platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef ATCHOPS_PLATFORM_H
#define ATCHOPS_PLATFORM_H

// NOTE: there are two platform specific files:
// - atchops/platform.h
// - atcommons/json.h

// Platforms we support

// Default MbedTLS version

#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#define ATCHOPS_TARGET_UNIX

#elif defined(_WIN32)
#define ATCHOPS_TARGET_WINDOWS

#elif defined(CONFIG_IDF_TARGET_ESP32)
#define ATCHOPS_TARGET_ESPIDF

#elif defined(TARGET_PORTENTA_H7)
#define ATCHOPS_TARGET_ARDUINO
#define ATCHOPS_MBEDTLS_VERSION_2

#endif
#endif
3 changes: 2 additions & 1 deletion packages/atchops/include/atchops/rsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
extern "C" {
#endif

#include "atchops/constants.h"
#include "atchops/constants.h" // IWYU pragma: keep
#include "atchops/rsa_key.h"
#include "atchops/sha.h"
#include <atchops/platform.h> // IWYU pragma: keep
#include <stddef.h>

/**
Expand Down
1 change: 1 addition & 0 deletions packages/atchops/include/atchops/rsa_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern "C" {
#endif

#include <atchops/platform.h> // IWYU pragma: keep
#include <stdbool.h>
#include <stddef.h>

Expand Down
1 change: 1 addition & 0 deletions packages/atchops/include/atchops/sha.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern "C" {
#endif

#include <atchops/constants.h>
#include <atchops/platform.h> // IWYU pragma: keep
#include <stddef.h>

typedef enum atchops_md_type {
Expand Down
1 change: 1 addition & 0 deletions packages/atchops/include/atchops/utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifdef __cplusplus
extern "C" {
#endif
#include <atchops/platform.h> // IWYU pragma: keep
#include <stddef.h>

/**
Expand Down
1 change: 1 addition & 0 deletions packages/atchops/include/atchops/uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern "C" {
#endif

#include <atchops/platform.h> // IWYU pragma: keep
#include <stddef.h>

/**
Expand Down
12 changes: 7 additions & 5 deletions packages/atchops/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#include "atchops/base64.h"
#include "atchops/constants.h"
#include "atchops/mbedtls.h"
#include <string.h>
#include <stddef.h>
#include <atchops/platform.h>
#include <atlogger/atlogger.h>
#include <stddef.h>
#include <string.h>

#define TAG "aes"

Expand All @@ -14,13 +15,13 @@ int atchops_aes_generate_key(unsigned char *key, const enum atchops_aes_size key
/*
* 1. Validate arguments
*/
if(key == NULL) {
if (key == NULL) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "key is NULL\n");
return ret;
}

if(keybits != ATCHOPS_AES_256) {
if (keybits != ATCHOPS_AES_256) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Unsupported keybits\n");
return ret;
Expand All @@ -45,7 +46,8 @@ int atchops_aes_generate_key(unsigned char *key, const enum atchops_aes_size key
/*
* 3. Seed the random number generator
*/
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (unsigned char *)personlization, strlen(personlization))) != 0) {
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (unsigned char *)personlization,
strlen(personlization))) != 0) {
goto exit;
}

Expand Down
8 changes: 5 additions & 3 deletions packages/atchops/src/aes_ctr.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "atchops/aes_ctr.h"
#include <atlogger/atlogger.h>
#include "atchops/mbedtls.h"
#include <atchops/platform.h>
#include <atlogger/atlogger.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -238,9 +239,10 @@ int atchops_aes_ctr_decrypt(const unsigned char *key, const enum atchops_aes_siz
/*
* 5. Return outputs
*/
if(plaintext_padded_len - pad_val > plaintext_size) {
if (plaintext_padded_len - pad_val > plaintext_size) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "plaintext_size is less than (plaintext_padded_len - pad_val). The buffer is too small.\n");
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR,
"plaintext_size is less than (plaintext_padded_len - pad_val). The buffer is too small.\n");
goto exit;
}

Expand Down
19 changes: 10 additions & 9 deletions packages/atchops/src/base64.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "atchops/base64.h"
#include <atlogger/atlogger.h>
#include "atchops/mbedtls.h"
#include <atchops/platform.h>
#include <atlogger/atlogger.h>
#include <stddef.h>

#define TAG "base64"
Expand Down Expand Up @@ -43,12 +44,12 @@ int atchops_base64_encode(const unsigned char *src, const size_t src_len, unsign
*/
if (dst_len == NULL) {
size_t x; // throw away variable
if((ret = mbedtls_base64_encode(dst, dst_size, &x, src, src_len)) != 0) {
if ((ret = mbedtls_base64_encode(dst, dst_size, &x, src, src_len)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: mbedtls_base64_encode failed\n");
goto exit;
}
} else {
if((ret = mbedtls_base64_encode(dst, dst_size, dst_len, src, src_len)) != 0) {
if ((ret = mbedtls_base64_encode(dst, dst_size, dst_len, src, src_len)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_encode: mbedtls_base64_encode failed\n");
goto exit;
}
Expand All @@ -65,25 +66,25 @@ int atchops_base64_decode(const unsigned char *src, const size_t src_len, unsign
/*
* 1. Validate arguments
*/
if(src == NULL) {
if (src == NULL) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: src is NULL\n");
return ret;
}

if(src_len <= 0) {
if (src_len <= 0) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: src_len is less than or equal to 0\n");
return ret;
}

if(dst == NULL) {
if (dst == NULL) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: dst is NULL\n");
return ret;
}

if(dst_size <= 0) {
if (dst_size <= 0) {
ret = 1;
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: dst_size is less than or equal to 0\n");
return ret;
Expand All @@ -94,12 +95,12 @@ int atchops_base64_decode(const unsigned char *src, const size_t src_len, unsign
*/
if (dst_len == NULL) {
size_t x; // throw away variable
if((ret = mbedtls_base64_decode(dst, dst_size, &x, src, src_len)) != 0) {
if ((ret = mbedtls_base64_decode(dst, dst_size, &x, src, src_len)) != 0) {
atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: mbedtls_base64_decode failed\n");
goto exit;
}
} else {
if((ret = mbedtls_base64_decode(dst, dst_size, dst_len, src, src_len)) != 0) {
if ((ret = mbedtls_base64_decode(dst, dst_size, dst_len, src, src_len)) != 0) {
atlogger_log("base64", ATLOGGER_LOGGING_LEVEL_ERROR, "atchops_base64_decode: mbedtls_base64_decode failed\n");
goto exit;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/atchops/src/hex.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "atchops/hex.h"
#include <atchops/platform.h>

#include <atlogger/atlogger.h>
#include <locale.h>
Expand Down Expand Up @@ -29,7 +30,7 @@ int atchops_hex_to_bytes(unsigned char *bytes, const size_t byte_len, const char
int atchops_bytes_to_hex(char *hex_str, const size_t hex_str_len, const unsigned char *bytes, const size_t bytes_len) {
// Ensure the hex string buffer is large enough: 2 chars for each byte + 1 for null terminator
if (hex_str_len < (bytes_len * 2 + 1)) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Insufficient space for hex string\n");
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Insufficient space for hex string\n");
return -1;
}

Expand All @@ -39,4 +40,4 @@ int atchops_bytes_to_hex(char *hex_str, const size_t hex_str_len, const unsigned

hex_str[bytes_len * 2] = '\0';
return 0;
}
}
3 changes: 2 additions & 1 deletion packages/atchops/src/iv.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "atchops/iv.h"
#include "atchops/base64.h"
#include "atchops/constants.h"
#include <atlogger/atlogger.h>
#include "atchops/mbedtls.h"
#include <atchops/platform.h>
#include <atlogger/atlogger.h>
#include <stddef.h>
#include <string.h>

Expand Down
1 change: 1 addition & 0 deletions packages/atchops/src/mbedtls.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "atchops/mbedtls.h"
#include "atchops/sha.h"
#include <atchops/platform.h>

const mbedtls_md_type_t atchops_mbedtls_md_map[] = {
[ATCHOPS_MD_NONE] = MBEDTLS_MD_NONE, [ATCHOPS_MD_MD5] = MBEDTLS_MD_MD5,
Expand Down
1 change: 1 addition & 0 deletions packages/atchops/src/rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "atchops/rsa_key.h"
#include "atchops/sha.h"
#include "atlogger/atlogger.h"
#include <atchops/platform.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
1 change: 1 addition & 0 deletions packages/atchops/src/rsa_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "atchops/base64.h"
#include "atchops/constants.h"
#include "atchops/mbedtls.h"
#include <atchops/platform.h>
#include <atlogger/atlogger.h>
#include <stddef.h>
#include <stdlib.h>
Expand Down
1 change: 1 addition & 0 deletions packages/atchops/src/sha.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "atchops/sha.h"
#include "atchops/mbedtls.h"
#include <atchops/platform.h>
#include <atlogger/atlogger.h>
#include <stddef.h>

Expand Down
1 change: 1 addition & 0 deletions packages/atchops/src/utf8.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <atchops/platform.h>
#include <atchops/utf8.h>
#include <atlogger/atlogger.h>
#include <stddef.h>
Expand Down
Loading

0 comments on commit 90029c7

Please sign in to comment.