{"action":"created","comment":{"url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/comments/83790311","pull_request_review_id":4609119,"id":83790311,"diff_hunk":"@@ -14,125 +14,153 @@\n  * limitations under the License.\n  */\n #include <stdint.h>\n-#include <stdlib.h>\n-#include <stdbool.h>\n #include <limits.h>\n #include \"randLIB.h\"\n #include \"platform/arm_hal_random.h\"\n \n-#if ((RAND_MAX+1) & RAND_MAX) != 0\n-#error \"RAND_MAX isn't 2^n-1 :(\"\n-#endif\n-\n /**\n- * This library is made for getting random numbers for Timing needs in protocols.\n+ * This library is made for getting random numbers for timing needs in\n+ * protocols, plus to generate dynamic ports, random IDs etc.\n  *\n  * **not safe to use for security or cryptographic operations.**\n  *\n+ * Base implementation is a pseudo-RNG, but may also use a system RNG.\n+ * Replay of sequence by reseeding is not possible.\n+ *\n+ * Base pseudo-RNG is the xoroshiro128+ generator by Marsaglia, Blackman and\n+ * Vigna:\n+ *\n+ * http://xoroshiro.di.unimi.it/\n+ *\n+ * Certainly not the fastest for 32-bit or smaller platforms, but speed\n+ * is not critical. None of the long operations in the core are actually hard,\n+ * unlike the divisions and multiplies in the utility functions below, where we\n+ * do try to keep the operations narrow.\n  */\n \n+/* On some platforms, read from a system RNG, rather than use our own */\n+/* RANDLIB_PRNG disables this and forces use of the PRNG (useful for test only?) */\n+#ifndef RANDLIB_PRNG\n+#ifdef __linux\n+#define RANDOM_DEVICE \"/dev/urandom\"\n+#endif\n+#endif // RANDLIB_PRNG\n+\n+/* RAM usage - 16 bytes of state (or a FILE * pointer and underlying FILE, which\n+ * will include a buffer) */\n+#ifdef RANDOM_DEVICE\n+#include <stdio.h>\n+static FILE *random_file;\n+#else\n+static uint64_t state[2];\n+#endif\n+\n+#ifdef RANDLIB_PRNG\n+void randLIB_reset(void)\n+{\n+    state[0] = 0;\n+    state[1] = 0;\n+}\n+#endif\n+\n+#ifndef RANDOM_DEVICE\n+static inline uint64_t rol(uint64_t n, int bits)\n+{\n+    return (n << bits) | (n >> (64 - bits));\n+}\n+#endif\n \n-/**\n-  * \\brief Init seed for Pseudo Random.\n-  *\n-  * \\return None\n-  *\n-  */\n void randLIB_seed_random(void)\n {\n-    uint32_t rand_seed;\n+#ifdef RANDOM_DEVICE","path":"source/randLIB.c","position":76,"original_position":76,"commit_id":"9d3579838c025d928c0f77e70f7df4f5a64e1c5f","original_commit_id":"9d3579838c025d928c0f77e70f7df4f5a64e1c5f","user":{"login":"kjbracey-arm","id":11025917,"avatar_url":"https://avatars.githubusercontent.com/u/11025917?v=3","gravatar_id":"","url":"https://api.github.com/users/kjbracey-arm","html_url":"https://github.com/kjbracey-arm","followers_url":"https://api.github.com/users/kjbracey-arm/followers","following_url":"https://api.github.com/users/kjbracey-arm/following{/other_user}","gists_url":"https://api.github.com/users/kjbracey-arm/gists{/gist_id}","starred_url":"https://api.github.com/users/kjbracey-arm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kjbracey-arm/subscriptions","organizations_url":"https://api.github.com/users/kjbracey-arm/orgs","repos_url":"https://api.github.com/users/kjbracey-arm/repos","events_url":"https://api.github.com/users/kjbracey-arm/events{/privacy}","received_events_url":"https://api.github.com/users/kjbracey-arm/received_events","type":"User","site_admin":false},"body":"Could do.\r\n\r\nCurrent API, which I wanted to preserve was that entropy was \"pulled\" by calling arm_random_seed_get(), and this change modifies it so that repeated calls to randLIB_seed_random() would pull more out (rather than reset).\r\n\r\nWas then considering maybe putting in occasional extra calls to arm_random_seed_get(), but was a bit nervous that some platforms might not like having arm_random_seed_get() called after boot.\r\n\r\nSo, yes, a seed_add(uint64_t) might be worthwhile. Won't do anything on RANDOM_DEVICE type things, of course.","created_at":"2016-10-18T06:45:12Z","updated_at":"2016-10-18T06:45:12Z","html_url":"https://github.com/ARMmbed/mbed-client-randlib/pull/6#discussion_r83790311","pull_request_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/6","_links":{"self":{"href":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/comments/83790311"},"html":{"href":"https://github.com/ARMmbed/mbed-client-randlib/pull/6#discussion_r83790311"},"pull_request":{"href":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/6"}}},"pull_request":{"url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/6","id":89579673,"html_url":"https://github.com/ARMmbed/mbed-client-randlib/pull/6","diff_url":"https://github.com/ARMmbed/mbed-client-randlib/pull/6.diff","patch_url":"https://github.com/ARMmbed/mbed-client-randlib/pull/6.patch","issue_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/issues/6","number":6,"state":"open","locked":false,"title":"Add local pseudo-RNG to randLIB ","user":{"login":"kjbracey-arm","id":11025917,"avatar_url":"https://avatars.githubusercontent.com/u/11025917?v=3","gravatar_id":"","url":"https://api.github.com/users/kjbracey-arm","html_url":"https://github.com/kjbracey-arm","followers_url":"https://api.github.com/users/kjbracey-arm/followers","following_url":"https://api.github.com/users/kjbracey-arm/following{/other_user}","gists_url":"https://api.github.com/users/kjbracey-arm/gists{/gist_id}","starred_url":"https://api.github.com/users/kjbracey-arm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kjbracey-arm/subscriptions","organizations_url":"https://api.github.com/users/kjbracey-arm/orgs","repos_url":"https://api.github.com/users/kjbracey-arm/repos","events_url":"https://api.github.com/users/kjbracey-arm/events{/privacy}","received_events_url":"https://api.github.com/users/kjbracey-arm/received_events","type":"User","site_admin":false},"body":"Rather than using system rand(), provide our own pseudo-RNG.\r\n\r\nGenerator used is \"xoroshiro128+\", which has 16 bytes of state and\r\n2^128-1 period.\r\n\r\nMain advantage is that we can now seed with up to 128 bits of entropy,\r\nrather than the 32 bits srand() limited us to. We also can be assured of\r\nthe quality of the algorithm.\r\n\r\nAs the core generator is 64-bit, we now provide a get 64-bit function,\r\nand others are based on this.\r\n\r\nIncorporate Linux's /dev/urandom use into the main source file.\r\n\r\n@SeppoTakalo, @mikter, @mikaleppanen, @yogpan01, @JarkkoPaso, @juhhei01 ","created_at":"2016-10-17T08:26:02Z","updated_at":"2016-10-18T06:45:12Z","closed_at":null,"merged_at":null,"merge_commit_sha":"67cc358599ea31425476b51e5d40e9ff3008f461","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/6/commits","review_comments_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/6/comments","review_comment_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/issues/6/comments","statuses_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/statuses/9d3579838c025d928c0f77e70f7df4f5a64e1c5f","head":{"label":"ARMmbed:xoroshiro128","ref":"xoroshiro128","sha":"9d3579838c025d928c0f77e70f7df4f5a64e1c5f","user":{"login":"ARMmbed","id":8916057,"avatar_url":"https://avatars.githubusercontent.com/u/8916057?v=3","gravatar_id":"","url":"https://api.github.com/users/ARMmbed","html_url":"https://github.com/ARMmbed","followers_url":"https://api.github.com/users/ARMmbed/followers","following_url":"https://api.github.com/users/ARMmbed/following{/other_user}","gists_url":"https://api.github.com/users/ARMmbed/gists{/gist_id}","starred_url":"https://api.github.com/users/ARMmbed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ARMmbed/subscriptions","organizations_url":"https://api.github.com/users/ARMmbed/orgs","repos_url":"https://api.github.com/users/ARMmbed/repos","events_url":"https://api.github.com/users/ARMmbed/events{/privacy}","received_events_url":"https://api.github.com/users/ARMmbed/received_events","type":"Organization","site_admin":false},"repo":{"id":39011873,"name":"mbed-client-randlib","full_name":"ARMmbed/mbed-client-randlib","owner":{"login":"ARMmbed","id":8916057,"avatar_url":"https://avatars.githubusercontent.com/u/8916057?v=3","gravatar_id":"","url":"https://api.github.com/users/ARMmbed","html_url":"https://github.com/ARMmbed","followers_url":"https://api.github.com/users/ARMmbed/followers","following_url":"https://api.github.com/users/ARMmbed/following{/other_user}","gists_url":"https://api.github.com/users/ARMmbed/gists{/gist_id}","starred_url":"https://api.github.com/users/ARMmbed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ARMmbed/subscriptions","organizations_url":"https://api.github.com/users/ARMmbed/orgs","repos_url":"https://api.github.com/users/ARMmbed/repos","events_url":"https://api.github.com/users/ARMmbed/events{/privacy}","received_events_url":"https://api.github.com/users/ARMmbed/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ARMmbed/mbed-client-randlib","description":"Pseudo random library from nanomesh.","fork":false,"url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib","forks_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/forks","keys_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/teams","hooks_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/hooks","issue_events_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/issues/events{/number}","events_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/events","assignees_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/assignees{/user}","branches_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/branches{/branch}","tags_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/tags","blobs_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/git/refs{/sha}","trees_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/statuses/{sha}","languages_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/languages","stargazers_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/stargazers","contributors_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/contributors","subscribers_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/subscribers","subscription_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/subscription","commits_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/commits{/sha}","git_commits_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/git/commits{/sha}","comments_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/comments{/number}","issue_comment_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/issues/comments{/number}","contents_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/contents/{+path}","compare_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/merges","archive_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/downloads","issues_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/issues{/number}","pulls_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls{/number}","milestones_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/milestones{/number}","notifications_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/labels{/name}","releases_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/releases{/id}","deployments_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/deployments","created_at":"2015-07-13T12:52:53Z","updated_at":"2016-05-20T13:30:05Z","pushed_at":"2016-10-17T09:53:08Z","git_url":"git://github.com/ARMmbed/mbed-client-randlib.git","ssh_url":"git@github.com:ARMmbed/mbed-client-randlib.git","clone_url":"https://github.com/ARMmbed/mbed-client-randlib.git","svn_url":"https://github.com/ARMmbed/mbed-client-randlib","homepage":null,"size":32,"stargazers_count":1,"watchers_count":1,"language":"Makefile","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":1,"forks":2,"open_issues":1,"watchers":1,"default_branch":"master"}},"base":{"label":"ARMmbed:master","ref":"master","sha":"80f5c491dd4d7c8e30b94eb575cdb402e9a17e83","user":{"login":"ARMmbed","id":8916057,"avatar_url":"https://avatars.githubusercontent.com/u/8916057?v=3","gravatar_id":"","url":"https://api.github.com/users/ARMmbed","html_url":"https://github.com/ARMmbed","followers_url":"https://api.github.com/users/ARMmbed/followers","following_url":"https://api.github.com/users/ARMmbed/following{/other_user}","gists_url":"https://api.github.com/users/ARMmbed/gists{/gist_id}","starred_url":"https://api.github.com/users/ARMmbed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ARMmbed/subscriptions","organizations_url":"https://api.github.com/users/ARMmbed/orgs","repos_url":"https://api.github.com/users/ARMmbed/repos","events_url":"https://api.github.com/users/ARMmbed/events{/privacy}","received_events_url":"https://api.github.com/users/ARMmbed/received_events","type":"Organization","site_admin":false},"repo":{"id":39011873,"name":"mbed-client-randlib","full_name":"ARMmbed/mbed-client-randlib","owner":{"login":"ARMmbed","id":8916057,"avatar_url":"https://avatars.githubusercontent.com/u/8916057?v=3","gravatar_id":"","url":"https://api.github.com/users/ARMmbed","html_url":"https://github.com/ARMmbed","followers_url":"https://api.github.com/users/ARMmbed/followers","following_url":"https://api.github.com/users/ARMmbed/following{/other_user}","gists_url":"https://api.github.com/users/ARMmbed/gists{/gist_id}","starred_url":"https://api.github.com/users/ARMmbed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ARMmbed/subscriptions","organizations_url":"https://api.github.com/users/ARMmbed/orgs","repos_url":"https://api.github.com/users/ARMmbed/repos","events_url":"https://api.github.com/users/ARMmbed/events{/privacy}","received_events_url":"https://api.github.com/users/ARMmbed/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ARMmbed/mbed-client-randlib","description":"Pseudo random library from nanomesh.","fork":false,"url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib","forks_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/forks","keys_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/teams","hooks_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/hooks","issue_events_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/issues/events{/number}","events_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/events","assignees_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/assignees{/user}","branches_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/branches{/branch}","tags_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/tags","blobs_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/git/refs{/sha}","trees_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/statuses/{sha}","languages_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/languages","stargazers_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/stargazers","contributors_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/contributors","subscribers_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/subscribers","subscription_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/subscription","commits_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/commits{/sha}","git_commits_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/git/commits{/sha}","comments_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/comments{/number}","issue_comment_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/issues/comments{/number}","contents_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/contents/{+path}","compare_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/merges","archive_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/downloads","issues_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/issues{/number}","pulls_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls{/number}","milestones_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/milestones{/number}","notifications_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/labels{/name}","releases_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/releases{/id}","deployments_url":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/deployments","created_at":"2015-07-13T12:52:53Z","updated_at":"2016-05-20T13:30:05Z","pushed_at":"2016-10-17T09:53:08Z","git_url":"git://github.com/ARMmbed/mbed-client-randlib.git","ssh_url":"git@github.com:ARMmbed/mbed-client-randlib.git","clone_url":"https://github.com/ARMmbed/mbed-client-randlib.git","svn_url":"https://github.com/ARMmbed/mbed-client-randlib","homepage":null,"size":32,"stargazers_count":1,"watchers_count":1,"language":"Makefile","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":1,"forks":2,"open_issues":1,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/6"},"html":{"href":"https://github.com/ARMmbed/mbed-client-randlib/pull/6"},"issue":{"href":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/issues/6"},"comments":{"href":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/issues/6/comments"},"review_comments":{"href":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/6/comments"},"review_comment":{"href":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/pulls/6/commits"},"statuses":{"href":"https://api.github.com/repos/ARMmbed/mbed-client-randlib/statuses/9d3579838c025d928c0f77e70f7df4f5a64e1c5f"}}}}
{"actor":{"display_login":"kjbracey-arm"}}