simple_128-128.c 6.85 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 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 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 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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
#include <string.h>
#include <stdlib.h>
#include "api.h"

#include "blockcipher.h"

//Message-Blocksize
#define MSZ 16


typedef unsigned char u8;		//used for Byte-Arrays
typedef unsigned int u32;		//used for regular counters
typedef unsigned long long ull;	//used for long counters

void E(u8 *ct, const u8 *key, const u8 *pt){
	
	blockcipher_encrypt(ct, pt, key);
	
	return;
}

void derive(u8 *K_E, u8 *K_MAC, u8 *N2, u8 *N1, const u8 *k, const u8 *npub){
	
	//N with first byte for counting
	u8 N_pad[MSZ] = { 0 };
	
	memcpy(&N_pad[1], npub, CRYPTO_NPUBBYTES);
	
	u8 T0[MSZ] = { 0 };
	u8 N2N1[MSZ] = { 0 };

	//(a) K_E
	E(K_E, k, N_pad);
	
	//(b) K_MAC
	N_pad[0] = 1;
	E(K_MAC, k, N_pad);
	
	//(c) [N2, N1]
	N_pad[0] = 2;
	E(N2N1, k, N_pad);
	for(u8 i=0; i<MSZ; i++){
		N2N1[i] ^= T0[i];
	}
	memcpy(N1, N2N1, MSZ/2);
	memcpy(N2, &N2N1[MSZ/2], MSZ/2);


	return;
}

void increase_ctr(u8 *ctr){
	
	for(u32 i=0; i<MSZ; i++){
		ctr[i]++;
		if(ctr[i] != 0){
			break;
		}
	}
	return;
}


int simple_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
){
	*clen = mlen+CRYPTO_ABYTES;
		
	//Don't use nsec
	if(nsec != NULL){
		u32 unused_32[1];
		memcpy(unused_32, nsec, 0);
	}

	//Rounding
	//Calculate number of blocks
	u32 r_M = mlen%MSZ;
	//u32 r_A = adlen%MSZ;
	ull mlen_blocks = r_M ? ((mlen/MSZ)+1) : (mlen/MSZ);
	//complete adlen_blocks to full blocks if last block is incomplete or add additional if last block is complete
	ull adlen_blocks = (adlen/MSZ)+1;
	
	
	u8 *T_A;
	T_A = (u8 *) malloc( MSZ*adlen_blocks * sizeof(u8));
	
	memset(T_A, 0, MSZ*adlen_blocks);
	memcpy(T_A, ad, adlen);
	//forced padding
	memset(&T_A[adlen], 0x01, 1);
	
	
	u8 *T_M;
	T_M = (u8 *) malloc( MSZ*mlen_blocks * sizeof(u8));
		
	memset(T_M, 0, MSZ*mlen_blocks);
	memcpy(T_M, m, mlen);
	
	
	u8 K_E[MSZ] = { 0 };
	u8 K_MAC[MSZ] = { 0 };
	u8 N1[MSZ/2] = { 0 };
	u8 N2[MSZ/2] = { 0 };
	u8 tag[MSZ] = { 0 };
	
	
	/*** Derive keys ***/
	derive(K_E, K_MAC, N2, N1, k, npub);
	
	
	/*** Encrypt ***/
	if(mlen == 0){
		;
	} else {
		u8 j_ctr[MSZ] = { 0 };
		
		//Set higher MSZ/2 Bytes of IV to N1
		memcpy(&j_ctr[MSZ/2], N1, MSZ/2);
		
		//CTRENC
		u8 V_j[MSZ] = { 0 };
		for(ull j=0; j<mlen_blocks; j++){
			
			//Vj = E(K_E, ctr_j)
			E(V_j, K_E, j_ctr);
			
			for(u32 i=0; i<MSZ; i++){
				T_M[j*MSZ+i] ^= V_j[i];
			}
			
			increase_ctr(j_ctr);
		}
	}
	memcpy(c, T_M, mlen);
	
	
	/*** Authenticate ***/
	//Reset T_M to message
	memset(T_M, 0, MSZ*mlen_blocks);
	memcpy(T_M, m, mlen);
	
	//Padding of M if last block is not full
	if(r_M != 0){
		memset(&T_M[mlen], 1, 1);
	}
	
	
	u8 X_temp[MSZ] = { 0 };
	
	u8 TMP[MSZ] = { 0 };
	
	//Set higher MSZ/2 Bytes of TMP to N2
	memcpy(&TMP[MSZ/2], N2, MSZ/2);
	
	
	//CBCMAC-IV
	for(ull j=0; j<mlen_blocks; j++){
		
		memcpy(X_temp, TMP, MSZ);
		
		//TMP XOR Xj
		for(u32 i=0; i<MSZ; i++){
			X_temp[i] ^= T_M[j*MSZ+i];
		}
		
		//TMP = E(K_MAC, TMP XOR Xj)
		E(TMP, K_MAC, X_temp);
	}
	
	//Padding of M if last block is full
	if( r_M == 0 ){
		
		memcpy(X_temp, TMP, MSZ);
		
		//TMP XOR 1
		X_temp[0] ^= 1;
		
		//TMP = E(K_MAC, TMP XOR Xj)
		E(TMP, K_MAC, X_temp);
	}
	
	for(ull j=0; j<adlen_blocks; j++){
		
		memcpy(X_temp, TMP, MSZ);
		
		//TMP XOR Xj
		for(u32 i=0; i<MSZ; i++){
			X_temp[i] ^= T_A[j*MSZ+i];
		}
		
		//TMP = E(K_MAC, TMP XOR Xj)
		E(TMP, K_MAC, X_temp);
	}
	
	//Tag = TMP
	memcpy(tag, TMP, MSZ);
	
	
	//Append Tag
	memcpy(&c[mlen], tag, CRYPTO_ABYTES);
	
	free(T_M);
	free(T_A);
	
	return 0;
}


