{"id":696,"date":"2008-10-24T14:21:52","date_gmt":"2008-10-24T14:21:52","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2008\/10\/24\/how-not-to-do-message-integrity-featuring-cbc-mac\/"},"modified":"2008-10-24T14:21:52","modified_gmt":"2008-10-24T14:21:52","slug":"how-not-to-do-message-integrity-featuring-cbc-mac","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2008\/10\/24\/how-not-to-do-message-integrity-featuring-cbc-mac\/","title":{"rendered":"How Not to Do Message Integrity, featuring CBC-MAC"},"content":{"rendered":"<p> In my <a href=\"http:\/\/scientopia.org\/blogs\/goodmath\/2008\/10\/cryptographic-integrity-using-message-authentication-codes\">last cryptography post<\/a>, I wrote about using message authentication codes<br \/>\n(MACs) as a way of guaranteeing message integrity. To review briefly, most ciphers<br \/>\nare designed to provide message confidentiality &#8211; which means that no one but the<br \/>\nsender and the intended receiver can see the plain-text of the message. But<br \/>\nciphers that provide confidentiality don&#8217;t necessarily make any guarantees that<br \/>\nthe message received is exactly the message that was sent. There are a good number<br \/>\nof cryptographic attacks that work by altering the message in transit, and<br \/>\ndepending on the cipher, that can result in a variety of undesirable<br \/>\nresults.<\/p>\n<p> For example, if you use DES encryption with the ECB mode of operation,<br \/>\nyou can insert new blocks anywhere in a message that you want.  By using<br \/>\na replay attack (where you take encrypted blocks from other messages using<br \/>\nthe same encryption, and resend them), an attacker can alter your messages, and<br \/>\nyou won&#8217;t be able to detect it.<\/p>\n<p> So in addition to just confidentiality, we need to provide integrity. What does integrity really  mean? Basically, it expands the definition of the<br \/>\ndecryption function. Written as a function signature, confidential message<br \/>\ndecryption is a function <code>decrypt : ciphertext &times; key &rarr; plaintext<\/code>. With message integrity, we add the<br \/>\noption that decrypt can return a result saying that the message is invalid: <code>decrypt<sub>integ<\/sub> : ciphertext &times; key &rarr; (plaintext | REJECT)<\/code>. <\/p>\n<p><!--more--><\/p>\n<p> In the last post, I explained the basic concept behind integrity schemes: you add an addition chunk of data to the message, which summarizes the message; any change to the message will (with a very high degree of probability) result in<br \/>\na change in the authentication code; so if the message is altered, that change<br \/>\nwill be detected, because the authentication code will not match the rest<br \/>\nof the message.<\/p>\n<p> The idea of the message authentication code seems quite simple. We know lots<br \/>\nof pseudo-random functions that can do a good job as MAC functions. But like<br \/>\nso many things in cryptography, the fact that the concept is simple doesn&#8217;t<br \/>\nmean that the actual application of that concept is simple: it&#8217;s incredibly<br \/>\neasy to get it disastrously wrong, even when all of the building blocks are right.<\/p>\n<p> For example, let&#8217;s look at a very simple (and very weak) integrity<br \/>\nsystem, called CBC-MAC.<\/p>\n<p> In CBC-MAC, you compute an authentication code by running a your<br \/>\nblock cipher on the message with an initialization vector set<br \/>\nto 0. The output for encrypting the last block is your MAC.<\/p>\n<p> The reason that this works is fairly simple &#8211; CBC keeps pushing the<br \/>\nresult from the previous encryption step through to the next one &#8211; so<br \/>\nthe output from the final block conceptually includes information<br \/>\nfrom all of the other blocks. So you&#8217;ve got an authentication code<br \/>\nwhose security is, roughly, equivalent to the security of the block<br \/>\ncipher used in CBC mode.<\/p>\n<p> CBC-MAC isn&#8217;t a bad mode of operation. It&#8217;s got serious flaws &#8211; the biggest<br \/>\none is that there are a number of attacks against it if it&#8217;s used for<br \/>\nvariable length messages. (And you can&#8217;t fix it just by adding a message<br \/>\nlength indicator to the message, it&#8217;s deeper than that.) But CBC is<br \/>\na pretty good integrity mode for fixed-length messages, and there&#8217;s a widely<br \/>\nused variant, called CMAC, which works for variable length messages.<\/p>\n<p> So how can this go wrong?<\/p>\n<p> What do you use as the password to the MAC computation? CBC is<br \/>\na secure cryptographic mode, so why not just use the same password? Lots<br \/>\nof novices (and even some not-no-novice folks!) have implemented CBC-MAC<br \/>\nwhere the integrity and confidentiality modes used the same key. If you<br \/>\ncompute the MAC using the same key as the message, then you&#8217;re open to<br \/>\nan attack which completely voids the integrity guarantee!<\/p>\n<p> Imagine a message M, which is divided into n blocks: (M<sub>1<\/sub>, &#8230;, M<sub>n<\/sub>). For convenience, we&#8217;ll treat the IV as if it were ciphertext block 0. Tuen using CBC with a key K, M encrypts to ciphertext blocks<br \/>\n(C<sub>1<\/sub>, &#8230;, C<sub>K<\/sub>), where C_i=Encrypt(M<sub>i<\/sub> &oplus; C<sub>i-1<\/sub>, K), plus a CBC-MAC integrity block. If you look<br \/>\nat the CBC block, if it&#8217;s using the same key as the main encryption,<br \/>\nthen the value of the MAC is the result of computing<br \/>\nEncrypt(M<sub>n<\/sub> &oplus;C<sub>n-1<\/sub>, k). That value<br \/>\nis <em>exactly<\/em> the same as C<sub>N<\/sub>! So it&#8217;s <em>trivial<\/em><br \/>\nto fake the MAC. If you use CBC-MAC with the same encryption key as<br \/>\nyou used to encrypt the message, you&#8217;re wasting your time: you might as well<br \/>\nnot bother with the MAC at all, because you&#8217;ve got no integrity guarantee.<\/p>\n<p> But that&#8217;s an amazingly common mistake. A huge number of implementations<br \/>\nof supposedly secure cryptosystems that claim to guarantee integrity have<br \/>\nactually used CBC-MAC with one key.<\/p>\n<p> I mentioned above that CBC-MAC is only safe for fixed-length messages. The reason for that is closely related to the trick I described above. Basically, the nature of CBC ultimately means that given a MAC value M, it&#8217;s not hard to generate a string to append to a message which will result in it having the desired<br \/>\nMAC. Suppose we know that the message M will generate the mac T<sub>M<\/sub>,<br \/>\nand we know that the block we want to replace the message with, N has<br \/>\nthe MAC T<sub>N<\/sub>. Then we can easily generate a string N&#8217;, such that<br \/>\nappending MAC(M,N) = MAC(N). All we do is XOR the first block of N<br \/>\nwith T<sub>M<\/sub>, and add it to the message. The new message<br \/>\nis is (M<sub>1<\/sub>,&#8230;,M<sub>n<\/sub>,(N<sub>1<\/sub>&oplus;T<sub>N<\/sub>),<br \/>\nN<sub>2<\/sub>,&#8230;,N<sub>n<\/sub>) &#8211; that is, we can remove the MAC from the<br \/>\nfirst message, use it to glue the two messages together, and tack T<sub>N<\/sub> on to the message as the MAC. T<sub>N<\/sub>, the MAC computed for T alone, will<br \/>\nbe the MAC for the combined message. <\/p>\n<p> It&#8217;s easy to do that if we&#8217;re allowed to change the length of the message. If the receiver doesn&#8217;t know how long the message is going to be, then they can&#8217;t<br \/>\ncount on its integrity using CBC-MAC. <\/p>\n<p> Next time, I&#8217;ll look at a variation of CBC-MAC that provides integrity<br \/>\nfor variable-length messages using 2 keys &#8211; one for confidentiality, and<br \/>\none for integrity. Then I&#8217;ll go on to a two-pass single-key mode of operation<br \/>\nthat provides both confidentiality and integrity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last cryptography post, I wrote about using message authentication codes (MACs) as a way of guaranteeing message integrity. To review briefly, most ciphers are designed to provide message confidentiality &#8211; which means that no one but the sender and the intended receiver can see the plain-text of the message. But ciphers that provide [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[84],"tags":[],"class_list":["post-696","post","type-post","status-publish","format-standard","hentry","category-encryption"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-be","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=696"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/696\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=696"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}