00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _AP4_STREAM_CIPHER_H_
00030 #define _AP4_STREAM_CIPHER_H_
00031
00032
00033
00034
00035 #include "Ap4Protection.h"
00036 #include "Ap4Results.h"
00037 #include "Ap4Types.h"
00038
00039
00040
00041
00042
00043 const unsigned int AP4_CIPHER_BLOCK_SIZE = 16;
00044
00045
00046
00047
00048 class AP4_CtrStreamCipher
00049 {
00050 public:
00051
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
00071 void SetCounterOffset(AP4_UI32 offset);
00072 void UpdateKeyStream();
00073
00074
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
00085
00086 class AP4_CbcStreamCipher
00087 {
00088 public:
00089
00090 typedef enum {
00091 ENCRYPT,
00092 DECRYPT
00093 } CipherDirection;
00094
00095
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
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_