summaryrefslogtreecommitdiff
path: root/reset-controller/fw/src/crypto.c
blob: f4f79a453f2fd7cd9c4f4f305e1e8461fe5de928 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <assert.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include <sodium.h>

#include "crypto.h"
#include "simulation.h"


void debug_hexdump(const char *name, const uint8_t *buf, size_t len);
int verify_trigger_dom(const uint8_t inkey[PRESIG_MSG_LEN],
        const char *domain_string, const uint8_t refkey[PRESIG_MSG_LEN]);


void debug_hexdump(const char *name, const uint8_t *buf, size_t len) {
    DEBUG_PRINTN("%20s: ", name);
    for (size_t i=0; i<len;) {
        for (size_t j=0; j<8 && i<len; i++, j++)
            DEBUG_PRINTN("%02x ", buf[i]);
        DEBUG_PRINTN(" ");
    }
    DEBUG_PRINTN("\n");
}

/* Returns trigger sig height for correct trigger */
int verify_trigger_dom(const uint8_t inkey[PRESIG_MSG_LEN],
        const char *domain_string, const uint8_t refkey[PRESIG_MSG_LEN]) {
    uint8_t key[crypto_auth_hmacsha512_KEYBYTES];
    uint8_t key_out[crypto_auth_hmacsha512_BYTES];

    static_assert(PRESIG_MSG_LEN <= crypto_auth_hmacsha512_KEYBYTES);
    memcpy(key, inkey, PRESIG_MSG_LEN);
    memset(key + PRESIG_MSG_LEN, 0, sizeof(key) - PRESIG_MSG_LEN);
    DEBUG_PRINT("ds \"%s\"", domain_string);
    debug_hexdump("ref", refkey, PRESIG_MSG_LEN);

    for (int i=0; i<presig_height; i++) {
        DEBUG_PRINT("Verifying height rel %d abs %d", i, presig_height-i);
        debug_hexdump("key", key, sizeof(key));
        (void)crypto_auth_hmacsha512(key_out, (uint8_t *)domain_string, strlen(domain_string), key);
        debug_hexdump("out", key_out, sizeof(key_out));
        memcpy(key, key_out, PRESIG_MSG_LEN);
        memset(key + PRESIG_MSG_LEN, 0, sizeof(key) - PRESIG_MSG_LEN);
        
        if (!memcmp(key, refkey, PRESIG_MSG_LEN))
            return presig_height-i;
    }

    return 0;
}

int verify_trigger(const uint8_t inkey[PRESIG_MSG_LEN], int *height_out, int *domain_out) {
    int res;
    for (int i=0; i<_TRIGGER_DOMAIN_COUNT; i++) {
        DEBUG_PRINT("Verifying domain %d", i);
        if ((res = verify_trigger_dom(inkey, presig_domain_strings[i], presig_keys[i]))) {
            DEBUG_PRINT("Match!");
            if (height_out)
                *height_out = res - 1;
            if (domain_out)
                *domain_out = i;
            return 1;
        }
    }
    return 0;
}

int oob_message_received(uint8_t msg[static OOB_TRIGGER_LEN]) {
    int height, domain;
    if (verify_trigger(msg, &height, &domain)) {
        oob_trigger_activated(domain, height);
        return 1;
    }

    return 0;
}