Skip to content

Commit bee450a

Browse files
committed
sha3
1 parent 8a99a2f commit bee450a

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

decoder/src/cover.c

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
#include "crypto.h"
1+
#include "cover.h"
2+
3+
#include <limits.h>
4+
#include <stdint.h>
25
#include <stdio.h>
36
#include <stdlib.h>
47
#include <string.h>
5-
#include <stdint.h>
6-
#include <limits.h>
7-
#include "cover.h"
8+
9+
#include "crypto.h"
810

911
#ifndef ASCON_KEY_SIZE
1012
#define ASCON_KEY_SIZE 16
1113
#endif
1214

1315
/**
1416
* @brief Derive the correct child of this key and write it to dest.
15-
*
17+
*
1618
* @param current_key a pointer to the key we will derive from
1719
* @param start start timestamp of current key
1820
* @param height height of current key in the tree
@@ -22,34 +24,36 @@
2224
* @param dest_height where we will write the height of the derived key
2325
* @return int 0 on success, -1 on failure
2426
*/
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) {
2630
// ending timestamp of current key's tree/range
2731
uint64_t end = start + (1ULL << height) - 1;
2832
// midpoint of current key's tree/range
29-
uint64_t mid = start + (end-start)/2;
33+
uint64_t mid = start + (end - start) / 2;
3034

31-
//Traverse binary tree (one step)
32-
if(target_timestamp <= mid){
35+
// Traverse binary tree (one step)
36+
if (target_timestamp <= mid) {
3337
end = mid;
3438
} else {
3539
start = mid + 1;
3640
}
3741

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);
4045

41-
if (ret != 0){
42-
//Error deriving key
46+
if (ret != 0) {
47+
// Error deriving key
4348
return -1;
4449
}
4550

46-
//write the start and the updated height of the derived key
51+
// write the start and the updated height of the derived key
4752
*dest_start = start;
48-
*dest_height = height-1;
53+
*dest_height = height - 1;
4954
return 0;
5055
}
5156

52-
5357
/**
5458
* @brief Get the correct key from the tree. Manage the stack of keys. Derive and Discard
5559
* keys as appropriate
@@ -58,7 +62,7 @@ int derive_child(uint8_t* current_key, uint64_t start, uint8_t height, uint64_t
5862
* @param timestamp target timestamp
5963
* @return address of key, NULL on failure
6064
*/
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) {
6266
if (timestamp < stack->start[stack->n_keys - 1]) {
6367
return NULL; // the timeframe is before our subscription period
6468
}
@@ -95,20 +99,22 @@ int derive_child(uint8_t* current_key, uint64_t start, uint8_t height, uint64_t
9599
}
96100

97101
/** @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+
*
105110
* @param uint64_t end: the end timestamp that cannot be exceeded.
106111
* @param uint64_t cur: the start timestamp (not global start) that cannot be preceded.
107112
* @return uint8_t h: the height of the key which covers the given timestamp range.
108113
*/
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"
112118
uint64_t diff = end - cur + 1;
113119

114120
// 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
135141

136142
/** @brief A helper function to determine the correct key for a given start timestamp.
137143
* 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).
140147
*
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)).
146155
*
147156
* @param uint64_t cur: the start timestamp
148157
*
149158
* @return height (distance from leaf node layer)
150159
*/
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
154163
if (cur == 0) {
155164
return 64;
156165
}

0 commit comments

Comments
 (0)