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))
86 //unsigned char mact[16] = {0};
88 unsigned char* result;
89 unsigned int result_len = 32;
92 unsigned char hash_sha256[SHA256_DIGEST_LENGTH];
93 char hash_sha256_hex[SHA256_DIGEST_LENGTH * 2 + 1];
95 memset( hash_sha256_hex, 0, SHA256_DIGEST_LENGTH * 2 + 1);
98 //printf("Time init %f\n",time);
101 // int cipherBlockSize = EVP_CIPHER_CTX_block_size(ctx);
102 // printf("INFO(evp_encrypt): block size: %d\n", cipherBlockSize);
105 /* Provide the message to be encrypted, and obtain the encrypted output.
106 * EVP_EncryptUpdate can be called multiple times if necessary
110 static double time=0;
114 for(int i=0;i<nb_test;i++)
117 if(1 != HMAC_Update(ctx, plaintext, plaintext_len))
119 ciphertext_len = len;
122 /* time+=TimeStop(t);
123 // if(index==nb_test-1)
124 printf("Time encrypt %f\n",time);
129 /* Finalise the encryption. Further ciphertext bytes may be written at
132 if(1 != HMAC_Final(ctx, hash_sha256, &result_len)) handleErrors();
135 ciphertext_len += len;
137 // printBytes(hash_sha256, result_len);
140 char *p = &hash_sha256_hex[0];
141 unsigned char *h = &hash_sha256[0];
142 for (int i = 0; i < result_len; ++i, ++h)
144 p += snprintf(p, 3, "%02x", *h);
147 printf("SHA256\t: %s\n", hash_sha256_hex);
153 return ciphertext_len;
159 int main (int argc, char** argv)
161 /* Set up the key and iv. Do I need to say to not hard code these in a
162 * real application? :-)
169 for(int i=1; i<argc; i++){
170 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
171 if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
172 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
173 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
174 if(strncmp(argv[i],"c",1)==0) change = atoi(&(argv[i][1])); //Use Lena or buffer
177 /* printf("nb times %d\n",nb_test);
178 printf("ctr %d\n",ctr);
179 printf("lena %d\n",lena);
180 printf("size_buf %d\n",size_buf);
187 unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
188 // unsigned char *key = (unsigned char *)"0123456789012345";
191 unsigned char *iv = (unsigned char *)"0123456789012345";
193 /* Message to be encrypted */
195 /* Buffer for ciphertext. Ensure the buffer is long enough for the
196 * ciphertext which may be longer than the plaintext, dependant on the
202 uchar *data_R, *data_G, *data_B;
208 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
209 imsize=width*height*3;
210 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
216 buffer=malloc(imsize*sizeof(uchar));
217 for(int i=0;i<imsize;i++) {
224 int oneD=width*height;
225 uchar *plaintext = malloc(imsize+1000); //add that for cbc
227 for(int i=0;i<oneD;i++) {
228 plaintext[i]=data_R[i];
229 plaintext[oneD+i]=data_G[i];
230 plaintext[2*oneD+i]=data_B[i];
235 for(int i=0;i<oneD;i++) {
236 plaintext[i]=buffer[i];
250 uchar *ciphertext = malloc(imsize+1000); //add that for cbc
252 /* Buffer for the decrypted text */
253 uchar *decryptedtext = malloc(imsize+1000); //add that for cbc
255 int decryptedtext_len, ciphertext_len;
257 /* Initialise the library */
258 /* ERR_load_crypto_strings();
259 OpenSSL_add_all_algorithms();
260 OPENSSL_config(NULL);
264 double time_encrypt=0;
265 double time_decrypt=0;
266 double t=TimeStart();
269 /* Encrypt the plaintext */
274 // for(i=0;i<nb_test;i++)
276 ciphertext_len = encrypt (plaintext, imsize, key, iv,
277 ciphertext, ctr, i );
280 time_encrypt+=TimeStop(t);
282 // printf("Time encrypt %f\n",time);
283 printf("%e\t",(double)imsize*nb_test/time_encrypt);
287 for(int i=0;i<oneD;i++) {
288 data_R[i]=ciphertext[i];
289 data_G[i]=ciphertext[oneD+i];
290 data_B[i]=ciphertext[2*oneD+i];
292 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
299 //for(int i=0;i<nb_test;i++)
301 // Decrypt the ciphertext
302 decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
303 decryptedtext,ctr, i);
306 time_decrypt+=TimeStop(t);
308 //printf("Time decrypt %f\n",time);
309 printf("%f\t",(double)imsize*nb_test/time_decrypt);
312 for(int i=0;i<oneD;i++) {
313 data_R[i]=decryptedtext[i];
314 data_G[i]=decryptedtext[oneD+i];
315 data_B[i]=decryptedtext[2*oneD+i];
317 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
321 for(int i=0;i<imsize;i++) {
322 //cout<<(int)buffer[i]<<endl;
323 if(buffer[i]!=decryptedtext[i]) {
327 // printf("RESULT CORRECT: %d\n",equal);