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