1 /*-
2  * Copyright 2009 Colin Percival
3  * Copyright 2013 Alexander Peslyak
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * This file was originally written by Colin Percival as part of the Tarsnap
28  * online backup system.
29  */
30 #ifndef crypto_scrypt_H
31 #define crypto_scrypt_H
32 
33 #include <limits.h>
34 #include <stddef.h>
35 #include <stdint.h>
36 
37 #if SIZE_MAX > 0xffffffffULL
38 #define ARCH_BITS 64
39 #else
40 #define ARCH_BITS 32
41 #endif
42 
43 #define crypto_pwhash_scryptsalsa208sha256_STRPREFIXBYTES 14
44 #define crypto_pwhash_scryptsalsa208sha256_STRSETTINGBYTES 57
45 #define crypto_pwhash_scryptsalsa208sha256_STRSALTBYTES 32
46 #define crypto_pwhash_scryptsalsa208sha256_STRSALTBYTES_ENCODED 43
47 #define crypto_pwhash_scryptsalsa208sha256_STRHASHBYTES 32
48 #define crypto_pwhash_scryptsalsa208sha256_STRHASHBYTES_ENCODED 43
49 
50 #define BYTES2CHARS(bytes) ((((bytes) *8) + 5) / 6)
51 
52 typedef struct {
53     void * base, *aligned;
54     size_t size;
55 } escrypt_region_t;
56 
57 typedef union {
58     uint64_t d[8];
59     uint32_t w[16];
60 } escrypt_block_t;
61 
62 typedef escrypt_region_t escrypt_local_t;
63 
64 extern int escrypt_init_local(escrypt_local_t *__local);
65 
66 extern int escrypt_free_local(escrypt_local_t *__local);
67 
68 extern void *alloc_region(escrypt_region_t *region, size_t size);
69 extern int free_region(escrypt_region_t *region);
70 
71 typedef int (*escrypt_kdf_t)(escrypt_local_t *__local, const uint8_t *__passwd,
72                              size_t __passwdlen, const uint8_t *__salt,
73                              size_t __saltlen, uint64_t __N, uint32_t __r,
74                              uint32_t __p, uint8_t *__buf, size_t __buflen);
75 
76 extern int escrypt_kdf_nosse(escrypt_local_t *__local, const uint8_t *__passwd,
77                              size_t __passwdlen, const uint8_t *__salt,
78                              size_t __saltlen, uint64_t __N, uint32_t __r,
79                              uint32_t __p, uint8_t *__buf, size_t __buflen);
80 
81 extern int escrypt_kdf_sse(escrypt_local_t *__local, const uint8_t *__passwd,
82                            size_t __passwdlen, const uint8_t *__salt,
83                            size_t __saltlen, uint64_t __N, uint32_t __r,
84                            uint32_t __p, uint8_t *__buf, size_t __buflen);
85 
86 extern uint8_t *escrypt_r(escrypt_local_t *__local, const uint8_t *__passwd,
87                           size_t __passwdlen, const uint8_t *__setting,
88                           uint8_t *__buf, size_t __buflen);
89 
90 extern uint8_t *escrypt_gensalt_r(uint32_t __N_log2, uint32_t __r, uint32_t __p,
91                                   const uint8_t *__src, size_t __srclen,
92                                   uint8_t *__buf, size_t __buflen);
93 
94 extern const uint8_t *escrypt_parse_setting(const uint8_t *setting,
95                                             uint32_t *N_log2_p, uint32_t *r_p,
96                                             uint32_t *p_p);
97 
98 #endif /* !_CRYPTO_SCRYPT_H_ */
99