Skip to main content

hopr_crypto_types/
seal.rs

1use libp2p_identity::PeerId;
2
3use crate::keypairs::OffchainKeypair;
4
5/// **CURRENTLY NOT IMPLEMENTED**, see https://github.com/hoprnet/hoprnet/issues/7172
6///
7/// Performs randomized encryption of the given data so that
8/// only the recipient with the given `peer_id` can [decrypt it](unseal_data).
9pub fn seal_data(_data: &[u8], _peer_id: PeerId) -> crate::errors::Result<Box<[u8]>> {
10    // TODO: sealing not implemented, see https://github.com/hoprnet/hoprnet/issues/7172"
11    Err(crate::errors::CryptoError::SealingError)
12}
13
14/// **CURRENTLY NOT IMPLEMENTED**, see https://github.com/hoprnet/hoprnet/issues/7172
15///
16/// Decrypts data previously encrypted with [`seal_data`].
17///
18/// The given `keypair` must correspond to the `peer_id` given during encryption.
19pub fn unseal_data(_data: &[u8], _keypair: &OffchainKeypair) -> crate::errors::Result<Box<[u8]>> {
20    // TODO: sealing not implemented, see https://github.com/hoprnet/hoprnet/issues/7172"
21    Err(crate::errors::CryptoError::SealingError)
22}
23
24#[cfg(test)]
25mod tests {
26    use std::ops::Not;
27
28    use hex_literal::hex;
29
30    use super::*;
31    use crate::keypairs::Keypair;
32
33    #[ignore = "sealing is not implemented yet, see https://github.com/hoprnet/hoprnet/issues/7172"]
34    #[test]
35    fn seal_unseal_should_be_identity() -> anyhow::Result<()> {
36        let data = "some test data".to_string();
37
38        let keypair = OffchainKeypair::random();
39
40        let sealed = seal_data(data.as_bytes(), keypair.public().into())?;
41
42        let unsealed = String::from_utf8(unseal_data(&sealed, &keypair)?.into_vec())?;
43
44        assert_eq!(data, unsealed);
45
46        Ok(())
47    }
48
49    #[ignore = "sealing is not implemented yet, see https://github.com/hoprnet/hoprnet/issues/7172"]
50    #[test]
51    fn unseal_should_fail_with_different_private_key() -> anyhow::Result<()> {
52        let data = "some test data".to_string();
53
54        let keypair_1 = OffchainKeypair::random();
55        let keypair_2 = OffchainKeypair::random();
56
57        let sealed = seal_data(data.as_bytes(), keypair_1.public().into())?;
58
59        assert_eq!(
60            Err(crate::errors::CryptoError::SealingError),
61            unseal_data(&sealed, &keypair_2)
62        );
63
64        Ok(())
65    }
66
67    #[ignore = "sealing is not implemented yet, see https://github.com/hoprnet/hoprnet/issues/7172"]
68    #[test]
69    fn unseal_should_fail_when_ciphertext_has_been_tampered_with() -> anyhow::Result<()> {
70        let data = "some test data".to_string();
71
72        let keypair = OffchainKeypair::random();
73
74        let mut sealed = seal_data(data.as_bytes(), keypair.public().into())?;
75        sealed[1] = sealed[1].not();
76
77        assert_eq!(
78            Err(crate::errors::CryptoError::SealingError),
79            unseal_data(&sealed, &keypair)
80        );
81
82        Ok(())
83    }
84
85    #[ignore = "sealing is not implemented yet, see https://github.com/hoprnet/hoprnet/issues/7172"]
86    #[test]
87    fn unseal_fixed_test() -> anyhow::Result<()> {
88        let data = hex!(
89            "d7538951e728a28c6381a481f9f33111b6d78211bd1d6a286bdf1b16ee1ad35837b5b0ffcd3b308a4fa9939af0a208150418629c7af31ad457d3fe51602dc9b5f0da253fb44ec0fb75cac9e0bcb9a3ef"
90        );
91        let peer_id: PeerId = "12D3KooWHcCWDKzMkypyLWvri5ioSVivCazU8jgbWzyerM5aMuf8".parse()?;
92
93        let keypair = OffchainKeypair::from_secret(&hex!(
94            "1142b6483e171aa577baea2290797023cd14e034d36f9febb975772ac2924c00"
95        ))?;
96        assert_eq!(PeerId::from(keypair.public()), peer_id);
97
98        let pt = String::from_utf8(unseal_data(&data, &keypair)?.into_vec())?;
99
100        assert_eq!("Hello, this is a secret message!", pt);
101
102        Ok(())
103    }
104}