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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
//! The onionsalt crate.
//!
//! The Onion Salt encryption scheme is an onion encryption scheme
//! that is closely derived from the NaCl `crypto_box` format.
//!
//! # Examples
//!
//! Here is a simple example of onion encrypting a message and
//! decrypting it.
//!
//! ```
//! # use onionsalt::*;
//! #
//! let pairs = [crypto::box_keypair(),
//!              crypto::box_keypair(),
//!              crypto::box_keypair(),
//!              crypto::box_keypair(),
//!              crypto::box_keypair(),
//!              crypto::box_keypair()];
//! let recipient = 2;
//! let recipient_key = pairs[recipient].clone();
//! let keys_and_routes: [(crypto::PublicKey, [u8; ROUTING_LENGTH]); ROUTE_COUNT]
//!                        = [(pairs[0].public, *b"address for 0 router    "),
//!                           (pairs[1].public, *b"the address for router 1"),
//!                           (pairs[2].public, *b"this is the recipient!!!"),
//!                           (pairs[3].public, *b"the next router is nice."),
//!                           (pairs[4].public, *b"the second-to-last node."),
//!                           (pairs[5].public, *b"This is my own address. ")];
//! let mut payload: [u8; PAYLOAD_LENGTH] = [0; PAYLOAD_LENGTH];
//! payload[3] = 3;
//! let payload = payload;
//! let our_personal_key = crypto::box_keypair();
//! let mut ob = onionbox(&keys_and_routes, recipient).unwrap();
//! ob.add_payload(our_personal_key, &payload);
//!
//! let mut packet = ob.packet();
//! let response = [1; PAYLOAD_LENGTH];
//! for i in 0..6 {
//!     println!("opening box {}", i);
//!     let mut oob = onionbox_open(&packet, &pairs[i].secret).unwrap();
//!     println!("grabbing routing for {}", i);
//!     let routing = oob.routing();
//!     // routing now holds the routing information sent to "i"
//! #     for j in 0..ROUTING_LENGTH {
//! #         assert_eq!(routing[j], keys_and_routes[i].1[j]);
//! #     }
//!     if i == recipient {
//!         // This is how to attach a response if you are the recipient.
//!         oob.respond(&recipient_key, &response);
//!     }
//!     packet = oob.packet();
//! }
//! let resp = ob.read_return(our_personal_key, &packet).unwrap();
//! // resp now holds the return message, authenticated and decrypted.
//! # for i in 0..PAYLOAD_LENGTH {
//! #     println!("{:02x} {:02x}", resp[i], response[i]);
//! # }
//! # for j in 0..PAYLOAD_LENGTH {
//! #     assert_eq!(resp[j], response[j]);
//! # }
//! ```

#![deny(warnings)]

#[cfg(test)]
extern crate quickcheck;

#[macro_use]
extern crate arrayref;

extern crate rand;
extern crate serde;
#[cfg(test)]
extern crate serde_json;

pub mod crypto;
mod bytes;
pub mod creatediagrams;

const AUTHENTICATIONBYTES: usize = 16;

/// The number of extra bytes needed per recipient.  Includes public
/// key and authentication bytes.
const OVERHEADBYTES: usize = 48;

/// The ROUTING_LENGTH is big enough for an ipv6 address and some
/// extra information.
pub const ROUTING_LENGTH: usize = 24;

const ROUTING_OVERHEAD: usize = ROUTING_LENGTH + OVERHEADBYTES;

/// The number of routers we send through.  Eventually I want to
/// implement the feature to send through fewer routers with the
/// message arriving back early.
pub const ROUTE_COUNT: usize = 6;

const AUTH_LENGTH: usize = (ROUTE_COUNT+1)*ROUTING_OVERHEAD - AUTHENTICATIONBYTES - 32;

/// `PACKET_LENGTH` is the size that we actually send to each recipient.
///
/// ```
/// # use onionsalt::*;
/// assert_eq!(PACKET_LENGTH, 1024);
/// ```
pub const PACKET_LENGTH: usize =
    bytes::BUFSIZE - 16 + 32 - ROUTING_OVERHEAD;

/// The size of an encrypted payload.
pub const ENCRYPTEDPAYLOAD_LENGTH: usize = PACKET_LENGTH - ROUTE_COUNT*ROUTING_OVERHEAD;

/// `PAYLOAD_LENGTH` is the size of the payload that the primary
/// recipient can get.  It differs from `ENCRYPTEDPAYLOAD_LENGTH` by 48
/// (or `OVERHEADBYTES`).
///
/// ```
/// # use onionsalt::*;
/// assert_eq!(PAYLOAD_LENGTH, 544);
/// ```
pub const PAYLOAD_LENGTH: usize = ENCRYPTEDPAYLOAD_LENGTH - OVERHEADBYTES;

