summaryrefslogtreecommitdiff
path: root/crypto_scrypt-check.c
blob: 1d954a13b9e9ac35546e33c091b81610eea5c3ef (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

#include "b64.h"
#include "libscrypt.h"

/* pow() works with doubles. Sounds like it should cast to int correctly,
* but doesn't always. This is faster anyway
*/
static uint16_t ipow(uint16_t base, uint32_t exp)
{
    uint16_t result = 1;
    while (exp != 0)
    {
        if ((exp & 1) != 0)
            result *= base;
        exp >>= 1;
        base *= base;
    }

    return result;
}

int libscrypt_check(char *mcf, char *password)
{

	uint32_t params;
	uint16_t N;
	uint8_t r, p;
	int retval;
	uint8_t hashbuf[64];
	char outbuf[128];
	char salt[32];
	char *tok;

	if(memcmp(mcf, SCRYPT_MCF_ID, 3) != 0)
	{
		/* Only version 0 supported */
		return -1;
	}

	tok = strtok(mcf, "$");
	tok = strtok(NULL, "$");

	if ( !tok )
		return -1;

	params = (uint32_t)strtoul(tok, NULL, 16);
	if ( params == 0 )
		return -1;

	tok = strtok(NULL, "$");

	if ( !tok )
		return -1;

	p = params & 0xff;
	r = (params >> 8) & 0xff;
	N = params >> 16;
	N = ipow(2, N);

	/* Useful debugging:
	printf("We've obtained salt 'N' r p of '%s' %d %d %d\n", tok, N,r,p);
	*/

	retval = libscrypt_b64_decode(tok, (uint8_t*)salt, sizeof(salt));
	if (retval < 1)
		return -1;
	retval = libscrypt_scrypt((uint8_t*)password,strlen(password), (uint8_t*)salt, (uint32_t)retval, N, r, p, hashbuf, sizeof(hashbuf));

	if (retval != 0)
		return retval;

	retval = libscrypt_b64_encode(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf));
	if (retval == 0)
		return -1;

	tok = strtok(NULL, "$");

	if ( !tok )
		return -1;

	if(strcmp(tok, outbuf) == 0)
	{
		return 1;
	}

	return 0;
}