The scrypt key derivation function was originally developed for use in the Tarsnap online backup system and is designed to be far more secure against hardware brute-force attacks than alternative functions such as PBKDF2 or bcrypt.

Graham Percival fc48052bdf DO NOT MERGE Faster blkxor in plain smix 2 年之前
.github 759c75f92c Actions: clarify -Wno-reserved-macro-identifier 1 年之前
lib fc48052bdf DO NOT MERGE Faster blkxor in plain smix 1 年之前
lib-platform 15df4c2b99 memlimit.c: remove leading underscores; handle non-POSIX in build system 2 年之前
libcperciva 6d38ae88cf Fix indentation of #define ... while (0) 1 年之前
libscrypt-kdf d2300546d6 style & spelling 1 年之前
m4 56a17c7d2a build: autoconf nitpick 1 年之前
release-tools 79497e00dc scripts: always use double quotes and curly braces 1 年之前
tests 566a86046a scripts: fix counting checks 1 年之前
.autom4te.cfg 2e589805a2 autom4te.cache is stupid. 9 年之前
.gitignore d079cf2e2a gitignore 1 年之前
BUILDING ef42bc6a26 BUILDING: mention cross-compiling runtime checks 1 年之前
COPYRIGHT c6e440c4f5 copyright: update dates 1 年之前
FORMAT c40745aa1f Update to my latest tree. 12 年之前
Makefile.am 02e2cd6189 scryptenc_print_error.{c,h}: add 1 年之前
README.md 710793dae2 Update version numbers to 1.3.2. 1 年之前
STYLE 1bf1554962 STYLE: mention switch() for cpusupport 3 年之前
configure.ac 9b55bbff52 build: move compiler warnings check to m4/ 2 年之前
get-version.sh 710793dae2 Update version numbers to 1.3.2. 1 年之前
main.c e8c48b4463 Fix missing fclose() warnp() 1 年之前
scrypt.1 f7d39dd581 man: new sentence, new line 1 年之前

README.md

The scrypt key derivation function

The scrypt key derivation function was originally developed for use in the Tarsnap online backup system and is designed to be far more secure against hardware brute-force attacks than alternative functions such as PBKDF2 or bcrypt.

We estimate that on modern (2009) hardware, if 5 seconds are spent computing a derived key, the cost of a hardware brute-force attack against scrypt is roughly 4000 times greater than the cost of a similar attack against bcrypt (to find the same password), and 20000 times greater than a similar attack against PBKDF2. If the scrypt encryption utility is used with default parameters, the cost of cracking the password on a file encrypted by scrypt enc is approximately 100 billion times more than the cost of cracking the same password on a file encrypted by openssl enc; this means that a five-character password using scrypt is stronger than a ten-character password using openssl.

Details of the scrypt key derivation function are given in:

Some additional articles may be of interest:

  • Filippo Valsorda presented a very well-written explanation about how the scrypt parameters impact the memory usage and CPU time of the algorithm.
  • J. Alwen, B. Chen, K. Pietrzak, L. Reyzin, S. Tessaro, Scrypt is Maximally Memory-Hard, Cryptology ePrint Archive: Report 2016/989.

The scrypt encryption utility

A simple password-based encryption utility is available as a demonstration of the scrypt key derivation function. It can be invoked as:

  • scrypt enc [options] infile [outfile] to encrypt data,
  • scrypt dec [options] infile [outfile] to decrypt data, or
  • scrypt info infile to see the encryption parameters used, and the memory required to decrypt the encrypted file.

If [outfile] is not specified, the output is written to standard output. scrypt also supports a number of command-line [options]:

  • -t maxtime will instruct scrypt to spend at most maxtime seconds computing the derived encryption key from the password; for encryption, this value will determine how secure the encrypted data is, while for decryption this value is used as an upper limit (if scrypt detects that it would take too long to decrypt the data, it will exit with an error message).
  • -m maxmemfrac instructs scrypt to use at most the specified fraction of the available RAM for computing the derived encryption key. For encryption, increasing this value might increase the security of the encrypted data, depending on the maxtime value; for decryption, this value is used as an upper limit and may cause scrypt to exit with an error.
  • -M maxmem instructs scrypt to use at most the specified number of bytes of RAM when computing the derived encryption key.
  • --logN value1, -r value2, -p value3 will set the encryption parameters explicitly.
  • --passphrase method:arg allows the user to specify whether to read the passphrase from stdin, /dev/tty, an environment variable, or a file.

If the encrypted data is corrupt, scrypt dec will exit with a non-zero status. However, scrypt dec may produce output before it determines that the encrypted data was corrupt, so for applications which require data to be authenticated, you must store the output of scrypt dec in a temporary location and check scrypt's exit code before using the decrypted data.

Using scrypt as a KDF

To use scrypt as a key derivation function (KDF) with libscrypt-kdf, include scrypt-kdf.h and use:

/**
 * scrypt_kdf(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
 * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
 * p, buflen) and write the result into buf.  The parameters r, p, and buflen
 * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32.  The parameter N
 * must be a power of 2 greater than 1.
 *
 * Return 0 on success; or -1 on error.
 */
int scrypt_kdf(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t,
    uint32_t, uint32_t, uint8_t *, size_t);

There is a sample of using this function in tests/libscrypt-kdf. If you installed the library, you can compile that file and run the binary:

$ cd tests/libscrypt-kdf/
$ c99 sample-libscrypt-kdf.c -lscrypt-kdf
$ ./a.out
crypto_scrypt(): success

If you would rather copy our source files directly into your project, then take a look at the lib/crypto/crypto_scrypt.h header, which provides crypto_scrypt().

Official releases

The scrypt utility has been tested on FreeBSD, NetBSD, OpenBSD, Linux (Slackware, CentOS, Gentoo, Ubuntu), Solaris, OS X, Cygwin, and GNU Hurd.

This cleartext signature of the SHA256 output can be verified with:

  gpg --decrypt scrypt-sigs-1.3.2.asc

You may then compare the displayed hash to the SHA256 hash of scrypt-1.3.2.tgz.

In addition, scrypt is available in the OpenBSD and FreeBSD ports trees and in NetBSD pkgsrc as security/scrypt.

Building

:exclamation: We strongly recommend that people use the latest official release tarball on https://www.tarsnap.com/scrypt.html

To build scrypt, extract the tarball and run ./configure && make. See the BUILDING file for more details (e.g., dealing with OpenSSL on OSX).

Testing

A small test suite can be run with:

make test

On platforms with less than 1 GB of RAM, use:

make test SMALLMEM=1

Memory-testing normal operations with valgrind (takes approximately 4 times as long as no valgrind tests) can be enabled with:

make test USE_VALGRIND=1

Memory-testing all tests with valgrind (requires over 1 GB memory, and takes approximately 4 times as long as USE_VALGRIND=1) can be enabled with:

make test USE_VALGRIND=2

Mailing list

The scrypt key derivation function and the scrypt encryption utility are discussed on the scrypt@tarsnap.com mailing list.