Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
gec.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Michal Podhradsky <mpodhradsky@galois.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
27 #ifndef SPPRZ_GEC_H
28 #define SPPRZ_GEC_H
29 
30 #include "std.h"
31 #include "mcu_periph/rng.h"
32 #include "generated/keys_uav.h"
33 #include "../ext/hacl-c/Hacl_Ed25519.h"
34 #include "../ext/hacl-c/Hacl_Curve25519.h"
35 #include "../ext/hacl-c/Hacl_SHA2_512.h"
36 #include "../ext/hacl-c/Hacl_Chacha20Poly1305.h"
37 #include "../ext/hacl-c/kremlib.h"
38 
39 #define PPRZ_MSG_TYPE_PLAINTEXT 0xaa
40 #define PPRZ_MSG_TYPE_ENCRYPTED 0x55
41 // index of encrypted/payload byte
42 #define PPRZ_GEC_IDX 0
43 // index of the beginning of the counter
44 #define PPRZ_CNTR_IDX 1
45 // index of the beginning of the authenticated bytes
46 #define PPRZ_AUTH_IDX 5
47 
48 // legth of the message signature
49 #define PPRZ_SIGN_LEN 64
50 // lenght of a hash for key derivation
51 #define PPRZ_HASH_LEN 64
52 // length of the encryption keys
53 #define PPRZ_KEY_LEN 32
54 // length of the message authentication tag
55 #define PPRZ_MAC_LEN 16
56 // length of the message nonce
57 #define PPRZ_NONCE_LEN 12
58 // length of the counter
59 #define PPRZ_COUNTER_LEN 4
60 // length of the crypto overhead (4 bytes of counter + 16 bytes of tag)
61 #define PPRZ_CRYPTO_OVERHEAD 20
62 // basepoint value for the scalar curve multiplication
63 #define PPRZ_CURVE_BASEPOINT 9
64 
65 typedef unsigned char ed25519_signature[64];
66 
67 struct gec_privkey {
70 };
71 
72 struct gec_pubkey {
74 };
75 
76 struct gec_sym_key {
80 };
81 
82 typedef enum {
84 } stage_t;
85 
86 typedef enum {
89 
90 typedef enum {
93 
94 typedef enum {
96  // RESPONDER ERRORS
102  // INITIATOR ERRORS
107  // BOTH PARTIES
111 } sts_error_t;
112 
113 // Intermediate data structure containing information relating to the stage of
114 // the STS protocol.
115 struct gec_sts_ctx {
120  struct gec_sym_key rx_sym_key;
121  struct gec_sym_key tx_sym_key;
128 };
129 
130 void gec_sts_init(struct gec_sts_ctx *sts);
131 void gec_clear_sts(struct gec_sts_ctx *sts);
134 
135 void gec_counter_to_bytes(uint32_t n, uint8_t *bytes);
137 
138 #endif /* SPPRZ_GEC_H */
struct sts3032 sts
struct gec_pubkey their_public_key
Definition: gec.h:116
uint8_t pub[PPRZ_KEY_LEN]
Definition: gec.h:73
stage_t
Definition: gec.h:82
@ INIT
Definition: gec.h:83
@ WAIT_MSG2
Definition: gec.h:83
@ WAIT_MSG3
Definition: gec.h:83
@ CRYPTO_OK
Definition: gec.h:83
@ WAIT_MSG1
Definition: gec.h:83
#define PPRZ_KEY_LEN
Definition: gec.h:53
uint8_t pub[PPRZ_KEY_LEN]
Definition: gec.h:69
void gec_derive_key_material(struct gec_sts_ctx *sts, uint8_t *z)
Derive key material for both sender and receiver.
Definition: gec.c:91
struct gec_privkey my_private_ephemeral
Definition: gec.h:119
bool ready
Definition: gec.h:73
struct gec_sym_key tx_sym_key
Definition: gec.h:121
uint8_t nonce[PPRZ_NONCE_LEN]
Definition: gec.h:78
uint32_t encrypt_err
Definition: gec.h:126
void gec_counter_to_bytes(uint32_t n, uint8_t *bytes)
Convert counter to bytes in network byte order.
Definition: gec.c:142
uint8_t priv[PPRZ_KEY_LEN]
Definition: gec.h:68
void gec_clear_sts(struct gec_sts_ctx *sts)
Definition: gec.c:54
unsigned char ed25519_signature[64]
Definition: gec.h:65
gec_sts_msg_type_t
Definition: gec.h:90
@ P_BE
Definition: gec.h:91
@ P_AE
Definition: gec.h:91
@ SIG
Definition: gec.h:91
uint32_t rx_counter_err
Definition: gec.h:125
void gec_sts_init(struct gec_sts_ctx *sts)
Definition: gec.c:33
void gec_generate_ephemeral_keys(struct gec_privkey *sk)
Generate private and public key pairs for future use.
Definition: gec.c:73
struct gec_pubkey their_public_ephemeral
Definition: gec.h:118
party_t
Definition: gec.h:86
@ RESPONDER
Definition: gec.h:87
@ CLIENT
Definition: gec.h:87
@ INVALID_PARTY
Definition: gec.h:87
@ INITIATOR
Definition: gec.h:87
party_t party
Definition: gec.h:123
stage_t protocol_stage
Definition: gec.h:122
uint32_t counter
Definition: gec.h:79
sts_error_t
Definition: gec.h:94
@ UNEXPECTED_MSG_ERROR
Definition: gec.h:110
@ MSG3_ENCRYPT_ERROR
Definition: gec.h:106
@ MSG3_SIGNVERIFY_ERROR
Definition: gec.h:101
@ MSG3_TIMEOUT_ERROR
Definition: gec.h:99
@ ERROR_NONE
Definition: gec.h:95
@ MSG1_ENCRYPT_ERROR
Definition: gec.h:98
@ MSG2_DECRYPT_ERROR
Definition: gec.h:104
@ MSG1_TIMEOUT_ERROR
Definition: gec.h:97
@ MSG3_DECRYPT_ERROR
Definition: gec.h:100
@ UNEXPECTED_MSG_TYPE_ERROR
Definition: gec.h:108
@ MSG2_SIGNVERIFY_ERROR
Definition: gec.h:105
@ UNEXPECTED_MSG_DATA_ERROR
Definition: gec.h:109
@ MSG2_TIMEOUT_ERROR
Definition: gec.h:103
uint32_t decrypt_err
Definition: gec.h:127
#define PPRZ_NONCE_LEN
Definition: gec.h:57
sts_error_t last_error
Definition: gec.h:124
bool ready
Definition: gec.h:69
bool ready
Definition: gec.h:79
struct gec_sym_key rx_sym_key
Definition: gec.h:120
uint32_t gec_bytes_to_counter(uint8_t *bytes)
Convert from network byte order (big endian) to the machine byte order.
Definition: gec.c:117
uint8_t key[PPRZ_KEY_LEN]
Definition: gec.h:77
struct gec_privkey my_private_key
Definition: gec.h:117
Definition: gec.h:72
arch independent Random Number Generator API
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98