blob: 913f5e3870a79c593eae039fc72504daa1e202f3 [file] [log] [blame]
Gilles Peskine881447d2022-12-08 15:24:52 +01001/**
2 * \file bignum_helpers.c
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
Dave Rodgman16799db2023-11-02 19:47:20 +000010 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine881447d2022-12-08 15:24:52 +010011 */
12
13#define MBEDTLS_ALLOW_PRIVATE_ACCESS
14#include <test/bignum_helpers.h>
15
16#if defined(MBEDTLS_BIGNUM_C)
17
18#include <stdlib.h>
19#include <string.h>
20
21#include <mbedtls/bignum.h>
22#include <bignum_core.h>
23#include <bignum_mod.h>
24#include <bignum_mod_raw.h>
25
26#include <test/helpers.h>
27#include <test/macros.h>
28
Gilles Peskine449bd832023-01-11 14:50:10 +010029int mbedtls_test_read_mpi_core(mbedtls_mpi_uint **pX, size_t *plimbs,
30 const char *input)
Gilles Peskine881447d2022-12-08 15:24:52 +010031{
32 /* Sanity check */
Gilles Peskine449bd832023-01-11 14:50:10 +010033 if (*pX != NULL) {
34 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
35 }
Gilles Peskine881447d2022-12-08 15:24:52 +010036
Gilles Peskine449bd832023-01-11 14:50:10 +010037 size_t hex_len = strlen(input);
38 size_t byte_len = (hex_len + 1) / 2;
39 *plimbs = CHARS_TO_LIMBS(byte_len);
Gilles Peskine881447d2022-12-08 15:24:52 +010040
41 /* A core bignum is not allowed to be empty. Forbid it as test data,
42 * this way static analyzers have a chance of knowing we don't expect
43 * the bignum functions to support empty inputs. */
Gilles Peskine449bd832023-01-11 14:50:10 +010044 if (*plimbs == 0) {
45 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine881447d2022-12-08 15:24:52 +010046 }
Gilles Peskine449bd832023-01-11 14:50:10 +010047
48 *pX = mbedtls_calloc(*plimbs, sizeof(**pX));
49 if (*pX == NULL) {
50 return MBEDTLS_ERR_MPI_ALLOC_FAILED;
51 }
52
53 unsigned char *byte_start = (unsigned char *) *pX;
54 if (byte_len % sizeof(mbedtls_mpi_uint) != 0) {
55 byte_start += sizeof(mbedtls_mpi_uint) - byte_len % sizeof(mbedtls_mpi_uint);
56 }
57 if ((hex_len & 1) != 0) {
Gilles Peskine881447d2022-12-08 15:24:52 +010058 /* mbedtls_test_unhexify wants an even number of hex digits */
Gilles Peskine449bd832023-01-11 14:50:10 +010059 TEST_ASSERT(mbedtls_test_ascii2uc(*input, byte_start) == 0);
Gilles Peskine881447d2022-12-08 15:24:52 +010060 ++byte_start;
61 ++input;
62 --byte_len;
63 }
Gilles Peskine449bd832023-01-11 14:50:10 +010064 TEST_ASSERT(mbedtls_test_unhexify(byte_start,
65 byte_len,
66 input,
67 &byte_len) == 0);
Gilles Peskine881447d2022-12-08 15:24:52 +010068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 mbedtls_mpi_core_bigendian_to_host(*pX, *plimbs);
70 return 0;
Gilles Peskine881447d2022-12-08 15:24:52 +010071
72exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010073 mbedtls_free(*pX);
74 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Gilles Peskine881447d2022-12-08 15:24:52 +010075}
76
Janos Follathf2334b72023-08-17 12:24:46 +000077#if defined(MBEDTLS_ECP_WITH_MPI_UINT)
Gilles Peskine449bd832023-01-11 14:50:10 +010078int mbedtls_test_read_mpi_modulus(mbedtls_mpi_mod_modulus *N,
79 const char *s,
80 mbedtls_mpi_mod_rep_selector int_rep)
Gilles Peskine195f9982022-12-07 22:59:54 +010081{
82 mbedtls_mpi_uint *p = NULL;
83 size_t limbs = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010084 if (N->limbs != 0) {
85 return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
86 }
87 int ret = mbedtls_test_read_mpi_core(&p, &limbs, s);
88 if (ret != 0) {
89 return ret;
90 }
Minos Galanakis88e16df2023-05-09 14:11:43 +010091
92 switch (int_rep) {
93 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
94 ret = mbedtls_mpi_mod_modulus_setup(N, p, limbs);
95 break;
96 case MBEDTLS_MPI_MOD_REP_OPT_RED:
97 ret = mbedtls_mpi_mod_optred_modulus_setup(N, p, limbs, NULL);
98 break;
99 default:
100 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
101 break;
102 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 if (ret != 0) {
104 mbedtls_free(p);
105 }
106 return ret;
Gilles Peskine195f9982022-12-07 22:59:54 +0100107}
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109void mbedtls_test_mpi_mod_modulus_free_with_limbs(mbedtls_mpi_mod_modulus *N)
Gilles Peskined008abb2022-12-08 19:50:29 +0100110{
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_free((mbedtls_mpi_uint *) N->p);
112 mbedtls_mpi_mod_modulus_free(N);
Gilles Peskined008abb2022-12-08 19:50:29 +0100113}
Janos Follathf2334b72023-08-17 12:24:46 +0000114#endif /* MBEDTLS_ECP_WITH_MPI_UINT */
Gilles Peskined008abb2022-12-08 19:50:29 +0100115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116int mbedtls_test_read_mpi(mbedtls_mpi *X, const char *s)
Gilles Peskine881447d2022-12-08 15:24:52 +0100117{
118 int negative = 0;
119 /* Always set the sign bit to -1 if the input has a minus sign, even for 0.
120 * This creates an invalid representation, which mbedtls_mpi_read_string()
121 * avoids but we want to be able to create that in test data. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (s[0] == '-') {
Gilles Peskine881447d2022-12-08 15:24:52 +0100123 ++s;
124 negative = 1;
125 }
126 /* mbedtls_mpi_read_string() currently retains leading zeros.
127 * It always allocates at least one limb for the value 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (s[0] == 0) {
129 mbedtls_mpi_free(X);
130 return 0;
Gilles Peskine881447d2022-12-08 15:24:52 +0100131 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 int ret = mbedtls_mpi_read_string(X, 16, s);
133 if (ret != 0) {
134 return ret;
135 }
136 if (negative) {
137 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
Paul Elliottc7a1e992023-11-03 18:44:57 +0000138 mbedtls_test_increment_case_uses_negative_0();
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 }
Gilles Peskine881447d2022-12-08 15:24:52 +0100140 X->s = -1;
141 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return 0;
Gilles Peskine881447d2022-12-08 15:24:52 +0100143}
144
145#endif /* MBEDTLS_BIGNUM_C */