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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
pub mod decryptor;
pub mod encryptor;
pub mod recipient;

use bs58;
use bson;
use serde::{Deserialize, Serialize};
use serde_bytes;

#[cfg(target_os = "ios")]
use crate::strings;

#[cfg(target_os = "ios")]
use std::os::raw::c_char;

use crate::{error::CoreError, message::recipient::MessageRecipientHeader};

pub(crate) const MAC_KEY_LABEL: &[u8] = b"ntge-message-mac-key";
pub(crate) const PAYLOAD_KEY_LABEL: &[u8] = b"ntge-message-payload";

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageMeta {
    pub timestamp: Option<String>,
    pub signature: Option<encryptor::Signature>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageMac {
    #[serde(with = "serde_bytes")]
    pub mac: Vec<u8>, // 32 bytes
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessagePayload {
    #[serde(with = "serde_bytes")]
    pub nonce: Vec<u8>, // 16 bytes
    #[serde(with = "serde_bytes")]
    pub ciphertext: Vec<u8>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    pub recipient_headers: Vec<MessageRecipientHeader>,
    pub meta: MessageMeta,
    pub mac: MessageMac,
    pub payload: MessagePayload,
}

#[allow(dead_code)]
impl Message {
    pub fn serialize_to_armor(&self) -> Result<String, CoreError> {
        match self.serialize_to_base58() {
            Ok(base58) => Ok(format!("MsgBegin_{}_EndMsg", base58)),
            Err(e) => Err(e),
        }
    }

    pub fn deserialize_from_armor(text: &str) -> Result<Message, CoreError> {
        let text = text.trim();
        let has_prefix = text.starts_with("MsgBegin_");
        let has_suffix = text.ends_with("_EndMsg");

        if has_prefix && has_suffix {
            let text = text.trim_start_matches("MsgBegin_");
            let text = text.trim_end_matches("_EndMsg");
            Message::deserialize_from_base58(text)
        } else {
            // no armor
            let e = CoreError::MessageSerializationError {
                name: "Message",
                reason: "cannot deserialize message armor text",
            };
            Err(e)
        }
    }
}

impl Message {
    pub fn serialize_to_base58(&self) -> Result<String, CoreError> {
        self.serialize_to_bson_bytes()
            .map(|bytes| bs58::encode(bytes).into_string())
            .map_err(|_| CoreError::MessageSerializationError {
                name: "Message",
                reason: "cannot encode message bytes to base58",
            })
    }

    pub fn deserialize_from_base58(text: &str) -> Result<Message, CoreError> {
        let bytes = match bs58::decode(text).into_vec() {
            Ok(bytes) => bytes,
            Err(_) => {
                let e = CoreError::MessageSerializationError {
                    name: "Message",
                    reason: "cannot decode message base58 text",
                };
                return Err(e);
            }
        };

        Message::deserialize_from_bson_bytes(&bytes)
    }
}

#[allow(dead_code)]
impl Message {
    fn serialize_to_bson_bytes(&self) -> Result<Vec<u8>, CoreError> {
        let document = match bson::to_bson(&self) {
            Ok(encoded) => {
                if let bson::Bson::Document(document) = encoded {
                    document
                } else {
                    let e = CoreError::MessageSerializationError {
                        name: "Message",
                        reason: "cannot encode message to bson",
                    };
                    return Err(e);
                }
            }
            Err(err) => {
                print!("{:?}", err);
                let e = CoreError::MessageSerializationError {
                    name: "Message",
                    reason: "cannot encode message to bson",
                };
                return Err(e);
            }
        };

        let mut bytes = Vec::new();
        match bson::encode_document(&mut bytes, &document) {
            Ok(_) => Ok(bytes),
            Err(_) => {
                let e = CoreError::KeyDeserializeError {
                    name: "Message",
                    reason: "cannot encode message bson document to bytes",
                };
                Err(e)
            }
        }
    }

    fn deserialize_from_bson_bytes(bytes: &Vec<u8>) -> Result<Message, CoreError> {
        let document = match bson::decode_document(&mut std::io::Cursor::new(&bytes[..])) {
            Ok(document) => document,
            Err(_) => {
                let e = CoreError::KeyDeserializeError {
                    name: "Message",
                    reason: "cannot decode bson bytes to message document",
                };
                return Err(e);
            }
        };

        let result: Result<Message, _> = bson::from_bson(bson::Bson::Document(document));
        result.map_err(|_| CoreError::KeyDeserializeError {
            name: "Message",
            reason: "cannot decode message document to message",
        })
    }
}

impl Drop for Message {
    fn drop(&mut self) {
        if cfg!(feature = "drop-log-enable") {
            println!("{:?} is being deallocated", self);
        }
    }
}

#[no_mangle]
#[cfg(target_os = "ios")]
pub unsafe extern "C" fn c_message_destory(message: *mut Message) {
    let _ = Box::from_raw(message);
}

#[no_mangle]
#[cfg(target_os = "ios")]
pub unsafe extern "C" fn c_message_serialize_to_armor(
    message: *mut Message,
    armor: *mut *mut c_char,
) -> i32 {
    let message = &mut *message;
    match message.serialize_to_armor() {
        Ok(text) => {
            let result = strings::string_to_c_char(text);
            *armor = result;
            return 0;
        }
        Err(_) => {
            // TODO:
            return 1;
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        aead,
        ed25519::keypair::Ed25519Keypair,
        message::{self, decryptor, encryptor, Message},
        x25519::{filekey::FileKey, private::X25519PrivateKey, public::X25519PublicKey},
    };

    use hkdf::Hkdf;
    use rand::rngs::OsRng;
    use rand::RngCore;
    use secrecy::ExposeSecret;
    use sha2::Sha256;

    #[allow(dead_code)]
    fn encrypt_plaintext(file_key: &FileKey, plaintext: &[u8]) -> Vec<u8> {
        // prepare nonce
        let mut nonce = [0; 16];
        OsRng.fill_bytes(&mut nonce);
        // create payload key
        let mut payload_key = [0; 32];
        Hkdf::<Sha256>::new(Some(&nonce), file_key.0.expose_secret())
            .expand(message::PAYLOAD_KEY_LABEL, &mut payload_key)
            .expect("payload_key is the correct length");
        aead::aead_encrypt(&payload_key, &plaintext)
    }

    #[test]
    fn it_encrypts_a_message_to_alice() {
        // plaintext
        let plaintext = b"Hello, World!";
        // create new file key
        let file_key = FileKey::new();
        // pass file key to encrypt plaintext
        let ciphertext = encrypt_plaintext(&file_key, plaintext);
        print!("{:?}", ciphertext);
    }

    #[test]
    fn it_encrypts_and_decrypts_a_message_to_alice() {
        let plaintext = b"Hello, World!";
        // alice
        let alice_keypair = Ed25519Keypair::new();
        let alice_secret_key: X25519PrivateKey = (&alice_keypair.get_private_key()).into();
        let alice_public_key: X25519PublicKey = (&alice_keypair.get_public_key()).into();

        let encryptor = encryptor::Encryptor::new(&vec![alice_public_key]);
        let message = encryptor.encrypt(plaintext, None);

        // create decryptor
        let decryptor = decryptor::Decryptor::new(&message);
        // get file key
        let file_key = decryptor
            .decrypt_file_key(&alice_secret_key)
            .expect("could decrypt file key");
        // check mac
        assert_eq!(decryptor.verify_message_mac(&file_key), true);
        // decrypt message payload
        let decrypted_plaintext = decryptor
            .decrypt_payload(&file_key)
            .expect("could decrypt payload");
        assert_eq!(decrypted_plaintext, plaintext);
    }

    #[test]
    fn it_encodes_and_decodes_a_message_to_alice() {
        let plaintext = b"Hello, World!";

        // alice
        let alice_keypair = Ed25519Keypair::new();
        let alice_public_key: X25519PublicKey = (&alice_keypair.get_public_key()).into();

        let encryptor = encryptor::Encryptor::new(&vec![alice_public_key]);
        let message = encryptor.encrypt(plaintext, None);
        // encode
        let encoded_message = message.serialize_to_armor().expect("could serialize");
        print!("{:?}", encoded_message);
        let decoded_message =
            Message::deserialize_from_armor(&encoded_message).expect("could deserialize");
        assert_eq!(decoded_message.mac.mac, message.mac.mac);
        assert_eq!(decoded_message.meta.timestamp, message.meta.timestamp);
    }

    #[test]
    fn it_encrypts_and_decrypts_a_message_to_alice_and_signs() {
        let plaintext = b"Hello, World!";
        // alice
        let alice_keypair = Ed25519Keypair::new();
        let alice_private_key = alice_keypair.get_private_key();
        let alice_public_key = alice_keypair.get_public_key();

        let alice_secret_key_x25519: X25519PrivateKey = (&alice_private_key).into();
        let alice_public_key_x25519: X25519PublicKey = (&alice_public_key).into();

        let encryptor = encryptor::Encryptor::new(&vec![alice_public_key_x25519]);
        let message = encryptor.encrypt(plaintext, Some(&alice_private_key));

        // create decryptor
        let decryptor = decryptor::Decryptor::new(&message);
        // get file key
        let file_key = decryptor
            .decrypt_file_key(&alice_secret_key_x25519)
            .expect("could decrypt file key");
        // check mac
        assert_eq!(decryptor.verify_message_mac(&file_key), true);
        // decrypt message payload
        let decrypted_plaintext = decryptor
            .decrypt_payload(&file_key)
            .expect("could decrypt payload");
        assert_eq!(decrypted_plaintext, plaintext);
    }

    #[test]
    fn it_encrypts_and_decrypts_a_message_to_alice_and_signs_and_verifies() {
        let plaintext = b"Hello, World!";
        // alice
        let alice_keypair = Ed25519Keypair::new();
        let alice_private_key = alice_keypair.get_private_key();
        let alice_public_key = alice_keypair.get_public_key();

        let alice_secret_key_x25519: X25519PrivateKey = (&alice_private_key).into();
        let alice_public_key_x25519: X25519PublicKey = (&alice_public_key).into();

        let encryptor = encryptor::Encryptor::new(&[alice_public_key_x25519]);
        let message = encryptor.encrypt(plaintext, Some(&alice_private_key));

        // create decryptor
        let decryptor = decryptor::Decryptor::new(&message);
        // get file key
        let file_key = decryptor
            .decrypt_file_key(&alice_secret_key_x25519)
            .expect("could decrypt file key");
        // check mac
        assert_eq!(decryptor.verify_message_mac(&file_key), true);
        // decrypt message payload
        let decrypted_plaintext = decryptor
            .decrypt_payload(&file_key)
            .expect("could decrypt payload");
        assert_eq!(decrypted_plaintext, plaintext);
        // verify signature
        assert!(decryptor::Decryptor::verify_signature(
            &message,
            &alice_public_key
        ));
    }

    #[test]
    fn it_encodes_and_decodes_a_signed_message_to_alice() {
        let plaintext = b"Hello, World!";

        // alice
        let alice_keypair = Ed25519Keypair::new();
        let alice_private_key = alice_keypair.get_private_key();
        let alice_public_key = alice_keypair.get_public_key();
        let alice_public_key_x25519: X25519PublicKey = (&alice_public_key).into();

        let encryptor = encryptor::Encryptor::new(&[alice_public_key_x25519]);
        let message = encryptor.encrypt(plaintext, Some(&alice_private_key));
        // encode
        let encoded_message = message.serialize_to_armor().expect("could serialize");
        print!("{:?}", encoded_message);
        let decoded_message =
            Message::deserialize_from_armor(&encoded_message).expect("could deserialize");
        assert_eq!(decoded_message.mac.mac, message.mac.mac);
        assert_eq!(decoded_message.meta.timestamp, message.meta.timestamp);
    }

    #[test]
    fn it_encrypts_and_decrypts_a_message_from_alice_to_bob_and_signs_and_verifies() {
        let plaintext = b"Hello, World!";
        // alice
        let alice_keypair = Ed25519Keypair::new();
        let alice_private_key = alice_keypair.get_private_key();
        let alice_public_key = alice_keypair.get_public_key();

        // bob
        let bob_keypair = Ed25519Keypair::new();
        let bob_private_key = bob_keypair.get_private_key();
        let bob_public_key = bob_keypair.get_public_key();

        let bob_secret_key_x25519: X25519PrivateKey = (&bob_private_key).into();
        let bob_public_key_x25519: X25519PublicKey = (&bob_public_key).into();

        // alice encrypts and signs
        let encryptor = encryptor::Encryptor::new(&[bob_public_key_x25519]);
        let message = encryptor.encrypt(plaintext, Some(&alice_private_key));

        // bob decrypts
        let decryptor = decryptor::Decryptor::new(&message);
        let file_key = decryptor
            .decrypt_file_key(&bob_secret_key_x25519)
            .expect("could decrypt file key");
        assert_eq!(decryptor.verify_message_mac(&file_key), true);
        let decrypted_plaintext = decryptor
            .decrypt_payload(&file_key)
            .expect("could decrypt payload");
        assert_eq!(decrypted_plaintext, plaintext);
        // verify signature
        assert!(decryptor::Decryptor::verify_signature(
            &message,
            &alice_public_key,
        ));
    }
}