Skip to content

Commit

Permalink
zrtp: Move zrtp structures from zrtp.hh to defines.hh
Browse files Browse the repository at this point in the history
This reduces unnecessary dependencies between zrtp components.
  • Loading branch information
jrsnen committed Mar 31, 2022
1 parent 4ecfedc commit 8952434
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 158 deletions.
144 changes: 1 addition & 143 deletions src/zrtp.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "zrtp/zrtp_receiver.hh"
#include "zrtp/defines.hh"


#ifdef _WIN32
Expand All @@ -23,156 +24,13 @@ namespace uvgrtp {
struct rtp_frame;
}

namespace crypto
{
namespace hmac {
class sha256;
}

class sha256;
class dh;
}

namespace zrtp_msg {
struct zrtp_hello_ack;
struct zrtp_commit;
struct zrtp_hello;
struct zrtp_dh;
}

enum ZRTP_ROLE {
INITIATOR,
RESPONDER
};

typedef struct capabilities {
/* Supported ZRTP version */
uint32_t version = 0;

/* Supported hash algorithms (empty for us) */
std::vector<uint32_t> hash_algos;

/* Supported cipher algorithms (empty for us) */
std::vector<uint32_t> cipher_algos;

/* Supported authentication tag types (empty for us) */
std::vector<uint32_t> auth_tags;

/* Supported Key Agreement types (empty for us) */
std::vector<uint32_t> key_agreements;

/* Supported SAS types (empty for us) */
std::vector<uint32_t> sas_types;
} zrtp_capab_t;

typedef struct zrtp_crypto_ctx {
uvgrtp::crypto::hmac::sha256 *hmac_sha256 = nullptr;
uvgrtp::crypto::sha256 *sha256 = nullptr;
uvgrtp::crypto::dh *dh = nullptr;
} zrtp_crypto_ctx_t;

typedef struct zrtp_secrets {
/* Retained (for uvgRTP, preshared mode is not supported so we're
* going to generate just some random values for these) */
uint8_t rs1[32];
uint8_t rs2[32];
uint8_t raux[32];
uint8_t rpbx[32];

/* Shared secrets
*
* Because uvgRTP supports only DH mode,
* other shared secrets (s1 - s3) are null */
uint8_t s0[32];
uint8_t *s1 = nullptr;
uint8_t *s2 = nullptr;
uint8_t *s3 = nullptr;
} zrtp_secrets_t;

typedef struct zrtp_messages {
std::pair<size_t, struct uvgrtp::zrtp_msg::zrtp_commit *> commit;
std::pair<size_t, struct uvgrtp::zrtp_msg::zrtp_hello *> hello;
std::pair<size_t, struct uvgrtp::zrtp_msg::zrtp_dh *> dh;
} zrtp_messages_t;

/* Various ZRTP-related keys */
typedef struct zrtp_key_ctx {
uint8_t zrtp_sess_key[32];
uint8_t sas_hash[32];

/* ZRTP keys used to encrypt Confirm1/Confirm2 messages */
uint8_t zrtp_keyi[16];
uint8_t zrtp_keyr[16];

/* HMAC keys used to authenticate Confirm1/Confirm2 messages */
uint8_t hmac_keyi[32];
uint8_t hmac_keyr[32];
} zrtp_key_ctx_t;

/* Diffie-Hellman context for the ZRTP session */
typedef struct zrtp_dh_ctx {
/* Our public/private key pair */
uint8_t private_key[22];
uint8_t public_key[384];

/* Remote public key received in DHPart1/DHPart2 Message */
uint8_t remote_public[384];

/* DHResult aka "remote_public ^ private_key mod p" (see src/crypto/crypto.cc) */
uint8_t dh_result[384];
} zrtp_dh_ctx_t;

typedef struct zrtp_hash_ctx {
uint8_t o_hvi[32]; /* our hash value of initator (if we're the initiator) */
uint8_t r_hvi[32]; /* remote's hash value of initiator (if they're the initiator) */

/* Session hashes (H0 - H3), Section 9 of RFC 6189 */
uint8_t o_hash[4][32]; /* our session hashes */
uint8_t r_hash[4][32]; /* remote's session hashes */

uint64_t r_mac[4];

/* Section 4.4.1.4 */
uint8_t total_hash[32];
} zrtp_hash_ctx_t;

