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

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

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

#include "wage.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
21 22 23
/*
 *rate_bytes: positions of rate bytes in state
 */
lwc-tester committed
24 25
const unsigned char rate_bytes[10] = {8,9,15,16,18,27,28,34,35,36};

lwc-tester committed
26 27 28 29 30 31
/*
 *wage_init: initialization with key and nonce
 *k: key
 *npub: nonce
 *state: state after initialization
 */
lwc-tester committed
32 33 34 35 36 37 38 39 40 41
int wage_init(
		unsigned char *state, 
		const unsigned char *npub,
		const unsigned char *k
	     )
{
	unsigned char i;
	unsigned char *key, *nonce;
	key = (unsigned char*)malloc(19*sizeof(unsigned char));
	nonce = (unsigned char*)malloc(19*sizeof(unsigned char));
lwc-tester committed
42 43
        
        //Convert 128-bit key and nonce arrays into 7-bit word arrays
lwc-tester committed
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
	convert_bytekey_to_wordkey(key, k);
	convert_bytekey_to_wordkey(nonce, npub);

	//Initialize the state to all-ZERO 
	for ( i = 0; i < STATEBYTES; i++ )
		state[i] = 0x0;
		
	if ( CRYPTO_KEYBYTES == 16 && CRYPTO_NPUBBYTES == 16 )
	{
		for ( i = 0; i < 9; i++ )
			state[i] = key[2*i];
		
		for ( i = 0; i < 7; i++ )
			state[i+9] = nonce[2*i+1];
		state[16] = nonce[17];

		key[18]=(key[18]^(nonce[18]>>2));
		
		state[17] = nonce[15];
		state[18] = key[18];
                
		for ( i = 0; i < 9; i++ )
			state[i+19] = key[2*i+1];
		
		for ( i = 0; i < 9; i++ )
			state[i+28] = nonce[2*i];
		
		wage_permutation(state);
lwc-tester committed
72 73
                
                //Absorbing first 64-bit key
lwc-tester committed
74 75 76 77 78 79
		for ( i = 0; i < 9; i++ )
			state[rate_bytes[i]]^=key[i];
		state[rate_bytes[9]]^=(key[18]&(0x40));

		wage_permutation(state);

lwc-tester committed
80
		//Absorbing last 64-bit key
lwc-tester committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
		for ( i = 0; i < 9; i++ )
			state[rate_bytes[i]]^=key[i+9];
		state[rate_bytes[9]]^=((key[18]<<1)&(0x40));

		wage_permutation(state);
	}
	else
	{
		return KAT_CRYPTO_FAILURE;
	}
	free(key);
	free(nonce);
return KAT_SUCCESS;
}

lwc-tester committed
96 97 98 99 100 101 102 103
/*
 *wage_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
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
int wage_ad(
			unsigned char *state,
			const unsigned char *ad, 
			const u64 adlen
		     )
{
	unsigned char i, lastblock[8], lblen;
        unsigned char mword[10];
	u64 j, ad64len = adlen/8;
	lblen = (unsigned char)(adlen%8);

	if ( adlen == 0 )
		return(KAT_SUCCESS);

	//Absorbing associated data
        if ( adlen != 0 )
        {
                for ( j = 0; j < ad64len; j++ )
                {
                        convert_8bytes_to_7bitwords(mword, ad, 8*j);
                        for ( i = 0; i < 10; i++ )
                                state[rate_bytes[i]]^=mword[i];
                        //Domain seperator
                        state[0]^=(0x40);
                        wage_permutation(state);
                }
lwc-tester committed
130
                //Process the last 64-bit block if "adlen" is not a multiple of 8
lwc-tester committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                if ( lblen != 0 )
                {
                        //Padding rule 10*
                        for ( i = 0; i < lblen; i++ )
                                lastblock[i] = ad[ad64len*8+(u64)i];
                        lastblock[lblen] = 0x80;
                        for ( i = lblen+1; i < 8; i++ )
                                lastblock[i] = 0x0;
                        
                        convert_8bytes_to_7bitwords(mword, lastblock, 0);
                        for ( i = 0; i < 10; i++ )
                                state[rate_bytes[i]]^=mword[i];
                        
                        //Domain seperator
                        state[0]^=(0x40);
                        wage_permutation(state );
                }
                else
                {
lwc-tester committed
150 151 152
                        state[rate_bytes[0]]^=(0x40); //Padding: 10*
                        //Domain seperator
                        state[0]^=(0x40);
lwc-tester committed
153 154 155 156 157 158 159
                        wage_permutation(state );
                }
        }

return (KAT_SUCCESS);
}

lwc-tester committed
160 161 162 163 164 165 166
/*
 *wage_gentag: generate tag
 *k: key
 *state: state before tag generation
 *tlen: length of tag in byte
 *tag: tag (in byte)
 */
