Ap4StreamCipher.h

Go to the documentation of this file.
00001 /*****************************************************************
00002 |
00003 |    AP4 - Stream Cipher
00004 |
00005 |    Copyright 2002-2006 Gilles Boccon-Gibod & Julien Boeuf
00006 |
00007 |
00008 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
00009 |
00010 |    Unless you have obtained Bento4 under a difference license,
00011 |    this version of Bento4 is Bento4|GPL.
00012 |    Bento4|GPL is free software; you can redistribute it and/or modify
00013 |    it under the terms of the GNU General Public License as published by
00014 |    the Free Software Foundation; either version 2, or (at your option)
00015 |    any later version.
00016 |
00017 |    Bento4|GPL is distributed in the hope that it will be useful,
00018 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 |    GNU General Public License for more details.
00021 |
00022 |    You should have received a copy of the GNU General Public License
00023 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
00024 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
00025 |    02111-1307, USA.
00026 |
00027 ****************************************************************/
00028 
00029 #ifndef _AP4_STREAM_CIPHER_H_
00030 #define _AP4_STREAM_CIPHER_H_
00031 
00032 /*----------------------------------------------------------------------
00033 |   includes
00034 +---------------------------------------------------------------------*/
00035 #include "Ap4Protection.h"
00036 #include "Ap4Results.h"
00037 #include "Ap4Types.h"
00038 
00039 /*----------------------------------------------------------------------
00040 |   constants
00041 +---------------------------------------------------------------------*/
00042 // we only support this for now 
00043 const unsigned int AP4_CIPHER_BLOCK_SIZE = 16;
00044 
00045 /*----------------------------------------------------------------------
00046 |   AP4_CtrStreamCipher class
00047 +---------------------------------------------------------------------*/
00048 class AP4_CtrStreamCipher
00049 {
00050 public:
00051    // methods
00052 
00057     AP4_CtrStreamCipher(AP4_BlockCipher* block_cipher, 
00058                         const AP4_UI08*  salt,
00059                         AP4_Size         counter_size);
00060     ~AP4_CtrStreamCipher();
00061     void       SetStreamOffset(AP4_UI64 offset);
00062     void       SetBaseCounter(const AP4_UI08* counter);
00063     AP4_Result ProcessBuffer(const AP4_UI08* in, 
00064                              AP4_UI08*       out,
00065                              AP4_Size        size);
00066     const AP4_UI08* GetBaseCounter()  { return m_BaseCounter;  }
00067     AP4_UI64        GetStreamOffset() { return m_StreamOffset; }
00068 
00069 private:
00070     // methods
00071     void SetCounterOffset(AP4_UI32 offset);
00072     void UpdateKeyStream();
00073 
00074     // members
00075     AP4_UI64         m_StreamOffset;
00076     AP4_Size         m_CounterSize;
00077     AP4_UI08         m_BaseCounter[AP4_CIPHER_BLOCK_SIZE];
00078     AP4_UI08         m_CBlock[AP4_CIPHER_BLOCK_SIZE];
00079     AP4_UI08         m_XBlock[AP4_CIPHER_BLOCK_SIZE];
00080     AP4_BlockCipher* m_BlockCipher;
00081 };
00082 
00083 /*----------------------------------------------------------------------
00084 |   AP4_CbcStreamCipher class
00085 +---------------------------------------------------------------------*/
00086 class AP4_CbcStreamCipher
00087 {
00088 public:
00089     // types
00090     typedef enum {
00091         ENCRYPT,
00092         DECRYPT
00093     } CipherDirection;
00094 
00095     // methods
00096 
00101     AP4_CbcStreamCipher(AP4_BlockCipher* block_cipher, CipherDirection direction);
00102     ~AP4_CbcStreamCipher();
00103     AP4_Result SetIV(const AP4_UI08* iv);
00104     AP4_Result ProcessBuffer(const AP4_UI08* in, 
00105                              AP4_Size        in_size,
00106                              AP4_UI08*       out,
00107                              AP4_Size*       out_size,
00108                              bool            is_last_buffer = false);
00109     AP4_UI64  GetStreamOffset() { return m_StreamOffset; }
00110 
00111 private:
00112     // members
00113     CipherDirection  m_Direction;
00114     AP4_UI64         m_StreamOffset;
00115     AP4_UI08         m_InBlockCache[AP4_CIPHER_BLOCK_SIZE];
00116     AP4_UI08         m_OutBlockCache[AP4_CIPHER_BLOCK_SIZE];
00117     AP4_BlockCipher* m_BlockCipher;
00118     bool             m_Eos;
00119 };
00120 
00121 #endif // _AP4_STREAM_CIPHER_H_

Generated on Thu Mar 15 16:06:12 2007 for Bento4 MP4 SDK by  doxygen 1.5.1-p1