mirror of
https://git.sr.ht/~sircmpwn/mkproof
synced 2024-03-28 14:30:05 +01:00
Return 1 for invalid proof, 2 for usage errors
This makes it easier to detect if the input proof is valid or if there was another error (eg: badly formatted challenge, etc)
This commit is contained in:
parent
a91ef0ee2c
commit
7de01c930f
|
@ -12,4 +12,6 @@ checkproof(1)
|
|||
|
||||
*checkproof* verifies a proof-of-work challenge which has been completed by
|
||||
*mkproof*(1). The result will be printed to stdout. If valid, *checkproof* will
|
||||
exit with a zero status code; if invalid, a nonzero status code.
|
||||
exit with a zero status code. If the proof is invalid, it will exit with a
|
||||
status code of 1. If any other usage error, it will exit with a status code of
|
||||
2.
|
||||
|
|
|
@ -7,11 +7,11 @@
|
|||
#include "util.h"
|
||||
|
||||
static void
|
||||
die(int check, char *why)
|
||||
die(int exitcode, int check, char *why)
|
||||
{
|
||||
if (check) {
|
||||
fprintf(stderr, "Error: %s\n", why);
|
||||
exit(1);
|
||||
exit(exitcode);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ main(int argc, char *argv[])
|
|||
{
|
||||
if (argc != 3) {
|
||||
fprintf(stderr, "Usage: %s <challenge> <proof>\n", argv[0]);
|
||||
return 1;
|
||||
return 2;
|
||||
}
|
||||
|
||||
int iters, memory, digits;
|
||||
|
@ -30,26 +30,26 @@ main(int argc, char *argv[])
|
|||
char *algo = strtok(challenge, ":");
|
||||
if (strcmp(algo, "argon2id") != 0) {
|
||||
fprintf(stderr, "Error: unknown challenge type %s\n", algo);
|
||||
return 1;
|
||||
return 2;
|
||||
}
|
||||
char *endptr;
|
||||
|
||||
char *iterstr = strtok(NULL, ":");
|
||||
iters = strtoul(iterstr, &endptr, 10);
|
||||
die(*endptr, "Invalid challenge");
|
||||
die(2, *endptr, "Invalid challenge");
|
||||
|
||||
char *memorystr = strtok(NULL, ":");
|
||||
memory = strtoul(memorystr, &endptr, 10);
|
||||
die(*endptr, "Invalid challenge");
|
||||
die(2, *endptr, "Invalid challenge");
|
||||
|
||||
char *digitsstr = strtok(NULL, ":");
|
||||
digits = strtoul(digitsstr, &endptr, 10);
|
||||
die(*endptr, "Invalid challenge");
|
||||
die(2, *endptr, "Invalid challenge");
|
||||
|
||||
char *saltstr = strtok(NULL, ":");
|
||||
die(strlen(saltstr) != 32, "Invalid challenge");
|
||||
die(2, strlen(saltstr) != 32, "Invalid challenge");
|
||||
int r = dechex(saltstr, strlen(saltstr), salt, sizeof(salt));
|
||||
die(r == -1, "Invalid challenge");
|
||||
die(2, r == -1, "Invalid challenge");
|
||||
|
||||
unsigned char password[16];
|
||||
unsigned char hash[32];
|
||||
|
@ -67,12 +67,12 @@ main(int argc, char *argv[])
|
|||
.flags = ARGON2_DEFAULT_FLAGS,
|
||||
};
|
||||
|
||||
die(strlen(argv[2]) != 32, "Invalid proof");
|
||||
die(1, strlen(argv[2]) != 32, "Invalid proof");
|
||||
r = dechex(argv[2], strlen(argv[2]) + 1, password, sizeof(password));
|
||||
die(r == -1, "Invalid proof");
|
||||
die(1, r == -1, "Invalid proof");
|
||||
|
||||
r = argon2id_ctx(&context);
|
||||
die(r != 0, "argon2id failed\n");
|
||||
die(1, r != 0, "argon2id failed\n");
|
||||
|
||||
for (int i = 0; i < digits; ++i) {
|
||||
unsigned char n = hash[i / 2] & (i % 2 ? 0x0F : 0xF0);
|
||||
|
|
Loading…
Reference in New Issue