Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 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 |
| 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 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 26 | #include <test/bignum_helpers.h> |
| 27 | |
| 28 | #if defined(MBEDTLS_BIGNUM_C) |
| 29 | |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | #include <mbedtls/bignum.h> |
| 34 | #include <bignum_core.h> |
| 35 | #include <bignum_mod.h> |
| 36 | #include <bignum_mod_raw.h> |
| 37 | |
| 38 | #include <test/helpers.h> |
| 39 | #include <test/macros.h> |
| 40 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 41 | int mbedtls_test_read_mpi_core(mbedtls_mpi_uint **pX, size_t *plimbs, |
| 42 | const char *input) |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 43 | { |
| 44 | /* Sanity check */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | if (*pX != NULL) { |
| 46 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 47 | } |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 48 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | size_t hex_len = strlen(input); |
| 50 | size_t byte_len = (hex_len + 1) / 2; |
| 51 | *plimbs = CHARS_TO_LIMBS(byte_len); |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 52 | |
| 53 | /* A core bignum is not allowed to be empty. Forbid it as test data, |
| 54 | * this way static analyzers have a chance of knowing we don't expect |
| 55 | * the bignum functions to support empty inputs. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | if (*plimbs == 0) { |
| 57 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 58 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | |
| 60 | *pX = mbedtls_calloc(*plimbs, sizeof(**pX)); |
| 61 | if (*pX == NULL) { |
| 62 | return MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 63 | } |
| 64 | |
| 65 | unsigned char *byte_start = (unsigned char *) *pX; |
| 66 | if (byte_len % sizeof(mbedtls_mpi_uint) != 0) { |
| 67 | byte_start += sizeof(mbedtls_mpi_uint) - byte_len % sizeof(mbedtls_mpi_uint); |
| 68 | } |
| 69 | if ((hex_len & 1) != 0) { |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 70 | /* mbedtls_test_unhexify wants an even number of hex digits */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | TEST_ASSERT(mbedtls_test_ascii2uc(*input, byte_start) == 0); |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 72 | ++byte_start; |
| 73 | ++input; |
| 74 | --byte_len; |
| 75 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | TEST_ASSERT(mbedtls_test_unhexify(byte_start, |
| 77 | byte_len, |
| 78 | input, |
| 79 | &byte_len) == 0); |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 80 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | mbedtls_mpi_core_bigendian_to_host(*pX, *plimbs); |
| 82 | return 0; |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 83 | |
| 84 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | mbedtls_free(*pX); |
| 86 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Janos Follath | f2334b7 | 2023-08-17 12:24:46 +0000 | [diff] [blame] | 89 | #if defined(MBEDTLS_ECP_WITH_MPI_UINT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | int mbedtls_test_read_mpi_modulus(mbedtls_mpi_mod_modulus *N, |
| 91 | const char *s, |
| 92 | mbedtls_mpi_mod_rep_selector int_rep) |
Gilles Peskine | 195f998 | 2022-12-07 22:59:54 +0100 | [diff] [blame] | 93 | { |
| 94 | mbedtls_mpi_uint *p = NULL; |
| 95 | size_t limbs = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | if (N->limbs != 0) { |
| 97 | return MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 98 | } |
| 99 | int ret = mbedtls_test_read_mpi_core(&p, &limbs, s); |
| 100 | if (ret != 0) { |
| 101 | return ret; |
| 102 | } |
Minos Galanakis | 88e16df | 2023-05-09 14:11:43 +0100 | [diff] [blame] | 103 | |
| 104 | switch (int_rep) { |
| 105 | case MBEDTLS_MPI_MOD_REP_MONTGOMERY: |
| 106 | ret = mbedtls_mpi_mod_modulus_setup(N, p, limbs); |
| 107 | break; |
| 108 | case MBEDTLS_MPI_MOD_REP_OPT_RED: |
| 109 | ret = mbedtls_mpi_mod_optred_modulus_setup(N, p, limbs, NULL); |
| 110 | break; |
| 111 | default: |
| 112 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 113 | break; |
| 114 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | if (ret != 0) { |
| 116 | mbedtls_free(p); |
| 117 | } |
| 118 | return ret; |
Gilles Peskine | 195f998 | 2022-12-07 22:59:54 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | void mbedtls_test_mpi_mod_modulus_free_with_limbs(mbedtls_mpi_mod_modulus *N) |
Gilles Peskine | d008abb | 2022-12-08 19:50:29 +0100 | [diff] [blame] | 122 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | mbedtls_free((mbedtls_mpi_uint *) N->p); |
| 124 | mbedtls_mpi_mod_modulus_free(N); |
Gilles Peskine | d008abb | 2022-12-08 19:50:29 +0100 | [diff] [blame] | 125 | } |
Janos Follath | f2334b7 | 2023-08-17 12:24:46 +0000 | [diff] [blame] | 126 | #endif /* MBEDTLS_ECP_WITH_MPI_UINT */ |
Gilles Peskine | d008abb | 2022-12-08 19:50:29 +0100 | [diff] [blame] | 127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | int mbedtls_test_read_mpi(mbedtls_mpi *X, const char *s) |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 129 | { |
| 130 | int negative = 0; |
| 131 | /* Always set the sign bit to -1 if the input has a minus sign, even for 0. |
| 132 | * This creates an invalid representation, which mbedtls_mpi_read_string() |
| 133 | * avoids but we want to be able to create that in test data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 134 | if (s[0] == '-') { |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 135 | ++s; |
| 136 | negative = 1; |
| 137 | } |
| 138 | /* mbedtls_mpi_read_string() currently retains leading zeros. |
| 139 | * It always allocates at least one limb for the value 0. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 140 | if (s[0] == 0) { |
| 141 | mbedtls_mpi_free(X); |
| 142 | return 0; |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 143 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | int ret = mbedtls_mpi_read_string(X, 16, s); |
| 145 | if (ret != 0) { |
| 146 | return ret; |
| 147 | } |
| 148 | if (negative) { |
| 149 | if (mbedtls_mpi_cmp_int(X, 0) == 0) { |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 150 | ++mbedtls_test_case_uses_negative_0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | } |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 152 | X->s = -1; |
| 153 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | return 0; |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | #endif /* MBEDTLS_BIGNUM_C */ |