blob: c4248544ab3f59c3cf39350820736320b8215c5d [file] [log] [blame]
Gabor Mezeif049dbf2022-07-18 23:02:33 +02001/**
2 * Internal bignum functions
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
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#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
33#include <stdio.h>
34#include <stdlib.h>
Gabor Mezeie66b1d42022-08-02 11:49:59 +020035#define mbedtls_printf printf
36#define mbedtls_calloc calloc
37#define mbedtls_free free
Janos Follathba5c1392022-07-19 13:42:07 +010038#endif
39
Janos Follathd1baedb2022-08-09 13:44:53 +010040#include "bignum_core.h"
41#include "bignum_mod.h"
42#include "bignum_mod_raw.h"
43#include "constant_time_internal.h"
44
Gabor Mezeif049dbf2022-07-18 23:02:33 +020045int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
46 mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +010047 mbedtls_mpi_uint *p,
48 size_t pn )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020049{
Janos Follath8b718b52022-07-25 11:31:02 +010050 if( p == NULL || m == NULL || r == NULL )
51 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
52
53 if( pn < m->n || !mbedtls_mpi_core_lt_ct( m->p, p, pn ) )
Gabor Mezeif049dbf2022-07-18 23:02:33 +020054 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
55
56 r->n = m->n;
Janos Follath8b718b52022-07-25 11:31:02 +010057 r->p = p;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020058
59 return( 0 );
60}
61
Gabor Mezei37b06362022-08-02 17:22:18 +020062void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
63{
64 if ( r == NULL )
65 return;
66
67 r->n = 0;
68 r->p = NULL;
69}
70
Gabor Mezeif049dbf2022-07-18 23:02:33 +020071void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
72{
73 if ( m == NULL )
74 return;
75
Janos Follath281ccda2022-07-19 13:14:36 +010076 m->p = NULL;
77 m->n = 0;
78 m->plen = 0;
79 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
80 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020081}
82
83void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
84{
85 if ( m == NULL )
86 return;
87
Janos Follathba5c1392022-07-19 13:42:07 +010088 switch( m->int_rep )
89 {
90 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
Janos Follath296ea662022-08-11 14:58:29 +010091 mbedtls_free( m->rep.mont );
92 break;
Janos Follathba5c1392022-07-19 13:42:07 +010093 case MBEDTLS_MPI_MOD_REP_OPT_RED:
Janos Follath296ea662022-08-11 14:58:29 +010094 mbedtls_free( m->rep.ored );
95 break;
96 case MBEDTLS_MPI_MOD_REP_INVALID:
Janos Follathba5c1392022-07-19 13:42:07 +010097 break;
98 }
99
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200100 m->p = NULL;
101 m->n = 0;
102 m->plen = 0;
Janos Follath281ccda2022-07-19 13:14:36 +0100103 m->ext_rep = MBEDTLS_MPI_MOD_EXT_REP_INVALID;
104 m->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200105}
106
107int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Gabor Mezei535f36d2022-08-02 11:50:44 +0200108 mbedtls_mpi_uint *p,
109 size_t pn,
Janos Follath296ea662022-08-11 14:58:29 +0100110 mbedtls_mpi_mod_ext_rep ext_rep,
111 mbedtls_mpi_mod_rep_selector int_rep )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200112{
Janos Follathba5c1392022-07-19 13:42:07 +0100113 int ret = 0;
114
Gabor Mezei535f36d2022-08-02 11:50:44 +0200115 if ( p == NULL || m == NULL )
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200116 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
117
Gabor Mezei535f36d2022-08-02 11:50:44 +0200118 m->p = p;
119 m->n = pn;
120 m->plen = mbedtls_mpi_core_bitlen( p, pn );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200121
Janos Follathba5c1392022-07-19 13:42:07 +0100122 switch( ext_rep )
123 {
124 case MBEDTLS_MPI_MOD_EXT_REP_LE:
125 case MBEDTLS_MPI_MOD_EXT_REP_BE:
Janos Follath296ea662022-08-11 14:58:29 +0100126 m->ext_rep = ext_rep;
127 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100128 default:
129 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
130 goto exit;
131 }
132
133 switch( int_rep )
134 {
135 case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
136 m->int_rep = int_rep;
Janos Follath296ea662022-08-11 14:58:29 +0100137 m->rep.mont = NULL;
138 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100139 case MBEDTLS_MPI_MOD_REP_OPT_RED:
140 m->int_rep = int_rep;
Janos Follath296ea662022-08-11 14:58:29 +0100141 m->rep.ored = NULL;
142 break;
Janos Follathba5c1392022-07-19 13:42:07 +0100143 default:
144 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
145 goto exit;
146 }
147
148exit:
149
150 if( ret != 0 )
151 {
152 mbedtls_mpi_mod_modulus_free( m );
153 }
154
155 return( ret );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200156}
157
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200158#endif /* MBEDTLS_BIGNUM_C */