affine_test.c 2.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#include "test.h"
#include <cry/affine.h>

#define RAW_MAX    100

struct affine_param {
    unsigned char keya[CRY_AFFINE_KEYMAX];
    unsigned char keyb[CRY_AFFINE_KEYMAX];
    unsigned char src[CRY_AFFINE_KEYMAX];
    unsigned char dst[CRY_AFFINE_KEYMAX];
Davide Galassi's avatar
Davide Galassi committed
11 12 13
    size_t keylen;
    size_t srclen;
    size_t dstlen;
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
};

static void param_init(struct affine_param *par, char *argv[])
{
    memset(par, 0, sizeof(*par));
    par->keylen = raw_init(par->keya, sizeof(par->keya), argv[0]);
    par->keylen = raw_init(par->keyb, sizeof(par->keyb), argv[1]);
    par->srclen = raw_init(par->src, sizeof(par->src), argv[2]);
    par->dstlen = raw_init(par->dst, sizeof(par->dst), argv[3]);
}

static void affine_encrypt(int argc, char *argv[])
{
    struct cry_affine_ctx ctx;
    struct affine_param par;
    unsigned char dst[32];

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

    cry_affine_init(&ctx, par.keya, par.keyb, par.keylen);
    cry_affine_encrypt(&ctx, dst, par.src, par.srclen);

    ASSERT_EQ_BUF(dst, par.dst, par.dstlen);
}

static void affine_decrypt(int argc, char *argv[])
{
    struct cry_affine_ctx ctx;
    struct affine_param par;
    unsigned char dst[32];

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

    cry_affine_init(&ctx, par.keya, par.keyb, par.keylen);
    cry_affine_decrypt(&ctx, dst, par.src, par.srclen);

    ASSERT_EQ_BUF(dst, par.dst, par.dstlen);
}


static void dispatch(int argc, char *argv[])
{
    char *test = *argv;

    argv++;
    argc--;

    if (strcmp(test, "affine_encrypt") == 0)
        affine_encrypt(argc, argv);
    else if (strcmp(test, "affine_decrypt") == 0)
        affine_decrypt(argc, argv);
    else
68
        TRACE("Test '%s' not defined\n", test);
69 70 71 72 73 74 75 76 77 78 79 80 81 82
}

void affine_test(void)
{
    struct cry_affine_ctx ctx;
    unsigned char keya[] = { 1, 1, 1 };
    unsigned char keyb[] = { 19, 10, 201 };
    unsigned char ciphertext[100] = {0};
    unsigned char cleartext[100] = {0};

    cry_affine_init(&ctx, keya, keyb, sizeof(keya));
    cry_affine_encrypt(&ctx, ciphertext, (unsigned char *)"HelloWorld", 10);
    cry_affine_decrypt(&ctx, cleartext, ciphertext, 10);

83
    TRACE("* Affine Test\n");
84
    func_test("affine_test.data", dispatch);
85
    TRACE("\n");
86
}