wolfssl-wolfcrypt 2.0.0

Rust wrapper for wolfssl C library cryptographic functionality
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
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
/*
 * 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
 */

/*!
This module provides a Rust wrapper for the wolfCrypt library's ECC
functionality.

The primary component is the `ECC` struct, which manages the lifecycle of a
wolfSSL `ecc_key` object. It ensures proper initialization and deallocation.
*/

#![cfg(ecc)]

use crate::sys;
#[cfg(random)]
use crate::random::{RNG, RngHandle};

/// Rust wrapper for wolfSSL `ecc_point` object.
pub struct ECCPoint {
    wc_ecc_point: *mut sys::ecc_point,
    heap: *mut core::ffi::c_void,
}

impl ECCPoint {
    /// Import an ECCPoint from a DER-formatted buffer.
    ///
    /// # Parameters
    ///
    /// * `din`: DER-formatted buffer.
    /// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
    /// * `heap`: Optional heap hint.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECCPoint) containing the ECCPoint struct instance or
    /// Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_import, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::{ECC,ECCPoint};
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let curve_id = ECC::SECP256R1;
    /// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
    /// let mut ecc = ECC::generate_ex(curve_size, &rng, curve_id, None, None).expect("Error with generate()");
    /// let ecc_point = ecc.make_pub_to_point(Some(&rng), None).expect("Error with make_pub_to_point()");
    /// let mut der = [0u8; 128];
    /// let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()");
    /// ECCPoint::import_der(&der[0..size], curve_id, None).expect("Error with import_der()");
    /// }
    /// ```
    #[cfg(ecc_import)]
    pub fn import_der(din: &[u8], curve_id: i32, heap: Option<*mut core::ffi::c_void>) -> Result<Self, i32> {
        let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) };
        if curve_idx < 0 {
            return Err(curve_idx);
        }
        let heap = match heap {
            Some(heap) => heap,
            None => core::ptr::null_mut(),
        };
        let wc_ecc_point = unsafe { sys::wc_ecc_new_point_h(heap) };
        if wc_ecc_point.is_null() {
            return Err(sys::wolfCrypt_ErrorCodes_MEMORY_E);
        }
        let eccpoint = ECCPoint { wc_ecc_point, heap };
        let din_size = crate::buffer_len_to_u32(din.len())?;
        let rc = unsafe {
            sys::wc_ecc_import_point_der(din.as_ptr(), din_size, curve_idx,
                eccpoint.wc_ecc_point)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(eccpoint)
    }

    /// Import an ECCPoint from a DER-formatted buffer.
    ///
    /// # Parameters
    ///
    /// * `din`: DER-formatted buffer.
    /// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
    /// * `short_key_size`: if shortKeySize != 0 then key size is always
    ///   (din.len() - 1) / 2.
    /// * `heap`: Optional heap hint.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECCPoint) containing the ECCPoint struct instance or
    /// Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_import, ecc_export, ecc_comp_key, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::{ECC,ECCPoint};
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let curve_id = ECC::SECP256R1;
    /// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
    /// let mut ecc = ECC::generate_ex(curve_size, &rng, curve_id, None, None).expect("Error with generate()");
    /// let ecc_point = ecc.make_pub_to_point(Some(&rng), None).expect("Error with make_pub_to_point()");
    /// let mut der = [0u8; 128];
    /// let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()");
    /// ECCPoint::import_der_ex(&der[0..size], curve_id, 0, None).expect("Error with import_der_ex()");
    /// }
    /// ```
    #[cfg(ecc_import)]
    pub fn import_der_ex(din: &[u8], curve_id: i32, short_key_size: i32, heap: Option<*mut core::ffi::c_void>) -> Result<Self, i32> {
        let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) };
        if curve_idx < 0 {
            return Err(curve_idx);
        }
        let heap = match heap {
            Some(heap) => heap,
            None => core::ptr::null_mut(),
        };
        let wc_ecc_point = unsafe { sys::wc_ecc_new_point_h(heap) };
        if wc_ecc_point.is_null() {
            return Err(sys::wolfCrypt_ErrorCodes_MEMORY_E);
        }
        let eccpoint = ECCPoint { wc_ecc_point, heap };
        let din_size = crate::buffer_len_to_u32(din.len())?;
        let rc = unsafe {
            sys::wc_ecc_import_point_der_ex(din.as_ptr(), din_size, curve_idx,
                wc_ecc_point, short_key_size)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(eccpoint)
    }

    /// Export an ECCPoint in DER format.
    ///
    /// # Parameters
    ///
    /// * `dout`: Output buffer.
    /// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
    ///
    /// # Returns
    ///
    /// Returns either Ok(size) containing the number of bytes written to
    /// `dout` or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_export, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::{ECC,ECCPoint};
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let curve_id = ECC::SECP256R1;
    /// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
    /// let mut ecc = ECC::generate_ex(curve_size, &rng, curve_id, None, None).expect("Error with generate()");
    /// let ecc_point = ecc.make_pub_to_point(Some(&rng), None).expect("Error with make_pub_to_point()");
    /// let mut der = [0u8; 128];
    /// let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()");
    /// assert!(size > 0 && size <= der.len());
    /// ECCPoint::import_der(&der[0..size], curve_id, None).expect("Error with import_der()");
    /// }
    /// ```
    #[cfg(ecc_export)]
    pub fn export_der(&self, dout: &mut [u8], curve_id: i32) -> Result<usize, i32> {
        let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) };
        if curve_idx < 0 {
            return Err(curve_idx);
        }
        let mut dout_size = crate::buffer_len_to_u32(dout.len())?;
        let rc = unsafe {
            sys::wc_ecc_export_point_der(curve_idx, self.wc_ecc_point,
                dout.as_mut_ptr(), &mut dout_size)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(dout_size as usize)
    }

    /// Export an ECCPoint in compressed DER format.
    ///
    /// # Parameters
    ///
    /// * `dout`: Output buffer.
    /// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
    ///
    /// # Returns
    ///
    /// Returns either Ok(size) containing the number of bytes written to
    /// `dout` or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_export, ecc_comp_key, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::{ECC,ECCPoint};
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let curve_id = ECC::SECP256R1;
    /// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
    /// let mut ecc = ECC::generate_ex(curve_size, &rng, curve_id, None, None).expect("Error with generate()");
    /// let ecc_point = ecc.make_pub_to_point(Some(&rng), None).expect("Error with make_pub_to_point()");
    /// let mut der = [0u8; 128];
    /// let size = ecc_point.export_der_compressed(&mut der, curve_id).expect("Error with export_der_compressed()");
    /// }
    /// ```
    #[cfg(all(ecc_export, ecc_comp_key))]
    pub fn export_der_compressed(&self, dout: &mut [u8], curve_id: i32) -> Result<usize, i32> {
        let curve_idx = unsafe { sys::wc_ecc_get_curve_idx(curve_id) };
        if curve_idx < 0 {
            return Err(curve_idx);
        }
        let mut dout_size = crate::buffer_len_to_u32(dout.len())?;
        let rc = unsafe {
            sys::wc_ecc_export_point_der_ex(curve_idx, self.wc_ecc_point,
                dout.as_mut_ptr(), &mut dout_size, 1)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(dout_size as usize)
    }

    /// Zeroize the ECCPoint.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let mut ecc_point = ecc.make_pub_to_point(Some(&rng), None).expect("Error with make_pub_to_point()");
    /// ecc_point.forcezero();
    /// }
    /// ```
    pub fn forcezero(&mut self) {
        unsafe { sys::wc_ecc_forcezero_point(self.wc_ecc_point) };
    }
}

impl ECCPoint {
    fn zeroize(&mut self) {
        self.wc_ecc_point = core::ptr::null_mut();
        self.heap = core::ptr::null_mut();
    }
}

