summaryrefslogtreecommitdiff
path: root/fw/src/noise.h
blob: 19699451b8e93ebefb0be741c4fbbaf161e166d5 (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
#ifndef __NOISE_H__
#define __NOISE_H__

#include <stdint.h>

#include <noise/protocol.h>

#include "usart_helpers.h"
#include "rand_stm32.h"


#define CURVE25519_KEY_LEN 32
#define MAX_HOST_PACKET_SIZE 128


extern volatile uint8_t host_packet_buf[MAX_HOST_PACKET_SIZE];
extern volatile int host_packet_length;

enum handshake_state {
    HANDSHAKE_UNINITIALIZED,
    HANDSHAKE_PHASE1,
    HANDSHAKE_PHASE2,
    HANDSHAKE_DONE_UNKNOWN_HOST,
    HANDSHAKE_DONE_KNOWN_HOST,
};

extern volatile enum handshake_state handshake_state;

struct NoiseState {
    NoiseHandshakeState *handshake;
    enum handshake_state handshake_state;
    NoiseCipherState *tx_cipher, *rx_cipher;
    uint8_t *local_key;
    uint8_t remote_key[CURVE25519_KEY_LEN];
    uint8_t *remote_key_reference;
    uint8_t handshake_hash[BLAKE2S_HASH_SIZE];
    int failed_handshakes;
};


void uninit_handshake(struct NoiseState *st, enum handshake_state new_state);
void noise_state_init(struct NoiseState *st, uint8_t *remote_key_reference, uint8_t *local_key);
void persist_remote_key(struct NoiseState *st);
int start_protocol_handshake(struct NoiseState *st);
int reset_protocol_handshake(struct NoiseState *st);
int generate_identity_key(struct NoiseState *st);
int try_continue_noise_handshake(struct NoiseState * const st, uint8_t *buf, size_t len);
int send_encrypted_message(struct NoiseState *st, const uint8_t *msg, size_t len);

/*@ assigns \nothing; */
void arm_key_scrubber(void);

/*@ assigns \nothing; */
void disarm_key_scrubber(void);

#endif