-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathatcab.py
More file actions
2989 lines (2463 loc) · 112 KB
/
Copy pathatcab.py
File metadata and controls
2989 lines (2463 loc) · 112 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Dynamic link library loading under ctypes and HAL initilization/release functions
"""
# Copyright (C) 2015-2026 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_uint8, c_uint32, byref, create_string_buffer, Structure, c_char, c_uint16, c_bool, c_void_p
from .status import Status
from .library import get_cryptoauthlib, AtcaReference
# Because this module directly mirrors the C api the following is an exception to the python coding standard
# pylint: disable-msg=too-many-arguments, invalid-name, too-few-public-methods
class atca_aes_cbc_ctx(Structure):
"""AES CBC Context"""
_fields_ = [("device", c_void_p),
("key_id", c_uint16),
("key_block", c_uint8),
("ciphertext", c_char*16)]
class atca_aes_cmac_ctx(Structure):
"""AES CMAC Context"""
_fields_ = [("cbc_ctx", atca_aes_cbc_ctx),
("block_size", c_uint32),
("block", c_char*16)]
class atca_aes_ctr_ctx(Structure):
"""AES CTR Context"""
_fields_ = [("key_id", c_uint16),
("key_block", c_uint8),
("iv", c_char*16),
("counter_size", c_uint8)]
class atca_sha256_ctx(Structure):
"""SHA256 context"""
_fields_ = [("total_msg_size", c_uint32),
("block_size", c_uint32),
("block", c_char*64*2)]
class atca_aes_gcm_ctx(Structure):
"""Context structure for AES GCM operations"""
_fields_ = [("key_id", c_uint16),
("key_block", c_uint8),
("cb", c_char*16),
("data_size", c_uint32),
("aad_size", c_uint32),
("h", c_char*16),
("j0", c_char*16),
("y", c_char*16),
("partial_aad", c_char*16),
("partial_aad_size", c_uint32),
("enc_cb", c_char*16),
("ciphertext_block", c_char*16)]
class atca_aes_ccm_ctx(Structure):
"""AES CCM Context"""
_fields_ = [("cbc_mac_ctx", atca_aes_cbc_ctx),
("ctr_ctx", atca_aes_ctr_ctx),
("iv_size", c_uint8),
("M", c_uint8),
("counter", c_char*16),
("partial_aad", c_char*16),
("partial_aad_size", c_uint32),
("text_size", c_uint32),
("enc_cb", c_char*16),
("data_size", c_uint32),
("ciphertext_block", c_char*16)]
class atca_aes_cbcmac_ctx(Structure):
"""AES CBCMAC Context"""
_fields_ = [("cbc_ctx", atca_aes_cbc_ctx),
("block_size", c_uint8),
("block", c_char*16)]
class atca_hmac_sha256_ctx(atca_sha256_ctx):
"""HMAC-SHA256 context"""
def atcab_init(iface_cfg):
"""
Initialize the communication stack and initializes the ATCK590 kit
Communication over USB HID and Kit Protocol by default
raise CryptoException
"""
status = get_cryptoauthlib().atcab_init(byref(iface_cfg))
return status
def atcab_release():
"""
Release the kit and the communication stack
raise CryptoException
"""
return get_cryptoauthlib().atcab_release()
def atcab_get_device():
"""
Return the global device instance
"""
return get_cryptoauthlib().atcab_get_device()
def atcab_get_device_type():
"""
Return the device type of the currently initialized device.
"""
return get_cryptoauthlib().atcab_get_device_type()
# CryptoAuthLib Basic API methods for AES command.
#
# The AES command supports 128-bit AES encryption or decryption of small
# messages or data packets in ECB mode. Also can perform GFM (Galois Field
# Multiply) calculation in support of AES-GCM.
def atcab_aes(mode, key_id, aes_in, aes_out):
"""
Compute the AES-128 encrypt, decrypt, or GFM calculation.
Args:
mode The mode for the AES command. (int)
key_id Key location. Can either be a slot number or
ATCA_TEMPKEY_KEYID for TempKey. (int)
aes_in Input data to the AES command (16 bytes). (Can be of type bytearray or bytes)
aes_out Output data from the AES command is returned here
(16 bytes). (Expects bytearray of size 16)
Returns:
Status Code
"""
c_aes_out = create_string_buffer(16)
if not isinstance(aes_out, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes(mode, key_id, bytes(aes_in), byref(c_aes_out))
aes_out[0:] = bytes(c_aes_out.raw)
return status
def atcab_aes_encrypt(key_id, key_block, plaintext, ciphertext):
"""
Perform an AES-128 encrypt operation with a key in the device.
Args:
key_id Key location. Can either be a slot number or
ATCA_TEMPKEY_KEYID for TempKey. (int)
key_block Index of the 16-byte block to use within the key
location for the actual key.(int)
plaintext Input plaintext to be encrypted (16 bytes).
(Can be of type bytearray or bytes)
ciphertext Output ciphertext is returned here (16 bytes).
(Expects bytearray of size 16)
Returns:
Status Code
"""
c_ciphertext = create_string_buffer(16)
if not isinstance(ciphertext, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_encrypt(key_id, key_block, bytes(plaintext), byref(c_ciphertext))
ciphertext[0:] = bytes(c_ciphertext.raw)
return status
def atcab_aes_decrypt(key_id, key_block, ciphertext, plaintext):
"""
Perform an AES-128 decrypt operation with a key in the device.
Args:
key_id Key location. Can either be a slot number or
ATCA_TEMPKEY_KEYID for TempKey.(int)
key_block Index of the 16-byte block to use within the key
location for the actual key. (int)
ciphertext Input ciphertext to be decrypted (16 bytes).
(bytearray or bytes)
plaintext Output plaintext is returned here (16 bytes).
(Expects bytearray of size 16)s
Returns:
Status Code
"""
c_plaintext = create_string_buffer(16)
if not isinstance(plaintext, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_decrypt(key_id, key_block, bytes(ciphertext), byref(c_plaintext))
plaintext[0:] = bytes(c_plaintext.raw)
return status
def atcab_aes_gfm(hash_key, inp, output):
"""
Perform a Galois Field Multiply (GFM) operation.
Args:
hash_key First input value (16 bytes).
(bytearray or bytes)
inp Second input value (16 bytes).
(bytearray or bytes)
output GFM result is returned here (16 bytes).
(Expects bytearray of size 16)
Returns:
Status Code
"""
c_output = create_string_buffer(16)
if not isinstance(output, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_gfm(bytes(hash_key), bytes(inp), byref(c_output))
output[0:] = bytes(c_output.raw)
return status
def atcab_aes_cbc_init(ctx, key_id, key_block, iv):
"""
Initialize context for AES CBC operation.
Args:
ctx AES CBC context to be initialized
key_id Key location. Can either be a slot number
or ATCA_TEMPKEY_KEYID for TempKey.
key_block Index of the 16-byte block to use within the
key location for the actual key.
iv Initialization vector (16 bytes). Bytearray format
Returns:
Status Code
"""
if not isinstance(iv, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_cbc_init(byref(ctx), key_id, key_block, bytes(iv))
return status
def atcab_aes_cbc_encrypt_block(ctx, plaintext, ciphertext):
"""
Encrypt a block of data using CBC mode and a key within the
ATECC608. atcab_aes_cbc_init() should be called before the
first use of this function.
Args:
ctx AES CBC context.
plaintext Plaintext to be encrypted (16 bytes).
(Bytearray or bytes)
ciphertext Encrypted data is returned here (16 bytes).
(Bytearray or bytes)
Returns:
Status code
"""
c_ciphertext = create_string_buffer(16)
if not isinstance(ciphertext, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_cbc_encrypt_block(byref(ctx), bytes(plaintext), byref(c_ciphertext))
ciphertext[0:] = bytes(c_ciphertext.raw)
return status
def atcab_aes_cbc_decrypt_block(ctx, ciphertext, plaintext):
"""
Decrypt a block of data using CBC mode and a key within the
ATECC608. atcab_aes_cbc_init() should be called before the
first use of this function.
Args:
ctx AES CBC context.
ciphertext Ciphertext to be decrypted (16 bytes).
(Bytearray or bytes)
plaintext Decrypted data is returned here (16 bytes).
(Bytearray or bytes)
Returns:
Status code
"""
c_plaintext = create_string_buffer(16)
if not isinstance(plaintext, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_cbc_decrypt_block(byref(ctx), bytes(ciphertext), byref(c_plaintext))
plaintext[0:] = bytes(c_plaintext.raw)
return status
def atcab_aes_cmac_init(ctx, key_id, key_block):
"""
Initialize a CMAC calculation using an AES-128 key in the ATECC608.
Args:
ctx AES-128 CMAC context.
key_id Key location. Can either be a slot number
or ATCA_TEMPKEY_KEYID for TempKey.
key_block Index of the 16-byte block to use within
the key location for the actual key.
Returns:
Status code
"""
status = get_cryptoauthlib().atcab_aes_cmac_init(byref(ctx), key_id, key_block)
return status
def atcab_aes_cmac_update(ctx, data, data_size):
"""
Add data to an initialized CMAC calculation.
Args:
ctx AES-128 CMAC context.
data Data to be added.
data_size Size of the data to be added in bytes.
Returns:
Status code
"""
if not isinstance(data, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_cmac_update(byref(ctx), bytes(data), data_size)
return status
def atcab_aes_cmac_finish(ctx, cmac, size):
"""
Finish a CMAC operation returning the CMAC value.
Args:
ctx AES-128 CMAC context.
cmac CMAC is returned here.
cmac_size Size of CMAC requested in bytes (max 16 bytes).
Returns:
Status code
"""
c_cmac = create_string_buffer(16)
if not isinstance(cmac, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_cmac_finish(byref(ctx), byref(c_cmac), size)
cmac[0:] = bytes(c_cmac.raw)
return status
def atcab_aes_ctr_init(ctx, key_id, key_block, counter_size, iv):
"""
Initialize context for AES CTR operation with an existing IV, which
is common when start a decrypt operation.
The IV is a combination of nonce (left-field) and big-endian counter
(right-field). The counter_size field sets the size of the counter and the
remaining bytes are assumed to be the nonce.
Args:
ctx AES CTR context to be initialized.
key_id Key location. Can either be a slot number or
ATCA_TEMPKEY_KEYID for TempKey.
key_block Index of the 16-byte block to use within the key
location for the actual key.
counter_size Size of counter in IV in bytes. 4 bytes is a
common size.
iv Initialization vector (concatenation of nonce and
counter) 16 bytes.
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
status = get_cryptoauthlib().atcab_aes_ctr_init(byref(ctx), key_id, key_block, counter_size, bytes(iv))
return status
def atcab_aes_ctr_init_rand(ctx, key_id, key_block, counter_size, iv):
"""
Initialize context for AES CTR operation with a random nonce and
counter set to 0 as the IV, which is common when starting an
encrypt operation.
The IV is a combination of nonce (left-field) and big-endian counter
(right-field). The counter_size field sets the size of the counter and the
remaining bytes are assumed to be the nonce.
Args:
ctx AES CTR context to be initialized.
key_id Key location. Can either be a slot number or
ATCA_TEMPKEY_KEYID for TempKey.
key_block Index of the 16-byte block to use within the key
location for the actual key.
counter_size Size of counter in IV in bytes. 4 bytes is a
common size.
iv Initialization vector (concatenation of nonce and
counter) is returned here (16 bytes).
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
c_iv = create_string_buffer(16)
if not isinstance(iv, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_ctr_init_rand(byref(ctx), key_id, key_block, counter_size, byref(c_iv))
iv[0:] = bytes(c_iv.raw)
return status
def atcab_aes_ctr_encrypt_block(ctx, plaintext, ciphertext):
"""
Encrypt a block of data using CTR mode and a key within the
ATECC608 device. atcab_aes_ctr_init() or atcab_aes_ctr_init_rand()
should be called before the first use of this function.
Args:
ctx AES CTR context structure.
plaintext Plaintext to be encrypted (16 bytes).
ciphertext Encrypted data is returned here (16 bytes).
Returns:
Status code
"""
c_ciphertext = create_string_buffer(16)
if not isinstance(ciphertext, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_ctr_encrypt_block(byref(ctx), bytes(plaintext), byref(c_ciphertext))
ciphertext[0:] = bytes(c_ciphertext.raw)
return status
def atcab_aes_ctr_decrypt_block(ctx, ciphertext, plaintext):
"""
Decrypt a block of data using CTR mode and a key within the
ATECC608 device. atcab_aes_ctr_init() or atcab_aes_ctr_init_rand()
should be called before the first use of this function.
Args:
ctx AES CTR context structure.
ciphertext Ciphertext to be decrypted (16 bytes).
plaintext Decrypted data is returned here (16 bytes).
Returns:
Status code
"""
c_plaintext = create_string_buffer(16)
if not isinstance(plaintext, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_ctr_decrypt_block(byref(ctx), bytes(ciphertext), byref(c_plaintext))
plaintext[0:] = bytes(c_plaintext.raw)
return status
def atcab_aes_gcm_init(ctx, key_id, key_block, iv, iv_size):
"""
Initialize context for AES GCM operation with an existing IV, which
is common when starting a decrypt operation.
Args:
ctx AES GCM context to be initialized.
key_id Key location. Can either be a slot number or
ATCA_TEMPKEY_KEYID for TempKey.
key_block Index of the 16-byte block to use within the key
location for the actual key.
iv Initialization vector.
iv_size Size of IV in bytes. Standard is 12 bytes.
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
status = get_cryptoauthlib().atcab_aes_gcm_init(byref(ctx), key_id, key_block, bytes(iv), iv_size)
return status
def atcab_aes_gcm_init_rand(ctx, key_id, key_block, rand_size, free_field, free_field_size, iv):
"""
Initialize context for AES GCM operation with a IV composed of a
random and optional fixed(free) field, which is common when
starting an encrypt operation.
Args:
ctx AES CTR context to be initialized.
key_id Key location. Can either be a slot number or
ATCA_TEMPKEY_KEYID for TempKey.
key_block Index of the 16-byte block to use within the
key location for the actual key.
rand_size Size of the random field in bytes. Minimum and
recommended size is 12 bytes. Max is 32 bytes.
free_field Fixed data to include in the IV after the
random field. Can be NULL if not used.
free_field_size Size of the free field in bytes.
iv Initialization vector is returned here. Its
size will be rand_size and free_field_size
combined.
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
c_iv = create_string_buffer(16)
if not isinstance(iv, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_gcm_init_rand(byref(ctx), key_id, key_block, rand_size,
bytes(free_field), free_field_size, byref(c_iv))
iv[0:] = bytes(c_iv.raw)
return status
def atcab_aes_gcm_aad_update(ctx, aad, aad_size):
"""
Process Additional Authenticated Data (AAD) using GCM mode and a
key within the ATECC608 device.
This can be called multiple times. atcab_aes_gcm_init() or
atcab_aes_gcm_init_rand() should be called before the first use of this
function. When there is AAD to include, this should be called before
atcab_aes_gcm_encrypt_update() or atcab_aes_gcm_decrypt_update().
Args:
ctx AES GCM context
aad Additional authenticated data to be added
aad_size Size of aad in bytes
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
status = get_cryptoauthlib().atcab_aes_gcm_aad_update(byref(ctx), bytes(aad), aad_size)
return status
def atcab_aes_gcm_encrypt_update(ctx, plaintext, plaintext_size, ciphertext):
"""
Encrypt data using GCM mode and a key within the ATECC608 device.
atcab_aes_gcm_init() or atcab_aes_gcm_init_rand() should be called
before the first use of this function.
Args:
ctx AES GCM context structure.
plaintext Plaintext to be encrypted (16 bytes).
plaintext_size Size of plaintext in bytes.
ciphertext Encrypted data is returned here.
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
c_ciphertext = create_string_buffer(plaintext_size)
if not isinstance(ciphertext, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_gcm_encrypt_update(byref(ctx),
bytes(plaintext), plaintext_size, byref(c_ciphertext))
ciphertext[0:] = bytes(c_ciphertext.raw)
return status
def atcab_aes_gcm_encrypt_finish(ctx, tag, tag_size):
"""
Complete a GCM encrypt operation returning the authentication tag.
Args:
ctx AES GCM context structure.
tag Authentication tag is returned here.
tag_size Tag size in bytes (12 to 16 bytes).
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
c_tag = create_string_buffer(tag_size)
if not isinstance(tag, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_gcm_encrypt_finish(byref(ctx), byref(c_tag), tag_size)
tag[0:] = bytes(c_tag.raw)
return status
def atcab_aes_gcm_decrypt_update(ctx, ciphertext, ciphertext_size, plaintext):
"""
Decrypt data using GCM mode and a key within the ATECC608 device.
atcab_aes_gcm_init() or atcab_aes_gcm_init_rand() should be called
before the first use of this function.
Args:
ctx AES GCM context structure.
ciphertext Ciphertext to be decrypted.
ciphertext_size Size of ciphertext in bytes.
plaintext Decrypted data is returned here.
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
c_plaintext = create_string_buffer(ciphertext_size)
if not isinstance(plaintext, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_gcm_decrypt_update(byref(ctx), bytes(ciphertext),
ciphertext_size, byref(c_plaintext))
plaintext[0:] = bytes(c_plaintext.raw)
return status
def atcab_aes_gcm_decrypt_finish(ctx, tag, tag_size, is_verified):
"""
Complete a GCM decrypt operation verifying the authentication tag.
Args:
ctx AES GCM context structure.
tag Expected authentication tag.
tag_size Size of tag in bytes (12 to 16 bytes).
is_verified Returns whether or not the tag verified.
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
if not isinstance(is_verified, AtcaReference):
status = Status.ATCA_BAD_PARAM
else:
c_is_verified = c_uint8(is_verified.value)
status = get_cryptoauthlib().atcab_aes_gcm_decrypt_finish(byref(ctx), bytes(tag),
tag_size, byref(c_is_verified))
is_verified.value = c_is_verified.value
return status
def atcab_aes_cbcmac_init(ctx, key_id, key_block):
"""
Initialize context for AES CBC-MAC operation.
Args:
ctx AES CBC-MAC context to be initialized
key_id Key location. Can either be a slot number or
ATCA_TEMPKEY_KEYID for TempKey.
key_block Index of the 16-byte block to use within the key
location for the actual key.
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
status = get_cryptoauthlib().atcab_aes_cbcmac_init(byref(ctx), key_id, key_block)
return status
def atcab_aes_cbcmac_update(ctx, data, data_size):
"""
Calculate AES CBC-MAC with key stored within ECC608A device.
atcab_aes_cbcmac_init() should be called before the first use of
this function.
Args:
ctx AES CBC-MAC context structure.
data Data to be added for AES CBC-MAC calculation. Can be
bytearray or bytes.
data_size Data length in bytes.
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
status = get_cryptoauthlib().atcab_aes_cbcmac_update(byref(ctx), bytes(data), data_size)
return status
def atcab_aes_cbcmac_finish(ctx, mac, mac_size):
"""
Finish a CBC-MAC operation returning the CBC-MAC value. If the data
provided to the atcab_aes_cbcmac_update() function has incomplete
block this function will return an error code.
Args:
ctx AES-128 CBC-MAC context.
mac CBC-MAC is returned here.
mac_size Size of CBC-MAC requested in bytes (max 16 bytes).
Returns:
ATCA_SUCCESS on success, otherwise an error code.
"""
c_mac = create_string_buffer(16)
if not isinstance(mac, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_cbcmac_finish(byref(ctx), byref(c_mac), mac_size)
mac[0:] = bytes(c_mac.raw)
return status
def atcab_aes_ccm_init(ctx, key_id, key_block, iv, iv_size, aad_size, text_size, tag_size):
"""
Initialize context for AES CCM operation with an existing IV, which
is common when starting a decrypt operation.
Args:
ctx AES CCM context to be initialized
key_id Key location. Can either be a slot number or
ATCA_TEMPKEY_KEYID for TempKey.
key_block Index of the 16-byte block to use within the key
location for the actual key.
iv Nonce to be fed into the AES CCM calculation.
iv_size Size of iv.
aad_size Size of Additional authtication data.
text_size Size of plaintext/ciphertext to be processed.
tag_size Prefered size of tag.
"""
status = get_cryptoauthlib().atcab_aes_ccm_init(byref(ctx), key_id, key_block,
bytes(iv), iv_size, aad_size, text_size, tag_size)
return status
def atcab_aes_ccm_init_rand(ctx, key_id, key_block, iv, iv_size, aad_size, text_size, tag_size):
"""
Initialize context for AES CCM operation with a random nonce
Args:
ctx AES CCM context to be initialized
key_id Key location. Can either be a slot number or
ATCA_TEMPKEY_KEYID for TempKey.
key_block Index of the 16-byte block to use within the key
location for the actual key.
iv Nonce to be fed into the AES CCM calculation.
iv_size Size of iv.
aad_size Size of Additional authtication data.
text_size Size of plaintext/ciphertext to be processed.
tag_size Prefered size of tag.
"""
c_iv = create_string_buffer(16)
if not isinstance(iv, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_ccm_init_rand(byref(ctx), key_id, key_block,
byref(c_iv), iv_size, aad_size, text_size, tag_size)
iv[0:] = bytes(c_iv.raw)
return status
def atcab_aes_ccm_aad_update(ctx, aad, aad_size):
"""
Process Additional Authenticated Data (AAD) using CCM mode and a
key within the ATECC608A device
Args:
ctx AES CCM context
aad Additional authenticated data to be added
aad_size Size of aad in bytes.
"""
status = get_cryptoauthlib().atcab_aes_ccm_aad_update(byref(ctx), bytes(aad), aad_size)
return status
def atcab_aes_ccm_aad_finish(ctx):
"""
Finish processing Additional Authenticated Data (AAD) using CCM mode.
Args:
ctx AES CCM context
"""
status = get_cryptoauthlib().atcab_aes_ccm_aad_finish(byref(ctx))
return status
def atcab_aes_ccm_encrypt_update(ctx, plaintext, plaintext_size, ciphertext):
"""
Process data using CCM mode and a key within the ATECC608A device.
atcab_aes_ccm_init() or atcab_aes_ccm_init_rand() should be called
before the first use of this function.
Args:
ctx AES CCM context structure.
plaintext Data to be processed.
plaintext_size Size of the data to be processed.
ciphertext Output data is returned here.
"""
c_ciphertext = create_string_buffer(16)
if not isinstance(ciphertext, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_ccm_encrypt_update(byref(ctx), bytes(plaintext),
plaintext_size, byref(c_ciphertext))
ciphertext[0:] = bytes(c_ciphertext.raw)
return status
def atcab_aes_ccm_decrypt_update(ctx, ciphertext, ciphertext_size, plaintext):
"""
Process data using CCM mode and a key within the ATECC608A device.
atcab_aes_ccm_init() or atcab_aes_ccm_init_rand() should be called
before the first use of this function.
Args:
ctx AES CCM context structure.
ciphertext Data to be processed.
ciphertext_size Size of the data to be processed.
plaintext Output data is returned here.
"""
c_plaintext = create_string_buffer(16)
if not isinstance(plaintext, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_ccm_decrypt_update(byref(ctx), bytes(ciphertext),
ciphertext_size, byref(c_plaintext))
plaintext[0:] = bytes(c_plaintext.raw)
return status
def atcab_aes_ccm_encrypt_finish(ctx, tag, tag_size):
"""
Complete a CCM encrypt operation returning the authentication tag.
Args:
ctx AES CCM context structure.
tag Authentication tag is returned here.
tag_size Tag size in bytes.
"""
c_tag = create_string_buffer(16)
c_tag_size = c_uint8()
if not isinstance(tag, bytearray) | isinstance(tag_size, bytearray):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_aes_ccm_encrypt_finish(byref(ctx), byref(c_tag), byref(c_tag_size))
tag[0:] = bytes(c_tag.raw)
tag_size[0] = c_tag_size.value
return status
def atcab_aes_ccm_decrypt_finish(ctx, tag, is_verified):
"""
Complete a CCM decrypt operation authenticating provided tag.
Args:
ctx AES CCM context structure.
tag Authentication tag is returned here.
is_verified Value is set to true if the tag is authenticated else
the value is set to false.
"""
if not isinstance(is_verified, AtcaReference):
status = Status.ATCA_BAD_PARAM
else:
c_is_verified = c_uint8(is_verified.value)
status = get_cryptoauthlib().atcab_aes_ccm_decrypt_finish(byref(ctx), bytes(tag), byref(c_is_verified))
is_verified.value = c_is_verified.value
return status
# CryptoAuthLib Basic API methods for CheckMAC command.
#
# The CheckMac command calculates a MAC response that would have been
# generated on a different CryptoAuthentication device and then compares the
# result with input value.
def atcab_checkmac(mode, key_id, challenge, response, other_data):
"""
Compares a MAC response with input values
Args:
mode Controls which fields within the device are used in
the message (int)
key_id Key location in the CryptoAuth device to use for the
MAC (int)
challenge Challenge data (32 bytes) (bytearray or bytes)
response MAC response data (32 bytes) (bytearray or bytes)
other_data OtherData parameter (13 bytes) (bytearray or bytes)
Returns:
Status code
"""
status = get_cryptoauthlib().atcab_checkmac(mode, key_id, bytes(challenge), bytes(response), bytes(other_data))
return status
# CryptoAuthLib Basic API methods for Counter command.
#
# The Counter command reads or increments the binary count value for one of the
# two monotonic counters.
def atcab_counter(mode, counter_id, counter_value):
"""
Compute the Counter functions
Args:
mode The mode used for the counter (int)
counter_id The counter to be used (int)
counter_value Counter value returned from device
(AtcaReference expected)
Returns:
Status code
"""
if not isinstance(counter_value, AtcaReference):
status = Status.ATCA_BAD_PARAM
else:
c_counter_value = c_uint32(counter_value.value)
status = get_cryptoauthlib().atcab_counter(mode, counter_id, byref(c_counter_value))
counter_value.value = c_counter_value.value
return status
def atcab_counter_increment(counter_id, counter_value):
"""
Increments one of the device's monotonic counters
Args:
counter_id Counter to be incremented (int)
counter_value New value of the counter is returned here
(AtcaReference expected)
Returns:
Status code
"""
if not isinstance(counter_value, AtcaReference):
status = Status.ATCA_BAD_PARAM
else:
c_counter_value = c_uint32(counter_value.value)
status = get_cryptoauthlib().atcab_counter_increment(counter_id, byref(c_counter_value))
counter_value.value = c_counter_value.value
return status
def atcab_counter_read(counter_id, counter_value):
"""
Reads one of the device's monotonic counters
Args:
counter_id Counter to be read (int)
counter_value Counter value is returned here
(AtcaReference expected)
Returns:
Status code
"""
if not isinstance(counter_value, AtcaReference):
status = Status.ATCA_BAD_PARAM
else:
c_counter_value = c_uint32(counter_value.value)
status = get_cryptoauthlib().atcab_counter_read(counter_id, byref(c_counter_value))
counter_value.value = c_counter_value.value
return status
# CryptoAuthLib Basic API methods for DeriveKey command.
#
# The DeriveKey command combines the current value of a key with the nonce
# stored in TempKey using SHA-256 and derives a new key.
def atcab_derivekey(mode, target_key, mac):
"""
Executes the DeviveKey command for deriving a new key from a
nonce (TempKey) and an existing key.
Args:
mode Bit 2 must match the value in TempKey.SourceFlag (int)
target_key Key slot to be written (int)
mac Optional 32 byte MAC used to validate operation.
(bytearray or bytes)
Returns:
Status code
"""
status = get_cryptoauthlib().atcab_derivekey(mode, target_key, bytes(mac))
return status
# CryptoAuthLib Basic API methods for ECDH command.
#
# The ECDH command implements the Elliptic Curve Diffie-Hellman algorithm to
# combine an internal private key with an external public key to calculate a
# shared secret.
def atcab_ecdh_base(mode, key_id, public_key, pms, out_nonce):
"""
Base function for generating premaster secret key using ECDH.
Args:
mode Mode to be used for ECDH computation (int)
key_id Slot of key for ECDH computation (int)
public_key Public key input to ECDH calculation. X and Y
integers in big-endian format. 64 bytes for P256
key. (bytearray or bytes)
pms ByteArray - Computed ECDH pre-master secret is returned here (32
bytes) if returned directly. Otherwise NULL.
out_nonce ByteArray - Nonce used to encrypt pre-master secret. NULL if
output encryption not used.
Returns:
Status code
"""
c_pms = create_string_buffer(32)
c_out_nonce = create_string_buffer(32)
if (not isinstance(pms, bytearray)) or (not isinstance(out_nonce, bytearray)):
status = Status.ATCA_BAD_PARAM
else:
status = get_cryptoauthlib().atcab_ecdh_base(mode, key_id, bytes(public_key), byref(c_pms), byref(c_out_nonce))
pms[0:] = bytes(c_pms.raw)
out_nonce[0:] = bytes(c_out_nonce.raw)
return status