-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
ecp.c
3618 lines (3107 loc) · 113 KB
/
ecp.c
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
/*
* Elliptic curves over GF(p): generic functions
*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* References:
*
* SEC1 http://www.secg.org/index.php?action=secg,docs_secg
* GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
* FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
* RFC 4492 for the related TLS structures and constants
* RFC 7748 for the Curve448 and Curve25519 curve definitions
*
* [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
*
* [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
* for elliptic curve cryptosystems. In : Cryptographic Hardware and
* Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
* <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
*
* [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
* render ECC resistant against Side Channel Attacks. IACR Cryptology
* ePrint Archive, 2004, vol. 2004, p. 342.
* <http://eprint.iacr.org/2004/342.pdf>
*/
#include "common.h"
/**
* \brief Function level alternative implementation.
*
* The MBEDTLS_ECP_INTERNAL_ALT macro enables alternative implementations to
* replace certain functions in this module. The alternative implementations are
* typically hardware accelerators and need to activate the hardware before the
* computation starts and deactivate it after it finishes. The
* mbedtls_internal_ecp_init() and mbedtls_internal_ecp_free() functions serve
* this purpose.
*
* To preserve the correct functionality the following conditions must hold:
*
* - The alternative implementation must be activated by
* mbedtls_internal_ecp_init() before any of the replaceable functions is
* called.
* - mbedtls_internal_ecp_free() must \b only be called when the alternative
* implementation is activated.
* - mbedtls_internal_ecp_init() must \b not be called when the alternative
* implementation is activated.
* - Public functions must not return while the alternative implementation is
* activated.
* - Replaceable functions are guarded by \c MBEDTLS_ECP_XXX_ALT macros and
* before calling them an \code if( mbedtls_internal_ecp_grp_capable( grp ) )
* \endcode ensures that the alternative implementation supports the current
* group.
*/
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
#endif
#if defined(MBEDTLS_ECP_C)
#include "mbedtls/ecp.h"
#include "mbedtls/threading.h"
#include "mbedtls/platform_util.h"
#include "mbedtls/error.h"
#include "bn_mul.h"
#include "ecp_invasive.h"
#include <string.h>
#if !defined(MBEDTLS_ECP_ALT)
#include "mbedtls/platform.h"
#include "ecp_internal_alt.h"
#if defined(MBEDTLS_SELF_TEST)
/*
* Counts of point addition and doubling, and field multiplications.
* Used to test resistance of point multiplication to simple timing attacks.
*/
static unsigned long add_count, dbl_count, mul_count;
#endif
#if defined(MBEDTLS_ECP_RESTARTABLE)
/*
* Maximum number of "basic operations" to be done in a row.
*
* Default value 0 means that ECC operations will not yield.
* Note that regardless of the value of ecp_max_ops, always at
* least one step is performed before yielding.
*
* Setting ecp_max_ops=1 can be suitable for testing purposes
* as it will interrupt computation at all possible points.
*/
static unsigned ecp_max_ops = 0;
/*
* Set ecp_max_ops
*/
void mbedtls_ecp_set_max_ops(unsigned max_ops)
{
ecp_max_ops = max_ops;
}
/*
* Check if restart is enabled
*/
int mbedtls_ecp_restart_is_enabled(void)
{
return ecp_max_ops != 0;
}
/*
* Restart sub-context for ecp_mul_comb()
*/
struct mbedtls_ecp_restart_mul {
mbedtls_ecp_point R; /* current intermediate result */
size_t i; /* current index in various loops, 0 outside */
mbedtls_ecp_point *T; /* table for precomputed points */
unsigned char T_size; /* number of points in table T */
enum { /* what were we doing last time we returned? */
ecp_rsm_init = 0, /* nothing so far, dummy initial state */
ecp_rsm_pre_dbl, /* precompute 2^n multiples */
ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
ecp_rsm_pre_add, /* precompute remaining points by adding */
ecp_rsm_pre_norm_add, /* normalize all precomputed points */
ecp_rsm_comb_core, /* ecp_mul_comb_core() */
ecp_rsm_final_norm, /* do the final normalization */
} state;
};
/*
* Init restart_mul sub-context
*/
static void ecp_restart_rsm_init(mbedtls_ecp_restart_mul_ctx *ctx)
{
mbedtls_ecp_point_init(&ctx->R);
ctx->i = 0;
ctx->T = NULL;
ctx->T_size = 0;
ctx->state = ecp_rsm_init;
}
/*
* Free the components of a restart_mul sub-context
*/
static void ecp_restart_rsm_free(mbedtls_ecp_restart_mul_ctx *ctx)
{
unsigned char i;
if (ctx == NULL) {
return;
}
mbedtls_ecp_point_free(&ctx->R);
if (ctx->T != NULL) {
for (i = 0; i < ctx->T_size; i++) {
mbedtls_ecp_point_free(ctx->T + i);
}
mbedtls_free(ctx->T);
}
ecp_restart_rsm_init(ctx);
}
/*
* Restart context for ecp_muladd()
*/
struct mbedtls_ecp_restart_muladd {
mbedtls_ecp_point mP; /* mP value */
mbedtls_ecp_point R; /* R intermediate result */
enum { /* what should we do next? */
ecp_rsma_mul1 = 0, /* first multiplication */
ecp_rsma_mul2, /* second multiplication */
ecp_rsma_add, /* addition */
ecp_rsma_norm, /* normalization */
} state;
};
/*
* Init restart_muladd sub-context
*/
static void ecp_restart_ma_init(mbedtls_ecp_restart_muladd_ctx *ctx)
{
mbedtls_ecp_point_init(&ctx->mP);
mbedtls_ecp_point_init(&ctx->R);
ctx->state = ecp_rsma_mul1;
}
/*
* Free the components of a restart_muladd sub-context
*/
static void ecp_restart_ma_free(mbedtls_ecp_restart_muladd_ctx *ctx)
{
if (ctx == NULL) {
return;
}
mbedtls_ecp_point_free(&ctx->mP);
mbedtls_ecp_point_free(&ctx->R);
ecp_restart_ma_init(ctx);
}
/*
* Initialize a restart context
*/
void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx)
{
ctx->ops_done = 0;
ctx->depth = 0;
ctx->rsm = NULL;
ctx->ma = NULL;
}
/*
* Free the components of a restart context
*/
void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx)
{
if (ctx == NULL) {
return;
}
ecp_restart_rsm_free(ctx->rsm);
mbedtls_free(ctx->rsm);
ecp_restart_ma_free(ctx->ma);
mbedtls_free(ctx->ma);
mbedtls_ecp_restart_init(ctx);
}
/*
* Check if we can do the next step
*/
int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
mbedtls_ecp_restart_ctx *rs_ctx,
unsigned ops)
{
if (rs_ctx != NULL && ecp_max_ops != 0) {
/* scale depending on curve size: the chosen reference is 256-bit,
* and multiplication is quadratic. Round to the closest integer. */
if (grp->pbits >= 512) {
ops *= 4;
} else if (grp->pbits >= 384) {
ops *= 2;
}
/* Avoid infinite loops: always allow first step.
* Because of that, however, it's not generally true
* that ops_done <= ecp_max_ops, so the check
* ops_done > ecp_max_ops below is mandatory. */
if ((rs_ctx->ops_done != 0) &&
(rs_ctx->ops_done > ecp_max_ops ||
ops > ecp_max_ops - rs_ctx->ops_done)) {
return MBEDTLS_ERR_ECP_IN_PROGRESS;
}
/* update running count */
rs_ctx->ops_done += ops;
}
return 0;
}
/* Call this when entering a function that needs its own sub-context */
#define ECP_RS_ENTER(SUB) do { \
/* reset ops count for this call if top-level */ \
if (rs_ctx != NULL && rs_ctx->depth++ == 0) \
rs_ctx->ops_done = 0; \
\
/* set up our own sub-context if needed */ \
if (mbedtls_ecp_restart_is_enabled() && \
rs_ctx != NULL && rs_ctx->SUB == NULL) \
{ \
rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \
if (rs_ctx->SUB == NULL) \
return MBEDTLS_ERR_ECP_ALLOC_FAILED; \
\
ecp_restart_## SUB ##_init(rs_ctx->SUB); \
} \
} while (0)
/* Call this when leaving a function that needs its own sub-context */
#define ECP_RS_LEAVE(SUB) do { \
/* clear our sub-context when not in progress (done or error) */ \
if (rs_ctx != NULL && rs_ctx->SUB != NULL && \
ret != MBEDTLS_ERR_ECP_IN_PROGRESS) \
{ \
ecp_restart_## SUB ##_free(rs_ctx->SUB); \
mbedtls_free(rs_ctx->SUB); \
rs_ctx->SUB = NULL; \
} \
\
if (rs_ctx != NULL) \
rs_ctx->depth--; \
} while (0)
#else /* MBEDTLS_ECP_RESTARTABLE */
#define ECP_RS_ENTER(sub) (void) rs_ctx;
#define ECP_RS_LEAVE(sub) (void) rs_ctx;
#endif /* MBEDTLS_ECP_RESTARTABLE */
static void mpi_init_many(mbedtls_mpi *arr, size_t size)
{
while (size--) {
mbedtls_mpi_init(arr++);
}
}
static void mpi_free_many(mbedtls_mpi *arr, size_t size)
{
while (size--) {
mbedtls_mpi_free(arr++);
}
}
/*
* List of supported curves:
* - internal ID
* - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2, RFC 8446 sec. 4.2.7)
* - size in bits
* - readable name
*
* Curves are listed in order: largest curves first, and for a given size,
* fastest curves first.
*
* Reminder: update profiles in x509_crt.c and ssl_tls.c when adding a new curve!
*/
static const mbedtls_ecp_curve_info ecp_supported_curves[] =
{
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
{ MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
#endif
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
{ MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
#endif
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
{ MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
#endif
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
{ MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
#endif
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
{ MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
#endif
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
{ MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
#endif
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
{ MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
#endif
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
{ MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
#endif
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
{ MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
#endif
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
{ MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
#endif
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
{ MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
#endif
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
{ MBEDTLS_ECP_DP_CURVE25519, 29, 256, "x25519" },
#endif
#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
{ MBEDTLS_ECP_DP_CURVE448, 30, 448, "x448" },
#endif
{ MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
};
#define ECP_NB_CURVES sizeof(ecp_supported_curves) / \
sizeof(ecp_supported_curves[0])
static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
/*
* List of supported curves and associated info
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void)
{
return ecp_supported_curves;
}
/*
* List of supported curves, group ID only
*/
const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
{
static int init_done = 0;
if (!init_done) {
size_t i = 0;
const mbedtls_ecp_curve_info *curve_info;
for (curve_info = mbedtls_ecp_curve_list();
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
ecp_supported_grp_id[i++] = curve_info->grp_id;
}
ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
init_done = 1;
}
return ecp_supported_grp_id;
}
/*
* Get the curve info for the internal identifier
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id)
{
const mbedtls_ecp_curve_info *curve_info;
for (curve_info = mbedtls_ecp_curve_list();
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
if (curve_info->grp_id == grp_id) {
return curve_info;
}
}
return NULL;
}
/*
* Get the curve info from the TLS identifier
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
{
const mbedtls_ecp_curve_info *curve_info;
for (curve_info = mbedtls_ecp_curve_list();
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
if (curve_info->tls_id == tls_id) {
return curve_info;
}
}
return NULL;
}
/*
* Get the curve info from the name
*/
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name)
{
const mbedtls_ecp_curve_info *curve_info;
if (name == NULL) {
return NULL;
}
for (curve_info = mbedtls_ecp_curve_list();
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
curve_info++) {
if (strcmp(curve_info->name, name) == 0) {
return curve_info;
}
}
return NULL;
}
/*
* Get the type of a curve
*/
mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp)
{
if (grp->G.X.p == NULL) {
return MBEDTLS_ECP_TYPE_NONE;
}
if (grp->G.Y.p == NULL) {
return MBEDTLS_ECP_TYPE_MONTGOMERY;
} else {
return MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS;
}
}
/*
* Initialize (the components of) a point
*/
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
{
mbedtls_mpi_init(&pt->X);
mbedtls_mpi_init(&pt->Y);
mbedtls_mpi_init(&pt->Z);
}
/*
* Initialize (the components of) a group
*/
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
{
grp->id = MBEDTLS_ECP_DP_NONE;
mbedtls_mpi_init(&grp->P);
mbedtls_mpi_init(&grp->A);
mbedtls_mpi_init(&grp->B);
mbedtls_ecp_point_init(&grp->G);
mbedtls_mpi_init(&grp->N);
grp->pbits = 0;
grp->nbits = 0;
grp->h = 0;
grp->modp = NULL;
grp->t_pre = NULL;
grp->t_post = NULL;
grp->t_data = NULL;
grp->T = NULL;
grp->T_size = 0;
}
/*
* Initialize (the components of) a key pair
*/
void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
{
mbedtls_ecp_group_init(&key->grp);
mbedtls_mpi_init(&key->d);
mbedtls_ecp_point_init(&key->Q);
}
/*
* Unallocate (the components of) a point
*/
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
{
if (pt == NULL) {
return;
}
mbedtls_mpi_free(&(pt->X));
mbedtls_mpi_free(&(pt->Y));
mbedtls_mpi_free(&(pt->Z));
}
/*
* Check that the comb table (grp->T) is static initialized.
*/
static int ecp_group_is_static_comb_table(const mbedtls_ecp_group *grp)
{
#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
return grp->T != NULL && grp->T_size == 0;
#else
(void) grp;
return 0;
#endif
}
/*
* Unallocate (the components of) a group
*/
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
{
size_t i;
if (grp == NULL) {
return;
}
if (grp->h != 1) {
mbedtls_mpi_free(&grp->A);
mbedtls_mpi_free(&grp->B);
mbedtls_ecp_point_free(&grp->G);
}
if (!ecp_group_is_static_comb_table(grp) && grp->T != NULL) {
for (i = 0; i < grp->T_size; i++) {
mbedtls_ecp_point_free(&grp->T[i]);
}
mbedtls_free(grp->T);
}
mbedtls_platform_zeroize(grp, sizeof(mbedtls_ecp_group));
}
/*
* Unallocate (the components of) a key pair
*/
void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
{
if (key == NULL) {
return;
}
mbedtls_ecp_group_free(&key->grp);
mbedtls_mpi_free(&key->d);
mbedtls_ecp_point_free(&key->Q);
}
/*
* Copy the contents of a point
*/
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->X, &Q->X));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Y, &Q->Y));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Z, &Q->Z));
cleanup:
return ret;
}
/*
* Copy the contents of a group object
*/
int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
{
return mbedtls_ecp_group_load(dst, src->id);
}
/*
* Set point to zero
*/
int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->X, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Y, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 0));
cleanup:
return ret;
}
/*
* Tell if a point is zero
*/
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
{
return mbedtls_mpi_cmp_int(&pt->Z, 0) == 0;
}
/*
* Compare two points lazily
*/
int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
const mbedtls_ecp_point *Q)
{
if (mbedtls_mpi_cmp_mpi(&P->X, &Q->X) == 0 &&
mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 &&
mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) {
return 0;
}
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
/*
* Import a non-zero point from ASCII strings
*/
int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
const char *x, const char *y)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->X, radix, x));
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->Y, radix, y));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1));
cleanup:
return ret;
}
/*
* Export a point into unsigned binary data (SEC1 2.3.3 and RFC7748)
*/
int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp,
const mbedtls_ecp_point *P,
int format, size_t *olen,
unsigned char *buf, size_t buflen)
{
int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
size_t plen;
if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
format != MBEDTLS_ECP_PF_COMPRESSED) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
plen = mbedtls_mpi_size(&grp->P);
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
(void) format; /* Montgomery curves always use the same point format */
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
*olen = plen;
if (buflen < *olen) {
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
}
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&P->X, buf, plen));
}
#endif
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
/*
* Common case: P == 0
*/
if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) {
if (buflen < 1) {
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
}
buf[0] = 0x00;
*olen = 1;
return 0;
}
if (format == MBEDTLS_ECP_PF_UNCOMPRESSED) {
*olen = 2 * plen + 1;
if (buflen < *olen) {
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
}
buf[0] = 0x04;
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->Y, buf + 1 + plen, plen));
} else if (format == MBEDTLS_ECP_PF_COMPRESSED) {
*olen = plen + 1;
if (buflen < *olen) {
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
}
buf[0] = 0x02 + mbedtls_mpi_get_bit(&P->Y, 0);
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
}
}
#endif
cleanup:
return ret;
}
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
static int mbedtls_ecp_sw_derive_y(const mbedtls_ecp_group *grp,
const mbedtls_mpi *X,
mbedtls_mpi *Y,
int parity_bit);
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
/*
* Import a point from unsigned binary data (SEC1 2.3.4 and RFC7748)
*/
int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp,
mbedtls_ecp_point *pt,
const unsigned char *buf, size_t ilen)
{
int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
size_t plen;
if (ilen < 1) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
plen = mbedtls_mpi_size(&grp->P);
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
if (plen != ilen) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&pt->X, buf, plen));
mbedtls_mpi_free(&pt->Y);
if (grp->id == MBEDTLS_ECP_DP_CURVE25519) {
/* Set most significant bit to 0 as prescribed in RFC7748 §5 */
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&pt->X, plen * 8 - 1, 0));
}
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
}
#endif
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
if (buf[0] == 0x00) {
if (ilen == 1) {
return mbedtls_ecp_set_zero(pt);
} else {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
}
if (ilen < 1 + plen) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->X, buf + 1, plen));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
if (buf[0] == 0x04) {
/* format == MBEDTLS_ECP_PF_UNCOMPRESSED */
if (ilen != 1 + plen * 2) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
return mbedtls_mpi_read_binary(&pt->Y, buf + 1 + plen, plen);
} else if (buf[0] == 0x02 || buf[0] == 0x03) {
/* format == MBEDTLS_ECP_PF_COMPRESSED */
if (ilen != 1 + plen) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
return mbedtls_ecp_sw_derive_y(grp, &pt->X, &pt->Y,
(buf[0] & 1));
} else {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
}
#endif
cleanup:
return ret;
}
/*
* Import a point from a TLS ECPoint record (RFC 4492)
* struct {
* opaque point <1..2^8-1>;
* } ECPoint;
*/
int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp,
mbedtls_ecp_point *pt,
const unsigned char **buf, size_t buf_len)
{
unsigned char data_len;
const unsigned char *buf_start;
/*
* We must have at least two bytes (1 for length, at least one for data)
*/
if (buf_len < 2) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
data_len = *(*buf)++;
if (data_len < 1 || data_len > buf_len - 1) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
/*
* Save buffer start for read_binary and update buf
*/
buf_start = *buf;
*buf += data_len;
return mbedtls_ecp_point_read_binary(grp, pt, buf_start, data_len);
}
/*
* Export a point as a TLS ECPoint record (RFC 4492)
* struct {
* opaque point <1..2^8-1>;
* } ECPoint;
*/
int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
int format, size_t *olen,
unsigned char *buf, size_t blen)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
format != MBEDTLS_ECP_PF_COMPRESSED) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
/*
* buffer length must be at least one, for our length byte
*/
if (blen < 1) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
if ((ret = mbedtls_ecp_point_write_binary(grp, pt, format,
olen, buf + 1, blen - 1)) != 0) {
return ret;
}
/*
* write length to the first byte and update total length
*/
buf[0] = (unsigned char) *olen;
++*olen;
return 0;
}
/*
* Set a group from an ECParameters record (RFC 4492)
*/
int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp,
const unsigned char **buf, size_t len)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_ecp_group_id grp_id;
if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, len)) != 0) {
return ret;
}
return mbedtls_ecp_group_load(grp, grp_id);
}
/*
* Read a group id from an ECParameters record (RFC 4492) and convert it to
* mbedtls_ecp_group_id.
*/
int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp,
const unsigned char **buf, size_t len)
{
uint16_t tls_id;
const mbedtls_ecp_curve_info *curve_info;
/*
* We expect at least three bytes (see below)
*/
if (len < 3) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
/*
* First byte is curve_type; only named_curve is handled
*/
if (*(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
/*
* Next two bytes are the namedcurve value
*/
tls_id = *(*buf)++;
tls_id <<= 8;
tls_id |= *(*buf)++;
if ((curve_info = mbedtls_ecp_curve_info_from_tls_id(tls_id)) == NULL) {
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
}
*grp = curve_info->grp_id;
return 0;
}
/*
* Write the ECParameters record corresponding to a group (RFC 4492)
*/
int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen,
unsigned char *buf, size_t blen)
{
const mbedtls_ecp_curve_info *curve_info;
if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
/*
* We are going to write 3 bytes (see below)
*/
*olen = 3;
if (blen < *olen) {
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
}
/*
* First byte is curve_type, always named_curve
*/
*buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
/*
* Next two bytes are the namedcurve value
*/
MBEDTLS_PUT_UINT16_BE(curve_info->tls_id, buf, 0);
return 0;
}
/*
* Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
* See the documentation of struct mbedtls_ecp_group.
*
* This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
*/
static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp)