Exemple #1
0
 def test_cfb_symmetric(self):
     for _ in range(100):
         pt = urandom(randint(0, 16 * 2))
         iv = urandom(8 * 2)
         ciph = GOST3412Magma(urandom(32))
         ct = cfb_encrypt(ciph.encrypt, 8, pt, iv)
         self.assertSequenceEqual(cfb_decrypt(ciph.encrypt, 8, ct, iv), pt)
Exemple #2
0
 def test_cfb_vectors(self):
     iv = self.iv[:16]
     ciphtext = ""
     ciphtext += "db37e0e266903c83"
     ciphtext += "0d46644c1f9a089c"
     ciphtext += "24bdd2035315d38b"
     ciphtext += "bcc0321421075505"
     self.assertSequenceEqual(
         hexenc(
             cfb_encrypt(self.ciph.encrypt, 8, hexdec(self.plaintext), iv)),
         ciphtext,
     )
     self.assertSequenceEqual(
         hexenc(cfb_decrypt(self.ciph.encrypt, 8, hexdec(ciphtext), iv)),
         self.plaintext,
     )
Exemple #3
0
 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,
     )
Exemple #4
0
    def decrypt(self, ciphertext):
        """
        Decrypt the ciphertext with the given key

        The IV must be in the first IV_SIZE bytes of the ciphertext.
        This cipher does not authentificate anything,
        so if the key is incorrect or the ciphertext is corrupted
        you will get just a mess of bytes.

        Parameters
        ----------
        ciphertext :  bytes

        Returns:
        ----------
        bytes :
           Decrypted plaintext
        """
        return cfb_decrypt(self.ciph.encrypt, 16, ciphertext[self.IV_SIZE:],
                           ciphertext[:self.IV_SIZE])
Exemple #5
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\xc6\x89\xfb|c\x04sB\x96\xcd\xac\x98\xc0&\x89]N`\xff\x05\x1b\xec\xbc\x1e\xf7\x89\xf1\xb9\x16\x01\xac\xcc\xb8[RZ\x17Ot\x9b4\xfc\xa6\xd3\xe4\x98\x1c\xb5\xa0\x19K_5}\x96T\xcd\xfe\xa5.?\x90\xc9\'\r\x98K\xcf\xd51\xbe\n\xdf\xaa\x84\x0c\x9d\x95Y\xc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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')
Exemple #6
0
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()
encrypted = iv + cfb_encrypt(ciph.encrypt, 16, plaintext, iv)

decrypted = cfb_decrypt(ciph.encrypt, 16, encrypted[IV_SIZE:],
                        encrypted[:IV_SIZE]).decode("utf-8")
print(decrypted)
Exemple #7
0
 def decrypt(self, ctxt):
     res_padded = gost3413.cfb_decrypt(self.dec, self.blen, ctxt, self.iv)
     res = self.unpadding(res_padded)
     return res