-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathiso18013.py
71 lines (56 loc) · 2.02 KB
/
iso18013.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
import cbor2
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from util.iso7816 import ISO7816Tag
#
# Code in this file is highly inspired by https://github.com/google/identity-credential
#
READER_CONTEXT = "SKReader".encode()
ENDPOINT_CONTEXT = "SKDevice".encode()
READER_MODE = bytes.fromhex("00000000")
ENDPOINT_MODE = bytes.fromhex("00000001")
class ISO18013SecureContext:
def __init__(self, tag: ISO7816Tag, shared_secret, salt, key_length):
self.tag = tag
self.reader_counter = 1
self.reader_key = HKDF(
algorithm=hashes.SHA256(),
length=key_length,
salt=salt,
info=READER_CONTEXT,
).derive(shared_secret)
self.endpoint_counter = 1
self.endpoint_key = HKDF(
algorithm=hashes.SHA256(),
length=key_length,
salt=salt,
info=ENDPOINT_CONTEXT,
).derive(shared_secret)
@property
def reader_iv(self):
return bytes([0x00] * 4) + READER_MODE + self.reader_counter.to_bytes(4, "big")
@property
def endpoint_iv(self):
return (
bytes([0x00] * 4) + ENDPOINT_MODE + self.endpoint_counter.to_bytes(4, "big")
)
def encrypt_message_to_endpoint(self, message: bytes):
ciphertext = cbor2.dumps(
{
"data": AESGCM(self.reader_key).encrypt(
nonce=self.reader_iv, associated_data=None, data=message
)
}
)
self.reader_counter += 1
return ciphertext
def decrypt_message_from_endpoint(self, message: bytes):
cbor = cbor2.loads(message)
cbor_ciphertext = cbor["data"]
cbor_plaintext = AESGCM(self.endpoint_key).decrypt(
nonce=self.endpoint_iv, data=cbor_ciphertext, associated_data=None
)
self.endpoint_counter += 1
return cbor_plaintext
__all__ = "ISO18013SecureContext"