1a8e1175bSopenharmony_ci/** 2a8e1175bSopenharmony_ci * \file bignum_helpers.h 3a8e1175bSopenharmony_ci * 4a8e1175bSopenharmony_ci * \brief This file contains the prototypes of helper functions for 5a8e1175bSopenharmony_ci * bignum-related testing. 6a8e1175bSopenharmony_ci */ 7a8e1175bSopenharmony_ci 8a8e1175bSopenharmony_ci/* 9a8e1175bSopenharmony_ci * Copyright The Mbed TLS Contributors 10a8e1175bSopenharmony_ci * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 11a8e1175bSopenharmony_ci */ 12a8e1175bSopenharmony_ci 13a8e1175bSopenharmony_ci#ifndef TEST_BIGNUM_HELPERS_H 14a8e1175bSopenharmony_ci#define TEST_BIGNUM_HELPERS_H 15a8e1175bSopenharmony_ci 16a8e1175bSopenharmony_ci#include <mbedtls/build_info.h> 17a8e1175bSopenharmony_ci 18a8e1175bSopenharmony_ci#if defined(MBEDTLS_BIGNUM_C) 19a8e1175bSopenharmony_ci 20a8e1175bSopenharmony_ci#include <mbedtls/bignum.h> 21a8e1175bSopenharmony_ci#include <bignum_mod.h> 22a8e1175bSopenharmony_ci 23a8e1175bSopenharmony_ci/** Allocate and populate a core MPI from a test case argument. 24a8e1175bSopenharmony_ci * 25a8e1175bSopenharmony_ci * This function allocates exactly as many limbs as necessary to fit 26a8e1175bSopenharmony_ci * the length of the input. In other words, it preserves leading zeros. 27a8e1175bSopenharmony_ci * 28a8e1175bSopenharmony_ci * The limb array is allocated with mbedtls_calloc() and must later be 29a8e1175bSopenharmony_ci * freed with mbedtls_free(). 30a8e1175bSopenharmony_ci * 31a8e1175bSopenharmony_ci * \param[in,out] pX The address where a pointer to the allocated limb 32a8e1175bSopenharmony_ci * array will be stored. 33a8e1175bSopenharmony_ci * \c *pX must be null on entry. 34a8e1175bSopenharmony_ci * On exit, \c *pX is null on error or if the number 35a8e1175bSopenharmony_ci * of limbs is 0. 36a8e1175bSopenharmony_ci * \param[out] plimbs The address where the number of limbs will be stored. 37a8e1175bSopenharmony_ci * \param[in] input The test argument to read. 38a8e1175bSopenharmony_ci * It is interpreted as a hexadecimal representation 39a8e1175bSopenharmony_ci * of a non-negative integer. 40a8e1175bSopenharmony_ci * 41a8e1175bSopenharmony_ci * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise. 42a8e1175bSopenharmony_ci */ 43a8e1175bSopenharmony_ciint mbedtls_test_read_mpi_core(mbedtls_mpi_uint **pX, size_t *plimbs, 44a8e1175bSopenharmony_ci const char *input); 45a8e1175bSopenharmony_ci 46a8e1175bSopenharmony_ci/** Read a modulus from a hexadecimal string. 47a8e1175bSopenharmony_ci * 48a8e1175bSopenharmony_ci * This function allocates exactly as many limbs as necessary to fit 49a8e1175bSopenharmony_ci * the length of the input. In other words, it preserves leading zeros. 50a8e1175bSopenharmony_ci * 51a8e1175bSopenharmony_ci * The limb array is allocated with mbedtls_calloc() and must later be 52a8e1175bSopenharmony_ci * freed with mbedtls_free(). You can do that by calling 53a8e1175bSopenharmony_ci * mbedtls_test_mpi_mod_modulus_free_with_limbs(). 54a8e1175bSopenharmony_ci * 55a8e1175bSopenharmony_ci * \param[in,out] N A modulus structure. It must be initialized, but 56a8e1175bSopenharmony_ci * not set up. 57a8e1175bSopenharmony_ci * \param[in] s The null-terminated hexadecimal string to read from. 58a8e1175bSopenharmony_ci * \param int_rep The desired representation of residues. 59a8e1175bSopenharmony_ci * 60a8e1175bSopenharmony_ci * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise. 61a8e1175bSopenharmony_ci */ 62a8e1175bSopenharmony_ciint mbedtls_test_read_mpi_modulus(mbedtls_mpi_mod_modulus *N, 63a8e1175bSopenharmony_ci const char *s, 64a8e1175bSopenharmony_ci mbedtls_mpi_mod_rep_selector int_rep); 65a8e1175bSopenharmony_ci 66a8e1175bSopenharmony_ci/** Free a modulus and its limbs. 67a8e1175bSopenharmony_ci * 68a8e1175bSopenharmony_ci * \param[in] N A modulus structure such that there is no other 69a8e1175bSopenharmony_ci * reference to `N->p`. 70a8e1175bSopenharmony_ci */ 71a8e1175bSopenharmony_civoid mbedtls_test_mpi_mod_modulus_free_with_limbs(mbedtls_mpi_mod_modulus *N); 72a8e1175bSopenharmony_ci 73a8e1175bSopenharmony_ci/** Read an MPI from a hexadecimal string. 74a8e1175bSopenharmony_ci * 75a8e1175bSopenharmony_ci * Like mbedtls_mpi_read_string(), but with tighter guarantees around 76a8e1175bSopenharmony_ci * edge cases. 77a8e1175bSopenharmony_ci * 78a8e1175bSopenharmony_ci * - This function guarantees that if \p s begins with '-' then the sign 79a8e1175bSopenharmony_ci * bit of the result will be negative, even if the value is 0. 80a8e1175bSopenharmony_ci * When this function encounters such a "negative 0", it calls 81a8e1175bSopenharmony_ci * mbedtls_test_increment_case_uses_negative_0(). 82a8e1175bSopenharmony_ci * - The size of the result is exactly the minimum number of limbs needed to fit 83a8e1175bSopenharmony_ci * the digits in the input. In particular, this function constructs a bignum 84a8e1175bSopenharmony_ci * with 0 limbs for an empty string, and a bignum with leading 0 limbs if the 85a8e1175bSopenharmony_ci * string has sufficiently many leading 0 digits. This is important so that 86a8e1175bSopenharmony_ci * the "0 (null)" and "0 (1 limb)" and "leading zeros" test cases do what they 87a8e1175bSopenharmony_ci * claim. 88a8e1175bSopenharmony_ci * 89a8e1175bSopenharmony_ci * \param[out] X The MPI object to populate. It must be initialized. 90a8e1175bSopenharmony_ci * \param[in] s The null-terminated hexadecimal string to read from. 91a8e1175bSopenharmony_ci * 92a8e1175bSopenharmony_ci * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise. 93a8e1175bSopenharmony_ci */ 94a8e1175bSopenharmony_ciint mbedtls_test_read_mpi(mbedtls_mpi *X, const char *s); 95a8e1175bSopenharmony_ci 96a8e1175bSopenharmony_ci#endif /* MBEDTLS_BIGNUM_C */ 97a8e1175bSopenharmony_ci 98a8e1175bSopenharmony_ci#endif /* TEST_BIGNUM_HELPERS_H */ 99