speed.c 4.69 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

 /*Bit-slice Implementation of sycon-ae128-r64
   Written by:
   Kalikinkar Mandal <kmandal@uwaterloo.ca>
   */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

#include "sycon.h"
#include "clock_cycle.h"

#define NUM_ITER        1000
#define NUM_TEST      	500

//#define NUMINSTANCE     1

void print_state ( u32 *state )
{
        u8 i, j;
        
        for ( j = 0; j < NUMINSTANCE*2; j++ )
        {
                for ( i = 0; i < STATE_SIZE; i++ )
                        printf("%.8X ", state[j*STATE_SIZE+i]);
                printf("\n");
        }
        printf("\n");
        return;
}

int main()
{
        u8 num_parallel_inst;
        u32 *state;
        int i, j;
        
        u64 t[NUM_ITER+1];
        u64 count_cc;
        
        //num_parallel_inst = 1;
        num_parallel_inst = 2*NUMINSTANCE;
        
        state = (u32 *)malloc(sizeof(u32)*num_parallel_inst*STATE_SIZE);
        
        u32 *plaintext, *ciphertext, *tag, *ad;
        u32 plen, tlen, klen, adlen;
        u8 *k, *pubn;
        
        
        num_parallel_inst = 2*NUMINSTANCE;
        adlen = 4; // Associated data length = adlen*32;
        plen = 64; // Message length = plen*32;
        tlen = 4; // 128 = 32*4 bits
        klen = 4; // 128 = 32*4 bits
        
        tag = (u32 *)malloc(sizeof(u32)*tlen*num_parallel_inst);
        ad = (u32 *)malloc(sizeof(u32)*adlen*num_parallel_inst);
        
        plaintext = (u32 *)malloc(sizeof(u32)*plen*num_parallel_inst);
        ciphertext = (u32 *)malloc(sizeof(u32)*plen*num_parallel_inst);
        
        k = (u8 *)malloc(sizeof(u8)*16*num_parallel_inst);
        pubn = (u8 *)malloc(sizeof(u8)*16*num_parallel_inst);
        
        for ( i = 0; i < num_parallel_inst*STATE_SIZE; i++ )
                *(state+i) = 0x0;
        
        print_state(state);
        sycon( state );
        print_state(state);
        //exit(0);
        
        //Assinging messages//
        for ( i = 0; i < num_parallel_inst; i++ )
        {
                for ( j = 0; j < plen; j++ )
                        *(plaintext+i*plen+j) = j%plen;
        }
        
        for ( i = 0; i < num_parallel_inst*adlen; i++ )
                *(ad+i) = i%adlen;
        
        for ( i = 0; i < num_parallel_inst*16; i++ )
        {
                *(k+i) = i%16;
                *(pubn+i) = i%16;
        }
        
        
        
        for ( i = 0; i < NUM_ITER+1; i++ )
        {
                count_cc = start_rdtsc();
                for ( j = 0; j < NUM_TEST; j++ )
                        sycon ( state );
                count_cc = end_rdtsc()-count_cc;
                printf("Cycles per byte = %f cpb\n", (double)(count_cc)/(double)(num_parallel_inst*40*NUM_TEST));
        }
        //print_state(state);
        //printf("\n");
        
        
        //AEAD: Encryption and Decryption Module//
        for ( i = 0; i < NUM_ITER; i++ ){
                count_cc = start_rdtsc();
                crypto_aead_encrypt( tag, tlen, ciphertext, plaintext, plen, ad, adlen, k, pubn, 16 );
                count_cc = end_rdtsc()-count_cc;
                printf("Encryption speed = %f cpb\n", (double)(count_cc)/(double)(num_parallel_inst*plen*4));
        }
        
        crypto_aead_encrypt( tag, tlen, ciphertext, plaintext, plen, ad, adlen, k, pubn, 16 );
        
        printf("Original plaintext:\n");
        for ( i = 0; i < num_parallel_inst; i++ )
        {
                for ( j = 0; j < plen; j++ )
                        printf("%08X", plaintext[i*plen+j]);
                printf("\n");
        }
        printf("Ciphertext:\n");
        for ( i = 0; i < num_parallel_inst; i++ )
        {
                for ( j = 0; j < plen; j++ )
                        printf("%08X", ciphertext[i*plen+j]);
                printf("\n");
        }
        printf("Tag:\n");
        for ( i = 0; i < num_parallel_inst; i++ )
        {
                for ( j = 0; j < tlen; j++ )
                        printf("%08X", tag[i*tlen+j]);
                printf("\n");
        }
        
        crypto_aead_decrypt( plaintext, ciphertext, plen, tag, tlen, ad, adlen, k, pubn, 16 );
        
        printf("Decrypted plaintext:\n");
        for ( i = 0; i < num_parallel_inst; i++ )
        {
                for ( j = 0; j < plen; j++ )
                        printf("%08X", plaintext[i*plen+j]);
                printf("\n");
        }
        
        printf("Tag:\n");
        for ( i = 0; i < num_parallel_inst; i++ )
        {
                for ( j = 0; j < tlen; j++ )
                        printf("%08X", tag[i*tlen+j]);
                printf("\n");
        }
        
        free(state);
        free(plaintext);
        free(ciphertext);
        free(tag);
        free(ad);

        return(0);
}