X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=pygost%2Fgost3413.py;h=f3cb5da82a5d0b2b6e5fa7444a0d95969c51ba7a;hb=4d035489662738d8b505a20d842cd07c512b9947;hp=ec16f8c6b0903f73d78a3e4ee2602722d6973bc8;hpb=5e92533267cb41c42af1243592c530304d18bff5;p=pygost.git diff --git a/pygost/gost3413.py b/pygost/gost3413.py index ec16f8c..f3cb5da 100644 --- a/pygost/gost3413.py +++ b/pygost/gost3413.py @@ -1,6 +1,6 @@ # coding: utf-8 # PyGOST -- Pure Python GOST cryptographic functions library -# Copyright (C) 2015-2020 Sergey Matveev +# Copyright (C) 2015-2023 Sergey Matveev # # 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 @@ -13,11 +13,13 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" GOST R 34.13-2015: Modes of operation for block ciphers +"""GOST R 34.13-2015: Modes of operation for block ciphers 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]