public RtpPacket protect ( RtpPacket packet ) { if ( txSessEncKey == null ) { log ( "protect() called out of session" ) ; return null ; } if ( packet == null ) { log ( "protect() called with null RTP packet" ) ; return null ; } int seqNum = packet . getSequenceNumber ( ) ; if ( seqNum == 0 ) { // wrapped round rollOverCounter += 0x10000 L ; } RtpPacket retPacket = null ; if ( ! transformPayload ( packet , rollOverCounter , seqNum , txSessSaltKey , true ) ) { log ( "protect() transformPayload error, encryption failed" ) ; return null ; } // Add authentication which is over whole rtp packet concatenated with // 48 bit ROC byte [ ] auth = null ; try { auth = getAuthentication ( packet , rollOverCounter , true ) ; // iTxSessAuthKey); if ( VERBOSE ) { log ( "protect() Adding HMAC:" ) ; logBuffer ( "auth:" , auth ) ; } } catch ( Throwable e ) { logError ( "protect() Authentication error EX: " + e ) ; e . printStackTrace ( ) ; return null ; } // aPacket should have getHmacAuthSizeBytes() bytes pre-allocated for the // auth-code // assert(aPacket.getPacket().length >= aPacket.getLength() + // getHmacAuthSizeBytes()); System . arraycopy ( auth , 0 , packet . getPacket ( ) , packet . getLength ( ) , getHmacAuthSizeBytes ( ) ) ; packet . setPayloadLength ( packet . getPayloadLength ( ) + getHmacAuthSizeBytes ( ) ) ; retPacket = packet ; if ( SUPER_VERBOSE ) { logBuffer ( "protect() After adding HMAC: " , retPacket . getPacket ( ) ) ; } return retPacket ; }
Protects an RTP Packet by encrypting payload and adds any additional SRTP trailer information