+static void cdebug(int mechno /*or -1*/, const char *msg) {
+ if (!crypto_debug) return;
+ printf("%-8.8s: CRYPTO: %-20s %s\n",
+ uname_result.nodename,
+ mechno >= 0 ? mechs[mechno]->name : "",
+ msg);
+}
+
+static void cdebughex(int mechno /*or -1*/, const char *msg, const void *ptr,
+ size_t sz, size_t skipbefore,
+ int spc_offset, int dot_offset) {
+ const unsigned char *p;
+ size_t i;
+ unsigned j= dot_offset;
+
+ if (!crypto_debug) return;
+ printf("%-8.8s: CRYPTO: %-20s %-10s",
+ uname_result.nodename,
+ mechno >= 0 ? mechs[mechno]->name : "",
+ msg);
+
+ for (i=0; i<spc_offset; i++, j++) fputs(j&3 ? " " : " ",stdout);
+ for (i=0; i<skipbefore; i++, j++) fputs(j&3 ? ".." : " ..",stdout);
+ for (i=0, p=ptr; i<sz; i++, j++, p++) printf(j&3 ? "%02x" : " %02x",*p);
+
+ fputc('\n',stdout);
+}
+
+static void cdebugbuf(int mechno /*or -1*/, const char *msg,
+ const struct buffer *buf, int spc_offset, int dot_offset) {
+ cdebughex(mechno, msg, buf->start, buf->size, buf->start - buf->base,
+ spc_offset, dot_offset);
+}
+
+void get_random(void *ptr, size_t sz) {
+ static FILE *randfile;
+
+ size_t r;
+
+ if (!randfile) {
+ randfile= fopen("/dev/urandom","rb");
+ if (!randfile && errno==ENOENT) randfile= fopen("/dev/random","rb");
+ if (!randfile) sysfail("open random number generator");
+ }
+
+ r= fread(ptr,1,sz,randfile);
+ if (r != sz)
+ (ferror(randfile) ? sysfail : fail)("cannot read random number generator");
+
+ cdebughex(-1, "get_random", ptr, sz, 0,0,0);
+}