haicrypt.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * SRT - Secure, Reliable, Transport
  3. * Copyright (c) 2018 Haivision Systems Inc.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. */
  10. /*****************************************************************************
  11. written by
  12. Haivision Systems Inc.
  13. 2011-06-23 (jdube)
  14. HaiCrypt initial implementation.
  15. 2014-03-11 (jdube)
  16. Adaptation for SRT.
  17. *****************************************************************************/
  18. #ifndef HAICRYPT_H
  19. #define HAICRYPT_H
  20. #include <sys/types.h>
  21. #include <stdint.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. typedef void *HaiCrypt_Cryspr;
  26. HaiCrypt_Cryspr HaiCryptCryspr_Get_Instance (void); /* Return a default cryspr instance */
  27. #define HAICRYPT_CIPHER_BLK_SZ 16 /* AES Block Size */
  28. #define HAICRYPT_PWD_MAX_SZ 80 /* MAX password (for Password-based Key Derivation) */
  29. #define HAICRYPT_KEY_MAX_SZ 32 /* MAX key */
  30. #define HAICRYPT_SECRET_MAX_SZ (HAICRYPT_PWD_MAX_SZ > HAICRYPT_KEY_MAX_SZ ? HAICRYPT_PWD_MAX_SZ : HAICRYPT_KEY_MAX_SZ)
  31. #define HAICRYPT_AUTHTAG_MAX 16 /* maximum length of the auth tag (e.g. GCM) */
  32. #define HAICRYPT_SALT_SZ 16
  33. #define HAICRYPT_WRAPKEY_SIGN_SZ 8 /* RFC3394 AES KeyWrap signature size */
  34. #define HAICRYPT_PBKDF2_SALT_LEN 8 /* PKCS#5 PBKDF2 Password based key derivation salt length */
  35. #define HAICRYPT_PBKDF2_ITER_CNT 2048 /* PKCS#5 PBKDF2 Password based key derivation iteration count */
  36. #define HAICRYPT_TS_PKT_SZ 188 /* Transport Stream packet size */
  37. typedef struct {
  38. #define HAICRYPT_SECTYP_UNDEF 0
  39. #define HAICRYPT_SECTYP_PRESHARED 1 /* Preshared KEK */
  40. #define HAICRYPT_SECTYP_PASSPHRASE 2 /* Password */
  41. unsigned typ;
  42. size_t len;
  43. unsigned char str[HAICRYPT_SECRET_MAX_SZ];
  44. }HaiCrypt_Secret;
  45. typedef struct {
  46. #define HAICRYPT_CFG_F_TX 0x01 /* !TX -> RX */
  47. #define HAICRYPT_CFG_F_CRYPTO 0x02 /* Perform crypto Tx:Encrypt Rx:Decrypt */
  48. #define HAICRYPT_CFG_F_FEC 0x04 /* Do Forward Error Correction */
  49. #define HAICRYPT_CFG_F_GCM 0x08 /* Use AES-GCM */
  50. unsigned flags;
  51. HaiCrypt_Secret secret; /* Security Association */
  52. HaiCrypt_Cryspr cryspr; /* CRYSPR implementation */
  53. #define HAICRYPT_DEF_KEY_LENGTH 16 /* default key length (bytes) */
  54. size_t key_len; /* SEK length (bytes) */
  55. #define HAICRYPT_DEF_DATA_MAX_LENGTH 1500 /* default packet data length (bytes) */
  56. size_t data_max_len; /* Maximum data_len passed to HaiCrypt (bytes) */
  57. #define HAICRYPT_XPT_STANDALONE 0
  58. #define HAICRYPT_XPT_SRT 1
  59. int xport;
  60. #define HAICRYPT_DEF_KM_TX_PERIOD 1000 /* Keying Material Default Tx Period (msec) */
  61. unsigned int km_tx_period_ms; /* Keying Material Tx period (msec) */
  62. #define HAICRYPT_DEF_KM_REFRESH_RATE 0x1000000 /* Keying Material Default Refresh Rate (pkts) */
  63. unsigned int km_refresh_rate_pkt; /* Keying Material Refresh Rate (pkts) */
  64. #define HAICRYPT_DEF_KM_PRE_ANNOUNCE 0x1000 /* Keying Material Default Pre/Post Announce (pkts) */
  65. unsigned int km_pre_announce_pkt; /* Keying Material Pre/Post Announce (pkts) */
  66. }HaiCrypt_Cfg;
  67. typedef enum HaiCrypt_CryptoDir { HAICRYPT_CRYPTO_DIR_RX, HAICRYPT_CRYPTO_DIR_TX } HaiCrypt_CryptoDir;
  68. //typedef void *HaiCrypt_Handle;
  69. // internally it will be correctly interpreted,
  70. // for the outsider it's just some kinda incomplete type
  71. // but still if you use any kinda pointer instead, you'll get complaints
  72. typedef struct hcrypt_Session_str* HaiCrypt_Handle;
  73. int HaiCrypt_SetLogLevel(int level, int logfa);
  74. int HaiCrypt_Create(const HaiCrypt_Cfg *cfg, HaiCrypt_Handle *phhc);
  75. int HaiCrypt_Clone(HaiCrypt_Handle hhcSrc, HaiCrypt_CryptoDir tx, HaiCrypt_Handle *phhc);
  76. int HaiCrypt_Close(HaiCrypt_Handle hhc);
  77. int HaiCrypt_Tx_GetBuf(HaiCrypt_Handle hhc, size_t data_len, unsigned char **in_p);
  78. int HaiCrypt_Tx_Process(HaiCrypt_Handle hhc, unsigned char *in, size_t in_len,
  79. void *out_p[], size_t out_len_p[], int maxout);
  80. int HaiCrypt_Rx_Process(HaiCrypt_Handle hhc, unsigned char *in, size_t in_len,
  81. void *out_p[], size_t out_len_p[], int maxout);
  82. int HaiCrypt_Tx_GetKeyFlags(HaiCrypt_Handle hhc);
  83. int HaiCrypt_Tx_ManageKeys(HaiCrypt_Handle hhc, void *out_p[], size_t out_len_p[], int maxout);
  84. int HaiCrypt_Tx_Data(HaiCrypt_Handle hhc, unsigned char *pfx, unsigned char *data, size_t data_len);
  85. int HaiCrypt_Rx_Data(HaiCrypt_Handle hhc, unsigned char *pfx, unsigned char *data, size_t data_len);
  86. /// @brief Check if the crypto service provider supports AES GCM.
  87. /// @return returns 1 if AES GCM is supported, 0 otherwise.
  88. int HaiCrypt_IsAESGCM_Supported(void);
  89. /* Status values */
  90. #define HAICRYPT_ERROR -1
  91. #define HAICRYPT_ERROR_WRONG_SECRET -2
  92. #define HAICRYPT_ERROR_CIPHER -3
  93. #define HAICRYPT_OK 0
  94. #ifdef __cplusplus
  95. }
  96. #endif
  97. #endif /* HAICRYPT_H */