1 /*****************************************************************
2 |
3 |    AP4 - ISMA E&A support
4 |
5 |    Copyright 2002-2008 Axiomatic Systems, LLC
6 |
7 |
8 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
9 |
10 |    Unless you have obtained Bento4 under a difference license,
11 |    this version of Bento4 is Bento4|GPL.
12 |    Bento4|GPL is free software; you can redistribute it and/or modify
13 |    it under the terms of the GNU General Public License as published by
14 |    the Free Software Foundation; either version 2, or (at your option)
15 |    any later version.
16 |
17 |    Bento4|GPL is distributed in the hope that it will be useful,
18 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
19 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 |    GNU General Public License for more details.
21 |
22 |    You should have received a copy of the GNU General Public License
23 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
24 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 |    02111-1307, USA.
26 |
27 ****************************************************************/
28 
29 #ifndef _AP4_ISMACRYP_H_
30 #define _AP4_ISMACRYP_H_
31 
32 /*----------------------------------------------------------------------
33 |   includes
34 +---------------------------------------------------------------------*/
35 #include "Ap4Types.h"
36 #include "Ap4SampleEntry.h"
37 #include "Ap4Atom.h"
38 #include "Ap4AtomFactory.h"
39 #include "Ap4SampleDescription.h"
40 #include "Ap4Processor.h"
41 #include "Ap4Protection.h"
42 
43 /*----------------------------------------------------------------------
44 |   class references
45 +---------------------------------------------------------------------*/
46 class AP4_CtrStreamCipher;
47 class AP4_IsfmAtom;
48 
49 /*----------------------------------------------------------------------
50 |   constants
51 +---------------------------------------------------------------------*/
52 const AP4_UI32 AP4_PROTECTION_SCHEME_TYPE_IAEC = AP4_ATOM_TYPE('i','A','E','C');
53 
54 /*----------------------------------------------------------------------
55 |   AP4_IsmaCipher
56 +---------------------------------------------------------------------*/
57 class AP4_IsmaCipher : public AP4_SampleDecrypter
58 {
59 public:
60     // factory
61     static AP4_Result CreateSampleDecrypter(AP4_ProtectedSampleDescription* sample_description,
62                                             const AP4_UI08*                 key,
63                                             AP4_Size                        key_size,
64                                             AP4_BlockCipherFactory*         block_cipher_factory,
65                                             AP4_IsmaCipher*&                decrypter);
66 
67     // constructor and destructor
68     AP4_IsmaCipher(AP4_BlockCipher* block_cipher,
69                    const AP4_UI08*  salt,
70                    AP4_UI08         iv_length,
71                    AP4_UI08         key_indicator_length,
72                    bool             selective_encryption);
73    ~AP4_IsmaCipher();
74     AP4_Result EncryptSampleData(AP4_DataBuffer& data_in,
75                                  AP4_DataBuffer& data_out,
76                                  AP4_UI32        block_counter);
77     AP4_Result DecryptSampleData(AP4_UI32 pool_id, AP4_DataBuffer& data_in,
78                                  AP4_DataBuffer& data_out,
79                                  const AP4_UI08* iv = NULL);
80     AP4_Size   GetDecryptedSampleSize(AP4_Sample& sample);
GetSalt()81     const AP4_UI08*      GetSalt()     { return m_Salt;     }
GetCipher()82     AP4_CtrStreamCipher* GetCipher()   { return m_Cipher;   }
GetIvLength()83     AP4_UI08             GetIvLength() { return m_IvLength; }
GetKeyIndicatorLength()84     AP4_UI08             GetKeyIndicatorLength() { return m_KeyIndicatorLength; }
GetSelectiveEncryption()85     bool                 GetSelectiveEncryption(){ return m_SelectiveEncryption;}
86 
87 private:
88     // members
89     AP4_CtrStreamCipher* m_Cipher;
90     AP4_UI08             m_Salt[8];
91     AP4_UI08             m_IvLength;
92     AP4_UI08             m_KeyIndicatorLength;
93     bool                 m_SelectiveEncryption;
94 };
95 
96 /*----------------------------------------------------------------------
97 |   AP4_IsmaTrackDecrypter
98 +---------------------------------------------------------------------*/
99 class AP4_IsmaTrackDecrypter : public AP4_Processor::TrackHandler {
100 public:
101     // construction
102     static AP4_Result Create(AP4_TrakAtom*                   trak,
103 		                     AP4_TrexAtom*                   trex,
104 		                     const AP4_UI08*                 key,
105                              AP4_Size                        key_size,
106                              AP4_ProtectedSampleDescription* sample_description,
107                              AP4_SampleEntry*                sample_entry,
108                              AP4_BlockCipherFactory*         block_cipher_factory,
109                              AP4_IsmaTrackDecrypter*&        decrypter);
110 
111     virtual ~AP4_IsmaTrackDecrypter();
112 
113     // methods
114     virtual AP4_Size   GetProcessedSampleSize(AP4_Sample& sample);
115     virtual AP4_Result ProcessTrack();
116     virtual AP4_Result ProcessSample(AP4_DataBuffer& data_in,
117                                      AP4_DataBuffer& data_out);
118 
119 private:
120     // constructor
121 	AP4_IsmaTrackDecrypter(AP4_TrakAtom*    trak,
122 		                   AP4_TrexAtom*    trex,
123 		                   AP4_IsmaCipher*  cipher,
124                            AP4_SampleEntry* sample_entry,
125                            AP4_UI32         original_format);
126 
127     // members
128     AP4_IsmaCipher*  m_Cipher;
129     AP4_SampleEntry* m_SampleEntry;
130     AP4_UI32         m_OriginalFormat;
131 };
132 
133 /*----------------------------------------------------------------------
134 |   AP4_IsmaEncryptingProcessor
135 +---------------------------------------------------------------------*/
136 class AP4_IsmaEncryptingProcessor : public AP4_Processor
137 {
138 public:
139     // constructors and destructor
140     AP4_IsmaEncryptingProcessor(const char*             kms_uri,
141                                 AP4_BlockCipherFactory* block_cipher_factory = NULL);
142 
143     // accessors
GetKeyMap()144     AP4_ProtectionKeyMap& GetKeyMap() { return m_KeyMap; }
145 
146     // methods
147     virtual AP4_Processor::TrackHandler* CreateTrackHandler(AP4_TrakAtom* trak);
148 
149 private:
150     // members
151     AP4_ProtectionKeyMap    m_KeyMap;
152     AP4_String              m_KmsUri;
153     AP4_BlockCipherFactory* m_BlockCipherFactory;
154 };
155 
156 #endif // _AP4_ISMACRYP_H_
157