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
#![no_std]
#![cfg_attr(feature = "cargo-clippy", allow(needless_range_loop))]

#[cfg(feature = "P4")]
#[macro_use] extern crate if_chain;
#[macro_use] extern crate arrayref;
extern crate subtle;
extern crate byteorder;
extern crate norx_permutation as permutation;

#[macro_use]
mod common;

#[cfg(feature = "P1")]
#[path = "p1.rs"]
mod imp;

#[cfg(feature = "P4")]
#[path = "p4.rs"]
mod imp;

pub mod constant;

use common::{ Tag, tags, with, absorb };
use constant::{ W, U, S, KEY_LENGTH, NONCE_LENGTH, TAG_LENGTH };
pub use imp::Process;


#[derive(Clone)]
pub struct Norx([U; S]);
pub struct Encrypt;
pub struct Decrypt;

impl Norx {
    pub fn new(key: &[u8; KEY_LENGTH], nonce: &[u8; NONCE_LENGTH]) -> Norx {
        // TODO use CTFE https://github.com/rust-lang/rust/issues/24111
        #![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
        let mut state = include!(concat!(env!("OUT_DIR"), "/", "init_state.rs"));

        with(&mut state, |state| {
            state[..NONCE_LENGTH].copy_from_slice(nonce);
            state[NONCE_LENGTH..][..KEY_LENGTH].copy_from_slice(key);
        });

        permutation::norx(&mut state);

        with(&mut state, |state| {
            for i in 0..KEY_LENGTH {
                state[(12 * W / 8)..][i] ^= key[i];
            }
        });

        Norx(state)
    }

    fn finalize(mut self, key: &[u8; KEY_LENGTH], aad: &[u8], tag: &mut [u8; TAG_LENGTH]) {
        let Norx(ref mut state) = self;

        absorb::<tags::Trailer>(state, aad);

        state[15] ^= <tags::Final as Tag>::TAG;
        permutation::norx(state);

        with(state, |state| {
            for i in 0..KEY_LENGTH {
                state[(12 * W / 8)..][i] ^= key[i];
            }
        });

        permutation::norx(state);

        with(state, |state| {
            for i in 0..KEY_LENGTH {
                tag[i] = state[(12 * W / 8)..][i] ^ key[i];
            }
        });

        // TODO zero state
    }
}