Skip to content

Commit

Permalink
Fixed C linting to include sgx code (#261)
Browse files Browse the repository at this point in the history
- Including sgx code in lint-c/format-c scripts
- Fixed reported sgx linting errors
  • Loading branch information
amendelzon authored Jan 15, 2025
1 parent 53e2507 commit 37ddeea
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 66 deletions.
1 change: 0 additions & 1 deletion firmware/src/sgx/src/trusted/ecall.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@ unsigned int ecall_system_process_apdu(unsigned int rx) {
SYNC_RELEASE_LOCK();
return result;
}

3 changes: 2 additions & 1 deletion firmware/src/sgx/src/trusted/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
static bool G_locked = false;

bool sync_try_aqcuire_lock() {
if (G_locked) return false;
if (G_locked)
return false;
G_locked = true;
return true;
}
Expand Down
18 changes: 9 additions & 9 deletions firmware/src/sgx/src/trusted/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ static unsigned int do_unlock(unsigned int rx) {
SET_APDU_OP(1);
return TX_NO_DATA();
}

if (APDU_DATA_SIZE(rx) == 0) {
THROW(ERR_INVALID_DATA_SIZE);
}

SET_APDU_OP(
access_unlock((char*)APDU_DATA_PTR, APDU_DATA_SIZE(rx)) ? 1 : 0);
SET_APDU_OP(access_unlock((char*)APDU_DATA_PTR, APDU_DATA_SIZE(rx)) ? 1
: 0);
return TX_NO_DATA();
}

Expand Down Expand Up @@ -158,16 +158,17 @@ unsigned int system_process_apdu(unsigned int rx) {
return hsm_process_apdu(rx);
}

