Skip to content

Commit 6395c5d

Browse files
authored
Merge pull request #541 from tock/ywf-hmac-scoped-allow
Yield-WaitFor: Introduce `defer` and update HMAC as example usage
2 parents 24c0f3f + bc7f0a1 commit 6395c5d

File tree

9 files changed

+93
-34
lines changed

9 files changed

+93
-34
lines changed

libtock-sync/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ $(LIBNAME)_SRC_ROOT := $(TOCK_USERLAND_BASE_DIR)
1010
# List all C and Assembly files
1111
$(LIBNAME)_SRCS := $(wildcard $($(LIBNAME)_DIR)/*.c)
1212
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/crypto/*.c)
13+
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/crypto/syscalls/*.c)
1314
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/display/*.c)
1415
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/interface/*.c)
1516
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/kernel/*.c)

libtock-sync/crypto/hmac.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
1-
#include "hmac.h"
2-
3-
struct hmac_data {
4-
bool fired;
5-
returncode_t ret;
6-
};
1+
#include <libtock/defer.h>
72

8-
static struct hmac_data result = {.fired = false};
9-
10-
static void hmac_cb_hmac(returncode_t ret) {
11-
result.fired = true;
12-
result.ret = ret;
13-
}
3+
#include "hmac.h"
144

155
returncode_t libtocksync_hmac_simple(libtock_hmac_algorithm_t hmac_type,
166
uint8_t* key_buffer, uint32_t key_length,
177
uint8_t* input_buffer, uint32_t input_length,
188
uint8_t* hmac_buffer, uint32_t hmac_length) {
199
returncode_t ret;
2010

21-
result.fired = false;
22-
23-
ret = libtock_hmac_simple(hmac_type, key_buffer, key_length, input_buffer, input_length, hmac_buffer, hmac_length,
24-
hmac_cb_hmac);
11+
ret = libtock_hmac_command_set_algorithm((uint32_t) hmac_type);
2512
if (ret != RETURNCODE_SUCCESS) return ret;
2613

27-
// Wait for the callback.
28-
yield_for(&result.fired);
29-
if (result.ret != RETURNCODE_SUCCESS) return result.ret;
14+
ret = libtock_hmac_set_readonly_allow_key_buffer(key_buffer, key_length);
15+
if (ret != RETURNCODE_SUCCESS) return ret;
16+
defer { libtock_hmac_set_readonly_allow_key_buffer(NULL, 0);
17+
};
3018

31-
ret = libtock_hmac_set_readonly_allow_key_buffer(NULL, 0);
19+
ret = libtock_hmac_set_readonly_allow_data_buffer(input_buffer, input_length);
3220
if (ret != RETURNCODE_SUCCESS) return ret;
21+
defer { libtock_hmac_set_readonly_allow_data_buffer(NULL, 0);
22+
};
3323

34-
ret = libtock_hmac_set_readonly_allow_data_buffer(NULL, 0);
24+
ret = libtock_hmac_set_readwrite_allow_destination_buffer(hmac_buffer, hmac_length);
3525
if (ret != RETURNCODE_SUCCESS) return ret;
26+
defer { libtock_hmac_set_readwrite_allow_destination_buffer(NULL, 0);
27+
};
3628

37-
ret = libtock_hmac_set_readwrite_allow_destination_buffer(NULL, 0);
29+
ret = libtock_hmac_command_run();
3830
if (ret != RETURNCODE_SUCCESS) return ret;
3931

40-
return RETURNCODE_SUCCESS;
32+
// Wait for the operation.
33+
ret = libtocksync_hmac_yield_wait_for();
34+
35+
return ret;
4136
}

libtock-sync/crypto/hmac.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#pragma once
22

3-
#include <libtock/crypto/hmac.h>
3+
#include <libtock/crypto/hmac_types.h>
44
#include <libtock/tock.h>
55

6+
#include "syscalls/hmac_syscalls.h"
7+
68
#ifdef __cplusplus
79
extern "C" {
810
#endif
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "hmac_syscalls.h"
2+
3+
returncode_t libtocksync_hmac_yield_wait_for(void) {
4+
yield_waitfor_return_t ret;
5+
ret = yield_wait_for(DRIVER_NUM_HMAC, 0);
6+
7+
return (returncode_t) ret.data0;
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <libtock/crypto/syscalls/hmac_syscalls.h>
4+
#include <libtock/tock.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
// Wait for an HMAC operation to finish.
11+
returncode_t libtocksync_hmac_yield_wait_for(void);
12+
13+
#ifdef __cplusplus
14+
}
15+
#endif

libtock/crypto/hmac.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static void hmac_upcall(int ret,
1111
returncode_t libtock_hmac_simple(libtock_hmac_algorithm_t hmac_type,
1212
uint8_t* key_buffer, uint32_t key_length,
1313
uint8_t* input_buffer, uint32_t input_length,
14-
uint8_t* hash_buffer, uint32_t hash_length,
14+
uint8_t* hmac_buffer, uint32_t hmac_length,
1515
libtock_hmac_callback_hmac cb) {
1616

1717
returncode_t ret;
@@ -25,7 +25,7 @@ returncode_t libtock_hmac_simple(libtock_hmac_algorithm_t hmac_type,
2525
ret = libtock_hmac_set_readonly_allow_data_buffer(input_buffer, input_length);
2626
if (ret != RETURNCODE_SUCCESS) return ret;
2727

28-
ret = libtock_hmac_set_readwrite_allow_destination_buffer(hash_buffer, hash_length);
28+
ret = libtock_hmac_set_readwrite_allow_destination_buffer(hmac_buffer, hmac_length);
2929
if (ret != RETURNCODE_SUCCESS) return ret;
3030

3131
ret = libtock_hmac_set_upcall(hmac_upcall, cb);

libtock/crypto/hmac.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "../tock.h"
4+
#include "hmac_types.h"
45
#include "syscalls/hmac_syscalls.h"
56

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

15-
typedef enum {
16-
LIBTOCK_HMAC_SHA256 = 0,
17-
LIBTOCK_HMAC_SHA384 = 1,
18-
LIBTOCK_HMAC_SHA512 = 2,
19-
} libtock_hmac_algorithm_t;
20-
21-
2216

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

3327
#ifdef __cplusplus

libtock/crypto/hmac_types.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "../tock.h"
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
typedef enum {
10+
LIBTOCK_HMAC_SHA256 = 0,
11+
LIBTOCK_HMAC_SHA384 = 1,
12+
LIBTOCK_HMAC_SHA512 = 2,
13+
} libtock_hmac_algorithm_t;
14+
15+
#ifdef __cplusplus
16+
}
17+
#endif

libtock/defer.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
// Implementation of the C `defer {}` feature.
4+
//
5+
// As of July 2025, upcoming versions of C will support `defer` as a way to run
6+
// code when a value goes out of scope. See:
7+
// https://thephd.dev/_vendor/future_cxx/technical%20specification/C%20-%20defer/C%20-%20defer%20Technical%20Specification.pdf
8+
//
9+
// This implements the same feature with a macro. This implementation used from
10+
// https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/.
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
#define __DEFER__(F, V) \
17+
auto void F(int* V##SENTINEL); \
18+
[[gnu::cleanup(F)]] int V; \
19+
auto void F(__attribute__ ((unused)) int* V##SENTINEL)
20+
21+
#define defer __DEFER(__COUNTER__)
22+
#define __DEFER(N) __DEFER_(N)
23+
#define __DEFER_(N) __DEFER__(__DEFER_FUNCTION_##N, __DEFER_VARIABLE_##N)
24+
25+
#ifdef __cplusplus
26+
}
27+
#endif

0 commit comments

Comments
 (0)