File: crypto-mcf.c

package info (click to toggle)
libscrypt 1.22-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 268 kB
  • sloc: ansic: 1,565; makefile: 144
file content (65 lines) | stat: -rw-r--r-- 1,290 bytes parent folder | download | duplicates (2)
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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

#include "libscrypt.h"

/* ilog2 for powers of two */
static uint32_t scrypt_ilog2(uint32_t n)
{
#ifndef S_SPLINT_S

	/* Check for a valid power of two */
	if (n < 2 || (n & (n - 1)))
		return -1;
#endif
	uint32_t t = 1;
	while (((uint32_t)1 << t) < n)
	{
		if(t > SCRYPT_SAFE_N)
			return (uint32_t) -1; /* Check for insanity */
		t++;
	}

	return t;
}

#ifdef _MSC_VER
  #define SNPRINTF _snprintf
#else
  #define SNPRINTF snprintf
#endif

int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt,
		const char *hash, char *mcf)
{

	uint32_t t, params;
	int s;

	if(!mcf || !hash)
		return 0;
	/* Although larger values of r, p are valid in scrypt, this mcf format
	* limits to 8 bits. If your number is larger, current computers will
	* struggle
	*/
	if(r > (uint8_t)(-1) || p > (uint8_t)(-1))
		return 0;

	t = scrypt_ilog2(N);
	if (t < 1)
		return 0;
		
	params = (r << 8) + p;
	params += (uint32_t)t << 16;
	
	/* Using snprintf - not checking for overflows. We've already
	* determined that mcf should be defined as at least SCRYPT_MCF_LEN
	* in length 
	*/
	s = SNPRINTF(mcf, SCRYPT_MCF_LEN,  SCRYPT_MCF_ID "$%06x$%s$%s", (unsigned int)params, salt, hash);
	if (s >= SCRYPT_MCF_LEN)
		return 0;

	return 1;
}