00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <mxflib/mxflib.h>
00031 using namespace mxflib;
00032
00033 #include <stdlib.h>
00034
00035
00036 #include "openssl/aes.h"
00037 #include "openssl/sha.h"
00038
00039
00041 extern bool Hashing;
00042
00044 extern bool ForceKeyMode;
00045
00046
00048
00049
00050
00051
00052 DataChunkPtr BuildHashKey(size_t Size, const UInt8 *CryptoKey);
00053
00055
00056
00057
00058
00059 inline DataChunkPtr BuildHashKey(DataChunkPtr &CryptoKey) { return BuildHashKey(CryptoKey->Size, CryptoKey->Data); }
00060
00062
00063
00064
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
00086
00087
00088 public:
00090 HashHMACSHA1() : KeyInited(false) {}
00091
00093
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
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
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
00370 if(Index)
00371 {
00372 Index->Update(IndexPos, (UInt64)Writer->GetStreamOffset());
00373 }
00374
00375
00376 Writer->WriteRaw(Object);
00377
00378
00379 IndexPos++;
00380
00381 return true;
00382 }
00383
00385 void SetIndex(IndexTablePtr Index) { this->Index = Index; }
00386 };
00387