encrypt.c 6.53 KB
Newer Older
lwc-tester committed
1 2
/*   
     TinyJAMBU-128: 128-bit key, 96-bit IV  
Hongjun Wu committed
3
     Reference implementation for 32-bit CPU 
lwc-tester committed
4 5 6
     The state consists of four 32-bit registers      
     state[3] || state[2] || state[1] || state[0]   

Hongjun Wu committed
7
     Implemented by: Hongjun Wu 
lwc-tester committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
*/   

#include "crypto_aead.h"

#define FrameBitsIV  0x10  
#define FrameBitsAD  0x30  
#define FrameBitsPC  0x50  //Framebits for plaintext/ciphertext      
#define FrameBitsFinalization 0x70       

#define NROUND1 128*3 
#define NROUND2 128*8 

/*no-optimized date update function*/    
void state_update(unsigned int *state, const unsigned char *key, unsigned int number_of_steps) 
{
Hongjun Wu committed
23 24 25 26 27 28 29 30 31 32 33 34 35
        unsigned int i;  
        unsigned int t1, t2, t3, t4, feedback; 
        for (i = 0; i < (number_of_steps >> 5); i++)
        {
                t1 = (state[1] >> 15) | (state[2] << 17);  // 47 = 1*32+15 
                t2 = (state[2] >> 6)  | (state[3] << 26);  // 47 + 23 = 70 = 2*32 + 6 
                t3 = (state[2] >> 21) | (state[3] << 11);  // 47 + 23 + 15 = 85 = 2*32 + 21      
                t4 = (state[2] >> 27) | (state[3] << 5);   // 47 + 23 + 15 + 6 = 91 = 2*32 + 27 
                feedback = state[0] ^ t1 ^ (~(t2 & t3)) ^ t4 ^ ((unsigned int*)key)[i & 3];
                // shift 32 bit positions 
                state[0] = state[1]; state[1] = state[2]; state[2] = state[3]; 
                state[3] = feedback ;
        }
lwc-tester committed
36 37 38 39 40 41 42 43 44
}

// The initialization  
/* The input to initialization is the 128-bit key; 96-bit IV;*/
void initialization(const unsigned char *key, const unsigned char *iv, unsigned int *state)
{
        int i;

        //initialize the state as 0  
Hongjun Wu committed
45
        for (i = 0; i < 4; i++) state[i] = 0;     
lwc-tester committed
46

Hongjun Wu committed
47 48
        //update the state with the key  
        state_update(state, key, NROUND2);  
lwc-tester committed
49

Hongjun Wu committed
50
        //introduce IV into the state  
lwc-tester committed
51 52
        for (i = 0;  i < 3; i++)  
        {
Hongjun Wu committed
53 54 55 56
                state[1] ^= FrameBitsIV;   
                state_update(state, key, NROUND1); 
                state[3] ^= ((unsigned int*)iv)[i]; 
        }   
lwc-tester committed
57 58 59 60 61
}

//process the associated data   
void process_ad(const unsigned char *k, const unsigned char *ad, unsigned long long adlen, unsigned int *state)
{
Hongjun Wu committed
62 63 64 65 66 67 68 69 70
        unsigned long long i; 
        unsigned int j; 

        for (i = 0; i < (adlen >> 2); i++)
        {
                state[1] ^= FrameBitsAD;
                state_update(state, k, NROUND1);
                state[3] ^= ((unsigned int*)ad)[i];
        }
lwc-tester committed
71

Hongjun Wu committed
72 73 74 75 76 77 78 79
        // if adlen is not a multiple of 4, we process the remaining bytes
        if ((adlen & 3) > 0)
        {
                state[1] ^= FrameBitsAD;
                state_update(state, k, NROUND1);
                for (j = 0; j < (adlen & 3); j++)  ((unsigned char*)state)[12 + j] ^= ad[(i << 2) + j];
                state[1] ^= adlen & 3;
        }   
lwc-tester committed
80 81 82 83 84 85 86 87 88 89 90 91
}     

