0.2.0-dev Download ZIP
185 lines ·7.5 KB Raw
| 1 | /** |
| 2 | * \file rsa_alt_helpers.h |
| 3 | * |
| 4 | * \brief Context-independent RSA helper functions |
| 5 | * |
| 6 | * Please note: The below explanation is historical and is no longer relevant |
| 7 | * due to there being no compelling reason to keep these functions separate |
| 8 | * since the RSA alt interface was removed. A future refactoring will address |
| 9 | * this which is tracked by the following issue: |
| 10 | * https://github.com/Mbed-TLS/TF-PSA-Crypto/issues/105. |
| 11 | * |
| 12 | * This module declares some RSA-related helper functions useful when |
| 13 | * implementing the RSA interface. These functions are provided in a separate |
| 14 | * compilation unit in order to make it easy for designers of alternative RSA |
| 15 | * implementations to use them in their own code, as it is conceived that the |
| 16 | * functionality they provide will be necessary for most complete |
| 17 | * implementations. |
| 18 | * |
| 19 | * End-users of Mbed TLS who are not providing their own alternative RSA |
| 20 | * implementations should not use these functions directly, and should instead |
| 21 | * use only the functions declared in rsa.h. |
| 22 | * |
| 23 | * The interface provided by this module will be maintained through LTS (Long |
| 24 | * Term Support) branches of Mbed TLS, but may otherwise be subject to change, |
| 25 | * and must be considered an internal interface of the library. |
| 26 | * |
| 27 | * There are two classes of helper functions: |
| 28 | * |
| 29 | * (1) Parameter-generating helpers. These are: |
| 30 | * - mbedtls_rsa_deduce_private_exponent |
| 31 | * - mbedtls_rsa_deduce_crt |
| 32 | * Each of these functions takes a set of core RSA parameters and |
| 33 | * generates some other, or CRT related parameters. |
| 34 | * |
| 35 | * (2) Parameter-checking helpers. These are: |
| 36 | * - mbedtls_rsa_validate_params |
| 37 | * - mbedtls_rsa_validate_crt |
| 38 | * They take a set of core or CRT related RSA parameters and check their |
| 39 | * validity. |
| 40 | * |
| 41 | */ |
| 42 | /* |
| 43 | * Copyright The Mbed TLS Contributors |
| 44 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 45 | */ |
| 46 | #ifndef TF_PSA_CRYPTO_RSA_ALT_HELPERS_H |
| 47 | #define TF_PSA_CRYPTO_RSA_ALT_HELPERS_H |
| 48 | |
| 49 | #include "tf-psa-crypto/build_info.h" |
| 50 | |
| 51 | #include "mbedtls/private/bignum.h" |
| 52 | |
| 53 | #ifdef __cplusplus |
| 54 | extern "C" { |
| 55 | #endif |
| 56 | |
| 57 | /** |
| 58 | * \brief Compute RSA private exponent from |
| 59 | * prime moduli and public key. |
| 60 | * |
| 61 | * \note This is a 'static' helper function not operating on |
| 62 | * an RSA context. Alternative implementations need not |
| 63 | * overwrite it. |
| 64 | * |
| 65 | * \param P First prime factor of RSA modulus |
| 66 | * \param Q Second prime factor of RSA modulus |
| 67 | * \param E RSA public exponent |
| 68 | * \param D Pointer to MPI holding the private exponent on success, |
| 69 | * i.e. the modular inverse of E modulo LCM(P-1,Q-1). |
| 70 | * |
| 71 | * \return \c 0 if successful. |
| 72 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. |
| 73 | * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if E is not coprime to P-1 |
| 74 | * and Q-1, that is, if GCD( E, (P-1)*(Q-1) ) != 1. |
| 75 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if inputs are otherwise |
| 76 | * invalid. |
| 77 | * |
| 78 | * \note This function does not check whether P and Q are primes. |
| 79 | * |
| 80 | */ |
| 81 | int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P, |
| 82 | mbedtls_mpi const *Q, |
| 83 | mbedtls_mpi const *E, |
| 84 | mbedtls_mpi *D); |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * \brief Generate RSA-CRT parameters |
| 89 | * |
| 90 | * \note This is a 'static' helper function not operating on |
| 91 | * an RSA context. Alternative implementations need not |
| 92 | * overwrite it. |
| 93 | * |
| 94 | * \param P First prime factor of N |
| 95 | * \param Q Second prime factor of N |
| 96 | * \param D RSA private exponent |
| 97 | * \param DP Output variable for D modulo P-1 |
| 98 | * \param DQ Output variable for D modulo Q-1 |
| 99 | * \param QP Output variable for the modular inverse of Q modulo P. |
| 100 | * |
| 101 | * \return 0 on success, non-zero error code otherwise. |
| 102 | * |
| 103 | * \note This function does not check whether P, Q are |
| 104 | * prime and whether D is a valid private exponent. |
| 105 | * |
| 106 | */ |
| 107 | int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q, |
| 108 | const mbedtls_mpi *D, mbedtls_mpi *DP, |
| 109 | mbedtls_mpi *DQ, mbedtls_mpi *QP); |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * \brief Check validity of core RSA parameters |
| 114 | * |
| 115 | * \note This is a 'static' helper function not operating on |
| 116 | * an RSA context. Alternative implementations need not |
| 117 | * overwrite it. |
| 118 | * |
| 119 | * \param N RSA modulus N = PQ |
| 120 | * \param P First prime factor of N |
| 121 | * \param Q Second prime factor of N |
| 122 | * \param D RSA private exponent |
| 123 | * \param E RSA public exponent |
| 124 | * \param f_rng PRNG to be used for primality check, or NULL |
| 125 | * \param p_rng PRNG context for f_rng, or NULL |
| 126 | * |
| 127 | * \return |
| 128 | * - 0 if the following conditions are satisfied |
| 129 | * if all relevant parameters are provided: |
| 130 | * - P prime if f_rng != NULL (%) |
| 131 | * - Q prime if f_rng != NULL (%) |
| 132 | * - 1 < N = P * Q |
| 133 | * - 1 < D, E < N |
| 134 | * - D and E are modular inverses modulo P-1 and Q-1 |
| 135 | * (%) This is only done if MBEDTLS_GENPRIME is defined. |
| 136 | * - A non-zero error code otherwise. |
| 137 | * |
| 138 | * \note The function can be used with a restricted set of arguments |
| 139 | * to perform specific checks only. E.g., calling it with |
| 140 | * (-,P,-,-,-) and a PRNG amounts to a primality check for P. |
| 141 | */ |
| 142 | int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P, |
| 143 | const mbedtls_mpi *Q, const mbedtls_mpi *D, |
| 144 | const mbedtls_mpi *E, |
| 145 | int (*f_rng)(void *, unsigned char *, size_t), |
| 146 | void *p_rng); |
| 147 | |
| 148 | /** |
| 149 | * \brief Check validity of RSA CRT parameters |
| 150 | * |
| 151 | * \note This is a 'static' helper function not operating on |
| 152 | * an RSA context. Alternative implementations need not |
| 153 | * overwrite it. |
| 154 | * |
| 155 | * \param P First prime factor of RSA modulus |
| 156 | * \param Q Second prime factor of RSA modulus |
| 157 | * \param D RSA private exponent |
| 158 | * \param DP MPI to check for D modulo P-1 |
| 159 | * \param DQ MPI to check for D modulo P-1 |
| 160 | * \param QP MPI to check for the modular inverse of Q modulo P. |
| 161 | * |
| 162 | * \return |
| 163 | * - 0 if the following conditions are satisfied: |
| 164 | * - D = DP mod P-1 if P, D, DP != NULL |
| 165 | * - Q = DQ mod P-1 if P, D, DQ != NULL |
| 166 | * - QP = Q^-1 mod P if P, Q, QP != NULL |
| 167 | * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed, |
| 168 | * potentially including \c MBEDTLS_ERR_MPI_XXX if some |
| 169 | * MPI calculations failed. |
| 170 | * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient |
| 171 | * data was provided to check DP, DQ or QP. |
| 172 | * |
| 173 | * \note The function can be used with a restricted set of arguments |
| 174 | * to perform specific checks only. E.g., calling it with the |
| 175 | * parameters (P, -, D, DP, -, -) will check DP = D mod P-1. |
| 176 | */ |
| 177 | int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q, |
| 178 | const mbedtls_mpi *D, const mbedtls_mpi *DP, |
| 179 | const mbedtls_mpi *DQ, const mbedtls_mpi *QP); |
| 180 | |
| 181 | #ifdef __cplusplus |
| 182 | } |
| 183 | #endif |
| 184 | |
| 185 | #endif /* TF_PSA_CRYPTO_RSA_ALT_HELPERS_H */ |