0.2.0-dev Download ZIP
172 lines ·8.0 KB Raw
| 1 | /** |
| 2 | * \file rsa_internal.h |
| 3 | * |
| 4 | * \brief Internal-only RSA public-key cryptosystem API. |
| 5 | * |
| 6 | * This file declares RSA-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_RSA_INTERNAL_H |
| 15 | #define TF_PSA_CRYPTO_RSA_INTERNAL_H |
| 16 | |
| 17 | #include "mbedtls/private/rsa.h" |
| 18 | #include "mbedtls/asn1.h" |
| 19 | |
| 20 | /** |
| 21 | * \brief Parse a PKCS#1 (ASN.1) encoded private RSA key. |
| 22 | * |
| 23 | * \param rsa The RSA context where parsed data will be stored. |
| 24 | * \param key The buffer that contains the key. |
| 25 | * \param keylen The length of the key buffer in bytes. |
| 26 | * |
| 27 | * \return 0 on success. |
| 28 | * \return MBEDTLS_ERR_ASN1_xxx in case of ASN.1 parsing errors. |
| 29 | * \return MBEDTLS_ERR_RSA_xxx in case of RSA internal failures while |
| 30 | * parsing data. |
| 31 | * \return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if validity checks on the |
| 32 | * provided key fail. |
| 33 | */ |
| 34 | int mbedtls_rsa_parse_key(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen); |
| 35 | |
| 36 | /** |
| 37 | * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key. |
| 38 | * |
| 39 | * \param rsa The RSA context where parsed data will be stored. |
| 40 | * \param key The buffer that contains the key. |
| 41 | * \param keylen The length of the key buffer in bytes. |
| 42 | * |
| 43 | * \return 0 on success. |
| 44 | * \return MBEDTLS_ERR_ASN1_xxx in case of ASN.1 parsing errors. |
| 45 | * \return MBEDTLS_ERR_RSA_xxx in case of RSA internal failures while |
| 46 | * parsing data. |
| 47 | * \return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if validity checks on the |
| 48 | * provided key fail. |
| 49 | */ |
| 50 | int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen); |
| 51 | |
| 52 | /** |
| 53 | * \brief Write a PKCS#1 (ASN.1) encoded private RSA key. |
| 54 | * |
| 55 | * \param rsa The RSA context which contains the data to be written. |
| 56 | * \param start Beginning of the buffer that will be filled with the |
| 57 | * private key. |
| 58 | * \param p End of the buffer that will be filled with the private key. |
| 59 | * On successful return, the referenced pointer will be |
| 60 | * updated in order to point to the beginning of written data. |
| 61 | * |
| 62 | * \return On success, the number of bytes written to the output buffer |
| 63 | * (i.e. a value > 0). |
| 64 | * \return MBEDTLS_ERR_ASN1_xxx in case of failure while writing to the |
| 65 | * output buffer. |
| 66 | * |
| 67 | * \note The output buffer is filled backward, i.e. starting from its |
| 68 | * end and moving toward its start. |
| 69 | */ |
| 70 | int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start, |
| 71 | unsigned char **p); |
| 72 | |
| 73 | /** |
| 74 | * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key. |
| 75 | * |
| 76 | * \param rsa The RSA context which contains the data to be written. |
| 77 | * \param start Beginning of the buffer that will be filled with the |
| 78 | * private key. |
| 79 | * \param p End of the buffer that will be filled with the private key. |
| 80 | * On successful return, the referenced pointer will be |
| 81 | * updated in order to point to the beginning of written data. |
| 82 | * |
| 83 | * \return On success, the number of bytes written to the output buffer |
| 84 | * (i.e. a value > 0). |
| 85 | * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the RSA context does not |
| 86 | * contain a valid public key. |
| 87 | * \return MBEDTLS_ERR_ASN1_xxx in case of failure while writing to the |
| 88 | * output buffer. |
| 89 | * |
| 90 | * \note The output buffer is filled backward, i.e. starting from its |
| 91 | * end and moving toward its start. |
| 92 | */ |
| 93 | int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start, |
| 94 | unsigned char **p); |
| 95 | |
| 96 | #if defined(MBEDTLS_PKCS1_V15) |
| 97 | /** |
| 98 | * \brief This function performs a PKCS#1 v1.5 decryption |
| 99 | * operation (RSAES-PKCS1-v1_5-DECRYPT). |
| 100 | * |
| 101 | * \warning This is an inherently dangerous function (CWE-242). Unless |
| 102 | * it is used in a side channel free and safe way, the calling |
| 103 | * code is vulnerable. |
| 104 | * |
| 105 | * \note The output buffer length \c output_max_len should be |
| 106 | * as large as the size \p ctx->len of \p ctx->N, for example, |
| 107 | * 128 Bytes if RSA-1024 is used, to be able to hold an |
| 108 | * arbitrary decrypted message. If it is not large enough to |
| 109 | * hold the decryption of the particular ciphertext provided, |
| 110 | * the function returns #PSA_ERROR_BUFFER_TOO_SMALL. |
| 111 | * |
| 112 | * \param ctx The initialized RSA context to use. |
| 113 | * \param f_rng The RNG function. This is used for blinding and is |
| 114 | * mandatory; see mbedtls_rsa_private() for more. |
| 115 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 116 | * \c NULL if \p f_rng doesn't need a context. |
| 117 | * \param olen The address at which to store the length of |
| 118 | * the plaintext. This must not be \c NULL. |
| 119 | * \param input The ciphertext buffer. This must be a readable buffer |
| 120 | * of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 121 | * for an 2048-bit RSA modulus. |
| 122 | * \param output The buffer used to hold the plaintext. This must |
| 123 | * be a writable buffer of length \p output_max_len Bytes. |
| 124 | * \param output_max_len The length in Bytes of the output buffer \p output. |
| 125 | * \param sensitive_ret |
| 126 | * If this function returns \c 0, this is set to either 0 for |
| 127 | * success, or #PSA_ERROR_INVALID_PADDING if the padding |
| 128 | * was invalid, or #PSA_ERROR_BUFFER_TOO_SMALL if the |
| 129 | * padding was valid but resulted in a plaintext larger than the |
| 130 | * output buffer. |
| 131 | * If this function returns non-zero this is set to \c 0. |
| 132 | * |
| 133 | * \return \c 0 on success, or when the only error is invalid padding, |
| 134 | * or valid padding that results in a plaintext larger than the |
| 135 | * output buffer. |
| 136 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on other failure. |
| 137 | * |
| 138 | */ |
| 139 | int mbedtls_rsa_rsaes_pkcs1_v15_decrypt_ext(mbedtls_rsa_context *ctx, |
| 140 | int (*f_rng)(void *, unsigned char *, size_t), |
| 141 | void *p_rng, |
| 142 | size_t *olen, |
| 143 | const unsigned char *input, |
| 144 | unsigned char *output, |
| 145 | size_t output_max_len, |
| 146 | int *sensitive_ret); |
| 147 | #endif |
| 148 | |
| 149 | #if defined(MBEDTLS_PKCS1_V21) |
| 150 | /** |
| 151 | * \brief This function is analogue to \c mbedtls_rsa_rsassa_pss_sign_ext(). |
| 152 | * The only difference between them is that this function is more flexible |
| 153 | * on the parameters of \p ctx that are set with \c mbedtls_rsa_set_padding(). |
| 154 | * |
| 155 | * \note Compared to its counterpart, this function: |
| 156 | * - does not check the padding setting of \p ctx. |
| 157 | * - allows the hash_id of \p ctx to be MBEDTLS_MD_NONE, |
| 158 | * in which case it uses \p md_alg as the hash_id. |
| 159 | * |
| 160 | * \note Refer to \c mbedtls_rsa_rsassa_pss_sign_ext() for a description |
| 161 | * of the functioning and parameters of this function. |
| 162 | */ |
| 163 | int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx, |
| 164 | int (*f_rng)(void *, unsigned char *, size_t), |
| 165 | void *p_rng, |
| 166 | mbedtls_md_type_t md_alg, |
| 167 | unsigned int hashlen, |
| 168 | const unsigned char *hash, |
| 169 | unsigned char *sig); |
| 170 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 171 | |
| 172 | #endif /* TF_PSA_CRYPTO_RSA_INTERNAL_H */ |