Skip to content

Commit 6670d1c

Browse files
bradjcppannuto
authored andcommitted
libtock-sync: hmac: use ywf
1 parent 7a0c7c0 commit 6670d1c

File tree

6 files changed

+65
-33
lines changed

6 files changed

+65
-33
lines changed

libtock-sync/crypto/hmac.c

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
11
#include "hmac.h"
22

3-
struct hmac_data {
4-
bool fired;
5-
returncode_t ret;
6-
};
7-
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-
}
14-
153
returncode_t libtocksync_hmac_simple(libtock_hmac_algorithm_t hmac_type,
164
uint8_t* key_buffer, uint32_t key_length,
175
uint8_t* input_buffer, uint32_t input_length,
186
uint8_t* hmac_buffer, uint32_t hmac_length) {
197
returncode_t ret;
208

21-
result.fired = false;
9+
ret = libtock_hmac_command_set_algorithm((uint32_t) hmac_type);
10+
if (ret != RETURNCODE_SUCCESS) return ret;
2211

23-
ret = libtock_hmac_simple(hmac_type, key_buffer, key_length, input_buffer, input_length, hmac_buffer, hmac_length,
24-
hmac_cb_hmac);
12+
ret = libtock_hmac_set_readonly_allow_key_buffer(key_buffer, key_length);
2513
if (ret != RETURNCODE_SUCCESS) return ret;
2614

27-
// Wait for the callback.
28-
yield_for(&result.fired);
29-
if (result.ret != RETURNCODE_SUCCESS) return result.ret;
15+
ret = libtock_hmac_set_readonly_allow_data_buffer(input_buffer, input_length);
16+
if (ret != RETURNCODE_SUCCESS) goto exit1;
3017

31-
ret = libtock_hmac_set_readonly_allow_key_buffer(NULL, 0);
32-
if (ret != RETURNCODE_SUCCESS) return ret;
18+
ret = libtock_hmac_set_readwrite_allow_destination_buffer(hmac_buffer, hmac_length);
19+
if (ret != RETURNCODE_SUCCESS) goto exit2;
3320

34-
ret = libtock_hmac_set_readonly_allow_data_buffer(NULL, 0);
35-
if (ret != RETURNCODE_SUCCESS) return ret;
21+
ret = libtock_hmac_command_run();
22+
if (ret != RETURNCODE_SUCCESS) goto exit3;
3623

37-
ret = libtock_hmac_set_readwrite_allow_destination_buffer(NULL, 0);
38-
if (ret != RETURNCODE_SUCCESS) return ret;
24+
// Wait for the operation.
25+
ret = libtocksync_hmac_yield_wait_for();
26+
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);
3935

40-
return RETURNCODE_SUCCESS;
36+
return ret;
4137
}

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.h

Lines changed: 1 addition & 7 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`.

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

0 commit comments

Comments
 (0)