* 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)
@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.
PyGOST is free software: see the file COPYING for copying conditions.
"""
-__version__ = "5.9"
+__version__ = "5.10"
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
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]
bs: int,
data: bytes,
) -> bytes: ...
+
+
+def pad_iso10126(data: bytes, blocksize: int) -> bytes: ...
+
+
+def unpad_iso10126(data: bytes, blocksize: int) -> bytes: ...
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
),
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)
(@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)