Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

I've been reading through the PGP Standard and here I'm a little confused. This section is discussing converting string data to a session key. I'm confused about the paragraph in bold. First off, what does it mean by "hash context"? Secondly, which "data" is it referring to? Is that paragraph still talking about what to do if the hash is too small?

Simple S2K

This directly hashes the string to produce the key data. See below for how this hashing is done.

   Octet 0:        0x00
   Octet 1:        hash algorithm

Simple S2K hashes the passphrase to produce the session key. The
manner in which this is done depends on the size of the session key
(which will depend on the cipher used) and the size of the hash algorithm's output.

If the hash size is greater than the session key size, the high-order (leftmost) octets of the hash are used as the key.

If the hash size is less than the key size, multiple instances of the hash context are created -- enough to produce the required key data. These instances are preloaded with 0, 1, 2, ... octets of zeros (that is to say, the first instance has no preloading, the second gets preloaded with 1 octet of zero, the third is preloaded with two octets of zeros, and so forth).

As the data is hashed, it is given independently to each hash context. Since the contexts have been initialized differently, they will each produce different hash output. Once the passphrase is hashed, the output data from the multiple hashes is concatenated, first hash leftmost, to produce the key data, with any excess octets on the right discarded.

share|improve this question

2 Answers 2

Yes, it's still talking about the case where multiple hashes are needed to get to the key size. It explains that they are taken independently (different contexts) over the passphrase (the data).

For example, with a 160-bit hash (e.g. SHA-1) and a 256-bit key, you would concatenate $H(p)||H(0x00||p)$, then take the leftmost 256 bits (i.e. discard the rightmost 8 octets).

If you needed one more hash block, that would be $H(0x00||0x00||p)$, etc..

share|improve this answer

Not really disagreeing but to be more specific about context:

In general, crypto hashes (and other hashes for that matter) and symmetric ciphers sometimes need to handle data too large to conveniently put in a single buffer, or fit in memory (especially on small or embedded systems). Thus they are often coded in a way where one routine (function, method, etc) call sets up the initial state, another processes a chunk of data and can be done more than once if there are multiple chunks of data, and a "final" or "done" or "complete" routine produces the result (for hash) or completes and/or verifies the data (padding for block cipher modes, authentication tag for authenticated cipher modes, perhaps others). The internal state that needs to be carried from one routine call to the next is called the context.

As an example, in OpenSSL to MD5 some data you can:

  • declare a variable of type MD5_CTX (or malloc space of that size)
  • call MD5_Init with the address of the context
  • call MD5_Update with the address of the context, and the address and length of some data
  • if there is more than one buffer of data, call MD5_Update again as needed
  • call MD5_Final with the address of the context, and the address and length where the hash value is stored
  • "release" (or free) the context space

For S2K the data being hashed is small enough it's easy to just compute each hash input, and output, directly as in @otus answer. The description in RFC4880 allows for the case where you "start" all the needed hashes, and then run the (passphrase) data into all of them, and then concatenate the outputs. Here that's overkill, but since the purpose of RFCs is to allow different implementations to interoperate, they are careful not to overspecify implementation.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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