Ejemplo n.º 1
0
    def test_encrypt_decrypt_block(self):
        algorithm1 = RSA_OAEP(512)
        self._helper_test_encrypt_decrypt_block(algorithm1, b'\x10' * (algorithm1._block_length // 8))
        self._helper_test_encrypt_decrypt_block(algorithm1, b'message' + b'7' * 55)

        algorithm2 = RSA_OAEP(1024)
        self._helper_test_encrypt_decrypt_block(algorithm2, b'\x10' * (algorithm2._block_length // 8))
Ejemplo n.º 2
0
    def test_encrypt_decrypt(self):
        algorithm1 = RSA_OAEP(512)
        self._helper_test_encrypt_decrypt(algorithm1, b'message')
        self._helper_test_encrypt_decrypt(algorithm1,
                                          b'RSA is a public-key cryptosystem that is widely used for secure data transmission. It is also one of the oldest.')

        algorithm2 = RSA_OAEP(1024)
        self._helper_test_encrypt_decrypt(algorithm2, b'message')
        self._helper_test_encrypt_decrypt(algorithm2,
                                          b'RSA is a public-key cryptosystem that is widely used for secure data transmission. It is also one of the oldest.')
Ejemplo n.º 3
0
 def test_init(self):
     algorithm = RSA_OAEP(512)
     self.assertEqual(512 - 16, algorithm._block_length)
     self.assertEqual(512 - 8, algorithm._padded_block_length)
     algorithm = RSA_OAEP(1024)
     self.assertEqual(1024 - 512 - 8, algorithm._block_length)
     self.assertEqual(1024 - 512, algorithm._padded_block_length)
     with self.assertRaises(AssertionError):
         _ = RSA_OAEP(126)
         _ = RSA_OAEP(760)
Ejemplo n.º 4
0
    def test_encrypt_block(self):
        algorithm1 = RSA_OAEP(512)
        message1 = b'\x00' * (algorithm1._block_length // 8)
        message2 = b'\x00' * ((algorithm1._block_length + 8) // 8)
        self.assertEqual(128, len(algorithm1._encrypt_block(message1)))
        with self.assertRaises(AssertionError):
            algorithm1._encrypt_block(message2)

        algorithm2 = RSA_OAEP(1024)
        message3 = b'\x00' * (algorithm2._block_length // 8)
        message4 = b'\x00' * (1024 // 8)
        self.assertEqual(256, len(algorithm2._encrypt_block(message3)))
        with self.assertRaises(AssertionError):
            algorithm2._encrypt_block(message4)
Ejemplo n.º 5
0
    def test_decrypt_block(self):
        algorithm1 = RSA_OAEP(512)
        encrypted1 = algorithm1._encrypt_block(b'\x00' * (algorithm1._block_length // 8))
        self.assertEqual(algorithm1._block_length // 8, len(algorithm1._decrypt_block(encrypted1)))

        algorithm2 = RSA_OAEP(1024)
        encrypted3 = algorithm2._encrypt_block(b'\x00' * (algorithm2._block_length // 8))
        self.assertEqual(algorithm2._block_length // 8, len(algorithm2._decrypt_block(encrypted3)))
Ejemplo n.º 6
0
 def test_split_blocks(self):
     algorithm = RSA_OAEP(512)
     block_bytes = algorithm._block_length // 8
     blocks = _split_blocks(b'\x00' * (block_bytes * 4), 62)
     self.assertEqual(4, len(blocks))
     for block in blocks:
         self.assertEqual(len(block), block_bytes)
Ejemplo n.º 7
0
 def test_un_pad(self):
     algorithm = RSA_OAEP(512)
     block_bytes = algorithm._block_length // 8
     self.assertEqual(0, len(_un_pad(
         b'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>',
         algorithm._block_length // 8)))
     self.assertEqual(10, len(_un_pad(
         b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x004444444444444444444444444444444444444444444444444444',
         algorithm._block_length // 8)))
     self.assertEqual(block_bytes + 1, len(_un_pad(
         b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00=============================================================',
         algorithm._block_length // 8)))
Ejemplo n.º 8
0
    def test_un_pad_block(self):
        algorithm = RSA_OAEP(512)
        self.assertEqual(algorithm._block_length // 8,
                         len(algorithm._un_pad_block(b'\x00' * (algorithm._padded_block_length // 8))))

        algorithm = RSA_OAEP(1024)
        self.assertEqual(algorithm._block_length // 8,
                         len(algorithm._un_pad_block(b'\x00' * (algorithm._padded_block_length // 8))))
Ejemplo n.º 9
0
 def test_xor(self):
     self.assertEqual(b'\x01\x01', RSA_OAEP._xor(b'\x01\x00', b'\x00\x01'))
     with self.assertRaises(AssertionError):
         RSA_OAEP._xor(b'', b'\x00')
Ejemplo n.º 10
0
 def test_pad(self):
     algorithm = RSA_OAEP(512)
     block_bytes = algorithm._block_length // 8
     self.assertEqual(block_bytes, len(_pad(b'', block_bytes)))
     self.assertEqual(block_bytes, len(_pad(b'\x00' * 10, block_bytes)))
     self.assertEqual(block_bytes * 2, len(_pad(b'\x00' * (block_bytes + 1), block_bytes)))