blob: 9d28bc7e45f24735c6df8a35a2d8dd9039178a8e [file] [log] [blame]
Gabor Mezeif049dbf2022-07-18 23:02:33 +02001/**
Janos Follath63184682022-08-11 17:42:59 +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#ifndef MBEDTLS_BIGNUM_MOD_H
21#define MBEDTLS_BIGNUM_MOD_H
22
23#include "common.h"
24
25#if defined(MBEDTLS_BIGNUM_C)
26#include "mbedtls/bignum.h"
27#endif
28
Janos Follath296ea662022-08-11 14:58:29 +010029/* Skip 1 as it is slightly easier to accidentally pass to functions. */
30typedef enum
31{
32 MBEDTLS_MPI_MOD_REP_INVALID = 0,
33 MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
34 MBEDTLS_MPI_MOD_REP_OPT_RED
35} mbedtls_mpi_mod_rep_selector;
36
37/* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to
38 * make it easier to catch when they are accidentally swapped. */
39typedef enum
40{
41 MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0,
42 MBEDTLS_MPI_MOD_EXT_REP_LE = 8,
43 MBEDTLS_MPI_MOD_EXT_REP_BE
44} mbedtls_mpi_mod_ext_rep;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020045
46typedef struct
47{
Gabor Mezeif049dbf2022-07-18 23:02:33 +020048 mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +020049 size_t limbs;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020050} mbedtls_mpi_mod_residue;
51
Gabor Mezei89e31462022-08-12 15:36:56 +020052typedef void *mbedtls_mpi_mont_struct;
53typedef void *mbedtls_mpi_opt_red_struct;
Gabor Mezeif049dbf2022-07-18 23:02:33 +020054
55typedef struct {
Janos Follathed5c8d32022-08-15 11:50:22 +010056 const mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +020057 size_t limbs; // number of limbs
58 size_t bits; // bitlen of p
Janos Follath296ea662022-08-11 14:58:29 +010059 mbedtls_mpi_mod_ext_rep ext_rep; // signals external representation (eg. byte order)
60 mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
Gabor Mezeif049dbf2022-07-18 23:02:33 +020061 union rep
62 {
63 mbedtls_mpi_mont_struct mont;
64 mbedtls_mpi_opt_red_struct ored;
65 } rep;
66} mbedtls_mpi_mod_modulus;
67
Gabor Mezei37b06362022-08-02 17:22:18 +020068/** Setup a residue structure.
69 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +010070 * \param[out] r The address of residue to setup. The size is determined by
71 * \p m.
72 * (In particular, it must have at least as many limbs as the
73 * modulus \p m.)
74 * \param[in] m The address of the modulus related to \p r.
75 * \param[in] p The address of the limb array storing the value of \p r.
76 * The memory pointed to by \p p will be used by \p r and must
77 * not be modified in any way until after
78 * mbedtls_mpi_mod_residue_release() is called.
Janos Follathb7a88ec2022-08-19 12:24:40 +010079 * \param p_limbs The number of limbs of \p p.
Gabor Mezei37b06362022-08-02 17:22:18 +020080 *
81 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +010082 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
83 * limbs in \p m or if \p p is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +020084 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +020085int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010086 const mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +010087 mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +010088 size_t p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +020089
Gabor Mezei37b06362022-08-02 17:22:18 +020090/** Unbind elements of a residue structure.
91 *
Janos Follathdae11472022-08-08 11:50:02 +010092 * This function removes the reference to the limb array that was passed to
93 * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again.
94 *
95 * This function invalidates \p r and it must not be used until after
96 * mbedtls_mpi_mod_residue_setup() is called on it again.
97 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +010098 * \param[out] r The address of residue to release.
Gabor Mezei37b06362022-08-02 17:22:18 +020099 */
100void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r );
101
102/** Initialize a modulus structure.
103 *
Janos Follatha95f2042022-08-19 12:09:17 +0100104 * \param[out] m The address of the modulus structure to initialize.
Gabor Mezei37b06362022-08-02 17:22:18 +0200105 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200106void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
107
Janos Follath63184682022-08-11 17:42:59 +0100108/** Setup a modulus structure.
Gabor Mezei37b06362022-08-02 17:22:18 +0200109 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100110 * \param[out] m The address of the modulus structure to populate.
111 * \param[in] p The address of the limb array storing the value of \p m.
112 * The memory pointed to by \p p will be used by \p m and must
113 * not be modified in any way until after
Janos Follathdae11472022-08-08 11:50:02 +0100114 * mbedtls_mpi_mod_modulus_free() is called.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100115 * \param p_limbs The number of limbs of \p p.
Janos Follathdae11472022-08-08 11:50:02 +0100116 * \param ext_rep The external representation to be used for residues
117 * associated with \p m (see #mbedtls_mpi_mod_ext_rep).
118 * \param int_rep The internal representation to be used for residues
119 * associated with \p m (see #mbedtls_mpi_mod_rep_selector).
Gabor Mezei37b06362022-08-02 17:22:18 +0200120 *
121 * \return \c 0 if successful.
Janos Follath56a10f92022-08-11 15:19:00 +0100122 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep or \p int_rep is
123 * invalid.
Gabor Mezei37b06362022-08-02 17:22:18 +0200124 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200125int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100126 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100127 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100128 mbedtls_mpi_mod_ext_rep ext_rep,
129 mbedtls_mpi_mod_rep_selector int_rep );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200130
Janos Follathdae11472022-08-08 11:50:02 +0100131/** Free elements of a modulus structure.
132 *
133 * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup().
134 *
135 * \warning This function does not free the limb array passed to
136 * mbedtls_mpi_mod_modulus_setup() only removes the reference to it,
137 * making it safe to free or to use it again.
Gabor Mezei37b06362022-08-02 17:22:18 +0200138 *
Janos Follatha95f2042022-08-19 12:09:17 +0100139 * \param[in,out] m The address of the modulus structure to free.
Gabor Mezei37b06362022-08-02 17:22:18 +0200140 */
141void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m );
142
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200143#endif /* MBEDTLS_BIGNUM_MOD_H */