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 
12 /*****************************************************************************
13 written by
14    Haivision Systems Inc.
15 
16    2011-06-23 (jdube)
17         HaiCrypt initial implementation.
18    2014-03-11 (jdube)
19         Adaptation for SRT.
20 *****************************************************************************/
21 
22 #ifndef HCRYPT_CTX_H
23 #define HCRYPT_CTX_H
24 
25 #include <stdbool.h>
26 #include <sys/types.h>
27 #include "hcrypt.h"
28 
29 #if !defined(HAISRT_VERSION_INT)
30 #include "haicrypt.h"
31 #include "hcrypt_msg.h"
32 #else
33 // Included by haisrt.h or similar
34 #include "haisrt/haicrypt.h"
35 #include "haisrt/hcrypt_msg.h"
36 #endif
37 
38 typedef struct {
39         unsigned char *pfx; //Prefix described by transport msg info (in ctx)
40         unsigned char *payload;
41         size_t len; //Payload size
42 }hcrypt_DataDesc;
43 
44 
45 typedef struct tag_hcrypt_Ctx {
46         struct tag_hcrypt_Ctx * alt;    /* Alternative ctx (even/odd) */
47 
48 #define HCRYPT_CTX_F_MSG        0x00FF  /* Aligned wiht message header flags */
49 #define HCRYPT_CTX_F_eSEK       HCRYPT_MSG_F_eSEK
50 #define HCRYPT_CTX_F_oSEK       HCRYPT_MSG_F_oSEK
51 #define HCRYPT_CTX_F_xSEK       HCRYPT_MSG_F_xSEK
52 
53 #define HCRYPT_CTX_F_ENCRYPT    0x0100  /* 0:decrypt 1:encrypt */
54 #define HCRYPT_CTX_F_ANNOUNCE   0x0200  /* Announce KM */
55 #define HCRYPT_CTX_F_TTSEND     0x0400  /* time to send */
56         unsigned         flags;
57 #define hcryptCtx_GetKeyFlags(ctx)      ((ctx)->flags & HCRYPT_CTX_F_xSEK)
58 #define hcryptCtx_GetKeyIndex(ctx)      (((ctx)->flags & HCRYPT_CTX_F_xSEK)>>1)
59 
60 #define HCRYPT_CTX_S_INIT       1
61 #define HCRYPT_CTX_S_SARDY      2   /* Security Association (KEK) ready */
62 #define HCRYPT_CTX_S_KEYED      3   /* Media Stream Encrypting Key (SEK) ready */
63 #define HCRYPT_CTX_S_ACTIVE     4   /* Announced and in use */
64 #define HCRYPT_CTX_S_DEPRECATED 5   /* Still announced but no longer used */
65         unsigned         status;
66 
67 #define HCRYPT_CTX_MODE_CLRTXT  0   /* NULL cipher (for tests) */
68 #define HCRYPT_CTX_MODE_AESECB  1   /* Electronic Code Book mode */
69 #define HCRYPT_CTX_MODE_AESCTR  2   /* Counter mode */
70 #define HCRYPT_CTX_MODE_AESCBC  3   /* Cipher-block chaining mode */
71         unsigned         mode;
72 
73         struct {
74             size_t       key_len;
75             size_t       pwd_len;
76             char         pwd[HAICRYPT_PWD_MAX_SZ];
77         } cfg;
78 
79         size_t           salt_len;
80         unsigned char    salt[HAICRYPT_SALT_SZ];
81 
82         size_t           sek_len;
83         unsigned char    sek[HAICRYPT_KEY_MAX_SZ];
84 
85         hcrypt_MsgInfo * msg_info;  /* Transport message handler */
86         unsigned         pkt_cnt;   /* Key usage counter */
87 
88 #define HCRYPT_CTX_MAX_KM_PFX_SZ   16
89         size_t           KMmsg_len;
90         unsigned char    KMmsg_cache[HCRYPT_CTX_MAX_KM_PFX_SZ + HCRYPT_MSG_KM_MAX_SZ];
91 
92 #define HCRYPT_CTX_MAX_MS_PFX_SZ   16
93         unsigned char    MSpfx_cache[HCRYPT_CTX_MAX_MS_PFX_SZ];
94 } hcrypt_Ctx;
95 
96 
97 #endif /* HCRYPT_CTX_H */
98