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