2020-01-28 06:11:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-06-02 08:02:21 +00:00
|
|
|
#if __cplusplus >= 201703L || _MSC_VER >= 1911
|
2020-12-11 04:24:22 +00:00
|
|
|
#if __has_include(<cryptopp/aes.h>) && \
|
|
|
|
|
__has_include(<cryptopp/base32.h>) && \
|
|
|
|
|
__has_include(<cryptopp/cryptlib.h>) && \
|
|
|
|
|
__has_include(<cryptopp/dh.h>) && \
|
|
|
|
|
__has_include(<cryptopp/hmac.h>) && \
|
|
|
|
|
__has_include(<cryptopp/modes.h>) && \
|
|
|
|
|
__has_include(<cryptopp/osrng.h>) && \
|
|
|
|
|
__has_include(<cryptopp/sha.h>) && \
|
|
|
|
|
__has_include(<cryptopp/crc.h>) && \
|
|
|
|
|
!defined(__RTP_NO_CRYPTO__)
|
|
|
|
|
|
|
|
|
|
#define __RTP_CRYPTO__
|
|
|
|
|
|
2020-02-13 06:36:23 +00:00
|
|
|
#include <cryptopp/aes.h>
|
|
|
|
|
#include <cryptopp/base32.h>
|
|
|
|
|
#include <cryptopp/cryptlib.h>
|
|
|
|
|
#include <cryptopp/dh.h>
|
|
|
|
|
#include <cryptopp/hmac.h>
|
|
|
|
|
#include <cryptopp/modes.h>
|
|
|
|
|
#include <cryptopp/osrng.h>
|
|
|
|
|
#include <cryptopp/sha.h>
|
|
|
|
|
#include <cryptopp/crc.h>
|
2020-12-11 04:24:22 +00:00
|
|
|
|
2020-09-09 07:31:11 +00:00
|
|
|
#endif
|
2021-06-01 07:54:23 +00:00
|
|
|
#else // __cplusplus < 201703L
|
2021-05-25 15:58:30 +00:00
|
|
|
#ifndef __RTP_NO_CRYPTO__
|
|
|
|
|
#define __RTP_CRYPTO__
|
|
|
|
|
|
|
|
|
|
#include <cryptopp/aes.h>
|
|
|
|
|
#include <cryptopp/base32.h>
|
|
|
|
|
#include <cryptopp/cryptlib.h>
|
|
|
|
|
#include <cryptopp/dh.h>
|
|
|
|
|
#include <cryptopp/hmac.h>
|
|
|
|
|
#include <cryptopp/modes.h>
|
|
|
|
|
#include <cryptopp/osrng.h>
|
|
|
|
|
#include <cryptopp/sha.h>
|
|
|
|
|
#include <cryptopp/crc.h>
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
#endif // __cplusplus
|
2020-01-28 06:11:38 +00:00
|
|
|
|
2021-06-01 07:54:23 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2021-02-19 01:13:43 +00:00
|
|
|
namespace uvgrtp {
|
2020-01-28 06:11:38 +00:00
|
|
|
|
|
|
|
|
namespace crypto {
|
|
|
|
|
|
|
|
|
|
/* hash-based message authentication code */
|
|
|
|
|
namespace hmac {
|
|
|
|
|
class sha1 {
|
|
|
|
|
public:
|
|
|
|
|
sha1(uint8_t *key, size_t key_size);
|
|
|
|
|
~sha1();
|
|
|
|
|
|
|
|
|
|
void update(uint8_t *data, size_t len);
|
|
|
|
|
void final(uint8_t *digest);
|
|
|
|
|
|
2020-09-03 07:53:57 +00:00
|
|
|
/* truncate digest to "size" bytes */
|
|
|
|
|
void final(uint8_t *digest, size_t size);
|
|
|
|
|
|
2020-01-28 06:11:38 +00:00
|
|
|
private:
|
2020-09-04 08:25:16 +00:00
|
|
|
#ifdef __RTP_CRYPTO__
|
2020-01-28 06:11:38 +00:00
|
|
|
CryptoPP::HMAC<CryptoPP::SHA1> hmac_;
|
2020-09-04 08:25:16 +00:00
|
|
|
#endif
|
2020-01-28 06:11:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class sha256 {
|
|
|
|
|
public:
|
|
|
|
|
sha256(uint8_t *key, size_t key_size);
|
|
|
|
|
~sha256();
|
|
|
|
|
|
|
|
|
|
void update(uint8_t *data, size_t len);
|
|
|
|
|
void final(uint8_t *digest);
|
|
|
|
|
|
|
|
|
|
private:
|
2020-09-04 08:25:16 +00:00
|
|
|
#ifdef __RTP_CRYPTO__
|
2020-01-28 06:11:38 +00:00
|
|
|
CryptoPP::HMAC<CryptoPP::SHA256> hmac_;
|
2020-09-04 08:25:16 +00:00
|
|
|
#endif
|
2020-01-28 06:11:38 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class sha256 {
|
|
|
|
|
public:
|
|
|
|
|
sha256();
|
|
|
|
|
~sha256();
|
|
|
|
|
|
|
|
|
|
void update(uint8_t *data, size_t len);
|
|
|
|
|
void final(uint8_t *digest);
|
|
|
|
|
|
|
|
|
|
private:
|
2020-09-04 08:25:16 +00:00
|
|
|
#ifdef __RTP_CRYPTO__
|
2020-01-28 06:11:38 +00:00
|
|
|
CryptoPP::SHA256 sha_;
|
2020-09-04 08:25:16 +00:00
|
|
|
#endif
|
2020-01-28 06:11:38 +00:00
|
|
|
};
|
|
|
|
|
|
2020-01-31 09:46:39 +00:00
|
|
|
namespace aes {
|
2020-01-28 06:11:38 +00:00
|
|
|
|
2020-02-25 07:48:02 +00:00
|
|
|
class ecb {
|
|
|
|
|
public:
|
|
|
|
|
ecb(uint8_t *key, size_t key_size);
|
|
|
|
|
~ecb();
|
|
|
|
|
|
|
|
|
|
void encrypt(uint8_t *output, uint8_t *input, size_t len);
|
|
|
|
|
void decrypt(uint8_t *output, uint8_t *input, size_t len);
|
|
|
|
|
|
|
|
|
|
private:
|
2020-09-04 08:25:16 +00:00
|
|
|
#ifdef __RTP_CRYPTO__
|
2020-02-25 07:48:02 +00:00
|
|
|
CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption enc_;
|
|
|
|
|
CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption dec_;
|
2020-09-04 08:25:16 +00:00
|
|
|
#endif
|
2020-02-25 07:48:02 +00:00
|
|
|
};
|
|
|
|
|
|
2020-01-31 09:46:39 +00:00
|
|
|
class cfb {
|
|
|
|
|
public:
|
|
|
|
|
cfb(uint8_t *key, size_t key_size, uint8_t *iv);
|
|
|
|
|
~cfb();
|
2020-01-28 06:11:38 +00:00
|
|
|
|
2020-02-25 07:48:02 +00:00
|
|
|
void encrypt(uint8_t *output, uint8_t *input, size_t len);
|
|
|
|
|
void decrypt(uint8_t *output, uint8_t *input, size_t len);
|
2020-01-31 09:46:39 +00:00
|
|
|
|
|
|
|
|
private:
|
2020-09-04 08:25:16 +00:00
|
|
|
#ifdef __RTP_CRYPTO__
|
2020-01-31 09:46:39 +00:00
|
|
|
CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption enc_;
|
|
|
|
|
CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption dec_;
|
2020-09-04 08:25:16 +00:00
|
|
|
#endif
|
2020-01-31 09:46:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class ctr {
|
|
|
|
|
public:
|
|
|
|
|
ctr(uint8_t *key, size_t key_size, uint8_t *iv);
|
|
|
|
|
~ctr();
|
|
|
|
|
|
2020-02-25 07:48:02 +00:00
|
|
|
void encrypt(uint8_t *output, uint8_t *input, size_t len);
|
|
|
|
|
void decrypt(uint8_t *output, uint8_t *input, size_t len);
|
2020-01-31 09:46:39 +00:00
|
|
|
|
|
|
|
|
private:
|
2020-09-04 08:25:16 +00:00
|
|
|
#ifdef __RTP_CRYPTO__
|
2020-01-31 09:46:39 +00:00
|
|
|
CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption enc_;
|
|
|
|
|
CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption dec_;
|
2020-09-04 08:25:16 +00:00
|
|
|
#endif
|
2020-01-31 09:46:39 +00:00
|
|
|
};
|
2020-01-28 06:11:38 +00:00
|
|
|
};
|
|
|
|
|
|
2020-01-29 09:19:46 +00:00
|
|
|
/* diffie-hellman key derivation, 3072-bits */
|
2020-01-28 06:11:38 +00:00
|
|
|
class dh {
|
|
|
|
|
public:
|
|
|
|
|
dh();
|
|
|
|
|
~dh();
|
|
|
|
|
|
2020-01-29 09:19:46 +00:00
|
|
|
/* TODO: */
|
|
|
|
|
void generate_keys();
|
|
|
|
|
|
|
|
|
|
/* TODO: */
|
|
|
|
|
void get_pk(uint8_t *pk, size_t len);
|
|
|
|
|
|
|
|
|
|
/* TODO: */
|
|
|
|
|
void set_remote_pk(uint8_t *pk, size_t len);
|
|
|
|
|
|
|
|
|
|
/* TODO: */
|
|
|
|
|
void get_shared_secret(uint8_t *ss, size_t len);
|
2020-01-28 06:11:38 +00:00
|
|
|
|
|
|
|
|
private:
|
2020-09-04 08:25:16 +00:00
|
|
|
#ifdef __RTP_CRYPTO__
|
2020-01-28 06:11:38 +00:00
|
|
|
CryptoPP::AutoSeededRandomPool prng_;
|
|
|
|
|
CryptoPP::DH dh_;
|
2020-01-29 09:19:46 +00:00
|
|
|
CryptoPP::Integer sk_, pk_, rpk_;
|
2020-09-04 08:25:16 +00:00
|
|
|
#endif
|
2020-01-28 06:11:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* base32 */
|
|
|
|
|
class b32 {
|
|
|
|
|
public:
|
|
|
|
|
b32();
|
|
|
|
|
~b32();
|
|
|
|
|
|
|
|
|
|
void encode(uint8_t *input, uint8_t *output, size_t len);
|
|
|
|
|
|
|
|
|
|
private:
|
2020-09-04 08:25:16 +00:00
|
|
|
#ifdef __RTP_CRYPTO__
|
2020-01-28 06:11:38 +00:00
|
|
|
CryptoPP::Base32Encoder enc_;
|
2020-09-04 08:25:16 +00:00
|
|
|
#endif
|
2020-01-28 06:11:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace random {
|
|
|
|
|
void generate_random(uint8_t *out, size_t len);
|
|
|
|
|
};
|
2020-02-02 06:22:58 +00:00
|
|
|
|
|
|
|
|
namespace crc32 {
|
|
|
|
|
void get_crc32(uint8_t *input, size_t len, uint32_t *output);
|
|
|
|
|
bool verify_crc32(uint8_t *input, size_t len, uint32_t old_crc);
|
2020-08-13 03:47:26 +00:00
|
|
|
uint32_t calculate_crc32(uint8_t *input, size_t len);
|
2020-02-02 06:22:58 +00:00
|
|
|
};
|
2020-09-04 08:25:16 +00:00
|
|
|
|
|
|
|
|
bool enabled();
|
2020-01-28 06:11:38 +00:00
|
|
|
};
|
|
|
|
|
};
|
2021-02-19 01:13:43 +00:00
|
|
|
|
|
|
|
|
namespace uvg_rtp = uvgrtp;
|