-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathecc.h
More file actions
3876 lines (3146 loc) · 116 KB
/
Copy pathecc.h
File metadata and controls
3876 lines (3146 loc) · 116 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
/*!
\ingroup ECC
\brief This function generates a new ecc_key and stores it in key.
\return 0 Returned on success.
\return ECC_BAD_ARG_E Returned if rng or key evaluate to NULL
\return BAD_FUNC_ARG Returned if the specified key size is not in the
correct range of supported keys
\return MEMORY_E Returned if there is an error allocating memory while
computing the ecc key
\return MP_INIT_E may be returned if there is an error while computing
the ecc key
\return MP_READ_E may be returned if there is an error while computing
the ecc key
\return MP_CMP_E may be returned if there is an error while computing the
ecc key
\return MP_INVMOD_E may be returned if there is an error while computing
the ecc key
\return MP_EXPTMOD_E may be returned if there is an error while computing
the ecc key
\return MP_MOD_E may be returned if there is an error while computing the
ecc key
\return MP_MUL_E may be returned if there is an error while computing the
ecc key
\return MP_ADD_E may be returned if there is an error while computing the
ecc key
\return MP_MULMOD_E may be returned if there is an error while computing
the ecc key
\return MP_TO_E may be returned if there is an error while computing the
ecc key
\return MP_MEM may be returned if there is an error while computing the
ecc key
\param rng pointer to an initialized RNG object with which to generate
the key
\param keysize desired length for the ecc_key
\param key pointer to the ecc_key for which to generate a key
_Example_
\code
ecc_key key;
wc_ecc_init(&key);
WC_RNG rng;
wc_InitRng(&rng);
wc_ecc_make_key(&rng, 32, &key); // initialize 32 byte ecc key
\endcode
\sa wc_ecc_init
\sa wc_ecc_shared_secret
*/
int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key);
/*!
\ingroup ECC
\brief This function generates a new ecc_key and stores it in key.
\return 0 Returned on success.
\return ECC_BAD_ARG_E Returned if rng or key evaluate to NULL
\return BAD_FUNC_ARG Returned if the specified key size is not in the
correct range of supported keys
\return MEMORY_E Returned if there is an error allocating memory while
computing the ecc key
\return MP_INIT_E may be returned if there is an error while computing
the ecc key
\return MP_READ_E may be returned if there is an error while computing
the ecc key
\return MP_CMP_E may be returned if there is an error while computing the
ecc key
\return MP_INVMOD_E may be returned if there is an error while computing
the ecc key
\return MP_EXPTMOD_E may be returned if there is an error while computing
the ecc key
\return MP_MOD_E may be returned if there is an error while computing the
ecc key
\return MP_MUL_E may be returned if there is an error while computing the
ecc key
\return MP_ADD_E may be returned if there is an error while computing the
ecc key
\return MP_MULMOD_E may be returned if there is an error while computing
the ecc key
\return MP_TO_E may be returned if there is an error while computing the
ecc key
\return MP_MEM may be returned if there is an error while computing the
ecc key
\param key Pointer to store the created key.
\param keysize size of key to be created in bytes, set based on curveId
\param rng Rng to be used in key creation
\param curve_id Curve to use for key
_Example_
\code
ecc_key key;
int ret;
WC_RNG rng;
wc_ecc_init(&key);
wc_InitRng(&rng);
int curveId = ECC_SECP521R1;
int keySize = wc_ecc_get_curve_size_from_id(curveId);
ret = wc_ecc_make_key_ex(&rng, keySize, &key, curveId);
if (ret != MP_OKAY) {
// error handling
}
\endcode
\sa wc_ecc_make_key
\sa wc_ecc_get_curve_size_from_id
*/
int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id);
/*!
\ingroup ECC
\brief wc_ecc_make_pub computes the public component from an ecc_key with an
existing private component. If pubOut is supplied, the computed public key
is stored there, else it is stored in the supplied ecc_key public component
slot.
\return 0 Returned on success.
\return ECC_BAD_ARG_E Returned if key is NULL
\return BAD_FUNC_ARG Returned if the supplied key is not a valid ecc_key.
\return MEMORY_E Returned if there is an error allocating memory while
computing the public key
\return MP_INIT_E may be returned if there is an error while computing
the public key
\return MP_READ_E may be returned if there is an error while computing
the public key
\return MP_CMP_E may be returned if there is an error while computing the
public key
\return MP_INVMOD_E may be returned if there is an error while computing
the public key
\return MP_EXPTMOD_E may be returned if there is an error while computing
the public key
\return MP_MOD_E may be returned if there is an error while computing the
public key
\return MP_MUL_E may be returned if there is an error while computing the
public key
\return MP_ADD_E may be returned if there is an error while computing the
public key
\return MP_MULMOD_E may be returned if there is an error while computing
the public key
\return MP_TO_E may be returned if there is an error while computing the
public key
\return MP_MEM may be returned if there is an error while computing the
public key
\return ECC_OUT_OF_RANGE_E may be returned if there is an error while computing the
public key
\return ECC_PRIV_KEY_E may be returned if there is an error while computing the
public key
\return ECC_INF_E may be returned if there is an error while computing the
public key
\param key Pointer to an ecc_key containing a valid private component
\param pubOut Optional pointer to an ecc_point struct in which to store
the computed public key
\sa wc_ecc_make_pub_ex
\sa wc_ecc_make_key
*/
int wc_ecc_make_pub(ecc_key* key, ecc_point* pubOut);
/*!
\ingroup ECC
\brief wc_ecc_make_pub_ex computes the public component from an ecc_key with
an existing private component. If pubOut is supplied, the computed public
key is stored there, else it is stored in the supplied ecc_key public
component slot. The supplied rng, if non-NULL, is used to blind the private
key value used in the computation.
\return 0 Returned on success.
\return ECC_BAD_ARG_E Returned if key is NULL
\return BAD_FUNC_ARG Returned if the supplied key is not a valid ecc_key.
\return MEMORY_E Returned if there is an error allocating memory while
computing the public key
\return MP_INIT_E may be returned if there is an error while computing
the public key
\return MP_READ_E may be returned if there is an error while computing
the public key
\return MP_CMP_E may be returned if there is an error while computing the
public key
\return MP_INVMOD_E may be returned if there is an error while computing
the public key
\return MP_EXPTMOD_E may be returned if there is an error while computing
the public key
\return MP_MOD_E may be returned if there is an error while computing the
public key
\return MP_MUL_E may be returned if there is an error while computing the
public key
\return MP_ADD_E may be returned if there is an error while computing the
public key
\return MP_MULMOD_E may be returned if there is an error while computing
the public key
\return MP_TO_E may be returned if there is an error while computing the
public key
\return MP_MEM may be returned if there is an error while computing the
public key
\return ECC_OUT_OF_RANGE_E may be returned if there is an error while computing the
public key
\return ECC_PRIV_KEY_E may be returned if there is an error while computing the
public key
\return ECC_INF_E may be returned if there is an error while computing the
public key
\param key Pointer to an ecc_key containing a valid private component
\param pubOut Optional pointer to an ecc_point struct in which to store
the computed public key
\param rng Rng to be used in the public key computation
\sa wc_ecc_make_pub
\sa wc_ecc_make_key
\sa wc_InitRng
*/
int wc_ecc_make_pub_ex(ecc_key* key, ecc_point* pubOut, WC_RNG* rng);
/*!
\ingroup ECC
\brief Perform sanity checks on ecc key validity.
\return MP_OKAY Success, key is OK.
\return BAD_FUNC_ARG Returns if key is NULL.
\return ECC_INF_E Returns if wc_ecc_point_is_at_infinity returns 1.
\param key Pointer to key to check.
_Example_
\code
ecc_key key;
WC_RNG rng;
int check_result;
wc_ecc_init(&key);
wc_InitRng(&rng);
wc_ecc_make_key(&rng, 32, &key);
check_result = wc_ecc_check_key(&key);
if (check_result == MP_OKAY)
{
// key check succeeded
}
else
{
// key check failed
}
\endcode
\sa wc_ecc_point_is_at_infinity
*/
int wc_ecc_check_key(ecc_key* key);
/*!
\ingroup ECC
\brief This function frees an ecc_key key after it has been used.
\param key pointer to the ecc_key structure to free
_Example_
\code
// initialize key and perform ECC operations
...
wc_ecc_key_free(&key);
\endcode
\sa wc_ecc_key_new
\sa wc_ecc_init_ex
*/
void wc_ecc_key_free(ecc_key* key);
/*!
\ingroup ECC
\brief This function generates a new secret key using a local private key
and a received public key. It stores this shared secret key in the buffer
out and updates outlen to hold the number of bytes written to the output
buffer.
\return 0 Returned upon successfully generating a shared secret key
\return BAD_FUNC_ARG Returned if any of the input parameters evaluate to
NULL
\return ECC_BAD_ARG_E Returned if the type of the private key given as
argument, private_key, is not ECC_PRIVATEKEY, or if the public and private
key types (given by ecc->dp) are not equivalent
\return MEMORY_E Returned if there is an error generating a new ecc point
\return BUFFER_E Returned if the generated shared secret key is too long
to store in the provided buffer
\return MP_INIT_E may be returned if there is an error while computing the
shared key
\return MP_READ_E may be returned if there is an error while computing the
shared key
\return MP_CMP_E may be returned if there is an error while computing the
shared key
\return MP_INVMOD_E may be returned if there is an error while computing
the shared key
\return MP_EXPTMOD_E may be returned if there is an error while computing
the shared key
\return MP_MOD_E may be returned if there is an error while computing the
shared key
\return MP_MUL_E may be returned if there is an error while computing the
shared key
\return MP_ADD_E may be returned if there is an error while computing the
shared key
\return MP_MULMOD_E may be returned if there is an error while computing
the shared key
\return MP_TO_E may be returned if there is an error while computing the
shared key
\return MP_MEM may be returned if there is an error while computing the
shared key
\return ECC_INF_E returned when the computed shared secret is the point at
infinity
\param private_key pointer to the ecc_key structure containing the local
private key
\param public_key pointer to the ecc_key structure containing the received
public key
\param out pointer to an output buffer in which to store the generated
shared secret key
\param outlen pointer to the word32 object containing the length of the
output buffer. Will be overwritten with the length written to the output
buffer upon successfully generating a shared secret key
_Example_
\code
ecc_key priv, pub;
WC_RNG rng;
byte secret[1024]; // can hold 1024 byte shared secret key
word32 secretSz = sizeof(secret);
int ret;
wc_InitRng(&rng); // initialize rng
wc_ecc_init(&priv); // initialize key
wc_ecc_make_key(&rng, 32, &priv); // make public/private key pair
// receive public key, and initialise into pub
ret = wc_ecc_shared_secret(&priv, &pub, secret, &secretSz);
// generate secret key
if ( ret != 0 ) {
// error generating shared secret key
}
\endcode
\sa wc_ecc_init
\sa wc_ecc_make_key
*/
int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
word32* outlen);
/*!
\ingroup ECC
\brief Create an ECC shared secret between private key and public point.
\return MP_OKAY Indicates success.
\return BAD_FUNC_ARG Error returned when any arguments are null.
\return ECC_BAD_ARG_E Error returned if private_key->type is not
ECC_PRIVATEKEY or private_key->idx fails to validate.
\return BUFFER_E Error when outlen is too small.
\return MEMORY_E Error to create a new point.
\return MP_VAL possible when an initialization failure occurs.
\return MP_MEM possible when an initialization failure occurs.
\param private_key The private ECC key.
\param point The point to use (public key).
\param out Output destination of the shared secret. Conforms to
EC-DH from ANSI X9.63.
\param outlen Input the max size and output the resulting size of
the shared secret.
_Example_
\code
ecc_key key;
ecc_point* point;
byte shared_secret[];
int secret_size;
int result;
point = wc_ecc_new_point();
result = wc_ecc_shared_secret_ex(&key, point,
&shared_secret, &secret_size);
if (result != MP_OKAY)
{
// Handle error
}
\endcode
\sa wc_ecc_verify_hash_ex
*/
int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
byte* out, word32 *outlen);
/*!
\ingroup ECC
\brief This function signs a message digest using an ecc_key object to
guarantee authenticity.
\return 0 Returned upon successfully generating a signature for the
message digest
\return BAD_FUNC_ARG Returned if any of the input parameters evaluate to
NULL, or if the output buffer is too small to store the generated signature
\return ECC_BAD_ARG_E Returned if the input key is not a private key, or
if the ECC OID is invalid
\return RNG_FAILURE_E Returned if the rng cannot successfully generate a
satisfactory key
\return MP_INIT_E may be returned if there is an error while computing
the message signature
\return MP_READ_E may be returned if there is an error while computing
the message signature
\return MP_CMP_E may be returned if there is an error while computing the
message signature
\return MP_INVMOD_E may be returned if there is an error while computing
the message signature
\return MP_EXPTMOD_E may be returned if there is an error while computing
the message signature
\return MP_MOD_E may be returned if there is an error while computing the
message signature
\return MP_MUL_E may be returned if there is an error while computing the
message signature
\return MP_ADD_E may be returned if there is an error while computing the
message signature
\return MP_MULMOD_E may be returned if there is an error while computing
the message signature
\return MP_TO_E may be returned if there is an error while computing the
message signature
\return MP_MEM may be returned if there is an error while computing the
message signature
\param in pointer to the buffer containing the message hash to sign
\param inlen length of the message hash to sign
\param out buffer in which to store the generated signature
\param outlen max length of the output buffer. Will store the bytes
written to out upon successfully generating a message signature
\param key pointer to a private ECC key with which to generate the
signature
_Example_
\code
ecc_key key;
WC_RNG rng;
int ret, sigSz;
byte sig[512]; // will hold generated signature
sigSz = sizeof(sig);
byte digest[] = { // initialize with message hash };
wc_InitRng(&rng); // initialize rng
wc_ecc_init(&key); // initialize key
wc_ecc_make_key(&rng, 32, &key); // make public/private key pair
ret = wc_ecc_sign_hash(digest, sizeof(digest), sig, &sigSz, &key);
if ( ret != 0 ) {
// error generating message signature
}
\endcode
\sa wc_ecc_verify_hash
*/
int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
WC_RNG* rng, ecc_key* key);
/*!
\ingroup ECC
\brief Sign a message digest.
\return MP_OKAY Returned upon successfully generating a signature for the
message digest
\return ECC_BAD_ARG_E Returned if the input key is not a private key, or
if the ECC IDX is invalid, or if any of the input parameters evaluate to
NULL, or if the output buffer is too small to store the generated signature
\return RNG_FAILURE_E Returned if the rng cannot successfully generate a
satisfactory key
\return MP_INIT_E may be returned if there is an error while computing the
message signature
\return MP_READ_E may be returned if there is an error while computing the
message signature
\return MP_CMP_E may be returned if there is an error while computing the
message signature
\return MP_INVMOD_E may be returned if there is an error while computing
the message signature
\return MP_EXPTMOD_E may be returned if there is an error while computing
the message signature
\return MP_MOD_E may be returned if there is an error while computing the
message signature
\return MP_MUL_E may be returned if there is an error while computing the
message signature
\return MP_ADD_E may be returned if there is an error while computing the
message signature
\return MP_MULMOD_E may be returned if there is an error while computing
the message signature
\return MP_TO_E may be returned if there is an error while computing the
message signature
\return MP_MEM may be returned if there is an error while computing the
message signature
\param in The message digest to sign.
\param inlen The length of the digest.
\param rng Pointer to WC_RNG struct.
\param key A private ECC key.
\param r The destination for r component of the signature.
\param s The destination for s component of the signature.
_Example_
\code
ecc_key key;
WC_RNG rng;
int ret, sigSz;
mp_int r; // destination for r component of signature.
mp_int s; // destination for s component of signature.
byte sig[512]; // will hold generated signature
sigSz = sizeof(sig);
byte digest[] = { initialize with message hash };
wc_InitRng(&rng); // initialize rng
wc_ecc_init(&key); // initialize key
mp_init(&r); // initialize r component
mp_init(&s); // initialize s component
wc_ecc_make_key(&rng, 32, &key); // make public/private key pair
ret = wc_ecc_sign_hash_ex(digest, sizeof(digest), &rng, &key, &r, &s);
if ( ret != MP_OKAY ) {
// error generating message signature
}
\endcode
\sa wc_ecc_verify_hash_ex
*/
int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
ecc_key* key, mp_int *r, mp_int *s);
/*!
\ingroup ECC
\brief This function verifies the ECC signature of a hash to ensure
authenticity. It returns the answer through res, with 1 corresponding
to a valid signature, and 0 corresponding to an invalid signature.
\return 0 Returned upon successfully performing the signature
verification. Note: This does not mean that the signature is verified.
The authenticity information is stored instead in res
\return BAD_FUNC_ARG Returned any of the input parameters evaluate to NULL
\return MEMORY_E Returned if there is an error allocating memory
\return MP_INIT_E may be returned if there is an error while computing
the message signature
\return MP_READ_E may be returned if there is an error while computing
the message signature
\return MP_CMP_E may be returned if there is an error while computing
the message signature
\return MP_INVMOD_E may be returned if there is an error while computing
the message signature
\return MP_EXPTMOD_E may be returned if there is an error while
computing the message signature
\return MP_MOD_E may be returned if there is an error while computing
the message signature
\return MP_MUL_E may be returned if there is an error while computing
the message signature
\return MP_ADD_E may be returned if there is an error while computing
the message signature
\return MP_MULMOD_E may be returned if there is an error while computing
the message signature
\return MP_TO_E may be returned if there is an error while computing the
message signature
\return MP_MEM may be returned if there is an error while computing the
message signature
\param sig pointer to the buffer containing the signature to verify
\param siglen length of the signature to verify
\param hash pointer to the buffer containing the hash of the message
verified
\param hashlen length of the hash of the message verified
\param res pointer to the result of the verification. 1 indicates the
message was successfully verified
\param key pointer to a public ECC key with which to verify the signature
_Example_
\code
ecc_key key;
int ret, verified = 0;
byte sig[1024] { initialize with received signature };
byte digest[] = { initialize with message hash };
// initialize key with received public key
ret = wc_ecc_verify_hash(sig, sizeof(sig), digest,sizeof(digest),
&verified, &key);
if ( ret != 0 ) {
// error performing verification
} else if ( verified == 0 ) {
// the signature is invalid
}
\endcode
\sa wc_ecc_sign_hash
\sa wc_ecc_verify_hash_ex
*/
int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
word32 hashlen, int* res, ecc_key* key);
/*!
\ingroup ECC
\brief Verify an ECC signature. Result is written to res.
1 is valid, 0 is invalid.
Note: Do not use the return value to test for valid. Only use res.
\return MP_OKAY If successful (even if the signature is not valid)
\return ECC_BAD_ARG_E Returns if arguments are null or if
key-idx is invalid.
\return MEMORY_E Error allocating ints or points.
\param r The signature R component to verify
\param s The signature S component to verify
\param hash The hash (message digest) that was signed
\param hashlen The length of the hash (octets)
\param res Result of signature, 1==valid, 0==invalid
\param key The corresponding public ECC key
_Example_
\code
mp_int r;
mp_int s;
int res;
byte hash[] = { Some hash }
ecc_key key;
if(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &res, &key) == MP_OKAY)
{
// Check res
}
\endcode
\sa wc_ecc_verify_hash
*/
int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
word32 hashlen, int* res, ecc_key* key);
/*!
\ingroup ECC
\brief This function initializes an ecc_key object for future
use with message verification or key negotiation.
\return 0 Returned upon successfully initializing the ecc_key object
\return MEMORY_E Returned if there is an error allocating memory
\param key pointer to the ecc_key object to initialize
_Example_
\code
ecc_key key;
wc_ecc_init(&key);
\endcode
\sa wc_ecc_make_key
\sa wc_ecc_free
*/
int wc_ecc_init(ecc_key* key);
/*!
\ingroup ECC
\brief This function initializes an ecc_key object for future
use with message verification or key negotiation.
\return 0 Returned upon successfully initializing the ecc_key object
\return MEMORY_E Returned if there is an error allocating memory
\param key pointer to the ecc_key object to initialize
\param heap pointer to a heap identifier
\param devId ID to use with crypto callbacks or async hardware. Set to
INVALID_DEVID if not used
_Example_
\code
ecc_key key;
wc_ecc_init_ex(&key, heap, devId);
\endcode
\sa wc_ecc_make_key
\sa wc_ecc_free
\sa wc_ecc_init
*/
int wc_ecc_init_ex(ecc_key* key, void* heap, int devId);
/*!
\ingroup ECC
\brief This function uses a user defined heap and allocates space for the
key structure.
\param heap pointer to a heap identifier
\return Non-null returned upon successfully allocating and initializing the
ecc_key object
\return NULL returned if there is an error allocating memory
_Example_
\code
wc_ecc_key_new(&heap);
\endcode
\sa wc_ecc_key_new_ex
\sa wc_ecc_make_key
\sa wc_ecc_key_free
\sa wc_ecc_init
*/
ecc_key* wc_ecc_key_new(void* heap);
/*!
\ingroup ECC
\brief This function uses a user defined heap and allocates space for the
key structure.
\param heap pointer to a heap identifier
\param devId ID to use with crypto callbacks or async hardware. Set to
INVALID_DEVID if not used
\return Non-null returned upon successfully allocating and initializing the
ecc_key object
\return NULL returned if there is an error allocating memory
_Example_
\code
wc_ecc_key_new_ex(&heap, MY_DEVID);
\endcode
\sa wc_ecc_key_new
\sa wc_ecc_make_key
\sa wc_ecc_key_free
\sa wc_ecc_init
*/
ecc_key* wc_ecc_key_new_ex(void* heap, int devId);
/*!
\ingroup ECC
\brief This function frees an ecc_key object after it has been used.
\return int integer returned indicating wolfSSL error or success status.
\param key pointer to the ecc_key object to free
_Example_
\code
// initialize key and perform secure exchanges
...
wc_ecc_free(&key);
\endcode
\sa wc_ecc_init
*/
int wc_ecc_free(ecc_key* key);
/*!
\ingroup ECC
\brief This function frees the fixed-point cache, which can be used
with ecc to speed up computation times. To use this functionality,
FP_ECC (fixed-point ecc), should be defined. Threaded applications should
call this function before exiting the thread.
\return none No returns.
\param none No parameters.
_Example_
\code
ecc_key key;
// initialize key and perform secure exchanges
...
wc_ecc_fp_free();
\endcode
\sa wc_ecc_free
*/
void wc_ecc_fp_free(void);
/*!
\ingroup ECC
\brief Checks if an ECC idx is valid.
\return 1 Return if valid.
\return 0 Return if not valid.
\param n The idx number to check.
_Example_
\code
ecc_key key;
WC_RNG rng;
int is_valid;
wc_ecc_init(&key);
wc_InitRng(&rng);
wc_ecc_make_key(&rng, 32, &key);
is_valid = wc_ecc_is_valid_idx(key.idx);
if (is_valid == 1)
{
// idx is valid
}
else if (is_valid == 0)
{
// idx is not valid
}
\endcode
\sa none
*/
int wc_ecc_is_valid_idx(int n);
/*!
\ingroup ECC
\brief Allocate a new ECC point.
\return p A newly allocated point.
\return NULL Returns NULL on error.
\param none No parameters.
_Example_
\code
ecc_point* point;
point = wc_ecc_new_point();
if (point == NULL)
{
// Handle point creation error
}
// Do stuff with point
\endcode
\sa wc_ecc_del_point
\sa wc_ecc_cmp_point
\sa wc_ecc_copy_point
*/
ecc_point* wc_ecc_new_point(void);
/*!
\ingroup ECC
\brief Free an ECC point from memory.
\return none No returns.
\param p The point to free.
_Example_
\code
ecc_point* point;
point = wc_ecc_new_point();
if (point == NULL)
{
// Handle point creation error
}
// Do stuff with point
wc_ecc_del_point(point);
\endcode
\sa wc_ecc_new_point
\sa wc_ecc_cmp_point
\sa wc_ecc_copy_point
*/
void wc_ecc_del_point(ecc_point* p);
/*!
\ingroup ECC
\brief Copy the value of one point to another one.
\return ECC_BAD_ARG_E Error thrown when p or r is null.
\return MP_OKAY Point copied successfully
\return ret Error from internal functions. Can be...
\param p The point to copy.
\param r The created point.
_Example_
\code
ecc_point* point;
ecc_point* copied_point;
int copy_return;
point = wc_ecc_new_point();
copy_return = wc_ecc_copy_point(point, copied_point);
if (copy_return != MP_OKAY)
{
// Handle error
}
\endcode
\sa wc_ecc_new_point
\sa wc_ecc_cmp_point
\sa wc_ecc_del_point
*/
int wc_ecc_copy_point(const ecc_point* p, ecc_point *r);
/*!
\ingroup ECC
\brief Compare the value of a point with another one.
\return BAD_FUNC_ARG One or both arguments are NULL.
\return MP_EQ The points are equal.
\return ret Either MP_LT or MP_GT and signifies that the
points are not equal.
\param a First point to compare.
\param b Second point to compare.
_Example_
\code
ecc_point* point;
ecc_point* point_to_compare;
int cmp_result;
point = wc_ecc_new_point();
point_to_compare = wc_ecc_new_point();
cmp_result = wc_ecc_cmp_point(point, point_to_compare);
if (cmp_result == BAD_FUNC_ARG)
{
// arguments are invalid
}
else if (cmp_result == MP_EQ)
{
// Points are equal
}
else
{
// Points are not equal
}
\endcode
\sa wc_ecc_new_point
\sa wc_ecc_del_point
\sa wc_ecc_copy_point
*/
int wc_ecc_cmp_point(ecc_point* a, ecc_point *b);
/*!
\ingroup ECC
\brief Checks if a point is at infinity. Returns 1 if point is
at infinity, 0 if not, < 0 on error
\return 1 p is at infinity.
\return 0 p is not at infinity.
\return <0 Error.
\param p The point to check.
_Example_
\code
ecc_point* point;
int is_infinity;
point = wc_ecc_new_point();
is_infinity = wc_ecc_point_is_at_infinity(point);
if (is_infinity < 0)
{
// Handle error
}
else if (is_infinity == 0)
{
// Point is not at infinity
}
else if (is_infinity == 1)
{
// Point is at infinity
}
\endcode