gcm_test.c 2.59 KB
Newer Older
davxy's avatar
davxy committed
1 2 3 4 5 6 7
#include "test.h"
#include <cry/gcm.h>
#include <cry/aes.h>

static const struct cry_ciph_itf aes_itf = {
    .init = NULL,
    .clean = NULL,
Davide Galassi's avatar
Davide Galassi committed
8 9 10
    .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
davxy's avatar
davxy committed
11 12 13 14
};

void cry_aes_128_gcm_encrypt(unsigned char *dst,
                             const unsigned char *src,
15
                             size_t src_size,
davxy's avatar
davxy committed
16 17
                             const unsigned char *key,
                             const unsigned char *iv,
18
                             unsigned char *mac,
davxy's avatar
davxy committed
19
                             const unsigned char *aad,
20
                             size_t aad_size)
davxy's avatar
davxy committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
{
    struct cry_aes_ctx aes;
    struct cry_gcm_ctx gcm;

    gcm.ciph_itf = &aes_itf;
    gcm.ciph_ctx = &aes;
    cry_gcm_key_set(&gcm, key, 16);
    cry_gcm_iv_set(&gcm, iv, 16);
    cry_gcm_encrypt(&gcm, dst, src, src_size);
    if (aad != NULL)
        cry_gcm_update(&gcm, aad, aad_size);
    if (mac != NULL)
        cry_gcm_digest(&gcm, mac, 16);
}

void cry_aes_128_gcm_decrypt(unsigned char *dst,
                             const unsigned char *src,
38
                             size_t src_size,
davxy's avatar
davxy committed
39 40
                             const unsigned char *key,
                             const unsigned char *iv,
41
                             unsigned char *mac,
davxy's avatar
davxy committed
42
                             const unsigned char *aad,
43
                             size_t aad_size)
davxy's avatar
davxy committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
{
    struct cry_aes_ctx aes;
    struct cry_gcm_ctx gcm;

    gcm.ciph_itf = &aes_itf;
    gcm.ciph_ctx = &aes;
    cry_gcm_key_set(&gcm, key, 16);
    cry_gcm_iv_set(&gcm, iv, 16);
    cry_gcm_decrypt(&gcm, dst, src, src_size);
    if (aad != NULL)
        cry_gcm_update(&gcm, aad, aad_size);
    if (mac != NULL)
        cry_gcm_digest(&gcm, mac, 16);
}

59

davxy's avatar
davxy committed
60 61
void gcm_test(void)
{
62
    char *msg = "This file is part of CRY software.";
Davide Galassi's avatar
Davide Galassi committed
63 64 65 66
    char key[] = {  0, 1,  2,  3,  4,  5,  6,  7,
                    8, 9, 10, 11, 12, 13, 14, 15 };
    char iv[] =  {  0, 1,  2,  3,  4,  5,  6,  7,
                    8, 9, 10, 11, 12, 13, 14, 15 };
davxy's avatar
davxy committed
67 68 69 70
    char *aad = "Hello";
    int msglen = strlen(msg);
    int aadlen = strlen(aad);

71
    memcpy(buf, msg, msglen);
davxy's avatar
davxy committed
72 73 74

    cry_aes_128_gcm_encrypt(buf, msg, msglen, key, iv,
                            buf+msglen, aad, aadlen);
75 76
    PRINT_HEX("ciphertext", buf, msglen);

davxy's avatar
davxy committed
77 78
    cry_aes_128_gcm_decrypt(buf, buf, msglen, key, iv,
                            buf+msglen+16, aad, aadlen);
79 80 81 82 83
    PRINT_ASC("plaintext ", buf, msglen);

    /* GMAC check */
    PRINT_HEX("gmac", buf+msglen, 16);
    ASSERT_EQ_BUF(buf+msglen, buf+msglen+16, 16);
davxy's avatar
davxy committed
84
}