def test_symmetric(self):
     random = np.random.RandomState(seed=1)
     for i in range(10):
         length = random.randint(1, 4096)
         key = bytes(random.randint(0, 256, 32, dtype=np.uint8))
         nonce = bytes(random.randint(0, 256, 24, dtype=np.uint8))
         msg = bytes(random.randint(0, 256, length, dtype=np.uint8))
         mac, c = monocypher.lock(key, nonce, msg)
         msg2 = monocypher.unlock(key, nonce, mac, c)
         self.assertNotEqual(msg, c)
         self.assertEqual(msg, msg2)
 def test_symmetric_aead(self):
     random = np.random.RandomState(seed=1)
     for i in range(10):
         message_length = random.randint(1, 4096)
         aead_length = random.randint(1, 128)
         key = bytes(random.randint(0, 256, 32, dtype=np.uint8))
         nonce = bytes(random.randint(0, 256, 24, dtype=np.uint8))
         aead = bytes(random.randint(0, 256, aead_length, dtype=np.uint8))
         msg = bytes(random.randint(0, 256, message_length, dtype=np.uint8))
         mac, c = monocypher.lock(key, nonce, msg, associated_data=aead)
         msg2 = monocypher.unlock(key, nonce, mac, c, associated_data=aead)
         self.assertEqual(msg, msg2)
Example #3
0
 def append_encrypted(self,
                      tag,
                      data,
                      signing_key,
                      encryption_key,
                      nonce,
                      associated_data=None,
                      compress=None):
     flags = 0
     if bool(compress):
         flags, data = _maybe_compress(data)
     flags |= FLAG_ENCRYPT
     signature = monocypher.signature_sign(signing_key, data)
     mac, data = monocypher.lock(encryption_key, nonce, data,
                                 associated_data)
     log.info('signature = %r', binascii.hexlify(signature))
     log.info('mac       = %r', binascii.hexlify(mac))
     pos = self._append(tag, flags, data)
     self._append(TAG_ENCRYPTION, 0, mac + signature)
     return pos
Example #4
0
    def test_symmetric(self):
        random = np.random.RandomState(seed=1)
        for i in range(10):
            length = random.randint(1, 4096)
            key = bytes(random.randint(0, 256, 32, dtype=np.uint8))
            nonce = bytes(random.randint(0, 256, 24, dtype=np.uint8))
            msg = bytes(random.randint(0, 256, length, dtype=np.uint8))
            mac, c = monocypher.lock(key, nonce, msg)
            msg2 = monocypher.unlock(key, nonce, mac, c)
            self.assertNotEqual(msg, c)
            self.assertEqual(msg, msg2)

            e = monocypher.Encrypt(key, nonce)
            c2 = e.update(msg)
            mac2 = e.finalize()
            self.assertEqual(mac, mac2)
            self.assertEqual(c, c2)

            d = monocypher.Decrypt(key, nonce)
            msg3 = d.update(c2)
            self.assertEqual(0, d.finalize(mac2))
            self.assertEqual(msg, msg3)
Example #5
0
    def test_symmetric(self):
        random = np.random.RandomState(seed=1)
        for i in range(10):
            length = random.randint(1, 4096)
            key = bytes(random.randint(0, 256, 32, dtype=np.uint8))
            nonce = bytes(random.randint(0, 256, 24, dtype=np.uint8))
            msg = bytes(random.randint(0, 256, length, dtype=np.uint8))
            mac, c = monocypher.lock(key, nonce, msg)
            msg2 = monocypher.unlock(key, nonce, mac, c)
            self.assertNotEqual(msg, c)
            self.assertEqual(msg, msg2)

            e = monocypher.Encrypt(key, nonce)
            c2 = e.update(msg)
            mac2 = e.finalize()
            self.assertEqual(mac, mac2)
            self.assertEqual(c, c2)

            d = monocypher.Decrypt(key, nonce)
            msg3 = d.update(c2)
            self.assertEqual(0, d.finalize(mac2))
            self.assertEqual(msg, msg3)
Example #6
0
    def test_symmetric_aead(self):
        random = np.random.RandomState(seed=1)
        for i in range(10):
            message_length = random.randint(1, 4096)
            aead_length = random.randint(1, 128)
            key = bytes(random.randint(0, 256, 32, dtype=np.uint8))
            nonce = bytes(random.randint(0, 256, 24, dtype=np.uint8))
            aead = bytes(random.randint(0, 256, aead_length, dtype=np.uint8))
            msg = bytes(random.randint(0, 256, message_length, dtype=np.uint8))
            mac, c = monocypher.lock(key, nonce, msg, associated_data=aead)
            msg2 = monocypher.unlock(key, nonce, mac, c, associated_data=aead)
            self.assertEqual(msg, msg2)

            e = monocypher.Encrypt(key, nonce, associated_data=aead)
            c2 = e.update(msg)
            mac2 = e.finalize()
            self.assertEqual(mac, mac2)
            self.assertEqual(c, c2)

            d = monocypher.Decrypt(key, nonce, associated_data=aead)
            msg3 = d.update(c2)
            self.assertEqual(0, d.finalize(mac2))
            self.assertEqual(msg, msg3)
Example #7
0
    def test_symmetric_aead(self):
        random = np.random.RandomState(seed=1)
        for i in range(10):
            message_length = random.randint(1, 4096)
            aead_length = random.randint(1, 128)
            key = bytes(random.randint(0, 256, 32, dtype=np.uint8))
            nonce = bytes(random.randint(0, 256, 24, dtype=np.uint8))
            aead = bytes(random.randint(0, 256, aead_length, dtype=np.uint8))
            msg = bytes(random.randint(0, 256, message_length, dtype=np.uint8))
            mac, c = monocypher.lock(key, nonce, msg, associated_data=aead)
            msg2 = monocypher.unlock(key, nonce, mac, c, associated_data=aead)
            self.assertEqual(msg, msg2)

            e = monocypher.Encrypt(key, nonce, associated_data=aead)
            c2 = e.update(msg)
            mac2 = e.finalize()
            self.assertEqual(mac, mac2)
            self.assertEqual(c, c2)

            d = monocypher.Decrypt(key, nonce, associated_data=aead)
            msg3 = d.update(c2)
            self.assertEqual(0, d.finalize(mac2))
            self.assertEqual(msg, msg3)