blob: ab3c86a12f5d011c4386edc4cf8171390c00035b [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>
Gilles Peskine195f9982022-12-07 22:59:54 +010033#include <bignum_mod.h>
Gilles Peskine881447d2022-12-08 15:24:52 +010034
35/** Allocate and populate a core MPI from a test case argument.
36 *
37 * This function allocates exactly as many limbs as necessary to fit
38 * the length of the input. In other words, it preserves leading zeros.
39 *
40 * The limb array is allocated with mbedtls_calloc() and must later be
41 * freed with mbedtls_free().
42 *
43 * \param[in,out] pX The address where a pointer to the allocated limb
44 * array will be stored.
45 * \c *pX must be null on entry.
46 * On exit, \c *pX is null on error or if the number
47 * of limbs is 0.
48 * \param[out] plimbs The address where the number of limbs will be stored.
49 * \param[in] input The test argument to read.
50 * It is interpreted as a hexadecimal representation
51 * of a non-negative integer.
52 *
53 * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
54 */
55int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs,
56 const char *input );
57
Gilles Peskine195f9982022-12-07 22:59:54 +010058/** Read a modulus from a hexadecimal string.
59 *
60 * This function allocates exactly as many limbs as necessary to fit
61 * the length of the input. In other words, it preserves leading zeros.
62 *
63 * The limb array is allocated with mbedtls_calloc() and must later be
64 * freed with mbedtls_free().
65 *
66 * \param[in,out] N A modulus structure. It must be initialized, but
67 * not set up.
68 * \param[in] s The null-terminated hexadecimal string to read from.
69 * \param int_rep The desired representation of residues.
70 *
71 * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
72 */
73int mbedtls_test_read_mpi_modulus( mbedtls_mpi_mod_modulus *N,
74 const char *s,
75 mbedtls_mpi_mod_rep_selector int_rep );
76
Gilles Peskine881447d2022-12-08 15:24:52 +010077/** Read an MPI from a hexadecimal string.
78 *
79 * Like mbedtls_mpi_read_string(), but with tighter guarantees around
80 * edge cases.
81 *
82 * - This function guarantees that if \p s begins with '-' then the sign
83 * bit of the result will be negative, even if the value is 0.
84 * When this function encounters such a "negative 0", it
85 * increments #mbedtls_test_case_uses_negative_0.
86 * - The size of the result is exactly the minimum number of limbs needed
87 * to fit the digits in the input. In particular, this function constructs
88 * a bignum with 0 limbs for an empty string, and a bignum with leading 0
89 * limbs if the string has sufficiently many leading 0 digits.
90 * This is important so that the "0 (null)" and "0 (1 limb)" and
91 * "leading zeros" test cases do what they claim.
92 *
93 * \param[out] X The MPI object to populate. It must be initialized.
94 * \param[in] s The null-terminated hexadecimal string to read from.
95 *
96 * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
97 */
98int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s );
99
100/** Nonzero if the current test case had an input parsed with
101 * mbedtls_test_read_mpi() that is a negative 0 (`"-"`, `"-0"`, `"-00"`, etc.,
102 * constructing a result with the sign bit set to -1 and the value being
103 * all-limbs-0, which is not a valid representation in #mbedtls_mpi but is
104 * tested for robustness).
105 */
106extern unsigned mbedtls_test_case_uses_negative_0;
107
108#endif /* MBEDTLS_BIGNUM_C */
109
110#endif /* TEST_BIGNUM_HELPERS_H */