hcrypt_msg.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 HCRYPT_MSG_H
  19. #define HCRYPT_MSG_H
  20. /*
  21. * HaiCrypt Transport Message Header info
  22. */
  23. #ifndef HCRYPT_DSP
  24. #include <sys/types.h>
  25. typedef uint32_t hcrypt_Pki;
  26. #endif /* HCRYPT_DSP */
  27. #define HCRYPT_MSG_VERSION 1 /* Current HaiCrypt version */
  28. #define HCRYPT_MSG_SIGN (('H'-'@')<<10 | ('A'-'@')<<5 | ('I'-'@')) /* Haivision PnP Mfr ID 'HAI' */
  29. #define HCRYPT_PKI_SZ 4 /* Packet Index size (CTR mode cipher) */
  30. #define HCRYPT_MSG_PT_MS 1 /* Media stream */
  31. #define HCRYPT_MSG_PT_KM 2 /* Keying Material */
  32. #define HCRYPT_MSG_PT_RESV7 7 /* Reserved to dicriminate MPEG-TS packet (SyncByte=0x47) */
  33. #define HCRYPT_MSG_F_eSEK 0x01 /* Even Stream Encrypting Key */
  34. #define HCRYPT_MSG_F_oSEK 0x02 /* Odd Stream Encrypting Key */
  35. #define HCRYPT_MSG_F_xSEK 0x03 /* Both Stream Encrypting Keys */
  36. typedef struct {
  37. int hdr_len; // data and control common prefix portion
  38. int pfx_len; // Message Prefix len. Also payload offset
  39. unsigned (*getKeyFlags)(unsigned char *msg);
  40. hcrypt_Pki (*getPki)(unsigned char *msg, int nwko);
  41. void (*setPki)(unsigned char *msg, hcrypt_Pki);
  42. void (*resetCache)(unsigned char *pfx_cache, unsigned pkt_type, unsigned flags);
  43. void (*indexMsg)(unsigned char *msg, unsigned char *pfx_cache);
  44. int (*parseMsg)(unsigned char *msg);
  45. }hcrypt_MsgInfo;
  46. #define hcryptMsg_GetKeyIndex(mi,msg) ((mi)->getKeyFlags(msg)>>1)
  47. #define hcryptMsg_GetPki(mi,msg,nwko) ((mi)->getPki(msg,nwko))
  48. #define hcryptMsg_SetPki(mi,msg,pki) (mi)->setPki(msg, pki)
  49. #define hcryptMsg_HasEvenSek(mi,msg) ((mi)->getKeyFlags(msg) & HCRYPT_MSG_F_eSEK)
  50. #define hcryptMsg_HasOddSek(mi,msg) ((mi)->getKeyFlags(msg) & HCRYPT_MSG_F_oSEK)
  51. #define hcryptMsg_HasBothSek(mi,msg) (HCRYPT_MSG_F_xSEK == ((mi)->getKeyFlags(msg) & HCRYPT_MSG_F_xSEK))
  52. #define hcryptMsg_HasNoSek(mi,msg) (0 == ((mi)->getKeyFlags(msg) & HCRYPT_MSG_F_xSEK))
  53. #define hcryptMsg_PaddedLen(len, fact) ((((len)+(fact)-1)/(fact))*(fact))
  54. /*
  55. * HaiCrypt KMmsg (Keying Material):
  56. *
  57. * 0 1 2 3
  58. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  59. * +-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-+
  60. *+0x00 |0|Vers | PT | Sign | resv |KF |
  61. * +-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-+
  62. *+0x04 | KEKI |
  63. * +-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-+
  64. *+0x08 | Cipher | Auth | SE | Resv1 |
  65. * +-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-+
  66. *+0x0C | Resv2 | Slen/4 | Klen/4 |
  67. * +-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-+
  68. *+0x10 | Salt |
  69. * | ... |
  70. * +-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-+
  71. * | Wrap |
  72. * | ... |
  73. * +-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-+
  74. */
  75. #define HCRYPT_MSG_KM_OFS_VERSION 0
  76. #define HCRYPT_MSG_KM_OFS_PT 0
  77. #define HCRYPT_MSG_KM_OFS_SIGN 1
  78. #define HCRYPT_MSG_KM_OFS_KFLGS 3
  79. #define HCRYPT_MSG_KM_RSH_KFLGS 0 /* Right shift (in byte) */
  80. #define HCRYPT_MSG_KM_OFS_KEKI 4
  81. #define HCRYPT_MSG_KM_OFS_CIPHER 8
  82. #define HCRYPT_MSG_KM_OFS_AUTH 9
  83. #define HCRYPT_MSG_KM_OFS_SE 10
  84. #define HCRYPT_MSG_KM_OFS_RESV2 12
  85. #define HCRYPT_MSG_KM_OFS_SLEN 14
  86. #define HCRYPT_MSG_KM_OFS_KLEN 15
  87. #define HCRYPT_MSG_KM_OFS_SALT 16
  88. #define HCRYPT_MSG_KM_MAX_SZ (0 \
  89. + HCRYPT_MSG_KM_OFS_SALT \
  90. + HAICRYPT_SALT_SZ \
  91. + (HAICRYPT_KEY_MAX_SZ * 2) \
  92. + HAICRYPT_WRAPKEY_SIGN_SZ)
  93. #define HCRYPT_CIPHER_NONE 0
  94. #define HCRYPT_CIPHER_AES_ECB 1
  95. #define HCRYPT_CIPHER_AES_CTR 2
  96. #define HCRYPT_CIPHER_AES_CBC 3
  97. #define HCRYPT_CIPHER_AES_GCM 4
  98. #define HCRYPT_AUTH_NONE 0
  99. #define HCRYPT_AUTH_AES_GCM 1
  100. #define HCRYPT_SE_TSUDP 1
  101. hcrypt_MsgInfo * hcryptMsg_STA_MsgInfo(void);
  102. #define HCRYPT_SE_TSSRT 2
  103. hcrypt_MsgInfo * hcryptMsg_SRT_MsgInfo(void);
  104. #define hcryptMsg_KM_GetVersion(msg) (((msg)[HCRYPT_MSG_KM_OFS_VERSION]>>4)& 0xF)
  105. #define hcryptMsg_KM_GetPktType(msg) (((msg)[HCRYPT_MSG_KM_OFS_PT]) & 0xF)
  106. #define hcryptMsg_KM_GetSign(msg) (((msg)[HCRYPT_MSG_KM_OFS_SIGN]<<8) | (msg)[HCRYPT_MSG_KM_OFS_SIGN+1])
  107. #define hcryptMsg_KM_GetKeyIndex(msg) (((msg)[HCRYPT_MSG_KM_OFS_KFLGS] & HCRYPT_MSG_F_xSEK)>>1)
  108. #define hcryptMsg_KM_HasEvenSek(msg) ((msg)[HCRYPT_MSG_KM_OFS_KFLGS] & HCRYPT_MSG_F_eSEK)
  109. #define hcryptMsg_KM_HasOddSek(msg) ((msg)[HCRYPT_MSG_KM_OFS_KFLGS] & HCRYPT_MSG_F_oSEK)
  110. #define hcryptMsg_KM_HasBothSek(msg) (HCRYPT_MSG_F_xSEK == ((msg)[HCRYPT_MSG_KM_OFS_KFLGS] & HCRYPT_MSG_F_xSEK))
  111. #define hcryptMsg_KM_HasNoSek(msg) (0 == ((msg)[HCRYPT_MSG_KM_OFS_KFLGS] & HCRYPT_MSG_F_xSEK))
  112. #define hcryptMsg_KM_GetCipher(msg) ((msg)[HCRYPT_MSG_KM_OFS_CIPHER])
  113. #define hcryptMsg_KM_GetAuth(msg) ((msg)[HCRYPT_MSG_KM_OFS_AUTH])
  114. #define hcryptMsg_KM_GetSE(msg) ((msg)[HCRYPT_MSG_KM_OFS_SE])
  115. #define hcryptMsg_KM_GetSaltLen(msg) (size_t)((msg)[HCRYPT_MSG_KM_OFS_SLEN] * 4)
  116. #define hcryptMsg_KM_GetSekLen(msg) (size_t)((msg)[HCRYPT_MSG_KM_OFS_KLEN] * 4)
  117. #define hcryptMsg_KM_SetSaltLen(msg,len)do {(msg)[HCRYPT_MSG_KM_OFS_SLEN] = (unsigned char)(len)/4;} while(0)
  118. #define hcryptMsg_KM_SetSekLen(msg,len) do {(msg)[HCRYPT_MSG_KM_OFS_KLEN] = (unsigned char)(len)/4;} while(0)
  119. #endif /* HCRYPT_MSG_H */