tinydtls  0.8.6
Macros | Functions | Variables
crypto.c File Reference
#include <stdio.h>
#include "tinydtls.h"
#include <assert.h>
#include "global.h"
#include "dtls_debug.h"
#include "numeric.h"
#include "dtls.h"
#include "crypto.h"
#include "ccm.h"
#include "ecc/ecc.h"
#include "prng.h"
#include "netq.h"
#include <pthread.h>

Go to the source code of this file.

Macros

#define HMAC_UPDATE_SEED(Context, Seed, Length)   if (Seed) dtls_hmac_update(Context, (Seed), (Length))
 

Functions

static struct dtls_cipher_context_tdtls_cipher_context_get (void)
 
static void dtls_cipher_context_release (void)
 
void crypto_init (void)
 
static dtls_handshake_parameters_tdtls_handshake_malloc (void)
 
static void dtls_handshake_dealloc (dtls_handshake_parameters_t *handshake)
 
static dtls_security_parameters_tdtls_security_malloc (void)
 
static void dtls_security_dealloc (dtls_security_parameters_t *security)
 
dtls_handshake_parameters_tdtls_handshake_new (void)
 
void dtls_handshake_free (dtls_handshake_parameters_t *handshake)
 
dtls_security_parameters_tdtls_security_new (void)
 
void dtls_security_free (dtls_security_parameters_t *security)
 
size_t dtls_p_hash (dtls_hashfunc_t h, const unsigned char *key, size_t keylen, const unsigned char *label, size_t labellen, const unsigned char *random1, size_t random1len, const unsigned char *random2, size_t random2len, unsigned char *buf, size_t buflen)
 
size_t dtls_prf (const unsigned char *key, size_t keylen, const unsigned char *label, size_t labellen, const unsigned char *random1, size_t random1len, const unsigned char *random2, size_t random2len, unsigned char *buf, size_t buflen)
 
void dtls_mac (dtls_hmac_context_t *hmac_ctx, const unsigned char *record, const unsigned char *packet, size_t length, unsigned char *buf)
 
static size_t dtls_ccm_encrypt (aes128_ccm_t *ccm_ctx, const unsigned char *src, size_t srclen, unsigned char *buf, unsigned char *nounce, const unsigned char *aad, size_t la)
 
static size_t dtls_ccm_decrypt (aes128_ccm_t *ccm_ctx, const unsigned char *src, size_t srclen, unsigned char *buf, unsigned char *nounce, const unsigned char *aad, size_t la)
 
int dtls_psk_pre_master_secret (unsigned char *key, size_t keylen, unsigned char *result, size_t result_len)
 
static void dtls_ec_key_to_uint32 (const unsigned char *key, size_t key_size, uint32_t *result)
 
static void dtls_ec_key_from_uint32 (const uint32_t *key, size_t key_size, unsigned char *result)
 
int dtls_ec_key_from_uint32_asn1 (const uint32_t *key, size_t key_size, unsigned char *buf)
 
int dtls_ecdh_pre_master_secret (unsigned char *priv_key, unsigned char *pub_key_x, unsigned char *pub_key_y, size_t key_size, unsigned char *result, size_t result_len)
 
void dtls_ecdsa_generate_key (unsigned char *priv_key, unsigned char *pub_key_x, unsigned char *pub_key_y, size_t key_size)
 
void dtls_ecdsa_create_sig_hash (const unsigned char *priv_key, size_t key_size, const unsigned char *sign_hash, size_t sign_hash_size, uint32_t point_r[9], uint32_t point_s[9])
 
void dtls_ecdsa_create_sig (const unsigned char *priv_key, size_t key_size, const unsigned char *client_random, size_t client_random_size, const unsigned char *server_random, size_t server_random_size, const unsigned char *keyx_params, size_t keyx_params_size, uint32_t point_r[9], uint32_t point_s[9])
 
int dtls_ecdsa_verify_sig_hash (const unsigned char *pub_key_x, const unsigned char *pub_key_y, size_t key_size, const unsigned char *sign_hash, size_t sign_hash_size, unsigned char *result_r, unsigned char *result_s)
 
int dtls_ecdsa_verify_sig (const unsigned char *pub_key_x, const unsigned char *pub_key_y, size_t key_size, const unsigned char *client_random, size_t client_random_size, const unsigned char *server_random, size_t server_random_size, const unsigned char *keyx_params, size_t keyx_params_size, unsigned char *result_r, unsigned char *result_s)
 
int dtls_encrypt (const unsigned char *src, size_t length, unsigned char *buf, unsigned char *nounce, unsigned char *key, size_t keylen, const unsigned char *aad, size_t la)
 
int dtls_decrypt (const unsigned char *src, size_t length, unsigned char *buf, unsigned char *nounce, unsigned char *key, size_t keylen, const unsigned char *aad, size_t la)
 

