3

I need to use a stream cipher in my application. I know using the same key for many messages is not a good practice for stream ciphers (RC4). But I also dont want to make a new key all the time so I came up with the following.
I create securelly a secret key. Then for every message I send, the key I use in the encryption will be the sha1 hash of the previous key.
So for 1st message the key will be key = sha1(key)
for the 2nd message the key will be key = sha1(sha1(key)), etc..

key := makeSecureKey()
while(true){
     key := sha1hash(key);
     message := getNewMessage();
     encrypted_message := RC4.Encrypt(message, key);
     send(encrypted_message);
}

Is encryption used correctly here?
Finally if instead of using the 160bit result of the sha1 algorithm, I use only the first 128 of the 160bits as a key is there a problem?

1

This is bad practice for so many reasons.

  1. RC4 has been broken every which way since Sunday.
  2. Your key generation algorithm is home grown and may not be safe. Perhaps someone on crypto.stackexchange.com is willing to analyze it for you. I'd leave it behind and use a published KDF.
  3. There are known attacks against RC4 that involve subsequent derivations of a starting key. I do not know if there is one for this.
  4. The SHA hashing function wasn't designed to be a good PRNG, nor a good key generator, these aren't its purpose. There may be artifacts within the resulting hash that compromise how any cipher uses it as a key, let alone RC4.
  5. Modern block ciphers with CBC mode are faster than your network stream can handle data, so if this is traveling over a wire, you don't need a stream cipher.

This Q&A has an excellent answer for why you should use block ciphers over stream ciphers. Also, I prefer AES+CBC for Privacy (encryption) and HMAC for Integrity protection over algorithms like EAX or GCM which perform both in one step. Not sure if I could defend this position (security, mathematically), nevertheless the one algorithm to address both bothers me.

  • I'm not sure where you're reading in that Q&A that you should use CBC as the preferred block cipher mode... – puzzlepalace Nov 26 '15 at 22:08
  • @puzzlepalace, fair enough, I skimmed that Answer and saw him test (for performance) AES+CBC. I'm not convinced combining authn+enc in one step (EAX or GCM) is really secure against attacks. I prefer separate AES+CBC for Privacy and HMAC for Integrity. Changing my answer to reflect this. – Andrew Philips Nov 26 '15 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.