tinydtls  0.8.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Macros | Functions | Variables
crypto.c File Reference
#include <stdio.h>
#include "tinydtls.h"
#include "dtls_config.h"
#include <assert.h>
#include "global.h"
#include "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_t
dtls_cipher_context_get (void)
 
static void dtls_cipher_context_release (void)
 
void crypto_init ()
 
static
dtls_handshake_parameters_t
dtls_handshake_malloc ()
 
static void dtls_handshake_dealloc (dtls_handshake_parameters_t *handshake)
 
static dtls_security_parameters_tdtls_security_malloc ()
 
static void dtls_security_dealloc (dtls_security_parameters_t *security)
 
dtls_handshake_parameters_tdtls_handshake_new ()
 
void dtls_handshake_free (dtls_handshake_parameters_t *handshake)
 
dtls_security_parameters_tdtls_security_new ()
 
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 52 of file crypto.c.

Function Documentation

void crypto_init ( )

Definition at line 76 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 300 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 283 of file crypto.c.

static struct dtls_cipher_context_t* dtls_cipher_context_get ( void  )
static

Definition at line 60 of file crypto.c.

static void dtls_cipher_context_release ( void  )
static

Definition at line 68 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 550 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 352 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 362 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 342 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 398 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 463 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 446 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 425 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 503 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 483 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 524 of file crypto.c.

static void dtls_handshake_dealloc ( dtls_handshake_parameters_t handshake)
static

Definition at line 84 of file crypto.c.

void dtls_handshake_free ( dtls_handshake_parameters_t handshake)

Definition at line 146 of file crypto.c.

static dtls_handshake_parameters_t* dtls_handshake_malloc ( )
static

Definition at line 80 of file crypto.c.

dtls_handshake_parameters_t* dtls_handshake_new ( )

Definition at line 123 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 266 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 183 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 249 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 318 of file crypto.c.

static void dtls_security_dealloc ( dtls_security_parameters_t security)
static

Definition at line 92 of file crypto.c.

void dtls_security_free ( dtls_security_parameters_t security)

Definition at line 174 of file crypto.c.

static dtls_security_parameters_t* dtls_security_malloc ( )
static

Definition at line 88 of file crypto.c.

dtls_security_parameters_t* dtls_security_new ( )

Definition at line 155 of file crypto.c.

Variable Documentation

struct dtls_cipher_context_t cipher_context
static

Definition at line 55 of file crypto.c.

pthread_mutex_t cipher_context_mutex = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 57 of file crypto.c.