1
- #include "crypto.h"
1
+ #include "cover.h"
2
+
3
+ #include <limits.h>
4
+ #include <stdint.h>
2
5
#include <stdio.h>
3
6
#include <stdlib.h>
4
7
#include <string.h>
5
- #include <stdint.h>
6
- #include <limits.h>
7
- #include "cover.h"
8
+
9
+ #include "crypto.h"
8
10
9
11
#ifndef ASCON_KEY_SIZE
10
12
#define ASCON_KEY_SIZE 16
11
13
#endif
12
14
13
15
/**
14
16
* @brief Derive the correct child of this key and write it to dest.
15
- *
17
+ *
16
18
* @param current_key a pointer to the key we will derive from
17
19
* @param start start timestamp of current key
18
20
* @param height height of current key in the tree
22
24
* @param dest_height where we will write the height of the derived key
23
25
* @return int 0 on success, -1 on failure
24
26
*/
25
- int derive_child (uint8_t * current_key , uint64_t start , uint8_t height , uint64_t target_timestamp , uint8_t * dest , uint64_t * dest_start , uint8_t * dest_height ){
27
+ int derive_child (uint8_t * current_key , uint64_t start , uint8_t height ,
28
+ uint64_t target_timestamp , uint8_t * dest , uint64_t * dest_start ,
29
+ uint8_t * dest_height ) {
26
30
// ending timestamp of current key's tree/range
27
31
uint64_t end = start + (1ULL << height ) - 1 ;
28
32
// midpoint of current key's tree/range
29
- uint64_t mid = start + (end - start )/ 2 ;
33
+ uint64_t mid = start + (end - start ) / 2 ;
30
34
31
- //Traverse binary tree (one step)
32
- if (target_timestamp <= mid ){
35
+ // Traverse binary tree (one step)
36
+ if (target_timestamp <= mid ) {
33
37
end = mid ;
34
38
} else {
35
39
start = mid + 1 ;
36
40
}
37
41
38
- //utilizing the timestamps, and other information derive the key
39
- int ret = wc_HKDF (WC_SHA , current_key , ASCON_KEY_SIZE , NULL , 0 , (uint8_t * ) & start , sizeof (start ),dest , ASCON_KEY_SIZE );
42
+ // utilizing the timestamps, and other information derive the key
43
+ int ret = wc_HKDF (WC_SHA3_224 , current_key , ASCON_KEY_SIZE , NULL , 0 , (uint8_t * )& start ,
44
+ sizeof (start ), dest , ASCON_KEY_SIZE );
40
45
41
- if (ret != 0 ){
42
- //Error deriving key
46
+ if (ret != 0 ) {
47
+ // Error deriving key
43
48
return -1 ;
44
49
}
45
50
46
- //write the start and the updated height of the derived key
51
+ // write the start and the updated height of the derived key
47
52
* dest_start = start ;
48
- * dest_height = height - 1 ;
53
+ * dest_height = height - 1 ;
49
54
return 0 ;
50
55
}
51
56
52
-
53
57
/**
54
58
* @brief Get the correct key from the tree. Manage the stack of keys. Derive and Discard
55
59
* keys as appropriate
@@ -58,7 +62,7 @@ int derive_child(uint8_t* current_key, uint64_t start, uint8_t height, uint64_t
58
62
* @param timestamp target timestamp
59
63
* @return address of key, NULL on failure
60
64
*/
61
- uint8_t * tree_get_key (keys_flash_entry_t * stack , uint64_t timestamp ) {
65
+ uint8_t * tree_get_key (keys_flash_entry_t * stack , uint64_t timestamp ) {
62
66
if (timestamp < stack -> start [stack -> n_keys - 1 ]) {
63
67
return NULL ; // the timeframe is before our subscription period
64
68
}
@@ -95,20 +99,22 @@ int derive_child(uint8_t* current_key, uint64_t start, uint8_t height, uint64_t
95
99
}
96
100
97
101
/** @brief A helper function to determine the correct key for a given start and end tim.
98
- *
99
- * @details Update_subscription calls this function and compute_h_bnd1(), and takes the minimum
100
- * of both to get the accurate height. The height (distance from the start timestamp as a leaf node)
101
- * gets the necessary key. Bound 2 ensures the height maximization doesn't overshoot the end timestamp
102
- * by calculating the largest n for 2^n that can be added to start that isn't greater than the end
103
- * timestamp. The maximization is: h <= log_2(end + 1 - st), with bit shifting instead of log.
104
- *
102
+ *
103
+ * @details Update_subscription calls this function and compute_h_bnd1(), and takes the
104
+ * minimum of both to get the accurate height. The height (distance from the start
105
+ * timestamp as a leaf node) gets the necessary key. Bound 2 ensures the height
106
+ * maximization doesn't overshoot the end timestamp by calculating the largest n for 2^n
107
+ * that can be added to start that isn't greater than the end timestamp. The maximization
108
+ * is: h <= log_2(end + 1 - st), with bit shifting instead of log.
109
+ *
105
110
* @param uint64_t end: the end timestamp that cannot be exceeded.
106
111
* @param uint64_t cur: the start timestamp (not global start) that cannot be preceded.
107
112
* @return uint8_t h: the height of the key which covers the given timestamp range.
108
113
*/
109
-
110
- uint8_t compute_h_bnd2 (uint64_t end , uint64_t cur ) {
111
- // The distance between end and start timestamps -- inclusive of start, which is the "+1"
114
+
115
+ uint8_t compute_h_bnd2 (uint64_t end , uint64_t cur ) {
116
+ // The distance between end and start timestamps -- inclusive of start, which is the
117
+ // "+1"
112
118
uint64_t diff = end - cur + 1 ;
113
119
114
120
// For the full range of timestamps (0, 2^64-1), diff = 2^64 and overflows to equal 0.
@@ -135,22 +141,25 @@ int derive_child(uint8_t* current_key, uint64_t start, uint8_t height, uint64_t
135
141
136
142
/** @brief A helper function to determine the correct key for a given start timestamp.
137
143
* Computes height (distance from leaf node layer) using a trailing zeros method.
138
- * This height is used to determine the location on the tree of the inputted keys based on the given range.
139
- * Location meaning - is a key a leaf node or a parent key node (and where is the parent key located).
144
+ * This height is used to determine the location on the tree of the inputted keys based on
145
+ * the given range. Location meaning - is a key a leaf node or a parent key node (and
146
+ * where is the parent key located).
140
147
*
141
- * @details Update_subscription calls this function and compute_h_bnd2(), and takes the minimum of both to get the accurate height.
142
- * Trailing zeros method: Assign each node on the tree a number represented in binary.
143
- * Imagine the leaf node layer where the first node is the first timestamp of 0 and the last node is the max timestamp 64bit.
144
- * The trailing zeros of each node represent the height
145
- * (how many layers up the tree to go starting at the leaf node layer, specifically the leaf node at the start timestamp (cur)).
148
+ * @details Update_subscription calls this function and compute_h_bnd2(), and takes the
149
+ * minimum of both to get the accurate height. Trailing zeros method: Assign each node on
150
+ * the tree a number represented in binary. Imagine the leaf node layer where the first
151
+ * node is the first timestamp of 0 and the last node is the max timestamp 64bit. The
152
+ * trailing zeros of each node represent the height (how many layers up the tree to go
153
+ * starting at the leaf node layer, specifically the leaf node at the start timestamp
154
+ * (cur)).
146
155
*
147
156
* @param uint64_t cur: the start timestamp
148
157
*
149
158
* @return height (distance from leaf node layer)
150
159
*/
151
- uint8_t compute_h_bnd1 (uint64_t cur ) {
152
-
153
- // Case when start timestamp is first possible timestamp, so corresponding parent key is 64 layers from the leaf node
160
+ uint8_t compute_h_bnd1 (uint64_t cur ) {
161
+ // Case when start timestamp is first possible timestamp, so corresponding parent key
162
+ // is 64 layers from the leaf node
154
163
if (cur == 0 ) {
155
164
return 64 ;
156
165
}
0 commit comments