Debian Codegraph @

lib/Crypto/SelfTest/IO/test_PBES.py

raw symbols SCIP
1 #
2 # SelfTest/IO/test_PBES.py: Self-test for the _PBES module
3 #
4 # ===================================================================
5 #
6 # Copyright (c) 2014, Legrandin <[email protected]>
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in
17 # the documentation and/or other materials provided with the
18 # distribution.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 # ===================================================================
33
34 """Self-tests for Crypto.IO._PBES module"""
35
36 import unittest
37
38 from Crypto.IO._PBES import PBES2
39
40
41 class TestPBES2(unittest.TestCase):
42
43 def setUp(self):
44 self.ref = b"Test data"
45 self.passphrase = b"Passphrase"
46
47 def test1(self):
48 ct = PBES2.encrypt(self.ref, self.passphrase,
49 'PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC')
50 pt = PBES2.decrypt(ct, self.passphrase)
51 self.assertEqual(self.ref, pt)
52
53 def test2(self):
54 ct = PBES2.encrypt(self.ref, self.passphrase,
55 'PBKDF2WithHMAC-SHA224AndAES128-CBC')
56 pt = PBES2.decrypt(ct, self.passphrase)
57 self.assertEqual(self.ref, pt)
58
59 def test3(self):
60 ct = PBES2.encrypt(self.ref, self.passphrase,
61 'PBKDF2WithHMAC-SHA256AndAES192-CBC')
62 pt = PBES2.decrypt(ct, self.passphrase)
63 self.assertEqual(self.ref, pt)
64
65 def test4(self):
66 ct = PBES2.encrypt(self.ref, self.passphrase,
67 'PBKDF2WithHMAC-SHA384AndAES256-CBC')
68 pt = PBES2.decrypt(ct, self.passphrase)
69 self.assertEqual(self.ref, pt)
70
71 def test5(self):
72 ct = PBES2.encrypt(self.ref, self.passphrase,
73 'PBKDF2WithHMAC-SHA512AndAES128-GCM')
74 pt = PBES2.decrypt(ct, self.passphrase)
75 self.assertEqual(self.ref, pt)
76
77 def test6(self):
78 ct = PBES2.encrypt(self.ref, self.passphrase,
79 'PBKDF2WithHMAC-SHA512-224AndAES192-GCM')
80 pt = PBES2.decrypt(ct, self.passphrase)
81 self.assertEqual(self.ref, pt)
82
83 def test7(self):
84 ct = PBES2.encrypt(self.ref, self.passphrase,
85 'PBKDF2WithHMAC-SHA3-256AndAES256-GCM')
86 pt = PBES2.decrypt(ct, self.passphrase)
87 self.assertEqual(self.ref, pt)
88
89 def test8(self):
90 ct = PBES2.encrypt(self.ref, self.passphrase,
91 'scryptAndAES128-CBC')
92 pt = PBES2.decrypt(ct, self.passphrase)
93 self.assertEqual(self.ref, pt)
94
95 def test9(self):
96 ct = PBES2.encrypt(self.ref, self.passphrase,
97 'scryptAndAES192-CBC')
98 pt = PBES2.decrypt(ct, self.passphrase)
99 self.assertEqual(self.ref, pt)
100
101 def test10(self):
102 ct = PBES2.encrypt(self.ref, self.passphrase,
103 'scryptAndAES256-CBC')
104 pt = PBES2.decrypt(ct, self.passphrase)
105 self.assertEqual(self.ref, pt)
106
107
108 def get_tests(config={}):
109 from Crypto.SelfTest.st_common import list_test_cases
110 listTests = []
111 listTests += list_test_cases(TestPBES2)
112 return listTests
113
114
115 if __name__ == '__main__':
116 def suite():
117 return unittest.TestSuite(get_tests())
118 unittest.main(defaultTest='suite')