blob: 22ce7f76fc0828f7a0a02e994dd8439be223d648 [file] [log] [blame]
Gilles Peskine881447d2022-12-08 15:24:52 +01001/**
2 * \file bignum_helpers.h
3 *
4 * \brief This file contains the prototypes of helper functions for
5 * bignum-related testing.
6 */
7
8/*
9 * Copyright The Mbed TLS Contributors
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25#ifndef TEST_BIGNUM_HELPERS_H
26#define TEST_BIGNUM_HELPERS_H
27
28#include <mbedtls/build_info.h>
29
30#if defined(MBEDTLS_BIGNUM_C)
31
32#include <mbedtls/bignum.h>
33
34/** Allocate and populate a core MPI from a test case argument.
35 *
36 * This function allocates exactly as many limbs as necessary to fit
37 * the length of the input. In other words, it preserves leading zeros.
38 *
39 * The limb array is allocated with mbedtls_calloc() and must later be
40 * freed with mbedtls_free().
41 *
42 * \param[in,out] pX The address where a pointer to the allocated limb
43 * array will be stored.
44 * \c *pX must be null on entry.
45 * On exit, \c *pX is null on error or if the number
46 * of limbs is 0.
47 * \param[out] plimbs The address where the number of limbs will be stored.
48 * \param[in] input The test argument to read.
49 * It is interpreted as a hexadecimal representation
50 * of a non-negative integer.
51 *
52 * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
53 */
54int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs,
55 const char *input );
56
57/** Read an MPI from a hexadecimal string.
58 *
59 * Like mbedtls_mpi_read_string(), but with tighter guarantees around
60 * edge cases.
61 *
62 * - This function guarantees that if \p s begins with '-' then the sign
63 * bit of the result will be negative, even if the value is 0.
64 * When this function encounters such a "negative 0", it
65 * increments #mbedtls_test_case_uses_negative_0.
66 * - The size of the result is exactly the minimum number of limbs needed
67 * to fit the digits in the input. In particular, this function constructs
68 * a bignum with 0 limbs for an empty string, and a bignum with leading 0
69 * limbs if the string has sufficiently many leading 0 digits.
70 * This is important so that the "0 (null)" and "0 (1 limb)" and
71 * "leading zeros" test cases do what they claim.
72 *
73 * \param[out] X The MPI object to populate. It must be initialized.
74 * \param[in] s The null-terminated hexadecimal string to read from.
75 *
76 * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
77 */
78int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s );
79
80/** Nonzero if the current test case had an input parsed with
81 * mbedtls_test_read_mpi() that is a negative 0 (`"-"`, `"-0"`, `"-00"`, etc.,
82 * constructing a result with the sign bit set to -1 and the value being
83 * all-limbs-0, which is not a valid representation in #mbedtls_mpi but is
84 * tested for robustness).
85 */
86extern unsigned mbedtls_test_case_uses_negative_0;
87
88#endif /* MBEDTLS_BIGNUM_C */
89
90#endif /* TEST_BIGNUM_HELPERS_H */