PolarSSL v1.3.3
pk.h
Go to the documentation of this file.
1 
28 #ifndef POLARSSL_PK_H
29 #define POLARSSL_PK_H
30 
31 #include "config.h"
32 
33 #include "md.h"
34 
35 #if defined(POLARSSL_RSA_C)
36 #include "rsa.h"
37 #endif
38 
39 #if defined(POLARSSL_ECP_C)
40 #include "ecp.h"
41 #endif
42 
43 #if defined(POLARSSL_ECDSA_C)
44 #include "ecdsa.h"
45 #endif
46 
47 #define POLARSSL_ERR_PK_MALLOC_FAILED -0x2F80
48 #define POLARSSL_ERR_PK_TYPE_MISMATCH -0x2F00
49 #define POLARSSL_ERR_PK_BAD_INPUT_DATA -0x2E80
50 #define POLARSSL_ERR_PK_FILE_IO_ERROR -0x2E00
51 #define POLARSSL_ERR_PK_KEY_INVALID_VERSION -0x2D80
52 #define POLARSSL_ERR_PK_KEY_INVALID_FORMAT -0x2D00
53 #define POLARSSL_ERR_PK_UNKNOWN_PK_ALG -0x2C80
54 #define POLARSSL_ERR_PK_PASSWORD_REQUIRED -0x2C00
55 #define POLARSSL_ERR_PK_PASSWORD_MISMATCH -0x2B80
56 #define POLARSSL_ERR_PK_INVALID_PUBKEY -0x2B00
57 #define POLARSSL_ERR_PK_INVALID_ALG -0x2A80
58 #define POLARSSL_ERR_PK_UNKNOWN_NAMED_CURVE -0x2A00
59 #define POLARSSL_ERR_PK_FEATURE_UNAVAILABLE -0x2980
62 #if defined(POLARSSL_RSA_C)
63 
69 #define pk_rsa( pk ) ( (rsa_context *) (pk).pk_ctx )
70 #endif /* POLARSSL_RSA_C */
71 
72 #if defined(POLARSSL_ECP_C)
73 
79 #define pk_ec( pk ) ( (ecp_keypair *) (pk).pk_ctx )
80 #endif /* POLARSSL_ECP_C */
81 
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
90 typedef enum {
97 } pk_type_t;
98 
102 typedef enum
103 {
107 } pk_debug_type;
108 
112 typedef struct
113 {
115  const char *name;
116  void *value;
117 } pk_debug_item;
118 
120 #define POLARSSL_PK_DEBUG_MAX_ITEMS 3
121 
125 typedef struct
126 {
129 
131  const char *name;
132 
134  size_t (*get_size)( const void * );
135 
137  int (*can_do)( pk_type_t type );
138 
140  int (*verify_func)( void *ctx, md_type_t md_alg,
141  const unsigned char *hash, size_t hash_len,
142  const unsigned char *sig, size_t sig_len );
143 
145  int (*sign_func)( void *ctx, md_type_t md_alg,
146  const unsigned char *hash, size_t hash_len,
147  unsigned char *sig, size_t *sig_len,
148  int (*f_rng)(void *, unsigned char *, size_t),
149  void *p_rng );
150 
152  int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
153  unsigned char *output, size_t *olen, size_t osize,
154  int (*f_rng)(void *, unsigned char *, size_t),
155  void *p_rng );
156 
158  int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
159  unsigned char *output, size_t *olen, size_t osize,
160  int (*f_rng)(void *, unsigned char *, size_t),
161  void *p_rng );
162 
164  void * (*ctx_alloc_func)( void );
165 
167  void (*ctx_free_func)( void *ctx );
168 
170  void (*debug_func)( const void *ctx, pk_debug_item *items );
171 
172 } pk_info_t;
173 
177 typedef struct
178 {
179  const pk_info_t * pk_info;
180  void * pk_ctx;
181 } pk_context;
182 
186 typedef int (*pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
187  const unsigned char *input, unsigned char *output,
188  size_t output_max_len );
189 typedef int (*pk_rsa_alt_sign_func)( void *ctx,
190  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
191  int mode, md_type_t md_alg, unsigned int hashlen,
192  const unsigned char *hash, unsigned char *sig );
193 typedef size_t (*pk_rsa_alt_key_len_func)( void *ctx );
194 
202 const pk_info_t *pk_info_from_type( pk_type_t pk_type );
203 
207 void pk_init( pk_context *ctx );
208 
212 void pk_free( pk_context *ctx );
213 
228 int pk_init_ctx( pk_context *ctx, const pk_info_t *info );
229 
244 int pk_init_ctx_rsa_alt( pk_context *ctx, void * key,
245  pk_rsa_alt_decrypt_func decrypt_func,
246  pk_rsa_alt_sign_func sign_func,
247  pk_rsa_alt_key_len_func key_len_func );
248 
256 size_t pk_get_size( const pk_context *ctx );
257 
264 static inline size_t pk_get_len( const pk_context *ctx )
265 {
266  return( ( pk_get_size( ctx ) + 7 ) / 8 );
267 }
268 
278 int pk_can_do( pk_context *ctx, pk_type_t type );
279 
298 int pk_verify( pk_context *ctx, md_type_t md_alg,
299  const unsigned char *hash, size_t hash_len,
300  const unsigned char *sig, size_t sig_len );
301 
321 int pk_sign( pk_context *ctx, md_type_t md_alg,
322  const unsigned char *hash, size_t hash_len,
323  unsigned char *sig, size_t *sig_len,
324  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
325 
340 int pk_decrypt( pk_context *ctx,
341  const unsigned char *input, size_t ilen,
342  unsigned char *output, size_t *olen, size_t osize,
343  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
344 
359 int pk_encrypt( pk_context *ctx,
360  const unsigned char *input, size_t ilen,
361  unsigned char *output, size_t *olen, size_t osize,
362  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
363 
372 int pk_debug( const pk_context *ctx, pk_debug_item *items );
373 
381 const char * pk_get_name( const pk_context *ctx );
382 
390 pk_type_t pk_get_type( const pk_context *ctx );
391 
392 #if defined(POLARSSL_PK_PARSE_C)
393 
405 int pk_parse_key( pk_context *ctx,
406  const unsigned char *key, size_t keylen,
407  const unsigned char *pwd, size_t pwdlen );
408 
420  const unsigned char *key, size_t keylen );
421 
422 #if defined(POLARSSL_FS_IO)
423 
433 int pk_parse_keyfile( pk_context *ctx,
434  const char *path, const char *password );
435 
445 int pk_parse_public_keyfile( pk_context *ctx, const char *path );
446 #endif /* POLARSSL_FS_IO */
447 #endif /* POLARSSL_PK_PARSE_C */
448 
449 #if defined(POLARSSL_PK_WRITE_C)
450 
463 int pk_write_key_der( pk_context *ctx, unsigned char *buf, size_t size );
464 
478 int pk_write_pubkey_der( pk_context *ctx, unsigned char *buf, size_t size );
479 
480 #if defined(POLARSSL_PEM_WRITE_C)
481 
490 int pk_write_pubkey_pem( pk_context *ctx, unsigned char *buf, size_t size );
491 
501 int pk_write_key_pem( pk_context *ctx, unsigned char *buf, size_t size );
502 #endif /* POLARSSL_PEM_WRITE_C */
503 #endif /* POLARSSL_PK_WRITE_C */
504 
505 /*
506  * WARNING: Low-level functions. You probably do not want to use these unless
507  * you are certain you do ;)
508  */
509 
510 #if defined(POLARSSL_PK_PARSE_C)
511 
520 int pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
521  pk_context *pk );
522 #endif /* POLARSSL_PK_PARSE_C */
523 
524 #if defined(POLARSSL_PK_WRITE_C)
525 
535 int pk_write_pubkey( unsigned char **p, unsigned char *start,
536  const pk_context *key );
537 #endif /* POLARSSL_PK_WRITE_C */
538 
539 #ifdef __cplusplus
540 }
541 #endif
542 
543 #endif /* POLARSSL_PK_H */
static size_t pk_get_len(const pk_context *ctx)
Get the length in bytes of the underlying key.
Definition: pk.h:264
int pk_write_key_der(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 pk_info_t * pk_info_from_type(pk_type_t pk_type)
Return information associated with the given PK type.
Elliptic curves over GF(p)
size_t pk_get_size(const pk_context *ctx)
Get the size in bits of the underlying key.
int pk_write_key_pem(pk_context *ctx, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 PEM string.
Elliptic curve DSA.
int pk_decrypt(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.
int pk_debug(const pk_context *ctx, pk_debug_item *items)
Export debug information.
Configuration options (set of defines)
const pk_info_t * pk_info
Public key informations.
Definition: pk.h:179
pk_type_t pk_get_type(const pk_context *ctx)
Get the key type.
const char * pk_get_name(const pk_context *ctx)
Access the type name.
int pk_init_ctx_rsa_alt(pk_context *ctx, void *key, pk_rsa_alt_decrypt_func decrypt_func, pk_rsa_alt_sign_func sign_func, pk_rsa_alt_key_len_func key_len_func)
Initialize an RSA-alt context.
md_type_t
Definition: md.h:51
const char * name
Definition: pk.h:115
int(* pk_rsa_alt_sign_func)(void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Definition: pk.h:189
int pk_write_pubkey(unsigned char **p, unsigned char *start, const pk_context *key)
Write a subjectPublicKey to ASN.1 data Note: function works backwards in data buffer.
pk_debug_type type
Definition: pk.h:114
int pk_write_pubkey_der(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...
Item to send to the debug module.
Definition: pk.h:112
int pk_verify(pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature.
Public key information and operations.
Definition: pk.h:125
int pk_can_do(pk_context *ctx, pk_type_t type)
Tell if a context can do the operation given by type.
void * pk_ctx
Underlying public key context.
Definition: pk.h:180
pk_type_t
Public key types.
Definition: pk.h:90
int pk_parse_public_keyfile(pk_context *ctx, const char *path)
Load and parse a public key.
int pk_parse_subpubkey(unsigned char **p, const unsigned char *end, pk_context *pk)
Parse a SubjectPublicKeyInfo DER structure.
int pk_init_ctx(pk_context *ctx, const pk_info_t *info)
Initialize a PK context with the information given and allocates the type-specific PK subcontext...
Generic message digest wrapper.
The RSA public-key cryptosystem.
size_t(* pk_rsa_alt_key_len_func)(void *ctx)
Definition: pk.h:193
void pk_free(pk_context *ctx)
Free a pk_context.
int pk_sign(pk_context *ctx, 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.
int pk_parse_public_key(pk_context *ctx, const unsigned char *key, size_t keylen)
Parse a public key.
int(* 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:186
int pk_encrypt(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.
void pk_init(pk_context *ctx)
Initialize a pk_context (as NONE)
pk_debug_type
Types for interfacing with the debug module.
Definition: pk.h:102
int pk_parse_key(pk_context *ctx, const unsigned char *key, size_t keylen, const unsigned char *pwd, size_t pwdlen)
Parse a private key.
int pk_write_pubkey_pem(pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a PEM string.
const char * name
Type name.
Definition: pk.h:131
pk_type_t type
Public key type.
Definition: pk.h:128
int pk_parse_keyfile(pk_context *ctx, const char *path, const char *password)
Load and parse a private key.
void * value
Definition: pk.h:116
Public key container.
Definition: pk.h:177