mbed TLS v2.14.1
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 {
87 
93 {
96 
98 
102 typedef enum
103 {
108 
112 typedef struct mbedtls_pk_debug_item
113 {
115  const char *name;
116  void *value;
118 
120 #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
121 
126 
130 typedef struct mbedtls_pk_context
131 {
133  void * pk_ctx;
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;
145 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
146 /* Now we can declare functions that take a pointer to that */
148 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
149 
150 #if defined(MBEDTLS_RSA_C)
151 
158 {
159  return( (mbedtls_rsa_context *) (pk).pk_ctx );
160 }
161 #endif /* MBEDTLS_RSA_C */
162 
163 #if defined(MBEDTLS_ECP_C)
164 
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 
198 
203 
208 
209 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
210 
213 void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx );
214 
218 void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
219 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
220 
235 int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
236 
237 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
238 
252 int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
253  mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
255  mbedtls_pk_rsa_alt_key_len_func key_len_func );
256 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
257 
265 size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
266 
273 static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
274 {
275  return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
276 }
277 
288 
314  const unsigned char *hash, size_t hash_len,
315  const unsigned char *sig, size_t sig_len );
316 
338  mbedtls_md_type_t md_alg,
339  const unsigned char *hash, size_t hash_len,
340  const unsigned char *sig, size_t sig_len,
341  mbedtls_pk_restart_ctx *rs_ctx );
342 
372 int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
374  const unsigned char *hash, size_t hash_len,
375  const unsigned char *sig, size_t sig_len );
376 
402  const unsigned char *hash, size_t hash_len,
403  unsigned char *sig, size_t *sig_len,
404  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
405 
429  mbedtls_md_type_t md_alg,
430  const unsigned char *hash, size_t hash_len,
431  unsigned char *sig, size_t *sig_len,
432  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
433  mbedtls_pk_restart_ctx *rs_ctx );
434 
452  const unsigned char *input, size_t ilen,
453  unsigned char *output, size_t *olen, size_t osize,
454  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
455 
473  const unsigned char *input, size_t ilen,
474  unsigned char *output, size_t *olen, size_t osize,
475  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
476 
485 int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
486 
496 
504 const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
505 
514 
515 #if defined(MBEDTLS_PK_PARSE_C)
516 
536  const unsigned char *key, size_t keylen,
537  const unsigned char *pwd, size_t pwdlen );
538 
557  const unsigned char *key, size_t keylen );
558 
559 #if defined(MBEDTLS_FS_IO)
560 
577  const char *path, const char *password );
578 
595 int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
596 #endif /* MBEDTLS_FS_IO */
597 #endif /* MBEDTLS_PK_PARSE_C */
598 
599 #if defined(MBEDTLS_PK_WRITE_C)
600 
613 int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
614 
628 int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
629 
630 #if defined(MBEDTLS_PEM_WRITE_C)
631 
640 int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
641 
651 int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
652 #endif /* MBEDTLS_PEM_WRITE_C */
653 #endif /* MBEDTLS_PK_WRITE_C */
654 
655 /*
656  * WARNING: Low-level functions. You probably do not want to use these unless
657  * you are certain you do ;)
658  */
659 
660 #if defined(MBEDTLS_PK_PARSE_C)
661 
670 int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
671  mbedtls_pk_context *pk );
672 #endif /* MBEDTLS_PK_PARSE_C */
673 
674 #if defined(MBEDTLS_PK_WRITE_C)
675 
685 int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
686  const mbedtls_pk_context *key );
687 #endif /* MBEDTLS_PK_WRITE_C */
688 
689 /*
690  * Internal module functions. You probably do not want to use these unless you
691  * know you do.
692  */
693 #if defined(MBEDTLS_FS_IO)
694 int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
695 #endif
696 
697 #ifdef __cplusplus
698 }
699 #endif
700 
701 #endif /* MBEDTLS_PK_H */