ccRTP
CryptoContext.h
Go to the documentation of this file.
00001 /*
00002   Copyright (C) 2004-2006 the Minisip Team
00003 
00004   This library is free software; you can redistribute it and/or
00005   modify it under the terms of the GNU Lesser General Public
00006   License as published by the Free Software Foundation; either
00007   version 2.1 of the License, or (at your option) any later version.
00008 
00009   This library is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012   Lesser General Public License for more details.
00013 
00014   You should have received a copy of the GNU Lesser General Public
00015   License along with this library; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 */
00018 
00019 
00020 
00021 #ifndef CRYPTOCONTEXT_H
00022 #define CRYPTOCONTEXT_H
00023 
00024 #include <commoncpp/config.h>
00025 
00026 #include <ccrtp/rtppkt.h>
00027 
00028 
00029 #define REPLAY_WINDOW_SIZE 64
00030 
00031 
00032 // const int SrtpAuthenticationNull     =  0;
00033 // const int SrtpAuthenticationSha1Hmac =  1;
00034 // const int SrtpAuthenticationSkeinHmac = 2;
00035 // 
00036 // const int SrtpEncryptionNull  = 0;
00037 // const int SrtpEncryptionAESCM = 1;
00038 // const int SrtpEncryptionAESF8 = 2;
00039 // const int SrtpEncryptionTWOCM = 3;
00040 // const int SrtpEncryptionTWOF8 = 4;
00041 
00042 #ifdef SRTP_SUPPORT
00043 #include <ccrtp/crypto/AesSrtp.h>
00044 #endif
00045 
00046 NAMESPACE_COMMONCPP
00047 
00048     class RTPPacket;
00049 
00078     class __EXPORT CryptoContext {
00079     public:
00089         CryptoContext( uint32 ssrc );
00090 
00165         CryptoContext( uint32 ssrc, int32 roc,
00166                int64  keyDerivRate,
00167                const  int32 ealg,
00168                const  int32 aalg,
00169                uint8* masterKey,
00170                int32  masterKeyLength,
00171                uint8* masterSalt,
00172                int32  masterSaltLength,
00173                int32  ekeyl,
00174                int32  akeyl,
00175                int32  skeyl,
00176                int32  tagLength );
00182         ~CryptoContext();
00183 
00193         inline void
00194         setRoc(uint32 r)
00195         {roc = r;}
00196 
00205         inline uint32
00206         getRoc() const
00207         {return roc;}
00208 
00225         void srtpEncrypt( RTPPacket* rtp, uint64 index, uint32 ssrc );
00226 
00243         void srtpAuthenticate(RTPPacket* rtp, uint32 roc, uint8* tag );
00244 
00256         void deriveSrtpKeys(uint64 index);
00257 
00270         uint64 guessIndex(uint16 newSeqNumber);
00271 
00287         bool checkReplay(uint16 newSeqNumber);
00288 
00298         void update( uint16 newSeqNumber );
00299 
00305         inline int32
00306         getTagLength() const
00307         {return tagLength;}
00308 
00309 
00315         inline int32
00316         getMkiLength() const
00317         {return mkiLength;}
00318 
00324         inline uint32
00325         getSsrc() const
00326         {return ssrc;}
00327 
00350             CryptoContext* newCryptoContextForSSRC(uint32 ssrc, int roc, int64 keyDerivRate);
00351 
00352     private:
00353 
00354         uint32 ssrc;
00355         bool   using_mki;
00356         uint32 mkiLength;
00357         uint8* mki;
00358 
00359         uint32 roc;
00360         uint32 guessed_roc;
00361         uint16 s_l;
00362         int64  key_deriv_rate;
00363 
00364         /* bitmask for replay check */
00365         uint64 replay_window;
00366 
00367         uint8* master_key;
00368         uint32 master_key_length;
00369         uint32 master_key_srtp_use_nb;
00370         uint32 master_key_srtcp_use_nb;
00371         uint8* master_salt;
00372         uint32 master_salt_length;
00373 
00374         /* Session Encryption, Authentication keys, Salt */
00375         int32  n_e;
00376         uint8* k_e;
00377         int32  n_a;
00378         uint8* k_a;
00379         int32  n_s;
00380         uint8* k_s;
00381 
00382         int32 ealg;
00383         int32 aalg;
00384         int32 ekeyl;
00385         int32 akeyl;
00386         int32 skeyl;
00387         int32 tagLength;
00388         bool  seqNumSet;
00389 
00390         void*   macCtx;
00391 
00392 #ifdef SRTP_SUPPORT
00393         AesSrtp* cipher;
00394         AesSrtp* f8Cipher;
00395 #else
00396         void* cipher;
00397         void* f8Cipher;
00398 #endif
00399 
00400     };
00401 
00402 END_NAMESPACE
00403 
00404 #endif
00405