For various reasons I've been studying all kinds of authentication protocols, lately. A good authentication protocol should, for my purposes, fulfill the following properties:

  1. It does not disclose the secret to an eavesdropper over the wire.
  2. It does not require the server to store the secret.
  3. It is reasonably simple to implement.

As always, it seems to be a case of "choose two".

A little surprise for me was CRAM-MD5. Of course MD5 should not be used anymore, but CRAM-MD5 can trivially be changed into a CRAM-SHA256 just by replacing the underlying hash function. So let's keep the weak hash function out for the purpose of the discussion.

The point is, CRAM-MD5 fulfills 1. and 3. but NOT 2. This often is not immediately obvious and users and server administrators might not be aware of it. E.g. when you look at the native user database of a CRAM-MD5 enabled dovecot server, you will see something like this:


This looks like a hash of the password you use to authenticate to the dovecot server. But CRAM-MD5 is more or less just an alias for a classic challenge-response protocol based on HMAC-MD5. In a classic HMAC based authentication protocol the server sends the client a random nonce value and the client is supposed to respond with HMAC(secret, nonce). Then the server must also calculate HMAC(secret, nonce) on his side and compare the clients response with the expected result. If they match, he can know that the client also knew the secret.

As you can already see, the server MUST know the secret (unless there is some magical trick somewhere). So I looked into the relevant RFC-2159 for the CRAM protocol and RFC-2104 for the HMAC details. And deep in there in section "4. Implementation Note" in RFC-2104 you will find the answer.

Let's look at it in detail. First we need the definition of the HMAC function.

HMAC(secret, nonce) =
  H( secret ^ opad,
    H( secret ^ ipad, nonce )

Where H(m1, m2, ...) is a cryptographic hash function applied on it's concatenated arguments. opad and ipad are padding values that depend on the length of secret. ^ is a bitwise XOR. Now most hash functions can also be implemented with an additional initialization vector (IV) argument which contains the hash functions last internal state. With such an IV, a previous hash calculation can be continued at the place where it was stopped. If you only store the IVs for H( secret ^ opad ) and H( secret ^ ipad ) you can still calculate the complete HMAC while not storing the secret in plain.

HMAC(secret, nonce) =
  H( IV_opad,
    H( IV_ipad, nonce )

Turns out this is exactly what dovecot and probably any other sane CRAM-MD5 implementation does and has to do.

While this protects the original secret (your password) to some degree, the stored IVs are exactly as powerfull as the secret. If an attacker manages to get the stored CRAM-MD5 IV he can use it to log in into any other account that supports CRAM-MD5 and uses the same password.

That means, unlike crypt or bcrypt hashed password databases, a database with CRAM-MD5 IVs must be kept secret from any non-authorized user.

So while the CRAM-MD5 challenge-response authentication looks like beeing more secure than a plaintext authentication, it actually depends on the attacker model. You might be better off with just plaintext authentication over SSL (and a proper bcrypt password-hash database).