mbed TLS v2.16.12
rsa.h
Go to the documentation of this file.
1 
12 /*
13  * Copyright The Mbed TLS Contributors
14  * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
15  *
16  * This file is provided under the Apache License 2.0, or the
17  * GNU General Public License v2.0 or later.
18  *
19  * **********
20  * Apache License 2.0:
21  *
22  * Licensed under the Apache License, Version 2.0 (the "License"); you may
23  * not use this file except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  * http://www.apache.org/licenses/LICENSE-2.0
27  *
28  * Unless required by applicable law or agreed to in writing, software
29  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * **********
35  *
36  * **********
37  * GNU General Public License v2.0 or later:
38  *
39  * This program is free software; you can redistribute it and/or modify
40  * it under the terms of the GNU General Public License as published by
41  * the Free Software Foundation; either version 2 of the License, or
42  * (at your option) any later version.
43  *
44  * This program is distributed in the hope that it will be useful,
45  * but WITHOUT ANY WARRANTY; without even the implied warranty of
46  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47  * GNU General Public License for more details.
48  *
49  * You should have received a copy of the GNU General Public License along
50  * with this program; if not, write to the Free Software Foundation, Inc.,
51  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
52  *
53  * **********
54  */
55 #ifndef MBEDTLS_RSA_H
56 #define MBEDTLS_RSA_H
57 
58 #if !defined(MBEDTLS_CONFIG_FILE)
59 #include "config.h"
60 #else
61 #include MBEDTLS_CONFIG_FILE
62 #endif
63 
64 #include "bignum.h"
65 #include "md.h"
66 
67 #if defined(MBEDTLS_THREADING_C)
68 #include "threading.h"
69 #endif
70 
71 /*
72  * RSA Error codes
73  */
75 #define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
76 
77 #define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
78 
79 #define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
80 
81 #define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
82 
83 #define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
84 
85 #define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
86 
87 #define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
88 
89 #define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
90 
91 #define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
92 
93 /* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
94  */
96 #define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500
97 
98 /* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
100 #define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580
101 
102 /*
103  * RSA constants
104  */
105 #define MBEDTLS_RSA_PUBLIC 0
106 #define MBEDTLS_RSA_PRIVATE 1
108 #define MBEDTLS_RSA_PKCS_V15 0
109 #define MBEDTLS_RSA_PKCS_V21 1
111 #define MBEDTLS_RSA_SIGN 1
112 #define MBEDTLS_RSA_CRYPT 2
114 #define MBEDTLS_RSA_SALT_LEN_ANY -1
115 
116 /*
117  * The above constants may be used even if the RSA module is compile out,
118  * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
119  */
120 
121 #ifdef __cplusplus
122 extern "C" {
123 #endif
124 
125 #if !defined(MBEDTLS_RSA_ALT)
126 // Regular implementation
127 //
128 
136 typedef struct mbedtls_rsa_context
137 {
138  int ver;
142  size_t len;
163  int padding;
166  int hash_id;
170 #if defined(MBEDTLS_THREADING_C)
171  /* Invariant: the mutex is initialized iff ver != 0. */
173 #endif
174 }
176 
177 #else /* MBEDTLS_RSA_ALT */
178 #include "rsa_alt.h"
179 #endif /* MBEDTLS_RSA_ALT */
180 
209  int padding,
210  int hash_id );
211 
242  const mbedtls_mpi *N,
243  const mbedtls_mpi *P, const mbedtls_mpi *Q,
244  const mbedtls_mpi *D, const mbedtls_mpi *E );
245 
281  unsigned char const *N, size_t N_len,
282  unsigned char const *P, size_t P_len,
283  unsigned char const *Q, size_t Q_len,
284  unsigned char const *D, size_t D_len,
285  unsigned char const *E, size_t E_len );
286 
320 
363  mbedtls_mpi *D, mbedtls_mpi *E );
364 
413  unsigned char *N, size_t N_len,
414  unsigned char *P, size_t P_len,
415  unsigned char *Q, size_t Q_len,
416  unsigned char *D, size_t D_len,
417  unsigned char *E, size_t E_len );
418 
439  mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
440 
450 void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
451  int hash_id );
452 
461 size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
462 
482  int (*f_rng)(void *, unsigned char *, size_t),
483  void *p_rng,
484  unsigned int nbits, int exponent );
485 
501 
539 
552  const mbedtls_rsa_context *prv );
553 
574  const unsigned char *input,
575  unsigned char *output );
576 
609  int (*f_rng)(void *, unsigned char *, size_t),
610  void *p_rng,
611  const unsigned char *input,
612  unsigned char *output );
613 
653  int (*f_rng)(void *, unsigned char *, size_t),
654  void *p_rng,
655  int mode, size_t ilen,
656  const unsigned char *input,
657  unsigned char *output );
658 
693  int (*f_rng)(void *, unsigned char *, size_t),
694  void *p_rng,
695  int mode, size_t ilen,
696  const unsigned char *input,
697  unsigned char *output );
698 
737  int (*f_rng)(void *, unsigned char *, size_t),
738  void *p_rng,
739  int mode,
740  const unsigned char *label, size_t label_len,
741  size_t ilen,
742  const unsigned char *input,
743  unsigned char *output );
744 
790  int (*f_rng)(void *, unsigned char *, size_t),
791  void *p_rng,
792  int mode, size_t *olen,
793  const unsigned char *input,
794  unsigned char *output,
795  size_t output_max_len );
796 
840  int (*f_rng)(void *, unsigned char *, size_t),
841  void *p_rng,
842  int mode, size_t *olen,
843  const unsigned char *input,
844  unsigned char *output,
845  size_t output_max_len );
846 
894  int (*f_rng)(void *, unsigned char *, size_t),
895  void *p_rng,
896  int mode,
897  const unsigned char *label, size_t label_len,
898  size_t *olen,
899  const unsigned char *input,
900  unsigned char *output,
901  size_t output_max_len );
902 
954  int (*f_rng)(void *, unsigned char *, size_t),
955  void *p_rng,
956  int mode,
957  mbedtls_md_type_t md_alg,
958  unsigned int hashlen,
959  const unsigned char *hash,
960  unsigned char *sig );
961 
1002  int (*f_rng)(void *, unsigned char *, size_t),
1003  void *p_rng,
1004  int mode,
1005  mbedtls_md_type_t md_alg,
1006  unsigned int hashlen,
1007  const unsigned char *hash,
1008  unsigned char *sig );
1009 
1064  int (*f_rng)(void *, unsigned char *, size_t),
1065  void *p_rng,
1066  int mode,
1067  mbedtls_md_type_t md_alg,
1068  unsigned int hashlen,
1069  const unsigned char *hash,
1070  unsigned char *sig );
1071 
1117  int (*f_rng)(void *, unsigned char *, size_t),
1118  void *p_rng,
1119  int mode,
1120  mbedtls_md_type_t md_alg,
1121  unsigned int hashlen,
1122  const unsigned char *hash,
1123  const unsigned char *sig );
1124 
1163  int (*f_rng)(void *, unsigned char *, size_t),
1164  void *p_rng,
1165  int mode,
1166  mbedtls_md_type_t md_alg,
1167  unsigned int hashlen,
1168  const unsigned char *hash,
1169  const unsigned char *sig );
1170 
1220  int (*f_rng)(void *, unsigned char *, size_t),
1221  void *p_rng,
1222  int mode,
1223  mbedtls_md_type_t md_alg,
1224  unsigned int hashlen,
1225  const unsigned char *hash,
1226  const unsigned char *sig );
1227 
1268  int (*f_rng)(void *, unsigned char *, size_t),
1269  void *p_rng,
1270  int mode,
1271  mbedtls_md_type_t md_alg,
1272  unsigned int hashlen,
1273  const unsigned char *hash,
1274  mbedtls_md_type_t mgf1_hash_id,
1275  int expected_salt_len,
1276  const unsigned char *sig );
1277 
1288 
1297 
1298 #if defined(MBEDTLS_SELF_TEST)
1299 
1306 int mbedtls_rsa_self_test( int verbose );
1307 
1308 #endif /* MBEDTLS_SELF_TEST */
1309 
1310 #ifdef __cplusplus
1311 }
1312 #endif
1313 
1314 #endif /* rsa.h */
int mbedtls_rsa_self_test(int verbose)
The RSA checkup routine.
mbedtls_mpi DP
Definition: rsa.h:151
int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx, unsigned char *N, size_t N_len, unsigned char *P, size_t P_len, unsigned char *Q, size_t Q_len, unsigned char *D, size_t D_len, unsigned char *E, size_t E_len)
This function exports core parameters of an RSA key in raw big-endian binary format.
int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
This function performs a PKCS#1 v1.5 signature operation (RSASSA-PKCS1-v1_5-SIGN).
int mbedtls_rsa_public(mbedtls_rsa_context *ctx, const unsigned char *input, unsigned char *output)
This function performs an RSA public key operation.
int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, const unsigned char *label, size_t label_len, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
This function performs a PKCS#1 v2.1 OAEP decryption operation (RSAES-OAEP-DECRYPT).
int mbedtls_rsa_import(mbedtls_rsa_context *ctx, const mbedtls_mpi *N, const mbedtls_mpi *P, const mbedtls_mpi *Q, const mbedtls_mpi *D, const mbedtls_mpi *E)
This function imports a set of core parameters into an RSA context.
int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
This function performs a public RSA operation and checks the message digest.
Configuration options (set of defines)
int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
This function completes an RSA context from a set of imported core parameters.
mbedtls_mpi DQ
Definition: rsa.h:152
int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
This function performs a private RSA operation to sign a message digest using PKCS#1.
void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding, int hash_id)
This function sets padding for an already initialized RSA context. See mbedtls_rsa_init() for details...
Multi-precision integer library.
int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, mbedtls_md_type_t mgf1_hash_id, int expected_salt_len, const unsigned char *sig)
This function performs a PKCS#1 v2.1 PSS verification operation (RSASSA-PSS-VERIFY).
mbedtls_threading_mutex_t mutex
Definition: rsa.h:172
int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
This function performs an RSA operation, then removes the message padding.
int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
This function performs a PKCS#1 v2.1 PSS signature operation (RSASSA-PSS-SIGN).
int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx, unsigned char const *N, size_t N_len, unsigned char const *P, size_t P_len, unsigned char const *Q, size_t Q_len, unsigned char const *D, size_t D_len, unsigned char const *E, size_t E_len)
This function imports core RSA parameters, in raw big-endian binary format, into an RSA context...
mbedtls_mpi RP
Definition: rsa.h:157
int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv)
This function checks a public-private RSA key pair.
Threading abstraction layer.
mbedtls_mpi P
Definition: rsa.h:148
struct mbedtls_rsa_context mbedtls_rsa_context
The RSA context structure.
int mbedtls_rsa_export(const mbedtls_rsa_context *ctx, mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q, mbedtls_mpi *D, mbedtls_mpi *E)
This function exports the core parameters of an RSA key.
int mbedtls_rsa_private(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, const unsigned char *input, unsigned char *output)
This function performs an RSA private key operation.
int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
This function performs a PKCS#1 v1.5 decryption operation (RSAES-PKCS1-v1_5-DECRYPT).
int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
This function performs a PKCS#1 v1.5 verification operation (RSASSA-PKCS1-v1_5-VERIFY).
mbedtls_mpi E
Definition: rsa.h:145
int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx, mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
This function exports CRT parameters of a private RSA key.
size_t len
Definition: rsa.h:142
mbedtls_mpi Vf
Definition: rsa.h:161
int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
This function adds the message padding, then performs an RSA operation.
mbedtls_mpi QP
Definition: rsa.h:153
mbedtls_mpi D
Definition: rsa.h:147
This file contains the generic message-digest wrapper.
int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
This function performs a PKCS#1 v2.1 PSS verification operation (RSASSA-PSS-VERIFY).
int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
This function performs a PKCS#1 v1.5 encryption operation (RSAES-PKCS1-v1_5-ENCRYPT).
size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
This function retrieves the length of RSA modulus in Bytes.
MPI structure.
Definition: bignum.h:218
mbedtls_mpi N
Definition: rsa.h:144
int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
This function checks if a context contains at least an RSA public key.
mbedtls_mpi RN
Definition: rsa.h:155
void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
This function frees the components of an RSA key.
mbedtls_mpi RQ
Definition: rsa.h:158
int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, unsigned int nbits, int exponent)
This function generates an RSA keypair.
int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
This function copies the components of an RSA context.
int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
This function checks if a context contains an RSA private key and perform basic consistency checks...
mbedtls_mpi Vi
Definition: rsa.h:160
mbedtls_md_type_t
Supported message digests.
Definition: md.h:88
mbedtls_mpi Q
Definition: rsa.h:149
The RSA context structure.
Definition: rsa.h:136
int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, const unsigned char *label, size_t label_len, size_t ilen, const unsigned char *input, unsigned char *output)
This function performs a PKCS#1 v2.1 OAEP encryption operation (RSAES-OAEP-ENCRYPT).
void mbedtls_rsa_init(mbedtls_rsa_context *ctx, int padding, int hash_id)
This function initializes an RSA context.