Speed

Other Password Functions

scrypt and Argon2 are password functions that also take a memory cost, and OpenSSL's enc command derives a cipher key and IV from a password with PBKDF2; run scrypt on the test vector of RFC 7914 and encrypt a message from a password file.

The question

The lessons so far used PBKDF2 through a program. Other password functions exist in the same tool, and one of its everyday commands, enc, uses PBKDF2 inside to turn a password into the key and the IV of a cipher. What does each of them take as input, and what does a command that uses one of them have to say?

This lesson runs three of them and says what the page did not check.

Work it by hand

RFC 7914 section 12 gives test vectors for scrypt, with the parameters in order: the password P, the salt S, the CPU and memory cost N, the block size r, the parallelization p and the output size dkLen. The first vector is scrypt(P="", S="", N=16, r=1, p=1, dkLen=64), and the output begins 77 d6 57 62 38 65 7b 20 3b 19 ca 42 c1 8a 04 97 and ends e8 d3 e0 fb 2e 0d 36 28 cf 35 e2 0c 38 d1 89 06.

An empty password and an empty salt are a vector, not a recommendation: they are test material. N = 16 is a toy value of the RFC's test vector.

Run it

The command computes that vector. kdf takes the password and the salt as options, n, r and p as scrypt's parameters, and -keylen 64 as the output size. With -binary and -out out.bin it writes the 64 bytes.

The 64 bytes that scrypt wrote to out.bin, starting 77d65762 38657b20 3b19ca42 c18a0497 and ending 38d18906, and their count, 64.

Public test material, generated for this lesson. Never use it for anything real.

# scrypt with the first test vector of RFC 7914 section 12.
# The 64 bytes go to out.bin.
openssl kdf -keylen 64 -kdfopt pass: -kdfopt salt: -kdfopt n:16 -kdfopt r:1 -kdfopt p:1 -binary -out out.bin SCRYPT
{
  "answer": "77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906",
  "file": "out.bin",
  "bytes": 64
}

You can change the output size from 1 byte up, the password and the salt, and n:32; -keylen 0 stops with Invalid derived key length. An n that is not a power of two, such as n:15, an r:0 or a p:0 stops with KDF parameter error. Large values of n, r and p need memory and time in the tab, and the page did not measure that limit: keep them small.

Your turn

The command below uses the enc command with a password. It runs and writes out.bin. It is meant to encrypt the file in/message.txt with AES-128-CBC, with the key and the IV derived by PBKDF2-HMAC-SHA-256 from the password in in/pass.txt with 1000 iterations and the salt 0102030405060708, which is -S 0102030405060708, and to write the ciphertext, with no header, to out.bin. Edit the command until it does. You may type any single openssl command. Check runs your command on the data shown and on four others.

The password hunter2, the message attack at dawn, the 16 bytes that the starting command wrote to out.bin, starting 5978fc28, and their count, 16.

Public test material, generated for this lesson. Never use it for anything real.

# in/pass.txt holds the password and in/message.txt the message.
# The ciphertext goes to out.bin.
openssl enc -aes-128-cbc -pbkdf2 -iter 1000 -S 0102030405060708 -pass pass:in/pass.txt -in in/message.txt -out out.bin
{
  "answer": "5978fc28eb967c83b24a209b3d60f946",
  "file": "out.bin",
  "bytes": 16
}

Write one openssl command that encrypts the file in/message.txt with AES-128-CBC and writes the ciphertext to out.bin, with no header. The key and the IV come from PBKDF2-HMAC-SHA-256 applied to the password in the file in/pass.txt (no final newline), the salt 0102030405060708 given in hex and 1000 iterations. Check runs your command with other passwords and messages in those files, including an empty message.

Read the result carefully

The page's oracle splits one 32-byte PBKDF2-HMAC-SHA-256 output into a key of 16 bytes and an IV of 16 bytes, encrypts with its own AES code, and agrees with enc on every case. That is what the command does here, not a rule about every version of the tool.

kdf also runs Argon2id: with iter:1, memcost:8 and lanes:1, toy values, the password password and the salt somesalt, the command kdf -keylen 16 ... ARGON2ID gave f73a73d271859d21ef45465da12bdf86 for 16 bytes: stated here, not run on this page. The page did not compare this output with a test vector of RFC 9106. The page does not show scrypt's or Argon2's internals, hardware cost, a pepper, password storage in a server, rate limiting or password policy.

Recap

  • kdf runs scrypt with the parameters N, r and p of RFC 7914.
  • The output of a password function depends on every parameter you give it.
  • enc -pbkdf2 derives the cipher key and IV from the password with the salt and the iteration count.
  • The salt and the iteration count are part of what the output depends on.