pk.c File Reference

#include "polarssl/config.h"
#include "polarssl/pk.h"
#include "polarssl/pk_wrap.h"
#include "polarssl/rsa.h"
#include "polarssl/ecp.h"
#include "polarssl/ecdsa.h"
Include dependency graph for pk.c:

Go to the source code of this file.

Functions

void pk_init (pk_context *ctx)
 Initialize a pk_context (as NONE).
void pk_free (pk_context *ctx)
 Free a pk_context.
const pk_info_tpk_info_from_type (pk_type_t pk_type)
 Return information associated with the given PK type.
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.
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.
int pk_can_do (pk_context *ctx, pk_type_t type)
 Tell if a context can do the operation given by type.
static int pk_hashlen_helper (md_type_t md_alg, size_t *hash_len)
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.
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_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_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.
size_t pk_get_size (const pk_context *ctx)
 Get the size in bits of the underlying key.
int pk_debug (const pk_context *ctx, pk_debug_item *items)
 Export debug information.
const char * pk_get_name (const pk_context *ctx)
 Access the type name.
pk_type_t pk_get_type (const pk_context *ctx)
 Get the key type.

Function Documentation

int pk_can_do ( pk_context ctx,
pk_type_t  type 
)
int pk_debug ( const pk_context ctx,
pk_debug_item items 
)

Export debug information.

Parameters:
ctx Context to use
items Place to write debug items
Returns:
0 on success or POLARSSL_ERR_PK_BAD_INPUT_DATA

Definition at line 256 of file pk.c.

References pk_info_t::debug_func, pk_context::pk_ctx, pk_context::pk_info, and POLARSSL_ERR_PK_BAD_INPUT_DATA.

Referenced by debug_print_pk().

int pk_decrypt ( pk_context ctx,
const unsigned char *  input,
size_t  ilen,
unsigned char *  output,
size_t *  olen,
size_t  osize,
int(*)(void *, unsigned char *, size_t)  f_rng,
void *  p_rng 
)

Decrypt message.

Parameters:
ctx PK context to use
input Input to decrypt
ilen Input size
output Decrypted output
olen Decrypted message length
osize Size of the output buffer
f_rng RNG function
p_rng RNG parameter
Returns:
0 on success, or a specific error code.

Definition at line 209 of file pk.c.

References pk_info_t::decrypt_func, pk_context::pk_ctx, pk_context::pk_info, POLARSSL_ERR_PK_BAD_INPUT_DATA, and POLARSSL_ERR_PK_TYPE_MISMATCH.

Referenced by ssl_parse_encrypted_pms(), test_suite_pk_ec_nocrypt(), and test_suite_pk_rsa_decrypt_test_vec().

int pk_encrypt ( pk_context ctx,
const unsigned char *  input,
size_t  ilen,
unsigned char *  output,
size_t *  olen,
size_t  osize,
int(*)(void *, unsigned char *, size_t)  f_rng,
void *  p_rng 
)

Encrypt message.

Parameters:
ctx PK context to use
input Message to encrypt
ilen Message size
output Encrypted output
olen Encrypted output length
osize Size of the output buffer
f_rng RNG function
p_rng RNG parameter
Returns:
0 on success, or a specific error code.

Definition at line 227 of file pk.c.

References pk_info_t::encrypt_func, pk_context::pk_ctx, pk_context::pk_info, POLARSSL_ERR_PK_BAD_INPUT_DATA, and POLARSSL_ERR_PK_TYPE_MISMATCH.

Referenced by ssl_write_encrypted_pms(), test_suite_pk_ec_nocrypt(), and test_suite_pk_rsa_encrypt_test_vec().

void pk_free ( pk_context ctx  ) 
const char* pk_get_name ( const pk_context ctx  ) 

Access the type name.

Parameters:
ctx Context to use
Returns:
Type name on success, or "invalid PK"

Definition at line 268 of file pk.c.

References pk_info_t::name, and pk_context::pk_info.

Referenced by test_suite_pk_utils(), x509_crt_info(), and x509_csr_info().

size_t pk_get_size ( const pk_context ctx  ) 

Get the size in bits of the underlying key.

Parameters:
ctx Context to use
Returns:
Key size in bits, or 0 on error

Definition at line 245 of file pk.c.

References pk_info_t::get_size, pk_context::pk_ctx, and pk_context::pk_info.

Referenced by pk_get_len(), test_suite_pk_utils(), x509_crt_info(), and x509_csr_info().

pk_type_t pk_get_type ( const pk_context ctx  ) 

Get the key type.

Parameters:
ctx Context to use
Returns:
Type on success, or POLARSSL_PK_NONE

Definition at line 279 of file pk.c.

References pk_context::pk_info, POLARSSL_PK_NONE, and pk_info_t::type.

