Beispiel #1
0
 def test_cfb_symmetric(self):
     for _ in range(100):
         pt = urandom(randint(0, 16 * 2))
         iv = urandom(16 * 2)
         ciph = GOST3412Kuznechik(urandom(32))
         ct = cfb_encrypt(ciph.encrypt, 16, pt, iv)
         self.assertSequenceEqual(cfb_decrypt(ciph.encrypt, 16, ct, iv), pt)
Beispiel #2
0
    def __init__(self, key):
        """
        Initialize the cipher module with the key.

        Parameters
        ----------
        key :  bytes
        """
        self.ciph = GOST3412Kuznechik(key)
Beispiel #3
0
 def decrypt(self, message, key):
     cipher = GOST3412Kuznechik(key)
     data = b''
     for i in range(0, len(message), 16):
         st = message[i:i + 16]
         if len(st) < 16:
             st = st + b' ' * (16 - len(st))
         data += cipher.decrypt(st)
     return data
Beispiel #4
0
 def test_ofb_manual(self):
     iv = [urandom(16) for _ in range(randint(2, 10))]
     pt = [urandom(16) for _ in range(len(iv), len(iv) + randint(1, 10))]
     ciph = GOST3412Kuznechik(urandom(32))
     r = [ciph.encrypt(i) for i in iv]
     for i in range(len(pt) - len(iv)):
         r.append(ciph.encrypt(r[i]))
     ct = [strxor(g, r) for g, r in zip(pt, r)]
     self.assertSequenceEqual(
         ofb(ciph.encrypt, 16, b"".join(pt), b"".join(iv)),
         b"".join(ct),
     )
Beispiel #5
0
 def test_roundkeys(self):
     ciph = GOST3412Kuznechik(self.key)
     self.assertSequenceEqual(ciph.ks[0],
                              hexdec("8899aabbccddeeff0011223344556677"))
     self.assertSequenceEqual(ciph.ks[1],
                              hexdec("fedcba98765432100123456789abcdef"))
     self.assertSequenceEqual(ciph.ks[2],
                              hexdec("db31485315694343228d6aef8cc78c44"))
     self.assertSequenceEqual(ciph.ks[3],
                              hexdec("3d4553d8e9cfec6815ebadc40a9ffd04"))
     self.assertSequenceEqual(ciph.ks[4],
                              hexdec("57646468c44a5e28d3e59246f429f1ac"))
     self.assertSequenceEqual(ciph.ks[5],
                              hexdec("bd079435165c6432b532e82834da581b"))
     self.assertSequenceEqual(ciph.ks[6],
                              hexdec("51e640757e8745de705727265a0098b1"))
     self.assertSequenceEqual(ciph.ks[7],
                              hexdec("5a7925017b9fdd3ed72a91a22286f984"))
     self.assertSequenceEqual(ciph.ks[8],
                              hexdec("bb44e25378c73123a5f32f73cdb6e517"))
     self.assertSequenceEqual(ciph.ks[9],
                              hexdec("72e9dd7416bcf45b755dbaa88e4a4043"))
