]> Cypherpunks.ru repositories - pygost.git/blobdiff - pygost/gost34112012.py
Fixed incorrect digest calculation
[pygost.git] / pygost / gost34112012.py
index f1a7acc84a9dc9f1e28377d2ad46548136b3f168..91782de66365658f6212ee4392e58205fec8b3d1 100644 (file)
@@ -1,6 +1,6 @@
 # coding: utf-8
 # PyGOST -- Pure Python GOST cryptographic functions library
-# Copyright (C) 2015-2022 Sergey Matveev <stargrave@stargrave.org>
+# Copyright (C) 2015-2023 Sergey Matveev <stargrave@stargrave.org>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -184,16 +184,10 @@ LCache = _lcache()
 
 
 def add512bit(a, b):
-    """Add two 512 integers
-    """
-    a = bytearray(a)
-    b = bytearray(b)
-    cb = 0
-    res = bytearray(64)
-    for i in range(64):
-        cb = a[i] + b[i] + (cb >> 8)
-        res[i] = cb & 0xff
-    return res
+    a = int.from_bytes(a, "little")
+    b = int.from_bytes(b, "little")
+    r = (a + b) % (1 << 512)
+    return r.to_bytes(512 // 8, "little")
 
 
 def g(n, hsh, msg):
@@ -274,8 +268,9 @@ class GOST34112012(PEP247):
         """Update state with the new data
         """
         if len(self.buf) > 0:
-            self.buf += data[:BLOCKSIZE - len(self.buf)]
-            data = data[BLOCKSIZE - len(self.buf):]
+            chunk_len = BLOCKSIZE - len(self.buf)
+            self.buf += data[:chunk_len]
+            data = data[chunk_len:]
             if len(self.buf) == BLOCKSIZE:
                 self._update_block(self.buf)
                 self.buf = b""