1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
|
/* BEGIN_HEADER */
#include "mbedtls/ecp.h"
#include "ecp_invasive.h"
#include "mbedtls/ecdsa.h"
#include "mbedtls/ecdh.h"
#include "bignum_core.h"
#include "ecp_invasive.h"
#include "bignum_mod_raw_invasive.h"
#include "constant_time_internal.h"
#define ECP_PF_UNKNOWN -1
#define ECP_PT_RESET(x) \
mbedtls_ecp_point_free(x); \
mbedtls_ecp_point_init(x);
/* Auxiliary function to compare two mbedtls_ecp_group objects. */
inline static int mbedtls_ecp_group_cmp(mbedtls_ecp_group *grp1,
mbedtls_ecp_group *grp2)
{
if (mbedtls_mpi_cmp_mpi(&grp1->P, &grp2->P) != 0) {
return 1;
}
if (mbedtls_mpi_cmp_mpi(&grp1->A, &grp2->A) != 0) {
return 1;
}
if (mbedtls_mpi_cmp_mpi(&grp1->B, &grp2->B) != 0) {
return 1;
}
if (mbedtls_mpi_cmp_mpi(&grp1->N, &grp2->N) != 0) {
return 1;
}
if (mbedtls_ecp_point_cmp(&grp1->G, &grp2->G) != 0) {
return 1;
}
if (grp1->id != grp2->id) {
return 1;
}
if (grp1->pbits != grp2->pbits) {
return 1;
}
if (grp1->nbits != grp2->nbits) {
return 1;
}
if (grp1->h != grp2->h) {
return 1;
}
if (grp1->modp != grp2->modp) {
return 1;
}
if (grp1->t_pre != grp2->t_pre) {
return 1;
}
if (grp1->t_post != grp2->t_post) {
return 1;
}
if (grp1->t_data != grp2->t_data) {
return 1;
}
if (grp1->T_size != grp2->T_size) {
return 1;
}
if (grp1->T != grp2->T) {
return 1;
}
return 0;
}
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_ECP_LIGHT
* END_DEPENDENCIES
*/
/* BEGIN_CASE */
void ecp_invalid_param()
{
mbedtls_ecp_group grp;
mbedtls_ecp_point P;
int invalid_fmt = 42;
size_t olen;
unsigned char buf[42] = { 0 };
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point_init(&P);
TEST_EQUAL(MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
mbedtls_ecp_point_write_binary(&grp, &P,
invalid_fmt,
&olen,
buf, sizeof(buf)));
TEST_EQUAL(MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
mbedtls_ecp_tls_write_point(&grp, &P,
invalid_fmt,
&olen,
buf,
sizeof(buf)));
exit:
return;
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_ecp_curve_info(int id, int tls_id, int size, char *name)
{
const mbedtls_ecp_curve_info *by_id, *by_tls, *by_name;
by_id = mbedtls_ecp_curve_info_from_grp_id(id);
by_tls = mbedtls_ecp_curve_info_from_tls_id(tls_id);
by_name = mbedtls_ecp_curve_info_from_name(name);
TEST_ASSERT(by_id != NULL);
TEST_ASSERT(by_tls != NULL);
TEST_ASSERT(by_name != NULL);
TEST_ASSERT(by_id == by_tls);
TEST_ASSERT(by_id == by_name);
TEST_ASSERT(by_id->bit_size == size);
TEST_ASSERT(size <= MBEDTLS_ECP_MAX_BITS);
TEST_ASSERT(size <= MBEDTLS_ECP_MAX_BYTES * 8);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_check_pub(int grp_id, char *x_hex, char *y_hex, char *z_hex,
int ret)
{
mbedtls_ecp_group grp;
mbedtls_ecp_point P;
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point_init(&P);
TEST_ASSERT(mbedtls_ecp_group_load(&grp, grp_id) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&P.X, x_hex) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&P.Y, y_hex) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&P.Z, z_hex) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &P) == ret);
exit:
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&P);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
void ecp_test_vect_restart(int id,
char *dA_str, char *xA_str, char *yA_str,
char *dB_str, char *xZ_str, char *yZ_str,
int max_ops, int min_restarts, int max_restarts)
{
/*
* Test for early restart. Based on test vectors like ecp_test_vect(),
* but for the sake of simplicity only does half of each side. It's
* important to test both base point and random point, though, as memory
* management is different in each case.
*
* Don't try using too precise bounds for restarts as the exact number
* will depend on settings such as MBEDTLS_ECP_FIXED_POINT_OPTIM and
* MBEDTLS_ECP_WINDOW_SIZE, as well as implementation details that may
* change in the future. A factor 2 is a minimum safety margin.
*
* For reference, with Mbed TLS 2.4 and default settings, for P-256:
* - Random point mult: ~3250M
* - Cold base point mult: ~3300M
* - Hot base point mult: ~1100M
* With MBEDTLS_ECP_WINDOW_SIZE set to 2 (minimum):
* - Random point mult: ~3850M
*/
mbedtls_ecp_restart_ctx ctx;
mbedtls_ecp_group grp;
mbedtls_ecp_point R, P;
mbedtls_mpi dA, xA, yA, dB, xZ, yZ;
int cnt_restarts;
int ret;
mbedtls_test_rnd_pseudo_info rnd_info;
mbedtls_ecp_restart_init(&ctx);
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point_init(&R); mbedtls_ecp_point_init(&P);
mbedtls_mpi_init(&dA); mbedtls_mpi_init(&xA); mbedtls_mpi_init(&yA);
mbedtls_mpi_init(&dB); mbedtls_mpi_init(&xZ); mbedtls_mpi_init(&yZ);
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&dA, dA_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&xA, xA_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&yA, yA_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&dB, dB_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&xZ, xZ_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&yZ, yZ_str) == 0);
mbedtls_ecp_set_max_ops((unsigned) max_ops);
/* Base point case */
cnt_restarts = 0;
do {
ECP_PT_RESET(&R);
ret = mbedtls_ecp_mul_restartable(&grp, &R, &dA, &grp.G,
&mbedtls_test_rnd_pseudo_rand, &rnd_info, &ctx);
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts);
TEST_ASSERT(ret == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xA) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yA) == 0);
TEST_ASSERT(cnt_restarts >= min_restarts);
TEST_ASSERT(cnt_restarts <= max_restarts);
/* Non-base point case */
mbedtls_ecp_copy(&P, &R);
cnt_restarts = 0;
do {
ECP_PT_RESET(&R);
ret = mbedtls_ecp_mul_restartable(&grp, &R, &dB, &P,
&mbedtls_test_rnd_pseudo_rand, &rnd_info, &ctx);
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts);
TEST_ASSERT(ret == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xZ) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yZ) == 0);
TEST_ASSERT(cnt_restarts >= min_restarts);
TEST_ASSERT(cnt_restarts <= max_restarts);
/* Do we leak memory when aborting an operation?
* This test only makes sense when we actually restart */
if (min_restarts > 0) {
ret = mbedtls_ecp_mul_restartable(&grp, &R, &dB, &P,
&mbedtls_test_rnd_pseudo_rand, &rnd_info, &ctx);
TEST_ASSERT(ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
}
exit:
mbedtls_ecp_restart_free(&ctx);
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&R); mbedtls_ecp_point_free(&P);
mbedtls_mpi_free(&dA); mbedtls_mpi_free(&xA); mbedtls_mpi_free(&yA);
mbedtls_mpi_free(&dB); mbedtls_mpi_free(&xZ); mbedtls_mpi_free(&yZ);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
void ecp_muladd_restart(int id, char *xR_str, char *yR_str,
char *u1_str, char *u2_str,
char *xQ_str, char *yQ_str,
int max_ops, int min_restarts, int max_restarts)
{
/*
* Compute R = u1 * G + u2 * Q
* (test vectors mostly taken from ECDSA intermediate results)
*
* See comments at the top of ecp_test_vect_restart()
*/
mbedtls_ecp_restart_ctx ctx;
mbedtls_ecp_group grp;
mbedtls_ecp_point R, Q;
mbedtls_mpi u1, u2, xR, yR;
int cnt_restarts;
int ret;
mbedtls_ecp_restart_init(&ctx);
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point_init(&R);
mbedtls_ecp_point_init(&Q);
mbedtls_mpi_init(&u1); mbedtls_mpi_init(&u2);
mbedtls_mpi_init(&xR); mbedtls_mpi_init(&yR);
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&u1, u1_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&u2, u2_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&xR, xR_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&yR, yR_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&Q.X, xQ_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&Q.Y, yQ_str) == 0);
TEST_ASSERT(mbedtls_mpi_lset(&Q.Z, 1) == 0);
mbedtls_ecp_set_max_ops((unsigned) max_ops);
cnt_restarts = 0;
do {
ECP_PT_RESET(&R);
ret = mbedtls_ecp_muladd_restartable(&grp, &R,
&u1, &grp.G, &u2, &Q, &ctx);
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts);
TEST_ASSERT(ret == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xR) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yR) == 0);
TEST_ASSERT(cnt_restarts >= min_restarts);
TEST_ASSERT(cnt_restarts <= max_restarts);
/* Do we leak memory when aborting an operation?
* This test only makes sense when we actually restart */
if (min_restarts > 0) {
ret = mbedtls_ecp_muladd_restartable(&grp, &R,
&u1, &grp.G, &u2, &Q, &ctx);
TEST_ASSERT(ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
}
exit:
mbedtls_ecp_restart_free(&ctx);
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&R);
mbedtls_ecp_point_free(&Q);
mbedtls_mpi_free(&u1); mbedtls_mpi_free(&u2);
mbedtls_mpi_free(&xR); mbedtls_mpi_free(&yR);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECP_C */
void ecp_test_vect(int id, char *dA_str, char *xA_str, char *yA_str,
char *dB_str, char *xB_str, char *yB_str,
char *xZ_str, char *yZ_str)
{
mbedtls_ecp_group grp;
mbedtls_ecp_point R;
mbedtls_mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
mbedtls_test_rnd_pseudo_info rnd_info;
mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&R);
mbedtls_mpi_init(&dA); mbedtls_mpi_init(&xA); mbedtls_mpi_init(&yA); mbedtls_mpi_init(&dB);
mbedtls_mpi_init(&xB); mbedtls_mpi_init(&yB); mbedtls_mpi_init(&xZ); mbedtls_mpi_init(&yZ);
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &grp.G) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&dA, dA_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&xA, xA_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&yA, yA_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&dB, dB_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&xB, xB_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&yB, yB_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&xZ, xZ_str) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&yZ, yZ_str) == 0);
TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dA, &grp.G,
&mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xA) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yA) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0);
TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dB, &R,
&mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xZ) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yZ) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0);
TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dB, &grp.G,
&mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xB) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yB) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0);
TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dA, &R,
&mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xZ) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.Y, &yZ) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0);
exit:
mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&R);
mbedtls_mpi_free(&dA); mbedtls_mpi_free(&xA); mbedtls_mpi_free(&yA); mbedtls_mpi_free(&dB);
mbedtls_mpi_free(&xB); mbedtls_mpi_free(&yB); mbedtls_mpi_free(&xZ); mbedtls_mpi_free(&yZ);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECP_C */
void ecp_test_vec_x(int id, char *dA_hex, char *xA_hex, char *dB_hex,
char *xB_hex, char *xS_hex)
{
mbedtls_ecp_group grp;
mbedtls_ecp_point R;
mbedtls_mpi dA, xA, dB, xB, xS;
mbedtls_test_rnd_pseudo_info rnd_info;
mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&R);
mbedtls_mpi_init(&dA); mbedtls_mpi_init(&xA);
mbedtls_mpi_init(&dB); mbedtls_mpi_init(&xB);
mbedtls_mpi_init(&xS);
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &grp.G) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&dA, dA_hex) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&dB, dB_hex) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&xA, xA_hex) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&xB, xB_hex) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&xS, xS_hex) == 0);
TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dA, &grp.G,
&mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xA) == 0);
TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dB, &R,
&mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xS) == 0);
TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dB, &grp.G,
&mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xB) == 0);
TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &dA, &R,
&mbedtls_test_rnd_pseudo_rand, &rnd_info) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &R) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R.X, &xS) == 0);
exit:
mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&R);
mbedtls_mpi_free(&dA); mbedtls_mpi_free(&xA);
mbedtls_mpi_free(&dB); mbedtls_mpi_free(&xB);
mbedtls_mpi_free(&xS);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECP_C */
void ecp_test_mul(int id, data_t *n_hex,
data_t *Px_hex, data_t *Py_hex, data_t *Pz_hex,
data_t *nPx_hex, data_t *nPy_hex, data_t *nPz_hex,
int expected_ret)
{
mbedtls_ecp_group grp;
mbedtls_ecp_point P, nP, R;
mbedtls_mpi n;
mbedtls_test_rnd_pseudo_info rnd_info;
mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&R);
mbedtls_ecp_point_init(&P); mbedtls_ecp_point_init(&nP);
mbedtls_mpi_init(&n);
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &grp.G) == 0);
TEST_ASSERT(mbedtls_mpi_read_binary(&n, n_hex->x, n_hex->len) == 0);
TEST_ASSERT(mbedtls_mpi_read_binary(&P.X, Px_hex->x, Px_hex->len) == 0);
TEST_ASSERT(mbedtls_mpi_read_binary(&P.Y, Py_hex->x, Py_hex->len) == 0);
TEST_ASSERT(mbedtls_mpi_read_binary(&P.Z, Pz_hex->x, Pz_hex->len) == 0);
TEST_ASSERT(mbedtls_mpi_read_binary(&nP.X, nPx_hex->x, nPx_hex->len)
== 0);
TEST_ASSERT(mbedtls_mpi_read_binary(&nP.Y, nPy_hex->x, nPy_hex->len)
== 0);
TEST_ASSERT(mbedtls_mpi_read_binary(&nP.Z, nPz_hex->x, nPz_hex->len)
== 0);
TEST_ASSERT(mbedtls_ecp_mul(&grp, &R, &n, &P,
&mbedtls_test_rnd_pseudo_rand, &rnd_info)
== expected_ret);
if (expected_ret == 0) {
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&nP.X, &R.X) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&nP.Y, &R.Y) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&nP.Z, &R.Z) == 0);
}
exit:
mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&R);
mbedtls_ecp_point_free(&P); mbedtls_ecp_point_free(&nP);
mbedtls_mpi_free(&n);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECP_C */
void ecp_test_mul_rng(int id, data_t *d_hex)
{
mbedtls_ecp_group grp;
mbedtls_mpi d;
mbedtls_ecp_point Q;
mbedtls_ecp_group_init(&grp); mbedtls_mpi_init(&d);
mbedtls_ecp_point_init(&Q);
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &grp.G) == 0);
TEST_ASSERT(mbedtls_mpi_read_binary(&d, d_hex->x, d_hex->len) == 0);
TEST_ASSERT(mbedtls_ecp_mul(&grp, &Q, &d, &grp.G,
&mbedtls_test_rnd_zero_rand, NULL)
== MBEDTLS_ERR_ECP_RANDOM_FAILED);
exit:
mbedtls_ecp_group_free(&grp); mbedtls_mpi_free(&d);
mbedtls_ecp_point_free(&Q);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED:MBEDTLS_ECP_C */
void ecp_muladd(int id,
data_t *u1_bin, data_t *P1_bin,
data_t *u2_bin, data_t *P2_bin,
data_t *expected_result)
{
/* Compute R = u1 * P1 + u2 * P2 */
mbedtls_ecp_group grp;
mbedtls_ecp_point P1, P2, R;
mbedtls_mpi u1, u2;
uint8_t actual_result[MBEDTLS_ECP_MAX_PT_LEN];
size_t len;
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point_init(&P1);
mbedtls_ecp_point_init(&P2);
mbedtls_ecp_point_init(&R);
mbedtls_mpi_init(&u1);
mbedtls_mpi_init(&u2);
TEST_EQUAL(0, mbedtls_ecp_group_load(&grp, id));
TEST_EQUAL(0, mbedtls_mpi_read_binary(&u1, u1_bin->x, u1_bin->len));
TEST_EQUAL(0, mbedtls_mpi_read_binary(&u2, u2_bin->x, u2_bin->len));
TEST_EQUAL(0, mbedtls_ecp_point_read_binary(&grp, &P1,
P1_bin->x, P1_bin->len));
TEST_EQUAL(0, mbedtls_ecp_point_read_binary(&grp, &P2,
P2_bin->x, P2_bin->len));
TEST_EQUAL(0, mbedtls_ecp_muladd(&grp, &R, &u1, &P1, &u2, &P2));
TEST_EQUAL(0, mbedtls_ecp_point_write_binary(
&grp, &R, MBEDTLS_ECP_PF_UNCOMPRESSED,
&len, actual_result, sizeof(actual_result)));
TEST_ASSERT(len <= MBEDTLS_ECP_MAX_PT_LEN);
TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
actual_result, len);
exit:
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&P1);
mbedtls_ecp_point_free(&P2);
mbedtls_ecp_point_free(&R);
mbedtls_mpi_free(&u1);
mbedtls_mpi_free(&u2);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_fast_mod(int id, char *N_str)
{
mbedtls_ecp_group grp;
mbedtls_mpi N, R;
mbedtls_mpi_init(&N); mbedtls_mpi_init(&R);
mbedtls_ecp_group_init(&grp);
TEST_ASSERT(mbedtls_test_read_mpi(&N, N_str) == 0);
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(grp.modp != NULL);
/*
* Store correct result before we touch N
*/
TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &N, &grp.P) == 0);
TEST_ASSERT(grp.modp(&N) == 0);
TEST_ASSERT(mbedtls_mpi_bitlen(&N) <= grp.pbits + 3);
/*
* Use mod rather than addition/subtraction in case previous test fails
*/
TEST_ASSERT(mbedtls_mpi_mod_mpi(&N, &N, &grp.P) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&N, &R) == 0);
exit:
mbedtls_mpi_free(&N); mbedtls_mpi_free(&R);
mbedtls_ecp_group_free(&grp);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_write_binary(int id, char *x, char *y, char *z, int format,
data_t *out, int blen, int ret)
{
mbedtls_ecp_group grp;
mbedtls_ecp_point P;
mbedtls_ecp_keypair key;
unsigned char buf[256];
size_t olen;
memset(buf, 0, sizeof(buf));
mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&P);
mbedtls_ecp_keypair_init(&key);
TEST_EQUAL(mbedtls_ecp_group_load(&grp, id), 0);
TEST_EQUAL(mbedtls_test_read_mpi(&P.X, x), 0);
TEST_EQUAL(mbedtls_test_read_mpi(&P.Y, y), 0);
TEST_EQUAL(mbedtls_test_read_mpi(&P.Z, z), 0);
TEST_EQUAL(mbedtls_ecp_point_write_binary(&grp, &P, format,
&olen, buf, blen), ret);
if (ret == 0) {
TEST_LE_U(olen, MBEDTLS_ECP_MAX_PT_LEN);
ASSERT_COMPARE(buf, olen,
out->x, out->len);
}
memset(buf, 0, blen);
TEST_EQUAL(mbedtls_ecp_set_public_key(grp.id, &key, &P), 0);
TEST_EQUAL(mbedtls_ecp_write_public_key(&key, format,
&olen, buf, blen), ret);
if (ret == 0) {
ASSERT_COMPARE(buf, olen,
out->x, out->len);
}
exit:
mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&P);
mbedtls_ecp_keypair_free(&key);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_read_binary(int id, data_t *buf, char *x, char *y, char *z,
int ret)
{
mbedtls_ecp_group grp;
mbedtls_ecp_point P;
mbedtls_mpi X, Y, Z;
mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&P);
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&X, x) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&Y, y) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&Z, z) == 0);
TEST_ASSERT(mbedtls_ecp_point_read_binary(&grp, &P, buf->x, buf->len) == ret);
if (ret == 0) {
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.X, &X) == 0);
if (mbedtls_ecp_get_type(&grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
TEST_ASSERT(mbedtls_mpi_cmp_int(&Y, 0) == 0);
TEST_ASSERT(P.Y.p == NULL);
TEST_ASSERT(mbedtls_mpi_cmp_int(&Z, 1) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_int(&P.Z, 1) == 0);
} else {
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.Y, &Y) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.Z, &Z) == 0);
if (buf->x[0] == 0x04 &&
/* (reading compressed format supported only for
* Short Weierstrass curves with prime p where p = 3 mod 4) */
id != MBEDTLS_ECP_DP_SECP224R1 &&
id != MBEDTLS_ECP_DP_SECP224K1) {
/* re-encode in compressed format and test read again */
mbedtls_mpi_free(&P.Y);
buf->x[0] = 0x02 + mbedtls_mpi_get_bit(&Y, 0);
TEST_ASSERT(mbedtls_ecp_point_read_binary(&grp, &P, buf->x, buf->len/2+1) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.Y, &Y) == 0);
}
}
}
exit:
mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&P);
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_ecp_tls_read_point(int id, data_t *buf, char *x, char *y,
char *z, int ret)
{
mbedtls_ecp_group grp;
mbedtls_ecp_point P;
mbedtls_mpi X, Y, Z;
const unsigned char *vbuf = buf->x;
mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&P);
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&X, x) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&Y, y) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&Z, z) == 0);
TEST_ASSERT(mbedtls_ecp_tls_read_point(&grp, &P, &vbuf, buf->len) == ret);
if (ret == 0) {
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.X, &X) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.Y, &Y) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P.Z, &Z) == 0);
TEST_ASSERT((uint32_t) (vbuf - buf->x) == buf->len);
}
exit:
mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&P);
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_tls_write_read_point(int id)
{
mbedtls_ecp_group grp;
mbedtls_ecp_point pt;
unsigned char buf[256];
const unsigned char *vbuf;
size_t olen;
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point_init(&pt);
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
memset(buf, 0x00, sizeof(buf)); vbuf = buf;
TEST_ASSERT(mbedtls_ecp_tls_write_point(&grp, &grp.G,
MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256) == 0);
TEST_ASSERT(mbedtls_ecp_tls_read_point(&grp, &pt, &vbuf, olen) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp.G.X, &pt.X) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp.G.Y, &pt.Y) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp.G.Z, &pt.Z) == 0);
TEST_ASSERT(vbuf == buf + olen);
memset(buf, 0x00, sizeof(buf)); vbuf = buf;
TEST_ASSERT(mbedtls_ecp_tls_write_point(&grp, &grp.G,
MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256) == 0);
TEST_ASSERT(mbedtls_ecp_tls_read_point(&grp, &pt, &vbuf, olen) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp.G.X, &pt.X) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp.G.Y, &pt.Y) == 0);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp.G.Z, &pt.Z) == 0);
TEST_ASSERT(vbuf == buf + olen);
memset(buf, 0x00, sizeof(buf)); vbuf = buf;
TEST_ASSERT(mbedtls_ecp_set_zero(&pt) == 0);
TEST_ASSERT(mbedtls_ecp_tls_write_point(&grp, &pt,
MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256) == 0);
TEST_ASSERT(mbedtls_ecp_tls_read_point(&grp, &pt, &vbuf, olen) == 0);
TEST_ASSERT(mbedtls_ecp_is_zero(&pt));
TEST_ASSERT(vbuf == buf + olen);
memset(buf, 0x00, sizeof(buf)); vbuf = buf;
TEST_ASSERT(mbedtls_ecp_set_zero(&pt) == 0);
TEST_ASSERT(mbedtls_ecp_tls_write_point(&grp, &pt,
MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256) == 0);
TEST_ASSERT(mbedtls_ecp_tls_read_point(&grp, &pt, &vbuf, olen) == 0);
TEST_ASSERT(mbedtls_ecp_is_zero(&pt));
TEST_ASSERT(vbuf == buf + olen);
exit:
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&pt);
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_ecp_tls_read_group(data_t *buf, int result, int bits,
int record_len)
{
mbedtls_ecp_group grp;
const unsigned char *vbuf = buf->x;
int ret;
mbedtls_ecp_group_init(&grp);
ret = mbedtls_ecp_tls_read_group(&grp, &vbuf, buf->len);
TEST_ASSERT(ret == result);
if (ret == 0) {
TEST_ASSERT(mbedtls_mpi_bitlen(&grp.P) == (size_t) bits);
TEST_ASSERT(vbuf - buf->x == record_len);
}
exit:
mbedtls_ecp_group_free(&grp);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_tls_write_read_group(int id)
{
mbedtls_ecp_group grp1, grp2;
unsigned char buf[10];
const unsigned char *vbuf = buf;
size_t len;
int ret;
mbedtls_ecp_group_init(&grp1);
mbedtls_ecp_group_init(&grp2);
memset(buf, 0x00, sizeof(buf));
TEST_ASSERT(mbedtls_ecp_group_load(&grp1, id) == 0);
TEST_ASSERT(mbedtls_ecp_tls_write_group(&grp1, &len, buf, 10) == 0);
ret = mbedtls_ecp_tls_read_group(&grp2, &vbuf, len);
TEST_ASSERT(ret == 0);
if (ret == 0) {
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp1.N, &grp2.N) == 0);
TEST_ASSERT(grp1.id == grp2.id);
}
exit:
mbedtls_ecp_group_free(&grp1);
mbedtls_ecp_group_free(&grp2);
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_ecp_group_metadata(int id, int bit_size, int crv_type,
char *P, char *A, char *B,
char *G_x, char *G_y, char *N,
int tls_id)
{
mbedtls_ecp_group grp, grp_read, grp_cpy;
const mbedtls_ecp_group_id *g_id;
mbedtls_ecp_group_id read_g_id;
const mbedtls_ecp_curve_info *crv, *crv_tls_id, *crv_name;
mbedtls_mpi exp_P, exp_A, exp_B, exp_G_x, exp_G_y, exp_N;
unsigned char buf[3], ecparameters[3] = { 3, 0, tls_id };
const unsigned char *vbuf = buf;
size_t olen;
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_group_init(&grp_read);
mbedtls_ecp_group_init(&grp_cpy);
mbedtls_mpi_init(&exp_P);
mbedtls_mpi_init(&exp_A);
mbedtls_mpi_init(&exp_B);
mbedtls_mpi_init(&exp_G_x);
mbedtls_mpi_init(&exp_G_y);
mbedtls_mpi_init(&exp_N);
// Read expected parameters
TEST_EQUAL(mbedtls_test_read_mpi(&exp_P, P), 0);
TEST_EQUAL(mbedtls_test_read_mpi(&exp_A, A), 0);
TEST_EQUAL(mbedtls_test_read_mpi(&exp_G_x, G_x), 0);
TEST_EQUAL(mbedtls_test_read_mpi(&exp_N, N), 0);
TEST_EQUAL(mbedtls_test_read_mpi(&exp_B, B), 0);
TEST_EQUAL(mbedtls_test_read_mpi(&exp_G_y, G_y), 0);
// Convert exp_A to internal representation (A+2)/4
if (crv_type == MBEDTLS_ECP_TYPE_MONTGOMERY) {
TEST_EQUAL(mbedtls_mpi_add_int(&exp_A, &exp_A, 2), 0);
TEST_EQUAL(mbedtls_mpi_div_int(&exp_A, NULL, &exp_A, 4), 0);
}
// Load group
TEST_EQUAL(mbedtls_ecp_group_load(&grp, id), 0);
// Compare group with expected parameters
// A is NULL for SECPxxxR1 curves
// B and G_y are NULL for curve25519 and curve448
TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_P, &grp.P), 0);
if (*A != 0) {
TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_A, &grp.A), 0);
}
if (*B != 0) {
TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_B, &grp.B), 0);
}
TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_G_x, &grp.G.X), 0);
if (*G_y != 0) {
TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_G_y, &grp.G.Y), 0);
}
TEST_EQUAL(mbedtls_mpi_cmp_mpi(&exp_N, &grp.N), 0);
// Load curve info and compare with known values
crv = mbedtls_ecp_curve_info_from_grp_id(id);
TEST_EQUAL(crv->grp_id, id);
TEST_EQUAL(crv->bit_size, bit_size);
TEST_EQUAL(crv->tls_id, tls_id);
// Load curve from TLS ID and name, and compare IDs
crv_tls_id = mbedtls_ecp_curve_info_from_tls_id(crv->tls_id);
crv_name = mbedtls_ecp_curve_info_from_name(crv->name);
TEST_EQUAL(crv_tls_id->grp_id, id);
TEST_EQUAL(crv_name->grp_id, id);
// Validate write_group against test data
TEST_EQUAL(mbedtls_ecp_tls_write_group(&grp, &olen,
buf, sizeof(buf)),
0);
TEST_EQUAL(mbedtls_test_hexcmp(buf, ecparameters, olen,
sizeof(ecparameters)),
0);
// Read group from buffer and compare with expected ID
TEST_EQUAL(mbedtls_ecp_tls_read_group_id(&read_g_id, &vbuf, olen),
0);
TEST_EQUAL(read_g_id, id);
vbuf = buf;
TEST_EQUAL(mbedtls_ecp_tls_read_group(&grp_read, &vbuf, olen),
0);
TEST_EQUAL(grp_read.id, id);
// Check curve type, and if it can be used for ECDH/ECDSA
TEST_EQUAL(mbedtls_ecp_get_type(&grp), crv_type);
#if defined(MBEDTLS_ECDH_C)
TEST_EQUAL(mbedtls_ecdh_can_do(id), 1);
#endif
#if defined(MBEDTLS_ECDSA_C)
TEST_EQUAL(mbedtls_ecdsa_can_do(id),
crv_type == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS);
#endif
// Copy group and compare with original
TEST_EQUAL(mbedtls_ecp_group_copy(&grp_cpy, &grp), 0);
TEST_EQUAL(mbedtls_ecp_group_cmp(&grp, &grp_cpy), 0);
// Check curve is in curve list and group ID list
for (crv = mbedtls_ecp_curve_list();
crv->grp_id != MBEDTLS_ECP_DP_NONE &&
crv->grp_id != (unsigned) id;
crv++) {
;
}
TEST_EQUAL(crv->grp_id, id);
for (g_id = mbedtls_ecp_grp_id_list();
*g_id != MBEDTLS_ECP_DP_NONE && *g_id != (unsigned) id;
g_id++) {
;
}
TEST_EQUAL(*g_id, (unsigned) id);
exit:
mbedtls_ecp_group_free(&grp); mbedtls_ecp_group_free(&grp_cpy);
mbedtls_ecp_group_free(&grp_read);
mbedtls_mpi_free(&exp_P); mbedtls_mpi_free(&exp_A);
mbedtls_mpi_free(&exp_B); mbedtls_mpi_free(&exp_G_x);
mbedtls_mpi_free(&exp_G_y); mbedtls_mpi_free(&exp_N);
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_ecp_check_privkey(int id, char *key_hex, int ret)
{
mbedtls_ecp_group grp;
mbedtls_mpi d;
mbedtls_ecp_group_init(&grp);
mbedtls_mpi_init(&d);
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&d, key_hex) == 0);
TEST_ASSERT(mbedtls_ecp_check_privkey(&grp, &d) == ret);
exit:
mbedtls_ecp_group_free(&grp);
mbedtls_mpi_free(&d);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECP_C */
void mbedtls_ecp_check_pub_priv(int id_pub, char *Qx_pub, char *Qy_pub,
int id, char *d, char *Qx, char *Qy,
int ret)
{
mbedtls_ecp_keypair pub, prv;
mbedtls_test_rnd_pseudo_info rnd_info;
mbedtls_ecp_keypair_init(&pub);
mbedtls_ecp_keypair_init(&prv);
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
if (id_pub != MBEDTLS_ECP_DP_NONE) {
TEST_ASSERT(mbedtls_ecp_group_load(&pub.grp, id_pub) == 0);
}
TEST_ASSERT(mbedtls_ecp_point_read_string(&pub.Q, 16, Qx_pub, Qy_pub) == 0);
if (id != MBEDTLS_ECP_DP_NONE) {
TEST_ASSERT(mbedtls_ecp_group_load(&prv.grp, id) == 0);
}
TEST_ASSERT(mbedtls_ecp_point_read_string(&prv.Q, 16, Qx, Qy) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&prv.d, d) == 0);
TEST_ASSERT(mbedtls_ecp_check_pub_priv(&pub, &prv,
&mbedtls_test_rnd_pseudo_rand, &rnd_info) == ret);
exit:
mbedtls_ecp_keypair_free(&pub);
mbedtls_ecp_keypair_free(&prv);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECP_C */
void ecp_calc_public(int grp_id, data_t *private_data,
int expected_ret, data_t *expected_public)
{
mbedtls_ecp_keypair key;
mbedtls_ecp_keypair_init(&key);
mbedtls_test_rnd_pseudo_info rnd_info;
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
TEST_EQUAL(mbedtls_ecp_group_load(&key.grp, grp_id), 0);
TEST_EQUAL(mbedtls_mpi_read_binary(&key.d,
private_data->x, private_data->len), 0);
TEST_EQUAL(mbedtls_ecp_keypair_calc_public(&key,
&mbedtls_test_rnd_pseudo_rand, &rnd_info),
expected_ret);
if (expected_ret == 0) {
TEST_EQUAL(mbedtls_ecp_check_pub_priv(&key, &key,
&mbedtls_test_rnd_pseudo_rand, &rnd_info),
0);
unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
size_t length;
TEST_EQUAL(mbedtls_ecp_point_write_binary(&key.grp, &key.Q,
MBEDTLS_ECP_PF_UNCOMPRESSED,
&length, buf, sizeof(buf)),
0);
ASSERT_COMPARE(expected_public->x, expected_public->len, buf, length);
}
exit:
mbedtls_ecp_keypair_free(&key);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECP_C */
void mbedtls_ecp_gen_keypair(int id)
{
mbedtls_ecp_group grp;
mbedtls_ecp_point Q;
mbedtls_mpi d;
mbedtls_test_rnd_pseudo_info rnd_info;
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point_init(&Q);
mbedtls_mpi_init(&d);
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(mbedtls_ecp_gen_keypair(&grp, &d, &Q,
&mbedtls_test_rnd_pseudo_rand,
&rnd_info) == 0);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&grp, &Q) == 0);
TEST_ASSERT(mbedtls_ecp_check_privkey(&grp, &d) == 0);
exit:
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&Q);
mbedtls_mpi_free(&d);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_ECP_C */
void mbedtls_ecp_gen_key(int id)
{
mbedtls_ecp_keypair key;
mbedtls_test_rnd_pseudo_info rnd_info;
mbedtls_ecp_keypair_init(&key);
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
TEST_ASSERT(mbedtls_ecp_gen_key(id, &key,
&mbedtls_test_rnd_pseudo_rand,
&rnd_info) == 0);
TEST_EQUAL(mbedtls_ecp_keypair_get_group_id(&key), id);
TEST_ASSERT(mbedtls_ecp_check_pubkey(&key.grp, &key.Q) == 0);
TEST_ASSERT(mbedtls_ecp_check_privkey(&key.grp, &key.d) == 0);
exit:
mbedtls_ecp_keypair_free(&key);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_set_public_key_group_check(int grp_id, int expected_ret)
{
mbedtls_ecp_keypair key;
mbedtls_ecp_keypair_init(&key);
mbedtls_ecp_point Q;
mbedtls_ecp_point_init(&Q);
TEST_EQUAL(mbedtls_ecp_set_public_key(grp_id, &key, &Q),
expected_ret);
exit:
mbedtls_ecp_keypair_free(&key);
mbedtls_ecp_point_free(&Q);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_set_public_key_good(int grp_id, data_t *public_data)
{
mbedtls_ecp_keypair key;
mbedtls_ecp_keypair_init(&key);
mbedtls_ecp_group grp;
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point Q;
mbedtls_ecp_point_init(&Q);
TEST_EQUAL(mbedtls_ecp_group_load(&grp, grp_id), 0);
TEST_EQUAL(mbedtls_ecp_point_read_binary(&grp, &Q,
public_data->x, public_data->len),
0);
/* Freshly initialized key */
TEST_EQUAL(mbedtls_ecp_set_public_key(grp_id, &key, &Q), 0);
TEST_EQUAL(key.grp.id, grp_id);
TEST_EQUAL(mbedtls_ecp_point_cmp(&key.Q, &Q), 0);
/* Key with a public key already set to a different value */
TEST_EQUAL(mbedtls_mpi_add_int(&key.Q.X, &key.Q.X, 1), 0);
TEST_EQUAL(mbedtls_mpi_add_int(&key.Q.Y, &key.Q.Y, 1), 0);
TEST_EQUAL(mbedtls_mpi_add_int(&key.Q.Z, &key.Q.Z, 1), 0);
TEST_EQUAL(mbedtls_ecp_set_public_key(grp_id, &key, &Q), 0);
TEST_EQUAL(key.grp.id, grp_id);
TEST_EQUAL(mbedtls_ecp_point_cmp(&key.Q, &Q), 0);
exit:
mbedtls_ecp_keypair_free(&key);
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&Q);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_set_public_key_after_private(int private_grp_id, data_t *private_data,
int public_grp_id, data_t *public_data)
{
mbedtls_ecp_keypair key;
mbedtls_ecp_keypair_init(&key);
mbedtls_ecp_group grp;
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point Q;
mbedtls_ecp_point_init(&Q);
mbedtls_mpi d;
mbedtls_mpi_init(&d);
TEST_EQUAL(mbedtls_ecp_group_load(&grp, public_grp_id), 0);
TEST_EQUAL(mbedtls_ecp_point_read_binary(&grp, &Q,
public_data->x, public_data->len),
0);
TEST_EQUAL(mbedtls_ecp_read_key(private_grp_id, &key,
private_data->x, private_data->len),
0);
TEST_EQUAL(mbedtls_mpi_copy(&d, &key.d), 0);
int ret = mbedtls_ecp_set_public_key(public_grp_id, &key, &Q);
if (private_grp_id == public_grp_id) {
TEST_EQUAL(ret, 0);
TEST_EQUAL(key.grp.id, public_grp_id);
TEST_EQUAL(mbedtls_ecp_point_cmp(&key.Q, &Q), 0);
TEST_EQUAL(mbedtls_mpi_cmp_mpi(&d, &key.d), 0);
} else {
TEST_EQUAL(ret, MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
}
exit:
mbedtls_ecp_keypair_free(&key);
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&Q);
mbedtls_mpi_free(&d);
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_ecp_read_key(int grp_id, data_t *in_key, int expected, int canonical)
{
int ret = 0;
mbedtls_ecp_keypair key;
mbedtls_ecp_keypair_init(&key);
mbedtls_ecp_keypair key2;
mbedtls_ecp_keypair_init(&key2);
TEST_EQUAL(mbedtls_mpi_lset(&key.Q.X, 1), 0);
TEST_EQUAL(mbedtls_mpi_lset(&key.Q.Y, 2), 0);
TEST_EQUAL(mbedtls_mpi_lset(&key.Q.Z, 3), 0);
ret = mbedtls_ecp_read_key(grp_id, &key, in_key->x, in_key->len);
TEST_ASSERT(ret == expected);
if (expected == 0) {
TEST_EQUAL(mbedtls_ecp_keypair_get_group_id(&key), grp_id);
ret = mbedtls_ecp_check_privkey(&key.grp, &key.d);
TEST_ASSERT(ret == 0);
TEST_EQUAL(mbedtls_mpi_cmp_int(&key.Q.X, 1), 0);
TEST_EQUAL(mbedtls_mpi_cmp_int(&key.Q.Y, 2), 0);
TEST_EQUAL(mbedtls_mpi_cmp_int(&key.Q.Z, 3), 0);
if (canonical && in_key->len == (key.grp.nbits + 7) / 8) {
unsigned char buf[MBEDTLS_ECP_MAX_BYTES];
size_t length = 0xdeadbeef;
TEST_EQUAL(mbedtls_ecp_write_key_ext(&key,
&length, buf, in_key->len), 0);
TEST_MEMORY_COMPARE(in_key->x, in_key->len,
buf, length);
#if defined(MBEDTLS_TEST_DEPRECATED)
memset(buf, 0, sizeof(buf));
TEST_EQUAL(mbedtls_ecp_write_key(&key, buf, in_key->len), 0);
TEST_MEMORY_COMPARE(in_key->x, in_key->len,
buf, in_key->len);
#endif /* MBEDTLS_TEST_DEPRECATED */
} else {
unsigned char export1[MBEDTLS_ECP_MAX_BYTES];
unsigned char export2[MBEDTLS_ECP_MAX_BYTES];
size_t length1 = 0xdeadbeef;
TEST_EQUAL(mbedtls_ecp_write_key_ext(&key, &length1,
export1, sizeof(export1)), 0);
TEST_EQUAL(mbedtls_ecp_read_key(grp_id, &key2, export1, length1),
expected);
size_t length2 = 0xdeadbeef;
TEST_EQUAL(mbedtls_ecp_write_key_ext(&key2, &length2,
export2, sizeof(export2)), 0);
TEST_MEMORY_COMPARE(export1, length1,
export2, length2);
#if defined(MBEDTLS_TEST_DEPRECATED)
memset(export1, 0, sizeof(export1));
memset(export2, 0, sizeof(export2));
TEST_EQUAL(mbedtls_ecp_write_key(&key, export1, in_key->len), 0);
TEST_EQUAL(mbedtls_ecp_read_key(grp_id, &key2, export1, in_key->len),
expected);
TEST_EQUAL(mbedtls_ecp_write_key(&key2, export2, in_key->len), 0);
TEST_MEMORY_COMPARE(export1, in_key->len,
export2, in_key->len);
#endif /* MBEDTLS_TEST_DEPRECATED */
}
}
exit:
mbedtls_ecp_keypair_free(&key);
mbedtls_ecp_keypair_free(&key2);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_DEPRECATED */
void ecp_write_key(int grp_id, data_t *in_key,
int exported_size, int expected_ret)
{
mbedtls_ecp_keypair key;
mbedtls_ecp_keypair_init(&key);
unsigned char *exported = NULL;
TEST_EQUAL(mbedtls_ecp_read_key(grp_id, &key, in_key->x, in_key->len), 0);
TEST_CALLOC(exported, exported_size);
TEST_EQUAL(mbedtls_ecp_write_key(&key, exported, exported_size),
expected_ret);
if (expected_ret == 0) {
size_t length = (key.grp.nbits + 7) / 8;
const unsigned char *key_start = NULL;
const unsigned char *zeros_start = NULL;
switch (mbedtls_ecp_get_type(&key.grp)) {
case MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:
if ((size_t) exported_size < length) {
length = exported_size;
}
key_start = exported + exported_size - length;
zeros_start = exported;
break;
case MBEDTLS_ECP_TYPE_MONTGOMERY:
TEST_LE_U(length, exported_size);
key_start = exported;
zeros_start = exported + length;
break;
default:
TEST_FAIL("Unknown ECP curve type");
break;
}
if (length < in_key->len) {
/* Shorter output (only possible with Weierstrass keys) */
for (size_t i = 0; i < in_key->len - length; i++) {
mbedtls_test_set_step(i);
TEST_EQUAL(in_key->x[i], 0);
}
TEST_MEMORY_COMPARE(in_key->x + in_key->len - length, length,
key_start, length);
} else {
TEST_MEMORY_COMPARE(in_key->x, in_key->len,
key_start, length);
for (size_t i = 0; i < exported_size - length; i++) {
mbedtls_test_set_step(i);
TEST_EQUAL(zeros_start[i], 0);
}
}
}
exit:
mbedtls_ecp_keypair_free(&key);
mbedtls_free(exported);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_write_key_ext(int grp_id, data_t *in_key,
int exported_size, int expected_ret)
{
mbedtls_ecp_keypair key;
mbedtls_ecp_keypair_init(&key);
unsigned char *exported = NULL;
if (in_key->len != 0) {
TEST_EQUAL(mbedtls_ecp_read_key(grp_id, &key, in_key->x, in_key->len), 0);
} else if (grp_id != MBEDTLS_ECP_DP_NONE) {
TEST_EQUAL(mbedtls_ecp_group_load(&key.grp, grp_id), 0);
}
TEST_CALLOC(exported, exported_size);
size_t olen = 0xdeadbeef;
TEST_EQUAL(mbedtls_ecp_write_key_ext(&key, &olen, exported, exported_size),
expected_ret);
if (expected_ret == 0) {
TEST_EQUAL(olen, (key.grp.nbits + 7) / 8);
TEST_LE_U(olen, MBEDTLS_ECP_MAX_BYTES);
TEST_MEMORY_COMPARE(in_key->x, in_key->len,
exported, olen);
} else {
/* Robustness check: even in the error case, insist that olen is less
* than the buffer size. */
TEST_LE_U(olen, exported_size);
}
exit:
mbedtls_ecp_keypair_free(&key);
mbedtls_free(exported);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_MONTGOMERY_ENABLED:MBEDTLS_ECP_LIGHT */
void genkey_mx_known_answer(int bits, data_t *seed, data_t *expected)
{
mbedtls_test_rnd_buf_info rnd_info;
mbedtls_mpi d;
int ret;
uint8_t *actual = NULL;
mbedtls_mpi_init(&d);
rnd_info.buf = seed->x;
rnd_info.length = seed->len;
rnd_info.fallback_f_rng = NULL;
rnd_info.fallback_p_rng = NULL;
TEST_CALLOC(actual, expected->len);
ret = mbedtls_ecp_gen_privkey_mx(bits, &d,
mbedtls_test_rnd_buffer_rand, &rnd_info);
if (expected->len == 0) {
/* Expecting an error (happens if there isn't enough randomness) */
TEST_ASSERT(ret != 0);
} else {
TEST_EQUAL(ret, 0);
TEST_EQUAL((size_t) bits + 1, mbedtls_mpi_bitlen(&d));
TEST_EQUAL(0, mbedtls_mpi_write_binary(&d, actual, expected->len));
/* Test the exact result. This assumes that the output of the
* RNG is used in a specific way, which is overly constraining.
* The advantage is that it's easier to test the expected properties
* of the generated key:
* - The most significant bit must be at a specific positions
* (can be enforced by checking the bit-length).
* - The least significant bits must have specific values
* (can be enforced by checking these bits).
* - Other bits must be random (by testing with different RNG outputs,
* we validate that those bits are indeed influenced by the RNG). */
TEST_MEMORY_COMPARE(expected->x, expected->len,
actual, expected->len);
}
exit:
mbedtls_free(actual);
mbedtls_mpi_free(&d);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_set_zero(int id, data_t *P_bin)
{
mbedtls_ecp_group grp;
mbedtls_ecp_point pt, zero_pt, nonzero_pt;
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point_init(&pt);
mbedtls_ecp_point_init(&zero_pt);
mbedtls_ecp_point_init(&nonzero_pt);
// Set zero and non-zero points for comparison
TEST_EQUAL(mbedtls_ecp_set_zero(&zero_pt), 0);
TEST_EQUAL(mbedtls_ecp_group_load(&grp, id), 0);
TEST_EQUAL(mbedtls_ecp_point_read_binary(&grp, &nonzero_pt,
P_bin->x, P_bin->len), 0);
TEST_EQUAL(mbedtls_ecp_is_zero(&zero_pt), 1);
TEST_EQUAL(mbedtls_ecp_is_zero(&nonzero_pt), 0);
// Test initialized point
TEST_EQUAL(mbedtls_ecp_set_zero(&pt), 0);
TEST_EQUAL(mbedtls_ecp_is_zero(&pt), 1);
TEST_EQUAL(mbedtls_ecp_point_cmp(&zero_pt, &pt), 0);
TEST_EQUAL(mbedtls_ecp_point_cmp(&nonzero_pt, &zero_pt),
MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
// Test zeroed point
TEST_EQUAL(mbedtls_ecp_set_zero(&pt), 0);
TEST_EQUAL(mbedtls_ecp_is_zero(&pt), 1);
TEST_EQUAL(mbedtls_ecp_point_cmp(&zero_pt, &pt), 0);
TEST_EQUAL(mbedtls_ecp_point_cmp(&nonzero_pt, &pt),
MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
// Set point to non-zero value
TEST_EQUAL(mbedtls_ecp_point_read_binary(&grp, &pt,
P_bin->x, P_bin->len), 0);
TEST_EQUAL(mbedtls_ecp_is_zero(&pt), 0);
TEST_EQUAL(mbedtls_ecp_point_cmp(&zero_pt, &pt),
MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
TEST_EQUAL(mbedtls_ecp_point_cmp(&nonzero_pt, &pt), 0);
// Test non-zero point
TEST_EQUAL(mbedtls_ecp_set_zero(&pt), 0);
TEST_EQUAL(mbedtls_ecp_is_zero(&pt), 1);
TEST_EQUAL(mbedtls_ecp_point_cmp(&zero_pt, &pt), 0);
TEST_EQUAL(mbedtls_ecp_point_cmp(&nonzero_pt, &pt),
MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
// Test freed non-zero point
TEST_EQUAL(mbedtls_ecp_point_read_binary(&grp, &pt,
P_bin->x, P_bin->len), 0);
mbedtls_ecp_point_free(&pt);
TEST_EQUAL(mbedtls_ecp_set_zero(&pt), 0);
TEST_EQUAL(mbedtls_ecp_is_zero(&pt), 1);
TEST_EQUAL(mbedtls_ecp_point_cmp(&zero_pt, &pt), 0);
TEST_EQUAL(mbedtls_ecp_point_cmp(&nonzero_pt, &pt),
MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
exit:
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&pt);
mbedtls_ecp_point_free(&zero_pt);
mbedtls_ecp_point_free(&nonzero_pt);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
void ecp_selftest()
{
TEST_ASSERT(mbedtls_ecp_self_test(1) == 0);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_export(int id, char *Qx, char *Qy, char *d, int expected_ret, int invalid_grp)
{
mbedtls_ecp_keypair key;
mbedtls_ecp_group export_grp;
mbedtls_mpi export_d;
mbedtls_ecp_point export_Q;
mbedtls_ecp_group_init(&export_grp);
mbedtls_ecp_group_init(&key.grp);
mbedtls_mpi_init(&export_d);
mbedtls_ecp_point_init(&export_Q);
mbedtls_ecp_keypair_init(&key);
if (invalid_grp == 0) {
TEST_ASSERT(mbedtls_ecp_group_load(&key.grp, id) == 0);
}
TEST_ASSERT(mbedtls_ecp_point_read_string(&key.Q, 16, Qx, Qy) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&key.d, d) == 0);
TEST_EQUAL(mbedtls_ecp_export(&key, &export_grp,
&export_d, &export_Q), expected_ret);
if (expected_ret == 0) {
TEST_EQUAL(mbedtls_ecp_point_cmp(&key.Q, &export_Q), 0);
TEST_EQUAL(mbedtls_mpi_cmp_mpi(&key.d, &export_d), 0);
TEST_EQUAL(mbedtls_ecp_group_cmp(&key.grp, &export_grp), 0);
/* Check consistency with the group id */
TEST_EQUAL(export_grp.id,
mbedtls_ecp_keypair_get_group_id(&key));
/* Test null arguments: grp only */
mbedtls_ecp_group_free(&export_grp);
mbedtls_ecp_group_init(&export_grp);
TEST_EQUAL(mbedtls_ecp_export(&key, &export_grp, NULL, NULL), 0);
TEST_EQUAL(mbedtls_ecp_group_cmp(&key.grp, &export_grp), 0);
/* Test null arguments: d only */
mbedtls_mpi_free(&export_d);
mbedtls_mpi_init(&export_d);
TEST_EQUAL(mbedtls_ecp_export(&key, NULL, &export_d, NULL), 0);
TEST_EQUAL(mbedtls_mpi_cmp_mpi(&key.d, &export_d), 0);
/* Test null arguments: Q only */
mbedtls_ecp_point_free(&export_Q);
mbedtls_ecp_point_init(&export_Q);
TEST_EQUAL(mbedtls_ecp_export(&key, NULL, NULL, &export_Q), 0);
TEST_EQUAL(mbedtls_ecp_point_cmp(&key.Q, &export_Q), 0);
}
exit:
mbedtls_ecp_keypair_free(&key);
mbedtls_ecp_group_free(&export_grp);
mbedtls_mpi_free(&export_d);
mbedtls_ecp_point_free(&export_Q);
}
/* END_CASE */
/* BEGIN_CASE */
void ecp_check_order(int id, char *expected_order_hex)
{
mbedtls_ecp_group grp;
mbedtls_mpi expected_n;
mbedtls_ecp_group_init(&grp);
mbedtls_mpi_init(&expected_n);
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
TEST_ASSERT(mbedtls_test_read_mpi(&expected_n, expected_order_hex) == 0);
// check sign bits are well-formed (i.e. 1 or -1) - see #5810
TEST_ASSERT(grp.N.s == -1 || grp.N.s == 1);
TEST_ASSERT(expected_n.s == -1 || expected_n.s == 1);
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&grp.N, &expected_n) == 0);
exit:
mbedtls_ecp_group_free(&grp);
mbedtls_mpi_free(&expected_n);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_WITH_MPI_UINT */
void ecp_mod_p_generic_raw(int curve_id,
char *input_N,
char *input_X,
char *result)
{
mbedtls_mpi_uint *X = NULL;
mbedtls_mpi_uint *N = NULL;
mbedtls_mpi_uint *res = NULL;
size_t limbs_X;
size_t limbs_N;
size_t limbs_res;
size_t bytes;
size_t limbs;
size_t curve_bits;
int (*curve_func)(mbedtls_mpi_uint *X, size_t X_limbs);
mbedtls_mpi_mod_modulus m;
mbedtls_mpi_mod_modulus_init(&m);
TEST_EQUAL(mbedtls_test_read_mpi_core(&X, &limbs_X, input_X), 0);
TEST_EQUAL(mbedtls_test_read_mpi_core(&N, &limbs_N, input_N), 0);
TEST_EQUAL(mbedtls_test_read_mpi_core(&res, &limbs_res, result), 0);
bytes = limbs_N * sizeof(mbedtls_mpi_uint);
switch (curve_id) {
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) && defined(MBEDTLS_ECP_NIST_OPTIM)
case MBEDTLS_ECP_DP_SECP192R1:
limbs = BITS_TO_LIMBS(192) * 2;
curve_bits = 192;
curve_func = &mbedtls_ecp_mod_p192_raw;
break;
#endif
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) && defined(MBEDTLS_ECP_NIST_OPTIM)
case MBEDTLS_ECP_DP_SECP224R1:
limbs = BITS_TO_LIMBS(224) * 2;
curve_bits = 224;
curve_func = &mbedtls_ecp_mod_p224_raw;
break;
#endif
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) && defined(MBEDTLS_ECP_NIST_OPTIM)
case MBEDTLS_ECP_DP_SECP256R1:
limbs = BITS_TO_LIMBS(256) * 2;
curve_bits = 256;
curve_func = &mbedtls_ecp_mod_p256_raw;
break;
#endif
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) && defined(MBEDTLS_ECP_NIST_OPTIM)
case MBEDTLS_ECP_DP_SECP384R1:
limbs = BITS_TO_LIMBS(384) * 2;
curve_bits = 384;
curve_func = &mbedtls_ecp_mod_p384_raw;
break;
#endif
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) && defined(MBEDTLS_ECP_NIST_OPTIM)
case MBEDTLS_ECP_DP_SECP521R1:
limbs = BITS_TO_LIMBS(521) * 2;
curve_bits = 521;
curve_func = &mbedtls_ecp_mod_p521_raw;
break;
#endif
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
case MBEDTLS_ECP_DP_SECP192K1:
limbs = BITS_TO_LIMBS(192) * 2;
curve_bits = 192;
curve_func = &mbedtls_ecp_mod_p192k1_raw;
break;
#endif
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
case MBEDTLS_ECP_DP_SECP224K1:
limbs = BITS_TO_LIMBS(224) * 2;
curve_bits = 224;
curve_func = &mbedtls_ecp_mod_p224k1_raw;
break;
#endif
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
case MBEDTLS_ECP_DP_SECP256K1:
limbs = BITS_TO_LIMBS(256) * 2;
curve_bits = 256;
curve_func = &mbedtls_ecp_mod_p256k1_raw;
break;
#endif
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
case MBEDTLS_ECP_DP_CURVE25519:
limbs = BITS_TO_LIMBS(255) * 2;
curve_bits = 255;
curve_func = &mbedtls_ecp_mod_p255_raw;
break;
#endif
#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
case MBEDTLS_ECP_DP_CURVE448:
limbs = BITS_TO_LIMBS(448) * 2;
curve_bits = 448;
curve_func = &mbedtls_ecp_mod_p448_raw;
break;
#endif
default:
mbedtls_test_fail("Unsupported curve_id", __LINE__, __FILE__);
goto exit;
}
TEST_EQUAL(limbs_X, limbs);
TEST_EQUAL(limbs_res, limbs_N);
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
&m, N, limbs_N), 0);
TEST_EQUAL((*curve_func)(X, limbs_X), 0);
mbedtls_mpi_mod_raw_fix_quasi_reduction(X, &m);
TEST_LE_U(mbedtls_mpi_core_bitlen(X, limbs_X), curve_bits);
TEST_MEMORY_COMPARE(X, bytes, res, bytes);
exit:
mbedtls_free(X);
mbedtls_free(res);
mbedtls_mpi_mod_modulus_free(&m);
mbedtls_free(N);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_WITH_MPI_UINT */
void ecp_mod_setup(char *input_A, int id, int ctype, int iret)
{
int ret;
mbedtls_mpi_mod_modulus m;
mbedtls_mpi_mod_modulus_init(&m);
mbedtls_mpi_uint *p = NULL;
size_t p_limbs;
size_t bytes;
TEST_EQUAL(mbedtls_test_read_mpi_core(&p, &p_limbs, input_A), 0);
ret = mbedtls_ecp_modulus_setup(&m, id, ctype);
TEST_EQUAL(ret, iret);
if (ret == 0) {
TEST_ASSERT(m.int_rep != MBEDTLS_MPI_MOD_REP_INVALID);
/* Test for limb sizes */
TEST_EQUAL(m.limbs, p_limbs);
bytes = p_limbs * sizeof(mbedtls_mpi_uint);
if (m.int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY) {
/* Test for validity of moduli by the presence of Montgomery consts */
TEST_ASSERT(m.rep.mont.mm != 0);
TEST_ASSERT(m.rep.mont.rr != NULL);
} else {
TEST_ASSERT(m.rep.ored.modp != NULL);
}
/* Compare output byte-by-byte */
TEST_MEMORY_COMPARE(p, bytes, m.p, bytes);
/* Test for user free-ing allocated memory */
mbedtls_mpi_mod_modulus_free(&m);
}
exit:
mbedtls_mpi_mod_modulus_free(&m);
mbedtls_free(p);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_WITH_MPI_UINT */
void ecp_mod_mul_inv(char *input_A, int id, int ctype)
{
size_t limbs;
mbedtls_mpi_mod_modulus m;
mbedtls_mpi_mod_residue rA; // For input
mbedtls_mpi_mod_residue rA_inverse; // For inverse input
mbedtls_mpi_mod_residue rX; // For result
mbedtls_mpi_uint *rX_raw = NULL;
mbedtls_mpi_uint *A_inverse = NULL;
mbedtls_mpi_uint *A = NULL;
mbedtls_mpi_uint *bufx = NULL;
const mbedtls_mpi_uint one[1] = { 1 };
mbedtls_mpi_mod_modulus_init(&m);
TEST_ASSERT(mbedtls_ecp_modulus_setup(&m, id, ctype) == 0);
TEST_EQUAL(mbedtls_test_read_mpi_core(&A, &limbs, input_A), 0);
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&rA, &m, A, limbs));
/* Test for limb sizes */
TEST_EQUAL(m.limbs, limbs);
TEST_CALLOC(A_inverse, limbs);
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&rA_inverse, &m, A_inverse, limbs));
TEST_CALLOC(rX_raw, limbs);
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&rX, &m, rX_raw, limbs));
/* Get inverse of A mode m, and multiply it with itself,
* to see whether the result equal to '1' */
TEST_EQUAL(0, mbedtls_mpi_mod_inv(&rA_inverse, &rA, &m));
TEST_EQUAL(mbedtls_mpi_mod_mul(&rX, &rA, &rA_inverse, &m), 0);
TEST_CALLOC(bufx, limbs);
TEST_EQUAL(mbedtls_mpi_mod_write(&rX, &m, (unsigned char *) bufx,
limbs * ciL,
MBEDTLS_MPI_MOD_EXT_REP_LE), 0);
TEST_MEMORY_COMPARE(bufx, ciL, one, ciL);
/*Borrow the buffer of A to compare the left lims with 0 */
memset(A, 0, limbs * ciL);
TEST_MEMORY_COMPARE(&bufx[1], (limbs - 1) * ciL, A, (limbs - 1) * ciL);
exit:
mbedtls_mpi_mod_modulus_free(&m);
mbedtls_mpi_mod_residue_release(&rA);
mbedtls_mpi_mod_residue_release(&rA_inverse);
mbedtls_mpi_mod_residue_release(&rX);
mbedtls_free(A);
mbedtls_free(A_inverse);
mbedtls_free(rX_raw);
mbedtls_free(bufx);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_WITH_MPI_UINT */
void ecp_mod_add_sub(char *input_A, char *input_B, int id, int ctype)
{
size_t p_A_limbs;
size_t p_B_limbs;
size_t bytes;
mbedtls_mpi_mod_modulus m;
mbedtls_mpi_mod_residue rA;
mbedtls_mpi_mod_residue rB;
mbedtls_mpi_mod_residue rS;
mbedtls_mpi_uint *p_A = NULL;
mbedtls_mpi_uint *p_B = NULL;
mbedtls_mpi_uint *p_S = NULL;
mbedtls_mpi_mod_modulus_init(&m);
TEST_EQUAL(mbedtls_test_read_mpi_core(&p_A, &p_A_limbs, input_A), 0);
TEST_EQUAL(mbedtls_test_read_mpi_core(&p_B, &p_B_limbs, input_B), 0);
TEST_EQUAL(0, mbedtls_ecp_modulus_setup(&m, id, ctype));
/* Test for limb sizes for two input value and modulus */
TEST_EQUAL(p_A_limbs, p_B_limbs);
TEST_EQUAL(m.limbs, p_A_limbs);
bytes = p_A_limbs * ciL;
TEST_CALLOC(p_S, p_A_limbs);
TEST_EQUAL(mbedtls_mpi_mod_residue_setup(&rA, &m, p_A, p_A_limbs), 0);
TEST_EQUAL(mbedtls_mpi_mod_residue_setup(&rB, &m, p_B, p_B_limbs), 0);
TEST_EQUAL(mbedtls_mpi_mod_residue_setup(&rS, &m, p_S, p_A_limbs), 0);
/* Firstly add A and B to get the sum S, then subtract B,
* the difference should be equal to A*/
TEST_EQUAL(0, mbedtls_mpi_mod_add(&rS, &rA, &rB, &m));
TEST_EQUAL(0, mbedtls_mpi_mod_sub(&rS, &rS, &rB, &m));
/* Compare difference with rA byte-by-byte */
TEST_MEMORY_COMPARE(rA.p, bytes, rS.p, bytes);
exit:
mbedtls_mpi_mod_modulus_free(&m);
mbedtls_mpi_mod_residue_release(&rA);
mbedtls_mpi_mod_residue_release(&rB);
mbedtls_mpi_mod_residue_release(&rS);
mbedtls_free(p_A);
mbedtls_free(p_B);
mbedtls_free(p_S);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_WITH_MPI_UINT */
void ecp_mod_read_write(char *input_A, int id, int ctype)
{
size_t limbs;
size_t bytes;
mbedtls_mpi_mod_modulus m;
mbedtls_mpi_mod_residue rA; // For input
mbedtls_mpi_mod_residue rX; // For read back
mbedtls_mpi_uint *rX_raw = NULL;
mbedtls_mpi_uint *A = NULL;
mbedtls_mpi_uint *bufx = NULL;
mbedtls_mpi_mod_modulus_init(&m);
TEST_EQUAL(0, mbedtls_ecp_modulus_setup(&m, id, ctype));
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&A, &limbs, input_A));
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&rA, &m, A, limbs));
/* Test for limb sizes */
TEST_EQUAL(m.limbs, limbs);
TEST_CALLOC(rX_raw, limbs);
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&rX, &m, rX_raw, limbs));
bytes = limbs * ciL;
TEST_CALLOC(bufx, limbs);
/* Write source mod residue to a buffer, then read it back to
* the destination mod residue, compare the two mod residues.
* Firstly test little endian write and read */
TEST_EQUAL(0, mbedtls_mpi_mod_write(&rA, &m, (unsigned char *) bufx,
bytes, MBEDTLS_MPI_MOD_EXT_REP_LE));
TEST_EQUAL(0, mbedtls_mpi_mod_read(&rX, &m, (unsigned char *) bufx,
bytes, MBEDTLS_MPI_MOD_EXT_REP_LE));
TEST_EQUAL(limbs, rX.limbs);
TEST_MEMORY_COMPARE(rA.p, bytes, rX.p, bytes);
memset(bufx, 0x00, bytes);
memset(rX_raw, 0x00, bytes);
/* Then test big endian write and read */
TEST_EQUAL(0, mbedtls_mpi_mod_write(&rA, &m, (unsigned char *) bufx,
bytes,
MBEDTLS_MPI_MOD_EXT_REP_BE));
TEST_EQUAL(0, mbedtls_mpi_mod_read(&rX, &m, (unsigned char *) bufx,
bytes,
MBEDTLS_MPI_MOD_EXT_REP_BE));
TEST_EQUAL(limbs, rX.limbs);
TEST_MEMORY_COMPARE(rA.p, bytes, rX.p, bytes);
exit:
mbedtls_mpi_mod_modulus_free(&m);
mbedtls_mpi_mod_residue_release(&rA);
mbedtls_mpi_mod_residue_release(&rX);
mbedtls_free(A);
mbedtls_free(rX_raw);
mbedtls_free(bufx);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_WITH_MPI_UINT */
void ecp_mod_random(int id, int ctype)
{
size_t limbs;
mbedtls_mpi_mod_modulus m;
mbedtls_mpi_mod_residue rX; // For random data
mbedtls_mpi_uint *rX_raw = NULL;
mbedtls_mpi_mod_modulus_init(&m);
TEST_EQUAL(0, mbedtls_ecp_modulus_setup(&m, id, ctype));
limbs = m.limbs;
TEST_CALLOC(rX_raw, limbs);
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&rX, &m, rX_raw, limbs));
TEST_EQUAL(0, mbedtls_mpi_mod_random(&rX, 1, &m,
mbedtls_test_rnd_std_rand, NULL));
TEST_ASSERT(mbedtls_mpi_core_lt_ct(rX.p, m.p, limbs) == MBEDTLS_CT_TRUE);
exit:
mbedtls_mpi_mod_modulus_free(&m);
mbedtls_mpi_mod_residue_release(&rX);
mbedtls_free(rX_raw);
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_LIGHT */
void check_variant()
{
mbedtls_ecp_variant variant = mbedtls_ecp_get_variant();
#if defined(MBEDTLS_ECP_WITH_MPI_UINT)
TEST_EQUAL(variant, MBEDTLS_ECP_VARIANT_WITH_MPI_UINT);
#else
TEST_EQUAL(variant, MBEDTLS_ECP_VARIANT_WITH_MPI_STRUCT);
#endif
}
/* END_CASE */
|