2 //gcc openssl_evp_ctr.c pixmap_io.o -o openssl_evp_ctr -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;
19 struct timeval tstart;
20 gettimeofday(&tstart,0);
21 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
24 double TimeStop(double t)
28 gettimeofday(&tend,0);
29 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
34 void handleErrors(void)
36 ERR_print_errors_fp(stderr);
41 int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
42 unsigned char *iv, unsigned char *ciphertext)
50 /* Create and initialise the context */
51 if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
53 /* Initialise the encryption operation. IMPORTANT - ensure you use a key
54 * and IV size appropriate for your cipher
55 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
56 * IV size for *most* modes is the same as the block size. For AES this
61 if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
64 // int cipherBlockSize = EVP_CIPHER_CTX_block_size(ctx);
65 // printf("INFO(evp_encrypt): block size: %d\n", cipherBlockSize);
68 /* Provide the message to be encrypted, and obtain the encrypted output.
69 * EVP_EncryptUpdate can be called multiple times if necessary
72 if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
82 /* Finalise the encryption. Further ciphertext bytes may be written at
85 if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
86 ciphertext_len += len;
89 EVP_CIPHER_CTX_free(ctx);
91 return ciphertext_len;
94 int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
95 unsigned char *iv, unsigned char *plaintext)
103 /* Create and initialise the context */
104 if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
106 /* Initialise the decryption operation. IMPORTANT - ensure you use a key
107 * and IV size appropriate for your cipher
108 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
109 * IV size for *most* modes is the same as the block size. For AES this
115 if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv))
118 /* Provide the message to be decrypted, and obtain the plaintext output.
119 * EVP_DecryptUpdate can be called multiple times if necessary
123 if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
127 /* Finalise the decryption. Further plaintext bytes may be written at
130 if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
131 plaintext_len += len;
134 EVP_CIPHER_CTX_free(ctx);
136 return plaintext_len;
140 int main (int argc, char** argv)
142 /* Set up the key and iv. Do I need to say to not hard code these in a
143 * real application? :-)
147 // unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
148 unsigned char *key = (unsigned char *)"0123456789012345";
151 unsigned char *iv = (unsigned char *)"0123456789012345";
153 /* Message to be encrypted */
159 nb_test=atoi(argv[1]);
160 if(nb_test<=0 || nb_test>10000) {
161 printf("nb tests is not correct\n");
165 printf("nb tests = %d\n\n",nb_test);
169 /* Buffer for ciphertext. Ensure the buffer is long enough for the
170 * ciphertext which may be longer than the plaintext, dependant on the
176 uchar *data_R, *data_G, *data_B;
177 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
180 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
181 // load_RGB_pixmap("4096.ppm", &width, &height, &data_R, &data_G, &data_B);
182 int size=width*height*3;
184 int oneD=width*height;
185 uchar *plaintext = malloc(size);
188 for(int i=0;i<oneD;i++) {
189 plaintext[i]=data_R[i];
190 plaintext[oneD+i]=data_G[i];
191 plaintext[2*oneD+i]=data_B[i];
196 uchar *ciphertext = malloc(size);
198 /* Buffer for the decrypted text */
199 uchar *decryptedtext = malloc(size);
201 int decryptedtext_len, ciphertext_len;
203 /* Initialise the library */
204 /* ERR_load_crypto_strings();
205 OpenSSL_add_all_algorithms();
206 OPENSSL_config(NULL);
209 double t=TimeStart();
212 /* Encrypt the plaintext */
217 for(i=0;i<nb_test;i++)
219 ciphertext_len = encrypt (plaintext, size, key, iv,
225 printf("Time encrypt %f\n",time);
228 for(int i=0;i<oneD;i++) {
229 data_R[i]=ciphertext[i];
230 data_G[i]=ciphertext[oneD+i];
231 data_B[i]=ciphertext[2*oneD+i];
233 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
240 for(int i=0;i<nb_test;i++)
242 /* Decrypt the ciphertext */
243 decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,
249 printf("Time decrypt %f\n",time);
252 for(int i=0;i<oneD;i++) {
253 data_R[i]=decryptedtext[i];
254 data_G[i]=decryptedtext[oneD+i];
255 data_B[i]=decryptedtext[2*oneD+i];
257 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);