pub struct OnionBox {
    packet: [u8; PACKET_LENGTH],
    return_key: [u8; PACKET_LENGTH],
    payload_recipient_key: crypto::PublicKey,
    payload_nonce: crypto::Nonce,
}

// The following trait implementations are needed because we cannot
// derive traits with arrays longer than 32.
impl std::hash::Hash for OnionBox {
    fn hash<H: std::hash::Hasher>(&self, h: &mut H) {
        // For speed, we only bother hashing the payload_nonce, since
        // it is generated securely and randomly for each OnionBox.
        // If there is a collision in payload_nonces, we have worse
        // problems than our hash tables getting messed up!
        self.payload_nonce.hash(h);
    }
}
impl Clone for OnionBox {
    fn clone(&self) -> Self {
        OnionBox {
            packet: self.packet,
            return_key: self.return_key,
            payload_recipient_key: self.payload_recipient_key,
            payload_nonce: self.payload_nonce,
        }
    }
}
impl PartialEq for OnionBox {
    fn eq(&self, o: &OnionBox) -> bool {
        if self.payload_recipient_key != o.payload_recipient_key
           || self.payload_nonce != o.payload_nonce {
            return false;
        }
        let mut same = true;
        for i in 0..PACKET_LENGTH {
            same = same && self.packet[i] == o.packet[i]
                && self.return_key[i] == o.return_key[i];
        }
        same
    }
}
impl Eq for OnionBox {}
impl std::fmt::Debug for OnionBox {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "OnionBox{{ recipient: {}, payload_nonce: {} }}",
               self.payload_recipient_key, self.payload_nonce)
    }
}

impl OnionBox {
    /// The encrypted packet, to be sent to the first receiver.
    pub fn packet(&self) -> [u8; PACKET_LENGTH] {
        self.packet
    }
    /// The response when we get it will begin with these bytes.
    pub fn return_magic(&self) -> [u8; 32] {
        *array_ref![self.return_key,0,32]
    }
    /// This function accepts a packet that has been sent to us, and
    /// decrypts it without authentication if it is the response to
    /// our original message.
    pub fn read_return(&self, payload_key: crypto::KeyPair, msg: &[u8; PACKET_LENGTH])
                       -> Result<[u8; PAYLOAD_LENGTH], crypto::NaClError> {
        if *array_ref![msg,0,32] != self.return_magic() {
            // this doesn't look to be the return packet
            return Err(crypto::NaClError::AuthFailed);
        }
        let mut encrypted = *array_ref![msg, PACKET_LENGTH - ENCRYPTEDPAYLOAD_LENGTH,
                                        ENCRYPTEDPAYLOAD_LENGTH];
        let simple_key = array_ref![self.return_key, PACKET_LENGTH - ENCRYPTEDPAYLOAD_LENGTH,
                                    ENCRYPTEDPAYLOAD_LENGTH];
        for i in 0 .. ENCRYPTEDPAYLOAD_LENGTH {
            encrypted[i] ^= simple_key[i];
        }
        let semidecrypted = &mut encrypted;
        let response_nonce = crypto::Nonce(*array_ref![semidecrypted,0,24]);
        let payload = array_mut_ref![semidecrypted, 16, PAYLOAD_LENGTH+32];
        *array_mut_ref![payload,0,16] = [0;16];
        let mut plain = [0; PAYLOAD_LENGTH+32];
        try!(crypto::box_open(&mut plain, payload, &response_nonce,
                              &self.payload_recipient_key, &payload_key.secret));
        Ok(*array_ref![plain, 32, PAYLOAD_LENGTH])
    }
    pub fn add_payload(&mut self,
                       payload_key: crypto::KeyPair,
                       payload_contents: &[u8; PAYLOAD_LENGTH]) -> &mut Self {
        let mut plain = [0; PAYLOAD_LENGTH + 32];
        for i in 0..PAYLOAD_LENGTH {
            plain[i+32] = payload_contents[i];
        }
        let mut cipher = [0; ENCRYPTEDPAYLOAD_LENGTH];
        crypto::box_up(&mut cipher[16..], &plain, &self.payload_nonce,
                       &self.payload_recipient_key, &payload_key.secret);
        *array_mut_ref![cipher, 0, 32] = payload_key.public.0;
        self.add_encryptedpayload(&cipher);
        self
    }
    pub fn add_encryptedpayload(&mut self,
                                ciphertext: &[u8; ENCRYPTEDPAYLOAD_LENGTH]) -> &mut Self {
        for i in 0..ENCRYPTEDPAYLOAD_LENGTH {
            self.packet[PACKET_LENGTH - ENCRYPTEDPAYLOAD_LENGTH + i] ^= ciphertext[i];
        }
        self
    }
}

