c# - Encrypt-then-MAC, how to afterwards add data to HMAC -


i want include iv , salt in hmacsha512 calculation without add encrypted data.
@ moment change iv , wouldn't noticed that.

i chain different streams ensure encrypt-then-mac, later want encrypt large files, design necessary.
if add the iv , salt stream, e.g. new memorystream(iv).copyto(hmacstream); result contain data.

this code far:

    private static ihmacanddata encryptinternal(byte[] key, byte[] iv, byte[] plaindata, byte[] salt)     {         byte[] hmachash;         byte[] encryptedbytes;          using (var aesmanaged = createaesmanaged(iv, key))         {             var encryptor = aesmanaged.createencryptor(aesmanaged.key, aesmanaged.iv);             var hmacsha512 = new hmacsha512(key);              using (var resultstream = new memorystream())             {                 using (var hmacstream = new cryptostream(resultstream, hmacsha512, cryptostreammode.write))                 {                     using (var aesstream = new cryptostream(hmacstream, encryptor, cryptostreammode.write))                     {                         using (var plainstream = new memorystream(plaindata))                         {                             plainstream.copyto(aesstream);                         }                     }                 }                 encryptedbytes = resultstream.toarray();             }             hmachash = hmacsha512.hash;         }          return new message {hmac = hmachash, data = encryptedbytes};     }      private static aesmanaged createaesmanaged(byte[] iv, byte[] key)     {         var aesmanaged = new aesmanaged         {             mode = ciphermode.cbc,             padding = paddingmode.pkcs7,             keysize = keysize,             iv = iv,             key = key         };         return aesmanaged;     } 

my temporary solution make second hmacsha512 calculation @ end. seems not right in way.

var overallhmac = new hmacsha512(keyhmac); hmachash = overallhmac.computehash(hmachash.concat(iv).concat(saltpassword).concat(salthmac).toarray()); 

here full sample, search createoverallhmackey find spot. https://gist.github.com/dhcgn/85b88b516953e8996af8544ee9d7b567


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -