wolfssl-sys 4.0.0

System bindings for WolfSSL
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
/* max3266x.c
 *
 * Copyright (C) 2006-2026 wolfSSL Inc.
 *
 * This file is part of wolfSSL.
 *
 * wolfSSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * wolfSSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
 */

#ifdef HAVE_CONFIG_H
    #include <config.h>
#endif

#include <wolfssl/wolfcrypt/settings.h>

#if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD)

#include <stdint.h>
#include <stdarg.h>

#include <wolfssl/wolfcrypt/wolfmath.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/port/maxim/max3266x.h>
#include <wolfssl/wolfcrypt/hash.h>
#ifdef NO_INLINE
    #include <wolfssl/wolfcrypt/misc.h>
#else
    #define WOLFSSL_MISC_INCLUDED
    #include <wolfcrypt/src/misc.c>
#endif

#ifdef WOLF_CRYPTO_CB
    #include <wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h>
#endif

#if defined(USE_FAST_MATH) || defined(USE_INTEGER_HEAP_MATH)
    #error  MXC Not Compatible with Fast Math or Heap Math
    #include <wolfssl/wolfcrypt/tfm.h>
    #define MXC_WORD_SIZE               DIGIT_BIT
#elif defined(WOLFSSL_SP_MATH_ALL)
    #include <wolfssl/wolfcrypt/sp_int.h>
    #define MXC_WORD_SIZE               SP_WORD_SIZE
#else
    #error MXC HW port needs #define WOLFSSL_SP_MATH_ALL
#endif

/* Max size MAA can handle */
#define MXC_MAA_MAX_SIZE (2048 / MXC_WORD_SIZE)

int wc_MXC_TPU_Init(void)
{
    /* Initialize the TPU device */
    if (MXC_TPU_Init(MXC_SYS_PERIPH_CLOCK_TRNG) != 0) {
        MAX3266X_MSG("Device did not initialize");
        return RNG_FAILURE_E;
    }
    return 0;
}

int wc_MXC_TPU_Shutdown(void)
{
    /* Shutdown the TPU device */
#if defined(WOLFSSL_MAX3266X_OLD)
    MXC_TPU_Shutdown(); /* Is a void return in older SDK */
#else
    if (MXC_TPU_Shutdown(MXC_SYS_PERIPH_CLOCK_TRNG) != 0) {
        MAX3266X_MSG("Device did not shutdown");
        return RNG_FAILURE_E;
    }
#endif
    MAX3266X_MSG("TPU Hardware Shutdown");
    return 0;
}


#ifdef WOLF_CRYPTO_CB
int wc_MxcAesCryptoCb(wc_CryptoInfo* info)
{
    switch (info->cipher.type) {
#ifdef HAVE_AES_CBC
        case WC_CIPHER_AES_CBC:
            if (info->cipher.enc == 1) {
                return wc_MxcCb_AesCbcEncrypt(info->cipher.aescbc.aes,
                                                info->cipher.aescbc.out,
                                                info->cipher.aescbc.in,
                                                info->cipher.aescbc.sz);
            }
            #ifdef HAVE_AES_DECRYPT
            else if (info->cipher.enc == 0) {
                return wc_MxcCb_AesCbcDecrypt(info->cipher.aescbc.aes,
                                                info->cipher.aescbc.out,
                                                info->cipher.aescbc.in,
                                                info->cipher.aescbc.sz);
                }
            #endif
            break; /* Break out and return error */
#endif
#ifdef HAVE_AES_ECB
        case WC_CIPHER_AES_ECB:
            if (info->cipher.enc == 1) {
                return wc_MxcCb_AesEcbEncrypt(info->cipher.aesecb.aes,
                                                info->cipher.aesecb.out,
                                                info->cipher.aesecb.in,
                                                info->cipher.aesecb.sz);
            }
            #ifdef HAVE_AES_DECRYPT
            else if (info->cipher.enc == 0) {
                return wc_MxcCb_AesEcbDecrypt(info->cipher.aesecb.aes,
                                                info->cipher.aesecb.out,
                                                info->cipher.aesecb.in,
                                                info->cipher.aesecb.sz);
                }
            #endif
            break; /* Break out and return error */
#endif
        default:
            /* Is not ECB/CBC/GCM */
            return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
    }
    /* Just in case code breaks of switch statement return error */
    return BAD_FUNC_ARG;
}

#ifdef MAX3266X_SHA_CB

/* Shared callback handler: Update grows buffer, Final computes hash. */
static int wc_MxcShaCbDispatch(byte** msg, word32* used, word32* len,
                                void* heap, const byte* in, word32 inSz,
                                byte* digest, MXC_TPU_HASH_TYPE algo)
{
    if (in != NULL && digest == NULL) {
        MAX3266X_MSG("Update CB");
        return _wc_Hash_Grow(msg, used, len, in, (int)inSz, heap);
    }
    if (in == NULL && digest != NULL) {
        MAX3266X_MSG("Final CB");
        return wc_MXC_TPU_SHA_Final(msg, used, len, heap, digest, algo);
    }
    if (inSz == 0) {
        return 0; /* Don't need to Update when Size is Zero */
    }
    return BAD_FUNC_ARG;
}

int wc_MxcShaCryptoCb(wc_CryptoInfo* info)
{
    switch (info->hash.type) {
    #ifndef NO_SHA
        case WC_HASH_TYPE_SHA:
            return wc_MxcShaCbDispatch(&info->hash.sha1->msg,
                        &info->hash.sha1->used, &info->hash.sha1->len,
                        info->hash.sha1->heap, info->hash.in,
                        info->hash.inSz, info->hash.digest,
                        MXC_TPU_HASH_SHA1);
    #endif
    #ifdef WOLFSSL_SHA224
        case WC_HASH_TYPE_SHA224:
            return wc_MxcShaCbDispatch(&info->hash.sha224->msg,
                        &info->hash.sha224->used, &info->hash.sha224->len,
                        info->hash.sha224->heap, info->hash.in,
                        info->hash.inSz, info->hash.digest,
                        MXC_TPU_HASH_SHA224);
    #endif
    #ifndef NO_SHA256
        case WC_HASH_TYPE_SHA256:
            return wc_MxcShaCbDispatch(&info->hash.sha256->msg,
                        &info->hash.sha256->used, &info->hash.sha256->len,
                        info->hash.sha256->heap, info->hash.in,
                        info->hash.inSz, info->hash.digest,
                        MXC_TPU_HASH_SHA256);
    #endif
    #ifdef WOLFSSL_SHA384
        case WC_HASH_TYPE_SHA384:
            return wc_MxcShaCbDispatch(&info->hash.sha384->msg,
                        &info->hash.sha384->used, &info->hash.sha384->len,
                        info->hash.sha384->heap, info->hash.in,
                        info->hash.inSz, info->hash.digest,
                        MXC_TPU_HASH_SHA384);
    #endif
    #ifdef WOLFSSL_SHA512
        case WC_HASH_TYPE_SHA512:
            return wc_MxcShaCbDispatch(&info->hash.sha512->msg,
                        &info->hash.sha512->used, &info->hash.sha512->len,
                        info->hash.sha512->heap, info->hash.in,
                        info->hash.inSz, info->hash.digest,
                        MXC_TPU_HASH_SHA512);
    #endif
        default:
            return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
    }
}
#endif /* MAX3266X_SHA_CB */

