s2n_quic_core/crypto/
packet_protection.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//= https://www.rfc-editor.org/rfc/rfc9001#section-5.1
5//# The current encryption level secret and the label "quic key" are
6//# input to the KDF to produce the AEAD key;
7pub const QUIC_KEY_LABEL: [u8; 8] = *b"quic key";
8
9//= https://www.rfc-editor.org/rfc/rfc9001#section-5.1
10//# the label "quic iv" is used
11//# to derive the Initialization Vector (IV); see Section 5.3.
12pub const QUIC_IV_LABEL: [u8; 7] = *b"quic iv";
13
14//= https://www.rfc-editor.org/rfc/rfc9001#section-5.1
15//# The header protection key uses the "quic hp" label; see Section 5.4.
16pub const QUIC_HP_LABEL: [u8; 7] = *b"quic hp";
17
18use core::fmt;
19use s2n_codec::DecoderError;
20
21/// Error type for errors during removal of packet protection
22#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
23#[cfg_attr(feature = "thiserror", derive(thiserror::Error))]
24pub struct Error {
25    pub reason: &'static str,
26}
27
28impl Error {
29    pub const DECODE_ERROR: Self = Self {
30        reason: "DECODE_ERROR",
31    };
32    pub const DECRYPT_ERROR: Self = Self {
33        reason: "DECRYPT_ERROR",
34    };
35    pub const INTERNAL_ERROR: Self = Self {
36        reason: "INTERNAL_ERROR",
37    };
38}
39
40impl fmt::Display for Error {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        if !self.reason.is_empty() {
43            self.reason.fmt(f)
44        } else {
45            write!(f, "packet_protection::Error")
46        }
47    }
48}
49
50impl fmt::Debug for Error {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        let mut d = f.debug_struct("packet_protection::Error");
53
54        if !self.reason.is_empty() {
55            d.field("reason", &self.reason);
56        }
57
58        d.finish()
59    }
60}
61
62impl From<DecoderError> for Error {
63    fn from(decoder_error: DecoderError) -> Self {
64        Self {
65            reason: decoder_error.into(),
66        }
67    }
68}