summaryrefslogtreecommitdiffstats
path: root/src/aes.c
blob: 944da0352fc85eb26215c3c43b782d66f1792045 (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
/* $Id$ */

/*
 * This file is part of waproamd.
 *
 * waproamd is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * waproamd is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with waproamd; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#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;
}