1b0d17251Schristos /* 2*97e3c585Schristos * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved. 3b0d17251Schristos * 4b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"); 5b0d17251Schristos * you may not use this file except in compliance with the License. 6b0d17251Schristos * You may obtain a copy of the License at 7b0d17251Schristos * https://www.openssl.org/source/license.html 8b0d17251Schristos * or in the file LICENSE in the source distribution. 9b0d17251Schristos */ 10b0d17251Schristos 11b0d17251Schristos /* 12b0d17251Schristos * This program tests the use of OSSL_PARAM, currently in raw form. 13b0d17251Schristos */ 14b0d17251Schristos 15b0d17251Schristos #include "internal/nelem.h" 16b0d17251Schristos #include "internal/cryptlib.h" 17b0d17251Schristos #include "testutil.h" 18b0d17251Schristos 19b0d17251Schristos struct testdata 20b0d17251Schristos { 21b0d17251Schristos const char *in; 22b0d17251Schristos const unsigned char *expected; 23b0d17251Schristos size_t expected_len; 24b0d17251Schristos const char sep; 25b0d17251Schristos }; 26b0d17251Schristos 27b0d17251Schristos static const unsigned char test_1[] = { 0xAB, 0xCD, 0xEF, 0xF1 }; 28b0d17251Schristos static const unsigned char test_2[] = { 0xAB, 0xCD, 0xEF, 0x76, 0x00 }; 29b0d17251Schristos 30b0d17251Schristos static struct testdata tbl_testdata[] = { 31b0d17251Schristos { 32b0d17251Schristos "AB:CD:EF:F1", 33b0d17251Schristos test_1, sizeof(test_1), 34b0d17251Schristos ':', 35b0d17251Schristos }, 36b0d17251Schristos { 37b0d17251Schristos "AB:CD:EF:76:00", 38b0d17251Schristos test_2, sizeof(test_2), 39b0d17251Schristos ':', 40b0d17251Schristos }, 41b0d17251Schristos { 42b0d17251Schristos "AB_CD_EF_F1", 43b0d17251Schristos test_1, sizeof(test_1), 44b0d17251Schristos '_', 45b0d17251Schristos }, 46b0d17251Schristos { 47b0d17251Schristos "AB_CD_EF_76_00", 48b0d17251Schristos test_2, sizeof(test_2), 49b0d17251Schristos '_', 50b0d17251Schristos }, 51b0d17251Schristos { 52b0d17251Schristos "ABCDEFF1", 53b0d17251Schristos test_1, sizeof(test_1), 54b0d17251Schristos '\0', 55b0d17251Schristos }, 56b0d17251Schristos { 57b0d17251Schristos "ABCDEF7600", 58b0d17251Schristos test_2, sizeof(test_2), 59b0d17251Schristos '\0', 60b0d17251Schristos }, 61b0d17251Schristos }; 62b0d17251Schristos 63b0d17251Schristos static int test_hexstr_sep_to_from(int test_index) 64b0d17251Schristos { 65b0d17251Schristos int ret = 0; 66b0d17251Schristos long len = 0; 67b0d17251Schristos unsigned char *buf = NULL; 68b0d17251Schristos char *out = NULL; 69b0d17251Schristos struct testdata *test = &tbl_testdata[test_index]; 70b0d17251Schristos 71b0d17251Schristos if (!TEST_ptr(buf = ossl_hexstr2buf_sep(test->in, &len, test->sep)) 72b0d17251Schristos || !TEST_mem_eq(buf, len, test->expected, test->expected_len) 73b0d17251Schristos || !TEST_ptr(out = ossl_buf2hexstr_sep(buf, len, test->sep)) 74b0d17251Schristos || !TEST_str_eq(out, test->in)) 75b0d17251Schristos goto err; 76b0d17251Schristos 77b0d17251Schristos ret = 1; 78b0d17251Schristos err: 79b0d17251Schristos OPENSSL_free(buf); 80b0d17251Schristos OPENSSL_free(out); 81b0d17251Schristos return ret; 82b0d17251Schristos } 83b0d17251Schristos 84b0d17251Schristos static int test_hexstr_to_from(int test_index) 85b0d17251Schristos { 86b0d17251Schristos int ret = 0; 87b0d17251Schristos long len = 0; 88b0d17251Schristos unsigned char *buf = NULL; 89b0d17251Schristos char *out = NULL; 90b0d17251Schristos struct testdata *test = &tbl_testdata[test_index]; 91b0d17251Schristos 92b0d17251Schristos if (test->sep != '_') { 93b0d17251Schristos if (!TEST_ptr(buf = OPENSSL_hexstr2buf(test->in, &len)) 94b0d17251Schristos || !TEST_mem_eq(buf, len, test->expected, test->expected_len) 95b0d17251Schristos || !TEST_ptr(out = OPENSSL_buf2hexstr(buf, len))) 96b0d17251Schristos goto err; 97b0d17251Schristos if (test->sep == ':') { 98b0d17251Schristos if (!TEST_str_eq(out, test->in)) 99b0d17251Schristos goto err; 100b0d17251Schristos } else if (!TEST_str_ne(out, test->in)) { 101b0d17251Schristos goto err; 102b0d17251Schristos } 103b0d17251Schristos } else { 104b0d17251Schristos if (!TEST_ptr_null(buf = OPENSSL_hexstr2buf(test->in, &len))) 105b0d17251Schristos goto err; 106b0d17251Schristos } 107b0d17251Schristos ret = 1; 108b0d17251Schristos err: 109b0d17251Schristos OPENSSL_free(buf); 110b0d17251Schristos OPENSSL_free(out); 111b0d17251Schristos return ret; 112b0d17251Schristos } 113b0d17251Schristos 114b0d17251Schristos static int test_hexstr_ex_to_from(int test_index) 115b0d17251Schristos { 116b0d17251Schristos size_t len = 0; 117b0d17251Schristos char out[64]; 118b0d17251Schristos unsigned char buf[64]; 119b0d17251Schristos struct testdata *test = &tbl_testdata[test_index]; 120b0d17251Schristos 121b0d17251Schristos return TEST_true(OPENSSL_hexstr2buf_ex(buf, sizeof(buf), &len, test->in, ':')) 122b0d17251Schristos && TEST_mem_eq(buf, len, test->expected, test->expected_len) 123*97e3c585Schristos && TEST_false(OPENSSL_buf2hexstr_ex(out, 3 * len - 1, NULL, buf, len, 124*97e3c585Schristos ':')) 125b0d17251Schristos && TEST_true(OPENSSL_buf2hexstr_ex(out, sizeof(out), NULL, buf, len, 126b0d17251Schristos ':')) 127*97e3c585Schristos && TEST_str_eq(out, test->in) 128*97e3c585Schristos && TEST_true(OPENSSL_buf2hexstr_ex(out, sizeof(out), NULL, buf, 0, 129*97e3c585Schristos ':')) 130*97e3c585Schristos && TEST_size_t_eq(strlen(out), 0); 131b0d17251Schristos } 132b0d17251Schristos 133b0d17251Schristos int setup_tests(void) 134b0d17251Schristos { 135b0d17251Schristos ADD_ALL_TESTS(test_hexstr_sep_to_from, OSSL_NELEM(tbl_testdata)); 136b0d17251Schristos ADD_ALL_TESTS(test_hexstr_to_from, OSSL_NELEM(tbl_testdata)); 137b0d17251Schristos ADD_ALL_TESTS(test_hexstr_ex_to_from, 2); 138b0d17251Schristos return 1; 139b0d17251Schristos } 140