Example #1
0
    def _helper_test_encrypt_decrypt(self,
                                     key: bytes,
                                     nonce: bytes,
                                     block_counter: bytes,
                                     rounds: int,
                                     plaintext: bytes,
                                     ciphertext: bytes):
        encryptor = salsa20.Salsa20(key, nonce, block_counter, rounds)
        decryptor = salsa20.Salsa20(key, nonce, block_counter, rounds)

        encrypted = encryptor.encrypt(plaintext)
        decrypted = decryptor.decrypt(encrypted)

        self.assertEqual(ciphertext, encrypted)
        self.assertEqual(plaintext, decrypted)
        print(f'\ndecrypted: {decrypted.decode("utf-8")}')
 def __call__(self, data):
     iv = secrets.token_bytes(4)
     inner = mode_normal()(data)
     return (
         b'E' + bin.uint8_t(len(self.keyname))  # key_name_length
         + self.keyname  # key_name
         + bin.uint8_t(len(iv))  # iv_length
         + iv  # iv
         + b'S'  # type
         +
         salsa20.Salsa20(salsa20_keys[self.keyname], iv, 20).encrypt(inner))
def challenge_distinguisher():
    coin = ord(os.urandom(1)[0]) % 2
    ctxt_blocks = []
    if coin == 0:
        #print "THIS ACTUALLY IS A SALSA"
        #   salsa bytes
        sk = os.urandom(32)
        #   initialization vector (nonce)
        iv = 0
        for i in range(B):
            salsa = salsa20.Salsa20(sk, pack("<Q", iv), r)
            ctxt = salsa.encrypt('\x00' * 64)
            ctxt_blocks.append(ctxt)
            iv += 1
    else:
        #print "NOT SALSA"
        #   random bytes
        for i in range(B):
            ctxt_blocks.append(os.urandom(64))
    if distinguish(ctxt_blocks) == coin:
        return True
    return False
 def data_to(self, data):
     iv = os.urandom(8)
     m_data = pickle.dumps(data)
     sal = salsa20.Salsa20(self.enc_key, iv, 20)
     s_data = sal.encrypt(m_data)
     return iv + s_data
 def data_from(self, data):
     iv = data[:8]
     sal = salsa20.Salsa20(self.enc_key, iv, 20)
     s_data = sal.decrypt(data[8:])
     m_data = pickle.loads(s_data)
     return m_data