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