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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use deku::prelude::*;

mod addressee;
mod network;

pub use addressee::Addressee;
pub use network::{Control, Frame, HoppingControl};

use crate::types::VarInt;

/// Network Layer Security
/// SPEC: 7.4
#[derive(DekuRead, DekuWrite, Default, Debug, Copy, Clone, PartialEq)]
#[deku(bits = 3, type = "u8")]
pub enum NlsMethod {
    /// No security
    #[default]
    #[deku(id = "0x00")]
    None,

    /// Encryption only, Counter Mode
    #[deku(id = "0x01")]
    AesCtr,

    /// No encryption, Authentication, Cipher-block chaining with 128 bit MAC
    #[deku(id = "0x02")]
    AesCbcMac128,

    /// No encryption, Authentication, Cipher-block chaining with 64 bit MAC
    #[deku(id = "0x03")]
    AesCbcMac64,

    /// No encryption, Authentication, Cipher-block chaining with 32 bit MAC
    #[deku(id = "0x04")]
    AesCbcMac32,

    /// Authentication with CBC-MAC-128 and Encryption with Counter Mode
    #[deku(id = "0x05")]
    AesCcm128,

    /// Authentication with CBC-MAC-64 and Encryption with Counter Mode
    #[deku(id = "0x06")]
    AesCcm64,

    /// Authentication with CBC-MAC-32 and Encryption with Counter Mode
    #[deku(id = "0x07")]
    AesCcm32,
}

/// Encryption algorithm for over-the-air packets
#[derive(DekuRead, DekuWrite, Default, Debug, Clone, PartialEq)]
#[deku(ctx = "nls_method: NlsMethod", id = "nls_method")]
pub enum NlsState {
    #[default]
    #[deku(id = "NlsMethod::None")]
    None,
    #[deku(id = "NlsMethod::AesCtr")]
    AesCtr([u8; 5]),
    #[deku(id = "NlsMethod::AesCbcMac128")]
    AesCbcMac128([u8; 5]),
    #[deku(id = "NlsMethod::AesCbcMac64")]
    AesCbcMac64([u8; 5]),
    #[deku(id = "NlsMethod::AesCbcMac32")]
    AesCbcMac32([u8; 5]),
    #[deku(id = "NlsMethod::AesCcm128")]
    AesCcm128([u8; 5]),
    #[deku(id = "NlsMethod::AesCcm64")]
    AesCcm64([u8; 5]),
    #[deku(id = "NlsMethod::AesCcm32")]
    AesCcm32([u8; 5]),
}

#[derive(DekuRead, DekuWrite, Default, Debug, Copy, Clone, PartialEq)]
#[deku(bits = 2, type = "u8")]
pub enum AddressType {
    /// Broadcast to an estimated number of receivers, encoded in compressed format on a byte.
    #[default]
    #[deku(id = "0x00")]
    NbId,
    /// Broadcast to everyone
    #[deku(id = "0x01")]
    NoId,
    /// Unicast to target via its UID (Unique Dash7 ID)
    #[deku(id = "0x02")]
    Uid,
    /// Unicast to target via its VID (Virtual ID)
    #[deku(id = "0x03")]
    Vid,
}

#[derive(DekuRead, DekuWrite, Debug, Clone, PartialEq)]
#[deku(ctx = "address_type: AddressType", id = "address_type")]
pub enum Address {
    /// Broadcast to an estimated number of receivers, encoded in compressed format on a byte.
    #[deku(id = "AddressType::NbId")]
    NbId(VarInt),
    /// Broadcast to everyone
    #[deku(id = "AddressType::NoId")]
    NoId,
    /// Unicast to target via its UID (Unique Dash7 ID)
    #[deku(id = "AddressType::Uid")]
    Uid(#[deku(endian = "big")] u64),
    /// Unicast to target via its VID (Virtual ID)
    #[deku(id = "AddressType::Vid")]
    Vid(#[deku(endian = "big")] u16),
}

impl Default for Address {
    fn default() -> Self {
        Self::NbId(VarInt::default())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use hex_literal::hex;

    use crate::{transport::GroupCondition, test_tools::test_item};

    #[test]
    fn test_vid_aesccm32() {
        test_item(
            Addressee::new(
                false,
                GroupCondition::Any,
                Address::Vid(0xABCD),
                NlsState::AesCcm32(hex!("00 11 22 33 44")),
                0xFF,
            ),
            &hex!("37 FF ABCD 0011223344"),
        )
    }

    #[test]
    fn test_noid_none() {
        test_item(
            Addressee::new(false, GroupCondition::Any, Address::NoId, NlsState::None, 0),
            &[0b0010000, 0],
        );
    }

    #[test]
    fn test_nbid_none() {
        test_item(
            Addressee::new(
                false,
                GroupCondition::Any,
                Address::NbId(VarInt::new(0, false).unwrap()),
                NlsState::None,
                0,
            ),
            &[0, 0, 0],
        );
    }

    #[test]
    fn test_uid_none() {
        test_item(
            Addressee::new(
                false,
                GroupCondition::Any,
                Address::Uid(0),
                NlsState::None,
                0,
            ),
            &[0b00100000, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        );
    }

    #[test]
    fn test_nbid_aesctr() {
        test_item(
            Addressee::new(
                false,
                GroupCondition::Any,
                Address::NbId(VarInt::new(1, false).unwrap()),
                NlsState::AesCtr([0, 1, 2, 3, 4]),
                0,
            ),
            &[0b00000001, 0, 1, 0, 1, 2, 3, 4],
        );
    }

    #[test]
    fn test_vid_none() {
        test_item(
            Addressee::new(
                false,
                GroupCondition::Any,
                Address::Vid(0x1234),
                NlsState::None,
                5,
            ),
            &[0b00110000, 5, 0b00010010, 0b00110100],
        );
    }

    #[test]
    fn test_uid_none2() {
        test_item(
            Addressee::new(
                false,
                GroupCondition::Any,
                Address::Uid(0x1234567890123456),
                NlsState::None,
                105,
            ),
            &[
                0b00100000, 105, 0b00010010, 0b00110100, 0b01010110, 0b01111000, 0b10010000,
                0b00010010, 0b00110100, 0b01010110,
            ],
        );
    }

    #[test]
    fn test_noid_aescbcmac128() {
        test_item(
            Addressee::new(
                false,
                GroupCondition::Any,
                Address::NoId,
                NlsState::AesCbcMac128([10, 20, 30, 40, 50]),
                0xBE,
            ),
            &[0b00010010, 0xBE, 10, 20, 30, 40, 50],
        );
    }

    #[test]
    fn test_nbid_none2() {
        test_item(
            Addressee::new(
                false,
                GroupCondition::Any,
                Address::NbId(VarInt::new(100, false).unwrap()),
                NlsState::None,
                0,
            ),
            &[0, 0, 0x39],
        );
    }
}