pub struct OpenedOnionBox {
    packet: [u8; PACKET_LENGTH],
    routing: [u8; ROUTING_LENGTH],
    payload_nonce: crypto::Nonce,
}
impl OpenedOnionBox {
    /// The packet to be forwarded onwards to the next router.
    pub fn packet(&self) -> [u8; PACKET_LENGTH] {
        self.packet
    }
    /// The routing information for us.
    pub fn routing(&self) -> [u8; ROUTING_LENGTH] {
        self.routing
    }
    /// The payload public key of the sender (if we are the recipient).
    pub fn key(&self) -> crypto::PublicKey {
        crypto::PublicKey(*array_ref![self.packet,PACKET_LENGTH-ENCRYPTEDPAYLOAD_LENGTH,32])
    }
    /// Attempt to decrypt and authenticate the payload.
    pub fn payload(&self, response_key: &crypto::KeyPair)
                   -> Result<[u8; PAYLOAD_LENGTH], crypto::NaClError> {
        let mut ciphertext = *array_ref![self.packet, PACKET_LENGTH - PAYLOAD_LENGTH - 32,
                                         PAYLOAD_LENGTH + 32];
        *array_mut_ref![ciphertext,0,16] = [0;16];
        let mut plaintext = [0; PAYLOAD_LENGTH + 32];
        try!(crypto::box_open(&mut plaintext, &ciphertext, &self.payload_nonce,
                              &self.key(), &response_key.secret));
        Ok(*array_ref![plaintext,32,PAYLOAD_LENGTH])
    }
    /// Set `response` to the response payload information.  This is
    /// only likely to work correctly if the sender intended to ask us
    /// for a response.
    pub fn respond(&mut self,
                   response_key: &crypto::KeyPair,
                   response: &[u8; PAYLOAD_LENGTH]) {
        let mut buffer = bytes::Bytes([0; bytes::BUFSIZE]);
        for i in 0..PACKET_LENGTH {
            buffer.0[i] = self.packet[i];
        }
        let mut pl = [0; PAYLOAD_LENGTH+32];
        let mut ci = [0; ENCRYPTEDPAYLOAD_LENGTH];
        *array_mut_ref![pl,32,PAYLOAD_LENGTH] = *response;
        let rand_data = crypto::random_32();
        let response_nonce = crypto::Nonce(*array_ref![rand_data,0,24]);
        crypto::box_up(&mut ci[16..], &pl, &response_nonce, &self.key(),
                       &response_key.secret);
        *array_mut_ref![ci, 0, 32] = rand_data;
        onionbox_insert_response_algorithm(&mut buffer, &ci);
        for i in 0..PACKET_LENGTH {
            self.packet[i] = buffer.0[i];
        }
    }
}

/// Encrypt a message in an onion defined by `keys_and_routings`, with
/// `payload` directed to `payload_recipient`.
///
/// `keys_and_routings` is the sequence of public keys owned by
/// recipients, and the routing information that said recipient should
/// use, presumably to send the message to the next recipient.  There
/// can be up to `ROUTE_COUNT` elements in this slice.  The final
/// address should be our own, if a return message is desired.  The
/// routing information should indicate to the payload recipient
/// (whose index in the slice is `payload_recipient`) what to do with
/// the payload.
///
/// # Security properties
///
/// No recipient (not possessing the secret keys of any other
/// recipient) by examining the packet received should be able to
/// determine any information other than the plaintext contents of the
/// routing information (except for the payload recipient, who should
/// also be able to read the plaintext payload.  Similarly, each
/// recipient can be confident that the routing information has not
/// been tampered with, although it could have been replaced in its
/// entirety with other routing information.
///
/// The recipient of the message payload may ensure that the payload
/// originated from the sender (or someone with the sender's secret
/// key), although the recipient cannot prove this to anyone else.
///
/// Things to keep in mind:
///
/// 1. There is no protection against tampering with the payload until
///    the payload is received by the recipient.  Thus the recipient
///    must take particular care not to reveal anything by its
///    response to the payload.
///
/// 2. No recipient can in any way determine (from the message
///    received) her place in the series of routing.
pub fn onionbox(keys_and_routings: &[(crypto::PublicKey,
                                      [u8; ROUTING_LENGTH])],
                payload_recipient: usize) -> Result<OnionBox, crypto::NaClError> {
    let mut out = OnionBox {
        packet: [0; PACKET_LENGTH],
        return_key: [0; PACKET_LENGTH],
        payload_recipient_key: keys_and_routings[payload_recipient].0,
        payload_nonce: crypto::Nonce([0;24]),
    };
    let mut buffer = bytes::Bytes([0; bytes::BUFSIZE]);
    let mut return_key = bytes::Bytes([0; bytes::BUFSIZE]);
    out.payload_nonce = try!(onionbox_algorithm(&mut buffer, &mut return_key,
                                                keys_and_routings, payload_recipient));
    for i in 0..PACKET_LENGTH {
        out.packet[i] = buffer.0[i];
        out.return_key[i] = return_key.0[i];
    }
    Ok(out)
}

