1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! Sealed boxes are designed to anonymously send messages to a recipient given
//! its public key.
//!
//! Only the recipient can decrypt these messages, using its private key. While
//! the recipient can verify the integrity of the message, it cannot verify the
//! identity of the sender.
//!
//! A message is encrypted using an ephemeral key pair, whose secret part is
//! destroyed right after the encryption process.
//!
//! Without knowing the secret key used for a given message, the sender cannot
//! decrypt its own message later. And without additional data, a message cannot
//! be correlated with the identity of its sender.
use ;
use ;
use secmem;
// 32 bytes.
pub const PUBLICKEYBYTES: usize = 32;
// 32 bytes.
pub const SECRETKEYBYTES: usize = 32;
// 32 bytes.
pub const ZEROBYTES: usize = 32;
// 16 bytes.
pub const BOXZEROBYTES: usize = 16;
// 16 bytes.
pub const MACBYTES: usize = ZEROBYTES - BOXZEROBYTES;
// 48 bytes.
pub const SEALBYTES: usize = PUBLICKEYBYTES + MACBYTES;
extern "C"
/// The *seal()* function encrypts a message for a recipients public key. It
/// returns the ciphertext.
///
/// The function creates a new key pair for each message, and attaches the
/// public key to the ciphertext. The secret key is overwritten and is not
/// accessible after this function returns.
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::init;
/// use sodium_sys::crypto::asymmetrickey::{sealbox,auth_keypair};
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Create a keypair and activate for use.
/// let theirkeypair = auth_keypair::KeyPair::new().unwrap();
/// theirkeypair.activate_pk();
///
/// // Generate the ciphertext and protect it as readonly.
/// let ciphertext = sealbox::seal(b"test",
/// theirkeypair.pk_bytes()).unwrap();
/// println!("{:?}", ciphertext);
/// ```
/// The *open()* function decrypts the ciphertext using the key pair (pk, sk),
/// returns the message.
///
/// This function doesn't require passing the public key of the sender, as the
/// ciphertext already includes this information.
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::init;
/// use sodium_sys::crypto::asymmetrickey::{sealbox,auth_keypair};
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Create another keypair and activate for use.
/// let keypair = auth_keypair::KeyPair::new().unwrap();
/// keypair.activate_pk();
/// keypair.activate_sk();
///
/// // Generate the ciphertext and protect it as readonly.
/// let ciphertext = sealbox::seal(b"test", keypair.pk_bytes()).unwrap();
///
/// // Decrypt the ciphertext.
/// let message = sealbox::open(ciphertext,
/// keypair.pk_bytes(),
/// keypair.sk_bytes()).unwrap();
/// assert!(b"test" == message);
/// ```