blob: 7cf1b012c54c080e63d4b87610375f0aba9c55b7 [file] [log] [blame]
Gabor Mezeif049dbf2022-07-18 23:02:33 +02001/**
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Modular bignum functions
Gabor Mezeif049dbf2022-07-18 23:02:33 +02003 *
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
Gabor Mezeib9030702022-07-18 23:09:45 +020024#include <string.h>
25
26#include "mbedtls/platform_util.h"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020027#include "mbedtls/error.h"
28#include "mbedtls/bignum.h"
Gabor Mezeif049dbf2022-07-18 23:02:33 +020029
Janos Follathba5c1392022-07-19 13:42:07 +010030#include "mbedtls/platform.h"
Janos Follathba5c1392022-07-19 13:42:07 +010031
Janos Follathd1baedb2022-08-09 13:44:53 +010032#include "bignum_core.h"
33#include "bignum_mod.h"
34#include "bignum_mod_raw.h"
35#include "constant_time_internal.h"
36
Gabor Mezeif049dbf2022-07-18 23:02:33 +020037int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010038 const mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +010039 mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +010040 size_t p_limbs )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020041{
Janos Follathb7a88ec2022-08-19 12:24:40 +010042 if( p_limbs < m->limbs || !mbedtls_mpi_core_lt_ct( m->p, p, p_limbs ) )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020043 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
44
Gabor Mezeifd65e822022-08-12 18:09:12 +020045 r->limbs = m->limbs;
Janos Follath8b718b52022-07-25 11:31:02 +010046 r->p = p;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020047
48 return( 0 );
49}
50
Gabor Mezei37b06362022-08-02 17:22:18 +020051void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
52{
53 if ( r == NULL )
54 return;
55
Gabor Mezeifd65e822022-08-12 18:09:12 +020056 r->limbs = 0;
Gabor Mezei37b06362022-08-02 17:22:18 +020057 r->p = NULL;
58}
59
Gabor Mezeif049dbf2022-07-18 23:02:33 +020060void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
61{
62 if ( m == NULL )
63 return;
64
Janos Follath281ccda2022-07-19 13:14:36 +010065 m->p = NULL;
Gabor Mezeifd65e822022-08-12 18:09:12 +020066 m->limbs = 0;
67 m->bits = 0;
Janos Follath281ccda2022-07-19 13:14:36 +010068 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
69 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020070}
71
72void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
73{
74 if ( m == NULL )
75 return;
76
Janos Follathba5c1392022-07-19 13:42:07 +010077 switch( m->int_rep )
78 {
79 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Minos Galanakis760f5d62022-08-11 12:21:09 +010080 m->rep.mont.rr = NULL;
81 m->rep.mont.mm = 0; break;
Janos Follathba5c1392022-07-19 13:42:07 +010082 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Janos Follath296ea662022-08-11 14:58:29 +010083 mbedtls_free( m->rep.ored );
84 break;
85 case MBEDTLS_MPI_MOD_REP_INVALID:
Janos Follathba5c1392022-07-19 13:42:07 +010086 break;
87 }
88
Gabor Mezeif049dbf2022-07-18 23:02:33 +020089 m->p = NULL;
Gabor Mezeifd65e822022-08-12 18:09:12 +020090 m->limbs = 0;
91 m->bits = 0;
Janos Follath281ccda2022-07-19 13:14:36 +010092 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
93 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020094}
95
96int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +010097 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +010098 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +010099 mbedtls_mpi_mod_ext_rep ext_rep,
100 mbedtls_mpi_mod_rep_selector int_rep )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200101{
Janos Follathba5c1392022-07-19 13:42:07 +0100102 int ret = 0;
103
Gabor Mezei535f36d2022-08-02 11:50:44 +0200104 m->p = p;
Janos Follathb7a88ec2022-08-19 12:24:40 +0100105 m->limbs = p_limbs;
106 m->bits = mbedtls_mpi_core_bitlen( p, p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200107
Janos Follathba5c1392022-07-19 13:42:07 +0100108 switch( ext_rep )
109 {
110 case MBEDTLS_MPI_MOD_EXT_REP_LE:
111 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follath296ea662022-08-11 14:58:29 +0100112 m->ext_rep = ext_rep;
113 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100114 default:
115 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
116 goto exit;
117 }
118
119 switch( int_rep )
120 {
121 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
122 m->int_rep = int_rep;
Minos Galanakis760f5d62022-08-11 12:21:09 +0100123 m->rep.mont.rr = NULL;
124 m->rep.mont.mm = 0; break;
Janos Follathba5c1392022-07-19 13:42:07 +0100125 case MBEDTLS_MPI_MOD_REP_OPT_RED:
126 m->int_rep = int_rep;
Janos Follath296ea662022-08-11 14:58:29 +0100127 m->rep.ored = NULL;
128 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100129 default:
130 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
131 goto exit;
132 }
133
134exit:
135
136 if( ret != 0 )
137 {
138 mbedtls_mpi_mod_modulus_free( m );
139 }
140
141 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200142}
143
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200144#endif /* MBEDTLS_BIGNUM_C */