/// The message is passed in `input`, and a struct is returned which
/// has methods to access the decrypted routing information, the
/// decrypted message (to be passed to the next router), to access the
/// payload, and to insert an encrypted response to the payload.
pub fn onionbox_open(input: &[u8; PACKET_LENGTH],
                     secret_key: &crypto::SecretKey)
                     -> Result<OpenedOnionBox, crypto::NaClError> {
    let mut oob = OpenedOnionBox {
        packet: [0; PACKET_LENGTH],
        routing: [0; ROUTING_LENGTH],
        payload_nonce: crypto::Nonce(*array_ref![input, 0, 24]),
    };
    let mut buffer = bytes::Bytes([0; bytes::BUFSIZE]);
    for i in 0..PACKET_LENGTH {
        buffer.0[i] = input[i];
    }
    oob.routing = try!(onionbox_open_algorithm(&mut buffer, secret_key));
    for i in 0..PACKET_LENGTH {
        oob.packet[i] = buffer.0[i];
    }
    Ok(oob)
}


/// **Not for public consumption!** Encrypt a message in an onion
/// defined by `keys_and_routings`, with `payload` directed to
/// `payload_recipient`.
fn onionbox_algorithm<T: bytes::SelfDocumenting>(buffer: &mut T,
                                                 return_key: &mut T,
                                                 keys_and_routings: &[(crypto::PublicKey,
                                                                       [u8; ROUTING_LENGTH])],
                                                 payload_recipient: usize)
                                                 -> Result<crypto::Nonce, crypto::NaClError> {
    let route_count = keys_and_routings.len();
    assert!(payload_recipient < route_count);
    assert!(route_count <= ROUTE_COUNT);

    assert_eq!(PACKET_LENGTH, bytes::PACKET_LENGTH);
    assert_eq!(PACKET_LENGTH, ROUTE_COUNT*ROUTING_OVERHEAD + ENCRYPTEDPAYLOAD_LENGTH);
    assert_eq!(16 + (ROUTE_COUNT+1)*ROUTING_OVERHEAD - 32 + ENCRYPTEDPAYLOAD_LENGTH,
               bytes::BUFSIZE);
    // We always use a zero nonce.
    let nonce = crypto::Nonce([0; 24]);

    // Here we create buffers for my_public_keys, my_private_keys, and
    // our plaintext.
    let mut my_keypairs = [crypto::box_keypair(); ROUTE_COUNT];
    for i in 1..route_count {
        my_keypairs[i] = crypto::box_keypair();
    }
    let mut skeys: [[u8; 32]; ROUTE_COUNT] = [[0;32]; ROUTE_COUNT];
    for i in 0..route_count {
        skeys[i] = crypto::sillybox_beforenm(&keys_and_routings[i].0,
                                             &my_keypairs[i].secret);
    }

    // First let's grab the ciphertext to decrypt the return message...
    for i in payload_recipient+1..route_count {
        return_key.sillybox_afternm(AUTH_LENGTH, &nonce, &skeys[i],
                                    &format!("{}", i));
        return_key.annotate(&format!("Preparing to decrypt with respect to key {}",
                                     i));
    }
    return_key.move_bytes(bytes::BUFSIZE - ENCRYPTEDPAYLOAD_LENGTH,
                          PACKET_LENGTH - ENCRYPTEDPAYLOAD_LENGTH,
                          ENCRYPTEDPAYLOAD_LENGTH);
    return_key.set_bytes(0,
                         PACKET_LENGTH - ENCRYPTEDPAYLOAD_LENGTH,
                         &[0;PACKET_LENGTH-ENCRYPTEDPAYLOAD_LENGTH],
                         "0");

    buffer.set_bytes(0, bytes::BUFSIZE, &[0; bytes::BUFSIZE], "0");
    if route_count == ROUTE_COUNT {
        buffer.annotate(&format!("Starting with buffer of zeros."));
    } else {
        // For a short onion, we need to start with random data
        let nonce = crypto::random_nonce();
        let skey = crypto::box_keypair().secret.0;
        buffer.sillybox_afternm(AUTH_LENGTH, &nonce, &skey, &format!("Random"));
        buffer.set_bytes(0,32, &[0;32], "0");
        buffer.set_bytes(bytes::BUFSIZE - ENCRYPTEDPAYLOAD_LENGTH - ROUTING_OVERHEAD,
                         ENCRYPTEDPAYLOAD_LENGTH + ROUTING_OVERHEAD,
                         &[0; ENCRYPTEDPAYLOAD_LENGTH + ROUTING_OVERHEAD], "0");
        buffer.annotate(&format!("Starting with a random+zeros buffer for {}-layer onion.",
                                 route_count));
    }

    for i in 0..route_count {
        buffer.sillybox_afternm(AUTH_LENGTH, &nonce, &skeys[i],
                                &format!("{}", i));
        buffer.annotate(&format!("Encrypting to key {}", i));
        buffer.set_bytes(0, 32, &[0;32], "0");
        if i != route_count-1 {
            buffer.move_bytes(bytes::BUFSIZE + 32 - ENCRYPTEDPAYLOAD_LENGTH - ROUTE_COUNT*ROUTING_OVERHEAD,
                              bytes::BUFSIZE + 32 - ENCRYPTEDPAYLOAD_LENGTH - (ROUTE_COUNT+1)*ROUTING_OVERHEAD,
                              ROUTE_COUNT*ROUTING_OVERHEAD - 32);
            buffer.annotate(&format!("Shifting left routing bytes"));
        }
    }
    // At this stage, plaintext should be set up for the innermost
    // layer of the onion, although offset by a ROUTING_OVERHEAD.

    for i in (0..route_count).rev() {
        // Move into place the portion of the routing information
        // which has already been encrypted.
        if i != route_count-1 {
            buffer.move_bytes(bytes::BUFSIZE + 32 - ENCRYPTEDPAYLOAD_LENGTH - (ROUTE_COUNT+1)*ROUTING_OVERHEAD,
                              bytes::BUFSIZE + 32 - ENCRYPTEDPAYLOAD_LENGTH - ROUTE_COUNT*ROUTING_OVERHEAD,
                              ROUTE_COUNT*ROUTING_OVERHEAD - 32);
            buffer.annotate(&format!("Shifting right routing info"));
        }
        // Now we add the routing info!
        buffer.set_bytes(32, ROUTING_LENGTH, &keys_and_routings[i].1,
                         &format!("R{}", i) );
        // Add the public key we are using for the encryption.
        if i < route_count-1 {
            buffer.set_bytes(32+ROUTING_LENGTH, 32, &my_keypairs[i+1].public.0,
                             &format!("P{}", i+1));
            buffer.annotate(&format!("Adding routing info for {}", i));
        } else {
            return_key.copy_bytes(0, buffer, 32+ROUTING_LENGTH, 32);
            return_key.annotate("Shifting and adding the final public key to the return key");
            buffer.annotate(&format!("Adding routing info but no public key {}", i));
        }
        // Add the payload if it is the right time.
        if i == payload_recipient {
            buffer.set_bytes(bytes::BUFSIZE-ENCRYPTEDPAYLOAD_LENGTH,
                             ENCRYPTEDPAYLOAD_LENGTH, &[0; ENCRYPTEDPAYLOAD_LENGTH], "Payload".into());
            buffer.annotate(&format!("Adding payload {}", i));
        }
        // Now we encrypt the plaintext, which expands it by
        // AUTHENTICATIONBYTES.
        buffer.sillybox_afternm(AUTH_LENGTH, &nonce, &skeys[i],
                                &format!("{}", i));
        buffer.annotate(&format!("Encrypting to key {}", i));
        // for j in auth_length-ROUTING_OVERHEAD .. auth_length {
        //     assert!(ciphertext[j] == 0);
        // }
    }
    buffer.move_bytes(16, 32, ROUTE_COUNT*ROUTING_OVERHEAD);
    buffer.move_bytes(bytes::BUFSIZE-ENCRYPTEDPAYLOAD_LENGTH,
                      ROUTE_COUNT*ROUTING_OVERHEAD,
                      ENCRYPTEDPAYLOAD_LENGTH);
    buffer.annotate(&format!("Putting packet into place"));
    buffer.set_bytes(0, 32, &my_keypairs[0].public.0, "P0");
    buffer.annotate(&format!("Adding the last public key"));
    Ok(crypto::Nonce(*array_ref![my_keypairs[payload_recipient].public.0,0,24]))
}

