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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use maidsafe_utilities::serialisation::{deserialise, serialise, SerialisationError};
use priv_prelude::*;
use rust_sodium::crypto::box_::{PublicKey, SecretKey};
use secure_serialisation::{
anonymous_deserialise, anonymous_serialise, deserialise as secure_deserialise,
serialise as secure_serialise, Error as SecureSerialiseError,
};
use serde::de::DeserializeOwned;
use serde::Serialize;
quick_error! {
#[derive(Debug)]
pub enum CryptoError {
Serialize(e: SerialisationError) {
description("Error serializing message")
display("Error serializing message: {}", e)
cause(e)
}
Deserialize(e: SerialisationError) {
description("Error deserializing message")
display("Error deserializing message: {}", e)
cause(e)
}
Encrypt(e: SecureSerialiseError) {
description("Error encrypting message")
display("Error encrypting message: {:?}", e)
}
Decrypt(e: SecureSerialiseError) {
description("Error decrypting message")
display("Error decrypting message: {:?}", e)
}
EncryptForbidden {
description("Encrypt operation is not allowed within this crypto context")
}
DecryptForbidden {
description("Decrypt operation is not allowed within this crypto context")
}
}
}
#[derive(Clone, Debug)]
pub enum CryptoContext {
Null,
Authenticated {
their_pk: PublicKey,
our_sk: SecretKey,
},
AnonymousDecrypt {
our_pk: PublicKey,
our_sk: SecretKey,
},
AnonymousEncrypt {
their_pk: PublicKey,
},
}
impl CryptoContext {
pub fn authenticated(their_pk: PublicKey, our_sk: SecretKey) -> Self {
CryptoContext::Authenticated { their_pk, our_sk }
}
pub fn null() -> Self {
CryptoContext::Null
}
pub fn anonymous_decrypt(our_pk: PublicKey, our_sk: SecretKey) -> Self {
CryptoContext::AnonymousDecrypt { our_pk, our_sk }
}
pub fn anonymous_encrypt(their_pk: PublicKey) -> Self {
CryptoContext::AnonymousEncrypt { their_pk }
}
pub fn encrypt<T: Serialize>(&self, msg: &T) -> Result<BytesMut, CryptoError> {
match *self {
CryptoContext::Null => serialise(msg)
.map_err(CryptoError::Serialize)
.map(BytesMut::from),
CryptoContext::Authenticated {
ref their_pk,
ref our_sk,
} => secure_serialise(msg, their_pk, our_sk)
.map_err(CryptoError::Encrypt)
.map(BytesMut::from),
CryptoContext::AnonymousEncrypt { ref their_pk } => anonymous_serialise(msg, their_pk)
.map_err(CryptoError::Encrypt)
.map(BytesMut::from),
CryptoContext::AnonymousDecrypt { .. } => Err(CryptoError::DecryptForbidden),
}
}
pub fn decrypt<T>(&self, msg: &[u8]) -> Result<T, CryptoError>
where
T: Serialize + DeserializeOwned,
{
match *self {
CryptoContext::Null => deserialise(msg).map_err(CryptoError::Deserialize),
CryptoContext::Authenticated {
ref their_pk,
ref our_sk,
} => secure_deserialise(msg, their_pk, our_sk).map_err(CryptoError::Decrypt),
CryptoContext::AnonymousDecrypt {
ref our_pk,
ref our_sk,
} => anonymous_deserialise(msg, our_pk, our_sk).map_err(CryptoError::Decrypt),
CryptoContext::AnonymousEncrypt { .. } => Err(CryptoError::EncryptForbidden),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
mod crypto_context {
use super::*;
use rust_sodium::crypto::box_::gen_keypair;
#[test]
fn when_encryption_is_null_it_serializes_and_deserializes_data() {
let crypto = CryptoContext::null();
let encrypted = unwrap!(crypto.encrypt(b"test123"));
let decrypted: [u8; 7] = unwrap!(crypto.decrypt(&encrypted[..]));
assert_eq!(&decrypted, b"test123");
}
#[test]
fn when_encryption_keys_are_given_it_encrypts_and_decrypts_data_with_them() {
let (pk1, sk1) = gen_keypair();
let (pk2, sk2) = gen_keypair();
let crypto1 = CryptoContext::authenticated(pk2, sk1);
let crypto2 = CryptoContext::authenticated(pk1, sk2);
let encrypted = unwrap!(crypto1.encrypt(b"test123"));
let decrypted: [u8; 7] = unwrap!(crypto2.decrypt(&encrypted[..]));
assert_eq!(&decrypted, b"test123");
}
#[test]
fn anonymous_encryption() {
let (pk2, sk2) = gen_keypair();
let crypto1 = CryptoContext::anonymous_encrypt(pk2);
let crypto2 = CryptoContext::anonymous_decrypt(pk2, sk2);
let encrypted = unwrap!(crypto1.encrypt(b"test123"));
let decrypted: [u8; 7] = unwrap!(crypto2.decrypt(&encrypted[..]));
assert_eq!(&decrypted, b"test123");
}
}
}