lwc-tester committed
167 168 169 170 171 172 173 174 175 176 177
int wage_gentag(
                unsigned char *tag,
                const unsigned char tlen,
                unsigned char *state,
                const unsigned char *k
                )
{
        unsigned char i;
        unsigned char key[19], tmp_tag[19];
        if ( CRYPTO_KEYBYTES == 16 && tlen == 16 )
        {
lwc-tester committed
178
                //Convert 16-byte key into 7-bit words before absorbing
lwc-tester committed
179 180
                convert_bytekey_to_wordkey(key, k);
                
lwc-tester committed
181
                //Absorbing first 64-bit key
lwc-tester committed
182 183 184 185 186 187
                for ( i = 0; i < 9; i++ )
                        state[rate_bytes[i]]^=key[i];
                state[rate_bytes[9]]^=(key[18]&(0x40));
                
                wage_permutation(state);
                
lwc-tester committed
188
                //Absorbing last 64-bit key
lwc-tester committed
189 190 191 192 193 194 195 196 197 198 199 200
                for ( i = 0; i < 9; i++ )
                        state[rate_bytes[i]]^=key[i+9];
                state[rate_bytes[9]]^=((key[18]<<1)&(0x40));
                
                wage_permutation(state);
                
                //Extracting 128-bit tag
                for ( i = 0; i < 9; i++ )
                {
                        tmp_tag[2*i] = state[28+i];
                        tmp_tag[2*i+1] = state[9+i];
                }
lwc-tester committed
201 202
         
                tmp_tag[18] = ((state[18]<<2)&(0x60));
lwc-tester committed
203
                
lwc-tester committed
204
                //Return tag in byte
lwc-tester committed
205 206 207 208 209 210 211 212 213 214
                convert_tag_word_to_byte(tag, tmp_tag);
        }
        else
        {
                printf("Invalid key and tag length pair.\n");
                return KAT_CRYPTO_FAILURE;
        }
        return KAT_SUCCESS;
}

