I have constructed a stream cipher from a secure hash algorithm and a HMAC. Here is a brief description of the algorithm:
Let: (Actually Objective-C styled pseudo code)
[data SHA512Hash]: SHA-512 hash ofdata,[data SHA512HMAC:key]: SHA-512 HMAC ofdatawithkey,[data xor:another]: bitwise XOR of twodataandanotherwhich have the same length,[data bytesTo:index]: firstindexbytes ofdata,[data removeFirstBytes:count]: remove firstcountbytes fromdata,[data length]: length ofdata.[data concat:other]: Concatenateotherafterdata.[Data new]: allocate an empty buffer.
Pseudocode: (Or actual Objective-C code?)
Data hkey = [key SHA512]; // Hash to make lengths match.
Data segKey = [IV SHA512];
Data dest = [Data new]; // This will be the output
Data last = (Some constant)
do {
// Cut off hash-sized chunks of source data
Data segment = [data bytesTo:[hkey length]];
[data removeFirstBytes:[hkey length]];
// Derive a new segment key
segKey = [segKey SHA512HMAC:hkey]; // or [segKey SHA512HMAC:[hkey concat:last]]? If so, how to decrypt?
// XOR
last = [segment xor:[segKey bytesTo:[segment length]]] // Truncates to make length match
dest = [dest concat:last];
} while ([data length] > 0)
From some other questions I am suggested that this is SHA-512 HMAC running in OFB mode but what about the change in the code comment?
This code seem to decipher itself when given the same key.
Data IV = [key SHA512];means... or maybe try to explain the thing in english. – lvella Sep 18 '13 at 19:14