/* Collection of algorithms that are used by ZRTP
* (based on information gathered from Hello message) */
typedef struct zrtp_session {
int role = 0; /* initiator/responder */
uint32_t ssrc = 0;
uint16_t seq = 0;

uint32_t hash_algo = 0;
uint32_t cipher_algo = 0;
uint32_t auth_tag_type = 0;
uint32_t key_agreement_type = 0;
uint32_t sas_type = 0;

/* Session capabilities */
zrtp_capab_t capabilities;

/* Various hash values of the ZRTP session */
zrtp_hash_ctx_t hash_ctx;

/* DH-related variables */
zrtp_dh_ctx_t dh_ctx;

/* ZRTP keying material (for HMAC/AES etc) */
zrtp_key_ctx_t key_ctx;

/* Retained and shared secrets of the ZRTP session */
zrtp_secrets_t secrets;

uint8_t o_zid[12]; /* our ZID */
uint8_t r_zid[12]; /* remote ZID */

/* Pointers to messages sent by us and messages received from remote.
* These are used to calculate various hash values */
zrtp_messages_t l_msg;
zrtp_messages_t r_msg;
} zrtp_session_t;

class zrtp {
public:
zrtp();
Expand Down
2 changes: 0 additions & 2 deletions src/zrtp/commit.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "commit.hh"

#include "../zrtp.hh"

#include "uvgrtp/crypto.hh"
#include "uvgrtp/debug.hh"
#include "uvgrtp/frame.hh"
Expand Down
2 changes: 0 additions & 2 deletions src/zrtp/confack.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "confack.hh"

#include "../zrtp.hh"

#include "uvgrtp/crypto.hh"
#include "uvgrtp/frame.hh"
#include "uvgrtp/socket.hh"
Expand Down
2 changes: 0 additions & 2 deletions src/zrtp/confirm.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "confirm.hh"

#include "../zrtp.hh"

#include "uvgrtp/crypto.hh"
#include "uvgrtp/frame.hh"
#include "uvgrtp/socket.hh"
Expand Down
143 changes: 143 additions & 0 deletions src/zrtp/defines.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "uvgrtp/util.hh"

#include <vector>

namespace uvgrtp {

namespace zrtp_msg {
Expand Down Expand Up @@ -131,6 +133,147 @@ namespace uvgrtp {
ZRTP_ERR_GOCLEAR_NOT_ALLOWED = 0x100, /* Goclear received but not supported */
};
}


typedef struct capabilities {
/* Supported ZRTP version */
uint32_t version = 0;

/* Supported hash algorithms (empty for us) */
std::vector<uint32_t> hash_algos;

/* Supported cipher algorithms (empty for us) */
std::vector<uint32_t> cipher_algos;

/* Supported authentication tag types (empty for us) */
std::vector<uint32_t> auth_tags;

/* Supported Key Agreement types (empty for us) */
std::vector<uint32_t> key_agreements;

/* Supported SAS types (empty for us) */
std::vector<uint32_t> sas_types;
} zrtp_capab_t;

namespace crypto
{
namespace hmac {
class sha256;
}

class sha256;
class dh;
}

typedef struct zrtp_crypto_ctx {
uvgrtp::crypto::hmac::sha256* hmac_sha256 = nullptr;
uvgrtp::crypto::sha256* sha256 = nullptr;
uvgrtp::crypto::dh* dh = nullptr;
} zrtp_crypto_ctx_t;

typedef struct zrtp_secrets {
/* Retained (for uvgRTP, preshared mode is not supported so we're
* going to generate just some random values for these) */
uint8_t rs1[32];
uint8_t rs2[32];
uint8_t raux[32];
uint8_t rpbx[32];

/* Shared secrets
*
* Because uvgRTP supports only DH mode,
* other shared secrets (s1 - s3) are null */
uint8_t s0[32];
uint8_t* s1 = nullptr;
uint8_t* s2 = nullptr;
uint8_t* s3 = nullptr;
} zrtp_secrets_t;

typedef struct zrtp_messages {
std::pair<size_t, struct uvgrtp::zrtp_msg::zrtp_commit*> commit;
std::pair<size_t, struct uvgrtp::zrtp_msg::zrtp_hello*> hello;
std::pair<size_t, struct uvgrtp::zrtp_msg::zrtp_dh*> dh;
} zrtp_messages_t;

/* Various ZRTP-related keys */
typedef struct zrtp_key_ctx {
uint8_t zrtp_sess_key[32];
uint8_t sas_hash[32];

/* ZRTP keys used to encrypt Confirm1/Confirm2 messages */
uint8_t zrtp_keyi[16];
uint8_t zrtp_keyr[16];

/* HMAC keys used to authenticate Confirm1/Confirm2 messages */
uint8_t hmac_keyi[32];
uint8_t hmac_keyr[32];
} zrtp_key_ctx_t;

/* Diffie-Hellman context for the ZRTP session */
typedef struct zrtp_dh_ctx {
/* Our public/private key pair */
uint8_t private_key[22];
uint8_t public_key[384];

/* Remote public key received in DHPart1/DHPart2 Message */
uint8_t remote_public[384];

/* DHResult aka "remote_public ^ private_key mod p" (see src/crypto/crypto.cc) */
uint8_t dh_result[384];
} zrtp_dh_ctx_t;

typedef struct zrtp_hash_ctx {
uint8_t o_hvi[32]; /* our hash value of initator (if we're the initiator) */
uint8_t r_hvi[32]; /* remote's hash value of initiator (if they're the initiator) */

/* Session hashes (H0 - H3), Section 9 of RFC 6189 */
uint8_t o_hash[4][32]; /* our session hashes */
uint8_t r_hash[4][32]; /* remote's session hashes */

uint64_t r_mac[4];

/* Section 4.4.1.4 */
uint8_t total_hash[32];
} zrtp_hash_ctx_t;

/* Collection of algorithms that are used by ZRTP
* (based on information gathered from Hello message) */
typedef struct zrtp_session {
int role = 0; /* initiator/responder */
uint32_t ssrc = 0;
uint16_t seq = 0;

uint32_t hash_algo = 0;
uint32_t cipher_algo = 0;
uint32_t auth_tag_type = 0;
uint32_t key_agreement_type = 0;
uint32_t sas_type = 0;

/* Session capabilities */
zrtp_capab_t capabilities;

/* Various hash values of the ZRTP session */
zrtp_hash_ctx_t hash_ctx;

/* DH-related variables */
zrtp_dh_ctx_t dh_ctx;

/* ZRTP keying material (for HMAC/AES etc) */
zrtp_key_ctx_t key_ctx;

/* Retained and shared secrets of the ZRTP session */
zrtp_secrets_t secrets;

uint8_t o_zid[12]; /* our ZID */
uint8_t r_zid[12]; /* remote ZID */

/* Pointers to messages sent by us and messages received from remote.
* These are used to calculate various hash values */
zrtp_messages_t l_msg;
zrtp_messages_t r_msg;
} zrtp_session_t;


}

namespace uvg_rtp = uvgrtp;
2 changes: 0 additions & 2 deletions src/zrtp/dh_kxchng.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include "zrtp_receiver.hh"

#include "../zrtp.hh"

#include "uvgrtp/crypto.hh"
#include "uvgrtp/frame.hh"
#include "uvgrtp/socket.hh"
Expand Down
2 changes: 0 additions & 2 deletions src/zrtp/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include "zrtp_receiver.hh"

#include "../zrtp.hh"

#include "uvgrtp/crypto.hh"
#include "uvgrtp/socket.hh"
#include "uvgrtp/frame.hh"
Expand Down
2 changes: 0 additions & 2 deletions src/zrtp/hello.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include "zrtp_receiver.hh"

#include "../zrtp.hh"

#include "uvgrtp/crypto.hh"
#include "uvgrtp/frame.hh"
#include "uvgrtp/socket.hh"
Expand Down
2 changes: 0 additions & 2 deletions src/zrtp/hello_ack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include "zrtp_receiver.hh"

#include "../zrtp.hh"

#include "uvgrtp/crypto.hh"
#include "uvgrtp/frame.hh"
#include "uvgrtp/socket.hh"
Expand Down
4 changes: 3 additions & 1 deletion src/zrtp/zrtp_message.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "zrtp_receiver.hh"
#include "defines.hh"
#include "../zrtp.hh"

#include "uvgrtp/frame.hh"
#include "uvgrtp/util.hh"
Expand All @@ -10,6 +10,8 @@

namespace uvgrtp {

class socket;

namespace zrtp_msg {

class zrtp_message {
Expand Down

0 comments on commit 8952434

Please sign in to comment.