1
1
#include "hmac.h"
2
2
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
+
3
8
returncode_t libtocksync_hmac_simple (libtock_hmac_algorithm_t hmac_type ,
4
9
uint8_t * key_buffer , uint32_t key_length ,
5
10
uint8_t * input_buffer , uint32_t input_length ,
@@ -9,29 +14,25 @@ returncode_t libtocksync_hmac_simple(libtock_hmac_algorithm_t hmac_type,
9
14
ret = libtock_hmac_command_set_algorithm ((uint32_t ) hmac_type );
10
15
if (ret != RETURNCODE_SUCCESS ) return ret ;
11
16
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);
14
21
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 ;
17
24
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 ;
20
30
21
31
ret = libtock_hmac_command_run ();
22
- if (ret != RETURNCODE_SUCCESS ) goto exit3 ;
32
+ if (ret != RETURNCODE_SUCCESS ) return ret ;
23
33
24
34
// Wait for the operation.
25
35
ret = libtocksync_hmac_yield_wait_for ();
26
36
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
-
36
37
return ret ;
37
38
}
0 commit comments