ccx-utils

Miscellaneous utilities written in C
git clone https://ccx.te2000.cz/git/ccx-utils
Log | Files | Refs

commit 526fce1ea18343535048def444d86cc2cf22da98
parent 6a91f5a9d2af86320aef98b04ce1915c718b7da0
Author: Jan Pobrislo <ccx@te2000.cz>
Date:   Sun,  6 Oct 2024 15:05:11 +0000

Basic parsing and HMAC computation in miniroon

Diffstat:
Msrc/miniroon.c | 78+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 65 insertions(+), 13 deletions(-)

diff --git a/src/miniroon.c b/src/miniroon.c @@ -42,7 +42,17 @@ typedef struct miniroon_header_s { } miniroon_header; +/* declarations */ +void parse_payload(bytebuffer *payload); +void parse_header(miniroon_header *header, bytebuffer *source); +void parse_caveat(bytebuffer *source); // TODO +void read_secret(bytebuffer *secret); // TODO +void hmac_b2s_256(const bytebuffer *key, const bytebuffer *msg, bytebuffer *output); +#define MINIROON_HMAC_SIZE 32 +#define MINIROON_HMAC_FUNC(key, msg, out) hmac_b2s_256(key, msg, out) + +/* definitions */ void netstring_chunk_init (netstring_chunk *chunk, const bytebuffer source) { memset(chunk, 0, sizeof(netstring_chunk)); chunk->source = source; @@ -122,7 +132,7 @@ void parse_header(miniroon_header *header, bytebuffer *outer) { } -int handle_payload(size_t payload_size) { +void handle_payload(size_t payload_size) { char payload[payload_size+1]; char *read_next = payload; ssize_t read_size; @@ -143,22 +153,62 @@ int handle_payload(size_t payload_size) { strerr_dief1x(111, "Invalid netstring terminator"); } - bytebuffer input, chunk, next; - input.data = payload; - input.len = payload_size; + bytebuffer payload_bb = {payload, payload_size}; + parse_payload(&payload_bb); +} - if(!netstring_get_chunk(&input, &chunk, &next)) { - strerr_dief1x(111, "Malformed netstring"); +void parse_caveat(bytebuffer *source) { + // TODO +} + +void read_secret(bytebuffer *secret){ + assert(secret->len == MINIROON_HMAC_SIZE); + // TODO + memset(secret->data, 0, secret->len); +} + +void parse_payload(bytebuffer *payload) { + netstring_chunk c; + netstring_chunk_init(&c, *payload); + + if(!netstring_chunk_next(&c)) { + strerr_dief1x(111, "Mising miniroon header"); } miniroon_header hdr; - parse_header(&hdr, &chunk); + parse_header(&hdr, &c.inner); + // header should be verified by now, we can start hashing + uint8_t hmac_data[MINIROON_HMAC_SIZE]; + bytebuffer hmac_bb = {hmac_data, MINIROON_HMAC_SIZE}; + read_secret(&hmac_bb); + MINIROON_HMAC_FUNC(&hmac_bb, &c.inner, &hmac_bb); + + if(!netstring_chunk_next(&c)) { + strerr_dief1x(111, "Mising miniroon body"); + } + netstring_chunk body; + netstring_chunk_init(&body, c.inner); - while(next.len) { - input = next; - if(!netstring_get_chunk(&input, &chunk, &next)) { - strerr_dief1x(111, "Malformed netstring"); - } + while(netstring_chunk_next(&body)) { + parse_caveat(&body.inner); + MINIROON_HMAC_FUNC(&hmac_bb, &body.inner, &hmac_bb); + } + if(!netstring_chunk_next(&c)) { + strerr_dief1x(111, "Mising miniroon signature"); + } + if(c.inner.len != MINIROON_HMAC_SIZE) { + strerr_dief1x(111, "Mising miniroon signature length"); + } + /* constant time hash compare */ + uint8_t bitdiff = 0; + for(size_t i=0; i<=MINIROON_HMAC_SIZE; i++) { + bitdiff |= hmac_data[i] ^ c.inner.data[i]; + } + if(netstring_chunk_next(&c)) { + strerr_dief1x(111, "Extraneous data in miniroon"); + } + if(bitdiff) { + strerr_dief1x(111, "Invalid miniroon signature"); } } @@ -232,7 +282,9 @@ int main (int argc, char const *const *argv) break; case 1: if(read_char == ':') { - return handle_payload(payload_size); + handle_payload(payload_size); + strerr_dief1x(110, "Internal logic error, should not get here"); + return 1; } else if(read_char >= '0' && read_char <= '9') { payload_size *= 10; payload_size += read_char - '0';