aes_test.c 6.69 KB
Newer Older
davxy's avatar
davxy committed
1 2
#include "test.h"
#include <cry/aes.h>
3 4
#include <cry/cbc.h>
#include <cry/ctr.h>
5 6
#include <cry/gcm.h>

7 8 9 10 11 12 13 14 15

static const cry_ciph_itf aes_itf = {
    .key_set = (cry_ciph_key_set_f) cry_aes_key_set,
    .encrypt = (cry_ciph_encrypt_f) cry_aes_encrypt,
    .decrypt = (cry_ciph_decrypt_f) cry_aes_decrypt,
};

struct aes_param {
    unsigned int  keylen;
16 17 18 19 20
    unsigned int  ivlen;
    unsigned int  aadlen;
    unsigned int  srclen;
    unsigned int  dstlen;
    unsigned int  maclen;
21
    unsigned char key[32];
22 23 24 25 26
    unsigned char iv[16];
    unsigned char mac[16];
    unsigned char aad[128];
    unsigned char src[128];
    unsigned char dst[128];
27
};
davxy's avatar
davxy committed
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#define EMPTY_STRING "NULL"

int raw_init(unsigned char *raw, unsigned int rawlen, const char *asc)
{
    int len;

    if (strcmp(asc, EMPTY_STRING) == 0)
        return 0;
    if (strcmp(asc, "-1") == 0)
        return -1;
    len = strlen(asc);
    if (rawlen < (len >> 1))
        len = rawlen << 1;
    asc_to_raw(asc, len, raw);
    return len >> 1;
}

46 47 48 49
static void param_init(struct aes_param *par, int argc, char *argv[])
{
    int i = 0;

50 51 52 53 54 55 56 57 58 59
    memset(par, 0, sizeof(*par));
    par->keylen = raw_init(par->key, sizeof(par->key), argv[i++]);
    if (argc >= 4) {
        /* CBC, CTR, GCM */
        par->ivlen = raw_init(par->iv, sizeof(par->iv), argv[i++]);
        if (argc == 6) {
            /* GCM */
            par->aadlen = raw_init(par->aad, sizeof(par->aad), argv[i++]);
            par->maclen = raw_init(par->mac, sizeof(par->mac), argv[i++]);
        }
60
    }
61 62 63 64
    par->srclen = raw_init(par->src, sizeof(par->src), argv[i++]);
    par->dstlen = raw_init(par->dst, sizeof(par->dst), argv[i++]);
    if (par->dstlen != -1)
        ASSERT_EQ(par->srclen, par->dstlen);
65
}
66

67
static void aes_ecb_encrypt(int argc, char *argv[])
68
{
69
    cry_aes_ctx ctx;
70 71
    struct aes_param par;
    unsigned char dst[32];
72

73 74 75 76
    ASSERT(argc == 3);
    param_init(&par, argc, argv);

    cry_aes_key_set(&ctx, par.key, par.keylen);
77
    cry_aes_encrypt(&ctx, dst, par.src, par.srclen);
78

79
    ASSERT_EQ_BUF(dst, par.dst, par.srclen);
80 81
}

82
static void aes_ecb_decrypt(int argc, char *argv[])
83 84
{
    cry_aes_ctx ctx;
85 86
    struct aes_param par;
    unsigned char dst[32];
87

88 89
    ASSERT(argc == 3);
    param_init(&par, argc, argv);
90

91
    cry_aes_key_set(&ctx, par.key, par.keylen);
92
    cry_aes_decrypt(&ctx, dst, par.src, par.srclen);
93

94
    ASSERT_EQ_BUF(dst, par.dst, par.srclen);
95 96
}

97
static void aes_cbc_encrypt(int argc, char *argv[])
98
{
99 100 101 102 103 104 105 106 107 108 109
    cry_cbc_ctx ctx;
    cry_aes_ctx aes_ctx;
    struct aes_param par;
    unsigned char dst[32];

    ASSERT(argc == 4);
    param_init(&par, argc, argv);

    cry_cbc_init(&ctx, &aes_ctx, &aes_itf);
    cry_cbc_key_set(&ctx, par.key, par.keylen);
    cry_cbc_iv_set(&ctx, par.iv, 16);
110
    cry_cbc_encrypt(&ctx, dst, par.src, par.srclen);
111

112
    ASSERT_EQ_BUF(dst, par.dst, par.srclen);
113 114
}

115
static void aes_cbc_decrypt(int argc, char *argv[])
116
{
117 118 119 120 121 122 123 124 125 126 127
    cry_cbc_ctx ctx;
    cry_aes_ctx aes_ctx;
    struct aes_param par;
    unsigned char dst[32];

    ASSERT(argc == 4);
    param_init(&par, argc, argv);

    cry_cbc_init(&ctx, &aes_ctx, &aes_itf);
    cry_cbc_key_set(&ctx, par.key, par.keylen);
    cry_cbc_iv_set(&ctx, par.iv, 16);
128
    cry_cbc_decrypt(&ctx, dst, par.src, par.srclen);
129

130
    ASSERT_EQ_BUF(dst, par.dst, par.srclen);
131
}
132