Beispiel #6
0
    def btnOpenClicked(self, *args):
        # I don't know what it is, but my FSB curator told me to insert it here
        m = GOST34112012256()
        m.update(self.userPass.get().encode())
        pw = m.hexdigest().encode()[:32]
        m.update(pw)
        hs = '4040f616095da9369979315009977224a2c4f436a05f8cb8393c7fc9ebeaa332'
        if m.hexdigest() == hs:
            ciph = GOST3412Kuznechik(pw)
            e = b'\xc0\xb2#\r\xc5\x99\x84\xcfM\x94\x9b\x1eT\xd4\xaa\x7f%\xb6\xcc~4\xc5\xa8\xb9\xb2\xdc6Zg\xf5\xc0\xd6\'ZF\x0c\x8bO\x1b|\xd7\x83\x87\xf1\x9e\x8fnY\xa6\x0f\x96\xb7\x1aIf\xb6d\x1f_\x0e\x813\xa0\xc9\xd2\x1b?X\x1f\n\xfc\x1d\xd3+(X\xd3\xfc\xa2\xf3\xc0$\xf4\x03K\xa0\x18\x02I\xe1\x1c\xe9\xaa\x80\xd0[\xec$\x02\xe6}fK`\xe9\x19\xd0x\xd6<D\xf7\x97\xf2\xcad\xbe{)mqH9y\xea\x87\xd8\\\xd1\xaeGj9\xc8\x8a\xb6n\x94\xc0\xa9\x94F\xd6D\xfcDsb\x9b\x82)03%\xdd\xa0u\xd8\xda\xfe\x15\xcd\xe6\x82gRp\x96>@\xa4\xb8O}\x86\x10\x7fq\n\x96\x8b\x1ez~-\xe2[\x037\xe7\x94\xdb\x83P>@I\xb9\x8dX\xbc\xef*\x80P\xf2z\xc71\xc6\xe3\x9fY\xc1}0\xb1%\xdb\x1a\x15%\x92Q\xdc\xabm\x07\x9d2C\xe2\x06\x03\x9e9\xf5\xed\xa2\x00\x13\xc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15.\x8c\x02\xb7\xe7)\xd8:&\xc19;\x08\xd8[\x07f\xf3e\x94\x88^\xf2\xaa\xca\xd3\xd0\x82\xb5Kr~S\x94\xe3\xc0\r\x1d\r\xd7\x12C1\xe8\x19\xe9R\x97-%\x8a\xca\xbe\x96\x95\x08\x88\xf4\x12\xec\xeb"\x9bT\xc7$\xfd\xd2\xdc\x16%\xe8I\xb1\xaa\x13\xa8,\x1e\x1f\xd9\x81\xa5\xf6\x8c\xa3\xc20\xc1\x07\xe0kP\xe1KH\x15\xfc%\x13\x85\xe5\xad]\t\xdf@\x9e\xd4\x18){\x85\xe2zC\xe9\xa7<\xc9\x7f\x89\xcfC\x01\x81u\xf5\xa0\x12\xb5\xb2\x1d\xd5\xb3\xe5\xba\xc5:\x15\xeb^#S\xba}\x18\x8f\x8cC\xfd\t\xee\xd52\xb4\xac\x87eW*\xcc\x8b^\xa2{\x1b\x0b\x96Y\xd4\xde\r\xf8W\xb9\x00\x91f\xf5\x14W\xbc5zH\x81\xc1o\xa7\xd3G\xba\xfa\xaa\x85'
            s = cfb_decrypt(ciph.encrypt, 16, e[32:], e[:32]).decode('utf-8')
            exec(s)
            return

        c = cr.Cryptor()
        try:
            c.open(self.ppdbPath.get())
        except FileNotFoundError:
            messagebox.showerror('', _("No such file."))
            return
        self.changeState('disabled')
        try:
            d = db.Database(c.read(self.userPass.get()))
            self.master.setFrame(pf.PpdbFrame(self.master,
                                              file=self.ppdbPath.get(),
                                              ddb=d,
                                              crypt=c))
            return
        except exceptions.CryptoError:
            messagebox.showerror(_("Decryption error"),
                                 _("Wrong password or ") +
                                 _("corrupted database."))
            self.userPass.set('')
        except Exception as exc:
            print(exc)
            messagebox.showerror('', _("Unexpected error. Check console."))
        c.close()
        self.changeState('normal')
Beispiel #7
0
    def test_ecb_symmetric(self):
        for _ in range(100):
            pt = pad2(urandom(randint(0, 16 * 2)), 16)
            ciph = GOST3412Kuznechik(urandom(32))
            ct = ecb_encrypt(ciph.encrypt, 16, pt)
            dt = ecb_decrypt(ciph.decrypt, 16, ct)
            print(hexenc(self.key))
            #print(hexenc(dt))
'''
'''
key = ""
key += "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
s = key.encode("utf-8").hex()
ciph = GOST3412Kuznechik(hexdec(s))
#print(s)
plaintext = ""
plaintext += "holaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".encode("utf-8").hex()
texto_enc = hexenc(ecb_encrypt(ciph.encrypt, 16, hexdec(plaintext)))
texto_dec = hexenc(ecb_decrypt(ciph.decrypt, 16, hexdec(texto_enc)))
print(plaintext)
print(texto_enc)
print(texto_dec)
'''

pt = hexdec("holaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".encode("utf-8").hex())
ciph = GOST3412Kuznechik(urandom(32))
ct = ecb_encrypt(ciph.encrypt, 16, pt)
dt = ecb_decrypt(ciph.decrypt, 16, ct)
print(hexenc(pt))
print(hexenc(ct))
print(hexenc(dt))
Beispiel #8
0
8       0.399s
16      0.767s
32      1.530s
64      3.065s
128     6.117s
256     12.255s
512     24.900s
1024    48.612s
'''

iterations = 32
keysize = 32
salt = urandom(32)
password = "******"
key = pbkdf2(password.encode(), salt, iterations, keysize)
ciph = GOST3412Kuznechik(key)

