]> Cypherpunks.ru repositories - pygost.git/commitdiff
(un)pad_iso10126 5.10
authorSergey Matveev <stargrave@stargrave.org>
Fri, 4 Feb 2022 15:17:16 +0000 (18:17 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 4 Feb 2022 15:17:16 +0000 (18:17 +0300)
README
news.texi
pygost/__init__.py
pygost/gost3413.py
pygost/stubs/pygost/gost3413.pyi
pygost/test_gost3413.py
www.texi

diff --git a/README b/README
index 97ee0ed1f1f305f949ae17442e78cb8c7c5b0735..203471549796813796e758a1cd8dc413bd168449 100644 (file)
--- a/README
+++ b/README
@@ -22,7 +22,7 @@ GOST is GOvernment STandard of Russian Federation (and Soviet Union).
 * GOST R 34.12-2015 128-bit block cipher Кузнечик (Kuznechik) (RFC 7801)
 * GOST R 34.12-2015 64-bit block cipher Магма (Magma)
 * GOST R 34.13-2015 padding methods and block cipher modes of operation
-  (ECB, CTR, OFB, CBC, CFB, MAC)
+  (ECB, CTR, OFB, CBC, CFB, MAC), ISO 10126 padding
 * MGM AEAD mode for 64 and 128 bit ciphers (RFC 9058)
 * CTR-ACPKM, OMAC-ACPKM-Master modes of operation (Р 1323565.1.017-2018)
 * KExp15/KImp15 key export/import functions (Р 1323565.1.017-2018)
index 96db53f11f537d2e922b66a408a2a9f270265ba8..eeacd5567185920cfd590053c128c2079471b2e4 100644 (file)
--- a/news.texi
+++ b/news.texi
@@ -3,6 +3,10 @@
 
 @table @strong
 
+@anchor{Release 5.10}
+@item 5.10
+Added ISO 10126 @code{pygost.gost3413.(un)pad_iso10126} padding support.
+
 @anchor{Release 5.9}
 @item 5.9
 Fixed @code{wrap.wrap_cryptopro}, that ignored Sbox for key diversification.
index 7800ed42a565f97a80b1a7af83038733485b62cf..38d8dbe3c7a5a5c4d29dec94e850ce514a819166 100644 (file)
@@ -3,4 +3,4 @@
 PyGOST is free software: see the file COPYING for copying conditions.
 """
 
-__version__ = "5.9"
+__version__ = "5.10"
index 93244da09d720a08d2f6ec573425e1b918f3f82d..27f0c30dc79a75de4e66f8e284455221474fa215 100644 (file)
@@ -18,6 +18,8 @@
 This module currently includes only padding methods.
 """
 
+from os import urandom
+
 from pygost.utils import bytes2long
 from pygost.utils import long2bytes
 from pygost.utils import strxor
@@ -365,3 +367,26 @@ def mac_acpkm_master(algo_class, encrypter, key_section_size, section_size, bs,
         strxor(pad3(tail, bs), prev),
         k1 if len(tail) == bs else k2,
     ))
+
+
+def pad_iso10126(data, blocksize):
+    """ISO 10126 padding
+
+    Does not exist in 34.13, but added for convenience.
+    It uses urandom call for getting the randomness.
+    """
+    pad_len = blocksize - len(data) % blocksize
+    if pad_len == 0:
+        pad_len = blocksize
+    return b"".join((data, urandom(pad_len - 1), bytes((pad_len,))))
+
+
+def unpad_iso10126(data, blocksize):
+    """Unpad :py:func:`pygost.gost3413.pad_iso10126`
+    """
+    if len(data) % blocksize != 0:
+        raise ValueError("Data length is not multiple of blocksize")
+    pad_len = bytearray(data)[-1]
+    if pad_len > blocksize:
+        raise ValueError("Padding length is bigger than blocksize")
+    return data[:-pad_len]
index d9d9c96b462f8f893888167f4cc5318bae263e4a..4cfd694076d3d06e184ec33f937a4cf2d6c94f31 100644 (file)
@@ -73,3 +73,9 @@ def mac_acpkm_master(
         bs: int,
         data: bytes,
 ) -> bytes: ...
+
+
+def pad_iso10126(data: bytes, blocksize: int) -> bytes: ...
+
+
+def unpad_iso10126(data: bytes, blocksize: int) -> bytes: ...
index 2be55956d2f10939e709480867cc1d8396f269a7..159f7fe9c02dc0e8d8ecaa4c5491bdba7e535684 100644 (file)
@@ -36,7 +36,9 @@ from pygost.gost3413 import mac
 from pygost.gost3413 import mac_acpkm_master
 from pygost.gost3413 import ofb
 from pygost.gost3413 import pad2
+from pygost.gost3413 import pad_iso10126
 from pygost.gost3413 import unpad2
+from pygost.gost3413 import unpad_iso10126
 from pygost.utils import hexdec
 from pygost.utils import hexenc
 from pygost.utils import strxor
@@ -747,3 +749,18 @@ A8 1C 79 A0 4F 29 66 0E A3 FD A8 74 C6 30 79 9E
             ),
             hexdec("FBB8DCEE45BEA67C35F58C5700898E5D"),
         )
+
+
+class ISO10126Test(TestCase):
+    def test_symmetric(self):
+        for _ in range(100):
+            for blocksize in (GOST3412Magma.blocksize, GOST3412Kuznechik.blocksize):
+                data = urandom(randint(0, blocksize * 3))
+                padded = pad_iso10126(data, blocksize)
+                self.assertSequenceEqual(unpad_iso10126(padded, blocksize), data)
+                with self.assertRaises(ValueError):
+                    unpad_iso10126(padded[1:], blocksize)
+
+    def test_small(self):
+        with self.assertRaises(ValueError):
+            unpad_iso10126(b"foobar\x00\x09", 8)
index a9a210206f80ea56752c3d563aca9456b995e6d4..329a227fc1a393926c35e5efc99b68b545c78da2 100644 (file)
--- a/www.texi
+++ b/www.texi
@@ -54,7 +54,7 @@ Currently supported algorithms are:
     (@url{https://tools.ietf.org/html/rfc7801.html, RFC 7801})
 @item GOST R 34.12-2015 64-bit block cipher Магма (Magma)
 @item GOST R 34.13-2015 padding methods and block cipher modes of operation
-      (ECB, CTR, OFB, CBC, CFB, MAC)
+      (ECB, CTR, OFB, CBC, CFB, MAC), ISO 10126 padding
 @item MGM AEAD mode for 64 and 128 bit ciphers
     (@url{https://tools.ietf.org/html/rfc9058.html, RFC 9058})
 @item CTR-ACPKM, OMAC-ACPKM-Master modes of operation (Р 1323565.1.017-2018)