Skip to main content

floe_rs/types/header/
parameters.rs

1// Copyright 2026 Damir Jelić
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use zerocopy::{BigEndian, FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned};
17
18use crate::{FloeAead, FloeKdf};
19
20/// Information about the parameters a Floe session is using.
21#[derive(
22    Debug, Clone, Copy, PartialEq, Eq, FromBytes, IntoBytes, Unaligned, Immutable, KnownLayout,
23)]
24#[repr(C)]
25pub struct Parameters {
26    aead_id: u8,
27    kdf_id: u8,
28    segment_length: zerocopy::U32<BigEndian>,
29    floe_iv_size: zerocopy::U32<BigEndian>,
30}
31
32impl Parameters {
33    /// The length of the parameter info in the header, in bytes.
34    pub const LENGTH: usize = 10;
35
36    /// Create a new set of Floe parameters.
37    ///
38    /// This is the `PARAM_ENCODE(params) -> bytes` function from the [spec].
39    ///
40    /// # Panics
41    ///
42    /// This function will panic if the Floe IV length (N) is too large, it
43    /// needs to fit into a `u32`.
44    ///
45    /// [spec]: https://github.com/Snowflake-Labs/floe-specification/blob/main/spec/README.md#internal-functions
46    pub(crate) fn new<A, K, const N: usize, const S: u32>() -> Self
47    where
48        A: FloeAead,
49        K: FloeKdf,
50    {
51        // The floe IV length, needs to converted to an u32 as the Floe spec expects 4
52        // bytes. See the TODO item in the floe_iv.rs file how we can avoid this
53        // panic in the future.
54        #[allow(clippy::expect_used)]
55        let floe_iv_length =
56            u32::try_from(N).expect("the Floe IV is too long, it must be smaller than u32::MAX");
57        let floe_iv_length = zerocopy::U32::new(floe_iv_length);
58
59        Self {
60            aead_id: A::AEAD_ID,
61            kdf_id: K::KDF_ID,
62            segment_length: zerocopy::U32::new(S),
63            floe_iv_size: floe_iv_length,
64        }
65    }
66
67    /// Get the unique ID of the AEAD that is used for this Floe session.
68    pub fn aead_id(&self) -> u8 {
69        self.aead_id
70    }
71
72    /// Get the unique ID of the KDF implementation that is used for this Floe
73    /// session.
74    pub fn kdf_id(&self) -> u8 {
75        self.kdf_id
76    }
77
78    /// Get configured segment length of this Floe session.
79    pub fn segment_length(&self) -> u32 {
80        self.segment_length.get()
81    }
82
83    /// Get the size of the Floe initialization vector of this Floe session.
84    pub fn floe_iv_size(&self) -> u32 {
85        self.floe_iv_size.get()
86    }
87}