Skip to main content

vodozemac/hpke/
error.rs

1// Copyright 2026 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use thiserror::Error;
16
17use crate::KeyError;
18
19/// The error type for the HPKE message decoding failures.
20#[derive(Debug, Error)]
21pub enum MessageDecodeError {
22    /// The message failed to be decoded because the message isn't long enough.
23    #[error("The message doesn't contain enough bytes to be decoded")]
24    MessageIncomplete,
25    /// The initial message could not have been decoded, the embedded Curve25519
26    /// key is malformed.
27    #[error("The embedded ephemeral Curve25519 key could not have been decoded: {0:?}")]
28    KeyError(#[from] KeyError),
29    /// The ciphertext is not valid base64.
30    #[error("The ciphertext could not have been decoded from a base64 string: {0:?}")]
31    Base64(#[from] base64::DecodeError),
32}
33
34/// The Error type for the HPKE submodule.
35#[derive(Debug, Error)]
36pub enum Error {
37    /// Message decryption failed. Either the message was corrupted, the message
38    /// was replayed, or the wrong key is being used to decrypt the message.
39    #[error("Failed decrypting the message")]
40    Decryption,
41    /// The encapsulation of the initial message failed.
42    #[error("Failed decrypting the message")]
43    Encapsulation(#[from] hpke::HpkeError),
44}