blob: 6046850fcc9b35a8657d4e8961a181fcf7b4e48d [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 */
31int mbedtls_rsa_key_parse(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen);
32
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.
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 Settib328c442024-01-23 10:48:45 +010046 */
47int mbedtls_rsa_pubkey_parse(mbedtls_rsa_context *rsa, unsigned char **p,
48 const unsigned char *end);
49
50/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010051 * \brief Write a PKCS#1 (ASN.1) encoded private RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010052 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010053 * \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 Settib328c442024-01-23 10:48:45 +010069 */
70int mbedtls_rsa_key_write(const mbedtls_rsa_context *rsa, unsigned char *start,
71 unsigned char **p);
72
73/**
Valerio Settia5f36fc2024-01-24 10:49:02 +010074 * \brief Parse a PKCS#1 (ASN.1) encoded public RSA key.
Valerio Settib328c442024-01-23 10:48:45 +010075 *
Valerio Settia5f36fc2024-01-24 10:49:02 +010076 * \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 Settib328c442024-01-23 10:48:45 +010092 */
93int mbedtls_rsa_pubkey_write(const mbedtls_rsa_context *rsa, unsigned char *start,
94 unsigned char **p);
95
Tomi Fontanilles573dc232023-12-10 14:57:51 +020096#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 */
110int 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 */