int simple_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
)
{
	*mlen = clen-CRYPTO_ABYTES;
		
	//Don't use nsec
	if(nsec != NULL){
		u32 unused_32[1];
		memcpy(unused_32, nsec, 0);
	}

	//Rounding
	//Calculate number of blocks
	u32 r_C = *mlen%MSZ;
	//u32 r_A = adlen%MSZ;
	ull mlen_blocks = r_C ? ((*mlen/MSZ)+1) : (*mlen/MSZ);
	//complete adlen_blocks to full blocks if last block is incomplete or add additional if last block is complete
	ull adlen_blocks = (adlen/MSZ)+1;
	
	
	u8 *T_A;
	T_A = (u8 *) malloc( MSZ*adlen_blocks * sizeof(u8));
	
	memset(T_A, 0, MSZ*adlen_blocks);
	memcpy(T_A, ad, adlen);
	//forced padding
	memset(&T_A[adlen], 0x01, 1);
	
	
	u8 *T_C;
	T_C = (u8 *) malloc( MSZ*mlen_blocks * sizeof(u8));
	
	memset(T_C, 0, MSZ*mlen_blocks);
	memcpy(T_C, c, *mlen);
	
	
	u8 K_E[MSZ] = { 0 };
	u8 K_MAC[MSZ] = { 0 };
	u8 N1[MSZ/2] = { 0 };
	u8 N2[MSZ/2] = { 0 };
	u8 tag[MSZ] = { 0 };
	
	int is_auth = 0;
	
	
	/*** Derive keys ***/
	derive(K_E, K_MAC, N2, N1, k, npub);
	
	
	/*** Decrypt ***/
	if(*mlen == 0){
		;
	} else {
		u8 j_ctr[MSZ] = { 0 };
		
		//Set higher MSZ/2 Bytes of IV to N1
		memcpy(&j_ctr[MSZ/2], N1, MSZ/2);
		
		//CTRENC
		u8 V_j[MSZ] = { 0 };
		for(ull j=0; j<mlen_blocks; j++){
			
			//Vj = E(K_E, j)
			E(V_j, K_E, j_ctr);
			
			for(u32 i=0; i<MSZ; i++){
				T_C[j*MSZ+i] ^= V_j[i];
			}
			
			increase_ctr(j_ctr);
		}
		if(r_C > 0){
			//Truncate
			memset(&T_C[(mlen_blocks-1)*MSZ+r_C], 0, MSZ-r_C);
		}
	}
	
	
	/*** Authenticate ***/
	
	//Padding of M if last block is not full
	if(r_C != 0){
		memset(&T_C[*mlen], 1, 1);
	}
	
	u8 X_temp[MSZ] = { 0 };
	
	u8 TMP[MSZ] = { 0 };
	
	//Set higher MSZ/2 Bytes of TMP to N2
	memcpy(&TMP[MSZ/2], N2, MSZ/2);
	
	
	//CBCMAC-IV
	for(ull j=0; j<mlen_blocks; j++){
		
		memcpy(X_temp, TMP, MSZ);
		
		//TMP XOR Xj
		for(u32 i=0; i<MSZ; i++){
			X_temp[i] ^= T_C[j*MSZ+i];
		}
		
		//TMP = E(K_MAC, TMP XOR Xj)
		E(TMP, K_MAC, X_temp);
	}
	
	//Padding of M if last block is full
	if( r_C == 0 ){
		
		memcpy(X_temp, TMP, MSZ);
		
		//TMP XOR 1
		X_temp[0] ^= 1;
		
		//TMP = E(K_MAC, TMP XOR Xj)
		E(TMP, K_MAC, X_temp);
	}
	
	for(ull j=0; j<adlen_blocks; j++){
		
		memcpy(X_temp, TMP, MSZ);
		
		//TMP XOR Xj
		for(u32 i=0; i<MSZ; i++){
			X_temp[i] ^= T_A[j*MSZ+i];
		}
		
		//TMP = E(K_MAC, TMP XOR Xj)
		E(TMP, K_MAC, X_temp);
	}
	
	//Tag = TMP
	memcpy(tag, TMP, MSZ);
	
	
	//compare T' == T (0 for equal)
	if( memcmp(tag, &c[*mlen], CRYPTO_ABYTES) == 0 ){
				
		//reverse Padding of M if last block is not full
		if(r_C != 0){
			memset(&T_C[*mlen], 0, 1);
		}
	
		//only copy message if auth succeeded
		//M = (Mm-1,...,M0)
		memcpy(m, T_C, *mlen);
	
		//if T' == T -> is_auth = 1	
		is_auth = 1;
	
	} else {
		
		//if T' =/= T -> is_auth = 0
		is_auth = 0;
		
	}
	
	free(T_C);
	free(T_A);
	
	if(is_auth){
		return 0;
	} else {
		return -1;
	}
}