#ifdef WOLF_CRYPTO_CB_COPY
static int wc_MxcCopyCb(wc_CryptoInfo* info)
{
    if (info == NULL || info->copy.src == NULL || info->copy.dst == NULL) {
        return BAD_FUNC_ARG;
    }

    switch (info->copy.type) {
#ifdef MAX3266X_SHA_CB
    #ifndef NO_SHA
        case WC_HASH_TYPE_SHA:
            return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst,
                        sizeof(wc_Sha),
                        &((wc_Sha*)info->copy.dst)->msg,
                        &((wc_Sha*)info->copy.dst)->used,
                        &((wc_Sha*)info->copy.dst)->len,
                        ((wc_Sha*)info->copy.dst)->heap,
                        ((wc_Sha*)info->copy.src)->heap);
    #endif
    #ifdef WOLFSSL_SHA224
        case WC_HASH_TYPE_SHA224:
            return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst,
                        sizeof(wc_Sha224),
                        &((wc_Sha224*)info->copy.dst)->msg,
                        &((wc_Sha224*)info->copy.dst)->used,
                        &((wc_Sha224*)info->copy.dst)->len,
                        ((wc_Sha224*)info->copy.dst)->heap,
                        ((wc_Sha224*)info->copy.src)->heap);
    #endif
    #ifndef NO_SHA256
        case WC_HASH_TYPE_SHA256:
            return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst,
                        sizeof(wc_Sha256),
                        &((wc_Sha256*)info->copy.dst)->msg,
                        &((wc_Sha256*)info->copy.dst)->used,
                        &((wc_Sha256*)info->copy.dst)->len,
                        ((wc_Sha256*)info->copy.dst)->heap,
                        ((wc_Sha256*)info->copy.src)->heap);
    #endif
    #ifdef WOLFSSL_SHA384
        case WC_HASH_TYPE_SHA384:
            return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst,
                        sizeof(wc_Sha384),
                        &((wc_Sha384*)info->copy.dst)->msg,
                        &((wc_Sha384*)info->copy.dst)->used,
                        &((wc_Sha384*)info->copy.dst)->len,
                        ((wc_Sha384*)info->copy.dst)->heap,
                        ((wc_Sha384*)info->copy.src)->heap);
    #endif
    #ifdef WOLFSSL_SHA512
        case WC_HASH_TYPE_SHA512:
            return wc_MXC_TPU_SHA_Copy(info->copy.src, info->copy.dst,
                        sizeof(wc_Sha512),
                        &((wc_Sha512*)info->copy.dst)->msg,
                        &((wc_Sha512*)info->copy.dst)->used,
                        &((wc_Sha512*)info->copy.dst)->len,
                        ((wc_Sha512*)info->copy.dst)->heap,
                        ((wc_Sha512*)info->copy.src)->heap);
    #endif
#endif /* MAX3266X_SHA_CB */
        default:
            return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
    }
}
#endif /* WOLF_CRYPTO_CB_COPY */

#ifdef WOLF_CRYPTO_CB_FREE
static int wc_MxcFreeCb(wc_CryptoInfo* info)
{
    if (info == NULL || info->free.obj == NULL) {
        return BAD_FUNC_ARG;
    }

    switch (info->free.type) {
#ifdef MAX3266X_SHA_CB
    #ifndef NO_SHA
        case WC_HASH_TYPE_SHA:
            wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha),
                        &((wc_Sha*)info->free.obj)->msg,
                        &((wc_Sha*)info->free.obj)->used,
                        &((wc_Sha*)info->free.obj)->len,
                        ((wc_Sha*)info->free.obj)->heap);
            return 0;
    #endif
    #ifdef WOLFSSL_SHA224
        case WC_HASH_TYPE_SHA224:
            wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha224),
                        &((wc_Sha224*)info->free.obj)->msg,
                        &((wc_Sha224*)info->free.obj)->used,
                        &((wc_Sha224*)info->free.obj)->len,
                        ((wc_Sha224*)info->free.obj)->heap);
            return 0;
    #endif
    #ifndef NO_SHA256
        case WC_HASH_TYPE_SHA256:
            wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha256),
                        &((wc_Sha256*)info->free.obj)->msg,
                        &((wc_Sha256*)info->free.obj)->used,
                        &((wc_Sha256*)info->free.obj)->len,
                        ((wc_Sha256*)info->free.obj)->heap);
            return 0;
    #endif
    #ifdef WOLFSSL_SHA384
        case WC_HASH_TYPE_SHA384:
            wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha384),
                        &((wc_Sha384*)info->free.obj)->msg,
                        &((wc_Sha384*)info->free.obj)->used,
                        &((wc_Sha384*)info->free.obj)->len,
                        ((wc_Sha384*)info->free.obj)->heap);
            return 0;
    #endif
    #ifdef WOLFSSL_SHA512
        case WC_HASH_TYPE_SHA512:
            wc_MXC_TPU_SHA_FreeCtx(info->free.obj, sizeof(wc_Sha512),
                        &((wc_Sha512*)info->free.obj)->msg,
                        &((wc_Sha512*)info->free.obj)->used,
                        &((wc_Sha512*)info->free.obj)->len,
                        ((wc_Sha512*)info->free.obj)->heap);
            return 0;
    #endif
#endif /* MAX3266X_SHA_CB */
        default:
            return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
    }
}
#endif /* WOLF_CRYPTO_CB_FREE */

/* General Callback Function to determine ALGO Type */
int wc_MxcCryptoCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
{
    int ret;
    (void)ctx;
    (void)devIdArg;

    if (info == NULL) {
        return BAD_FUNC_ARG;
    }

#ifdef DEBUG_CRYPTOCB
    wc_CryptoCb_InfoString(info);
#endif

    switch (info->algo_type) {
        case WC_ALGO_TYPE_CIPHER:
            MAX3266X_MSG("Using MXC AES HW Callback:");
            ret = wc_MxcAesCryptoCb(info); /* Determine AES HW or SW */
            break;
#ifdef MAX3266X_SHA_CB
        case WC_ALGO_TYPE_HASH:
            MAX3266X_MSG("Using MXC SHA HW Callback:");
            ret = wc_MxcShaCryptoCb(info); /* Determine SHA HW or SW */
            break;
#endif /* MAX3266X_SHA_CB */
#ifdef WOLF_CRYPTO_CB_COPY
        case WC_ALGO_TYPE_COPY:
            MAX3266X_MSG("Using MXC Copy Callback:");
            ret = wc_MxcCopyCb(info);
            break;
#endif /* WOLF_CRYPTO_CB_COPY */
#ifdef WOLF_CRYPTO_CB_FREE
        case WC_ALGO_TYPE_FREE:
            MAX3266X_MSG("Using MXC Free Callback:");
            ret = wc_MxcFreeCb(info);
            break;
#endif /* WOLF_CRYPTO_CB_FREE */
        default:
            MAX3266X_MSG("Callback not support with MXC, using SW");
            /* return this to bypass HW and use SW */
            ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
    }

    return ret;
}
#endif

/* Convert Error Codes Correctly and Report HW error when */
/* using #define MAX3266X_VERBOSE */
int wc_MXC_error(int *ret)
{
    if (ret == NULL) {
        /* In case somehow pointer to the return code is NULL */
        return BAD_FUNC_ARG;
    }
    switch (*ret) {
        case E_SUCCESS:
            return 0;

        case E_INVALID: /* Process Failed */
            MAX3266X_MSG("HW Reported: E_INVALID Error");
            *ret = WC_HW_E;
            break;

        case E_NULL_PTR:
            MAX3266X_MSG("HW Reported: E_NULL_PTR Error");
            *ret = BAD_FUNC_ARG;
            break;

        case E_BAD_PARAM:
            MAX3266X_MSG("HW Reported: E_BAD_PARAM Error");
            *ret = BAD_FUNC_ARG;
            break;

        case E_BAD_STATE:
            MAX3266X_MSG("HW Reported: E_BAD_STATE Error");
            *ret = WC_HW_E;
            break;

        default:
            MAX3266X_MSG("HW Reported an Unknown Error");
            *ret = WC_HW_E; /* If something else return HW Error */
            break;
    }
    return *ret;
}


