summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Matveev <stargrave@stargrave.org>2018-12-02 20:05:30 +0300
committerSergey Matveev <stargrave@stargrave.org>2018-12-09 13:01:32 +0300
commite8a28983794d026ccb59439a684dbdc363fcc8c6 (patch)
tree5b2cfb3223a077d66213f91e1ca7e82759d2d36f
parentb29e2bff6538edcca8285cb880f8dca45ab85c2b (diff)
downloadpygost-e8a28983794d026ccb59439a684dbdc363fcc8c6.tar.xz
pygost.wrap.* Sbox specifying
-rw-r--r--NEWS4
-rw-r--r--VERSION2
-rw-r--r--news.texi7
-rw-r--r--pygost/Makefile19
-rw-r--r--pygost/test_cms_enveloped.py302
-rw-r--r--pygost/wrap.py29
6 files changed, 341 insertions, 22 deletions
diff --git a/NEWS b/NEWS
index 5726ed8..e2e8679 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+3.13:
+ * Ability to explicitly specify used 28147-89 Sbox in
+ pygost.wrap.* functions
+
3.12:
* Added mode argument to pygost.gost3410_vko.kek_34102012256,
because 256-bit private keys can be used with that algorithm too
diff --git a/VERSION b/VERSION
index e4fba21..24ee5b1 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.12
+3.13
diff --git a/news.texi b/news.texi
index a0ca9d1..7841bef 100644
--- a/news.texi
+++ b/news.texi
@@ -3,6 +3,13 @@
@table @strong
+@anchor{Release 3.13}
+@item 3.13
+ @itemize
+ @item Ability to explicitly specify used 28147-89 Sbox in
+ @code{pygost.wrap.*} functions
+ @end itemize
+
@anchor{Release 3.12}
@item 3.12
@itemize
diff --git a/pygost/Makefile b/pygost/Makefile
index 35eda67..33ebd2a 100644
--- a/pygost/Makefile
+++ b/pygost/Makefile
@@ -1,12 +1,13 @@
PYTHON ?= python
test:
- PYTHONPATH=.. $(PYTHON) -m unittest test_gost28147
- PYTHONPATH=.. $(PYTHON) -m unittest test_gost28147_mac
- PYTHONPATH=.. $(PYTHON) -m unittest test_gost341194
- PYTHONPATH=.. $(PYTHON) -m unittest test_gost34112012
- PYTHONPATH=.. $(PYTHON) -m unittest test_gost3410
- PYTHONPATH=.. $(PYTHON) -m unittest test_gost3410_vko
- PYTHONPATH=.. $(PYTHON) -m unittest test_wrap
- PYTHONPATH=.. $(PYTHON) -m unittest test_gost3412
- PYTHONPATH=.. $(PYTHON) -m unittest test_gost3413
+ PYTHONPATH=$(PYTHONPATH):.. $(PYTHON) -m unittest test_gost28147
+ PYTHONPATH=$(PYTHONPATH):.. $(PYTHON) -m unittest test_gost28147_mac
+ PYTHONPATH=$(PYTHONPATH):.. $(PYTHON) -m unittest test_gost341194
+ PYTHONPATH=$(PYTHONPATH):.. $(PYTHON) -m unittest test_gost34112012
+ PYTHONPATH=$(PYTHONPATH):.. $(PYTHON) -m unittest test_gost3410
+ PYTHONPATH=$(PYTHONPATH):.. $(PYTHON) -m unittest test_gost3410_vko
+ PYTHONPATH=$(PYTHONPATH):.. $(PYTHON) -m unittest test_wrap
+ PYTHONPATH=$(PYTHONPATH):.. $(PYTHON) -m unittest test_gost3412
+ PYTHONPATH=$(PYTHONPATH):.. $(PYTHON) -m unittest test_gost3413
+ PYTHONPATH=$(PYTHONPATH):.. $(PYTHON) -m unittest test_cms_enveloped
diff --git a/pygost/test_cms_enveloped.py b/pygost/test_cms_enveloped.py
new file mode 100644
index 0000000..b257798
--- /dev/null
+++ b/pygost/test_cms_enveloped.py
@@ -0,0 +1,302 @@
+# coding: utf-8
+
+from base64 import b64decode
+from unittest import skipIf
+from unittest import TestCase
+
+from pygost.gost28147 import cfb_decrypt
+from pygost.gost3410 import CURVE_PARAMS
+from pygost.gost3410 import GOST3410Curve
+from pygost.gost3410 import prv_unmarshal
+from pygost.gost3410 import pub_unmarshal
+from pygost.gost3410_vko import kek_34102012256
+from pygost.gost3410_vko import ukm_unmarshal
+from pygost.utils import hexdec
+from pygost.wrap import unwrap_cryptopro
+
+try:
+
+ from pyderasn import Any
+ from pyderasn import BitString
+ from pyderasn import Choice
+ from pyderasn import Integer
+ from pyderasn import ObjectIdentifier
+ from pyderasn import OctetString
+ from pyderasn import Sequence
+ from pyderasn import SetOf
+ from pyderasn import tag_ctxc
+ from pyderasn import tag_ctxp
+
+ class CMSVersion(Integer):
+ pass
+
+ class ContentType(ObjectIdentifier):
+ pass
+
+ class RecipientIdentifier(Choice):
+ schema = (
+ ("issuerAndSerialNumber", Any()),
+ # ("subjectKeyIdentifier", SubjectKeyIdentifier(impl=tag_ctxp(0))),
+ )
+
+ class AlgorithmIdentifier(Sequence):
+ schema = (
+ ("algorithm", ObjectIdentifier()),
+ ("parameters", Any(optional=True)),
+ )
+
+ class KeyEncryptionAlgorithmIdentifier(AlgorithmIdentifier):
+ pass
+
+ class EncryptedKey(OctetString):
+ pass
+
+ class KeyTransRecipientInfo(Sequence):
+ schema = (
+ ("version", CMSVersion()),
+ ("rid", RecipientIdentifier()),
+ ("keyEncryptionAlgorithm", KeyEncryptionAlgorithmIdentifier()),
+ ("encryptedKey", EncryptedKey()),
+ )
+
+ class RecipientInfo(Choice):
+ schema = (
+ ("ktri", KeyTransRecipientInfo()),
+ # ("kari", KeyAgreeRecipientInfo(impl=tag_ctxc(1))),
+ # ("kekri", KEKRecipientInfo(impl=tag_ctxc(2))),
+ # ("pwri", PasswordRecipientInfo(impl=tag_ctxc(3))),
+ # ("ori", OtherRecipientInfo(impl=tag_ctxc(4))),
+ )
+
+ class RecipientInfos(SetOf):
+ schema = RecipientInfo()
+ bounds = (1, float("+inf"))
+
+ class ContentEncryptionAlgorithmIdentifier(AlgorithmIdentifier):
+ pass
+
+ class EncryptedContent(OctetString):
+ pass
+
+ class EncryptedContentInfo(Sequence):
+ schema = (
+ ("contentType", ContentType()),
+ ("contentEncryptionAlgorithm", ContentEncryptionAlgorithmIdentifier()),
+ ("encryptedContent", EncryptedContent(impl=tag_ctxp(0), optional=True)),
+ )
+
+ class EnvelopedData(Sequence):
+ schema = (
+ ("version", CMSVersion()),
+ # ("originatorInfo", OriginatorInfo(impl=tag_ctxc(0), optional=True)),
+ ("recipientInfos", RecipientInfos()),
+ ("encryptedContentInfo", EncryptedContentInfo()),
+ # ("unprotectedAttrs", UnprotectedAttributes(impl=tag_ctxc(1), optional=True)),
+ )
+
+ class ContentInfo(Sequence):
+ schema = (
+ ("contentType", ContentType()),
+ ("content", Any(expl=tag_ctxc(0))),
+ )
+
+ class Gost2814789IV(OctetString):
+ bounds = (8, 8)
+
+ class Gost2814789Parameters(Sequence):
+ schema = (
+ ("iv", Gost2814789IV()),
+ ("encryptionParamSet", ObjectIdentifier()),
+ )
+
+ class Gost2814789Key(OctetString):
+ bounds = (32, 32)
+
+ class Gost2814789MAC(OctetString):
+ bounds = (4, 4)
+
+ class Gost2814789EncryptedKey(Sequence):
+ schema = (
+ ("encryptedKey", Gost2814789Key()),
+ ("maskKey", Gost2814789Key(impl=tag_ctxp(0), optional=True)),
+ ("macKey", Gost2814789MAC()),
+ )
+
+ class SubjectPublicKeyInfo(Sequence):
+ schema = (
+ ("algorithm", AlgorithmIdentifier()),
+ ("subjectPublicKey", BitString()),
+ )
+
+ class GostR34102001TransportParameters(Sequence):
+ schema = (
+ ("encryptionParamSet", ObjectIdentifier()),
+ ("ephemeralPublicKey", SubjectPublicKeyInfo(
+ impl=tag_ctxc(0),
+ optional=True,
+ )),
+ ("ukm", OctetString()),
+ )
+
+ class GostR3410KeyTransport(Sequence):
+ schema = (
+ ("sessionEncryptedKey", Gost2814789EncryptedKey()),
+ ("transportParameters", GostR34102001TransportParameters(
+ impl=tag_ctxc(0),
+ optional=True,
+ )),
+ )
+
+except ImportError:
+ pyderasn_exists = False
+else:
+ pyderasn_exists = True
+
+
+@skipIf(not pyderasn_exists, "PyDERASN dependency is required")
+class TestCMSEnveloped(TestCase):
+ """KeyTransRecipientInfo-based test vectors from "Использование
+ алгоритмов ГОСТ 28147-89, ГОСТ Р 34.11 и ГОСТ Р 34.10 в
+ криптографических сообщениях формата CMS" (TK26CMS.pdf)
+ """
+
+ def process_cms(
+ self,
+ content_info_raw,
+ prv_key_our,
+ curve_name,
+ keker,
+ plaintext_expected,
+ ):
+ sbox = "Gost28147_tc26_ParamZ"
+ content_info, _ = ContentInfo().decode(content_info_raw)
+ enveloped_data, _ = EnvelopedData().decode(bytes(content_info["content"]))
+ eci = enveloped_data["encryptedContentInfo"]
+ ri = enveloped_data["recipientInfos"][0]
+ encrypted_key, _ = GostR3410KeyTransport().decode(
+ bytes(ri["ktri"]["encryptedKey"])
+ )
+ ukm = bytes(encrypted_key["transportParameters"]["ukm"])
+ spk = bytes(encrypted_key["transportParameters"]["ephemeralPublicKey"]["subjectPublicKey"])
+ pub_key_their, _ = OctetString().decode(spk)
+ curve = GOST3410Curve(*CURVE_PARAMS[curve_name])
+ kek = keker(curve, prv_key_our, bytes(pub_key_their), ukm)
+ key_wrapped = bytes(encrypted_key["sessionEncryptedKey"]["encryptedKey"])
+ mac = bytes(encrypted_key["sessionEncryptedKey"]["macKey"])
+ cek = unwrap_cryptopro(kek, ukm + key_wrapped + mac, sbox=sbox)
+ ciphertext = bytes(eci["encryptedContent"])
+ encryption_params, _ = Gost2814789Parameters().decode(
+ bytes(eci["contentEncryptionAlgorithm"]["parameters"])
+ )
+ iv = bytes(encryption_params["iv"])
+ self.assertSequenceEqual(
+ cfb_decrypt(cek, ciphertext, iv, sbox=sbox, mesh=True),
+ plaintext_expected,
+ )
+
+ def test_256(self):
+ content_info_raw = b64decode("""
+MIIKGgYJKoZIhvcNAQcDoIIKCzCCCgcCAQAxggE0MIIBMAIBADBbMFYxKTAnBgkq
+hkiG9w0BCQEWGkdvc3RSMzQxMC0yMDEyQGV4YW1wbGUuY29tMSkwJwYDVQQDEyBH
+b3N0UjM0MTAtMjAxMiAyNTYgYml0cyBleGNoYW5nZQIBATAfBggqhQMHAQEBATAT
+BgcqhQMCAiQABggqhQMHAQECAgSBrDCBqTAoBCCVJxUMdbKRzCJ5K1NWJIXnN7Ul
+zaceeFlblA2qH4wZrgQEsHnIG6B9BgkqhQMHAQIFAQGg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ToQ/i4vrtgA+eONuKT2uKZFikxA+VNmeeGdhkgqETMihQ==
+ """)
+ prv_key_our = hexdec("BFCF1D623E5CDD3032A7C6EABB4A923C46E43D640FFEAAF2C3ED39A8FA399924")[::-1]
+
+ def keker(curve, prv, pub, ukm):
+ return kek_34102012256(
+ curve,
+ prv_unmarshal(prv),
+ pub_unmarshal(pub),
+ ukm_unmarshal(ukm),
+ mode=2001,
+ )
+
+ self.process_cms(
+ content_info_raw,
+ prv_key_our,
+ "GostR3410_2001_CryptoPro_XchA_ParamSet",
+ keker,
+ b"Test data to encrypt.\n" * 100,
+ )
+
+ def test_512(self):
+ content_info_raw = b64decode("""
+MIIB0gYJKoZIhvcNAQcDoIIBwzCCAb8CAQAxggF8MIIBeAIBADBbMFYxKTAnBgkq
+hkiG9w0BCQEWGkdvc3RSMzQxMC0yMDEyQGV4YW1wbGUuY29tMSkwJwYDVQQDEyBH
+b3N0UjM0MTAtMjAxMiA1MTIgYml0cyBleGNoYW5nZQIBATAhBggqhQMHAQEBAjAV
+BgkqhQMHAQIBAgIGCCqFAwcBAQIDBIHyMIHvMCgEIIsYzbVLn33aLinQ7SLNA7y+
+Lrm02khqDCfXrNS9iiMhBATerS8zoIHCBgkqhQMHAQIFAQGggaowIQYIKoUDBwEB
+AQIwFQYJKoUDBwECAQICBggqhQMHAQECAwOBhAAEgYAYiTVLKpSGaAvjJEDQ0hdK
+qR/jek5Q9Q2pXC+NkOimQh7dpCi+wcaHlPcBk96hmpnOFvLaiokX8V6jqtBl5gdk
+M40kOXv8kcDdTzEVKA/ZLxA8xanL+gTD6ZjaPsUu06nsA2MoMBWcHLUzueaP3bGT
+/yHTV+Za5xdcQehag/lNBgQIvCw4uUl0XC4wOgYJKoZIhvcNAQcBMB8GBiqFAwIC
+FTAVBAj+1QzaXaN9FwYJKoUDBwECBQEBgAyK54euw0sHhEVEkA0=
+ """)
+ prv_key_our = hexdec("3FC01CDCD4EC5F972EB482774C41E66DB7F380528DFE9E67992BA05AEE462435757530E641077CE587B976C8EEB48C48FD33FD175F0C7DE6A44E014E6BCB074B")[::-1]
+
+ def keker(curve, prv, pub, ukm):
+ return kek_34102012256(
+ curve,
+ prv_unmarshal(prv),
+ pub_unmarshal(pub, mode=2012),
+ ukm_unmarshal(ukm),
+ )
+
+ self.process_cms(
+ content_info_raw,
+ prv_key_our,
+ "GostR3410_2012_TC26_ParamSetB",
+ keker,
+ b"Test message",
+ )
diff --git a/pygost/wrap.py b/pygost/wrap.py
index e7d7f7c..59f26bf 100644
--- a/pygost/wrap.py
+++ b/pygost/wrap.py
@@ -23,12 +23,13 @@ from struct import pack
from struct import unpack
from pygost.gost28147 import cfb_encrypt
+from pygost.gost28147 import DEFAULT_SBOX
from pygost.gost28147 import ecb_decrypt
from pygost.gost28147 import ecb_encrypt
from pygost.gost28147_mac import MAC
-def wrap_gost(ukm, kek, cek):
+def wrap_gost(ukm, kek, cek, sbox=DEFAULT_SBOX):
"""28147-89 key wrapping
:param ukm: UKM
@@ -40,12 +41,12 @@ def wrap_gost(ukm, kek, cek):
:returns: wrapped key
:rtype: bytes, 44 bytes
"""
- cek_mac = MAC(kek, data=cek, iv=ukm).digest()[:4]
- cek_enc = ecb_encrypt(kek, cek)
+ cek_mac = MAC(kek, data=cek, iv=ukm, sbox=sbox).digest()[:4]
+ cek_enc = ecb_encrypt(kek, cek, sbox=sbox)
return ukm + cek_enc + cek_mac
-def unwrap_gost(kek, data):
+def unwrap_gost(kek, data, sbox=DEFAULT_SBOX):
"""28147-89 key unwrapping
:param kek: key encryption key
@@ -58,13 +59,13 @@ def unwrap_gost(kek, data):
if len(data) != 44:
raise ValueError("Invalid data length")
ukm, cek_enc, cek_mac = data[:8], data[8:8 + 32], data[-4:]
- cek = ecb_decrypt(kek, cek_enc)
- if MAC(kek, data=cek, iv=ukm).digest()[:4] != cek_mac:
+ cek = ecb_decrypt(kek, cek_enc, sbox=sbox)
+ if MAC(kek, data=cek, iv=ukm, sbox=sbox).digest()[:4] != cek_mac:
raise ValueError("Invalid MAC")
return cek
-def wrap_cryptopro(ukm, kek, cek):
+def wrap_cryptopro(ukm, kek, cek, sbox=DEFAULT_SBOX):
"""CryptoPro key wrapping
:param ukm: UKM
@@ -76,10 +77,10 @@ def wrap_cryptopro(ukm, kek, cek):
:returns: wrapped key
:rtype: bytes, 44 bytes
"""
- return wrap_gost(ukm, diversify(kek, bytearray(ukm)), cek)
+ return wrap_gost(ukm, diversify(kek, bytearray(ukm)), cek, sbox=sbox)
-def unwrap_cryptopro(kek, data):
+def unwrap_cryptopro(kek, data, sbox=DEFAULT_SBOX):
"""CryptoPro key unwrapping
:param kek: key encryption key
@@ -91,10 +92,14 @@ def unwrap_cryptopro(kek, data):
"""
if len(data) < 8:
raise ValueError("Invalid data length")
- return unwrap_gost(diversify(kek, bytearray(data[:8])), data)
+ return unwrap_gost(
+ diversify(kek, bytearray(data[:8]), sbox=sbox),
+ data,
+ sbox=sbox,
+ )
-def diversify(kek, ukm):
+def diversify(kek, ukm, sbox=DEFAULT_SBOX):
out = kek
for i in range(8):
s1, s2 = 0, 0
@@ -105,5 +110,5 @@ def diversify(kek, ukm):
else:
s2 += k
iv = pack("<I", s1 % 2 ** 32) + pack("<I", s2 % 2 ** 32)
- out = cfb_encrypt(out, out, iv=iv)
+ out = cfb_encrypt(out, out, iv=iv, sbox=sbox)
return out