xref: /haiku/src/system/libroot/posix/crypt/crypto_scrypt.cpp (revision 1831c1c60dd1d26997e8dff4c0f281be3a237c62)
1f31b1a2fSAndrew Aldridge /*-
2f31b1a2fSAndrew Aldridge  * Copyright 2009 Colin Percival
3f31b1a2fSAndrew Aldridge  * All rights reserved.
4f31b1a2fSAndrew Aldridge  *
5f31b1a2fSAndrew Aldridge  * Redistribution and use in source and binary forms, with or without
6f31b1a2fSAndrew Aldridge  * modification, are permitted provided that the following conditions
7f31b1a2fSAndrew Aldridge  * are met:
8f31b1a2fSAndrew Aldridge  * 1. Redistributions of source code must retain the above copyright
9f31b1a2fSAndrew Aldridge  *    notice, this list of conditions and the following disclaimer.
10f31b1a2fSAndrew Aldridge  * 2. Redistributions in binary form must reproduce the above copyright
11f31b1a2fSAndrew Aldridge  *    notice, this list of conditions and the following disclaimer in the
12f31b1a2fSAndrew Aldridge  *    documentation and/or other materials provided with the distribution.
13f31b1a2fSAndrew Aldridge  *
14f31b1a2fSAndrew Aldridge  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15f31b1a2fSAndrew Aldridge  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16f31b1a2fSAndrew Aldridge  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17f31b1a2fSAndrew Aldridge  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18f31b1a2fSAndrew Aldridge  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19f31b1a2fSAndrew Aldridge  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20f31b1a2fSAndrew Aldridge  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21f31b1a2fSAndrew Aldridge  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22f31b1a2fSAndrew Aldridge  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23f31b1a2fSAndrew Aldridge  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24f31b1a2fSAndrew Aldridge  * SUCH DAMAGE.
25f31b1a2fSAndrew Aldridge  *
26f31b1a2fSAndrew Aldridge  * This file was originally written by Colin Percival as part of the Tarsnap
27f31b1a2fSAndrew Aldridge  * online backup system.
28f31b1a2fSAndrew Aldridge  */
29f31b1a2fSAndrew Aldridge #include <sys/types.h>
30f31b1a2fSAndrew Aldridge #include <sys/mman.h>
31f31b1a2fSAndrew Aldridge 
32f31b1a2fSAndrew Aldridge #include <errno.h>
33f31b1a2fSAndrew Aldridge #include <stdint.h>
34f31b1a2fSAndrew Aldridge #include <stdlib.h>
35f31b1a2fSAndrew Aldridge #include <string.h>
36f31b1a2fSAndrew Aldridge 
37f31b1a2fSAndrew Aldridge #include "pbkdf2.h"
38f31b1a2fSAndrew Aldridge 
39f31b1a2fSAndrew Aldridge #include "crypto_scrypt_smix.h"
40f31b1a2fSAndrew Aldridge 
41f31b1a2fSAndrew Aldridge #include "crypto_scrypt.h"
42f31b1a2fSAndrew Aldridge 
43f31b1a2fSAndrew Aldridge /**
44*1831c1c6SDavid Karoly  * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
45*1831c1c6SDavid Karoly  * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
46*1831c1c6SDavid Karoly  * p, buflen) and write the result into buf.  The parameters r, p, and buflen
47*1831c1c6SDavid Karoly  * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32.  The parameter N
48*1831c1c6SDavid Karoly  * must be a power of 2 greater than 1.
49*1831c1c6SDavid Karoly  *
50*1831c1c6SDavid Karoly  * Return 0 on success; or -1 on error.
51f31b1a2fSAndrew Aldridge  */
52*1831c1c6SDavid Karoly int
crypto_scrypt(const uint8_t * passwd,size_t passwdlen,const uint8_t * salt,size_t saltlen,uint64_t N,uint32_t _r,uint32_t _p,uint8_t * buf,size_t buflen)53*1831c1c6SDavid Karoly crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
54f31b1a2fSAndrew Aldridge     const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t _r, uint32_t _p,
55*1831c1c6SDavid Karoly     uint8_t * buf, size_t buflen)
56f31b1a2fSAndrew Aldridge {
57f31b1a2fSAndrew Aldridge 	void * B0, * V0, * XY0;
58f31b1a2fSAndrew Aldridge 	uint8_t * B;
59f31b1a2fSAndrew Aldridge 	uint32_t * V;
60f31b1a2fSAndrew Aldridge 	uint32_t * XY;
61f31b1a2fSAndrew Aldridge 	size_t r = _r, p = _p;
62f31b1a2fSAndrew Aldridge 	uint32_t i;
63f31b1a2fSAndrew Aldridge 
64f31b1a2fSAndrew Aldridge 	/* Sanity-check parameters. */
65f31b1a2fSAndrew Aldridge #if SIZE_MAX > UINT32_MAX
66f31b1a2fSAndrew Aldridge 	if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
67f31b1a2fSAndrew Aldridge 		errno = EFBIG;
68f31b1a2fSAndrew Aldridge 		goto err0;
69f31b1a2fSAndrew Aldridge 	}
70f31b1a2fSAndrew Aldridge #endif
71f31b1a2fSAndrew Aldridge 	if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
72f31b1a2fSAndrew Aldridge 		errno = EFBIG;
73f31b1a2fSAndrew Aldridge 		goto err0;
74f31b1a2fSAndrew Aldridge 	}
75f31b1a2fSAndrew Aldridge 	if (((N & (N - 1)) != 0) || (N < 2)) {
76f31b1a2fSAndrew Aldridge 		errno = EINVAL;
77f31b1a2fSAndrew Aldridge 		goto err0;
78f31b1a2fSAndrew Aldridge 	}
79f31b1a2fSAndrew Aldridge 	if ((r > SIZE_MAX / 128 / p) ||
80f31b1a2fSAndrew Aldridge #if SIZE_MAX / 256 <= UINT32_MAX
81f31b1a2fSAndrew Aldridge 	    (r > (SIZE_MAX - 64) / 256) ||
82f31b1a2fSAndrew Aldridge #endif
83f31b1a2fSAndrew Aldridge 	    (N > SIZE_MAX / 128 / r)) {
84f31b1a2fSAndrew Aldridge 		errno = ENOMEM;
85f31b1a2fSAndrew Aldridge 		goto err0;
86f31b1a2fSAndrew Aldridge 	}
87f31b1a2fSAndrew Aldridge 
88f31b1a2fSAndrew Aldridge 	/* Allocate memory. */
89f31b1a2fSAndrew Aldridge #ifdef HAVE_POSIX_MEMALIGN
90f31b1a2fSAndrew Aldridge 	if ((errno = posix_memalign(&B0, 64, 128 * r * p)) != 0)
91f31b1a2fSAndrew Aldridge 		goto err0;
92f31b1a2fSAndrew Aldridge 	B = (uint8_t *)(B0);
93f31b1a2fSAndrew Aldridge 	if ((errno = posix_memalign(&XY0, 64, 256 * r + 64)) != 0)
94f31b1a2fSAndrew Aldridge 		goto err1;
95f31b1a2fSAndrew Aldridge 	XY = (uint32_t *)(XY0);
96f31b1a2fSAndrew Aldridge #if !defined(MAP_ANON) || !defined(HAVE_MMAP)
97f31b1a2fSAndrew Aldridge 	if ((errno = posix_memalign(&V0, 64, 128 * r * N)) != 0)
98f31b1a2fSAndrew Aldridge 		goto err2;
99f31b1a2fSAndrew Aldridge 	V = (uint32_t *)(V0);
100f31b1a2fSAndrew Aldridge #endif
101f31b1a2fSAndrew Aldridge #else
102f31b1a2fSAndrew Aldridge 	if ((B0 = malloc(128 * r * p + 63)) == NULL)
103f31b1a2fSAndrew Aldridge 		goto err0;
104f31b1a2fSAndrew Aldridge 	B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
105f31b1a2fSAndrew Aldridge 	if ((XY0 = malloc(256 * r + 64 + 63)) == NULL)
106f31b1a2fSAndrew Aldridge 		goto err1;
107f31b1a2fSAndrew Aldridge 	XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63));
108f31b1a2fSAndrew Aldridge #if !defined(MAP_ANON) || !defined(HAVE_MMAP)
109f31b1a2fSAndrew Aldridge 	if ((V0 = malloc(128 * r * N + 63)) == NULL)
110f31b1a2fSAndrew Aldridge 		goto err2;
111f31b1a2fSAndrew Aldridge 	V = (uint32_t *)(((uintptr_t)(V0) + 63) & ~ (uintptr_t)(63));
112f31b1a2fSAndrew Aldridge #endif
113f31b1a2fSAndrew Aldridge #endif
114f31b1a2fSAndrew Aldridge #if defined(MAP_ANON) && defined(HAVE_MMAP)
115f31b1a2fSAndrew Aldridge 	if ((V0 = mmap(NULL, 128 * r * N, PROT_READ | PROT_WRITE,
116f31b1a2fSAndrew Aldridge #ifdef MAP_NOCORE
117f31b1a2fSAndrew Aldridge 	    MAP_ANON | MAP_PRIVATE | MAP_NOCORE,
118f31b1a2fSAndrew Aldridge #else
119f31b1a2fSAndrew Aldridge 	    MAP_ANON | MAP_PRIVATE,
120f31b1a2fSAndrew Aldridge #endif
121f31b1a2fSAndrew Aldridge 	    -1, 0)) == MAP_FAILED)
122f31b1a2fSAndrew Aldridge 		goto err2;
123f31b1a2fSAndrew Aldridge 	V = (uint32_t *)(V0);
124f31b1a2fSAndrew Aldridge #endif
125f31b1a2fSAndrew Aldridge 
126f31b1a2fSAndrew Aldridge 	/* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
127f31b1a2fSAndrew Aldridge 	PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
128f31b1a2fSAndrew Aldridge 
129f31b1a2fSAndrew Aldridge 	/* 2: for i = 0 to p - 1 do */
130f31b1a2fSAndrew Aldridge 	for (i = 0; i < p; i++) {
131f31b1a2fSAndrew Aldridge 		/* 3: B_i <-- MF(B_i, N) */
132*1831c1c6SDavid Karoly 		crypto_scrypt_smix(&B[i * 128 * r], r, N, V, XY);
133f31b1a2fSAndrew Aldridge 	}
134f31b1a2fSAndrew Aldridge 
135f31b1a2fSAndrew Aldridge 	/* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
136f31b1a2fSAndrew Aldridge 	PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
137f31b1a2fSAndrew Aldridge 
138f31b1a2fSAndrew Aldridge 	/* Free memory. */
139f31b1a2fSAndrew Aldridge #if defined(MAP_ANON) && defined(HAVE_MMAP)
140f31b1a2fSAndrew Aldridge 	if (munmap(V0, 128 * r * N))
141f31b1a2fSAndrew Aldridge 		goto err2;
142f31b1a2fSAndrew Aldridge #else
143f31b1a2fSAndrew Aldridge 	free(V0);
144f31b1a2fSAndrew Aldridge #endif
145f31b1a2fSAndrew Aldridge 	free(XY0);
146f31b1a2fSAndrew Aldridge 	free(B0);
147f31b1a2fSAndrew Aldridge 
148f31b1a2fSAndrew Aldridge 	/* Success! */
149f31b1a2fSAndrew Aldridge 	return (0);
150f31b1a2fSAndrew Aldridge 
151f31b1a2fSAndrew Aldridge err2:
152f31b1a2fSAndrew Aldridge 	free(XY0);
153f31b1a2fSAndrew Aldridge err1:
154f31b1a2fSAndrew Aldridge 	free(B0);
155f31b1a2fSAndrew Aldridge err0:
156f31b1a2fSAndrew Aldridge 	/* Failure! */
157f31b1a2fSAndrew Aldridge 	return (-1);
158f31b1a2fSAndrew Aldridge }
159