Paparazzi UAS  v5.18.0_stable
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 {
88 } party_t;
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 {
128 };
129 
130 void gec_sts_init(struct gec_sts_ctx *sts);
131 void gec_clear_sts(struct gec_sts_ctx *sts);
133 void gec_derive_key_material(struct gec_sts_ctx *sts, uint8_t *z);
134 
135 void gec_counter_to_bytes(uint32_t n, uint8_t *bytes);
137 
138 #endif /* SPPRZ_GEC_H */
P_BE
@ P_BE
Definition: gec.h:91
PPRZ_KEY_LEN
#define PPRZ_KEY_LEN
Definition: gec.h:53
gec_sts_ctx::protocol_stage
stage_t protocol_stage
Definition: gec.h:122
stage_t
stage_t
Definition: gec.h:82
gec_clear_sts
void gec_clear_sts(struct gec_sts_ctx *sts)
Definition: gec.c:54
gec_sym_key::nonce
uint8_t nonce[PPRZ_NONCE_LEN]
Definition: gec.h:78
gec_sts_ctx::my_private_ephemeral
struct gec_privkey my_private_ephemeral
Definition: gec.h:119
P_AE
@ P_AE
Definition: gec.h:91
sts_error_t
sts_error_t
Definition: gec.h:94
ed25519_signature
unsigned char ed25519_signature[64]
Definition: gec.h:65
gec_pubkey::ready
bool ready
Definition: gec.h:73
INIT
@ INIT
Definition: gec.h:83
gec_sts_ctx::their_public_ephemeral
struct gec_pubkey their_public_ephemeral
Definition: gec.h:118
ERROR_NONE
@ ERROR_NONE
Definition: gec.h:95
gec_sts_ctx::encrypt_err
uint32_t encrypt_err
Definition: gec.h:126
gec_sts_ctx::rx_counter_err
uint32_t rx_counter_err
Definition: gec.h:125
gec_sts_ctx::party
party_t party
Definition: gec.h:123
uint32_t
unsigned long uint32_t
Definition: types.h:18
PPRZ_NONCE_LEN
#define PPRZ_NONCE_LEN
Definition: gec.h:57
INITIATOR
@ INITIATOR
Definition: gec.h:87
INVALID_PARTY
@ INVALID_PARTY
Definition: gec.h:87
gec_privkey::ready
bool ready
Definition: gec.h:69
gec_derive_key_material
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
gec_bytes_to_counter
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
gec_privkey
Definition: gec.h:67
gec_sym_key::key
uint8_t key[PPRZ_KEY_LEN]
Definition: gec.h:77
rng.h
arch independent Random Number Generator API
gec_sts_ctx::tx_sym_key
struct gec_sym_key tx_sym_key
Definition: gec.h:121
std.h
gec_sym_key::ready
bool ready
Definition: gec.h:79
CRYPTO_OK
@ CRYPTO_OK
Definition: gec.h:83
SIG
@ SIG
Definition: gec.h:91
gec_pubkey::pub
uint8_t pub[PPRZ_KEY_LEN]
Definition: gec.h:73
MSG3_SIGNVERIFY_ERROR
@ MSG3_SIGNVERIFY_ERROR
Definition: gec.h:101
gec_sym_key::counter
uint32_t counter
Definition: gec.h:79
MSG3_ENCRYPT_ERROR
@ MSG3_ENCRYPT_ERROR
Definition: gec.h:106
gec_generate_ephemeral_keys
void gec_generate_ephemeral_keys(struct gec_privkey *sk)
Generate private and public key pairs for future use.
Definition: gec.c:73
gec_privkey::priv
uint8_t priv[PPRZ_KEY_LEN]
Definition: gec.h:68
gec_sts_ctx::last_error
sts_error_t last_error
Definition: gec.h:124
uint8_t
unsigned char uint8_t
Definition: types.h:14
gec_sts_ctx::my_private_key
struct gec_privkey my_private_key
Definition: gec.h:117
CLIENT
@ CLIENT
Definition: gec.h:87
WAIT_MSG2
@ WAIT_MSG2
Definition: gec.h:83
MSG2_SIGNVERIFY_ERROR
@ MSG2_SIGNVERIFY_ERROR
Definition: gec.h:105
UNEXPECTED_MSG_ERROR
@ UNEXPECTED_MSG_ERROR
Definition: gec.h:110
WAIT_MSG1
@ WAIT_MSG1
Definition: gec.h:83
MSG1_TIMEOUT_ERROR
@ MSG1_TIMEOUT_ERROR
Definition: gec.h:97
MSG3_DECRYPT_ERROR
@ MSG3_DECRYPT_ERROR
Definition: gec.h:100
MSG2_DECRYPT_ERROR
@ MSG2_DECRYPT_ERROR
Definition: gec.h:104
MSG3_TIMEOUT_ERROR
@ MSG3_TIMEOUT_ERROR
Definition: gec.h:99
gec_sym_key
Definition: gec.h:76
gec_sts_msg_type_t
gec_sts_msg_type_t
Definition: gec.h:90
gec_sts_ctx::their_public_key
struct gec_pubkey their_public_key
Definition: gec.h:116
UNEXPECTED_MSG_DATA_ERROR
@ UNEXPECTED_MSG_DATA_ERROR
Definition: gec.h:109
gec_sts_ctx
Definition: gec.h:115
MSG2_TIMEOUT_ERROR
@ MSG2_TIMEOUT_ERROR
Definition: gec.h:103
party_t
party_t
Definition: gec.h:86
WAIT_MSG3
@ WAIT_MSG3
Definition: gec.h:83
gec_sts_init
void gec_sts_init(struct gec_sts_ctx *sts)
Definition: gec.c:33
gec_sts_ctx::rx_sym_key
struct gec_sym_key rx_sym_key
Definition: gec.h:120
MSG1_ENCRYPT_ERROR
@ MSG1_ENCRYPT_ERROR
Definition: gec.h:98
gec_counter_to_bytes
void gec_counter_to_bytes(uint32_t n, uint8_t *bytes)
Convert counter to bytes in network byte order.
Definition: gec.c:142
gec_privkey::pub
uint8_t pub[PPRZ_KEY_LEN]
Definition: gec.h:69
gec_sts_ctx::decrypt_err
uint32_t decrypt_err
Definition: gec.h:127
RESPONDER
@ RESPONDER
Definition: gec.h:87
gec_pubkey
Definition: gec.h:72
UNEXPECTED_MSG_TYPE_ERROR
@ UNEXPECTED_MSG_TYPE_ERROR
Definition: gec.h:108