blob: 982a9ac5c92fd72b2c542851458fbaddf3c11399 [file] [log] [blame]
Gabor Mezeif049dbf2022-07-18 23:02:33 +02001/**
Janos Follath63184682022-08-11 17:42:59 +01002 * Modular bignum functions
Gilles Peskine7f887bd2022-09-27 13:12:30 +02003 */
4
5/*
Gabor Mezeif049dbf2022-07-18 23:02:33 +02006 * Copyright The Mbed TLS Contributors
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22#ifndef MBEDTLS_BIGNUM_MOD_H
23#define MBEDTLS_BIGNUM_MOD_H
24
25#include "common.h"
26
27#if defined(MBEDTLS_BIGNUM_C)
28#include "mbedtls/bignum.h"
29#endif
30
Janos Follath296ea662022-08-11 14:58:29 +010031/* Skip 1 as it is slightly easier to accidentally pass to functions. */
32typedef enum
33{
34 MBEDTLS_MPI_MOD_REP_INVALID = 0,
35 MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
36 MBEDTLS_MPI_MOD_REP_OPT_RED
37} mbedtls_mpi_mod_rep_selector;
38
39/* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to
40 * make it easier to catch when they are accidentally swapped. */
41typedef enum
42{
43 MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0,
44 MBEDTLS_MPI_MOD_EXT_REP_LE = 8,
45 MBEDTLS_MPI_MOD_EXT_REP_BE
46} mbedtls_mpi_mod_ext_rep;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020047
48typedef struct
49{
Gabor Mezeif049dbf2022-07-18 23:02:33 +020050 mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +020051 size_t limbs;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020052} mbedtls_mpi_mod_residue;
53
Gabor Mezei89e31462022-08-12 15:36:56 +020054typedef void *mbedtls_mpi_mont_struct;
55typedef void *mbedtls_mpi_opt_red_struct;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020056
57typedef struct {
Janos Follathed5c8d32022-08-15 11:50:22 +010058 const mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +020059 size_t limbs; // number of limbs
60 size_t bits; // bitlen of p
Janos Follath296ea662022-08-11 14:58:29 +010061 mbedtls_mpi_mod_ext_rep ext_rep; // signals external representation (eg. byte order)
62 mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
Gabor Mezeif049dbf2022-07-18 23:02:33 +020063 union rep
64 {
65 mbedtls_mpi_mont_struct mont;
66 mbedtls_mpi_opt_red_struct ored;
67 } rep;
68} mbedtls_mpi_mod_modulus;
69
Gabor Mezei37b06362022-08-02 17:22:18 +020070/** Setup a residue structure.
71 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +010072 * \param[out] r The address of residue to setup. The size is determined by
73 * \p m.
74 * (In particular, it must have at least as many limbs as the
75 * modulus \p m.)
76 * \param[in] m The address of the modulus related to \p r.
77 * \param[in] p The address of the limb array storing the value of \p r.
78 * The memory pointed to by \p p will be used by \p r and must
79 * not be modified in any way until after
80 * mbedtls_mpi_mod_residue_release() is called.
Janos Follathb7a88ec2022-08-19 12:24:40 +010081 * \param p_limbs The number of limbs of \p p.
Gabor Mezei37b06362022-08-02 17:22:18 +020082 *
83 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +010084 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
85 * limbs in \p m or if \p p is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +020086 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +020087int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010088 const mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +010089 mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +010090 size_t p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +020091
Gabor Mezei37b06362022-08-02 17:22:18 +020092/** Unbind elements of a residue structure.
93 *
Janos Follathdae11472022-08-08 11:50:02 +010094 * This function removes the reference to the limb array that was passed to
95 * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again.
96 *
97 * This function invalidates \p r and it must not be used until after
98 * mbedtls_mpi_mod_residue_setup() is called on it again.
99 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100100 * \param[out] r The address of residue to release.
Gabor Mezei37b06362022-08-02 17:22:18 +0200101 */
102void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r );
103
104/** Initialize a modulus structure.
105 *
Janos Follatha95f2042022-08-19 12:09:17 +0100106 * \param[out] m The address of the modulus structure to initialize.
Gabor Mezei37b06362022-08-02 17:22:18 +0200107 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200108void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
109
Janos Follath63184682022-08-11 17:42:59 +0100110/** Setup a modulus structure.
Gabor Mezei37b06362022-08-02 17:22:18 +0200111 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100112 * \param[out] m The address of the modulus structure to populate.
113 * \param[in] p The address of the limb array storing the value of \p m.
114 * The memory pointed to by \p p will be used by \p m and must
115 * not be modified in any way until after
Janos Follathdae11472022-08-08 11:50:02 +0100116 * mbedtls_mpi_mod_modulus_free() is called.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100117 * \param p_limbs The number of limbs of \p p.
Janos Follathdae11472022-08-08 11:50:02 +0100118 * \param ext_rep The external representation to be used for residues
119 * associated with \p m (see #mbedtls_mpi_mod_ext_rep).
120 * \param int_rep The internal representation to be used for residues
121 * associated with \p m (see #mbedtls_mpi_mod_rep_selector).
Gabor Mezei37b06362022-08-02 17:22:18 +0200122 *
123 * \return \c 0 if successful.
Janos Follath56a10f92022-08-11 15:19:00 +0100124 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep or \p int_rep is
125 * invalid.
Gabor Mezei37b06362022-08-02 17:22:18 +0200126 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200127int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100128 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100129 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100130 mbedtls_mpi_mod_ext_rep ext_rep,
131 mbedtls_mpi_mod_rep_selector int_rep );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200132
Janos Follathdae11472022-08-08 11:50:02 +0100133/** Free elements of a modulus structure.
134 *
135 * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup().
136 *
137 * \warning This function does not free the limb array passed to
138 * mbedtls_mpi_mod_modulus_setup() only removes the reference to it,
139 * making it safe to free or to use it again.
Gabor Mezei37b06362022-08-02 17:22:18 +0200140 *
Janos Follatha95f2042022-08-19 12:09:17 +0100141 * \param[in,out] m The address of the modulus structure to free.
Gabor Mezei37b06362022-08-02 17:22:18 +0200142 */
143void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m );
144
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200145#endif /* MBEDTLS_BIGNUM_MOD_H */