summaryrefslogtreecommitdiff
path: root/controller/fw/src/crypto.c
blob: 73ad7832215f34c3cc9d0ac602433fcfff3bdf4c (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
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include <aes.h>

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

void debug_hexdump(const char *name, uint8_t *buf, size_t len);
void debug_hexdump(const char *name, 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");
}

int oob_message_received(uint8_t msg[static OOB_TRIGGER_LEN]) {
    struct AES_ctx ctx;
    uint8_t buf[crypto_sign_BYTES];

    for (size_t serial=0; serial<PRESIG_STORE_SIZE; serial++) {
        for (size_t dom=0; dom<_TRIGGER_DOMAIN_COUNT; dom++) {

            DEBUG_PRINT("Trying domain %zd serial %zd", dom, serial);
            debug_hexdump("oob_presig_iv", oob_presig_iv, sizeof(oob_presig_iv));

            memcpy(buf, presig_store[dom][serial], crypto_sign_BYTES);
            debug_hexdump("presig", buf, sizeof(buf));
            AES_init_ctx_iv(&ctx, msg, oob_presig_iv);
            AES_CBC_decrypt_buffer(&ctx, buf, crypto_sign_BYTES);
            debug_hexdump("decrypted", buf, sizeof(buf));

            if (!crypto_sign_verify_detached(buf, presig_messages[dom][serial], PRESIG_MSG_LEN, oob_trigger_pubkey)) {
                oob_trigger_activated(dom, presig_first_serial + serial);
                return 1;
            }
            DEBUG_PRINTN("\n");
        }
    }

    return 0;
}