blob: c4a3eb8dca7ec5c8cf8706c120577d5160636543 [file] [log] [blame]
Janos Follath0ded6312022-08-09 13:34:54 +01001/*
2 * Multi-precision integer library
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
22#if defined(MBEDTLS_BIGNUM_C)
23
24#include <string.h>
25
26#include "mbedtls/error.h"
27#include "mbedtls/platform_util.h"
28
29#if defined(MBEDTLS_PLATFORM_C)
30#include "mbedtls/platform.h"
31#else
32#include <stdio.h>
33#include <stdlib.h>
34#define mbedtls_printf printf
35#define mbedtls_calloc calloc
36#define mbedtls_free free
37#endif
38
39#include "bignum_core.h"
40#include "bignum_mod_raw.h"
41#include "bignum_mod.h"
42#include "constant_time_internal.h"
43
44int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
45 mbedtls_mpi_mod_modulus *m,
46 unsigned char *buf,
47 size_t buflen )
48{
49 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
50
Janos Follath296ea662022-08-11 14:58:29 +010051 switch( m->ext_rep )
52 {
53 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Gabor Mezeifd65e822022-08-12 18:09:12 +020054 ret = mbedtls_mpi_core_read_le( X, m->limbs, buf, buflen );
Janos Follath296ea662022-08-11 14:58:29 +010055 break;
56 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Gabor Mezeifd65e822022-08-12 18:09:12 +020057 ret = mbedtls_mpi_core_read_be( X, m->limbs, buf, buflen );
Janos Follath296ea662022-08-11 14:58:29 +010058 break;
59 default:
60 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
61 }
Janos Follath0ded6312022-08-09 13:34:54 +010062
63 if( ret != 0 )
64 goto cleanup;
65
Gabor Mezeifd65e822022-08-12 18:09:12 +020066 if( !mbedtls_mpi_core_lt_ct( X, m->p, m->limbs ) )
Janos Follath0ded6312022-08-09 13:34:54 +010067 {
68 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
69 goto cleanup;
70 }
71
72cleanup:
73
74 return( ret );
75}
76
77int mbedtls_mpi_mod_raw_write( mbedtls_mpi_uint *X,
78 mbedtls_mpi_mod_modulus *m,
79 unsigned char *buf,
80 size_t buflen )
81{
Janos Follath296ea662022-08-11 14:58:29 +010082 switch( m->ext_rep )
83 {
84 case MBEDTLS_MPI_MOD_EXT_REP_LE:
Gabor Mezeifd65e822022-08-12 18:09:12 +020085 return( mbedtls_mpi_core_write_le( X, m->limbs, buf, buflen ) );
Janos Follath296ea662022-08-11 14:58:29 +010086 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Gabor Mezeifd65e822022-08-12 18:09:12 +020087 return( mbedtls_mpi_core_write_be( X, m->limbs, buf, buflen ) );
Janos Follath296ea662022-08-11 14:58:29 +010088 default:
89 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
90 }
Janos Follath0ded6312022-08-09 13:34:54 +010091}
92
93#endif /* MBEDTLS_BIGNUM_C */