2
0

hcrypt.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. 2014-03-26 (jsantiago)
  18. OS-X Build.
  19. 2014-03-27 (jdube)
  20. Remove dependency on internal Crypto API.
  21. 2016-07-22 (jsantiago)
  22. MINGW-W64 Build.
  23. *****************************************************************************/
  24. #ifndef INC_SRT_HCRYPT_H
  25. #define INC_SRT_HCRYPT_H
  26. #include <sys/types.h>
  27. #ifdef _WIN32
  28. #include <winsock2.h>
  29. #include <ws2tcpip.h>
  30. #else
  31. #include <sys/time.h>
  32. #endif
  33. #ifdef __GNUC__
  34. #define ATR_UNUSED __attribute__((unused))
  35. #else
  36. #define ATR_UNUSED
  37. #endif
  38. #include "haicrypt.h"
  39. #include "hcrypt_msg.h"
  40. #include "hcrypt_ctx.h"
  41. #include "cryspr.h"
  42. //#define HCRYPT_DEV 1 /* Development: should not be defined in committed code */
  43. #ifdef HAICRYPT_SUPPORT_CRYPTO_API
  44. /* See CRYPTOFEC_OBJECT in session structure */
  45. #define CRYPTO_API_SERVER 1 /* Enable handler's structures */
  46. #include "crypto_api.h"
  47. #endif /* HAICRYPT_SUPPORT_CRYPTO_API */
  48. typedef struct hcrypt_Session_str {
  49. #ifdef HAICRYPT_SUPPORT_CRYPTO_API
  50. /*
  51. * Resv matches internal upper layer handle (crypto_api)
  52. * They are not used in HaiCrypt.
  53. * This make 3 layers using the same handle.
  54. * To get rid of this dependency for a portable HaiCrypt,
  55. * revise caller (crypto_hc.c) to allocate its own buffer.
  56. */
  57. CRYPTOFEC_OBJECT resv; /* See above comment */
  58. #endif /* HAICRYPT_SUPPORT_CRYPTO_API */
  59. hcrypt_Ctx ctx_pair[2]; /* Even(0)/Odd(1) crypto contexts */
  60. hcrypt_Ctx * ctx; /* Current context */
  61. CRYSPR_methods * cryspr;
  62. CRYSPR_cb * cryspr_cb;
  63. unsigned char * inbuf; /* allocated if cipher has no getinbuf() func */
  64. size_t inbuf_siz;
  65. int se; /* Stream Encapsulation (HCRYPT_SE_xxx) */
  66. hcrypt_MsgInfo * msg_info;
  67. struct {
  68. size_t data_max_len;
  69. }cfg;
  70. struct {
  71. struct timeval tx_period; /* Keying Material tx period (milliseconds) */
  72. struct timeval tx_last; /* Keying Material last tx time */
  73. unsigned int refresh_rate; /* SEK use period */
  74. unsigned int pre_announce; /* Pre/Post next/old SEK announce */
  75. }km;
  76. } hcrypt_Session;
  77. #if ENABLE_HAICRYPT_LOGGING
  78. #include "haicrypt_log.h"
  79. #else
  80. #define HCRYPT_LOG_INIT()
  81. #define HCRYPT_LOG_EXIT()
  82. #define HCRYPT_LOG(lvl, fmt, ...)
  83. #endif
  84. #ifdef HCRYPT_DEV
  85. #define HCRYPT_PRINTKEY(key, len, tag) HCRYPT_LOG(LOG_DEBUG, \
  86. "%s[%d]=0x%02x%02x..%02x%02x\n", tag, len, \
  87. (key)[0], (key)[1], (key)[(len)-2], (key)[(len)-1])
  88. #else /* HCRYPT_DEV */
  89. #define HCRYPT_PRINTKEY(key,len,tag)
  90. #endif /* HCRYPT_DEV */
  91. #ifndef ASSERT
  92. #include <assert.h>
  93. #define ASSERT(c) assert(c)
  94. #endif
  95. /* HaiCrypt-TP CTR mode IV (128-bit):
  96. * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  97. * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  98. * | 0s | pki | ctr |
  99. * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  100. * XOR
  101. * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  102. * | nonce +
  103. * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  104. *
  105. * pki (32-bit): packet index
  106. * ctr (16-bit): block counter
  107. * nonce (112-bit): number used once (salt)
  108. */
  109. #define hcrypt_SetCtrIV(pki, nonce, iv) do { \
  110. memset(&(iv)[0], 0, 128/8); \
  111. memcpy(&(iv)[10], (pki), HCRYPT_PKI_SZ); \
  112. hcrypt_XorStream(&(iv)[0], (nonce), 112/8); \
  113. } while(0)
  114. #define hcrypt_XorStream(dst, strm, len) do { \
  115. int __XORSTREAMi; \
  116. for (__XORSTREAMi = 0 \
  117. ;__XORSTREAMi < (int)(len) \
  118. ;__XORSTREAMi += 1) { \
  119. (dst)[__XORSTREAMi] ^= (strm)[__XORSTREAMi]; \
  120. } \
  121. } while(0)
  122. int hcryptCtx_SetSecret(hcrypt_Session *crypto, hcrypt_Ctx *ctx, const HaiCrypt_Secret *secret);
  123. int hcryptCtx_GenSecret(hcrypt_Session *crypto, hcrypt_Ctx *ctx);
  124. int hcryptCtx_Tx_Init(hcrypt_Session *crypto, hcrypt_Ctx *ctx, const HaiCrypt_Cfg *cfg);
  125. int hcryptCtx_Tx_Rekey(hcrypt_Session *crypto, hcrypt_Ctx *ctx);
  126. int hcryptCtx_Tx_CloneKey(hcrypt_Session *crypto, hcrypt_Ctx *ctx, const hcrypt_Session* cryptoSrc);
  127. int hcryptCtx_Tx_Refresh(hcrypt_Session *crypto);
  128. int hcryptCtx_Tx_PreSwitch(hcrypt_Session *crypto);
  129. int hcryptCtx_Tx_Switch(hcrypt_Session *crypto);
  130. int hcryptCtx_Tx_PostSwitch(hcrypt_Session *crypto);
  131. int hcryptCtx_Tx_AsmKM(hcrypt_Session *crypto, hcrypt_Ctx *ctx, unsigned char *alt_sek);
  132. int hcryptCtx_Tx_ManageKM(hcrypt_Session *crypto);
  133. int hcryptCtx_Tx_InjectKM(hcrypt_Session *crypto, void *out_p[], size_t out_len_p[], int maxout);
  134. /// @brief Initialize receiving crypto context.
  135. /// @param crypto library instance handle.
  136. /// @param ctx additional crypto context.
  137. /// @param cfg crypto configuration.
  138. /// @return -1 on error, 0 otherwise.
  139. int hcryptCtx_Rx_Init(hcrypt_Session *crypto, hcrypt_Ctx *ctx, const HaiCrypt_Cfg *cfg);
  140. /// @brief Parse an incoming message related to cryptography module.
  141. /// @param crypto library instance handle.
  142. /// @param msg a message to parse.
  143. /// @param msg_len length of the message in bytes.
  144. /// @return 0 on success; -3 on cipher mode mismatch; -2 on unmatched shared secret; -1 on other failures.
  145. int hcryptCtx_Rx_ParseKM(hcrypt_Session *crypto, unsigned char *msg, size_t msg_len);
  146. #endif /* HCRYPT_H */