xref: /freebsd/crypto/krb5/src/util/support/t_hex.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* util/support/t_hex.c - Test hex encoding and decoding */
3 /*
4  * Copyright (C) 2018 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <k5-platform.h>
34 #include <k5-hex.h>
35 
36 struct {
37     const char *hex;
38     const char *binary;
39     size_t binary_len;
40     int uppercase;
41 } tests[] = {
42     /* Invalid hex strings */
43     { "1" },
44     { "123" },
45     { "0/" },
46     { "/0" },
47     { "0:" },
48     { ":0" },
49     { "0@" },
50     { "@0" },
51     { "0G" },
52     { "G0" },
53     { "0`" },
54     { "`0" },
55     { "0g" },
56     { "g0" },
57     { " 00 " },
58     { "0\x01" },
59 
60     { "", "", 0 },
61     { "00", "\x00", 1 },
62     { "01", "\x01", 1 },
63     { "10", "\x10", 1 },
64     { "01ff", "\x01\xFF", 2 },
65     { "A0B0C0", "\xA0\xB0\xC0", 3, 1 },
66     { "1a2b3c4d5e6f", "\x1A\x2B\x3C\x4D\x5E\x6F", 6 },
67     { "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
68       "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
69       "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 32 },
70 
71     /* All byte values, lowercase */
72     { "0001020304050607", "\x00\x01\x02\x03\x04\x05\x06\x07", 8 },
73     { "08090a0b0c0d0e0f", "\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", 8 },
74     { "1011121314151617", "\x10\x11\x12\x13\x14\x15\x16\x17", 8 },
75     { "18191a1b1c1d1e1f", "\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", 8 },
76     { "2021222324252627", "\x20\x21\x22\x23\x24\x25\x26\x27", 8 },
77     { "28292a2b2c2d2e2f", "\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F", 8 },
78     { "3031323334353637", "\x30\x31\x32\x33\x34\x35\x36\x37", 8 },
79     { "38393a3b3c3d3e3f", "\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F", 8 },
80     { "4041424344454647", "\x40\x41\x42\x43\x44\x45\x46\x47", 8 },
81     { "48494a4b4c4d4e4f", "\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F", 8 },
82     { "5051525354555657", "\x50\x51\x52\x53\x54\x55\x56\x57", 8 },
83     { "58595a5b5c5d5e5f", "\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F", 8 },
84     { "6061626364656667", "\x60\x61\x62\x63\x64\x65\x66\x67", 8 },
85     { "68696a6b6c6d6e6f", "\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F", 8 },
86     { "7071727374757677", "\x70\x71\x72\x73\x74\x75\x76\x77", 8 },
87     { "78797a7b7c7d7e7f", "\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F", 8 },
88     { "8081828384858687", "\x80\x81\x82\x83\x84\x85\x86\x87", 8 },
89     { "88898a8b8c8d8e8f", "\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F", 8 },
90     { "9091929394959697", "\x90\x91\x92\x93\x94\x95\x96\x97", 8 },
91     { "98999a9b9c9d9e9f", "\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F", 8 },
92     { "a0a1a2a3a4a5a6a7", "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7", 8 },
93     { "a8a9aaabacadaeaf", "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF", 8 },
94     { "b0b1b2b3b4b5b6b7", "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7", 8 },
95     { "b8b9babbbcbdbebf", "\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF", 8 },
96     { "c0c1c2c3c4c5c6c7", "\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7", 8 },
97     { "c8c9cacbcccdcecf", "\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF", 8 },
98     { "d0d1d2d3d4d5d6d7", "\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7", 8 },
99     { "d8d9dadbdcdddedf", "\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF", 8 },
100     { "e0e1e2e3e4e5e6e7", "\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7", 8 },
101     { "e8e9eaebecedeeef", "\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF", 8 },
102     { "f0f1f2f3f4f5f6f7", "\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7", 8 },
103     { "f8f9fafbfcfdfeff", "\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF", 8 },
104 
105     /* All byte values, uppercase */
106     { "0001020304050607", "\x00\x01\x02\x03\x04\x05\x06\x07", 8, 1 },
107     { "08090A0B0C0D0E0F", "\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", 8, 1 },
108     { "1011121314151617", "\x10\x11\x12\x13\x14\x15\x16\x17", 8, 1 },
109     { "18191A1B1C1D1E1F", "\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", 8, 1 },
110     { "2021222324252627", "\x20\x21\x22\x23\x24\x25\x26\x27", 8, 1 },
111     { "28292A2B2C2D2E2F", "\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F", 8, 1 },
112     { "3031323334353637", "\x30\x31\x32\x33\x34\x35\x36\x37", 8, 1 },
113     { "38393A3B3C3D3E3F", "\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F", 8, 1 },
114     { "4041424344454647", "\x40\x41\x42\x43\x44\x45\x46\x47", 8, 1 },
115     { "48494A4B4C4D4E4F", "\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F", 8, 1 },
116     { "5051525354555657", "\x50\x51\x52\x53\x54\x55\x56\x57", 8, 1 },
117     { "58595A5B5C5D5E5F", "\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F", 8, 1 },
118     { "6061626364656667", "\x60\x61\x62\x63\x64\x65\x66\x67", 8, 1 },
119     { "68696A6B6C6D6E6F", "\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F", 8, 1 },
120     { "7071727374757677", "\x70\x71\x72\x73\x74\x75\x76\x77", 8, 1 },
121     { "78797A7B7C7D7E7F", "\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F", 8, 1 },
122     { "8081828384858687", "\x80\x81\x82\x83\x84\x85\x86\x87", 8, 1 },
123     { "88898A8B8C8D8E8F", "\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F", 8, 1 },
124     { "9091929394959697", "\x90\x91\x92\x93\x94\x95\x96\x97", 8, 1 },
125     { "98999A9B9C9D9E9F", "\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F", 8, 1 },
126     { "A0A1A2A3A4A5A6A7", "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7", 8, 1 },
127     { "A8A9AAABACADAEAF", "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF", 8, 1 },
128     { "B0B1B2B3B4B5B6B7", "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7", 8, 1 },
129     { "B8B9BABBBCBDBEBF", "\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF", 8, 1 },
130     { "C0C1C2C3C4C5C6C7", "\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7", 8, 1 },
131     { "C8C9CACBCCCDCECF", "\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF", 8, 1 },
132     { "D0D1D2D3D4D5D6D7", "\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7", 8, 1 },
133     { "D8D9DADBDCDDDEDF", "\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF", 8, 1 },
134     { "E0E1E2E3E4E5E6E7", "\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7", 8, 1 },
135     { "E8E9EAEBECEDEEEF", "\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF", 8, 1 },
136     { "F0F1F2F3F4F5F6F7", "\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7", 8, 1 },
137     { "F8F9FAFBFCFDFEFF", "\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF", 8, 1 },
138 };
139 
140 int
main(void)141 main(void)
142 {
143     size_t i;
144     char *hex;
145     int ret;
146     uint8_t *bytes;
147     size_t len;
148 
149     for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
150         if (tests[i].binary == NULL) {
151             ret = k5_hex_decode(tests[i].hex, &bytes, &len);
152             assert(ret == EINVAL && bytes == NULL && len == 0);
153             continue;
154         }
155 
156         ret = k5_hex_decode(tests[i].hex, &bytes, &len);
157         assert(ret == 0);
158         assert(len == tests[i].binary_len);
159         assert(memcmp(bytes, tests[i].binary, len) == 0);
160         assert(bytes[len] == 0);
161         free(bytes);
162 
163         ret = k5_hex_encode((uint8_t *)tests[i].binary, tests[i].binary_len,
164                             tests[i].uppercase, &hex);
165         assert(ret == 0);
166         assert(strcmp(tests[i].hex, hex) == 0);
167         free(hex);
168     }
169     return 0;
170 }
171