encrypt.c 8.25 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


/* Reference implementation of SpoC64-128, AEAD
   Written by:
   Kalikinkar Mandal <kmandal@uwaterloo.ca>
*/

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<stdint.h>

#include "sliscp_light192.h"
#include "crypto_aead.h" 
#include "api.h" 

#define KAT_SUCCESS          0
#define KAT_FILE_OPEN_ERROR -1
#define KAT_DATA_ERROR      -3
#define KAT_CRYPTO_FAILURE  -4

lwc-tester committed
22 23 24
/*
   *rate_bytes192: positions of rate bytes in state
*/
lwc-tester committed
25
const unsigned char rate_bytes192[8] = {0,1,2,3,12,13,14,15};
lwc-tester committed
26 27 28
/*
   *masking_bytes192: positions of masked capacity bytes in state
*/
lwc-tester committed
29 30
const unsigned char masking_bytes192[8] = {6,7,8,9,18,19,20,21};

lwc-tester committed
31 32 33 34 35 36
/*
   *spoc64_init: initialization with key and nonce
   *k: key 
   *npub: nonce
   *state: state after initialization
*/
lwc-tester committed
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
int spoc64_init(
			unsigned char *state, 
			const unsigned char *npub,
			const unsigned char *k
			)
{
	unsigned char i;

	// Initialize the state to all-ZERO 
	for ( i = 0; i < STATEBYTES; i++ )
		state[i] = 0x0;

	if ( CRYPTO_KEYBYTES == 16 && CRYPTO_NPUBBYTES == 16 )
	{
		//load-SpoC-64(N,K):
		//Assigning nonce
		for ( i = 0; i < 4; i++ )
			state[i] = npub[i];
		for ( i = 0; i < 4; i++ )
			state[12+i] = npub[4+i];

		//Assigning key
		for ( i = 0; i < 6; i++ )
			state[6+i] = k[i];
		state[4] = k[6]; 
		state[5] = k[7];
		for ( i = 0; i < 6; i++ )
			state[18+i] = k[8+i];
		state[16] = k[14]; 
		state[17] = k[15];
		
		sliscp_permutation192r18(state );

		for ( i = 0; i < 8; i++ )
			state[masking_bytes192[i]]^=npub[i+8];
	}
	else
	{
		return KAT_CRYPTO_FAILURE;
	}
return KAT_SUCCESS;
}

lwc-tester committed
80 81 82 83 84 85 86 87
/*
   *spoc64_ad: processing associated data
   *adlen: byte-length of ad
   *ad: associated data
   *state: state after initialization, 
           and output state is stored 
	   in "state" (inplace) 
*/
lwc-tester committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
int spoc64_ad(
			unsigned char *state,
			const unsigned char *ad, 
			const u64 adlen
		     )
{
	unsigned char i, lblen;
	u64 j, ad64len = adlen/8;
	lblen = (unsigned char)(adlen%8);

	if ( adlen == 0 )
		return(KAT_SUCCESS);
	
	//Absorbing associated data
	for ( j = 0; j < ad64len; j++ )
	{
                sliscp_permutation192r18(state);
		for ( i = 0; i < 8; i++ )
			state[masking_bytes192[i]]^=ad[8*j+((u64)i)];
		//ctrl_ad_full
                state[0] = state[0]^(0x20);
	}

lwc-tester committed
111
	//Processing the last 64-bit block if "adlen" is not a multiple of 8
lwc-tester committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125
	if ( lblen != 0 )
	{
                sliscp_permutation192r18(state);
		for ( i = 0; i < lblen; i++ )
			state[masking_bytes192[i]]^=ad[ad64len*8+(u64)i];

                state[masking_bytes192[lblen]]^=0x80; //Padding: 10*
                //ctrl_ad_par
                state[0] = state[0]^(0x30);
	}

return (KAT_SUCCESS);
}

lwc-tester committed
126 127 128 129 130 131 132
/*
   *spoc64_gentag: generate tag
   *k: key
   *state: state before tag generation
   *tlen: length of tag in byte
   *tag: tag
*/
lwc-tester committed
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
int spoc64_gentag(
			unsigned char *tag, 
			const unsigned char tlen,
			unsigned char *state 
		        )
{
	unsigned char i;
	if ( CRYPTO_KEYBYTES == 16 && tlen == 8 )
	{
		//ctrl_tag
		state[0] = state[0]^(0x80);
		sliscp_permutation192r18(state );
		//Extracting 64-bit tag from X1 and X3
		for ( i = 0; i < 4; i++ )
		{
			tag[i] = state[6+i];
			tag[4+i] = state[18+i];
		}
	}
	else
	{
		printf("Invalid key and tag length pair.\n");
		return KAT_CRYPTO_FAILURE;
	}

return KAT_SUCCESS;
}