/// **Not for public consumption!** The buffer already contains the
/// message, and contains the next message on exit.
fn onionbox_open_algorithm<T: bytes::SelfDocumenting>(buffer: &mut T,
                                                      secret_key: &crypto::SecretKey)
                                                      -> Result<[u8; ROUTING_LENGTH],
                                                                crypto::NaClError>
{
    let pk = {
        let pkbytes = buffer.get_bytes(0, 32);
        crypto::PublicKey(*array_ref![pkbytes,0,32])
    };
    buffer.move_bytes(ROUTE_COUNT*ROUTING_OVERHEAD,
                      bytes::BUFSIZE-ENCRYPTEDPAYLOAD_LENGTH,
                      ENCRYPTEDPAYLOAD_LENGTH);
    buffer.move_bytes(32, 16, ROUTE_COUNT*ROUTING_OVERHEAD);
    // the following is only to beautify the picture, since the bytes
    // are already zero.
    buffer.set_bytes(bytes::BUFSIZE-ENCRYPTEDPAYLOAD_LENGTH-ROUTING_OVERHEAD,
                     ROUTING_OVERHEAD,
                     &[0;ROUTING_OVERHEAD],
                     "0");
    buffer.annotate(&format!("Extract the public key and insert zeros"));

    let skey = crypto::sillybox_beforenm(&pk, secret_key);
    try!(buffer.sillybox_open_afternm(AUTH_LENGTH, &crypto::Nonce([0;24]), &skey));
    buffer.annotate(&format!("Decrypting with our secret key"));
    let routevec = buffer.get_bytes(32, ROUTING_LENGTH);
    buffer.move_bytes(32+ROUTING_LENGTH,
                      0, bytes::BUFSIZE - 32 - ROUTING_LENGTH);
    buffer.annotate(&format!("Extracting routing information and shifted back"));
    let mut route = [0;ROUTING_LENGTH];
    for i in 0..ROUTING_LENGTH {
        route[i] = routevec[i];
    }
    Ok(route)
}

