mbed TLS v2.2.0
pk.h
Go to the documentation of this file.
1 
24 #ifndef MBEDTLS_PK_H
25 #define MBEDTLS_PK_H
26 
27 #if !defined(MBEDTLS_CONFIG_FILE)
28 #include "config.h"
29 #else
30 #include MBEDTLS_CONFIG_FILE
31 #endif
32 
33 #include "md.h"
34 
35 #if defined(MBEDTLS_RSA_C)
36 #include "rsa.h"
37 #endif
38 
39 #if defined(MBEDTLS_ECP_C)
40 #include "ecp.h"
41 #endif
42 
43 #if defined(MBEDTLS_ECDSA_C)
44 #include "ecdsa.h"
45 #endif
46 
47 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
48  !defined(inline) && !defined(__cplusplus)
49 #define inline __inline
50 #endif
51 
52 #define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
53 #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
54 #define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
55 #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
56 #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
57 #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
58 #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
59 #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
60 #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
61 #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
62 #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
63 #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
64 #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
65 #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 
74 typedef enum {
83 
88 typedef struct
89 {
92 
94 
98 typedef enum
99 {
104 
108 typedef struct
109 {
111  const char *name;
112  void *value;
114 
116 #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
117 
122 
126 typedef struct
127 {
129  void * pk_ctx;
131 
132 #if defined(MBEDTLS_RSA_C)
133 
139 static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
140 {
141  return( (mbedtls_rsa_context *) (pk).pk_ctx );
142 }
143 #endif /* MBEDTLS_RSA_C */
144 
145 #if defined(MBEDTLS_ECP_C)
146 
152 static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
153 {
154  return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
155 }
156 #endif /* MBEDTLS_ECP_C */
157 
158 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
159 
162 typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
163  const unsigned char *input, unsigned char *output,
164  size_t output_max_len );
165 typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
166  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
167  int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
168  const unsigned char *hash, unsigned char *sig );
169 typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
170 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
171 
180 
185 
190 
205 int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
206 
207 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
208 
222 int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
223  mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
224  mbedtls_pk_rsa_alt_sign_func sign_func,
225  mbedtls_pk_rsa_alt_key_len_func key_len_func );
226 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
227 
235 size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
236 
243 static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
244 {
245  return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
246 }
247 
258 
284  const unsigned char *hash, size_t hash_len,
285  const unsigned char *sig, size_t sig_len );
286 
316 int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
318  const unsigned char *hash, size_t hash_len,
319  const unsigned char *sig, size_t sig_len );
320 
346  const unsigned char *hash, size_t hash_len,
347  unsigned char *sig, size_t *sig_len,
348  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
349 
367  const unsigned char *input, size_t ilen,
368  unsigned char *output, size_t *olen, size_t osize,
369  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
370 
388  const unsigned char *input, size_t ilen,
389  unsigned char *output, size_t *olen, size_t osize,
390  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
391 
400 int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
401 
411 
419 const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
420 
429 
430 #if defined(MBEDTLS_PK_PARSE_C)
431 
450 int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
451  const unsigned char *key, size_t keylen,
452  const unsigned char *pwd, size_t pwdlen );
453 
471 int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
472  const unsigned char *key, size_t keylen );
473 
474 #if defined(MBEDTLS_FS_IO)
475 
491 int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
492  const char *path, const char *password );
493 
509 int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
510 #endif /* MBEDTLS_FS_IO */
511 #endif /* MBEDTLS_PK_PARSE_C */
512 
513 #if defined(MBEDTLS_PK_WRITE_C)
514 
527 int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
528 
542 int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
543 
544 #if defined(MBEDTLS_PEM_WRITE_C)
545 
554 int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
555 
565 int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
566 #endif /* MBEDTLS_PEM_WRITE_C */
567 #endif /* MBEDTLS_PK_WRITE_C */
568 
569 /*
570  * WARNING: Low-level functions. You probably do not want to use these unless
571  * you are certain you do ;)
572  */
573 
574 #if defined(MBEDTLS_PK_PARSE_C)
575 
584 int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
585  mbedtls_pk_context *pk );
586 #endif /* MBEDTLS_PK_PARSE_C */
587 
588 #if defined(MBEDTLS_PK_WRITE_C)
589 
599 int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
600  const mbedtls_pk_context *key );
601 #endif /* MBEDTLS_PK_WRITE_C */
602 
603 /*
604  * Internal module functions. You probably do not want to use these unless you
605  * know you do.
606  */
607 #if defined(MBEDTLS_FS_IO)
608 int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
609 #endif
610 
611 #ifdef __cplusplus
612 }
613 #endif
614 
615 #endif /* MBEDTLS_PK_H */
Options for RSASSA-PSS signature verification.
Definition: pk.h:88
Public key container.
Definition: pk.h:126
void mbedtls_pk_init(mbedtls_pk_context *ctx)
Initialize a mbedtls_pk_context (as NONE)
mbedtls_pk_debug_type
Types for interfacing with the debug module.
Definition: pk.h:98
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.
Elliptic curves over GF(p)
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.
ECP key pair structure.
Definition: ecp.h:163
Elliptic curve DSA.
const mbedtls_pk_info_t * pk_info
Public key informations.
Definition: pk.h:128
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).
size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Get the size in bits of the underlying key.
mbedtls_pk_type_t
Public key types.
Definition: pk.h:74
Compatibility names (set of defines)
void * value
Definition: pk.h:112
static size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Get the length in bytes of the underlying key.
Definition: pk.h:243
mbedtls_pk_type_t type
Public key type.
Definition: pk_internal.h:38
int(* decrypt_func)(void *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.
Definition: pk_internal.h:62
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.
const char * name
Definition: pk.h:111
int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Export debug information.
void * pk_ctx
Underlying public key context.
Definition: pk.h:129
void mbedtls_pk_free(mbedtls_pk_context *ctx)
Free a mbedtls_pk_context.
mbedtls_md_type_t mgf1_hash_id
Definition: pk.h:90
Generic message digest wrapper.
The RSA public-key cryptosystem.
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).
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Get the key type.
const char * mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Access the type name.
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_debug_type type
Definition: pk.h:110
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_md_type_t
Definition: md.h:39
Item to send to the debug module.
Definition: pk.h:108
const mbedtls_pk_info_t * mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Return information associated with the given PK type.
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).
int(* sign_func)(void *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.
Definition: pk_internal.h:55