:param s: S-box
:param _in: 32-bit word
- :return: substituted 32-bit word
+ :returns: substituted 32-bit word
"""
return (
(s[0][(_in >> 0) & 0x0F] << 0) +
:param bytes key: 256-bit encryption key
:param ns: N1 and N2 integers
:type ns: (int, int)
- :return: resulting N1 and N2
+ :returns: resulting N1 and N2
:rtype: (int, int)
"""
s = SBOXES[sbox]
:param bytes key: encryption key
:param data: plaintext
:type data: bytes, multiple of BLOCKSIZE
- :param func action: encrypt/decrypt
+ :param func action: "encrypt"/"decrypt"
:param sbox: S-box parameters to use
:type sbox: str, SBOXES'es key
- :return: ciphertext
+ :returns: ciphertext
:rtype: bytes
"""
validate_key(key)
:type bool pad: perform ISO/IEC 7816-4 padding
:param sbox: S-box parameters to use
:type sbox: str, SBOXES'es key
- :return: ciphertext
+ :returns: ciphertext
:rtype: bytes
34.13-2015 padding method 2 is used.
:type bool pad: perform ISO/IEC 7816-4 unpadding after decryption
:param sbox: S-box parameters to use
:type sbox: str, SBOXES'es key
- :return: plaintext
+ :returns: plaintext
:rtype: bytes
"""
validate_key(key)
:type iv: bytes, BLOCKSIZE length
:param sbox: S-box parameters to use
:type sbox: str, SBOXES'es key
- :return: ciphertext
+ :returns: ciphertext
:rtype: bytes
For decryption you use the same function again.
:param sbox: S-box parameters to use
:type sbox: str, SBOXES'es key
:param bool mesh: enable key meshing
- :return: ciphertext
+ :returns: ciphertext
:rtype: bytes
"""
validate_key(key)
:param sbox: S-box parameters to use
:type sbox: str, SBOXES'es key
:param bool mesh: enable key meshing
- :return: ciphertext
+ :returns: ciphertext
:rtype: bytes
"""
validate_key(key)
:param GOST3410Curve curve: curve to use
:param long prv: private key
- :return: public key's parts, X and Y
+ :returns: public key's parts, X and Y
:rtype: (long, long)
"""
return curve.exp(prv)
:param long prv: private key
:param digest: digest for signing
:type digest: bytes, 32 or 64 bytes
- :return: signature
+ :returns: signature
:rtype: bytes, 64 or 128 bytes
"""
size = MODE2SIZE[mode]
:param GOST3410Curve curve: curve to use
:param long prv: private key
+ :param pubkey: public key
+ :type pubkey: (long, long)
:param ukm: UKM value (VKO-factor)
:type ukm: bytes, 8 bytes
- :param pubkey: public key's part
- :type pubkey: (long, long)
- :return: Key Encryption Key (shared key)
+ :returns: Key Encryption Key (shared key)
:rtype: bytes, 32 bytes
Shared Key Encryption Key computation is based on
:param GOST3410Curve curve: curve to use
:param long prv: private key
+ :param pubkey: public key
+ :type pubkey: (long, long)
:param ukm: UKM value (VKO-factor)
:type ukm: bytes, 8 bytes
- :param pubkey: public key's part
- :type pubkey: (long, long)
- :return: Key Encryption Key (shared key)
+ :returns: Key Encryption Key (shared key)
:rtype: bytes, 32 bytes
"""
key = curve.exp(prv, pubkey[0], pubkey[1])
:param GOST3410Curve curve: curve to use
:param long prv: private key
+ :param pubkey: public key
+ :type pubkey: (long, long)
:param ukm: UKM value (VKO-factor)
:type ukm: bytes, 8 bytes
- :param pubkey: public key's part
- :type pubkey: (long, long)
- :return: Key Encryption Key (shared key)
+ :returns: Key Encryption Key (shared key)
:rtype: bytes, 32 bytes
"""
key = curve.exp(prv, pubkey[0], pubkey[1])
"""GOST 34.12-2015 128-bit block cipher Кузнечик (Kuznechik)
:rfc:`7801`. Pay attention that 34.12-2015 also defines 64-bit block
-cipher Магма (Magma) -- it is **not** implemented here.
+cipher Магма (Magma) -- it is **not** implemented here, but in gost28147
+module.
Several precalculations are performed during this module importing.
"""
""" Deserialize big-endian bytes into long number
:param bytes raw: binary string
- :return: deserialized long number
+ :returns: deserialized long number
:rtype: int
"""
return int(hexenc(raw), 16)
""" Serialize long number into big-endian bytestring
:param long n: long number
- :return: serialized bytestring
+ :returns: serialized bytestring
:rtype: bytes
"""
res = hex(int(n))[2:].rstrip("L")
def modinvert(a, n):
""" Modular multiplicative inverse
- :return: inverse number. -1 if it does not exist
+ :returns: inverse number. -1 if it does not exist
Realization is taken from:
https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
:type kek: bytes, 32 bytes
:param cek: content encryption key
:type cek: bytes, 32 bytes
- :return: wrapped key
+ :returns: wrapped key
:rtype: bytes, 44 bytes
"""
cek_mac = MAC(kek, data=cek, iv=ukm).digest()[:4]
:type kek: bytes, 32 bytes
:param data: wrapped key
:type data: bytes, 44 bytes
- :return: unwrapped CEK
+ :returns: unwrapped CEK
:rtype: 32 bytes
"""
if len(data) != 44:
:type kek: bytes, 32 bytes
:param cek: content encryption key
:type cek: bytes, 32 bytes
- :return: wrapped key
+ :returns: wrapped key
:rtype: bytes, 44 bytes
"""
return wrap_gost(ukm, diversify(kek, bytearray(ukm)), cek)
:type kek: bytes, 32 bytes
:param data: wrapped key
:type data: bytes, 44 bytes
- :return: unwrapped CEK
+ :returns: unwrapped CEK
:rtype: 32 bytes
"""
if len(data) < 8: