--- /dev/null
+//gcc pixmap_io.c -c
+//gcc openssl_evp.c pixmap_io.o -o openssl_evp -I /usr/include/openssl/ -lcrypto -O3 -std=c99
+
+
+#include <openssl/conf.h>
+#include <openssl/evp.h>
+#include <openssl/err.h>
+#include <openssl/ssl.h>
+#include <openssl/bio.h>
+#include <string.h>
+#include <sys/time.h>
+#include "pixmap_io.h"
+
+typedef unsigned char uchar;
+
+int nb_test=1;
+int ctr=0;
+
+
+
+
+
+double TimeStart()
+{
+ struct timeval tstart;
+ gettimeofday(&tstart,0);
+ return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
+}
+
+double TimeStop(double t)
+{
+ struct timeval tend;
+
+ gettimeofday(&tend,0);
+ t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
+ return (t);
+}
+
+
+void handleErrors(void)
+{
+ ERR_print_errors_fp(stderr);
+ abort();
+}
+
+
+int encryptccm(unsigned char *plaintext, int plaintext_len, unsigned char *aad,
+ int aad_len, unsigned char *key, unsigned char *iv,
+ unsigned char *ciphertext, unsigned char *tag)
+{
+ EVP_CIPHER_CTX *ctx;
+
+ int len;
+
+ int ciphertext_len;
+
+
+ /* Create and initialise the context */
+ if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
+
+ /* Initialise the encryption operation. */
+ if(1 != EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL, NULL))
+ handleErrors();
+
+ /* Setting IV len to 7. Not strictly necessary as this is the default
+ * but shown here for the purposes of this example */
+ if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, 7, NULL))
+ handleErrors();
+
+ /* Set tag length */
+ EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL);
+
+ /* Initialise key and IV */
+ if(1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) handleErrors();
+
+ /* Provide the total plaintext length
+ */
+ if(1 != EVP_EncryptUpdate(ctx, NULL, &len, NULL, plaintext_len))
+ handleErrors();
+
+ /* Provide any AAD data. This can be called zero or one times as
+ * required
+ */
+ if(1 != EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_len))
+ handleErrors();
+
+ /* Provide the message to be encrypted, and obtain the encrypted output.
+ * EVP_EncryptUpdate can only be called once for this
+ */
+ if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
+ handleErrors();
+ ciphertext_len = len;
+
+ /* Finalise the encryption. Normally ciphertext bytes may be written at
+ * this stage, but this does not occur in CCM mode
+ */
+ if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
+ ciphertext_len += len;
+
+ /* Get the tag */
+ if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16, tag))
+ handleErrors();
+
+ /* Clean up */
+ EVP_CIPHER_CTX_free(ctx);
+
+ return ciphertext_len;
+}
+
+
+int decryptccm(unsigned char *ciphertext, int ciphertext_len, unsigned char *aad,
+ int aad_len, unsigned char *tag, unsigned char *key, unsigned char *iv,
+ unsigned char *plaintext)
+{
+ EVP_CIPHER_CTX *ctx;
+ int len;
+ int plaintext_len;
+ int ret;
+
+ /* Create and initialise the context */
+ if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
+
+ /* Initialise the decryption operation. */
+ if(1 != EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL, NULL))
+ handleErrors();
+
+ /* Setting IV len to 7. Not strictly necessary as this is the default
+ * but shown here for the purposes of this example */
+ if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, 7, NULL))
+ handleErrors();
+
+ /* Set expected tag value. */
+ if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, tag))
+ handleErrors();
+
+ /* Initialise key and IV */
+ if(1 != EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv)) handleErrors();
+
+
+ /* Provide the total ciphertext length
+ */
+ if(1 != EVP_DecryptUpdate(ctx, NULL, &len, NULL, ciphertext_len))
+ handleErrors();
+
+ /* Provide any AAD data. This can be called zero or more times as
+ * required
+ */
+ if(1 != EVP_DecryptUpdate(ctx, NULL, &len, aad, aad_len))
+ handleErrors();
+
+ /* Provide the message to be decrypted, and obtain the plaintext output.
+ * EVP_DecryptUpdate can be called multiple times if necessary
+ */
+ ret = EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len);
+
+ // printf("RET %d len %d\n",ret,len);
+
+ plaintext_len = len;
+
+ /* Clean up */
+ EVP_CIPHER_CTX_free(ctx);
+
+ if(ret > 0)
+ {
+ /* Success */
+ return plaintext_len;
+ }
+ else
+ {
+ /* Verify failed */
+ return -1;
+ }
+}
+
+
+
+
+/* int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, */
+/* unsigned char *iv, unsigned char *ciphertext, int ctr, int index) */
+/* { */
+/* EVP_CIPHER_CTX *ctx; */
+
+/* int len; */
+
+/* int ciphertext_len; */
+
+/* /\* Create and initialise the context *\/ */
+/* if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); */
+
+/* /\* Initialise the encryption operation. IMPORTANT - ensure you use a key */
+/* * and IV size appropriate for your cipher */
+/* * In this example we are using 256 bit AES (i.e. a 256 bit key). The */
+/* * IV size for *most* modes is the same as the block size. For AES this */
+/* * is 128 bits *\/ */
+/* //static double time=0; */
+/* //double t=0; */
+/* //t=TimeStart(); */
+/* //256 */
+/* //avant ecb */
+/* if(ctr) { */
+/* if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) */
+/* handleErrors(); */
+/* } */
+/* else */
+/* if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv)) */
+/* handleErrors(); */
+
+/* //time+=TimeStop(t); */
+/* //printf("Time init %f\n",time); */
+
+
+/* // int cipherBlockSize = EVP_CIPHER_CTX_block_size(ctx); */
+/* // printf("INFO(evp_encrypt): block size: %d\n", cipherBlockSize); */
+
+
+/* /\* Provide the message to be encrypted, and obtain the encrypted output. */
+/* * EVP_EncryptUpdate can be called multiple times if necessary */
+/* *\/ */
+
+/* /\* */
+/* static double time=0; */
+/* double t=0; */
+/* t=TimeStart(); */
+/* *\/ */
+/* for(int i=0;i<nb_test;i++) */
+/* { */
+
+/* if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) */
+/* handleErrors(); */
+/* ciphertext_len = len; */
+
+/* } */
+/* /\* time+=TimeStop(t); */
+/* // if(index==nb_test-1) */
+/* printf("Time encrypt %f\n",time); */
+
+/* *\/ */
+
+
+/* /\* Finalise the encryption. Further ciphertext bytes may be written at */
+/* * this stage. */
+/* *\/ */
+/* if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors(); */
+/* ciphertext_len += len; */
+
+/* /\* Clean up *\/ */
+/* EVP_CIPHER_CTX_free(ctx); */
+
+/* return ciphertext_len; */
+/* } */
+
+/* int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, */
+/* unsigned char *iv, unsigned char *plaintext, int ctr, int index) */
+/* { */
+/* EVP_CIPHER_CTX *ctx; */
+
+/* int len; */
+
+/* int plaintext_len; */
+
+/* /\* Create and initialise the context *\/ */
+/* if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); */
+
+/* /\* Initialise the decryption operation. IMPORTANT - ensure you use a key */
+/* * and IV size appropriate for your cipher */
+/* * In this example we are using 256 bit AES (i.e. a 256 bit key). The */
+/* * IV size for *most* modes is the same as the block size. For AES this */
+/* * is 128 bits *\/ */
+
+/* //256 */
+
+/* //avant => ecb */
+/* if(ctr) { */
+/* if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) */
+/* handleErrors(); */
+/* } */
+/* else */
+/* if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv)) */
+/* handleErrors(); */
+
+/* /\* Provide the message to be decrypted, and obtain the plaintext output. */
+/* * EVP_DecryptUpdate can be called multiple times if necessary */
+/* *\/ */
+
+/* /\* static double time=0; */
+/* double t=0; */
+/* t=TimeStart(); */
+/* *\/ */
+/* for(int i=0;i<nb_test;i++) */
+/* { */
+/* plaintext_len = 0; */
+/* if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) */
+/* handleErrors(); */
+/* plaintext_len = len; */
+/* } */
+/* /\* time+=TimeStop(t); */
+/* // if(index==nb_test-1) */
+/* printf("Time decrypt %f\n",time); */
+/* *\/ */
+
+
+/* /\* Finalise the decryption. Further plaintext bytes may be written at */
+/* * this stage. */
+/* *\/ */
+/* if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors(); */
+/* plaintext_len += len; */
+
+
+
+/* /\* Clean up *\/ */
+/* EVP_CIPHER_CTX_free(ctx); */
+
+/* return plaintext_len; */
+/* } */
+
+
+
+
+int main (int argc, char** argv)
+{
+ /* Set up the key and iv. Do I need to say to not hard code these in a
+ * real application? :-)
+ */
+
+ int size_buf=1;
+ int lena=0;
+
+
+ for(int i=1; i<argc; i++){
+ if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
+ if(strncmp(argv[i],"ctr",3)==0) ctr = atoi(&(argv[i][3])); //CTR ? 1 otherwise CBC like
+ if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
+ if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
+ }
+
+/* printf("nb times %d\n",nb_test);
+ printf("ctr %d\n",ctr);
+ printf("lena %d\n",lena);
+ printf("size_buf %d\n",size_buf);
+*/
+
+
+
+
+ /* A 256 bit key */
+// unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
+ unsigned char *key = (unsigned char *)"01234567890123450123456789012345";
+
+ /* A 128 bit IV */
+ unsigned char *iv = (unsigned char *)"0123456789012345";
+
+ unsigned char *tag= malloc(16);
+
+ /* Message to be encrypted */
+
+ /* Buffer for ciphertext. Ensure the buffer is long enough for the
+ * ciphertext which may be longer than the plaintext, dependant on the
+ * algorithm and mode
+ */
+
+ int width;
+ int height;
+ uchar *data_R, *data_G, *data_B;
+ int imsize;
+ uchar *buffer;
+
+
+ if(lena==1) {
+ load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
+ imsize=width*height*3;
+// load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
+ }
+ else {
+ width=size_buf;
+ height=size_buf;
+ imsize=width*height;
+ buffer=malloc(imsize*sizeof(uchar));
+ for(int i=0;i<imsize;i++) {
+ buffer[i]=lrand48();
+ }
+ }
+
+
+
+ int oneD=width*height;
+ uchar *plaintext = malloc(imsize+1000); //add that for cbc
+ if(lena) {
+ for(int i=0;i<oneD;i++) {
+ plaintext[i]=data_R[i];
+ plaintext[oneD+i]=data_G[i];
+ plaintext[2*oneD+i]=data_B[i];
+ }
+ }
+ else
+ {
+ for(int i=0;i<oneD;i++) {
+ plaintext[i]=buffer[i];
+ }
+ }
+
+
+
+ uchar *ciphertext = malloc(imsize+1000); //add that for cbc
+
+ /* Buffer for the decrypted text */
+ uchar *decryptedtext = malloc(imsize+1000); //add that for cbc
+
+ int decryptedtext_len, ciphertext_len;
+
+ /* Initialise the library */
+/* ERR_load_crypto_strings();
+ OpenSSL_add_all_algorithms();
+ OPENSSL_config(NULL);
+*/
+
+
+ double time_encrypt=0;
+ double time_decrypt=0;
+ double t=TimeStart();
+
+
+ /* Encrypt the plaintext */
+
+
+ int i;
+
+
+
+ /* for(int i=0;i<16;i++)
+ printf("%d ",tag[i]);
+ printf("\n");
+ */
+ for(i=0;i<nb_test;i++)
+ {
+ ciphertext_len = encryptccm (plaintext, imsize, plaintext, imsize, key, iv,
+ ciphertext, tag);
+ }
+ /*for(int i=0;i<16;i++) {
+ printf("%d ",tag[i]);
+ }
+ printf("\n");*/
+ time_encrypt+=TimeStop(t);
+
+// printf("Time encrypt %f\n",time);
+ printf("%e\t",(double)imsize*nb_test/time_encrypt);
+
+ if(lena) {
+ for(int i=0;i<oneD;i++) {
+ data_R[i]=ciphertext[i];
+ data_G[i]=ciphertext[oneD+i];
+ data_B[i]=ciphertext[2*oneD+i];
+ }
+ store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
+ }
+
+
+ t=0;
+ t=TimeStart();
+
+
+
+ for(int i=0;i<nb_test;i++)
+ {
+ /* Decrypt the ciphertext */
+ decryptedtext_len = decryptccm(ciphertext, ciphertext_len,ciphertext, ciphertext_len,tag, key, iv,
+ decryptedtext);
+ }
+
+ time_decrypt+=TimeStop(t);
+
+ //printf("Time decrypt %f\n",time);
+ printf("%e\t",(double)imsize*nb_test/time_decrypt);
+
+ if(lena) {
+ for(int i=0;i<oneD;i++) {
+ data_R[i]=decryptedtext[i];
+ data_G[i]=decryptedtext[oneD+i];
+ data_B[i]=decryptedtext[2*oneD+i];
+ }
+ store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
+ }
+ else {
+ int equal=1;
+ for(int i=0;i<imsize;i++) {
+ //cout<<(int)buffer[i]<<endl;
+ if(buffer[i]!=decryptedtext[i]) {
+ equal=0;
+ }
+ }
+// printf("RESULT CORRECT: %d\n",equal);
+ }
+
+
+ /* Clean up */
+ EVP_cleanup();
+ ERR_free_strings();
+
+ return 0;
+}