bignum_helpers.h 4.6 KB

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