summaryrefslogtreecommitdiff
path: root/pygost/gost3413.py
diff options
context:
space:
mode:
authorSergey Matveev <stargrave@stargrave.org>2016-10-04 21:34:48 +0300
committerSergey Matveev <stargrave@stargrave.org>2016-10-04 21:46:09 +0300
commit34c9c9a4f95eecfee75fc36d75d2ee45d1054a25 (patch)
tree1629a5127f7e33765b5566a15b2e56fbc539b13c /pygost/gost3413.py
downloadpygost-34c9c9a4f95eecfee75fc36d75d2ee45d1054a25.tar.xz
2.3 release is ready
Diffstat (limited to 'pygost/gost3413.py')
-rw-r--r--pygost/gost3413.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/pygost/gost3413.py b/pygost/gost3413.py
new file mode 100644
index 0000000..a31a3c2
--- /dev/null
+++ b/pygost/gost3413.py
@@ -0,0 +1,54 @@
+# coding: utf-8
+# PyGOST -- Pure Python GOST cryptographic functions library
+# Copyright (C) 2015-2016 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
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+""" GOST R 34.13-2015: Modes of operation for block ciphers
+
+This module currently includes only padding methods.
+"""
+
+
+def pad_size(data_size, blocksize):
+ """Calculate required pad size to full up BLOCKSIZE
+ """
+ if data_size < blocksize:
+ return blocksize - data_size
+ if data_size % blocksize == 0:
+ return 0
+ return blocksize - data_size % blocksize
+
+
+def pad1(data, blocksize):
+ """Padding method 1
+
+ Just fill up with zeros if necessary.
+ """
+ return data + b'\x00' * pad_size(len(data), blocksize)
+
+
+def pad2(data, blocksize):
+ """Padding method 2 (also known as ISO/IEC 7816-4)
+
+ Add one bit and then fill up with zeros.
+ """
+ return data + b'\x80' + b'\x00' * pad_size(len(data) + 1, blocksize)
+
+
+def pad3(data, blocksize):
+ """Padding method 3
+ """
+ if pad_size(len(data), blocksize) == 0:
+ return data
+ return pad2(data, blocksize)