133
static void aes_ctr_encrypt(int argc, char *argv[])
134
{
135 136 137 138 139 140 141 142 143 144 145
    cry_ctr_ctx ctx;
    cry_aes_ctx aes_ctx;
    struct aes_param par;
    unsigned char dst[32];

    ASSERT(argc == 4);
    param_init(&par, argc, argv);

    cry_ctr_init(&ctx, &aes_ctx, &aes_itf);
    cry_ctr_key_set(&ctx, par.key, par.keylen);
    cry_ctr_iv_set(&ctx, par.iv, 16);
146
    cry_ctr_encrypt(&ctx, dst, par.src, par.srclen);
147

148
    ASSERT_EQ_BUF(dst, par.dst, par.srclen);
149 150
}

151
static void aes_ctr_decrypt(int argc, char *argv[])
152
{
153 154 155 156 157 158 159 160 161 162 163
    cry_ctr_ctx ctx;
    cry_aes_ctx aes_ctx;
    struct aes_param par;
    unsigned char dst[32];

    ASSERT(argc == 4);
    param_init(&par, argc, argv);

    cry_ctr_init(&ctx, &aes_ctx, &aes_itf);
    cry_ctr_key_set(&ctx, par.key, par.keylen);
    cry_ctr_iv_set(&ctx, par.iv, 16);
164
    cry_ctr_decrypt(&ctx, dst, par.src, par.srclen);
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    ASSERT_EQ_BUF(dst, par.dst, par.srclen);
}

static void aes_gcm_encrypt(int argc, char *argv[])
{
    cry_gcm_ctx ctx;
    cry_aes_ctx aes_ctx;
    struct aes_param par;
    unsigned char dst[128];
    unsigned char mac[16];

    ASSERT(argc == 6);
    param_init(&par, argc, argv);

    cry_gcm_init(&ctx, &aes_ctx, &aes_itf);
    cry_gcm_key_set(&ctx, par.key, par.keylen);
    cry_gcm_iv_set(&ctx, par.iv, 16);
    cry_gcm_update(&ctx, par.aad, par.aadlen);
    cry_gcm_encrypt(&ctx, dst, par.src, par.srclen);
    cry_gcm_digest(&ctx, mac, par.maclen);

    ASSERT_EQ_BUF(dst, par.dst, par.srclen);
    ASSERT_EQ_BUF(mac, par.mac, par.maclen);
}

static void aes_gcm_decrypt(int argc, char *argv[])
{
    cry_gcm_ctx ctx;
    cry_aes_ctx aes_ctx;
    struct aes_param par;
    unsigned char dst[128];
    unsigned char mac[16];

    ASSERT(argc == 6);
    param_init(&par, argc, argv);

    cry_gcm_init(&ctx, &aes_ctx, &aes_itf);
    cry_gcm_key_set(&ctx, par.key, par.keylen);
    cry_gcm_iv_set(&ctx, par.iv, 16);
    cry_gcm_update(&ctx, par.aad, par.aadlen);
    cry_gcm_decrypt(&ctx, dst, par.src, par.srclen);
    cry_gcm_digest(&ctx, mac, par.maclen);

    if (par.dstlen == par.srclen) {
        ASSERT_EQ_BUF(mac, par.mac, par.maclen);
        ASSERT_EQ_BUF(dst, par.dst, par.dstlen);
    } else {
        ASSERT_NE_BUF(mac, par.mac, par.maclen);
    }
215
}
davxy's avatar
davxy committed
216

217

218 219
static void dispatch(int argc, char *argv[])
{
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    char *test = *argv;

    argv++;
    argc--;

    if (strcmp(test, "aes_ecb_encrypt") == 0)
        aes_ecb_encrypt(argc, argv);
    else if (strcmp(test, "aes_ecb_decrypt") == 0)
        aes_ecb_decrypt(argc, argv);
    else if (strcmp(test, "aes_cbc_encrypt") == 0)
        aes_cbc_encrypt(argc, argv);
    else if (strcmp(test, "aes_cbc_decrypt") == 0)
        aes_cbc_decrypt(argc, argv);
    else if (strcmp(test, "aes_ctr_encrypt") == 0)
        aes_ctr_encrypt(argc, argv);
    else if (strcmp(test, "aes_ctr_decrypt") == 0)
        aes_ctr_decrypt(argc, argv);
237 238 239 240
    else if (strcmp(test, "aes_gcm_encrypt") == 0)
        aes_gcm_encrypt(argc, argv);
    else if (strcmp(test, "aes_gcm_decrypt") == 0)
        aes_gcm_decrypt(argc, argv);
241
    else
242
        printf("Test '%s' not defined\n", test);
243 244
}

davxy's avatar
davxy committed
245 246
void aes_test(void)
{
247
    printf("* AES NIST AESAVS KAT\n");
248
    func_test("aes_test.data", dispatch);
249
    printf("* AES GCM NIST Encrypt Validation (iv,txt,aad,mac)\n");
250
    func_test("aes_gcm_en_test.data", dispatch);
251
    printf("* AES GCM NIST Decrypt Validation (iv,txt,aad,mac)\n");
252
    func_test("aes_gcm_de_test.data", dispatch);
253
    printf("* AES-192 GCM NIST Encrypt Validation (iv,txt,aad,mac)\n");
254
    func_test("aes_gcm_en_192_test.data", dispatch);
255
    printf("\n");
davxy's avatar
davxy committed
256
}