Skip to content

Commit a33d088

Browse files
committed
PoC: scoped allow for hmac
1 parent 8b3d1f0 commit a33d088

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

libtock-sync/crypto/hmac.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
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); \
7+
38
returncode_t libtocksync_hmac_simple(libtock_hmac_algorithm_t hmac_type,
49
uint8_t* key_buffer, uint32_t key_length,
510
uint8_t* input_buffer, uint32_t input_length,
@@ -9,29 +14,25 @@ returncode_t libtocksync_hmac_simple(libtock_hmac_algorithm_t hmac_type,
914
ret = libtock_hmac_command_set_algorithm((uint32_t) hmac_type);
1015
if (ret != RETURNCODE_SUCCESS) return ret;
1116

12-
ret = libtock_hmac_set_readonly_allow_key_buffer(key_buffer, key_length);
13-
if (ret != RETURNCODE_SUCCESS) return ret;
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);
1421

15-
ret = libtock_hmac_set_readonly_allow_data_buffer(input_buffer, input_length);
16-
if (ret != RETURNCODE_SUCCESS) goto exit1;
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;
1724

18-
ret = libtock_hmac_set_readwrite_allow_destination_buffer(hmac_buffer, hmac_length);
19-
if (ret != RETURNCODE_SUCCESS) goto exit2;
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;
27+
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;
2030

2131
ret = libtock_hmac_command_run();
22-
if (ret != RETURNCODE_SUCCESS) goto exit3;
32+
if (ret != RETURNCODE_SUCCESS) return ret;
2333

2434
// Wait for the operation.
2535
ret = libtocksync_hmac_yield_wait_for();
2636

27-
exit3:
28-
libtock_hmac_set_readwrite_allow_destination_buffer(NULL, 0);
29-
30-
exit2:
31-
libtock_hmac_set_readonly_allow_data_buffer(NULL, 0);
32-
33-
exit1:
34-
libtock_hmac_set_readonly_allow_key_buffer(NULL, 0);
35-
3637
return ret;
3738
}

0 commit comments

Comments
 (0)