blob: 4cb564efaef5e73a80bcaf9f78070d99a48f633c [file] [log] [blame]
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001/**
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 Setti6def24c2024-01-24 12:33:04 +010018#include "mbedtls/asn1.h"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020019
Valerio Settib328c442024-01-23 10:48:45 +010020/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010021 * \brief Parse a PKCS#1 (ASN.1) encoded private RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010022 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010023 * \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 *
Valerio Setti5fe9f662024-02-01 17:35:56 +010027 * \return 0 on success.
28 * \return MBEDTLS_ERR_RSA_xxx in case of RSA internal failures while
29 * parsing data.
30 * \return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if validity checks on the
31 * provided key fail.
Valerio Settib328c442024-01-23 10:48:45 +010032 */
Valerio Setti135ebde2024-02-01 17:00:29 +010033int mbedtls_rsa_parse_key(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen);
Valerio Settib328c442024-01-23 10:48:45 +010034
35/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010036 * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010037 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010038 * \param rsa The RSA context where parsed data will be stored.
Valerio Setti201e6432024-02-01 17:19:37 +010039 * \param key The buffer that contains the key.
40 * \param keylen The length of the key buffer in bytes.
Valerio Settia5f36fc2024-01-24 10:49:02 +010041 *
42 * \return 0 on success.
43 * \return MBEDTLS_ERR_ASN1_xxx in case of ASN.1 parsing errors.
Valerio Setti5fe9f662024-02-01 17:35:56 +010044 * \return MBEDTLS_ERR_RSA_xxx in case of RSA internal failures while
45 * parsing data.
46 * \return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if validity checks on the
47 * provided key fail.
Valerio Settib328c442024-01-23 10:48:45 +010048 */
Valerio Setti201e6432024-02-01 17:19:37 +010049int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen);
Valerio Settib328c442024-01-23 10:48:45 +010050
51/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010052 * \brief Write a PKCS#1 (ASN.1) encoded private RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010053 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010054 * \param rsa The RSA context which contains the data to be written.
55 * \param start Beginning of the buffer that will be filled with the
56 * private key.
57 * \param p End of the buffer that will be filled with the private key.
58 * On successful return, the referenced pointer will be
59 * updated in order to point to the beginning of written data.
60 *
61 * \return On success, the number of bytes written to the output buffer
62 * (i.e. a value > 0).
Valerio Setti5fe9f662024-02-01 17:35:56 +010063 * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the RSA context does not
64 * contain a valid key pair.
Valerio Settia5f36fc2024-01-24 10:49:02 +010065 * \return MBEDTLS_ERR_ASN1_xxx in case of failure while writing to the
66 * output buffer.
67 *
68 * \note The output buffer is filled backward, i.e. starting from its
69 * end and moving toward its start.
Valerio Settib328c442024-01-23 10:48:45 +010070 */
Valerio Setti135ebde2024-02-01 17:00:29 +010071int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Settib328c442024-01-23 10:48:45 +010072 unsigned char **p);
73
74/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010075 * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010076 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010077 * \param rsa The RSA context which contains the data to be written.
78 * \param start Beginning of the buffer that will be filled with the
79 * private key.
80 * \param p End of the buffer that will be filled with the private key.
81 * On successful return, the referenced pointer will be
82 * updated in order to point to the beginning of written data.
83 *
84 * \return On success, the number of bytes written to the output buffer
85 * (i.e. a value > 0).
Valerio Setti5fe9f662024-02-01 17:35:56 +010086 * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the RSA context does not
87 * contain a valid public key.
Valerio Settia5f36fc2024-01-24 10:49:02 +010088 * \return MBEDTLS_ERR_ASN1_xxx in case of failure while writing to the
89 * output buffer.
90 *
91 * \note The output buffer is filled backward, i.e. starting from its
92 * end and moving toward its start.
Valerio Settib328c442024-01-23 10:48:45 +010093 */
Valerio Setti135ebde2024-02-01 17:00:29 +010094int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Settib328c442024-01-23 10:48:45 +010095 unsigned char **p);
96
Tomi Fontanilles573dc232023-12-10 14:57:51 +020097#if defined(MBEDTLS_PKCS1_V21)
98/**
99 * \brief This function is analogue to \c mbedtls_rsa_rsassa_pss_sign().
100 * The only difference between them is that this function is more flexible
101 * on the parameters of \p ctx that are set with \c mbedtls_rsa_set_padding().
102 *
103 * \note Compared to its counterpart, this function:
104 * - does not check the padding setting of \p ctx.
105 * - allows the hash_id of \p ctx to be MBEDTLS_MD_NONE,
106 * in which case it uses \p md_alg as the hash_id.
107 *
108 * \note Refer to \c mbedtls_rsa_rsassa_pss_sign() for a description
109 * of the functioning and parameters of this function.
110 */
111int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
112 int (*f_rng)(void *, unsigned char *, size_t),
113 void *p_rng,
114 mbedtls_md_type_t md_alg,
115 unsigned int hashlen,
116 const unsigned char *hash,
117 unsigned char *sig);
118#endif /* MBEDTLS_PKCS1_V21 */
119
120#endif /* rsa_internal.h */