1
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
80
82
83
84
85
86
87
88
96
97
98
99
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
135
136
137
141
142
143
148
149
150
151
152
153
154
155
156
157
158
159
160
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
186
187
188
192
193
194
195
199
200
207
213
214
215
216
217
218
219
220
221
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
260
264
265
268
272
273
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
293
294
295
296
299
300
301
302
303
304
305
306
307
309
310
311
312
315
316
317
318
319
322
323
324
325
331
332
333
334
335
336
337
338
339
342
352
353
356
362
363
366
367
368
371
372
373
374
375
376
377
378
381
382
383
384
385
386
387
388
389
390
393
394
395
398
399
400
401
402
403
404
405
406
407
408
411
412
413
414
415
416
417
418
420
425
426
430
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
459
460
461
462
463
464
465
466
471
472
473
474
475
476
477
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
499
500
502
503
505
506
508
509
511
512
514
515
517
518
520
521
523
524
526
527
529
530
532
533
535
536
537
538
539
541
542
543
544
547
551
552
555
556
557
558
559
560
561
562
563
568
569
570
571
572
573
574
575
576
579
580
581
582
583
590
591
592
593
594
597
598
599
600
601
608
609
610
611
612
615
616
617
618
619
622
623
630
631
632
633
634
637
649
650
653
654
655
656
657
658
659
660
661
662
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
689
690
691
692
693
694
695
696
697
698
701
702
703
706
707
708
709
710
711
712
715
716
717
718
719
722
723
730
731
737
738
739
740
741
744
745
746
749
750
751
752
753
754
755
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
775
782
783
786
787
788
789
790
791
792
793
794
795
796
797
798
799
802
808
809
812
813
814
815
816
817
818
823
824
825
826
827
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
876
877
878
881
882
885
886
887
888
889
890
891
892
893
894
895
898
899
900
901
902
903
904
905
908
909
910
911
912
913
914
915
916
917
918
919
922
923
924
925
926
927
928
929
930
931
932
935
936
937
938
939
940
943
944
945
946
947
951
952
953
954
955
956
957
964
965
968
969
972
973
974
975
976
977
978
979
980
981
982
983
984
990
991
992
993
994
995
996
997
998
999
1000
1001
1004
1007
1008
1009
1012
1013
1016
1017
1018
1019
1020
1021
1022
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1043
1046
1047
1051
1052
1055
1056
1057
1058
1059
1060
1061
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1076
1077
1078
1079
1080
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1096
1099
1100
1103
1106
1107
1110
1111
1112
1113
1114
1117
1118
1119
1120
1121
1122
1123
1126
1127
1128
1129
1130
1131
1132
1133
1134
1137
1138
1141
1142
1145
1146
1149
1150
1151
1154
1155
1156
1157
1158
1159
1165
1166
1167
1168
1169
1172
1173
1174
1178
1179
1180
1181
1182
1185
1186
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1208
1209
1210
1211
1212
1213
1214
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1237
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1266
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
1312
1313
1314
1317
1318
1319
1323
1324
1325
1326
1327
1328
1329
1330
1331
1334
1335
1336
1337
1338
1341
1342
1343
1344
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
1374
1375
1376
1380
1381
1382
1383
1384
1385
1386
1387
1388
1391
1392
1395
1396
1397
1398
1401
1402
1405
1406
1409
1410
1411
1412
1416
1422
1423
1426
1427
1428
1429
1430
1431
1437
1438
1439
1440
1441
1444
1445
1446
1447
1448
1449
1452
1453
1454
1455
1456
1457
1458
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
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
1606
1607
1608
1609
1610
1611
1612
1613
1616
1619
1620
1623
1624
1627
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
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
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
1723
1724
1725
1726
1727
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
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
1868
1871
1874
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1896
1897
1898
1899
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1914
1915
1916
1917
1918
1919
1924
1925
1929
1930
1933
1934
1935
1936
1937
1938
1939
1944
1945
1949
1950
1951
1957
1958
1959
1964
1965
1970
1973
1974
1975
1976
1977
1978
1979
1980
1987
1988
1989
1990
1991
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2011
2012
2013
2014
2015
2016
2017
2018
2019
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2048
2049
2050
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2068
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2178
2179
2180
2181
2182
2183
2184
2185
2186
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2209
2212
2213
2214
2215
2216
2220
2221
2222
2223
2224
2225
2228
2229
2230
2231
2232
2233
2238
2239
2240
2245
2248
2249
2253
2254
2258
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2305
2306
2307
2308
2309
2310
2312
2313
2317
2318
2322
2323
2324
2325
2326
2327
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2363
2364
2367
2368
2369
2370
2371
2372
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2416
2417
2418
2419
2420
2425
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2449
2450
2451
2452
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2623
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2662
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2703
2704
2705
2706
2707
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2732
2733
2737
2738
2739
2740
2741
2745
2746
2747
2751
2752
2753
2754
2755
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2786
2787
2788
2789
2793
2794
2795
2796
2797
2802
2803
2804
2805
2806
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2847
2848
2849
2850
2851
2852
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2894
2897
2900
2901
2902
2903
2904
2905
2910
2911
2912
2913
2914
2918
2919
2920
2925
2926
2927
2928
2929
2934
2935
2936
2937
2938
2939
2943
2944
2945
2946
2950
2951
2952
2953
2954
2955
2956
2957
2958
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2981
2986
2992
2998
2999
3000
3001
3002
3003
3004
3010
3011
3012
3013
3014
3015
3016
3017
3018
3020
3021
3024
3025
3028
3032
3033
3034
3046
3047
3048
3049
3050
3051
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3067
3068
3069
3070
3072
3075
3076
3079
3082
3083
3084
3085
3086
3087
3090
3091
3092
3093
3094
3095
3096
3097
3100
3101
3102
3106
3107
3111
3112
3113
3114
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3131
3132
3133
3136
3137
3138
3139
3140
3141
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3169
3170
3171
3172
3173
3174
3175
3177
3178
3179
3182
3183
3184
3185
3186
3187
3188
3189
3202
3203
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3220
3221
3222
3226
3227
3228
3229
3230
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3272
3273
3274
3275
3276
3277
3278
3279
3282
3283
3284
3285
3286
3287
3290
3291
3292
3293
3294
3295
3296
3297
3298
3301
3302
3303
3304
3305
3306
3309
3310
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3335
3336
3337
3338
3339
3345
3346
3347
3348
3351
3352
3353
3354
3355
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3379
3380
3381
3385
3386
3387
3388
3389
3390
3391
3392
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3439
3440
3441
3442
3443
3444
3446
3447
3448
3449
3451
3452
3453
3455
3456
3457
3458
3459
3460
3461
3466
3467
3468
3469
3470
3471
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3510
3511
3512
3513
3520
3521
3522
3523
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3578
3579
3580
3581
3582
3583
3584
3585
3588
3589
3592
3593
3594
3595
3596
3597
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3623
3624
3625
3626
3627
3630
3631
3632
3633
3634
3635
3636
3639
3640
3641
3642
3643
3644
3645
3646
3647
/* ... */
/* ... */
#include "common.h"
/* ... */
#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 "mbedtls/bn_mul.h"
#include "ecp_invasive.h"
#include <string.h>
7 includes
#if !defined(MBEDTLS_ECP_ALT)
#define ECP_VALIDATE_RET(cond) \
MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA)...
#define ECP_VALIDATE(cond) \
MBEDTLS_INTERNAL_VALIDATE(cond)...
#include "mbedtls/platform.h"
#include "mbedtls/ecp_internal.h"
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
#if defined(MBEDTLS_HMAC_DRBG_C)
#include "mbedtls/hmac_drbg.h"
#elif defined(MBEDTLS_CTR_DRBG_C)
#include "mbedtls/ctr_drbg.h"
#else
#error \
"Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."/* ... */
#endif/* ... */
#endif
#if defined(MBEDTLS_SELF_TEST)
/* ... */
static unsigned long add_count, dbl_count, mul_count;/* ... */
#endif
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
/* ... */
#if defined(MBEDTLS_HMAC_DRBG_C)
typedef mbedtls_hmac_drbg_context ecp_drbg_context;
static inline void ecp_drbg_init(ecp_drbg_context *ctx)
{
mbedtls_hmac_drbg_init(ctx);
}ecp_drbg_init (ecp_drbg_context *ctx) { ... }
static inline void ecp_drbg_free(ecp_drbg_context *ctx)
{
mbedtls_hmac_drbg_free(ctx);
}ecp_drbg_free (ecp_drbg_context *ctx) { ... }
static inline int ecp_drbg_random(void *p_rng,
unsigned char *output, size_t output_len)
{
return mbedtls_hmac_drbg_random(p_rng, output, output_len);
}ecp_drbg_random (void *p_rng, unsigned char *output, size_t output_len) { ... }
static int ecp_drbg_seed(ecp_drbg_context *ctx,
const mbedtls_mpi *secret, size_t secret_len)
{
int ret;
unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES];
const mbedtls_md_type_t md_type =
(mbedtls_md_type_t) (mbedtls_md_list()[0]);
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
if (secret_len > MBEDTLS_ECP_MAX_BYTES) {
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
goto cleanup;
}if (secret_len > MBEDTLS_ECP_MAX_BYTES) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(secret,
secret_bytes, secret_len));
ret = mbedtls_hmac_drbg_seed_buf(ctx, md_info, secret_bytes, secret_len);
cleanup:
mbedtls_platform_zeroize(secret_bytes, secret_len);
return ret;
}ecp_drbg_seed (ecp_drbg_context *ctx, const mbedtls_mpi *secret, size_t secret_len) { ... }
/* ... */
#elif defined(MBEDTLS_CTR_DRBG_C)
typedef mbedtls_ctr_drbg_context ecp_drbg_context;
static inline void ecp_drbg_init(ecp_drbg_context *ctx)
{
mbedtls_ctr_drbg_init(ctx);
}{ ... }
static inline void ecp_drbg_free(ecp_drbg_context *ctx)
{
mbedtls_ctr_drbg_free(ctx);
}{ ... }
static inline int ecp_drbg_random(void *p_rng,
unsigned char *output, size_t output_len)
{
return mbedtls_ctr_drbg_random(p_rng, output, output_len);
}{ ... }
/* ... */
static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len)
{
(void) ctx;
memset(out, 0, len);
return 0;
}{ ... }
static int ecp_drbg_seed(ecp_drbg_context *ctx,
const mbedtls_mpi *secret, size_t secret_len)
{
int ret;
unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES];
if (secret_len > MBEDTLS_ECP_MAX_BYTES) {
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
goto cleanup;
}if (secret_len > MBEDTLS_ECP_MAX_BYTES) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(secret,
secret_bytes, secret_len));
ret = mbedtls_ctr_drbg_seed(ctx, ecp_ctr_drbg_null_entropy, NULL,
secret_bytes, secret_len);
cleanup:
mbedtls_platform_zeroize(secret_bytes, secret_len);
return ret;
}{ ... }
/* ... */#else
#error \
"Invalid configuration detected. Include check_config.h to ensure that the configuration is valid."/* ... */
#endif /* ... */
#endif
#if defined(MBEDTLS_ECP_RESTARTABLE)
/* ... */
static unsigned ecp_max_ops = 0;
/* ... */
void mbedtls_ecp_set_max_ops(unsigned max_ops)
{
ecp_max_ops = max_ops;
}mbedtls_ecp_set_max_ops (unsigned max_ops) { ... }
/* ... */
int mbedtls_ecp_restart_is_enabled(void)
{
return ecp_max_ops != 0;
}mbedtls_ecp_restart_is_enabled (void) { ... }
/* ... */
struct mbedtls_ecp_restart_mul {
mbedtls_ecp_point R;
size_t i;
mbedtls_ecp_point *T;
unsigned char T_size;
enum {
ecp_rsm_init = 0,
ecp_rsm_pre_dbl,
ecp_rsm_pre_norm_dbl,
ecp_rsm_pre_add,
ecp_rsm_pre_norm_add,
ecp_rsm_comb_core,
ecp_rsm_final_norm,
...} state;
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
ecp_drbg_context drbg_ctx;
unsigned char drbg_seeded;/* ... */
#endif
...};
/* ... */
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;
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
ecp_drbg_init(&ctx->drbg_ctx);
ctx->drbg_seeded = 0;/* ... */
#endif
}ecp_restart_rsm_init (mbedtls_ecp_restart_mul_ctx *ctx) { ... }
/* ... */
static void ecp_restart_rsm_free(mbedtls_ecp_restart_mul_ctx *ctx)
{
unsigned char i;
if (ctx == NULL) {
return;
}if (ctx == NULL) { ... }
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);
}for (i = 0; i < ctx->T_size; i++) { ... }
mbedtls_free(ctx->T);
}if (ctx->T != NULL) { ... }
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
ecp_drbg_free(&ctx->drbg_ctx);
#endif
ecp_restart_rsm_init(ctx);
}ecp_restart_rsm_free (mbedtls_ecp_restart_mul_ctx *ctx) { ... }
/* ... */
struct mbedtls_ecp_restart_muladd {
mbedtls_ecp_point mP;
mbedtls_ecp_point R;
enum {
ecp_rsma_mul1 = 0,
ecp_rsma_mul2,
ecp_rsma_add,
ecp_rsma_norm,
...} state;
...};
/* ... */
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;
}ecp_restart_ma_init (mbedtls_ecp_restart_muladd_ctx *ctx) { ... }
/* ... */
static void ecp_restart_ma_free(mbedtls_ecp_restart_muladd_ctx *ctx)
{
if (ctx == NULL) {
return;
}if (ctx == NULL) { ... }
mbedtls_ecp_point_free(&ctx->mP);
mbedtls_ecp_point_free(&ctx->R);
ecp_restart_ma_init(ctx);
}ecp_restart_ma_free (mbedtls_ecp_restart_muladd_ctx *ctx) { ... }
/* ... */
void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx)
{
ECP_VALIDATE(ctx != NULL);
ctx->ops_done = 0;
ctx->depth = 0;
ctx->rsm = NULL;
ctx->ma = NULL;
}mbedtls_ecp_restart_init (mbedtls_ecp_restart_ctx *ctx) { ... }
/* ... */
void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx)
{
if (ctx == NULL) {
return;
}if (ctx == NULL) { ... }
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);
}mbedtls_ecp_restart_free (mbedtls_ecp_restart_ctx *ctx) { ... }
/* ... */
int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
mbedtls_ecp_restart_ctx *rs_ctx,
unsigned ops)
{
ECP_VALIDATE_RET(grp != NULL);
if (rs_ctx != NULL && ecp_max_ops != 0) {
/* ... */
if (grp->pbits >= 512) {
ops *= 4;
}if (grp->pbits >= 512) { ... } else if (grp->pbits >= 384) {
ops *= 2;
}else if (grp->pbits >= 384) { ... }
/* ... */
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;
}if ((rs_ctx->ops_done != 0) && (rs_ctx->ops_done > ecp_max_ops || ops > ecp_max_ops - rs_ctx->ops_done)) { ... }
rs_ctx->ops_done += ops;
}if (rs_ctx != NULL && ecp_max_ops != 0) { ... }
return 0;
}mbedtls_ecp_check_budget (const mbedtls_ecp_group *grp, mbedtls_ecp_restart_ctx *rs_ctx, unsigned ops) { ... }
#define ECP_RS_ENTER(SUB) do { \
\
if (rs_ctx != NULL && rs_ctx->depth++ == 0) \
rs_ctx->ops_done = 0; \
\
\
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)...
#define ECP_RS_LEAVE(SUB) do { \
\
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
#define ECP_RS_ENTER(sub) (void) rs_ctx;
#define ECP_RS_LEAVE(sub) (void) rs_ctx;
/* ... */
#endif
/* ... */
static const mbedtls_ecp_curve_info ecp_supported_curves[] =
{
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
{ MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" }defined (MBEDTLS_ECP_DP_SECP521R1_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
{ MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" }defined (MBEDTLS_ECP_DP_BP512R1_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
{ MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" }defined (MBEDTLS_ECP_DP_SECP384R1_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
{ MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" }defined (MBEDTLS_ECP_DP_BP384R1_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
{ MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" }defined (MBEDTLS_ECP_DP_SECP256R1_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
{ MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" }defined (MBEDTLS_ECP_DP_SECP256K1_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
{ MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" }defined (MBEDTLS_ECP_DP_BP256R1_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
{ MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" }defined (MBEDTLS_ECP_DP_SECP224R1_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
{ MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" }defined (MBEDTLS_ECP_DP_SECP224K1_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
{ MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" }defined (MBEDTLS_ECP_DP_SECP192R1_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
{ MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" }defined (MBEDTLS_ECP_DP_SECP192K1_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
{ MBEDTLS_ECP_DP_CURVE25519, 29, 256, "x25519" }defined (MBEDTLS_ECP_DP_CURVE25519_ENABLED) { ... },
#endif
#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
{ MBEDTLS_ECP_DP_CURVE448, 30, 448, "x448" }defined (MBEDTLS_ECP_DP_CURVE448_ENABLED) { ... },
#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];
/* ... */
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void)
{
return ecp_supported_curves;
}{ ... }
/* ... */
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;
}for (curve_info = mbedtls_ecp_curve_list(); curve_info->grp_id != MBEDTLS_ECP_DP_NONE; curve_info++) { ... }
ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
init_done = 1;
}if (!init_done) { ... }
return ecp_supported_grp_id;
}{ ... }
/* ... */
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;
}if (curve_info->grp_id == grp_id) { ... }
}for (curve_info = mbedtls_ecp_curve_list(); curve_info->grp_id != MBEDTLS_ECP_DP_NONE; curve_info++) { ... }
return NULL;
}{ ... }
/* ... */
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;
}if (curve_info->tls_id == tls_id) { ... }
}for (curve_info = mbedtls_ecp_curve_list(); curve_info->grp_id != MBEDTLS_ECP_DP_NONE; curve_info++) { ... }
return NULL;
}{ ... }
/* ... */
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;
}if (name == 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;
}if (strcmp(curve_info->name, name) == 0) { ... }
}for (curve_info = mbedtls_ecp_curve_list(); curve_info->grp_id != MBEDTLS_ECP_DP_NONE; curve_info++) { ... }
return NULL;
}{ ... }
/* ... */
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.X.p == NULL) { ... }
if (grp->G.Y.p == NULL) {
return MBEDTLS_ECP_TYPE_MONTGOMERY;
}if (grp->G.Y.p == NULL) { ... } else {
return MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS;
}else { ... }
}{ ... }
/* ... */
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
{
ECP_VALIDATE(pt != NULL);
mbedtls_mpi_init(&pt->X);
mbedtls_mpi_init(&pt->Y);
mbedtls_mpi_init(&pt->Z);
}{ ... }
/* ... */
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
{
ECP_VALIDATE(grp != NULL);
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;
}{ ... }
/* ... */
void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
{
ECP_VALIDATE(key != NULL);
mbedtls_ecp_group_init(&key->grp);
mbedtls_mpi_init(&key->d);
mbedtls_ecp_point_init(&key->Q);
}{ ... }
/* ... */
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
{
if (pt == NULL) {
return;
}if (pt == NULL) { ... }
mbedtls_mpi_free(&(pt->X));
mbedtls_mpi_free(&(pt->Y));
mbedtls_mpi_free(&(pt->Z));
}{ ... }
/* ... */
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
{
size_t i;
if (grp == NULL) {
return;
}if (grp == NULL) { ... }
if (grp->h != 1) {
mbedtls_mpi_free(&grp->P);
mbedtls_mpi_free(&grp->A);
mbedtls_mpi_free(&grp->B);
mbedtls_ecp_point_free(&grp->G);
mbedtls_mpi_free(&grp->N);
}if (grp->h != 1) { ... }
if (grp->T != NULL) {
for (i = 0; i < grp->T_size; i++) {
mbedtls_ecp_point_free(&grp->T[i]);
}for (i = 0; i < grp->T_size; i++) { ... }
mbedtls_free(grp->T);
}if (grp->T != NULL) { ... }
mbedtls_platform_zeroize(grp, sizeof(mbedtls_ecp_group));
}{ ... }
/* ... */
void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
{
if (key == NULL) {
return;
}if (key == NULL) { ... }
mbedtls_ecp_group_free(&key->grp);
mbedtls_mpi_free(&key->d);
mbedtls_ecp_point_free(&key->Q);
}{ ... }
/* ... */
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
ECP_VALIDATE_RET(P != NULL);
ECP_VALIDATE_RET(Q != NULL);
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;
}{ ... }
/* ... */
int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
{
ECP_VALIDATE_RET(dst != NULL);
ECP_VALIDATE_RET(src != NULL);
return mbedtls_ecp_group_load(dst, src->id);
}{ ... }
/* ... */
int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
ECP_VALIDATE_RET(pt != NULL);
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;
}{ ... }
/* ... */
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
{
ECP_VALIDATE_RET(pt != NULL);
return mbedtls_mpi_cmp_int(&pt->Z, 0) == 0;
}{ ... }
/* ... */
int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
const mbedtls_ecp_point *Q)
{
ECP_VALIDATE_RET(P != NULL);
ECP_VALIDATE_RET(Q != NULL);
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;
}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 MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}{ ... }
/* ... */
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;
ECP_VALIDATE_RET(P != NULL);
ECP_VALIDATE_RET(x != NULL);
ECP_VALIDATE_RET(y != NULL);
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;
}{ ... }
/* ... */
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;
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(P != NULL);
ECP_VALIDATE_RET(olen != NULL);
ECP_VALIDATE_RET(buf != NULL);
ECP_VALIDATE_RET(format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
format == MBEDTLS_ECP_PF_COMPRESSED);
plen = mbedtls_mpi_size(&grp->P);
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
(void) format;
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
*olen = plen;
if (buflen < *olen) {
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
}if (buflen < *olen) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&P->X, buf, plen));
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
/* ... */
if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) {
if (buflen < 1) {
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
}if (buflen < 1) { ... }
buf[0] = 0x00;
*olen = 1;
return 0;
}if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) { ... }
if (format == MBEDTLS_ECP_PF_UNCOMPRESSED) {
*olen = 2 * plen + 1;
if (buflen < *olen) {
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
}if (buflen < *olen) { ... }
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));
}if (format == MBEDTLS_ECP_PF_UNCOMPRESSED) { ... } else if (format == MBEDTLS_ECP_PF_COMPRESSED) {
*olen = plen + 1;
if (buflen < *olen) {
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
}if (buflen < *olen) { ... }
buf[0] = 0x02 + mbedtls_mpi_get_bit(&P->Y, 0);
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
}else if (format == MBEDTLS_ECP_PF_COMPRESSED) { ... }
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { ... }
/* ... */#endif
cleanup:
return ret;
}{ ... }
/* ... */
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;
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(pt != NULL);
ECP_VALIDATE_RET(ilen == 0 || buf != NULL);
if (ilen < 1) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if (ilen < 1) { ... }
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;
}if (plen != ilen) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&pt->X, buf, plen));
mbedtls_mpi_free(&pt->Y);
if (grp->id == MBEDTLS_ECP_DP_CURVE25519) {
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&pt->X, plen * 8 - 1, 0));
}if (grp->id == MBEDTLS_ECP_DP_CURVE25519) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { ... }
/* ... */#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);
}if (ilen == 1) { ... } else {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}else { ... }
}if (buf[0] == 0x00) { ... }
if (buf[0] != 0x04) {
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
}if (buf[0] != 0x04) { ... }
if (ilen != 2 * plen + 1) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if (ilen != 2 * plen + 1) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->X, buf + 1, plen));
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->Y,
buf + 1 + plen, plen));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { ... }
/* ... */#endif
cleanup:
return ret;
}{ ... }
/* ... */
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;
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(pt != NULL);
ECP_VALIDATE_RET(buf != NULL);
ECP_VALIDATE_RET(buf_len == 0 || *buf != NULL);
/* ... */
if (buf_len < 2) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if (buf_len < 2) { ... }
data_len = *(*buf)++;
if (data_len < 1 || data_len > buf_len - 1) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if (data_len < 1 || data_len > buf_len - 1) { ... }
/* ... */
buf_start = *buf;
*buf += data_len;
return mbedtls_ecp_point_read_binary(grp, pt, buf_start, data_len);
}{ ... }
/* ... */
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;
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(pt != NULL);
ECP_VALIDATE_RET(olen != NULL);
ECP_VALIDATE_RET(buf != NULL);
ECP_VALIDATE_RET(format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
format == MBEDTLS_ECP_PF_COMPRESSED);
/* ... */
if (blen < 1) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if (blen < 1) { ... }
if ((ret = mbedtls_ecp_point_write_binary(grp, pt, format,
olen, buf + 1, blen - 1)) != 0) {
return ret;
}if ((ret = mbedtls_ecp_point_write_binary(grp, pt, format, olen, buf + 1, blen - 1)) != 0) { ... }
/* ... */
buf[0] = (unsigned char) *olen;
++*olen;
return 0;
}{ ... }
/* ... */
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;
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(buf != NULL);
ECP_VALIDATE_RET(len == 0 || *buf != NULL);
if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, len)) != 0) {
return ret;
}if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, len)) != 0) { ... }
return mbedtls_ecp_group_load(grp, grp_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;
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(buf != NULL);
ECP_VALIDATE_RET(len == 0 || *buf != NULL);
/* ... */
if (len < 3) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if (len < 3) { ... }
/* ... */
if (*(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if (*(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) { ... }
/* ... */
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;
}if ((curve_info = mbedtls_ecp_curve_info_from_tls_id(tls_id)) == NULL) { ... }
*grp = curve_info->grp_id;
return 0;
}{ ... }
/* ... */
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;
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(buf != NULL);
ECP_VALIDATE_RET(olen != NULL);
if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL) { ... }
/* ... */
*olen = 3;
if (blen < *olen) {
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
}if (blen < *olen) { ... }
/* ... */
*buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
/* ... */
MBEDTLS_PUT_UINT16_BE(curve_info->tls_id, buf, 0);
return 0;
}{ ... }
/* ... */
static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
if (grp->modp == NULL) {
return mbedtls_mpi_mod_mpi(N, N, &grp->P);
}if (grp->modp == NULL) { ... }
if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) ||
mbedtls_mpi_bitlen(N) > 2 * grp->pbits) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) || mbedtls_mpi_bitlen(N) > 2 * grp->pbits) { ... }
MBEDTLS_MPI_CHK(grp->modp(N));
while (N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(N, N, &grp->P));
}while (N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) { ... }
while (mbedtls_mpi_cmp_mpi(N, &grp->P) >= 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(N, N, &grp->P));
}while (mbedtls_mpi_cmp_mpi(N, &grp->P) >= 0) { ... }
cleanup:
return ret;
}{ ... }
/* ... */
/* ... */
#if defined(MBEDTLS_SELF_TEST)
#define INC_MUL_COUNT mul_count++;
#else
#define INC_MUL_COUNT
#endif
#define MOD_MUL(N) \
do \
{ \
MBEDTLS_MPI_CHK(ecp_modp(&(N), grp)); \
INC_MUL_COUNT \
...} while (0)...
static inline int mbedtls_mpi_mul_mod(const mbedtls_ecp_group *grp,
mbedtls_mpi *X,
const mbedtls_mpi *A,
const mbedtls_mpi *B)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(X, A, B));
MOD_MUL(*X);
cleanup:
return ret;
}{ ... }
/* ... */
#define MOD_SUB(N) \
while ((N).s < 0 && mbedtls_mpi_cmp_int(&(N), 0) != 0) \
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&(N), &(N), &grp->P))...
#if (defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \
!(defined(MBEDTLS_ECP_NO_FALLBACK) && \
defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \
defined(MBEDTLS_ECP_ADD_MIXED_ALT))) || \
(defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) && \
!(defined(MBEDTLS_ECP_NO_FALLBACK) && \
defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)))
static inline int mbedtls_mpi_sub_mod(const mbedtls_ecp_group *grp,
mbedtls_mpi *X,
const mbedtls_mpi *A,
const mbedtls_mpi *B)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(X, A, B));
MOD_SUB(*X);
cleanup:
return ret;
}{ ... }
/* ... */#endif
/* ... */
#define MOD_ADD(N) \
while (mbedtls_mpi_cmp_mpi(&(N), &grp->P) >= 0) \
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&(N), &(N), &grp->P))...
static inline int mbedtls_mpi_add_mod(const mbedtls_ecp_group *grp,
mbedtls_mpi *X,
const mbedtls_mpi *A,
const mbedtls_mpi *B)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, A, B));
MOD_ADD(*X);
cleanup:
return ret;
}{ ... }
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \
!(defined(MBEDTLS_ECP_NO_FALLBACK) && \
defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \
defined(MBEDTLS_ECP_ADD_MIXED_ALT))
static inline int mbedtls_mpi_shift_l_mod(const mbedtls_ecp_group *grp,
mbedtls_mpi *X,
size_t count)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, count));
MOD_ADD(*X);
cleanup:
return ret;
}{ ... }
/* ... */#endif \
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
/* ... */
/* ... */
static int ecp_normalize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt)
{
if (mbedtls_mpi_cmp_int(&pt->Z, 0) == 0) {
return 0;
}if (mbedtls_mpi_cmp_int(&pt->Z, 0) == 0) { ... }
#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
if (mbedtls_internal_ecp_grp_capable(grp)) {
return mbedtls_internal_ecp_normalize_jac(grp, pt);
}if (mbedtls_internal_ecp_grp_capable(grp)) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
#else
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi Zi, ZZi;
mbedtls_mpi_init(&Zi); mbedtls_mpi_init(&ZZi);
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&Zi, &pt->Z, &grp->P));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ZZi, &Zi, &Zi));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->X, &pt->X, &ZZi));
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Y, &pt->Y, &ZZi));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Y, &pt->Y, &Zi));
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
cleanup:
mbedtls_mpi_free(&Zi); mbedtls_mpi_free(&ZZi);
return ret;/* ... */
#endif
}{ ... }
/* ... */
static int ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
mbedtls_ecp_point *T[], size_t T_size)
{
if (T_size < 2) {
return ecp_normalize_jac(grp, *T);
}if (T_size < 2) { ... }
#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
if (mbedtls_internal_ecp_grp_capable(grp)) {
return mbedtls_internal_ecp_normalize_jac_many(grp, T, T_size);
}if (mbedtls_internal_ecp_grp_capable(grp)) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
#else
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t i;
mbedtls_mpi *c, u, Zi, ZZi;
if ((c = mbedtls_calloc(T_size, sizeof(mbedtls_mpi))) == NULL) {
return MBEDTLS_ERR_ECP_ALLOC_FAILED;
}if ((c = mbedtls_calloc(T_size, sizeof(mbedtls_mpi))) == NULL) { ... }
for (i = 0; i < T_size; i++) {
mbedtls_mpi_init(&c[i]);
}for (i = 0; i < T_size; i++) { ... }
mbedtls_mpi_init(&u); mbedtls_mpi_init(&Zi); mbedtls_mpi_init(&ZZi);
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&c[0], &T[0]->Z));
for (i = 1; i < T_size; i++) {
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &c[i], &c[i-1], &T[i]->Z));
}for (i = 1; i < T_size; i++) { ... }
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&u, &c[T_size-1], &grp->P));
for (i = T_size - 1;; i--) {
/* ... */
if (i == 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Zi, &u));
}if (i == 0) { ... } else {
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &Zi, &u, &c[i-1]));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &u, &u, &T[i]->Z));
}else { ... }
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ZZi, &Zi, &Zi));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T[i]->X, &T[i]->X, &ZZi));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T[i]->Y, &T[i]->Y, &ZZi));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T[i]->Y, &T[i]->Y, &Zi));
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->X, grp->P.n));
MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->Y, grp->P.n));
mbedtls_mpi_free(&T[i]->Z);
if (i == 0) {
break;
}if (i == 0) { ... }
}for (i = T_size - 1;; i--) { ... }
cleanup:
mbedtls_mpi_free(&u); mbedtls_mpi_free(&Zi); mbedtls_mpi_free(&ZZi);
for (i = 0; i < T_size; i++) {
mbedtls_mpi_free(&c[i]);
}for (i = 0; i < T_size; i++) { ... }
mbedtls_free(c);
return ret;/* ... */
#endif
}{ ... }
/* ... */
static int ecp_safe_invert_jac(const mbedtls_ecp_group *grp,
mbedtls_ecp_point *Q,
unsigned char inv)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
unsigned char nonzero;
mbedtls_mpi mQY;
mbedtls_mpi_init(&mQY);
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mQY, &grp->P, &Q->Y));
nonzero = mbedtls_mpi_cmp_int(&Q->Y, 0) != 0;
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&Q->Y, &mQY, inv & nonzero));
cleanup:
mbedtls_mpi_free(&mQY);
return ret;
}{ ... }
/* ... */
static int ecp_double_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_ecp_point *P)
{
#if defined(MBEDTLS_SELF_TEST)
dbl_count++;
#endif
#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
if (mbedtls_internal_ecp_grp_capable(grp)) {
return mbedtls_internal_ecp_double_jac(grp, R, P);
}if (mbedtls_internal_ecp_grp_capable(grp)) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
#else
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi M, S, T, U;
mbedtls_mpi_init(&M); mbedtls_mpi_init(&S); mbedtls_mpi_init(&T); mbedtls_mpi_init(&U);
if (grp->A.p == NULL) {
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->Z, &P->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &T, &P->X, &S));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &U, &P->X, &S));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &T, &U));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&M, &S, 3)); MOD_ADD(M);
}if (grp->A.p == NULL) { ... } else {
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->X, &P->X));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&M, &S, 3)); MOD_ADD(M);
if (mbedtls_mpi_cmp_int(&grp->A, 0) != 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->Z, &P->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T, &S, &S));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &T, &grp->A));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &M, &M, &S));
}if (mbedtls_mpi_cmp_int(&grp->A, 0) != 0) { ... }
}else { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T, &P->Y, &P->Y));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &T, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->X, &T));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &S, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &U, &T, &T));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &U, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T, &M, &M));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T, &T, &S));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T, &T, &S));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &S, &S, &T));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &S, &M));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &S, &S, &U));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &U, &P->Y, &P->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &U, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->X, &T));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Y, &S));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Z, &U));
cleanup:
mbedtls_mpi_free(&M); mbedtls_mpi_free(&S); mbedtls_mpi_free(&T); mbedtls_mpi_free(&U);
return ret;/* ... */
#endif
}{ ... }
/* ... */
static int ecp_add_mixed(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
{
#if defined(MBEDTLS_SELF_TEST)
add_count++;
#endif
#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
if (mbedtls_internal_ecp_grp_capable(grp)) {
return mbedtls_internal_ecp_add_mixed(grp, R, P, Q);
}if (mbedtls_internal_ecp_grp_capable(grp)) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_ADD_MIXED_ALT)
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
#else
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
/* ... */
if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) {
return mbedtls_ecp_copy(R, Q);
}if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) { ... }
if (Q->Z.p != NULL && mbedtls_mpi_cmp_int(&Q->Z, 0) == 0) {
return mbedtls_ecp_copy(R, P);
}if (Q->Z.p != NULL && mbedtls_mpi_cmp_int(&Q->Z, 0) == 0) { ... }
/* ... */
if (Q->Z.p != NULL && mbedtls_mpi_cmp_int(&Q->Z, 1) != 0) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if (Q->Z.p != NULL && mbedtls_mpi_cmp_int(&Q->Z, 1) != 0) { ... }
mbedtls_mpi_init(&T1); mbedtls_mpi_init(&T2); mbedtls_mpi_init(&T3); mbedtls_mpi_init(&T4);
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T1, &P->Z, &P->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T2, &T1, &P->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T1, &T1, &Q->X));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T2, &T2, &Q->Y));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T1, &T1, &P->X));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T2, &T2, &P->Y));
if (mbedtls_mpi_cmp_int(&T1, 0) == 0) {
if (mbedtls_mpi_cmp_int(&T2, 0) == 0) {
ret = ecp_double_jac(grp, R, P);
goto cleanup;
}if (mbedtls_mpi_cmp_int(&T2, 0) == 0) { ... } else {
ret = mbedtls_ecp_set_zero(R);
goto cleanup;
}else { ... }
}if (mbedtls_mpi_cmp_int(&T1, 0) == 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &Z, &P->Z, &T1));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T3, &T1, &T1));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T4, &T3, &T1));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T3, &T3, &P->X));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &T3));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &T1, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &X, &T2, &T2));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &X, &X, &T1));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &X, &X, &T4));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T3, &T3, &X));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T3, &T3, &T2));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T4, &T4, &P->Y));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &Y, &T3, &T4));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->X, &X));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Y, &Y));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Z, &Z));
cleanup:
mbedtls_mpi_free(&T1); mbedtls_mpi_free(&T2); mbedtls_mpi_free(&T3); mbedtls_mpi_free(&T4);
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
return ret;/* ... */
#endif
}{ ... }
/* ... */
static int ecp_randomize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
if (mbedtls_internal_ecp_grp_capable(grp)) {
return mbedtls_internal_ecp_randomize_jac(grp, pt, f_rng, p_rng);
}if (mbedtls_internal_ecp_grp_capable(grp)) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
#else
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi l, ll;
mbedtls_mpi_init(&l); mbedtls_mpi_init(&ll);
MBEDTLS_MPI_CHK(mbedtls_mpi_random(&l, 2, &grp->P, f_rng, p_rng));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Z, &pt->Z, &l));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ll, &l, &l));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->X, &pt->X, &ll));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ll, &ll, &l));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Y, &pt->Y, &ll));
cleanup:
mbedtls_mpi_free(&l); mbedtls_mpi_free(&ll);
if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
}if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) { ... }
return ret;/* ... */
#endif
}{ ... }
/* ... */
#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
#endif
#define COMB_MAX_D (MBEDTLS_ECP_MAX_BITS + 1) / 2
#define COMB_MAX_PRE (1 << (MBEDTLS_ECP_WINDOW_SIZE - 1))
/* ... */
static void ecp_comb_recode_core(unsigned char x[], size_t d,
unsigned char w, const mbedtls_mpi *m)
{
size_t i, j;
unsigned char c, cc, adjust;
memset(x, 0, d+1);
for (i = 0; i < d; i++) {
for (j = 0; j < w; j++) {
x[i] |= mbedtls_mpi_get_bit(m, i + d * j) << j;
}for (j = 0; j < w; j++) { ... }
}for (i = 0; i < d; i++) { ... }
c = 0;
for (i = 1; i <= d; i++) {
cc = x[i] & c;
x[i] = x[i] ^ c;
c = cc;
adjust = 1 - (x[i] & 0x01);
c |= x[i] & (x[i-1] * adjust);
x[i] = x[i] ^ (x[i-1] * adjust);
x[i-1] |= adjust << 7;
}for (i = 1; i <= d; i++) { ... }
}{ ... }
/* ... */
static int ecp_precompute_comb(const mbedtls_ecp_group *grp,
mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
unsigned char w, size_t d,
mbedtls_ecp_restart_ctx *rs_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
unsigned char i;
size_t j = 0;
const unsigned char T_size = 1U << (w - 1);
mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
goto dbl;
}if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) { ... }
if (rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl) {
goto norm_dbl;
}if (rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl) { ... }
if (rs_ctx->rsm->state == ecp_rsm_pre_add) {
goto add;
}if (rs_ctx->rsm->state == ecp_rsm_pre_add) { ... }
if (rs_ctx->rsm->state == ecp_rsm_pre_norm_add) {
goto norm_add;
}if (rs_ctx->rsm->state == ecp_rsm_pre_norm_add) { ... }
}if (rs_ctx != NULL && rs_ctx->rsm != NULL) { ... }
/* ... */#else
(void) rs_ctx;
#endif
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
rs_ctx->rsm->state = ecp_rsm_pre_dbl;
rs_ctx->rsm->i = 0;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL) { ... }
dbl:/* ... */
#endif
/* ... */
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&T[0], P));
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
j = rs_ctx->rsm->i;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) { ... } else/* ... */
#endif
j = 0;
for (; j < d * (w - 1); j++) {
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL);
i = 1U << (j / d);
cur = T + i;
if (j % d == 0) {
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(cur, T + (i >> 1)));
}if (j % d == 0) { ... }
MBEDTLS_MPI_CHK(ecp_double_jac(grp, cur, cur));
}for (; j < d * (w - 1); j++) { ... }
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL) { ... }
norm_dbl:/* ... */
#endif
/* ... */
j = 0;
for (i = 1; i < T_size; i <<= 1) {
TT[j++] = T + i;
}for (i = 1; i < T_size; i <<= 1) { ... }
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
rs_ctx->rsm->state = ecp_rsm_pre_add;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL) { ... }
add:/* ... */
#endif
/* ... */
MBEDTLS_ECP_BUDGET((T_size - 1) * MBEDTLS_ECP_OPS_ADD);
for (i = 1; i < T_size; i <<= 1) {
j = i;
while (j--) {
MBEDTLS_MPI_CHK(ecp_add_mixed(grp, &T[i + j], &T[j], &T[i]));
}while (j--) { ... }
}for (i = 1; i < T_size; i <<= 1) { ... }
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL) { ... }
norm_add:/* ... */
#endif
/* ... */
for (j = 0; j + 1 < T_size; j++) {
TT[j] = T + j + 1;
}for (j = 0; j + 1 < T_size; j++) { ... }
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
cleanup:
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
rs_ctx->rsm->i = j;
}if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) { ... }
}if (rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { ... }
/* ... */#endif
return ret;
}{ ... }
/* ... */
static int ecp_select_comb(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_ecp_point T[], unsigned char T_size,
unsigned char i)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
unsigned char ii, j;
ii = (i & 0x7Fu) >> 1;
for (j = 0; j < T_size; j++) {
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&R->X, &T[j].X, j == ii));
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&R->Y, &T[j].Y, j == ii));
}for (j = 0; j < T_size; j++) { ... }
MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, R, i >> 7));
cleanup:
return ret;
}{ ... }
/* ... */
static int ecp_mul_comb_core(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_ecp_point T[], unsigned char T_size,
const unsigned char x[], size_t d,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
mbedtls_ecp_restart_ctx *rs_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_ecp_point Txi;
size_t i;
mbedtls_ecp_point_init(&Txi);
#if !defined(MBEDTLS_ECP_RESTARTABLE)
(void) rs_ctx;
#endif
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
rs_ctx->rsm->state != ecp_rsm_comb_core) {
rs_ctx->rsm->i = 0;
rs_ctx->rsm->state = ecp_rsm_comb_core;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->state != ecp_rsm_comb_core) { ... }
if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
i = rs_ctx->rsm->i;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) { ... } else
#endif
{
int have_rng = 1;
i = d;
MBEDTLS_MPI_CHK(ecp_select_comb(grp, R, T, T_size, x[i]));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&R->Z, 1));
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
if (f_rng == NULL) {
have_rng = 0;
}if (f_rng == NULL) { ... }
/* ... */#endif
if (have_rng) {
MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, R, f_rng, p_rng));
}if (have_rng) { ... }
}else { ... }
while (i != 0) {
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD);
--i;
MBEDTLS_MPI_CHK(ecp_double_jac(grp, R, R));
MBEDTLS_MPI_CHK(ecp_select_comb(grp, &Txi, T, T_size, x[i]));
MBEDTLS_MPI_CHK(ecp_add_mixed(grp, R, R, &Txi));
}while (i != 0) { ... }
cleanup:
mbedtls_ecp_point_free(&Txi);
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
rs_ctx->rsm->i = i;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { ... }
/* ... */#endif
return ret;
}{ ... }
/* ... */
static int ecp_comb_recode_scalar(const mbedtls_ecp_group *grp,
const mbedtls_mpi *m,
unsigned char k[COMB_MAX_D + 1],
size_t d,
unsigned char w,
unsigned char *parity_trick)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi M, mm;
mbedtls_mpi_init(&M);
mbedtls_mpi_init(&mm);
if (mbedtls_mpi_get_bit(&grp->N, 0) != 1) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if (mbedtls_mpi_get_bit(&grp->N, 0) != 1) { ... }
*parity_trick = (mbedtls_mpi_get_bit(m, 0) == 0);
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&M, m));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mm, &grp->N, m));
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&M, &mm, *parity_trick));
ecp_comb_recode_core(k, d, w, &M);
cleanup:
mbedtls_mpi_free(&mm);
mbedtls_mpi_free(&M);
return ret;
}{ ... }
/* ... */
static int ecp_mul_comb_after_precomp(const mbedtls_ecp_group *grp,
mbedtls_ecp_point *R,
const mbedtls_mpi *m,
const mbedtls_ecp_point *T,
unsigned char T_size,
unsigned char w,
size_t d,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
mbedtls_ecp_restart_ctx *rs_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
unsigned char parity_trick;
unsigned char k[COMB_MAX_D + 1];
mbedtls_ecp_point *RR = R;
int have_rng = 1;
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
RR = &rs_ctx->rsm->R;
if (rs_ctx->rsm->state == ecp_rsm_final_norm) {
goto final_norm;
}if (rs_ctx->rsm->state == ecp_rsm_final_norm) { ... }
}if (rs_ctx != NULL && rs_ctx->rsm != NULL) { ... }
/* ... */#endif
MBEDTLS_MPI_CHK(ecp_comb_recode_scalar(grp, m, k, d, w,
&parity_trick));
MBEDTLS_MPI_CHK(ecp_mul_comb_core(grp, RR, T, T_size, k, d,
f_rng, p_rng, rs_ctx));
MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, RR, parity_trick));
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
rs_ctx->rsm->state = ecp_rsm_final_norm;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL) { ... }
final_norm:
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);/* ... */
#endif
/* ... */
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
if (f_rng == NULL) {
have_rng = 0;
}if (f_rng == NULL) { ... }
/* ... */#endif
if (have_rng) {
MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, RR, f_rng, p_rng));
}if (have_rng) { ... }
MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, RR));
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, RR));
}if (rs_ctx != NULL && rs_ctx->rsm != NULL) { ... }
/* ... */#endif
cleanup:
return ret;
}{ ... }
/* ... */
static unsigned char ecp_pick_window_size(const mbedtls_ecp_group *grp,
unsigned char p_eq_g)
{
unsigned char w;
/* ... */
w = grp->nbits >= 384 ? 5 : 4;
/* ... */
if (p_eq_g) {
w++;
}if (p_eq_g) { ... }
/* ... */
#if (MBEDTLS_ECP_WINDOW_SIZE < 6)
if (w > MBEDTLS_ECP_WINDOW_SIZE) {
w = MBEDTLS_ECP_WINDOW_SIZE;
}if (w > MBEDTLS_ECP_WINDOW_SIZE) { ... }
/* ... */#endif
if (w >= grp->nbits) {
w = 2;
}if (w >= grp->nbits) { ... }
return w;
}{ ... }
/* ... */
static int ecp_mul_comb(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
mbedtls_ecp_restart_ctx *rs_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
unsigned char w, p_eq_g, i;
size_t d;
unsigned char T_size = 0, T_ok = 0;
mbedtls_ecp_point *T = NULL;
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
ecp_drbg_context drbg_ctx;
ecp_drbg_init(&drbg_ctx);/* ... */
#endif
ECP_RS_ENTER(rsm);
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
if (f_rng == NULL) {
f_rng = &ecp_drbg_random;
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
p_rng = &rs_ctx->rsm->drbg_ctx;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL) { ... } else/* ... */
#endif
p_rng = &drbg_ctx;
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx == NULL || rs_ctx->rsm == NULL ||
rs_ctx->rsm->drbg_seeded == 0)/* ... */
#endif
{
const size_t m_len = (grp->nbits + 7) / 8;
MBEDTLS_MPI_CHK(ecp_drbg_seed(p_rng, m, m_len));
...}
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
rs_ctx->rsm->drbg_seeded = 1;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL) { ... }
/* ... */#endif
}if (f_rng == NULL) { ... }
/* ... */#endif
#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
p_eq_g = (mbedtls_mpi_cmp_mpi(&P->Y, &grp->G.Y) == 0 &&
mbedtls_mpi_cmp_mpi(&P->X, &grp->G.X) == 0);/* ... */
#else
p_eq_g = 0;
#endif
w = ecp_pick_window_size(grp, p_eq_g);
T_size = 1U << (w - 1);
d = (grp->nbits + w - 1) / w;
if (p_eq_g && grp->T != NULL) {
T = grp->T;
T_ok = 1;
}if (p_eq_g && grp->T != NULL) { ... } else
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL) {
T = rs_ctx->rsm->T;
rs_ctx->rsm->T = NULL;
rs_ctx->rsm->T_size = 0;
T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL) { ... } else
#endif
{
T = mbedtls_calloc(T_size, sizeof(mbedtls_ecp_point));
if (T == NULL) {
ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
goto cleanup;
}if (T == NULL) { ... }
for (i = 0; i < T_size; i++) {
mbedtls_ecp_point_init(&T[i]);
}for (i = 0; i < T_size; i++) { ... }
T_ok = 0;
}else { ... }
if (!T_ok) {
MBEDTLS_MPI_CHK(ecp_precompute_comb(grp, T, P, w, d, rs_ctx));
if (p_eq_g) {
/* ... */
grp->T = T;
grp->T_size = T_size;
}if (p_eq_g) { ... }
}if (!T_ok) { ... }
MBEDTLS_MPI_CHK(ecp_mul_comb_after_precomp(grp, R, m,
T, T_size, w, d,
f_rng, p_rng, rs_ctx));
cleanup:
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
ecp_drbg_free(&drbg_ctx);
#endif
if (T == grp->T) {
T = NULL;
}if (T == grp->T) { ... }
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL) {
rs_ctx->rsm->T_size = T_size;
rs_ctx->rsm->T = T;
T = NULL;
}if (rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL) { ... }
/* ... */#endif
if (T != NULL) {
for (i = 0; i < T_size; i++) {
mbedtls_ecp_point_free(&T[i]);
}for (i = 0; i < T_size; i++) { ... }
mbedtls_free(T);
}if (T != NULL) { ... }
int should_free_R = (ret != 0);
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
should_free_R = 0;
}if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { ... }
/* ... */#endif
if (should_free_R) {
mbedtls_ecp_point_free(R);
}if (should_free_R) { ... }
ECP_RS_LEAVE(rsm);
return ret;
}{ ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
/* ... */
/* ... */
static int ecp_normalize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P)
{
#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
if (mbedtls_internal_ecp_grp_capable(grp)) {
return mbedtls_internal_ecp_normalize_mxz(grp, P);
}if (mbedtls_internal_ecp_grp_capable(grp)) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
#else
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&P->Z, &P->Z, &grp->P));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &P->X, &P->X, &P->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1));
cleanup:
return ret;/* ... */
#endif
}{ ... }
/* ... */
static int ecp_randomize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
if (mbedtls_internal_ecp_grp_capable(grp)) {
return mbedtls_internal_ecp_randomize_mxz(grp, P, f_rng, p_rng);
}if (mbedtls_internal_ecp_grp_capable(grp)) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
#else
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi l;
mbedtls_mpi_init(&l);
MBEDTLS_MPI_CHK(mbedtls_mpi_random(&l, 2, &grp->P, f_rng, p_rng));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &P->X, &P->X, &l));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &P->Z, &P->Z, &l));
cleanup:
mbedtls_mpi_free(&l);
if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
}if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) { ... }
return ret;/* ... */
#endif
}{ ... }
/* ... */
static int ecp_double_add_mxz(const mbedtls_ecp_group *grp,
mbedtls_ecp_point *R, mbedtls_ecp_point *S,
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
const mbedtls_mpi *d)
{
#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
if (mbedtls_internal_ecp_grp_capable(grp)) {
return mbedtls_internal_ecp_double_add_mxz(grp, R, S, P, Q, d);
}if (mbedtls_internal_ecp_grp_capable(grp)) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
#else
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
mbedtls_mpi_init(&A); mbedtls_mpi_init(&AA); mbedtls_mpi_init(&B);
mbedtls_mpi_init(&BB); mbedtls_mpi_init(&E); mbedtls_mpi_init(&C);
mbedtls_mpi_init(&D); mbedtls_mpi_init(&DA); mbedtls_mpi_init(&CB);
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &A, &P->X, &P->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &AA, &A, &A));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &B, &P->X, &P->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &BB, &B, &B));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &E, &AA, &BB));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &C, &Q->X, &Q->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &D, &Q->X, &Q->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &DA, &D, &A));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &CB, &C, &B));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &S->X, &DA, &CB));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S->X, &S->X, &S->X));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &S->Z, &DA, &CB));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S->Z, &S->Z, &S->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S->Z, d, &S->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &R->X, &AA, &BB));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &R->Z, &grp->A, &E));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &R->Z, &BB, &R->Z));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &R->Z, &E, &R->Z));
cleanup:
mbedtls_mpi_free(&A); mbedtls_mpi_free(&AA); mbedtls_mpi_free(&B);
mbedtls_mpi_free(&BB); mbedtls_mpi_free(&E); mbedtls_mpi_free(&C);
mbedtls_mpi_free(&D); mbedtls_mpi_free(&DA); mbedtls_mpi_free(&CB);
return ret;/* ... */
#endif
}{ ... }
/* ... */
static int ecp_mul_mxz(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
int have_rng = 1;
size_t i;
unsigned char b;
mbedtls_ecp_point RP;
mbedtls_mpi PX;
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
ecp_drbg_context drbg_ctx;
ecp_drbg_init(&drbg_ctx);/* ... */
#endif
mbedtls_ecp_point_init(&RP); mbedtls_mpi_init(&PX);
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
if (f_rng == NULL) {
const size_t m_len = (grp->nbits + 7) / 8;
MBEDTLS_MPI_CHK(ecp_drbg_seed(&drbg_ctx, m, m_len));
f_rng = &ecp_drbg_random;
p_rng = &drbg_ctx;
}if (f_rng == NULL) { ... }
/* ... */#endif
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&PX, &P->X));
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&RP, P));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&R->X, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&R->Z, 0));
mbedtls_mpi_free(&R->Y);
MOD_ADD(RP.X);
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
if (f_rng == NULL) {
have_rng = 0;
}if (f_rng == NULL) { ... }
/* ... */#endif
if (have_rng) {
MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, &RP, f_rng, p_rng));
}if (have_rng) { ... }
i = grp->nbits + 1;
while (i-- > 0) {
b = mbedtls_mpi_get_bit(m, i);
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->X, &RP.X, b));
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->Z, &RP.Z, b));
MBEDTLS_MPI_CHK(ecp_double_add_mxz(grp, R, &RP, R, &RP, &PX));
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->X, &RP.X, b));
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->Z, &RP.Z, b));
}while (i-- > 0) { ... }
/* ... */
have_rng = 1;
#if defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
if (f_rng == NULL) {
have_rng = 0;
}if (f_rng == NULL) { ... }
/* ... */#endif
if (have_rng) {
MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, R, f_rng, p_rng));
}if (have_rng) { ... }
MBEDTLS_MPI_CHK(ecp_normalize_mxz(grp, R));
cleanup:
#if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG)
ecp_drbg_free(&drbg_ctx);
#endif
mbedtls_ecp_point_free(&RP); mbedtls_mpi_free(&PX);
return ret;
}{ ... }
/* ... */#endif
/* ... */
int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
mbedtls_ecp_restart_ctx *rs_ctx)
{
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
char is_grp_capable = 0;
#endif
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(R != NULL);
ECP_VALIDATE_RET(m != NULL);
ECP_VALIDATE_RET(P != NULL);
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->depth++ == 0) {
rs_ctx->ops_done = 0;
}if (rs_ctx != NULL && rs_ctx->depth++ == 0) { ... }
/* ... */#else
(void) rs_ctx;
#endif
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) {
MBEDTLS_MPI_CHK(mbedtls_internal_ecp_init(grp));
}if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) { ... }
/* ... */#endif
int restarting = 0;
#if defined(MBEDTLS_ECP_RESTARTABLE)
restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL);
#endif
if (!restarting) {
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_CHK);
MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(grp, m));
MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
}if (!restarting) { ... }
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
MBEDTLS_MPI_CHK(ecp_mul_mxz(grp, R, m, P, f_rng, p_rng));
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
MBEDTLS_MPI_CHK(ecp_mul_comb(grp, R, m, P, f_rng, p_rng, rs_ctx));
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { ... }
/* ... */#endif
cleanup:
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
if (is_grp_capable) {
mbedtls_internal_ecp_free(grp);
}if (is_grp_capable) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL) {
rs_ctx->depth--;
}if (rs_ctx != NULL) { ... }
/* ... */#endif
return ret;
}{ ... }
/* ... */
int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(R != NULL);
ECP_VALIDATE_RET(m != NULL);
ECP_VALIDATE_RET(P != NULL);
return mbedtls_ecp_mul_restartable(grp, R, m, P, f_rng, p_rng, NULL);
}{ ... }
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
/* ... */
static int ecp_check_pubkey_sw(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi YY, RHS;
if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 ||
mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 ||
mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 ||
mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0) {
return MBEDTLS_ERR_ECP_INVALID_KEY;
}if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 || mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 || mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 || mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0) { ... }
mbedtls_mpi_init(&YY); mbedtls_mpi_init(&RHS);
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &YY, &pt->Y, &pt->Y));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &RHS, &pt->X, &pt->X));
if (grp->A.p == NULL) {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&RHS, &RHS, 3)); MOD_SUB(RHS);
}if (grp->A.p == NULL) { ... } else {
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &RHS, &RHS, &grp->A));
}else { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &RHS, &RHS, &pt->X));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &RHS, &RHS, &grp->B));
if (mbedtls_mpi_cmp_mpi(&YY, &RHS) != 0) {
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
}if (mbedtls_mpi_cmp_mpi(&YY, &RHS) != 0) { ... }
cleanup:
mbedtls_mpi_free(&YY); mbedtls_mpi_free(&RHS);
return ret;
}{ ... }
#endif/* ... */
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
/* ... */
static int mbedtls_ecp_mul_shortcuts(mbedtls_ecp_group *grp,
mbedtls_ecp_point *R,
const mbedtls_mpi *m,
const mbedtls_ecp_point *P,
mbedtls_ecp_restart_ctx *rs_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
if (mbedtls_mpi_cmp_int(m, 0) == 0) {
MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
MBEDTLS_MPI_CHK(mbedtls_ecp_set_zero(R));
}if (mbedtls_mpi_cmp_int(m, 0) == 0) { ... } else if (mbedtls_mpi_cmp_int(m, 1) == 0) {
MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
}else if (mbedtls_mpi_cmp_int(m, 1) == 0) { ... } else if (mbedtls_mpi_cmp_int(m, -1) == 0) {
MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
if (mbedtls_mpi_cmp_int(&R->Y, 0) != 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&R->Y, &grp->P, &R->Y));
}if (mbedtls_mpi_cmp_int(&R->Y, 0) != 0) { ... }
}else if (mbedtls_mpi_cmp_int(m, -1) == 0) { ... } else {
MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, R, m, P,
NULL, NULL, rs_ctx));
}else { ... }
cleanup:
return ret;
}{ ... }
/* ... */
int mbedtls_ecp_muladd_restartable(
mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
mbedtls_ecp_restart_ctx *rs_ctx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_ecp_point mP;
mbedtls_ecp_point *pmP = &mP;
mbedtls_ecp_point *pR = R;
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
char is_grp_capable = 0;
#endif
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(R != NULL);
ECP_VALIDATE_RET(m != NULL);
ECP_VALIDATE_RET(P != NULL);
ECP_VALIDATE_RET(n != NULL);
ECP_VALIDATE_RET(Q != NULL);
if (mbedtls_ecp_get_type(grp) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
}if (mbedtls_ecp_get_type(grp) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { ... }
mbedtls_ecp_point_init(&mP);
ECP_RS_ENTER(ma);
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->ma != NULL) {
pmP = &rs_ctx->ma->mP;
pR = &rs_ctx->ma->R;
if (rs_ctx->ma->state == ecp_rsma_mul2) {
goto mul2;
}if (rs_ctx->ma->state == ecp_rsma_mul2) { ... }
if (rs_ctx->ma->state == ecp_rsma_add) {
goto add;
}if (rs_ctx->ma->state == ecp_rsma_add) { ... }
if (rs_ctx->ma->state == ecp_rsma_norm) {
goto norm;
}if (rs_ctx->ma->state == ecp_rsma_norm) { ... }
}if (rs_ctx != NULL && rs_ctx->ma != NULL) { ... }
/* ... */#endif
MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pmP, m, P, rs_ctx));
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->ma != NULL) {
rs_ctx->ma->state = ecp_rsma_mul2;
}if (rs_ctx != NULL && rs_ctx->ma != NULL) { ... }
mul2:/* ... */
#endif
MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pR, n, Q, rs_ctx));
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) {
MBEDTLS_MPI_CHK(mbedtls_internal_ecp_init(grp));
}if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->ma != NULL) {
rs_ctx->ma->state = ecp_rsma_add;
}if (rs_ctx != NULL && rs_ctx->ma != NULL) { ... }
add:/* ... */
#endif
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_ADD);
MBEDTLS_MPI_CHK(ecp_add_mixed(grp, pR, pmP, pR));
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->ma != NULL) {
rs_ctx->ma->state = ecp_rsma_norm;
}if (rs_ctx != NULL && rs_ctx->ma != NULL) { ... }
norm:/* ... */
#endif
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);
MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, pR));
#if defined(MBEDTLS_ECP_RESTARTABLE)
if (rs_ctx != NULL && rs_ctx->ma != NULL) {
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, pR));
}if (rs_ctx != NULL && rs_ctx->ma != NULL) { ... }
/* ... */#endif
cleanup:
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
if (is_grp_capable) {
mbedtls_internal_ecp_free(grp);
}if (is_grp_capable) { ... }
/* ... */#endif
mbedtls_ecp_point_free(&mP);
ECP_RS_LEAVE(ma);
return ret;
}{ ... }
/* ... */
int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
const mbedtls_mpi *n, const mbedtls_ecp_point *Q)
{
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(R != NULL);
ECP_VALIDATE_RET(m != NULL);
ECP_VALIDATE_RET(P != NULL);
ECP_VALIDATE_RET(n != NULL);
ECP_VALIDATE_RET(Q != NULL);
return mbedtls_ecp_muladd_restartable(grp, R, m, P, n, Q, NULL);
}{ ... }
#endif/* ... */
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
#define ECP_MPI_INIT(s, n, p) { s, (n), (mbedtls_mpi_uint *) (p) }
#define ECP_MPI_INIT_ARRAY(x) \
ECP_MPI_INIT(1, sizeof(x) / sizeof(mbedtls_mpi_uint), x)...
/* ... */
static const mbedtls_mpi_uint x25519_bad_point_1[] = {
MBEDTLS_BYTES_TO_T_UINT_8(0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae),
MBEDTLS_BYTES_TO_T_UINT_8(0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a),
MBEDTLS_BYTES_TO_T_UINT_8(0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd),
MBEDTLS_BYTES_TO_T_UINT_8(0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00),
...};
static const mbedtls_mpi_uint x25519_bad_point_2[] = {
MBEDTLS_BYTES_TO_T_UINT_8(0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24),
MBEDTLS_BYTES_TO_T_UINT_8(0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b),
MBEDTLS_BYTES_TO_T_UINT_8(0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86),
MBEDTLS_BYTES_TO_T_UINT_8(0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57),
...};
static const mbedtls_mpi ecp_x25519_bad_point_1 = ECP_MPI_INIT_ARRAY(
x25519_bad_point_1);
static const mbedtls_mpi ecp_x25519_bad_point_2 = ECP_MPI_INIT_ARRAY(
x25519_bad_point_2);/* ... */
#endif
/* ... */
static int ecp_check_bad_points_mx(const mbedtls_mpi *X, const mbedtls_mpi *P,
const mbedtls_ecp_group_id grp_id)
{
int ret;
mbedtls_mpi XmP;
mbedtls_mpi_init(&XmP);
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&XmP, X));
while (mbedtls_mpi_cmp_mpi(&XmP, P) >= 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&XmP, &XmP, P));
}while (mbedtls_mpi_cmp_mpi(&XmP, P) >= 0) { ... }
/* ... */
if (mbedtls_mpi_cmp_int(&XmP, 1) <= 0) {
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
goto cleanup;
}if (mbedtls_mpi_cmp_int(&XmP, 1) <= 0) { ... }
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_1) == 0) {
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
goto cleanup;
}if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_1) == 0) { ... }
if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_2) == 0) {
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
goto cleanup;
}if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_2) == 0) { ... }
}if (grp_id == MBEDTLS_ECP_DP_CURVE25519) { ... }
/* ... */#else
(void) grp_id;
#endif
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&XmP, &XmP, 1));
if (mbedtls_mpi_cmp_mpi(&XmP, P) == 0) {
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
goto cleanup;
}if (mbedtls_mpi_cmp_mpi(&XmP, P) == 0) { ... }
ret = 0;
cleanup:
mbedtls_mpi_free(&XmP);
return ret;
}{ ... }
/* ... */
static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
{
/* ... */
if (mbedtls_mpi_size(&pt->X) > (grp->nbits + 7) / 8) {
return MBEDTLS_ERR_ECP_INVALID_KEY;
}if (mbedtls_mpi_size(&pt->X) > (grp->nbits + 7) / 8) { ... }
/* ... */
if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0) {
return MBEDTLS_ERR_ECP_INVALID_KEY;
}if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0) { ... }
return ecp_check_bad_points_mx(&pt->X, &grp->P, grp->id);
}{ ... }
#endif/* ... */
/* ... */
int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp,
const mbedtls_ecp_point *pt)
{
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(pt != NULL);
if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0) {
return MBEDTLS_ERR_ECP_INVALID_KEY;
}if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0) { ... }
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
return ecp_check_pubkey_mx(grp, pt);
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
return ecp_check_pubkey_sw(grp, pt);
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { ... }
/* ... */#endif
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}{ ... }
/* ... */
int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
const mbedtls_mpi *d)
{
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(d != NULL);
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
if (mbedtls_mpi_get_bit(d, 0) != 0 ||
mbedtls_mpi_get_bit(d, 1) != 0 ||
mbedtls_mpi_bitlen(d) - 1 != grp->nbits) {
return MBEDTLS_ERR_ECP_INVALID_KEY;
}if (mbedtls_mpi_get_bit(d, 0) != 0 || mbedtls_mpi_get_bit(d, 1) != 0 || mbedtls_mpi_bitlen(d) - 1 != grp->nbits) { ... }
if (grp->nbits == 254 && mbedtls_mpi_get_bit(d, 2) != 0) {
return MBEDTLS_ERR_ECP_INVALID_KEY;
}if (grp->nbits == 254 && mbedtls_mpi_get_bit(d, 2) != 0) { ... }
return 0;
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
if (mbedtls_mpi_cmp_int(d, 1) < 0 ||
mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) {
return MBEDTLS_ERR_ECP_INVALID_KEY;
}if (mbedtls_mpi_cmp_int(d, 1) < 0 || mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) { ... } else {
return 0;
}else { ... }
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { ... }
/* ... */#endif
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}{ ... }
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
MBEDTLS_STATIC_TESTABLE
int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
mbedtls_mpi *d,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
size_t n_random_bytes = high_bit / 8 + 1;
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(d, n_random_bytes,
f_rng, p_rng));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(d, 8 * n_random_bytes - high_bit - 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, high_bit, 1));
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 0, 0));
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 1, 0));
if (high_bit == 254) {
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 2, 0));
}if (high_bit == 254) { ... }
cleanup:
return ret;
}{ ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
static int mbedtls_ecp_gen_privkey_sw(
const mbedtls_mpi *N, mbedtls_mpi *d,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int ret = mbedtls_mpi_random(d, 1, N, f_rng, p_rng);
switch (ret) {
case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
return MBEDTLS_ERR_ECP_RANDOM_FAILED;case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
default:
return ret;default
}switch (ret) { ... }
}{ ... }
/* ... */#endif
/* ... */
int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
mbedtls_mpi *d,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(d != NULL);
ECP_VALIDATE_RET(f_rng != NULL);
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
return mbedtls_ecp_gen_privkey_mx(grp->nbits, d, f_rng, p_rng);
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { ... }
/* ... */#endif
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
return mbedtls_ecp_gen_privkey_sw(&grp->N, d, f_rng, p_rng);
}if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { ... }
/* ... */#endif
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}{ ... }
/* ... */
int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
const mbedtls_ecp_point *G,
mbedtls_mpi *d, mbedtls_ecp_point *Q,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(d != NULL);
ECP_VALIDATE_RET(G != NULL);
ECP_VALIDATE_RET(Q != NULL);
ECP_VALIDATE_RET(f_rng != NULL);
MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, d, f_rng, p_rng));
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, Q, d, G, f_rng, p_rng));
cleanup:
return ret;
}{ ... }
/* ... */
int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp,
mbedtls_mpi *d, mbedtls_ecp_point *Q,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
ECP_VALIDATE_RET(grp != NULL);
ECP_VALIDATE_RET(d != NULL);
ECP_VALIDATE_RET(Q != NULL);
ECP_VALIDATE_RET(f_rng != NULL);
return mbedtls_ecp_gen_keypair_base(grp, &grp->G, d, Q, f_rng, p_rng);
}{ ... }
/* ... */
int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
ECP_VALIDATE_RET(key != NULL);
ECP_VALIDATE_RET(f_rng != NULL);
if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
return ret;
}if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) { ... }
return mbedtls_ecp_gen_keypair(&key->grp, &key->d, &key->Q, f_rng, p_rng);
}{ ... }
#define ECP_CURVE25519_KEY_SIZE 32
/* ... */
int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
const unsigned char *buf, size_t buflen)
{
int ret = 0;
ECP_VALIDATE_RET(key != NULL);
ECP_VALIDATE_RET(buf != NULL);
if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
return ret;
}if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) { ... }
ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
/* ... */
if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
if (buflen != ECP_CURVE25519_KEY_SIZE) {
return MBEDTLS_ERR_ECP_INVALID_KEY;
}if (buflen != ECP_CURVE25519_KEY_SIZE) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen));
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0));
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0));
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 2, 0));
MBEDTLS_MPI_CHK(
mbedtls_mpi_set_bit(&key->d,
ECP_CURVE25519_KEY_SIZE * 8 - 1, 0)
);
MBEDTLS_MPI_CHK(
mbedtls_mpi_set_bit(&key->d,
ECP_CURVE25519_KEY_SIZE * 8 - 2, 1)
);
}if (grp_id == MBEDTLS_ECP_DP_CURVE25519) { ... } else {
ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
}else { ... }
}if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { ... }
/* ... */
#endif
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&key->d, buf, buflen));
MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(&key->grp, &key->d));
}if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { ... }
/* ... */
#endif
cleanup:
if (ret != 0) {
mbedtls_mpi_free(&key->d);
}if (ret != 0) { ... }
return ret;
}{ ... }
/* ... */
int mbedtls_ecp_write_key(mbedtls_ecp_keypair *key,
unsigned char *buf, size_t buflen)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
ECP_VALIDATE_RET(key != NULL);
ECP_VALIDATE_RET(buflen == 0 || buf != NULL);
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
if (key->grp.id == MBEDTLS_ECP_DP_CURVE25519) {
if (buflen < ECP_CURVE25519_KEY_SIZE) {
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
}if (buflen < ECP_CURVE25519_KEY_SIZE) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&key->d, buf, buflen));
}if (key->grp.id == MBEDTLS_ECP_DP_CURVE25519) { ... } else {
ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
}else { ... }
}if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { ... }
/* ... */
#endif
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&key->d, buf, buflen));
}if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { ... }
/* ... */
#endif
cleanup:
return ret;
}{ ... }
/* ... */
int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_ecp_point Q;
mbedtls_ecp_group grp;
ECP_VALIDATE_RET(pub != NULL);
ECP_VALIDATE_RET(prv != NULL);
if (pub->grp.id == MBEDTLS_ECP_DP_NONE ||
pub->grp.id != prv->grp.id ||
mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) ||
mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) ||
mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}if (pub->grp.id == MBEDTLS_ECP_DP_NONE || pub->grp.id != prv->grp.id || mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) || mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) || mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) { ... }
mbedtls_ecp_point_init(&Q);
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_group_copy(&grp, &prv->grp);
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &Q, &prv->d, &prv->grp.G, NULL, NULL));
if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) ||
mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) ||
mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) {
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
goto cleanup;
}if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) || mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) || mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) { ... }
cleanup:
mbedtls_ecp_point_free(&Q);
mbedtls_ecp_group_free(&grp);
return ret;
}{ ... }
#if defined(MBEDTLS_SELF_TEST)
/* ... */
static int self_test_adjust_exponent(const mbedtls_ecp_group *grp,
mbedtls_mpi *m)
{
int ret = 0;
switch (grp->id) {
/* ... */
#if !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
case MBEDTLS_ECP_DP_CURVE448:
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, 254, 0));
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, grp->nbits, 1));
/* ... */
MBEDTLS_MPI_CHK(
mbedtls_mpi_set_bit(m, grp->nbits - 1,
mbedtls_mpi_get_bit(m, 253)));
break;/* ... */
#endif/* ... */
#endif
default:
(void) grp;
(void) m;
goto cleanup;default
}switch (grp->id) { ... }
cleanup:
return ret;
}self_test_adjust_exponent (const mbedtls_ecp_group *grp, mbedtls_mpi *m) { ... }
/* ... */
static int self_test_point(int verbose,
mbedtls_ecp_group *grp,
mbedtls_ecp_point *R,
mbedtls_mpi *m,
const mbedtls_ecp_point *P,
const char *const *exponents,
size_t n_exponents)
{
int ret = 0;
size_t i = 0;
unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
add_count = 0;
dbl_count = 0;
mul_count = 0;
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[0]));
MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, NULL, NULL));
for (i = 1; i < n_exponents; i++) {
add_c_prev = add_count;
dbl_c_prev = dbl_count;
mul_c_prev = mul_count;
add_count = 0;
dbl_count = 0;
mul_count = 0;
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[i]));
MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, NULL, NULL));
if (add_count != add_c_prev ||
dbl_count != dbl_c_prev ||
mul_count != mul_c_prev) {
ret = 1;
break;
}if (add_count != add_c_prev || dbl_count != dbl_c_prev || mul_count != mul_c_prev) { ... }
}for (i = 1; i < n_exponents; i++) { ... }
cleanup:
if (verbose != 0) {
if (ret != 0) {
mbedtls_printf("failed (%u)\n", (unsigned int) i);
}if (ret != 0) { ... } else {
mbedtls_printf("passed\n");
}else { ... }
}if (verbose != 0) { ... }
return ret;
}self_test_point (int verbose, mbedtls_ecp_group *grp, mbedtls_ecp_point *R, mbedtls_mpi *m, const mbedtls_ecp_point *P, const char *const *exponents, size_t n_exponents) { ... }
/* ... */
int mbedtls_ecp_self_test(int verbose)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_ecp_group grp;
mbedtls_ecp_point R, P;
mbedtls_mpi m;
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
/* ... */
const char *sw_exponents[] =
{
"000000000000000000000000000000000000000000000001",
"FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8C",
"5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25",
"400000000000000000000000000000000000000000000000",
"7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
"555555555555555555555555555555555555555555555555",
...};/* ... */
#endif
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
const char *m_exponents[] =
{
/* ... */
"4000000000000000000000000000000000000000000000000000000000000000",
"5C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C30",
"5715ECCE24583F7A7023C24164390586842E816D7280A49EF6DF4EAE6B280BF8",
"41A2B017516F6D254E1F002BCCBADD54BE30F8CEC737A0E912B4963B6BA74460",
"5555555555555555555555555555555555555555555555555555555555555550",
"7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8",
...};/* ... */
#endif
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_point_init(&R);
mbedtls_ecp_point_init(&P);
mbedtls_mpi_init(&m);
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP192R1));
#else
MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, mbedtls_ecp_curve_list()->grp_id));
#endif
if (verbose != 0) {
mbedtls_printf(" ECP SW test #1 (constant op_count, base point G): ");
}if (verbose != 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&m, 2));
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &P, &m, &grp.G, NULL, NULL));
ret = self_test_point(verbose,
&grp, &R, &m, &grp.G,
sw_exponents,
sizeof(sw_exponents) / sizeof(sw_exponents[0]));
if (ret != 0) {
goto cleanup;
}if (ret != 0) { ... }
if (verbose != 0) {
mbedtls_printf(" ECP SW test #2 (constant op_count, other point): ");
}if (verbose != 0) { ... }
ret = self_test_point(verbose,
&grp, &R, &m, &P,
sw_exponents,
sizeof(sw_exponents) / sizeof(sw_exponents[0]));
if (ret != 0) {
goto cleanup;
}if (ret != 0) { ... }
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&R);/* ... */
#endif
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
if (verbose != 0) {
mbedtls_printf(" ECP Montgomery test (constant op_count): ");
}if (verbose != 0) { ... }
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE25519));
#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE448));
#else
#error "MBEDTLS_ECP_MONTGOMERY_ENABLED is defined, but no curve is supported for self-test"
#endif
ret = self_test_point(verbose,
&grp, &R, &m, &grp.G,
m_exponents,
sizeof(m_exponents) / sizeof(m_exponents[0]));
if (ret != 0) {
goto cleanup;
}if (ret != 0) { ... }
/* ... */#endif
cleanup:
if (ret < 0 && verbose != 0) {
mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
}if (ret < 0 && verbose != 0) { ... }
mbedtls_ecp_group_free(&grp);
mbedtls_ecp_point_free(&R);
mbedtls_ecp_point_free(&P);
mbedtls_mpi_free(&m);
if (verbose != 0) {
mbedtls_printf("\n");
}if (verbose != 0) { ... }
return ret;
}mbedtls_ecp_self_test (int verbose) { ... }
/* ... */
#endif
/* ... */
#endif
/* ... */
#endif