5
$\begingroup$

Looking in some cryptographic algorithms, I've realized that: The way the plain text is encrypted/decrypted is always specified, but what about the key? Every paper I've seen describing the algorithm never show a way to generate a key, but show the available key sizes.

This leads me to a question: How should I generate a key as someone who is implementing an algorithm? For example: The Twofish paper (https://www.schneier.com/paper-twofish-paper.pdf) says that Twofish has available key sizes of 128, 192 and 256 bits, but how should I create a 128 bit key? Not even the reference implementations I found contains code that seems to be aimed to be a key generation algorithm (again speaking of Twofish).

For a university work, I'll write a simple implementation of Twofish in C#, and I need a way to create a key but I don't know how to do it or even if there's a correct way to do this, that's why I ask this question.

A real example: When I encrypt anything with GnuPG (https://www.gnupg.org/) using symmetric keys, it does not generate any key or the like, it just asks for a password and does it. What's happening behind the scenes? How does GPG uses this password and how it's is related to the key generation?

$\endgroup$
  • 2
    $\begingroup$ crypto.stackexchange.com/questions/3332/… $\endgroup$ – Richie Frame Oct 6 '15 at 23:22
  • 2
    $\begingroup$ crypto.stackexchange.com/questions/25485/… $\endgroup$ – Richie Frame Oct 6 '15 at 23:22
  • $\begingroup$ TL;DR, symmetric keys for modern ciphers are nothing more than cryptographically random blobs of the appropriate length. $\endgroup$ – Stephen Touset Oct 7 '15 at 0:07
  • $\begingroup$ The easiest low-tech way for a cryptographically secure key is to get one of those bingo drums. Leave in the chits for 1 through 63, and put another one in that you designate 0. Each draw of a chit is 6 bits of entropy. Keep drawing until you fill your key, placing the chit back in the drum for each spin. As long as only the 2 people sharing secrets know those numbers, their secret is secure. But it's much more common to hash a password millions of times. Check PBKDF2 for a formal specification to make passwords into keys. $\endgroup$ – WDS Oct 7 '15 at 2:23
  • $\begingroup$ PGP is using a Key Derivation to produce a symmetric session key from a passphrase. It has even two modes, it can genrate a randome symmetric key and wrap this with the derived key from the password or it can use the key derived from the password directly. Typically langages offer PBE ciphers to do this yourself. $\endgroup$ – eckes Aug 22 '16 at 18:42
5
$\begingroup$

Symmetric keys don't need to be in any particular format -- they're just a sequence of (pseudo)random bits.

Most programming environments provide some sort of "secure random" mechanism (a CSPRNG). You can use this to acquire a byte array of the appropriate length (e.g. 32 bytes for AES256), which can be used as a key. Be sure to pass in the raw bytes, and not, e.g., a hex-encoded string.

Alternatively, you may want to derive a key from some other source. A Key Derivation Function (KDF) is a function that transforms some input into a key. GPG uses a Password-Based KDF (PBKDF, also known as a "password hash") to transform a password into a symmetric key.

Many PBKDFs use an iterated hashing approach; you might be interested in reviewing the design of "PBKDF2", or see scrypt for a more modern design. Generally though, as a developer, you can use an existing implementation and not be particularly concerned about the underlying details.

For completeness, there are also Key-Based KDFs such as HKDF that can derive symmetric keys from other keys, but I suspect that's not what you're after.

$\endgroup$
  • $\begingroup$ There are many ciphers which have restrictions on weak keys. I would not blindly use a RNG if the programming environment actually has a function for key generation (or key derivation). In Java you would use the KeyGenerator.getInstance("Algo") method to get a matching generator. $\endgroup$ – eckes Aug 22 '16 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.