aranya-crypto 0.14.1

The Aranya Cryptography Engine
Documentation
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use core::{
    fmt,
    hash::{Hash, Hasher},
    iter,
    marker::PhantomData,
    slice,
};

use derive_where::derive_where;
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use sha3_utils::{EncodedString, encode_string};
use spideroak_crypto::{
    hash,
    kdf::{self, Expand, Kdf as _, KdfError},
    oid::{Identified, Oid},
    typenum::Unsigned as _,
};

use crate::ciphersuite::CipherSuite;

/// Extension trait for [`CipherSuite`].
///
/// Its primary purpose is to provide convenience methods that
/// include the OIDs of the cipher suite's algorithms.
pub(crate) trait CipherSuiteExt: CipherSuite {
    /// Computes the following hash:
    ///
    /// ```text
    /// digest = TupleHash(
    ///     tag,
    ///     oids[0],
    ///     oids[1],
    ///     ..,
    ///     oids[n],
    ///     context,
    /// )
    /// ```
    fn tuple_hash<'a, I>(tag: &'a [u8], context: I) -> Digest<Self>
    where
        I: IntoIterator<Item = &'a [u8]>;

    /// Performs `LabeledExtract` per [RFC 9180].
    ///
    /// - `domain` provides domain separation. See [RFC 9180]
    ///   section 9.6.
    /// - `salt`, `label`, and `ikm` are regular KDF parameters.
    ///
    /// ```text
    /// def LabeledExtract(salt, label, ikm):
    ///     labeled_ikm = concat(domain, suite_id, label, ikm)
    ///     return Extract(salt, labeled_ikm)
    /// ```
    ///
    /// Note that in [RFC 9180] `suite_id` contains 16-bit HPKE
    /// algorithm identifiers, but in this function it contains
    /// OIDs. Since an OID does not have a fixed length, each OID
    /// is unambiguously encoded with [`encode_string`].
    ///
    /// [RFC 9180]: https://www.rfc-editor.org/rfc/rfc9180.html#name-cryptographic-dependencies
    fn labeled_extract<'a>(
        domain: &'static [u8],
        salt: &[u8],
        label: &'static [u8],
        ikm: impl IntoIterator<Item = &'a [u8]>,
    ) -> Prk<Self>;

    /// Performs `LabeledExpand` per [RFC 9180].
    ///
    /// - `domain` provides domain separation. See [RFC 9180]
    ///   section 9.6.
    /// - `prk`, `label`, and `info` are regular KDF parameters.
    ///
    /// ```text
    /// def LabeledExpand(prk, label, info):
    ///     labeled_info = concat(I2OSP(L, 2), domain, suite_id,
    ///                   label, info)
    ///     return Expand(prk, labeled_info)
    /// ```
    ///
    /// Note that in [RFC 9180] `suite_id` contains 16-bit HPKE
    /// algorithm identifiers, but in this function it contains
    /// OIDs. Since an OID does not have a fixed length, each OID
    /// is unambiguously encoded with [`encode_string`].
    ///
    /// [RFC 9180]: https://www.rfc-editor.org/rfc/rfc9180.html#name-cryptographic-dependencies
    fn labeled_expand<T, const N: usize>(
        domain: &'static [u8],
        prk: &Prk<Self>,
        label: &'static [u8],
        info: [&[u8]; N],
    ) -> Result<T, KdfError>
    where
        T: Expand;
}

