blob: acf14a2ff66403fdf28ace273e3960663d8994f2 [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 *
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 Settib328c442024-01-23 10:48:45 +010030 */
Valerio Setti135ebde2024-02-01 17:00:29 +010031int mbedtls_rsa_parse_key(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen);
Valerio Settib328c442024-01-23 10:48:45 +010032
33/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010034 * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010035 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010036 * \param rsa The RSA context where parsed data will be stored.
Valerio Setti201e6432024-02-01 17:19:37 +010037 * \param key The buffer that contains the key.
38 * \param keylen The length of the key buffer in bytes.
Valerio Settia5f36fc2024-01-24 10:49:02 +010039 *
40 * \return 0 on success.
41 * \return MBEDTLS_ERR_ASN1_xxx in case of ASN.1 parsing errors.
42 * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA in case of importing or
43 * priv/pub validation errors.
Valerio Settib328c442024-01-23 10:48:45 +010044 */
Valerio Setti201e6432024-02-01 17:19:37 +010045int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen);
Valerio Settib328c442024-01-23 10:48:45 +010046
47/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010048 * \brief Write a PKCS#1 (ASN.1) encoded private RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010049 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010050 * \param rsa The RSA context which contains the data to be written.
51 * \param start Beginning of the buffer that will be filled with the
52 * private key.
53 * \param p End of the buffer that will be filled with the private key.
54 * On successful return, the referenced pointer will be
55 * updated in order to point to the beginning of written data.
56 *
57 * \return On success, the number of bytes written to the output buffer
58 * (i.e. a value > 0).
59 * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA is the RSA context does not
60 * cointain valid.
61 * \return MBEDTLS_ERR_ASN1_xxx in case of failure while writing to the
62 * output buffer.
63 *
64 * \note The output buffer is filled backward, i.e. starting from its
65 * end and moving toward its start.
Valerio Settib328c442024-01-23 10:48:45 +010066 */
Valerio Setti135ebde2024-02-01 17:00:29 +010067int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Settib328c442024-01-23 10:48:45 +010068 unsigned char **p);
69
70/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010071 * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010072 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010073 * \param rsa The RSA context which contains the data to be written.
74 * \param start Beginning of the buffer that will be filled with the
75 * private key.
76 * \param p End of the buffer that will be filled with the private key.
77 * On successful return, the referenced pointer will be
78 * updated in order to point to the beginning of written data.
79 *
80 * \return On success, the number of bytes written to the output buffer
81 * (i.e. a value > 0).
82 * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA is the RSA context does not
83 * cointain valid.
84 * \return MBEDTLS_ERR_ASN1_xxx in case of failure while writing to the
85 * output buffer.
86 *
87 * \note The output buffer is filled backward, i.e. starting from its
88 * end and moving toward its start.
Valerio Settib328c442024-01-23 10:48:45 +010089 */
Valerio Setti135ebde2024-02-01 17:00:29 +010090int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start,
Valerio Settib328c442024-01-23 10:48:45 +010091 unsigned char **p);
92
Tomi Fontanilles573dc232023-12-10 14:57:51 +020093#if defined(MBEDTLS_PKCS1_V21)
94/**
95 * \brief This function is analogue to \c mbedtls_rsa_rsassa_pss_sign().
96 * The only difference between them is that this function is more flexible
97 * on the parameters of \p ctx that are set with \c mbedtls_rsa_set_padding().
98 *
99 * \note Compared to its counterpart, this function:
100 * - does not check the padding setting of \p ctx.
101 * - allows the hash_id of \p ctx to be MBEDTLS_MD_NONE,
102 * in which case it uses \p md_alg as the hash_id.
103 *
104 * \note Refer to \c mbedtls_rsa_rsassa_pss_sign() for a description
105 * of the functioning and parameters of this function.
106 */
107int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
108 int (*f_rng)(void *, unsigned char *, size_t),
109 void *p_rng,
110 mbedtls_md_type_t md_alg,
111 unsigned int hashlen,
112 const unsigned char *hash,
113 unsigned char *sig);
114#endif /* MBEDTLS_PKCS1_V21 */
115
116#endif /* rsa_internal.h */