# coding: utf-8
# PyGOST -- Pure Python GOST cryptographic functions library
-# Copyright (C) 2015-2017 Sergey Matveev <stargrave@stargrave.org>
+# Copyright (C) 2015-2020 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.
+# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
def ecb_encrypt(encrypter, bs, pt):
"""ECB encryption mode of operation
- :param encrypter: Encrypting function, that takes block as an input
+ :param encrypter: encrypting function, that takes block as an input
:param int bs: cipher's blocksize
:param bytes pt: already padded plaintext
"""
def ctr(encrypter, bs, data, iv):
"""Counter mode of operation
- :param encrypter: Encrypting function, that takes block as an input
+ :param encrypter: encrypting function, that takes block as an input
:param int bs: cipher's blocksize
:param bytes data: plaintext/ciphertext
:param bytes iv: half blocksize-sized initialization vector
def ofb(encrypter, bs, data, iv):
"""OFB mode of operation
- :param encrypter: Encrypting function, that takes block as an input
+ :param encrypter: encrypting function, that takes block as an input
:param int bs: cipher's blocksize
:param bytes data: plaintext/ciphertext
- :param bytes iv: double blocksize-sized initialization vector
+ :param bytes iv: blocksize-sized initialization vector
For decryption you use the same function again.
"""
- if len(iv) < 2 * bs or len(iv) % bs != 0:
+ if len(iv) < bs or len(iv) % bs != 0:
raise ValueError("Invalid IV size")
r = [iv[i:i + bs] for i in range(0, len(iv), bs)]
result = []
def cbc_encrypt(encrypter, bs, pt, iv):
"""CBC encryption mode of operation
- :param encrypter: Encrypting function, that takes block as an input
+ :param encrypter: encrypting function, that takes block as an input
:param int bs: cipher's blocksize
:param bytes pt: already padded plaintext
- :param bytes iv: double blocksize-sized initialization vector
+ :param bytes iv: blocksize-sized initialization vector
"""
if not pt or len(pt) % bs != 0:
raise ValueError("Plaintext is not blocksize aligned")
- if len(iv) < 2 * bs or len(iv) % bs != 0:
+ if len(iv) < bs or len(iv) % bs != 0:
raise ValueError("Invalid IV size")
r = [iv[i:i + bs] for i in range(0, len(iv), bs)]
ct = []
:param decrypter: Decrypting function, that takes block as an input
:param int bs: cipher's blocksize
:param bytes ct: ciphertext
- :param bytes iv: double blocksize-sized initialization vector
+ :param bytes iv: blocksize-sized initialization vector
"""
if not ct or len(ct) % bs != 0:
raise ValueError("Ciphertext is not blocksize aligned")
- if len(iv) < 2 * bs or len(iv) % bs != 0:
+ if len(iv) < bs or len(iv) % bs != 0:
raise ValueError("Invalid IV size")
r = [iv[i:i + bs] for i in range(0, len(iv), bs)]
pt = []
def cfb_encrypt(encrypter, bs, pt, iv):
"""CFB encryption mode of operation
- :param encrypter: Encrypting function, that takes block as an input
+ :param encrypter: encrypting function, that takes block as an input
:param int bs: cipher's blocksize
:param bytes pt: plaintext
- :param bytes iv: double blocksize-sized initialization vector
+ :param bytes iv: blocksize-sized initialization vector
"""
- if len(iv) < 2 * bs or len(iv) % bs != 0:
+ if len(iv) < bs or len(iv) % bs != 0:
raise ValueError("Invalid IV size")
r = [iv[i:i + bs] for i in range(0, len(iv), bs)]
ct = []
def cfb_decrypt(encrypter, bs, ct, iv):
"""CFB decryption mode of operation
- :param encrypter: Encrypting function, that takes block as an input
+ :param encrypter: encrypting function, that takes block as an input
:param int bs: cipher's blocksize
:param bytes ct: ciphertext
- :param bytes iv: double blocksize-sized initialization vector
+ :param bytes iv: blocksize-sized initialization vector
"""
- if len(iv) < 2 * bs or len(iv) % bs != 0:
+ if len(iv) < bs or len(iv) % bs != 0:
raise ValueError("Invalid IV size")
r = [iv[i:i + bs] for i in range(0, len(iv), bs)]
pt = []
def mac(encrypter, bs, data):
"""MAC (known here as CMAC, OMAC1) mode of operation
- :param encrypter: Encrypting function, that takes block as an input
+ :param encrypter: encrypting function, that takes block as an input
:param int bs: cipher's blocksize
:param bytes data: data to authenticate