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 | |
| 41 | int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs, |
| 42 | const char *input ) |
| 43 | { |
| 44 | /* Sanity check */ |
| 45 | if( *pX != NULL ) |
| 46 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 47 | |
| 48 | size_t hex_len = strlen( input ); |
| 49 | size_t byte_len = ( hex_len + 1 ) / 2; |
| 50 | *plimbs = CHARS_TO_LIMBS( byte_len ); |
| 51 | |
| 52 | /* A core bignum is not allowed to be empty. Forbid it as test data, |
| 53 | * this way static analyzers have a chance of knowing we don't expect |
| 54 | * the bignum functions to support empty inputs. */ |
| 55 | if( *plimbs == 0 ) |
| 56 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 57 | |
| 58 | *pX = mbedtls_calloc( *plimbs, sizeof( **pX ) ); |
| 59 | if( *pX == NULL ) |
| 60 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 61 | |
| 62 | unsigned char *byte_start = ( unsigned char * ) *pX; |
| 63 | if( byte_len % sizeof( mbedtls_mpi_uint ) != 0 ) |
| 64 | { |
| 65 | byte_start += sizeof( mbedtls_mpi_uint ) - byte_len % sizeof( mbedtls_mpi_uint ); |
| 66 | } |
| 67 | if( ( hex_len & 1 ) != 0 ) |
| 68 | { |
| 69 | /* mbedtls_test_unhexify wants an even number of hex digits */ |
| 70 | TEST_ASSERT( mbedtls_test_ascii2uc( *input, byte_start ) == 0 ); |
| 71 | ++byte_start; |
| 72 | ++input; |
| 73 | --byte_len; |
| 74 | } |
| 75 | TEST_ASSERT( mbedtls_test_unhexify( byte_start, |
| 76 | byte_len, |
| 77 | input, |
| 78 | &byte_len ) == 0 ); |
| 79 | |
| 80 | mbedtls_mpi_core_bigendian_to_host( *pX, *plimbs ); |
| 81 | return( 0 ); |
| 82 | |
| 83 | exit: |
| 84 | mbedtls_free( *pX ); |
| 85 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 86 | } |
| 87 | |
| 88 | int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s ) |
| 89 | { |
| 90 | int negative = 0; |
| 91 | /* Always set the sign bit to -1 if the input has a minus sign, even for 0. |
| 92 | * This creates an invalid representation, which mbedtls_mpi_read_string() |
| 93 | * avoids but we want to be able to create that in test data. */ |
| 94 | if( s[0] == '-' ) |
| 95 | { |
| 96 | ++s; |
| 97 | negative = 1; |
| 98 | } |
| 99 | /* mbedtls_mpi_read_string() currently retains leading zeros. |
| 100 | * It always allocates at least one limb for the value 0. */ |
| 101 | if( s[0] == 0 ) |
| 102 | { |
| 103 | mbedtls_mpi_free( X ); |
| 104 | return( 0 ); |
| 105 | } |
| 106 | int ret = mbedtls_mpi_read_string( X, 16, s ); |
| 107 | if( ret != 0 ) |
| 108 | return( ret ); |
| 109 | if( negative ) |
| 110 | { |
| 111 | if( mbedtls_mpi_cmp_int( X, 0 ) == 0 ) |
| 112 | ++mbedtls_test_case_uses_negative_0; |
| 113 | X->s = -1; |
| 114 | } |
| 115 | return( 0 ); |
| 116 | } |
| 117 | |
| 118 | #endif /* MBEDTLS_BIGNUM_C */ |
| 119 | |