Referenced by pk_genkey(), pk_write_key_der(), pk_write_key_pem(), pk_write_pubkey(), pk_write_pubkey_der(), test_suite_pk_utils(), x509write_crt_der(), and x509write_csr_der().

static int pk_hashlen_helper ( md_type_t  md_alg,
size_t *  hash_len 
) [inline, static]

Definition at line 155 of file pk.c.

References md_info_from_type(), and md_info_t::size.

Referenced by pk_sign(), and pk_verify().

const pk_info_t* pk_info_from_type ( pk_type_t  pk_type  ) 
void pk_init ( pk_context ctx  ) 
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.

Parameters:
ctx Context to initialize. Must be empty (type NONE).
info Information to use
Returns:
0 on success, POLARSSL_ERR_PK_BAD_INPUT_DATA on invalid input, POLARSSL_ERR_PK_MALLOC_FAILED on allocation failure.
Note:
For contexts holding an RSA-alt key, use pk_init_ctx_rsa_alt() instead.

Definition at line 98 of file pk.c.

References pk_info_t::ctx_alloc_func, pk_context::pk_ctx, pk_context::pk_info, POLARSSL_ERR_PK_BAD_INPUT_DATA, and POLARSSL_ERR_PK_MALLOC_FAILED.

Referenced by pk_parse_key(), pk_parse_key_pkcs8_unencrypted_der(), pk_parse_subpubkey(), ssl_set_own_cert_rsa(), test_suite_pk_ec_nocrypt(), test_suite_pk_ec_test_vec(), test_suite_pk_rsa_decrypt_test_vec(), test_suite_pk_rsa_encrypt_test_vec(), test_suite_pk_rsa_verify_test_vec(), test_suite_pk_sign_verify(), and test_suite_pk_utils().

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.

Parameters:
ctx Context to initialize. Must be empty (type NONE).
key RSA key pointer
decrypt_func Decryption function
sign_func Signing function
key_len_func Function returning key length
Returns:
0 on success, or POLARSSL_ERR_PK_BAD_INPUT_DATA if the context wasn't already initialized as RSA_ALT.
Note:
This function replaces pk_init_ctx() for RSA-alt.

Definition at line 114 of file pk.c.

References pk_info_t::ctx_alloc_func, rsa_alt_context::decrypt_func, rsa_alt_context::key, rsa_alt_context::key_len_func, pk_context::pk_ctx, pk_context::pk_info, POLARSSL_ERR_PK_BAD_INPUT_DATA, POLARSSL_ERR_PK_MALLOC_FAILED, rsa_alt_info, and rsa_alt_context::sign_func.

Referenced by ssl_set_own_cert_alt().

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(*)(void *, unsigned char *, size_t)  f_rng,
void *  p_rng 
)

Make signature.

Parameters:
ctx PK context to use
md_alg Hash algorithm used (see notes)
hash Hash of the message to sign
hash_len Hash length or 0 (see notes)
sig Place to write the signature
sig_len Number of bytes written
f_rng RNG function
p_rng RNG parameter
Returns:
0 on success, or a specific error code.
Note:
If hash_len is 0, then the length associated with md_alg is used instead, or an error returned if it is invalid.
md_alg may be POLARSSL_MD_NONE, only if hash_len != 0

Definition at line 190 of file pk.c.

References pk_context::pk_ctx, pk_hashlen_helper(), pk_context::pk_info, POLARSSL_ERR_PK_BAD_INPUT_DATA, POLARSSL_ERR_PK_TYPE_MISMATCH, and pk_info_t::sign_func.

Referenced by ssl_write_certificate_verify(), ssl_write_server_key_exchange(), test_suite_pk_sign_verify(), x509write_crt_der(), and x509write_csr_der().

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.

Parameters:
ctx PK context to use
md_alg Hash algorithm used (see notes)
hash Hash of the message to sign
hash_len Hash length or 0 (see notes)
sig Signature to verify
sig_len Signature length
Returns:
0 on success (signature is valid), or a specific error code.
Note:
If hash_len is 0, then the length associated with md_alg is used instead, or an error returned if it is invalid.
md_alg may be POLARSSL_MD_NONE, only if hash_len != 0

Definition at line 172 of file pk.c.

References pk_context::pk_ctx, pk_hashlen_helper(), pk_context::pk_info, POLARSSL_ERR_PK_BAD_INPUT_DATA, POLARSSL_ERR_PK_TYPE_MISMATCH, and pk_info_t::verify_func.

Referenced by ssl_parse_certificate_verify(), ssl_parse_server_key_exchange(), test_suite_pk_ec_test_vec(), test_suite_pk_rsa_verify_test_vec(), test_suite_pk_sign_verify(), x509_crt_verify_child(), x509_crt_verify_top(), and x509_crt_verifycrl().


Generated on 9 Apr 2014 for PolarSSL v1.3.2 by  doxygen 1.6.1