lwc-tester committed
215 216 217 218 219 220 221 222 223 224 225 226
/*
 *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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
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 char *state;
	unsigned char *tag;
	unsigned char i, lastblock[8], lblen;
        unsigned char mword[10], cword[10], tmp_c[8];
	u64 j, m64len;


	m64len = mlen/8;
	lblen = (unsigned char)(mlen%8);
lwc-tester committed
245
	nsec = nsec;
lwc-tester committed
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

	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 ( wage_init(state, npub, k)!= KAT_SUCCESS )
		return(KAT_CRYPTO_FAILURE);
        
        
	//Absorbing "ad"
        if ( adlen != 0 )
        {
                if ( wage_ad( state, ad, adlen) != KAT_SUCCESS)
                        return(KAT_CRYPTO_FAILURE);
        }

	//Encrypting "message(m)" and producing "ciphertext (c)"
        if ( mlen != 0 )
        {
                for ( j = 0; j < m64len; j++ )
                {
                        convert_8bytes_to_7bitwords(mword, m, 8*j);
                        
                        for ( i = 0; i < 10; i++ )
                        {
                                cword[i] = mword[i]^state[rate_bytes[i]];
                                state[rate_bytes[i]] = cword[i];
                        }
                        convert_7bitwords_to_8bytes(tmp_c, cword, 0);
                        for ( i = 0; i < 8; i++ )
                              c[8*j+((u64)i)] = tmp_c[i];

                        //Domain seperator
                        state[0]^=(0x20);
                        wage_permutation(state);
                }

                if ( lblen != 0 )
                {
                        //Padding the last block
                        for ( i = 0; i < lblen; i++ )
                                lastblock[i] = m[m64len*8+(u64)i];

                        lastblock[lblen] = 0x80; //Padding: 10*
                        for ( i = lblen+1; i < 8; i++ )
                                lastblock[i] = 0x0;
                        
                        //Encrypting the padded 64-bit block when "mlen" is not a multiple of 8
                        convert_8bytes_to_7bitwords (mword, lastblock, 0);
                        
                        for ( i = 0; i < 10; i++ )
                        {
                                cword[i] = mword[i]^state[rate_bytes[i]];
                                state[rate_bytes[i]] = cword[i];
                        }
                        convert_7bitwords_to_8bytes(tmp_c, cword, 0);
                        for ( i = 0; i < lblen; i++ )
                                c[8*m64len+((u64)i)] = tmp_c[i];

                        //Domain seperator
                        state[0]^=(0x20);
                        wage_permutation(state);
                }
                else
                {
lwc-tester committed
311
                        state[rate_bytes[0]]^=(0x40); //Padding: 10*
lwc-tester committed
312 313 314 315 316 317 318
                        //Domain seperator
                        state[0]^=(0x20);
                        wage_permutation(state );
                }
        }
        else
        {
lwc-tester committed
319
                state[rate_bytes[0]]^=(0x40); //Padding: 10*
lwc-tester committed
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
                //Domain seperator
                state[0]^=(0x20);
                wage_permutation(state );
        }
        //Appending tag to the end of ciphertext
	if ( wage_gentag( tag, CRYPTO_ABYTES, state, k ) != KAT_SUCCESS )
        {
		return(KAT_CRYPTO_FAILURE);
        }
        else
        {
                for ( i = 0; i < CRYPTO_ABYTES; i++ )
                        c[mlen+(u64)i] = tag[i];
        }
	*clen = mlen+CRYPTO_ABYTES;


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

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

lwc-tester committed
347 348 349 350 351 352 353 354 355 356 357 358
/*
 *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
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
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 char i, lblen, lastblock[8];
	u64 j, clen1, c64len = clen/8;
        unsigned char mword[10], cword[10], tmp_m[8];
        clen1 = clen - CRYPTO_ABYTES;
        c64len = clen1/8;
	lblen = (unsigned char)(clen1%8);
lwc-tester committed
374
	nsec = nsec;
lwc-tester committed
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
	
	unsigned char *state;
	unsigned char *tag, tlen = 16;

	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 ( wage_init(state, npub, k)!= KAT_SUCCESS )
		return(KAT_CRYPTO_FAILURE);

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

        if ( clen1 != 0 )
        {
                for ( j = 0; j < c64len; j++ )
                {
                        convert_8bytes_to_7bitwords(cword, c, 8*j);
                        for ( i = 0; i < 9; i++ )
                        {
                                mword[i] = cword[i]^state[rate_bytes[i]];
                                state[rate_bytes[i]] = cword[i];
                        }
			mword[9] = (cword[9]^state[rate_bytes[9]])&(0x40);
			state[rate_bytes[9]]^=mword[9];

			//for ( i = 0; i < 10; i++ )
			//	mword[i] = reverse1(mword[i]);
                        convert_7bitwords_to_8bytes(tmp_m, mword, 0);

                        for ( i = 0; i < 8; i++ )
                                m[8*j+((u64)i)] = tmp_m[i];

                        //Domain seperator
                        state[0]^=(0x20);
                        wage_permutation(state);
                }

                if ( lblen != 0 )
                {
                        //Decrypting last 64-bit block
                        for ( i = 0; i < lblen; i++ )
                                lastblock[i] = c[8*c64len +((u64)i)];
                        for ( i = lblen; i < 8; i++ )
                                lastblock[i] = 0x0;
                        convert_8bytes_to_7bitwords(cword, lastblock, 0);
                        for ( i = 0; i < 10; i++ )
                        {
                                mword[i] = cword[i]^state[rate_bytes[i]];
                        }
                        convert_7bitwords_to_8bytes (tmp_m, mword, 0);

                        for ( i = 0; i < lblen; i++ )
                                m[8*c64len+((u64)i)] = tmp_m[i];

                        for ( i = 0; i < lblen; i++ )
                                lastblock[i] = m[8*c64len+((u64)i)];
                        lastblock[lblen] = 0x80; //Padding: 10*
                        for ( i = lblen+1; i < 8; i++ )
                                lastblock[i] = 0x0;
                        convert_8bytes_to_7bitwords(mword, lastblock, 0);

                        for ( i = 0; i < 10; i++ )
                                state[rate_bytes[i]] = state[rate_bytes[i]]^mword[i];

                        //Domain seperator
                        state[0]^=0x20;
                        wage_permutation(state);
                }
                else
                {
lwc-tester committed
451
                        state[rate_bytes[0]]^=(0x40); //Padding: 10*
lwc-tester committed
452 453 454 455 456 457 458 459
                        //Domain seperator
                        state[0]^=(0x20);
                        wage_permutation(state );
                }
        }
        else
        {
                m=NULL;
lwc-tester committed
460
                state[rate_bytes[0]]^=(0x40); //Padding: 10*
lwc-tester committed
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
                //Domain seperator
                state[0]^=(0x20);
                wage_permutation(state );
        }
        
        //Generating and verifying the tag
	if ( wage_gentag( tag, tlen, state, k ) != KAT_SUCCESS )
        {
		return(KAT_CRYPTO_FAILURE);
        }
        else
        {
                for ( i = 0; i < CRYPTO_ABYTES; i++ )
                {
                        if ( c[clen1 + (u64)i] != tag[i] )
                              return(KAT_CRYPTO_FAILURE);
                }
        }
	*mlen = clen-CRYPTO_ABYTES;
	
	/*printf("Print tag after dec.:\n");
	for ( i = 0; i < tlen; i++ )
		printf("%.2X", tag[i]);
	printf("\n");*/

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