impl Drop for ECCPoint {
    /// Safely free the underlying wolfSSL ecc_point context.
    ///
    /// This calls the `wc_ecc_del_point_h()` wolfssl library function.
    ///
    /// The Rust Drop trait guarantees that this method is called when the
    /// ECCPoint struct instance goes out of scope, automatically cleaning up
    /// resources and preventing memory leaks.
    fn drop(&mut self) {
        unsafe { sys::wc_ecc_del_point_h(self.wc_ecc_point, self.heap); }
        self.zeroize();
    }
}

/// The `ECC` struct manages the lifecycle of a wolfSSL `ecc_key` object.
///
/// It ensures proper initialization and deallocation. The struct holds a
/// pointer to a `sys::ecc_key` allocated on the C heap.
///
/// An instance can be created with `generate()`, `import_x963()`,
/// `import_x963_ex()`, `import_private_key()`, `import_private_key_ex()`,
/// `import_raw()`, or `import_raw_ex()`.
pub struct ECC {
    pub(crate) wc_ecc_key: *mut sys::ecc_key,
    /// RNG bound to this key via `set_rng`, kept alive for as long as the C
    /// struct holds its pointer.
    #[cfg(random)]
    rng: Option<RngHandle>,
}

#[cfg(ecc_curve_ids)]
impl ECC {
    pub const CURVE_INVALID: i32 = sys::ecc_curve_ids_ECC_CURVE_INVALID;
    pub const CURVE_DEF: i32 = sys::ecc_curve_ids_ECC_CURVE_DEF;
    pub const SECP192R1: i32 = sys::ecc_curve_ids_ECC_SECP192R1;
    pub const PRIME192V2: i32 = sys::ecc_curve_ids_ECC_PRIME192V2;
    pub const PRIME192V3: i32 = sys::ecc_curve_ids_ECC_PRIME192V3;
    pub const PRIME239V1: i32 = sys::ecc_curve_ids_ECC_PRIME239V1;
    pub const PRIME239V2: i32 = sys::ecc_curve_ids_ECC_PRIME239V2;
    pub const PRIME239V3: i32 = sys::ecc_curve_ids_ECC_PRIME239V3;
    pub const SECP256R1: i32 = sys::ecc_curve_ids_ECC_SECP256R1;
    pub const SECP112R1: i32 = sys::ecc_curve_ids_ECC_SECP112R1;
    pub const SECP112R2: i32 = sys::ecc_curve_ids_ECC_SECP112R2;
    pub const SECP128R1: i32 = sys::ecc_curve_ids_ECC_SECP128R1;
    pub const SECP128R2: i32 = sys::ecc_curve_ids_ECC_SECP128R2;
    pub const SECP160R1: i32 = sys::ecc_curve_ids_ECC_SECP160R1;
    pub const SECP160R2: i32 = sys::ecc_curve_ids_ECC_SECP160R2;
    pub const SECP224R1: i32 = sys::ecc_curve_ids_ECC_SECP224R1;
    pub const SECP384R1: i32 = sys::ecc_curve_ids_ECC_SECP384R1;
    pub const SECP521R1: i32 = sys::ecc_curve_ids_ECC_SECP521R1;
    pub const SECP160K1: i32 = sys::ecc_curve_ids_ECC_SECP160K1;
    pub const SECP192K1: i32 = sys::ecc_curve_ids_ECC_SECP192K1;
    pub const SECP224K1: i32 = sys::ecc_curve_ids_ECC_SECP224K1;
    pub const SECP256K1: i32 = sys::ecc_curve_ids_ECC_SECP256K1;
    pub const BRAINPOOLP160R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP160R1;
    pub const BRAINPOOLP192R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP192R1;
    pub const BRAINPOOLP224R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP224R1;
    pub const BRAINPOOLP256R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP256R1;
    pub const BRAINPOOLP320R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP320R1;
    pub const BRAINPOOLP384R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP384R1;
    pub const BRAINPOOLP512R1: i32 = sys::ecc_curve_ids_ECC_BRAINPOOLP512R1;
    #[cfg(ecc_curve_sm2p256v1)]
    pub const SM2P256V1: i32 = sys::ecc_curve_ids_ECC_SM2P256V1;
    #[cfg(ecc_curve_25519)]
    pub const X25519: i32 = sys::ecc_curve_ids_ECC_X25519;
    #[cfg(ecc_curve_448)]
    pub const X448: i32 = sys::ecc_curve_ids_ECC_X448;
    #[cfg(ecc_curve_sakke)]
    pub const SAKKE_1: i32 = sys::ecc_curve_ids_ECC_SAKKE_1;
    #[cfg(ecc_custom_curves)]
    pub const CURVE_CUSTOM: i32 = sys::ecc_curve_ids_ECC_CURVE_CUSTOM;
    pub const CURVE_MAX: i32 = sys::ecc_curve_ids_ECC_CURVE_MAX;
}

#[cfg(not(ecc_curve_ids))]
impl ECC {
    pub const CURVE_INVALID: i32 = sys::ecc_curve_id_ECC_CURVE_INVALID;
    pub const CURVE_DEF: i32 = sys::ecc_curve_id_ECC_CURVE_DEF;
    pub const SECP192R1: i32 = sys::ecc_curve_id_ECC_SECP192R1;
    pub const PRIME192V2: i32 = sys::ecc_curve_id_ECC_PRIME192V2;
    pub const PRIME192V3: i32 = sys::ecc_curve_id_ECC_PRIME192V3;
    pub const PRIME239V1: i32 = sys::ecc_curve_id_ECC_PRIME239V1;
    pub const PRIME239V2: i32 = sys::ecc_curve_id_ECC_PRIME239V2;
    pub const PRIME239V3: i32 = sys::ecc_curve_id_ECC_PRIME239V3;
    pub const SECP256R1: i32 = sys::ecc_curve_id_ECC_SECP256R1;
    pub const SECP112R1: i32 = sys::ecc_curve_id_ECC_SECP112R1;
    pub const SECP112R2: i32 = sys::ecc_curve_id_ECC_SECP112R2;
    pub const SECP128R1: i32 = sys::ecc_curve_id_ECC_SECP128R1;
    pub const SECP128R2: i32 = sys::ecc_curve_id_ECC_SECP128R2;
    pub const SECP160R1: i32 = sys::ecc_curve_id_ECC_SECP160R1;
    pub const SECP160R2: i32 = sys::ecc_curve_id_ECC_SECP160R2;
    pub const SECP224R1: i32 = sys::ecc_curve_id_ECC_SECP224R1;
    pub const SECP384R1: i32 = sys::ecc_curve_id_ECC_SECP384R1;
    pub const SECP521R1: i32 = sys::ecc_curve_id_ECC_SECP521R1;
    pub const SECP160K1: i32 = sys::ecc_curve_id_ECC_SECP160K1;
    pub const SECP192K1: i32 = sys::ecc_curve_id_ECC_SECP192K1;
    pub const SECP224K1: i32 = sys::ecc_curve_id_ECC_SECP224K1;
    pub const SECP256K1: i32 = sys::ecc_curve_id_ECC_SECP256K1;
    pub const BRAINPOOLP160R1: i32 = sys::ecc_curve_id_ECC_BRAINPOOLP160R1;
    pub const BRAINPOOLP192R1: i32 = sys::ecc_curve_id_ECC_BRAINPOOLP192R1;
    pub const BRAINPOOLP224R1: i32 = sys::ecc_curve_id_ECC_BRAINPOOLP224R1;
    pub const BRAINPOOLP256R1: i32 = sys::ecc_curve_id_ECC_BRAINPOOLP256R1;
    pub const BRAINPOOLP320R1: i32 = sys::ecc_curve_id_ECC_BRAINPOOLP320R1;
    pub const BRAINPOOLP384R1: i32 = sys::ecc_curve_id_ECC_BRAINPOOLP384R1;
    pub const BRAINPOOLP512R1: i32 = sys::ecc_curve_id_ECC_BRAINPOOLP512R1;
    #[cfg(ecc_curve_sm2p256v1)]
    pub const SM2P256V1: i32 = sys::ecc_curve_id_ECC_SM2P256V1;
    #[cfg(ecc_curve_25519)]
    pub const X25519: i32 = sys::ecc_curve_id_ECC_X25519;
    #[cfg(ecc_curve_448)]
    pub const X448: i32 = sys::ecc_curve_id_ECC_X448;
    #[cfg(ecc_curve_sakke)]
    pub const SAKKE_1: i32 = sys::ecc_curve_id_ECC_SAKKE_1;
    #[cfg(ecc_custom_curves)]
    pub const CURVE_CUSTOM: i32 = sys::ecc_curve_id_ECC_CURVE_CUSTOM;
    pub const CURVE_MAX: i32 = sys::ecc_curve_id_ECC_CURVE_MAX;
}

