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 struct mbedtls_pk_info_t
31 {
32     /** Public key type */
33     mbedtls_pk_type_t type;
34 
35     /** Type name */
36     const char *name;
37 
38     /** Get key size in bits */
39     size_t (*get_bitlen)( const void * );
40 
41     /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */
42     int (*can_do)( mbedtls_pk_type_t type );
43 
44     /** Verify signature */
45     int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg,
46                         const unsigned char *hash, size_t hash_len,
47                         const unsigned char *sig, size_t sig_len );
48 
49     /** Make signature */
50     int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg,
51                       const unsigned char *hash, size_t hash_len,
52                       unsigned char *sig, size_t sig_size, size_t *sig_len,
53                       int (*f_rng)(void *, unsigned char *, size_t),
54                       void *p_rng );
55 
56 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
57     /** Verify signature (restartable) */
58     int (*verify_rs_func)( void *ctx, mbedtls_md_type_t md_alg,
59                            const unsigned char *hash, size_t hash_len,
60                            const unsigned char *sig, size_t sig_len,
61                            void *rs_ctx );
62 
63     /** Make signature (restartable) */
64     int (*sign_rs_func)( void *ctx, mbedtls_md_type_t md_alg,
65                          const unsigned char *hash, size_t hash_len,
66                          unsigned char *sig, size_t sig_size, size_t *sig_len,
67                          int (*f_rng)(void *, unsigned char *, size_t),
68                          void *p_rng, void *rs_ctx );
69 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
70 
71     /** Decrypt message */
72     int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
73                          unsigned char *output, size_t *olen, size_t osize,
74                          int (*f_rng)(void *, unsigned char *, size_t),
75                          void *p_rng );
76 
77     /** Encrypt message */
78     int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
79                          unsigned char *output, size_t *olen, size_t osize,
80                          int (*f_rng)(void *, unsigned char *, size_t),
81                          void *p_rng );
82 
83     /** Check public-private key pair */
84     int (*check_pair_func)( const void *pub, const void *prv,
85                             int (*f_rng)(void *, unsigned char *, size_t),
86                             void *p_rng );
87 
88     /** Allocate a new context */
89     void * (*ctx_alloc_func)( void );
90 
91     /** Free the given context */
92     void (*ctx_free_func)( void *ctx );
93 
94 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
95     /** Allocate the restart context */
96     void * (*rs_alloc_func)( void );
97 
98     /** Free the restart context */
99     void (*rs_free_func)( void *rs_ctx );
100 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
101 
102     /** Interface with the debug module */
103     void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items );
104 
105 };
106 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
107 /* Container for RSA-alt */
108 typedef struct
109 {
110     void *key;
111     mbedtls_pk_rsa_alt_decrypt_func decrypt_func;
112     mbedtls_pk_rsa_alt_sign_func sign_func;
113     mbedtls_pk_rsa_alt_key_len_func key_len_func;
114 } mbedtls_rsa_alt_context;
115 #endif
116 
117 #if defined(MBEDTLS_RSA_C)
118 extern const mbedtls_pk_info_t mbedtls_rsa_info;
119 #endif
120 
121 #if defined(MBEDTLS_ECP_C)
122 extern const mbedtls_pk_info_t mbedtls_eckey_info;
123 extern const mbedtls_pk_info_t mbedtls_eckeydh_info;
124 #endif
125 
126 #if defined(MBEDTLS_ECDSA_C)
127 extern const mbedtls_pk_info_t mbedtls_ecdsa_info;
128 #endif
129 
130 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
131 extern const mbedtls_pk_info_t mbedtls_rsa_alt_info;
132 #endif
133 
134 #if defined(MBEDTLS_USE_PSA_CRYPTO)
135 extern const mbedtls_pk_info_t mbedtls_pk_opaque_info;
136 #endif
137 
138 #endif /* MBEDTLS_PK_WRAP_H */
139