-
Notifications
You must be signed in to change notification settings - Fork 970
Expand file tree
/
Copy pathecc.rs
More file actions
1989 lines (1941 loc) · 76.2 KB
/
ecc.rs
File metadata and controls
1989 lines (1941 loc) · 76.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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;
use core::mem::{MaybeUninit};
/// 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 mut 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, &mut rng, curve_id, None, None).expect("Error with generate()");
/// let ecc_point = ecc.make_pub_to_point(Some(&mut 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 mut 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, &mut rng, curve_id, None, None).expect("Error with generate()");
/// let ecc_point = ecc.make_pub_to_point(Some(&mut 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 mut 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, &mut rng, curve_id, None, None).expect("Error with generate()");
/// let ecc_point = ecc.make_pub_to_point(Some(&mut 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 mut 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, &mut rng, curve_id, None, None).expect("Error with generate()");
/// let ecc_point = ecc.make_pub_to_point(Some(&mut 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 mut rng = RNG::new().expect("Failed to create RNG");
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
/// let mut ecc_point = ecc.make_pub_to_point(Some(&mut 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.
///
/// 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: sys::ecc_key,
}
#[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;
/// 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 mut rng = RNG::new().expect("Failed to create RNG");
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
/// ecc.check().expect("Error with check()");
/// }
/// ```
#[cfg(random)]
pub fn generate(size: i32, rng: &mut RNG, heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = MaybeUninit::uninit();
let heap = match heap {
Some(heap) => heap,
None => core::ptr::null_mut(),
};
let dev_id = match dev_id {
Some(dev_id) => dev_id,
None => sys::INVALID_DEVID,
};
let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
if rc != 0 {
return Err(rc);
}
let wc_ecc_key = unsafe { wc_ecc_key.assume_init() };
let mut ecc = ECC { wc_ecc_key };
let rc = unsafe {
sys::wc_ecc_make_key(&mut rng.wc_rng, size, &mut 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 mut 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, &mut 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: &mut RNG, curve_id: i32, heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = MaybeUninit::uninit();
let heap = match heap {
Some(heap) => heap,
None => core::ptr::null_mut(),
};
let dev_id = match dev_id {
Some(dev_id) => dev_id,
None => sys::INVALID_DEVID,
};
let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
if rc != 0 {
return Err(rc);
}
let wc_ecc_key = unsafe { wc_ecc_key.assume_init() };
let mut ecc = ECC { wc_ecc_key };
let rc = unsafe {
sys::wc_ecc_make_key_ex(&mut rng.wc_rng, size, &mut 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 mut 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, &mut 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: &mut RNG, curve_id: i32, flags: i32, heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = MaybeUninit::uninit();
let heap = match heap {
Some(heap) => heap,
None => core::ptr::null_mut(),
};
let dev_id = match dev_id {
Some(dev_id) => dev_id,
None => sys::INVALID_DEVID,
};
let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
if rc != 0 {
return Err(rc);
}
let wc_ecc_key = unsafe { wc_ecc_key.assume_init() };
let mut ecc = ECC { wc_ecc_key };
let rc = unsafe {
sys::wc_ecc_make_key_ex2(&mut rng.wc_rng, size, &mut 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 mut 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, &mut 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 mut 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 mut wc_ecc_key: MaybeUninit<sys::ecc_key> = MaybeUninit::uninit();
let heap = match heap {
Some(heap) => heap,
None => core::ptr::null_mut(),
};
let dev_id = match dev_id {
Some(dev_id) => dev_id,
None => sys::INVALID_DEVID,
};
let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
if rc != 0 {
return Err(rc);
}
let wc_ecc_key = unsafe { wc_ecc_key.assume_init() };
let mut ecc = ECC { wc_ecc_key };
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, &mut 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 mut 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, &mut 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 mut wc_ecc_key: MaybeUninit<sys::ecc_key> = MaybeUninit::uninit();
let heap = match heap {
Some(heap) => heap,
None => core::ptr::null_mut(),
};
let dev_id = match dev_id {
Some(dev_id) => dev_id,
None => sys::INVALID_DEVID,
};
let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
if rc != 0 {
return Err(rc);
}
let wc_ecc_key = unsafe { wc_ecc_key.assume_init() };
let mut ecc = ECC { wc_ecc_key };
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, &mut 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 mut rng = RNG::new().expect("Failed to create RNG");
/// let mut ecc = ECC::generate(32, &mut 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, &mut 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 mut wc_ecc_key: MaybeUninit<sys::ecc_key> = MaybeUninit::uninit();
let heap = match heap {
Some(heap) => heap,
None => core::ptr::null_mut(),
};
let dev_id = match dev_id {
Some(dev_id) => dev_id,
None => sys::INVALID_DEVID,
};
let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
if rc != 0 {
return Err(rc);
}
let wc_ecc_key = unsafe { wc_ecc_key.assume_init() };
let mut ecc = ECC { wc_ecc_key };
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, &mut 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 mut 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, &mut 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, &mut 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 mut wc_ecc_key: MaybeUninit<sys::ecc_key> = MaybeUninit::uninit();
let heap = match heap {
Some(heap) => heap,
None => core::ptr::null_mut(),
};
let dev_id = match dev_id {
Some(dev_id) => dev_id,
None => sys::INVALID_DEVID,
};
let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
if rc != 0 {
return Err(rc);
}
let wc_ecc_key = unsafe { wc_ecc_key.assume_init() };
let mut ecc = ECC { wc_ecc_key };
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, &mut 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> {
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = MaybeUninit::uninit();
let heap = match heap {
Some(heap) => heap,
None => core::ptr::null_mut(),
};
let dev_id = match dev_id {
Some(dev_id) => dev_id,
None => sys::INVALID_DEVID,
};
let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
if rc != 0 {
return Err(rc);
}
let wc_ecc_key = unsafe { wc_ecc_key.assume_init() };
let mut ecc = ECC { wc_ecc_key };
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(&mut 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> {
let mut wc_ecc_key: MaybeUninit<sys::ecc_key> = MaybeUninit::uninit();
let heap = match heap {
Some(heap) => heap,
None => core::ptr::null_mut(),
};
let dev_id = match dev_id {
Some(dev_id) => dev_id,
None => sys::INVALID_DEVID,
};
let rc = unsafe { sys::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
if rc != 0 {
return Err(rc);
}
let wc_ecc_key = unsafe { wc_ecc_key.assume_init() };
let mut ecc = ECC { wc_ecc_key };
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(&mut 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))]