start	end	text
1350	11430	Hello, my name is Mike Stay. I'm the CTO of Pyrefx Corporation, and today I'm going to tell you about how we recovered several hundred thousand dollars worth of Bitcoin from an encrypted zip file.
12950	20170	Around 20 years ago, I was working as a reverse engineer and cryptanalyst for AccessData while getting my physics degree at BYU.
20590	23250	It was the late 90s, early 2000s.
23250	32970	The US government had been gradually easing off export restrictions on software containing cryptography, but most software still contained pretty worthless password protection.
33130	40410	We'd buy desktop office software, I'd reverse engineer it to figure out what algorithm it was using for access control, and then break the crypto.
40450	44970	It was a never-ending stream of interesting but not impossible math puzzles.
44970	48710	I wrote about 40 password crackers during my time there.
48870	54230	We would sell them to home users, system administrators, local and federal law enforcement agencies.
54490	64370	I got to go down to the Federal Law Enforcement Training Center in Glynco a few times to teach secret service agents, FBI agents, ATF, about cryptography and the use of our products.
65250	67710	Two of the projects really stand out in my memory.
67710	70370	The first was Microsoft Word 97.
70950	78210	Before Word 97, the files were encrypted by XORing the bytes with a repeating 16-byte string derived from the password.
78490	84470	The most common bytes in a Word file were either 0, 255, or 32, which is space.
84470	94570	So we'd just look at the most common character in each of the 16 columns, try the 3 to the 16th variations on those, and recovering the key was usually instantaneous.
94570	107090	But to help people feel like they'd gotten their money's worth, we'd put on a little show like the scene in War Games, where it would be animated, showing random characters that would gradually reveal the right password.
107990	110610	Microsoft 97 changed that.
110910	118710	It might have been possible to find out the encryption format through Microsoft Developer Network, but we were a small company and couldn't afford the subscription.
118850	123650	It also wasn't clear we'd be allowed to write the cracker if we did get the info from them.
123650	127370	So to figure out how it worked, I used softdice.
127810	132150	There was a button on a cable that would go to a card in the computer.
132170	143430	I'd type the password, and then hit enter and the button on the cable as fast as I could, and hope that it would break somewhere in the crypto stuff.
143650	148830	I'd walk up the stack, trying to figure out where the algorithm was.
148830	165250	This was in the days before IDA Pro, so I printed out a few dozen pages of assembly code and taped it to the wall, and then drew all over it with a red crayon, updating the function names and so on as I figured stuff out.
165250	167790	I was very pleased when I finally worked it all out.
167990	174550	At the time, Microsoft was only allowed to export 40-bit cryptography, so they did as much as they were legally permitted to do.
174550	188710	They'd repeatedly MD5 the password with some salt, which is randomly chosen bytes that is stored in the file, and then they'd get a 40-bit key, then they'd add salt to that and repeatedly hash it again.
188710	193090	So it took about half a second to test a password on the computers at the time.
193090	198290	So despite being a 40-bit key space, it was a fairly hard problem to attack.
198770	203550	We had to resort to a dictionary attack, because breaking it outright was pretty much impossible.
204550	222770	We did eventually write a cracker for larger companies that had their own computer labs and resources, other large agencies, to brute force the 40-bit key space using the fancy MMX instructions on the Pentium.
222810	227590	This was long before graphics processors were available.
227590	232670	I knew of one place that ran the software for nine months before finally getting in.
232670	237090	The other really fun one I did was Zip Archives.
237090	246150	The developer of PKZip, Phil Katz, made the decision, which was unusual at the time, to document his file format and include it with his software.
246670	249090	That made it a favorite of developers.
249090	252730	Roger Schlafly designed the stream cipher used for encrypting the archives.
252730	257790	The Zip standard quickly became by far the most popular compression format on Windows.
257790	267010	And many other formats, like Java's JAR format, OpenOffice's document formats, are really just Zip files with a particular directory structure inside.
269370	273490	InfoZip is an open source implementation of the software.
273490	278250	It was used as the basis for nearly all other branded Zip software, like WinZip.
278250	285030	And at the time I was trying to crack it, WinZip had 95% of the market, according to CNET downloads.
287210	293150	Eli Biem and Paul Kosher had published a known plaintext attack on the cipher.
293150	296490	But the known plaintext was compressed plaintext.
296490	303390	To get the Huffman codes at the start of a deflated file, you basically need 32 kilobytes of the file.
303870	308210	The attack was practically useless to law enforcement agencies.
308250	314690	This diagram is an illustration of the internal workings of the Zip cipher.
314690	318570	Each of these boxes represents a byte, 8 bits.
318670	324210	The cipher has 96 bits of internal state, split into three 32-bit chunks.
324210	328610	One is called Key0, Key1, and Key2.
328610	339150	This subscript indicates the state of that chunk after having processed that many bytes of text.
339250	347710	So here, this is the initial state. We feed in the first byte of text, and we get the subsequent state of Key0.
347710	352990	Then we take part of that and feed it in here, and we get the subsequent state of Key1.
352990	357750	Then we take one of these bytes and feed it in here, and get the subsequent state of Key2.
357750	363870	Then we do one more operation on these two low bytes to get the stream byte.
364870	370570	Going into a little more detail, this first section is CRC32.
370570	373790	CRC stands for Cyclical Redundancy Check.
373790	381090	It was designed for detecting errors on the circular tracks that you would find on floppy disks.
381370	385230	So here we can see the shift, it's being shifted right 8 bits.
385230	393210	We take these 8 bits that got shifted off the bottom, and use them to look up a 32-bit word in this table.
393370	399430	Then we take the byte that's coming in, and we also look up a 32-bit word for that one in the table.
399430	409350	Then we XOR the high 24 bits here, that were left after shifting it down, with these two words, and we get the new state of Key0.
410150	423130	We take the low byte of the new value of Key0, add it to the current state of Key1, multiply by this constant, add 1, and get the new state of Key1.
423770	427590	So this is called a truncated linear congruential generator.
427590	433070	Truncated because we're only taking the high byte as output.
433070	435850	Linear because it's addition.
436290	439710	Congruential because it's modulo 2 to the 32.
439710	446290	And generator because it's spitting out pseudo-random bytes.
446290	451450	So it takes this byte that it's spitting out and feeds it into CRC32 again.
451450	462610	Now, this is a linear feedback shift register, but this one is linear with respect to XOR, while this one is linear with respect to addition, with carries.
462610	468430	So they don't work well together, and that's what really gives it whatever strength it does have.
469730	478510	So, CRC32, truncated linear congruential generator, another CRC32, and then this pseudo-squaring operation.
478510	483250	If you look in the source code, the pseudo-squaring operation is like this.
483250	492390	You take Key2 and the low 16 bits of that, and you OR it with 2, so you set bit 1.
492790	496910	And then you multiply that by itself XOR1.
496910	500050	So one of the values is going to be even and the other one's odd.
500050	503670	You shift it down by 8 and take 8 bits.
503670	509670	So it's the middle of this pseudo-squaring operation.
509710	514390	And the result is a stream byte.
514390	525650	Now, the way this stream cipher works is that you initialize it first with a password, and then you encrypt the plaintext that you want to encrypt.
525690	530610	To make it harder, they encrypt some salt first.
533530	542170	PKZip used bytes that were in memory, so it would allocate 10 bytes and use whatever bytes were there.
542170	544010	That's how it got its entropy.
545850	556130	InfoZip, because it was built for use on Linux as well as Windows, couldn't do that because when the memory was allocated, it was also initialized.
556130	560110	So they got their entropy from the process ID and the timestamp.
560110	566470	You'd XOR those two together and call srand on it to seed the random number generator.
566470	572570	Then it would use rand to generate 10 bytes as salt.
572730	578090	In the file, then you'd follow it by CRC32 and the plaintext.
578290	584450	Now, BM and Kosher had their attack, and it was published when InfoZip was implemented.
584450	595750	So they knew that if they just used rand, then if somebody had the timestamp and the process ID, they could compute these bytes.
595850	600230	The CRC was stored in the file to check that the password was entered correctly.
600230	607650	So they would know these bytes, and that would give them 12 bytes, which was sufficient to mount a known plaintext attack.
607650	610550	So InfoZip tried to avoid that.
611450	614430	And they made it harder, but not quite hard enough.
617520	625200	So renaming these bytes individually, R0 through R9 are the bytes generated by rand.
625200	628260	C0, C1 are the CRC32 bytes.
628420	633860	And P0 through P3 and so on are the plaintext bytes.
633860	635920	And I'll call this array X.
636940	644220	Now, when you feed the password into the cipher, it treats it as though it's plaintext.
644220	650660	You feed in the first password byte, it updates key 0, key 1, key 2, spits out a stream byte.
651160	658680	And then it throws the stream byte away until it gets to the last character of the password.
658760	664700	And that final stream byte is used to encrypt the first salt byte, R0.
665540	676240	So I get this set of 10 bytes that I call Y, which is just these first 10 bytes generated by rand,
676240	680140	exclusive word with the 10 bytes generated by the stream cipher.
680440	685600	Now you'll notice here I call this S0, but this one is S1X.
685600	697600	These ones that end in X depend on the values in X, but S0 only depends on the password itself.
698180	705760	That turned out to be their tragic flaw, that when they encrypted this a second time,
705760	710420	so they were using Y as the salt bytes in the file,
711420	717200	they would generate S0 again because it was the same password.
718300	729120	So they would XOR with S0 once to get the salt byte, XOR with S0 again to get the byte that was stored in the encryption header.
729120	741180	Now all of these are hard to figure out because they depend both on the randomly generated bytes from rand,
741180	745280	as well as the stream cipher bytes, twice over.
745280	747480	So this is doubly encrypted stuff.
747480	756940	But this first byte leaked, and given five files in the archive, all encrypted with the same password,
756940	770920	this gave me eight bits of entropy per file to find the 32-bit internal state of the rand pseudorandom number generator.
771920	779260	So given five files, I could simply take the first byte of each archive,
780160	792880	run through all 32 bits of possible internal state, and verify that every tenth byte gave me the byte I was expecting.
792880	801440	So I could uniquely identify the internal state, could uniquely find these ten bytes.
801440	804980	And so then I had some place to stand to mount the attack.
804980	813960	So the next thing I did was notice that I didn't need all 96 bits to produce the next stream byte.
814460	817140	I only needed 40 bits.
817140	828560	So here I didn't need to know both key 0 and... or rather the second and the first byte of key 0.
828560	831900	I only needed to know the XOR of this byte with that one.
831900	833680	So that's an 8-bit guess.
833680	839680	I know the value from rand that's going in, so I can predict this thing.
842080	845080	Here I'm multiplying by this constant.
846840	857960	If I take the... I don't need to know the entire value of key 1, I just need to know the high byte of key 1 times the constant.
857960	861820	So I've distributed this multiplication across the addition we had earlier.
861820	864040	So here is the high byte of that.
866860	871500	And when I add 1, it might cause a carry bit, so I have to guess a carry bit.
871640	873540	Then it comes in here.
874880	877580	I know what the high byte of this is.
878280	882140	I feed it in. I just need to know these values.
882140	891300	And given the stream byte, I can then figure out what the low 6 bits are here that are needed.
891820	899540	This is how to figure out, given a stream byte, well, a plain text and a cipher text pair.
899540	907040	These are the bits that you have to guess in order to get this stream byte.
907040	922460	So what I would do is I would guess 40 bits, which would allow me to process this byte here.
922760	929920	And so then I could check for each of the five files, does this value match what I expect it to?
929920	941980	And since I had 40 bits that I was guessing and 40 bits of entropy to compare it to from the file, I could filter out all but one guess.
942160	951760	And then for the next byte, I would guess 26 more bits and then just a few more bits to get the rest of the state after that point.
951760	958740	Usually, once I found this 40-bit one, the next one I could get within a few minutes.
958740	969780	The whole attack ran in about two hours on a Pentium at the time, cipher text only attack.
969780	977820	I was able to break into these zip files. We caught several child pornographers that way.
978900	980960	And I got a paper out of it.
980960	996380	So in 2001, fast software encryption, I got this paper and that led 20 years later to a surprising message.
996380	1009200	So in October of last year, a guy contacts me out of the blue and says, I read your paper on known plaintext attacks and I've got this password that I've forgotten.
1009200	1014300	Is there anything you can do to help? Can you tell me how things are looking these days?
1014300	1022880	And so I looked it over, turns out he only had two files in the archive.
1022880	1037100	With only two files, I didn't have enough information to derive the internal state of RAND and I didn't have enough information to filter the guesses I was making.
1037100	1043080	So far more guesses would pass each filtering stage.
1043080	1051740	And it worked out to be something like, on my back of the envelope calculations, something like 2 to the 70.
1051740	1058020	So I told him, you know, compare the work done this year to find hash collisions in SHA-1.
1058160	1065080	SHA-1 has 128-bit output, so to find a collision, you've got to look at 2 to the 64th hashes.
1065080	1072200	That cost around $100,000. This is not something that you can use off-the-shelf software for.
1072620	1077440	And then he blew my mind and said, I could spend so much on this archive.
1078200	1085700	At that point I knew that he probably had several hundred thousand dollars of Bitcoin in this thing.
1087260	1091080	At this point I started doing a more careful analysis.
1091080	1096280	There are 96 bits of key material that need to be guessed in various stages.
1096440	1099540	But I can't guess the bits of key material directly.
1099540	1109300	Instead I have to guess bits of a function of the key material and use those constraints to limit what the key material could be.
1110260	1117120	This first part is CRC32 of the initial key 0 value with the 0 byte.
1117120	1129580	Since CRC32 itself is invertible, once I guess these 4 bytes, there's a very simple linear operation I do to recover the initial key 0 value itself.
1129920	1132920	With key 1 it's a little more complicated.
1132920	1142340	We take the initial key 1 value and multiply it by this constant from the truncated linear congruential generator to the nth power.
1142780	1146440	And I'm guessing the high byte of that value.
1146440	1156720	Given 4 of these, that'll put enough constraints on key 1 that I can brute force it and figure out what key 1 is given those 4 bytes.
1156760	1160560	The initial value of key 2 I can guess directly.
1161440	1168840	So in the first stage I guess these 5 bytes, that's 40 bits, and I guess 4 carry bits.
1168840	1192460	These carry bits come from the possibility that when I add 1 in the truncated linear congruential generator, it carries over all the way to the 24th bit, which would change the value of this most significant byte.
1192460	1203700	So I have to guess these carry bits. I need to guess 2 for each file, there are 2 files, because I have 2 passes of encryption that I do to each byte.
1203740	1209160	So 2 passes of encryption, 2 files, makes 4 carry bits that need to be guessed.
1209160	1220800	So the total work for this stage is 2 to the 44th, and after filtering the 16 bits from the file, I get 2 to the 28th key guesses passing.
1220800	1228720	In stage 2 I guess these 3 bytes, that's 24 bits, 4 more carry bits makes 28.
1228900	1238960	Added to the previous 2 to the 28th key guesses, I have 2 to the 56th keys to filter. And after filtering, 2 to the 40th remain.
1239900	1254200	In stage 3 I guess 16 bits plus 4 carry bits makes 20. Previous 40 carried forward gives us 2 to the 60th keys to test. And after filtering, 2 to the 44th pass the third stage.
1255740	1269700	In the fourth stage, I guess 16, as before, 4 carry bits gives us 20. 44 from the previous stage, 20 now makes 64. This was the most complicated stage, 2 to the 64th.
1269760	1272440	After filtering, 2 to the 48th remain.
1272440	1291420	Now in my original attack, this is where I would stop. I have all the key material. With these 4 bytes, I can brute force key 1, run through the 2 to the 32 possibilities, and get the value itself. I can invert this and that would work.
1291420	1309560	But at this point it struck me, if I have to do 2 to the 32 work for each of these 2 to the 48 keys, that will give a total cost of 2 to the 80th. There is no way we could do a 2 to the 80th attack.
1309560	1321950	So I had to come up with a special approach, a new idea to make this step much less expensive.
1323000	1330740	I remembered having read somewhere about an attack on truncated linear congruential generators that uses lattice reduction.
1330740	1345660	Lattice reduction is the idea that if you have a lattice, which is like a vector space but only integer coordinates, and a basis for that lattice, then you can find a nicer basis.
1345820	1350540	Where nicer means that the vectors are shorter and closer to orthogonal.
1350540	1358640	So here I have a two-dimensional lattice. Here's a basis for that lattice, where one of these goes here and another goes there.
1358640	1367580	Every point on this grid has a coordinate in terms of how many steps of this kind and how many steps of that kind it takes to get there.
1368000	1373680	But lattice reduction gives you a much nicer basis. These are both shorter and closer to orthogonal.
1376340	1389880	Lattice reduction is in general a hard problem. About half of the candidates for the post-quantum cryptography ciphers that NIST is considering right now use lattice reduction, for example, NTRU.
1389880	1397780	But in small cases, it's tractable. And I had a very small case, only four or five dimensional.
1397780	1405160	So I went and poked around on the cryptography stack exchange and found exactly what I needed.
1405640	1415200	The vectors, in this case, the basis vectors were powers of the constant from the truncated linear congruential generator.
1415200	1420880	In our case, it was 08088405 in hex.
1422580	1432720	So you take the, if we call that C, then C and C squared and C cubed and C to the fourth and so on become basis vectors for this lattice.
1432720	1439420	And then you add in one more basis vector for the modulus, which in our case was two to the 32, here called M.
1439420	1451200	So the idea was that given the four most significant bytes of the key one value times this consecutive powers of C,
1451200	1462100	I would do a linear transformation, get a value that was close to a basis vector in this small vector basis,
1462100	1467880	then round off to get it exactly to a basis vector and then transform back.
1467880	1473160	That would give me the exact value of key one zero.
1473340	1478520	So I wrote up a Sage program to figure out how exactly that would work.
1479320	1485000	Sage is a library for Python that lets you do linear algebra, among many other things.
1486160	1489860	So here we have the modulus, two to the 32.
1490120	1494060	We have the constant from the truncated linear congruential generator.
1494720	1500040	We have the matrix that defines the basis for our four dimensional vector space.
1500400	1510300	Now the vectors themselves are the modulus and the powers of the constant with minus ones on the diagonal.
1510580	1513260	This does the lattice reduction step.
1513260	1520180	Now to test how B worked, I would generate a random initial k1 value,
1520180	1532120	then compute the actual powers of C times k1, modulo m, that I was going to try to recover.
1532120	1533600	So I'd print these out.
1534100	1537120	The most significant bytes were the things that I had guessed.
1537120	1541040	I wanted to recover k's from the MSBs.
1541040	1550700	So the secret stuff that I needed to know were the low 24 bits, the part that I had masked off up here.
1550700	1555700	So this is the secret 24 bit values that I need to guess.
1555920	1561520	Given the vector that is the most significant bytes, I multiplied it by B,
1561520	1566360	which switched from one lattice to the other, did a basis change.
1566360	1574380	Then I rounded to the nearest vector in this nicer basis.
1574520	1583800	And that meant that I would get an actual value of k10 that would produce those same most significant bytes.
1584380	1590200	Then I reversed that, transformed back, and got the guess.
1590200	1591960	And I'd print it out.
1591960	1595520	And when I started doing this, the guess was never right.
1596360	1598720	Or rarely right.
1599220	1605180	And that was because of the limited number of guesses I had here.
1607900	1617500	If I could do one more stage, then I would be able to get the right answer every time.
1617500	1624600	But here I had too few powers of C to find out what it was exactly.
1624600	1629890	But then I started checking the difference between the guess and the actual value.
1630540	1637660	So I take the keys, subtract off the MSBs, and the difference between the guess and the actual one
1642250	1648290	turned out to fall into one of 36 possibilities every time.
1648290	1652630	So as I thought about it, I realized that in this four dimensional space,
1652630	1658030	by truncating it and just using the most significant bytes, I was adding a bunch of noise.
1658350	1666350	And so the point in the four dimensional space I wanted was at the center of a cell.
1666350	1675710	And what I was actually getting was a point near the hull of this cell that tiled this four dimensional space.
1675710	1681550	And so because it was always within this cell,
1681550	1687550	there were only a finite number of other possible lattice points that it could be.
1688350	1695090	And so I computed for all of the two to the 32 possible K10 values,
1695430	1703750	what the set of guesses were, and found that they fit into this one of 36 classes.
1703750	1708930	And so instead of having to try out all two to the 32 possible key ones each time,
1708930	1714870	I would only have to do 36. So instead of four billion, 36 values.
1714870	1717090	And that made the attack feasible again.
1718270	1723770	Once we'd guessed all the key material, we could filter the two to the 48 keys at stage four
1723770	1727190	by using the remaining bytes in the encryption header.
1727770	1734770	My business partner, Nash Foster, then started working on adapting my CPU based attack to run on GPUs.
1734770	1738350	He wrote the code harness for getting code and data onto the GPUs,
1738350	1742310	did all the CUDA stuff, advised me on how to structure the attack.
1742730	1749210	We discovered very quickly, though, that getting the petabytes of possible keys onto the GPU would take too long,
1749210	1753210	that the GPUs would just be sitting idle for most of the time we were paying for them,
1753210	1756210	waiting for the data to arrive to process.
1756210	1758330	So I went back to the drawing board.
1758470	1762290	In each stage, I was guessing a whole bunch of bits and then filtering those results
1762290	1766110	using the two bytes from the two archives in the file.
1766350	1770170	I'd only keep about one out of 65,000.
1770250	1774030	If I had some way of using that information to derive bits,
1774030	1777730	rather than just guessing and checking, it'd save a lot of work,
1777730	1779930	and more importantly, a lot of network traffic.
1780090	1784410	The problem with that idea was that the math is too complicated.
1784410	1788250	It involves mixing finite fields with integer rings,
1788250	1790990	and those simply don't play well together.
1791130	1794290	So I thought about some other cryptanalytic attacks I knew,
1794290	1797690	and one that seemed promising was a meet-in-the-middle attack.
1798330	1802570	A meet-in-the-middle attack usually applies to block ciphers,
1803230	1807850	when it uses one part of the key material to do the first half of the encryption,
1807850	1811810	and the second part of the key material to do the second half of the encryption.
1812830	1822170	The reason we have triple DES instead of double DES is precisely because of this.
1822170	1828650	Triple DES is a 112-bit cipher, even though there are three keys involved.
1829690	1832950	You can do encrypt, and then decrypt, and then encrypt.
1832950	1835190	So 112 bits of strength.
1835190	1839690	If it's 112 bits, why not just do DES with one key,
1839690	1842290	and then DES with another key, encrypting it twice?
1842290	1844210	Well, the answer is right here.
1844210	1849070	What you do is you take your plaintext-ciphertext pair,
1849070	1854910	and then encrypt the plaintext under all 256 possible first DES keys.
1855430	1861390	And then you take the ciphertext and decrypt it under all possible 256 DES keys.
1861390	1866510	And right here in the middle, chances are that one of them will match.
1866510	1871210	And so you sort all of these intermediate plaintexts,
1871210	1878190	and whenever you find two intermediate texts that are the same,
1878190	1882090	one of them came from the plaintext and one came from the ciphertext,
1882090	1887130	and that uniquely identifies the key that should go between these two.
1887130	1891590	So a meet-in-the-middle attack works when you've got key material
1891590	1899210	that is not all used at every stage of the cipher.
1899210	1902190	And in this stream cipher, that seemed to be the case, right?
1902190	1907150	I had only key 0 at the beginning and only key 2 at the end,
1907150	1910630	and it seemed like we could do something in the middle,
1912230	1914310	but it wasn't quite right.
1914310	1917570	But then I realized I could use the XOR of certain bytes
1917570	1920310	in the middle of the cipher to mount the attack.
1921470	1924550	Here's a diagram of the cipher again.
1924550	1929590	And the cipher gets called four times in each stage for each byte,
1930370	1935630	two for each file, the double encryption of the byte from rand.
1936110	1939590	So in the center of the cipher,
1939590	1943870	there are four different most significant bytes for key 1
1945390	1948030	after each of these encryptions.
1948090	1952770	So what I did was take the first of those four
1952770	1955670	and exclusivore it with each of the other three
1955670	1959350	to get a 24-bit key into a table.
1959350	1963690	And under that key, I would store my guess for these bits.
1964910	1969370	Then from the other side, given a stream byte,
1969590	1973990	there are 64 possible 14-bit values here.
1973990	1978410	So I would guess some intermediate stream bytes,
1978410	1982990	six bits of this, I guess it's boxed here,
1982990	1986850	these six bits to figure out what the other eight are.
1986850	1990130	And that would allow me to derive linearly from this stuff
1990130	1995750	what this most significant byte of key 1 was again.
1995750	1999930	And so given these bits that I was guessing,
1999930	2005190	I would derive a key and store these bits under that key.
2005230	2008090	And then anytime I had a collision,
2008090	2015410	I would know that the set of bits here that I guessed
2015410	2019730	were consistent with the set of bits here that I guessed
2019730	2025130	to give this plaintext going to that stream byte.
2026330	2033750	Now that is far smaller than running through
2033750	2036340	all of the possibilities and then filtering.
2037010	2043590	The amount of memory it required was on the order of a few megabytes.
2043590	2046430	So here is the code itself.
2046730	2050990	This is stage 1a moving forward through the cipher.
2050990	2054750	We guess the initial stream byte,
2054750	2057430	because it was XORed with itself twice,
2057430	2060470	we have no information about it, so we would guess that one.
2060470	2065250	Then we'd guess chunk 2, which came from CRC 32 of key 0.
2065250	2069150	Chunk 3 is the most significant byte of key 1.
2070210	2073390	Here are the carry bits.
2073570	2076770	There are two bits for each of the two files,
2076770	2080850	so 2 to the 4th is 16 possible carry bits.
2083790	2090050	This is debugging code for checking against files that we created ourselves.
2091410	2094270	This is the first half of the cipher,
2094270	2102450	and it updates an upper and a lower bound on the possible low 24 bits of key 1,
2102450	2106850	so that if it ever goes out of bounds, we can throw it away.
2108610	2113850	We compute various bytes, get through here.
2113850	2117450	If it has passed all of these upper and lower bound checks,
2117450	2122370	then this converts those four most significant bytes to a map key
2122370	2129270	and stores the candidate under that map key.
2129830	2131470	In the second half,
2132290	2139970	we're guessing the stream1x byte for file 0.
2140290	2143230	We're guessing the 64-bit prefix,
2143230	2151710	because each stream byte has 64 possible pre-images under the pseudo-squaring operation.
2152270	2155390	So these are all of the indexed pre-images,
2155390	2159530	and this gets the specific pre-image that we're caring about.
2160090	2164070	There's more debugging information. Again, we do the second half step.
2164070	2171990	And if there are no consistent results, then we just go to the next guess.
2172010	2177190	Here we're guessing the stream1x pass for file 1.
2177630	2179870	We do that second half step.
2179870	2183990	And again, if there are no consistent ones, we go back to the next guess.
2184830	2191310	Finally, we compute the stream1y bytes.
2193010	2197950	And if there are no consistent ones with that prefix, we continue.
2197950	2204310	So now given each of the possibilities for the first and second and third bytes,
2204310	2206990	we put them together into a map key,
2209130	2212370	and then meet in the middle.
2214020	2217320	Here is the CRC20.
2218480	2223900	If you'd like to look at the code, you can see how these exclusive work together,
2223900	2225860	and we can derive some certain things.
2225860	2235240	So starting at the bottom, we derive bits 15 to 2 of K and L from 15 to 2 of S and T,
2235240	2239180	and these bytes OP that we've computed.
2239180	2248130	So in the center, we find those groups that...
2249130	2251430	those guesses that work,
2252130	2258410	and we push back that guess whenever the two match up with each other.
2258410	2260290	And here's some more debugging information.
2260650	2264650	So that's the idea of the differential meet in the middle attack,
2264650	2267450	is computing forwards and backwards,
2267450	2270850	exclusive oring one of the bytes with the other three,
2270850	2274490	and then whenever the two guesses match,
2275090	2280650	we have a set of candidates that's a product of the set from the first half of the attack
2281250	2283790	with the set from the second half of the attack.
2283790	2287930	Using the differential meet in the middle attack on stage 1 and stage 2
2288310	2293170	allowed us to reduce the complexity of the attack in stage 1 from 2 to the 40th
2293170	2295030	down to 2 to the 22nd.
2295030	2298730	That's from trillions down to mere millions.
2298730	2304290	And then in stage 2 from 2 to the 56th down to 2 to the 40th.
2304290	2311210	And that let us run stages 1 and 2 on each of the machines that had GPUs attached to it.
2311210	2316650	We completely eliminated the need to ship batches of keys over the network.
2316650	2319110	And so we could generate the keys in place
2319110	2325110	and then run what we called the GPU stage 3 kernel that did all the rest of the attack.
2325550	2327750	Now we ran it for 10 days.
2328310	2333630	We had tried it on all of our test archives that we'd created.
2333630	2334910	It worked fine.
2336190	2340870	10 days passed and it didn't find a key.
2340870	2343010	We were distraught, pulling our hair out.
2343010	2344230	What have we done wrong?
2344230	2350690	We went back and discovered that there were a few other process IDs that could have worked.
2350990	2354930	And we were wondering, oh no, are we going to have to do this four more times?
2356290	2360890	But then our client, who was a programmer himself,
2360890	2369830	discovered that there was a bug between the GPU stage and the CPU stage.
2369830	2373470	So when I had run the tests, I had done it locally on my computer
2374810	2377250	using the CPU version.
2377270	2380390	And the test, we knew exactly where the key had to be,
2380390	2385010	so it was a very small piece of key space that we had to check.
2385310	2390910	But when my client went back and tried it again using the GPU stage 3,
2390910	2397010	he discovered that when the key candidate was the first one in a list,
2397010	2400810	it succeeded, but when it was the second one, it failed.
2401250	2404210	And that led me to this line right here.
2404230	2408810	We had swapped the thread index with the block index.
2408810	2415110	So instead of incrementing by one into the current block,
2415110	2420550	it would increment the block number and go off into space that was uninitialized,
2420550	2422420	and so it would never work.
2423010	2426910	So by switching the block index with the thread index,
2426910	2428910	we started the search over again.
2428910	2432010	Within a day and a half, we had found the three keys
2432010	2435250	that decrypted the archive,
2435250	2440230	and our client was able to get his Bitcoin keys out.
2441350	2445010	In the end, the improvements that I made to my old attack
2445910	2450310	took it from something that we estimated at approximately $100,000
2451610	2454090	and a year of processing,
2454090	2458470	down to something that took about $10,000 of GPU time
2458910	2460990	and a little under two weeks.
2461710	2465370	Our client was very pleased and gave us a big bonus,
2465370	2468110	and that's how we recovered his Bitcoin core.
2468230	2469250	Thank you very much.
