holochain_deterministic_integrity/
x_salsa20_poly1305.rs

1use crate::prelude::*;
2
3/// Libsodium secret-key authenticated encryption: secretbox_open
4///
5/// Opens encrypted data created by secretbox.
6///
7/// If the encrypted data fails authentication and cannot be decrypted this function returns None.
8///
9/// This means that if any decrypted data is returned by this function it was created by a holder
10/// of the shared key and has not been tampered with.
11///
12/// See [aeads](https://www.imperialviolet.org/2015/05/16/aeads.html)
13pub fn x_salsa20_poly1305_decrypt(
14    key_ref: XSalsa20Poly1305KeyRef,
15    encrypted_data: XSalsa20Poly1305EncryptedData,
16) -> ExternResult<Option<XSalsa20Poly1305Data>> {
17    HDI.with(|h| {
18        h.borrow()
19            .x_salsa20_poly1305_decrypt(XSalsa20Poly1305Decrypt::new(key_ref, encrypted_data))
20    })
21}
22
23/// Libsodium keypair based authenticated encryption: box_open
24///
25/// Opens encrypted data created by box.
26///
27/// If the encrypted data fails authentication and cannot be decrypted this function returns [ `None` ].
28///
29/// This means that if any decrypted data is returned by this function it was created by _either_
30/// keypair and has not been tampered with.
31///
32/// See <https://www.imperialviolet.org/2015/05/16/aeads.html>
33pub fn x_25519_x_salsa20_poly1305_decrypt(
34    recipient: X25519PubKey,
35    sender: X25519PubKey,
36    encrypted_data: XSalsa20Poly1305EncryptedData,
37) -> ExternResult<Option<XSalsa20Poly1305Data>> {
38    HDI.with(|h| {
39        h.borrow()
40            .x_25519_x_salsa20_poly1305_decrypt(X25519XSalsa20Poly1305Decrypt::new(
41                recipient,
42                sender,
43                encrypted_data,
44            ))
45    })
46}