Variables

static struct dtls_cipher_context_t cipher_context
 
static pthread_mutex_t cipher_context_mutex = PTHREAD_MUTEX_INITIALIZER
 

Macro Definition Documentation

#define HMAC_UPDATE_SEED (   Context,
  Seed,
  Length 
)    if (Seed) dtls_hmac_update(Context, (Seed), (Length))

Definition at line 42 of file crypto.c.

Function Documentation

void crypto_init ( void  )

Definition at line 66 of file crypto.c.

static size_t dtls_ccm_decrypt ( aes128_ccm_t ccm_ctx,
const unsigned char *  src,
size_t  srclen,
unsigned char *  buf,
unsigned char *  nounce,
const unsigned char *  aad,
size_t  la 
)
static

Definition at line 292 of file crypto.c.

static size_t dtls_ccm_encrypt ( aes128_ccm_t ccm_ctx,
const unsigned char *  src,
size_t  srclen,
unsigned char *  buf,
unsigned char *  nounce,
const unsigned char *  aad,
size_t  la 
)
static

Definition at line 274 of file crypto.c.

static struct dtls_cipher_context_t* dtls_cipher_context_get ( void  )
static

Definition at line 50 of file crypto.c.

static void dtls_cipher_context_release ( void  )
static

Definition at line 58 of file crypto.c.

int dtls_decrypt ( const unsigned char *  src,
size_t  length,
unsigned char *  buf,
unsigned char *  nounce,
unsigned char *  key,
size_t  keylen,
const unsigned char *  a_data,
size_t  a_data_length 
)

Decrypts the given buffer src of given length, writing the result to buf. The function returns -1 in case of an error, or the number of bytes written. Note that for block ciphers, length must be a multiple of the cipher's block size. A return value between 0 and the actual length indicates that only n-1 block have been processed. Unlike dtls_encrypt(), the source and destination of dtls_decrypt() may overlap.

Parameters
ctxThe cipher context to use.
srcThe buffer to decrypt.
lengthThe length of the input buffer.
bufThe result buffer.
aadadditional authentication data for AEAD ciphers
aad_lengthactual size of aad
Returns
Less than zero on error, the number of decrypted bytes otherwise.

Definition at line 543 of file crypto.c.

static void dtls_ec_key_from_uint32 ( const uint32_t key,
size_t  key_size,
unsigned char *  result 
)
static

Definition at line 345 of file crypto.c.

int dtls_ec_key_from_uint32_asn1 ( const uint32_t key,
size_t  key_size,
unsigned char *  buf 
)

Definition at line 355 of file crypto.c.

static void dtls_ec_key_to_uint32 ( const unsigned char *  key,
size_t  key_size,
uint32_t result 
)
static

Definition at line 335 of file crypto.c.

int dtls_ecdh_pre_master_secret ( unsigned char *  priv_key,
unsigned char *  pub_key_x,
unsigned char *  pub_key_y,
size_t  key_size,
unsigned char *  result,
size_t  result_len 
)

Definition at line 391 of file crypto.c.

void dtls_ecdsa_create_sig ( const unsigned char *  priv_key,
size_t  key_size,
const unsigned char *  client_random,
size_t  client_random_size,
const unsigned char *  server_random,
size_t  server_random_size,
const unsigned char *  keyx_params,
size_t  keyx_params_size,
uint32_t  point_r[9],
uint32_t  point_s[9] 
)

Definition at line 456 of file crypto.c.

void dtls_ecdsa_create_sig_hash ( const unsigned char *  priv_key,
size_t  key_size,
const unsigned char *  sign_hash,
size_t  sign_hash_size,
uint32_t  point_r[9],
uint32_t  point_s[9] 
)

Definition at line 439 of file crypto.c.

void dtls_ecdsa_generate_key ( unsigned char *  priv_key,
unsigned char *  pub_key_x,
unsigned char *  pub_key_y,
size_t  key_size 
)

Definition at line 418 of file crypto.c.

int dtls_ecdsa_verify_sig ( const unsigned char *  pub_key_x,
const unsigned char *  pub_key_y,
size_t  key_size,
const unsigned char *  client_random,
size_t  client_random_size,
const unsigned char *  server_random,
size_t  server_random_size,
const unsigned char *  keyx_params,
size_t  keyx_params_size,
unsigned char *  result_r,
unsigned char *  result_s 
)

Definition at line 496 of file crypto.c.

int dtls_ecdsa_verify_sig_hash ( const unsigned char *  pub_key_x,
const unsigned char *  pub_key_y,
size_t  key_size,
const unsigned char *  sign_hash,
size_t  sign_hash_size,
unsigned char *  result_r,
unsigned char *  result_s 
)