/// **Not for public consumption!**
fn onionbox_insert_response_algorithm<T: bytes::SelfDocumenting>(buffer: &mut T,
                                                                 payload: &[u8; ENCRYPTEDPAYLOAD_LENGTH]) {
    buffer.set_bytes(bytes::BUFSIZE - ENCRYPTEDPAYLOAD_LENGTH - 32 - ROUTING_LENGTH,
                     ENCRYPTEDPAYLOAD_LENGTH, payload, "Response");
    buffer.annotate(&format!("Read payload and replace with response"));
}

#[test]
fn check_onionbox_on_diagram() {
    use bytes::{SelfDocumenting};

    let mut diagram = bytes::Diagram::new();
    let mut return_key = bytes::Diagram::new();

    let pairs = [crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair()];

    let keys_and_routes: [(crypto::PublicKey, [u8; ROUTING_LENGTH]); ROUTE_COUNT]
                           = [(pairs[0].public, *b"123456789012345612345678"),
                              (pairs[1].public, *b"my friend is hermy frien"),
                              (pairs[2].public, *b"address 3 for yoaddress "),
                              (pairs[3].public, *b"another is - heranother "),
                              (pairs[4].public, *b"router here is orouter h"),
                              (pairs[5].public, *b"how to get to mehow to g")];
    onionbox_algorithm(&mut diagram, &mut return_key, &keys_and_routes, 2).unwrap();

    println!("{}", diagram.postscript());

    for i in 0..6 {
        diagram.clear();

        diagram.annotate(&format!("Message as received"));
        let route = onionbox_open_algorithm(&mut diagram, &pairs[i].secret).unwrap();
        assert_eq!(route, keys_and_routes[i].1);

        if i == 2 {
            // We are the recipient!
            let mut response: [u8; ENCRYPTEDPAYLOAD_LENGTH] = [0; ENCRYPTEDPAYLOAD_LENGTH];
            for j in 0..ENCRYPTEDPAYLOAD_LENGTH {
                response[j] = j as u8;
            }
            onionbox_insert_response_algorithm(&mut diagram, &response);
        }

        println!("{}", diagram.postscript());
    }
}

#[test]
fn check_onionbox_on_buffer() {
    use bytes::{SelfDocumenting};

    let mut buffer = bytes::Bytes([0; bytes::BUFSIZE]);
    let mut return_key = bytes::Bytes([0; bytes::BUFSIZE]);

    let pairs = [crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair()];

    let keys_and_routes: [(crypto::PublicKey, [u8; ROUTING_LENGTH]); ROUTE_COUNT]
                           = [(pairs[0].public, *b"123456789012345612345678"),
                              (pairs[1].public, *b"my friend is hermy frien"),
                              (pairs[2].public, *b"address 3 for yoaddress "),
                              (pairs[3].public, *b"another is - heranother "),
                              (pairs[4].public, *b"router here is orouter h"),
                              (pairs[5].public, *b"how to get to mehow to g")];
    onionbox_algorithm(&mut buffer, &mut return_key, &keys_and_routes, 2).unwrap();

    for i in 0..6 {
        buffer.clear();

        buffer.annotate(&format!("Message as received by {}", i));
        let route = onionbox_open_algorithm(&mut buffer, &pairs[i].secret).unwrap();
        println!("route should be {}", String::from_utf8_lossy(&keys_and_routes[i].1));
        println!("route is actually {}", String::from_utf8_lossy(&route));
        assert_eq!(route, keys_and_routes[i].1);
    }
}

