Skip to main content
1 of 6

Can I construct a feasible stream cipher out of HMAC and a secure hash algorithm?

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 of data,
  • [data SHA512HMAC:key]: SHA-512 HMAC of data with key,
  • [data xor:another]: bitwise XOR of two data and another which have the same length,
  • [data bytesTo:index]: first index bytes of data,
  • [data removeFirstBytes:count]: remove first count bytes from data,
  • [data length]: length of data.
  • [data concat:other]: Concatenate other after data.
  • [Data new]: allocate an empty buffer.

Pseudocode: (Or actual Objective-C code?)

Data IV = [key SHA512];
Data segKey = IV;

Data dest = [Data new]; // This will be the output

do {
    // Cut off hash-sized chunks of source data
    Data segment = [data bytesTo:[IV length]];
    [data removeFirstBytes:[IV length]];

    // Derive a new segment key
    segKey = [segKey SHA512HMAC:IV];

    // XOR
    [dest concat:[segment xor:segKey]];
} while ([data length] > 0)

This code seem to decipher itself when given the same key.