impl ECC {
    pub const FLAG_NONE: i32 = sys::WC_ECC_FLAG_NONE as i32;
    pub const FLAG_COFACTOR: i32 = sys::WC_ECC_FLAG_COFACTOR as i32;
    pub const FLAG_DEC_SIGN: i32 = sys::WC_ECC_FLAG_DEC_SIGN as i32;

    /// Allocate and initialize a new `sys::ecc_key` on the C heap.
    fn new_ecc_key(heap: *mut core::ffi::c_void, dev_id: i32) -> Result<*mut sys::ecc_key, i32> {
        let key = unsafe { sys::wc_ecc_key_new(heap) };
        if key.is_null() {
            return Err(sys::wolfCrypt_ErrorCodes_MEMORY_E);
        }
        // wc_ecc_key_new() always initializes the key with INVALID_DEVID.
        // Calling wc_ecc_init_ex() a second time to install the user's dev_id
        // would re-run every initialization step (including allocations under
        // some build configurations such as async crypto without WOLF_CRYPTO_CB)
        // and orphan resources from the first init. Instead, just patch devId
        // in place for WOLF_CRYPTO_CB builds.
        #[cfg(wolf_crypto_cb)]
        unsafe { (*key).devId = dev_id; }
        #[cfg(not(wolf_crypto_cb))]
        let _ = dev_id;
        Ok(key)
    }

