live
SRTPCryptographicContext.hh
Go to the documentation of this file.
1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15**********/
16// Copyright (c) 1996-2025 Live Networks, Inc. All rights reserved.
17// The SRTP 'Cryptographic Context', used in all of our uses of SRTP.
18// Definition
19
20#ifndef _SRTP_CRYPTOGRAPHIC_CONTEXT_HH
21#define _SRTP_CRYPTOGRAPHIC_CONTEXT_HH
22
23#ifndef _MIKEY_HH
24#include "MIKEY.hh"
25#endif
26
28public:
31
32 // Authenticate (if necessary) and decrypt (if necessary) incoming SRTP and SRTCP packets.
33 // Returns True iff the packet is well-formed and authenticates OK.
34 // ("outPacketSize" will be <= "inPacketSize".)
35 Boolean processIncomingSRTPPacket(u_int8_t* buffer, unsigned inPacketSize,
36 unsigned& outPacketSize);
37 Boolean processIncomingSRTCPPacket(u_int8_t* buffer, unsigned inPacketSize,
38 unsigned& outPacketSize);
39
40 // Encrypt (if necessary) and add an authentication tag (if necessary) to an outgoing
41 // RTP and RTCP packet.
42 // Returns True iff the packet is well-formed.
43 // ("outPacketSize" will be >= "inPacketSize"; there must be enough space at the end of
44 // "buffer" for the extra (4+10 bytes for SRTP; 4+4+10 bytes for SRTCP).)
45 Boolean processOutgoingSRTPPacket(u_int8_t* buffer, unsigned inPacketSize,
46 unsigned& outPacketSize);
47 Boolean processOutgoingSRTCPPacket(u_int8_t* buffer, unsigned inPacketSize,
48 unsigned& outPacketSize);
49
50 u_int32_t sendingROC() const;
51
52#ifndef NO_OPENSSL
53private:
54 // Definitions specific to the "SRTP_AES128_CM_HMAC_SHA1_80" ciphersuite.
55 // Later generalize to support more SRTP ciphersuites #####
56#define SRTP_CIPHER_KEY_LENGTH (128/8) // in bytes
57#define SRTP_CIPHER_SALT_LENGTH (112/8) // in bytes
58#define SRTP_MKI_LENGTH 4 // in bytes
59#define SRTP_AUTH_KEY_LENGTH (160/8) // in bytes
60#define SRTP_AUTH_TAG_LENGTH (80/8) // in bytes
61
62 struct derivedKeys {
66 };
67
71 };
72
73 typedef enum {
79 label_srtcp_salt = 0x05
81
82 unsigned generateSRTPAuthenticationTag(u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
83 u_int8_t* resultAuthenticationTag);
84 // returns the size of the resulting authentication tag
85 unsigned generateSRTCPAuthenticationTag(u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
86 u_int8_t* resultAuthenticationTag);
87 // returns the size of the resulting authentication tag
88
89 Boolean verifySRTPAuthenticationTag(u_int8_t* dataToAuthenticate, unsigned numBytesToAuthenticate,
90 u_int32_t roc, u_int8_t const* authenticationTag);
91 Boolean verifySRTCPAuthenticationTag(u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
92 u_int8_t const* authenticationTag);
93
94 void decryptSRTPPacket(u_int64_t index, u_int32_t ssrc, u_int8_t* data, unsigned numDataBytes);
95 void decryptSRTCPPacket(u_int32_t index, u_int32_t ssrc, u_int8_t* data, unsigned numDataBytes);
96
97 void encryptSRTPPacket(u_int64_t index, u_int32_t ssrc, u_int8_t* data, unsigned numDataBytes);
98 void encryptSRTCPPacket(u_int32_t index, u_int32_t ssrc, u_int8_t* data, unsigned numDataBytes);
99
101 u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
102 u_int8_t* resultAuthenticationTag);
103 // returns the size of the resulting authentication tag
104 // "resultAuthenticationTag" must point to an array of at least SRTP_AUTH_TAG_LENGTH
106 u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
107 u_int8_t const* authenticationTag);
108
109 void cryptData(derivedKeys& keys, u_int64_t index, u_int32_t ssrc,
110 u_int8_t* data, unsigned numDataBytes);
111
113
114 void deriveKeysFromMaster(u_int8_t const* masterKey, u_int8_t const* salt,
115 allDerivedKeys& allKeysResult);
116 // used to implement "performKeyDerivation()"
117 void deriveSingleKey(u_int8_t const* masterKey, u_int8_t const* salt,
119 unsigned resultKeyLength, u_int8_t* resultKey);
120 // used to implement "deriveKeysFromMaster()".
121 // ("resultKey" must be an existing buffer, of size >= "resultKeyLength")
122
123private:
125
126 // Master key + salt:
127 u_int8_t const* masterKeyPlusSalt() const { return fMIKEYState.keyData(); }
128
129 u_int8_t const* masterKey() const { return &masterKeyPlusSalt()[0]; }
130 u_int8_t const* masterSalt() const { return &masterKeyPlusSalt()[SRTP_CIPHER_KEY_LENGTH]; }
131
135 u_int32_t MKI() const { return fMIKEYState.MKI(); }
136
137 // Derived (i.e., session) keys:
139
140 // State used for handling the reception of SRTP packets:
143 u_int32_t fReceptionROC; // rollover counter
144
145 // State used for handling the sending of SRTP packets:
147 u_int32_t fSendingROC;
148
149 // State used for handling the sending of SRTCP packets:
150 u_int32_t fSRTCPIndex;
151#endif
152};
153
154#endif
unsigned char Boolean
Definition: Boolean.hh:25
#define SRTP_AUTH_KEY_LENGTH
#define SRTP_CIPHER_KEY_LENGTH
#define SRTP_CIPHER_SALT_LENGTH
u_int32_t MKI() const
Definition: MIKEY.hh:51
Boolean encryptSRTCP() const
Definition: MIKEY.hh:49
Boolean encryptSRTP() const
Definition: MIKEY.hh:48
Boolean useAuthentication() const
Definition: MIKEY.hh:53
u_int8_t const * keyData() const
Definition: MIKEY.hh:50
Boolean verifySRTCPAuthenticationTag(u_int8_t const *dataToAuthenticate, unsigned numBytesToAuthenticate, u_int8_t const *authenticationTag)
Boolean processIncomingSRTCPPacket(u_int8_t *buffer, unsigned inPacketSize, unsigned &outPacketSize)
u_int32_t sendingROC() const
Boolean verifyAuthenticationTag(derivedKeys &keysToUse, u_int8_t const *dataToAuthenticate, unsigned numBytesToAuthenticate, u_int8_t const *authenticationTag)
virtual ~SRTPCryptographicContext()
unsigned generateAuthenticationTag(derivedKeys &keysToUse, u_int8_t const *dataToAuthenticate, unsigned numBytesToAuthenticate, u_int8_t *resultAuthenticationTag)
void cryptData(derivedKeys &keys, u_int64_t index, u_int32_t ssrc, u_int8_t *data, unsigned numDataBytes)
void deriveKeysFromMaster(u_int8_t const *masterKey, u_int8_t const *salt, allDerivedKeys &allKeysResult)
Boolean processOutgoingSRTCPPacket(u_int8_t *buffer, unsigned inPacketSize, unsigned &outPacketSize)
u_int8_t const * masterKey() const
Boolean verifySRTPAuthenticationTag(u_int8_t *dataToAuthenticate, unsigned numBytesToAuthenticate, u_int32_t roc, u_int8_t const *authenticationTag)
void deriveSingleKey(u_int8_t const *masterKey, u_int8_t const *salt, SRTPKeyDerivationLabel label, unsigned resultKeyLength, u_int8_t *resultKey)
unsigned generateSRTCPAuthenticationTag(u_int8_t const *dataToAuthenticate, unsigned numBytesToAuthenticate, u_int8_t *resultAuthenticationTag)
Boolean processIncomingSRTPPacket(u_int8_t *buffer, unsigned inPacketSize, unsigned &outPacketSize)
u_int8_t const * masterSalt() const
void decryptSRTCPPacket(u_int32_t index, u_int32_t ssrc, u_int8_t *data, unsigned numDataBytes)
Boolean processOutgoingSRTPPacket(u_int8_t *buffer, unsigned inPacketSize, unsigned &outPacketSize)
unsigned generateSRTPAuthenticationTag(u_int8_t const *dataToAuthenticate, unsigned numBytesToAuthenticate, u_int8_t *resultAuthenticationTag)
u_int8_t const * masterKeyPlusSalt() const
void encryptSRTCPPacket(u_int32_t index, u_int32_t ssrc, u_int8_t *data, unsigned numDataBytes)
void decryptSRTPPacket(u_int64_t index, u_int32_t ssrc, u_int8_t *data, unsigned numDataBytes)
SRTPCryptographicContext(MIKEYState const &mikeyState)
void encryptSRTPPacket(u_int64_t index, u_int32_t ssrc, u_int8_t *data, unsigned numDataBytes)
u_int8_t cipherKey[SRTP_CIPHER_KEY_LENGTH]
u_int8_t salt[SRTP_CIPHER_SALT_LENGTH]