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
64 if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
68 if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
71 // int cipherBlockSize = EVP_CIPHER_CTX_block_size(ctx);
72 // printf("INFO(evp_encrypt): block size: %d\n", cipherBlockSize);
75 /* Provide the message to be encrypted, and obtain the encrypted output.
76 * EVP_EncryptUpdate can be called multiple times if necessary
84 for(int i=0;i<nb_test;i++)
88 if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
94 // if(index==nb_test-1)
95 printf("Time encrypt %f\n",time);
100 /* Finalise the encryption. Further ciphertext bytes may be written at
103 if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
104 ciphertext_len += len;
107 EVP_CIPHER_CTX_free(ctx);
109 return ciphertext_len;
112 int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
113 unsigned char *iv, unsigned char *plaintext, int ctr, int index)
121 /* Create and initialise the context */
122 if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
124 /* Initialise the decryption operation. IMPORTANT - ensure you use a key
125 * and IV size appropriate for your cipher
126 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
127 * IV size for *most* modes is the same as the block size. For AES this
134 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
138 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
141 /* Provide the message to be decrypted, and obtain the plaintext output.
142 * EVP_DecryptUpdate can be called multiple times if necessary
145 /* static double time=0;
149 for(int i=0;i<nb_test;i++)
152 if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
156 /* time+=TimeStop(t);
157 // if(index==nb_test-1)
158 printf("Time decrypt %f\n",time);
162 /* Finalise the decryption. Further plaintext bytes may be written at
165 if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
166 plaintext_len += len;
171 EVP_CIPHER_CTX_free(ctx);
173 return plaintext_len;
177 int main (int argc, char** argv)
179 /* Set up the key and iv. Do I need to say to not hard code these in a
180 * real application? :-)
187 for(int i=1; i<argc; i++){
188 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
189 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
190 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
191 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
194 /* printf("nb times %d\n",nb_test);
195 printf("ctr %d\n",ctr);
196 printf("lena %d\n",lena);
197 printf("size_buf %d\n",size_buf);
204 // unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
205 unsigned char *key = (unsigned char *)"0123456789012345";
208 unsigned char *iv = (unsigned char *)"0123456789012345";
210 /* Message to be encrypted */
212 /* Buffer for ciphertext. Ensure the buffer is long enough for the
213 * ciphertext which may be longer than the plaintext, dependant on the
219 uchar *data_R, *data_G, *data_B;
225 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
226 imsize=width*height*3;
227 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
230 width=height=size_buf;
232 buffer=malloc(imsize*sizeof(uchar));
233 for(int i=0;i<imsize;i++) {
240 int oneD=width*height;
241 uchar *plaintext = malloc(imsize);
243 for(int i=0;i<oneD;i++) {
244 plaintext[i]=data_R[i];
245 plaintext[oneD+i]=data_G[i];
246 plaintext[2*oneD+i]=data_B[i];
251 for(int i=0;i<oneD;i++) {
252 plaintext[i]=buffer[i];
258 uchar *ciphertext = malloc(imsize);
260 /* Buffer for the decrypted text */
261 uchar *decryptedtext = malloc(imsize);
263 int decryptedtext_len, ciphertext_len;
265 /* Initialise the library */
266 /* ERR_load_crypto_strings();
267 OpenSSL_add_all_algorithms();
268 OPENSSL_config(NULL);
273 double t=TimeStart();
276 /* Encrypt the plaintext */
281 // for(i=0;i<nb_test;i++)
283 ciphertext_len = encrypt (plaintext, imsize, key, iv,
284 ciphertext, ctr, i );
289 printf("Time encrypt %f\n",time);
292 for(int i=0;i<oneD;i++) {
293 data_R[i]=ciphertext[i];
294 data_G[i]=ciphertext[oneD+i];
295 data_B[i]=ciphertext[2*oneD+i];
297 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
305 //for(int i=0;i<nb_test;i++)
307 /* Decrypt the ciphertext */
308 decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
309 decryptedtext,ctr, i);
314 printf("Time decrypt %f\n",time);
317 for(int i=0;i<oneD;i++) {
318 data_R[i]=decryptedtext[i];
319 data_G[i]=decryptedtext[oneD+i];
320 data_B[i]=decryptedtext[2*oneD+i];
322 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
326 for(int i=0;i<imsize;i++) {
327 //cout<<(int)buffer[i]<<endl;
328 if(buffer[i]!=decryptedtext[i]) {
332 // printf("RESULT CORRECT: %d\n",equal);