-
Notifications
You must be signed in to change notification settings - Fork 100
Yield-WaitFor: Introduce defer
and update HMAC as example usage
#541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0dd7cc4
libtock-sync: make: add crypto syscalls
bradjc 565d653
libtock: hmac: correct buffer arg name
bradjc 248d5b7
libtock-sync: hmac: use ywf
bradjc 9133bf9
PoC: scoped allow for hmac
ppannuto 6a9f7dc
PoC: C25-preview defer approach
ppannuto ede5ed8
defer: name parameter in macro to avoid warning
ppannuto 8d7d488
defer: make fmt
ppannuto bc7f0a1
libtock: comment defer
bradjc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.