mbed TLS v2.0.0
pk.h
Go to the documentation of this file.
1 
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 #define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
49 #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
50 #define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
51 #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
52 #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
53 #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
54 #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
55 #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
56 #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
57 #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
58 #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
59 #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
60 #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
61 #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
71 typedef enum {
80 
85 typedef struct
86 {
89 
91 
95 typedef enum
96 {
101 
105 typedef struct
106 {
108  const char *name;
109  void *value;
111 
113 #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
114 
119 
123 typedef struct
124 {
126  void * pk_ctx;
128 
129 #if defined(MBEDTLS_RSA_C)
130 
137 {
138  return( (mbedtls_rsa_context *) (pk).pk_ctx );
139 }
140 #endif /* MBEDTLS_RSA_C */
141 
142 #if defined(MBEDTLS_ECP_C)
143 
150 {
151  return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
152 }
153 #endif /* MBEDTLS_ECP_C */
154 
155 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
156 
159 typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
160  const unsigned char *input, unsigned char *output,
161  size_t output_max_len );
162 typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
163  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
164  int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
165  const unsigned char *hash, unsigned char *sig );
166 typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
167 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
168 
177 
182 
187 
202 int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
203 
204 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
205 
219 int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
222  mbedtls_pk_rsa_alt_key_len_func key_len_func );
223 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
224 
232 size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
233 
240 static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
241 {
242  return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
243 }
244 
255 
281  const unsigned char *hash, size_t hash_len,
282  const unsigned char *sig, size_t sig_len );
283 
313 int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
315  const unsigned char *hash, size_t hash_len,
316  const unsigned char *sig, size_t sig_len );
317 
343  const unsigned char *hash, size_t hash_len,
344  unsigned char *sig, size_t *sig_len,
345  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
346 
364  const unsigned char *input, size_t ilen,
365  unsigned char *output, size_t *olen, size_t osize,
366  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
367 
385  const unsigned char *input, size_t ilen,
386  unsigned char *output, size_t *olen, size_t osize,
387  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
388 
397 int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
398 
408 
416 const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
417 
426 
427 #if defined(MBEDTLS_PK_PARSE_C)
428 
448  const unsigned char *key, size_t keylen,
449  const unsigned char *pwd, size_t pwdlen );
450 
469  const unsigned char *key, size_t keylen );
470 
471 #if defined(MBEDTLS_FS_IO)
472 
489  const char *path, const char *password );
490 
506 int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
507 #endif /* MBEDTLS_FS_IO */
508 #endif /* MBEDTLS_PK_PARSE_C */
509 
510 #if defined(MBEDTLS_PK_WRITE_C)
511 
524 int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
525 
539 int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
540 
541 #if defined(MBEDTLS_PEM_WRITE_C)
542 
551 int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
552 
562 int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
563 #endif /* MBEDTLS_PEM_WRITE_C */
564 #endif /* MBEDTLS_PK_WRITE_C */
565 
566 /*
567  * WARNING: Low-level functions. You probably do not want to use these unless
568  * you are certain you do ;)
569  */
570 
571 #if defined(MBEDTLS_PK_PARSE_C)
572 
581 int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
582  mbedtls_pk_context *pk );
583 #endif /* MBEDTLS_PK_PARSE_C */
584 
585 #if defined(MBEDTLS_PK_WRITE_C)
586 
596 int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
597  const mbedtls_pk_context *key );
598 #endif /* MBEDTLS_PK_WRITE_C */
599 
600 /*
601  * Internal module functions. You probably do not want to use these unless you
602  * know you do.
603  */
604 #if defined(MBEDTLS_FS_IO)
605 int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
606 #endif
607 
608 #ifdef __cplusplus
609 }
610 #endif
611 
612 #endif /* MBEDTLS_PK_H */
int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a PEM string.
Options for RSASSA-PSS signature verification.
Definition: pk.h:85
Public key container.
Definition: pk.h:123
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.
int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end, mbedtls_pk_context *pk)
Parse a SubjectPublicKeyInfo DER structure.
void mbedtls_pk_init(mbedtls_pk_context *ctx)
Initialize a mbedtls_pk_context (as NONE)
static mbedtls_rsa_context * mbedtls_pk_rsa(const mbedtls_pk_context pk)
Quick access to an RSA context inside a PK context.
Definition: pk.h:136
mbedtls_pk_debug_type
Types for interfacing with the debug module.
Definition: pk.h:95
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.
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...
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.
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:157
Elliptic curve DSA.
const mbedtls_pk_info_t * pk_info
Public key informations.
Definition: pk.h:125
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:71
Compatibility names (set of defines)
void * value
Definition: pk.h:109
static size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Get the length in bytes of the underlying key.
Definition: pk.h:240
mbedtls_pk_type_t type
Public key type.
Definition: pk_internal.h:39
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:63
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:56
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:108
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:126
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.
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.
void mbedtls_pk_free(mbedtls_pk_context *ctx)
Free a mbedtls_pk_context.
mbedtls_md_type_t mgf1_hash_id
Definition: pk.h:87
size_t(* mbedtls_pk_rsa_alt_key_len_func)(void *ctx)
Definition: pk.h:166
int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx, const char *path, const char *password)
Load and parse a private key.
The RSA public-key cryptosystem.
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:159
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.
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.
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:162
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!...
const char * mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Access the type name.
int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
static mbedtls_ecp_keypair * mbedtls_pk_ec(const mbedtls_pk_context pk)
Quick access to an EC context inside a PK context.
Definition: pk.h:149
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:107
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:44
RSA context structure.
Definition: rsa.h:80
Item to send to the debug module.
Definition: pk.h:105
int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Load and parse a public key.
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).