hdi/
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}
47
48/// Libsodium crypto_box decryption, but converts ed25519 *signing*
49/// keys into x25519 encryption keys.
50/// WARNING: Please first understand the downsides of using this function:
51/// <https://doc.libsodium.org/advanced/ed25519-curve25519>
52pub fn ed_25519_x_salsa20_poly1305_decrypt(
53    recipient: AgentPubKey,
54    sender: AgentPubKey,
55    encrypted_data: XSalsa20Poly1305EncryptedData,
56) -> ExternResult<XSalsa20Poly1305Data> {
57    HDI.with(|h| {
58        h.borrow()
59            .ed_25519_x_salsa20_poly1305_decrypt(Ed25519XSalsa20Poly1305Decrypt::new(
60                recipient,
61                sender,
62                encrypted_data,
63            ))
64    })
65}