]>
Commit | Line | Data |
---|---|---|
d918d9d1 | 1 | /* |
eadd1644 | 2 | * Copyright (c) 2009, 2010, 2013, 2014 Nicira, Inc. |
d918d9d1 BP |
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> | |
3f636c7e | 18 | #undef NDEBUG |
d918d9d1 | 19 | #include "aes128.h" |
3f636c7e | 20 | #include <ctype.h> |
eadd1644 | 21 | #include "ovstest.h" |
3f636c7e | 22 | #include "util.h" |
d918d9d1 BP |
23 | |
24 | static void | |
25 | hex_to_uint8(const char *input, uint8_t *output, size_t n) | |
26 | { | |
27 | size_t i; | |
28 | ||
29 | if (strlen(input) != n * 2) { | |
30 | goto error; | |
31 | } | |
32 | for (i = 0; i < n; i++) { | |
bf971267 | 33 | bool ok; |
d918d9d1 | 34 | |
bf971267 BP |
35 | output[i] = hexits_value(&input[i * 2], 2, &ok); |
36 | if (!ok) { | |
d918d9d1 BP |
37 | goto error; |
38 | } | |
d918d9d1 BP |
39 | } |
40 | return; | |
41 | ||
42 | error: | |
34582733 | 43 | ovs_fatal(0, "\"%s\" is not exactly %"PRIuSIZE" hex digits", input, n * 2); |
d918d9d1 BP |
44 | } |
45 | ||
eadd1644 AZ |
46 | static void |
47 | test_aes128_main(int argc, char *argv[]) | |
d918d9d1 BP |
48 | { |
49 | struct aes128 aes; | |
50 | uint8_t plaintext[16]; | |
51 | uint8_t ciphertext[16]; | |
52 | uint8_t key[16]; | |
53 | size_t i; | |
54 | ||
55 | if (argc != 3) { | |
56 | ovs_fatal(0, "usage: %s KEY PLAINTEXT, where KEY and PLAINTEXT each " | |
57 | "consist of 32 hex digits", argv[0]); | |
58 | } | |
59 | ||
60 | hex_to_uint8(argv[1], key, 16); | |
61 | hex_to_uint8(argv[2], plaintext, 16); | |
62 | ||
63 | aes128_schedule(&aes, key); | |
64 | aes128_encrypt(&aes, plaintext, ciphertext); | |
65 | for (i = 0; i < 16; i++) { | |
66 | printf("%02x", ciphertext[i]); | |
67 | } | |
68 | putchar('\n'); | |
d918d9d1 | 69 | } |
eadd1644 AZ |
70 | |
71 | OVSTEST_REGISTER("test-aes128", test_aes128_main); |