Skip to content

Commit de8d962

Browse files
committed
1 parent a33d088 commit de8d962

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

libtock-sync/crypto/hmac.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#include "hmac.h"
22

3-
// would be in a libtock-sync/support.h or internal.h or similar
4-
#define SCOPED_ALLOW(method, buffer, length) \
5-
inline void unallow_##buffer(returncode_t* sentinel##buffer) { if (sentinel##buffer == RETURNCODE_SUCCESS) method(NULL, 0); } \
6-
returncode_t ret_##buffer __attribute__((cleanup(unallow_##buffer))) = method(buffer, length); \
3+
#include <libtock/defer.h>
74

85
returncode_t libtocksync_hmac_simple(libtock_hmac_algorithm_t hmac_type,
96
uint8_t* key_buffer, uint32_t key_length,
@@ -14,19 +11,17 @@ returncode_t libtocksync_hmac_simple(libtock_hmac_algorithm_t hmac_type,
1411
ret = libtock_hmac_command_set_algorithm((uint32_t) hmac_type);
1512
if (ret != RETURNCODE_SUCCESS) return ret;
1613

17-
// Macro expands to this:
18-
//
19-
// inline void unallow_key_buffer(returncode_t* sentinel_key_buffer) { if (sentinel_key_buffer == RETURNCODE_SUCCESS) libtock_hmac_set_readonly_allow_key_buffer(NULL, 0); }
20-
// returncode_t ret_key_buffer __attribute__((cleanup(unallow_key_buffer))) = libtock_hmac_set_readonly_allow_key_buffer(key_buffer, key_length);
21-
22-
SCOPED_ALLOW(libtock_hmac_set_readonly_allow_key_buffer, key_buffer, key_length);
23-
if (ret_key_buffer != RETURNCODE_SUCCESS) return ret_key_buffer;
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); };
2417

25-
SCOPED_ALLOW(libtock_hmac_set_readonly_allow_data_buffer, input_buffer, input_length);
26-
if (ret_input_buffer != RETURNCODE_SUCCESS) return ret_input_buffer;
18+
ret = libtock_hmac_set_readonly_allow_data_buffer(input_buffer, input_length);
19+
if (ret != RETURNCODE_SUCCESS) return ret;
20+
defer { libtock_hmac_set_readonly_allow_data_buffer(NULL, 0); };
2721

28-
SCOPED_ALLOW(libtock_hmac_set_readwrite_allow_destination_buffer, hmac_buffer, hmac_length);
29-
if (ret_hmac_buffer != RETURNCODE_SUCCESS) return ret_hmac_buffer;
22+
ret = libtock_hmac_set_readwrite_allow_destination_buffer(hmac_buffer, hmac_length);
23+
if (ret != RETURNCODE_SUCCESS) return ret;
24+
defer { libtock_hmac_set_readwrite_allow_destination_buffer(NULL, 0); };
3025

3126
ret = libtock_hmac_command_run();
3227
if (ret != RETURNCODE_SUCCESS) return ret;

libtock/defer.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#define __DEFER__(F, V) \
8+
auto void F(int*); \
9+
[[gnu::cleanup(F)]] int V; \
10+
auto void F(int*)
11+
12+
#define defer __DEFER(__COUNTER__)
13+
#define __DEFER(N) __DEFER_(N)
14+
#define __DEFER_(N) __DEFER__(__DEFER_FUNCTION_ ## N, __DEFER_VARIABLE_ ## N)
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif

0 commit comments

Comments
 (0)