1 /**
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
10  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
11  */
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 
mbedtls_test_read_mpi_core(mbedtls_mpi_uint ** pX,size_t * plimbs,const char * input)29 int mbedtls_test_read_mpi_core(mbedtls_mpi_uint **pX, size_t *plimbs,
30                                const char *input)
31 {
32     /* Sanity check */
33     if (*pX != NULL) {
34         return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
35     }
36 
37     size_t hex_len = strlen(input);
38     size_t byte_len = (hex_len + 1) / 2;
39     *plimbs = CHARS_TO_LIMBS(byte_len);
40 
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. */
44     if (*plimbs == 0) {
45         return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
46     }
47 
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) {
58         /* mbedtls_test_unhexify wants an even number of hex digits */
59         TEST_ASSERT(mbedtls_test_ascii2uc(*input, byte_start) == 0);
60         ++byte_start;
61         ++input;
62         --byte_len;
63     }
64     TEST_ASSERT(mbedtls_test_unhexify(byte_start,
65                                       byte_len,
66                                       input,
67                                       &byte_len) == 0);
68 
69     mbedtls_mpi_core_bigendian_to_host(*pX, *plimbs);
70     return 0;
71 
72 exit:
73     mbedtls_free(*pX);
74     return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
75 }
76 
77 #if defined(MBEDTLS_ECP_WITH_MPI_UINT)
mbedtls_test_read_mpi_modulus(mbedtls_mpi_mod_modulus * N,const char * s,mbedtls_mpi_mod_rep_selector int_rep)78 int mbedtls_test_read_mpi_modulus(mbedtls_mpi_mod_modulus *N,
79                                   const char *s,
80                                   mbedtls_mpi_mod_rep_selector int_rep)
81 {
82     mbedtls_mpi_uint *p = NULL;
83     size_t limbs = 0;
84     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     }
91 
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     }
103     if (ret != 0) {
104         mbedtls_free(p);
105     }
106     return ret;
107 }
108 
mbedtls_test_mpi_mod_modulus_free_with_limbs(mbedtls_mpi_mod_modulus * N)109 void mbedtls_test_mpi_mod_modulus_free_with_limbs(mbedtls_mpi_mod_modulus *N)
110 {
111     mbedtls_free((mbedtls_mpi_uint *) N->p);
112     mbedtls_mpi_mod_modulus_free(N);
113 }
114 #endif /* MBEDTLS_ECP_WITH_MPI_UINT */
115 
mbedtls_test_read_mpi(mbedtls_mpi * X,const char * s)116 int mbedtls_test_read_mpi(mbedtls_mpi *X, const char *s)
117 {
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. */
122     if (s[0] == '-') {
123         ++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. */
128     if (s[0] == 0) {
129         mbedtls_mpi_free(X);
130         return 0;
131     }
132     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) {
138             mbedtls_test_increment_case_uses_negative_0();
139         }
140         X->s = -1;
141     }
142     return 0;
143 }
144 
145 #endif /* MBEDTLS_BIGNUM_C */
146