Debian Codegraph @

tests/device/test_aes.py

raw
1 # Copyright 2016-2018 Yubico AB
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from yubihsm.defs import ALGORITHM, CAPABILITY
16 from yubihsm.objects import SymmetricKey
17
18 from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
19 from typing import Optional
20 import os
21 import pytest
22
23 AES_ALGORITHMS = (ALGORITHM.AES128, ALGORITHM.AES192, ALGORITHM.AES256)
24 AES_CAPABILITIES = (
25 CAPABILITY.ENCRYPT_ECB
26 | CAPABILITY.DECRYPT_ECB
27 | CAPABILITY.ENCRYPT_CBC
28 | CAPABILITY.DECRYPT_CBC
29 )
30
31
32 @pytest.fixture(autouse=True, scope="module")
33 def prerequisites(info):
34 if info.version < (2, 3, 0):
35 pytest.skip("Symmetric keys require YubiHSM 2.3.0")
36
37
38 @pytest.fixture(scope="module", params=AES_ALGORITHMS)
39 def generated_key(session, request):
40 algorithm = request.param
41 key = SymmetricKey.generate(
42 session,
43 0,
44 "Generated AES Key %x" % algorithm,
45 0xFFFF,
46 AES_CAPABILITIES,
47 algorithm,
48 )
49 yield key
50 key.delete()
51
52
53 @pytest.fixture(scope="module", params=AES_ALGORITHMS)
54 def imported_key(session, request):
55 algorithm = request.param
56 key_to_import = os.urandom(algorithm.to_key_size())
57
58 key = SymmetricKey.put(
59 session,
60 0,
61 "Imported AES Key %x" % algorithm,
62 0xFFFF,
63 AES_CAPABILITIES,
64 algorithm,
65 key_to_import,
66 )
67 yield key, key_to_import
68 key.delete()
69
70
71 def test_import_invalid_key_size(session):
72 # Key length must match algorithm
73 with pytest.raises(ValueError):
74 SymmetricKey.put(
75 session,
76 0,
77 "Test PUT invalid key length",
78 0xFFFF,
79 AES_CAPABILITIES,
80 ALGORITHM.AES128,
81 os.urandom(24),
82 )
83
84
85 def test_import_invalid_algorithm(session):
86 # Algorithm must be AES128, AES192 or AES256
87 with pytest.raises(ValueError):
88 SymmetricKey.put(
89 session,
90 0,
91 "Test PUT invalid algorithm",
92 0xFFFF,
93 AES_CAPABILITIES,
94 ALGORITHM.AES128_CCM_WRAP,
95 os.urandom(16),
96 )
97
98
99 class TestSymmetricECB:
100 def validate_ecb(
101 self, pt: bytes, keyobj: SymmetricKey, key: Optional[bytes] = None
102 ):
103 ct = keyobj.encrypt_ecb(pt)
104 if key:
105 encryptor = Cipher(algorithms.AES(key), modes.ECB()).encryptor()
106 assert ct == encryptor.update(pt) + encryptor.finalize()
107 assert pt == keyobj.decrypt_ecb(ct)
108
109 def test_ecb_generated_key(self, generated_key):
110 pt = os.urandom(256)
111 self.validate_ecb(pt, generated_key)
112
113 def test_ecb_imported_key(self, imported_key):
114 pt = os.urandom(256)
115 self.validate_ecb(pt, *imported_key)
116
117 def test_ecb_large_pt_generated_key(self, generated_key):
118 pt = os.urandom(4096)
119 self.validate_ecb(pt, generated_key)
120
121 def test_ecb_large_pt_imported_key(self, imported_key):
122 pt = os.urandom(4096)
123 self.validate_ecb(pt, *imported_key)
124
125
126 class TestSymmetricCBC:
127 def validate_cbc(
128 self, pt: bytes, keyobj: SymmetricKey, key: Optional[bytes] = None
129 ):
130 iv = os.urandom(16)
131 ct = keyobj.encrypt_cbc(iv, pt)
132 if key:
133 encryptor = Cipher(algorithms.AES(key), modes.CBC(iv)).encryptor()
134 assert ct == encryptor.update(pt) + encryptor.finalize()
135 assert pt == keyobj.decrypt_cbc(iv, ct)
136
137 def test_cbc_generated_key(self, generated_key):
138 pt = os.urandom(256)
139 self.validate_cbc(pt, generated_key)
140
141 def test_cbc_imported_key(self, imported_key):
142 pt = os.urandom(256)
143 self.validate_cbc(pt, *imported_key)
144
145 def test_cbc_large_pt_generated_key(self, generated_key):
146 pt = os.urandom(4096)
147 self.validate_cbc(pt, generated_key)
148
149 def test_cbc_large_pt_imported_key(self, imported_key):
150 pt = os.urandom(4096)
151 self.validate_cbc(pt, *imported_key)