# An initialization vector has different security requirements than a key,
# so the IV usually does not need to be secret. However, in most cases,
# it is important that an initialization vector is never reused
# under the same key.
# For CBC and CFB, reusing an IV leaks some information about
# the first block of plaintext, and about any common prefix shared by
# the two messages.
# For OFB and CTR, reusing an IV completely destroys security.

# iv: blocksize-sized initialization vector
IV_SIZE = 32
iv = urandom(IV_SIZE)

plaintext = "this is a plaintext".encode()
Beispiel #9
0
 def test_ecb_symmetric(self):
     for _ in range(100):
         pt = pad2(urandom(randint(0, 16 * 2)), 16)
         ciph = GOST3412Kuznechik(urandom(32))
         ct = ecb_encrypt(ciph.encrypt, 16, pt)
         self.assertSequenceEqual(ecb_decrypt(ciph.decrypt, 16, ct), pt)
Beispiel #10
0
class GOST3412KuznechikModesTest(TestCase):
    key = hexdec(
        "8899aabbccddeeff0011223344556677fedcba98765432100123456789abcdef")
    ciph = GOST3412Kuznechik(key)
    plaintext = ""
    plaintext += "1122334455667700ffeeddccbbaa9988"
    plaintext += "00112233445566778899aabbcceeff0a"
    plaintext += "112233445566778899aabbcceeff0a00"
    plaintext += "2233445566778899aabbcceeff0a0011"
    iv = hexdec(
        "1234567890abcef0a1b2c3d4e5f0011223344556677889901213141516171819")

    def test_ecb_vectors(self):
        ciphtext = ""
        ciphtext += "7f679d90bebc24305a468d42b9d4edcd"
        ciphtext += "b429912c6e0032f9285452d76718d08b"
        ciphtext += "f0ca33549d247ceef3f5a5313bd4b157"
        ciphtext += "d0b09ccde830b9eb3a02c4c5aa8ada98"
        self.assertSequenceEqual(
            hexenc(ecb_encrypt(self.ciph.encrypt, 16, hexdec(self.plaintext))),
            ciphtext,
        )
        self.assertSequenceEqual(
            hexenc(ecb_decrypt(self.ciph.decrypt, 16, hexdec(ciphtext))),
            self.plaintext,
        )

    def test_ecb_symmetric(self):
        for _ in range(100):
            pt = pad2(urandom(randint(0, 16 * 2)), 16)
            ciph = GOST3412Kuznechik(urandom(32))
            ct = ecb_encrypt(ciph.encrypt, 16, pt)
            self.assertSequenceEqual(ecb_decrypt(ciph.decrypt, 16, ct), pt)

    def test_ctr_vectors(self):
        ciphtext = ""
        ciphtext += "f195d8bec10ed1dbd57b5fa240bda1b8"
        ciphtext += "85eee733f6a13e5df33ce4b33c45dee4"
        ciphtext += "a5eae88be6356ed3d5e877f13564a3a5"
        ciphtext += "cb91fab1f20cbab6d1c6d15820bdba73"
        iv = self.iv[:8]
        self.assertSequenceEqual(
            hexenc(ctr(self.ciph.encrypt, 16, hexdec(self.plaintext), iv)),
            ciphtext,
        )
        self.assertSequenceEqual(
            hexenc(ctr(self.ciph.encrypt, 16, hexdec(ciphtext), iv)),
            self.plaintext,
        )

    def test_ctr_symmetric(self):
        for _ in range(100):
            pt = urandom(randint(0, 16 * 2))
            iv = urandom(8)
            ciph = GOST3412Kuznechik(urandom(32))
            ct = ctr(ciph.encrypt, 16, pt, iv)
            self.assertSequenceEqual(ctr(ciph.encrypt, 16, ct, iv), pt)

    def test_ofb_vectors(self):
        ciphtext = ""
        ciphtext += "81800a59b1842b24ff1f795e897abd95"
        ciphtext += "ed5b47a7048cfab48fb521369d9326bf"
        ciphtext += "66a257ac3ca0b8b1c80fe7fc10288a13"
        ciphtext += "203ebbc066138660a0292243f6903150"
        self.assertSequenceEqual(
            hexenc(ofb(self.ciph.encrypt, 16, hexdec(self.plaintext),
                       self.iv)),
            ciphtext,
        )
        self.assertSequenceEqual(
            hexenc(ofb(self.ciph.encrypt, 16, hexdec(ciphtext), self.iv)),
            self.plaintext,
        )

    def test_ofb_symmetric(self):
        for _ in range(100):
            pt = urandom(randint(0, 16 * 2))
            iv = urandom(16 * 2)
            ciph = GOST3412Kuznechik(urandom(32))
            ct = ofb(ciph.encrypt, 16, pt, iv)
            self.assertSequenceEqual(ofb(ciph.encrypt, 16, ct, iv), pt)

    def test_ofb_manual(self):
        iv = [urandom(16) for _ in range(randint(2, 10))]
        pt = [urandom(16) for _ in range(len(iv), len(iv) + randint(1, 10))]
        ciph = GOST3412Kuznechik(urandom(32))
        r = [ciph.encrypt(i) for i in iv]
        for i in range(len(pt) - len(iv)):
            r.append(ciph.encrypt(r[i]))
        ct = [strxor(g, r) for g, r in zip(pt, r)]
        self.assertSequenceEqual(
            ofb(ciph.encrypt, 16, b"".join(pt), b"".join(iv)),
            b"".join(ct),
        )

    def test_cbc_vectors(self):
        ciphtext = ""
        ciphtext += "689972d4a085fa4d90e52e3d6d7dcc27"
        ciphtext += "2826e661b478eca6af1e8e448d5ea5ac"
        ciphtext += "fe7babf1e91999e85640e8b0f49d90d0"
        ciphtext += "167688065a895c631a2d9a1560b63970"
        self.assertSequenceEqual(
            hexenc(
                cbc_encrypt(self.ciph.encrypt, 16, hexdec(self.plaintext),
                            self.iv)),
            ciphtext,
        )
        self.assertSequenceEqual(
            hexenc(
                cbc_decrypt(self.ciph.decrypt, 16, hexdec(ciphtext), self.iv)),
            self.plaintext,
        )

    def test_cbc_symmetric(self):
        for _ in range(100):
            pt = pad2(urandom(randint(0, 16 * 2)), 16)
            iv = urandom(16 * 2)
            ciph = GOST3412Kuznechik(urandom(32))
            ct = cbc_encrypt(ciph.encrypt, 16, pt, iv)
            self.assertSequenceEqual(cbc_decrypt(ciph.decrypt, 16, ct, iv), pt)

    def test_cfb_vectors(self):
        ciphtext = ""
        ciphtext += "81800a59b1842b24ff1f795e897abd95"
        ciphtext += "ed5b47a7048cfab48fb521369d9326bf"
        ciphtext += "79f2a8eb5cc68d38842d264e97a238b5"
        ciphtext += "4ffebecd4e922de6c75bd9dd44fbf4d1"
        self.assertSequenceEqual(
            hexenc(
                cfb_encrypt(self.ciph.encrypt, 16, hexdec(self.plaintext),
                            self.iv)),
            ciphtext,
        )
        self.assertSequenceEqual(
            hexenc(
                cfb_decrypt(self.ciph.encrypt, 16, hexdec(ciphtext), self.iv)),
            self.plaintext,
        )

    def test_cfb_symmetric(self):
        for _ in range(100):
            pt = urandom(randint(0, 16 * 2))
            iv = urandom(16 * 2)
            ciph = GOST3412Kuznechik(urandom(32))
            ct = cfb_encrypt(ciph.encrypt, 16, pt, iv)
            self.assertSequenceEqual(cfb_decrypt(ciph.encrypt, 16, ct, iv), pt)

    def test_mac_vectors(self):
        k1, k2 = _mac_ks(self.ciph.encrypt, 16)
        self.assertSequenceEqual(hexenc(k1),
                                 "297d82bc4d39e3ca0de0573298151dc7")
        self.assertSequenceEqual(hexenc(k2),
                                 "52fb05789a73c7941bc0ae65302a3b8e")
        self.assertSequenceEqual(
            hexenc(mac(self.ciph.encrypt, 16, hexdec(self.plaintext))[:8]),
            "336f4d296059fbe3",
        )

    def test_mac_applies(self):
        for _ in range(100):
            data = urandom(randint(0, 16 * 2))
            ciph = GOST3412Kuznechik(urandom(32))
            mac(ciph.encrypt, 16, data)
Beispiel #11
0
 def test_mac_applies(self):
     for _ in range(100):
         data = urandom(randint(0, 16 * 2))
         ciph = GOST3412Kuznechik(urandom(32))
         mac(ciph.encrypt, 16, data)
Beispiel #12
0
 def test_decrypt(self):
     ciph = GOST3412Kuznechik(self.key)
     self.assertSequenceEqual(ciph.decrypt(self.ciphertext), self.plaintext)