summaryrefslogtreecommitdiff
path: root/benchmarks/CUDA/AES/aescuda.cpp
blob: 4b1f1b4620c880a4a9fc287d78752e805b25861c (plain)
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

/***************************************************************************
 *   Copyright (C) 2006                                                    *
 *                                                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


/**
	@author Svetlin Manavski <[email protected]>
 */

#include <iostream>
#include "aesCudaUtils.h"
#include "utilsBox.h"
#include <string.h>

using namespace std;

const int INPUTSIZE = 33*1024*1024;
bool MODE = 1;

//handler
extern "C"
int aesHost(unsigned char* result, const unsigned char* inData, int inputSize, const unsigned char* key, int keySize, bool toEncrypt);

int main(int argc, char *argv[])
{	
	try {
		unsigned numPairs = commandLineManager(argc, argv);

		unsigned aesType = static_cast<unsigned>(atoi(argv[2]));

		unsigned ekSize=240;
		if (aesType != 256)
			ekSize = 176;

		//per ogni coppia di file di input e di chiave svolge l'operazione
		for (unsigned cnt=0; cnt<numPairs; ++cnt) {

			char *h_Input = new char[INPUTSIZE];
			if ( !h_Input )
				throw string("cannot allocate memory for the input\n");	
			memset(h_Input, 0, INPUTSIZE);
	
			unsigned char myExpKey[ekSize];
			memset(myExpKey, 0, ekSize);
			
			//funzione di inizializzazione che legge i file di input ed espande le chiavi.
			unsigned usefulData = initAesCuda(argv[(cnt*2)+4], myExpKey, aesType, argv[(cnt*2)+3], h_Input, INPUTSIZE);
	
			// memory for the result
			unsigned char *h_Result = new unsigned char[INPUTSIZE];
			if ( !h_Result )
				throw string("cannot allocate memory for the result\n");	
	
			//la seguente operazione ha il seguente significato: poichè l'input viene scomposto in 256 blocchi di unsigned e cioè 1024 bytes, bisogna assicurarsi che la dimensione dell'input sia sempre multiplo di tale valore e cioè 1024 appunto.
			unsigned modUsefulData = ( usefulData - ( usefulData % 1024 ) + 1024 );

			//chiamata all'handler
			int errorReturned = aesHost(h_Result, reinterpret_cast<unsigned char*>(h_Input), modUsefulData, myExpKey, sizeof(myExpKey), MODE);
	
			if ( errorReturned == -1 )
				throw string("aesHost: cannot use an input size minor of 256\n");
	
			if ( errorReturned == -11 )
				throw string("aesHost: cannot use an input size not multiple of 256\n");
	
			if ( errorReturned == -2 )
				throw string("aesHost: cannot use an expanded key size different from 240 or 176\n");
	
			if ( errorReturned == -3 )
				throw string("aesHost: some input not allocated\n");
	
			cout << "\n###############################################################\n\n";

			char numFile[10000];
			sprintf(numFile, "%d", cnt);
			string nameOutFile("output_");
			nameOutFile.append(numFile);
			nameOutFile.append(".dat");

			//scrittura risultati in uscita
			writeToFile(nameOutFile, reinterpret_cast<char *>(h_Result), usefulData, INPUTSIZE);
	
			delete[] h_Result;
			delete[] h_Input;
		}

	} catch(string ex){
		cout << "Exception occurred: " << ex << endl;
	}

    return EXIT_SUCCESS;
}