Catalli's threaded switch
[sliver-openvswitch.git] / tests / test-aes128.c
1 /*
2  * Copyright (c) 2009 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include <ctype.h>
19 #include "aes128.h"
20 #include "util.h"
21
22 static void
23 hex_to_uint8(const char *input, uint8_t *output, size_t n)
24 {
25     size_t i;
26
27     if (strlen(input) != n * 2) {
28         goto error;
29     }
30     for (i = 0; i < n; i++) {
31         unsigned char hi = input[i * 2];
32         unsigned char lo = input[i * 2 + 1];
33
34         if (!isxdigit(hi) || !isxdigit(lo)) {
35             goto error;
36         }
37         output[i] = (hexit_value(hi) << 4) + hexit_value(lo);
38     }
39     return;
40
41 error:
42     ovs_fatal(0, "\"%s\" is not exactly %zu hex digits", input, n * 2);
43 }
44
45 int
46 main(int argc, char *argv[])
47 {
48     struct aes128 aes;
49     uint8_t plaintext[16];
50     uint8_t ciphertext[16];
51     uint8_t key[16];
52     size_t i;
53
54     if (argc != 3) {
55         ovs_fatal(0, "usage: %s KEY PLAINTEXT, where KEY and PLAINTEXT each "
56                   "consist of 32 hex digits", argv[0]);
57     }
58
59     hex_to_uint8(argv[1], key, 16);
60     hex_to_uint8(argv[2], plaintext, 16);
61
62     aes128_schedule(&aes, key);
63     aes128_encrypt(&aes, plaintext, ciphertext);
64     for (i = 0; i < 16; i++) {
65         printf("%02x", ciphertext[i]);
66     }
67     putchar('\n');
68
69     return 0;
70 }