impl<CS: CipherSuite> CipherSuiteExt for CS {
    fn tuple_hash<'a, I>(tag: &'a [u8], context: I) -> Digest<Self>
    where
        I: IntoIterator<Item = &'a [u8]>,
    {
        let iter = iter::once(tag)
            .chain(CS::OIDS.into_iter().map(Oid::as_bytes))
            .chain(context);
        hash::tuple_hash::<Self::Hash, _>(iter)
    }

    fn labeled_extract<'a>(
        domain: &'static [u8],
        salt: &[u8],
        label: &'static [u8],
        ikm: impl IntoIterator<Item = &'a [u8]>,
    ) -> Prk<Self> {
        let labeled_ikm = iter::once(domain)
            .chain(CS::OIDS.encode())
            .chain(iter::once(label))
            .chain(ikm);
        Self::Kdf::extract_multi(labeled_ikm, salt)
    }

    fn labeled_expand<T, const N: usize>(
        domain: &'static [u8],
        prk: &Prk<Self>,
        label: &'static [u8],
        info: [&[u8]; N],
    ) -> Result<T, KdfError>
    where
        T: Expand,
    {
        let size = T::Size::U16.to_be_bytes();
        let labeled_info = iter::once(&size)
            .map(AsRef::as_ref)
            .chain(iter::once(domain))
            .chain(
                #[allow(clippy::useless_conversion, reason = "It helps with type inference")]
                CS::OIDS.encode().into_iter(),
            )
            .chain(iter::once(label))
            .chain(info.iter().copied());
        T::expand_multi::<Self::Kdf, _>(prk, labeled_info)
    }
}

pub(crate) type Digest<CS> = hash::Digest<<<CS as CipherSuite>::Hash as hash::Hash>::DigestSize>;
pub(crate) type Prk<CS> = kdf::Prk<<<CS as CipherSuite>::Kdf as kdf::Kdf>::PrkSize>;

/// The OIDs used by a [`CipherSuite`].
#[derive_where(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Oids<CS: CipherSuite + ?Sized> {
    aead: AeadOid<CS>,
    hash: HashOid<CS>,
    kdf: KdfOid<CS>,
    kem: KemOid<CS>,
    mac: MacOid<CS>,
    signer: SignerOid<CS>,
}

impl<CS: CipherSuite + ?Sized> Oids<CS> {
    pub(super) const fn new() -> Self {
        Self {
            aead: AeadOid::<CS>(PhantomData),
            hash: HashOid::<CS>(PhantomData),
            kdf: KdfOid::<CS>(PhantomData),
            kem: KemOid::<CS>(PhantomData),
            mac: MacOid::<CS>(PhantomData),
            signer: SignerOid::<CS>(PhantomData),
        }
    }

    /// Returns the OIDs as an array.
    const fn all() -> [&'static Oid; 6] {
        const {
            [
                AeadOid::<CS>::OID,
                HashOid::<CS>::OID,
                KdfOid::<CS>::OID,
                KemOid::<CS>::OID,
                MacOid::<CS>::OID,
                SignerOid::<CS>::OID,
            ]
        }
    }

    /// Encods the OIDs with [`encode_string`].
    pub(crate) const fn encode(self) -> EncodedOids<CS> {
        EncodedOids::<CS>(PhantomData)
    }
}

impl<CS> IntoIterator for Oids<CS>
where
    CS: CipherSuite,
{
    type Item = &'static Oid;
    type IntoIter = iter::Copied<slice::Iter<'static, &'static Oid>>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        const { Self::all() }.iter().copied()
    }
}

/// Encodes the OIDs with [`encode_string`], then flattens the
/// [`EncodedString`]s into their parts.
///
/// [TupleHash]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-185.pdf
#[derive_where(Copy, Clone, Debug)]
pub(crate) struct EncodedOids<CS: ?Sized>(PhantomData<CS>);

impl<CS: CipherSuite + ?Sized> EncodedOids<CS> {
    const ITEMS: [&'static [u8]; 12] = {
        // ([0], [1]) = AEAD.into_parts()
        // ([2], [3]) = HASH.into_parts()
        // ...
        // ([10], [11]) = SIGNER.into_parts()
        let mut buf: [&[u8]; 12] = [&[]; 12];
        let mut i = 0;
        let mut j = 0;
        while i < Self::ENCODED.len() {
            let (p, s) = Self::ENCODED[i].as_parts();
            buf[j] = p.as_bytes();
            buf[j + 1] = s;
            i += 1;
            j += 2;
        }
        buf
    };

