mbed TLS v2.12.0

rsa.h
Go to the documentation of this file.
1
12 /*
13 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 *
28 * This file is part of Mbed TLS (https://tls.mbed.org)
29 */
30 #ifndef MBEDTLS_RSA_H
31 #define MBEDTLS_RSA_H
32
33 #if !defined(MBEDTLS_CONFIG_FILE)
34 #include "config.h"
35 #else
36 #include MBEDTLS_CONFIG_FILE
37 #endif
38
39 #include "bignum.h"
40 #include "md.h"
41
42 #if defined(MBEDTLS_THREADING_C)
43 #include "threading.h"
44 #endif
45
46 /*
47 * RSA Error codes
48 */
49 #define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
50 #define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
51 #define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
52 #define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
53 #define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
54 #define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
55 #define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
56 #define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
57 #define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
58 #define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500
59 #define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580
61 /*
62 * RSA constants
63 */
64 #define MBEDTLS_RSA_PUBLIC 0
65 #define MBEDTLS_RSA_PRIVATE 1
67 #define MBEDTLS_RSA_PKCS_V15 0
68 #define MBEDTLS_RSA_PKCS_V21 1
70 #define MBEDTLS_RSA_SIGN 1
71 #define MBEDTLS_RSA_CRYPT 2
73 #define MBEDTLS_RSA_SALT_LEN_ANY -1
74
75 /*
76 * The above constants may be used even if the RSA module is compile out,
77 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
78 */
79
80 #ifdef __cplusplus
81 extern "C" {
82 #endif
83
84 #if !defined(MBEDTLS_RSA_ALT)
85 // Regular implementation
86 //
87
95 typedef struct
96 {
97 int ver;
98 size_t len;
100 mbedtls_mpi N;
101 mbedtls_mpi E;
103 mbedtls_mpi D;
104 mbedtls_mpi P;
105 mbedtls_mpi Q;
107 mbedtls_mpi DP;
108 mbedtls_mpi DQ;
109 mbedtls_mpi QP;
111 mbedtls_mpi RN;
113 mbedtls_mpi RP;
114 mbedtls_mpi RQ;
116 mbedtls_mpi Vi;
117 mbedtls_mpi Vf;
119 int padding;
122 int hash_id;
126 #if defined(MBEDTLS_THREADING_C)
127 mbedtls_threading_mutex_t mutex;
128 #endif
129 }
130 mbedtls_rsa_context;
131
132 #else /* MBEDTLS_RSA_ALT */
133 #include "rsa_alt.h"
134 #endif /* MBEDTLS_RSA_ALT */
135
162 void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
163 int padding,
164 int hash_id);
165
195 int mbedtls_rsa_import( mbedtls_rsa_context *ctx,
196 const mbedtls_mpi *N,
197 const mbedtls_mpi *P, const mbedtls_mpi *Q,
198 const mbedtls_mpi *D, const mbedtls_mpi *E );
199
234 int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx,
235 unsigned char const *N, size_t N_len,
236 unsigned char const *P, size_t P_len,
237 unsigned char const *Q, size_t Q_len,
238 unsigned char const *D, size_t D_len,
239 unsigned char const *E, size_t E_len );
240
273 int mbedtls_rsa_complete( mbedtls_rsa_context *ctx );
274
310 int mbedtls_rsa_export( const mbedtls_rsa_context *ctx,
311 mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
312 mbedtls_mpi *D, mbedtls_mpi *E );
313
358 int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
359 unsigned char *N, size_t N_len,
360 unsigned char *P, size_t P_len,
361 unsigned char *Q, size_t Q_len,
362 unsigned char *D, size_t D_len,
363 unsigned char *E, size_t E_len );
364
381 int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
382 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
383
393 void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
394 int hash_id);
395
404 size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
405
421 int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
422 int (*f_rng)(void *, unsigned char *, size_t),
423 void *p_rng,
424 unsigned int nbits, int exponent );
425
440 int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
441
478 int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
479
491 int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub,
492 const mbedtls_rsa_context *prv );
493
512 int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
513 const unsigned char *input,
514 unsigned char *output );
515
543 int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
544 int (*f_rng)(void *, unsigned char *, size_t),
545 void *p_rng,
546 const unsigned char *input,
547 unsigned char *output );
548
580 int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
581 int (*f_rng)(void *, unsigned char *, size_t),
582 void *p_rng,
583 int mode, size_t ilen,
584 const unsigned char *input,
585 unsigned char *output );
586
615 int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
616 int (*f_rng)(void *, unsigned char *, size_t),
617 void *p_rng,
618 int mode, size_t ilen,
619 const unsigned char *input,
620 unsigned char *output );
621
652 int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
653 int (*f_rng)(void *, unsigned char *, size_t),
654 void *p_rng,
655 int mode,
656 const unsigned char *label, size_t label_len,
657 size_t ilen,
658 const unsigned char *input,
659 unsigned char *output );
660
699 int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
700 int (*f_rng)(void *, unsigned char *, size_t),
701 void *p_rng,
702 int mode, size_t *olen,
703 const unsigned char *input,
704 unsigned char *output,
705 size_t output_max_len );
706
743 int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
744 int (*f_rng)(void *, unsigned char *, size_t),
745 void *p_rng,
746 int mode, size_t *olen,
747 const unsigned char *input,
748 unsigned char *output,
749 size_t output_max_len );
750
789 int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
790 int (*f_rng)(void *, unsigned char *, size_t),
791 void *p_rng,
792 int mode,
793 const unsigned char *label, size_t label_len,
794 size_t *olen,
795 const unsigned char *input,
796 unsigned char *output,
797 size_t output_max_len );
798
836 int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
837 int (*f_rng)(void *, unsigned char *, size_t),
838 void *p_rng,
839 int mode,
840 mbedtls_md_type_t md_alg,
841 unsigned int hashlen,
842 const unsigned char *hash,
843 unsigned char *sig );
844
874 int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
875 int (*f_rng)(void *, unsigned char *, size_t),
876 void *p_rng,
877 int mode,
878 mbedtls_md_type_t md_alg,
879 unsigned int hashlen,
880 const unsigned char *hash,
881 unsigned char *sig );
882
920 int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
921 int (*f_rng)(void *, unsigned char *, size_t),
922 void *p_rng,
923 int mode,
924 mbedtls_md_type_t md_alg,
925 unsigned int hashlen,
926 const unsigned char *hash,
927 unsigned char *sig );
928
965 int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
966 int (*f_rng)(void *, unsigned char *, size_t),
967 void *p_rng,
968 int mode,
969 mbedtls_md_type_t md_alg,
970 unsigned int hashlen,
971 const unsigned char *hash,
972 const unsigned char *sig );
973
1003 int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
1004 int (*f_rng)(void *, unsigned char *, size_t),
1005 void *p_rng,
1006 int mode,
1007 mbedtls_md_type_t md_alg,
1008 unsigned int hashlen,
1009 const unsigned char *hash,
1010 const unsigned char *sig );
1011
1052 int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
1053 int (*f_rng)(void *, unsigned char *, size_t),
1054 void *p_rng,
1055 int mode,
1056 mbedtls_md_type_t md_alg,
1057 unsigned int hashlen,
1058 const unsigned char *hash,
1059 const unsigned char *sig );
1060
1090 int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
1091 int (*f_rng)(void *, unsigned char *, size_t),
1092 void *p_rng,
1093 int mode,
1094 mbedtls_md_type_t md_alg,
1095 unsigned int hashlen,
1096 const unsigned char *hash,
1097 mbedtls_md_type_t mgf1_hash_id,
1098 int expected_salt_len,
1099 const unsigned char *sig );
1100
1110 int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
1111
1117 void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
1118
1125 int mbedtls_rsa_self_test( int verbose );
1126
1127 #ifdef __cplusplus
1128 }
1129 #endif
1130
1131 #endif /* rsa.h */
mbedtls_rsa_self_test
int mbedtls_rsa_self_test(int verbose)
The RSA checkup routine.
mbedtls_rsa_context::DP
mbedtls_mpi DP
Definition: rsa.h:107
mbedtls_rsa_export_raw
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.
mbedtls_rsa_rsassa_pkcs1_v15_sign
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).
mbedtls_rsa_public
int mbedtls_rsa_public(mbedtls_rsa_context *ctx, const unsigned char *input, unsigned char *output)
This function performs an RSA public key operation.
mbedtls_rsa_rsaes_oaep_decrypt
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).
mbedtls_rsa_context::hash_id
int hash_id
Definition: rsa.h:122
mbedtls_rsa_import
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.
mbedtls_rsa_pkcs1_verify
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.
config.h
Configuration options (set of defines)
mbedtls_rsa_complete
int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
This function completes an RSA context from a set of imported core parameters.
mbedtls_rsa_context::DQ
mbedtls_mpi DQ
Definition: rsa.h:108
mbedtls_rsa_pkcs1_sign
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.
mbedtls_rsa_set_padding
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...
bignum.h
Multi-precision integer library.
mbedtls_rsa_rsassa_pss_verify_ext
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_rsa_pkcs1_decrypt
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.
mbedtls_rsa_context::ver
int ver
Definition: rsa.h:97
mbedtls_rsa_rsassa_pss_sign
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).
mbedtls_rsa_import_raw
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_rsa_context::RP
mbedtls_mpi RP
Definition: rsa.h:113
mbedtls_rsa_check_pub_priv
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.h
Threading abstraction layer.
mbedtls_rsa_context::P
mbedtls_mpi P
Definition: rsa.h:104
mbedtls_rsa_export
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.
mbedtls_rsa_private
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.
mbedtls_rsa_rsaes_pkcs1_v15_decrypt
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).
mbedtls_rsa_rsassa_pkcs1_v15_verify
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_rsa_context::E
mbedtls_mpi E
Definition: rsa.h:101
mbedtls_rsa_export_crt
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.
mbedtls_rsa_context::len
size_t len
Definition: rsa.h:98
mbedtls_rsa_context::Vf
mbedtls_mpi Vf
Definition: rsa.h:117
mbedtls_rsa_pkcs1_encrypt
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_rsa_context::QP
mbedtls_mpi QP
Definition: rsa.h:109
mbedtls_rsa_context::D
mbedtls_mpi D
Definition: rsa.h:103
mbedtls_rsa_context::padding
int padding
Definition: rsa.h:119
md.h
This file contains the generic message-digest wrapper.
mbedtls_rsa_rsassa_pss_verify
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).
mbedtls_rsa_rsaes_pkcs1_v15_encrypt
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).
mbedtls_rsa_get_len
size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
This function retrieves the length of RSA modulus in Bytes.
mbedtls_mpi
MPI structure.
Definition: bignum.h:180
mbedtls_rsa_context::N
mbedtls_mpi N
Definition: rsa.h:100
mbedtls_rsa_check_pubkey
int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
This function checks if a context contains at least an RSA public key.
mbedtls_rsa_context::RN
mbedtls_mpi RN
Definition: rsa.h:111
mbedtls_rsa_free
void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
This function frees the components of an RSA key.
mbedtls_rsa_context::RQ
mbedtls_mpi RQ
Definition: rsa.h:114
mbedtls_rsa_gen_key
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.
mbedtls_rsa_copy
int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
This function copies the components of an RSA context.
mbedtls_rsa_check_privkey
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_rsa_context::Vi
mbedtls_mpi Vi
Definition: rsa.h:116
mbedtls_md_type_t
mbedtls_md_type_t
Supported message digests.
Definition: md.h:56
mbedtls_rsa_context::Q
mbedtls_mpi Q
Definition: rsa.h:105
mbedtls_rsa_context
The RSA context structure.
Definition: rsa.h:95
mbedtls_rsa_rsaes_oaep_encrypt
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).
mbedtls_rsa_init
void mbedtls_rsa_init(mbedtls_rsa_context *ctx, int padding, int hash_id)
This function initializes an RSA context.