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 const int SrtpAuthenticationNull     =  0;
00032 const int SrtpAuthenticationSha1Hmac =  1;
00033 const int SrtpAuthenticationSkeinHmac = 2;
00034 
00035 const int SrtpEncryptionNull  = 0;
00036 const int SrtpEncryptionAESCM = 1;
00037 const int SrtpEncryptionAESF8 = 2;
00038 const int SrtpEncryptionTWOCM = 3;
00039 const int SrtpEncryptionTWOF8 = 4;
00040 
00041 #ifndef CRYPTOCONTEXTCTRL_H
00042 
00043 #include <stdint.h>
00044 
00045 #ifdef SRTP_SUPPORT
00046 #include <ccrtp/crypto/SrtpSymCrypto.h>
00047 #endif
00048 
00049 class SrtpSymCrypto;
00050 
00051 NAMESPACE_COMMONCPP
00052 
00053     class RTPPacket;
00054 
00083     class __EXPORT CryptoContext {
00084     public:
00094         CryptoContext( uint32 ssrc );
00095 
00170         CryptoContext( uint32 ssrc, int32 roc,
00171                int64  keyDerivRate,
00172                const  int32 ealg,
00173                const  int32 aalg,
00174                uint8* masterKey,
00175                int32  masterKeyLength,
00176                uint8* masterSalt,
00177                int32  masterSaltLength,
00178                int32  ekeyl,
00179                int32  akeyl,
00180                int32  skeyl,
00181                int32  tagLength );
00187         ~CryptoContext();
00188 
00198         inline void
00199         setRoc(uint32 r)
00200         {roc = r;}
00201 
00210         inline uint32
00211         getRoc() const
00212         {return roc;}
00213 
00230         void srtpEncrypt( RTPPacket* rtp, uint64 index, uint32 ssrc );
00231 
00248         void srtpAuthenticate(RTPPacket* rtp, uint32 roc, uint8* tag );
00249 
00261         void deriveSrtpKeys(uint64 index);
00262 
00275         uint64 guessIndex(uint16 newSeqNumber);
00276 
00292         bool checkReplay(uint16 newSeqNumber);
00293 
00303         void update( uint16 newSeqNumber );
00304 
00310         inline int32
00311         getTagLength() const
00312         {return tagLength;}
00313 
00314 
00320         inline int32
00321         getMkiLength() const
00322         {return mkiLength;}
00323 
00329         inline uint32
00330         getSsrc() const
00331         {return ssrcCtx;}
00332 
00354      CryptoContext* newCryptoContextForSSRC(uint32 ssrc, int roc, int64 keyDerivRate);
00355 
00356     private:
00357 
00358         uint32 ssrcCtx;
00359         bool   using_mki;
00360         uint32 mkiLength;
00361         uint8* mki;
00362 
00363         uint32 roc;
00364         uint32 guessed_roc;
00365         uint16 s_l;
00366         int64  key_deriv_rate;
00367 
00368         /* bitmask for replay check */
00369         uint64 replay_window;
00370 
00371         uint8* master_key;
00372         uint32 master_key_length;
00373         uint32 master_key_srtp_use_nb;
00374         uint32 master_key_srtcp_use_nb;
00375         uint8* master_salt;
00376         uint32 master_salt_length;
00377 
00378         /* Session Encryption, Authentication keys, Salt */
00379         int32  n_e;
00380         uint8* k_e;
00381         int32  n_a;
00382         uint8* k_a;
00383         int32  n_s;
00384         uint8* k_s;
00385 
00386         int32 ealg;
00387         int32 aalg;
00388         int32 ekeyl;
00389         int32 akeyl;
00390         int32 skeyl;
00391         int32 tagLength;
00392         bool  seqNumSet;
00393 
00394         void*   macCtx;
00395 
00396 #ifdef SRTP_SUPPORT
00397         SrtpSymCrypto* cipher;
00398         SrtpSymCrypto* f8Cipher;
00399 #else
00400         void* cipher;
00401         void* f8Cipher;
00402 #endif
00403 
00404     };
00405 
00406 END_NAMESPACE
00407 
00408 #endif
00409 
00410 #endif
00411