{"id":6241,"date":"2018-04-11T18:37:10","date_gmt":"2018-04-11T18:37:10","guid":{"rendered":"https:\/\/davidgerard.co.uk\/blockchain\/?p=6241"},"modified":"2018-04-12T21:12:38","modified_gmt":"2018-04-12T21:12:38","slug":"javascript-securerandom-isnt-securely-random-most-web-wallets-affected-and-the-bug-was-warned-of-five-years-ago","status":"publish","type":"post","link":"https:\/\/davidgerard.co.uk\/blockchain\/2018\/04\/11\/javascript-securerandom-isnt-securely-random-most-web-wallets-affected-and-the-bug-was-warned-of-five-years-ago\/","title":{"rendered":"JavaScript SecureRandom() isn&#8217;t securely random \u2014 many old web wallets affected \u2014 and the bug was warned of five years ago (UPDATED)"},"content":{"rendered":"<p><b>Update:<\/b> The word &#8220;Most&#8221; in the original title of this post is incorrect &mdash; see details at end.<\/p>\n<hr>\n<p>Cryptography is profoundly unforgiving of errors. You don&#8217;t mess with it. You don&#8217;t roll your own \u2014 you need battle-hardened algorithms that have been torture-tested by the most technically ruthless cryptographers you can find.<\/p>\n<p>And you stop using old cryptographic algorithms that have <a href=\"https:\/\/en.wikipedia.org\/wiki\/RC4#Security\">known weaknesses.<\/a> And you fix this sort of thing <i>straight away<\/i>.<\/p>\n<p>Unless you&#8217;re in cryptocurrency, apparently.<\/p>\n<p>The popular JavaScript <tt>SecureRandom()<\/tt> library &#8230; <a href=\"https:\/\/lists.linuxfoundation.org\/pipermail\/bitcoin-dev\/2018-April\/015873.html\">isn\u2019t securely random.<\/a><\/p>\n<p>It will generate cryptographic keys that, despite their length, have less than 48 bits of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Entropy_(computing)\">entropy<\/a> \u2014 since JavaScript isn&#8217;t <a href=\"https:\/\/en.wikipedia.org\/wiki\/Type_safety\">type safe,<\/a> there&#8217;s a bug in <tt>SecureRandom()<\/tt> such that it fails to use the browser&#8217;s <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/crypto\">window.crypto<\/a> &#8230; so it falls back to the cryptographically insecure <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Math\/random\"><tt>Math.random()<\/tt>,<\/a> which is only <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pseudorandomness\">pseudo-random,<\/a> and can generate no more than 2<sup>48<\/sup> different values\u2014 so its output will have no more than 48 bits of entropy even if its <a href=\"https:\/\/en.wikipedia.org\/wiki\/Seed_(randomness)\">seed<\/a> has more than that (which is unlikely). <tt>SecureRandom()<\/tt> then runs the number it gets through the <a href=\"https:\/\/lists.linuxfoundation.org\/pipermail\/bitcoin-dev\/2018-April\/015881.html\">obsolete RC4 algorithm,<\/a> which is known to be more predictable than it should be, <i>i.e.<\/i> less bits of entropy. Thus, your key is more predictable:<\/p>\n<blockquote><p>The conclusion seems to be that at least all wallets generated by js tools inside browsers since bitcoin exists until 2011 are impacted by the Math.random weakness if applicable to the related implementations, the Math.random or RC4 (Chrome) weakness between 2011 and 2013, and RC4 weakness for Chrome users until end of 2015<\/p>\n<p>And all wallets using jsbn are impacted by Math.random and RC4 until 2013 (or end 2015 for Chrome), then still by the RC4 fallback step after<\/p><\/blockquote>\n<p>What that means is that the keys will be <a href=\"https:\/\/en.wikipedia.org\/wiki\/Entropy_(information_theory)#Limitations_of_entropy_in_cryptography\">predictable enough to crack<\/a> by sheer brute-force attack of computing power. You might think you have a key long enough that it can&#8217;t be cracked before the sun goes out \u2014 but it turns out you could do it in a week.<\/p>\n<p>So a\u00a0<em>lot<\/em> of browser-based cryptocurrency products that still use <tt>SecureRandom()<\/tt> are generating keys that are open to being cracked.<\/p>\n<p><strong>Check with your product&#8217;s author or vendor<\/strong> \u2014 there&#8217;s a real mess to clean up here.<\/p>\n<p>Like all good cryptocurrency bugs, this one isn&#8217;t new at all \u2014 here&#8217;s Greg Maxwell talking about it nearly <a href=\"https:\/\/www.youtube.com\/watch?v=TYQ-3VvNCHE&amp;feature=youtu.be&amp;t=3060\"><em>three years ago<\/em><\/a> (51:00 on):<\/p>\n<p>&nbsp;<\/p>\n<div class=\"jetpack-video-wrapper\"><span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"735\" height=\"414\" src=\"https:\/\/www.youtube.com\/embed\/TYQ-3VvNCHE?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-GB&#038;autohide=2&#038;start=3060&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span><\/div>\n<p>&nbsp;<\/p>\n<p>And here&#8217;s Bitcointalk user Ditto <a href=\"https:\/\/bitcointalk.org\/index.php?topic=150341.msg1598549#msg1598549\">talking about the RC4 bit in March 2013:<\/a> &#8220;Patch the window.SecureRandom function, or the ArcFour PRNG inside it.&#8221; And note the response:<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/davidgerard.co.uk\/blockchain\/2018\/04\/11\/news-uk-parliament-inquiry-friday-monero-and-ethereum-vs-asics-bitfinex-and-poland-ripple-tries-to-buy-xrp-onto-exchanges\/ditto-securerandom-march-2013\/\" rel=\"attachment wp-att-6238\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6238\" src=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/ditto-securerandom-march-2013.jpg\" alt=\"\" width=\"947\" height=\"142\" srcset=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/ditto-securerandom-march-2013.jpg 947w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/ditto-securerandom-march-2013-300x45.jpg 300w, https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2018\/04\/ditto-securerandom-march-2013-768x115.jpg 768w\" sizes=\"auto, (max-width: 947px) 100vw, 947px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><em>Why is cryptocurrency software like this.<\/em><\/p>\n<p>(you know why)<\/p>\n<hr>\n<p><b>Update:<\/b> EVERYBODY DON&#8217;T PANIC!! <i>(cough)<\/i> This basically affects you if you (a) use old cryptocurrency addresses (b) that were generated with JavaScript, <i>i.e.<\/i>, in a web browser. Move your funds out of those addresses and don&#8217;t use them again.<\/p>\n<p>Possibly affected:<\/p>\n<ul>\n<li>BitAddress pre-2013;\n<li>bitcoinjs before 2014;\n<li><b>current<\/b> software that uses <a href=\"https:\/\/www.mail-archive.com\/bitcoin-dev@lists.linuxfoundation.org\/msg06934.html\">old repos<\/a> they found on Github.\n<\/ul>\n<p>Recent JavaScript-generated addresses <i>probably<\/i> aren&#8217;t affected. But cryptographers consider JavaScript an inappropriate and imprecise language to use for such a critical and brittle use case as key generation. So don&#8217;t generate keys in JavaScript at all, and you should probably move your funds out of JavaScript-generated addresses anyway.<\/p>\n<p>Also, check what your web wallet&#8217;s author or vendor says about this one.<\/p>\n<br><br><div align=\"center\"><p><a href=\"https:\/\/www.patreon.com\/bePatron?u=8420236\"><img src=\"https:\/\/davidgerard.co.uk\/blockchain\/wp-content\/uploads\/2021\/10\/become_a_patron_button.svg\" alt=\"Become a Patron!\" title=\"Become a Patron!\" width=217 height=51><\/a><br><p style=\"align:center;\" class=\"patreon-badge\"><i>Your subscriptions keep this site going. <a href=\"https:\/\/www.patreon.com\/bePatron?u=8420236\">Sign up today!<\/a><\/i><\/p><\/div>","protected":false},"excerpt":{"rendered":"<p>If you used a web wallet to generate your keys, <i>check with your author or vendor<\/i>.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"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":""},"categories":[1],"tags":[131,575,574,576],"class_list":["post-6241","post","type-post","status-publish","format-standard","hentry","category-uncategorised","tag-greg-maxwell","tag-javascript","tag-rc4","tag-securerandom"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/6241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/comments?post=6241"}],"version-history":[{"count":30,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/6241\/revisions"}],"predecessor-version":[{"id":6351,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/posts\/6241\/revisions\/6351"}],"wp:attachment":[{"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/media?parent=6241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/categories?post=6241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidgerard.co.uk\/blockchain\/wp-json\/wp\/v2\/tags?post=6241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}