{"id":10961,"date":"2014-08-13T22:26:14","date_gmt":"2014-08-14T02:26:14","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=10961"},"modified":"2014-08-13T22:26:14","modified_gmt":"2014-08-14T02:26:14","slug":"screamingly-fast-hwrng-on-arduino-due","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2014\/08\/13\/screamingly-fast-hwrng-on-arduino-due\/","title":{"rendered":"Screamingly fast HWRNG on Arduino Due"},"content":{"rendered":"<p>Well, look at this:<\/p>\n<pre>$ stty -F \/dev\/ttyACM0 speed 115200 raw cs8\r\n$ rngtest -t 6 &lt; \/dev\/ttyACM0\r\n  \u00e2\u20ac\u00a6 much snippage \u00e2\u20ac\u00a6\r\nrngtest: bits received from input: 312368864\r\nrngtest: FIPS 140-2 successes: 15602\r\nrngtest: FIPS 140-2 failures: 16\r\nrngtest: FIPS 140-2(2001-10-10) Monobit: 2\r\nrngtest: FIPS 140-2(2001-10-10) Poker: 2\r\nrngtest: FIPS 140-2(2001-10-10) Runs: 8\r\nrngtest: FIPS 140-2(2001-10-10) Long run: 4\r\nrngtest: FIPS 140-2(2001-10-10) Continuous run: 0\r\nrngtest: input channel speed: (min=837.317; avg=1168.033; max=1948.060)Kibits\/s\r\nrngtest: FIPS tests speed: (min=16.834; avg=27.779; max=77.221)Mibits\/s\r\nrngtest: Program run time: 271917796 microseconds<\/pre>\n<p>Over a megabit\/second of decent quality random data. This is from an <a href=\"http:\/\/arduino.cc\/en\/Main\/ArduinoBoardDue\">Arduino Due<\/a>, which has an Atmel <span class=\"wikiword\">SAM3X8E<\/span> ARM Cortex-M3 microcontroller on board. I hadn&#8217;t found much use for this board previously, as it fell between a regular 8-bit Arduino and my (many!) Raspberry Pis.<\/p>\n<p>This changed when I found out about Walter Anderson&#8217;s <a href=\"https:\/\/sites.google.com\/site\/astudyofentropy\/project-definition\/timer-jitter-entropy-sources\/entropy-library\">Entropy<\/a> library, which uses \u00c2\u00b5c timer jitter as a source of entropy. Originally designed as a slow but true source of random integers on the Atmel AVR chips, it&#8217;s been extended to use the <span class=\"wikiword\">SAM3X8E<\/span>&#8216;s built-in hardware RNG. Since the Due has a native USB port, you&#8217;re not limited to standard baud rates.<\/p>\n<p>Here&#8217;s the code, trivially modified from one of Walter&#8217;s examples:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/ Generate_Random_Bytes_Due - speedy demo of Arduino Due's HWRNG\r\n\/\/ based on Generate_Random_Bytes, for Entropy, an Arduino library.\r\n\/\/ Copyright 2012 by Walter Anderson\r\n\/\/\u00c2\u00a0 modified - scruss - 2014-08-13\r\n\/\/ remember to reconnect to native USB port\r\n\r\n#include &lt;Entropy.h&gt;\r\n\r\nvoid setup() {\r\n\u00c2\u00a0 SerialUSB.begin(115200);\r\n\u00c2\u00a0 while (!SerialUSB) {\r\n\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 ; \/\/ wait for serial port to connect.\r\n\u00c2\u00a0 }\r\n\u00c2\u00a0 Entropy.initialize();\r\n}\r\n\r\nvoid loop() {\r\n\u00c2\u00a0 uint16_t r = Entropy.random();\r\n\u00c2\u00a0 SerialUSB.write(lowByte(r));\r\n\u00c2\u00a0 SerialUSB.write(highByte(r));\r\n}\r\n<\/pre>\n<p>It&#8217;s a minor pain to have to reconnect the USB cable to the other port on the Arduino Due after programming, but it&#8217;s worth it just to see an 84 MHz \u00c2\u00b5c belting out random bytes 37\u00c2\u00bd% faster than an 800 MHz <a href=\"http:\/\/scruss.com\/blog\/2013\/06\/07\/well-that-was-unexpected-the-raspberry-pis-hardware-random-number-generator\/\">Raspberry Pi<\/a> \u00e2\u20ac\u00a6<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, look at this: $ stty -F \/dev\/ttyACM0 speed 115200 raw cs8 $ rngtest -t 6 &lt; \/dev\/ttyACM0 \u00e2\u20ac\u00a6 much snippage \u00e2\u20ac\u00a6 rngtest: bits received from input: 312368864 rngtest: FIPS 140-2 successes: 15602 rngtest: FIPS 140-2 failures: 16 rngtest: FIPS 140-2(2001-10-10) Monobit: 2 rngtest: FIPS 140-2(2001-10-10) Poker: 2 rngtest: FIPS 140-2(2001-10-10) Runs: 8 rngtest: FIPS [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2],"tags":[2207,2828,2510,2669],"class_list":["post-10961","post","type-post","status-publish","format-standard","hentry","category-goatee-stroking-musing-or-something","tag-arduino","tag-entropy","tag-raspberrypi","tag-rng"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-2QN","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/10961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=10961"}],"version-history":[{"count":5,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/10961\/revisions"}],"predecessor-version":[{"id":10966,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/10961\/revisions\/10966"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=10961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=10961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=10961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}