blob: 62972c63450a748f9fa925bbda36d275a98c2ea5 [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"
18
Valerio Settib328c442024-01-23 10:48:45 +010019/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010020 * \brief Parse a PKCS#1 (ASN.1) encoded private RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010021 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010022 * \param rsa The RSA context where parsed data will be stored.
23 * \param key The buffer that contains the key.
24 * \param keylen The length of the key buffer in bytes.
25 *
26 * \return 0 in success
27 * \return MBEDTLS_ERR_ASN1_xxx in case of ASN.1 parsing errors.
28 * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA in case of invalid version.
Valerio Settib328c442024-01-23 10:48:45 +010029 */
30int mbedtls_rsa_key_parse(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen);
31
32/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010033 * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010034 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010035 * \param rsa The RSA context where parsed data will be stored.
36 * \param p Beginning of the buffer containing the key to be parsed.
37 * On successful return, the referenced pointer will be
38 * updated in order to point to the end of the parsed data.
39 * \param end End of the buffer containing the key to be parsed.
40 *
41 * \return 0 on success.
42 * \return MBEDTLS_ERR_ASN1_xxx in case of ASN.1 parsing errors.
43 * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA in case of importing or
44 * priv/pub validation errors.
Valerio Settib328c442024-01-23 10:48:45 +010045 */
46int mbedtls_rsa_pubkey_parse(mbedtls_rsa_context *rsa, unsigned char **p,
47 const unsigned char *end);
48
49/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010050 * \brief Write a PKCS#1 (ASN.1) encoded private RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010051 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010052 * \param rsa The RSA context which contains the data to be written.
53 * \param start Beginning of the buffer that will be filled with the
54 * private key.
55 * \param p End of the buffer that will be filled with the private key.
56 * On successful return, the referenced pointer will be
57 * updated in order to point to the beginning of written data.
58 *
59 * \return On success, the number of bytes written to the output buffer
60 * (i.e. a value > 0).
61 * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA is the RSA context does not
62 * cointain valid.
63 * \return MBEDTLS_ERR_ASN1_xxx in case of failure while writing to the
64 * output buffer.
65 *
66 * \note The output buffer is filled backward, i.e. starting from its
67 * end and moving toward its start.
Valerio Settib328c442024-01-23 10:48:45 +010068 */
69int mbedtls_rsa_key_write(const mbedtls_rsa_context *rsa, unsigned char *start,
70 unsigned char **p);
71
72/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010073 * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010074 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010075 * \param rsa The RSA context which contains the data to be written.
76 * \param start Beginning of the buffer that will be filled with the
77 * private key.
78 * \param p End of the buffer that will be filled with the private key.
79 * On successful return, the referenced pointer will be
80 * updated in order to point to the beginning of written data.
81 *
82 * \return On success, the number of bytes written to the output buffer
83 * (i.e. a value > 0).
84 * \return MBEDTLS_ERR_RSA_BAD_INPUT_DATA is the RSA context does not
85 * cointain valid.
86 * \return MBEDTLS_ERR_ASN1_xxx in case of failure while writing to the
87 * output buffer.
88 *
89 * \note The output buffer is filled backward, i.e. starting from its
90 * end and moving toward its start.
Valerio Settib328c442024-01-23 10:48:45 +010091 */
92int mbedtls_rsa_pubkey_write(const mbedtls_rsa_context *rsa, unsigned char *start,
93 unsigned char **p);
94
Tomi Fontanilles573dc232023-12-10 14:57:51 +020095#if defined(MBEDTLS_PKCS1_V21)
96/**
97 * \brief This function is analogue to \c mbedtls_rsa_rsassa_pss_sign().
98 * The only difference between them is that this function is more flexible
99 * on the parameters of \p ctx that are set with \c mbedtls_rsa_set_padding().
100 *
101 * \note Compared to its counterpart, this function:
102 * - does not check the padding setting of \p ctx.
103 * - allows the hash_id of \p ctx to be MBEDTLS_MD_NONE,
104 * in which case it uses \p md_alg as the hash_id.
105 *
106 * \note Refer to \c mbedtls_rsa_rsassa_pss_sign() for a description
107 * of the functioning and parameters of this function.
108 */
109int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
110 int (*f_rng)(void *, unsigned char *, size_t),
111 void *p_rng,
112 mbedtls_md_type_t md_alg,
113 unsigned int hashlen,
114 const unsigned char *hash,
115 unsigned char *sig);
116#endif /* MBEDTLS_PKCS1_V21 */
117
118#endif /* rsa_internal.h */