Skip to content
0.2.0-dev Download ZIP
164 lines ·6.9 KB Raw
1/**
2 * \file bignum_internal.h
3 *
4 * \brief Internal-only bignum public-key cryptosystem API.
5 *
6 * This file declares bignum-related functions that are to be used
7 * only from within the Mbed TLS library itself.
8 *
9 */
10/*
11 * Copyright The Mbed TLS Contributors
12 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
13 */
14#ifndef TF_PSA_CRYPTO_BIGNUM_INTERNAL_H
15#define TF_PSA_CRYPTO_BIGNUM_INTERNAL_H
16
17#include "constant_time_internal.h"
18
19/**
20 * \brief Perform a modular exponentiation: X = A^E mod N
21 *
22 * \warning This function is not constant time with respect to \p E (the exponent).
23 *
24 * \param X The destination MPI. This must point to an initialized MPI.
25 * This must not alias E or N.
26 * \param A The base of the exponentiation.
27 * This must point to an initialized MPI.
28 * \param E The exponent MPI. This must point to an initialized MPI.
29 * \param N The base for the modular reduction. This must point to an
30 * initialized MPI.
31 * \param prec_RR A helper MPI depending solely on \p N which can be used to
32 * speed-up multiple modular exponentiations for the same value
33 * of \p N. This may be \c NULL. If it is not \c NULL, it must
34 * point to an initialized MPI. If it hasn't been used after
35 * the call to mbedtls_mpi_init(), this function will compute
36 * the helper value and store it in \p prec_RR for reuse on
37 * subsequent calls to this function. Otherwise, the function
38 * will assume that \p prec_RR holds the helper value set by a
39 * previous call to mbedtls_mpi_exp_mod(), and reuse it.
40 *
41 * \return \c 0 if successful.
42 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
43 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \c N is negative or
44 * even, or if \c E is negative.
45 * \return Another negative error code on different kinds of failures.
46 *
47 */
48int mbedtls_mpi_exp_mod_unsafe(mbedtls_mpi *X, const mbedtls_mpi *A,
49 const mbedtls_mpi *E, const mbedtls_mpi *N,
50 mbedtls_mpi *prec_RR);
51
52/**
53 * \brief A wrapper around a constant time function to compute
54 * GCD(A, N) and/or A^-1 mod N if it exists.
55 *
56 * \warning Requires N to be odd, and 0 <= A <= N. Additionally, if
57 * I != NULL, requires N > 1.
58 * The wrapper part of this function is not constant time.
59 *
60 * \note A and N must not alias each other.
61 * When I == NULL (computing only the GCD), G can alias A or N.
62 * When I != NULL (computing the modular inverse), G or I can
63 * alias A, but neither of them can alias N (the modulus).
64 *
65 * \param[out] G The GCD of \p A and \p N.
66 * This may be NULL, to only compute I.
67 * \param[out] I The inverse of \p A modulo \p N if it exists (that is,
68 * if \p G above is 1 on exit), in the range [1, \p N);
69 * indeterminate otherwise.
70 * This may be NULL, to only compute G.
71 * \param[in] A The 1st operand of GCD and number to invert.
72 * This value must be less than or equal to \p N.
73 * \param[in] N The 2nd operand of GCD and modulus for inversion.
74 * Must be odd or the results are indeterminate.
75 *
76 * \return \c 0 if successful.
77 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
78 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if preconditions were not
79 * met.
80 */
81int mbedtls_mpi_gcd_modinv_odd(mbedtls_mpi *G,
82 mbedtls_mpi *I,
83 const mbedtls_mpi *A,
84 const mbedtls_mpi *N);
85
86/**
87 * \brief Modular inverse: X = A^-1 mod N with N odd
88 *
89 * \param[out] X The inverse of \p A modulo \p N in the range [1, \p N)
90 * on success; indeterminate otherwise.
91 * \param[in] A The number to invert.
92 * \param[in] N The modulus. Must be odd and greater than 1.
93 *
94 * \return \c 0 if successful.
95 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
96 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if preconditions were not
97 * met.
98 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A is not invertible mod N.
99 */
100int mbedtls_mpi_inv_mod_odd(mbedtls_mpi *X,
101 const mbedtls_mpi *A,
102 const mbedtls_mpi *N);
103
104/**
105 * \brief Modular inverse: X = A^-1 mod N with N even,
106 * A odd and 1 < A < N.
107 *
108 * \param[out] X The inverse of \p A modulo \p N in the range [1, \p N)
109 * on success; indeterminate otherwise.
110 * \param[in] A The number to invert. Must be odd, greated than 1
111 * and less than \p N.
112 * \param[in] N The modulus. Must be even and greater than 1.
113 *
114 * \return \c 0 if successful.
115 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
116 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if preconditions were not
117 * met.
118 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A is not invertible mod N.
119 */
120int mbedtls_mpi_inv_mod_even_in_range(mbedtls_mpi *X,
121 mbedtls_mpi const *A,
122 mbedtls_mpi const *N);
123
124/** Choose between two mbedtls_mpi_uint values.
125 *
126 * Functionally equivalent to:
127 *
128 * condition ? if1 : if0.
129 *
130 * \param condition Condition to test.
131 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
132 * \param if0 Value to use if \p condition == MBEDTLS_CT_FALSE.
133 *
134 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise \c if0.
135 */
136static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if(mbedtls_ct_condition_t condition,
137 mbedtls_mpi_uint if1,
138 mbedtls_mpi_uint if0)
139{
140 return (mbedtls_mpi_uint) mbedtls_ct_if(condition,
141 (mbedtls_ct_uint_t) if1,
142 (mbedtls_ct_uint_t) if0);
143}
144
145/** Choose between an mbedtls_mpi_uint value and 0.
146 *
147 * Functionally equivalent to:
148 *
149 * condition ? if1 : 0.
150 *
151 * Functionally equivalent to mbedtls_ct_mpi_uint_if(condition, if1, 0) but
152 * results in smaller code size.
153 *
154 * \param condition Condition to test.
155 * \param if1 Value to use if \p condition == MBEDTLS_CT_TRUE.
156 *
157 * \return \c if1 if \p condition == MBEDTLS_CT_TRUE, otherwise 0.
158 */
159static inline mbedtls_mpi_uint mbedtls_ct_mpi_uint_if_else_0(
160 mbedtls_ct_condition_t condition, mbedtls_mpi_uint if1)
161{
162 return (mbedtls_mpi_uint) (condition & if1);
163}
164#endif /* TF_PSA_CRYPTO_BIGNUM_INTERNAL_H */