Tomi Fontanilles | 573dc23 | 2023-12-10 14:57:51 +0200 | [diff] [blame] | 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 MBEDTLS_RSA_INTERNAL_H |
| 15 | #define MBEDTLS_RSA_INTERNAL_H |
| 16 | |
| 17 | #include "mbedtls/rsa.h" |
Valerio Setti | 6def24c | 2024-01-24 12:33:04 +0100 | [diff] [blame^] | 18 | #include "mbedtls/asn1.h" |
Tomi Fontanilles | 573dc23 | 2023-12-10 14:57:51 +0200 | [diff] [blame] | 19 | |
Valerio Setti | b328c44 | 2024-01-23 10:48:45 +0100 | [diff] [blame] | 20 | /** |
Valerio Setti | a5f36fc | 2024-01-24 10:49:02 +0100 | [diff] [blame] | 21 | * \brief Parse a PKCS#1 (ASN.1) encoded private RSA key. |
Valerio Setti | b328c44 | 2024-01-23 10:48:45 +0100 | [diff] [blame] | 22 | * |
Valerio Setti | a5f36fc | 2024-01-24 10:49:02 +0100 | [diff] [blame] | 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 in success |
| 28 | * \return MBEDTLS_ERR_ASN1_xxx in case of ASN.1 parsing errors. |
| 29 | * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA in case of invalid version. |
Valerio Setti | b328c44 | 2024-01-23 10:48:45 +0100 | [diff] [blame] | 30 | */ |
| 31 | int mbedtls_rsa_key_parse(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen); |
| 32 | |
| 33 | /** |
Valerio Setti | a5f36fc | 2024-01-24 10:49:02 +0100 | [diff] [blame] | 34 | * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key. |
Valerio Setti | b328c44 | 2024-01-23 10:48:45 +0100 | [diff] [blame] | 35 | * |
Valerio Setti | a5f36fc | 2024-01-24 10:49:02 +0100 | [diff] [blame] | 36 | * \param rsa The RSA context where parsed data will be stored. |
| 37 | * \param p Beginning of the buffer containing the key to be parsed. |
| 38 | * On successful return, the referenced pointer will be |
| 39 | * updated in order to point to the end of the parsed data. |
| 40 | * \param end End of the buffer containing the key to be parsed. |
| 41 | * |
| 42 | * \return 0 on success. |
| 43 | * \return MBEDTLS_ERR_ASN1_xxx in case of ASN.1 parsing errors. |
| 44 | * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA in case of importing or |
| 45 | * priv/pub validation errors. |
Valerio Setti | b328c44 | 2024-01-23 10:48:45 +0100 | [diff] [blame] | 46 | */ |
| 47 | int mbedtls_rsa_pubkey_parse(mbedtls_rsa_context *rsa, unsigned char **p, |
| 48 | const unsigned char *end); |
| 49 | |
| 50 | /** |
Valerio Setti | a5f36fc | 2024-01-24 10:49:02 +0100 | [diff] [blame] | 51 | * \brief Write a PKCS#1 (ASN.1) encoded private RSA key. |
Valerio Setti | b328c44 | 2024-01-23 10:48:45 +0100 | [diff] [blame] | 52 | * |
Valerio Setti | a5f36fc | 2024-01-24 10:49:02 +0100 | [diff] [blame] | 53 | * \param rsa The RSA context which contains the data to be written. |
| 54 | * \param start Beginning of the buffer that will be filled with the |
| 55 | * private key. |
| 56 | * \param p End of the buffer that will be filled with the private key. |
| 57 | * On successful return, the referenced pointer will be |
| 58 | * updated in order to point to the beginning of written data. |
| 59 | * |
| 60 | * \return On success, the number of bytes written to the output buffer |
| 61 | * (i.e. a value > 0). |
| 62 | * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA is the RSA context does not |
| 63 | * cointain valid. |
| 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. |
Valerio Setti | b328c44 | 2024-01-23 10:48:45 +0100 | [diff] [blame] | 69 | */ |
| 70 | int mbedtls_rsa_key_write(const mbedtls_rsa_context *rsa, unsigned char *start, |
| 71 | unsigned char **p); |
| 72 | |
| 73 | /** |
Valerio Setti | a5f36fc | 2024-01-24 10:49:02 +0100 | [diff] [blame] | 74 | * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key. |
Valerio Setti | b328c44 | 2024-01-23 10:48:45 +0100 | [diff] [blame] | 75 | * |
Valerio Setti | a5f36fc | 2024-01-24 10:49:02 +0100 | [diff] [blame] | 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 is the RSA context does not |
| 86 | * cointain valid. |
| 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. |
Valerio Setti | b328c44 | 2024-01-23 10:48:45 +0100 | [diff] [blame] | 92 | */ |
| 93 | int mbedtls_rsa_pubkey_write(const mbedtls_rsa_context *rsa, unsigned char *start, |
| 94 | unsigned char **p); |
| 95 | |
Tomi Fontanilles | 573dc23 | 2023-12-10 14:57:51 +0200 | [diff] [blame] | 96 | #if defined(MBEDTLS_PKCS1_V21) |
| 97 | /** |
| 98 | * \brief This function is analogue to \c mbedtls_rsa_rsassa_pss_sign(). |
| 99 | * The only difference between them is that this function is more flexible |
| 100 | * on the parameters of \p ctx that are set with \c mbedtls_rsa_set_padding(). |
| 101 | * |
| 102 | * \note Compared to its counterpart, this function: |
| 103 | * - does not check the padding setting of \p ctx. |
| 104 | * - allows the hash_id of \p ctx to be MBEDTLS_MD_NONE, |
| 105 | * in which case it uses \p md_alg as the hash_id. |
| 106 | * |
| 107 | * \note Refer to \c mbedtls_rsa_rsassa_pss_sign() for a description |
| 108 | * of the functioning and parameters of this function. |
| 109 | */ |
| 110 | int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx, |
| 111 | int (*f_rng)(void *, unsigned char *, size_t), |
| 112 | void *p_rng, |
| 113 | mbedtls_md_type_t md_alg, |
| 114 | unsigned int hashlen, |
| 115 | const unsigned char *hash, |
| 116 | unsigned char *sig); |
| 117 | #endif /* MBEDTLS_PKCS1_V21 */ |
| 118 | |
| 119 | #endif /* rsa_internal.h */ |