-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathdigital_key.py
169 lines (143 loc) · 5.29 KB
/
digital_key.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from enum import IntEnum
from typing import Tuple
from util.crypto import (
aes_cmac,
decrypt_aes_cbc,
encrypt_aes_cbc,
pad_mode_3,
unpad_mode_3,
)
from util.iso7816 import ISO7816Command, ISO7816Response, ISO7816Tag
COMMAND_PCB = bytes.fromhex("000000000000000000000000000000")
RESPONSE_PCB = bytes.fromhex("800000000000000000000000000000")
INITIAL_MAC_CHAINING_VALUE = bytes.fromhex("00000000000000000000000000000000")
class DigitalKeyTransactionType(IntEnum):
UNLOCK = 0x01
class DigitalKeyTransactionFlags(IntEnum):
STANDARD = 0x00
FAST = 0x01
class DigitalKeyFlow(IntEnum):
FAST = 0x00
STANDARD = 0x01
ATTESTATION = 0x02
def encrypt(plaintext: bytes, pcb: bytes, kenc: bytes, counter: int, block_size=16):
if not len(plaintext):
return plaintext
padded_plaintext = pad_mode_3(plaintext, block_size=block_size)
icv = encrypt_aes_cbc(
kenc,
iv=b"\x00" * 16,
plaintext=pcb + bytes([counter % 256]),
)
return encrypt_aes_cbc(kenc, iv=icv, plaintext=padded_plaintext)
def decrypt(ciphertext: bytes, pcb: bytes, kenc: bytes, counter: int, block_size=16):
if not len(ciphertext):
return ciphertext
icv = encrypt_aes_cbc(
kenc,
iv=b"\x00" * 16,
plaintext=pcb + bytes([counter % 256]),
)
padded_plaintext = decrypt_aes_cbc(kenc, iv=icv, ciphertext=ciphertext)
return unpad_mode_3(padded_plaintext, block_size=block_size)
class DigitalKeySecureContext:
def __init__(self, tag: ISO7816Tag, kenc, kmac, krmac):
self.tag = tag
self.kenc = kenc
self.kmac = kmac
self.krmac = krmac
self.counter = 0
self.mac_chaining_value = INITIAL_MAC_CHAINING_VALUE
def encrypt_command(self, command: ISO7816Command) -> Tuple[ISO7816Command, bytes]:
ciphertext = encrypt(
plaintext=command.data,
pcb=COMMAND_PCB,
kenc=self.kenc,
counter=self.counter,
)
calculated_rmac = aes_cmac(self.kmac, self.mac_chaining_value + ciphertext)
data = ciphertext + calculated_rmac[:8]
return (
ISO7816Command(
cla=command.cla,
ins=command.ins,
p1=command.p1,
p2=command.p2,
data=data,
le=command.le,
),
calculated_rmac,
)
def encrypt_response(
self, response: ISO7816Response
) -> Tuple[ISO7816Response, int]:
ciphertext = encrypt(
plaintext=response.data,
pcb=RESPONSE_PCB,
kenc=self.kenc,
counter=self.counter,
)
calculated_rmac = aes_cmac(self.krmac, self.mac_chaining_value + ciphertext)
data = ciphertext + calculated_rmac[:8]
return (
ISO7816Response(sw1=response.sw1, sw2=response.sw2, data=data),
self.counter + 1,
)
def decrypt_command(self, command: ISO7816Command) -> Tuple[ISO7816Command, bytes]:
ciphertext, mac = command.data[:-8], command.data[-8:]
calculated_mac = aes_cmac(self.kmac, self.mac_chaining_value + ciphertext)
assert (
mac == calculated_mac[:8]
), f"MAC Does {mac.hex()=} {calculated_mac[:8].hex()=}"
plaintext = decrypt(
ciphertext=ciphertext, pcb=COMMAND_PCB, kenc=self.kenc, counter=self.counter
)
return (
ISO7816Command(
cla=command.cla,
ins=command.ins,
p1=command.p1,
p2=command.p2,
data=plaintext,
le=command.le,
),
calculated_mac,
)
def decrypt_response(
self, response: ISO7816Response
) -> Tuple[ISO7816Response, int]:
ciphertext, rmac = response.data[:-8], response.data[-8:]
calculated_rmac = aes_cmac(self.krmac, self.mac_chaining_value + ciphertext)
assert (
rmac == calculated_rmac[:8]
), f"RMAC Does {rmac.hex()=} {calculated_rmac[:8].hex()=}"
plaintext = decrypt(
ciphertext=ciphertext,
pcb=RESPONSE_PCB,
kenc=self.kenc,
counter=self.counter,
)
return (
ISO7816Response(sw1=response.sw1, sw2=response.sw2, data=plaintext),
self.counter + 1,
)
def transceive_plain_secure(self, command: ISO7816Command) -> ISO7816Response:
"""Sends a plain command and expects a secure response"""
decrypted_response, self.counter = self.decrypt_response(
self.tag.transceive(command)
)
return decrypted_response
def transceive_secure_secure(self, command: ISO7816Command) -> ISO7816Response:
"""Sends a secure command and expects a secure response"""
encrypted_command, self.mac_chaining_value = self.encrypt_command(command)
encrypted_response = self.tag.transceive(encrypted_command)
decrypted_response, self.counter = self.decrypt_response(encrypted_response)
return decrypted_response
def transceive(self, command: ISO7816Command) -> ISO7816Response:
return self.transceive_secure_secure(command)
__all__ = (
"DigitalKeyFlow",
"DigitalKeyTransactionType",
"DigitalKeyTransactionFlags",
"DigitalKeySecureContext",
)