-
Notifications
You must be signed in to change notification settings - Fork 17
/
api.c
81 lines (61 loc) · 1.74 KB
/
api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <balloon.h>
#include <stdio.h>
#include <string.h>
#include "constants.h"
#include "errors.h"
#include "parse.h"
int balloon_internal (uint8_t out[BLOCK_SIZE],
const uint8_t salt[SALT_LEN],
const void *in, size_t inlen,
struct balloon_options *opts);
static int
get_salt (uint8_t *salt)
{
FILE *fp = fopen("/dev/urandom", "r");
if (!fp) return ERROR_URANDOM;
int ch;
for (int i = 0; i < SALT_LEN; i++) {
ch = getc (fp);
if (ch < 0) return ERROR_URANDOM;
salt[i] = (uint8_t)ch;
}
if (fclose(fp))
return ERROR_URANDOM;
return ERROR_NONE;
}
int
Balloon_Hash (char out[BLOB_LEN], struct balloon_options *opt,
const char *passwd, size_t passwd_len)
{
int error;
uint8_t hash_bytes[BLOCK_SIZE];
uint8_t salt_bytes[SALT_LEN];
if ((error = get_salt (salt_bytes)) != ERROR_NONE)
return error;
if ((error = balloon_internal (hash_bytes, salt_bytes, passwd, passwd_len, opt)) != ERROR_NONE)
return error;
if ((error = write_blob (out, BLOB_LEN,
salt_bytes,
hash_bytes, BLOCK_SIZE,
opt->s_cost, opt->t_cost, opt->n_threads)))
return error;
return ERROR_NONE;
}
int
Balloon_Verify (const char blob[BLOB_LEN], const char *passwd, size_t passwd_len)
{
int error;
uint8_t hash[BLOCK_SIZE];
uint8_t salt[SALT_LEN];
struct balloon_options opt;
if ((error = read_blob (blob, BLOB_LEN,
salt,
hash, BLOCK_SIZE,
&opt.s_cost, &opt.t_cost, &opt.n_threads)))
return error;
uint8_t hash2[BLOCK_SIZE];
if ((error = balloon_internal (hash2, salt, passwd, passwd_len, &opt)) != ERROR_NONE)
return error;
// Return ERROR_NONE on success.
return !memcmp ((char *)hash, (char *)hash2, BLOCK_SIZE) ? ERROR_NONE : ERROR_HASH_MISMATCH;
}