aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/bitsliced-aes/testbench/app.c
blob: 71e2c3cec3014e4141bfef61e7001bfc933bf1b2 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include <openssl/bn.h>
#include <openssl/err.h>

#include "app.h"
#include "../bs.h"
#include "../aes.h"

static void openssl_die(void)
{
    fprintf(stderr,"error: %s\n",
            ERR_error_string(ERR_get_error(),NULL) );
    exit(2);
}

static unsigned int hex2bin(unsigned char ** bin, const char * hex)
{
    int len;
    BIGNUM * bn = NULL;
    if(BN_hex2bn(&bn, (char*)hex) == 0)
    {   openssl_die();    }

    len = BN_num_bytes(bn);
    *bin = (unsigned char *)malloc(len);

    if(BN_bn2bin(bn, *bin) == 0)
    {   openssl_die();  }
    return len;
}


int cli_app(int argc, char * argv[])
{
    uint8_t key[16];
    uint8_t iv[16];
    
    uint8_t * key_p = NULL, * iv_p = NULL;

    int keylen, ivlen;

    if (argc != 5)
    {
        fprintf(stderr, 
                "Bitsliced AES-CTR\n"
                "usage: %s <key-hex> <iv-hex> <input-file> <output-file>\n", argv[0]);
        exit(1);
    }

    char * key_s = argv[1];
    char * iv_s = argv[2];
    char * input_name = argv[3];
    char * output_name = argv[4];

    FILE * input = fopen(input_name, "r");
    if (input == NULL)
    {
        perror("fopen");
        exit(2);
    }

    FILE * output = fopen(output_name, "w+");
    if (output == NULL)
    {
        perror("fopen");
        exit(2);
    }


    ERR_load_crypto_strings();

    keylen = hex2bin(&key_p,key_s);
    if (keylen > 16)
    {
        free(key_p);
        fprintf(stderr,"key must be 16 bytes or less\n");
        exit(2);
    }

    ivlen = hex2bin(&iv_p,iv_s);
    if (ivlen > 16)
    {
        free(iv_p);
        free(key_p);
        fprintf(stderr,"iv must be 16 bytes or less\n");
        exit(2);
    }

    memset(iv,0,16);
    memset(key,0,16);

    memmove(iv, iv_p, ivlen);
    memmove(key, key_p, keylen);

    free(iv_p);
    free(key_p);

    fseek(input, 0L, SEEK_END);
    size_t amt = ftell(input);
    fseek(input, 0L, SEEK_SET);

    uint8_t * pt = (uint8_t *) malloc(amt);
    uint8_t * ct = (uint8_t *) malloc(amt);

    int ptlen = fread(pt, 1, amt, input);
    if (ptlen != amt)
    {
        perror("fread");
        exit(2);
    }

    struct timespec tstart,tend;
    clock_gettime(CLOCK_MONOTONIC, &tstart);
    {
        aes_ctr_encrypt(ct, pt, amt, key, iv);
    }
    clock_gettime(CLOCK_MONOTONIC, &tend);

    double total = (((double)tend.tv_sec + 1.0e-9 * (double)tend.tv_nsec) -
                ((double)tstart.tv_sec + 1.0e-9 * (double)tstart.tv_nsec));

    printf("performance for %d word length\n", WORD_SIZE);
    printf("-------------------------------\n");
    printf("%.5f s\n", total);
    printf("%.15f s/byte\n", total/amt);
    printf("%.5f cycles/byte (for 4 GHz)\n", 4000ull * (1ull<<20) * total/amt);


    if (write(fileno(output), ct, amt) == -1)
    {
        perror("write");
        exit(2);
    }

    free(ct);
    free(pt);

    return 0;
}