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 <commoncpp/config.h>
25 
26 #include <ccrtp/rtppkt.h>
27 
28 
29 #define REPLAY_WINDOW_SIZE 64
30 
31 
32 // const int SrtpAuthenticationNull = 0;
33 // const int SrtpAuthenticationSha1Hmac = 1;
34 // const int SrtpAuthenticationSkeinHmac = 2;
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 NAMESPACE_COMMONCPP
47 
48  class RTPPacket;
49 
78  class __EXPORT CryptoContext {
79  public:
89  CryptoContext( uint32 ssrc );
90 
165  CryptoContext( uint32 ssrc, int32 roc,
166  int64 keyDerivRate,
167  const int32 ealg,
168  const int32 aalg,
169  uint8* masterKey,
170  int32 masterKeyLength,
171  uint8* masterSalt,
172  int32 masterSaltLength,
173  int32 ekeyl,
174  int32 akeyl,
175  int32 skeyl,
176  int32 tagLength );
182  ~CryptoContext();
183 
193  inline void
194  setRoc(uint32 r)
195  {roc = r;}
196 
205  inline uint32
206  getRoc() const
207  {return roc;}
208 
225  void srtpEncrypt( RTPPacket* rtp, uint64 index, uint32 ssrc );
226 
243  void srtpAuthenticate(RTPPacket* rtp, uint32 roc, uint8* tag );
244 
256  void deriveSrtpKeys(uint64 index);
257 
270  uint64 guessIndex(uint16 newSeqNumber);
271 
287  bool checkReplay(uint16 newSeqNumber);
288 
298  void update( uint16 newSeqNumber );
299 
305  inline int32
306  getTagLength() const
307  {return tagLength;}
308 
309 
315  inline int32
316  getMkiLength() const
317  {return mkiLength;}
318 
324  inline uint32
325  getSsrc() const
326  {return ssrc;}
327 
350  CryptoContext* newCryptoContextForSSRC(uint32 ssrc, int roc, int64 keyDerivRate);
351 
352  private:
353 
354  uint32 ssrc;
355  bool using_mki;
356  uint32 mkiLength;
357  uint8* mki;
358 
359  uint32 roc;
360  uint32 guessed_roc;
361  uint16 s_l;
362  int64 key_deriv_rate;
363 
364  /* bitmask for replay check */
365  uint64 replay_window;
366 
367  uint8* master_key;
368  uint32 master_key_length;
369  uint32 master_key_srtp_use_nb;
370  uint32 master_key_srtcp_use_nb;
371  uint8* master_salt;
372  uint32 master_salt_length;
373 
374  /* Session Encryption, Authentication keys, Salt */
375  int32 n_e;
376  uint8* k_e;
377  int32 n_a;
378  uint8* k_a;
379  int32 n_s;
380  uint8* k_s;
381 
382  int32 ealg;
383  int32 aalg;
384  int32 ekeyl;
385  int32 akeyl;
386  int32 skeyl;
387  int32 tagLength;
388  bool seqNumSet;
389 
390  void* macCtx;
391 
392 #ifdef SRTP_SUPPORT
393  AesSrtp* cipher;
394  AesSrtp* f8Cipher;
395 #else
396  void* cipher;
397  void* f8Cipher;
398 #endif
399 
400  };
401 
402 END_NAMESPACE
403 
404 #endif
405