Definition at line 476 of file crypto.c.

int dtls_encrypt ( const unsigned char *  src,
size_t  length,
unsigned char *  buf,
unsigned char *  nounce,
unsigned char *  key,
size_t  keylen,
const unsigned char *  aad,
size_t  aad_length 
)

Encrypts the specified src of given length, writing the result to buf. The cipher implementation may add more data to the result buffer such as an initialization vector or padding (e.g. for block cipers in CBC mode). The caller therefore must ensure that buf provides sufficient storage to hold the result. Usually this means ( 2 + length / blocksize ) * blocksize. The function returns a value less than zero on error or otherwise the number of bytes written.

Parameters
ctxThe cipher context to use.
srcThe data to encrypt.
lengthThe actual size of of src.
bufThe result buffer. src and buf must not overlap.
aadadditional data for AEAD ciphers
aad_lengthactual size of aad
Returns
The number of encrypted bytes on success, less than zero otherwise.

Definition at line 517 of file crypto.c.

static void dtls_handshake_dealloc ( dtls_handshake_parameters_t handshake)
static

Definition at line 74 of file crypto.c.

void dtls_handshake_free ( dtls_handshake_parameters_t handshake)

Definition at line 136 of file crypto.c.

static dtls_handshake_parameters_t* dtls_handshake_malloc ( void  )
static

Definition at line 70 of file crypto.c.

dtls_handshake_parameters_t* dtls_handshake_new ( void  )

Definition at line 113 of file crypto.c.

void dtls_mac ( dtls_hmac_context_t hmac_ctx,
const unsigned char *  record,
const unsigned char *  packet,
size_t  length,
unsigned char *  buf 
)

Calculates MAC for record + cleartext packet and places the result in buf. The given hmac_ctx must be initialized with the HMAC function to use and the proper secret. As the DTLS mac calculation requires data from the record header, record must point to a buffer of at least sizeof(dtls_record_header_t) bytes. Usually, the remaining packet will be encrypted, therefore, the cleartext is passed separately in packet.

Parameters
hmac_ctxThe HMAC context to use for MAC calculation.
recordThe record header.
packetCleartext payload to apply the MAC to.
lengthSize of packet.
bufA result buffer that is large enough to hold the generated digest.

Definition at line 257 of file crypto.c.

size_t dtls_p_hash ( dtls_hashfunc_t  h,
const unsigned char *  key,
size_t  keylen,
const unsigned char *  label,
size_t  labellen,
const unsigned char *  random1,
size_t  random1len,
const unsigned char *  random2,
size_t  random2len,
unsigned char *  buf,
size_t  buflen 
)

Expands the secret and key to a block of DTLS_HMAC_MAX size according to the algorithm specified in section 5 of RFC 4346.

Parameters
hIdentifier of the hash function to use.
keyThe secret.
keylenLength of key.
seedThe seed.
seedlenLength of seed.
bufOutput buffer where the result is XORed into The buffe must be capable to hold at least buflen bytes.
Returns
The actual number of bytes written to buf or 0 on error.

Definition at line 173 of file crypto.c.

size_t dtls_prf ( const unsigned char *  key,
size_t  keylen,
const unsigned char *  label,
size_t  labellen,
const unsigned char *  random1,
size_t  random1len,
const unsigned char *  random2,
size_t  random2len,
unsigned char *  buf,
size_t  buflen 
)

This function implements the TLS PRF for DTLS_VERSION. For version 1.0, the PRF is P_MD5 ^ P_SHA1 while version 1.2 uses P_SHA256. Currently, the actual PRF is selected at compile time.

Definition at line 240 of file crypto.c.

int dtls_psk_pre_master_secret ( unsigned char *  key,
size_t  keylen,
unsigned char *  result,
size_t  result_len 
)

Generates pre_master_sercet from given PSK and fills the result according to the "plain PSK" case in section 2 of RFC 4279. Diffie-Hellman and RSA key exchange are currently not supported.

Parameters
keyThe shared key.
keylenLength of key in bytes.
resultThe derived pre master secret.
Returns
The actual length of result.

Definition at line 311 of file crypto.c.

static void dtls_security_dealloc ( dtls_security_parameters_t security)
static

Definition at line 82 of file crypto.c.

void dtls_security_free ( dtls_security_parameters_t security)

Definition at line 164 of file crypto.c.

static dtls_security_parameters_t* dtls_security_malloc ( void  )
static

Definition at line 78 of file crypto.c.

dtls_security_parameters_t* dtls_security_new ( void  )

Definition at line 145 of file crypto.c.

Variable Documentation

struct dtls_cipher_context_t cipher_context
static

Definition at line 45 of file crypto.c.

pthread_mutex_t cipher_context_mutex = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 47 of file crypto.c.