FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cryptoauthlib/python/cryptoauthlib/tng.py at main · rrcoco/cryptoauthlib · GitHub
rrcoco
/
cryptoauthlib
Public
forked from
MicrochipTech/cryptoauthlib
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cryptoauthlib
/
python
/
cryptoauthlib
/
tng.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
264 lines (228 loc) · 10 KB
Breadcrumbs
cryptoauthlib
/
python
/
cryptoauthlib
/
tng.py
Copy path
File metadata and controls
264 lines (228 loc) · 10 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""
TNG: classes and functions for interacting with TNG devices
"""
# (c) 2015-2019 Microchip Technology Inc. and its subsidiaries.
#
# Subject to your compliance with these terms, you may use Microchip software
# and any derivatives exclusively with Microchip products. It is your
# responsibility to comply with third party license terms applicable to your
# use of third party software (including open source software) that may
# accompany Microchip software.
#
# THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
# EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
# WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
# PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT,
# SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE
# OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
# MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
# FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL
# LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED
# THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR
# THIS SOFTWARE.
from
ctypes
import
c_int
,
c_size_t
,
byref
,
create_string_buffer
from
.
library
import
get_cryptoauthlib
,
AtcaReference
from
.
atcaenum
import
AtcaEnum
from
.
status
import
Status
# Because this module directly mirrors the C api the following is an exception to the python coding standard
# pylint: disable-msg=invalid-name
def
tng_get_device_pubkey
(
public_key
):
"""
Uses GenKey command to calculate the public key from the primary
device public key.
Args:
public_key Public key will be returned here. Format will be
the X and Y integers in big-endian format.
64 bytes for P256 curve. Expects bytearray.
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
if
not
isinstance
(
public_key
,
bytearray
):
status
=
Status
.
ATCA_BAD_PARAM
else
:
c_public_key
=
create_string_buffer
(
64
)
status
=
get_cryptoauthlib
().
tng_get_device_pubkey
(
byref
(
c_public_key
))
public_key
[
0
:]
=
bytes
(
c_public_key
.
raw
)
return
status
def
tng_atcacert_max_device_cert_size
(
max_cert_size
):
"""
Return the maximum possible certificate size in bytes for a TNG
device certificate. Certificate can be variable size, so this
gives an appropriate buffer size when reading the certificate.
Args:
max_cert_size Maximum certificate size will be returned here
in bytes. Expects AtcaReference.
Returns:
ATCACERT_E_SUCCESS on success, otherwise an error code.
"""
if
not
isinstance
(
max_cert_size
,
AtcaReference
):
status
=
Status
.
ATCA_BAD_PARAM
else
:
c_max_cert_size
=
c_size_t
(
0
)
status
=
get_cryptoauthlib
().
tng_atcacert_max_device_cert_size
(
byref
(
c_max_cert_size
))
max_cert_size
.
value
=
c_max_cert_size
.
value
return
status
def
tng_atcacert_read_device_cert
(
cert
,
cert_size
,
signer_cert
=
None
):
"""
Reads the device certificate for a TNG device.
Args:
cert Buffer to received the certificate (DER format).
Expects bytearray.
cert_size As input, the size of the cert buffer in bytes.
As output, the size of the certificate returned
in cert in bytes. Expects AtcaReference.
signer_cert If supplied, the signer public key is used from
this certificate. If set to None, the signer
public key is read from the device.
Expects bytes or None.
Returns:
ATCACERT_E_SUCCESS on success, otherwise an error code.
"""
if
not
isinstance
(
cert
,
bytearray
)
or
not
isinstance
(
cert_size
,
AtcaReference
):
status
=
Status
.
ATCA_BAD_PARAM
else
:
c_cert
=
create_string_buffer
(
cert_size
.
value
)
c_cert_size
=
c_size_t
(
cert_size
.
value
)
if
signer_cert
is
not
None
:
signer_cert
=
bytes
(
signer_cert
)
status
=
get_cryptoauthlib
().
tng_atcacert_read_device_cert
(
byref
(
c_cert
),
byref
(
c_cert_size
),
signer_cert
)
cert
[:]
=
bytes
(
c_cert
.
raw
)[
0
:
c_cert_size
.
value
]
cert_size
.
value
=
c_cert_size
.
value
return
status
def
tng_atcacert_device_public_key
(
public_key
,
cert
=
None
):
"""
Reads the device public key.
Args:
public_key Public key will be returned here. Format will be
the X and Y integers in big-endian format.
64 bytes for P256 curve. Expects bytearray.
cert If supplied, the device public key is used from
this certificate. If set to None, the device
public key is read from the device. Expects bytes or None.
Returns:
ATCACERT_E_SUCCESS on success, otherwise an error code.
"""
if
not
isinstance
(
public_key
,
bytearray
):
status
=
Status
.
ATCA_BAD_PARAM
else
:
c_public_key
=
create_string_buffer
(
64
)
if
cert
is
not
None
:
cert
=
bytes
(
cert
)
status
=
get_cryptoauthlib
().
tng_atcacert_device_public_key
(
byref
(
c_public_key
),
cert
)
public_key
[:]
=
bytes
(
c_public_key
.
raw
)
return
status
def
tng_atcacert_max_signer_cert_size
(
max_cert_size
):
"""
Return the maximum possible certificate size in bytes for a TNG
signer certificate. Certificate can be variable size, so this
gives an appropriate buffer size when reading the certificate.
Args:
max_cert_size Maximum certificate size will be returned here
in bytes. Expects AtcaReference.
Returns:
ATCACERT_E_SUCCESS on success, otherwise an error code.
"""
if
not
isinstance
(
max_cert_size
,
AtcaReference
):
status
=
Status
.
ATCA_BAD_PARAM
else
:
c_max_cert_size
=
c_size_t
(
0
)
status
=
get_cryptoauthlib
().
tng_atcacert_max_signer_cert_size
(
byref
(
c_max_cert_size
))
max_cert_size
.
value
=
c_max_cert_size
.
value
return
status
def
tng_atcacert_read_signer_cert
(
cert
,
cert_size
):
"""
Reads the signer certificate for a TNG device.
Args:
cert Buffer to received the certificate (DER format).
Expects bytearray.
cert_size As input, the size of the cert buffer in bytes.
As output, the size of the certificate returned
in cert in bytes. Expects AtcaReference.
Returns:
ATCACERT_E_SUCCESS on success, otherwise an error code.
"""
if
not
isinstance
(
cert
,
bytearray
)
or
not
isinstance
(
cert_size
,
AtcaReference
):
status
=
Status
.
ATCA_BAD_PARAM
else
:
c_cert
=
create_string_buffer
(
cert_size
.
value
)
c_cert_size
=
c_size_t
(
cert_size
.
value
)
status
=
get_cryptoauthlib
().
tng_atcacert_read_signer_cert
(
byref
(
c_cert
),
byref
(
c_cert_size
))
cert
[:]
=
bytes
(
c_cert
.
raw
)[
0
:
c_cert_size
.
value
]
cert_size
.
value
=
c_cert_size
.
value
return
status
def
tng_atcacert_signer_public_key
(
public_key
,
cert
=
None
):
"""
Reads the signer public key.
Args:
public_key Public key will be returned here. Format will be
the X and Y integers in big-endian format.
64 bytes for P256 curve. Expects bytearray.
cert If supplied, the signer public key is used from
this certificate. If set to None, the signer
public key is read from the device. Expects bytes or None.
Returns:
ATCACERT_E_SUCCESS on success, otherwise an error code.
"""
if
not
isinstance
(
public_key
,
bytearray
):
status
=
Status
.
ATCA_BAD_PARAM
else
:
c_public_key
=
create_string_buffer
(
64
)
if
cert
is
not
None
:
cert
=
bytes
(
cert
)
status
=
get_cryptoauthlib
().
tng_atcacert_signer_public_key
(
byref
(
c_public_key
),
cert
)
public_key
[:]
=
bytes
(
c_public_key
.
raw
)
return
status
def
tng_atcacert_root_cert_size
(
cert_size
):
"""
Get the size of the TNG root cert.
Args:
cert_size Certificate size will be returned here in bytes.
Expects AtcaReference.
Returns:
ATCACERT_E_SUCCESS on success, otherwise an error code.
"""
if
not
isinstance
(
cert_size
,
AtcaReference
):
status
=
Status
.
ATCA_BAD_PARAM
else
:
c_cert_size
=
c_size_t
(
0
)
status
=
get_cryptoauthlib
().
tng_atcacert_root_cert_size
(
byref
(
c_cert_size
))
cert_size
.
value
=
c_cert_size
.
value
return
status
def
tng_atcacert_root_cert
(
cert
,
cert_size
):
"""
Get the TNG root cert.
Args:
cert Buffer to received the certificate (DER format).
Expects bytearray.
cert_size As input, the size of the cert buffer in bytes.
As output, the size of the certificate returned
in cert in bytes. Expects AtcaReference.
Returns:
ATCACERT_E_SUCCESS on success, otherwise an error code.
"""
if
not
isinstance
(
cert
,
bytearray
)
or
not
isinstance
(
cert_size
,
AtcaReference
):
status
=
Status
.
ATCA_BAD_PARAM
else
:
c_cert
=
create_string_buffer
(
cert_size
.
value
)
c_cert_size
=
c_size_t
(
cert_size
.
value
)
status
=
get_cryptoauthlib
().
tng_atcacert_root_cert
(
byref
(
c_cert
),
byref
(
c_cert_size
))
cert
[:]
=
bytes
(
c_cert
.
raw
)[
0
:
c_cert_size
.
value
]
cert_size
.
value
=
c_cert_size
.
value
return
status
def
tng_atcacert_root_public_key
(
public_key
):
"""
Gets the root public key.
Args:
public_key Public key will be returned here. Format will be
the X and Y integers in big-endian format.
64 bytes for P256 curve. Expects bytearray.
Returns:
ATCACERT_E_SUCCESS on success, otherwise an error code.
"""
if
not
isinstance
(
public_key
,
bytearray
):
status
=
Status
.
ATCA_BAD_PARAM
else
:
c_public_key
=
create_string_buffer
(
64
)
status
=
get_cryptoauthlib
().
tng_atcacert_root_public_key
(
byref
(
c_public_key
))
public_key
[:]
=
bytes
(
c_public_key
.
raw
)
return
status
Back
|
FazBrowse Home
|
New Git URL