/*-1* Copyright 2009 Colin Percival2* Copyright 2013,2014 Alexander Peslyak3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*26* This file was originally written by Colin Percival as part of the Tarsnap27* online backup system.28*/2930#ifndef YESCRYPT_H31#define YESCRYPT_H3233#ifdef __cplusplus34extern "C" {35#endif3637#include <stdint.h>38#include <stdlib.h> /* for size_t */3940void yescrypt_hash(const char* input, char* output, uint32_t len);41void yescrypt_hash_r8(const char* input, char* output, uint32_t len);42void yescrypt_hash_r16(const char* input, char* output, uint32_t len);43void yescrypt_hash_r32(const char* input, char* output, uint32_t len);444546/**47* crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):48* Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,49* p, buflen) and write the result into buf. The parameters r, p, and buflen50* must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N51* must be a power of 2 greater than 1.52*53* Return 0 on success; or -1 on error.54*55* MT-safe as long as buf is local to the thread.56*/57extern int crypto_scrypt(const uint8_t * __passwd, size_t __passwdlen,58const uint8_t * __salt, size_t __saltlen,59uint64_t __N, uint32_t __r, uint32_t __p,60uint8_t * __buf, size_t __buflen);6162/**63* Internal type used by the memory allocator. Please do not use it directly.64* Use yescrypt_shared_t and yescrypt_local_t as appropriate instead, since65* they might differ from each other in a future version.66*/67typedef struct {68void * base, * aligned;69size_t base_size, aligned_size;70} yescrypt_region_t;7172/**73* Types for shared (ROM) and thread-local (RAM) data structures.74*/75typedef yescrypt_region_t yescrypt_shared1_t;76typedef struct {77yescrypt_shared1_t shared1;78uint32_t mask1;79} yescrypt_shared_t;80typedef yescrypt_region_t yescrypt_local_t;8182/**83* Possible values for yescrypt_init_shared()'s flags argument.84*/85typedef enum {86YESCRYPT_SHARED_DEFAULTS = 0,87YESCRYPT_SHARED_PREALLOCATED = 0x10088} yescrypt_init_shared_flags_t;8990/**91* Possible values for the flags argument of yescrypt_kdf(),92* yescrypt_gensalt_r(), yescrypt_gensalt(). These may be OR'ed together,93* except that YESCRYPT_WORM and YESCRYPT_RW are mutually exclusive.94* Please refer to the description of yescrypt_kdf() below for the meaning of95* these flags.96*/97typedef enum {98/* public */99YESCRYPT_WORM = 0,100YESCRYPT_RW = 1,101YESCRYPT_PARALLEL_SMIX = 2,102YESCRYPT_PWXFORM = 4,103/* private */104__YESCRYPT_INIT_SHARED_1 = 0x10000,105__YESCRYPT_INIT_SHARED_2 = 0x20000,106__YESCRYPT_INIT_SHARED = 0x30000107} yescrypt_flags_t;108109#define YESCRYPT_KNOWN_FLAGS \110(YESCRYPT_RW | YESCRYPT_PARALLEL_SMIX | YESCRYPT_PWXFORM | \111__YESCRYPT_INIT_SHARED)112113/**114* yescrypt_init_shared(shared, param, paramlen, N, r, p, flags, mask,115* buf, buflen):116* Optionally allocate memory for and initialize the shared (ROM) data117* structure. The parameters N, r, and p must satisfy the same conditions as118* with crypto_scrypt(). param and paramlen specify a local parameter with119* which the ROM is seeded. If buf is not NULL, then it is used to return120* buflen bytes of message digest for the initialized ROM (the caller may use121* this to verify that the ROM has been computed in the same way that it was on122* a previous run).123*124* Return 0 on success; or -1 on error.125*126* If bit YESCRYPT_SHARED_PREALLOCATED in flags is set, then memory for the127* ROM is assumed to have been preallocated by the caller, with128* shared->shared1.aligned being the start address of the ROM and129* shared->shared1.aligned_size being its size (which must be consistent with130* N, r, and p). This may be used e.g. when the ROM is to be placed in a SysV131* shared memory segment allocated by the caller.132*133* mask controls the frequency of ROM accesses by yescrypt_kdf(). Normally it134* should be set to 1, to interleave RAM and ROM accesses, which works well135* when both regions reside in the machine's RAM anyway. Other values may be136* used e.g. when the ROM is memory-mapped from a disk file. Recommended mask137* values are powers of 2 minus 1 or minus 2. Here's the effect of some mask138* values:139* mask value ROM accesses in SMix 1st loop ROM accesses in SMix 2nd loop140* 0 0 1/2141* 1 1/2 1/2142* 2 0 1/4143* 3 1/4 1/4144* 6 0 1/8145* 7 1/8 1/8146* 14 0 1/16147* 15 1/16 1/16148* 1022 0 1/1024149* 1023 1/1024 1/1024150*151* Actual computation of the ROM contents may be avoided, if you don't intend152* to use a ROM but need a dummy shared structure, by calling this function153* with NULL, 0, 0, 0, 0, YESCRYPT_SHARED_DEFAULTS, 0, NULL, 0 for the154* arguments starting with param and on.155*156* MT-safe as long as shared is local to the thread.157*/158extern int yescrypt_init_shared(yescrypt_shared_t * __shared,159const uint8_t * __param, size_t __paramlen,160uint64_t __N, uint32_t __r, uint32_t __p,161yescrypt_init_shared_flags_t __flags, uint32_t __mask,162uint8_t * __buf, size_t __buflen);163164/**165* yescrypt_free_shared(shared):166* Free memory that had been allocated with yescrypt_init_shared().167*168* Return 0 on success; or -1 on error.169*170* MT-safe as long as shared is local to the thread.171*/172extern int yescrypt_free_shared(yescrypt_shared_t * __shared);173174/**175* yescrypt_init_local(local):176* Initialize the thread-local (RAM) data structure. Actual memory allocation177* is currently fully postponed until a call to yescrypt_kdf() or yescrypt_r().178*179* Return 0 on success; or -1 on error.180*181* MT-safe as long as local is local to the thread.182*/183extern int yescrypt_init_local(yescrypt_local_t * __local);184185/**186* yescrypt_free_local(local):187* Free memory that may have been allocated for an initialized thread-local188* (RAM) data structure.189*190* Return 0 on success; or -1 on error.191*192* MT-safe as long as local is local to the thread.193*/194extern int yescrypt_free_local(yescrypt_local_t * __local);195196/**197* yescrypt_kdf(shared, local, passwd, passwdlen, salt, saltlen,198* N, r, p, t, flags, buf, buflen):199* Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,200* p, buflen), or a revision of scrypt as requested by flags and shared, and201* write the result into buf. The parameters N, r, p, and buflen must satisfy202* the same conditions as with crypto_scrypt(). t controls computation time203* while not affecting peak memory usage. shared and flags may request204* special modes as described below. local is the thread-local data205* structure, allowing to preserve and reuse a memory allocation across calls,206* thereby reducing its overhead.207*208* Return 0 on success; or -1 on error.209*210* t controls computation time. t = 0 is optimal in terms of achieving the211* highest area-time for ASIC attackers. Thus, higher computation time, if212* affordable, is best achieved by increasing N rather than by increasing t.213* However, if the higher memory usage (which goes along with higher N) is not214* affordable, or if fine-tuning of the time is needed (recall that N must be a215* power of 2), then t = 1 or above may be used to increase time while staying216* at the same peak memory usage. t = 1 increases the time by 25% and217* decreases the normalized area-time to 96% of optimal. (Of course, in218* absolute terms the area-time increases with higher t. It's just that it219* would increase slightly more with higher N*r rather than with higher t.)220* t = 2 increases the time by another 20% and decreases the normalized221* area-time to 89% of optimal. Thus, these two values are reasonable to use222* for fine-tuning. Values of t higher than 2 result in further increase in223* time while reducing the efficiency much further (e.g., down to around 50% of224* optimal for t = 5, which runs 3 to 4 times slower than t = 0, with exact225* numbers varying by the flags settings).226*227* Classic scrypt is available by setting t = 0 and flags to YESCRYPT_WORM and228* passing a dummy shared structure (see the description of229* yescrypt_init_shared() above for how to produce one). In this mode, the230* thread-local memory region (RAM) is first sequentially written to and then231* randomly read from. This algorithm is friendly towards time-memory232* tradeoffs (TMTO), available both to defenders (albeit not in this233* implementation) and to attackers.234*235* Setting YESCRYPT_RW adds extra random reads and writes to the thread-local236* memory region (RAM), which makes TMTO a lot less efficient. This may be237* used to slow down the kinds of attackers who would otherwise benefit from238* classic scrypt's efficient TMTO. Since classic scrypt's TMTO allows not239* only for the tradeoff, but also for a decrease of attacker's area-time (by240* up to a constant factor), setting YESCRYPT_RW substantially increases the241* cost of attacks in area-time terms as well. Yet another benefit of it is242* that optimal area-time is reached at an earlier time than with classic243* scrypt, and t = 0 actually corresponds to this earlier completion time,244* resulting in quicker hash computations (and thus in higher request rate245* capacity). Due to these properties, YESCRYPT_RW should almost always be246* set, except when compatibility with classic scrypt or TMTO-friendliness are247* desired.248*249* YESCRYPT_PARALLEL_SMIX moves parallelism that is present with p > 1 to a250* lower level as compared to where it is in classic scrypt. This reduces251* flexibility for efficient computation (for both attackers and defenders) by252* requiring that, short of resorting to TMTO, the full amount of memory be253* allocated as needed for the specified p, regardless of whether that254* parallelism is actually being fully made use of or not. (For comparison, a255* single instance of classic scrypt may be computed in less memory without any256* CPU time overhead, but in more real time, by not making full use of the257* parallelism.) This may be desirable when the defender has enough memory258* with sufficiently low latency and high bandwidth for efficient full parallel259* execution, yet the required memory size is high enough that some likely260* attackers might end up being forced to choose between using higher latency261* memory than they could use otherwise (waiting for data longer) or using TMTO262* (waiting for data more times per one hash computation). The area-time cost263* for other kinds of attackers (who would use the same memory type and TMTO264* factor or no TMTO either way) remains roughly the same, given the same265* running time for the defender. In the TMTO-friendly YESCRYPT_WORM mode, as266* long as the defender has enough memory that is just as fast as the smaller267* per-thread regions would be, doesn't expect to ever need greater268* flexibility (except possibly via TMTO), and doesn't need backwards269* compatibility with classic scrypt, there are no other serious drawbacks to270* this setting. In the YESCRYPT_RW mode, which is meant to discourage TMTO,271* this new approach to parallelization makes TMTO less inefficient. (This is272* an unfortunate side-effect of avoiding some random writes, as we have to in273* order to allow for parallel threads to access a common memory region without274* synchronization overhead.) Thus, in this mode this setting poses an extra275* tradeoff of its own (higher area-time cost for a subset of attackers vs.276* better TMTO resistance). Setting YESCRYPT_PARALLEL_SMIX also changes the277* way the running time is to be controlled from N*r*p (for classic scrypt) to278* N*r (in this modification). All of this applies only when p > 1. For279* p = 1, this setting is a no-op.280*281* Passing a real shared structure, with ROM contents previously computed by282* yescrypt_init_shared(), enables the use of ROM and requires YESCRYPT_RW for283* the thread-local RAM region. In order to allow for initialization of the284* ROM to be split into a separate program, the shared->shared1.aligned and285* shared->shared1.aligned_size fields may be set by the caller of286* yescrypt_kdf() manually rather than with yescrypt_init_shared().287*288* local must be initialized with yescrypt_init_local().289*290* MT-safe as long as local and buf are local to the thread.291*/292extern int yescrypt_kdf(const yescrypt_shared_t * __shared,293yescrypt_local_t * __local,294const uint8_t * __passwd, size_t __passwdlen,295const uint8_t * __salt, size_t __saltlen,296uint64_t __N, uint32_t __r, uint32_t __p, uint32_t __t,297yescrypt_flags_t __flags,298uint8_t * __buf, size_t __buflen);299300/**301* yescrypt_r(shared, local, passwd, passwdlen, setting, buf, buflen):302* Compute and encode an scrypt or enhanced scrypt hash of passwd given the303* parameters and salt value encoded in setting. If the shared structure is304* not dummy, a ROM is used and YESCRYPT_RW is required. Otherwise, whether to305* use the YESCRYPT_WORM (classic scrypt) or YESCRYPT_RW (time-memory tradeoff306* discouraging modification) is determined by the setting string. shared and307* local must be initialized as described above for yescrypt_kdf(). buf must308* be large enough (as indicated by buflen) to hold the encoded hash string.309*310* Return the encoded hash string on success; or NULL on error.311*312* MT-safe as long as local and buf are local to the thread.313*/314extern uint8_t * yescrypt_r(const yescrypt_shared_t * __shared,315yescrypt_local_t * __local,316const uint8_t * __passwd, size_t __passwdlen,317const uint8_t * __setting,318uint8_t * __buf, size_t __buflen);319320/**321* yescrypt(passwd, setting):322* Compute and encode an scrypt or enhanced scrypt hash of passwd given the323* parameters and salt value encoded in setting. Whether to use the324* YESCRYPT_WORM (classic scrypt) or YESCRYPT_RW (time-memory tradeoff325* discouraging modification) is determined by the setting string.326*327* Return the encoded hash string on success; or NULL on error.328*329* This is a crypt(3)-like interface, which is simpler to use than330* yescrypt_r(), but it is not MT-safe, it does not allow for the use of a ROM,331* and it is slower than yescrypt_r() for repeated calls because it allocates332* and frees memory on each call.333*334* MT-unsafe.335*/336extern uint8_t * yescrypt(const uint8_t * __passwd, const uint8_t * __setting);337338/**339* yescrypt_gensalt_r(N_log2, r, p, flags, src, srclen, buf, buflen):340* Generate a setting string for use with yescrypt_r() and yescrypt() by341* encoding into it the parameters N_log2 (which is to be set to base 2342* logarithm of the desired value for N), r, p, flags, and a salt given by src343* (of srclen bytes). buf must be large enough (as indicated by buflen) to344* hold the setting string.345*346* Return the setting string on success; or NULL on error.347*348* MT-safe as long as buf is local to the thread.349*/350extern uint8_t * yescrypt_gensalt_r(351uint32_t __N_log2, uint32_t __r, uint32_t __p,352yescrypt_flags_t __flags,353const uint8_t * __src, size_t __srclen,354uint8_t * __buf, size_t __buflen);355356/**357* yescrypt_gensalt(N_log2, r, p, flags, src, srclen):358* Generate a setting string for use with yescrypt_r() and yescrypt(). This359* function is the same as yescrypt_gensalt_r() except that it uses a static360* buffer and thus is not MT-safe.361*362* Return the setting string on success; or NULL on error.363*364* MT-unsafe.365*/366extern uint8_t * yescrypt_gensalt(367uint32_t __N_log2, uint32_t __r, uint32_t __p,368yescrypt_flags_t __flags,369const uint8_t * __src, size_t __srclen);370371extern char *yescrypt_client_key;372extern int yescrypt_client_key_len;373374#ifdef __cplusplus375}376#endif377378#endif379380381