#if defined(MAX3266X_RNG)
/* Simple call to SDK's TRNG HW */
int wc_MXC_TRNG_Random(unsigned char* output, unsigned int sz)
{
    int status;
    if (output == NULL) {
        return BAD_FUNC_ARG;
    }
    status = wolfSSL_HwRngMutexLock(); /* Lock Mutex needed since */
                                         /* calling TPU init */
    if (status != 0) {
        return status;
    }
    status = MXC_TPU_Init(MXC_SYS_PERIPH_CLOCK_TRNG);
    if (status == 0) {
        /* void return function */
        MXC_TPU_TRNG_Read(MXC_TRNG, output, sz);
        MAX3266X_MSG("TRNG Hardware Used");
    }
    else {
        MAX3266X_MSG("TRNG Device did not initialize");
        status = RNG_FAILURE_E;
    }
    wolfSSL_HwRngMutexUnLock(); /* Unlock Mutex no matter status value */
    return status;
}
#endif /* MAX3266X_RNG */

#if defined(MAX3266X_AES)
/* Generic call to the SDK's AES 1 shot Encrypt based on inputs given */
int wc_MXC_TPU_AesEncrypt(const unsigned char* in, const unsigned char* iv,
                            const unsigned char* enc_key,
                            MXC_TPU_MODE_TYPE mode, unsigned int data_size,
                            unsigned char* out, unsigned int keySize)
{
    int status;
    if (in == NULL || iv == NULL || enc_key == NULL || out == NULL) {
        return BAD_FUNC_ARG;
    }
    status = wolfSSL_HwAesMutexLock();
    MAX3266X_MSG("AES HW Encryption");
    if (status != 0) {
        MAX3266X_MSG("Hardware Mutex Failure");
        return status;
    }
    switch (keySize) {
        case MXC_AES_KEY_128_LEN:
            MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES128);
            status = MXC_TPU_Cipher_AES_Encrypt((const char*)in,
                        (const char*)iv, (const char*)enc_key,
                        MXC_TPU_CIPHER_AES128, mode, data_size, (char*)out);
            MAX3266X_MSG("AES HW Acceleration Used: 128 Bit");
            break;
        case MXC_AES_KEY_192_LEN:
            MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES192);
            status = MXC_TPU_Cipher_AES_Encrypt((const char*)in,
                        (const char*)iv, (const char*)enc_key,
                        MXC_TPU_CIPHER_AES192, mode, data_size, (char*)out);
            MAX3266X_MSG("AES HW Acceleration Used: 192 Bit");
            break;
        case MXC_AES_KEY_256_LEN:
            MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES256);
            status = MXC_TPU_Cipher_AES_Encrypt((const char*)in,
                        (const char*)iv, (const char*)enc_key,
                        MXC_TPU_CIPHER_AES256, mode, data_size, (char*)out);
            MAX3266X_MSG("AES HW Acceleration Used: 256 Bit");
            break;
        default:
            MAX3266X_MSG("AES HW ERROR: Length Not Supported");
            wolfSSL_HwAesMutexUnLock();
            return BAD_FUNC_ARG;
    }
    wolfSSL_HwAesMutexUnLock();
    if (status != 0) {
        MAX3266X_MSG("AES HW Acceleration Error Occurred");
        return WC_HW_E;
    }
    return status;
}


/* Encrypt AES Crypto Callbacks*/
#if defined(WOLF_CRYPTO_CB)

#ifdef HAVE_AES_ECB
int wc_MxcCb_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
    int status;
    word32 keySize;

    if ((in == NULL) || (out == NULL) || (aes == NULL)) {
        return BAD_FUNC_ARG;
    }

    status = wc_AesGetKeySize(aes, &keySize);
    if (status != 0) {
        return status;
    }

    status = wc_MXC_TPU_AesEncrypt(in, (byte*)aes->reg, (byte*)aes->devKey,
                                        MXC_TPU_MODE_ECB, sz, out, keySize);

    return status;
}
#endif /* HAVE_AES_ECB */

#ifdef HAVE_AES_CBC
int wc_MxcCb_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
    word32 keySize;
    int status;
    byte *iv;

    if ((in == NULL) || (out == NULL) || (aes == NULL)) {
        return BAD_FUNC_ARG;
    }

    /* Always enforce a length check */
    if (sz % WC_AES_BLOCK_SIZE) {
    #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
        return BAD_LENGTH_E;
    #else
        return BAD_FUNC_ARG;
    #endif
    }
    if (sz == 0) {
        return 0;
    }

    iv = (byte*)aes->reg;
    status = wc_AesGetKeySize(aes, &keySize);
    if (status != 0) {
        return status;
    }

    status = wc_MXC_TPU_AesEncrypt(in, iv, (byte*)aes->devKey,
                                    MXC_TPU_MODE_CBC, sz, out,
                                    (unsigned int)keySize);
    /* store iv for next call */
    if (status == 0) {
        XMEMCPY(iv, out + sz - WC_AES_BLOCK_SIZE, WC_AES_BLOCK_SIZE);
    }
    return status;
}
#endif /* HAVE_AES_CBC */
#endif /* WOLF_CRYPTO_CB */

#ifdef HAVE_AES_DECRYPT
/* Generic call to the SDK's AES 1 shot decrypt based on inputs given */
int wc_MXC_TPU_AesDecrypt(const unsigned char* in, const unsigned char* iv,
                            const unsigned char* dec_key,
                            MXC_TPU_MODE_TYPE mode, unsigned int data_size,
                            unsigned char* out, unsigned int keySize)
{
    int status;
    if (in == NULL || iv == NULL || dec_key == NULL || out == NULL) {
        return BAD_FUNC_ARG;
    }
    status = wolfSSL_HwAesMutexLock();
    if (status != 0) {
        return status;
    }
    switch (keySize) {
        case MXC_AES_KEY_128_LEN:
            MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES128);
            status = MXC_TPU_Cipher_AES_Decrypt((const char*)in,
                        (const char*)iv, (const char*)dec_key,
                        MXC_TPU_CIPHER_AES128, mode, data_size, (char*)out);
            MAX3266X_MSG("AES HW Acceleration Used: 128 Bit");
            break;
        case MXC_AES_KEY_192_LEN:
            MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES192);
            status = MXC_TPU_Cipher_AES_Decrypt((const char*)in,
                        (const char*)iv, (const char*)dec_key,
                        MXC_TPU_CIPHER_AES192, mode, data_size, (char*)out);
            MAX3266X_MSG("AES HW Acceleration Used: 192 Bit");
            break;
        case MXC_AES_KEY_256_LEN:
            MXC_TPU_Cipher_Config(mode, MXC_TPU_CIPHER_AES256);
            status = MXC_TPU_Cipher_AES_Decrypt((const char*)in,
                        (const char*)iv, (const char*)dec_key,
                        MXC_TPU_CIPHER_AES256, mode, data_size, (char*)out);
            MAX3266X_MSG("AES HW Acceleration Used: 256 Bit");
            break;
        default:
            MAX3266X_MSG("AES HW ERROR: Length Not Supported");
            wolfSSL_HwAesMutexUnLock();
            return BAD_FUNC_ARG;
    }
    wolfSSL_HwAesMutexUnLock();
    if (status != 0) {
        MAX3266X_MSG("AES HW Acceleration Error Occurred");
        return WC_HW_E;
    }
    return status;
}

/* Decrypt Aes Crypto Callbacks*/
#if defined(WOLF_CRYPTO_CB)

