OPTIGA Trust M  1.1.0
C++ library for Optiga Trust M Chip Security Controller
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
59 /* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
60  */
61 #define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500
63 /* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
64 #define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580
66 /*
67  * RSA constants
68  */
69 #define MBEDTLS_RSA_PUBLIC 0
70 #define MBEDTLS_RSA_PRIVATE 1
72 #define MBEDTLS_RSA_PKCS_V15 0
73 #define MBEDTLS_RSA_PKCS_V21 1
75 #define MBEDTLS_RSA_SIGN 1
76 #define MBEDTLS_RSA_CRYPT 2
78 #define MBEDTLS_RSA_SALT_LEN_ANY -1
79 
80 /*
81  * The above constants may be used even if the RSA module is compile out,
82  * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
83  */
84 
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 
89 #if !defined(MBEDTLS_RSA_ALT)
90 // Regular implementation
91 //
92 
100 typedef struct mbedtls_rsa_context
101 {
102  int ver;
103  size_t len;
124  int padding;
127  int hash_id;
131 #if defined(MBEDTLS_THREADING_C)
132  mbedtls_threading_mutex_t mutex;
133 #endif
134 }
136 
137 #else /* MBEDTLS_RSA_ALT */
138 #include "rsa_alt.h"
139 #endif /* MBEDTLS_RSA_ALT */
140 
169  int padding,
170  int hash_id );
171 
202  const mbedtls_mpi *N,
203  const mbedtls_mpi *P, const mbedtls_mpi *Q,
204  const mbedtls_mpi *D, const mbedtls_mpi *E );
205 
241  unsigned char const *N, size_t N_len,
242  unsigned char const *P, size_t P_len,
243  unsigned char const *Q, size_t Q_len,
244  unsigned char const *D, size_t D_len,
245  unsigned char const *E, size_t E_len );
246 
280 
323  mbedtls_mpi *D, mbedtls_mpi *E );
324 
373  unsigned char *N, size_t N_len,
374  unsigned char *P, size_t P_len,
375  unsigned char *Q, size_t Q_len,
376  unsigned char *D, size_t D_len,
377  unsigned char *E, size_t E_len );
378 
399  mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
400 
410 void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
411  int hash_id );
412 
421 size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx );
422 
442  int (*f_rng)(void *, unsigned char *, size_t),
443  void *p_rng,
444  unsigned int nbits, int exponent );
445 
461 
499 
512  const mbedtls_rsa_context *prv );
513 
534  const unsigned char *input,
535  unsigned char *output );
536 
569  int (*f_rng)(void *, unsigned char *, size_t),
570  void *p_rng,
571  const unsigned char *input,
572  unsigned char *output );
573 
613  int (*f_rng)(void *, unsigned char *, size_t),
614  void *p_rng,
615  int mode, size_t ilen,
616  const unsigned char *input,
617  unsigned char *output );
618 
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 
697  int (*f_rng)(void *, unsigned char *, size_t),
698  void *p_rng,
699  int mode,
700  const unsigned char *label, size_t label_len,
701  size_t ilen,
702  const unsigned char *input,
703  unsigned char *output );
704 
750  int (*f_rng)(void *, unsigned char *, size_t),
751  void *p_rng,
752  int mode, size_t *olen,
753  const unsigned char *input,
754  unsigned char *output,
755  size_t output_max_len );
756 
800  int (*f_rng)(void *, unsigned char *, size_t),
801  void *p_rng,
802  int mode, size_t *olen,
803  const unsigned char *input,
804  unsigned char *output,
805  size_t output_max_len );
806 
854  int (*f_rng)(void *, unsigned char *, size_t),
855  void *p_rng,
856  int mode,
857  const unsigned char *label, size_t label_len,
858  size_t *olen,
859  const unsigned char *input,
860  unsigned char *output,
861  size_t output_max_len );
862 
913  int (*f_rng)(void *, unsigned char *, size_t),
914  void *p_rng,
915  int mode,
916  mbedtls_md_type_t md_alg,
917  unsigned int hashlen,
918  const unsigned char *hash,
919  unsigned char *sig );
920 
960  int (*f_rng)(void *, unsigned char *, size_t),
961  void *p_rng,
962  int mode,
963  mbedtls_md_type_t md_alg,
964  unsigned int hashlen,
965  const unsigned char *hash,
966  unsigned char *sig );
967 
1021  int (*f_rng)(void *, unsigned char *, size_t),
1022  void *p_rng,
1023  int mode,
1024  mbedtls_md_type_t md_alg,
1025  unsigned int hashlen,
1026  const unsigned char *hash,
1027  unsigned char *sig );
1028 
1074  int (*f_rng)(void *, unsigned char *, size_t),
1075  void *p_rng,
1076  int mode,
1077  mbedtls_md_type_t md_alg,
1078  unsigned int hashlen,
1079  const unsigned char *hash,
1080  const unsigned char *sig );
1081 
1120  int (*f_rng)(void *, unsigned char *, size_t),
1121  void *p_rng,
1122  int mode,
1123  mbedtls_md_type_t md_alg,
1124  unsigned int hashlen,
1125  const unsigned char *hash,
1126  const unsigned char *sig );
1127 
1177  int (*f_rng)(void *, unsigned char *, size_t),
1178  void *p_rng,
1179  int mode,
1180  mbedtls_md_type_t md_alg,
1181  unsigned int hashlen,
1182  const unsigned char *hash,
1183  const unsigned char *sig );
1184 
1225  int (*f_rng)(void *, unsigned char *, size_t),
1226  void *p_rng,
1227  int mode,
1228  mbedtls_md_type_t md_alg,
1229  unsigned int hashlen,
1230  const unsigned char *hash,
1231  mbedtls_md_type_t mgf1_hash_id,
1232  int expected_salt_len,
1233  const unsigned char *sig );
1234 
1245 
1254 
1261 int mbedtls_rsa_self_test( int verbose );
1262 
1263 #ifdef __cplusplus
1264 }
1265 #endif
1266 
1267 #endif /* rsa.h */
int mbedtls_rsa_self_test(int verbose)
The RSA checkup routine.
mbedtls_mpi DP
Definition: rsa.h:112
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 hash_id
Definition: rsa.h:127
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:113
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).
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 ver
Definition: rsa.h:102
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.
#define P(a, b, c, d, e, f, g, h, x, K)
Definition: sha256.c:193
mbedtls_mpi RP
Definition: rsa.h:118
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:109
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:106
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:103
mbedtls_mpi Vf
Definition: rsa.h:122
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:114
mbedtls_mpi D
Definition: rsa.h:108
int padding
Definition: rsa.h:124
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:180
mbedtls_mpi N
Definition: rsa.h:105
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:116
void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
This function frees the components of an RSA key.
mbedtls_mpi RQ
Definition: rsa.h:119
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:121
mbedtls_md_type_t
Supported message digests.
Definition: md.h:58
mbedtls_mpi Q
Definition: rsa.h:110
The RSA context structure.
Definition: rsa.h:100
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.