int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
- unsigned char *iv, unsigned char *ciphertext, int ctr)
+ unsigned char *iv, unsigned char *ciphertext, int ctr, int index)
{
EVP_CIPHER_CTX *ctx;
//256
//avant ecb
- if(ctr)
+ if(ctr) {
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
handleErrors();
- else
+ }
+ else
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
handleErrors();
* EVP_EncryptUpdate can be called multiple times if necessary
*/
- if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
- handleErrors();
- ciphertext_len = len;
-
-
+ static double time=0;
+ double t=0;
+ t=TimeStart();
+// for(int i=0;i<nb_test;i++)
+ {
+
+
+ if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
+ handleErrors();
+ ciphertext_len = len;
+
+ }
+ time+=TimeStop(t);
+ if(index==nb_test-1)
+ printf("Time encrypt %f\n",time);
+
}
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
- unsigned char *iv, unsigned char *plaintext, int ctr)
+ unsigned char *iv, unsigned char *plaintext, int ctr, int index)
{
EVP_CIPHER_CTX *ctx;
//256
//avant => ecb
- if(ctr)
+ if(ctr) {
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
handleErrors();
- else
+ }
+ else
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
handleErrors();
* EVP_DecryptUpdate can be called multiple times if necessary
*/
-
- if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
- handleErrors();
- plaintext_len = len;
+ static double time=0;
+ double t=0;
+ t=TimeStart();
+// for(int i=0;i<nb_test;i++)
+ {
+ if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
+ handleErrors();
+ plaintext_len = len;
+ }
+
+ time+=TimeStop(t);
+ if(index==nb_test-1)
+ printf("Time decrypt %f\n",time);
+
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
* real application? :-)
*/
- for(int i=1; i<argc; i++){
+ int size_buf=1;
+ int lena=0;
+
+
+ for(int i=1; i<argc; i++){
if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
+ if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
+ if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
}
- printf("nb times %d\n",nb_test);
+/* printf("nb times %d\n",nb_test);
printf("ctr %d\n",ctr);
+ printf("lena %d\n",lena);
+ printf("size_buf %d\n",size_buf);
+*/
+
-
/* A 256 bit key */
int width;
int height;
uchar *data_R, *data_G, *data_B;
- load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
+ int imsize;
+ uchar *buffer;
+ if(lena==1) {
+ load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
+ imsize=width*height*3;
// load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
-// load_RGB_pixmap("4096.ppm", &width, &height, &data_R, &data_G, &data_B);
- int size=width*height*3;
+ }
+ else {
+ width=height=size_buf;
+ imsize=width*height;
+ buffer=malloc(imsize*sizeof(uchar));
+ for(int i=0;i<imsize;i++) {
+ buffer[i]=lrand48();
+ }
+ }
+
- int oneD=width*height;
- uchar *plaintext = malloc(size);
-
- for(int i=0;i<oneD;i++) {
- plaintext[i]=data_R[i];
- plaintext[oneD+i]=data_G[i];
- plaintext[2*oneD+i]=data_B[i];
+ int oneD=width*height;
+ uchar *plaintext = malloc(imsize);
+ if(lena) {
+ for(int i=0;i<oneD;i++) {
+ plaintext[i]=data_R[i];
+ plaintext[oneD+i]=data_G[i];
+ plaintext[2*oneD+i]=data_B[i];
+ }
+ }
+ else
+ {
+ for(int i=0;i<oneD;i++) {
+ plaintext[i]=buffer[i];
+ }
}
- uchar *ciphertext = malloc(size);
+ uchar *ciphertext = malloc(imsize);
/* Buffer for the decrypted text */
- uchar *decryptedtext = malloc(size);
+ uchar *decryptedtext = malloc(imsize);
int decryptedtext_len, ciphertext_len;
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
*/
- double time=0;
+
+
+ double time=0;
double t=TimeStart();
for(i=0;i<nb_test;i++)
{
- ciphertext_len = encrypt (plaintext, size, key, iv,
- ciphertext, ctr);
+ ciphertext_len = encrypt (plaintext, imsize, key, iv,
+ ciphertext, ctr, i );
}
time+=TimeStop(t);
printf("Time encrypt %f\n",time);
-
- for(int i=0;i<oneD;i++) {
- data_R[i]=ciphertext[i];
- data_G[i]=ciphertext[oneD+i];
- data_B[i]=ciphertext[2*oneD+i];
- }
- store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
+ if(lena) {
+ for(int i=0;i<oneD;i++) {
+ data_R[i]=ciphertext[i];
+ data_G[i]=ciphertext[oneD+i];
+ data_B[i]=ciphertext[2*oneD+i];
+ }
+ store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
+ }
time=0;
{
/* Decrypt the ciphertext */
decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
- decryptedtext,ctr);
+ decryptedtext,ctr, i);
}
time+=TimeStop(t);
printf("Time decrypt %f\n",time);
-
- for(int i=0;i<oneD;i++) {
- data_R[i]=decryptedtext[i];
- data_G[i]=decryptedtext[oneD+i];
- data_B[i]=decryptedtext[2*oneD+i];
- }
- store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
-
+ if(lena) {
+ for(int i=0;i<oneD;i++) {
+ data_R[i]=decryptedtext[i];
+ data_G[i]=decryptedtext[oneD+i];
+ data_B[i]=decryptedtext[2*oneD+i];
+ }
+ store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
+ }
+ else {
+ int equal=1;
+ for(int i=0;i<imsize;i++) {
+ //cout<<(int)buffer[i]<<endl;
+ if(buffer[i]!=decryptedtext[i]) {
+ equal=0;
+ }
+ }
+ printf("RESULT CORRECT: %d\n",equal);
+ }
/* Clean up */