# MAC with key supplied afterwards

Looking at HMAC and KMAC it seems that the key is always required up front, i.e. before starting to process any other data. However, in many protocols the key may not be known it advance. For instance, in TLS, the PRF (MAC) is calculated over the messages produced before key agreement.

TLS 1.2 solves this by producing a separate secure hash:

    PRF(master_secret, finished_label, Hash(handshake_messages))
[0..verify_data_length-1];


That's great but somewhat inefficient as it requires a separate hash (both regarding processing time and configuration options). Are there any MAC algorithms that can configure the key afterwards without caching the data? Or are there any reasons not to do this?

• Any MAC with a bounded amount of state, and which processes the key last would need to do a collision resistant mapping from the message to the state. Given this, wouldn't it be best if you used a designed collision-resistant function for this purpose? – poncho Mar 12 '18 at 20:22
• @poncho Um, I'm trying to see what you are meaning. So uh, you're saying that you'd need a hash anyway (the collision resistant function) so you might as well do $\operatorname{MAC}(K, \operatorname{Hash}(M))$? – Maarten Bodewes Mar 12 '18 at 20:25
• It would certainly look like the best option to me; the alternative would be to rely on a mapping that's not designed to be a collision resistant hash function; perhaps that's doable (as the state size need not be limited to twice the target security strength, it might be a tad more efficient), but still to me, it would raise concerns... – poncho Mar 12 '18 at 20:31
• But an MD construction is already collision resistant before you calculate the final result right? Wouldn't you be able to use that as intermediate state? The final state of an MD hash is little more than a continuation of the hash function after all? – Maarten Bodewes Mar 12 '18 at 20:52
• Until you stir the key in, there's nothing in the MD that the adversary can't compute, hence (unlike MACs in general), collision attacks are certainly on the table. – poncho Mar 12 '18 at 21:14