Debian Codegraph @

tests/device/test_otp.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, ERROR
16 from yubihsm.objects import OtpAeadKey, OtpData
17 from yubihsm.exceptions import YubiHsmDeviceError
18 from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
19 from cryptography.hazmat.primitives.ciphers.aead import AESCCM
20 from cryptography.hazmat.backends import default_backend
21 from dataclasses import dataclass
22 import os
23 import struct
24 import pytest
25 from typing import Mapping
26
27
28 @dataclass
29 class OtpTestVector:
30 key: bytes
31 id: bytes
32 otps: Mapping[str, OtpData]
33
34
35 # From: https://developers.yubico.com/OTP/Specifications/Test_vectors.html
36 TEST_VECTORS = [
37 OtpTestVector(
38 key=b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
39 id=b"\x01\x02\x03\x04\x05\x06",
40 otps={
41 "dvgtiblfkbgturecfllberrvkinnctnn": OtpData(1, 1, 1, 1),
42 "rnibcnfhdninbrdebccrndfhjgnhftee": OtpData(1, 2, 1, 1),
43 "iikkijbdknrrdhfdrjltvgrbkkjblcbh": OtpData(0xFFF, 1, 1, 1),
44 },
45 ),
46 OtpTestVector(
47 key=b"\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88",
48 id=b"\x88\x88\x88\x88\x88\x88",
49 otps={"dcihgvrhjeucvrinhdfddbjhfjftjdei": OtpData(0x8888, 0x88, 0x88, 0x8888)},
50 ),
51 OtpTestVector(
52 key=b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
53 id=b"\x00\x00\x00\x00\x00\x00",
54 otps={"kkkncjnvcnenkjvjgncjihljiibgbhbh": OtpData(0, 0, 0, 0)},
55 ),
56 OtpTestVector(
57 key=b"\xc4\x42\x28\x90\x65\x30\x76\xcd\xe7\x3d\x44\x9b\x19\x1b\x41\x6a",
58 id=b"\x33\xc6\x9e\x7f\x24\x9e",
59 otps={"iucvrkjiegbhidrcicvlgrcgkgurhjnj": OtpData(1, 0, 0x24, 0x13A7)},
60 ),
61 ]
62
63
64 def _crc16(data):
65 crc = 0xFFFF
66 for b in bytearray(data):
67 crc ^= b
68 for _ in range(8):
69 j = crc & 1
70 crc >>= 1
71 if j:
72 crc ^= 0x8408
73 return struct.pack("<H", ~crc & 0xFFFF)
74
75
76 def _construct_otp(aes_key, private_id, otp_data, random_number=0):
77 token = private_id + struct.pack(
78 "<HHBBH",
79 otp_data.use_counter,
80 otp_data.timestamp_low,
81 otp_data.timestamp_high,
82 otp_data.session_counter,
83 random_number,
84 )
85 token += _crc16(token)
86 cipher = Cipher(algorithms.AES(aes_key), modes.ECB(), default_backend())
87 encryptor = cipher.encryptor()
88 return encryptor.update(token) + encryptor.finalize()
89
90
91 def test_randomize_aead(session):
92 aes_key = os.urandom(16)
93 nonce_id = 0x01234567
94 key = OtpAeadKey.put(
95 session,
96 0,
97 "Test OTP Randomize AEAD",
98 1,
99 CAPABILITY.DECRYPT_OTP | CAPABILITY.RANDOMIZE_OTP_AEAD,
100 ALGORITHM.AES128_YUBICO_OTP,
101 nonce_id,
102 aes_key,
103 )
104
105 aead = key.randomize_otp_aead()
106 assert len(aead) == 36
107
108 # Decrypt generated AEAD
109 aes_ccm = AESCCM(aes_key, 8)
110 nonce, ct = aead[:6], aead[6:]
111 pt = aes_ccm.decrypt(struct.pack("<I6sBBB", nonce_id, nonce, 0, 0, 0), ct, None)
112
113 # Construct an OTP
114 otp_data = OtpData(1, 2, 3, 4)
115 otp = _construct_otp(pt[:16], pt[16:], otp_data)
116
117 # Compare YubiHSM decrypted output
118 assert key.decrypt_otp(aead, otp) == otp_data
119
120 key.delete()
121
122
123 def test_decrypt_invalid_otp(session):
124 key = OtpAeadKey.generate(
125 session,
126 0,
127 "Test OTP invalid",
128 1,
129 CAPABILITY.RANDOMIZE_OTP_AEAD | CAPABILITY.DECRYPT_OTP,
130 ALGORITHM.AES128_YUBICO_OTP,
131 0x12345678,
132 )
133 aead = key.randomize_otp_aead()
134
135 with pytest.raises(YubiHsmDeviceError) as context:
136 key.decrypt_otp(aead, os.urandom(16))
137 assert context.value.code == ERROR.INVALID_OTP
138
139 with pytest.raises(YubiHsmDeviceError) as context:
140 key.decrypt_otp(aead, os.urandom(15))
141 assert context.value.code, ERROR.WRONG_LENGTH
142
143 key.delete()
144
145
146 def test_otp_vectors(session):
147 key1 = OtpAeadKey.generate(
148 session,
149 0,
150 "Test OTP OtpTestVectors",
151 1,
152 CAPABILITY.CREATE_OTP_AEAD
153 | CAPABILITY.REWRAP_FROM_OTP_AEAD_KEY
154 | CAPABILITY.DECRYPT_OTP,
155 ALGORITHM.AES128_YUBICO_OTP,
156 0x12345678,
157 )
158 key2 = OtpAeadKey.generate(
159 session,
160 0,
161 "Test OTP OtpTestVectors",
162 1,
163 CAPABILITY.REWRAP_FROM_OTP_AEAD_KEY
164 | CAPABILITY.REWRAP_TO_OTP_AEAD_KEY
165 | CAPABILITY.DECRYPT_OTP,
166 ALGORITHM.AES192_YUBICO_OTP,
167 0x87654321,
168 )
169 keydata = os.urandom(32)
170 key3 = OtpAeadKey.put(
171 session,
172 0,
173 "Test OTP OtpTestVectors",
174 1,
175 CAPABILITY.DECRYPT_OTP
176 | CAPABILITY.CREATE_OTP_AEAD
177 | CAPABILITY.REWRAP_TO_OTP_AEAD_KEY,
178 ALGORITHM.AES256_YUBICO_OTP,
179 0x00000001,
180 keydata,
181 )
182
183 modhex = bytes.maketrans(b"cbdefghijklnrtuv", b"0123456789abcdef")
184
185 for v in TEST_VECTORS:
186 aead1 = key1.create_otp_aead(v.key, v.id)
187 aead2 = key1.rewrap_otp_aead(key2.id, aead1)
188 assert aead1 != aead2
189
190 aead3 = key2.rewrap_otp_aead(key3.id, aead2)
191 assert aead1 != aead3
192 assert aead2 != aead3
193 assert aead1 != key3.create_otp_aead(v.key, v.id)
194
195 for otp, data in v.otps.items():
196 otp_bin = bytes.fromhex(otp.translate(modhex))
197 assert data == key1.decrypt_otp(aead1, otp_bin)
198 assert data == key2.decrypt_otp(aead2, otp_bin)
199 assert data == key3.decrypt_otp(aead3, otp_bin)
200
201 key1.delete()
202 key2.delete()
203 key3.delete()
204
205
206 def test_import_invalid_key_size(session):
207 # Key length must match algorithm
208 with pytest.raises(ValueError):
209 OtpAeadKey.put(
210 session,
211 0,
212 "Test PUT invalid key length",
213 0xFFFF,
214 CAPABILITY.CREATE_OTP_AEAD
215 | CAPABILITY.REWRAP_FROM_OTP_AEAD_KEY
216 | CAPABILITY.DECRYPT_OTP,
217 ALGORITHM.AES128_YUBICO_OTP,
218 0x12345678,
219 os.urandom(24),
220 )
221
222
223 def test_import_invalid_algorithm(session):
224 # Algorithm must be AES128_YUBICO_OTP, AES192_YUBICO_OTP or AES256_YUBICO_OTP
225 with pytest.raises(ValueError):
226 OtpAeadKey.put(
227 session,
228 0,
229 "Test PUT invalid algorithm",
230 0xFFFF,
231 CAPABILITY.CREATE_OTP_AEAD
232 | CAPABILITY.REWRAP_FROM_OTP_AEAD_KEY
233 | CAPABILITY.DECRYPT_OTP,
234 ALGORITHM.AES128,
235 0x12345678,
236 os.urandom(16),
237 )