    /// All OIDs encoded with [`encode_string`].
    const ENCODED: [EncodedString<'static>; 6] = {
        let mut buf = [encode_string(b""); 6];
        let mut i = 0;
        while i < buf.len() {
            buf[i] = encode_string(Oids::<CS>::all()[i].as_bytes());
            i += 1;
        }
        buf
    };
}

impl<CS> IntoIterator for EncodedOids<CS>
where
    CS: CipherSuite + ?Sized,
{
    type Item = &'static [u8];
    type IntoIter = iter::Copied<slice::Iter<'static, &'static [u8]>>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        Self::ITEMS.iter().copied()
    }
}

/// Creates a ZST that serializes and deserializes as a specific
/// OID.
macro_rules! oid_repr {
    ($($name:ident => $ty:ident),* $(,)?) => {$(
        #[derive_where(Copy, Clone, Debug, Eq, PartialEq)]
        struct $name<CS: ?Sized>(PhantomData<CS>);

        impl<CS: CipherSuite + ?Sized> Identified for $name<CS> {
            const OID: &Oid = <<CS as CipherSuite>::$ty as Identified>::OID;
        }

        impl<CS: CipherSuite + ?Sized> Hash for $name<CS> {
            fn hash<H: Hasher>(&self, state: &mut H) {
                Self::OID.hash(state);
            }
        }

        #[automatically_derived]
        impl<CS> Serialize for $name<CS>
        where
            CS: CipherSuite + ?Sized,
        {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: Serializer,
            {
                Self::OID.serialize(serializer)
            }
        }

        #[automatically_derived]
        impl<'de, CS> Deserialize<'de> for $name<CS>
        where
            CS: CipherSuite + ?Sized,
        {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: Deserializer<'de>,
            {
                struct OidVisitor<CS: ?Sized>(PhantomData<CS>);

                impl<'de, CS> de::Visitor<'de> for OidVisitor<CS>
                where
                    CS: CipherSuite + ?Sized,
                {
                    type Value = $name<CS>;

                    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                        f.write_str("an OID")
                    }

                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
                    where
                        E: de::Error,
                    {
                        if $name::<CS>::OID != v {
                            Err(de::Error::custom("unexpected OID"))
                        } else {
                            Ok($name(PhantomData))
                        }
                    }

                    fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
                    where
                        E: de::Error,
                    {
                        if $name::<CS>::OID != v {
                            Err(de::Error::custom("unexpected OID"))
                        } else {
                            Ok($name(PhantomData))
                        }
                    }
                }
                deserializer.deserialize_bytes(OidVisitor(PhantomData))
            }
        }
    )*};
}
oid_repr! {
    AeadOid => Aead,
    HashOid => Hash,
    KdfOid => Kdf,
    KemOid => Kem,
    MacOid => Mac,
    SignerOid => Signer,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        default::DefaultCipherSuite as CS,
        test_util::{assert_ct_eq, assert_ct_ne},
    };

    mod labeled_extract {
        use super::*;

        #[test]
        fn test_smoke() {
            let lhs =
                CS::labeled_extract(b"domain", b"salt", b"label", iter::once::<&[u8]>(b"ikm"));
            let rhs =
                CS::labeled_extract(b"domain", b"salt", b"label", iter::once::<&[u8]>(b"ikm"));
            assert_ct_eq!(lhs, rhs);
        }

        #[test]
        fn test_different_inputs() {
            let tests = [
                (
                    "domain",
                    (b"domain", b"salt", b"label", b"ikm"),
                    (b"DOMAIN", b"salt", b"label", b"ikm"),
                ),
                (
                    "salt",
                    (b"domain", b"salt", b"label", b"ikm"),
                    (b"domain", b"SALT", b"label", b"ikm"),
                ),
                (
                    "label",
                    (b"domain", b"salt", b"label", b"ikm"),
                    (b"domain", b"salt", b"LABEL", b"ikm"),
                ),
                (
                    "ikm",
                    (b"domain", b"salt", b"label", b"ikm"),
                    (b"domain", b"salt", b"label", b"IKM"),
                ),
            ];
            for (i, (name, lhs, rhs)) in tests.iter().enumerate() {
                let lhs = CS::labeled_extract(lhs.0, lhs.1, lhs.2, iter::once::<&[u8]>(lhs.3));
                let rhs = CS::labeled_extract(rhs.0, rhs.1, rhs.2, iter::once::<&[u8]>(rhs.3));
                assert_ct_ne!(lhs, rhs, "#{i}: `{name}`:");
            }
        }
    }

    mod labeled_expand {
        use super::*;

        #[test]
        fn test_smoke() {
            let prk =
                CS::labeled_extract(b"domain", b"salt", b"label", iter::once::<&[u8]>(b"ikm"));
            let lhs: [u8; 16] =
                CS::labeled_expand(b"domain", &prk, b"label", [b"ikm", b"info"]).unwrap();
            let rhs: [u8; 16] =
                CS::labeled_expand(b"domain", &prk, b"label", [b"ikm", b"info"]).unwrap();
            assert_ct_eq!(lhs[..], rhs[..]);
        }

        #[test]
        fn test_different_inputs() {
            let prk1 =
                CS::labeled_extract(b"domain", b"salt", b"label", iter::once::<&[u8]>(b"ikm"));
            let prk2 =
                CS::labeled_extract(b"DOMAIN", b"SALT", b"LABEL", iter::once::<&[u8]>(b"IKM"));
            #[allow(
                clippy::type_complexity,
                reason = "I wouldn't need this if Rust's type inference were better"
            )]
            let tests: [(_, (_, _, _, [&[u8]; 1]), (_, _, _, [&[u8]; 1])); 4] = [
                (
                    "domain",
                    (b"domain", &prk1, b"label", [b"info"]),
                    (b"DOMAIN", &prk1, b"label", [b"info"]),
                ),
                (
                    "prk",
                    (b"domain", &prk1, b"label", [b"info"]),
                    (b"domain", &prk2, b"label", [b"info"]),
                ),
                (
                    "label",
                    (b"domain", &prk1, b"label", [b"info"]),
                    (b"domain", &prk1, b"LABEL", [b"info"]),
                ),
                (
                    "info",
                    (b"domain", &prk1, b"label", [b"info"]),
                    (b"domain", &prk1, b"label", [b"INFO"]),
                ),
            ];
            for (i, (name, lhs, rhs)) in tests.iter().enumerate() {
                let lhs: [u8; 16] = CS::labeled_expand(lhs.0, lhs.1, lhs.2, lhs.3).unwrap();
                let rhs: [u8; 16] = CS::labeled_expand(rhs.0, rhs.1, rhs.2, rhs.3).unwrap();
                assert_ct_ne!(lhs[..], rhs[..], "#{i}: `{name}`:");
            }
        }

        #[test]
        fn test_info_concat() {
            let prk =
                CS::labeled_extract(b"domain", b"salt", b"label", iter::once::<&[u8]>(b"ikm"));

            macro_rules! tests {
                ($(($lhs:expr, $rhs:expr),)*) => {$({
                    let lhs: [u8; 16] =
                        CS::labeled_expand(b"domain", &prk, b"label", $lhs).unwrap();
                    let rhs: [u8; 16] =
                        CS::labeled_expand(b"domain", &prk, b"label", $rhs).unwrap();
                    assert_ct_eq!(lhs[..], rhs[..], "{} != {}",
                        stringify!($lhs), stringify!($rhs));
                })*};
            }
            tests! {
                ([], []),
                ([], [&[], &[]]),
                ([], [b"", b""]),
                ([&[]], [&[], &[]]),
                ([b"info"], [b"info"]),
                ([b"info", b""], [b"info", b""]),
                ([b"", b"info"], [b"", b"info"]),
                ([b"info", b"in", b"fo"], [b"info", b"in", b"fo"]),
                ([b"in", b"fo", b"info"], [b"in", b"fo", b"info"]),
            }
        }
    }
}