#ifdef HAVE_AES_ECB
int wc_MxcCb_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
    int status;
    word32 keySize;

    if ((in == NULL) || (out == NULL) || (aes == NULL)) {
        return BAD_FUNC_ARG;
    }

    status = wc_AesGetKeySize(aes, &keySize);
    if (status != 0) {
        return status;
    }

    status = wc_MXC_TPU_AesDecrypt(in, (byte*)aes->reg, (byte*)aes->devKey,
                                        MXC_TPU_MODE_ECB, sz, out, keySize);

    return status;
}
#endif /* HAVE_AES_ECB */

#ifdef HAVE_AES_CBC
int wc_MxcCb_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
    word32 keySize;
    int status;
    byte *iv;
    byte temp_block[WC_AES_BLOCK_SIZE];

    if ((in == NULL) || (out == NULL) || (aes == NULL)) {
        return BAD_FUNC_ARG;
    }

    /* Always enforce a length check */
    if (sz % WC_AES_BLOCK_SIZE) {
    #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
        return BAD_LENGTH_E;
    #else
        return BAD_FUNC_ARG;
    #endif
    }
    if (sz == 0) {
        return 0;
    }

    iv = (byte*)aes->reg;
    status = wc_AesGetKeySize(aes, &keySize);
    if (status != 0) {
        return status;
    }

    /* get IV for next call */
    XMEMCPY(temp_block, in + sz - WC_AES_BLOCK_SIZE, WC_AES_BLOCK_SIZE);
    status = wc_MXC_TPU_AesDecrypt(in, iv, (byte*)aes->devKey,
                                    MXC_TPU_MODE_CBC, sz, out,
                                    keySize);

    /* store iv for next call */
    if (status == 0) {
        XMEMCPY(iv, temp_block, WC_AES_BLOCK_SIZE);
    }
    return status;
}
#endif /* HAVE_AES_CBC */
#endif /* WOLF_CRYPTO_CB */
#endif /* HAVE_AES_DECRYPT */
#endif /* MAX3266X_AES */

#if defined(MAX3266X_SHA) || defined(MAX3266X_SHA_CB)

/* Check for empty message and provide pre-computed digest if so */
/* Returns 1 if empty (digest filled), 0 if needs hardware processing */
int wc_MXC_TPU_SHA_GetDigest(const unsigned char* msg, unsigned int msgSz,
                                        unsigned char* digest,
                                        MXC_TPU_HASH_TYPE algo)
{
    if (digest == NULL) {
        return BAD_FUNC_ARG;
    }
    if (msg == NULL && msgSz == 0) {
        switch (algo) {
            #ifndef NO_SHA
            case MXC_TPU_HASH_SHA1:
                XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA1, WC_SHA_DIGEST_SIZE);
                break;
            #endif /* NO_SHA */
            #ifdef WOLFSSL_SHA224
            case MXC_TPU_HASH_SHA224:
                XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA224, WC_SHA224_DIGEST_SIZE);
                break;
            #endif /* WOLFSSL_SHA224 */
            #ifndef NO_SHA256
            case MXC_TPU_HASH_SHA256:
                XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA256, WC_SHA256_DIGEST_SIZE);
                break;
            #endif /* NO_SHA256 */
            #ifdef WOLFSSL_SHA384
            case MXC_TPU_HASH_SHA384:
                XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA384, WC_SHA384_DIGEST_SIZE);
                break;
            #endif /* WOLFSSL_SHA384 */
            #ifdef WOLFSSL_SHA512
            case MXC_TPU_HASH_SHA512:
                XMEMCPY(digest, MXC_EMPTY_DIGEST_SHA512, WC_SHA512_DIGEST_SIZE);
                break;
            #endif /* WOLFSSL_SHA512 */
            default:
                return BAD_FUNC_ARG;
        }
        return 1; /* True: empty digest provided */
    }
    return 0; /* False: needs hardware processing */
}

/* Compute hash from accumulated message using TPU hardware */
int wc_MXC_TPU_SHA_GetHash(const unsigned char* msg, unsigned int msgSz,
                                unsigned char* digest,
                                MXC_TPU_HASH_TYPE algo)
{
    int status;
    if (digest == NULL || (msg == NULL && msgSz != 0)) {
        return BAD_FUNC_ARG;
    }
    status = wc_MXC_TPU_SHA_GetDigest(msg, msgSz, digest, algo);
    /* True Case that msg is an empty string */
    if (status == 1) {
        /* Hardware cannot handle the case of an empty string */
        /* so in the case of this we will provide the hash via software */
        return 0;
    }
    /* False Case where msg needs to be processed */
    else if (status == 0) {
        status = wolfSSL_HwHashMutexLock(); /* Set Mutex */
        if (status != 0) { /* Mutex Call Check */
            return status;
        }
        MXC_TPU_Init(MXC_SYS_PERIPH_CLOCK_TPU);
        MXC_TPU_Hash_Config(algo);
        status = MXC_TPU_Hash_SHA((const char *)msg, algo, msgSz,
                                         (char *)digest);
        MAX3266X_MSG("SHA HW Acceleration Used");
        wolfSSL_HwHashMutexUnLock(); /* Release Mutex */
        if (wc_MXC_error(&status) != 0) {
            MAX3266X_MSG("SHA HW Error Occurred");
            return status;
        }
    }
    /* Error Occurred */
    return status;
}

/* Free HASH_KEEP message buffer and reset fields */
void wc_MXC_TPU_SHA_Free(byte** msg, word32* used, word32* len, void* heap)
{
    if (msg == NULL) {
        return;
    }
    if (*msg != NULL) {
        XFREE(*msg, heap, DYNAMIC_TYPE_TMP_BUFFER);
        *msg = NULL;
    }
    if (used != NULL) {
        *used = 0;
    }
    if (len != NULL) {
        *len = 0;
    }
}

/* Free HASH_KEEP message buffer and zero the full SHA context struct */
void wc_MXC_TPU_SHA_FreeCtx(void* ctx, word32 ctxSz, byte** msg, word32* used,
                                word32* len, void* heap)
{
    if (ctx == NULL) {
        return;
    }
    wc_MXC_TPU_SHA_Free(msg, used, len, heap);
    XMEMSET(ctx, 0, ctxSz);
}

/* Copy SHA context struct and deep copy the HASH_KEEP message buffer.
 * Frees any existing dst msg buffer to prevent memory leaks when copying
 * into an already-used context. */
int wc_MXC_TPU_SHA_Copy(void* src, void* dst, word32 ctxSz,
                                byte** dstMsg, word32* dstUsed, word32* dstLen,
                                void* dstHeap, void* srcHeap)
{
    byte* srcBuf = NULL;

    if (src == NULL || dst == NULL || dstMsg == NULL ||
        dstUsed == NULL || dstLen == NULL || ctxSz == 0) {
        return BAD_FUNC_ARG;
    }

    /* Free existing dst msg buffer using dst's original heap */
    wc_MXC_TPU_SHA_Free(dstMsg, dstUsed, dstLen, dstHeap);

    /* Shallow copy the full context struct */
    XMEMCPY(dst, src, ctxSz);

    /* Deep copy src msg buffer if present. Since dstMsg points into the dst
     * struct, the XMEMCPY above overwrites it with the src's msg pointer.
     * Save that pointer, allocate a new buffer for dst, and copy the data.
     * Do NOT move srcBuf assignment before XMEMCPY - it must capture the
     * src msg pointer that lands in *dstMsg after the shallow copy. */
    if (*dstMsg != NULL) {
        srcBuf = *dstMsg;
        *dstMsg = (byte*)XMALLOC(*dstLen, srcHeap, DYNAMIC_TYPE_TMP_BUFFER);
        if (*dstMsg == NULL) {
            return MEMORY_E;
        }
        XMEMCPY(*dstMsg, srcBuf, *dstUsed);
    }

    return 0;
}

