summaryrefslogtreecommitdiff
path: root/tests/test-aes128.c
blob: 7960551be9834ef8430c29d097eff0d4dd370fb8 (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
/*
 * Copyright (c) 2009, 2010, 2013, 2014 Nicira, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>
#undef NDEBUG
#include "aes128.h"
#include <ctype.h>
#include "ovstest.h"
#include "util.h"

static void
hex_to_uint8(const char *input, uint8_t *output, size_t n)
{
    size_t i;

    if (strlen(input) != n * 2) {
        goto error;
    }
    for (i = 0; i < n; i++) {
        bool ok;

        output[i] = hexits_value(&input[i * 2], 2, &ok);
        if (!ok) {
            goto error;
        }
    }
    return;

error:
    ovs_fatal(0, "\"%s\" is not exactly %"PRIuSIZE" hex digits", input, n * 2);
}

static void
test_aes128_main(int argc, char *argv[])
{
    struct aes128 aes;
    uint8_t plaintext[16];
    uint8_t ciphertext[16];
    uint8_t key[16];
    size_t i;

    if (argc != 3) {
        ovs_fatal(0, "usage: %s KEY PLAINTEXT, where KEY and PLAINTEXT each "
                  "consist of 32 hex digits", argv[0]);
    }

    hex_to_uint8(argv[1], key, 16);
    hex_to_uint8(argv[2], plaintext, 16);

    aes128_schedule(&aes, key);
    aes128_encrypt(&aes, plaintext, ciphertext);
    for (i = 0; i < 16; i++) {
        printf("%02x", ciphertext[i]);
    }
    putchar('\n');
}

OVSTEST_REGISTER("test-aes128", test_aes128_main);