#[test]
fn check_short_onionbox_on_buffer() {
    use bytes::{SelfDocumenting};

    let mut buffer = bytes::Bytes([0; bytes::BUFSIZE]);
    let mut return_key = bytes::Bytes([0; bytes::BUFSIZE]);

    let pairs = [crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair()];

    let keys_and_routes = [(pairs[0].public, *b"123456789012345612345678"),
                           (pairs[1].public, *b"my friend is hermy frien"),
                           (pairs[2].public, *b"address 3 for yoaddress ")];
    onionbox_algorithm(&mut buffer, &mut return_key, &keys_and_routes, 1).unwrap();

    for i in 0..pairs.len() {
        buffer.clear();

        buffer.annotate(&format!("Message as received by {}", i));
        let route = onionbox_open_algorithm(&mut buffer, &pairs[i].secret).unwrap();
        println!("route should be {}", String::from_utf8_lossy(&keys_and_routes[i].1));
        println!("route is actually {}", String::from_utf8_lossy(&route));
        assert_eq!(route, keys_and_routes[i].1);
    }
}

#[test]
fn test_onionbox_auth() {
    use crypto::KeyPair;
    fn f(data: Vec<u8>, response_data: Vec<u8>,
         payload_recipient: usize,
         pairs : (KeyPair,KeyPair,KeyPair,KeyPair,KeyPair,KeyPair))
         -> quickcheck::TestResult {
        if data.len() == 0 || response_data.len() == 0 {
            return quickcheck::TestResult::discard();
        }

        let payload_recipient = payload_recipient % ROUTE_COUNT;

        let mut buffer = bytes::Bytes([0; bytes::BUFSIZE]);
        let mut return_key = bytes::Bytes([0; bytes::BUFSIZE]);

        let pairs = [pairs.0, pairs.1, pairs.2, pairs.3, pairs.4, pairs.5];
        let keys_and_routes: [(crypto::PublicKey, [u8; ROUTING_LENGTH]); ROUTE_COUNT]
            = [(pairs[0].public, *b"123456789012345612345678"),
               (pairs[1].public, *b"my friend is hermy frien"),
               (pairs[2].public, *b"address 3 for yoaddress "),
               (pairs[3].public, *b"another is - heranother "),
               (pairs[4].public, *b"router here is orouter h"),
               (pairs[5].public, *b"how to get to mehow to g")];
        onionbox_algorithm(&mut buffer, &mut return_key,
                           &keys_and_routes, payload_recipient).unwrap();

        for i in 0..6 {
            let route = onionbox_open_algorithm(&mut buffer, &pairs[i].secret).unwrap();
            if route != keys_and_routes[i].1 {
                return quickcheck::TestResult::error(
                    format!("route[{}] {:?} != {:?}", i, route, keys_and_routes[i].1));
            }

            if i == payload_recipient {
                // We are the recipient!
                let mut response: [u8; ENCRYPTEDPAYLOAD_LENGTH] = [0; ENCRYPTEDPAYLOAD_LENGTH];
                for j in 0..ENCRYPTEDPAYLOAD_LENGTH {
                    response[j] = response_data[j % response_data.len()];
                    if buffer.0[PACKET_LENGTH - ENCRYPTEDPAYLOAD_LENGTH + j] != 0 {
                        return quickcheck::TestResult::error(
                            format!("Response {:?} != 0",
                                    &buffer.0[PACKET_LENGTH - ENCRYPTEDPAYLOAD_LENGTH + j]));
                    }
                }
                onionbox_insert_response_algorithm(&mut buffer, &response);
            }
        }
        for j in 0..32 {
            if buffer.0[j] != return_key.0[j] {
                return quickcheck::TestResult::error(
                    format!("Bad return key {:?} != {:?}",
                            &buffer.0[j], &return_key.0[j]));
            }
        }
        for j in 0 .. ENCRYPTEDPAYLOAD_LENGTH {
            let decrypted = buffer.0[PACKET_LENGTH-ENCRYPTEDPAYLOAD_LENGTH+j] ^ return_key.0[PACKET_LENGTH-ENCRYPTEDPAYLOAD_LENGTH+j];
            let resp = response_data[j % response_data.len()];
            if decrypted != resp {
                return quickcheck::TestResult::error(
                    format!("Bad response {:?} != {:?}", decrypted, resp));
            }
        }
        quickcheck::TestResult::passed()
    }
    quickcheck::quickcheck(f as fn(Vec<u8>, Vec<u8>, usize,
                                   (KeyPair, KeyPair, KeyPair,
                                    KeyPair, KeyPair, KeyPair)) -> quickcheck::TestResult);
}


