#ifndef _DTLS_CRYPTO_H_
#define _DTLS_CRYPTO_H_
#include <stdlib.h>
#include <stdint.h>
#include "aes/rijndael.h"
#include "tinydtls.h"
#include "global.h"
#include "state.h"
#include "numeric.h"
#include "hmac.h"
#include "ccm.h"
#define DTLS_MAC_KEY_LENGTH 0
#define DTLS_KEY_LENGTH 16
#define DTLS_BLK_LENGTH 16
#define DTLS_MAC_LENGTH DTLS_HMAC_DIGEST_SIZE
#define DTLS_IV_LENGTH 4
#define MAX_KEYBLOCK_LENGTH \
(2 * DTLS_MAC_KEY_LENGTH + 2 * DTLS_KEY_LENGTH + 2 * DTLS_IV_LENGTH)
#define DTLS_MASTER_SECRET_LENGTH 48
#define DTLS_RANDOM_LENGTH 32
typedef uint8_t dtls_cipher_index_t;
#define DTLS_CIPHER_INDEX_NULL 0
#define DTLS_MAX_CIPHER_SUITES 4
typedef enum { AES128=0
} dtls_crypto_alg;
typedef enum {
DTLS_ECDH_CURVE_SECP256R1
} dtls_ecdh_curve;
typedef struct {
rijndael_ctx ctx;
uint8_t tag_length;
uint8_t l;
} aes128_ccm_t;
typedef struct dtls_cipher_context_t {
aes128_ccm_t data;
} dtls_cipher_context_t;
typedef struct {
uint8 own_eph_priv[32];
uint8 other_eph_pub_x[32];
uint8 other_eph_pub_y[32];
uint8 other_pub_x[32];
uint8 other_pub_y[32];
} dtls_handshake_parameters_ecdsa_t;
#ifndef DTLS_PSK_MAX_CLIENT_IDENTITY_LEN
#define DTLS_PSK_MAX_CLIENT_IDENTITY_LEN 32
#endif
#define DTLS_PSK_MAX_KEY_LEN DTLS_KEY_LENGTH
typedef struct {
uint16_t id_length;
unsigned char identity[DTLS_PSK_MAX_CLIENT_IDENTITY_LEN];
} dtls_handshake_parameters_psk_t;
typedef struct {
uint64_t cseq;
uint64_t bitfield;
} seqnum_t;
typedef struct {
dtls_compression_t compression;
dtls_cipher_index_t cipher_index;
uint16_t epoch;
uint64_t rseq;
uint8 key_block[MAX_KEYBLOCK_LENGTH];
seqnum_t cseq;
} dtls_security_parameters_t;
struct netq_t;
typedef struct dtls_user_parameters_t {
dtls_cipher_t cipher_suites[DTLS_MAX_CIPHER_SUITES + 1];
unsigned int force_extended_master_secret:1;
unsigned int force_renegotiation_info:1;
} dtls_user_parameters_t;
typedef struct {
union {
struct random_t {
uint8 client[DTLS_RANDOM_LENGTH];
uint8 server[DTLS_RANDOM_LENGTH];
} random;
uint8 master_secret[DTLS_MASTER_SECRET_LENGTH];
} tmp;
struct netq_t *reorder_queue;
dtls_hs_state_t hs_state;
dtls_compression_t compression;
dtls_user_parameters_t user_parameters;
dtls_cipher_index_t cipher_index;
unsigned int do_client_auth:1;
unsigned int extended_master_secret:1;
unsigned int renegotiation_info:1;
union {
#ifdef DTLS_ECC
dtls_handshake_parameters_ecdsa_t ecdsa;
#endif
#ifdef DTLS_PSK
dtls_handshake_parameters_psk_t psk;
#endif
} keyx;
} dtls_handshake_parameters_t;
#define dtls_kb_client_mac_secret(Param, Role) ((Param)->key_block)
#define dtls_kb_server_mac_secret(Param, Role) \
(dtls_kb_client_mac_secret(Param, Role) + DTLS_MAC_KEY_LENGTH)
#define dtls_kb_remote_mac_secret(Param, Role) \
((Role) == DTLS_SERVER \
? dtls_kb_client_mac_secret(Param, Role) \
: dtls_kb_server_mac_secret(Param, Role))
#define dtls_kb_local_mac_secret(Param, Role) \
((Role) == DTLS_CLIENT \
? dtls_kb_client_mac_secret(Param, Role) \
: dtls_kb_server_mac_secret(Param, Role))
#define dtls_kb_mac_secret_size(Param, Role) DTLS_MAC_KEY_LENGTH
#define dtls_kb_client_write_key(Param, Role) \
(dtls_kb_server_mac_secret(Param, Role) + DTLS_MAC_KEY_LENGTH)
#define dtls_kb_server_write_key(Param, Role) \
(dtls_kb_client_write_key(Param, Role) + DTLS_KEY_LENGTH)
#define dtls_kb_remote_write_key(Param, Role) \
((Role) == DTLS_SERVER \
? dtls_kb_client_write_key(Param, Role) \
: dtls_kb_server_write_key(Param, Role))
#define dtls_kb_local_write_key(Param, Role) \
((Role) == DTLS_CLIENT \
? dtls_kb_client_write_key(Param, Role) \
: dtls_kb_server_write_key(Param, Role))
#define dtls_kb_key_size(Param, Role) DTLS_KEY_LENGTH
#define dtls_kb_client_iv(Param, Role) \
(dtls_kb_server_write_key(Param, Role) + DTLS_KEY_LENGTH)
#define dtls_kb_server_iv(Param, Role) \
(dtls_kb_client_iv(Param, Role) + DTLS_IV_LENGTH)
#define dtls_kb_remote_iv(Param, Role) \
((Role) == DTLS_SERVER \
? dtls_kb_client_iv(Param, Role) \
: dtls_kb_server_iv(Param, Role))
#define dtls_kb_local_iv(Param, Role) \
((Role) == DTLS_CLIENT \
? dtls_kb_client_iv(Param, Role) \
: dtls_kb_server_iv(Param, Role))
#define dtls_kb_iv_size(Param, Role) DTLS_IV_LENGTH
#define dtls_kb_size(Param, Role) \
(2 * (dtls_kb_mac_secret_size(Param, Role) + \
dtls_kb_key_size(Param, Role) + dtls_kb_iv_size(Param, Role)))
#define dtls_kb_digest_size(Param, Role) DTLS_MAC_LENGTH
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);
typedef struct {
const uint8_t *nonce;
uint8_t tag_length;
uint8_t l;
} dtls_ccm_params_t;
int dtls_encrypt_params(const dtls_ccm_params_t *params,
const unsigned char *src, size_t length,
unsigned char *buf,
const unsigned char *key, size_t keylen,
const unsigned char *aad, size_t aad_length);
int dtls_encrypt(const unsigned char *src, size_t length,
unsigned char *buf,
const unsigned char *nonce,
const unsigned char *key, size_t keylen,
const unsigned char *aad, size_t aad_length);
int dtls_decrypt_params(const dtls_ccm_params_t *params,
const unsigned char *src, size_t length,
unsigned char *buf,
const unsigned char *key, size_t keylen,
const unsigned char *aad, size_t aad_length);
int dtls_decrypt(const unsigned char *src, size_t length,
unsigned char *buf,
const unsigned char *nonce,
const unsigned char *key, size_t keylen,
const unsigned char *a_data, size_t a_data_length);
int dtls_psk_pre_master_secret(unsigned char *key, size_t keylen,
unsigned char *result, size_t result_len);
#define DTLS_EC_KEY_SIZE 32
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_ec_key_asn1_from_uint32(const uint32_t *key, size_t key_size,
unsigned char *buf);
dtls_handshake_parameters_t *dtls_handshake_new(void);
void dtls_handshake_free(dtls_handshake_parameters_t *handshake);
dtls_security_parameters_t *dtls_security_new(void);
void dtls_security_free(dtls_security_parameters_t *security);
void crypto_init(void);
#endif