bool system_init(unsigned char *msg_buffer, size_t msg_buffer_size) {
bool system_init(unsigned char* msg_buffer, size_t msg_buffer_size) {
// Setup the shared APDU buffer
if (msg_buffer_size != EXPECTED_APDU_BUFFER_SIZE) {
LOG("Expected APDU buffer size to be %u but got %lu\n",
EXPECTED_APDU_BUFFER_SIZE, msg_buffer_size);
EXPECTED_APDU_BUFFER_SIZE,
msg_buffer_size);
return false;
}
apdu_buffer = msg_buffer;
apdu_buffer_size = msg_buffer_size;

// Initialize modules
LOG("Initializing modules...\n");
if (!sest_init()) {
Expand Down Expand Up @@ -206,9 +207,8 @@ bool system_init(unsigned char *msg_buffer, size_t msg_buffer_size) {
}

nvmem_init();
if (!nvmem_register_block("bcstate",
&N_bc_state_var,
sizeof(N_bc_state_var))) {
if (!nvmem_register_block(
"bcstate", &N_bc_state_var, sizeof(N_bc_state_var))) {
LOG("Error registering bcstate block\n");
return false;
}
Expand Down
7 changes: 3 additions & 4 deletions firmware/src/sgx/src/trusted/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

/**
* @brief Initializes the system module
*
*
* @param msg_buffer the APDU buffer to use
* @param msg_buffer_size the size of the APDU buffer in bytes
*
Expand All @@ -37,12 +37,11 @@ bool system_init(unsigned char *msg_buffer, size_t msg_buffer_size);

/**
* @brief Process an APDU message
*
*
* @param rx number of received bytes
*
*
* @returns number of bytes to transmit
*/
unsigned int system_process_apdu(unsigned int rx);


#endif // __TRUSTED_SYSTEM_H
15 changes: 10 additions & 5 deletions firmware/src/sgx/src/untrusted/enclave_provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* IN THE SOFTWARE.
*/


#include <unistd.h>

#include "hsm_u.h"
Expand All @@ -36,7 +35,8 @@
#define CREATE_ENCLAVE_FLAGS OE_ENCLAVE_FLAG_SIMULATE
#endif

// Global pointer to the enclave. This should be the only global pointer to the enclave
// Global pointer to the enclave. This should be the only global pointer to the
// enclave
static char* G_enclave_path = NULL;
static oe_enclave_t* G_enclave = NULL;

Expand All @@ -51,13 +51,18 @@ bool epro_init(char* enclave_path) {

oe_enclave_t* epro_get_enclave() {
if (NULL == G_enclave) {
oe_enclave_t *enclave = NULL;
oe_enclave_t* enclave = NULL;
LOG("Creating HSM enclave...\n");
oe_result_t result = oe_create_hsm_enclave(G_enclave_path,
OE_ENCLAVE_TYPE_AUTO,
CREATE_ENCLAVE_FLAGS, NULL, 0, &enclave);
CREATE_ENCLAVE_FLAGS,
NULL,
0,
&enclave);
if (OE_OK != result) {
LOG("Failed to create enclave: oe_result=%u (%s)\n", result, oe_result_str(result));
LOG("Failed to create enclave: oe_result=%u (%s)\n",
result,
oe_result_str(result));
return NULL;
}

Expand Down
8 changes: 4 additions & 4 deletions firmware/src/sgx/src/untrusted/enclave_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@

/**
* @brief Initializes the enclave provider with the given enclave binary path
*
*
* @returns Whether initialization succeeded
*/
bool epro_init(char* enclave_path);

/**
* @brief Returns a pointer to the HSM enclave. This function should always
* return a valid pointer to the enclave, which can be used to perform
* @brief Returns a pointer to the HSM enclave. This function should always
* return a valid pointer to the enclave, which can be used to perform
* ecall operations.
*
*
* @returns A valid pointer to the HSM enclave, or NULL if an error occurred
*/
oe_enclave_t* epro_get_enclave();
Expand Down
14 changes: 7 additions & 7 deletions firmware/src/sgx/src/untrusted/enclave_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@
* ECALLS
*/

bool eprx_system_init(unsigned char *msg_buffer, size_t msg_buffer_size) {
oe_enclave_t *enclave = epro_get_enclave();
bool eprx_system_init(unsigned char* msg_buffer, size_t msg_buffer_size) {
oe_enclave_t* enclave = epro_get_enclave();
if (enclave == NULL) {
LOG("Failed to retrieve the enclave. "
"Unable to call system_init().\n");
return false;
}

bool result;
oe_result_t oe_result = ecall_system_init(enclave, &result,
msg_buffer, msg_buffer_size);
oe_result_t oe_result =
ecall_system_init(enclave, &result, msg_buffer, msg_buffer_size);
CHECK_ECALL_RESULT(oe_result, "Failed to call system_init()", false);
return result;
}

unsigned int eprx_system_process_apdu(unsigned int rx) {
oe_enclave_t *enclave = epro_get_enclave();
oe_enclave_t* enclave = epro_get_enclave();
if (enclave == NULL) {
LOG("Failed to retrieve the enclave. "
"Unable to call system_process_command().\n");
Expand All @@ -48,7 +48,8 @@ unsigned int eprx_system_process_apdu(unsigned int rx) {
unsigned int result;
oe_result_t oe_result = ecall_system_process_apdu(enclave, &result, rx);

CHECK_ECALL_RESULT(oe_result, "Failed to call ecall_system_process_apdu()", false);
CHECK_ECALL_RESULT(
oe_result, "Failed to call ecall_system_process_apdu()", false);
return result;
}

Expand Down Expand Up @@ -85,4 +86,3 @@ bool ocall_kvstore_remove(char* key) {
log_clear_prefix();
return retval;
}

6 changes: 3 additions & 3 deletions firmware/src/sgx/src/untrusted/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
extern unsigned char io_apdu_buffer[APDU_BUFFER_SIZE];

/**
* @brief Initializes the I/O module. Starts a TCP server at the given host and
* @brief Initializes the I/O module. Starts a TCP server at the given host and
* port.
*
*
* @param port the port on which to listen for connections
* @param host the interface to bind to
*
*
*/
bool io_init(int port, const char *host);

Expand Down
23 changes: 9 additions & 14 deletions firmware/src/sgx/src/untrusted/keyvalue_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
#define KVSTORE_SUFFIX ".dat"

static char* filename_for(char* key) {
size_t filename_size = strlen(KVSTORE_PREFIX) +
strlen(KVSTORE_SUFFIX) +
strlen(key);
char* filename = malloc(filename_size+1);
size_t filename_size =
strlen(KVSTORE_PREFIX) + strlen(KVSTORE_SUFFIX) + strlen(key);
char* filename = malloc(filename_size + 1);
strcpy(filename, "");
strcat(filename, KVSTORE_PREFIX);
strcat(filename, key);
Expand All @@ -45,7 +44,8 @@ static FILE* open_file_for(char* key, char* mode, size_t* file_size) {
char* filename = filename_for(key);
struct stat fst;
stat(filename, &fst);
if (file_size) *file_size = fst.st_size;
if (file_size)
*file_size = fst.st_size;
FILE* file = fopen(filename, mode);
free(filename);
return file;
Expand All @@ -64,10 +64,7 @@ bool kvstore_save(char* key, uint8_t* data, size_t data_size) {
return false;
}

if (fwrite(data,
sizeof(data[0]),
data_size,
file) != data_size) {
if (fwrite(data, sizeof(data[0]), data_size, file) != data_size) {
LOG("Error writing secret payload for key <%s>\n", key);
fclose(file);
return false;
Expand Down Expand Up @@ -109,10 +106,7 @@ size_t kvstore_get(char* key, uint8_t* data_buf, size_t buffer_size) {
return 0;
}

if (fread(data_buf,
sizeof(data_buf[0]),
file_size,
file) != file_size) {
if (fread(data_buf, sizeof(data_buf[0]), file_size, file) != file_size) {
LOG("Could not read payload for key <%s>\n", key);
fclose(file);
return 0;
Expand All @@ -125,7 +119,8 @@ size_t kvstore_get(char* key, uint8_t* data_buf, size_t buffer_size) {
bool kvstore_remove(char* key) {
char* filename = filename_for(key);
int result = remove(filename);
if (result) LOG("Error removing file for key <%s>\n", key);
if (result)
LOG("Error removing file for key <%s>\n", key);
free(filename);
return !result;
}
16 changes: 8 additions & 8 deletions firmware/src/sgx/src/untrusted/keyvalue_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,40 @@

/**
* @brief Tell whether a given key currently exists
*
*
* @param key the key to check for
*
*
* @returns whether the key exists
*/
bool kvstore_exists(char* key);

/**
* @brief Save the given data to the given key
*
*
* @param key the key to save the data to
* @param data the buffer containing the data to write
* @param data_size the data size in bytes
*
*
* @returns whether saving succeeded
*/
bool kvstore_save(char* key, uint8_t* data, size_t data_size);

/**
* @brief Read the given key into the given buffer
*
*
* @param key the key to read from
* @param data_buf the buffer to read the data to
* @param buffer_size the buffer size in bytes
*
*
* @returns the number of bytes read, or ZERO upon error
*/
size_t kvstore_get(char* key, uint8_t* data_buf, size_t buffer_size);

/**
* @brief Remove any data associated with the given key
*
*
* @param key the key to remove
*
*
* @returns whether key removal was successful
*/
bool kvstore_remove(char* key);
Expand Down
8 changes: 4 additions & 4 deletions firmware/src/sgx/src/untrusted/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include "log.h"

static char* log_prefix = (char*)NULL;
static char *log_prefix = (char *)NULL;

void LOG(const char *format, ...) {
va_list args;
Expand Down Expand Up @@ -57,10 +57,10 @@ void LOG_HEX(const char *prefix, const void *buffer, const size_t size) {
printf("\n");
}

void log_set_prefix(const char* prefix) {
log_prefix = (char*)prefix;
void log_set_prefix(const char *prefix) {
log_prefix = (char *)prefix;
}

void log_clear_prefix() {
log_prefix = (char*)NULL;
log_prefix = (char *)NULL;
}
4 changes: 2 additions & 2 deletions firmware/src/sgx/src/untrusted/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ void LOG_HEX(const char *prefix, const void *buffer, const size_t size);

/**
* @brief Set a prefix for all logs
*
*
* @param prefix the prefix to use for logs
*/
void log_set_prefix(const char* prefix);
void log_set_prefix(const char *prefix);

/**
* @brief Clear any prefix set for logs
Expand Down
7 changes: 4 additions & 3 deletions firmware/src/sgx/src/untrusted/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
static struct argp_option options[] = {
{"bind", 'b', "ADDRESS", 0, "Address to bind to", 0},
{"port", 'p', "PORT", 0, "Port to listen on", 0},
{0}
};
{0}};

// Argument definitions for argp
struct arguments {
Expand Down Expand Up @@ -91,7 +90,9 @@ static struct argp argp = {
parse_opt,
"ENCLAVE_PATH",
"SGX powHSM",
NULL, NULL, NULL,
NULL,
NULL,
NULL,
};

static void finalise_with(int exit_code) {
Expand Down
3 changes: 2 additions & 1 deletion lint-c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ if [[ $1 == "exec" ]]; then
fi

SRC_DIR="firmware/src"
SEARCH_DIRS="$SRC_DIR/ledger/signer $SRC_DIR/ledger/ui $SRC_DIR/tcpsigner $SRC_DIR/common $SRC_DIR/hal"
SEARCH_DIRS="$SRC_DIR/ledger/signer $SRC_DIR/ledger/ui $SRC_DIR/tcpsigner $SRC_DIR/common $SRC_DIR/hal $SRC_DIR/sgx"

find $SEARCH_DIRS -name "*.[ch]" | \
egrep -v "(bigdigits|bigdtypes|keccak256)\.[ch]$" | \
egrep -v "firmware/src/ledger/ui/src/glyphs.[ch]" | \
egrep -v "firmware/src/sgx/src/(trusted|untrusted)/hsm_([tu]|args).[ch]" | \
xargs clang-format-10 --style=file $CLANG_ARGS
else
# Script directory
Expand Down

0 comments on commit 37ddeea

Please sign in to comment.