1 /**
2  * \file pk_wrap.h
3  *
4  * \brief Public Key abstraction layer: wrapper functions
5  */
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0
9  *
10  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
11  *  not use this file except in compliance with the License.
12  *  You may obtain a copy of the License at
13  *
14  *  http://www.apache.org/licenses/LICENSE-2.0
15  *
16  *  Unless required by applicable law or agreed to in writing, software
17  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  *  See the License for the specific language governing permissions and
20  *  limitations under the License.
21  */
22 
23 #ifndef MBEDTLS_PK_WRAP_H
24 #define MBEDTLS_PK_WRAP_H
25 
26 #include "mbedtls/build_info.h"
27 
28 #include "mbedtls/pk.h"
29 
30 #if defined(MBEDTLS_PSA_CRYPTO_C)
31 #include "psa/crypto.h"
32 #endif /* MBEDTLS_PSA_CRYPTO_C */
33 
34 struct mbedtls_pk_info_t
35 {
36     /** Public key type */
37     mbedtls_pk_type_t type;
38 
39     /** Type name */
40     const char *name;
41 
42     /** Get key size in bits */
43     size_t (*get_bitlen)( const void * );
44 
45     /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */
46     int (*can_do)( mbedtls_pk_type_t type );
47 
48     /** Verify signature */
49     int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg,
50                         const unsigned char *hash, size_t hash_len,
51                         const unsigned char *sig, size_t sig_len );
52 
53     /** Make signature */
54     int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg,
55                       const unsigned char *hash, size_t hash_len,
56                       unsigned char *sig, size_t sig_size, size_t *sig_len,
57                       int (*f_rng)(void *, unsigned char *, size_t),
58                       void *p_rng );
59 
60 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
61     /** Verify signature (restartable) */
62     int (*verify_rs_func)( void *ctx, mbedtls_md_type_t md_alg,
63                            const unsigned char *hash, size_t hash_len,
64                            const unsigned char *sig, size_t sig_len,
65                            void *rs_ctx );
66 
67     /** Make signature (restartable) */
68     int (*sign_rs_func)( void *ctx, mbedtls_md_type_t md_alg,
69                          const unsigned char *hash, size_t hash_len,
70                          unsigned char *sig, size_t sig_size, size_t *sig_len,
71                          int (*f_rng)(void *, unsigned char *, size_t),
72                          void *p_rng, void *rs_ctx );
73 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
74 
75     /** Decrypt message */
76     int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
77                          unsigned char *output, size_t *olen, size_t osize,
78                          int (*f_rng)(void *, unsigned char *, size_t),
79                          void *p_rng );
80 
81     /** Encrypt message */
82     int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
83                          unsigned char *output, size_t *olen, size_t osize,
84                          int (*f_rng)(void *, unsigned char *, size_t),
85                          void *p_rng );
86 
87     /** Check public-private key pair */
88     int (*check_pair_func)( const void *pub, const void *prv,
89                             int (*f_rng)(void *, unsigned char *, size_t),
90                             void *p_rng );
91 
92     /** Allocate a new context */
93     void * (*ctx_alloc_func)( void );
94 
95     /** Free the given context */
96     void (*ctx_free_func)( void *ctx );
97 
98 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
99     /** Allocate the restart context */
100     void * (*rs_alloc_func)( void );
101 
102     /** Free the restart context */
103     void (*rs_free_func)( void *rs_ctx );
104 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
105 
106     /** Interface with the debug module */
107     void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items );
108 
109 };
110 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
111 /* Container for RSA-alt */
112 typedef struct
113 {
114     void *key;
115     mbedtls_pk_rsa_alt_decrypt_func decrypt_func;
116     mbedtls_pk_rsa_alt_sign_func sign_func;
117     mbedtls_pk_rsa_alt_key_len_func key_len_func;
118 } mbedtls_rsa_alt_context;
119 #endif
120 
121 #if defined(MBEDTLS_RSA_C)
122 extern const mbedtls_pk_info_t mbedtls_rsa_info;
123 #endif
124 
125 #if defined(MBEDTLS_ECP_C)
126 extern const mbedtls_pk_info_t mbedtls_eckey_info;
127 extern const mbedtls_pk_info_t mbedtls_eckeydh_info;
128 #endif
129 
130 #if defined(MBEDTLS_ECDSA_C)
131 extern const mbedtls_pk_info_t mbedtls_ecdsa_info;
132 #endif
133 
134 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
135 extern const mbedtls_pk_info_t mbedtls_rsa_alt_info;
136 #endif
137 
138 #if defined(MBEDTLS_USE_PSA_CRYPTO)
139 extern const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info;
140 extern const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info;
141 
142 #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
143 int mbedtls_pk_error_from_psa_ecdsa( psa_status_t status );
144 #endif
145 
146 #endif /* MBEDTLS_USE_PSA_CRYPTO */
147 
148 #if defined(MBEDTLS_PSA_CRYPTO_C)
149 int mbedtls_pk_error_from_psa( psa_status_t status );
150 
151 #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) ||    \
152     defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
153 int mbedtls_pk_error_from_psa_rsa( psa_status_t status );
154 #endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
155 
156 #if defined(MBEDTLS_RSA_C)
157 int  mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t psa_alg_md,
158                                   mbedtls_rsa_context *rsa_ctx,
159                                   const unsigned char *hash, size_t hash_len,
160                                   unsigned char *sig, size_t sig_size,
161                                   size_t *sig_len );
162 #endif /* MBEDTLS_RSA_C */
163 
164 #endif /* MBEDTLS_PSA_CRYPTO_C */
165 
166 #endif /* MBEDTLS_PK_WRAP_H */
167