2 //gcc openssl_evp.c pixmap_io.o -o openssl_evp -I /usr/include/openssl/ -lcrypto -O3 -std=c99
5 #include <openssl/conf.h>
6 #include <openssl/evp.h>
7 #include <openssl/err.h>
8 #include <openssl/ssl.h>
9 #include <openssl/bio.h>
10 #include <openssl/cmac.h>
13 #include "pixmap_io.h"
15 typedef unsigned char uchar;
22 struct timeval tstart;
23 gettimeofday(&tstart,0);
24 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
27 double TimeStop(double t)
31 gettimeofday(&tend,0);
32 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
37 void handleErrors(void)
39 ERR_print_errors_fp(stderr);
44 int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
45 unsigned char *iv, unsigned char *ciphertext, int ctr, int index)
53 /* Create and initialise the context */
54 if(!(ctx = CMAC_CTX_new())) handleErrors();
56 /* Initialise the encryption operation. IMPORTANT - ensure you use a key
57 * and IV size appropriate for your cipher
58 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
59 * IV size for *most* modes is the same as the block size. For AES this
61 //static double time=0;
67 if(1 != CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
71 if(1 != CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
74 unsigned char mact[16] = {0};
76 //printf("Time init %f\n",time);
79 // int cipherBlockSize = EVP_CIPHER_CTX_block_size(ctx);
80 // printf("INFO(evp_encrypt): block size: %d\n", cipherBlockSize);
83 /* Provide the message to be encrypted, and obtain the encrypted output.
84 * EVP_EncryptUpdate can be called multiple times if necessary
92 for(int i=0;i<nb_test;i++)
95 if(1 != CMAC_Update(ctx, plaintext, plaintext_len))
100 /* time+=TimeStop(t);
101 // if(index==nb_test-1)
102 printf("Time encrypt %f\n",time);
107 /* Finalise the encryption. Further ciphertext bytes may be written at
110 if(1 != CMAC_Final(ctx, mact, &mactlen)) handleErrors();
111 ciphertext_len += len;
116 return ciphertext_len;
119 int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
120 unsigned char *iv, unsigned char *plaintext, int ctr, int index)
128 /* Create and initialise the context */
129 if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
131 /* Initialise the decryption operation. IMPORTANT - ensure you use a key
132 * and IV size appropriate for your cipher
133 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
134 * IV size for *most* modes is the same as the block size. For AES this
141 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
145 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
148 /* Provide the message to be decrypted, and obtain the plaintext output.
149 * EVP_DecryptUpdate can be called multiple times if necessary
152 /* static double time=0;
156 for(int i=0;i<nb_test;i++)
159 if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
163 /* time+=TimeStop(t);
164 // if(index==nb_test-1)
165 printf("Time decrypt %f\n",time);
169 /* Finalise the decryption. Further plaintext bytes may be written at
172 if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
173 plaintext_len += len;
178 EVP_CIPHER_CTX_free(ctx);
180 return plaintext_len;
184 int main (int argc, char** argv)
186 /* Set up the key and iv. Do I need to say to not hard code these in a
187 * real application? :-)
194 for(int i=1; i<argc; i++){
195 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
196 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
197 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
198 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
201 /* printf("nb times %d\n",nb_test);
202 printf("ctr %d\n",ctr);
203 printf("lena %d\n",lena);
204 printf("size_buf %d\n",size_buf);
211 // unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
212 unsigned char *key = (unsigned char *)"0123456789012345";
215 unsigned char *iv = (unsigned char *)"0123456789012345";
217 /* Message to be encrypted */
219 /* Buffer for ciphertext. Ensure the buffer is long enough for the
220 * ciphertext which may be longer than the plaintext, dependant on the
226 uchar *data_R, *data_G, *data_B;
232 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
233 imsize=width*height*3;
234 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
240 buffer=malloc(imsize*sizeof(uchar));
241 for(int i=0;i<imsize;i++) {
248 int oneD=width*height;
249 uchar *plaintext = malloc(imsize+1000); //add that for cbc
251 for(int i=0;i<oneD;i++) {
252 plaintext[i]=data_R[i];
253 plaintext[oneD+i]=data_G[i];
254 plaintext[2*oneD+i]=data_B[i];
259 for(int i=0;i<oneD;i++) {
260 plaintext[i]=buffer[i];
266 uchar *ciphertext = malloc(imsize+1000); //add that for cbc
268 /* Buffer for the decrypted text */
269 uchar *decryptedtext = malloc(imsize+1000); //add that for cbc
271 int decryptedtext_len, ciphertext_len;
273 /* Initialise the library */
274 /* ERR_load_crypto_strings();
275 OpenSSL_add_all_algorithms();
276 OPENSSL_config(NULL);
280 double time_encrypt=0;
281 double time_decrypt=0;
282 double t=TimeStart();
285 /* Encrypt the plaintext */
290 // for(i=0;i<nb_test;i++)
292 ciphertext_len = encrypt (plaintext, imsize, key, iv,
293 ciphertext, ctr, i );
296 time_encrypt+=TimeStop(t);
298 // printf("Time encrypt %f\n",time);
299 printf("%f\t",(double)imsize*nb_test/time_encrypt);
302 for(int i=0;i<oneD;i++) {
303 data_R[i]=ciphertext[i];
304 data_G[i]=ciphertext[oneD+i];
305 data_B[i]=ciphertext[2*oneD+i];
307 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
314 //for(int i=0;i<nb_test;i++)
316 // Decrypt the ciphertext
317 decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
318 decryptedtext,ctr, i);
321 time_decrypt+=TimeStop(t);
323 //printf("Time decrypt %f\n",time);
324 printf("%f\t",(double)imsize*nb_test/time_decrypt);
327 for(int i=0;i<oneD;i++) {
328 data_R[i]=decryptedtext[i];
329 data_G[i]=decryptedtext[oneD+i];
330 data_B[i]=decryptedtext[2*oneD+i];
332 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
336 for(int i=0;i<imsize;i++) {
337 //cout<<(int)buffer[i]<<endl;
338 if(buffer[i]!=decryptedtext[i]) {
342 // printf("RESULT CORRECT: %d\n",equal);