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>
12 #include "pixmap_io.h"
14 typedef unsigned char uchar;
21 struct timeval tstart;
22 gettimeofday(&tstart,0);
23 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
26 double TimeStop(double t)
30 gettimeofday(&tend,0);
31 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
36 void handleErrors(void)
38 ERR_print_errors_fp(stderr);
43 int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
44 unsigned char *iv, unsigned char *ciphertext, int ctr, int index)
52 /* Create and initialise the context */
53 if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
55 /* Initialise the encryption operation. IMPORTANT - ensure you use a key
56 * and IV size appropriate for your cipher
57 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
58 * IV size for *most* modes is the same as the block size. For AES this
60 //static double time=0;
65 for(int i=0;i<nb_test;i++)
69 if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
73 if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
77 //printf("Time init %f\n",time);
80 // int cipherBlockSize = EVP_CIPHER_CTX_block_size(ctx);
81 // printf("INFO(evp_encrypt): block size: %d\n", cipherBlockSize);
84 /* Provide the message to be encrypted, and obtain the encrypted output.
85 * EVP_EncryptUpdate can be called multiple times if necessary
94 if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
100 // if(index==nb_test-1)
101 printf("Time encrypt %f\n",time);
106 /* Finalise the encryption. Further ciphertext bytes may be written at
109 if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
110 ciphertext_len += len;
113 EVP_CIPHER_CTX_free(ctx);
115 return ciphertext_len;
118 int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
119 unsigned char *iv, unsigned char *plaintext, int ctr, int index)
127 /* Create and initialise the context */
128 if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
130 /* Initialise the decryption operation. IMPORTANT - ensure you use a key
131 * and IV size appropriate for your cipher
132 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
133 * IV size for *most* modes is the same as the block size. For AES this
140 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
144 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
147 /* Provide the message to be decrypted, and obtain the plaintext output.
148 * EVP_DecryptUpdate can be called multiple times if necessary
151 /* static double time=0;
155 for(int i=0;i<nb_test;i++)
158 if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
162 /* time+=TimeStop(t);
163 // if(index==nb_test-1)
164 printf("Time decrypt %f\n",time);
168 /* Finalise the decryption. Further plaintext bytes may be written at
171 if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
172 plaintext_len += len;
177 EVP_CIPHER_CTX_free(ctx);
179 return plaintext_len;
183 int main (int argc, char** argv)
185 /* Set up the key and iv. Do I need to say to not hard code these in a
186 * real application? :-)
193 for(int i=1; i<argc; i++){
194 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
195 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
196 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
197 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
200 /* printf("nb times %d\n",nb_test);
201 printf("ctr %d\n",ctr);
202 printf("lena %d\n",lena);
203 printf("size_buf %d\n",size_buf);
210 // unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
211 unsigned char *key = (unsigned char *)"0123456789012345";
214 unsigned char *iv = (unsigned char *)"0123456789012345";
216 /* Message to be encrypted */
218 /* Buffer for ciphertext. Ensure the buffer is long enough for the
219 * ciphertext which may be longer than the plaintext, dependant on the
225 uchar *data_R, *data_G, *data_B;
231 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
232 imsize=width*height*3;
233 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
239 buffer=malloc(imsize*sizeof(uchar));
240 for(int i=0;i<imsize;i++) {
247 int oneD=width*height;
248 uchar *plaintext = malloc(imsize+1000); //add that for cbc
250 for(int i=0;i<oneD;i++) {
251 plaintext[i]=data_R[i];
252 plaintext[oneD+i]=data_G[i];
253 plaintext[2*oneD+i]=data_B[i];
258 for(int i=0;i<oneD;i++) {
259 plaintext[i]=buffer[i];
265 uchar *ciphertext = malloc(imsize+1000); //add that for cbc
267 /* Buffer for the decrypted text */
268 uchar *decryptedtext = malloc(imsize+1000); //add that for cbc
270 int decryptedtext_len, ciphertext_len;
272 /* Initialise the library */
273 /* ERR_load_crypto_strings();
274 OpenSSL_add_all_algorithms();
275 OPENSSL_config(NULL);
279 double time_encrypt=0;
280 double time_decrypt=0;
281 double t=TimeStart();
284 /* Encrypt the plaintext */
289 // for(i=0;i<nb_test;i++)
291 ciphertext_len = encrypt (plaintext, imsize, key, iv,
292 ciphertext, ctr, i );
295 time_encrypt+=TimeStop(t);
297 // printf("Time encrypt %f\n",time);
298 printf("%f\t",(double)imsize*nb_test/time_encrypt);
301 for(int i=0;i<oneD;i++) {
302 data_R[i]=ciphertext[i];
303 data_G[i]=ciphertext[oneD+i];
304 data_B[i]=ciphertext[2*oneD+i];
306 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
313 //for(int i=0;i<nb_test;i++)
315 /* Decrypt the ciphertext */
316 decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
317 decryptedtext,ctr, i);
320 time_decrypt+=TimeStop(t);
322 //printf("Time decrypt %f\n",time);
323 printf("%f\t",(double)imsize*nb_test/time_decrypt);
326 for(int i=0;i<oneD;i++) {
327 data_R[i]=decryptedtext[i];
328 data_G[i]=decryptedtext[oneD+i];
329 data_B[i]=decryptedtext[2*oneD+i];
331 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
335 for(int i=0;i<imsize;i++) {
336 //cout<<(int)buffer[i]<<endl;
337 if(buffer[i]!=decryptedtext[i]) {
341 // printf("RESULT CORRECT: %d\n",equal);