//encrypt plaintext   
int crypto_aead_encrypt(
	unsigned char *c,unsigned long long *clen,
	const unsigned char *m,unsigned long long mlen,
	const unsigned char *ad,unsigned long long adlen,
	const unsigned char *nsec,
	const unsigned char *npub,
	const unsigned char *k
	)
{
Hongjun Wu committed
92
        unsigned long long i;
lwc-tester committed
93
	unsigned int j; 
Hongjun Wu committed
94 95
        unsigned char mac[8]; 
        unsigned int state[4];   
lwc-tester committed
96

Hongjun Wu committed
97 98
        //initialization stage
        initialization(k, npub, state);
lwc-tester committed
99

Hongjun Wu committed
100 101
        //process the associated data   
        process_ad(k, ad, adlen, state); 
lwc-tester committed
102

Hongjun Wu committed
103 104 105
        //process the plaintext    
        for (i = 0; i < (mlen >> 2); i++)
        {
lwc-tester committed
106 107 108 109
		state[1] ^= FrameBitsPC;  
		state_update(state, k, NROUND2); 
		state[3] ^= ((unsigned int*)m)[i];  
		((unsigned int*)c)[i] = state[2] ^ ((unsigned int*)m)[i];  
Hongjun Wu committed
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
        }
        // if mlen is not a multiple of 4, we process the remaining bytes
        if ((mlen & 3) > 0)
        {   
                state[1] ^= FrameBitsPC; 
                state_update(state, k, NROUND2);    
                for (j = 0; j < (mlen & 3); j++)  
                {
                        ((unsigned char*)state)[12 + j] ^= m[(i << 2) + j];   
                        c[(i << 2) + j] = ((unsigned char*)state)[8 + j] ^ m[(i << 2) + j];
                }   
                state[1] ^= mlen & 3;   
        }

        //finalization stage, we assume that the tag length is 8 bytes
        state[1] ^= FrameBitsFinalization;
        state_update(state, k, NROUND2);
        ((unsigned int*)mac)[0] = state[2];

        state[1] ^= FrameBitsFinalization;
        state_update(state, k, NROUND1);
        ((unsigned int*)mac)[1] = state[2];

        *clen = mlen + 8; 
        for (j = 0; j < 8; j++) c[mlen+j] = mac[j];  

        return 0;
lwc-tester committed
137 138 139 140 141 142 143 144 145 146 147 148
}

//decrypt a message
int crypto_aead_decrypt(
	unsigned char *m,unsigned long long *mlen,
	unsigned char *nsec,
	const unsigned char *c,unsigned long long clen,
	const unsigned char *ad,unsigned long long adlen,
	const unsigned char *npub,
	const unsigned char *k
	)
{
Hongjun Wu committed
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
        unsigned long long i;
        unsigned int j, check = 0;
        unsigned char mac[8];
        unsigned int state[4];

        *mlen = clen - 8; 

        //initialization stage
        initialization(k, npub, state);

        //process the associated data   
        process_ad(k, ad, adlen, state);

        //process the ciphertext    
        for (i = 0; i < (*mlen >> 2); i++)
        {
                state[1] ^= FrameBitsPC;
                state_update(state, k, NROUND2);
                ((unsigned int*)m)[i] = state[2] ^ ((unsigned int*)c)[i];
                state[3] ^= ((unsigned int*)m)[i]; 
        }
        // if mlen is not a multiple of 4, we process the remaining bytes
        if ((*mlen & 3) > 0)   
        {
                state[1] ^= FrameBitsPC;  
                state_update(state, k, NROUND2);
                for (j = 0; j < (*mlen & 3); j++)
                {
                        m[(i << 2) + j] = c[(i << 2) + j] ^ ((unsigned char*)state)[8 + j];
                        ((unsigned char*)state)[12 + j] ^= m[(i << 2) + j];
                }   
                state[1] ^= *mlen & 3;  
        }
lwc-tester committed
182
	
Hongjun Wu committed
183 184 185 186
        //finalization stage, we assume that the tag length is 8 bytes
        state[1] ^= FrameBitsFinalization;
        state_update(state, k, NROUND2);
        ((unsigned int*)mac)[0] = state[2];
lwc-tester committed
187
	
Hongjun Wu committed
188 189 190 191 192 193 194 195
        state[1] ^= FrameBitsFinalization;
        state_update(state, k, NROUND1);    
        ((unsigned int*)mac)[1] = state[2];

        //verification of the authentication tag   
        for (j = 0; j < 8; j++) { check |= (mac[j] ^ c[clen - 8 + j]); }
        if (check == 0) return 0;  
        else return -1;
lwc-tester committed
196
}