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>
11 #include <openssl/hmac.h>
14 #include "pixmap_io.h"
16 typedef unsigned char uchar;
23 struct timeval tstart;
24 gettimeofday(&tstart,0);
25 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
28 double TimeStop(double t)
32 gettimeofday(&tend,0);
33 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
38 void printBytes(unsigned char *buf, size_t len) {
39 for(int i=0; i<len; i++) {
40 printf("%02x ", buf[i]);
46 void handleErrors(void)
48 ERR_print_errors_fp(stderr);
53 int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
54 unsigned char *iv, unsigned char *ciphertext, int ctr, int index)
63 // ENGINE_load_builtin_engines();
64 //ENGINE_register_all_complete();
67 /* Create and initialise the context */
68 if(!(ctx = HMAC_CTX_new())) handleErrors();
70 /* Initialise the encryption operation. IMPORTANT - ensure you use a key
71 * and IV size appropriate for your cipher
72 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
73 * IV size for *most* modes is the same as the block size. For AES this
75 //static double time=0;
80 if(1 != HMAC_Init_ex(ctx, key, 32, EVP_sha256(), NULL))
83 //unsigned char mact[16] = {0};
85 unsigned char* result;
86 unsigned int result_len = 32;
89 unsigned char hash_sha256[SHA256_DIGEST_LENGTH];
90 char hash_sha256_hex[SHA256_DIGEST_LENGTH * 2 + 1];
92 memset( hash_sha256_hex, 0, SHA256_DIGEST_LENGTH * 2 + 1);
95 //printf("Time init %f\n",time);
98 // int cipherBlockSize = EVP_CIPHER_CTX_block_size(ctx);
99 // printf("INFO(evp_encrypt): block size: %d\n", cipherBlockSize);
102 /* Provide the message to be encrypted, and obtain the encrypted output.
103 * EVP_EncryptUpdate can be called multiple times if necessary
107 static double time=0;
111 for(int i=0;i<nb_test;i++)
114 if(1 != HMAC_Update(ctx, plaintext, plaintext_len))
116 ciphertext_len = len;
119 /* time+=TimeStop(t);
120 // if(index==nb_test-1)
121 printf("Time encrypt %f\n",time);
126 /* Finalise the encryption. Further ciphertext bytes may be written at
129 if(1 != HMAC_Final(ctx, hash_sha256, &result_len)) handleErrors();
132 ciphertext_len += len;
134 printBytes(hash_sha256, result_len);
137 char *p = &hash_sha256_hex[0];
138 unsigned char *h = &hash_sha256[0];
139 for (int i = 0; i < result_len; ++i, ++h)
141 p += snprintf(p, 3, "%02x", *h);
144 printf("SHA256\t: %s\n", hash_sha256_hex);
150 return ciphertext_len;
156 int main (int argc, char** argv)
158 /* Set up the key and iv. Do I need to say to not hard code these in a
159 * real application? :-)
166 for(int i=1; i<argc; i++){
167 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
168 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
169 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
170 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
171 if(strncmp(argv[i],"c",1)==0) change = atoi(&(argv[i][1])); //Use Lena or buffer
174 /* printf("nb times %d\n",nb_test);
175 printf("ctr %d\n",ctr);
176 printf("lena %d\n",lena);
177 printf("size_buf %d\n",size_buf);
184 unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
185 // unsigned char *key = (unsigned char *)"0123456789012345";
188 unsigned char *iv = (unsigned char *)"0123456789012345";
190 /* Message to be encrypted */
192 /* Buffer for ciphertext. Ensure the buffer is long enough for the
193 * ciphertext which may be longer than the plaintext, dependant on the
199 uchar *data_R, *data_G, *data_B;
205 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
206 imsize=width*height*3;
207 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
213 buffer=malloc(imsize*sizeof(uchar));
214 for(int i=0;i<imsize;i++) {
221 int oneD=width*height;
222 uchar *plaintext = malloc(imsize+1000); //add that for cbc
224 for(int i=0;i<oneD;i++) {
225 plaintext[i]=data_R[i];
226 plaintext[oneD+i]=data_G[i];
227 plaintext[2*oneD+i]=data_B[i];
232 for(int i=0;i<oneD;i++) {
233 plaintext[i]=buffer[i];
247 uchar *ciphertext = malloc(imsize+1000); //add that for cbc
249 /* Buffer for the decrypted text */
250 uchar *decryptedtext = malloc(imsize+1000); //add that for cbc
252 int decryptedtext_len, ciphertext_len;
254 /* Initialise the library */
255 /* ERR_load_crypto_strings();
256 OpenSSL_add_all_algorithms();
257 OPENSSL_config(NULL);
261 double time_encrypt=0;
262 double time_decrypt=0;
263 double t=TimeStart();
266 /* Encrypt the plaintext */
271 // for(i=0;i<nb_test;i++)
273 ciphertext_len = encrypt (plaintext, imsize, key, iv,
274 ciphertext, ctr, i );
277 time_encrypt+=TimeStop(t);
279 // printf("Time encrypt %f\n",time);
280 printf("%e\t",(double)imsize*nb_test/time_encrypt);
284 for(int i=0;i<oneD;i++) {
285 data_R[i]=ciphertext[i];
286 data_G[i]=ciphertext[oneD+i];
287 data_B[i]=ciphertext[2*oneD+i];
289 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
296 //for(int i=0;i<nb_test;i++)
298 // Decrypt the ciphertext
299 decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
300 decryptedtext,ctr, i);
303 time_decrypt+=TimeStop(t);
305 //printf("Time decrypt %f\n",time);
306 printf("%f\t",(double)imsize*nb_test/time_decrypt);
309 for(int i=0;i<oneD;i++) {
310 data_R[i]=decryptedtext[i];
311 data_G[i]=decryptedtext[oneD+i];
312 data_B[i]=decryptedtext[2*oneD+i];
314 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
318 for(int i=0;i<imsize;i++) {
319 //cout<<(int)buffer[i]<<endl;
320 if(buffer[i]!=decryptedtext[i]) {
324 // printf("RESULT CORRECT: %d\n",equal);