summaryrefslogtreecommitdiffstats
path: root/src/aes.c
blob: 5165992f97c4fea5c9ac7b7e930c528c6600807c (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
#include <mcrypt.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <time.h>

#include "aes.h"
#include "util.h"
#include "aeswepd.h"

#define MAX_CACHE 10

static MCRYPT m = MCRYPT_FAILED;


struct cache_entry {
    uint8_t key[AES_KEY_LEN];
    uint8_t data[AES_KEY_LEN];
    uint8_t result[AES_KEY_LEN];
    time_t timestamp;
};

static struct cache_entry cache[MAX_CACHE];
static int n_cache = 0;

int aes_crypt(uint8_t *key, uint8_t *data, uint8_t *result) {
    int r, j;
    struct cache_entry *e;
    time_t now;
    
    for (j = 0; j < n_cache; j++)
        if (!memcmp(cache[j].key, key, AES_KEY_LEN) && !memcmp(cache[j].data, data, AES_KEY_LEN)) {
            //fprintf(stderr, "Cache hit\n");
            memcpy(result, cache[j].result, AES_KEY_LEN);
            return 0;
        }
    
    //fprintf(stderr, "Cache miss\n");
    
    if (m == MCRYPT_FAILED) {
        if ((m = mcrypt_module_open("rijndael-128", NULL, "ecb", NULL)) == MCRYPT_FAILED) {
            fprintf(stderr, "Failed to open rijndael mcrypt module\n");
            return -1;
        }
    }

    if ((r = mcrypt_generic_init(m, key, AES_KEY_LEN, NULL)) != 0) {
        fprintf(stderr, "Failed to encrypt: %s\n", mcrypt_strerror(r));
        return -1;
    }

    memcpy(result, data, AES_KEY_LEN);
    if (mcrypt_generic(m, result, AES_KEY_LEN) != 0) {
        fprintf(stderr, "mdecrypt_generic() failed.\n");
        return -1;
    }

    if (mcrypt_generic_deinit(m) != 0) {
        fprintf(stderr, "mdecrypt_generic() failed.\n");
        return -1;
    }

    now = time(NULL);
    
    if (n_cache < n_max_keys)
        e = &cache[n_cache++];
    else {
        if (n_cache > n_max_keys)
            n_cache = n_max_keys;

        e = NULL;
        for (j = 0; j < n_cache; j++)
            if (!e || cache[j].timestamp < e->timestamp)
                e = &cache[j];
    }
        
    memcpy(e->key, key, AES_KEY_LEN);
    memcpy(e->data, data, AES_KEY_LEN);
    memcpy(e->result, result, AES_KEY_LEN);
    e->timestamp = now;
    
    return 0;
}

int aes_done(void) {
    if (m != MCRYPT_FAILED) {
        mcrypt_module_close(m);
        m = MCRYPT_FAILED;
    }

    n_cache = 0;

    return 0;
}