2
0
Fork 0
mirror of https://git.sr.ht/~sircmpwn/mkproof synced 2024-05-29 19:06:32 +02:00
mkproof/src/checkproof.c

87 lines
1.9 KiB
C
Raw Normal View History

2020-11-25 18:05:19 +01:00
#include <assert.h>
2020-11-25 17:15:59 +01:00
#include <stdio.h>
2020-11-25 18:05:19 +01:00
#include <stdlib.h>
#include <string.h>
#include "argon2.h"
#include "random.h"
#include "util.h"
#include "proof.h"
2020-11-25 18:05:19 +01:00
static void
die(int exitcode, int check, char *why)
2020-11-25 18:05:19 +01:00
{
if (check) {
fprintf(stderr, "Error: %s\n", why);
exit(exitcode);
2020-11-25 18:05:19 +01:00
}
}
2020-11-25 17:15:59 +01:00
int
main(int argc, char *argv[])
{
2020-11-25 18:05:19 +01:00
if (argc != 3) {
fprintf(stderr, "Usage: %s <challenge> <proof>\n", argv[0]);
return 2;
2020-11-25 18:05:19 +01:00
}
int iters, memory, digits;
unsigned char salt[16];
char *challenge = argv[1];
char *algo = strtok(challenge, ":");
if (strcmp(algo, "argon2id") != 0) {
fprintf(stderr, "Error: unknown challenge type %s\n", algo);
return 2;
2020-11-25 18:05:19 +01:00
}
char *endptr;
char *iterstr = strtok(NULL, ":");
iters = strtoul(iterstr, &endptr, 10);
die(2, *endptr, "Invalid challenge");
2020-11-25 18:05:19 +01:00
char *memorystr = strtok(NULL, ":");
memory = strtoul(memorystr, &endptr, 10);
die(2, *endptr, "Invalid challenge");
2020-11-25 18:05:19 +01:00
char *digitsstr = strtok(NULL, ":");
digits = strtoul(digitsstr, &endptr, 10);
die(2, *endptr, "Invalid challenge");
2020-11-25 18:05:19 +01:00
char *saltstr = strtok(NULL, ":");
die(2, strlen(saltstr) != 32, "Invalid challenge");
2020-11-25 18:05:19 +01:00
int r = dechex(saltstr, strlen(saltstr), salt, sizeof(salt));
die(2, r == -1, "Invalid challenge");
2020-11-25 18:05:19 +01:00
unsigned char password[16];
unsigned char hash[32];
argon2_context context = {
.out = hash,
.outlen = sizeof(hash),
.salt = salt,
.saltlen = sizeof(salt),
.pwd = password,
.pwdlen = sizeof(password),
.t_cost = iters,
.m_cost = memory,
.lanes = 1,
.threads = 1,
.flags = ARGON2_DEFAULT_FLAGS,
.version = ARGON2_VERSION_NUMBER,
2020-11-25 18:05:19 +01:00
};
die(1, strlen(argv[2]) != 32, "Invalid proof");
2020-11-25 18:05:19 +01:00
r = dechex(argv[2], strlen(argv[2]) + 1, password, sizeof(password));
die(1, r == -1, "Invalid proof");
2020-11-25 18:05:19 +01:00
r = argon2id_ctx(&context);
die(1, r != 0, "argon2id failed\n");
2020-11-25 18:05:19 +01:00
if (hash_msb(hash) < digits) {
printf("proof: failed\n");
return 1;
2020-11-25 18:05:19 +01:00
}
printf("proof: ok\n");
2020-11-25 17:15:59 +01:00
return 0;
}