/* Compute hash, free message buffer, and reset HASH_KEEP fields */
int wc_MXC_TPU_SHA_Final(unsigned char** msg, unsigned int* used,
                                    unsigned int* len, void* heap,
                                    unsigned char* digest,
                                    MXC_TPU_HASH_TYPE algo)
{
    int status;
    if (msg == NULL || used == NULL || len == NULL || digest == NULL) {
        return BAD_FUNC_ARG;
    }
    status = wc_MXC_TPU_SHA_GetHash(*msg, *used, digest, algo);
    wc_MXC_TPU_SHA_Free(msg, used, len, heap);
    return status;
}

#ifndef MAX3266X_SHA_CB
#if !defined(NO_SHA)

WOLFSSL_API int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
{
    if (sha == NULL) {
        return BAD_FUNC_ARG;
    }
    (void)devId;
    XMEMSET(sha, 0, sizeof(*sha));
    sha->heap = heap;
    return 0;
}

WOLFSSL_API int wc_ShaUpdate(wc_Sha* sha, const unsigned char* data,
                                        unsigned int len)
{
    if (sha == NULL || (data == NULL && len > 0)) {
        return BAD_FUNC_ARG;
    }
    return _wc_Hash_Grow(&sha->msg, &sha->used, &sha->len,
                                        data, (int)len, sha->heap);
}

WOLFSSL_API int wc_ShaFinal(wc_Sha* sha, unsigned char* hash)
{
    if (sha == NULL || hash == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_Final(&sha->msg, &sha->used, &sha->len,
                                        sha->heap, hash, MXC_TPU_HASH_SHA1);
}

WOLFSSL_API int wc_ShaGetHash(wc_Sha* sha, unsigned char* hash)
{
    if (sha == NULL || hash == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_GetHash((const unsigned char*)sha->msg,
                                        sha->used, hash, MXC_TPU_HASH_SHA1);
}

WOLFSSL_API int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
{
    if (src == NULL || dst == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha),
                                        &dst->msg, &dst->used, &dst->len,
                                        dst->heap, src->heap);
}

WOLFSSL_API void wc_ShaFree(wc_Sha* sha)
{
    if (sha == NULL) {
        return;
    }
    wc_MXC_TPU_SHA_FreeCtx(sha, sizeof(wc_Sha), &sha->msg, &sha->used,
                                        &sha->len, sha->heap);
}

#endif /* NO_SHA */

#if defined(WOLFSSL_SHA224)

WOLFSSL_API int wc_InitSha224_ex(wc_Sha224* sha224, void* heap, int devId)
{
    if (sha224 == NULL) {
        return BAD_FUNC_ARG;
    }
    (void)devId;
    XMEMSET(sha224, 0, sizeof(*sha224));
    sha224->heap = heap;
    return 0;
}

WOLFSSL_API int wc_InitSha224(wc_Sha224* sha224)
{
    return wc_InitSha224_ex(sha224, NULL, INVALID_DEVID);
}

WOLFSSL_API int wc_Sha224Update(wc_Sha224* sha224, const unsigned char* data,
                                        unsigned int len)
{
    if (sha224 == NULL || (data == NULL && len > 0)) {
        return BAD_FUNC_ARG;
    }
    return _wc_Hash_Grow(&sha224->msg, &sha224->used, &sha224->len,
                                        data, (int)len, sha224->heap);
}

WOLFSSL_API int wc_Sha224Final(wc_Sha224* sha224, unsigned char* hash)
{
    if (sha224 == NULL || hash == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_Final(&sha224->msg, &sha224->used, &sha224->len,
                                        sha224->heap, hash,
                                        MXC_TPU_HASH_SHA224);
}

WOLFSSL_API int wc_Sha224GetHash(wc_Sha224* sha224, unsigned char* hash)
{
    if (sha224 == NULL || hash == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_GetHash((const unsigned char*)sha224->msg,
                                        sha224->used, hash,
                                        MXC_TPU_HASH_SHA224);
}

WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst)
{
    if (src == NULL || dst == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha224),
                                        &dst->msg, &dst->used, &dst->len,
                                        dst->heap, src->heap);
}

WOLFSSL_API void wc_Sha224Free(wc_Sha224* sha224)
{
    if (sha224 == NULL) {
        return;
    }
    wc_MXC_TPU_SHA_FreeCtx(sha224, sizeof(wc_Sha224), &sha224->msg,
                                        &sha224->used, &sha224->len,
                                        sha224->heap);
}

#endif /* WOLFSSL_SHA224 */

#if !defined(NO_SHA256)

WOLFSSL_API int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
{
    if (sha256 == NULL) {
        return BAD_FUNC_ARG;
    }
    (void)devId;
    XMEMSET(sha256, 0, sizeof(*sha256));
    sha256->heap = heap;
    return 0;
}

WOLFSSL_API int wc_InitSha256(wc_Sha256* sha256)
{
    return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID);
}

WOLFSSL_API int wc_Sha256Update(wc_Sha256* sha256, const unsigned char* data,
                                        unsigned int len)
{
    if (sha256 == NULL || (data == NULL && len > 0)) {
        return BAD_FUNC_ARG;
    }
    return _wc_Hash_Grow(&sha256->msg, &sha256->used, &sha256->len,
                                        data, (int)len, sha256->heap);
}

