{"id":678,"date":"2008-09-04T15:36:49","date_gmt":"2008-09-04T15:36:49","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2008\/09\/04\/introduction-to-block-ciphers\/"},"modified":"2008-09-04T15:36:49","modified_gmt":"2008-09-04T15:36:49","slug":"introduction-to-block-ciphers","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2008\/09\/04\/introduction-to-block-ciphers\/","title":{"rendered":"Introduction to Block Ciphers"},"content":{"rendered":"<p> Where encryption starts getting really interesting, in my opinion, is<br \/>\nblock ciphers. Block ciphers are a general category of ciphers that<br \/>\nare sort of a combination of substitution and transposition ciphers, and<br \/>\nsort of something entirely different. They&#8217;re really fascinating<br \/>\nthings, but they&#8217;re pretty complicated.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Tux.jpg\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_322.jpg?resize=196%2C216\" width=\"196\" height=\"216\" class=\"inset\" \/><br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Tux_ecb.jpg\" src=\"https:\/\/i0.wp.com\/scientopia.org\/img-archive\/goodmath\/img_323.jpg?resize=196%2C216\" width=\"196\" height=\"216\" class=\"inset\" \/><\/p>\n<p> The basic core of block ciphers is encryption of blocks. A block is<br \/>\na fixed-length series of bits. The basic cipher is a pair of functions (E,E<sup>-1<\/sup>), where E (the <em>encryption function<\/em>) takes a block B and a key K, and generates a new block B&#8217;=E(K,B), which is the encrypted form of the block; and E<sup>-1<\/sup> (the <em>decryption<\/em> function) takes a key and an encrypted block, and returns the original plaintext block: B=E<sup>-1<\/sup>(K,B&#8217;).<\/p>\n<p><!--more--><\/p>\n<p> The ideal block cipher will have the properties that:<\/p>\n<ol>\n<li> For most blocks B and keys K, E(K, B) is not very similar to B.<\/li>\n<li> For most blocks B and C which differ by one bit, E(K,B) and E(K,C)<br \/>\nwill differ by much more than one bit.<\/li>\n<li> Given E and K, for any block B, it&#8217;s easy to compute E(K,B)<br \/>\nand E<sup>-1<\/sup>(K,B)<\/li>\n<li> Given E and an encrypted text B, it&#8217;s hard to compute either<br \/>\nthe encryption key, or the plaintext block.<\/li>\n<\/ol>\n<p> What happens to the block? It depends a lot on the particular block cipher. But in<br \/>\ngeneral, it&#8217;s treated as a set of sub-blocks, which are, in turn, treated as a sort of<br \/>\nlarge alphabet. The block is encrypted using what amounts to some substitutions, and some<br \/>\ntranspositions, performed in sequence. In many block ciphers, there&#8217;s a fairly simple<br \/>\nbasic mechanism for substitution and transposition, which is repeated multiple times to<br \/>\nproduce a final result. The specific set of substitutions and transpositions is determined<br \/>\nboth by the specifics of the block cipher being used, <em>and<\/em> by the encryption key<br \/>\nused for the message. So the way that the key gets incorporated in the encryption of a<br \/>\nsingle block in a block cipher is by selecting the particular sequence of substitutions<br \/>\nand transpositions on the elements of the block.<\/p>\n<p> Of course, there&#8217;s a huge problem with the description so far, even given how<br \/>\nincomplete it is. Not all messages that we want to encrypt are the same size, but we&#8217;ve<br \/>\nonly described the basic idea of encrypting a single block! Filling in that gap makes it<br \/>\nclear why this kind of crypto is called a block cipher. Take a message of arbitrary size,<br \/>\nS. To encrypt it with a block cipher with block size B, you break the message into<br \/>\n&lceil;S\/B&rceil; blocks, padding out the last block in some way to make it full. Then you<br \/>\nencrypt the blocks individually one at a time, and then&#8230;<\/p>\n<p> You were expecting me to say something like &#8220;output them&#8221;, right? Sorry. Not that<br \/>\nsimple. There&#8217;s another little trick for block ciphers. You don&#8217;t really just<br \/>\ndo the blocks in sequence, and then combine them. You usually add something: some kind of feedback between the blocks, some kind of transposition to re-order them, etc. Without that, you can get really poor results. Wikipedia has a fantastic example of what happens if you naively just encrypt each block, without any feedback or transposition.  The<br \/>\nfirst figure to the right is a bitmap image of Tux the Linux penguin without any encryption. The second image is the Tux bitmap, encrypted with a block cipher without<br \/>\nany feedback or transposition. <\/p>\n<p> So, given a message that consists of multiple blocks, you don&#8217;t want to just naively<br \/>\nencode them and combine them into a message. In fact, there&#8217;s no one answer to &#8220;How do you<br \/>\ngenerate the encrypted blocks and put them together to produce the ciphertext?&#8221; The answer<br \/>\ndepends on something called the ciphers <em>mode of operation<\/em>.<\/p>\n<p> I hate the term &#8220;Mode of operation&#8221;. It&#8217;s one of those terms that&#8217;s so mundane that it<br \/>\ndoesn&#8217;t sound like it describes something important or technical. It sounds like<br \/>\nIBM-manager-speak for trying to pad out a description, to make it sound deep. You know,<br \/>\nlike when a manager says &#8220;form-factor&#8221; instead of &#8220;size&#8221;. But it really is something very<br \/>\nimportant. There are two key functions performed by the mode of operation (hereafter<br \/>\n&#8220;MoO&#8221;): the first, I&#8217;ve already mentioned: re-assemble the separately encrypted blocks to<br \/>\nproduce a ciphertext message. The second is to provide an extra layer of security. Used<br \/>\nnaively, two identical blocks of cleartext will produce two identical blocks of<br \/>\nciphertext. That&#8217;s bad: it gives someone trying to crack the message a major clue about<br \/>\nthe structure of the message. The mode of operation can define some kind of additional<br \/>\nstate mechanism which will alter the ciphertext, so that even identical blocks of<br \/>\nplaintext will generate different blocks of ciphertext. <\/p>\n<p> The naive method of just encrypting each block separately and concatenating them is a<br \/>\nvalid mode of operation, called the electronic codebook (ECB) MoO &#8211; it&#8217;s just a lousy one<br \/>\nthat no one in their right mind would use, because it provides such pathetic security.<\/p>\n<p> As I get to real examples of block ciphers, I&#8217;ll describe more modes of operation, but I&#8217;ll use one here: the cipher-block chaining MoO. In cipher-block chaining (CBC) mode,<br \/>\nyou maintain a state vector the size of a block. You initialize that with something<br \/>\ncomputed from the encryption key. Then, for each block, you first do a bitwise exclusive-or of the <em>plaintext<\/em> block with the state vector. Then you encrypt the modified plaintext, and set the state vector to the value of the encrypted block. So<br \/>\nin CBC mode, the blocks are output in sequence, but the ciphertext of each<br \/>\nblock depends on the encryption key, the cleartext, and the ciphertext of the previous<br \/>\nblock.<\/p>\n<p> The selection of a mode of operation is critical for ensuring that the encryption<br \/>\nsystem works appropriately for your application. Selecting an appropriate MoO involves some fairly hairy tradeoffs between (among others):<\/p>\n<ol>\n<li> Ease of cracking;<\/li>\n<li> Ease of detecting errors<\/li>\n<li> Ease of detecting tampering<\/li>\n<li> Ease of detecting improper message duplication<\/li>\n<\/ol>\n<p> In particular, most of the basic MoOs can provide <em>either<\/em> message<br \/>\nintegrity (meaning that you can be confident that you got the complete message without<br \/>\nerrors or tampering), or message confidentiality (meaning that you can be confident<br \/>\nthat no one other that the intended recipient received the message). Basic MoOs<br \/>\ncan&#8217;t provide both.<\/p>\n<p> The first major standardized block cipher was called DES, for &#8220;data encryption standard&#8221;. It was a variation on one of the first published block ciphers, an IBM system called <em>lucifer<\/em>. In the next post, I&#8217;ll describe the DEA &#8211; the data encryption algorithm of the DES. It&#8217;s a pretty typical example of a block cipher. It&#8217;s fairly complicated, and so it deserves a post of its own to describe its<br \/>\nbasic mechanism, and its modes of operation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Where encryption starts getting really interesting, in my opinion, is block ciphers. Block ciphers are a general category of ciphers that are sort of a combination of substitution and transposition ciphers, and sort of something entirely different. They&#8217;re really fascinating things, but they&#8217;re pretty complicated. The basic core of block ciphers is encryption of blocks. [&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-678","post","type-post","status-publish","format-standard","hentry","category-encryption"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-aW","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/678","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=678"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/678\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}