ccRTP
CryptoContext.h
Go to the documentation of this file.
1 /*
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 
24 #include <cc++/config.h>
25 
26 #include <ccrtp/rtppkt.h>
27 
28 
29 #define REPLAY_WINDOW_SIZE 64
30 
31 
32 const int SrtpAuthenticationNull = 0;
35 
36 const int SrtpEncryptionNull = 0;
37 const int SrtpEncryptionAESCM = 1;
38 const int SrtpEncryptionAESF8 = 2;
39 const int SrtpEncryptionTWOCM = 3;
40 const int SrtpEncryptionTWOF8 = 4;
41 
42 #ifdef SRTP_SUPPORT
43 #include <ccrtp/crypto/AesSrtp.h>
44 #endif
45 
46 #ifdef CCXX_NAMESPACES
47 namespace ost {
48 #endif
49 
50  class RTPPacket;
51 
80  class __EXPORT CryptoContext {
81  public:
91  CryptoContext( uint32 ssrc );
92 
167  CryptoContext( uint32 ssrc, int32 roc,
168  int64 keyDerivRate,
169  const int32 ealg,
170  const int32 aalg,
171  uint8* masterKey,
172  int32 masterKeyLength,
173  uint8* masterSalt,
174  int32 masterSaltLength,
175  int32 ekeyl,
176  int32 akeyl,
177  int32 skeyl,
178  int32 tagLength );
184  ~CryptoContext();
185 
195  inline void
196  setRoc(uint32 r)
197  {roc = r;}
198 
207  inline uint32
208  getRoc() const
209  {return roc;}
210 
227  void srtpEncrypt( RTPPacket* rtp, uint64 index, uint32 ssrc );
228 
245  void srtpAuthenticate(RTPPacket* rtp, uint32 roc, uint8* tag );
246 
258  void deriveSrtpKeys(uint64 index);
259 
272  uint64 guessIndex(uint16 newSeqNumber);
273 
289  bool checkReplay(uint16 newSeqNumber);
290 
300  void update( uint16 newSeqNumber );
301 
307  inline int32
308  getTagLength() const
309  {return tagLength;}
310 
311 
317  inline int32
318  getMkiLength() const
319  {return mkiLength;}
320 
326  inline uint32
327  getSsrc() const
328  {return ssrc;}
329 
352  CryptoContext* newCryptoContextForSSRC(uint32 ssrc, int roc, int64 keyDerivRate);
353 
354  private:
355 
356  uint32 ssrc;
357  bool using_mki;
358  uint32 mkiLength;
359  uint8* mki;
360 
361  uint32 roc;
362  uint32 guessed_roc;
363  uint16 s_l;
364  int64 key_deriv_rate;
365 
366  /* bitmask for replay check */
367  uint64 replay_window;
368 
369  uint8* master_key;
370  uint32 master_key_length;
371  uint32 master_key_srtp_use_nb;
372  uint32 master_key_srtcp_use_nb;
373  uint8* master_salt;
374  uint32 master_salt_length;
375 
376  /* Session Encryption, Authentication keys, Salt */
377  int32 n_e;
378  uint8* k_e;
379  int32 n_a;
380  uint8* k_a;
381  int32 n_s;
382  uint8* k_s;
383 
384  int32 ealg;
385  int32 aalg;
386  int32 ekeyl;
387  int32 akeyl;
388  int32 skeyl;
389  int32 tagLength;
390  bool seqNumSet;
391 
392  void* macCtx;
393 
394 #ifdef SRTP_SUPPORT
395  AesSrtp* cipher;
396  AesSrtp* f8Cipher;
397 #else
398  void* cipher;
399  void* f8Cipher;
400 #endif
401 
402  };
403 #ifdef CCXX_NAMESPACES
404 }
405 #endif
406 
407 #endif
408