2
0

cryspr.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * SRT - Secure, Reliable, Transport
  3. * Copyright (c) 2019 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. 2019-06-28 (jdube)
  14. CRYSPR/4SRT Initial implementation.
  15. *****************************************************************************/
  16. #ifndef CRYSPR_H
  17. #define CRYSPR_H
  18. #include <stdbool.h>
  19. #include <sys/types.h>
  20. #if !defined(HAISRT_VERSION_INT)
  21. #include "haicrypt.h"
  22. #include "hcrypt_msg.h"
  23. #else
  24. // Included by haisrt.h or similar
  25. #include "haisrt/haicrypt.h"
  26. #include "haisrt/hcrypt_msg.h"
  27. #endif
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include "cryspr-config.h"
  32. typedef struct tag_CRYSPR_cb {
  33. #ifdef CRYSPR2
  34. CRYSPR_AESCTX *aes_kek; /* Key Encrypting Key (KEK) */
  35. CRYSPR_AESCTX *aes_sek[2]; /* even/odd Stream Encrypting Key (SEK) */
  36. #define CRYSPR_GETKEK(cb) ((cb)->aes_kek)
  37. #define CRYSPR_GETSEK(cb,kk) ((cb)->aes_sek[kk])
  38. #else /*CRYSPR2*/
  39. CRYSPR_AESCTX aes_kek; /* Key Encrypting Key (KEK) */
  40. CRYSPR_AESCTX aes_sek[2]; /* even/odd Stream Encrypting Key (SEK) */
  41. #define CRYSPR_GETKEK(cb) (&((cb)->aes_kek))
  42. #define CRYSPR_GETSEK(cb,kk) (&((cb)->aes_sek[kk]))
  43. #endif /*CRYSPR2*/
  44. struct tag_CRYSPR_methods *cryspr;
  45. #if !CRYSPR_HAS_AESCTR
  46. /* Reserve room to build the counter stream ourself */
  47. #define HCRYPT_CTR_BLK_SZ CRYSPR_AESBLKSZ
  48. #define HCRYPT_CTR_STREAM_SZ 2048
  49. unsigned char * ctr_stream;
  50. size_t ctr_stream_len; /* Content size */
  51. size_t ctr_stream_siz; /* Allocated length */
  52. #endif /* !CRYSPR_HAS_AESCTR */
  53. #define CRYSPR_OUTMSGMAX 6
  54. uint8_t * outbuf; /* output circle buffer */
  55. size_t outbuf_ofs; /* write offset in circle buffer */
  56. size_t outbuf_siz; /* circle buffer size */
  57. } CRYSPR_cb;
  58. typedef struct tag_CRYSPR_methods {
  59. /*
  60. * prng:
  61. * Pseudo-Random Number Generator
  62. */
  63. int (*prng)(
  64. unsigned char *rn, /* out: pseudo random number */
  65. int rn_len);
  66. int (*aes_set_key)(
  67. int cipher_type, /* One of HCRYPT_CTX_MODE_[CLRTXT|AESECB|AESCTR|AESGDM] */
  68. bool bEncrypt, /* true Enxcrypt key, false: decrypt */
  69. const unsigned char *kstr,/* key string*/
  70. size_t kstr_len, /* kstr len in bytes (16, 24, or 32 bytes (for AES128,AES192, or AES256) */
  71. CRYSPR_AESCTX *aeskey); /* Cryptolib Specific AES key context */
  72. int (*aes_ecb_cipher)(
  73. bool bEncrypt, /* true:encrypt false:decrypt */
  74. CRYSPR_AESCTX *aes_key, /* ctx */
  75. const unsigned char *indata, /* src (clear text)*/
  76. size_t inlen, /* src length */
  77. unsigned char *out_txt, /* dst (cipher text) */
  78. size_t *outlen); /* dst length */
  79. int (*aes_ctr_cipher)(
  80. bool bEncrypt, /* true:encrypt false:decrypt (don't care with CTR) */
  81. CRYSPR_AESCTX *aes_key, /* ctx */
  82. unsigned char *iv, /* iv */
  83. const unsigned char *indata, /* src (clear text) */
  84. size_t inlen, /* src length */
  85. unsigned char *out_txt);/* dest */
  86. int (*aes_gcm_cipher)(
  87. bool bEncrypt, /* true:encrypt false:decrypt (don't care with CTR) */
  88. CRYSPR_AESCTX* aes_key, /* ctx */
  89. unsigned char* iv, /* iv */
  90. const unsigned char* aad, /* associated data */
  91. size_t aadlen,
  92. const unsigned char* indata, /* src (clear text) */
  93. size_t inlen, /* src length */
  94. unsigned char* out_txt, /* dest */
  95. unsigned char* out_tag);
  96. unsigned char *(*sha1_msg_digest)(
  97. const unsigned char *m, /* in: message */
  98. size_t m_len, /* message length */
  99. unsigned char *md); /* out: message digest buffer *160 bytes */
  100. /*
  101. * open:
  102. * Create a cipher instance
  103. * Allocate output buffers
  104. */
  105. CRYSPR_cb *(*open)(
  106. struct tag_CRYSPR_methods *cryspr,
  107. size_t max_len); /* Maximum packet length that will be encrypted/decrypted */
  108. /*
  109. * close:
  110. * Release any cipher resources
  111. */
  112. int (*close)(
  113. CRYSPR_cb *cryspr_data); /* Cipher handle, internal data */
  114. /*
  115. * pbkdf2_hmac_sha1
  116. * Password-based Key Derivation Function 2
  117. */
  118. int (*km_pbkdf2)(
  119. CRYSPR_cb *cryspr_cb, /* Cryspr Control Block */
  120. char *passwd, /* passphrase */
  121. size_t passwd_len, /* passphrase len */
  122. unsigned char *salt, /* salt */
  123. size_t salt_len, /* salt_len */
  124. int itr, /* iterations */
  125. size_t out_len, /* key_len */
  126. unsigned char *out); /* derived key */
  127. /*
  128. * km_setkey:
  129. * Set the Key Encypting Key for Wrap (Encryption) or UnWrap (Decryption).
  130. * Context (ctx) tells if it's for Wrap or Unwrap
  131. * A Context flags (ctx->flags) also tells if this is for wrap(encryption) or unwrap(decryption) context (HCRYPT_CTX_F_ENCRYPT)
  132. */
  133. int (*km_setkey)(
  134. CRYSPR_cb *cryspr_cb, /* Cryspr Control Block */
  135. bool bWrap, /* True: Wrap KEK, False: Unwrap KEK */
  136. const unsigned char *kek, size_t kek_len); /* KEK: Key Encrypting Key */
  137. /*
  138. * km_wrap:
  139. * wrap media stream key
  140. */
  141. int (*km_wrap)(CRYSPR_cb *cryspr_cb,
  142. unsigned char *wrap,
  143. const unsigned char *sek,
  144. unsigned int seklen);
  145. /*
  146. * km_unwrap:
  147. * wrap media stream key
  148. */
  149. int (*km_unwrap)(CRYSPR_cb *cryspr_cb,
  150. unsigned char *sek,
  151. const unsigned char *wrap,
  152. unsigned int wraplen);
  153. /*
  154. * setkey:
  155. * Set the Odd or Even, Encryption or Decryption key.
  156. * Context (ctx) tells if it's for Odd or Even key (hcryptCtx_GetKeyIndex(ctx))
  157. * A Context flags (ctx->flags) also tells if this is an encryption or decryption context (HCRYPT_CTX_F_ENCRYPT)
  158. */
  159. int (*ms_setkey)(
  160. CRYSPR_cb *cryspr_cb, /* Cryspr Control Block */
  161. hcrypt_Ctx *ctx, /* HaiCrypt Context (cipher, keys, Odd/Even, etc..) */
  162. const unsigned char *key, size_t kwelen); /* New Key */
  163. /*
  164. * encrypt:
  165. * Submit a list of nbin clear transport packets (hcrypt_DataDesc *in_data) to encryption
  166. * returns *nbout encrypted data packets of length out_len_p[] into out_p[]
  167. *
  168. * If cipher implements deferred encryption (co-processor, async encryption),
  169. * it may return no encrypted packets, or encrypted packets for clear text packets of a previous call.
  170. */
  171. int (*ms_encrypt)(
  172. CRYSPR_cb *cryspr_cb, /* Cryspr Control Block */
  173. hcrypt_Ctx *ctx, /* HaiCrypt Context (cipher, keys, Odd/Even, etc..) */
  174. hcrypt_DataDesc *in_data, int nbin, /* Clear text transport packets: header and payload */
  175. void *out_p[], size_t out_len_p[], int *nbout); /* Encrypted packets */
  176. /*
  177. * decrypt:
  178. * Submit a list of nbin encrypted transport packets (hcrypt_DataDesc *in_data) to decryption
  179. * returns *nbout clear text data packets of length out_len_p[] into out_p[]
  180. *
  181. * If cipher implements deferred decryption (co-processor, async encryption),
  182. * it may return no decrypted packets, or decrypted packets for encrypted packets of a previous call.
  183. */
  184. int (*ms_decrypt)(
  185. CRYSPR_cb *cryspr_cb, /* Cryspr Control Block */
  186. hcrypt_Ctx *ctx, /* HaiCrypt Context (cipher, keys, Odd/Even, etc..) */
  187. hcrypt_DataDesc *in_data, int nbin, /* Clear text transport packets: header and payload */
  188. void *out_p[], size_t out_len_p[], int *nbout); /* Encrypted packets */
  189. } CRYSPR_methods;
  190. CRYSPR_cb *crysprHelper_Open(CRYSPR_methods *cryspr, size_t cb_len, size_t max_len);
  191. int crysprHelper_Close(CRYSPR_cb *cryspr_cb);
  192. CRYSPR_methods *crysprInit(CRYSPR_methods *cryspr);
  193. #ifdef __cplusplus
  194. }
  195. #endif
  196. #endif /* CRYSPR_H */