worker_batch_pass.inc.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #ifdef PASSPHRASE
  2. void *CRYPTO_NAMESPACE(worker_batch_pass)(void *task)
  3. {
  4. union pubonionunion pubonion;
  5. u8 * const pk = &pubonion.raw[PKPREFIX_SIZE];
  6. u8 secret[SKPREFIX_SIZE + SECRET_LEN];
  7. u8 * const sk = &secret[SKPREFIX_SIZE];
  8. u8 seed[SEED_LEN];
  9. u8 hashsrc[checksumstrlen + PUBLIC_LEN + 1];
  10. u8 wpk[PUBLIC_LEN + 1];
  11. ge_p3 ALIGN(16) ge_public;
  12. char *sname;
  13. // state to keep batch data
  14. ge_p3 ALIGN(16) ge_batch [BATCHNUM];
  15. fe ALIGN(16) tmp_batch[BATCHNUM];
  16. bytes32 ALIGN(16) pk_batch [BATCHNUM];
  17. size_t counter,oldcounter;
  18. size_t i;
  19. #ifdef STATISTICS
  20. struct statstruct *st = (struct statstruct *)task;
  21. #else
  22. (void) task;
  23. #endif
  24. PREFILTER
  25. memcpy(secret,skprefix,SKPREFIX_SIZE);
  26. wpk[PUBLIC_LEN] = 0;
  27. memset(&pubonion,0,sizeof(pubonion));
  28. memcpy(pubonion.raw,pkprefix,PKPREFIX_SIZE);
  29. // write version later as it will be overwritten by hash
  30. memcpy(hashsrc,checksumstr,checksumstrlen);
  31. hashsrc[checksumstrlen + PUBLIC_LEN] = 0x03; // version
  32. sname = makesname();
  33. int seednear;
  34. initseed:
  35. #ifdef STATISTICS
  36. ++st->numrestart.v;
  37. #endif
  38. seednear = 0;
  39. pthread_mutex_lock(&determseed_mutex);
  40. for (int i = 0; i < SEED_LEN; i++)
  41. if (++determseed[i])
  42. break;
  43. memcpy(seed,determseed,SEED_LEN);
  44. pthread_mutex_unlock(&determseed_mutex);
  45. ed25519_seckey_expand(sk,seed);
  46. ge_scalarmult_base(&ge_public,sk);
  47. for (counter = oldcounter = 0;counter < DETERMINISTIC_LOOP_COUNT - (BATCHNUM - 1) * 8;counter += BATCHNUM * 8) {
  48. ge_p1p1 ALIGN(16) sum;
  49. if (unlikely(endwork))
  50. goto end;
  51. for (size_t b = 0;b < BATCHNUM;++b) {
  52. ge_batch[b] = ge_public;
  53. ge_add(&sum,&ge_public,&ge_eightpoint);
  54. ge_p1p1_to_p3(&ge_public,&sum);
  55. }
  56. // NOTE: leaves unfinished one bit at the very end
  57. ge_p3_batchtobytes_destructive_1(pk_batch,ge_batch,tmp_batch,BATCHNUM);
  58. #ifdef STATISTICS
  59. st->numcalc.v += BATCHNUM;
  60. #endif
  61. for (size_t b = 0;b < BATCHNUM;++b) {
  62. DOFILTER(i,pk_batch[b],{
  63. if (numwords > 1) {
  64. shiftpk(wpk,pk_batch[b],filter_len(i));
  65. size_t j;
  66. for (int w = 1;;) {
  67. DOFILTER(j,wpk,goto secondfind);
  68. goto next;
  69. secondfind:
  70. if (++w >= numwords)
  71. break;
  72. shiftpk(wpk,wpk,filter_len(j));
  73. }
  74. }
  75. // found!
  76. // finish it up
  77. ge_p3_batchtobytes_destructive_finish(pk_batch[b],&ge_batch[b]);
  78. // copy public key
  79. memcpy(pk,pk_batch[b],PUBLIC_LEN);
  80. // update secret key with counter
  81. addsztoscalar32(sk,counter + (b * 8) - oldcounter);
  82. oldcounter = counter + (b * 8);
  83. // sanity check
  84. if ((sk[0] & 248) != sk[0] || ((sk[31] & 63) | 64) != sk[31])
  85. goto initseed;
  86. // reseed right half of key to avoid reuse, it won't change public key anyway
  87. reseedright(sk);
  88. ADDNUMSUCCESS;
  89. // calc checksum
  90. memcpy(&hashsrc[checksumstrlen],pk,PUBLIC_LEN);
  91. FIPS202_SHA3_256(hashsrc,sizeof(hashsrc),&pk[PUBLIC_LEN]);
  92. // version byte
  93. pk[PUBLIC_LEN + 2] = 0x03;
  94. // full name
  95. strcpy(base32_to(&sname[direndpos],pk,PUBONION_LEN),".onion");
  96. onionready(sname,secret,pubonion.raw,seednear && pw_warnnear);
  97. pk[PUBLIC_LEN] = 0; // what is this for?
  98. if (pw_skipnear)
  99. goto initseed;
  100. seednear = 1;
  101. });
  102. next:
  103. ;
  104. }
  105. }
  106. // continue if have leftovers, DETERMINISTIC_LOOP_COUNT - counter < BATCHNUM * 8
  107. // can't have leftovers in theory if BATCHNUM was power of 2 and smaller than DETERMINISTIC_LOOP_COUNT bound
  108. #if (BATCHNUM & (BATCHNUM - 1)) || (BATCHNUM * 8) > DETERMINISTIC_LOOP_COUNT
  109. if (counter < DETERMINISTIC_LOOP_COUNT) {
  110. ge_p1p1 ALIGN(16) sum;
  111. if (unlikely(endwork))
  112. goto end;
  113. const size_t remaining = (DETERMINISTIC_LOOP_COUNT - counter) / 8;
  114. for (size_t b = 0;b < remaining;++b) {
  115. ge_batch[b] = ge_public;
  116. ge_add(&sum,&ge_public,&ge_eightpoint);
  117. ge_p1p1_to_p3(&ge_public,&sum);
  118. }
  119. // NOTE: leaves unfinished one bit at the very end
  120. ge_p3_batchtobytes_destructive_1(pk_batch,ge_batch,tmp_batch,remaining);
  121. #ifdef STATISTICS
  122. st->numcalc.v += remaining;
  123. #endif
  124. for (size_t b = 0;b < remaining;++b) {
  125. DOFILTER(i,pk_batch[b],{
  126. if (numwords > 1) {
  127. shiftpk(wpk,pk_batch[b],filter_len(i));
  128. size_t j;
  129. for (int w = 1;;) {
  130. DOFILTER(j,wpk,goto secondfind2);
  131. goto next2;
  132. secondfind2:
  133. if (++w >= numwords)
  134. break;
  135. shiftpk(wpk,wpk,filter_len(j));
  136. }
  137. }
  138. // found!
  139. // finish it up
  140. ge_p3_batchtobytes_destructive_finish(pk_batch[b],&ge_batch[b]);
  141. // copy public key
  142. memcpy(pk,pk_batch[b],PUBLIC_LEN);
  143. // update secret key with counter
  144. addsztoscalar32(sk,counter + (b * 8) - oldcounter);
  145. oldcounter = counter + (b * 8);
  146. // sanity check
  147. if ((sk[0] & 248) != sk[0] || ((sk[31] & 63) | 64) != sk[31])
  148. goto initseed;
  149. // reseed right half of key to avoid reuse, it won't change public key anyway
  150. reseedright(sk);
  151. ADDNUMSUCCESS;
  152. // calc checksum
  153. memcpy(&hashsrc[checksumstrlen],pk,PUBLIC_LEN);
  154. FIPS202_SHA3_256(hashsrc,sizeof(hashsrc),&pk[PUBLIC_LEN]);
  155. // version byte
  156. pk[PUBLIC_LEN + 2] = 0x03;
  157. // full name
  158. strcpy(base32_to(&sname[direndpos],pk,PUBONION_LEN),".onion");
  159. onionready(sname,secret,pubonion.raw,seednear && pw_warnnear);
  160. pk[PUBLIC_LEN] = 0; // what is this for?
  161. if (pw_skipnear)
  162. goto initseed;
  163. seednear = 1;
  164. });
  165. next2:
  166. ;
  167. }
  168. }
  169. #endif // (BATCHNUM & (BATCHNUM - 1)) || (BATCHNUM * 8) > DETERMINISTIC_LOOP_COUNT
  170. goto initseed;
  171. end:
  172. free(sname);
  173. POSTFILTER
  174. sodium_memzero(secret,sizeof(secret));
  175. sodium_memzero(seed,sizeof(seed));
  176. return 0;
  177. }
  178. #endif // PASSPHRASE