#[test]
fn test_short_onionbox_auth() {
    use crypto::KeyPair;
    fn f(data: Vec<u8>, response_data: Vec<u8>,
         payload_recipient: usize,
         pairs : (KeyPair,KeyPair,KeyPair))
         -> quickcheck::TestResult {
        if data.len() == 0 || response_data.len() == 0 {
            return quickcheck::TestResult::discard();
        }

        let mut buffer = bytes::Bytes([0; bytes::BUFSIZE]);
        let mut return_key = bytes::Bytes([0; bytes::BUFSIZE]);

        let pairs = [pairs.0, pairs.1, pairs.2];
        let payload_recipient = payload_recipient % pairs.len();
        let keys_and_routes
            = [(pairs[0].public, *b"123456789012345612345678"),
               (pairs[1].public, *b"my friend is hermy frien"),
               (pairs[2].public, *b"address 3 for yoaddress ")];
        onionbox_algorithm(&mut buffer, &mut return_key,
                           &keys_and_routes, payload_recipient).unwrap();

        for i in 0..pairs.len() {
            let route = onionbox_open_algorithm(&mut buffer, &pairs[i].secret).unwrap();
            if route != keys_and_routes[i].1 {
                return quickcheck::TestResult::error(
                    format!("route[{}] {:?} != {:?}", i, route, keys_and_routes[i].1));
            }

            if i == payload_recipient {
                // We are the recipient!
                let mut response: [u8; ENCRYPTEDPAYLOAD_LENGTH] = [0; ENCRYPTEDPAYLOAD_LENGTH];
                for j in 0..ENCRYPTEDPAYLOAD_LENGTH {
                    response[j] = response_data[j % response_data.len()];
                    if buffer.0[PACKET_LENGTH - ENCRYPTEDPAYLOAD_LENGTH + j] != 0 {
                        return quickcheck::TestResult::error(
                            format!("Response {:?} != 0",
                                    &buffer.0[PACKET_LENGTH - ENCRYPTEDPAYLOAD_LENGTH + j]));
                    }
                }
                onionbox_insert_response_algorithm(&mut buffer, &response);
            }
        }
        for j in 0..32 {
            if buffer.0[j] != return_key.0[j] {
                return quickcheck::TestResult::error(
                    format!("Bad return key {:?} != {:?}",
                            &buffer.0[j], &return_key.0[j]));
            }
        }
        for j in 0 .. ENCRYPTEDPAYLOAD_LENGTH {
            let decrypted = buffer.0[PACKET_LENGTH-ENCRYPTEDPAYLOAD_LENGTH+j]
                ^ return_key.0[PACKET_LENGTH-ENCRYPTEDPAYLOAD_LENGTH+j];
            let resp = response_data[j % response_data.len()];
            if decrypted != resp {
                return quickcheck::TestResult::error(
                    format!("Bad response {:?} != {:?}", decrypted, resp));
            }
        }
        quickcheck::TestResult::passed()
    }
    quickcheck::quickcheck(f as fn(Vec<u8>, Vec<u8>, usize,
                                   (KeyPair, KeyPair, KeyPair)) -> quickcheck::TestResult);
}

#[test]
fn test_onionbox_simple() {
    let pairs = [crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair(),
                 crypto::box_keypair()];
    let recipient = 2;
    let recipient_key = pairs[recipient].clone();
    let keys_and_routes: [(crypto::PublicKey, [u8; ROUTING_LENGTH]); ROUTE_COUNT]
        = [(pairs[0].public, *b"address for 0 router    "),
           (pairs[1].public, *b"the address for router 1"),
           (pairs[2].public, *b"this is the recipient!!!"),
           (pairs[3].public, *b"the next router is nice."),
           (pairs[4].public, *b"the second-to-last node."),
           (pairs[5].public, *b"This is my own address. ")];
    let mut payload: [u8; PAYLOAD_LENGTH] = [0; PAYLOAD_LENGTH];
    payload[3] = 3;
    let payload = payload;
    let our_personal_key = crypto::box_keypair();
    let mut ob = onionbox(&keys_and_routes, recipient).unwrap();
    ob.add_payload(our_personal_key, &payload);

    let mut packet = ob.packet();
    let response = [1; PAYLOAD_LENGTH];
    for i in 0..6 {
        println!("opening box {}", i);
        let mut oob = onionbox_open(&packet, &pairs[i].secret).unwrap();
        println!("grabbing routing for {}", i);
        let routing = oob.routing();
        // routing now holds the routing information sent to "i"
        for j in 0..ROUTING_LENGTH {
            assert_eq!(routing[j], keys_and_routes[i].1[j]);
        }
        if i == recipient {
            // This is how to attach a response if you are the recipient.
            println!("opening payload should be from {}", our_personal_key.public);
            let payl = oob.payload(&recipient_key).unwrap();
            println!("got payload");
            for j in 0..PAYLOAD_LENGTH {
                assert_eq!(payl[j], payload[j]);
            }
            println!("\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
            oob.respond(&recipient_key, &response);
            println!("\nYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
        }
        packet = oob.packet();
    }
    println!("\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    let resp = ob.read_return(our_personal_key, &packet).unwrap();
    println!("\nYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
    // resp now holds the return message
    for i in 0..PAYLOAD_LENGTH {
        println!("{:02x} {:02x}", resp[i], response[i]);
    }
    for j in 0..PAYLOAD_LENGTH {
        assert_eq!(resp[j], response[j]);
    }
}