encrypt.c 5.76 KB
Newer Older
lwc-tester committed
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
/*   
     CLX-128: 128-bit key, 96-bit IV, 160-bit state  
     Optimized implementation of CLX-128 on 32-bit processor.   
     The state consists of five 32-bit registers
     state[4] || state[3] || state[2] || state[1] || state[0]
*/   

#include <string.h>
#include <stdio.h>
#include "crypto_aead.h"

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

#define NROUND1 384 
#define NROUND2 1152 

/*optimized state update function*/
void state_update(unsigned int *state, unsigned int number_of_steps)
{
	unsigned int i, j, temp;
	unsigned int t1, t2, t3, t4;
	for (i = 0; i < number_of_steps; i = i + 128)
	{
		t1 = (state[1] >> 3)  | (state[2] << 29);    //35   = 1*32 + 3   
		t2 = (state[2] >> 29) | (state[3] << 3);     //93   = 2*32 + 29   
		t3 = (state[3] >> 10) | (state[4] << 22);    //106  = 3*32 + 10  
		t4 = (state[3] >> 31) | (state[4] << 1);     //127  = 3*32 + 31  
		state[0] ^= t1 ^ (~(t2 & t3)) ^ t4;

		t1 = (state[2] >> 3)  | (state[3] << 29);       
		t2 = (state[3] >> 29) | (state[4] << 3);        
		t3 = (state[4] >> 10) | (state[0] << 22);      
		t4 = (state[4] >> 31) | (state[0] << 1);       
		state[1] ^= t1 ^ (~(t2 & t3)) ^ t4;

		t1 = (state[3] >> 3)  | (state[4] << 29);
		t2 = (state[4] >> 29) | (state[0] << 3);
		t3 = (state[0] >> 10) | (state[1] << 22);
		t4 = (state[0] >> 31) | (state[1] << 1);
		state[2] ^= t1 ^ (~(t2 & t3)) ^ t4;

		t1 = (state[4] >> 3)  | (state[0] << 29);
		t2 = (state[0] >> 29) | (state[1] << 3);
		t3 = (state[1] >> 10) | (state[2] << 22);
		t4 = (state[1] >> 31) | (state[2] << 1);
		state[3] ^= t1 ^ (~(t2 & t3)) ^ t4;

		// rotate 32 bit positions
		temp = state[4]; 
		for (j = 4; j >= 1; j--) state[j] = state[j - 1]; 
		state[0] = temp;  
	}
}


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

	//initialize the state as key
	state[0] = 0x80000000; 
	for (i = 0; i < 4; i++) state[i+1] = ((unsigned int*)key)[i]; 

	//update the state 
	state_update(state, NROUND2);

	//introduce iv into the state  
	for (i = 0;  i < 3; i++)  
	{
		state[2] ^= FrameBitsIV;
		state_update(state, NROUND1); 
		state[4] ^= ((unsigned int*)iv)[i]; 
	}    
}

//process the associated data     
void process_ad(const unsigned char *ad, unsigned long long adlen, unsigned int *state)
{
	unsigned long long i;
	unsigned int j;

	for (i = 0; i < (adlen >> 2); i++)
	{
		state[2] ^= FrameBitsAD;
		state_update(state, NROUND1);
		state[4] ^= ((unsigned int*)ad)[i];
	}

	// if adlen is not a multiple of 4, we process the remaining bytes
	if ((adlen & 3) > 0)
	{
		state[2] ^= FrameBitsAD;
		state_update(state, NROUND1);
		for (j = 0; j < (adlen & 3); j++)  ((unsigned char*)state)[16 + j] ^= ad[(i << 2) + j];
		state[2] ^= adlen & 3;
	}
}

//encrypt a message
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
	)
{
	unsigned long long i;
	unsigned int j; 
	unsigned char mac[8]; 
	unsigned int state[5]; 

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

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

	//process the plaintext    
	for (i = 0; i < (mlen >> 2); i++)
	{
		state[2] ^= FrameBitsPC; 
		state_update(state, NROUND2);
		state[4] ^= ((unsigned int*)m)[i];
		((unsigned int*)c)[i] = state[4]; 
	}
	// if mlen is not a multiple of 4, we process the remaining bytes
	if ((mlen & 3) > 0)
	{
		state[2] ^= FrameBitsPC;
		state_update(state, NROUND2);  
		for (j = 0; j < (mlen & 3); j++)
		{
			((unsigned char*)state)[16 + j] ^= m[(i << 2) + j];
			c[(i << 2) + j] = ((unsigned char*)state)[16 + j];
		}  
		state[2] ^= (mlen & 3);  
	}

	//finalization stage, we assume that the tag length is a multiple of bytes
	for (i = 0; i < 2; i++)
	{
		state[2] ^= FrameBitsFinalization;
		state_update(state, NROUND2);
		((unsigned int*)mac)[i] = state[4];
	}

	*clen = mlen + 8; 
	memcpy(c+mlen, mac, 8);  

	return 0;
}

//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
	)
{
	unsigned long long i;
	unsigned int j, check = 0;
 	unsigned char mac[8];
	unsigned int state[5];

	*mlen = clen - 8; 

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

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

	//process the ciphertext    
	for (i = 0; i < (*mlen >> 2); i++)
	{
		state[2] ^= FrameBitsPC;       
		state_update(state, NROUND2);
		((unsigned int*)m)[i] = state[4] ^ ((unsigned int*)c)[i];
		state[4] = ((unsigned int*)c)[i];
	}
	// if mlen is not a multiple of 4, we process the remaining bytes
	if ((*mlen & 3) > 0) 
	{
		state[2] ^= FrameBitsPC;
		state_update(state, NROUND2);
		for (j = 0; j < (*mlen & 3); j++)
		{
			m[(i << 2) + j] = ((unsigned char*)state)[16 + j] ^ c[(i << 2) + j];
			((unsigned char*)state)[16 + j] = c[(i << 2) + j];
		}   
		state[2] ^= *mlen & 3;  
	}

	//finalization stage, we assume that the tag length is a multiple of bytes
	for (i = 0; i < 2; i++)
	{
		state[2] ^= FrameBitsFinalization;
		state_update(state, NROUND2);
		((unsigned int*)mac)[i] = state[4];
	}
			
	for (i = 0; i  < 8; i++) check |= (mac[i] ^ c[clen - 8 + i]);
	if (check == 0) return 0;
	else return -1;
}