lwc-tester committed
161 162 163 164 165 166 167 168 169 170 171 172
/*
   *crypto_aead_encrypt: encrypt message and produce tag
   *k: key 
   *npub: nonce
   *nsec: NULL
   *adlen: length of ad
   *ad: associated data
   *mlen: length of message
   *m: message to be encrypted
   *clen: ciphertext length + tag length
   *c: ciphertext, followed by tag
*/
lwc-tester committed
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
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
			)
{
	//avoiding unused parameter warning
	nsec=nsec;

	unsigned char *state;
	unsigned char *tag;
	unsigned char i, lblen;
	//int func_ret;
	u64 j, m64len;

	m64len = mlen/8;
	lblen = (unsigned char)(mlen%8);

	state = (unsigned char *)malloc(sizeof(unsigned char)*STATEBYTES);
	tag = (unsigned char *)malloc(sizeof(unsigned char)*CRYPTO_ABYTES);

	//Initialize state with "key" and "nonce" and then absorbe "nonce" again
	if ( spoc64_init(state, npub, k)!= KAT_SUCCESS )
		return(KAT_CRYPTO_FAILURE);
	//Absorbing "ad" if non-empty
	if ( adlen != 0 )
	{
		if ( spoc64_ad( state, ad, adlen) != KAT_SUCCESS)
			return(KAT_CRYPTO_FAILURE);
	}
	
	if ( mlen != 0 )
	{
		//Encrypting "message(m)" and producing "ciphertext (c)"
		for ( j = 0; j < m64len; j++ )
		{
                        sliscp_permutation192r18(state);
			for ( i = 0; i < 8; i++ )
			{
				c[8*j+((u64)i)] = m[8*j+((u64)i)]^state[rate_bytes192[i]];
				state[masking_bytes192[i]]^=m[8*j+((u64)i)];
			}
			//ctrl_pt
                        state[0] = state[0]^(0x40);
		}

		if ( lblen != 0 )
		{
                        sliscp_permutation192r18(state);
			for ( i = 0; i < lblen; i++ )
			{
				c[8*m64len+((u64)i)] = m[m64len*8+(u64)i]^state[rate_bytes192[i]];
				state[masking_bytes192[i]]^=m[8*m64len+((u64)i)];
			}
			state[masking_bytes192[lblen]]^=(0x80); //Padding: 10*
                        //ctrl_pt_par
                        state[0] = state[0]^(0x50);
		}
	}
	*clen = mlen+CRYPTO_ABYTES;

        //Appending tag to the end of ciphertext
	if ( spoc64_gentag( tag, CRYPTO_ABYTES, state ) != KAT_SUCCESS )
		return(KAT_CRYPTO_FAILURE);
        else
        {
                for ( i = 0; i < CRYPTO_ABYTES; i++ )
                        c[mlen+(u64)i] = tag[i];
        }
	
	/*printf("Print tag:\n");
	for ( i = 0; i < 8; i++ )
		printf("%.2x ", tag[i]);
	printf("\n");*/

	free(state);
	free(tag);
return KAT_SUCCESS;
}

lwc-tester committed
256 257 258 259 260 261 262 263 264 265 266 267
/*
   *crypto_aead_decrypt: decrypt ciphertext and verify tag
   *k: key 
   *npub: nonce
   *nsec: NULL
   *adlen: length of ad
   *ad: associated data
   *clen: ciphertext length + tag length
   *c: ciphertext, followed by tag
   *mlen: length of message
   *m: message
*/
lwc-tester committed
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
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
			)
{
	//avoiding unused parameter warning
	nsec=nsec;

	unsigned char i, lblen;
	u64 j, clen1, c64len;
        clen1 = clen-CRYPTO_ABYTES;
        c64len = clen1/8;
        lblen = (unsigned char)(clen1%8);
        
        //If clen < the tag length, return error.
        if ( clen < CRYPTO_ABYTES)
                return(KAT_CRYPTO_FAILURE);
	
	unsigned char *state;
	unsigned char *tag;

	state = (unsigned char *)malloc(sizeof(unsigned char)*STATEBYTES);
	tag = (unsigned char *)malloc(sizeof(unsigned char)*CRYPTO_ABYTES);

	//Initialize state with "key" and "nonce" and then absorbe "key" again
	if ( spoc64_init(state, npub, k)!= KAT_SUCCESS )
		return(KAT_CRYPTO_FAILURE);

	//Absorbing "ad"
	if (adlen != 0 )
	{
		if ( spoc64_ad( state, ad, adlen) != KAT_SUCCESS)
			return(KAT_CRYPTO_FAILURE);
	}

	if ( clen1 != 0 )
	{
		for ( j = 0; j < c64len; j++ )
		{
                        sliscp_permutation192r18(state);
			for ( i = 0; i < 8; i++ )
			{
				m[8*j+((u64)i)] = c[8*j+((u64)i)]^state[rate_bytes192[i]];
				state[masking_bytes192[i]]^=m[8*j+((u64)i)];
			}
                        //ctrl_pt
                        state[0] = state[0]^(0x40);
		}

		if ( lblen != 0 )
		{
                        sliscp_permutation192r18(state);
			for ( i = 0; i < lblen; i++ )
			{
				m[8*c64len +((u64)i)] = c[8*c64len +((u64)i)]^state[rate_bytes192[i]];
				state[masking_bytes192[i]]^=m[8*c64len +((u64)i)];
			}
			state[masking_bytes192[lblen]]^=(0x80); //Padding 10*
			//ctrl_pt_par
                        state[0] = state[0]^(0x50);
		}
	}
        
	//Generating and verifying the tag
	if ( spoc64_gentag( tag, CRYPTO_ABYTES, state ) != KAT_SUCCESS )
		return(KAT_CRYPTO_FAILURE);
        else
        {
                for ( i = 0; i < CRYPTO_ABYTES; i++ )
                {
                        if ( c[clen1 + (u64)i] != tag[i] )
                                return -1; // authentication failure should result in return code -1.
                }
        }
	*mlen = clen-CRYPTO_ABYTES;

	/*printf("Print tag:\n");
	for ( i = 0; i < 8; i++ )
		printf("%.2x ", tag[i]);
	printf("\n");*/

	free(state);
	free(tag);
	
return KAT_SUCCESS;
}