Add salted string-to-key support to PGPPacket._string_to_key
authorW. Trevor King <wking@tremily.us>
Sat, 21 Dec 2013 18:30:44 +0000 (10:30 -0800)
committerW. Trevor King <wking@tremily.us>
Mon, 23 Dec 2013 21:41:05 +0000 (13:41 -0800)
From RFC 4880 [1]:

  Salted S2K is exactly like Simple S2K, except that the input to the
  hash function(s) consists of the 8 octets of salt from the S2K
  specifier, followed by the passphrase.

[1]: http://tools.ietf.org/search/rfc4880#section-3.7.1.2

gpg-migrate.py

index 0bf1133aca14eb87564fad122b5dccde65cac768..c683a5b7506ac56feec4dfd0c76fc194f9c5a1e7 100755 (executable)
@@ -861,6 +861,8 @@ class PGPPacket (dict):
         key = b''
         if self['string-to-key-type'] == 'simple':
             update_bytes = string
+        elif self['string-to-key-type'] == 'salted':
+            update_bytes = self['string-to-key-salt'] + string
         else:
             raise NotImplementedError(
                 'key calculation for string-to-key type {}'.format(
@@ -868,7 +870,10 @@ class PGPPacket (dict):
         for padding in range(hashes):
             string_hash = _hashlib.new(hash_name)
             string_hash.update(padding * b'\x00')
-            if self['string-to-key-type'] == 'simple':
+            if self['string-to-key-type'] in [
+                    'simple',
+                    'salted',
+                    ]:
                 string_hash.update(update_bytes)
             key += string_hash.digest()
         key = key[:key_size_bytes]