mirror of
https://git.sr.ht/~sircmpwn/mkproof
synced 2024-05-08 10:46:15 +02:00
Use leading zero bits instead of digits
This allows for a more granular difficulty setting Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
parent
14cfc3f783
commit
738f584ff6
4
README
4
README
|
@ -40,8 +40,8 @@ To make a proof, split the challenge by ":" and verify that the first token is
|
||||||
"argon2id". Decode the iterations, memory, and digits parameters, and the
|
"argon2id". Decode the iterations, memory, and digits parameters, and the
|
||||||
challenge bytes.
|
challenge bytes.
|
||||||
|
|
||||||
Repeat the following algorithm to generate proofs until an argon2id key is found
|
Repeat the following algorithm to generate proofs until an argon2id key
|
||||||
whose first N hexadecimal digits are zero, where N is equal to the digits
|
is found whose first N bits are zero, where N is equal to the digits
|
||||||
parameter:
|
parameter:
|
||||||
|
|
||||||
1. Generate 16 random bytes (password).
|
1. Generate 16 random bytes (password).
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include "argon2.h"
|
#include "argon2.h"
|
||||||
#include "random.h"
|
#include "random.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "proof.h"
|
||||||
|
|
||||||
static void
|
static void
|
||||||
die(int exitcode, int check, char *why)
|
die(int exitcode, int check, char *why)
|
||||||
|
@ -75,12 +76,9 @@ main(int argc, char *argv[])
|
||||||
r = argon2id_ctx(&context);
|
r = argon2id_ctx(&context);
|
||||||
die(1, r != 0, "argon2id failed\n");
|
die(1, r != 0, "argon2id failed\n");
|
||||||
|
|
||||||
for (int i = 0; i < digits; ++i) {
|
if (hash_msb(hash) < digits) {
|
||||||
unsigned char n = hash[i / 2] & (i % 2 ? 0x0F : 0xF0);
|
printf("proof: failed\n");
|
||||||
if (n != 0) {
|
return 1;
|
||||||
printf("proof: failed\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("proof: ok\n");
|
printf("proof: ok\n");
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
#define ALGORITHM "argon2id"
|
#define ALGORITHM "argon2id"
|
||||||
#define ITERATIONS 10
|
#define ITERATIONS 10
|
||||||
#define MEMORY 12
|
#define MEMORY 12
|
||||||
#define DIGITS 5
|
#define DIGITS 20
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(char *argv_0)
|
usage(char *argv_0)
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include "argon2.h"
|
#include "argon2.h"
|
||||||
#include "random.h"
|
#include "random.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "proof.h"
|
||||||
|
|
||||||
static void
|
static void
|
||||||
die(int check, char *why)
|
die(int check, char *why)
|
||||||
|
@ -88,14 +89,7 @@ main(int argc, char *argv[])
|
||||||
r = argon2id_ctx(&context);
|
r = argon2id_ctx(&context);
|
||||||
die(r != 0, "argon2id failed\n");
|
die(r != 0, "argon2id failed\n");
|
||||||
|
|
||||||
valid = 1;
|
valid = hash_msb(hash) >= digits;
|
||||||
for (int i = 0; i < digits; ++i) {
|
|
||||||
unsigned char n = hash[i / 2] & (i % 2 ? 0x0F : 0xF0);
|
|
||||||
if (n != 0) {
|
|
||||||
valid = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isatty(STDERR_FILENO)) {
|
if (isatty(STDERR_FILENO)) {
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
|
||||||
|
/* number of leading zero bits in a byte */
|
||||||
|
static inline int
|
||||||
|
msb(unsigned char b)
|
||||||
|
{
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
|
if (b == 0)
|
||||||
|
return 8;
|
||||||
|
|
||||||
|
while (b >>= 1)
|
||||||
|
n++;
|
||||||
|
|
||||||
|
return 7-n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* find the number of leading zero bits in a hash */
|
||||||
|
static int
|
||||||
|
hash_msb(unsigned char *hash)
|
||||||
|
{
|
||||||
|
int bits, total, i;
|
||||||
|
|
||||||
|
for (i = 0, total = 0; i < 32; i++) {
|
||||||
|
bits = msb(hash[i]);
|
||||||
|
total += bits;
|
||||||
|
if (bits != 8)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue