ruma_identifiers/
crypto_algorithms.rs

1//! Key algorithms used in Matrix spec.
2
3#[cfg(feature = "serde")]
4use ruma_serde::{DeserializeFromCowStr, SerializeAsRefStr};
5use ruma_serde_macros::{AsRefStr, DisplayAsRefStr, FromString};
6
7use crate::PrivOwnedStr;
8
9/// The basic key algorithms in the specification.
10///
11/// This type can hold an arbitrary string. To check for algorithms that are not available as a
12/// documented variant here, use its string representation, obtained through `.as_str()`.
13#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, DisplayAsRefStr, FromString)]
14#[non_exhaustive]
15#[ruma_enum(rename_all = "snake_case")]
16#[cfg_attr(feature = "serde", derive(DeserializeFromCowStr, SerializeAsRefStr))]
17pub enum DeviceKeyAlgorithm {
18    /// The Ed25519 signature algorithm.
19    Ed25519,
20
21    /// The Curve25519 ECDH algorithm.
22    Curve25519,
23
24    /// The Curve25519 ECDH algorithm, but the key also contains signatures
25    SignedCurve25519,
26
27    #[doc(hidden)]
28    _Custom(PrivOwnedStr),
29}
30
31/// The signing key algorithms defined in the Matrix spec.
32///
33/// This type can hold an arbitrary string. To check for algorithms that are not available as a
34/// documented variant here, use its string representation, obtained through `.as_str()`.
35#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, DisplayAsRefStr, FromString)]
36#[non_exhaustive]
37#[ruma_enum(rename_all = "snake_case")]
38#[cfg_attr(feature = "serde", derive(DeserializeFromCowStr, SerializeAsRefStr))]
39pub enum SigningKeyAlgorithm {
40    /// The Ed25519 signature algorithm.
41    Ed25519,
42
43    #[doc(hidden)]
44    _Custom(PrivOwnedStr),
45}
46
47/// An encryption algorithm to be used to encrypt messages sent to a room.
48///
49/// This type can hold an arbitrary string. To check for algorithms that are not available as a
50/// documented variant here, use its string representation, obtained through `.as_str()`.
51#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, DisplayAsRefStr, FromString)]
52#[non_exhaustive]
53#[cfg_attr(feature = "serde", derive(DeserializeFromCowStr, SerializeAsRefStr))]
54pub enum EventEncryptionAlgorithm {
55    /// Olm version 1 using Curve25519, AES-256, and SHA-256.
56    #[ruma_enum(rename = "m.olm.v1.curve25519-aes-sha2")]
57    OlmV1Curve25519AesSha2,
58
59    /// Megolm version 1 using AES-256 and SHA-256.
60    #[ruma_enum(rename = "m.megolm.v1.aes-sha2")]
61    MegolmV1AesSha2,
62
63    #[doc(hidden)]
64    _Custom(PrivOwnedStr),
65}
66
67#[cfg(test)]
68mod tests {
69    use super::{DeviceKeyAlgorithm, SigningKeyAlgorithm};
70
71    #[test]
72    fn parse_device_key_algorithm() {
73        assert_eq!(DeviceKeyAlgorithm::from("ed25519"), DeviceKeyAlgorithm::Ed25519);
74        assert_eq!(DeviceKeyAlgorithm::from("curve25519"), DeviceKeyAlgorithm::Curve25519);
75        assert_eq!(
76            DeviceKeyAlgorithm::from("signed_curve25519"),
77            DeviceKeyAlgorithm::SignedCurve25519
78        );
79    }
80
81    #[test]
82    fn parse_signing_key_algorithm() {
83        assert_eq!(SigningKeyAlgorithm::from("ed25519"), SigningKeyAlgorithm::Ed25519);
84    }
85
86    #[test]
87    #[cfg(feature = "serde")]
88    fn event_encryption_algorithm_serde() {
89        use ruma_serde::test::serde_json_eq;
90        use serde_json::json;
91
92        use super::EventEncryptionAlgorithm;
93
94        serde_json_eq(EventEncryptionAlgorithm::MegolmV1AesSha2, json!("m.megolm.v1.aes-sha2"));
95        serde_json_eq(
96            EventEncryptionAlgorithm::OlmV1Curve25519AesSha2,
97            json!("m.olm.v1.curve25519-aes-sha2"),
98        );
99        serde_json_eq(EventEncryptionAlgorithm::from("io.ruma.test"), json!("io.ruma.test"));
100    }
101}