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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//! MAC Look-up Table
//!
//! This module contains the MAC Look-up Table defined in ANNEX C of the
//! [OSNMA SIS ICD v1.1](https://www.gsc-europa.eu/sites/default/files/sites/all/files/Galileo_OSNMA_SIS_ICD_v1.1.pdf).
//! and the supporting code required to use it.

use crate::bitfields::Adkd;
use core::fmt;

const MSG: usize = 2;

// Maximum value of nt in the MAC Look-up Table
const MAX_NT: usize = 10;

// Number of entries in the MAC Look-up Table
const MAC_LT_ENTRIES: usize = 12;

/// Maximum number of FLX entries in a single MAC Look-up Table sequence.
///
/// This constant is needed to dimension the buffer used in MACSEQ verification.
pub const MAX_FLX_ENTRIES: usize = 4;

// Constants used for defining MAC Look-up Table entries more briefly
const F00S: MacLTSlot = MacLTSlot::Fixed {
    adkd: Adkd::InavCed,
    object: AuthObject::SelfAuth,
};
const F00E: MacLTSlot = MacLTSlot::Fixed {
    adkd: Adkd::InavCed,
    object: AuthObject::CrossAuth,
};
const F04S: MacLTSlot = MacLTSlot::Fixed {
    adkd: Adkd::InavTiming,
    object: AuthObject::SelfAuth,
};
const F12S: MacLTSlot = MacLTSlot::Fixed {
    adkd: Adkd::SlowMac,
    object: AuthObject::SelfAuth,
};
const F12E: MacLTSlot = MacLTSlot::Fixed {
    adkd: Adkd::SlowMac,
    object: AuthObject::CrossAuth,
};
const FLX: MacLTSlot = MacLTSlot::Flex;

struct MacLTEntry {
    id: u8,
    nt: u8,
    // The first entry in the sequence is omitted, since it is always 00S and is
    // not looked up, because it corresponds to tag0.
    //
    // Inexistent entries in the sequence are filled with FLX.
    //
    // Entries with Msg = 1 (currently none of these exist) use
    // the same values in the two arrays of `sequence`.
    sequence: [[MacLTSlot; MAX_NT - 1]; MSG],
}

// MAC Look-up Table
static MACLT: [MacLTEntry; MAC_LT_ENTRIES] = [
    MacLTEntry {
        id: 27,
        nt: 6,
        sequence: [
            [F00E, F00E, F00E, F12S, F00E, FLX, FLX, FLX, FLX],
            [F00E, F00E, F04S, F12S, F00E, FLX, FLX, FLX, FLX],
        ],
    },
    MacLTEntry {
        id: 28,
        nt: 10,
        sequence: [
            [F00E, F00E, F00E, F00S, F00E, F00E, F12S, F00E, F00E],
            [F00E, F00E, F00S, F00E, F00E, F04S, F12S, F00E, F00E],
        ],
    },
    MacLTEntry {
        id: 31,
        nt: 5,
        sequence: [
            [F00E, F00E, F12S, F00E, FLX, FLX, FLX, FLX, FLX],
            [F00E, F00E, F12S, F04S, FLX, FLX, FLX, FLX, FLX],
        ],
    },
    MacLTEntry {
        id: 33,
        nt: 6,
        sequence: [
            [F00E, F04S, F00E, F12S, F00E, FLX, FLX, FLX, FLX],
            [F00E, F00E, F12S, F00E, F12E, FLX, FLX, FLX, FLX],
        ],
    },
    MacLTEntry {
        id: 34,
        nt: 6,
        sequence: [
            [FLX, F04S, FLX, F12S, F00E, FLX, FLX, FLX, FLX],
            [FLX, F00E, F12S, F00E, F12E, FLX, FLX, FLX, FLX],
        ],
    },
    MacLTEntry {
        id: 35,
        nt: 6,
        sequence: [
            [FLX, F04S, FLX, F12S, FLX, FLX, FLX, FLX, FLX],
            [FLX, FLX, F12S, FLX, FLX, FLX, FLX, FLX, FLX],
        ],
    },
    MacLTEntry {
        id: 36,
        nt: 5,
        sequence: [
            [FLX, F04S, FLX, F12S, FLX, FLX, FLX, FLX, FLX],
            [FLX, F00E, F12S, F12E, FLX, FLX, FLX, FLX, FLX],
        ],
    },
    MacLTEntry {
        id: 37,
        nt: 5,
        sequence: [
            [F00E, F04S, F00E, F12S, FLX, FLX, FLX, FLX, FLX],
            [F00E, F00E, F12S, F12E, FLX, FLX, FLX, FLX, FLX],
        ],
    },
    MacLTEntry {
        id: 38,
        nt: 5,
        sequence: [
            [FLX, F04S, FLX, F12S, FLX, FLX, FLX, FLX, FLX],
            [FLX, FLX, F12S, FLX, FLX, FLX, FLX, FLX, FLX],
        ],
    },
    MacLTEntry {
        id: 39,
        nt: 4,
        sequence: [
            [FLX, F04S, FLX, FLX, FLX, FLX, FLX, FLX, FLX],
            [FLX, F00E, F12S, FLX, FLX, FLX, FLX, FLX, FLX],
        ],
    },
    MacLTEntry {
        id: 40,
        nt: 4,
        sequence: [
            [F00E, F04S, F12S, FLX, FLX, FLX, FLX, FLX, FLX],
            [F00E, F00E, F12E, FLX, FLX, FLX, FLX, FLX, FLX],
        ],
    },
    MacLTEntry {
        id: 41,
        nt: 4,
        sequence: [
            [FLX, F04S, FLX, FLX, FLX, FLX, FLX, FLX, FLX],
            [FLX, FLX, F12S, FLX, FLX, FLX, FLX, FLX, FLX],
        ],
    },
];

