include header version info in scrypt handler
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 22 May 2017 12:10:33 +0000 (14:10 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:09 +0000 (13:34 +0200)
deltatar/crypto.py

index 478e279..f8573db 100755 (executable)
@@ -576,6 +576,14 @@ def scrypt_hashsource (pw, ins):
 
     This does not validate whether the first object is encrypted correctly.
     """
+    if isinstance (pw, str) is True:
+        pw = str.encode (pw)
+    elif isinstance (pw, bytes) is False:
+        raise InvalidParameter ("password must be a string, not %s"
+                                % type (password))
+    if isinstance (ins, io.BufferedReader) is False and \
+            isinstance (ins, io.FileIO) is False:
+        raise InvalidParameter ("file to hash must be opened in “binary” mode")
     hdr = None
     try:
         hdr = hdr_read_stream (ins)
@@ -603,7 +611,7 @@ def scrypt_hashsource (pw, ins):
 
     hsh, _void = kdf (pw, nacl)
 
-    return hsh, nacl, params
+    return hsh, nacl, hdr ["version"], pver
 
 
 def scrypt_hashfile (pw, fname):
@@ -613,7 +621,7 @@ def scrypt_hashfile (pw, fname):
     offset zero.
     """
     with deptdcrypt_mk_stream (PDTCRYPT_SOURCE, fname  or "-") as ins:
-        hsh, _void, _void = scrypt_hashsource (pw, ins)
+        hsh, _void, _void, _void = scrypt_hashsource (pw, ins)
         return hsh
 
 
@@ -1482,11 +1490,14 @@ def mode_depdtcrypt (mode, secret, ins, outs):
 
 
 def mode_scrypt (pw, ins):
-    hsh, nacl, params = scrypt_hashsource (pw, ins)
+    hsh, nacl, version, paramversion = scrypt_hashsource (pw, ins)
+    defs = ENCRYPTION_PARAMETERS.get(paramversion, None)
+    _kdfname, params = defs ["kdf"]
 
     import json
     out = json.dumps ({ "salt"          : binascii.hexlify (nacl).decode ()
                       , "hash"          : binascii.hexlify (hsh).decode ()
+                      , "version"       : version
                       , "scrypt_params" : { "N"     : params ["N"]
                                           , "r"     : params ["r"]
                                           , "p"     : params ["p"]