WOLFSSL_API int wc_Sha256Final(wc_Sha256* sha256, unsigned char* hash)
{
    if (sha256 == NULL || hash == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_Final(&sha256->msg, &sha256->used, &sha256->len,
                                        sha256->heap, hash,
                                        MXC_TPU_HASH_SHA256);
}

WOLFSSL_API int wc_Sha256GetHash(wc_Sha256* sha256, unsigned char* hash)
{
    if (sha256 == NULL || hash == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_GetHash((const unsigned char*)sha256->msg,
                                        sha256->used, hash,
                                        MXC_TPU_HASH_SHA256);
}

WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
{
    if (src == NULL || dst == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha256),
                                        &dst->msg, &dst->used, &dst->len,
                                        dst->heap, src->heap);
}

WOLFSSL_API void wc_Sha256Free(wc_Sha256* sha256)
{
    if (sha256 == NULL) {
        return;
    }
    wc_MXC_TPU_SHA_FreeCtx(sha256, sizeof(wc_Sha256), &sha256->msg,
                                        &sha256->used, &sha256->len,
                                        sha256->heap);
}

#endif /* NO_SHA256 */

#if defined(WOLFSSL_SHA384)

WOLFSSL_API int wc_InitSha384_ex(wc_Sha384* sha384, void* heap, int devId)
{
    if (sha384 == NULL) {
        return BAD_FUNC_ARG;
    }
    (void)devId;
    XMEMSET(sha384, 0, sizeof(*sha384));
    sha384->heap = heap;
    return 0;
}

WOLFSSL_API int wc_InitSha384(wc_Sha384* sha384)
{
    return wc_InitSha384_ex(sha384, NULL, INVALID_DEVID);
}

WOLFSSL_API int wc_Sha384Update(wc_Sha384* sha384, const unsigned char* data,
                                        unsigned int len)
{
    if (sha384 == NULL || (data == NULL && len > 0)) {
        return BAD_FUNC_ARG;
    }
    return _wc_Hash_Grow(&sha384->msg, &sha384->used, &sha384->len,
                                        data, (int)len, sha384->heap);
}

WOLFSSL_API int wc_Sha384Final(wc_Sha384* sha384, unsigned char* hash)
{
    if (sha384 == NULL || hash == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_Final(&sha384->msg, &sha384->used, &sha384->len,
                                        sha384->heap, hash,
                                        MXC_TPU_HASH_SHA384);
}

WOLFSSL_API int wc_Sha384GetHash(wc_Sha384* sha384, unsigned char* hash)
{
    if (sha384 == NULL || hash == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_GetHash((const unsigned char*)sha384->msg,
                                        sha384->used, hash,
                                        MXC_TPU_HASH_SHA384);
}

WOLFSSL_API int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
{
    if (src == NULL || dst == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha384),
                                        &dst->msg, &dst->used, &dst->len,
                                        dst->heap, src->heap);
}

WOLFSSL_API void wc_Sha384Free(wc_Sha384* sha384)
{
    if (sha384 == NULL) {
        return;
    }
    wc_MXC_TPU_SHA_FreeCtx(sha384, sizeof(wc_Sha384), &sha384->msg,
                                        &sha384->used, &sha384->len,
                                        sha384->heap);
}

#endif /* WOLFSSL_SHA384 */

#if defined(WOLFSSL_SHA512)

WOLFSSL_API int wc_InitSha512_ex(wc_Sha512* sha512, void* heap, int devId)
{
    if (sha512 == NULL) {
        return BAD_FUNC_ARG;
    }
    (void)devId;
    XMEMSET(sha512, 0, sizeof(*sha512));
    sha512->heap = heap;
    return 0;
}

WOLFSSL_API int wc_InitSha512(wc_Sha512* sha512)
{
    return wc_InitSha512_ex(sha512, NULL, INVALID_DEVID);
}

WOLFSSL_API int wc_Sha512Update(wc_Sha512* sha512, const unsigned char* data,
                                        unsigned int len)
{
    if (sha512 == NULL || (data == NULL && len > 0)) {
        return BAD_FUNC_ARG;
    }
    return _wc_Hash_Grow(&sha512->msg, &sha512->used, &sha512->len,
                                        data, (int)len, sha512->heap);
}

WOLFSSL_API int wc_Sha512Final(wc_Sha512* sha512, unsigned char* hash)
{
    if (sha512 == NULL || hash == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_Final(&sha512->msg, &sha512->used, &sha512->len,
                                        sha512->heap, hash,
                                        MXC_TPU_HASH_SHA512);
}

WOLFSSL_API int wc_Sha512GetHash(wc_Sha512* sha512, unsigned char* hash)
{
    if (sha512 == NULL || hash == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_GetHash((const unsigned char*)sha512->msg,
                                        sha512->used, hash,
                                        MXC_TPU_HASH_SHA512);
}

WOLFSSL_API int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
{
    if (src == NULL || dst == NULL) {
        return BAD_FUNC_ARG;
    }
    return wc_MXC_TPU_SHA_Copy(src, dst, sizeof(wc_Sha512),
                                        &dst->msg, &dst->used, &dst->len,
                                        dst->heap, src->heap);
}

WOLFSSL_API void wc_Sha512Free(wc_Sha512* sha512)
{
    if (sha512 == NULL) {
        return;
    }
    wc_MXC_TPU_SHA_FreeCtx(sha512, sizeof(wc_Sha512), &sha512->msg,
                                        &sha512->used, &sha512->len,
                                        sha512->heap);
}

#endif /* WOLFSSL_SHA512 */
#endif /* !MAX3266X_SHA_CB*/
#endif /* MAX3266X_SHA || MAX3266X_SHA_CB */

#if defined(MAX3266X_MATH)

/* Sets mutex and initializes hardware according to needed operation size */
int wc_MXC_MAA_init(unsigned int len)
{
    int status;
    MAX3266X_MSG("Setting Hardware Mutex and Starting MAA");
    status = wolfSSL_HwPkMutexLock();
    if (status == 0) {
        status = MXC_TPU_MAA_Init(len);
    }
    return wc_MXC_error(&status); /* Return Status of Init */
}

/* Unlocks mutex and performs graceful shutdown of hardware */
int wc_MXC_MAA_Shutdown(void)
{
    int status;
    MAX3266X_MSG("Unlocking Hardware Mutex and Shutting Down MAA");
    status = MXC_TPU_MAA_Shutdown();
    if (status == E_BAD_PARAM) { /* Miss leading, Send WC_HW_ERROR */
                                /* This is returned when MAA cannot stop */
        status = WC_HW_E;
    }
    wolfSSL_HwPkMutexUnLock(); /* Always call Unlock in shutdown */
    return wc_MXC_error(&status);
}

/* Update used number for mp_int struct for results */
int wc_MXC_MAA_adjustUsed(unsigned int *array, unsigned int length)
{
    int i, lastNonZeroIndex;
    lastNonZeroIndex = -1; /* Track the last non-zero index */
    for (i = 0; i < length; i++) {
        if (array[i] != 0) {
            lastNonZeroIndex = i;
        }
    }
    return (lastNonZeroIndex + 1);
}

/* Determines the size of operation that needs to happen */
unsigned int wc_MXC_MAA_Largest(unsigned int count, ...)
{
    va_list args;
    int i;
    unsigned int largest, num;
    va_start(args, count);
    largest = va_arg(args, unsigned int);
    for (i = 1; i < count; i++) {
        num = va_arg(args, unsigned int);
        if (num > largest) {
            largest = num;
        }
    }
    va_end(args);
    return largest;
}

/* Determines if we need to fallback to Software */
int wc_MXC_MAA_Fallback(unsigned int count, ...)
{
    va_list args;
    int num, i;
    va_start(args, count);
    for (i = 0; i < count; i++) {
        num = va_arg(args, unsigned int);
        if (num > MXC_MAA_MAX_SIZE) {
            MAX3266X_MSG("HW Falling Back to Software");
            return 1;
        }
    }
    va_end(args);
    MAX3266X_MSG("HW Can Handle Input");
    return 0;
}

/* Have to zero pad the entire data array up to 256 bytes(2048 bits) */
/* If length > 256 bytes then error */
int wc_MXC_MAA_zeroPad(mp_int* multiplier, mp_int* multiplicand,
                            mp_int* exp, mp_int* mod, mp_int* result,
                            MXC_TPU_MAA_TYPE clc, unsigned int length)
{
    mp_digit* zero_tmp;
    MAX3266X_MSG("Zero Padding Buffers for Hardware");
    if (length > MXC_MAA_MAX_SIZE) {
        MAX3266X_MSG("Hardware cannot exceed 2048 bit input");
        return BAD_FUNC_ARG;
    }
    if ((result == NULL) || (multiplier == NULL) || (multiplicand == NULL) ||
            ((exp == NULL) && (clc == MXC_TPU_MAA_EXP)) || (mod == NULL)) {
        return BAD_FUNC_ARG;
    }

    /* Create an array to compare values to to check edge for error edge case */
    zero_tmp = (mp_digit*)XMALLOC(multiplier->size*sizeof(mp_digit), NULL,
                                    DYNAMIC_TYPE_TMP_BUFFER);
    if (zero_tmp == NULL) {
        MAX3266X_MSG("NULL pointer found after XMALLOC call");
        return MEMORY_E;
    }
    XMEMSET(zero_tmp, 0x00, multiplier->size*sizeof(mp_digit));

    /* Check for invalid arguments before padding */
    switch ((char)clc) {
        case MXC_TPU_MAA_EXP:
            /* Cannot be 0 for a^e mod m operation */
            if (XMEMCMP(zero_tmp, exp, (exp->used*sizeof(mp_digit))) == 0) {
                XFREE(zero_tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
                MAX3266X_MSG("Cannot use Value 0 for Exp");
                return BAD_FUNC_ARG;
            }

            /* Pad out rest of data if used != length to ensure no */
            /* garbage is used in calculation */
            if ((exp != NULL) && (clc == MXC_TPU_MAA_EXP)) {
                if ((exp->dp != NULL) && (exp->used < length)) {
                    MAX3266X_MSG("Zero Padding Exp Buffer");
                    XMEMSET(exp->dp + exp->used, 0x00,
                            sizeof(int) *(length - exp->used));
                }
            }

        /* Fall through to check mod is not 0 */
        case MXC_TPU_MAA_SQ:
        case MXC_TPU_MAA_MUL:
        case MXC_TPU_MAA_SQMUL:
        case MXC_TPU_MAA_ADD:
        case MXC_TPU_MAA_SUB:
            /* Cannot be 0 for mod m value */
            if (XMEMCMP(zero_tmp, mod, (exp->used*sizeof(mp_digit))) == 0) {
                XFREE(zero_tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
                MAX3266X_MSG("Cannot use Value 0 for Exp");
                return BAD_FUNC_ARG;
            }

            /* Pad out rest of data if used != length to ensure no */
            /* garbage is used in calculation */
            if ((multiplier->dp != NULL) && (multiplier->used < length)) {
                MAX3266X_MSG("Zero Padding Multiplier Buffer");
                XMEMSET(multiplier->dp + multiplier->used, 0x00,
                    sizeof(int) * (length - multiplier->used));
            }
            if ((multiplicand->dp != NULL) && (multiplicand->used < length)) {
                MAX3266X_MSG("Zero Padding Multiplicand Buffer");
                XMEMSET(multiplicand->dp + multiplicand->used, 0x00,
                    sizeof(int) * (length - multiplicand->used));
            }
            if ((mod->dp != NULL) && (mod->used < length)) {
                MAX3266X_MSG("Zero Padding Mod Buffer");
                XMEMSET(mod->dp + mod->used, 0x00,
                            sizeof(int) *(length - mod->used));
            }
            break;
        default:
            /* Free the zero array used to check values */
            XFREE(zero_tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
            return BAD_FUNC_ARG; /* Invalid clc given */
    }
    /* Free the zero array used to check values */
    XFREE(zero_tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);

    /* Make sure result is 0 padded */
    if (result->dp != NULL) {
        ForceZero(result->dp, sizeof(int)*(length));
        result->used = length;
    }
    return 0;
}



/* General Control Over MAA Hardware to handle all needed Cases */
int wc_MXC_MAA_math(mp_int* multiplier, mp_int* multiplicand, mp_int* exp,
                                mp_int* mod, mp_int* result,
                                MXC_TPU_MAA_TYPE clc)
{
    int ret;
    int length;
    mp_int* result_tmp_ptr;
    mp_int result_tmp;
    if (multiplier == NULL || multiplicand == NULL || mod == NULL ||
            (exp == NULL && clc == MXC_TPU_MAA_EXP) || result == NULL) {
        return BAD_FUNC_ARG;
    }

    /* Check if result shares struct pointer */
    if ((multiplier == result) || (multiplicand == result) || (exp == result) ||
            (mod == result)) {
            MAX3266X_MSG("Creating Temp Result Buffer for Hardware");
            result_tmp_ptr = &result_tmp; /* Assign point to temp struct */
    }
    else {
        result_tmp_ptr = result; /* No Shared Point to directly assign */
    }
    if (result_tmp_ptr == NULL) {
        MAX3266X_MSG("tmp ptr is null");
        return MP_VAL;
    }

    if (clc == MXC_TPU_MAA_EXP) {
        length = wc_MXC_MAA_Largest(5, multiplier->used, multiplicand->used,
                                           exp->used, mod->used, result->used);
    }
    else {
        length = wc_MXC_MAA_Largest(4, multiplier->used, multiplicand->used,
                                        mod->used, result->used);
    }

    /* Zero Pad everything if needed */
    ret = wc_MXC_MAA_zeroPad(multiplier, multiplicand, exp, mod, result_tmp_ptr,
                                clc, length);
    if (ret != 0) {
        MAX3266X_MSG("Zero Padding Failed");
        return ret;
    }

    /* Init MAA HW */
    ret = wc_MXC_MAA_init(length*sizeof(mp_digit)*8);
    if (ret != 0) {
        MAX3266X_MSG("HW Init Failed");
        wolfSSL_HwPkMutexUnLock();
        return ret;
    }

    /* Start Math And Cast to expect types for SDK */
    MAX3266X_MSG("Starting Computation in MAA");
    ret = MXC_TPU_MAA_Compute(clc, (char *)(multiplier->dp),
                                    (char *)(multiplicand->dp),
                                    (char *)((exp == NULL) ? NULL: exp->dp),
                                    (char *)(mod->dp),
                                    (int *)(result_tmp_ptr->dp),
                                    (length*sizeof(mp_digit)));
    MAX3266X_MSG("MAA Finished Computation");
    if (wc_MXC_error(&ret) != 0) {
        MAX3266X_MSG("HW Computation Error");
        wolfSSL_HwPkMutexUnLock();
        return ret;
    }

    ret = wc_MXC_MAA_Shutdown();
    if (ret != 0) {
        MAX3266X_MSG("HW Shutdown Failure");
        /* Shutdown will always call wolfSSL_HwPkMutexUnLock(); */
        /* before returning */
        return ret;
    }

    /* Copy tmp result if needed */
    if ((multiplier == result) || (multiplicand == result) || (exp == result) ||
            (mod == result)) {
        mp_copy(result_tmp_ptr, result);
        ForceZero(result_tmp_ptr, sizeof(mp_int)); /* force zero */
    }

    result->used = wc_MXC_MAA_adjustUsed(result->dp, length);
    return ret;
}



int wc_MXC_MAA_expmod(mp_int* base, mp_int* exp, mp_int* mod,
                            mp_int* result)
{
    mp_int multiplicand;
    if (base == NULL || exp == NULL || mod == NULL || result == NULL) {
        return BAD_FUNC_ARG;
    }
    XMEMSET(&multiplicand, 0, sizeof(mp_int));
    multiplicand.dp[0] = 0x01;
    multiplicand.used = mod->used;
    MAX3266X_MSG("Preparing exptmod MAA HW Call");
    return wc_MXC_MAA_math(base, &multiplicand, exp, mod, result,
                            MXC_TPU_MAA_EXP);
}

int wc_MXC_MAA_sqrmod(mp_int* multiplier, mp_int* mod, mp_int* result)
{
    mp_int multiplicand;
    if (multiplier == NULL || mod == NULL || result == NULL) {
        return BAD_FUNC_ARG;
    }
    XMEMSET(&multiplicand, 0, sizeof(mp_int));
    multiplicand.dp[0] = 0x01;
    multiplicand.used = mod->used;
    MAX3266X_MSG("Preparing sqrmod MAA HW Call");
    return wc_MXC_MAA_math(multiplier, &multiplicand, NULL, mod, result,
                            MXC_TPU_MAA_SQ);
}

int wc_MXC_MAA_mulmod(mp_int* multiplier, mp_int* multiplicand, mp_int* mod,
                            mp_int* result)
{
    if (multiplier == NULL || multiplicand == NULL || mod == NULL ||
            result == NULL) {
        return BAD_FUNC_ARG;
    }
    MAX3266X_MSG("Preparing mulmod MAA HW Call");
    return wc_MXC_MAA_math(multiplier, multiplicand, NULL, mod, result,
                            MXC_TPU_MAA_MUL);
}

int wc_MXC_MAA_sqrmulmod(mp_int* multiplier, mp_int* multiplicand,
                            mp_int* exp, mp_int* mod, mp_int* result)
{
    if (multiplier == NULL || multiplicand == NULL || exp == NULL ||
            mod == NULL || result == NULL) {
        return BAD_FUNC_ARG;
    }
    MAX3266X_MSG("Preparing sqrmulmod MAA HW Call");
    return wc_MXC_MAA_math(multiplier, multiplicand, NULL, mod, result,
                            MXC_TPU_MAA_SQMUL);
}

int wc_MXC_MAA_addmod(mp_int* multiplier, mp_int* multiplicand, mp_int* mod,
                            mp_int* result)
{
    if (multiplier == NULL || multiplicand == NULL || mod == NULL ||
            result == NULL) {
        return BAD_FUNC_ARG;
    }
    MAX3266X_MSG("Preparing addmod MAA HW Call");
    return wc_MXC_MAA_math(multiplier, multiplicand, NULL, mod, result,
                            MXC_TPU_MAA_ADD);
}

int wc_MXC_MAA_submod(mp_int* multiplier, mp_int* multiplicand, mp_int* mod,
                            mp_int* result)
{
    if (multiplier == NULL || multiplicand == NULL || mod == NULL ||
            result == NULL) {
        return BAD_FUNC_ARG;
    }
    MAX3266X_MSG("Preparing submod MAA HW Call");
    if ((mod->used < multiplier->used) || (mod->used < multiplicand->used)) {
            MAX3266X_MSG("HW Limitation: Defaulting back to software");
            return mxc_submod(multiplier, multiplicand, mod, result);
    }
    else {
        return wc_MXC_MAA_math(multiplier, multiplicand, NULL, mod, result,
                             MXC_TPU_MAA_SUB);
    }
}

/* General Function to call hardware control */
int hw_mulmod(mp_int* multiplier, mp_int* multiplicand, mp_int* mod,
                    mp_int* result)
{
    if (multiplier == NULL || multiplicand == NULL || mod == NULL ||
            result == NULL) {
        return MP_VAL;
    }
    if ((multiplier->used == 0) || (multiplicand->used == 0)) {
        mp_zero(result);
        return 0;
    }
    else {
        if (wc_MXC_MAA_Fallback(3, multiplier->used, mod->used,
                                multiplicand->used) != 0) {
                return mxc_mulmod(multiplier, multiplicand, mod, result);
        }
        else {
            return wc_MXC_MAA_mulmod(multiplier, multiplicand, mod, result);
        }
    }
}

int hw_addmod(mp_int* a, mp_int* b, mp_int* mod, mp_int* result)
{
    int err = MP_OKAY;
    /* Validate parameters. */
    if ((a == NULL) || (b == NULL) || (mod == NULL) || (result == NULL)) {
        err = MP_VAL;
    }
    if (err == MP_OKAY) {
        if (wc_MXC_MAA_Fallback(3, a->used, b->used, mod->used) != 0) {
            err = mxc_addmod(a, b, mod, result);
        }
        else {
            err = wc_MXC_MAA_addmod(a, b, mod, result);
        }
    }
    return err;
}


int hw_submod(mp_int* a, mp_int* b, mp_int* mod, mp_int* result)
{
    int err = MP_OKAY;
    /* Validate parameters. */
    if ((a == NULL) || (b == NULL) || (mod == NULL) || (result == NULL)) {
        err = MP_VAL;
    }
    if (err == MP_OKAY) {
        if (wc_MXC_MAA_Fallback(3, a->used, b->used, mod->used) != 0) {
            err = mxc_submod(a, b, mod, result);
        }
        else{
            err = wc_MXC_MAA_submod(a, b, mod, result);
        }
    }
    return err;
}

int hw_exptmod(mp_int* base, mp_int* exp, mp_int* mod, mp_int* result)
{
    int err = MP_OKAY;
    /* Validate parameters. */
    if ((base == NULL) || (exp == NULL) || (mod == NULL) || (result == NULL)) {
        err = MP_VAL;
    }
    if (err == MP_OKAY) {
        if ((mod->used < exp->used) || (mod->used < base->used)) {
            err = mxc_exptmod(base, exp, mod, result);
        }
        else if (wc_MXC_MAA_Fallback(3, base->used, exp->used, mod->used)
                    != 0) {
            return mxc_exptmod(base, exp, mod, result);
        }
        else{
            err = wc_MXC_MAA_expmod(base, exp, mod, result);
        }
    }
    return err;
}


/* No mod function available with hardware, however perform a submod    */
/* (a - 0) mod m will essentially perform the same operation as a mod m */
int hw_mod(mp_int* a, mp_int* mod, mp_int* result)
{
    mp_int b;
    if (a == NULL || mod == NULL || result == NULL) {
        return MP_VAL;
    }
    if (wc_MXC_MAA_Fallback(2, a->used, mod->used) != 0) {
        return mxc_mod(a, mod, result);
    }
    XMEMSET(&b, 0, sizeof(mp_int));
    b.used = mod->used; /* assume mod is determining size */
    return hw_submod(a, &b, mod, result);
}

int hw_sqrmod(mp_int* base, mp_int* mod, mp_int* result)
{
    if (base == NULL || mod == NULL || result == NULL) {
        return MP_VAL;
    }
    if (base->used == 0) {
        mp_zero(result);
        return 0;
    }
    return wc_MXC_MAA_sqrmod(base, mod, result);
}

#endif /* MAX3266X_MATH */

#if defined(MAX3266X_RTC)
/* Initialize the RTC */
int wc_MXC_RTC_Init(void)
{
    /* RTC Init for benchmark */
    if (MXC_RTC_Init(0, 0) != E_NO_ERROR) {
        return WC_HW_E;
    }
    /* Disable the Interrupt */
    if (MXC_RTC_DisableInt(MXC_RTC_INT_EN_LONG) == E_BUSY) {
        return WC_HW_E;
    }
    /* Start Clock for RTC */
    if (MXC_RTC_SquareWaveStart(MXC_RTC_F_512HZ) == E_BUSY) {
        return E_BUSY;
    }
    /* Begin RTC count */
    if (MXC_RTC_Start() != E_NO_ERROR) {
        return WC_HW_E;
    }
    return 0;
}

/* Reset the RTC */
int wc_MXC_RTC_Reset(void)
{
    /* Stops Counts */
    if (MXC_RTC_Stop() != E_NO_ERROR) {
        return WC_HW_E;
    }
    /* Restart RTC via Init */
    if (wc_MXC_RTC_Init() != E_NO_ERROR) {
        return WC_HW_E;
    }
    return 0;
}

/* Function to handle RTC read retries */
void wc_MXC_RTC_GetRTCValue(int32_t (*rtcGetFunction)(uint32_t*),
                                uint32_t* outValue, int32_t* err)
{
    *err = rtcGetFunction(outValue);  /* Initial attempt to get the value */
    while (*err != E_NO_ERROR) {
        *err = rtcGetFunction(outValue);  /* Retry if the error persists */
    }
}

/* Function to provide the current time as a double */
/* Returns seconds and millisecond */
double wc_MXC_RTC_Time(void)
{
    int32_t err;
    uint32_t rtc_seconds, rtc_subseconds;

    /* Retrieve sub-seconds from RTC */
    wc_MXC_RTC_GetRTCValue((int32_t (*)(uint32_t*))MXC_RTC_GetSubSeconds,
                                    &rtc_subseconds, &err);
    if (err != E_NO_ERROR) {
        return (double)err;
    }
    /* Retrieve seconds from RTC */
    wc_MXC_RTC_GetRTCValue((int32_t (*)(uint32_t*))MXC_RTC_GetSeconds,
                                &rtc_seconds, &err);
    if (err != E_NO_ERROR) {
        return (double)err;
    }
    /* Per the device documentation, subsecond register holds up to 1 second */
    /* subsecond register is size 2^12, so divide by 4096 to get milliseconds */
    return ((double)rtc_seconds + ((double)rtc_subseconds / 4096));
}

#endif /* MAX3266X_RTC */

#endif /* WOLFSSL_MAX32665 || WOLFSSL_MAX32666 */