mbed TLS v2.16.3: pk.h Source File

mbed TLS v2.16.3

pk.h
Go to the documentation of this file.
1
6 /*
7 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * This file is part of mbed TLS (https://tls.mbed.org)
23 */
24
25 #ifndef MBEDTLS_PK_H
26 #define MBEDTLS_PK_H
27
28 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include "config.h"
30 #else
31 #include MBEDTLS_CONFIG_FILE
32 #endif
33
34 #include "md.h"
35
36 #if defined(MBEDTLS_RSA_C)
37 #include "rsa.h"
38 #endif
39
40 #if defined(MBEDTLS_ECP_C)
41 #include "ecp.h"
42 #endif
43
44 #if defined(MBEDTLS_ECDSA_C)
45 #include "ecdsa.h"
46 #endif
47
48 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
49 !defined(inline) && !defined(__cplusplus)
50 #define inline __inline
51 #endif
52
53 #define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
54 #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
55 #define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
56 #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
57 #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
58 #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
59 #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
60 #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
61 #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
62 #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
63 #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
64 #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
65 #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
66 #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
68 /* MBEDTLS_ERR_PK_HW_ACCEL_FAILED is deprecated and should not be used. */
69 #define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74
78 typedef enum {
79 MBEDTLS_PK_NONE=0,
80 MBEDTLS_PK_RSA,
81 MBEDTLS_PK_ECKEY,
82 MBEDTLS_PK_ECKEY_DH,
83 MBEDTLS_PK_ECDSA,
84 MBEDTLS_PK_RSA_ALT,
85 MBEDTLS_PK_RSASSA_PSS,
86 } mbedtls_pk_type_t;
87
92 typedef struct mbedtls_pk_rsassa_pss_options
93 {
94 mbedtls_md_type_t mgf1_hash_id;
95 int expected_salt_len;
96
97 } mbedtls_pk_rsassa_pss_options;
98
102 typedef enum
103 {
104 MBEDTLS_PK_DEBUG_NONE = 0,
105 MBEDTLS_PK_DEBUG_MPI,
106 MBEDTLS_PK_DEBUG_ECP,
107 } mbedtls_pk_debug_type;
108
112 typedef struct mbedtls_pk_debug_item
113 {
114 mbedtls_pk_debug_type type;
115 const char *name;
116 void *value;
117 } mbedtls_pk_debug_item;
118
120 #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
121
125 typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
126
130 typedef struct mbedtls_pk_context
131 {
132 const mbedtls_pk_info_t * pk_info;
133 void * pk_ctx;
134 } mbedtls_pk_context;
135
136 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
137
140 typedef struct
141 {
142 const mbedtls_pk_info_t * pk_info;
143 void * rs_ctx;
144 } mbedtls_pk_restart_ctx;
145 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
146 /* Now we can declare functions that take a pointer to that */
147 typedef void mbedtls_pk_restart_ctx;
148 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
149
150 #if defined(MBEDTLS_RSA_C)
151
157 static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
158 {
159 return( (mbedtls_rsa_context *) (pk).pk_ctx );
160 }
161 #endif /* MBEDTLS_RSA_C */
162
163 #if defined(MBEDTLS_ECP_C)
164
170 static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
171 {
172 return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
173 }
174 #endif /* MBEDTLS_ECP_C */
175
176 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
177
180 typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
181 const unsigned char *input, unsigned char *output,
182 size_t output_max_len );
183 typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
184 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
185 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
186 const unsigned char *hash, unsigned char *sig );
187 typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
188 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
189
197 const mbedtls_pk_info_t *mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
198
205 void mbedtls_pk_init( mbedtls_pk_context *ctx );
206
213 void mbedtls_pk_free( mbedtls_pk_context *ctx );
214
215 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
216
222 void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx );
223
230 void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
231 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
232
248 int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
249
250 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
251
266 int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
267 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
268 mbedtls_pk_rsa_alt_sign_func sign_func,
269 mbedtls_pk_rsa_alt_key_len_func key_len_func );
270 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
271
279 size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
280
288 static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
289 {
290 return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
291 }
292
305 int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
306
331 int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
332 const unsigned char *hash, size_t hash_len,
333 const unsigned char *sig, size_t sig_len );
334
355 int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
356 mbedtls_md_type_t md_alg,
357 const unsigned char *hash, size_t hash_len,
358 const unsigned char *sig, size_t sig_len,
359 mbedtls_pk_restart_ctx *rs_ctx );
360
390 int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
391 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
392 const unsigned char *hash, size_t hash_len,
393 const unsigned char *sig, size_t sig_len );
394
424 int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
425 const unsigned char *hash, size_t hash_len,
426 unsigned char *sig, size_t *sig_len,
427 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
428
456 int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
457 mbedtls_md_type_t md_alg,
458 const unsigned char *hash, size_t hash_len,
459 unsigned char *sig, size_t *sig_len,
460 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
461 mbedtls_pk_restart_ctx *rs_ctx );
462
480 int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
481 const unsigned char *input, size_t ilen,
482 unsigned char *output, size_t *olen, size_t osize,
483 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
484
501 int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
502 const unsigned char *input, size_t ilen,
503 unsigned char *output, size_t *olen, size_t osize,
504 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
505
514 int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
515
524 int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
525
533 const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
534
543 mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
544
545 #if defined(MBEDTLS_PK_PARSE_C)
546
575 int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
576 const unsigned char *key, size_t keylen,
577 const unsigned char *pwd, size_t pwdlen );
578
601 int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
602 const unsigned char *key, size_t keylen );
603
604 #if defined(MBEDTLS_FS_IO)
605
626 int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
627 const char *path, const char *password );
628
646 int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
647 #endif /* MBEDTLS_FS_IO */
648 #endif /* MBEDTLS_PK_PARSE_C */
649
650 #if defined(MBEDTLS_PK_WRITE_C)
651
664 int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
665
679 int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
680
681 #if defined(MBEDTLS_PEM_WRITE_C)
682
692 int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
693
704 int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
705 #endif /* MBEDTLS_PEM_WRITE_C */
706 #endif /* MBEDTLS_PK_WRITE_C */
707
708 /*
709 * WARNING: Low-level functions. You probably do not want to use these unless
710 * you are certain you do ;)
711 */
712
713 #if defined(MBEDTLS_PK_PARSE_C)
714
724 int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
725 mbedtls_pk_context *pk );
726 #endif /* MBEDTLS_PK_PARSE_C */
727
728 #if defined(MBEDTLS_PK_WRITE_C)
729
739 int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
740 const mbedtls_pk_context *key );
741 #endif /* MBEDTLS_PK_WRITE_C */
742
743 /*
744 * Internal module functions. You probably do not want to use these unless you
745 * know you do.
746 */
747 #if defined(MBEDTLS_FS_IO)
748 int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
749 #endif
750
751 #ifdef __cplusplus
752 }
753 #endif
754
755 #endif /* MBEDTLS_PK_H */
mbedtls_pk_parse_public_keyfile
int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Load and parse a public key.
MBEDTLS_PK_RSA_ALT
Definition: pk.h:84
MBEDTLS_PK_ECKEY_DH
Definition: pk.h:82
mbedtls_pk_debug_item::name
const char * name
Definition: pk.h:115
mbedtls_pk_debug_item
struct mbedtls_pk_debug_item mbedtls_pk_debug_item
Item to send to the debug module.
mbedtls_pk_sign_restartable
int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, mbedtls_pk_restart_ctx *rs_ctx)
Restartable version of mbedtls_pk_sign()
mbedtls_md_type_t
mbedtls_md_type_t
Supported message digests.
Definition: md.h:58
mbedtls_pk_verify_restartable
int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len, mbedtls_pk_restart_ctx *rs_ctx)
Restartable version of mbedtls_pk_verify()
ecp.h
This file provides an API for Elliptic Curves over GF(P) (ECP).
mbedtls_pk_debug_item::value
void * value
Definition: pk.h:116
MBEDTLS_PK_DEBUG_MPI
Definition: pk.h:105
mbedtls_pk_parse_subpubkey
int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end, mbedtls_pk_context *pk)
Parse a SubjectPublicKeyInfo DER structure.
ecdsa.h
This file contains ECDSA definitions and functions.
mbedtls_pk_can_do
int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Tell if a context can do the operation given by type.
mbedtls_pk_rsassa_pss_options
Options for RSASSA-PSS signature verification. See mbedtls_rsa_rsassa_pss_verify_ext()
Definition: pk.h:92
mbedtls_pk_verify
int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature (including padding if relevant).
md.h
This file contains the generic message-digest wrapper.
mbedtls_pk_context
Public key container.
Definition: pk.h:130
mbedtls_pk_write_pubkey
int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start, const mbedtls_pk_context *key)
Write a subjectPublicKey to ASN.1 data Note: function works backwards in data buffer.
mbedtls_pk_parse_keyfile
int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx, const char *path, const char *password)
Load and parse a private key.
mbedtls_pk_get_type
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Get the key type.
mbedtls_pk_context::pk_ctx
void * pk_ctx
Definition: pk.h:133
mbedtls_pk_context::pk_info
const mbedtls_pk_info_t * pk_info
Definition: pk.h:132
mbedtls_pk_setup
int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Initialize a PK context with the information given and allocates the type-specific PK subcontext.
mbedtls_pk_ec
static mbedtls_ecp_keypair * mbedtls_pk_ec(const mbedtls_pk_context pk)
Definition: pk.h:170
MBEDTLS_PK_DEBUG_NONE
Definition: pk.h:104
mbedtls_pk_parse_public_key
int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx, const unsigned char *key, size_t keylen)
Parse a public key in PEM or DER format.
mbedtls_pk_restart_ctx
void mbedtls_pk_restart_ctx
Definition: pk.h:147
mbedtls_pk_rsa_alt_key_len_func
size_t(* mbedtls_pk_rsa_alt_key_len_func)(void *ctx)
Definition: pk.h:187
mbedtls_pk_rsassa_pss_options::expected_salt_len
int expected_salt_len
Definition: pk.h:95
mbedtls_pk_debug
int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Export debug information.
mbedtls_pk_debug_item::type
mbedtls_pk_debug_type type
Definition: pk.h:114
mbedtls_ecp_keypair
The ECP key-pair structure.
Definition: ecp.h:332
rsa.h
This file provides an API for the RSA public-key cryptosystem.
mbedtls_pk_get_bitlen
size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Get the size in bits of the underlying key.
mbedtls_pk_get_len
static size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Get the length in bytes of the underlying key.
Definition: pk.h:288
mbedtls_pk_debug_item
Item to send to the debug module.
Definition: pk.h:112
mbedtls_pk_parse_key
int mbedtls_pk_parse_key(mbedtls_pk_context *ctx, const unsigned char *key, size_t keylen, const unsigned char *pwd, size_t pwdlen)
Parse a private key in PEM or DER format.
mbedtls_pk_decrypt
int mbedtls_pk_decrypt(mbedtls_pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Decrypt message (including padding if relevant).
MBEDTLS_PK_NONE
Definition: pk.h:79
mbedtls_pk_verify_ext
int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options, mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature, with options. (Includes verification of the padding depending on type....
mbedtls_pk_type_t
mbedtls_pk_type_t
Public key types.
Definition: pk.h:78
mbedtls_pk_setup_rsa_alt
int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key, mbedtls_pk_rsa_alt_decrypt_func decrypt_func, mbedtls_pk_rsa_alt_sign_func sign_func, mbedtls_pk_rsa_alt_key_len_func key_len_func)
Initialize an RSA-alt context.
MBEDTLS_PK_RSASSA_PSS
Definition: pk.h:85
mbedtls_rsa_context
The RSA context structure.
Definition: rsa.h:100
MBEDTLS_PK_ECDSA
Definition: pk.h:83
mbedtls_pk_rsa_alt_decrypt_func
int(* mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Types for RSA-alt abstraction.
Definition: pk.h:180
mbedtls_pk_context
struct mbedtls_pk_context mbedtls_pk_context
Public key container.
mbedtls_pk_rsassa_pss_options::mgf1_hash_id
mbedtls_md_type_t mgf1_hash_id
Definition: pk.h:94
mbedtls_pk_write_pubkey_der
int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a SubjectPublicKeyInfo DER structure Note: data is written at the end of the bu...
mbedtls_pk_write_key_der
int mbedtls_pk_write_key_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 DER structure Note: data is written at the end of the buffer!...
mbedtls_pk_get_name
const char * mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Access the type name.
mbedtls_pk_debug_type
mbedtls_pk_debug_type
Types for interfacing with the debug module.
Definition: pk.h:102
mbedtls_pk_info_from_type
const mbedtls_pk_info_t * mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Return information associated with the given PK type.
mbedtls_pk_info_t
struct mbedtls_pk_info_t mbedtls_pk_info_t
Public key information and operations.
Definition: pk.h:125
config.h
Configuration options (set of defines)
MBEDTLS_PK_ECKEY
Definition: pk.h:81
mbedtls_pk_write_pubkey_pem
int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a PEM string.
MBEDTLS_PK_DEBUG_ECP
Definition: pk.h:106
mbedtls_pk_sign
int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Make signature, including padding if relevant.
mbedtls_pk_write_key_pem
int mbedtls_pk_write_key_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 PEM string.
mbedtls_pk_encrypt
int mbedtls_pk_encrypt(mbedtls_pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Encrypt message (including padding if relevant).
mbedtls_pk_init
void mbedtls_pk_init(mbedtls_pk_context *ctx)
Initialize a mbedtls_pk_context (as NONE).
mbedtls_pk_check_pair
int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv)
Check if a public-private pair of keys matches.
mbedtls_pk_rsassa_pss_options
struct mbedtls_pk_rsassa_pss_options mbedtls_pk_rsassa_pss_options
Options for RSASSA-PSS signature verification. See mbedtls_rsa_rsassa_pss_verify_ext()
mbedtls_pk_rsa
static mbedtls_rsa_context * mbedtls_pk_rsa(const mbedtls_pk_context pk)
Definition: pk.h:157
MBEDTLS_PK_RSA
Definition: pk.h:80
mbedtls_pk_free
void mbedtls_pk_free(mbedtls_pk_context *ctx)
Free the components of a mbedtls_pk_context.
mbedtls_pk_rsa_alt_sign_func
int(* mbedtls_pk_rsa_alt_sign_func)(void *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)
Definition: pk.h:183
mbedtls_pk_load_file
int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)