X-Git-Url: http://www.git.cypherpunks.ru/?p=pygost.git;a=blobdiff_plain;f=pygost%2Fgost3413.py;h=27f0c30dc79a75de4e66f8e284455221474fa215;hp=93244da09d720a08d2f6ec573425e1b918f3f82d;hb=a9f7e3dfc59a987c3d6cce4108f18d9f6b72867b;hpb=9e1dd476aac48ba781c7c7d99f1ec42c8a0c6034 diff --git a/pygost/gost3413.py b/pygost/gost3413.py index 93244da..27f0c30 100644 --- a/pygost/gost3413.py +++ b/pygost/gost3413.py @@ -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]