mkp224o/main.c

1303 lines
29 KiB
C
Raw Normal View History

2017-10-06 03:21:00 +02:00
#ifdef __linux__
#define _POSIX_C_SOURCE 200112L
#endif
2017-09-24 21:13:16 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
2017-09-25 19:49:47 +02:00
#include <time.h>
2017-09-24 21:13:16 +02:00
#include <pthread.h>
2017-09-25 19:49:47 +02:00
#include <signal.h>
2017-09-27 20:38:15 +02:00
#include <sodium/randombytes.h>
2017-09-24 21:13:16 +02:00
#include "types.h"
2017-09-27 20:38:15 +02:00
#include "likely.h"
2017-09-24 21:13:16 +02:00
#include "vec.h"
#include "base32.h"
2017-10-06 02:14:33 +02:00
#include "cpucount.h"
2017-09-24 21:13:16 +02:00
#include "keccak.h"
2017-10-02 16:11:23 +02:00
#include "ed25519/ed25519.h"
#include "ioutil.h"
2017-09-24 21:13:16 +02:00
2017-10-23 06:29:37 +02:00
// additional 0 terminator is added by C
2017-09-24 21:13:16 +02:00
static const char * const pkprefix = "== ed25519v1-public: type0 ==\0\0";
#define pkprefixlen (29 + 3)
static const char * const skprefix = "== ed25519v1-secret: type0 ==\0\0";
#define skprefixlen (29 + 3)
static const char * const checksumstr = ".onion checksum";
#define checksumstrlen 15
// output directory
static char *workdir = 0;
static size_t workdirlen = 0;
static int quietflag = 0;
#define SECRET_LEN 64
#define PUBLIC_LEN 32
#define SEED_LEN 32
// with checksum + version num
#define PUBONION_LEN (PUBLIC_LEN + 3)
// with newline included
#define ONIONLEN 62
static size_t onionendpos; // end of .onion within string
static size_t direndpos; // end of dir before .onion within string
static size_t printstartpos; // where to start printing from
static size_t printlen; // precalculated, related to printstartpos
static pthread_mutex_t fout_mutex;
2017-09-25 19:49:47 +02:00
static FILE *fout;
static size_t numneedgenerate = 0;
2017-10-22 07:07:45 +02:00
static int numwords = 1;
2017-09-25 19:49:47 +02:00
static pthread_mutex_t keysgenerated_mutex;
2017-09-24 21:13:16 +02:00
static volatile size_t keysgenerated = 0;
2017-09-25 19:49:47 +02:00
static volatile int endwork = 0;
static void termhandler(int sig)
{
switch (sig) {
case SIGTERM:
case SIGINT:
endwork = 1;
break;
}
}
2017-09-24 21:13:16 +02:00
2017-09-30 05:40:12 +02:00
// filters stuff
#ifdef INTFILTER
#ifdef BINSEARCH
#ifndef BESORT
#define OMITMASK
#endif
#endif
#endif
2017-10-22 07:07:45 +02:00
#ifdef OMITMASK
#define EXPANDMASK
#endif
2017-10-10 03:02:43 +02:00
#ifndef BINFILTERLEN
#define BINFILTERLEN PUBLIC_LEN
#endif
2017-09-24 23:52:17 +02:00
struct binfilter {
2017-10-10 03:02:43 +02:00
u8 f[BINFILTERLEN];
2017-09-24 23:52:17 +02:00
size_t len; // real len minus one
u8 mask;
} ;
2017-09-27 20:07:33 +02:00
#ifdef INTFILTER
struct intfilter {
2017-10-09 17:58:35 +02:00
IFT f;
# ifndef OMITMASK
2017-10-09 17:58:35 +02:00
IFT m;
# endif
2017-09-27 20:07:33 +02:00
} ;
VEC_STRUCT(ifiltervec,struct intfilter) ifilters;
# ifdef OMITMASK
2017-10-09 17:58:35 +02:00
IFT ifiltermask;
# endif // BINSEARCH
2017-10-09 17:58:35 +02:00
#else // INTFILTER
2017-09-27 20:07:33 +02:00
VEC_STRUCT(bfiltervec,struct binfilter) bfilters;
2017-10-09 17:58:35 +02:00
#endif // INTFILTER
2017-09-24 23:52:17 +02:00
2017-09-24 21:13:16 +02:00
static void filters_init()
{
2017-09-27 20:07:33 +02:00
#ifdef INTFILTER
VEC_INIT(ifilters);
#else
2017-09-24 23:52:17 +02:00
VEC_INIT(bfilters);
2017-09-27 20:07:33 +02:00
#endif
2017-09-24 21:13:16 +02:00
}
#ifdef INTFILTER
2017-10-21 20:42:19 +02:00
static void filter_sort(int (*compf)(const void *,const void *))
{
2017-10-21 20:42:19 +02:00
qsort(&VEC_BUF(ifilters,0),VEC_LENGTH(ifilters),sizeof(struct intfilter),compf);
}
2017-10-21 20:42:19 +02:00
2017-10-22 07:07:45 +02:00
static inline size_t filter_len(size_t i)
{
# ifndef OMITMASK
const u8 *m = (const u8 *)&VEC_BUF(ifilters,i).m;
# else // OMITMASK
const u8 *m = (const u8 *)&ifiltermask;
# endif // OMITMASK
size_t c = 0;
for (size_t j = 0;;) {
u8 v = m[j];
for (size_t k = 0;;) {
if (!v)
return c;
++c;
if (++k >= 8)
break;
v <<= 1;
}
if (++j >= sizeof(IFT))
break;
}
return c;
}
# ifdef OMITMASK
2017-10-21 20:42:19 +02:00
static int filter_compare(const void *p1,const void *p2)
{
2017-10-21 20:42:19 +02:00
if (((const struct intfilter *)p1)->f < ((const struct intfilter *)p2)->f)
return -1;
2017-10-21 20:42:19 +02:00
if (((const struct intfilter *)p1)->f > ((const struct intfilter *)p2)->f)
return 1;
2017-10-21 20:42:19 +02:00
return 0;
}
2017-10-22 07:07:45 +02:00
# ifdef EXPANDMASK
2017-10-09 17:24:50 +02:00
/*
* raw representation -- FF.FF.F0.00
* big endian -- 0xFFFFF000
* little endian -- 0x00F0FFFF
* b: 0xFFffF000 ^ 0xFFff0000 -> 0x0000F000
* 0x0000F000 + 1 -> 0x0000F001
* 0x0000F000 & 0x0000F001 -> 0x0000F000 <- shifted mask
* 0x0000F000 ^ 0x0000F000 -> 0x00000000 <- direct mask
* 0x0000F000 ^ 0x00000000 -> 0x0000F000 <- shifted mask
* l: 0x00f0FFff ^ 0x0000FFff -> 0x00f00000
* 0x00f00000 + 1 -> 0x00f00001
* 0x00f00000 & 0x00f00001 -> 0x00f00000 <- shifted mask
* 0x00f00000 ^ 0x00f00000 -> 0x00000000 <- direct mask
* 0x00f00000 ^ 0x00000000 -> 0x00f00000 <- shifted mask
*
* b: 0xFFffFFff ^ 0xF0000000 -> 0x0FffFFff
* 0x0FffFFff + 1 -> 0x10000000
* 0x0FffFFff & 0x10000000 -> 0x00000000 <- shifted mask
* 0x0FffFFff ^ 0x00000000 -> 0x0FffFFff <- direct mask
* 0x0FffFFff ^ 0x0FffFFff -> 0x00000000 <- shifted mask
* l: 0xFFffFFff ^ 0x000000f0 -> 0xFFffFF0f
* 0xFFffFF0f + 1 -> 0xFFffFF10
* 0xFFffFF0f & 0xFFffFF10 -> 0xFFffFF00 <- shifted mask
* 0xFFffFF0f ^ 0xFFffFF00 -> 0x0000000f <- direct mask
* 0xFFffFF0f ^ 0x0000000f -> 0xFFffFF00 <- shifted mask
*
* essentially, we have to make direct mask + shifted mask bits worth of information
* and then split it into 2 parts
* we do not need absolute shifted mask shifting value, just relative to direct mask
* 0x0sss00dd - shifted & direct mask combo
* 0x000sssdd - combined mask
* 8 - relshiftval
* generate values from 0x00000000 to 0x000sssdd
* for each value, realmask <- (val & 0x000000dd) | ((val & 0x000sss00) << relshiftval)
* or..
* realmask <- (val & 0x000000dd) | ((val << relshiftval) & 0x0sss0000)
* ...
* above method doesn't work in some cases. better way:
* l: 0x80ffFFff ^ 0x00f0FFff -> 0x800f0000
* 0x800f0000 >> 16 -> 0x0000800f
* 0x0000800f + 1 -> 0x00008010
* 0x0000800f & 0x00008010 -> 0x00008000 <- smask
* 0x0000800f ^ 0x00008000 -> 0x0000000f <- dmask
2017-10-09 17:24:50 +02:00
*/
2017-10-09 18:12:13 +02:00
#define EXPVAL(init,j,dmask,smask,ishift,rshift) \
((init) | ((((j) & (dmask)) | (((j) << (rshift)) & (smask))) << (ishift)))
// add expanded set of values
// allocates space on its own
static void ifilter_addexpanded(
struct intfilter *ifltr,
2017-10-09 18:12:13 +02:00
IFT dmask,IFT smask,IFT cmask,
int ishift,int rshift)
{
size_t i = VEC_LENGTH(ifilters);
VEC_ADDN(ifilters,cmask + 1);
for (size_t j = 0;;++j) {
2017-10-23 08:40:34 +02:00
VEC_BUF(ifilters,i + j).f =
EXPVAL(ifltr->f,j,dmask,smask,ishift,rshift);
if (j == cmask)
break;
2017-10-09 18:12:13 +02:00
}
}
// expand existing stuff
// allocates needed stuff on its own
static void ifilter_expand(IFT dmask,IFT smask,IFT cmask,int ishift,int rshift)
2017-10-09 18:12:13 +02:00
{
size_t len = VEC_LENGTH(ifilters);
VEC_ADDN(ifilters,cmask * len);
size_t esz = cmask + 1; // size of expanded elements
for (size_t i = len - 1;;--i) {
for (IFT j = 0;;++j) {
2017-10-23 08:40:34 +02:00
VEC_BUF(ifilters,i * esz + j).f =
EXPVAL(VEC_BUF(ifilters,i).f,j,dmask,smask,ishift,rshift);
if (j == cmask)
break;
}
if (i == 0)
break;
}
2017-10-09 18:12:13 +02:00
}
2017-10-09 17:58:35 +02:00
static inline void ifilter_addflatten(struct intfilter *ifltr,IFT mask)
2017-10-09 17:24:50 +02:00
{
if (VEC_LENGTH(ifilters) == 0) {
// simple
VEC_ADD(ifilters,*ifltr);
2017-10-09 17:58:35 +02:00
ifiltermask = mask;
2017-10-09 17:24:50 +02:00
return;
}
2017-10-09 17:58:35 +02:00
if (ifiltermask == mask) {
// lucky
VEC_ADD(ifilters,*ifltr);
2017-10-09 17:24:50 +02:00
return;
}
2017-10-09 17:58:35 +02:00
IFT cross = ifiltermask ^ mask;
2017-10-09 17:24:50 +02:00
int ishift = 0;
while ((cross & 1) == 0) {
++ishift;
cross >>= 1;
}
IFT smask = cross & (cross + 1); // shift mask
IFT dmask = cross ^ smask; // direct mask
IFT cmask; // combined mask
2017-10-09 17:58:35 +02:00
int rshift = 0; // relative shift
while (cmask = (smask >> rshift) | dmask,(cmask & (cmask + 1)) != 0)
2017-10-09 17:24:50 +02:00
++rshift;
// preparations done
2017-10-09 17:58:35 +02:00
if (ifiltermask > mask) {
2017-10-09 17:24:50 +02:00
// already existing stuff has more precise mask than we
// so we need to expand our stuff
ifilter_addexpanded(ifltr,dmask,smask,cmask,ishift,rshift);
}
else {
ifiltermask = mask;
ifilter_expand(dmask,smask,cmask,ishift,rshift);
VEC_ADD(ifilters,*ifltr);
2017-10-09 17:24:50 +02:00
}
}
2017-10-22 07:07:45 +02:00
# endif // EXPANDMASK
# else // OMITMASK
2017-10-21 20:42:19 +02:00
/*
* struct intfilter layout: filter,mask
* stuff is compared in big-endian way, so memcmp
* filter needs to be compared first
* if its equal, mask needs to be compared
* memcmp is aplicable there too
* due to struct intfilter layout, it all can be stuffed into one memcmp call
*/
static int filter_compare(const void *p1,const void *p2)
{
return memcmp(p1,p2,sizeof(struct intfilter));
}
# endif // OMITMASK
2017-10-21 20:42:19 +02:00
#else // INTFILTER
2017-10-22 07:07:45 +02:00
static inline size_t filter_len(size_t i)
{
size_t c = VEC_BUF(bfilters,i).len * 8;
u8 v = VEC_BUF(bfilters,i).mask;
for (size_t k = 0;;) {
if (!v)
return c;
++c;
if (++k >= 8)
return c;
v <<= 1;
}
}
2017-10-21 20:42:19 +02:00
static void filter_sort(int (*compf)(const void *,const void *))
{
qsort(&VEC_BUF(bfilters,0),VEC_LENGTH(bfilters),sizeof(struct binfilter),compf);
}
static int filter_compare(const void *p1,const void *p2)
{
const struct binfilter *b1 = (const struct binfilter *)p1;
const struct binfilter *b2 = (const struct binfilter *)p2;
size_t l = b1->len <= b2->len ? b1->len : b2->len;
int cmp = memcmp(b1->f,b2->f,l);
if (cmp)
return cmp;
if (b1->len < b2->len)
return -1;
if (b1->len > b2->len)
return 1;
if (b1->mask < b2->mask)
return -1;
if (b1->mask > b2->mask)
return 1;
return 0;
}
2017-10-09 17:24:50 +02:00
#endif // INTFILTER
2017-09-24 21:13:16 +02:00
static void filters_add(const char *filter)
{
2017-09-24 23:52:17 +02:00
struct binfilter bf;
size_t ret;
2017-09-27 20:07:33 +02:00
#ifdef INTFILTER
union intconv {
2017-10-08 23:15:08 +02:00
IFT i;
u8 b[sizeof(IFT)];
2017-09-27 20:07:33 +02:00
} fc,mc;
#endif
2017-09-24 23:52:17 +02:00
2017-10-10 03:02:43 +02:00
// skip regex start symbol. we do not support regex tho
if (*filter == '^')
++filter;
memset(&bf,0,sizeof(bf));
2017-09-25 19:49:47 +02:00
if (!base32_valid(filter,&ret)) {
2017-10-10 03:02:43 +02:00
fprintf(stderr,"filter \"%s\" is invalid\n",filter);
2017-09-25 19:49:47 +02:00
return;
}
ret = BASE32_FROM_LEN(ret);
2017-09-24 23:52:17 +02:00
if (!ret)
return;
2017-09-27 20:07:33 +02:00
#ifdef INTFILTER
2017-10-08 23:15:08 +02:00
if (ret > sizeof(IFT))
2017-09-27 20:07:33 +02:00
#else
2017-10-10 03:02:43 +02:00
if (ret > sizeof(bf.f))
2017-09-27 20:07:33 +02:00
#endif
{
2017-10-10 03:02:43 +02:00
fprintf(stderr,"filter \"%s\" is too long\n",filter);
2017-09-25 19:49:47 +02:00
return;
}
base32_from(bf.f,&bf.mask,filter);
2017-09-24 23:52:17 +02:00
bf.len = ret - 1;
2017-09-27 20:07:33 +02:00
#ifdef INTFILTER
mc.i = 0;
for (size_t i = 0;i < bf.len;++i)
mc.b[i] = 0xFF;
mc.b[bf.len] = bf.mask;
2017-10-08 23:15:08 +02:00
memcpy(fc.b,bf.f,sizeof(fc.b));
2017-09-27 20:07:33 +02:00
fc.i &= mc.i;
2017-10-09 17:58:35 +02:00
struct intfilter ifltr = {
.f = fc.i,
# ifndef OMITMASK
2017-10-09 17:58:35 +02:00
.m = mc.i,
2017-10-21 20:42:19 +02:00
# endif
2017-10-09 17:58:35 +02:00
};
# ifdef OMITMASK
ifilter_addflatten(&ifltr,mc.i);
# else // OMITMASK
2017-10-09 00:16:16 +02:00
VEC_ADD(ifilters,ifltr);
# endif // OMITMASK
2017-10-09 00:16:16 +02:00
#else // INTFILTER
2017-10-10 03:02:43 +02:00
VEC_ADD(bfilters,bf);
2017-10-09 00:16:16 +02:00
#endif // INTFILTER
2017-09-24 21:13:16 +02:00
}
static void filters_prepare()
{
if (!quietflag)
2017-10-21 20:42:19 +02:00
fprintf(stderr,"sorting filters...");
2017-10-22 02:40:23 +02:00
filter_sort(&filter_compare);
2017-10-21 20:42:19 +02:00
if (!quietflag)
fprintf(stderr," done.\n");
// TODO remove duplicates
}
2017-09-24 21:13:16 +02:00
static void filters_clean()
{
2017-09-27 20:07:33 +02:00
#ifdef INTFILTER
VEC_FREE(ifilters);
#else
2017-09-24 23:52:17 +02:00
VEC_FREE(bfilters);
2017-09-27 20:07:33 +02:00
#endif
2017-09-24 21:13:16 +02:00
}
2017-09-25 22:57:27 +02:00
static size_t filters_count()
{
2017-09-27 20:07:33 +02:00
#ifdef INTFILTER
return VEC_LENGTH(ifilters);
#else
2017-09-25 22:57:27 +02:00
return VEC_LENGTH(bfilters);
2017-09-27 20:07:33 +02:00
#endif
2017-09-25 22:57:27 +02:00
}
2017-10-22 02:40:23 +02:00
#ifdef STATISTICS
#define ADDNUMSUCCESS ++st->numsuccess.v
#else
2017-10-22 07:07:45 +02:00
#define ADDNUMSUCCESS do ; while (0)
2017-10-22 02:40:23 +02:00
#endif
2017-09-27 20:07:33 +02:00
#ifdef INTFILTER
2017-10-22 02:40:23 +02:00
# ifndef BINSEARCH
2017-09-27 20:07:33 +02:00
2017-10-10 03:02:43 +02:00
#define MATCHFILTER(it,pk) \
((*(IFT *)(pk) & VEC_BUF(ifilters,it).m) == VEC_BUF(ifilters,it).f)
2017-10-22 07:07:45 +02:00
#define DOFILTER(it,pk,code) \
do { \
2017-10-10 03:02:43 +02:00
for (it = 0;it < VEC_LENGTH(ifilters);++it) { \
if (unlikely(MATCHFILTER(it,pk))) { \
code; \
break; \
} \
} \
2017-10-22 07:07:45 +02:00
} while (0)
2017-10-10 03:02:43 +02:00
2017-10-22 02:40:23 +02:00
# else // BINSEARCH
2017-10-10 03:02:43 +02:00
# ifdef OMITMASK
2017-10-22 07:07:45 +02:00
#define DOFILTER(it,pk,code) \
do { \
register IFT maskedpk = *(IFT *)(pk) & ifiltermask; \
2017-10-10 03:02:43 +02:00
for (size_t down = 0,up = VEC_LENGTH(ifilters);down < up;) { \
it = (up + down) / 2; \
if (maskedpk < VEC_BUF(ifilters,it).f) \
2017-10-10 03:02:43 +02:00
up = it; \
else if (maskedpk > VEC_BUF(ifilters,it).f) \
2017-10-10 03:02:43 +02:00
down = it + 1; \
else { \
code; \
break; \
} \
} \
2017-10-22 07:07:45 +02:00
} while (0)
2017-10-10 03:02:43 +02:00
# else // OMITMASK
2017-10-22 07:07:45 +02:00
#define DOFILTER(it,pk,code) \
do { \
for (size_t down = 0,up = VEC_LENGTH(ifilters);down < up;) { \
it = (up + down) / 2; \
IFT maskedpk = *(IFT *)(pk) & VEC_BUF(ifilters,it).m; \
register int cmp = memcmp(&maskedpk,&VEC_BUF(ifilters,it).f,sizeof(IFT)); \
if (cmp < 0) \
up = it; \
else if (cmp > 0) \
down = it + 1; \
else { \
code; \
break; \
} \
} \
2017-10-22 07:07:45 +02:00
} while (0)
# endif // OMITMASK
2017-10-22 02:40:23 +02:00
# endif // BINSEARCH
2017-10-10 03:02:43 +02:00
#else // INTFILTER
2017-10-22 02:40:23 +02:00
# ifndef BINSEARCH
2017-09-27 20:07:33 +02:00
#define MATCHFILTER(it,pk) ( \
memcmp(pk,VEC_BUF(bfilters,it).f,VEC_BUF(bfilters,it).len) == 0 && \
(pk[VEC_BUF(bfilters,it).len] & VEC_BUF(bfilters,it).mask) == VEC_BUF(bfilters,it).f[VEC_BUF(bfilters,it).len])
2017-10-22 07:07:45 +02:00
#define DOFILTER(it,pk,code) \
do { \
2017-10-10 03:02:43 +02:00
for (it = 0;it < VEC_LENGTH(bfilters);++it) { \
if (unlikely(MATCHFILTER(it,pk))) { \
code; \
break; \
} \
} \
2017-10-22 07:07:45 +02:00
} while (0)
2017-10-10 03:02:43 +02:00
2017-10-22 02:40:23 +02:00
# else // BINSEARCH
2017-10-10 03:02:43 +02:00
2017-10-22 07:07:45 +02:00
#define DOFILTER(it,pk,code) \
do { \
2017-10-10 03:02:43 +02:00
for (size_t down = 0,up = VEC_LENGTH(bfilters);down < up;) { \
it = (up + down) / 2; \
{ \
register int filterdiff = memcmp(pk,VEC_BUF(bfilters,it).f,VEC_BUF(bfilters,it).len); \
if (filterdiff < 0) { \
up = it; \
continue; \
} \
if (filterdiff > 0) { \
down = it + 1; \
continue; \
} \
} \
if ((pk[VEC_BUF(bfilters,it).len] & VEC_BUF(bfilters,it).mask) < \
VEC_BUF(bfilters,it).f[VEC_BUF(bfilters,it).len]) \
{ \
up = it; \
continue; \
} \
if ((pk[VEC_BUF(bfilters,it).len] & VEC_BUF(bfilters,it).mask) > \
VEC_BUF(bfilters,it).f[VEC_BUF(bfilters,it).len]) \
{ \
down = it + 1; \
continue; \
} \
{ \
code; \
break; \
} \
} \
2017-10-22 07:07:45 +02:00
} while (0)
2017-10-10 03:02:43 +02:00
2017-10-22 02:40:23 +02:00
# endif // BINSEARCH
2017-10-10 03:02:43 +02:00
#endif // INTFILTER
2017-09-27 20:07:33 +02:00
2017-09-24 21:13:16 +02:00
static void loadfilterfile(const char *fname)
{
char buf[128];
2017-09-30 05:40:12 +02:00
FILE *f = fopen(fname,"r");
while (fgets(buf,sizeof(buf),f)) {
for (char *p = buf;*p;++p) {
2017-10-06 02:14:33 +02:00
if (*p == '\n') {
2017-09-24 21:13:16 +02:00
*p = 0;
2017-09-30 05:40:12 +02:00
break;
}
}
if (*buf && *buf != '#' && memcmp(buf,"//",2) != 0)
2017-09-24 21:13:16 +02:00
filters_add(buf);
}
}
static void filters_print()
2017-09-24 21:13:16 +02:00
{
if (quietflag)
return;
2017-09-27 20:07:33 +02:00
size_t i,l;
#ifdef INTFILTER
l = VEC_LENGTH(ifilters);
#else
l = VEC_LENGTH(bfilters);
#endif
2017-09-25 19:49:47 +02:00
if (l)
2017-10-23 08:40:34 +02:00
fprintf(stderr,"filters:\n");
2017-09-25 19:49:47 +02:00
for (i = 0;i < l;++i) {
2017-09-25 01:18:23 +02:00
char buf0[256],buf1[256];
u8 bufx[128];
if (i >= 20) {
2017-10-23 08:40:34 +02:00
size_t notshown = l - i;
fprintf(stderr,"[another %zu %s not shown]\n",
notshown,notshown == 1 ? "filter" : "filters");
break;
}
2017-10-21 20:42:19 +02:00
#ifdef INTFILTER
size_t len = 0;
u8 *imraw;
# ifndef OMITMASK
2017-10-09 17:58:35 +02:00
imraw = (u8 *)&VEC_BUF(ifilters,i).m;
# else
2017-10-09 17:58:35 +02:00
imraw = (u8 *)&ifiltermask;
# endif
2017-10-08 23:15:08 +02:00
while (len < sizeof(IFT) && imraw[len] != 0x00) ++len;
2017-09-27 20:07:33 +02:00
u8 mask = imraw[len-1];
u8 *ifraw = (u8 *)&VEC_BUF(ifilters,i).f;
#else
2017-09-25 01:18:23 +02:00
size_t len = VEC_BUF(bfilters,i).len + 1;
2017-09-27 20:07:33 +02:00
u8 mask = VEC_BUF(bfilters,i).mask;
u8 *ifraw = VEC_BUF(bfilters,i).f;
#endif
base32_to(buf0,ifraw,len);
memcpy(bufx,ifraw,len);
bufx[len - 1] |= ~mask;
2017-09-25 01:18:23 +02:00
base32_to(buf1,bufx,len);
char *a = buf0,*b = buf1;
while (*a && *a == *b)
++a, ++b;
*a = 0;
2017-10-23 08:40:34 +02:00
fprintf(stderr,"\t%s\n",buf0);
2017-09-24 23:52:17 +02:00
}
2017-10-23 08:40:34 +02:00
fprintf(stderr,"totally %zu %s\n",l,l == 1 ? "filter" : "filters");
2017-09-24 21:13:16 +02:00
}
2017-09-30 05:40:12 +02:00
// statistics, if enabled
#ifdef STATISTICS
struct statstruct {
union {
2017-10-06 02:25:51 +02:00
u32 v;
size_t align;
} numcalc;
2017-09-30 05:40:12 +02:00
union {
2017-10-06 02:25:51 +02:00
u32 v;
size_t align;
} numsuccess;
union {
u32 v;
size_t align;
} numrestart;
2017-09-30 05:40:12 +02:00
} ;
VEC_STRUCT(statsvec,struct statstruct);
struct tstatstruct {
u64 numcalc;
u64 numsuccess;
u64 numrestart;
2017-09-30 05:40:12 +02:00
u32 oldnumcalc;
u32 oldnumsuccess;
u32 oldnumrestart;
2017-09-30 05:40:12 +02:00
} ;
VEC_STRUCT(tstatsvec,struct tstatstruct);
#endif
2017-09-24 21:13:16 +02:00
static void onionready(char *sname, const u8 *secret, const u8 *pubonion)
{
2017-09-25 19:49:47 +02:00
if (endwork)
2017-09-24 21:13:16 +02:00
return;
2017-09-25 19:49:47 +02:00
if (numneedgenerate) {
pthread_mutex_lock(&keysgenerated_mutex);
if (keysgenerated >= numneedgenerate) {
pthread_mutex_unlock(&keysgenerated_mutex);
return;
}
}
if (createdir(sname,1) != 0) {
2017-09-25 19:49:47 +02:00
if (numneedgenerate)
pthread_mutex_unlock(&keysgenerated_mutex);
return;
}
if (numneedgenerate) {
++keysgenerated;
if (keysgenerated >= numneedgenerate)
endwork = 1;
pthread_mutex_unlock(&keysgenerated_mutex);
}
2017-10-23 08:40:34 +02:00
strcpy(&sname[onionendpos],"/hs_ed25519_secret_key");
writetofile(sname,secret,skprefixlen + SECRET_LEN,1);
2017-09-25 19:49:47 +02:00
2017-10-23 08:40:34 +02:00
strcpy(&sname[onionendpos],"/hostname");
FILE *hfile = fopen(sname,"w");
if (hfile) {
2017-09-24 21:13:16 +02:00
sname[onionendpos] = '\n';
fwrite(&sname[direndpos],ONIONLEN + 1,1,hfile);
fclose(hfile);
2017-09-24 21:13:16 +02:00
}
2017-10-23 08:40:34 +02:00
strcpy(&sname[onionendpos],"/hs_ed25519_public_key");
writetofile(sname,pubonion,pkprefixlen + PUBLIC_LEN,0);
2017-09-24 21:13:16 +02:00
if (fout) {
2017-09-30 05:40:12 +02:00
sname[onionendpos] = '\n';
2017-09-25 19:49:47 +02:00
pthread_mutex_lock(&fout_mutex);
2017-10-23 08:40:34 +02:00
fwrite(&sname[printstartpos],printlen,1,fout);
2017-09-24 21:13:16 +02:00
fflush(fout);
2017-09-25 19:49:47 +02:00
pthread_mutex_unlock(&fout_mutex);
2017-09-24 21:13:16 +02:00
}
}
// little endian inc
2017-10-23 07:13:13 +02:00
static void addsk32(u8 *sk)
2017-09-24 21:13:16 +02:00
{
2017-10-23 07:13:13 +02:00
register unsigned int c = 8;
for (size_t i = 0;i < 32;++i) {
c = (unsigned int)sk[i] + c; sk[i] = c & 0xFF; c >>= 8;
2017-09-24 21:13:16 +02:00
// unsure if needed
if (!c) break;
}
}
2017-10-22 07:07:45 +02:00
// 0123 4567 xxxx --3--> 3456 7xxx
// 0123 4567 xxxx --1--> 1234 567x
static inline void shiftpk(u8 *dst,const u8 *src,size_t sbits)
{
size_t i,sbytes = sbits / 8;
sbits %= 8;
2017-10-22 07:13:52 +02:00
for (i = 0;i + sbytes < PUBLIC_LEN;++i) {
2017-10-22 07:07:45 +02:00
dst[i] = (src[i+sbytes] << sbits) |
(src[i+sbytes+1] >> (8 - sbits));
}
for(;i < PUBLIC_LEN;++i)
dst[i] = 0;
}
2017-09-24 21:13:16 +02:00
static void *dowork(void *task)
{
2017-09-27 21:04:41 +02:00
union pubonionunion {
u8 raw[pkprefixlen + PUBLIC_LEN + 32];
struct {
u64 prefix[4];
u64 key[4];
u64 hash[4];
2017-10-06 02:30:49 +02:00
} i;
2017-09-27 21:04:41 +02:00
} pubonion;
u8 * const pk = &pubonion.raw[pkprefixlen];
2017-09-24 21:13:16 +02:00
u8 secret[skprefixlen + SECRET_LEN];
2017-09-24 23:52:17 +02:00
u8 * const sk = &secret[skprefixlen];
2017-09-24 21:13:16 +02:00
u8 seed[SEED_LEN];
u8 hashsrc[checksumstrlen + PUBLIC_LEN + 1];
2017-10-22 07:07:45 +02:00
u8 wpk[PUBLIC_LEN + 1];
2017-09-24 21:13:16 +02:00
size_t i;
char *sname;
2017-09-30 05:40:12 +02:00
#ifdef STATISTICS
struct statstruct *st = (struct statstruct *)task;
#endif
2017-09-24 21:13:16 +02:00
2017-09-24 23:52:17 +02:00
memcpy(secret,skprefix,skprefixlen);
2017-10-22 13:10:13 +02:00
wpk[PUBLIC_LEN] = 0;
2017-10-22 07:07:45 +02:00
memset(&pubonion,0,sizeof(pubonion));
2017-09-27 21:04:41 +02:00
memcpy(pubonion.raw,pkprefix,pkprefixlen);
2017-09-29 20:18:41 +02:00
// write version later as it will be overwritten by hash
2017-09-24 23:52:17 +02:00
memcpy(hashsrc,checksumstr,checksumstrlen);
2017-09-24 21:13:16 +02:00
hashsrc[checksumstrlen + PUBLIC_LEN] = 0x03; // version
2017-09-25 19:49:47 +02:00
sname = malloc(workdirlen + ONIONLEN + 63 + 1);
2017-10-10 03:02:43 +02:00
if (!sname)
abort();
2017-09-24 21:13:16 +02:00
if (workdir)
2017-09-24 23:52:17 +02:00
memcpy(sname,workdir,workdirlen);
2017-09-24 21:13:16 +02:00
initseed:
2017-09-24 23:52:17 +02:00
randombytes(seed,sizeof(seed));
2017-10-23 07:13:13 +02:00
ed25519_seckey_expand(sk,seed);
#ifdef STATISTICS
++st->numrestart.v;
#endif
2017-09-24 21:13:16 +02:00
again:
2017-09-27 20:38:15 +02:00
if (unlikely(endwork))
2017-09-24 21:13:16 +02:00
goto end;
ed25519_pubkey(pk,sk);
2017-09-30 05:40:12 +02:00
#ifdef STATISTICS
2017-10-06 02:25:51 +02:00
++st->numcalc.v;
2017-09-30 05:40:12 +02:00
#endif
2017-10-10 03:02:43 +02:00
DOFILTER(i,pk,{
2017-10-22 07:07:45 +02:00
if (numwords > 1) {
shiftpk(wpk,pk,filter_len(i));
size_t j;
for (int w = 1;;) {
DOFILTER(j,wpk,goto secondfind);
2017-10-22 13:10:13 +02:00
goto next;
2017-10-22 07:07:45 +02:00
secondfind:
if (++w >= numwords)
break;
shiftpk(wpk,wpk,filter_len(j));
}
}
2017-10-23 07:13:13 +02:00
// sanity check
if ((sk[0] & 248) != sk[0] || ((sk[31] & 63) | 64) != sk[31])
goto initseed;
2017-10-22 07:07:45 +02:00
2017-10-22 02:40:23 +02:00
ADDNUMSUCCESS;
2017-10-10 03:02:43 +02:00
// calc checksum
memcpy(&hashsrc[checksumstrlen],pk,PUBLIC_LEN);
FIPS202_SHA3_256(hashsrc,sizeof(hashsrc),&pk[PUBLIC_LEN]);
// version byte
pk[PUBLIC_LEN + 2] = 0x03;
// base32
strcpy(base32_to(&sname[direndpos],pk,PUBONION_LEN), ".onion");
2017-10-23 07:13:13 +02:00
onionready(sname,secret,pubonion.raw);
2017-10-22 13:10:13 +02:00
pk[PUBLIC_LEN] = 0;
2017-10-10 03:02:43 +02:00
goto initseed;
});
2017-10-22 13:10:13 +02:00
next:
2017-10-23 07:13:13 +02:00
addsk32(sk);
2017-09-24 21:13:16 +02:00
goto again;
end:
2017-09-25 19:49:47 +02:00
free(sname);
2017-09-24 21:13:16 +02:00
return 0;
}
2017-10-22 13:35:45 +02:00
static void addsztoscalar32(u8 *dst,size_t v)
2017-09-24 21:13:16 +02:00
{
int i;
u32 c = 0;
for (i = 0;i < 32;++i) {
2017-09-30 05:40:12 +02:00
c += *dst + (v & 0xFF); *dst = c & 0xFF; c >>= 8;
2017-09-24 21:13:16 +02:00
v >>= 8;
2017-09-30 05:40:12 +02:00
++dst;
2017-09-24 21:13:16 +02:00
}
}
static void *dofastwork(void *task)
{
2017-09-27 21:04:41 +02:00
union pubonionunion {
u8 raw[pkprefixlen + PUBLIC_LEN + 32];
struct {
u64 prefix[4];
u64 key[4];
u64 hash[4];
2017-10-06 02:30:49 +02:00
} i;
2017-09-27 21:04:41 +02:00
} pubonion;
u8 * const pk = &pubonion.raw[pkprefixlen];
2017-09-24 21:13:16 +02:00
u8 secret[skprefixlen + SECRET_LEN];
u8 * const sk = &secret[skprefixlen];
u8 seed[SEED_LEN];
u8 hashsrc[checksumstrlen + PUBLIC_LEN + 1];
2017-10-22 07:07:45 +02:00
u8 wpk[PUBLIC_LEN + 1];
2017-09-24 21:13:16 +02:00
ge_p3 ge_public;
size_t counter;
2017-09-24 21:13:16 +02:00
size_t i;
char *sname;
2017-09-30 05:40:12 +02:00
#ifdef STATISTICS
struct statstruct *st = (struct statstruct *)task;
#endif
2017-09-24 21:13:16 +02:00
memcpy(secret, skprefix, skprefixlen);
2017-10-22 13:10:13 +02:00
wpk[PUBLIC_LEN] = 0;
2017-10-22 07:07:45 +02:00
memset(&pubonion,0,sizeof(pubonion));
2017-10-23 07:13:13 +02:00
memcpy(pubonion.raw,pkprefix,pkprefixlen);
2017-09-29 20:18:41 +02:00
// write version later as it will be overwritten by hash
2017-10-23 07:13:13 +02:00
memcpy(hashsrc,checksumstr,checksumstrlen);
2017-09-24 21:13:16 +02:00
hashsrc[checksumstrlen + PUBLIC_LEN] = 0x03; // version
2017-09-25 19:49:47 +02:00
sname = malloc(workdirlen + ONIONLEN + 63 + 1);
2017-10-10 03:02:43 +02:00
if (!sname)
abort();
2017-09-24 21:13:16 +02:00
if (workdir)
2017-10-23 07:13:13 +02:00
memcpy(sname,workdir,workdirlen);
2017-09-24 21:13:16 +02:00
initseed:
#ifdef STATISTICS
++st->numrestart.v;
#endif
2017-09-24 23:52:17 +02:00
randombytes(seed,sizeof(seed));
ed25519_seckey_expand(sk,seed);
2017-09-24 21:13:16 +02:00
ge_scalarmult_base(&ge_public,sk);
2017-09-24 23:52:17 +02:00
ge_p3_tobytes(pk,&ge_public);
2017-09-24 21:13:16 +02:00
for (counter = 0;counter < SIZE_MAX-8;counter += 8) {
2017-09-24 21:13:16 +02:00
ge_p1p1 sum;
2017-09-27 20:38:15 +02:00
if (unlikely(endwork))
2017-09-24 21:13:16 +02:00
goto end;
2017-10-10 03:02:43 +02:00
DOFILTER(i,pk,{
2017-10-22 07:07:45 +02:00
if (numwords > 1) {
shiftpk(wpk,pk,filter_len(i));
size_t j;
for (int w = 1;;) {
DOFILTER(j,wpk,goto secondfind);
2017-10-22 13:10:13 +02:00
goto next;
2017-10-22 07:07:45 +02:00
secondfind:
if (++w >= numwords)
break;
shiftpk(wpk,wpk,filter_len(j));
}
}
2017-10-10 03:02:43 +02:00
// found!
// update secret key with counter
2017-10-22 13:35:45 +02:00
addsztoscalar32(sk,counter);
2017-10-10 03:02:43 +02:00
// sanity check
2017-10-23 07:13:13 +02:00
if ((sk[0] & 248) != sk[0] || ((sk[31] & 63) | 64) != sk[31])
2017-10-22 02:40:23 +02:00
goto initseed;
ADDNUMSUCCESS;
2017-10-10 03:02:43 +02:00
// calc checksum
memcpy(&hashsrc[checksumstrlen],pk,PUBLIC_LEN);
FIPS202_SHA3_256(hashsrc,sizeof(hashsrc),&pk[PUBLIC_LEN]);
// version byte
pk[PUBLIC_LEN + 2] = 0x03;
// full name
strcpy(base32_to(&sname[direndpos],pk,PUBONION_LEN),".onion");
onionready(sname,secret,pubonion.raw);
2017-10-22 13:10:13 +02:00
pk[PUBLIC_LEN] = 0;
2017-10-10 03:02:43 +02:00
// don't reuse same seed
goto initseed;
});
2017-10-22 13:10:13 +02:00
next:
2017-09-24 23:52:17 +02:00
ge_add(&sum, &ge_public,&ge_eightpoint);
ge_p1p1_to_p3(&ge_public,&sum);
ge_p3_tobytes(pk,&ge_public);
2017-09-30 05:40:12 +02:00
#ifdef STATISTICS
2017-10-06 02:25:51 +02:00
++st->numcalc.v;
2017-09-30 05:40:12 +02:00
#endif
2017-09-24 21:13:16 +02:00
}
goto initseed;
end:
2017-09-25 19:49:47 +02:00
free(sname);
2017-09-24 21:13:16 +02:00
return 0;
}
void printhelp(const char *progname)
{
fprintf(stderr,
"Usage: %s filter [filter...] [options]\n"
" %s -f filterfile [options]\n"
"Options:\n"
"\t-h - print help\n"
"\t-f - instead of specifying filter(s) via commandline, specify filter file which contains filters separated by newlines\n"
"\t-q - do not print diagnostic output to stderr\n"
"\t-x - do not print onion names\n"
"\t-o filename - output onion names to specified file\n"
"\t-F - include directory names in onion names output\n"
"\t-d dirname - output directory\n"
"\t-t numthreads - specify number of threads (default - auto)\n"
2017-09-30 05:40:12 +02:00
"\t-j numthreads - same as -t\n"
2017-09-24 21:13:16 +02:00
"\t-n numkeys - specify number of keys (default - 0 - unlimited)\n"
2017-10-22 07:07:45 +02:00
"\t-N numwords - specify number of words per key (default - 1)\n"
2017-09-30 14:43:32 +02:00
"\t-z - use faster key generation method. this is now default\n"
"\t-Z - use slower key generation method\n"
2017-09-30 05:40:12 +02:00
"\t-s - print statistics each 10 seconds\n"
"\t-S t - print statistics every specified ammount of seconds\n"
2017-09-30 14:43:32 +02:00
"\t-T - do not reset statistics counters when printing\n"
2017-09-24 21:13:16 +02:00
,progname,progname);
exit(1);
}
void setworkdir(const char *wd)
{
free(workdir);
size_t l = strlen(wd);
if (!l) {
workdir = 0;
workdirlen = 0;
if (!quietflag)
fprintf(stderr, "unset workdir\n");
return;
}
int needslash = 0;
if (wd[l-1] != '/')
needslash = 1;
char *s = malloc(l + needslash + 1);
2017-10-10 03:02:43 +02:00
if (!s)
abort();
2017-09-24 21:13:16 +02:00
memcpy(s, wd, l);
if (needslash)
s[l++] = '/';
s[l] = 0;
workdir = s;
workdirlen = l;
if (!quietflag)
2017-09-30 05:40:12 +02:00
fprintf(stderr,"set workdir: %s\n",workdir);
2017-09-24 21:13:16 +02:00
}
VEC_STRUCT(threadvec, pthread_t);
2017-09-30 05:40:12 +02:00
int main(int argc,char **argv)
2017-09-24 21:13:16 +02:00
{
char *outfile = 0;
const char *arg;
int ignoreargs = 0;
int dirnameflag = 0;
int numthreads = 0;
2017-09-30 14:43:32 +02:00
int fastkeygen = 1;
2017-09-24 21:13:16 +02:00
struct threadvec threads;
2017-09-30 05:40:12 +02:00
#ifdef STATISTICS
struct statsvec stats;
struct tstatsvec tstats;
u64 reportdelay = 0;
2017-09-30 14:43:32 +02:00
int realtimestats = 1;
2017-09-30 05:46:17 +02:00
#endif
2017-09-24 21:13:16 +02:00
int tret;
2017-10-02 16:11:23 +02:00
ge_initeightpoint();
2017-09-24 21:13:16 +02:00
filters_init();
2017-09-24 21:13:16 +02:00
fout = stdout;
2017-09-25 19:49:47 +02:00
pthread_mutex_init(&keysgenerated_mutex, 0);
2017-09-24 21:13:16 +02:00
pthread_mutex_init(&fout_mutex, 0);
const char *progname = argv[0];
if (argc <= 1)
printhelp(progname);
argc--, argv++;
while (argc--) {
arg = *argv++;
if (!ignoreargs && *arg == '-') {
int numargit = 0;
nextarg:
++arg;
++numargit;
if (*arg == '-') {
if (numargit > 1) {
fprintf(stderr, "unrecognised argument: -\n");
exit(1);
}
++arg;
if (!*arg)
ignoreargs = 1;
else if (!strcmp(arg, "help"))
printhelp(progname);
else {
fprintf(stderr, "unrecognised argument: --%s\n", arg);
exit(1);
}
numargit = 0;
}
else if (*arg == 0) {
if (numargit == 1)
ignoreargs = 1;
continue;
}
else if (*arg == 'h')
printhelp(progname);
else if (*arg == 'f') {
if (argc--)
loadfilterfile(*argv++);
else {
fprintf(stderr, "additional argument required\n");
exit(1);
}
}
else if (*arg == 'q')
++quietflag;
else if (*arg == 'x')
fout = 0;
else if (*arg == 'o') {
if (argc--)
outfile = *argv++;
else {
fprintf(stderr, "additional argument required\n");
exit(1);
}
}
else if (*arg == 'F')
dirnameflag = 1;
else if (*arg == 'd') {
if (argc--) {
setworkdir(*argv++);
}
else {
fprintf(stderr, "additional argument required\n");
}
}
2017-09-30 05:40:12 +02:00
else if (*arg == 't' || *arg == 'j') {
2017-09-24 21:13:16 +02:00
if (argc--)
numthreads = atoi(*argv++);
else {
fprintf(stderr, "additional argument required\n");
exit(1);
}
}
2017-09-25 19:49:47 +02:00
else if (*arg == 'n') {
if (argc--)
numneedgenerate = (size_t)atoll(*argv++);
else {
fprintf(stderr, "additional argument required\n");
exit(1);
}
}
2017-10-22 07:07:45 +02:00
else if (*arg == 'N') {
if (argc--)
numwords = atoi(*argv++);
else {
fprintf(stderr, "additional argument required\n");
exit(1);
}
}
2017-09-30 14:43:32 +02:00
else if (*arg == 'Z')
fastkeygen = 0;
2017-09-24 21:13:16 +02:00
else if (*arg == 'z')
fastkeygen = 1;
2017-09-30 05:40:12 +02:00
else if (*arg == 's') {
#ifdef STATISTICS
reportdelay = 10000000;
#else
fprintf(stderr,"statistics support not compiled in\n");
exit(1);
#endif
}
else if (*arg == 'S') {
#ifdef STATISTICS
if (argc--)
reportdelay = (u64)atoll(*argv++) * 1000000;
else {
fprintf(stderr, "additional argument required\n");
exit(1);
}
#else
fprintf(stderr,"statistics support not compiled in\n");
exit(1);
#endif
}
2017-09-30 14:43:32 +02:00
else if (*arg == 'T') {
2017-09-30 05:46:17 +02:00
#ifdef STATISTICS
2017-09-30 14:43:32 +02:00
realtimestats = 0;
2017-09-30 05:46:17 +02:00
#else
fprintf(stderr,"statistics support not compiled in\n");
exit(1);
#endif
}
2017-09-24 21:13:16 +02:00
else {
fprintf(stderr, "unrecognised argument: -%c\n", *arg);
exit(1);
}
if (numargit)
goto nextarg;
}
else filters_add(arg);
}
2017-09-25 19:49:47 +02:00
filters_prepare();
filters_print();
2017-09-24 21:13:16 +02:00
#ifdef STATISTICS
if (!filters_count() && !reportdelay)
#else
2017-09-25 22:57:27 +02:00
if (!filters_count())
#endif
2017-09-25 22:57:27 +02:00
return 0;
2017-09-30 05:40:12 +02:00
if (outfile)
fout = fopen(outfile, "w");
2017-09-24 21:13:16 +02:00
if (workdir)
createdir(workdir,1);
2017-09-25 19:49:47 +02:00
2017-09-24 21:13:16 +02:00
direndpos = workdirlen;
onionendpos = workdirlen + ONIONLEN;
2017-09-25 19:49:47 +02:00
2017-09-24 21:13:16 +02:00
if (!dirnameflag) {
printstartpos = direndpos;
printlen = ONIONLEN + 1;
} else {
printstartpos = 0;
printlen = onionendpos + 1;
}
2017-09-25 19:49:47 +02:00
2017-09-24 21:13:16 +02:00
if (numthreads <= 0) {
2017-10-06 02:14:33 +02:00
numthreads = cpucount();
if (numthreads <= 0)
numthreads = 1;
2017-10-23 08:40:34 +02:00
if (!quietflag)
fprintf(stderr,"using %d %s\n",
numthreads,numthreads == 1 ? "thread" : "threads");
2017-09-24 21:13:16 +02:00
}
2017-09-25 19:49:47 +02:00
2017-09-30 05:40:12 +02:00
signal(SIGTERM,termhandler);
signal(SIGINT,termhandler);
2017-09-25 19:49:47 +02:00
2017-09-24 21:13:16 +02:00
VEC_INIT(threads);
VEC_ADDN(threads,numthreads);
2017-09-30 05:40:12 +02:00
#ifdef STATISTICS
VEC_INIT(stats);
VEC_ADDN(stats,numthreads);
VEC_ZERO(stats);
2017-09-30 05:40:12 +02:00
VEC_INIT(tstats);
VEC_ADDN(tstats,numthreads);
VEC_ZERO(tstats);
2017-09-30 05:40:12 +02:00
#endif
2017-09-25 19:49:47 +02:00
2017-09-30 05:40:12 +02:00
for (size_t i = 0;i < VEC_LENGTH(threads);++i) {
void *tp = 0;
#ifdef STATISTICS
tp = &VEC_BUF(stats,i);
#endif
tret = pthread_create(&VEC_BUF(threads,i),0,fastkeygen ? dofastwork : dowork,tp);
2017-09-24 21:13:16 +02:00
if (tret) {
2017-10-23 08:40:34 +02:00
fprintf(stderr,"error while making %zuth thread: %d\n",i,tret);
2017-09-24 21:13:16 +02:00
exit(1);
}
}
2017-09-25 19:49:47 +02:00
2017-09-30 05:40:12 +02:00
#ifdef STATISTICS
struct timespec nowtime;
u64 istarttime,inowtime,ireporttime = 0,elapsedoffset = 0;
if (clock_gettime(CLOCK_MONOTONIC,&nowtime) < 0) {
fprintf(stderr, "failed to get time\n");
exit(1);
}
istarttime = (1000000 * (u64)nowtime.tv_sec) + (nowtime.tv_nsec / 1000);
#endif
2017-09-25 19:49:47 +02:00
struct timespec ts;
memset(&ts,0,sizeof(ts));
ts.tv_nsec = 100000000;
while (!endwork) {
if (numneedgenerate && keysgenerated >= numneedgenerate) {
endwork = 1;
break;
}
nanosleep(&ts,0);
2017-09-30 05:40:12 +02:00
#ifdef STATISTICS
clock_gettime(CLOCK_MONOTONIC,&nowtime);
inowtime = (1000000 * (u64)nowtime.tv_sec) + (nowtime.tv_nsec / 1000);
u64 sumcalc = 0,sumsuccess = 0,sumrestart = 0;
2017-09-30 05:40:12 +02:00
for (size_t i = 0;i < numthreads;++i) {
u32 newt,tdiff;
// numcalc
2017-10-06 02:25:51 +02:00
newt = VEC_BUF(stats,i).numcalc.v;
2017-09-30 05:40:12 +02:00
tdiff = newt - VEC_BUF(tstats,i).oldnumcalc;
VEC_BUF(tstats,i).oldnumcalc = newt;
VEC_BUF(tstats,i).numcalc += (u64)tdiff;
sumcalc += VEC_BUF(tstats,i).numcalc;
// numsuccess
2017-10-06 02:25:51 +02:00
newt = VEC_BUF(stats,i).numsuccess.v;
2017-09-30 05:40:12 +02:00
tdiff = newt - VEC_BUF(tstats,i).oldnumsuccess;
VEC_BUF(tstats,i).oldnumsuccess = newt;
VEC_BUF(tstats,i).numsuccess += (u64)tdiff;
sumsuccess += VEC_BUF(tstats,i).numsuccess;
// numrestart
newt = VEC_BUF(stats,i).numrestart.v;
tdiff = newt - VEC_BUF(tstats,i).oldnumrestart;
VEC_BUF(tstats,i).oldnumrestart = newt;
VEC_BUF(tstats,i).numrestart += (u64)tdiff;
sumrestart += VEC_BUF(tstats,i).numrestart;
2017-09-30 05:40:12 +02:00
}
if (reportdelay && (!ireporttime || inowtime - ireporttime >= reportdelay)) {
if (ireporttime)
ireporttime += reportdelay;
else
ireporttime = inowtime;
if (!ireporttime)
ireporttime = 1;
double calcpersec = (1000000.0 * sumcalc) / (inowtime - istarttime);
double succpersec = (1000000.0 * sumsuccess) / (inowtime - istarttime);
double restpersec = (1000000.0 * sumrestart) / (inowtime - istarttime);
fprintf(stderr,">calc/sec:%8lf, succ/sec:%8lf, rest/sec:%8lf, elapsed:%5.6lfsec\n",
calcpersec,succpersec,restpersec,
(inowtime - istarttime + elapsedoffset) / 1000000.0);
2017-09-30 05:40:12 +02:00
if (realtimestats) {
for (size_t i = 0;i < numthreads;++i) {
VEC_BUF(tstats,i).numcalc = 0;
VEC_BUF(tstats,i).numsuccess = 0;
VEC_BUF(tstats,i).numrestart = 0;
2017-09-30 05:40:12 +02:00
}
elapsedoffset += inowtime - istarttime;
istarttime = inowtime;
}
}
if (sumcalc > U64_MAX / 2) {
for (size_t i = 0;i < numthreads;++i) {
VEC_BUF(tstats,i).numcalc /= 2;
VEC_BUF(tstats,i).numsuccess /= 2;
VEC_BUF(tstats,i).numrestart /= 2;
2017-09-30 05:40:12 +02:00
}
u64 timediff = (inowtime - istarttime + 1) / 2;
elapsedoffset += timediff;
istarttime += timediff;
}
#endif
2017-09-25 19:49:47 +02:00
}
2017-09-30 05:40:12 +02:00
if (!quietflag)
2017-10-23 08:40:34 +02:00
fprintf(stderr, "waiting for threads to finish...");
2017-09-30 05:40:12 +02:00
for (size_t i = 0;i < VEC_LENGTH(threads);++i)
pthread_join(VEC_BUF(threads,i),0);
if (!quietflag)
2017-10-23 08:40:34 +02:00
fprintf(stderr, " done.\n");
2017-09-24 21:13:16 +02:00
2017-09-25 19:49:47 +02:00
pthread_mutex_destroy(&keysgenerated_mutex);
2017-09-24 21:13:16 +02:00
pthread_mutex_destroy(&fout_mutex);
filters_clean();
2017-09-25 19:49:47 +02:00
if (outfile)
fclose(fout);
2017-09-24 21:13:16 +02:00
return 0;
}