Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libtock-sync/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ $(LIBNAME)_SRC_ROOT := $(TOCK_USERLAND_BASE_DIR)
# List all C and Assembly files
$(LIBNAME)_SRCS := $(wildcard $($(LIBNAME)_DIR)/*.c)
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/crypto/*.c)
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/crypto/syscalls/*.c)
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/display/*.c)
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/interface/*.c)
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/kernel/*.c)
Expand Down
41 changes: 18 additions & 23 deletions libtock-sync/crypto/hmac.c
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
#include "hmac.h"

struct hmac_data {
bool fired;
returncode_t ret;
};
#include <libtock/defer.h>

static struct hmac_data result = {.fired = false};

static void hmac_cb_hmac(returncode_t ret) {
result.fired = true;
result.ret = ret;
}
#include "hmac.h"

returncode_t libtocksync_hmac_simple(libtock_hmac_algorithm_t hmac_type,
uint8_t* key_buffer, uint32_t key_length,
uint8_t* input_buffer, uint32_t input_length,
uint8_t* hmac_buffer, uint32_t hmac_length) {
returncode_t ret;

result.fired = false;

ret = libtock_hmac_simple(hmac_type, key_buffer, key_length, input_buffer, input_length, hmac_buffer, hmac_length,
hmac_cb_hmac);
ret = libtock_hmac_command_set_algorithm((uint32_t) hmac_type);
if (ret != RETURNCODE_SUCCESS) return ret;

// Wait for the callback.
yield_for(&result.fired);
if (result.ret != RETURNCODE_SUCCESS) return result.ret;
ret = libtock_hmac_set_readonly_allow_key_buffer(key_buffer, key_length);
if (ret != RETURNCODE_SUCCESS) return ret;
defer { libtock_hmac_set_readonly_allow_key_buffer(NULL, 0);
};

ret = libtock_hmac_set_readonly_allow_key_buffer(NULL, 0);
ret = libtock_hmac_set_readonly_allow_data_buffer(input_buffer, input_length);
if (ret != RETURNCODE_SUCCESS) return ret;
defer { libtock_hmac_set_readonly_allow_data_buffer(NULL, 0);
};

ret = libtock_hmac_set_readonly_allow_data_buffer(NULL, 0);
ret = libtock_hmac_set_readwrite_allow_destination_buffer(hmac_buffer, hmac_length);
if (ret != RETURNCODE_SUCCESS) return ret;
defer { libtock_hmac_set_readwrite_allow_destination_buffer(NULL, 0);
};

ret = libtock_hmac_set_readwrite_allow_destination_buffer(NULL, 0);
ret = libtock_hmac_command_run();
if (ret != RETURNCODE_SUCCESS) return ret;

return RETURNCODE_SUCCESS;
// Wait for the operation.
ret = libtocksync_hmac_yield_wait_for();

return ret;
}
4 changes: 3 additions & 1 deletion libtock-sync/crypto/hmac.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <libtock/crypto/hmac.h>
#include <libtock/crypto/hmac_types.h>
#include <libtock/tock.h>

#include "syscalls/hmac_syscalls.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
8 changes: 8 additions & 0 deletions libtock-sync/crypto/syscalls/hmac_syscalls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "hmac_syscalls.h"

returncode_t libtocksync_hmac_yield_wait_for(void) {
yield_waitfor_return_t ret;
ret = yield_wait_for(DRIVER_NUM_HMAC, 0);

return (returncode_t) ret.data0;
}
15 changes: 15 additions & 0 deletions libtock-sync/crypto/syscalls/hmac_syscalls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <libtock/crypto/syscalls/hmac_syscalls.h>
#include <libtock/tock.h>

#ifdef __cplusplus
extern "C" {
#endif

// Wait for an HMAC operation to finish.
returncode_t libtocksync_hmac_yield_wait_for(void);

#ifdef __cplusplus
}
#endif
4 changes: 2 additions & 2 deletions libtock/crypto/hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static void hmac_upcall(int ret,
returncode_t libtock_hmac_simple(libtock_hmac_algorithm_t hmac_type,
uint8_t* key_buffer, uint32_t key_length,
uint8_t* input_buffer, uint32_t input_length,
uint8_t* hash_buffer, uint32_t hash_length,
uint8_t* hmac_buffer, uint32_t hmac_length,
libtock_hmac_callback_hmac cb) {

returncode_t ret;
Expand All @@ -25,7 +25,7 @@ returncode_t libtock_hmac_simple(libtock_hmac_algorithm_t hmac_type,
ret = libtock_hmac_set_readonly_allow_data_buffer(input_buffer, input_length);
if (ret != RETURNCODE_SUCCESS) return ret;

ret = libtock_hmac_set_readwrite_allow_destination_buffer(hash_buffer, hash_length);
ret = libtock_hmac_set_readwrite_allow_destination_buffer(hmac_buffer, hmac_length);
if (ret != RETURNCODE_SUCCESS) return ret;

ret = libtock_hmac_set_upcall(hmac_upcall, cb);
Expand Down
10 changes: 2 additions & 8 deletions libtock/crypto/hmac.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../tock.h"
#include "hmac_types.h"
#include "syscalls/hmac_syscalls.h"

#ifdef __cplusplus
Expand All @@ -12,13 +13,6 @@ extern "C" {
// - `arg1` (`returncode_t`): Status from computing the HMAC.
typedef void (*libtock_hmac_callback_hmac)(returncode_t);

typedef enum {
LIBTOCK_HMAC_SHA256 = 0,
LIBTOCK_HMAC_SHA384 = 1,
LIBTOCK_HMAC_SHA512 = 2,
} libtock_hmac_algorithm_t;



// Compute an HMAC using `keyb_buffer` over `input_buffer` and store the result
// in `hash_buffer`.
Expand All @@ -27,7 +21,7 @@ typedef enum {
returncode_t libtock_hmac_simple(libtock_hmac_algorithm_t hmac_type,
uint8_t* key_buffer, uint32_t key_length,
uint8_t* input_buffer, uint32_t input_length,
uint8_t* hash_buffer, uint32_t hash_length,
uint8_t* hmac_buffer, uint32_t hmac_length,
libtock_hmac_callback_hmac cb);

#ifdef __cplusplus
Expand Down
17 changes: 17 additions & 0 deletions libtock/crypto/hmac_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "../tock.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {
LIBTOCK_HMAC_SHA256 = 0,
LIBTOCK_HMAC_SHA384 = 1,
LIBTOCK_HMAC_SHA512 = 2,
} libtock_hmac_algorithm_t;

#ifdef __cplusplus
}
#endif
27 changes: 27 additions & 0 deletions libtock/defer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

// Implementation of the C `defer {}` feature.
//
// As of July 2025, upcoming versions of C will support `defer` as a way to run
// code when a value goes out of scope. See:
// https://thephd.dev/_vendor/future_cxx/technical%20specification/C%20-%20defer/C%20-%20defer%20Technical%20Specification.pdf
//
// This implements the same feature with a macro. This implementation used from
// https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/.

#ifdef __cplusplus
extern "C" {
#endif

#define __DEFER__(F, V) \
auto void F(int* V##SENTINEL); \
[[gnu::cleanup(F)]] int V; \
auto void F(__attribute__ ((unused)) int* V##SENTINEL)

#define defer __DEFER(__COUNTER__)
#define __DEFER(N) __DEFER_(N)
#define __DEFER_(N) __DEFER__(__DEFER_FUNCTION_##N, __DEFER_VARIABLE_##N)

#ifdef __cplusplus
}
#endif