    /// Generate a new ECC key with the given size.
    ///
    /// # Parameters
    ///
    /// * `size`: Desired key length in bytes.
    /// * `rng`: Reference to a `RNG` struct to use for random number
    ///   generation while making the key.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// ecc.check().expect("Error with check()");
    /// }
    /// ```
    #[cfg(random)]
    pub fn generate(size: i32, rng: &RNG, heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let rc = unsafe {
            sys::wc_ecc_make_key(rng.wc_rng, size, ecc.wc_ecc_key)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Generate a new ECC key with the given size and curve.
    ///
    /// # Parameters
    ///
    /// * `size`: Desired key length in bytes.
    /// * `rng`: Reference to a `RNG` struct to use for random number
    ///   generation while making the key.
    /// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let curve_id = ECC::SECP256R1;
    /// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
    /// let mut ecc = ECC::generate_ex(curve_size, &rng, curve_id, None, None).expect("Error with generate_ex()");
    /// ecc.check().expect("Error with check()");
    /// }
    /// ```
    #[cfg(random)]
    pub fn generate_ex(size: i32, rng: &RNG, curve_id: i32, heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let rc = unsafe {
            sys::wc_ecc_make_key_ex(rng.wc_rng, size, ecc.wc_ecc_key, curve_id)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Generate a new ECC key with the given size, curve, and flags.
    ///
    /// # Parameters
    ///
    /// * `size`: Desired key length in bytes.
    /// * `rng`: Reference to a `RNG` struct to use for random number
    ///   generation while making the key.
    /// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
    /// * `flags`: Flags for making the key.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let curve_id = ECC::SECP256R1;
    /// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
    /// let mut ecc = ECC::generate_ex2(curve_size, &rng, curve_id, ECC::FLAG_COFACTOR, None, None).expect("Error with generate_ex2()");
    /// ecc.check().expect("Error with check()");
    /// }
    /// ```
    #[cfg(random)]
    pub fn generate_ex2(size: i32, rng: &RNG, curve_id: i32, flags: i32, heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let rc = unsafe {
            sys::wc_ecc_make_key_ex2(rng.wc_rng, size, ecc.wc_ecc_key, curve_id, flags)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Get the curve size corresponding to the given curve ID.
    ///
    /// # Parameters
    ///
    /// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
    ///
    /// # Returns
    ///
    /// Returns either Ok(size) containing the curve size or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let curve_id = ECC::SECP256R1;
    /// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
    /// let mut ecc = ECC::generate_ex(curve_size, &rng, curve_id, None, None).expect("Error with generate()");
    /// ecc.check().expect("Error with check()");
    /// }
    /// ```
    pub fn get_curve_size_from_id(curve_id: i32) -> Result<i32, i32> {
        let rc = unsafe { sys::wc_ecc_get_curve_size_from_id(curve_id) };
        if rc < 0 {
            return Err(rc);
        }
        Ok(rc)
    }

    /// Import public and private ECC key pair from DER input buffer.
    ///
    /// # Parameters
    ///
    /// * `der`: DER buffer containing the ECC public and private key pair.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// use std::fs;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let key_path = "../../../certs/ecc-client-key.der";
    /// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
    /// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
    /// }
    /// ```
    pub fn import_der(der: &[u8], heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let mut idx = 0u32;
        let der_size = crate::buffer_len_to_u32(der.len())?;
        let rc = unsafe {
            sys::wc_EccPrivateKeyDecode(der.as_ptr(), &mut idx, ecc.wc_ecc_key, der_size)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Import public ECC key from DER input buffer.
    ///
    /// # Parameters
    ///
    /// * `der`: DER buffer containing the ECC public key.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// use std::fs;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let key_path = "../../../certs/ecc-client-key.der";
    /// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
    /// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
    /// let hash = [0x42u8; 32];
    /// let mut signature = [0u8; 128];
    /// let signature_length = ecc.sign_hash(&hash, &mut signature, &rng).expect("Error with sign_hash()");
    /// assert!(signature_length > 0 && signature_length <= signature.len());
    /// let signature = &mut signature[0..signature_length];
    /// let key_path = "../../../certs/ecc-client-keyPub.der";
    /// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
    /// let mut ecc = ECC::import_public_der(&der, None, None).expect("Error with import_public_der()");
    /// }
    /// ```
    pub fn import_public_der(der: &[u8], heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let mut idx = 0u32;
        let der_size = crate::buffer_len_to_u32(der.len())?;
        let rc = unsafe {
            sys::wc_EccPublicKeyDecode(der.as_ptr(), &mut idx, ecc.wc_ecc_key, der_size)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Import a public/private ECC key pair from a buffer containing the raw
    /// private key and a second buffer containing the ANSI X9.63 formatted
    /// public key. This function handles both compressed and uncompressed
    /// keys as long as wolfSSL is built with the HAVE_COMP_KEY build option
    /// enabled.
    ///
    /// # Parameters
    ///
    /// * `priv_buf`: Buffer containing the raw private key.
    /// * `pub_buf`: Buffer containing the ANSI X9.63 formatted public key.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_import, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let hash = [0x42u8; 32];
    /// let mut signature = [0u8; 128];
    /// let signature_length = ecc.sign_hash(&hash, &mut signature, &rng).expect("Error with sign_hash()");
    /// let signature = &signature[0..signature_length];
    /// let mut d = [0u8; 32];
    /// let d_size = ecc.export_private(&mut d).expect("Error with export_private()");
    /// let mut x963 = [0u8; 128];
    /// let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
    /// let x963 = &x963[0..x963_size];
    /// let mut ecc2 = ECC::import_private_key(&d, x963, None, None).expect("Error with import_private_key()");
    /// let valid = ecc2.verify_hash(&signature, &hash).expect("Error with verify_hash()");
    /// assert_eq!(valid, true);
    /// }
    /// ```
    #[cfg(ecc_import)]
    pub fn import_private_key(priv_buf: &[u8], pub_buf: &[u8], heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let priv_size = crate::buffer_len_to_u32(priv_buf.len())?;
        let pub_ptr = if pub_buf.is_empty() {core::ptr::null()} else {pub_buf.as_ptr()};
        let pub_size = crate::buffer_len_to_u32(pub_buf.len())?;
        let rc = unsafe {
            sys::wc_ecc_import_private_key(priv_buf.as_ptr(), priv_size,
                pub_ptr, pub_size, ecc.wc_ecc_key)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Import a public/private ECC key pair from a buffer containing the raw
    /// private key and a second buffer containing the ANSI X9.63 formatted
    /// public key. This function handles both compressed and uncompressed
    /// keys as long as wolfSSL is built with the HAVE_COMP_KEY build option
    /// enabled. This function allows the curve ID to be explicitly specified.
    ///
    /// # Parameters
    ///
    /// * `priv_buf`: Buffer containing the raw private key.
    /// * `pub_buf`: Buffer containing the ANSI X9.63 formatted public key.
    /// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_import, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let curve_id = ECC::SECP256R1;
    /// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
    /// let mut ecc = ECC::generate_ex(curve_size, &rng, curve_id, None, None).expect("Error with generate_ex()");
    /// let hash = [0x42u8; 32];
    /// let mut signature = [0u8; 128];
    /// let signature_length = ecc.sign_hash(&hash, &mut signature, &rng).expect("Error with sign_hash()");
    /// let signature = &signature[0..signature_length];
    /// let mut d = [0u8; 32];
    /// let d_size = ecc.export_private(&mut d).expect("Error with export_private()");
    /// let mut x963 = [0u8; 128];
    /// let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
    /// let x963 = &x963[0..x963_size];
    /// let mut ecc2 = ECC::import_private_key_ex(&d, x963, curve_id, None, None).expect("Error with import_private_key_ex()");
    /// let valid = ecc2.verify_hash(&signature, &hash).expect("Error with verify_hash()");
    /// assert_eq!(valid, true);
    /// }
    /// ```
    #[cfg(ecc_import)]
    pub fn import_private_key_ex(priv_buf: &[u8], pub_buf: &[u8], curve_id: i32, heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let priv_size = crate::buffer_len_to_u32(priv_buf.len())?;
        let pub_ptr = if pub_buf.is_empty() {core::ptr::null()} else {pub_buf.as_ptr()};
        let pub_size = crate::buffer_len_to_u32(pub_buf.len())?;
        let rc = unsafe {
            sys::wc_ecc_import_private_key_ex(priv_buf.as_ptr(), priv_size,
                pub_ptr, pub_size, ecc.wc_ecc_key, curve_id)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Import raw ECC key from components in hexadecimal ASCII string format
    /// with curve name specified.
    ///
    /// # Parameters
    ///
    /// * `qx`: X component of public key as null terminated ASCII hex string.
    /// * `qy`: Y component of public key as null terminated ASCII hex string.
    /// * `d`: Private key as null terminated ASCII hex string.
    /// * `curve_name`: Null terminated ASCII string containing the curve name.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(ecc_import)]
    /// {
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let qx = b"7a4e287890a1a47ad3457e52f2f76a83ce46cbc947616d0cbaa82323818a793d\0";
    /// let qy = b"eec4084f5b29ebf29c44cce3b3059610922f8b30ea6e8811742ac7238fe87308\0";
    /// let d  = b"8c14b793cb19137e323a6d2e2a870bca2e7a493ec1153b3a95feb8a4873f8d08\0";
    /// ECC::import_raw(qx, qy, d, b"SECP256R1\0", None, None).expect("Error with import_raw()");
    /// }
    /// ```
    #[cfg(ecc_import)]
    pub fn import_raw(qx: &[u8], qy: &[u8], d: &[u8], curve_name: &[u8], heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
        if qx.is_empty() || qy.is_empty() || d.is_empty() || curve_name.is_empty() ||
            qx[qx.len() - 1] != 0 || qy[qy.len() - 1] != 0 || d[d.len() - 1] != 0 || curve_name[curve_name.len() - 1] != 0 {
            return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG);
        }
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let qx_ptr = qx.as_ptr() as *const core::ffi::c_char;
        let qy_ptr = qy.as_ptr() as *const core::ffi::c_char;
        let d_ptr = d.as_ptr() as *const core::ffi::c_char;
        let curve_name_ptr = curve_name.as_ptr() as *const core::ffi::c_char;
        let rc = unsafe {
            sys::wc_ecc_import_raw(ecc.wc_ecc_key, qx_ptr, qy_ptr, d_ptr,
                curve_name_ptr)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Import raw ECC key from components in hexadecimal ASCII string format
    /// with curve ID specified.
    ///
    /// # Parameters
    ///
    /// * `qx`: X component of public key as null terminated ASCII hex string.
    /// * `qy`: Y component of public key as null terminated ASCII hex string.
    /// * `d`: Private key as null terminated ASCII hex string.
    /// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(ecc_import)]
    /// {
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let qx = b"7a4e287890a1a47ad3457e52f2f76a83ce46cbc947616d0cbaa82323818a793d\0";
    /// let qy = b"eec4084f5b29ebf29c44cce3b3059610922f8b30ea6e8811742ac7238fe87308\0";
    /// let d  = b"8c14b793cb19137e323a6d2e2a870bca2e7a493ec1153b3a95feb8a4873f8d08\0";
    /// ECC::import_raw_ex(qx, qy, d, ECC::SECP256R1, None, None).expect("Error with import_raw_ex()");
    /// }
    /// ```
    #[cfg(ecc_import)]
    pub fn import_raw_ex(qx: &[u8], qy: &[u8], d: &[u8], curve_id: i32, heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
        if qx.is_empty() || qy.is_empty() || d.is_empty() ||
            qx[qx.len() - 1] != 0 || qy[qy.len() - 1] != 0 || d[d.len() - 1] != 0 {
            return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG);
        }
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let qx_ptr = qx.as_ptr() as *const core::ffi::c_char;
        let qy_ptr = qy.as_ptr() as *const core::ffi::c_char;
        let d_ptr = d.as_ptr() as *const core::ffi::c_char;
        let rc = unsafe {
            sys::wc_ecc_import_raw_ex(ecc.wc_ecc_key, qx_ptr, qy_ptr,
                d_ptr, curve_id)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Import raw ECC key from components in binary unsigned integer format
    /// with curve ID specified.
    ///
    /// # Parameters
    ///
    /// * `qx`: X component of public key in binary unsigned integer format.
    /// * `qy`: Y component of public key in binary unsigned integer format.
    /// * `d`: Private key in binary unsigned integer format.
    /// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_import, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let curve_id = ECC::SECP256R1;
    /// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
    /// let mut ecc = ECC::generate_ex(curve_size, &rng, curve_id, None, None).expect("Error with generate()");
    /// let mut qx = [0u8; 32];
    /// let mut qx_len = 0u32;
    /// let mut qy = [0u8; 32];
    /// let mut qy_len = 0u32;
    /// let mut d = [0u8; 32];
    /// let mut d_len = 0u32;
    /// ecc.export_ex(&mut qx, &mut qx_len, &mut qy, &mut qy_len, &mut d, &mut d_len, false).expect("Error with export_ex()");
    /// let mut ecc2 = ECC::import_unsigned(&qx, &qy, &d, curve_id, None, None).expect("Error with import_unsigned()");
    /// }
    /// ```
    #[cfg(ecc_import)]
    pub fn import_unsigned(qx: &[u8], qy: &[u8], d: &[u8], curve_id: i32, heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
        let curve_size = Self::get_curve_size_from_id(curve_id)? as usize;
        if qx.len() < curve_size || qy.len() < curve_size || d.len() < curve_size {
            return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG);
        }
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let rc = unsafe {
            sys::wc_ecc_import_unsigned(ecc.wc_ecc_key, qx.as_ptr(),
                qy.as_ptr(), d.as_ptr(), curve_id)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Import a public ECC key from the given buffer containing the key stored
    /// in ANSI X9.63 format. This function handles both compressed and
    /// uncompressed keys, as long as compressed keys are enabled at compile
    /// time with the HAVE_COMP_KEY build option.
    ///
    /// # Parameters
    ///
    /// * `din`: Buffer containing the ECC key encoded in ANSI X9.63 format.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_import, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let mut x963 = [0u8; 128];
    /// let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
    /// let x963 = &x963[0..x963_size];
    /// let _ecc2 = ECC::import_x963(x963, None, None).expect("Error with import_x963()");
    /// }
    /// ```
    #[cfg(ecc_import)]
    pub fn import_x963(din: &[u8], heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<ECC, i32> {
        let din_size = crate::buffer_len_to_u32(din.len())?;
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let rc = unsafe {
            sys::wc_ecc_import_x963(din.as_ptr(), din_size, ecc.wc_ecc_key)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Import a public ECC key from the given buffer containing the key stored
    /// in ANSI X9.63 format. This function handles both compressed and
    /// uncompressed keys, as long as compressed keys are enabled at compile
    /// time with the HAVE_COMP_KEY build option.
    ///
    /// This function allows specifying the ECC curve ID to use.
    ///
    /// # Parameters
    ///
    /// * `din`: Buffer containing the ECC key encoded in ANSI X9.63 format.
    /// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
    /// * `heap`: Optional heap hint.
    /// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_import, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let curve_id = ECC::SECP256R1;
    /// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
    /// let mut ecc = ECC::generate_ex(curve_size, &rng, curve_id, None, None).expect("Error with generate_ex()");
    /// let mut x963 = [0u8; 128];
    /// let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
    /// let x963 = &x963[0..x963_size];
    /// let _ecc2 = ECC::import_x963_ex(x963, curve_id, None, None).expect("Error with import_x963_ex()");
    /// }
    /// ```
    #[cfg(ecc_import)]
    pub fn import_x963_ex(din: &[u8], curve_id: i32, heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<ECC, i32> {
        let din_size = crate::buffer_len_to_u32(din.len())?;
        let heap = heap.unwrap_or(core::ptr::null_mut());
        let dev_id = dev_id.unwrap_or(sys::INVALID_DEVID);
        let wc_ecc_key = Self::new_ecc_key(heap, dev_id)?;
        let ecc = ECC {
            wc_ecc_key,
            #[cfg(random)]
            rng: None,
        };
        let rc = unsafe {
            sys::wc_ecc_import_x963_ex(din.as_ptr(), din_size, ecc.wc_ecc_key, curve_id)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc)
    }

    /// Convert the R and S portions (as hexadecimal ASCII strings) of an ECC
    /// signature into a DER-encoded ECDSA signature.
    ///
    /// # Parameters
    ///
    /// * `r`: R component of ECC signature as a null-terminated hexadecimal
    ///   ASCII string.
    /// * `s`: S component of ECC signature as a null-terminated hexadecimal
    ///   ASCII string.
    /// * `dout`: Buffer in which to store the output ECDSA signature.
    ///
    /// # Returns
    ///
    /// Returns either Ok(size) containing the number of bytes written to
    /// `dout` or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use std::fs;
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// fn bytes_to_asciiz_hex_string(bytes: &[u8]) -> String {
    ///     let mut hex_string = String::with_capacity(bytes.len() * 2 + 1);
    ///     for byte in bytes {
    ///         hex_string.push_str(&format!("{:02X}", byte));
    ///     }
    ///     hex_string.push('\0');
    ///     hex_string
    /// }
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let key_path = "../../../certs/ecc-client-key.der";
    /// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
    /// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
    /// let hash = [0x42u8; 32];
    /// let mut signature = [0u8; 128];
    /// let signature_length = ecc.sign_hash(&hash, &mut signature, &rng).expect("Error with sign_hash()");
    /// let signature = &mut signature[0..signature_length];
    /// let mut r = [0u8; 32];
    /// let mut r_size = 0u32;
    /// let mut s = [0u8; 32];
    /// let mut s_size = 0u32;
    /// ECC::sig_to_rs(signature, &mut r, &mut r_size, &mut s, &mut s_size).expect("Error with sig_to_rs()");
    /// let r = &r[0..r_size as usize];
    /// let s = &s[0..s_size as usize];
    /// let r_hex_string = bytes_to_asciiz_hex_string(r);
    /// let s_hex_string = bytes_to_asciiz_hex_string(s);
    /// let mut sig_out = [0u8; 128];
    /// let sig_out_size = ECC::rs_hex_to_sig(&r_hex_string[0..r_hex_string.len()].as_bytes(), &s_hex_string[0..s_hex_string.len()].as_bytes(), &mut sig_out).expect("Error with rs_hex_to_sig()");
    /// assert_eq!(*signature, *&sig_out[0..sig_out_size]);
    /// }
    /// ```
    pub fn rs_hex_to_sig(r: &[u8], s: &[u8], dout: &mut [u8]) -> Result<usize, i32> {
        if r.is_empty() || s.is_empty() || r[r.len() - 1] != 0 || s[s.len() - 1] != 0 {
            return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG);
        }
        let mut dout_size = crate::buffer_len_to_u32(dout.len())?;
        let r_ptr = r.as_ptr() as *const core::ffi::c_char;
        let s_ptr = s.as_ptr() as *const core::ffi::c_char;
        let rc = unsafe {
            sys::wc_ecc_rs_to_sig(r_ptr, s_ptr, dout.as_mut_ptr(),
                &mut dout_size)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(dout_size as usize)
    }

    /// Convert the R and S portions (as binary unsigned integers) of an ECC
    /// signature into a DER-encoded ECDSA signature.
    ///
    /// # Parameters
    ///
    /// * `r`: R component of ECC signature as a binary unsigned integer.
    /// * `s`: S component of ECC signature as a binary unsigned integer.
    /// * `dout`: Buffer in which to store the output ECDSA signature.
    ///
    /// # Returns
    ///
    /// Returns either Ok(size) containing the number of bytes written to
    /// `dout` or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use std::fs;
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let key_path = "../../../certs/ecc-client-key.der";
    /// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
    /// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
    /// let hash = [0x42u8; 32];
    /// let mut signature = [0u8; 128];
    /// let signature_length = ecc.sign_hash(&hash, &mut signature, &rng).expect("Error with sign_hash()");
    /// let signature = &mut signature[0..signature_length];
    /// let mut r = [0u8; 32];
    /// let mut r_size = 0u32;
    /// let mut s = [0u8; 32];
    /// let mut s_size = 0u32;
    /// ECC::sig_to_rs(signature, &mut r, &mut r_size, &mut s, &mut s_size).expect("Error with sig_to_rs()");
    /// let r = &r[0..r_size as usize];
    /// let s = &s[0..s_size as usize];
    /// let mut sig_out = [0u8; 128];
    /// let sig_out_size = ECC::rs_bin_to_sig(r, s, &mut sig_out).expect("Error with rs_bin_to_sig()");
    /// assert_eq!(*signature, *&sig_out[0..sig_out_size]);
    /// }
    /// ```
    pub fn rs_bin_to_sig(r: &[u8], s: &[u8], dout: &mut [u8]) -> Result<usize, i32> {
        let r_size = crate::buffer_len_to_u32(r.len())?;
        let s_size = crate::buffer_len_to_u32(s.len())?;
        let mut dout_size = crate::buffer_len_to_u32(dout.len())?;
        let rc = unsafe {
            sys::wc_ecc_rs_raw_to_sig(r.as_ptr(), r_size, s.as_ptr(), s_size,
                dout.as_mut_ptr(), &mut dout_size)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(dout_size as usize)
    }

    /// Convert ECDSA signature to R and S components.
    ///
    /// # Parameters
    ///
    /// * `sig`: ECDSA signature.
    /// * `r`: Output buffer for R component.
    /// * `r_size`: Number of bytes written to `r` buffer.
    /// * `s`: Output buffer for S component.
    /// * `s_size`: Number of bytes written to `s` buffer.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use std::fs;
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let key_path = "../../../certs/ecc-client-key.der";
    /// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
    /// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
    /// let hash = [0x42u8; 32];
    /// let mut signature = [0u8; 128];
    /// let signature_length = ecc.sign_hash(&hash, &mut signature, &rng).expect("Error with sign_hash()");
    /// let signature = &mut signature[0..signature_length];
    /// let mut r = [0u8; 32];
    /// let mut r_size = 0u32;
    /// let mut s = [0u8; 32];
    /// let mut s_size = 0u32;
    /// ECC::sig_to_rs(signature, &mut r, &mut r_size, &mut s, &mut s_size).expect("Error with sig_to_rs()");
    /// let r = &r[0..r_size as usize];
    /// let s = &s[0..s_size as usize];
    /// let mut sig_out = [0u8; 128];
    /// let sig_out_size = ECC::rs_bin_to_sig(r, s, &mut sig_out).expect("Error with rs_bin_to_sig()");
    /// assert_eq!(*signature, *&sig_out[0..sig_out_size]);
    /// }
    /// ```
    pub fn sig_to_rs(sig: &[u8], r: &mut [u8], r_size: &mut u32, s: &mut [u8], s_size: &mut u32) -> Result<(), i32> {
        let sig_len = crate::buffer_len_to_u32(sig.len())?;
        *r_size = crate::buffer_len_to_u32(r.len())?;
        *s_size = crate::buffer_len_to_u32(s.len())?;
        let rc = unsafe {
            sys::wc_ecc_sig_to_rs(sig.as_ptr(), sig_len,
                r.as_mut_ptr(), r_size, s.as_mut_ptr(), s_size)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(())
    }

    /// Perform basic sanity checks on the ECC key.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
    /// containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// ecc.check().expect("Error with check()");
    /// }
    /// ```
    pub fn check(&mut self) -> Result<(), i32> {
        let rc = unsafe { sys::wc_ecc_check_key(self.wc_ecc_key) };
        if rc != 0 {
            return Err(rc);
        }
        Ok(())
    }

    /// Export ECC key components in binary unsigned integer format.
    ///
    /// # Parameters
    ///
    /// * `qx`: Buffer in which to store public X component.
    /// * `qx_len`: Output parameter storing number of bytes written to `qx`.
    /// * `qy`: Buffer in which to store public Y component.
    /// * `qy_len`: Output parameter storing number of bytes written to `qy`.
    /// * `d`: Buffer in which to store private component.
    /// * `d_len`: Output parameter storing number of bytes written to `d`.
    ///
    /// # Returns
    ///
    /// Returns either Ok(()) or Err(e) containing the wolfSSL library error
    /// code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_import, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let mut qx = [0u8; 32];
    /// let mut qx_len = 0u32;
    /// let mut qy = [0u8; 32];
    /// let mut qy_len = 0u32;
    /// let mut d = [0u8; 32];
    /// let mut d_len = 0u32;
    /// ecc.export(&mut qx, &mut qx_len, &mut qy, &mut qy_len, &mut d, &mut d_len).expect("Error with export()");
    /// }
    /// ```
    #[cfg(ecc_import)]
    pub fn export(&mut self, qx: &mut [u8], qx_len: &mut u32,
            qy: &mut [u8], qy_len: &mut u32, d: &mut [u8], d_len: &mut u32) -> Result<(), i32> {
        *qx_len = crate::buffer_len_to_u32(qx.len())?;
        *qy_len = crate::buffer_len_to_u32(qy.len())?;
        *d_len = crate::buffer_len_to_u32(d.len())?;
        let rc = unsafe {
            sys::wc_ecc_export_private_raw(self.wc_ecc_key,
                qx.as_mut_ptr(), qx_len,
                qy.as_mut_ptr(), qy_len,
                d.as_mut_ptr(), d_len)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(())
    }

    /// Export ECC key components as either ASCII hexadecimal strings or
    /// in binary unsigned integer format.
    ///
    /// # Parameters
    ///
    /// * `qx`: Buffer in which to store public X component.
    /// * `qx_len`: Output parameter storing number of bytes written to `qx`.
    /// * `qy`: Buffer in which to store public Y component.
    /// * `qy_len`: Output parameter storing number of bytes written to `qy`.
    /// * `d`: Buffer in which to store private component.
    /// * `d_len`: Output parameter storing number of bytes written to `d`.
    /// * `hex`: true to output in ASCII hexadecimal string, false to output
    ///   as binary data.
    ///
    /// # Returns
    ///
    /// Returns either Ok(()) or Err(e) containing the wolfSSL library error
    /// code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_import, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let mut qx = [0u8; 32];
    /// let mut qx_len = 0u32;
    /// let mut qy = [0u8; 32];
    /// let mut qy_len = 0u32;
    /// let mut d = [0u8; 32];
    /// let mut d_len = 0u32;
    /// ecc.export_ex(&mut qx, &mut qx_len, &mut qy, &mut qy_len, &mut d, &mut d_len, false).expect("Error with export_ex()");
    /// }
    /// ```
    #[cfg(ecc_import)]
    #[allow(clippy::too_many_arguments)]
    pub fn export_ex(&mut self, qx: &mut [u8], qx_len: &mut u32,
            qy: &mut [u8], qy_len: &mut u32, d: &mut [u8], d_len: &mut u32,
            hex: bool) -> Result<(), i32> {
        *qx_len = crate::buffer_len_to_u32(qx.len())?;
        *qy_len = crate::buffer_len_to_u32(qy.len())?;
        *d_len = crate::buffer_len_to_u32(d.len())?;
        let enc_type =
            if hex {
                sys::WC_TYPE_HEX_STR as i32
            } else {
                sys::WC_TYPE_UNSIGNED_BIN as i32
            };
        let rc = unsafe {
            sys::wc_ecc_export_ex(self.wc_ecc_key,
                qx.as_mut_ptr(), qx_len,
                qy.as_mut_ptr(), qy_len,
                d.as_mut_ptr(), d_len,
                enc_type)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(())
    }

    /// Export private component from ECC key in binary unsigned integer form.
    ///
    /// # Parameters
    ///
    /// * `d`: Buffer in which to store private component.
    ///
    /// # Returns
    ///
    /// Returns either Ok(size) containing the number of bytes written to `d`
    /// or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_export, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let mut d = [0u8; 32];
    /// let d_size = ecc.export_private(&mut d).expect("Error with export_private()");
    /// assert_eq!(d_size, 32);
    /// }
    /// ```
    #[cfg(ecc_export)]
    pub fn export_private(&mut self, d: &mut [u8]) -> Result<usize, i32> {
        let mut d_size = crate::buffer_len_to_u32(d.len())?;
        let rc = unsafe {
            sys::wc_ecc_export_private_only(self.wc_ecc_key,
                d.as_mut_ptr(), &mut d_size)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(d_size as usize)
    }

    /// Export public ECC key components in binary unsigned integer format.
    ///
    /// # Parameters
    ///
    /// * `qx`: Buffer in which to store public X component.
    /// * `qx_len`: Output parameter storing number of bytes written to `qx`.
    /// * `qy`: Buffer in which to store public Y component.
    /// * `qy_len`: Output parameter storing number of bytes written to `qy`.
    ///
    /// # Returns
    ///
    /// Returns either Ok(()) or Err(e) containing the wolfSSL library error
    /// code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_export, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let mut qx = [0u8; 32];
    /// let mut qx_len = 0u32;
    /// let mut qy = [0u8; 32];
    /// let mut qy_len = 0u32;
    /// ecc.export_public(&mut qx, &mut qx_len, &mut qy, &mut qy_len).expect("Error with export_public()");
    /// }
    /// ```
    #[cfg(ecc_export)]
    pub fn export_public(&mut self, qx: &mut [u8], qx_len: &mut u32,
            qy: &mut [u8], qy_len: &mut u32) -> Result<(), i32> {
        *qx_len = crate::buffer_len_to_u32(qx.len())?;
        *qy_len = crate::buffer_len_to_u32(qy.len())?;
        let rc = unsafe {
            sys::wc_ecc_export_public_raw(self.wc_ecc_key,
                qx.as_mut_ptr(), qx_len,
                qy.as_mut_ptr(), qy_len)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(())
    }

    /// Export public key in ANSI X9.63 format.
    ///
    /// # Parameters
    ///
    /// * `dout`: Buffer to contain the output.
    ///
    /// # Returns
    ///
    /// Returns either Ok(size) containing the number of bytes written to
    /// `dout` or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_export, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let mut x963 = [0u8; 128];
    /// let _x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
    /// }
    /// ```
    #[cfg(ecc_export)]
    pub fn export_x963(&mut self, dout: &mut [u8]) -> Result<usize, i32> {
        let mut out_len = crate::buffer_len_to_u32(dout.len())?;
        let rc = unsafe {
            sys::wc_ecc_export_x963(self.wc_ecc_key, dout.as_mut_ptr(), &mut out_len)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(out_len as usize)
    }

    /// Export public key in ANSI X9.63 compressed format.
    ///
    /// # Parameters
    ///
    /// * `dout`: Buffer to contain the output.
    ///
    /// # Returns
    ///
    /// Returns either Ok(size) containing the number of bytes written to
    /// `dout` or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_export, ecc_comp_key, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let mut x963 = [0u8; 128];
    /// let _x963_size = ecc.export_x963_compressed(&mut x963).expect("Error with export_x963_compressed()");
    /// }
    /// ```
    #[cfg(all(ecc_export, ecc_comp_key))]
    pub fn export_x963_compressed(&mut self, dout: &mut [u8]) -> Result<usize, i32> {
        let mut out_len = crate::buffer_len_to_u32(dout.len())?;
        let rc = unsafe {
            sys::wc_ecc_export_x963_ex(self.wc_ecc_key, dout.as_mut_ptr(), &mut out_len, 1)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(out_len as usize)
    }

    /// Compute the public component from this key private component.
    ///
    /// # Parameters
    ///
    /// * `rng`: RNG struct used to blind the private key value used in the
    ///   computation.
    ///
    /// # Returns
    ///
    /// Returns either Ok(()) or Err(e) containing the wolfSSL library error
    /// code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use std::fs;
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let key_path = "../../../certs/ecc-client-key.der";
    /// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
    /// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
    /// ecc.make_pub(Some(&rng)).expect("Error with make_pub()");
    /// }
    /// ```
    #[cfg(random)]
    pub fn make_pub(&mut self, rng: Option<&RNG>) -> Result<(), i32> {
        let rng_ptr = match rng {
            Some(rng) => rng.wc_rng,
            None => core::ptr::null_mut(),
        };
        let rc = unsafe {
            sys::wc_ecc_make_pub_ex(self.wc_ecc_key, core::ptr::null_mut(), rng_ptr)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(())
    }

    /// Compute the public component from this key private component.
    ///
    /// # Parameters
    ///
    /// * `rng`: RNG struct used to blind the private key value used in the
    ///   computation.
    /// * `heap`: Optional heap hint.
    ///
    /// # Returns
    ///
    /// Returns either Ok(ECCPoint) containing the public component ECCPoint
    /// or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use std::fs;
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let key_path = "../../../certs/ecc-client-key.der";
    /// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
    /// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
    /// ecc.make_pub_to_point(Some(&rng), None).expect("Error with make_pub_to_point()");
    /// }
    /// ```
    #[cfg(random)]
    pub fn make_pub_to_point(&mut self, rng: Option<&RNG>, heap: Option<*mut core::ffi::c_void>) -> Result<ECCPoint, i32> {
        let rng_ptr = match rng {
            Some(rng) => rng.wc_rng,
            None => core::ptr::null_mut(),
        };
        let heap = match heap {
            Some(heap) => heap,
            None => core::ptr::null_mut(),
        };
        let wc_ecc_point = unsafe { sys::wc_ecc_new_point_h(heap) };
        if wc_ecc_point.is_null() {
            return Err(sys::wolfCrypt_ErrorCodes_MEMORY_E);
        }
        let ecc_point = ECCPoint { wc_ecc_point, heap };
        let rc = unsafe {
            sys::wc_ecc_make_pub_ex(self.wc_ecc_key, wc_ecc_point, rng_ptr)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(ecc_point)
    }

    /// Associates a `RNG` instance with this `ECC` instance.
    ///
    /// This is necessary when wolfSSL is built with the `ECC_TIMING_RESISTANT`
    /// build option enabled.
    ///
    /// # Parameters
    ///
    /// * `rng`: The `RNG` struct instance to associate with this `ECC`
    ///   instance.
    ///
    /// # Returns
    ///
    /// Returns Ok(()) on success or Err(e) containing the wolfSSL library
    /// error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// # extern crate std;
    /// #[cfg(random)]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let blinding_rng = RNG::new().expect("Failed to create RNG");
    /// let key_gen_rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &key_gen_rng, None, None).expect("Error with generate()");
    /// ecc.set_rng(blinding_rng).expect("Error with set_rng()");
    /// }
    /// ```
    #[cfg(random)]
    pub fn set_rng(&mut self, rng: RNG) -> Result<(), i32> {
        let wc_rng = rng.wc_rng;
        let rc = unsafe {
            sys::wc_ecc_set_rng(self.wc_ecc_key, wc_rng)
        };
        if rc != 0 {
            return Err(rc);
        }
        self.rng = Some(RngHandle::Owned(rng));
        Ok(())
    }

    /// Bind a shared `RNG` to this key. Available when the `alloc` feature
    /// is enabled.
    #[cfg(all(random, feature = "alloc"))]
    pub fn set_shared_rng(&mut self, rng: alloc::rc::Rc<RNG>) -> Result<(), i32> {
        let wc_rng = rng.wc_rng;
        let rc = unsafe {
            sys::wc_ecc_set_rng(self.wc_ecc_key, wc_rng)
        };
        if rc != 0 {
            return Err(rc);
        }
        self.rng = Some(RngHandle::Shared(rng));
        Ok(())
    }

    /// Borrow the RNG previously bound via `set_rng` or `set_shared_rng`.
    #[cfg(random)]
    pub fn rng(&self) -> Option<&RNG> {
        match &self.rng {
            Some(RngHandle::Owned(rng)) => Some(rng),
            #[cfg(feature = "alloc")]
            Some(RngHandle::Shared(rng)) => Some(rng),
            None => None,
        }
    }

    /// Compute the ECDH shared secret using this key's private component
    /// and the peer public key.
    ///
    /// # Parameters
    ///
    /// * `peer`: `ECC` public key.
    /// * `dout`: Buffer in which to store the computed secret value.
    ///
    /// # Returns
    ///
    /// Returns either Ok(size) containing the number of bytes written to
    /// `dout` or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_dh, random, feature = "alloc"))]
    /// {
    /// use std::rc::Rc;
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = Rc::new(RNG::new().expect("Failed to create RNG"));
    /// let mut ecc0 = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let mut ecc1 = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let mut ss0 = [0u8; 128];
    /// let mut ss1 = [0u8; 128];
    /// ecc0.set_shared_rng(Rc::clone(&rng)).expect("Error with set_shared_rng()");
    /// ecc1.set_shared_rng(Rc::clone(&rng)).expect("Error with set_shared_rng()");
    /// let ss0_size = ecc0.shared_secret(&mut ecc1, &mut ss0).expect("Error with shared_secret()");
    /// let ss1_size = ecc1.shared_secret(&mut ecc0, &mut ss1).expect("Error with shared_secret()");
    /// assert_eq!(ss0_size, ss1_size);
    /// let ss0 = &ss0[0..ss0_size];
    /// let ss1 = &ss1[0..ss1_size];
    /// assert_eq!(*ss0, *ss1);
    /// }
    /// ```
    #[cfg(ecc_dh)]
    pub fn shared_secret(&mut self, peer_key: &mut ECC, dout: &mut [u8]) -> Result<usize, i32> {
        let mut out_len = crate::buffer_len_to_u32(dout.len())?;
        let rc = unsafe {
            sys::wc_ecc_shared_secret(self.wc_ecc_key,
                peer_key.wc_ecc_key, dout.as_mut_ptr(), &mut out_len)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(out_len as usize)
    }

    /// Compute the ECDH shared secret using this key's private component
    /// and the peer public point.
    ///
    /// # Parameters
    ///
    /// * `peer`: `ECCPoint` struct holding the public components of the peer
    ///   ECC key.
    /// * `dout`: Buffer in which to store the computed secret value.
    ///
    /// # Returns
    ///
    /// Returns either Ok(size) containing the number of bytes written to
    /// `dout` or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_dh, random, feature = "alloc"))]
    /// {
    /// use std::rc::Rc;
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = Rc::new(RNG::new().expect("Failed to create RNG"));
    /// let mut ecc0 = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let mut ecc1 = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let ecc1_point = ecc1.make_pub_to_point(None, None).expect("Error with make_pub_to_point()");
    /// let mut ss0 = [0u8; 128];
    /// let mut ss1 = [0u8; 128];
    /// ecc0.set_shared_rng(Rc::clone(&rng)).expect("Error with set_shared_rng()");
    /// ecc1.set_shared_rng(Rc::clone(&rng)).expect("Error with set_shared_rng()");
    /// let ss0_size = ecc0.shared_secret_ex(&ecc1_point, &mut ss0).expect("Error with shared_secret_ex()");
    /// let ss1_size = ecc1.shared_secret(&mut ecc0, &mut ss1).expect("Error with shared_secret()");
    /// assert_eq!(ss0_size, ss1_size);
    /// let ss0 = &ss0[0..ss0_size];
    /// let ss1 = &ss1[0..ss1_size];
    /// assert_eq!(*ss0, *ss1);
    /// }
    /// ```
    #[cfg(ecc_dh)]
    pub fn shared_secret_ex(&mut self, peer: &ECCPoint, dout: &mut [u8]) -> Result<usize, i32> {
        let mut out_len = crate::buffer_len_to_u32(dout.len())?;
        let rc = unsafe {
            sys::wc_ecc_shared_secret_ex(self.wc_ecc_key,
                peer.wc_ecc_point, dout.as_mut_ptr(), &mut out_len)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(out_len as usize)
    }

    /// Sign a message digest using the ECC key.
    ///
    /// # Parameters
    ///
    /// * `din`: Message digest to sign.
    /// * `dout`: Buffer in which to store the signature.
    /// * `rng`: RNG struct to use for random number generation during signing.
    ///
    /// # Returns
    ///
    /// Returns either Ok(size) containing the number of bytes written to
    /// `dout` or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_sign, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let hash = [0x42u8; 32];
    /// let mut signature = [0u8; 128];
    /// let signature_length = ecc.sign_hash(&hash, &mut signature, &rng).expect("Error with sign_hash()");
    /// let signature = &mut signature[0..signature_length];
    /// let valid = ecc.verify_hash(&signature, &hash).expect("Error with verify_hash()");
    /// assert_eq!(valid, true);
    /// }
    /// ```
    #[cfg(all(ecc_sign, random))]
    pub fn sign_hash(&mut self, din: &[u8], dout: &mut [u8], rng: &RNG) -> Result<usize, i32> {
        let din_size = crate::buffer_len_to_u32(din.len())?;
        let mut dout_size = crate::buffer_len_to_u32(dout.len())?;
        let rc = unsafe {
            sys::wc_ecc_sign_hash(din.as_ptr(), din_size, dout.as_mut_ptr(),
                &mut dout_size, rng.wc_rng, self.wc_ecc_key)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(dout_size as usize)
    }

    /// Verify the ECC signature of a hash.
    ///
    /// # Parameters
    ///
    /// * `sig`: ECC signature.
    /// * `hash`: Message digest.
    ///
    /// # Returns
    ///
    /// Returns either Ok(valid) containing a flag for whether the signature is
    /// valid or Err(e) containing the wolfSSL library error code value.
    ///
    /// # Example
    ///
    /// ```rust
    /// #[cfg(all(ecc_verify, random))]
    /// {
    /// use wolfssl_wolfcrypt::random::RNG;
    /// use wolfssl_wolfcrypt::ecc::ECC;
    /// let rng = RNG::new().expect("Failed to create RNG");
    /// let mut ecc = ECC::generate(32, &rng, None, None).expect("Error with generate()");
    /// let hash = [0x42u8; 32];
    /// let mut signature = [0u8; 128];
    /// let signature_length = ecc.sign_hash(&hash, &mut signature, &rng).expect("Error with sign_hash()");
    /// let signature = &mut signature[0..signature_length];
    /// let valid = ecc.verify_hash(&signature, &hash).expect("Error with verify_hash()");
    /// assert_eq!(valid, true);
    /// }
    /// ```
    #[cfg(ecc_verify)]
    pub fn verify_hash(&mut self, sig: &[u8], hash: &[u8]) -> Result<bool, i32> {
        let mut res: i32 = 0;
        let sig_len = crate::buffer_len_to_u32(sig.len())?;
        let hash_len = crate::buffer_len_to_u32(hash.len())?;
        let rc = unsafe {
            sys::wc_ecc_verify_hash(sig.as_ptr(), sig_len,
                hash.as_ptr(), hash_len, &mut res, self.wc_ecc_key)
        };
        if rc != 0 {
            return Err(rc);
        }
        Ok(res == 1)
    }
}

impl Drop for ECC {
    /// Safely free the underlying wolfSSL ECC context.
    ///
    /// This calls the `wc_ecc_key_free()` wolfssl library function, which
    /// frees the C-heap-allocated `ecc_key` object.
    ///
    /// The Rust Drop trait guarantees that this method is called when the ECC
    /// struct goes out of scope, automatically cleaning up resources and
    /// preventing memory leaks.
    fn drop(&mut self) {
        unsafe { sys::wc_ecc_key_free(self.wc_ecc_key); }
    }
}