/// Looks up an entry in the MAC Look-up Table.
///
/// This function looks up and returns the entry of the MAC Look-up Table
/// corresponding to a `maclt` ID, message number `msg` (either zero or one) and
/// tag number `num_tag`. If the entry does not exist in the table, an error is
/// returned.
///
/// # Panics
///
/// This function panics if `msg` is not zero or one, or if `num_tag` is zero.
pub fn get_maclt_entry(maclt: u8, msg: usize, num_tag: usize) -> Result<MacLTSlot, MacLTError> {
    assert!((msg == 0) || (msg == 1));
    assert!(num_tag >= 1);
    let Some(entry) = MACLT.iter().find(|&x| x.id == maclt) else {
        return Err(MacLTError::InvalidMaclt);
    };
    if num_tag >= entry.nt.into() {
        return Err(MacLTError::InvalidTagNumber);
    }
    let entry = entry.sequence[msg][num_tag - 1];
    // Enforce that InavTiming must use SelfAuth as AuthObject
    if let MacLTSlot::Fixed { adkd, object } = entry {
        assert!(adkd != Adkd::InavTiming || object == AuthObject::SelfAuth);
    }
    Ok(entry)
}

/// Returns an iterator over the indices corresponding to FLX entries.
///
/// This function returns an iterator over the indices corresponding to FLX
/// entries for a particular `maclt` ID and message number `msg` (either zero or
/// one). If the ID does not exist in the table, an error is returned.
///
/// # Panics
///
/// This function panics if `msg` is not zero or one.
pub fn get_flx_indices(maclt: u8, msg: usize) -> Result<impl Iterator<Item = usize>, MacLTError> {
    assert!((msg == 0) || (msg == 1));
    let Some(entry) = MACLT.iter().find(|&x| x.id == maclt) else {
        return Err(MacLTError::InvalidMaclt);
    };
    Ok(entry.sequence[msg]
        .iter()
        .take(usize::from(entry.nt) - 1)
        .enumerate()
        .filter_map(|(j, &x)| if x == FLX { Some(j + 1) } else { None }))
}

/// MAC Look-up Table slot.
///
/// This enum represents a slot in the MAC Look-up Table.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum MacLTSlot {
    /// Fixed slot.
    ///
    /// A fixed slot, such as '00S', '04S', '12S', '00E', or '12E'. It is
    /// composed by an ADKD and an authentication object.
    Fixed {
        /// ADKD of the fixed slot.
        ///
        /// In the MAC Look-up Table it is represented by the numeric code of
        /// the ADKD ('00', '04', or '12').
        adkd: Adkd,
        /// Authentication object of the fixed slot.
        ///
        /// In the MAC Look-up Table it is represented by a character ('S' or
        /// 'E').
        object: AuthObject,
    },
    /// Flexible slot.
    ///
    /// Flexible slots are represented by 'FLX' in the MAC Look-up Table.
    Flex,
}

/// Authentication object.
///
/// This enum lists the possible objects that are authenticated by a MAC Look-up
/// Table entry.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum AuthObject {
    /// Self-authentication ('S' in the MAC Look-up Table entry).
    SelfAuth,
    /// Galileo Cross-authentication ('E' in the MAC Look-up Table entry).
    CrossAuth,
}

/// Errors produced during MAC Table look-up.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum MacLTError {
    /// The value of the MACLT does not appear as an ID in the MAC Look-up
    /// Table.
    InvalidMaclt,
    /// The tag number is greater than the number of tags 'nt' in the MAC
    /// Look-up Table entry.
    InvalidTagNumber,
}

impl fmt::Display for MacLTError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MacLTError::InvalidMaclt => "invalid MAC look-up table ID".fmt(f),
            MacLTError::InvalidTagNumber => "invalid tag number".fmt(f),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for MacLTError {}

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

    #[test]
    fn lookups() {
        assert_eq!(get_maclt_entry(34, 0, 1), Ok(FLX));
        assert_eq!(get_maclt_entry(34, 0, 2), Ok(F04S));
        assert_eq!(get_maclt_entry(34, 1, 5), Ok(F12E));
        assert_eq!(get_maclt_entry(26, 0, 1), Err(MacLTError::InvalidMaclt));
        assert_eq!(get_maclt_entry(34, 0, 6), Err(MacLTError::InvalidTagNumber));
    }

    #[test]
    #[should_panic]
    fn lookup_wrong_msg() {
        let _ = get_maclt_entry(34, 2, 1);
    }

    #[test]
    #[should_panic]
    fn lookup_wrong_tag_number() {
        let _ = get_maclt_entry(34, 0, 0);
    }

    /// Checks that the `MAX_FLX_ENTRIES` constant has the correct value.
    #[test]
    fn max_flx_entries() {
        let max = MACLT
            .iter()
            .map(|entry| {
                entry
                    .sequence
                    .iter()
                    .map(|s| {
                        s.iter()
                            .take(usize::from(entry.nt) - 1)
                            .filter(|&&x| x == FLX)
                            .count()
                    })
                    .max()
                    .unwrap()
            })
            .max()
            .unwrap();
        assert_eq!(max, MAX_FLX_ENTRIES);
    }

    #[test]
    fn flx_indices() {
        let indices = get_flx_indices(34, 0).unwrap().collect::<Vec<_>>();
        assert_eq!(&indices, &[1, 3]);
        let indices = get_flx_indices(34, 1).unwrap().collect::<Vec<_>>();
        assert_eq!(&indices, &[1]);
    }
}