blob: 3367e81769f8046ffef67ac561354fa81e5e650c [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001/*
2 Copyright (C) 2004-2006 the Minisip Team
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19
20
21#ifndef CRYPTOCONTEXT_H
22#define CRYPTOCONTEXT_H
23
Alexandre Lisionddd731e2014-01-31 11:50:08 -050024#include <commoncpp/config.h>
Emeric Vigier2f625822012-08-06 11:09:52 -040025
26#include <ccrtp/rtppkt.h>
27
28
29#define REPLAY_WINDOW_SIZE 64
30
Emeric Vigier2f625822012-08-06 11:09:52 -040031const int SrtpAuthenticationNull = 0;
32const int SrtpAuthenticationSha1Hmac = 1;
33const int SrtpAuthenticationSkeinHmac = 2;
34
35const int SrtpEncryptionNull = 0;
36const int SrtpEncryptionAESCM = 1;
37const int SrtpEncryptionAESF8 = 2;
38const int SrtpEncryptionTWOCM = 3;
39const int SrtpEncryptionTWOF8 = 4;
40
Alexandre Lisionddd731e2014-01-31 11:50:08 -050041#ifndef CRYPTOCONTEXTCTRL_H
42
43#include <stdint.h>
44
Emeric Vigier2f625822012-08-06 11:09:52 -040045#ifdef SRTP_SUPPORT
Alexandre Lisionddd731e2014-01-31 11:50:08 -050046#include <ccrtp/crypto/SrtpSymCrypto.h>
Emeric Vigier2f625822012-08-06 11:09:52 -040047#endif
48
Alexandre Lisionddd731e2014-01-31 11:50:08 -050049class SrtpSymCrypto;
50
51NAMESPACE_COMMONCPP
Emeric Vigier2f625822012-08-06 11:09:52 -040052
53 class RTPPacket;
54
55 /**
56 * The implementation for a SRTP cryptographic context.
57 *
58 * This class holds data and provides functions that implement a
59 * cryptographic context for SRTP, Refer to RFC 3711, chapter 3.2 for some
60 * more detailed information about the SRTP cryptographic context.
61 *
62 * Each SRTP cryptographic context maintains a RTP source identified by
63 * its SSRC. Thus you can independently protect each source inside a RTP
64 * session.
65 *
66 * Key management mechanisms negotiate the parameters for the SRTP
67 * cryptographic context, such as master key, key length, authentication
68 * length and so on. The key management mechanisms are not part of
69 * SRTP. Refer to MIKEY (RFC 3880) or to Phil Zimmermann's ZRTP protocol
70 * (draft-zimmermann-avt-zrtp-01). After key management negotiated the
71 * data the application can setup the SRTP cryptographic context and
72 * enable SRTP processing.
73 *
74 * Currently this implementation supports RTP only, not RTCP.
75 *
76 * @author Israel Abad <i_abad@terra.es>
77 * @author Erik Eliasson <eliasson@it.kth.se>
78 * @author Johan Bilien <jobi@via.ecp.fr>
79 * @author Joachim Orrblad <joachim@orrblad.com>
80 * @author Werner Dittmann <Werner.Dittmann@t-online.de>
81 */
82
83 class __EXPORT CryptoContext {
Alexandre Lisionddd731e2014-01-31 11:50:08 -050084 public:
85 /**
86 * Constructor for empty SRTP cryptographic context.
87 *
88 * This constructor creates an empty SRTP cryptographic context were
89 * all algorithms are set to the null algorithm, that is no SRTP processing
90 * is performed.
91 *
92 * @param ssrc The RTP SSRC that this SRTP cryptographic context protects.
93 */
94 CryptoContext( uint32 ssrc );
Emeric Vigier2f625822012-08-06 11:09:52 -040095
Alexandre Lisionddd731e2014-01-31 11:50:08 -050096 /**
97 * Constructor for an active SRTP cryptographic context.
98 *
99 * This constructor creates an active SRTP cryptographic context were
100 * algorithms are enabled, keys are computed and so on. This SRTP
101 * cryptographic context can protect a RTP SSRC stream.
102 *
103 * @param ssrc
104 * The RTP SSRC that this SRTP cryptographic context protects.
105 *
106 * @param roc
107 * The initial Roll-Over-Counter according to RFC 3711. These are the
108 * upper 32 bit of the overall 48 bit SRTP packet index. Refer to
109 * chapter 3.2.1 of the RFC.
110 *
111 * @param keyDerivRate
112 * The key derivation rate defines when to recompute the SRTP session
113 * keys. Refer to chapter 4.3.1 in the RFC.
114 *
115 * @param ealg
116 * The encryption algorithm to use. Possible values are <code>
117 * SrtpEncryptionNull, SrtpEncryptionAESCM, SrtpEncryptionAESF8
118 * </code>. See chapter 4.1.1 for AESCM (Counter mode) and 4.1.2
119 * for AES F8 mode.
120 *
121 * @param aalg
122 * The authentication algorithm to use. Possible values are <code>
123 * SrtpEncryptionNull, SrtpAuthenticationSha1Hmac</code>. The only
124 * active algorithm here is SHA1 HMAC, a SHA1 based hashed message
125 * authentication code as defined in RFC 2104.
126 *
127 * @param masterKey
128 * Pointer to the master key for this SRTP cryptographic context.
129 * Must point to <code>masterKeyLength</code> bytes. Refer to chapter
130 * 3.2.1 of the RFC about the role of the master key.
131 *
132 * @param masterKeyLength
133 * The length in bytes of the master key in bytes. The length must
134 * match the selected encryption algorithm. Because SRTP uses AES
135 * based encryption only, then master key length may be 16 or 32
136 * bytes (128 or 256 bit master key)
137 *
138 * @param masterSalt
139 * SRTP uses the master salt to computer the initialization vector
140 * that in turn is input to compute the session key, session
141 * authentication key and the session salt.
142 *
143 * @param masterSaltLength
144 * The length in bytes of the master salt data in bytes. SRTP uses
145 * AES as encryption algorithm. AES encrypts 16 byte blocks
146 * (independent of the key length). According to RFC3711 the standard
147 * value for the master salt length should be 112 bit (14 bytes).
148 *
149 * @param ekeyl
150 * The length in bytes of the session encryption key that SRTP shall
151 * compute and use. Usually the same length as for the master key
152 * length. But you may use a different length as well. Be carefull
153 * that the key management mechanisms supports different key lengths.
154 *
155 * @param akeyl
156 * The length in bytes of the session authentication key. SRTP
157 * computes this key and uses it as input to the authentication
158 * algorithm.
159 * The standard value is 160 bits (20 bytes).
160 *
161 * @param skeyl
162 * The length in bytes of the session salt. SRTP computes this salt
163 * key and uses it as input during encryption. The length usually
164 * is the same as the master salt length.
165 *
166 * @param tagLength
167 * The length is bytes of the authentication tag that SRTP appends
168 * to the RTP packet. Refer to chapter 4.2. in the RFC 3711.
169 */
170 CryptoContext( uint32 ssrc, int32 roc,
171 int64 keyDerivRate,
172 const int32 ealg,
173 const int32 aalg,
174 uint8* masterKey,
175 int32 masterKeyLength,
176 uint8* masterSalt,
177 int32 masterSaltLength,
178 int32 ekeyl,
179 int32 akeyl,
180 int32 skeyl,
181 int32 tagLength );
182 /**
183 * Destructor.
184 *
185 * Cleans the SRTP cryptographic context.
186 */
187 ~CryptoContext();
Emeric Vigier2f625822012-08-06 11:09:52 -0400188
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500189 /**
190 * Set the Roll-Over-Counter.
191 *
192 * Ths method sets the upper 32 bit of the 48 bit SRTP packet index
193 * (the roll-over-part)
194 *
195 * @param r
196 * The roll-over-counter
197 */
198 inline void
199 setRoc(uint32 r)
200 {roc = r;}
Emeric Vigier2f625822012-08-06 11:09:52 -0400201
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500202 /**
203 * Get the Roll-Over-Counter.
204 *
205 * Ths method get the upper 32 bit of the 48 bit SRTP packet index
206 * (the roll-over-part)
207 *
208 * @return The roll-over-counter
209 */
210 inline uint32
211 getRoc() const
212 {return roc;}
Emeric Vigier2f625822012-08-06 11:09:52 -0400213
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500214 /**
215 * Perform SRTP encryption.
216 *
217 * This method encrypts <em>and</em> decrypts SRTP payload data. Plain
218 * data gets encrypted, encrypted data get decrypted.
219 *
220 * @param rtp
221 * The RTP packet that contains the data to encrypt.
222 *
223 * @param index
224 * The 48 bit SRTP packet index. See the <code>guessIndex</code>
225 * method.
226 *
227 * @param ssrc
228 * The RTP SSRC data in <em>host</em> order.
229 */
230 void srtpEncrypt( RTPPacket* rtp, uint64 index, uint32 ssrc );
Emeric Vigier2f625822012-08-06 11:09:52 -0400231
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500232 /**
233 * Compute the authentication tag.
234 *
235 * Compute the authentication tag according the the paramters in the
236 * SRTP Cryptograhic context.
237 *
238 * @param rtp
239 * The RTP packet that contains the data to authenticate.
240 *
241 * @param roc
242 * The 32 bit SRTP roll-over-counter.
243 *
244 * @param tag
245 * Points to a buffer that hold the computed tag. This buffer must
246 * be able to hold <code>tagLength</code> bytes.
247 */
248 void srtpAuthenticate(RTPPacket* rtp, uint32 roc, uint8* tag );
Emeric Vigier2f625822012-08-06 11:09:52 -0400249
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500250 /**
251 * Perform key derivation according to SRTP specification
252 *
253 * This method computes the session key, session authentication key and the
254 * session salt key. This method must be called at least once after the
255 * SRTP Cryptograhic context was set up.
256 *
257 * @param index
258 * The 48 bit SRTP packet index. See the <code>guessIndex</code>
259 * method.
260 */
261 void deriveSrtpKeys(uint64 index);
Emeric Vigier2f625822012-08-06 11:09:52 -0400262
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500263 /**
264 * Compute (guess) the new SRTP index based on the sequence number of
265 * a received RTP packet.
266 *
267 * The method uses the algorithm show in RFC3711, Appendix A, to compute
268 * the new index.
269 *
270 * @param newSeqNumber
271 * The sequence number of the received RTP packet in host order.
272 *
273 * @return The new SRTP packet index
274 */
275 uint64 guessIndex(uint16 newSeqNumber);
Emeric Vigier2f625822012-08-06 11:09:52 -0400276
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500277 /**
278 * Check for packet replay.
279 *
280 * The method check if a received packet is either to old or was already
281 * received.
282 *
283 * The method supports a 64 packet history relative the the given
284 * sequence number.
285 *
286 * @param newSeqNumber
287 * The sequence number of the received RTP packet in host order.
288 *
289 * @return <code>true</code> if no replay, <code>false</code> if packet
290 * is too old ar was already received.
291 */
292 bool checkReplay(uint16 newSeqNumber);
Emeric Vigier2f625822012-08-06 11:09:52 -0400293
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500294 /**
295 * Update the SRTP packet index.
296 *
297 * Call this method after all checks were successful. See chapter
298 * 3.3.1 in the RFC when to update the ROC and ROC processing.
299 *
300 * @param newSeqNumber
301 * The sequence number of the received RTP packet in host order.
302 */
303 void update( uint16 newSeqNumber );
Emeric Vigier2f625822012-08-06 11:09:52 -0400304
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500305 /**
306 * Get the length of the SRTP authentication tag in bytes.
307 *
308 * @return the length of the authentication tag.
309 */
310 inline int32
311 getTagLength() const
312 {return tagLength;}
Emeric Vigier2f625822012-08-06 11:09:52 -0400313
314
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500315 /**
316 * Get the length of the MKI in bytes.
317 *
318 * @return the length of the MKI.
319 */
320 inline int32
321 getMkiLength() const
322 {return mkiLength;}
Emeric Vigier2f625822012-08-06 11:09:52 -0400323
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500324 /**
325 * Get the SSRC of this SRTP Cryptograhic context.
326 *
327 * @return the SSRC.
328 */
329 inline uint32
330 getSsrc() const
331 {return ssrcCtx;}
Emeric Vigier2f625822012-08-06 11:09:52 -0400332
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500333 /**
334 * Derive a new Crypto Context for use with a new SSRC
335 *
336 * This method returns a new Crypto Context initialized with the data
337 * of this crypto context. Replacing the SSRC, Roll-over-Counter, and
338 * the key derivation rate the application cab use this Crypto Context
339 * to encrypt / decrypt a new stream (Synchronization source) inside
340 * one RTP session.
341 *
342 * Before the application can use this crypto context it must call
343 * the <code>deriveSrtpKeys</code> method.
344 *
345 * @param ssrc
346 * The SSRC for this context
347 * @param roc
348 * The Roll-Over-Counter for this context
349 * @param keyDerivRate
350 * The key derivation rate for this context
351 * @return
352 * a new CryptoContext with all relevant data set.
353 */
354 CryptoContext* newCryptoContextForSSRC(uint32 ssrc, int roc, int64 keyDerivRate);
Emeric Vigier2f625822012-08-06 11:09:52 -0400355
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500356 private:
Emeric Vigier2f625822012-08-06 11:09:52 -0400357
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500358 uint32 ssrcCtx;
359 bool using_mki;
360 uint32 mkiLength;
361 uint8* mki;
Emeric Vigier2f625822012-08-06 11:09:52 -0400362
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500363 uint32 roc;
364 uint32 guessed_roc;
365 uint16 s_l;
366 int64 key_deriv_rate;
Emeric Vigier2f625822012-08-06 11:09:52 -0400367
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500368 /* bitmask for replay check */
369 uint64 replay_window;
Emeric Vigier2f625822012-08-06 11:09:52 -0400370
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500371 uint8* master_key;
372 uint32 master_key_length;
373 uint32 master_key_srtp_use_nb;
374 uint32 master_key_srtcp_use_nb;
375 uint8* master_salt;
376 uint32 master_salt_length;
Emeric Vigier2f625822012-08-06 11:09:52 -0400377
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500378 /* Session Encryption, Authentication keys, Salt */
379 int32 n_e;
380 uint8* k_e;
381 int32 n_a;
382 uint8* k_a;
383 int32 n_s;
384 uint8* k_s;
Emeric Vigier2f625822012-08-06 11:09:52 -0400385
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500386 int32 ealg;
387 int32 aalg;
388 int32 ekeyl;
389 int32 akeyl;
390 int32 skeyl;
391 int32 tagLength;
392 bool seqNumSet;
Emeric Vigier2f625822012-08-06 11:09:52 -0400393
394 void* macCtx;
395
396#ifdef SRTP_SUPPORT
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500397 SrtpSymCrypto* cipher;
398 SrtpSymCrypto* f8Cipher;
Emeric Vigier2f625822012-08-06 11:09:52 -0400399#else
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500400 void* cipher;
401 void* f8Cipher;
Emeric Vigier2f625822012-08-06 11:09:52 -0400402#endif
403
404 };
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500405
406END_NAMESPACE
407
Emeric Vigier2f625822012-08-06 11:09:52 -0400408#endif
409
410#endif
411
412/** EMACS **
413 * Local variables:
414 * mode: c++
415 * c-default-style: ellemtel
416 * c-basic-offset: 4
417 * End:
418 */
419