Example #1
0
int main(int argc, char*argv[])
{
	char src_file[100],c;
	char dest_file[100];

	FILE * fp_r,*fp_w;
	int sz;
	char com;
	double time,bsent;


	u32 mode;
	u32 version=1;
	u32 data_len,n;
	u32 inc = 20;

	u8 *data_in;
	u8 buffer_in[MAX_DATA_IN];
	u8 buffer_out[MAX_DATA_OUT];
	u32 i,m,j;
		
		mode = ROUNDS_10 | ECB_FLAG |FIRST_FLAG| ENCRYPT_FLAG;
		mode = ROUNDS_10 | ECB_FLAG |FIRST_FLAG | DECRYPT_FLAG;

		strcpy(src_file, "infile");
		strcpy(dest_file, "outfile");

		fp_r = fopen(src_file,"rb");
		fp_w = fopen(dest_file,"wb");

		/* Initialization*/

		init( mode );

	 	 /* cipher/decipher */

		while( (data_len = fread(buffer_in,1,MAX_DATA_IN,fp_r))>0){
			if(data_len == ERROR_CODE){
				printf("\n Error : fread() \n");
				return -1;
			}

			if(update(buffer_in,data_len,buffer_out,&n)== ERROR_CODE){
				printf("\n Error : Update() \n");
				return -1;
			}

			fwrite(buffer_out,1,n,fp_w);
		}


		if(doFinal(buffer_out,&n) == ERROR_CODE){
			printf("\n Error : Update() \n");
			return -1;
		}

		if(n>0){ fwrite(buffer_out,1,n,fp_w);}

		fclose(fp_r);
		fclose(fp_w);

}
void ThreePC::doMaster(bool ongo) {
	//Tag *tag = new Tag(size, myid);  // used to record reply status
	bool ongoing = ongo; // every time there is only one outstanding commitment,
	while (true) {
		fd_set fdset;
		struct timeval timeout = { 2, 0 };  // time for time out
		if (TESTELECTION1) {
			testElection1();
		} else if (TESTELECTION2) {
			testElection2();
		} else if (TESTSUB) {
			testSubsequentFail();
		}
		if (isTermination == false) { // normal 3pc protocol
			if (ongoing == false && state == INIT) { // if sending queue is not empty and no outstanding msg pick one and send to all
				char t = 0;
				cin >> t;
				if (t != 'y') {
					continue;
				}
				tag->setAlltoFalse();
				times = TRY;
				ongoing = true;
				value++;  // set value;
				char *cancomit = mkMsg(CANCMT, value, myid);

				sendAllMsg(CANCMT, cancomit);
				state = WAIT;
				deleteMsg(CANCMT, cancomit);
			}
			FD_ZERO(&fdset);
			FD_SET(sockfd, &fdset);
			select(sockfd + 1, &fdset, NULL, NULL, &timeout);
			if (FD_ISSET(sockfd, &fdset)) {
				char *p = NULL;
				int type = recvMessage(p);
				if (type == REPLY && state == WAIT) {
					Reply *msg = (Reply *) p;
					assert(value == msg->val);
					if (msg->isyes == 1) {  // received a yes from cohort
						int sender = msg->senderid;
						tag->setTrue(sender);
						if (tag->checkAllTrue() && state == WAIT) { // all said yes

							char *precomit = mkMsg(PRECMT, msg->val, myid);
							tag->setAlltoFalse(); // reSet tag to all false except my
							sendAllMsg(PRECMT, precomit);
							state = PREP; // set state to prepare after sending preCommit
							deleteMsg(PRECMT, precomit); // garbage collection
							times = TRY;
						}
					} else if (msg->isyes == 0) {  // received a no from cohort
						tag->setAlltoFalse();
						char *abrt = mkMsg(ABORT, value, myid);
						sendAllMsg(ABORT, abrt);
						state = ABRT;
						deleteMsg(ABORT, abrt);
						times = TRY;
					}
				} else if (type == ACK && state == PREP) {  // in PREP(prepare)

					Ack *msg = (Ack *) p;
					assert(value == msg->val);
					int sender = msg->senderid;
					tag->setTrue(sender);
					if (tag->checkAllTrue() && state == PREP) { // all ack transmit to CMI commit
						char *docomit = mkMsg(DOCMT, msg->val, myid);
						tag->setAlltoFalse();
						sendAllMsg(DOCMT, docomit);
						state = CMT;  // set state to cmt
						deleteMsg(DOCMT, docomit);
						times = TRY;
					}

				} else if (type == HAVECMTED && state == CMT) {
					HaveCommitted *msg = (HaveCommitted *) p;
					assert(value == msg->val);
					int sender = msg->senderid;
					tag->setTrue(sender);
					if (tag->checkAllTrue() && state == CMT) { // all commit state transmit to INIT
					//tag->setAlltoFalse();
						ongoing = false;
						// do commit
						doFinal(C, value);
						state = INIT;
						times = TRY;
					}
				} else if (type == ABORTACK && state == ABRT) {
					AbortAck *msg = (AbortAck *) p;
					assert(value == msg->val);
					int sender = msg->senderid;
					tag->setTrue(sender);
					if (tag->checkAllTrue() && state == ABRT) {
						ongoing = false;
						// do abort
						doFinal(A, value);
						state = INIT;
						times = TRY;
					}
				}
			} else {  // lost some msg
				if (state == WAIT) {
					if (times-- > 0) { // re-transmit cancommit
						int *ids = tag->getUnsetId();
						char *cancomit = mkMsg(CANCMT, value, myid);

						for (int i = 0; i < size; ++i) {
							if (ids[i] != -1) {
								sendMessage(CANCMT, cancomit, ids[i]);
							}
						}
						deleteMsg(CANCMT, cancomit);
					} else {  // time out
						tag->setsitedown();
						char *abrt = mkMsg(ABORT, value, myid);
						tag->setAlltoFalse();
						sendAllMsg(ABORT, abrt);
						state = ABRT;   // to do
						deleteMsg(ABRT, abrt);
						times = TRY;
					}

				} else if (state == PREP) {
					if (times-- > 0) {
						int *ids = tag->getUnsetId();
						char *precomit = mkMsg(PRECMT, value, myid);

						for (int i = 0; i < size; ++i) {
							if (ids[i] != -1) {
								sendMessage(PRECMT, precomit, ids[i]);
							}
						}
						deleteMsg(PRECMT, precomit);
					} else {  // time out
						char *docomit = mkMsg(DOCMT, value, myid);
						tag->setsitedown();
						tag->setAlltoFalse();
						sendAllMsg(DOCMT, docomit);
						state = CMT;  // set state to cmt
						deleteMsg(DOCMT, docomit);
						times = TRY;

					}

				} else if (state == CMT) {
					if (times-- > 0) {
						int *ids = tag->getUnsetId();
						char *docomit = mkMsg(DOCMT, value, myid);
						for (int i = 0; i < size; ++i) {
							if (ids[i] != -1) {
								sendMessage(DOCMT, docomit, ids[i]);
							}
						}
						deleteMsg(DOCMT, docomit);
					} else {  // different
						tag->setsitedown();
						ongoing = false;
						// do commit
						doFinal(C, value);
						state = INIT;
						times = TRY;
					}
				} else if (state == ABRT) {
					if (times-- > 0) {
						int *ids = tag->getUnsetId();
						char *abrt = mkMsg(ABORT, value, myid);
						for (int i = 0; i < size; ++i) {
							if (ids[i] != -1) {
								sendMessage(ABORT, abrt, ids[i]);
							}
						}
						deleteMsg(ABORT, abrt);
					} else {
						tag->setsitedown();
						ongoing = false;
						// do abort
						doFinal(A, value);
						state = INIT;
						times = TRY;
					}
				}
			}
		} else {
			// phase 1
			// sends its state to other sites to make them transit to the state
			if (!transited && state != CMT && state != ABRT) {
/*
 * Class:     AESboxJNI
 * Method:    Decrypt
 * Signature: ([B)[B
 */
JNIEXPORT jbyteArray JNICALL Java_aiss_aesBox_AESboxJNI_Decrypt(JNIEnv *env, jobject obj, jbyteArray array){

	u32 mode = ROUNDS_10 | ECB_FLAG |FIRST_FLAG| DECRYPT_FLAG;

	/* Initialization*/
	init( mode );

	// In Array and Size
	jbyte *inArr = env->GetByteArrayElements(array, NULL);
	jsize lengthOfArray = env->GetArrayLength(array);

	//Variaveis
	u32 fullPackets = lengthOfArray/MAX_DATA_IN;
	u32 remainBytes = lengthOfArray%MAX_DATA_IN;
	u8 buffer_out[MAX_DATA_OUT];
	u8 *buffer_temp = malloc((lengthOfArray + AES_BLOCK_SIZE)*sizeof(u8));
	u32 i, returnSize, totalSize = 0;
	jbyteArray outArray;

	//printf("Decryption\n");
	//printf("Number of Bytes:%d\n", lengthOfArray);
	//printf("Number of MAX_DATA_IN:%d\n", MAX_DATA_IN);
	//printf("Number of fullPackets:%u\n", fullPackets);
	//printf("Number of remainBytes:%u\n", remainBytes);

	// Se tiveremos mais que MAX_DATA_IN dividimos os dados
	// em pedacos do tamanho MAX_DATA_IN fazendo update() a cada um
	// fazemos doFinal aos remanescentes dados
	if(fullPackets > 0){
		for(i = 0; i < fullPackets; i++){

			//cipher
			update(&inArr[i * MAX_DATA_IN], MAX_DATA_IN, buffer_out, &returnSize);
			memcpy(&buffer_temp[totalSize], buffer_out, returnSize);
			totalSize += returnSize;

		}

		//Cifrar o ultimo Pacote
		doFinal(&inArr[fullPackets * MAX_DATA_IN], remainBytes, buffer_out, &returnSize);
		memcpy(&buffer_temp[totalSize], buffer_out, returnSize);
		totalSize += returnSize;

		//Cria Array de Retorno
		outArray = env->NewByteArray((jsize) totalSize);
		jsize start = (jsize) 0;
		jsize len = (jsize) totalSize;
		jbyte *buf = (jbyte*) buffer_temp;
		env->SetByteArrayRegion(outArray, start, len , buf);

	}else{

		//cipher
		doFinal(inArr, lengthOfArray, buffer_out, &returnSize);

		//Criar array de retorno
		outArray = env->NewByteArray((jsize) returnSize);
		jsize start = (jsize) 0;
		jsize len = (jsize) returnSize;
		jbyte *buf = (jbyte*) buffer_out;
		env->SetByteArrayRegion(outArray, start, len , buf);

	}

	//Limpar Bufers
	memset(&buffer_temp, 0, sizeof(buffer_temp));
	memset(&buffer_out, 0, sizeof(buffer_out));

	//release Array (memory leak problem)
	env->ReleaseByteArrayElements(array,inArr,JNI_ABORT);
	return outArray;
}