crypto_asdcp.h

Go to the documentation of this file.
00001 
00007 /*
00008  *  Copyright (c) 2004, Matt Beard
00009  *
00010  *  This software is provided 'as-is', without any express or implied warranty.
00011  *  In no event will the authors be held liable for any damages arising from
00012  *  the use of this software.
00013  *
00014  *  Permission is granted to anyone to use this software for any purpose,
00015  *  including commercial applications, and to alter it and redistribute it
00016  *  freely, subject to the following restrictions:
00017  *
00018  *    1. The origin of this software must not be misrepresented; you must
00019  *       not claim that you wrote the original software. If you use this
00020  *       software in a product, an acknowledgment in the product
00021  *       documentation would be appreciated but is not required.
00022  *  
00023  *    2. Altered source versions must be plainly marked as such, and must
00024  *       not be misrepresented as being the original software.
00025  *  
00026  *    3. This notice may not be removed or altered from any source
00027  *       distribution.
00028  */
00029 
00030 #include <mxflib/mxflib.h>
00031 using namespace mxflib;
00032 
00033 #include <stdlib.h>
00034 
00035 // Include AES encryption from OpenSSL
00036 #include "openssl/aes.h"
00037 #include "openssl/sha.h"
00038 
00039 
00041 extern bool Hashing;
00042 
00044 extern bool ForceKeyMode;
00045 
00046 
00048 /*  The hashing key is: 
00049  *  - trunc( HMAC-SHA-1( CipherKey, 0x00112233445566778899aabbccddeeff ) )
00050  *  Where trunc(x) is the first 128 bits of x
00051  */
00052 DataChunkPtr BuildHashKey(size_t Size, const UInt8 *CryptoKey);
00053 
00055 /*  The hashing key is: 
00056  *  - trunc( HMAC-SHA-1( CipherKey, 0x00112233445566778899aabbccddeeff ) )
00057  *  Where trunc(x) is the first 128 bits of x
00058  */
00059 inline DataChunkPtr BuildHashKey(DataChunkPtr &CryptoKey) { return BuildHashKey(CryptoKey->Size, CryptoKey->Data); }
00060 
00062 /*  The hashing key is: 
00063  *  - trunc( HMAC-SHA-1( CipherKey, 0x00112233445566778899aabbccddeeff ) )
00064  *  Where trunc(x) is the first 128 bits of x
00065  */
00066 inline DataChunkPtr BuildHashKey(DataChunk &CryptoKey) { return BuildHashKey(CryptoKey.Size, CryptoKey.Data); }
00067 
00068 
00069 // ============================================================================
00071 
00073 // ============================================================================
00074 
00075 class HashHMACSHA1 : public Hash_Base
00076 {
00077 protected:
00078     UInt8 KeyBuffer_i[64];                  
00079     UInt8 KeyBuffer_o[64];                  
00080 
00081     SHA_CTX Context;                        
00082 
00083     bool KeyInited;                         
00084 /*
00085 FileHandle OutFile;
00086 */
00087 
00088 public:
00090     HashHMACSHA1() : KeyInited(false) {}
00091 
00093     /*  \return True if key is accepted
00094      */
00095     bool SetKey(size_t Size, const UInt8 *Key);
00096 
00098     void HashData(size_t Size, const UInt8 *Data);
00099 
00101     DataChunkPtr GetHash(void);
00102 };
00103 
00104 
00105 
00106 // ============================================================================
00108 // ============================================================================
00109 class AESEncrypt : public Encrypt_Base
00110 {
00111 protected:
00112     AES_KEY CurrentKey;
00113     UInt8 CurrentIV[16];
00114 
00115 public:
00117 
00119     bool SetKey(size_t KeySize, const UInt8 *Key) 
00120     {
00121         int Ret = AES_set_encrypt_key(Key, 128, &CurrentKey);
00122 
00123         // Return true only if key setting was OK
00124         return Ret ? false : true; 
00125     };
00126 
00128 
00135     bool SetIV(size_t IVSize, const UInt8 *IV, bool Force = false) 
00136     { 
00137         if(!Force) return false;
00138 
00139         if(IVSize != 16)
00140         {
00141             error("IV for AES encryption must by 16 bytes, tried to use IV of size %d\n", IVSize);
00142             return false;
00143         }
00144 
00145         memcpy(CurrentIV, IV, 16);
00146 
00147         return true; 
00148     };
00149 
00151 
00155     DataChunkPtr GetIV(void) 
00156     {
00157         return new DataChunk(16, CurrentIV);
00158     }
00159 
00161 
00164     bool CanEncryptInPlace(size_t BlockSize = 0) { return false; }
00165 
00167 
00169     bool EncryptInPlace(size_t Size, UInt8 *Data) { return false; }
00170 
00172 
00174     DataChunkPtr Encrypt(size_t Size, const UInt8 *Data);
00175 };
00176 
00177 
00178 // ============================================================================
00180 // ============================================================================
00181 class Encrypt_GCReadHandler : public GCReadHandler_Base
00182 {
00183 protected:
00184     UInt32 OurSID;                                  
00185     GCWriterPtr Writer;                             
00186     UUIDPtr ContextID;                              
00187 
00188     DataChunk EncKey;                               
00189 
00190     Length PlaintextOffset;                         
00191 
00192     IndexTablePtr Index;                            
00193     Position IndexPos;                              
00194 
00195 private:
00196     Encrypt_GCReadHandler();                        
00197 
00198 public:
00200     Encrypt_GCReadHandler(GCWriterPtr Writer, UInt32 BodySID, UUIDPtr &ContextID, DataChunkPtr KeyID, std::string KeyFileName);
00201 
00203 
00205     virtual bool HandleData(GCReaderPtr Caller, KLVObjectPtr Object);
00206 
00208     void SetPlaintextOffset(Length Offset) { PlaintextOffset = Offset; }
00209 
00211     void SetIndex(IndexTablePtr Index) { this->Index = Index; }
00212 };
00213 
00214 
00215 
00216 // ============================================================================
00218 
00220 // ============================================================================
00221 class Basic_GCFillerHandler : public GCReadHandler_Base
00222 {
00223 protected:
00224     UInt32 OurSID;                              
00225     GCWriterPtr Writer;                         
00226 
00227 private:
00228     Basic_GCFillerHandler();                    
00229 
00230 public:
00232     Basic_GCFillerHandler(GCWriterPtr Writer, UInt32 BodySID) : OurSID(BodySID), Writer(Writer)  {};
00233 
00235 
00237     virtual bool HandleData(GCReaderPtr Caller, KLVObjectPtr Object) { return true; }
00238 };
00239 
00240 
00241 
00242 // ============================================================================
00244 // ============================================================================
00245 class AESDecrypt : public Decrypt_Base
00246 {
00247 protected:
00248     AES_KEY CurrentKey;
00249     UInt8 CurrentIV[16];
00250 
00251 public:
00253 
00255     virtual bool SetKey(size_t KeySize, const UInt8 *Key) 
00256     {
00257         int Ret = AES_set_decrypt_key(Key, 128, &CurrentKey);
00258 
00259         // Return true only if key setting was OK
00260         return Ret ? false : true; 
00261     };
00262 
00264 
00271     bool SetIV(size_t IVSize, const UInt8 *IV, bool Force = false)
00272     { 
00273         if(!Force) return false;
00274 
00275         if(IVSize != 16)
00276         {
00277             error("IV for AES encryption must by 16 bytes, tried to use IV of size %d\n", IVSize);
00278             return false;
00279         }
00280 
00281         memcpy(CurrentIV, IV, 16);
00282 
00283         return true; 
00284     };
00285 
00287 
00291     DataChunkPtr GetIV(void)
00292     {
00293         return new DataChunk(16, CurrentIV);
00294     }
00295 
00297 
00300     bool CanDecryptInPlace(size_t BlockSize = 0) { return false; }
00301 
00303 
00305     bool DecryptInPlace(size_t Size, UInt8 *Data) { return false; }
00306 
00308 
00310     DataChunkPtr Decrypt(size_t Size, const UInt8 *Data);
00311 };
00312 
00313 
00314 
00315 // ============================================================================
00317 // ============================================================================
00318 class Decrypt_GCEncryptionHandler : public GCReadHandler_Base
00319 {
00320 protected:
00321     UInt32 OurSID;                                      
00322 
00323     DataChunk DecKey;                                   
00324 
00325 private:
00326     Decrypt_GCEncryptionHandler();                      
00327 
00328 public:
00330     Decrypt_GCEncryptionHandler(UInt32 BodySID, DataChunkPtr KeyID, std::string KeyFileName);
00332 
00335     virtual bool HandleData(GCReaderPtr Caller, KLVObjectPtr Object);
00336 
00338     bool KeyValid(void) { return (DecKey.Size == 16); }
00339 };
00340 
00341 
00342 // ============================================================================
00344 
00347 // ============================================================================
00348 class Decrypt_GCReadHandler : public GCReadHandler_Base
00349 {
00350 protected:
00351     UInt32 OurSID;                                      
00352     GCWriterPtr Writer;                                 
00353 
00354     IndexTablePtr Index;                                
00355     Position IndexPos;                                  
00356 
00357 private:
00358     Decrypt_GCReadHandler();                            
00359 
00360 public:
00362     Decrypt_GCReadHandler(GCWriterPtr Writer, UInt32 BodySID) : OurSID(BodySID), Writer(Writer), IndexPos(0) {};
00363 
00365 
00367     virtual bool HandleData(GCReaderPtr Caller, KLVObjectPtr Object) 
00368     {
00369         // Update the index table to the new position
00370         if(Index)
00371         {
00372             Index->Update(IndexPos, (UInt64)Writer->GetStreamOffset());
00373         }
00374 
00375         // Write the data without further processing
00376         Writer->WriteRaw(Object);
00377 
00378         // Update the index position count (even if not yet indexing)
00379         IndexPos++;
00380 
00381         return true;
00382     }
00383 
00385     void SetIndex(IndexTablePtr Index) { this->Index = Index; }
00386 };
00387 

Generated on Mon Apr 2 15:20:51 2007 for MXFLib by  doxygen 1.5.1-p1