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 <cc++/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 #ifdef CCXX_NAMESPACES
00047 namespace ost {
00048 #endif
00049 
00050     class RTPPacket;
00051 
00080     class __EXPORT CryptoContext {
00081         public:
00091             CryptoContext( uint32 ssrc );
00092 
00167             CryptoContext( uint32 ssrc, int32 roc,
00168                            int64  keyDerivRate,
00169                            const  int32 ealg,
00170                            const  int32 aalg,
00171                            uint8* masterKey,
00172                            int32  masterKeyLength,
00173                            uint8* masterSalt,
00174                            int32  masterSaltLength,
00175                            int32  ekeyl,
00176                            int32  akeyl,
00177                            int32  skeyl,
00178                            int32  tagLength );
00184             ~CryptoContext();
00185 
00195             inline void
00196             setRoc(uint32 r)
00197             {roc = r;}
00198 
00207             inline uint32
00208             getRoc() const
00209             {return roc;}
00210 
00227             void srtpEncrypt( RTPPacket* rtp, uint64 index, uint32 ssrc );
00228 
00245             void srtpAuthenticate(RTPPacket* rtp, uint32 roc, uint8* tag );
00246 
00258             void deriveSrtpKeys(uint64 index);
00259 
00272             uint64 guessIndex(uint16 newSeqNumber);
00273 
00289             bool checkReplay(uint16 newSeqNumber);
00290 
00300             void update( uint16 newSeqNumber );
00301 
00307             inline int32
00308             getTagLength() const
00309             {return tagLength;}
00310 
00311 
00317             inline int32
00318             getMkiLength() const
00319             {return mkiLength;}
00320 
00326             inline uint32
00327             getSsrc() const
00328             {return ssrc;}
00329 
00352             CryptoContext* newCryptoContextForSSRC(uint32 ssrc, int roc, int64 keyDerivRate);
00353 
00354         private:
00355 
00356             uint32 ssrc;
00357             bool   using_mki;
00358             uint32 mkiLength;
00359             uint8* mki;
00360 
00361             uint32 roc;
00362             uint32 guessed_roc;
00363             uint16 s_l;
00364             int64  key_deriv_rate;
00365 
00366             /* bitmask for replay check */
00367             uint64 replay_window;
00368 
00369             uint8* master_key;
00370             uint32 master_key_length;
00371             uint32 master_key_srtp_use_nb;
00372             uint32 master_key_srtcp_use_nb;
00373             uint8* master_salt;
00374             uint32 master_salt_length;
00375 
00376             /* Session Encryption, Authentication keys, Salt */
00377             int32  n_e;
00378             uint8* k_e;
00379             int32  n_a;
00380             uint8* k_a;
00381             int32  n_s;
00382             uint8* k_s;
00383 
00384             int32 ealg;
00385             int32 aalg;
00386             int32 ekeyl;
00387             int32 akeyl;
00388             int32 skeyl;
00389             int32 tagLength;
00390             bool  seqNumSet;
00391 
00392         void*   macCtx;
00393 
00394 #ifdef SRTP_SUPPORT
00395             AesSrtp* cipher;
00396             AesSrtp* f8Cipher;
00397 #else
00398             void* cipher;
00399             void* f8Cipher;
00400 #endif
00401 
00402     };
00403 #ifdef  CCXX_NAMESPACES
00404 }
00405 #endif
00406 
00407 #endif
00408