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
44
45
46
47
48
49
50
51
55
56
57
58
59
63
64
67
75
76
79
80
81
84
85
89
90
91
92
93
94
95
98
99
100
101
102
103
106
107
108
111
112
117
118
119
120
121
122
123
124
125
129
130
131
132
133
134
135
138
139
140
143
144
145
150
151
152
155
156
159
160
165
166
167
168
169
170
171
172
173
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
207
208
215
216
221
222
223
224
225
230
231
232
233
234
235
236
237
238
241
242
243
244
245
246
247
248
249
250
251
252
263
264
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
286
287
288
289
290
293
294
295
296
297
298
300
301
304
305
306
307
308
309
310
311
314
315
322
323
324
325
326
327
328
329
330
331
334
335
336
337
338
339
346
347
348
349
350
353
354
355
356
357
358
365
366
367
368
369
372
373
374
375
376
379
380
385
386
387
388
389
390
391
394
398
399
402
403
404
405
406
409
412
415
416
419
420
421
422
423
426
427
428
429
430
431
432
433
434
435
436
439
440
441
442
446
447
451
452
453
454
455
458
459
460
461
462
463
464
468
477
478
481
482
483
484
485
486
487
488
489
492
493
494
495
496
497
498
499
500
501
504
505
506
507
510
515
516
517
518
519
520
521
522
523
524
525
526
527
530
531
532
533
534
535
536
537
538
539
540
541
544
545
546
552
557
558
560
561
563
564
568
569
570
571
572
576
577
578
579
580
581
582
583
584
585
588
589
590
591
592
593
594
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
627
628
629
630
631
632
635
636
637
640
641
642
645
646
649
652
653
654
659
660
661
662
663
666
667
668
669
670
674
675
676
677
680
681
682
683
684
685
688
689
690
691
692
693
694
702
703
704
705
706
707
708
709
710
712
713
714
715
716
717
718
719
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
743
744
745
746
750
751
752
753
761
762
763
764
766
767
768
769
770
771
772
773
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
792
793
794
795
796
797
798
801
802
803
804
805
806
807
808
809
810
811
814
815
816
817
822
823
824
825
828
829
830
831
832
833
834
835
836
837
838
839
840
841
843
849
850
851
852
857
858
859
860
863
864
865
866
867
868
869
870
883
884
887
888
892
893
894
895
896
899
900
901
902
903
904
905
906
907
908
909
910
911
912
932
933
936
937
938
939
940
943
944
945
946
947
948
949
950
951
952
953
954
955
958
959
960
961
964
973
974
977
985
986
987
988
989
990
991
994
995
996
997
998
999
1000
1001
1002
1003
1006
1007
1010
1019
1020
1023
1031
1032
1033
1034
1035
1038
1039
1040
1041
1042
1043
1044
1049
1050
1055
1056
1059
1060
1063
1066
1067
1075
1076
1077
1078
1079
1082
1083
1084
1085
1086
1087
1088
1093
1094
1099
1100
1103
1104
1107
1110
1111
1114
1117
1118
1126
1127
1128
1129
1130
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1162
1163
1166
1167
1170
1171
1172
1177
1178
1180
1183
1184
1185
1186
1187
1188
1191
1196
1197
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1240
1241
1242
1243
1244
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1261
1266
1267
1268
1269
1272
1275
1278
1279
1280
1281
1282
1285
1287
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1331
1332
1333
1334
1335
1336
1337
1340
1344
1345
1348
1352
1353
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1463
1464
1465
1468
1469
1470
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1488
1491
1492
1497
1500
1501
1506
1509
1510
1511
1512
1513
1516
1517
1521
1526
1527
1528
1529
1530
1531
1532
1533
1534
1537
1538
1539
1540
1541
1542
1543
1544
1547
1548
1550
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1576
1577
1578
1579
1580
1581
1582
1583
1584
1589
1590
1591
1594
1601
1602
1603
1604
1605
1606
1609
1610
1613
1614
1615
1616
1617
1621
1622
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1641
1642
1643
1644
1652
1653
1654
1655
1656
1657
1665
1666
1669
1670
1671
1672
1673
1674
1675
1676
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1732
1733
1734
1735
1736
1737
1741
1742
1743
1744
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1775
1776
1777
1781
1782
1783
1784
1785
1786
1787
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1823
1824
1825
1826
1827
1828
1829
1830
1833
1834
1835
1836
1839
1840
1843
1844
1845
1846
1847
1848
1849
1852
1853
1854
1855
1856
1857
1858
1859
1862
1863
1866
1867
1870
1874
1875
1879
1880
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1899
1902
1903
1904
1905
1906
1907
1908
1911
1912
1913
1914
1915
1916
1917
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1955
1956
1958
1959
1961
1962
1963
1964
1970
1971
1972
1973
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2058
2059
2062
2063
2067
2068
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2087
2088
2089
2090
2091
2092
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
2124
2130
2131
2132
2133
2134
2137
2138
2143
2144
2147
2156
2157
2160
2169
2170
2172
2173
2174
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2200
2201
2204
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2235
2238
2239
2247
2248
2251
2252
2253
2254
2255
2256
2257
2260
2265
2266
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2282
2283
2284
2285
2286
2287
2288
2292
2293
2294
2297
2298
2299
2303
2304
2307
2308
2309
2310
2311
2313
2316
2317
2318
2319
2320
2321
2322
2323
2326
2327
2328
2329
2330
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2355
2359
2360
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
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
2483
2484
2485
2486
2487
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2513
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2574
2575
2576
2577
2578
2579
2580
2581
2582
2585
2586
2587
2588
2589
2590
2591
2592
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2615
2616
2617
2618
2619
2620
2621
2622
2623
2627
2628
2629
2630
2631
2632
2641
2642
2643
2646
2647
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2707
2708
2709
2712
2713
2714
2715
2718
2719
2720
2721
2722
2723
2724
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2746
2747
2748
2749
2750
2751
2752
2755
2756
2757
2758
2759
2760
2761
2764
2765
2769
2770
2771
2772
2773
2776
2777
2778
2782
2783
2784
2785
2788
2789
2790
2791
2794
2795
2796
2797
2798
2801
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2836
2837
2840
2841
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2887
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2902
2903
2904
2905
2906
2907
2915
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2931
2932
2933
2936
2937
2938
2945
2950
2951
2952
2953
2954
2959
2960
2961
2962
2963
2964
2965
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2981
2982
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3012
3013
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3055
3056
3064
3065
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3082
3083
3084
3085
3088
3089
3090
3091
3092
3093
3096
3097
3098
3099
3100
3101
3102
3103
3104
3107
3108
3116
3117
3120
3121
3122
3123
3124
3125
3126
3127
3128
3131
3132
3140
3141
3144
3145
3148
3149
3150
3151
3152
3153
3154
3155
3163
3164
3165
3168
3169
3170
3171
3174
3175
3176
3177
3178
3181
3182
3183
3184
3185
3186
3187
/* ... */
/* ... */
#include "common.h"
#if defined(MBEDTLS_BIGNUM_C)
#include "mbedtls/bignum.h"
#include "mbedtls/bn_mul.h"
#include "mbedtls/platform_util.h"
#include "mbedtls/error.h"
#include "constant_time_internal.h"
#include "bignum_internal.h"
#include <limits.h>
#include <string.h>
#include "mbedtls/platform.h"
9 includes
#define MPI_VALIDATE_RET(cond) \
MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA)...
#define MPI_VALIDATE(cond) \
MBEDTLS_INTERNAL_VALIDATE(cond)...
#define ciL (sizeof(mbedtls_mpi_uint))
#define biL (ciL << 3)
#define biH (ciL << 2)
#define MPI_SIZE_T_MAX ((size_t) -1)
/* ... */
#define BITS_TO_LIMBS(i) ((i) / biL + ((i) % biL != 0))
#define CHARS_TO_LIMBS(i) ((i) / ciL + ((i) % ciL != 0))
8 defines
static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n)
{
mbedtls_platform_zeroize(v, ciL * n);
}{ ... }
/* ... */
void mbedtls_mpi_init(mbedtls_mpi *X)
{
MPI_VALIDATE(X != NULL);
X->s = 1;
X->n = 0;
X->p = NULL;
}{ ... }
/* ... */
void mbedtls_mpi_free(mbedtls_mpi *X)
{
if (X == NULL) {
return;
}if (X == NULL) { ... }
if (X->p != NULL) {
mbedtls_mpi_zeroize(X->p, X->n);
mbedtls_free(X->p);
}if (X->p != NULL) { ... }
X->s = 1;
X->n = 0;
X->p = NULL;
}{ ... }
/* ... */
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
{
mbedtls_mpi_uint *p;
MPI_VALIDATE_RET(X != NULL);
if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
return MBEDTLS_ERR_MPI_ALLOC_FAILED;
}if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) { ... }
if (X->n < nblimbs) {
if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) {
return MBEDTLS_ERR_MPI_ALLOC_FAILED;
}if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(nblimbs, ciL)) == NULL) { ... }
if (X->p != NULL) {
memcpy(p, X->p, X->n * ciL);
mbedtls_mpi_zeroize(X->p, X->n);
mbedtls_free(X->p);
}if (X->p != NULL) { ... }
X->n = nblimbs;
X->p = p;
}if (X->n < nblimbs) { ... }
return 0;
}{ ... }
/* ... */
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
{
mbedtls_mpi_uint *p;
size_t i;
MPI_VALIDATE_RET(X != NULL);
if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) {
return MBEDTLS_ERR_MPI_ALLOC_FAILED;
}if (nblimbs > MBEDTLS_MPI_MAX_LIMBS) { ... }
if (X->n <= nblimbs) {
return mbedtls_mpi_grow(X, nblimbs);
}if (X->n <= nblimbs) { ... }
for (i = X->n - 1; i > 0; i--) {
if (X->p[i] != 0) {
break;
}if (X->p[i] != 0) { ... }
}for (i = X->n - 1; i > 0; i--) { ... }
i++;
if (i < nblimbs) {
i = nblimbs;
}if (i < nblimbs) { ... }
if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) {
return MBEDTLS_ERR_MPI_ALLOC_FAILED;
}if ((p = (mbedtls_mpi_uint *) mbedtls_calloc(i, ciL)) == NULL) { ... }
if (X->p != NULL) {
memcpy(p, X->p, i * ciL);
mbedtls_mpi_zeroize(X->p, X->n);
mbedtls_free(X->p);
}if (X->p != NULL) { ... }
X->n = i;
X->p = p;
return 0;
}{ ... }
static int mbedtls_mpi_resize_clear(mbedtls_mpi *X, size_t limbs)
{
if (limbs == 0) {
mbedtls_mpi_free(X);
return 0;
}if (limbs == 0) { ... } else if (X->n == limbs) {
memset(X->p, 0, limbs * ciL);
X->s = 1;
return 0;
}else if (X->n == limbs) { ... } else {
mbedtls_mpi_free(X);
return mbedtls_mpi_grow(X, limbs);
}else { ... }
}{ ... }
/* ... */
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
{
int ret = 0;
size_t i;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(Y != NULL);
if (X == Y) {
return 0;
}if (X == Y) { ... }
if (Y->n == 0) {
if (X->n != 0) {
X->s = 1;
memset(X->p, 0, X->n * ciL);
}if (X->n != 0) { ... }
return 0;
}if (Y->n == 0) { ... }
for (i = Y->n - 1; i > 0; i--) {
if (Y->p[i] != 0) {
break;
}if (Y->p[i] != 0) { ... }
}for (i = Y->n - 1; i > 0; i--) { ... }
i++;
X->s = Y->s;
if (X->n < i) {
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i));
}if (X->n < i) { ... } else {
memset(X->p + i, 0, (X->n - i) * ciL);
}else { ... }
memcpy(X->p, Y->p, i * ciL);
cleanup:
return ret;
}{ ... }
/* ... */
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
{
mbedtls_mpi T;
MPI_VALIDATE(X != NULL);
MPI_VALIDATE(Y != NULL);
memcpy(&T, X, sizeof(mbedtls_mpi));
memcpy(X, Y, sizeof(mbedtls_mpi));
memcpy(Y, &T, sizeof(mbedtls_mpi));
}{ ... }
static inline mbedtls_mpi_uint mpi_sint_abs(mbedtls_mpi_sint z)
{
if (z >= 0) {
return z;
}if (z >= 0) { ... }
/* ... */
return (mbedtls_mpi_uint) 0 - (mbedtls_mpi_uint) z;
}{ ... }
/* ... */
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
MPI_VALIDATE_RET(X != NULL);
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
memset(X->p, 0, X->n * ciL);
X->p[0] = mpi_sint_abs(z);
X->s = (z < 0) ? -1 : 1;
cleanup:
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
{
MPI_VALIDATE_RET(X != NULL);
if (X->n * biL <= pos) {
return 0;
}if (X->n * biL <= pos) { ... }
return (X->p[pos / biL] >> (pos % biL)) & 0x01;
}{ ... }
#define GET_BYTE(X, i) \
(((X)->p[(i) / ciL] >> (((i) % ciL) * 8)) & 0xff)...
/* ... */
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
{
int ret = 0;
size_t off = pos / biL;
size_t idx = pos % biL;
MPI_VALIDATE_RET(X != NULL);
if (val != 0 && val != 1) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (val != 0 && val != 1) { ... }
if (X->n * biL <= pos) {
if (val == 0) {
return 0;
}if (val == 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, off + 1));
}if (X->n * biL <= pos) { ... }
X->p[off] &= ~((mbedtls_mpi_uint) 0x01 << idx);
X->p[off] |= (mbedtls_mpi_uint) val << idx;
cleanup:
return ret;
}{ ... }
/* ... */
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
{
size_t i, j, count = 0;
MBEDTLS_INTERNAL_VALIDATE_RET(X != NULL, 0);
for (i = 0; i < X->n; i++) {
for (j = 0; j < biL; j++, count++) {
if (((X->p[i] >> j) & 1) != 0) {
return count;
}if (((X->p[i] >> j) & 1) != 0) { ... }
}for (j = 0; j < biL; j++, count++) { ... }
}for (i = 0; i < X->n; i++) { ... }
return 0;
}{ ... }
/* ... */
static size_t mbedtls_clz(const mbedtls_mpi_uint x)
{
size_t j;
mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
for (j = 0; j < biL; j++) {
if (x & mask) {
break;
}if (x & mask) { ... }
mask >>= 1;
}for (j = 0; j < biL; j++) { ... }
return j;
}{ ... }
/* ... */
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
{
size_t i, j;
if (X->n == 0) {
return 0;
}if (X->n == 0) { ... }
for (i = X->n - 1; i > 0; i--) {
if (X->p[i] != 0) {
break;
}if (X->p[i] != 0) { ... }
}for (i = X->n - 1; i > 0; i--) { ... }
j = biL - mbedtls_clz(X->p[i]);
return (i * biL) + j;
}{ ... }
/* ... */
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
{
return (mbedtls_mpi_bitlen(X) + 7) >> 3;
}{ ... }
/* ... */
static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
{
*d = 255;
if (c >= 0x30 && c <= 0x39) {
*d = c - 0x30;
}if (c >= 0x30 && c <= 0x39) { ... }
if (c >= 0x41 && c <= 0x46) {
*d = c - 0x37;
}if (c >= 0x41 && c <= 0x46) { ... }
if (c >= 0x61 && c <= 0x66) {
*d = c - 0x57;
}if (c >= 0x61 && c <= 0x66) { ... }
if (*d >= (mbedtls_mpi_uint) radix) {
return MBEDTLS_ERR_MPI_INVALID_CHARACTER;
}if (*d >= (mbedtls_mpi_uint) radix) { ... }
return 0;
}{ ... }
/* ... */
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t i, j, slen, n;
int sign = 1;
mbedtls_mpi_uint d;
mbedtls_mpi T;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(s != NULL);
if (radix < 2 || radix > 16) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (radix < 2 || radix > 16) { ... }
mbedtls_mpi_init(&T);
if (s[0] == 0) {
mbedtls_mpi_free(X);
return 0;
}if (s[0] == 0) { ... }
if (s[0] == '-') {
++s;
sign = -1;
}if (s[0] == '-') { ... }
slen = strlen(s);
if (radix == 16) {
if (slen > MPI_SIZE_T_MAX >> 2) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (slen > MPI_SIZE_T_MAX >> 2) { ... }
n = BITS_TO_LIMBS(slen << 2);
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
for (i = slen, j = 0; i > 0; i--, j++) {
MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i - 1]));
X->p[j / (2 * ciL)] |= d << ((j % (2 * ciL)) << 2);
}for (i = slen, j = 0; i > 0; i--, j++) { ... }
}if (radix == 16) { ... } else {
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
for (i = 0; i < slen; i++) {
MBEDTLS_MPI_CHK(mpi_get_digit(&d, radix, s[i]));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T, X, radix));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, &T, d));
}for (i = 0; i < slen; i++) { ... }
}else { ... }
if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) {
X->s = -1;
}if (sign < 0 && mbedtls_mpi_bitlen(X) != 0) { ... }
cleanup:
mbedtls_mpi_free(&T);
return ret;
}{ ... }
/* ... */
static int mpi_write_hlp(mbedtls_mpi *X, int radix,
char **p, const size_t buflen)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi_uint r;
size_t length = 0;
char *p_end = *p + buflen;
do {
if (length >= buflen) {
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
}if (length >= buflen) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, radix));
MBEDTLS_MPI_CHK(mbedtls_mpi_div_int(X, NULL, X, radix));
/* ... */
if (r < 0xA) {
*(--p_end) = (char) ('0' + r);
}if (r < 0xA) { ... } else {
*(--p_end) = (char) ('A' + (r - 0xA));
}else { ... }
length++;
...} while (mbedtls_mpi_cmp_int(X, 0) != 0);
memmove(*p, p_end, length);
*p += length;
cleanup:
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
char *buf, size_t buflen, size_t *olen)
{
int ret = 0;
size_t n;
char *p;
mbedtls_mpi T;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(olen != NULL);
MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
if (radix < 2 || radix > 16) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (radix < 2 || radix > 16) { ... }
n = mbedtls_mpi_bitlen(X);
if (radix >= 4) {
n >>= 1;
/* ... */
}if (radix >= 4) { ... }
if (radix >= 16) {
n >>= 1;
/* ... */
}if (radix >= 16) { ... }
n += 1;
n += 1;
/* ... */
n += 1;
n += (n & 1);
/* ... */
if (buflen < n) {
*olen = n;
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
}if (buflen < n) { ... }
p = buf;
mbedtls_mpi_init(&T);
if (X->s == -1) {
*p++ = '-';
buflen--;
}if (X->s == -1) { ... }
if (radix == 16) {
int c;
size_t i, j, k;
for (i = X->n, k = 0; i > 0; i--) {
for (j = ciL; j > 0; j--) {
c = (X->p[i - 1] >> ((j - 1) << 3)) & 0xFF;
if (c == 0 && k == 0 && (i + j) != 2) {
continue;
}if (c == 0 && k == 0 && (i + j) != 2) { ... }
*(p++) = "0123456789ABCDEF" [c / 16];
*(p++) = "0123456789ABCDEF" [c % 16];
k = 1;
}for (j = ciL; j > 0; j--) { ... }
}for (i = X->n, k = 0; i > 0; i--) { ... }
}if (radix == 16) { ... } else {
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T, X));
if (T.s == -1) {
T.s = 1;
}if (T.s == -1) { ... }
MBEDTLS_MPI_CHK(mpi_write_hlp(&T, radix, &p, buflen));
}else { ... }
*p++ = '\0';
*olen = p - buf;
cleanup:
mbedtls_mpi_free(&T);
return ret;
}{ ... }
#if defined(MBEDTLS_FS_IO)
/* ... */
int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
{
mbedtls_mpi_uint d;
size_t slen;
char *p;
/* ... */
char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(fin != NULL);
if (radix < 2 || radix > 16) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (radix < 2 || radix > 16) { ... }
memset(s, 0, sizeof(s));
if (fgets(s, sizeof(s) - 1, fin) == NULL) {
return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
}if (fgets(s, sizeof(s) - 1, fin) == NULL) { ... }
slen = strlen(s);
if (slen == sizeof(s) - 2) {
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
}if (slen == sizeof(s) - 2) { ... }
if (slen > 0 && s[slen - 1] == '\n') {
slen--; s[slen] = '\0';
}if (slen > 0 && s[slen - 1] == '\n') { ... }
if (slen > 0 && s[slen - 1] == '\r') {
slen--; s[slen] = '\0';
}if (slen > 0 && s[slen - 1] == '\r') { ... }
p = s + slen;
while (p-- > s) {
if (mpi_get_digit(&d, radix, *p) != 0) {
break;
}if (mpi_get_digit(&d, radix, *p) != 0) { ... }
}while (p-- > s) { ... }
return mbedtls_mpi_read_string(X, radix, p + 1);
}mbedtls_mpi_read_file (mbedtls_mpi *X, int radix, FILE *fin) { ... }
/* ... */
int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t n, slen, plen;
/* ... */
char s[MBEDTLS_MPI_RW_BUFFER_SIZE];
MPI_VALIDATE_RET(X != NULL);
if (radix < 2 || radix > 16) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (radix < 2 || radix > 16) { ... }
memset(s, 0, sizeof(s));
MBEDTLS_MPI_CHK(mbedtls_mpi_write_string(X, radix, s, sizeof(s) - 2, &n));
if (p == NULL) {
p = "";
}if (p == NULL) { ... }
plen = strlen(p);
slen = strlen(s);
s[slen++] = '\r';
s[slen++] = '\n';
if (fout != NULL) {
if (fwrite(p, 1, plen, fout) != plen ||
fwrite(s, 1, slen, fout) != slen) {
return MBEDTLS_ERR_MPI_FILE_IO_ERROR;
}if (fwrite(p, 1, plen, fout) != plen || fwrite(s, 1, slen, fout) != slen) { ... }
}if (fout != NULL) { ... } else {
mbedtls_printf("%s%s", p, s);
}else { ... }
cleanup:
return ret;
}mbedtls_mpi_write_file (const char *p, const mbedtls_mpi *X, int radix, FILE *fout) { ... }
/* ... */#endif
/* ... */
static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c(mbedtls_mpi_uint x)
{
uint8_t i;
unsigned char *x_ptr;
mbedtls_mpi_uint tmp = 0;
for (i = 0, x_ptr = (unsigned char *) &x; i < ciL; i++, x_ptr++) {
tmp <<= CHAR_BIT;
tmp |= (mbedtls_mpi_uint) *x_ptr;
}for (i = 0, x_ptr = (unsigned char *) &x; i < ciL; i++, x_ptr++) { ... }
return tmp;
}{ ... }
static mbedtls_mpi_uint mpi_uint_bigendian_to_host(mbedtls_mpi_uint x)
{
#if defined(__BYTE_ORDER__)
#if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
return x;
#endif
#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
#if defined(__GNUC__) && defined(__GNUC_PREREQ)
#if __GNUC_PREREQ(4, 3)
#define have_bswap
#endif/* ... */
#endif
#if defined(__clang__) && defined(__has_builtin)
#if __has_builtin(__builtin_bswap32) && \
__has_builtin(__builtin_bswap64)
#define have_bswap
#endif/* ... */
#endif
#if defined(have_bswap)
switch (sizeof(mbedtls_mpi_uint)) {
case 4:
return __builtin_bswap32(x);case 4:
case 8:
return __builtin_bswap64(x);case 8:
}switch (sizeof(mbedtls_mpi_uint)) { ... }
/* ... */#endif/* ... */
#endif /* ... */
#endif
/* ... */
return mpi_uint_bigendian_to_host_c(x);
}{ ... }
static void mpi_bigendian_to_host(mbedtls_mpi_uint * const p, size_t limbs)
{
mbedtls_mpi_uint *cur_limb_left;
mbedtls_mpi_uint *cur_limb_right;
if (limbs == 0) {
return;
}if (limbs == 0) { ... }
/* ... */
for (cur_limb_left = p, cur_limb_right = p + (limbs - 1);
cur_limb_left <= cur_limb_right;
cur_limb_left++, cur_limb_right--) {
mbedtls_mpi_uint tmp;
/* ... */
tmp = mpi_uint_bigendian_to_host(*cur_limb_left);
*cur_limb_left = mpi_uint_bigendian_to_host(*cur_limb_right);
*cur_limb_right = tmp;
}for (cur_limb_left = p, cur_limb_right = p + (limbs - 1); cur_limb_left <= cur_limb_right; cur_limb_left++, cur_limb_right--) { ... }
}{ ... }
/* ... */
int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
const unsigned char *buf, size_t buflen)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t i;
size_t const limbs = CHARS_TO_LIMBS(buflen);
MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
for (i = 0; i < buflen; i++) {
X->p[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
}for (i = 0; i < buflen; i++) { ... }
cleanup:
/* ... */
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t const limbs = CHARS_TO_LIMBS(buflen);
size_t const overhead = (limbs * ciL) - buflen;
unsigned char *Xp;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
/* ... */
if (buflen != 0) {
Xp = (unsigned char *) X->p;
memcpy(Xp + overhead, buf, buflen);
mpi_bigendian_to_host(X->p, limbs);
}if (buflen != 0) { ... }
cleanup:
/* ... */
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
unsigned char *buf, size_t buflen)
{
size_t stored_bytes = X->n * ciL;
size_t bytes_to_copy;
size_t i;
if (stored_bytes < buflen) {
bytes_to_copy = stored_bytes;
}if (stored_bytes < buflen) { ... } else {
bytes_to_copy = buflen;
/* ... */
for (i = bytes_to_copy; i < stored_bytes; i++) {
if (GET_BYTE(X, i) != 0) {
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
}if (GET_BYTE(X, i) != 0) { ... }
}for (i = bytes_to_copy; i < stored_bytes; i++) { ... }
}else { ... }
for (i = 0; i < bytes_to_copy; i++) {
buf[i] = GET_BYTE(X, i);
}for (i = 0; i < bytes_to_copy; i++) { ... }
if (stored_bytes < buflen) {
memset(buf + stored_bytes, 0, buflen - stored_bytes);
}if (stored_bytes < buflen) { ... }
return 0;
}{ ... }
/* ... */
int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
unsigned char *buf, size_t buflen)
{
size_t stored_bytes;
size_t bytes_to_copy;
unsigned char *p;
size_t i;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(buflen == 0 || buf != NULL);
stored_bytes = X->n * ciL;
if (stored_bytes < buflen) {
/* ... */
bytes_to_copy = stored_bytes;
p = buf + buflen - stored_bytes;
memset(buf, 0, buflen - stored_bytes);
}if (stored_bytes < buflen) { ... } else {
/* ... */
bytes_to_copy = buflen;
p = buf;
for (i = bytes_to_copy; i < stored_bytes; i++) {
if (GET_BYTE(X, i) != 0) {
return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
}if (GET_BYTE(X, i) != 0) { ... }
}for (i = bytes_to_copy; i < stored_bytes; i++) { ... }
}else { ... }
for (i = 0; i < bytes_to_copy; i++) {
p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
}for (i = 0; i < bytes_to_copy; i++) { ... }
return 0;
}{ ... }
/* ... */
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t i, v0, t1;
mbedtls_mpi_uint r0 = 0, r1;
MPI_VALIDATE_RET(X != NULL);
v0 = count / (biL);
t1 = count & (biL - 1);
i = mbedtls_mpi_bitlen(X) + count;
if (X->n * biL < i) {
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, BITS_TO_LIMBS(i)));
}if (X->n * biL < i) { ... }
ret = 0;
/* ... */
if (v0 > 0) {
for (i = X->n; i > v0; i--) {
X->p[i - 1] = X->p[i - v0 - 1];
}for (i = X->n; i > v0; i--) { ... }
for (; i > 0; i--) {
X->p[i - 1] = 0;
}for (; i > 0; i--) { ... }
}if (v0 > 0) { ... }
/* ... */
if (t1 > 0) {
for (i = v0; i < X->n; i++) {
r1 = X->p[i] >> (biL - t1);
X->p[i] <<= t1;
X->p[i] |= r0;
r0 = r1;
}for (i = v0; i < X->n; i++) { ... }
}if (t1 > 0) { ... }
cleanup:
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
{
size_t i, v0, v1;
mbedtls_mpi_uint r0 = 0, r1;
MPI_VALIDATE_RET(X != NULL);
v0 = count / biL;
v1 = count & (biL - 1);
if (v0 > X->n || (v0 == X->n && v1 > 0)) {
return mbedtls_mpi_lset(X, 0);
}if (v0 > X->n || (v0 == X->n && v1 > 0)) { ... }
/* ... */
if (v0 > 0) {
for (i = 0; i < X->n - v0; i++) {
X->p[i] = X->p[i + v0];
}for (i = 0; i < X->n - v0; i++) { ... }
for (; i < X->n; i++) {
X->p[i] = 0;
}for (; i < X->n; i++) { ... }
}if (v0 > 0) { ... }
/* ... */
if (v1 > 0) {
for (i = X->n; i > 0; i--) {
r1 = X->p[i - 1] << (biL - v1);
X->p[i - 1] >>= v1;
X->p[i - 1] |= r0;
r0 = r1;
}for (i = X->n; i > 0; i--) { ... }
}if (v1 > 0) { ... }
return 0;
}{ ... }
/* ... */
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
{
size_t i, j;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(Y != NULL);
for (i = X->n; i > 0; i--) {
if (X->p[i - 1] != 0) {
break;
}if (X->p[i - 1] != 0) { ... }
}for (i = X->n; i > 0; i--) { ... }
for (j = Y->n; j > 0; j--) {
if (Y->p[j - 1] != 0) {
break;
}if (Y->p[j - 1] != 0) { ... }
}for (j = Y->n; j > 0; j--) { ... }
if (i == 0 && j == 0) {
return 0;
}if (i == 0 && j == 0) { ... }
if (i > j) {
return 1;
}if (i > j) { ... }
if (j > i) {
return -1;
}if (j > i) { ... }
for (; i > 0; i--) {
if (X->p[i - 1] > Y->p[i - 1]) {
return 1;
}if (X->p[i - 1] > Y->p[i - 1]) { ... }
if (X->p[i - 1] < Y->p[i - 1]) {
return -1;
}if (X->p[i - 1] < Y->p[i - 1]) { ... }
}for (; i > 0; i--) { ... }
return 0;
}{ ... }
/* ... */
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
{
size_t i, j;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(Y != NULL);
for (i = X->n; i > 0; i--) {
if (X->p[i - 1] != 0) {
break;
}if (X->p[i - 1] != 0) { ... }
}for (i = X->n; i > 0; i--) { ... }
for (j = Y->n; j > 0; j--) {
if (Y->p[j - 1] != 0) {
break;
}if (Y->p[j - 1] != 0) { ... }
}for (j = Y->n; j > 0; j--) { ... }
if (i == 0 && j == 0) {
return 0;
}if (i == 0 && j == 0) { ... }
if (i > j) {
return X->s;
}if (i > j) { ... }
if (j > i) {
return -Y->s;
}if (j > i) { ... }
if (X->s > 0 && Y->s < 0) {
return 1;
}if (X->s > 0 && Y->s < 0) { ... }
if (Y->s > 0 && X->s < 0) {
return -1;
}if (Y->s > 0 && X->s < 0) { ... }
for (; i > 0; i--) {
if (X->p[i - 1] > Y->p[i - 1]) {
return X->s;
}if (X->p[i - 1] > Y->p[i - 1]) { ... }
if (X->p[i - 1] < Y->p[i - 1]) {
return -X->s;
}if (X->p[i - 1] < Y->p[i - 1]) { ... }
}for (; i > 0; i--) { ... }
return 0;
}{ ... }
/* ... */
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
{
mbedtls_mpi Y;
mbedtls_mpi_uint p[1];
MPI_VALIDATE_RET(X != NULL);
*p = mpi_sint_abs(z);
Y.s = (z < 0) ? -1 : 1;
Y.n = 1;
Y.p = p;
return mbedtls_mpi_cmp_mpi(X, &Y);
}{ ... }
/* ... */
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t i, j;
mbedtls_mpi_uint *o, *p, c, tmp;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(A != NULL);
MPI_VALIDATE_RET(B != NULL);
if (X == B) {
const mbedtls_mpi *T = A; A = X; B = T;
}if (X == B) { ... }
if (X != A) {
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
}if (X != A) { ... }
/* ... */
X->s = 1;
for (j = B->n; j > 0; j--) {
if (B->p[j - 1] != 0) {
break;
}if (B->p[j - 1] != 0) { ... }
}for (j = B->n; j > 0; j--) { ... }
/* ... */
if (j == 0) {
return 0;
}if (j == 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, j));
o = B->p; p = X->p; c = 0;
/* ... */
for (i = 0; i < j; i++, o++, p++) {
tmp = *o;
*p += c; c = (*p < c);
*p += tmp; c += (*p < tmp);
}for (i = 0; i < j; i++, o++, p++) { ... }
while (c != 0) {
if (i >= X->n) {
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + 1));
p = X->p + i;
}if (i >= X->n) { ... }
*p += c; c = (*p < c); i++; p++;
}while (c != 0) { ... }
cleanup:
return ret;
}{ ... }
/* ... */
static mbedtls_mpi_uint mpi_sub_hlp(size_t n,
mbedtls_mpi_uint *d,
const mbedtls_mpi_uint *l,
const mbedtls_mpi_uint *r)
{
size_t i;
mbedtls_mpi_uint c = 0, t, z;
for (i = 0; i < n; i++) {
z = (l[i] < c); t = l[i] - c;
c = (t < r[i]) + z; d[i] = t - r[i];
}for (i = 0; i < n; i++) { ... }
return c;
}{ ... }
/* ... */
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t n;
mbedtls_mpi_uint carry;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(A != NULL);
MPI_VALIDATE_RET(B != NULL);
for (n = B->n; n > 0; n--) {
if (B->p[n - 1] != 0) {
break;
}if (B->p[n - 1] != 0) { ... }
}for (n = B->n; n > 0; n--) { ... }
if (n > A->n) {
ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
goto cleanup;
}if (n > A->n) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, A->n));
/* ... */
if (A->n > n && A != X) {
memcpy(X->p + n, A->p + n, (A->n - n) * ciL);
}if (A->n > n && A != X) { ... }
if (X->n > A->n) {
memset(X->p + A->n, 0, (X->n - A->n) * ciL);
}if (X->n > A->n) { ... }
carry = mpi_sub_hlp(n, X->p, A->p, B->p);
if (carry != 0) {
for (; n < X->n && X->p[n] == 0; n++) {
--X->p[n];
}for (; n < X->n && X->p[n] == 0; n++) { ... }
/* ... */
if (n == X->n) {
ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
goto cleanup;
}if (n == X->n) { ... }
--X->p[n];
}if (carry != 0) { ... }
X->s = 1;
cleanup:
return ret;
}{ ... }
/* ... */
static int add_sub_mpi(mbedtls_mpi *X,
const mbedtls_mpi *A, const mbedtls_mpi *B,
int flip_B)
{
int ret, s;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(A != NULL);
MPI_VALIDATE_RET(B != NULL);
s = A->s;
if (A->s * B->s * flip_B < 0) {
int cmp = mbedtls_mpi_cmp_abs(A, B);
if (cmp >= 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, A, B));
/* ... */
X->s = cmp == 0 ? 1 : s;
}if (cmp >= 0) { ... } else {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(X, B, A));
X->s = -s;
}else { ... }
}if (A->s * B->s * flip_B < 0) { ... } else {
MBEDTLS_MPI_CHK(mbedtls_mpi_add_abs(X, A, B));
X->s = s;
}else { ... }
cleanup:
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
return add_sub_mpi(X, A, B, 1);
}{ ... }
/* ... */
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
return add_sub_mpi(X, A, B, -1);
}{ ... }
/* ... */
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
{
mbedtls_mpi B;
mbedtls_mpi_uint p[1];
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(A != NULL);
p[0] = mpi_sint_abs(b);
B.s = (b < 0) ? -1 : 1;
B.n = 1;
B.p = p;
return mbedtls_mpi_add_mpi(X, A, &B);
}{ ... }
/* ... */
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
{
mbedtls_mpi B;
mbedtls_mpi_uint p[1];
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(A != NULL);
p[0] = mpi_sint_abs(b);
B.s = (b < 0) ? -1 : 1;
B.n = 1;
B.p = p;
return mbedtls_mpi_sub_mpi(X, A, &B);
}{ ... }
/* ... */
static
#if defined(__APPLE__) && defined(__arm__)
/* ... */
__attribute__((noinline))/* ... */
#endif
void mpi_mul_hlp(size_t i,
const mbedtls_mpi_uint *s,
mbedtls_mpi_uint *d,
mbedtls_mpi_uint b)
{
mbedtls_mpi_uint c = 0, t = 0;
(void) t;
#if defined(MULADDC_HUIT)
for (; i >= 8; i -= 8) {
MULADDC_INIT
MULADDC_HUIT
MULADDC_STOP
}for (; i >= 8; i -= 8) { ... }
for (; i > 0; i--) {
MULADDC_INIT
MULADDC_CORE
MULADDC_STOP
}for (; i > 0; i--) { ... }
/* ... */#else
for (; i >= 16; i -= 16) {
MULADDC_INIT
MULADDC_CORE MULADDC_CORE
MULADDC_CORE MULADDC_CORE
MULADDC_CORE MULADDC_CORE
MULADDC_CORE MULADDC_CORE
MULADDC_CORE MULADDC_CORE
MULADDC_CORE MULADDC_CORE
MULADDC_CORE MULADDC_CORE
MULADDC_CORE MULADDC_CORE
MULADDC_STOP
}for (; i >= 16; i -= 16) { ... }
for (; i >= 8; i -= 8) {
MULADDC_INIT
MULADDC_CORE MULADDC_CORE
MULADDC_CORE MULADDC_CORE
MULADDC_CORE MULADDC_CORE
MULADDC_CORE MULADDC_CORE
MULADDC_STOP
}for (; i >= 8; i -= 8) { ... }
for (; i > 0; i--) {
MULADDC_INIT
MULADDC_CORE
MULADDC_STOP
}for (; i > 0; i--) { ... }
/* ... */#endif
while (c != 0) {
*d += c; c = (*d < c); d++;
}while (c != 0) { ... }
}{ ... }
/* ... */
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t i, j;
mbedtls_mpi TA, TB;
int result_is_zero = 0;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(A != NULL);
MPI_VALIDATE_RET(B != NULL);
mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
if (X == A) {
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A)); A = &TA;
}if (X == A) { ... }
if (X == B) {
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B)); B = &TB;
}if (X == B) { ... }
for (i = A->n; i > 0; i--) {
if (A->p[i - 1] != 0) {
break;
}if (A->p[i - 1] != 0) { ... }
}for (i = A->n; i > 0; i--) { ... }
if (i == 0) {
result_is_zero = 1;
}if (i == 0) { ... }
for (j = B->n; j > 0; j--) {
if (B->p[j - 1] != 0) {
break;
}if (B->p[j - 1] != 0) { ... }
}for (j = B->n; j > 0; j--) { ... }
if (j == 0) {
result_is_zero = 1;
}if (j == 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, i + j));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 0));
for (; j > 0; j--) {
mpi_mul_hlp(i, A->p, X->p + j - 1, B->p[j - 1]);
}for (; j > 0; j--) { ... }
/* ... */
if (result_is_zero) {
X->s = 1;
}if (result_is_zero) { ... } else {
X->s = A->s * B->s;
}else { ... }
cleanup:
mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TA);
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
{
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(A != NULL);
size_t n = A->n;
while (n > 0 && A->p[n - 1] == 0) {
--n;
}while (n > 0 && A->p[n - 1] == 0) { ... }
/* ... */
if (b == 0 || n == 0) {
return mbedtls_mpi_lset(X, 0);
}if (b == 0 || n == 0) { ... }
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, n + 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A));
mpi_mul_hlp(n, A->p, X->p, b - 1);
cleanup:
return ret;
}{ ... }
/* ... */
static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
mbedtls_mpi_uint u0,
mbedtls_mpi_uint d,
mbedtls_mpi_uint *r)
{
#if defined(MBEDTLS_HAVE_UDBL)
mbedtls_t_udbl dividend, quotient;
#else
const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
const mbedtls_mpi_uint uint_halfword_mask = ((mbedtls_mpi_uint) 1 << biH) - 1;
mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
mbedtls_mpi_uint u0_msw, u0_lsw;
size_t s;/* ... */
#endif
/* ... */
if (0 == d || u1 >= d) {
if (r != NULL) {
*r = ~(mbedtls_mpi_uint) 0u;
}if (r != NULL) { ... }
return ~(mbedtls_mpi_uint) 0u;
}if (0 == d || u1 >= d) { ... }
#if defined(MBEDTLS_HAVE_UDBL)
dividend = (mbedtls_t_udbl) u1 << biL;
dividend |= (mbedtls_t_udbl) u0;
quotient = dividend / d;
if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) {
quotient = ((mbedtls_t_udbl) 1 << biL) - 1;
}if (quotient > ((mbedtls_t_udbl) 1 << biL) - 1) { ... }
if (r != NULL) {
*r = (mbedtls_mpi_uint) (dividend - (quotient * d));
}if (r != NULL) { ... }
return (mbedtls_mpi_uint) quotient;/* ... */
#else
/* ... */
/* ... */
s = mbedtls_clz(d);
d = d << s;
u1 = u1 << s;
u1 |= (u0 >> (biL - s)) & (-(mbedtls_mpi_sint) s >> (biL - 1));
u0 = u0 << s;
d1 = d >> biH;
d0 = d & uint_halfword_mask;
u0_msw = u0 >> biH;
u0_lsw = u0 & uint_halfword_mask;
/* ... */
q1 = u1 / d1;
r0 = u1 - d1 * q1;
while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) {
q1 -= 1;
r0 += d1;
if (r0 >= radix) {
break;
}if (r0 >= radix) { ... }
}while (q1 >= radix || (q1 * d0 > radix * r0 + u0_msw)) { ... }
rAX = (u1 * radix) + (u0_msw - q1 * d);
q0 = rAX / d1;
r0 = rAX - q0 * d1;
while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) {
q0 -= 1;
r0 += d1;
if (r0 >= radix) {
break;
}if (r0 >= radix) { ... }
}while (q0 >= radix || (q0 * d0 > radix * r0 + u0_lsw)) { ... }
if (r != NULL) {
*r = (rAX * radix + u0_lsw - q0 * d) >> s;
}if (r != NULL) { ... }
quotient = q1 * radix + q0;
return quotient;/* ... */
#endif
}{ ... }
/* ... */
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
const mbedtls_mpi *B)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t i, n, t, k;
mbedtls_mpi X, Y, Z, T1, T2;
mbedtls_mpi_uint TP2[3];
MPI_VALIDATE_RET(A != NULL);
MPI_VALIDATE_RET(B != NULL);
if (mbedtls_mpi_cmp_int(B, 0) == 0) {
return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
}if (mbedtls_mpi_cmp_int(B, 0) == 0) { ... }
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
mbedtls_mpi_init(&T1);
/* ... */
T2.s = 1;
T2.n = sizeof(TP2) / sizeof(*TP2);
T2.p = TP2;
if (mbedtls_mpi_cmp_abs(A, B) < 0) {
if (Q != NULL) {
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(Q, 0));
}if (Q != NULL) { ... }
if (R != NULL) {
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, A));
}if (R != NULL) { ... }
return 0;
}if (mbedtls_mpi_cmp_abs(A, B) < 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&X, A));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, B));
X.s = Y.s = 1;
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&Z, A->n + 2));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Z, 0));
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T1, A->n + 2));
k = mbedtls_mpi_bitlen(&Y) % biL;
if (k < biL - 1) {
k = biL - 1 - k;
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&X, k));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, k));
}if (k < biL - 1) { ... } else {
k = 0;
}else { ... }
n = X.n - 1;
t = Y.n - 1;
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&Y, biL * (n - t)));
while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) {
Z.p[n - t]++;
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &Y));
}while (mbedtls_mpi_cmp_mpi(&X, &Y) >= 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, biL * (n - t)));
for (i = n; i > t; i--) {
if (X.p[i] >= Y.p[t]) {
Z.p[i - t - 1] = ~(mbedtls_mpi_uint) 0u;
}if (X.p[i] >= Y.p[t]) { ... } else {
Z.p[i - t - 1] = mbedtls_int_div_int(X.p[i], X.p[i - 1],
Y.p[t], NULL);
}else { ... }
T2.p[0] = (i < 2) ? 0 : X.p[i - 2];
T2.p[1] = (i < 1) ? 0 : X.p[i - 1];
T2.p[2] = X.p[i];
Z.p[i - t - 1]++;
do {
Z.p[i - t - 1]--;
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&T1, 0));
T1.p[0] = (t < 1) ? 0 : Y.p[t - 1];
T1.p[1] = Y.p[t];
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &T1, Z.p[i - t - 1]));
...} while (mbedtls_mpi_cmp_mpi(&T1, &T2) > 0);
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&T1, &Y, Z.p[i - t - 1]));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&X, &X, &T1));
if (mbedtls_mpi_cmp_int(&X, 0) < 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &Y));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&T1, biL * (i - t - 1)));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&X, &X, &T1));
Z.p[i - t - 1]--;
}if (mbedtls_mpi_cmp_int(&X, 0) < 0) { ... }
}for (i = n; i > t; i--) { ... }
if (Q != NULL) {
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(Q, &Z));
Q->s = A->s * B->s;
}if (Q != NULL) { ... }
if (R != NULL) {
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&X, k));
X.s = A->s;
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(R, &X));
if (mbedtls_mpi_cmp_int(R, 0) == 0) {
R->s = 1;
}if (mbedtls_mpi_cmp_int(R, 0) == 0) { ... }
}if (R != NULL) { ... }
cleanup:
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
mbedtls_mpi_free(&T1);
mbedtls_platform_zeroize(TP2, sizeof(TP2));
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R,
const mbedtls_mpi *A,
mbedtls_mpi_sint b)
{
mbedtls_mpi B;
mbedtls_mpi_uint p[1];
MPI_VALIDATE_RET(A != NULL);
p[0] = mpi_sint_abs(b);
B.s = (b < 0) ? -1 : 1;
B.n = 1;
B.p = p;
return mbedtls_mpi_div_mpi(Q, R, A, &B);
}{ ... }
/* ... */
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
MPI_VALIDATE_RET(R != NULL);
MPI_VALIDATE_RET(A != NULL);
MPI_VALIDATE_RET(B != NULL);
if (mbedtls_mpi_cmp_int(B, 0) < 0) {
return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
}if (mbedtls_mpi_cmp_int(B, 0) < 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(NULL, R, A, B));
while (mbedtls_mpi_cmp_int(R, 0) < 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(R, R, B));
}while (mbedtls_mpi_cmp_int(R, 0) < 0) { ... }
while (mbedtls_mpi_cmp_mpi(R, B) >= 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(R, R, B));
}while (mbedtls_mpi_cmp_mpi(R, B) >= 0) { ... }
cleanup:
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
{
size_t i;
mbedtls_mpi_uint x, y, z;
MPI_VALIDATE_RET(r != NULL);
MPI_VALIDATE_RET(A != NULL);
if (b == 0) {
return MBEDTLS_ERR_MPI_DIVISION_BY_ZERO;
}if (b == 0) { ... }
if (b < 0) {
return MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
}if (b < 0) { ... }
/* ... */
if (b == 1 || A->n == 0) {
*r = 0;
return 0;
}if (b == 1 || A->n == 0) { ... }
if (b == 2) {
*r = A->p[0] & 1;
return 0;
}if (b == 2) { ... }
/* ... */
for (i = A->n, y = 0; i > 0; i--) {
x = A->p[i - 1];
y = (y << biH) | (x >> biH);
z = y / b;
y -= z * b;
x <<= biH;
y = (y << biH) | (x >> biH);
z = y / b;
y -= z * b;
}for (i = A->n, y = 0; i > 0; i--) { ... }
/* ... */
if (A->s < 0 && y != 0) {
y = b - y;
}if (A->s < 0 && y != 0) { ... }
*r = y;
return 0;
}{ ... }
/* ... */
mbedtls_mpi_uint mbedtls_mpi_montmul_init(const mbedtls_mpi_uint *N)
{
mbedtls_mpi_uint x = N[0];
x += ((N[0] + 2) & 4) << 1;
for (unsigned int i = biL; i >= 8; i /= 2) {
x *= (2 - (N[0] * x));
}for (unsigned int i = biL; i >= 8; i /= 2) { ... }
return ~x + 1;
}{ ... }
void mbedtls_mpi_montmul(mbedtls_mpi *A,
const mbedtls_mpi *B,
const mbedtls_mpi *N,
mbedtls_mpi_uint mm,
const mbedtls_mpi *T)
{
size_t i, n, m;
mbedtls_mpi_uint u0, u1, *d;
memset(T->p, 0, T->n * ciL);
d = T->p;
n = N->n;
m = (B->n < n) ? B->n : n;
for (i = 0; i < n; i++) {
/* ... */
u0 = A->p[i];
u1 = (d[0] + u0 * B->p[0]) * mm;
mpi_mul_hlp(m, B->p, d, u0);
mpi_mul_hlp(n, N->p, d, u1);
*d++ = u0; d[n + 1] = 0;
}for (i = 0; i < n; i++) { ... }
/* ... */
/* ... */
memcpy(A->p, d, n * ciL);
/* ... */
d[n] += 1;
d[n] -= mpi_sub_hlp(n, d, d, N->p);
/* ... */
mbedtls_ct_mpi_uint_cond_assign(n, A->p, d, (unsigned char) d[n]);
}{ ... }
/* ... */
static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
mbedtls_mpi_uint mm, const mbedtls_mpi *T)
{
mbedtls_mpi_uint z = 1;
mbedtls_mpi U;
U.n = U.s = (int) z;
U.p = &z;
mbedtls_mpi_montmul(A, &U, N, mm, T);
}{ ... }
/* ... */
static int mpi_select(mbedtls_mpi *R, const mbedtls_mpi *T, size_t T_size, size_t idx)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
for (size_t i = 0; i < T_size; i++) {
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(R, &T[i],
(unsigned char) mbedtls_ct_size_bool_eq(i,
idx)));
}for (size_t i = 0; i < T_size; i++) { ... }
cleanup:
return ret;
}{ ... }
int mbedtls_mpi_get_mont_r2_unsafe(mbedtls_mpi *X,
const mbedtls_mpi *N)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
cleanup:
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
const mbedtls_mpi *E, const mbedtls_mpi *N,
mbedtls_mpi *prec_RR)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t window_bitsize;
size_t i, j, nblimbs;
size_t bufsize, nbits;
size_t exponent_bits_in_window = 0;
mbedtls_mpi_uint ei, mm, state;
mbedtls_mpi RR, T, W[(size_t) 1 << MBEDTLS_MPI_WINDOW_SIZE], WW, Apos;
int neg;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(A != NULL);
MPI_VALIDATE_RET(E != NULL);
MPI_VALIDATE_RET(N != NULL);
if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (mbedtls_mpi_cmp_int(N, 0) <= 0 || (N->p[0] & 1) == 0) { ... }
if (mbedtls_mpi_cmp_int(E, 0) < 0) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (mbedtls_mpi_cmp_int(E, 0) < 0) { ... }
if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS ||
mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (mbedtls_mpi_bitlen(E) > MBEDTLS_MPI_MAX_BITS || mbedtls_mpi_bitlen(N) > MBEDTLS_MPI_MAX_BITS) { ... }
/* ... */
mm = mbedtls_mpi_montmul_init(N->p);
mbedtls_mpi_init(&RR); mbedtls_mpi_init(&T);
mbedtls_mpi_init(&Apos);
mbedtls_mpi_init(&WW);
memset(W, 0, sizeof(W));
i = mbedtls_mpi_bitlen(E);
window_bitsize = (i > 671) ? 6 : (i > 239) ? 5 :
(i > 79) ? 4 : (i > 23) ? 3 : 1;
#if (MBEDTLS_MPI_WINDOW_SIZE < 6)
if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) {
window_bitsize = MBEDTLS_MPI_WINDOW_SIZE;
}if (window_bitsize > MBEDTLS_MPI_WINDOW_SIZE) { ... }
/* ... */#endif
const size_t w_table_used_size = (size_t) 1 << window_bitsize;
/* ... */
const size_t x_index = 0;
mbedtls_mpi_init(&W[x_index]);
j = N->n + 1;
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[x_index], j));
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], j));
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&T, j * 2));
/* ... */
neg = (A->s == -1);
if (neg) {
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Apos, A));
Apos.s = 1;
A = &Apos;
}if (neg) { ... }
/* ... */
if (prec_RR == NULL || prec_RR->p == NULL) {
mbedtls_mpi_get_mont_r2_unsafe(&RR, N);
if (prec_RR != NULL) {
memcpy(prec_RR, &RR, sizeof(mbedtls_mpi));
}if (prec_RR != NULL) { ... }
}if (prec_RR == NULL || prec_RR->p == NULL) { ... } else {
memcpy(&RR, prec_RR, sizeof(mbedtls_mpi));
}else { ... }
/* ... */
if (mbedtls_mpi_cmp_mpi(A, N) >= 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&W[1], A, N));
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[1], N->n + 1));
}if (mbedtls_mpi_cmp_mpi(A, N) >= 0) { ... } else {
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[1], A));
}else { ... }
/* ... */
mbedtls_mpi_montmul(&W[1], &RR, N, mm, &T);
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[x_index], &RR));
mpi_montred(&W[x_index], N, mm, &T);
if (window_bitsize > 1) {
/* ... */
j = w_table_used_size / 2;
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[j], N->n + 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[j], &W[1]));
for (i = 0; i < window_bitsize - 1; i++) {
mbedtls_mpi_montmul(&W[j], &W[j], N, mm, &T);
}for (i = 0; i < window_bitsize - 1; i++) { ... }
/* ... */
for (i = j + 1; i < w_table_used_size; i++) {
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&W[i], N->n + 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&W[i], &W[i - 1]));
mbedtls_mpi_montmul(&W[i], &W[1], N, mm, &T);
}for (i = j + 1; i < w_table_used_size; i++) { ... }
}if (window_bitsize > 1) { ... }
nblimbs = E->n;
bufsize = 0;
nbits = 0;
state = 0;
while (1) {
if (bufsize == 0) {
if (nblimbs == 0) {
break;
}if (nblimbs == 0) { ... }
nblimbs--;
bufsize = sizeof(mbedtls_mpi_uint) << 3;
}if (bufsize == 0) { ... }
bufsize--;
ei = (E->p[nblimbs] >> bufsize) & 1;
/* ... */
if (ei == 0 && state == 0) {
continue;
}if (ei == 0 && state == 0) { ... }
if (ei == 0 && state == 1) {
/* ... */
MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
mbedtls_mpi_montmul(&W[x_index], &WW, N, mm, &T);
continue;
}if (ei == 0 && state == 1) { ... }
/* ... */
state = 2;
nbits++;
exponent_bits_in_window |= (ei << (window_bitsize - nbits));
if (nbits == window_bitsize) {
/* ... */
for (i = 0; i < window_bitsize; i++) {
MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
x_index));
mbedtls_mpi_montmul(&W[x_index], &WW, N, mm, &T);
}for (i = 0; i < window_bitsize; i++) { ... }
/* ... */
MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size,
exponent_bits_in_window));
mbedtls_mpi_montmul(&W[x_index], &WW, N, mm, &T);
state--;
nbits = 0;
exponent_bits_in_window = 0;
}if (nbits == window_bitsize) { ... }
}while (1) { ... }
/* ... */
for (i = 0; i < nbits; i++) {
MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, x_index));
mbedtls_mpi_montmul(&W[x_index], &WW, N, mm, &T);
exponent_bits_in_window <<= 1;
if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) {
MBEDTLS_MPI_CHK(mpi_select(&WW, W, w_table_used_size, 1));
mbedtls_mpi_montmul(&W[x_index], &WW, N, mm, &T);
}if ((exponent_bits_in_window & ((size_t) 1 << window_bitsize)) != 0) { ... }
}for (i = 0; i < nbits; i++) { ... }
/* ... */
mpi_montred(&W[x_index], N, mm, &T);
if (neg && E->n != 0 && (E->p[0] & 1) != 0) {
W[x_index].s = -1;
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&W[x_index], N, &W[x_index]));
}if (neg && E->n != 0 && (E->p[0] & 1) != 0) { ... }
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &W[x_index]));
cleanup:
/* ... */
for (i = w_table_used_size/2; i < w_table_used_size; i++) {
mbedtls_mpi_free(&W[i]);
}for (i = w_table_used_size/2; i < w_table_used_size; i++) { ... }
mbedtls_mpi_free(&W[x_index]);
mbedtls_mpi_free(&W[1]);
mbedtls_mpi_free(&T);
mbedtls_mpi_free(&Apos);
mbedtls_mpi_free(&WW);
if (prec_RR == NULL || prec_RR->p == NULL) {
mbedtls_mpi_free(&RR);
}if (prec_RR == NULL || prec_RR->p == NULL) { ... }
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t lz, lzt;
mbedtls_mpi TA, TB;
MPI_VALIDATE_RET(G != NULL);
MPI_VALIDATE_RET(A != NULL);
MPI_VALIDATE_RET(B != NULL);
mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TB);
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TA, A));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, B));
lz = mbedtls_mpi_lsb(&TA);
lzt = mbedtls_mpi_lsb(&TB);
/* ... */
if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) {
ret = mbedtls_mpi_copy(G, A);
goto cleanup;
}if (lzt == 0 && mbedtls_mpi_get_bit(&TB, 0) == 0) { ... }
if (lzt < lz) {
lz = lzt;
}if (lzt < lz) { ... }
TA.s = TB.s = 1;
/* ... */
/* ... */
while (mbedtls_mpi_cmp_int(&TA, 0) != 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, mbedtls_mpi_lsb(&TA)));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, mbedtls_mpi_lsb(&TB)));
/* ... */
if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TA, &TA, &TB));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TA, 1));
}if (mbedtls_mpi_cmp_mpi(&TA, &TB) >= 0) { ... } else {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&TB, &TB, &TA));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TB, 1));
}else { ... }
}while (mbedtls_mpi_cmp_int(&TA, 0) != 0) { ... }
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(&TB, lz));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(G, &TB));
cleanup:
mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TB);
return ret;
}{ ... }
/* ... */
static int mpi_fill_random_internal(
mbedtls_mpi *X, size_t n_bytes,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
const size_t limbs = CHARS_TO_LIMBS(n_bytes);
const size_t overhead = (limbs * ciL) - n_bytes;
if (X->n < limbs) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (X->n < limbs) { ... }
memset(X->p, 0, overhead);
memset((unsigned char *) X->p + limbs * ciL, 0, (X->n - limbs) * ciL);
MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X->p + overhead, n_bytes));
mpi_bigendian_to_host(X->p, limbs);
cleanup:
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t const limbs = CHARS_TO_LIMBS(size);
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(f_rng != NULL);
MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, limbs));
if (size == 0) {
return 0;
}if (size == 0) { ... }
ret = mpi_fill_random_internal(X, size, f_rng, p_rng);
cleanup:
return ret;
}{ ... }
int mbedtls_mpi_random(mbedtls_mpi *X,
mbedtls_mpi_sint min,
const mbedtls_mpi *N,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
int count;
unsigned lt_lower = 1, lt_upper = 0;
size_t n_bits = mbedtls_mpi_bitlen(N);
size_t n_bytes = (n_bits + 7) / 8;
mbedtls_mpi lower_bound;
if (min < 0) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (min < 0) { ... }
if (mbedtls_mpi_cmp_int(N, min) <= 0) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (mbedtls_mpi_cmp_int(N, min) <= 0) { ... }
/* ... */
count = (n_bytes > 4 ? 30 : 250);
mbedtls_mpi_init(&lower_bound);
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_resize_clear(X, N->n));
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&lower_bound, N->n));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&lower_bound, min));
/* ... */
do {
MBEDTLS_MPI_CHK(mpi_fill_random_internal(X, n_bytes, f_rng, p_rng));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, 8 * n_bytes - n_bits));
if (--count == 0) {
ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
goto cleanup;
}if (--count == 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(X, &lower_bound, <_lower));
MBEDTLS_MPI_CHK(mbedtls_mpi_lt_mpi_ct(X, N, <_upper));
...} while (lt_lower != 0 || lt_upper == 0);
cleanup:
mbedtls_mpi_free(&lower_bound);
return ret;
}{ ... }
/* ... */
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(A != NULL);
MPI_VALIDATE_RET(N != NULL);
if (mbedtls_mpi_cmp_int(N, 1) <= 0) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (mbedtls_mpi_cmp_int(N, 1) <= 0) { ... }
mbedtls_mpi_init(&TA); mbedtls_mpi_init(&TU); mbedtls_mpi_init(&U1); mbedtls_mpi_init(&U2);
mbedtls_mpi_init(&G); mbedtls_mpi_init(&TB); mbedtls_mpi_init(&TV);
mbedtls_mpi_init(&V1); mbedtls_mpi_init(&V2);
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&G, A, N));
if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
goto cleanup;
}if (mbedtls_mpi_cmp_int(&G, 1) != 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&TA, A, N));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TU, &TA));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TB, N));
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&TV, N));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U1, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&U2, 0));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V1, 0));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&V2, 1));
do {
while ((TU.p[0] & 1) == 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TU, 1));
if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&U1, &U1, &TB));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &TA));
}if ((U1.p[0] & 1) != 0 || (U2.p[0] & 1) != 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U1, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&U2, 1));
}while ((TU.p[0] & 1) == 0) { ... }
while ((TV.p[0] & 1) == 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&TV, 1));
if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, &TB));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &TA));
}if ((V1.p[0] & 1) != 0 || (V2.p[0] & 1) != 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V1, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&V2, 1));
}while ((TV.p[0] & 1) == 0) { ... }
if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TU, &TU, &TV));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U1, &U1, &V1));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&U2, &U2, &V2));
}if (mbedtls_mpi_cmp_mpi(&TU, &TV) >= 0) { ... } else {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&TV, &TV, &TU));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, &U1));
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V2, &V2, &U2));
}else { ... }
...} while (mbedtls_mpi_cmp_int(&TU, 0) != 0);
while (mbedtls_mpi_cmp_int(&V1, 0) < 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&V1, &V1, N));
}while (mbedtls_mpi_cmp_int(&V1, 0) < 0) { ... }
while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&V1, &V1, N));
}while (mbedtls_mpi_cmp_mpi(&V1, N) >= 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, &V1));
cleanup:
mbedtls_mpi_free(&TA); mbedtls_mpi_free(&TU); mbedtls_mpi_free(&U1); mbedtls_mpi_free(&U2);
mbedtls_mpi_free(&G); mbedtls_mpi_free(&TB); mbedtls_mpi_free(&TV);
mbedtls_mpi_free(&V1); mbedtls_mpi_free(&V2);
return ret;
}{ ... }
#if defined(MBEDTLS_GENPRIME)
static const int small_prime[] =
{
3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97,
101, 103, 107, 109, 113, 127, 131, 137,
139, 149, 151, 157, 163, 167, 173, 179,
181, 191, 193, 197, 199, 211, 223, 227,
229, 233, 239, 241, 251, 257, 263, 269,
271, 277, 281, 283, 293, 307, 311, 313,
317, 331, 337, 347, 349, 353, 359, 367,
373, 379, 383, 389, 397, 401, 409, 419,
421, 431, 433, 439, 443, 449, 457, 461,
463, 467, 479, 487, 491, 499, 503, 509,
521, 523, 541, 547, 557, 563, 569, 571,
577, 587, 593, 599, 601, 607, 613, 617,
619, 631, 641, 643, 647, 653, 659, 661,
673, 677, 683, 691, 701, 709, 719, 727,
733, 739, 743, 751, 757, 761, 769, 773,
787, 797, 809, 811, 821, 823, 827, 829,
839, 853, 857, 859, 863, 877, 881, 883,
887, 907, 911, 919, 929, 937, 941, 947,
953, 967, 971, 977, 983, 991, 997, -103
...};
/* ... */
static int mpi_check_small_factors(const mbedtls_mpi *X)
{
int ret = 0;
size_t i;
mbedtls_mpi_uint r;
if ((X->p[0] & 1) == 0) {
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
}if ((X->p[0] & 1) == 0) { ... }
for (i = 0; small_prime[i] > 0; i++) {
if (mbedtls_mpi_cmp_int(X, small_prime[i]) <= 0) {
return 1;
}if (mbedtls_mpi_cmp_int(X, small_prime[i]) <= 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, small_prime[i]));
if (r == 0) {
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
}if (r == 0) { ... }
}for (i = 0; small_prime[i] > 0; i++) { ... }
cleanup:
return ret;
}mpi_check_small_factors (const mbedtls_mpi *X) { ... }
/* ... */
static int mpi_miller_rabin(const mbedtls_mpi *X, size_t rounds,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
int ret, count;
size_t i, j, k, s;
mbedtls_mpi W, R, T, A, RR;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(f_rng != NULL);
mbedtls_mpi_init(&W); mbedtls_mpi_init(&R);
mbedtls_mpi_init(&T); mbedtls_mpi_init(&A);
mbedtls_mpi_init(&RR);
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&W, X, 1));
s = mbedtls_mpi_lsb(&W);
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R, &W));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&R, s));
for (i = 0; i < rounds; i++) {
/* ... */
count = 0;
do {
MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&A, X->n * ciL, f_rng, p_rng));
j = mbedtls_mpi_bitlen(&A);
k = mbedtls_mpi_bitlen(&W);
if (j > k) {
A.p[A.n - 1] &= ((mbedtls_mpi_uint) 1 << (k - (A.n - 1) * biL - 1)) - 1;
}if (j > k) { ... }
if (count++ > 30) {
ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
goto cleanup;
}if (count++ > 30) { ... }
...} while (mbedtls_mpi_cmp_mpi(&A, &W) >= 0 ||
mbedtls_mpi_cmp_int(&A, 1) <= 0);
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
mbedtls_mpi_cmp_int(&A, 1) == 0) {
continue;
}if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 || mbedtls_mpi_cmp_int(&A, 1) == 0) { ... }
j = 1;
while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) {
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &A, &A));
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&A, &T, X));
if (mbedtls_mpi_cmp_int(&A, 1) == 0) {
break;
}if (mbedtls_mpi_cmp_int(&A, 1) == 0) { ... }
j++;
}while (j < s && mbedtls_mpi_cmp_mpi(&A, &W) != 0) { ... }
/* ... */
if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
mbedtls_mpi_cmp_int(&A, 1) == 0) {
ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
break;
}if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 || mbedtls_mpi_cmp_int(&A, 1) == 0) { ... }
}for (i = 0; i < rounds; i++) { ... }
cleanup:
mbedtls_mpi_free(&W); mbedtls_mpi_free(&R);
mbedtls_mpi_free(&T); mbedtls_mpi_free(&A);
mbedtls_mpi_free(&RR);
return ret;
}mpi_miller_rabin (const mbedtls_mpi *X, size_t rounds, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) { ... }
/* ... */
int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi XX;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(f_rng != NULL);
XX.s = 1;
XX.n = X->n;
XX.p = X->p;
if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
mbedtls_mpi_cmp_int(&XX, 1) == 0) {
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
}if (mbedtls_mpi_cmp_int(&XX, 0) == 0 || mbedtls_mpi_cmp_int(&XX, 1) == 0) { ... }
if (mbedtls_mpi_cmp_int(&XX, 2) == 0) {
return 0;
}if (mbedtls_mpi_cmp_int(&XX, 2) == 0) { ... }
if ((ret = mpi_check_small_factors(&XX)) != 0) {
if (ret == 1) {
return 0;
}if (ret == 1) { ... }
return ret;
}if ((ret = mpi_check_small_factors(&XX)) != 0) { ... }
return mpi_miller_rabin(&XX, rounds, f_rng, p_rng);
}mbedtls_mpi_is_prime_ext (const mbedtls_mpi *X, int rounds, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) { ... }
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
/* ... */
int mbedtls_mpi_is_prime(const mbedtls_mpi *X,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(f_rng != NULL);
/* ... */
return mbedtls_mpi_is_prime_ext(X, 40, f_rng, p_rng);
}mbedtls_mpi_is_prime (const mbedtls_mpi *X, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) { ... }
/* ... */#endif
/* ... */
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
#ifdef MBEDTLS_HAVE_INT64
#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
/* ... */#else
#define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
/* ... */#endif
int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
size_t k, n;
int rounds;
mbedtls_mpi_uint r;
mbedtls_mpi Y;
MPI_VALIDATE_RET(X != NULL);
MPI_VALIDATE_RET(f_rng != NULL);
if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}if (nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS) { ... }
mbedtls_mpi_init(&Y);
n = BITS_TO_LIMBS(nbits);
if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) {
/* ... */
rounds = ((nbits >= 1300) ? 2 : (nbits >= 850) ? 3 :
(nbits >= 650) ? 4 : (nbits >= 350) ? 8 :
(nbits >= 250) ? 12 : (nbits >= 150) ? 18 : 27);
}if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR) == 0) { ... } else {
/* ... */
rounds = ((nbits >= 1450) ? 4 : (nbits >= 1150) ? 5 :
(nbits >= 1000) ? 6 : (nbits >= 850) ? 7 :
(nbits >= 750) ? 8 : (nbits >= 500) ? 13 :
(nbits >= 250) ? 28 : (nbits >= 150) ? 40 : 51);
}else { ... }
while (1) {
MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(X, n * ciL, f_rng, p_rng));
if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) {
continue;
}if (X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2) { ... }
k = n * biL;
if (k > nbits) {
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(X, k - nbits));
}if (k > nbits) { ... }
X->p[0] |= 1;
if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) {
ret = mbedtls_mpi_is_prime_ext(X, rounds, f_rng, p_rng);
if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
goto cleanup;
}if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) { ... }
}if ((flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) == 0) { ... } else {
/* ... */
X->p[0] |= 2;
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_int(&r, X, 3));
if (r == 0) {
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 8));
}if (r == 0) { ... } else if (r == 1) {
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 4));
}else if (r == 1) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Y, X));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&Y, 1));
while (1) {
/* ... */
if ((ret = mpi_check_small_factors(X)) == 0 &&
(ret = mpi_check_small_factors(&Y)) == 0 &&
(ret = mpi_miller_rabin(X, rounds, f_rng, p_rng))
== 0 &&
(ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng))
== 0) {
goto cleanup;
}if ((ret = mpi_check_small_factors(X)) == 0 && (ret = mpi_check_small_factors(&Y)) == 0 && (ret = mpi_miller_rabin(X, rounds, f_rng, p_rng)) == 0 && (ret = mpi_miller_rabin(&Y, rounds, f_rng, p_rng)) == 0) { ... }
if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
goto cleanup;
}if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) { ... }
/* ... */
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(X, X, 12));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&Y, &Y, 6));
}while (1) { ... }
}else { ... }
}while (1) { ... }
cleanup:
mbedtls_mpi_free(&Y);
return ret;
}mbedtls_mpi_gen_prime (mbedtls_mpi *X, size_t nbits, int flags, int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) { ... }
/* ... */
#endif
#if defined(MBEDTLS_SELF_TEST)
#define GCD_PAIR_COUNT 3
static const int gcd_pairs[GCD_PAIR_COUNT][3] =
{
{ 693, 609, 21 },
{ 1764, 868, 28 },
{ 768454923, 542167814, 1 }
...};
/* ... */
int mbedtls_mpi_self_test(int verbose)
{
int ret, i;
mbedtls_mpi A, E, N, X, Y, U, V;
mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N); mbedtls_mpi_init(&X);
mbedtls_mpi_init(&Y); mbedtls_mpi_init(&U); mbedtls_mpi_init(&V);
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&A, 16,
"EFE021C2645FD1DC586E69184AF4A31E" \
"D5F53E93B5F123FA41680867BA110131" \
"944FE7952E2517337780CB0DB80E61AA" \
"E7C8DDC6C5C6AADEB34EB38A2F40D5E6"));
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&E, 16,
"B2E7EFD37075B9F03FF989C7C5051C20" \
"34D2A323810251127E7BF8625A4F49A5" \
"F3E27F4DA8BD59C47D6DAABA4C8127BD" \
"5B5C25763222FEFCCFC38B832366C29E"));
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&N, 16,
"0066A198186C18C10B2F5ED9B522752A" \
"9830B69916E535C8F047518A889A43A5" \
"94B6BED27A168D31D4A52F88925AA8F5"));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&X, &A, &N));
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
"602AB7ECA597A3D6B56FF9829A5E8B85" \
"9E857EA95A03512E2BAE7391688D264A" \
"A5663B0341DB9CCFD2C4C5F421FEC814" \
"8001B72E848A38CAE1C65F78E56ABDEF" \
"E12D3C039B8A02D6BE593F0BBBDA56F1" \
"ECF677152EF804370C1A305CAF3B5BF1" \
"30879B56C61DE584A0F53A2447A51E"));
if (verbose != 0) {
mbedtls_printf(" MPI test #1 (mul_mpi): ");
}if (verbose != 0) { ... }
if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
if (verbose != 0) {
mbedtls_printf("failed\n");
}if (verbose != 0) { ... }
ret = 1;
goto cleanup;
}if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) { ... }
if (verbose != 0) {
mbedtls_printf("passed\n");
}if (verbose != 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_div_mpi(&X, &Y, &A, &N));
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
"256567336059E52CAE22925474705F39A94"));
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&V, 16,
"6613F26162223DF488E9CD48CC132C7A" \
"0AC93C701B001B092E4E5B9F73BCD27B" \
"9EE50D0657C77F374E903CDFA4C642"));
if (verbose != 0) {
mbedtls_printf(" MPI test #2 (div_mpi): ");
}if (verbose != 0) { ... }
if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
if (verbose != 0) {
mbedtls_printf("failed\n");
}if (verbose != 0) { ... }
ret = 1;
goto cleanup;
}if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 || mbedtls_mpi_cmp_mpi(&Y, &V) != 0) { ... }
if (verbose != 0) {
mbedtls_printf("passed\n");
}if (verbose != 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&X, &A, &E, &N, NULL));
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
"36E139AEA55215609D2816998ED020BB" \
"BD96C37890F65171D948E9BC7CBAA4D9" \
"325D24D6A3C12710F10A09FA08AB87"));
if (verbose != 0) {
mbedtls_printf(" MPI test #3 (exp_mod): ");
}if (verbose != 0) { ... }
if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
if (verbose != 0) {
mbedtls_printf("failed\n");
}if (verbose != 0) { ... }
ret = 1;
goto cleanup;
}if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) { ... }
if (verbose != 0) {
mbedtls_printf("passed\n");
}if (verbose != 0) { ... }
MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&X, &A, &N));
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&U, 16,
"003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
"C3DBA76456363A10869622EAC2DD84EC" \
"C5B8A74DAC4D09E03B5E0BE779F2DF61"));
if (verbose != 0) {
mbedtls_printf(" MPI test #4 (inv_mod): ");
}if (verbose != 0) { ... }
if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) {
if (verbose != 0) {
mbedtls_printf("failed\n");
}if (verbose != 0) { ... }
ret = 1;
goto cleanup;
}if (mbedtls_mpi_cmp_mpi(&X, &U) != 0) { ... }
if (verbose != 0) {
mbedtls_printf("passed\n");
}if (verbose != 0) { ... }
if (verbose != 0) {
mbedtls_printf(" MPI test #5 (simple gcd): ");
}if (verbose != 0) { ... }
for (i = 0; i < GCD_PAIR_COUNT; i++) {
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&X, gcd_pairs[i][0]));
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&Y, gcd_pairs[i][1]));
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(&A, &X, &Y));
if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) {
if (verbose != 0) {
mbedtls_printf("failed at %d\n", i);
}if (verbose != 0) { ... }
ret = 1;
goto cleanup;
}if (mbedtls_mpi_cmp_int(&A, gcd_pairs[i][2]) != 0) { ... }
}for (i = 0; i < GCD_PAIR_COUNT; i++) { ... }
if (verbose != 0) {
mbedtls_printf("passed\n");
}if (verbose != 0) { ... }
cleanup:
if (ret != 0 && verbose != 0) {
mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
}if (ret != 0 && verbose != 0) { ... }
mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N); mbedtls_mpi_free(&X);
mbedtls_mpi_free(&Y); mbedtls_mpi_free(&U); mbedtls_mpi_free(&V);
if (verbose != 0) {
mbedtls_printf("\n");
}if (verbose != 0) { ... }
return ret;
}mbedtls_mpi_self_test (int verbose) { ... }
/* ... */
#endif
/* ... */
#endif