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 printBytes(unsigned char *buf, size_t len) {
38 for(int i=0; i<len; i++) {
39 printf("%02x ", buf[i]);
45 void handleErrors(void)
47 ERR_print_errors_fp(stderr);
52 int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
53 unsigned char *iv, unsigned char *ciphertext, int ctr, int index)
61 /* Create and initialise the context */
62 if(!(ctx = CMAC_CTX_new())) handleErrors();
64 /* Initialise the encryption operation. IMPORTANT - ensure you use a key
65 * and IV size appropriate for your cipher
66 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
67 * IV size for *most* modes is the same as the block size. For AES this
69 //static double time=0;
75 if(1 != CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
79 if(1 != CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
82 unsigned char mact[16] = {0};
84 //printf("Time init %f\n",time);
87 // int cipherBlockSize = EVP_CIPHER_CTX_block_size(ctx);
88 // printf("INFO(evp_encrypt): block size: %d\n", cipherBlockSize);
91 /* Provide the message to be encrypted, and obtain the encrypted output.
92 * EVP_EncryptUpdate can be called multiple times if necessary
100 for(int i=0;i<nb_test;i++)
103 if(1 != CMAC_Update(ctx, plaintext, plaintext_len))
105 ciphertext_len = len;
108 /* time+=TimeStop(t);
109 // if(index==nb_test-1)
110 printf("Time encrypt %f\n",time);
115 /* Finalise the encryption. Further ciphertext bytes may be written at
118 if(1 != CMAC_Final(ctx, mact, &mactlen)) handleErrors();
119 ciphertext_len += len;
121 printBytes(mact, mactlen);
126 return ciphertext_len;
129 int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
130 unsigned char *iv, unsigned char *plaintext, int ctr, int index)
138 /* Create and initialise the context */
139 if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
141 /* Initialise the decryption operation. IMPORTANT - ensure you use a key
142 * and IV size appropriate for your cipher
143 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
144 * IV size for *most* modes is the same as the block size. For AES this
151 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
155 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
158 /* Provide the message to be decrypted, and obtain the plaintext output.
159 * EVP_DecryptUpdate can be called multiple times if necessary
162 /* static double time=0;
166 for(int i=0;i<nb_test;i++)
169 if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
173 /* time+=TimeStop(t);
174 // if(index==nb_test-1)
175 printf("Time decrypt %f\n",time);
179 /* Finalise the decryption. Further plaintext bytes may be written at
182 if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
183 plaintext_len += len;
188 EVP_CIPHER_CTX_free(ctx);
190 return plaintext_len;
194 int main (int argc, char** argv)
196 /* Set up the key and iv. Do I need to say to not hard code these in a
197 * real application? :-)
204 for(int i=1; i<argc; i++){
205 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
206 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
207 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
208 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
209 if(strncmp(argv[i],"c",1)==0) change = atoi(&(argv[i][1])); //Use Lena or buffer
212 /* printf("nb times %d\n",nb_test);
213 printf("ctr %d\n",ctr);
214 printf("lena %d\n",lena);
215 printf("size_buf %d\n",size_buf);
222 // unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
223 unsigned char *key = (unsigned char *)"0123456789012345";
226 unsigned char *iv = (unsigned char *)"0123456789012345";
228 /* Message to be encrypted */
230 /* Buffer for ciphertext. Ensure the buffer is long enough for the
231 * ciphertext which may be longer than the plaintext, dependant on the
237 uchar *data_R, *data_G, *data_B;
243 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
244 imsize=width*height*3;
245 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
251 buffer=malloc(imsize*sizeof(uchar));
252 for(int i=0;i<imsize;i++) {
259 int oneD=width*height;
260 uchar *plaintext = malloc(imsize+1000); //add that for cbc
262 for(int i=0;i<oneD;i++) {
263 plaintext[i]=data_R[i];
264 plaintext[oneD+i]=data_G[i];
265 plaintext[2*oneD+i]=data_B[i];
270 for(int i=0;i<oneD;i++) {
271 plaintext[i]=buffer[i];
285 uchar *ciphertext = malloc(imsize+1000); //add that for cbc
287 /* Buffer for the decrypted text */
288 uchar *decryptedtext = malloc(imsize+1000); //add that for cbc
290 int decryptedtext_len, ciphertext_len;
292 /* Initialise the library */
293 /* ERR_load_crypto_strings();
294 OpenSSL_add_all_algorithms();
295 OPENSSL_config(NULL);
299 double time_encrypt=0;
300 double time_decrypt=0;
301 double t=TimeStart();
304 /* Encrypt the plaintext */
309 // for(i=0;i<nb_test;i++)
311 ciphertext_len = encrypt (plaintext, imsize, key, iv,
312 ciphertext, ctr, i );
315 time_encrypt+=TimeStop(t);
317 // printf("Time encrypt %f\n",time);
318 printf("%f\t",(double)imsize*nb_test/time_encrypt);
321 for(int i=0;i<oneD;i++) {
322 data_R[i]=ciphertext[i];
323 data_G[i]=ciphertext[oneD+i];
324 data_B[i]=ciphertext[2*oneD+i];
326 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
333 //for(int i=0;i<nb_test;i++)
335 // Decrypt the ciphertext
336 decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
337 decryptedtext,ctr, i);
340 time_decrypt+=TimeStop(t);
342 //printf("Time decrypt %f\n",time);
343 printf("%f\t",(double)imsize*nb_test/time_decrypt);
346 for(int i=0;i<oneD;i++) {
347 data_R[i]=decryptedtext[i];
348 data_G[i]=decryptedtext[oneD+i];
349 data_B[i]=decryptedtext[2*oneD+i];
351 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
355 for(int i=0;i<imsize;i++) {
356 //cout<<(int)buffer[i]<<endl;
357 if(buffer[i]!=decryptedtext[i]) {
361 // printf("RESULT CORRECT: %d\n",equal);