live
SRTPCryptographicContext.hh
Go to the documentation of this file.
1 /**********
2 This library is free software; you can redistribute it and/or modify it under
3 the terms of the GNU Lesser General Public License as published by the
4 Free Software Foundation; either version 3 of the License, or (at your
5 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6 
7 This library is distributed in the hope that it will be useful, but WITHOUT
8 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10 more details.
11 
12 You should have received a copy of the GNU Lesser General Public License
13 along with this library; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 **********/
16 // Copyright (c) 1996-2021 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 
28 public:
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  // 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 SRTCP tags (4+4+10 bytes).)
45  Boolean processOutgoingSRTCPPacket(u_int8_t* buffer, unsigned inPacketSize,
46  unsigned& outPacketSize);
47 
48 #ifndef NO_OPENSSL
49 private:
50  // Definitions specific to the "SRTP_AES128_CM_HMAC_SHA1_80" ciphersuite.
51  // Later generalize to support more SRTP ciphersuites #####
52 #define SRTP_CIPHER_KEY_LENGTH (128/8) // in bytes
53 #define SRTP_CIPHER_SALT_LENGTH (112/8) // in bytes
54 #define SRTP_MKI_LENGTH 4 // in bytes
55 #define SRTP_AUTH_KEY_LENGTH (160/8) // in bytes
56 #define SRTP_AUTH_TAG_LENGTH (80/8) // in bytes
57 
58  struct derivedKeys {
62  };
63 
64  struct allDerivedKeys {
67  };
68 
69  typedef enum {
75  label_srtcp_salt = 0x05
77 
78  unsigned generateSRTCPAuthenticationTag(u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
79  u_int8_t* resultAuthenticationTag);
80  // returns the size of the resulting authentication tag
81 
82  Boolean verifySRTPAuthenticationTag(u_int8_t* dataToAuthenticate, unsigned numBytesToAuthenticate,
83  u_int32_t roc, u_int8_t const* authenticationTag);
84  Boolean verifySRTCPAuthenticationTag(u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
85  u_int8_t const* authenticationTag);
86 
87  void decryptSRTPPacket(u_int64_t index, u_int32_t ssrc, u_int8_t* data, unsigned numDataBytes);
88  void decryptSRTCPPacket(u_int32_t index, u_int32_t ssrc, u_int8_t* data, unsigned numDataBytes);
89 
90  void encryptSRTCPPacket(u_int32_t index, u_int32_t ssrc, u_int8_t* data, unsigned numDataBytes);
91 
93  u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
94  u_int8_t* resultAuthenticationTag);
95  // returns the size of the resulting authentication tag
96  // "resultAuthenticationTag" must point to an array of at least SRTP_AUTH_TAG_LENGTH
98  u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
99  u_int8_t const* authenticationTag);
100 
101  void cryptData(derivedKeys& keys, u_int64_t index, u_int32_t ssrc,
102  u_int8_t* data, unsigned numDataBytes);
103 
105 
106  void deriveKeysFromMaster(u_int8_t const* masterKey, u_int8_t const* salt,
107  allDerivedKeys& allKeysResult);
108  // used to implement "performKeyDerivation()"
109  void deriveSingleKey(u_int8_t const* masterKey, u_int8_t const* salt,
111  unsigned resultKeyLength, u_int8_t* resultKey);
112  // used to implement "deriveKeysFromMaster()".
113  // ("resultKey" must be an existing buffer, of size >= "resultKeyLength")
114 
115 private:
117 
118  // Master key + salt:
119  u_int8_t const* masterKeyPlusSalt() const { return fMIKEYState.keyData(); }
120 
121  u_int8_t const* masterKey() const { return &masterKeyPlusSalt()[0]; }
122  u_int8_t const* masterSalt() const { return &masterKeyPlusSalt()[SRTP_CIPHER_KEY_LENGTH]; }
123 
127  u_int32_t MKI() const { return fMIKEYState.MKI(); }
128 
129  // Derived (i.e., session) keys:
131 
132  // State used for handling the reception of SRTP packets:
135  u_int32_t fROC; // rollover counter
136 
137  // State used for handling the sending of SRTCP packets:
138  u_int32_t fSRTCPIndex;
139 #endif
140 };
141 
142 #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:50
Boolean encryptSRTCP() const
Definition: MIKEY.hh:48
Boolean encryptSRTP() const
Definition: MIKEY.hh:47
Boolean useAuthentication() const
Definition: MIKEY.hh:51
u_int8_t const * keyData() const
Definition: MIKEY.hh:49
Boolean verifySRTCPAuthenticationTag(u_int8_t const *dataToAuthenticate, unsigned numBytesToAuthenticate, u_int8_t const *authenticationTag)
Boolean processIncomingSRTCPPacket(u_int8_t *buffer, unsigned inPacketSize, unsigned &outPacketSize)
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)
u_int8_t const * masterSalt() const
void deriveKeysFromMaster(u_int8_t const *masterKey, u_int8_t const *salt, allDerivedKeys &allKeysResult)
u_int8_t const * masterKeyPlusSalt() const
Boolean processOutgoingSRTCPPacket(u_int8_t *buffer, unsigned inPacketSize, unsigned &outPacketSize)
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)
void decryptSRTCPPacket(u_int32_t index, u_int32_t ssrc, u_int8_t *data, unsigned numDataBytes)
u_int8_t const * masterKey() 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)
u_int8_t cipherKey[SRTP_CIPHER_KEY_LENGTH]
u_int8_t salt[SRTP_CIPHER_SALT_LENGTH]