File: aes_decrypter.py

package info (click to toggle)
dfvfs 20240505-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 475,508 kB
  • sloc: python: 36,533; vhdl: 1,922; sh: 448; xml: 52; makefile: 16
file content (100 lines) | stat: -rw-r--r-- 3,449 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for the AES decrypter object."""

import unittest

from dfvfs.encryption import aes_decrypter
from dfvfs.lib import definitions
from dfvfs.lib import errors

from tests.encryption import test_lib


class AESDecrypterTestCase(test_lib.DecrypterTestCase):
  """Tests for the AES decrypter object."""

  _AES_INITIALIZATION_VECTOR = b'This is an IV456'
  _AES_KEY = b'This is a key123'

  def testInitialization(self):
    """Tests the initialization method."""
    # Test missing initialization vector with valid block cipher mode.
    try:
      aes_decrypter.AESDecrypter(
          cipher_mode=definitions.ENCRYPTION_MODE_ECB, key=self._AES_KEY)
    except errors.BackEndError:
      raise unittest.SkipTest('missing cryptograpy AES support')

    # Test missing arguments.
    with self.assertRaises(ValueError):
      aes_decrypter.AESDecrypter()

    # Test unsupported block cipher mode.
    with self.assertRaises(ValueError):
      aes_decrypter.AESDecrypter(
          cipher_mode='bogus', key=self._AES_KEY)

    # Test missing initialization vector.
    with self.assertRaises(ValueError):
      aes_decrypter.AESDecrypter(
          cipher_mode=definitions.ENCRYPTION_MODE_CBC, key=self._AES_KEY)

    # Test incorrect key size.
    with self.assertRaises(ValueError):
      aes_decrypter.AESDecrypter(
          cipher_mode=definitions.ENCRYPTION_MODE_ECB, key=b'Wrong key size.')

    # Test incorrect initialization vector type.
    with self.assertRaises(TypeError):
      aes_decrypter.AESDecrypter(
          cipher_mode=definitions.ENCRYPTION_MODE_CBC,
          initialization_vector='Wrong IV type', key=self._AES_KEY)

    # Test incorrect initialization vector size.
    with self.assertRaises(ValueError):
      aes_decrypter.AESDecrypter(
          cipher_mode=definitions.ENCRYPTION_MODE_CBC,
          initialization_vector=b'Wrong IV size', key=self._AES_KEY)

  def testDecrypt(self):
    """Tests the Decrypt method."""
    try:
      decrypter = aes_decrypter.AESDecrypter(
          cipher_mode=definitions.ENCRYPTION_MODE_CBC,
          initialization_vector=self._AES_INITIALIZATION_VECTOR,
          key=self._AES_KEY)
    except errors.BackEndError:
      raise unittest.SkipTest('missing cryptograpy AES support')

    # Test full decryption.
    expected_decrypted_data = b'This is secret encrypted text!!!'

    decrypted_data, remaining_encrypted_data = decrypter.Decrypt(
        b'2|\x7f\xd7\xff\xbay\xf9\x95?\x81\xc7\xaafV\xceB\x01\xdb8E7\xfe'
        b'\x92j\xf0\x1d(\xb9\x9f\xad\x13', finalize=True)

    self.assertEqual(decrypted_data, expected_decrypted_data)
    self.assertEqual(remaining_encrypted_data, b'')

    # Reset decrypter.
    try:
      decrypter = aes_decrypter.AESDecrypter(
          cipher_mode=definitions.ENCRYPTION_MODE_CBC,
          initialization_vector=self._AES_INITIALIZATION_VECTOR,
          key=self._AES_KEY)
    except errors.BackEndError:
      raise unittest.SkipTest('missing cryptograpy AES support')

    # Test partial decryption.
    partial_encrypted_data = (
        b'2|\x7f\xd7\xff\xbay\xf9\x95?\x81\xc7\xaafV\xceB\x01\xdb8E7\xfe')

    decrypted_data, remaining_encrypted_data = decrypter.Decrypt(
        partial_encrypted_data)
    self.assertEqual(decrypted_data, b'This is secret e')
    self.assertEqual(remaining_encrypted_data, b'B\x01\xdb8E7\xfe')


if __name__ == '__main__':
  unittest.main()