blob: 456e58fec49ba2bdf6d5571153d6e663dca18606 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
Alexandre Lisione24852d2014-02-04 13:13:02 -05002 Copyright (C) 2004-2006 the Minisip Team
3 Copyright (C) 2011 Werner Dittmann for the SRTCP support
4
Alexandre Lision51140e12013-12-02 10:54:09 -05005 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
Alexandre Lisione24852d2014-02-04 13:13:02 -050020
21
Alexandre Lision51140e12013-12-02 10:54:09 -050022#ifndef CRYPTOCONTEXTCTRL_H
23#define CRYPTOCONTEXTCTRL_H
24
25/**
Alexandre Lisione24852d2014-02-04 13:13:02 -050026 * @file CryptoContext.h
27 * @brief The C++ SRTP implementation
Alexandre Lision51140e12013-12-02 10:54:09 -050028 * @ingroup Z_SRTP
29 * @{
30 */
31
Alexandre Lisione24852d2014-02-04 13:13:02 -050032#include <crypto/SrtpSymCrypto.h>
33
Alexandre Lision51140e12013-12-02 10:54:09 -050034class SrtpSymCrypto;
35
Alexandre Lisione24852d2014-02-04 13:13:02 -050036 /**
37 * The implementation for a SRTCP cryptographic context.
38 *
39 * This class holds data and provides functions that implement a
40 * cryptographic context for SRTP, Refer to RFC 3711, chapter 3.2 for some
41 * more detailed information about the SRTP cryptographic context.
42 *
43 * Each SRTP cryptographic context maintains a RTP source identified by
44 * its SSRC. Thus you can independently protect each source inside a RTP
45 * session.
46 *
47 * Key management mechanisms negotiate the parameters for the SRTP
48 * cryptographic context, such as master key, key length, authentication
49 * length and so on. The key management mechanisms are not part of
50 * SRTP. Refer to MIKEY (RFC 3880) or to Phil Zimmermann's ZRTP protocol
51 * (draft-zimmermann-avt-zrtp-01). After key management negotiated the
52 * data the application can setup the SRTCP cryptographic context and
53 * enable SRTCP processing.
54 *
55 *
56 * @author Israel Abad <i_abad@terra.es>
57 * @author Erik Eliasson <eliasson@it.kth.se>
58 * @author Johan Bilien <jobi@via.ecp.fr>
59 * @author Joachim Orrblad <joachim@orrblad.com>
60 * @author Werner Dittmann <Werner.Dittmann@t-online.de>
61 */
62
Alexandre Lision51140e12013-12-02 10:54:09 -050063class CryptoContextCtrl {
64 public:
65 /**
Alexandre Lisione24852d2014-02-04 13:13:02 -050066 * Constructor for an active SRTP cryptographic context.
Alexandre Lision51140e12013-12-02 10:54:09 -050067 *
Alexandre Lisione24852d2014-02-04 13:13:02 -050068 * This constructor creates an active SRTP cryptographic context were
69 * algorithms are enabled, keys are computed and so on. This SRTP
70 * cryptographic context can protect a RTP SSRC stream.
Alexandre Lision51140e12013-12-02 10:54:09 -050071 *
72 * @param ssrc
Alexandre Lisione24852d2014-02-04 13:13:02 -050073 * The RTP SSRC that this SRTP cryptographic context protects.
Alexandre Lision51140e12013-12-02 10:54:09 -050074 *
75 * @param ealg
76 * The encryption algorithm to use. Possible values are <code>
Alexandre Lisione24852d2014-02-04 13:13:02 -050077 * SrtpEncryptionNull, SrtpEncryptionAESCM, SrtpEncryptionAESF8
Alexandre Lision51140e12013-12-02 10:54:09 -050078 * </code>. See chapter 4.1.1 for AESCM (Counter mode) and 4.1.2
79 * for AES F8 mode.
80 *
81 * @param aalg
82 * The authentication algorithm to use. Possible values are <code>
Alexandre Lisione24852d2014-02-04 13:13:02 -050083 * SrtpEncryptionNull, SrtpAuthenticationSha1Hmac</code>. The only
84 * active algorithm here is SHA1 HMAC, a SHA1 based hashed message
85 * authentication code as defined in RFC 2104.
Alexandre Lision51140e12013-12-02 10:54:09 -050086 *
87 * @param masterKey
Alexandre Lisione24852d2014-02-04 13:13:02 -050088 * Pointer to the master key for this SRTP cryptographic context.
Alexandre Lision51140e12013-12-02 10:54:09 -050089 * Must point to <code>masterKeyLength</code> bytes. Refer to chapter
90 * 3.2.1 of the RFC about the role of the master key.
91 *
92 * @param masterKeyLength
93 * The length in bytes of the master key in bytes. The length must
Alexandre Lisione24852d2014-02-04 13:13:02 -050094 * match the selected encryption algorithm. Because SRTP uses AES
Alexandre Lision51140e12013-12-02 10:54:09 -050095 * based encryption only, then master key length may be 16 or 32
96 * bytes (128 or 256 bit master key)
97 *
98 * @param masterSalt
Alexandre Lisione24852d2014-02-04 13:13:02 -050099 * SRTP uses the master salt to computer the initialization vector
Alexandre Lision51140e12013-12-02 10:54:09 -0500100 * that in turn is input to compute the session key, session
101 * authentication key and the session salt.
102 *
103 * @param masterSaltLength
Alexandre Lisione24852d2014-02-04 13:13:02 -0500104 * The length in bytes of the master salt data in bytes. SRTP uses
Alexandre Lision51140e12013-12-02 10:54:09 -0500105 * AES as encryption algorithm. AES encrypts 16 byte blocks
106 * (independent of the key length). According to RFC3711 the standard
107 * value for the master salt length should be 112 bit (14 bytes).
108 *
109 * @param ekeyl
Alexandre Lisione24852d2014-02-04 13:13:02 -0500110 * The length in bytes of the session encryption key that SRTP shall
Alexandre Lision51140e12013-12-02 10:54:09 -0500111 * compute and use. Usually the same length as for the master key
112 * length. But you may use a different length as well. Be carefull
113 * that the key management mechanisms supports different key lengths.
114 *
115 * @param akeyl
Alexandre Lisione24852d2014-02-04 13:13:02 -0500116 * The length in bytes of the session authentication key. SRTP
Alexandre Lision51140e12013-12-02 10:54:09 -0500117 * computes this key and uses it as input to the authentication
118 * algorithm.
119 * The standard value is 160 bits (20 bytes).
120 *
121 * @param skeyl
Alexandre Lisione24852d2014-02-04 13:13:02 -0500122 * The length in bytes of the session salt. SRTP computes this salt
Alexandre Lision51140e12013-12-02 10:54:09 -0500123 * key and uses it as input during encryption. The length usually
124 * is the same as the master salt length.
125 *
126 * @param tagLength
Alexandre Lisione24852d2014-02-04 13:13:02 -0500127 * The length is bytes of the authentication tag that SRTP appends
Alexandre Lision51140e12013-12-02 10:54:09 -0500128 * to the RTP packet. Refer to chapter 4.2. in the RFC 3711.
129 */
Alexandre Lisione24852d2014-02-04 13:13:02 -0500130 CryptoContextCtrl( uint32_t ssrc,
Alexandre Lision51140e12013-12-02 10:54:09 -0500131 const int32_t ealg,
132 const int32_t aalg,
133 uint8_t* masterKey,
134 int32_t masterKeyLength,
135 uint8_t* masterSalt,
136 int32_t masterSaltLength,
137 int32_t ekeyl,
138 int32_t akeyl,
139 int32_t skeyl,
Alexandre Lisione24852d2014-02-04 13:13:02 -0500140 int32_t tagLength );
Alexandre Lision51140e12013-12-02 10:54:09 -0500141 /**
Alexandre Lisione24852d2014-02-04 13:13:02 -0500142 * Destructor.
Alexandre Lision51140e12013-12-02 10:54:09 -0500143 *
Alexandre Lisione24852d2014-02-04 13:13:02 -0500144 * Cleans the SRTP cryptographic context.
Alexandre Lision51140e12013-12-02 10:54:09 -0500145 */
146 ~CryptoContextCtrl();
147
148 /**
Alexandre Lisione24852d2014-02-04 13:13:02 -0500149 * Perform SRTP encryption.
Alexandre Lision51140e12013-12-02 10:54:09 -0500150 *
Alexandre Lisione24852d2014-02-04 13:13:02 -0500151 * This method encrypts <em>and</em> decrypts SRTP payload data. Plain
Alexandre Lision51140e12013-12-02 10:54:09 -0500152 * data gets encrypted, encrypted data get decrypted.
153 *
154 * @param rtp
155 * The RTP packet that contains the data to encrypt.
156 *
157 * @param index
Alexandre Lisione24852d2014-02-04 13:13:02 -0500158 * The 48 bit SRTP packet index. See the <code>guessIndex</code>
159 * method.
Alexandre Lision51140e12013-12-02 10:54:09 -0500160 *
161 * @param ssrc
Alexandre Lisione24852d2014-02-04 13:13:02 -0500162 * The RTP SSRC data in <em>host</em> order.
Alexandre Lision51140e12013-12-02 10:54:09 -0500163 */
Alexandre Lisione24852d2014-02-04 13:13:02 -0500164 void srtcpEncrypt( uint8_t* rtp, int32_t len, uint64_t index, uint32_t ssrc );
Alexandre Lision51140e12013-12-02 10:54:09 -0500165
166 /**
Alexandre Lisione24852d2014-02-04 13:13:02 -0500167 * Compute the authentication tag.
Alexandre Lision51140e12013-12-02 10:54:09 -0500168 *
169 * Compute the authentication tag according the the paramters in the
Alexandre Lisione24852d2014-02-04 13:13:02 -0500170 * SRTP Cryptograhic context.
Alexandre Lision51140e12013-12-02 10:54:09 -0500171 *
172 * @param rtp
Alexandre Lisione24852d2014-02-04 13:13:02 -0500173 * The RTP packet that contains the data to authenticate.
Alexandre Lision51140e12013-12-02 10:54:09 -0500174 *
Alexandre Lisione24852d2014-02-04 13:13:02 -0500175 * @param roc
176 * The 32 bit SRTP roll-over-counter.
Alexandre Lision51140e12013-12-02 10:54:09 -0500177 *
178 * @param tag
179 * Points to a buffer that hold the computed tag. This buffer must
180 * be able to hold <code>tagLength</code> bytes.
181 */
Alexandre Lisione24852d2014-02-04 13:13:02 -0500182 void srtcpAuthenticate(uint8_t* rtp, int32_t len, uint32_t roc, uint8_t* tag );
Alexandre Lision51140e12013-12-02 10:54:09 -0500183
184 /**
Alexandre Lisione24852d2014-02-04 13:13:02 -0500185 * Perform key derivation according to SRTP specification
Alexandre Lision51140e12013-12-02 10:54:09 -0500186 *
187 * This method computes the session key, session authentication key and the
188 * session salt key. This method must be called at least once after the
Alexandre Lisione24852d2014-02-04 13:13:02 -0500189 * SRTP Cryptograhic context was set up.
Alexandre Lision51140e12013-12-02 10:54:09 -0500190 *
Alexandre Lisione24852d2014-02-04 13:13:02 -0500191 * @param index
192 * The 48 bit SRTP packet index. See the <code>guessIndex</code>
193 * method.
Alexandre Lision51140e12013-12-02 10:54:09 -0500194 */
195 void deriveSrtcpKeys();
196
197 /**
Alexandre Lisione24852d2014-02-04 13:13:02 -0500198 * Check for packet replay.
Alexandre Lision51140e12013-12-02 10:54:09 -0500199 *
200 * The method check if a received packet is either to old or was already
201 * received.
202 *
203 * The method supports a 64 packet history relative the the given
204 * sequence number.
205 *
206 * @param newSeqNumber
207 * The sequence number of the received RTCP packet in host order.
208 *
209 * @return <code>true</code> if no replay, <code>false</code> if packet
210 * is too old ar was already received.
211 */
212 bool checkReplay(uint32_t newSeqNumber);
213
214 /**
Alexandre Lisione24852d2014-02-04 13:13:02 -0500215 * Update the SRTP packet index.
Alexandre Lision51140e12013-12-02 10:54:09 -0500216 *
217 * Call this method after all checks were successful. See chapter
218 * 3.3.1 in the RFC when to update the ROC and ROC processing.
219 *
220 * @param newSeqNumber
221 * The sequence number of the received RTCP packet in host order.
222 */
Alexandre Lisione24852d2014-02-04 13:13:02 -0500223 void update( uint32_t newSeqNumber );
Alexandre Lision51140e12013-12-02 10:54:09 -0500224
225 /**
Alexandre Lisione24852d2014-02-04 13:13:02 -0500226 * Get the length of the SRTP authentication tag in bytes.
Alexandre Lision51140e12013-12-02 10:54:09 -0500227 *
228 * @return the length of the authentication tag.
229 */
Alexandre Lisione24852d2014-02-04 13:13:02 -0500230 inline int32_t
231 getTagLength() const
232 {return tagLength;}
233
Alexandre Lision51140e12013-12-02 10:54:09 -0500234
235 /**
Alexandre Lisione24852d2014-02-04 13:13:02 -0500236 * Get the length of the MKI in bytes.
Alexandre Lision51140e12013-12-02 10:54:09 -0500237 *
238 * @return the length of the MKI.
239 */
Alexandre Lisione24852d2014-02-04 13:13:02 -0500240 inline int32_t
241 getMkiLength() const
242 {return mkiLength;}
Alexandre Lision51140e12013-12-02 10:54:09 -0500243
244 /**
Alexandre Lisione24852d2014-02-04 13:13:02 -0500245 * Get the SSRC of this SRTP Cryptograhic context.
Alexandre Lision51140e12013-12-02 10:54:09 -0500246 *
247 * @return the SSRC.
248 */
Alexandre Lisione24852d2014-02-04 13:13:02 -0500249 inline uint32_t
250 getSsrc() const
251 {return ssrcCtx;}
Alexandre Lision51140e12013-12-02 10:54:09 -0500252
253 /**
Alexandre Lisione24852d2014-02-04 13:13:02 -0500254 * Derive a new Crypto Context for use with a new SSRC
Alexandre Lision51140e12013-12-02 10:54:09 -0500255 *
Alexandre Lisione24852d2014-02-04 13:13:02 -0500256 * This method returns a new Crypto Context initialized with the data
257 * of this crypto context. Replacing the SSRC, Roll-over-Counter, and
258 * the key derivation rate the application cab use this Crypto Context
259 * to encrypt / decrypt a new stream (Synchronization source) inside
260 * one RTP session.
Alexandre Lision51140e12013-12-02 10:54:09 -0500261 *
Alexandre Lisione24852d2014-02-04 13:13:02 -0500262 * Before the application can use this crypto context it must call
263 * the <code>deriveSrtpKeys</code> method.
Alexandre Lision51140e12013-12-02 10:54:09 -0500264 *
265 * @param ssrc
266 * The SSRC for this context
Alexandre Lisione24852d2014-02-04 13:13:02 -0500267 * @param roc
268 * The Roll-Over-Counter for this context
269 * @param keyDerivRate
270 * The key derivation rate for this context
Alexandre Lision51140e12013-12-02 10:54:09 -0500271 * @return
Alexandre Lisione24852d2014-02-04 13:13:02 -0500272 * a new CryptoContext with all relevant data set.
Alexandre Lision51140e12013-12-02 10:54:09 -0500273 */
274 CryptoContextCtrl* newCryptoContextForSSRC(uint32_t ssrc);
275
276 private:
277
278 uint32_t ssrcCtx;
279 bool using_mki;
280 uint32_t mkiLength;
281 uint8_t* mki;
282
283 uint32_t s_l;
284
285 /* bitmask for replay check */
286 uint64_t replay_window;
287
288 uint8_t* master_key;
289 uint32_t master_key_length;
290 uint8_t* master_salt;
291 uint32_t master_salt_length;
292
293 /* Session Encryption, Authentication keys, Salt */
294 int32_t n_e;
295 uint8_t* k_e;
296 int32_t n_a;
297 uint8_t* k_a;
298 int32_t n_s;
299 uint8_t* k_s;
300
301 int32_t ealg;
302 int32_t aalg;
303 int32_t ekeyl;
304 int32_t akeyl;
305 int32_t skeyl;
306 int32_t tagLength;
307
308 void* macCtx;
309
310 SrtpSymCrypto* cipher;
311 SrtpSymCrypto* f8Cipher;
312 };
313
314/**
315 * @}
316 */
317
318#endif
319
Alexandre Lisione24852d2014-02-04 13:13:02 -0500320/** EMACS **
321 * Local variables:
322 * mode: c++
323 * c-default-style: ellemtel
324 * c-basic-offset: 4
325 * End:
326 */
327