utils.c
1 #include <stdio.h> 2 #include <stdarg.h> 3 #include <string.h> 4 #include <sodium.h> 5 6 #if (defined UNIT_TEST || defined UNITTEST_CORRUPT) 7 int liboprf_debug = 1; 8 #else 9 int liboprf_debug = 0; 10 #endif 11 FILE *liboprf_log_file=NULL; 12 13 #ifdef UNIT_TEST 14 void debian_rng_scalar(uint8_t *scalar) { 15 static int warned=0; 16 static uint8_t rng_i[4]={1,0,0,0}; 17 if(!warned) { 18 fprintf(stderr, "\x1b[0;31mWARNING! This version of liboprf DKG is compiled with a *NON* random generator for UNIT_TESTS\x1b[0m\n"); 19 warned=1; 20 } 21 memset(scalar,0,crypto_core_ristretto255_SCALARBYTES); 22 sodium_increment(rng_i,4); 23 memcpy(scalar,rng_i,4); 24 //static uint16_t rng_i=0; 25 //uint16_t tmp[64 / sizeof(uint16_t)]; 26 //for(unsigned j=0;j<(64/ sizeof(uint16_t));j++) { 27 // tmp[j]=rng_i++; 28 //} 29 //crypto_core_ristretto255_scalar_reduce(scalar,(uint8_t*)tmp); 30 } 31 #endif 32 33 void __attribute__((visibility("hidden"))) dump(const uint8_t *p, const size_t len, const char* msg, ...) { 34 FILE* lf = stderr; 35 if(!liboprf_debug) return; 36 if(liboprf_log_file!=NULL) lf = liboprf_log_file; 37 va_list args; 38 va_start(args, msg); 39 vfprintf(lf, msg, args); 40 va_end(args); 41 fprintf(lf," "); 42 for(size_t i=0;i<len;i++) 43 fprintf(lf,"%02x", p[i]); 44 fprintf(lf,"\n"); 45 fflush(lf); 46 } 47 48 void __attribute__((visibility("hidden"))) fail(const char* msg, ...) { 49 va_list args; 50 va_start(args, msg); 51 fprintf(stderr, "\x1b[0;31m"); 52 vfprintf(stderr, msg, args); 53 va_end(args); 54 fprintf(stderr, "\x1b[0m\n"); 55 } 56 57 #ifndef htonll 58 #include <arpa/inet.h> 59 uint64_t __attribute__((visibility("hidden"))) htonll(uint64_t n) { 60 #if __BYTE_ORDER == __BIG_ENDIAN 61 return n; 62 #else 63 return (((uint64_t)htonl((uint32_t)n)) << 32) + htonl((uint32_t) (n >> 32)); 64 #endif 65 } 66 #endif // htonll 67 68 #ifndef ntohll 69 uint64_t __attribute__((visibility("hidden"))) ntohll(uint64_t n) { 70 #if __BYTE_ORDER == __BIG_ENDIAN 71 return n; 72 #else 73 return (((uint64_t)ntohl((uint32_t)n)) << 32) + ntohl((uint32_t)(n >> 32)); 74 #endif 75 } 76 #endif // ntohll