unsigned long long random_generator::get_random() { unsigned long long llrn; #ifdef HAVE_RANDOM_R int32_t rn; // max is RAND_MAX, which is usually 2^31-1 (although can be as low as 2^16-1, which we ignore now) // this is fine, especially considering that random_r is a nonstandard glibc extension // it returns a positive int32_t, so either way the MSB is off int ret = random_r(&m_data_blob, &rn); assert(ret == 0); llrn = rn; llrn = llrn << 31; ret = random_r(&m_data_blob, &rn); assert(ret == 0); llrn |= rn; #elif (defined HAVE_DRAND48) long rn; // jrand48's range is -2^31..+2^31 (i.e. all 32 bits) rn = jrand48(m_data_blob); llrn = rn; llrn = llrn << 32; rn = jrand48(m_data_blob); llrn |= rn & 0xffffffff; // reset the sign extension bits of negative numbers llrn &= 0x8000000000000000; // avoid any trouble from sign mismatch and negative numbers #else #error no random function #endif return llrn; }
/** * virRandomBits: * @nbits: Number of bits of randommess required * * Generate an evenly distributed random number between [0,2^nbits), where * @nbits must be in the range (0,64]. * * Return: a random number with @nbits entropy */ uint64_t virRandomBits(int nbits) { uint64_t ret = 0; int32_t bits; if (virRandomInitialize() < 0) { /* You're already hosed, so this particular non-random value * isn't any worse. */ VIR_WARN("random number generation is broken"); return 0; } virMutexLock(&randomLock); while (nbits > RANDOM_BITS_PER_ITER) { random_r(&randomData, &bits); ret = (ret << RANDOM_BITS_PER_ITER) | (bits & RANDOM_BITS_MASK); nbits -= RANDOM_BITS_PER_ITER; } random_r(&randomData, &bits); ret = (ret << nbits) | (bits & ((1 << nbits) - 1)); virMutexUnlock(&randomLock); return ret; }
unsigned int xrandom() { int rnd = 0; pthread_mutex_lock(&rng_lock); random_r(&rng_state, &rnd); pthread_mutex_unlock(&rng_lock); return (unsigned int) rnd; }
/* * task_fileiolog_append -- appends to the log in the FILEIO mode */ int task_fileiolog_append(void *arg) { int32_t rand_number; struct thread_info *thread_info = arg; size_t vec_size = thread_info->vec_size; size_t el_size = thread_info->el_size; int fd = *(int *)thread_info->hndl; if (thread_info->rand_state != NULL) { random_r(thread_info->rand_state, &rand_number); vec_size = rand_number % vec_size + MIN_VEC_SIZE; el_size = rand_number % el_size + MIN_EL_SIZE; }; /* check vector size */ if (vec_size > 1) { for (int i = 0; i < vec_size; ++i) { thread_info->iov[i].iov_base = &thread_info->buf[i * el_size]; thread_info->iov[i].iov_len = el_size; } if ((writev(fd, thread_info->iov, vec_size)) == -1) { return EXIT_FAILURE; } } else { if ((write(fd, thread_info->buf, el_size)) == -1) { return EXIT_FAILURE; } } return EXIT_SUCCESS; }
static int ipv4ll_pick_address(sd_ipv4ll *ll) { struct in_addr in_addr; be32_t addr; int r; int32_t random; assert(ll); assert(ll->random_data); do { r = random_r(ll->random_data, &random); if (r < 0) return r; addr = htonl((random & 0x0000FFFF) | IPV4LL_NETWORK); } while (addr == ll->address || (ntohl(addr) & 0x0000FF00) == 0x0000 || (ntohl(addr) & 0x0000FF00) == 0xFF00); in_addr.s_addr = addr; r = sd_ipv4ll_set_address(ll, &in_addr); if (r < 0) return r; return 0; }
MojErr MojRandom(MojRandomDataT* buf, MojUInt32* result) { if (random_r(buf, (MojInt32*) result) < 0) MojErrThrowErrno(_T("random_r")); return MojErrNone; }
/* * task_malloc -- allocates MALLOC_SIZE memory in allocated_mem array */ int task_malloc(int i, void *arg, struct random_data *rand_state) { int size_to_alloc; int random_number; switch (allocation_type) { case ALLOCATION_STATIC: size_to_alloc = allocation_max; break; case ALLOCATION_RANGE: random_r(rand_state, &random_number); size_to_alloc = random_number % (allocation_max - allocation_min) + allocation_min; break; case ALLOCATION_UNKNOWN: break; } if (allocator == ALLOCATOR_VMEM) { VMEM *pool = arg; allocated_mem[i] = vmem_malloc(pool, size_to_alloc); } else { allocated_mem[i] = malloc(size_to_alloc); } if (allocated_mem[i] == NULL) { return FAILURE; } return SUCCESS; }
long int random (void) { int32_t retval; __UCLIBC_MUTEX_LOCK(mylock); random_r (&unsafe_state, &retval); __UCLIBC_MUTEX_UNLOCK(mylock); return retval; }
long random (void) { int32_t val; if (random_r (&generator, &val) < 0) abort (); /* should not happen */ return val; }
inline int Random::Impl::random () { int r = 0; #if defined CWT_HAVE_RANDOM_R CWT_VERIFY(random_r(& m_rdata, & r) == 0); #endif return r; }
static int32_t FcRandom(void) { int32_t result; #if HAVE_RANDOM_R static struct random_data fcrandbuf; static char statebuf[256]; static FcBool initialized = FcFalse; if (initialized != FcTrue) { initstate_r(time(NULL), statebuf, 256, &fcrandbuf); initialized = FcTrue; } random_r(&fcrandbuf, &result); #elif HAVE_RANDOM static char statebuf[256]; char *state; static FcBool initialized = FcFalse; if (initialized != FcTrue) { state = initstate(time(NULL), statebuf, 256); initialized = FcTrue; } else state = setstate(statebuf); result = random(); setstate(state); #elif HAVE_LRAND48 result = lrand48(); #elif HAVE_RAND_R static unsigned int seed = time(NULL); result = rand_r(&seed); #elif HAVE_RAND static FcBool initialized = FcFalse; if (initialized != FcTrue) { srand(time(NULL)); initialized = FcTrue; } result = rand(); #else # error no random number generator function available. #endif return result; }
static void *thread_worker(void *ctx) { struct array_s allocations; if (0 == array_init(&allocations, sizeof(void *), 0)) { fprintf(stderr, "no mem\n"); exit(1); } struct random_data rnd; memset(&rnd, 0, sizeof(rnd)); char rnd_state[8]; initstate_r(1, rnd_state, 8, &rnd); for (;;) { int32_t r; random_r(&rnd, &r); long alloc_size = 10 + r % 1024*20; while (memory_used_fn() + alloc_size >= limit && allocations.used) { /* delete half of our allocations */ unsigned to_delete = allocations.used / 2; for (unsigned i = 0; i < to_delete; i++) { void **allocation_p = array_item_last(&allocations); if (!allocation_p) { break; } allocations.used--; zfree(*allocation_p); } } void *buf = zmalloc(alloc_size); if (!buf) { fprintf(stderr, "no mem\n"); exit(1); } for (int i = 0; i < alloc_size; i++) { *((char *)buf + i) = 'a'; } //memset(buf, 0x01, sizeof(alloc_size)); *(void **)array_push(&allocations) = buf; } }
int32_t get_random(void) { int32_t val; if (random_r(&rnd_data, &val) < 0) { prterr("random_r"); exit(1); } return val; }
unsigned int random_generator::get_random() { int rn; #ifdef HAVE_RANDOM_R int ret = random_r(&m_data_blob, &rn);//max is RAND_MAX assert(ret == 0); #elif (defined HAVE_DRAND48) rn = nrand48(m_data_blob); // max is 1<<31 #else #error no random function #endif return rn; }
char *make_nonce() { char *nonce = malloc(NONCE_SIZE); int rnd; memset(nonce, 0, NONCE_SIZE); rnd = xrandom(); pthread_mutex_lock(&rng_lock); random_r(&rng_state, &rnd); pthread_mutex_unlock(&rng_lock); snprintf((char *) nonce, NONCE_SIZE - 1, "%d", rnd); return nonce; }
int main() { int i, v; char state[8]; struct random_data rnd; rnd.state = 0; initstate_r(0, state, sizeof(state), &rnd); for (i = 0; i < N; i++) { random_r(&rnd, &v); printf("%d\n", v%8); } return 0; }
void thename() { int c = 0; int return_value; struct random_data* qq = static_cast<struct random_data*>(calloc(1,sizeof(struct random_data))); char* buf = static_cast<char *>(calloc(STATE_SIZE,sizeof(char))); initstate_r(69L,buf,STATE_SIZE,qq); for(int i = 0 ; i < (1<<28); i++) { random_r(qq,&return_value); c+= return_value; } printf("Thread returns c = %d\n",c); }
/** * \return Random value between 0 and RAND_MAX * * This functions wraps around gnulib's random_r(). It performs a thread-safe seeding on the first use, * if not done before by wget_srandom(); */ int wget_random(void) { int32_t r; wget_thread_mutex_lock(&mutex); if (!seeded) { // seed random generator, used e.g. by Digest Authentication and --random-wait initstate_r((unsigned)(time(NULL) ^ getpid()), statebuf, sizeof(statebuf), &state); seeded = 1; } if (random_r(&state, &r)) r = 0; // return 0 on failure wget_thread_mutex_unlock(&mutex); return (int)r; }
void* random_printer(void *ptr) { //get the seed that was passed into the thread int seed = (size_t)ptr; //create our random_data buff on the stack struct random_data buff = {}; //create a place to store the result from random_r on the stack int32_t result =0; //create our statebuff for initState_r. Note: that statebuff must not be less than 8 bytes char statebuf[STATE_LEN]; //init our state for random_r initstate_r(seed,statebuf,STATE_LEN,&buff); printf("%p\n",&buff); int i; for(i=0;i<10000;i++ ){ //get our random numbers. random_r(&buff,&result); printf("%d\n",result); } pthread_exit(NULL); }
/* * task_pmemlog_read -- reads from the log in the PMEM mode */ int task_pmemlog_read(void *arg) { int32_t rand_number; struct thread_info *thread_info = arg; size_t vec_size = thread_info->vec_size; size_t el_size = thread_info->el_size; PMEMlog *plp = (PMEMlog *)thread_info->hndl; thread_info->buf_ptr = 0; if (thread_info->rand_state != NULL) { random_r(thread_info->rand_state, &rand_number); vec_size = rand_number % vec_size + MIN_VEC_SIZE; el_size = rand_number % el_size + MIN_EL_SIZE; } pmemlog_walk(plp, vec_size * el_size, process_data, arg); return EXIT_SUCCESS; }
static int ipv4ll_pick_address(sd_ipv4ll *ll, be32_t *address) { be32_t addr; int r; int32_t random; assert(ll); assert(address); assert(ll->random_data); do { r = random_r(ll->random_data, &random); if (r < 0) return r; addr = htonl((random & 0x0000FFFF) | IPV4LL_NETWORK); } while (addr == ll->address || (ntohl(addr) & IPV4LL_NETMASK) != IPV4LL_NETWORK || (ntohl(addr) & 0x0000FF00) == 0x0000 || (ntohl(addr) & 0x0000FF00) == 0xFF00); *address = addr; return 0; }
int main() { int c = 0; int return_value; struct random_data* qq = static_cast<struct random_data*>(calloc(1,sizeof(struct random_data))); char* buf = static_cast<char *>(calloc(STATE_SIZE,sizeof(char))); initstate_r(69L,buf,STATE_SIZE,qq); std::thread first(thename); for(int i = 0 ; i < (1<<28); i++) { random_r(qq,&return_value); c+= return_value; } printf("c is %d\n",c); first.join(); return 0; }
/* * task_fileiolog_read -- reads from the log in the FILEIO mode */ int task_fileiolog_read(void *arg) { int32_t rand_number; struct thread_info *thread_info = arg; size_t vec_size = thread_info->vec_size; size_t el_size = thread_info->el_size; int fd = *(int *)thread_info->hndl; if (thread_info->rand_state != NULL) { random_r(thread_info->rand_state, &rand_number); vec_size = rand_number % vec_size + MIN_VEC_SIZE; el_size = rand_number % el_size + MIN_EL_SIZE; } thread_info->buf_ptr = 0; char buf[vec_size * el_size]; while (pread(fd, buf, vec_size * el_size, thread_info->buf_ptr) != 0) { process_data(buf, vec_size * el_size, thread_info); } return EXIT_SUCCESS; }
int fail_myself(){ int index; if(B == 0) return 0; while(1){ random_r(s_buf,&index); index = index%N; //printf("---- FAIL INDEX -> %d\n", index); int i; if(failed_arr[index] == 1){ continue; } else { break; } } failed_arr[index] = 1; if(index == my_line_number){ return 1; } return 0; }
int64_t HHVM_FUNCTION(rand, int64_t min /* = 0 */, const Variant& max /* = null_variant */) { #if defined(__APPLE__) || defined(_MSC_VER) if (!s_rand_is_seeded) { #else if (s_state.state != RandomBuf::RequestInit) { #endif randinit(math_generate_seed()); } int64_t number; #ifdef __APPLE__ number = random(); #elif defined(_MSC_VER) number = rand(); #else int32_t numberIn; random_r(&s_state.data, &numberIn); number = numberIn; #endif int64_t int_max = max.isNull() ? RAND_MAX : max.toInt64(); if (min != 0 || int_max != RAND_MAX) { RAND_RANGE(number, min, int_max, RAND_MAX); } return number; } int64_t HHVM_FUNCTION(mt_getrandmax) { return MT_RAND_MAX;} void HHVM_FUNCTION(mt_srand, const Variant& seed /* = null_variant */) { if (seed.isNull()) { return math_mt_srand(math_generate_seed()); } if (seed.isNumeric(true)) { math_mt_srand(seed.toInt32()); } else { raise_warning("mt_srand() expects parameter 1 to be long"); } } int64_t HHVM_FUNCTION(mt_rand, int64_t min /* = 0 */, const Variant& max /* = null_variant */) { return math_mt_rand(min, max.isNull() ? RAND_MAX : max.toInt64()); } double HHVM_FUNCTION(lcg_value) { return math_combined_lcg();} Variant HHVM_FUNCTION(intdiv, int64_t numerator, int64_t divisor) { if (divisor == 0) { SystemLib::throwDivisionByZeroErrorObject(Strings::DIVISION_BY_ZERO); } else if (divisor == -1 && numerator == std::numeric_limits<int64_t>::min()) { SystemLib::throwArithmeticErrorObject( "Division of PHP_INT_MIN by -1 is not an integer"); } return numerator/divisor; } /////////////////////////////////////////////////////////////////////////////// const StaticString s_PHP_ROUND_HALF_UP("PHP_ROUND_HALF_UP"); const StaticString s_PHP_ROUND_HALF_DOWN("PHP_ROUND_HALF_DOWN"); const StaticString s_PHP_ROUND_HALF_EVEN("PHP_ROUND_HALF_EVEN"); const StaticString s_PHP_ROUND_HALF_ODD("PHP_ROUND_HALF_ODD"); #define ICONST(nm) \ Native::registerConstant<KindOfInt64>(makeStaticString(#nm), k_##nm) \ #define DCONST(nm) \ Native::registerConstant<KindOfDouble>(makeStaticString("M_"#nm), k_M_##nm) \ void StandardExtension::requestInitMath() { #if !defined(__APPLE__) && !defined(_MSC_VER) if (s_state.state == RandomBuf::RequestInit) { s_state.state = RandomBuf::ThreadInit; } #endif } void StandardExtension::initMath() { ICONST(PHP_ROUND_HALF_UP); ICONST(PHP_ROUND_HALF_DOWN); ICONST(PHP_ROUND_HALF_EVEN); ICONST(PHP_ROUND_HALF_ODD); DCONST(PI); DCONST(1_PI); DCONST(2_PI); DCONST(2_SQRTPI); DCONST(E); DCONST(EULER); DCONST(LN10); DCONST(LN2); DCONST(LNPI); DCONST(LOG10E); DCONST(LOG2E); DCONST(PI_2); DCONST(PI_4); DCONST(SQRT1_2); DCONST(SQRT2); DCONST(SQRT3); DCONST(SQRTPI); HHVM_FE(min); HHVM_FE(max); HHVM_FE(abs); HHVM_FE(is_finite); HHVM_FE(is_infinite); HHVM_FE(is_nan); HHVM_FE(ceil); HHVM_FE(floor); HHVM_FE(round); HHVM_FE(deg2rad); HHVM_FE(rad2deg); HHVM_FE(decbin); HHVM_FE(dechex); HHVM_FE(decoct); HHVM_FE(bindec); HHVM_FE(hexdec); HHVM_FE(octdec); HHVM_FE(base_convert); HHVM_FE(pow); HHVM_FE(exp); HHVM_FE(expm1); HHVM_FE(log10); HHVM_FE(log1p); HHVM_FE(log); HHVM_FE(cos); HHVM_FE(cosh); HHVM_FE(sin); HHVM_FE(sinh); HHVM_FE(tan); HHVM_FE(tanh); HHVM_FE(acos); HHVM_FE(acosh); HHVM_FE(asin); HHVM_FE(asinh); HHVM_FE(atan); HHVM_FE(atanh); HHVM_FE(atan2); HHVM_FE(hypot); HHVM_FE(fmod); HHVM_FE(sqrt); HHVM_FE(getrandmax); HHVM_FE(srand); HHVM_FE(rand); HHVM_FE(mt_getrandmax); HHVM_FE(mt_srand); HHVM_FE(mt_rand); HHVM_FE(lcg_value); HHVM_FE(intdiv); loadSystemlib("std_math"); }
static void* update_thread(void* data) { struct timespec ts; int ret; unsigned int diff, ntot, seed; struct gtec_device* gtdev = data; pthread_mutex_t* lock = &(gtdev->updatelock); struct random_data rdata = {.rand_type = 0}; int32_t randnum = 0; char state[128] = {0}; // Initialize random generator clock_gettime(CLOCK_REALTIME, &ts); seed = ts.tv_nsec; initstate_r(seed, state, sizeof(state), &rdata); // Wait for acquisition start pthread_mutex_lock(&acqlock); while (!acquiring) { pthread_cond_wait(&acqcond, &acqlock); } pthread_mutex_unlock(&acqlock); memcpy(&ts, &org, sizeof(ts)); random_r(&rdata, &randnum); addtime(&ts, 0, 7000000 + randnum/2500); pthread_mutex_lock(lock); while (gtdev->running) { ret = pthread_cond_timedwait(>dev->cond, lock, &ts); if (ret == ETIMEDOUT) { diff = difftime_ms(&ts, &org); ntot = (diff*gtdev->conf.sample_rate)/1000; gtdev->nsample = ntot; pthread_mutex_unlock(lock); gtdev->callback(gtdev->callback_data); random_r(&rdata, &randnum); addtime(&ts, 0, 7000000 + randnum/2500); pthread_mutex_lock(lock); } } pthread_mutex_unlock(lock); return NULL; } API_EXPORTED void GT_ShowDebugInformation( gt_bool show ) { (void)show; } API_EXPORTED gt_bool GT_UpdateDevices() { static int isinit = 0; int i; if (isinit) return GT_TRUE; for (i=0; i<NUMDEV; i++) initialize_device(>devices[i]); isinit = 1; return GT_TRUE; } API_EXPORTED gt_size GT_GetDeviceListSize() { return NUMDEV; } API_EXPORTED char** GT_GetDeviceList() { int i; char** devlist; devlist = malloc(NUMDEV*sizeof(devlist[0])); for (i=0; i<NUMDEV; i++) { devlist[i] = malloc(strlen(devname[i])+1); strcpy(devlist[i], devname[i]); } return devlist; }
void *index_file (void *arg) { int line = 0, found = 0, cnt = 0, cachecnt, idx; int ch, len = 0, slot, type = 0; unsigned char key[ARTmaxkey]; struct random_data buf[1]; ThreadArg *args = arg; ARTthread *thread; uint counts[8][2]; uchar state[64]; uint next[1]; ulong offset; int vallen; ARTval *val; uint size; FILE *in; if( args->idx < strlen (args->type) ) ch = args->type[args->idx]; else ch = args->type[strlen(args->type) - 1]; thread = ARTnewthread(args->trie, ARTdepth); switch(ch | 0x20) { case '4': // 4 byte random keys size = atoi(args->infile); memset (buf, 0, sizeof(buf)); initstate_r(args->idx * 100 + 100, state, 64, buf); for( line = 0; line < size; line++ ) { random_r(buf, next); key[0] = next[0]; next[0] >>= 8; key[1] = next[0]; next[0] >>= 8; key[2] = next[0]; next[0] >>= 8; key[3] = next[0]; ARTinsert (thread, key, 4, 4); } break; case '8': // 8 byte random keys of random length size = atoi(args->infile); memset (buf, 0, sizeof(buf)); initstate_r(args->idx * 100 + 100, state, 64, buf); for( line = 0; line < size; line++ ) { random_r(buf, next); key[0] = next[0]; next[0] >>= 8; key[1] = next[0]; next[0] >>= 8; key[2] = next[0]; next[0] >>= 8; key[3] = next[0]; random_r(buf, next); key[4] = next[0]; next[0] >>= 8; key[5] = next[0]; next[0] >>= 8; key[6] = next[0]; next[0] >>= 8; key[7] = next[0]; if( ARTinsert (thread, key, (line % 8) + 1, 8) ) found++; } fprintf(stderr, "finished %s for %d keys, duplicates %d\n", args->infile, line, found); break; case 'y': // 8 byte random keys of random length size = atoi(args->infile); memset (buf, 0, sizeof(buf)); initstate_r(args->idx * 100 + 100, state, 64, buf); for( line = 0; line < size; line++ ) { random_r(buf, next); key[0] = next[0]; next[0] >>= 8; key[1] = next[0]; next[0] >>= 8; key[2] = next[0]; next[0] >>= 8; key[3] = next[0]; random_r(buf, next); key[4] = next[0]; next[0] >>= 8; key[5] = next[0]; next[0] >>= 8; key[6] = next[0]; next[0] >>= 8; key[7] = next[0]; if( ARTfindkey (thread, key, line % 8 + 1) ) found++; } fprintf(stderr, "finished %s for %d keys, found %d\n", args->infile, line, found); break; case 'x': // find 4 byte random keys size = atoi(args->infile); memset (buf, 0, sizeof(buf)); initstate_r(args->idx * 100 + 100, state, 64, buf); for( line = 0; line < size; line++ ) { random_r(buf, next); key[0] = next[0]; next[0] >>= 8; key[1] = next[0]; next[0] >>= 8; key[2] = next[0]; next[0] >>= 8; key[3] = next[0]; if( ARTfindkey (thread, key, 4) ) found++; } fprintf(stderr, "finished %s for %d keys, found %d\n", args->infile, line, found); break; case 'd': // type = Delete; case 'p': // if( !type ) // type = Unique; // if( type == Delete ) // fprintf(stderr, "started pennysort delete for %s\n", args->infile); // else fprintf(stderr, "started pennysort insert for %s\n", args->infile); if( in = fopen (args->infile, "rb") ) while( ch = getc(in), ch != EOF ) if( ch == '\n' ) { line++; if( len > 10 ) { offset = art_space (thread, len - 10 + sizeof(ARTval)); val = (ARTval *)(Arena + offset); memcpy (val->value, key + 10, len - 10); val->len = len - 10; } else offset = 1; if( ARTinsert (thread, key, 10, offset) ) fprintf(stderr, "Duplicate key source: %d\n", line), exit(0); len = 0; continue; } else if( len < ARTmaxkey ) key[len++] = ch; fprintf(stderr, "finished %s for %d keys\n", args->infile, line); break; case 'w': fprintf(stderr, "started indexing for %s\n", args->infile); if( in = fopen (args->infile, "r") ) while( ch = getc(in), ch != EOF ) if( ch == '\n' ) { line++; ARTinsert (thread, key, len, 1); len = 0; } else if( len < ARTmaxkey ) key[len++] = ch; fprintf(stderr, "finished %s for %d keys\n", args->infile, line); break; case 'f': fprintf(stderr, "started finding keys for %s\n", args->infile); if( in = fopen (args->infile, "rb") ) while( ch = getc(in), ch != EOF ) if( ch == '\n' ) { line++; if( ARTfindkey (thread, key, len) ) found++; len = 0; } else if( len < ARTmaxkey ) key[len++] = ch; fprintf(stderr, "finished %s for %d keys, found %d\n", args->infile, line, found); break; case 's': fprintf(stderr, "started forward scan\n"); ARTstartkey (thread, NULL, 0); while( len = ARTnextkey (thread, key, ARTmaxkey) ) { fwrite (key, len, 1, stdout); val = thread->cursor->value; if( val->len ) fwrite (val->value, val->len, 1, stdout); fputc ('\n', stdout); cnt++; } fprintf(stderr, " Total keys read %d\n", cnt); break; case 'r': fprintf(stderr, "started reverse scan\n"); ARTlastkey (thread, NULL, 0); while( len = ARTprevkey (thread, key, ARTmaxkey) ) { fwrite (key, len, 1, stdout); val = thread->cursor->value; if( val->len ) fwrite (val->value, val->len, 1, stdout); fputc ('\n', stdout); cnt++; } fprintf(stderr, " Total keys read %d\n", cnt); break; } return NULL; }
inline double urandom(struct random_data * buffer) { int32_t res; random_r(buffer,&res); return (double)res/RAND_MAX; }
int main(int argc, char **argv){ if(argc != 9){ printf("Usage ./p4 N b c F B P S T %d\n", argc); exit(0); } N = atoi(argv[1]); b = atoi(argv[2]); c = atoi(argv[3]); F = atoi(argv[4]); B = atoi(argv[5]); P = atoi(argv[6]); S = atoi(argv[7]); T = atoi(argv[8]); node_list = (gossip_node*)malloc(sizeof(gossip_node)*N); neighbour_list = (gossip_node*)malloc(sizeof(gossip_node)*b); struct sockaddr_in remaddr; socklen_t addrlen = sizeof(remaddr); int recvlen; unsigned char buf[BUFSIZE]; pthread_mutex_init(&mutex, NULL); /*Creating a socket*/ if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("socket failed\n"); return 0; } memset((char *)&myaddr, 0, sizeof(myaddr)); myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = htonl(INADDR_ANY); myaddr.sin_port = htons(0); /*Binding to a socket*/ if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { perror("bind failed"); return 0; } socklen_t len = sizeof(myaddr); if (getsockname(fd, (struct sockaddr *)&myaddr, &len) == -1) perror("getsockname"); char hostname[1024]; char ip[100]; hostname[1023] = '\0'; gethostname(hostname, 1023); printf("Hostname: %s\n", hostname); struct hostent* h; h = gethostbyname(hostname); hostname_to_ip(h->h_name,ip); printf("IP Address: %s\n", ip); printf("Port number %d\n", ntohs(myaddr.sin_port)); /*Reading lines in endpoints.txt file */ FILE *fp1= fopen("endpoints.txt", "ab+"); int lines = 0; char ch; while(!feof(fp1)){ ch = fgetc(fp1); if(ch == '\n'){ lines++; } } my_line_number = lines; /* Creating 2 random buffers based on 2 different seeds [S, S+I] */ char *rand_buf1 = (char*)calloc(64, sizeof(char)); char *rand_buf2 = (char*)calloc(64, sizeof(char)); si_buf = (struct random_data*)calloc(1,sizeof(struct random_data)); s_buf = (struct random_data*)calloc(1,sizeof(struct random_data)); initstate_r(S+my_line_number,rand_buf1, 64, si_buf); initstate_r(S,rand_buf2, 64, s_buf); srandom_r(S+my_line_number, si_buf); srandom_r(S, s_buf); fclose(fp1); FILE *fp = fopen("endpoints.txt", "ab+"); int i; size_t buffsize = 150; char *line = malloc(sizeof(char)*buffsize); /*Last node check */ if(lines == N-1){ /*Send OK to all the other nodes*/ fprintf(fp, "%s:%d\n", ip, ntohs(myaddr.sin_port)); rewind(fp); for (i=0; i < N-1; i++) { int num = getline(&line, &buffsize, fp); printf("Line: %s\n",line); char ip[100]; char port[50]; int ipdone = 0; int j; int w; for(w=0; w<num; w++){ if(line[w] != ':' && !ipdone) ip[w] = line[w]; else{ ipdone = 1; ip[w] = '\0'; j = ++w; break; } } int k; for(w=0, k=j; k<num; w++, k++){ port[w] = line[k]; } port[w] = '\0'; memset((char *) &remaddr, 0, sizeof(remaddr)); remaddr.sin_family = AF_INET; remaddr.sin_port = htons(atoi(port)); remaddr.sin_addr.s_addr = inet_addr(ip); sprintf(buf, "OK"); sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&remaddr, addrlen); } fclose(fp); } else { /*Not the last node - add to endpoints file and wait for OK */ fprintf(fp, "%s:%d\n", ip, ntohs(myaddr.sin_port)); fclose(fp); recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen); printf("Receiver IP : %s\n", inet_ntoa(remaddr.sin_addr)); printf("received %d bytes\n", recvlen); if (recvlen > 0) { buf[recvlen] = 0; printf("received message: \"%s\"\n", buf); } } /*OK received - creating node_list from endpoints file */ FILE *fp2 = fopen("endpoints.txt", "ab+"); for (i=0; i < N; i++) { int num = getline(&line, &buffsize, fp); char ip[100]; char port[50]; int ipdone = 0; int j; int w; for(w=0; w<num; w++){ if(line[w] != ':' && !ipdone) ip[w] = line[w]; else{ ipdone = 1; ip[w] = '\0'; j = ++w; break; } } int k; for(w=0, k=j; k<num; w++, k++){ port[w] = line[k]; } port[w] = '\0'; gossip_node new_node; new_node.port = atoi(port); new_node.status = 1; new_node.heartbeat = 0; strcpy(new_node.ip, ip); node_list[i] = new_node; } for(i=0; i<N; i++){ printf("Node %d: %s %d %d %d\n",i, node_list[i].ip, node_list[i].port, node_list[i].status, node_list[i].heartbeat); } printf("\n\n"); pthread_t serverthread; /* Starting server thread */ pthread_create(&serverthread,NULL, server_handler, &myaddr); i=0; int send_msges = 1; /* Sending messages for c iterations */ while(1){ if(send_msges){ pthread_mutex_lock(&mutex); int j; int neighbour_count = 0; /*Randomly find b neighbours to send a message*/ while(neighbour_count < b){ int32_t send_index; random_r(si_buf, &send_index); send_index = send_index%N; //printf("Random Index : %d\n", send_index); if(!contains_neighbour(send_index, neighbour_list, neighbour_count)){ gossip_node new_node; new_node.port = node_list[send_index].port; new_node.status = node_list[send_index].status; new_node.heartbeat = node_list[send_index].heartbeat; strcpy(new_node.ip, node_list[send_index].ip); neighbour_list[neighbour_count] = new_node; neighbour_count++; } } for(j=0; j<b; j++){ memset((char *) &remaddr, 0, sizeof(remaddr)); remaddr.sin_family = AF_INET; remaddr.sin_port = htons(neighbour_list[j].port); remaddr.sin_addr.s_addr = inet_addr(neighbour_list[j].ip); //printf("Sending packet %d to %s port %d\n", j, neighbour_list[j].ip, neighbour_list[j].port ); //sprintf(buf, "Sending list"); char buffer[50*N]; node_list_to_char_arr(node_list, buffer, N); sendto(fd, buffer, strlen(buffer), 0, (struct sockaddr *)&remaddr, addrlen); } pthread_mutex_unlock(&mutex); } /*Check localClock for failing myself */ if(send_msges && localClock !=0 && localClock%P == 0){ if(get_fail_count() < B && fail_myself()){ printf("FAILING MYSELF %d\n",localClock); send_msges = 0; node_list[my_line_number].status = 0; } } i++; sleep(1); localClock++; printf("Localclock : %d\n", localClock); node_list[my_line_number].heartbeat = localClock; /*If c iterations completed or Time T is reached exit the send loop */ if(i==c || localClock >= T) break; } /*If c iterations completed before T, wait till localClock reaches T seconds */ while(localClock < T){ sleep(1); localClock++; } /*Write the final output to listX file */ char fileName[10]; sprintf(fileName, "list%d.txt", my_line_number); FILE *fp_new = fopen(fileName, "ab+"); fprintf(fp_new, "%s\n", node_list[my_line_number].status == 0 ? "FAIL" : "OK"); int index; for(i=0; i<N; i++){ fprintf(fp_new, "%d %d\n", i, node_list[i].heartbeat); } fclose(fp_new); for(i=0; i<N; i++){ printf("FINAL NODE LIST\n"); printf("Node %d: IP-%s Port-%d Status-%d Heartbeat-%d\n",i, node_list[i].ip, node_list[i].port, node_list[i].status, node_list[i].heartbeat); } }
int main(int argc, char **argv) { struct global *wps; if ((wps = calloc(1, sizeof(struct global)))) { wps->pke = 0; wps->pkr = 0; wps->e_hash1 = 0; wps->e_hash2 = 0; wps->authkey = 0; wps->e_nonce = 0; wps->r_nonce = 0; wps->e_bssid = 0; wps->psk1 = 0; wps->psk2 = 0; wps->dhkey = 0; wps->kdk = 0; wps->wrapkey = 0; wps->emsk = 0; wps->e_s1 = 0; wps->e_s2 = 0; wps->bruteforce = false; wps->verbosity = 2; wps->error = calloc(256, 1); if (!wps->error) goto memory_err; wps->error[0] = '\n'; } else { memory_err: fprintf(stderr, "\n [X] Memory allocation error!\n"); return MEM_ERROR; } int opt = 0; int long_index = 0; opt = getopt_long(argc, argv, option_string, long_options, &long_index); while (opt != -1) { switch (opt) { case 'e': wps->pke = malloc(WPS_PUBKEY_LEN); if (!wps->pke) goto memory_err; if (hex_string_to_byte_array(optarg, wps->pke, WPS_PUBKEY_LEN)) { snprintf(wps->error, 256, "\n [!] Bad enrollee public key -- %s\n\n", optarg); goto usage_err; } break; case 'r': wps->pkr = malloc(WPS_PUBKEY_LEN); if (!wps->pkr) goto memory_err; if (hex_string_to_byte_array(optarg, wps->pkr, WPS_PUBKEY_LEN)) { snprintf(wps->error, 256, "\n [!] Bad registrar public key -- %s\n\n", optarg); goto usage_err; } break; case 's': wps->e_hash1 = malloc(WPS_HASH_LEN); if (!wps->e_hash1) goto memory_err; if (hex_string_to_byte_array(optarg, wps->e_hash1, WPS_HASH_LEN)) { snprintf(wps->error, 256, "\n [!] Bad hash -- %s\n\n", optarg); goto usage_err; } break; case 'z': wps->e_hash2 = malloc(WPS_HASH_LEN); if (!wps->e_hash2) goto memory_err; if (hex_string_to_byte_array(optarg, wps->e_hash2, WPS_HASH_LEN)) { snprintf(wps->error, 256, "\n [!] Bad hash -- %s\n\n", optarg); goto usage_err; } break; case 'a': wps->authkey = malloc(WPS_AUTHKEY_LEN); if (!wps->authkey) goto memory_err; if (hex_string_to_byte_array(optarg, wps->authkey, WPS_HASH_LEN)) { snprintf(wps->error, 256, "\n [!] Bad authentication session key -- %s\n\n", optarg); goto usage_err; } break; case 'n': wps->e_nonce = malloc(WPS_NONCE_LEN); if (!wps->e_nonce) goto memory_err; if (hex_string_to_byte_array(optarg, wps->e_nonce, WPS_NONCE_LEN)) { snprintf(wps->error, 256, "\n [!] Bad enrollee nonce -- %s\n\n", optarg); goto usage_err; } break; case 'm': wps->r_nonce = malloc(WPS_NONCE_LEN); if (!wps->r_nonce) goto memory_err; if (hex_string_to_byte_array(optarg, wps->r_nonce, WPS_NONCE_LEN)) { snprintf(wps->error, 256, "\n [!] Bad registrar nonce -- %s\n\n", optarg); goto usage_err; } break; case 'b': wps->e_bssid = malloc(WPS_BSSID_LEN); if (!wps->e_bssid) goto memory_err; if (hex_string_to_byte_array(optarg, wps->e_bssid, WPS_BSSID_LEN)) { snprintf(wps->error, 256, "\n [!] Bad enrollee MAC address -- %s\n\n", optarg); goto usage_err; } break; case 'S': wps->small_dh_keys = true; break; case 'f': wps->bruteforce = true; break; case 'v': if (get_int(optarg, &wps->verbosity) != 0 || wps->verbosity < 1 || 3 < wps->verbosity) { snprintf(wps->error, 256, "\n [!] Bad verbosity level -- %s\n\n", optarg); goto usage_err; }; break; case 'h': goto usage_err; case '?': default: fprintf(stderr, "%s -h for help\n", argv[0]); return ARG_ERROR; } opt = getopt_long(argc, argv, option_string, long_options, &long_index); } /* Not all required arguments have been supplied */ if (wps->pke == 0 || wps->e_hash1 == 0 || wps->e_hash2 == 0) { wps->error = "\n [!] Not all required arguments have been supplied!\n\n"; usage_err: fprintf(stderr, usage, VERSION, argv[0], wps->error); return ARG_ERROR; } /* If --dh-small is selected then no --pkr should be supplied */ if (wps->pkr && wps->small_dh_keys) { wps->error = "\n [!] Options --dh-small and --pkr are mutually exclusive!\n\n"; goto usage_err; } /* Either --pkr or --dh-small must be specified */ if (!wps->pkr && !wps->small_dh_keys) { wps->error = "\n [!] Either --pkr or --dh-small must be specified!\n\n"; goto usage_err; } if (wps->small_dh_keys) { /* Small DH keys selected */ wps->pkr = malloc(WPS_PUBKEY_LEN); if (!wps->pkr) goto memory_err; /* g^A mod p = 2 (g = 2, A = 1, p > 2) */ memset(wps->pkr, 0, WPS_PUBKEY_LEN - 1); wps->pkr[WPS_PUBKEY_LEN - 1] = 0x02; if (!wps->authkey) { if (wps->e_nonce) { if (wps->r_nonce) { if (wps->e_bssid) { /* Computing AuthKey */ wps->dhkey = malloc(WPS_HASH_LEN); if (!wps->dhkey) goto memory_err; wps->kdk = malloc(WPS_HASH_LEN); if (!wps->kdk) goto memory_err; unsigned char *buffer = malloc(WPS_NONCE_LEN * 2 + WPS_BSSID_LEN); if (!buffer) goto memory_err; /* DHKey = SHA-256(g^(AB) mod p) = SHA-256(PKe^A mod p) = SHA-256(PKe) (g = 2, A = 1, p > 2) */ sha256(wps->pke, WPS_PUBKEY_LEN, wps->dhkey); memcpy(buffer, wps->e_nonce, WPS_NONCE_LEN); memcpy(buffer + WPS_NONCE_LEN, wps->e_bssid, WPS_BSSID_LEN); memcpy(buffer + WPS_NONCE_LEN + WPS_BSSID_LEN, wps->r_nonce, WPS_NONCE_LEN); /* KDK = HMAC-SHA-256{DHKey}(Enrollee nonce || Enrollee MAC || Registrar nonce) */ hmac_sha256(wps->dhkey, WPS_HASH_LEN, buffer, WPS_NONCE_LEN * 2 + WPS_BSSID_LEN, wps->kdk); buffer = realloc(buffer, WPS_HASH_LEN * 3); if (!buffer) goto memory_err; /* Key derivation function */ kdf(wps->kdk, WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN, buffer); wps->authkey = malloc(WPS_AUTHKEY_LEN); if (!wps->authkey) goto memory_err; memcpy(wps->authkey, buffer, WPS_AUTHKEY_LEN); if (wps->verbosity > 2) { wps->wrapkey = malloc(WPS_KEYWRAPKEY_LEN); if (!wps->wrapkey) goto memory_err; wps->emsk = malloc(WPS_EMSK_LEN); if (!wps->emsk) goto memory_err; memcpy(wps->wrapkey, buffer + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN); memcpy(wps->emsk, buffer + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN, WPS_EMSK_LEN); } if (wps->verbosity < 3) { free(wps->dhkey); free(wps->kdk); } free(buffer); } else { wps->error = "\n [!] Neither --authkey and --e-bssid have been supplied!\n\n"; goto usage_err; } } else { wps->error = "\n [!] Neither --authkey and --r-nonce have been supplied!\n\n"; goto usage_err; } } else { wps->error = "\n [!] Neither --authkey and --e-nonce have been supplied!\n\n"; goto usage_err; } } } /* E-S1 = E-S2 = 0 */ wps->e_s1 = calloc(WPS_SECRET_NONCE_LEN, 1); if (!wps->e_s1) goto memory_err; wps->e_s2 = calloc(WPS_SECRET_NONCE_LEN, 1); if (!wps->e_s2) goto memory_err; /* Allocating memory for digests */ wps->psk1 = malloc(WPS_HASH_LEN); if (!wps->psk1) goto memory_err; wps->psk2 = malloc(WPS_HASH_LEN); if (!wps->psk2) goto memory_err; unsigned char *result = (unsigned char *) malloc(WPS_HASH_LEN); if (!result) goto memory_err; unsigned char *buffer = (unsigned char *) malloc(WPS_SECRET_NONCE_LEN + WPS_PSK_LEN + WPS_PUBKEY_LEN * 2); if (!buffer) goto memory_err; uint32_t seed; uint32_t print_seed; /* Seed to display at the end */ unsigned int first_half; unsigned int second_half; unsigned char s_pin[4] = {0}; bool valid = false; int mode = 1; bool found = false; struct timeval t0, t1; gettimeofday(&t0, 0); while (mode <= MAX_MODE && !found) { seed = 0; print_seed = 0; /* ES-1 = ES-2 = E-Nonce */ if (mode == 2 && wps->e_nonce) { memcpy(wps->e_s1, wps->e_nonce, WPS_SECRET_NONCE_LEN); memcpy(wps->e_s2, wps->e_nonce, WPS_SECRET_NONCE_LEN); } /* PRNG bruteforce (rand_r) */ if (mode == 3 && wps->e_nonce) { /* Reducing entropy from 32 to 25 bits */ uint32_t index = wps->e_nonce[0] << 25; uint32_t limit = index | 0x01ffffff; while (1) { seed = index; int i; for (i = 1; i < WPS_NONCE_LEN; i++) { if (wps->e_nonce[i] != (unsigned char) rand_r(&seed)) break; } if (i == WPS_NONCE_LEN) { /* Seed found */ print_seed = seed; /* Advance to get ES-1 */ for (i = 0; i < WPS_SECRET_NONCE_LEN; i++) wps->e_s1[i] = (unsigned char) rand_r(&seed); /* Advance to get ES-2 */ for (i = 0; i < WPS_SECRET_NONCE_LEN; i++) wps->e_s2[i] = (unsigned char) rand_r(&seed); break; } if (index == limit) break; /* Complete bruteforce exausted */ index++; } } /* PRNG bruteforce (random_r) */ if (mode == 4 && wps->e_nonce) { /* Checks if the sequence may actually be generated by current random function */ if (wps->e_nonce[0] < 0x80 && wps->e_nonce[4] < 0x80 && wps->e_nonce[8] < 0x80 && wps->e_nonce[12] < 0x80) { valid = true; /* Converting enrollee nonce to the sequence may be generated by current random function */ uint32_t randr_enonce[4] = {0}; int j = 0; for (int i = 0; i < 4; i++) { randr_enonce[i] |= wps->e_nonce[j++]; randr_enonce[i] <<= 8; randr_enonce[i] |= wps->e_nonce[j++]; randr_enonce[i] <<= 8; randr_enonce[i] |= wps->e_nonce[j++]; randr_enonce[i] <<= 8; randr_enonce[i] |= wps->e_nonce[j++]; } uint32_t limit; struct timeval curr_time; gettimeofday(&curr_time, 0); if (wps->bruteforce) { seed = curr_time.tv_sec + SEC_PER_DAY * MODE4_DAYS - SEC_PER_HOUR * 2; limit = 0; } else { seed = curr_time.tv_sec + SEC_PER_HOUR * 2; limit = curr_time.tv_sec - SEC_PER_DAY * MODE4_DAYS - SEC_PER_HOUR * 2; } struct random_data *buf = (struct random_data *) calloc(1, sizeof(struct random_data)); char *rand_statebuf = (char *) calloc(1, 128); initstate_r(seed, rand_statebuf, 128, buf); int32_t res = 0; while (1) { srandom_r(seed, buf); int i; for (i = 0; i < 4; i++) { random_r(buf, &res); if (res != randr_enonce[i]) break; } if (i == 4) { print_seed = seed; srandom_r(print_seed + 1, buf); for (int i = 0; i < 4; i++) { random_r(buf, &res); uint32_t be = __be32_to_cpu(res); memcpy(&(wps->e_s1[4 * i]), &be, 4); memcpy(wps->e_s2, wps->e_s1, WPS_SECRET_NONCE_LEN); /* ES-1 = ES-2 != E-Nonce */ } } if (print_seed || seed == limit) { free(buf); free(rand_statebuf); break; } seed--; } } } /* WPS pin cracking */ if (mode == 1 || (mode == 2 && wps->e_nonce) || (mode == 3 && print_seed) || (mode == 4 && print_seed)) { crack: first_half = 0; second_half = 0; while (first_half < 10000) { uint_to_char_array(first_half, 4, s_pin); hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, (unsigned char *) s_pin, 4, wps->psk1); memcpy(buffer, wps->e_s1, WPS_SECRET_NONCE_LEN); memcpy(buffer + WPS_SECRET_NONCE_LEN, wps->psk1, WPS_PSK_LEN); memcpy(buffer + WPS_SECRET_NONCE_LEN + WPS_PSK_LEN, wps->pke, WPS_PUBKEY_LEN); memcpy(buffer + WPS_SECRET_NONCE_LEN + WPS_PSK_LEN + WPS_PUBKEY_LEN, wps->pkr, WPS_PUBKEY_LEN); hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, buffer, WPS_SECRET_NONCE_LEN + WPS_PSK_LEN + WPS_PUBKEY_LEN * 2, result); if (memcmp(result, wps->e_hash1, WPS_HASH_LEN)) { first_half++; } else { break; } } if (first_half < 10000) { /* First half found */ unsigned char checksum_digit; unsigned int c_second_half; /* Testing with checksum digit */ while (second_half < 1000) { checksum_digit = wps_pin_checksum(first_half * 1000 + second_half); c_second_half = second_half * 10 + checksum_digit; uint_to_char_array(c_second_half, 4, s_pin); hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, (unsigned char *) s_pin, 4, wps->psk2); memcpy(buffer, wps->e_s2, WPS_SECRET_NONCE_LEN); memcpy(buffer + WPS_SECRET_NONCE_LEN, wps->psk2, WPS_PSK_LEN); memcpy(buffer + WPS_SECRET_NONCE_LEN + WPS_PSK_LEN, wps->pke, WPS_PUBKEY_LEN); memcpy(buffer + WPS_SECRET_NONCE_LEN + WPS_PSK_LEN + WPS_PUBKEY_LEN, wps->pkr, WPS_PUBKEY_LEN); hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, buffer, WPS_SECRET_NONCE_LEN + WPS_PSK_LEN + WPS_PUBKEY_LEN * 2, result); if (memcmp(result, wps->e_hash2, WPS_HASH_LEN)) { second_half++; } else { second_half = c_second_half; found = true; break; } } /* Testing without checksum digit */ if (!found) { second_half = 0; while (second_half < 10000) { /* If already tested skip */ if (wps_pin_valid(first_half * 10000 + second_half)) { second_half++; continue; } uint_to_char_array(second_half, 4, s_pin); hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, (unsigned char *) s_pin, 4, wps->psk2); memcpy(buffer, wps->e_s2, WPS_SECRET_NONCE_LEN); memcpy(buffer + WPS_SECRET_NONCE_LEN, wps->psk2, WPS_PSK_LEN); memcpy(buffer + WPS_SECRET_NONCE_LEN + WPS_PSK_LEN, wps->pke, WPS_PUBKEY_LEN); memcpy(buffer + WPS_SECRET_NONCE_LEN + WPS_PSK_LEN + WPS_PUBKEY_LEN, wps->pkr, WPS_PUBKEY_LEN); hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, buffer, WPS_SECRET_NONCE_LEN + WPS_PSK_LEN + WPS_PUBKEY_LEN * 2, result); if (memcmp(result, wps->e_hash2, WPS_HASH_LEN)) { second_half++; } else { found = true; break; } } } } } /* E-S1 = E-Nonce != E-S2 */ if (mode == 4 && print_seed && !found) { memcpy(wps->e_s1, wps->e_nonce, WPS_SECRET_NONCE_LEN); mode++; goto crack; } mode++; } gettimeofday(&t1, 0); long elapsed_s = t1.tv_sec - t0.tv_sec; mode--; printf("\n Pixiewps %s\n", VERSION); if (found) { if (wps->e_nonce) { if ((mode == 3 || mode == 4) && wps->verbosity > 2) { printf("\n [*] PRNG Seed: %u", print_seed); } if (mode == 4 && wps->verbosity > 2) { time_t seed_time; struct tm ts; char buffer[30]; seed_time = print_seed; ts = *localtime(&seed_time); strftime(buffer, 30, "%c", &ts); printf(" (%s)", buffer); } } if (wps->verbosity > 2) { if (wps->dhkey) { /* To see if AuthKey was supplied or not */ printf("\n [*] DHKey: "); byte_array_print(wps->dhkey, WPS_HASH_LEN); printf("\n [*] KDK: "); byte_array_print(wps->kdk, WPS_HASH_LEN); printf("\n [*] AuthKey: "); byte_array_print(wps->authkey, WPS_AUTHKEY_LEN); printf("\n [*] EMSK: "); byte_array_print(wps->emsk, WPS_EMSK_LEN); printf("\n [*] KeyWrapKey: "); byte_array_print(wps->wrapkey, WPS_KEYWRAPKEY_LEN); } printf("\n [*] PSK1: "); byte_array_print(wps->psk1, WPS_PSK_LEN); printf("\n [*] PSK2: "); byte_array_print(wps->psk2, WPS_PSK_LEN); } if (wps->verbosity > 1) { printf("\n [*] E-S1: "); byte_array_print(wps->e_s1, WPS_SECRET_NONCE_LEN); printf("\n [*] E-S2: "); byte_array_print(wps->e_s2, WPS_SECRET_NONCE_LEN); } printf("\n [+] WPS pin: %04u%04u", first_half, second_half); } else { printf("\n [-] WPS pin not found!"); } printf("\n\n [*] Time taken: %lu s\n\n", elapsed_s); if (!found && mode == 4 && valid && !wps->bruteforce) { printf(" [!] The AP /might be/ vulnerable to mode 4. Try again with --force or with another (newer) set of data.\n\n"); } free(result); free(buffer); free(wps->pke); free(wps->pkr); free(wps->e_hash1); free(wps->e_hash2); free(wps->authkey); free(wps->e_nonce); free(wps->r_nonce); free(wps->e_bssid); free(wps->psk1); free(wps->psk2); free(wps->e_s1); free(wps->e_s2); free(wps->error); if (wps->verbosity > 2) { free(wps->dhkey); free(wps->kdk); free(wps->wrapkey); free(wps->emsk); } free(wps); return (!found); /* 0 success, 1 failure */ }