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
use ffi;
use libc::c_ulonglong;
use super::super::box_::curve25519xsalsa20poly1305 as box_;
const SEALBYTES: usize = ffi::crypto_box_SEALBYTES;
pub fn seal(m: &[u8],
&box_::PublicKey(ref pk): &box_::PublicKey) -> Vec<u8> {
let mut c = vec![0u8; m.len() + SEALBYTES];
unsafe {
ffi::crypto_box_seal(c.as_mut_ptr(),
m.as_ptr(), m.len() as c_ulonglong,
pk);
}
c
}
pub fn open(c: &[u8],
&box_::PublicKey(ref pk): &box_::PublicKey,
&box_::SecretKey(ref sk): &box_::SecretKey) -> Result<Vec<u8>, ()> {
if c.len() < SEALBYTES {
return Err(());
}
let mut m = vec![0u8; c.len() - SEALBYTES];
let ret = unsafe {
ffi::crypto_box_seal_open(m.as_mut_ptr(),
c.as_ptr(), c.len() as c_ulonglong,
pk, sk)
};
if ret == 0 {
Ok(m)
} else {
Err(())
}
}
#[cfg(test)]
mod test {
use super::*;
use super::super::super::box_::curve25519xsalsa20poly1305 as box_;
#[test]
fn test_seal_open() {
use randombytes::randombytes;
for i in 0..256usize {
let (pk, sk) = box_::gen_keypair();
let m = randombytes(i);
let c = seal(&m, &pk);
let opened = open(&c, &pk, &sk);
assert!(Ok(m) == opened);
}
}
#[test]
fn test_seal_open_tamper() {
use randombytes::randombytes;
for i in 0..32usize {
let (pk, sk) = box_::gen_keypair();
let m = randombytes(i);
let mut c = seal(&m, &pk);
for j in 0..c.len() {
c[j] ^= 0x20;
assert!(Err(()) == open(&mut c, &pk, &sk));
c[j] ^= 0x20;
}
}
}
}