wolfssl-sys 4.0.0

System bindings for WolfSSL
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
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
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
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
2125
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
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
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
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
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
2412
2413
2414
2415
2416
2417
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
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
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
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
2532
2533
2534
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
2575
2576
2577
2578
2579
2580
2581
/* ssl_bn.c
 *
 * Copyright (C) 2006-2026 wolfSSL Inc.
 *
 * This file is part of wolfSSL.
 *
 * wolfSSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * wolfSSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
 */

#include <wolfssl/wolfcrypt/libwolfssl_sources.h>

#include <wolfssl/internal.h>
#ifndef WC_NO_RNG
    #include <wolfssl/wolfcrypt/random.h>
#endif

#if !defined(WOLFSSL_SSL_BN_INCLUDED)
    #ifndef WOLFSSL_IGNORE_FILE_WARN
        #warning ssl_bn.c does not need to be compiled separately from ssl.c
    #endif
#else

/* Check on validity of big number.
 *
 * Used for parameter validation.
 *
 * @param [in] bn  Big number.
 * @return 1 when bn is not NULL and internal representation is not NULL.
 * @return 0 otherwise.
 */
#define BN_IS_NULL(bn) (((bn) == NULL) || ((bn)->internal == NULL))

/*******************************************************************************
 * Constructor/Destructor/Initializer APIs
 ******************************************************************************/

#if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
/* Set big number to be negative.
 *
 * @param [in, out] bn   Big number to make negative.
 * @param [in]      neg  Whether number is negative.
 * @return  1 on success.
 * @return  -1 when bn or internal representation of bn is NULL.
 */
static int wolfssl_bn_set_neg(WOLFSSL_BIGNUM* bn, int neg)
{
    int ret = 1;

    if (BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        ret = WOLFSSL_FATAL_ERROR;
    }
#if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_INT_NEGATIVE)
    else if (neg) {
        mp_setneg((mp_int*)bn->internal);
    }
    else {
        ((mp_int*)bn->internal)->sign = MP_ZPOS;
    }
#endif

    return ret;
}
#endif /* OPENSSL_EXTRA && !NO_ASN */

#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
/* Get the internal representation value into an MP integer.
 *
 * When calling wolfssl_bn_get_value, mpi should be cleared by caller if no
 * longer used. ie mp_free(mpi). This is to free data when fastmath is
 * disabled since a copy of mpi is made by this function and placed into bn.
 *
 * @param [in]      bn   Big number to copy value from.
 * @param [in, out] mpi  MP integer to copy into.
 * @return  1 on success.
 * @return  -1 when bn or internal representation of bn is NULL.
 * @return  -1 when mpi is NULL.
 * @return  -1 when copy fails.
 */
int wolfssl_bn_get_value(WOLFSSL_BIGNUM* bn, mp_int* mpi)
{
    int ret = 1;

    WOLFSSL_MSG("Entering wolfssl_bn_get_value_mp");

    /* Validate parameters. */
    if (BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        ret = WOLFSSL_FATAL_ERROR;
    }
    else if (mpi == NULL) {
        WOLFSSL_MSG("mpi NULL error");
        ret = WOLFSSL_FATAL_ERROR;
    }

    /* Copy the internal representation into MP integer. */
    if ((ret == 1) && mp_copy((mp_int*)bn->internal, mpi) != MP_OKAY) {
        WOLFSSL_MSG("mp_copy error");
        ret = WOLFSSL_FATAL_ERROR;
    }

    return ret;
}

/* Set big number internal representation to value in mpi.
 *
 * Will create a new big number if bn points to NULL.
 *
 * When calling wolfssl_bn_set_value, mpi should be cleared by caller if no
 * longer used. ie mp_free(mpi). This is to free data when fastmath is
 * disabled since a copy of mpi is made by this function and placed into bn.
 *
 * @param [in, out] bn   Pointer to big number to have value.
 * @param [in]      mpi  MP integer with value to set.
 * @return  1 on success.
 * @return  -1 when mpi or bn is NULL.
 * @return  -1 when creating a new big number fails.
 * @return  -1 when copying MP integer fails.
 */
int wolfssl_bn_set_value(WOLFSSL_BIGNUM** bn, mp_int* mpi)
{
    int ret = 1;
    WOLFSSL_BIGNUM* a = NULL;

#ifdef WOLFSSL_DEBUG_OPENSSL
    WOLFSSL_ENTER("wolfssl_bn_set_value");
#endif

    /* Validate parameters. */
    if ((bn == NULL) || (mpi == NULL)) {
        WOLFSSL_MSG("mpi or bn NULL error");
        ret = WOLFSSL_FATAL_ERROR;
    }

    /* Allocate a new big number if one not passed in. */
    if ((ret == 1) && (*bn == NULL)) {
        a = wolfSSL_BN_new();
        if (a == NULL) {
            WOLFSSL_MSG("wolfssl_bn_set_value alloc failed");
            ret = WOLFSSL_FATAL_ERROR;
        }
        *bn = a;
    }

    /* Copy MP integer value into internal representation of big number. */
    if ((ret == 1) && (mp_copy(mpi, (mp_int*)((*bn)->internal)) != MP_OKAY)) {
        WOLFSSL_MSG("mp_copy error");
        ret = WOLFSSL_FATAL_ERROR;
    }

    /* Dispose of any allocated big number on error. */
    if ((ret == -1) && (a != NULL)) {
        wolfSSL_BN_free(a);
        *bn = NULL;
    }
    return ret;
}

/* Initialize a big number.
 *
 * Assumes bn is not NULL.
 *
 * @param [in, out] bn  Big number to initialize.
 */
static void wolfssl_bn_init(WOLFSSL_BIGNUM* bn)
{
    /* Clear fields of big number. */
    XMEMSET(bn, 0, sizeof(WOLFSSL_BIGNUM));
    /* Initialization only fails when passed NULL. */
    (void)mp_init(&bn->mpi);
    /* Set an internal representation. */
    bn->internal = &bn->mpi;
}

/* Create a new big number.
 *
 * @return  An allocated and initialized big number on success.
 * @return  NULL on failure.
 */
WOLFSSL_BIGNUM* wolfSSL_BN_new(void)
{
    WOLFSSL_BIGNUM* bn = NULL;

#ifdef WOLFSSL_DEBUG_OPENSSL
    WOLFSSL_ENTER("wolfSSL_BN_new");
#endif

    /* Allocate memory for big number. */
    bn = (WOLFSSL_BIGNUM*)XMALLOC(sizeof(WOLFSSL_BIGNUM), NULL,
        DYNAMIC_TYPE_BIGINT);
    if (bn == NULL) {
        WOLFSSL_MSG("wolfSSL_BN_new malloc WOLFSSL_BIGNUM failure");
    }
    else {
        /* Initialize newly allocated object. */
        wolfssl_bn_init(bn);
    }

    return bn;
}

#if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
/* Initialize a big number.
 *
 * Call this instead of wolfSSL_BN_new() and wolfSSL_BN_free().
 *
 * Do not call this API after wolfSSL_BN_new() or wolfSSL_BN_init().
 *
 * @param [in, out] bn  Big number to initialize.
 */
void wolfSSL_BN_init(WOLFSSL_BIGNUM* bn)
{
#ifdef WOLFSSL_DEBUG_OPENSSL
    WOLFSSL_ENTER("wolfSSL_BN_init");
#endif

    /* Validate parameter. */
    if (bn != NULL) {
        /* Initialize big number object. */
        wolfssl_bn_init(bn);
    }
}
#endif

/* Dispose of big number.
 *
 * bn is unusable after this call.
 *
 * @param [in, out] bn  Big number to free.
 */
void wolfSSL_BN_free(WOLFSSL_BIGNUM* bn)
{
#ifdef WOLFSSL_DEBUG_OPENSSL
    WOLFSSL_ENTER("wolfSSL_BN_free");
#endif

    /* Validate parameter. */
    if (bn != NULL) {
        /* Cleanup any internal representation. */
        if (bn->internal != NULL) {
            /* Free MP integer. */
            mp_free(&bn->mpi);
        }
        /* Dispose of big number object. */
        XFREE(bn, NULL, DYNAMIC_TYPE_BIGINT);
        /* bn = NULL, don't try to access or double free it */
    }
}

/* Zeroize and dispose of big number.
 *
 * bn is unusable after this call.
 *
 * @param [in, out] bn  Big number to clear and free.
 */
void wolfSSL_BN_clear_free(WOLFSSL_BIGNUM* bn)
{
#ifdef WOLFSSL_DEBUG_OPENSSL
    WOLFSSL_ENTER("wolfSSL_BN_clear_free");
#endif

    /* Validate parameter. */
    if (bn != NULL) {
        /* Check for internal representation. */
        if (bn->internal != NULL) {
            /* Zeroize MP integer. */
            mp_forcezero((mp_int*)bn->internal);
        }
        /* Dispose of big number. */
        wolfSSL_BN_free(bn);
    }
}

/* Zeroize big number.
 *
 * @param [in, out] bn  Big number to clear.
 */
void wolfSSL_BN_clear(WOLFSSL_BIGNUM* bn)
{
#ifdef WOLFSSL_DEBUG_OPENSSL
    WOLFSSL_ENTER("wolfSSL_BN_clear");
#endif

    /* Validate parameter. */
    if (!BN_IS_NULL(bn)) {
        /* Zeroize MP integer. */
        mp_forcezero((mp_int*)bn->internal);
    }
}
#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */

#ifdef OPENSSL_EXTRA

static WOLFSSL_BIGNUM* bn_one = NULL;

/* Return a big number with the value of one.
 *
 * @return  A big number with the value one on success.
 * @return  NULL on failure.
 */
const WOLFSSL_BIGNUM* wolfSSL_BN_value_one(void)
{
    WOLFSSL_BIGNUM* one;

    WOLFSSL_ENTER("wolfSSL_BN_value_one");

    /* Get the global object. */
    one = bn_one;
    /* Create a new big number if global not set. */
    if ((one == NULL) && ((one = wolfSSL_BN_new()) != NULL)) {
        /* Set internal representation to have a value of 1. */
        if (mp_set_int((mp_int*)one->internal, 1) != MP_OKAY) {
            /* Dispose of big number on error. */
            wolfSSL_BN_free(one);
            one = NULL;
        }
        else
    #ifndef SINGLE_THREADED
        /* Ensure global has not been set by another thread. */
        if (bn_one == NULL)
    #endif
        {
            /* Set this big number as the global. */
            bn_one = one;
        }
    #ifndef SINGLE_THREADED
        /* Check if another thread has set the global. */
        if (bn_one != one) {
            /* Dispose of this big number and return the global.  */
            wolfSSL_BN_free(one);
            one = bn_one;
        }
    #endif
    }

    return one;
}

static void wolfSSL_BN_free_one(void) {
    wolfSSL_BN_free(bn_one);
    bn_one = NULL;
}

/* Create a new big number with the same value as the one passed in.
 *
 * @param [in] bn  Big number to duplicate.
 * @return  Big number on success.
 * @return  NULL when bn or internal representation of bn is NULL.
 * @return  NULL when creating a new big number fails.
 * @return  NULL when copying the internal representation fails.
 */
WOLFSSL_BIGNUM* wolfSSL_BN_dup(const WOLFSSL_BIGNUM* bn)
{
    int err = 0;
    WOLFSSL_BIGNUM* ret = NULL;

    WOLFSSL_ENTER("wolfSSL_BN_dup");

    /* Validate parameter. */
    if (BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        err = 1;
    }

    /* Create a new big number to return. */
    if ((!err) && ((ret = wolfSSL_BN_new()) == NULL)) {
        WOLFSSL_MSG("bn new error");
        err = 1;
    }

    if (!err) {
        err = (wolfSSL_BN_copy(ret, bn) == NULL);
    }

    if (err) {
         /* Dispose of dynamically allocated data. */
         wolfSSL_BN_free(ret);
         ret = NULL;
    }
    return ret;
}

/* Copy value from bn into another r.
 *
 * @param [in, out] r   Big number to copy into.
 * @param [in]      bn  Big number to copy from.
 * @return  Big number copied into on success.
 * @return  NULL when r or bn is NULL.
 * @return  NULL when copying fails.
 */
WOLFSSL_BIGNUM* wolfSSL_BN_copy(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* bn)
{
    WOLFSSL_ENTER("wolfSSL_BN_copy");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(bn)) {
        WOLFSSL_MSG("r or bn NULL error");
        r = NULL;
    }

    /* Copy the value in. */
    if ((r != NULL) && mp_copy((mp_int*)bn->internal, (mp_int*)r->internal) !=
            MP_OKAY) {
        WOLFSSL_MSG("mp_copy error");
        r = NULL;
    }

    if (r != NULL) {
        /* Copy other fields in a big number. */
        r->neg = bn->neg;
    }

    return r;
}


/*******************************************************************************
 * Encode/Decode APIs.
 ******************************************************************************/

/* Encode the number is a big-endian byte array.
 *
 * Assumes byte array is large enough to hold encoding when not NULL.
 * Use NULL for byte array to get length.
 *
 * Return compliant with OpenSSL.
 *
 * @param [in]  bn  Big number to reduced
 * @param [out] r   Byte array to encode into. May be NULL.
 * @return  Length of big number in bytes on success.
 * @return  -1 when bn is NULL or encoding fails.
 */
int wolfSSL_BN_bn2bin(const WOLFSSL_BIGNUM* bn, unsigned char* r)
{
    int ret;

    WOLFSSL_ENTER("wolfSSL_BN_bn2bin");

    /* Validate parameters. */
    if (BN_IS_NULL(bn)) {
        WOLFSSL_MSG("NULL bn error");
        ret = WOLFSSL_FATAL_ERROR;
    }
    else {
        /* Get the length of the encoding. */
        ret = mp_unsigned_bin_size((mp_int*)bn->internal);
        /* Encode if byte array supplied. */
        if ((r != NULL) && (mp_to_unsigned_bin((mp_int*)bn->internal, r) !=
                MP_OKAY)) {
            WOLFSSL_MSG("mp_to_unsigned_bin error");
            ret = WOLFSSL_FATAL_ERROR;
        }
    }

    return ret;
}


/* Return a big number with value of the decoding of the big-endian byte array.
 *
 * Returns ret when not NULL.
 * Allocates a big number when ret is NULL.
 * Assumes str is not NULL.
 *
 * @param [in]      data  Byte array to decode.
 * @param [in]      len   Number of bytes in byte array.
 * @param [in, out] ret   Big number to reduced. May be NULL.
 * @return  A big number on success.
 * @return  NULL on failure.
 */
WOLFSSL_BIGNUM* wolfSSL_BN_bin2bn(const unsigned char* data, int len,
    WOLFSSL_BIGNUM* ret)
{
    WOLFSSL_BIGNUM* bn = NULL;

    WOLFSSL_ENTER("wolfSSL_BN_bin2bn");

    /* Validate parameters. */
    if (len < 0) {
        ret = NULL;
    }
    /* Allocate a new big number when ret is NULL. */
    else if (ret == NULL) {
        ret = wolfSSL_BN_new();
        bn = ret;
    }

    /* Check ret is usable. */
    if (ret != NULL) {
        /* Check internal representation is usable. */
        if (ret->internal == NULL) {
            ret = NULL;
        }
        else if (data != NULL) {
            /* Decode into big number. */
            if (mp_read_unsigned_bin((mp_int*)ret->internal, data, (word32)len)
                    != 0) {
                WOLFSSL_MSG("mp_read_unsigned_bin failure");
                /* Don't return anything on failure. bn will be freed if set. */
                ret = NULL;
            }
            else {
                /* Don't free bn as we are returning it. */
                bn = NULL;
            }
        }
        else if (data == NULL) {
            wolfSSL_BN_zero(ret);
            /* Don't free bn as we are returning it. */
            bn = NULL;
        }
    }

    /* Dispose of allocated BN not being returned. */
    wolfSSL_BN_free(bn);

    return ret;
}

/* Encode the big number value into a string, of the radix, that is allocated.
 *
 * @param [in] bn     Big number to encode.
 * @param [in] radix  Radix to encode to.
 * @return  String with encoding on success.
 * @return  NULL when bn or internal representation of bn is NULL.
 * @return  NULL on failure.
 */
static char* wolfssl_bn_bn2radix(const WOLFSSL_BIGNUM* bn, int radix)
{
    int err = 0;
    int len = 0;
    char* str = NULL;


    /* Validate parameter. */
    if (BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        err = 1;
    }

    /* Determine length of encoding. */
    if ((!err) && (mp_radix_size((mp_int*)bn->internal, radix, &len) !=
            MP_OKAY)) {
        WOLFSSL_MSG("mp_radix_size failure");
        err = 1;
    }

    if (!err) {
        /* Allocate string. */
        str = (char*)XMALLOC((size_t)len, NULL, DYNAMIC_TYPE_OPENSSL);
        if (str == NULL) {
            WOLFSSL_MSG("BN_bn2hex malloc string failure");
            err = 1;
        }
    }

    /* Encode into string using wolfCrypt. */
    if ((!err) && (mp_toradix((mp_int*)bn->internal, str, radix) != MP_OKAY)) {
        err = 1;
    }

    if (err) {
        /* Dispose of dynamically allocated data. */
        XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
        /* Don't return freed string. */
        str = NULL;
    }
    return str;
}

/* Encode the big number value into hex string that is allocated.
 *
 * @param [in] bn  Big number to encode.
 * @return  String with encoding on success.
 * @return  NULL when bn or internal representation of bn is NULL.
 * @return  NULL on failure.
 */
char* wolfSSL_BN_bn2hex(const WOLFSSL_BIGNUM *bn)
{
    WOLFSSL_ENTER("wolfSSL_BN_bn2hex");
    return wolfssl_bn_bn2radix(bn, MP_RADIX_HEX);
}

/* Decode string of a radix into a big number.
 *
 * If bn is a pointer to NULL, then a new big number is allocated and assigned.
 *
 * Note on use: this function expects str to be an even length. It is
 * converting pairs of bytes into 8-bit values. As an example, the RSA
 * public exponent is commonly 0x010001. To get it to convert, you need
 * to pass in the string "010001", it will fail if you use "10001". This
 * is an affect of how Base16_Decode() works.
 *
 * @param [in, out] bn     Pointer to a big number. May point to NULL.
 * @param [in]      str    Hex string to decode.
 * @param [in]      radix  Radix to decode from.
 * @return  1 on success.
 * @return  0 when bn or str is NULL or str is zero length.
 * @return  0 when creating a new big number fails.
 * @return  0 when decoding fails.
 */
static int wolfssl_bn_radix2bn(WOLFSSL_BIGNUM** bn, const char* str, int radix)
{
    int ret = 1;
    WOLFSSL_BIGNUM* a = NULL;

    /* Validate parameters. */
    if ((bn == NULL) || (str == NULL) || (str[0] == '\0')) {
        WOLFSSL_MSG("Bad function argument");
        ret = 0;
    }
    /* Check if we have a big number to decode into. */
    if ((ret == 1) && (*bn == NULL)) {
        /* Allocate a new big number. */
        a = wolfSSL_BN_new();
        if (a == NULL) {
            WOLFSSL_MSG("BN new failed");
            ret = 0;
        }
        /* Return allocated big number. */
        *bn = a;
    }
    /* Decode hex string into internal representation. */
    if ((ret == 1) && (mp_read_radix((mp_int*)(*bn)->internal, str, radix) !=
            MP_OKAY)) {
        WOLFSSL_MSG("Bad read_radix error");
        ret = 0;
    }

    if ((ret == 0) && (a != NULL)) {
        /* Dispose of big number. */
        wolfSSL_BN_free(a);
        /* Don't return freed big number. */
        *bn = NULL;
    }
    return ret;
}

/* Decode hex string into a big number.
 *
 * If bn is a pointer to NULL, then a new big number is allocated and assigned.
 *
 * Note on use: this function expects str to be an even length. It is
 * converting pairs of bytes into 8-bit values. As an example, the RSA
 * public exponent is commonly 0x010001. To get it to convert, you need
 * to pass in the string "010001", it will fail if you use "10001". This
 * is an affect of how Base16_Decode() works.
 *
 * @param [in, out] bn   Pointer to a big number. May point to NULL.
 * @param [in]      str  Hex string to decode.
 * @return  1 on success.
 * @return  0 when bn or str is NULL or str is zero length.
 * @return  0 when creating a new big number fails.
 * @return  0 when decoding fails.
 */
int wolfSSL_BN_hex2bn(WOLFSSL_BIGNUM** bn, const char* str)
{
    WOLFSSL_ENTER("wolfSSL_BN_hex2bn");
    return wolfssl_bn_radix2bn(bn, str, MP_RADIX_HEX);
}

#if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
/* Encode big number into decimal string.
 *
 * @param [in] bn  Big number to encode.
 * @return  String with encoding on success.
 * @return  NULL when bn or internal representation of bn is NULL.
 * @return  NULL on failure.
 */
char* wolfSSL_BN_bn2dec(const WOLFSSL_BIGNUM *bn)
{
    WOLFSSL_ENTER("wolfSSL_BN_bn2hex");
    return wolfssl_bn_bn2radix(bn, MP_RADIX_DEC);
}
#else
/* Encode big number into decimal string.
 *
 * @param [in] bn  Big number to encode.
 * @return  NULL as implementation not available.
 */
char* wolfSSL_BN_bn2dec(const WOLFSSL_BIGNUM* bn)
{
    (void)bn;
    WOLFSSL_ENTER("wolfSSL_BN_bn2dec");
    return NULL;
}
#endif /* defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) */


#ifndef NO_RSA
/* Decode hex string into a big number.
 *
 * If bn is a pointer to NULL, then a new big number is allocated and assigned.
 *
 * Note on use: this function expects str to be an even length. It is
 * converting pairs of bytes into 8-bit values. As an example, the RSA
 * public exponent is commonly 0x010001. To get it to convert, you need
 * to pass in the string "010001", it will fail if you use "10001". This
 * is an affect of how Base16_Decode() works.
 *
 * @param [in, out] bn   Pointer to a big number. May point to NULL.
 * @param [in]      str  Hex string to decode.
 * @return  1 on success.
 * @return  0 when bn or str is NULL or str is zero length.
 * @return  0 when creating a new big number fails.
 * @return  0 when decoding fails.
 */
int wolfSSL_BN_dec2bn(WOLFSSL_BIGNUM** bn, const char* str)
{
    WOLFSSL_ENTER("wolfSSL_BN_bn2dec");
    return wolfssl_bn_radix2bn(bn, str, MP_RADIX_DEC);
}
#else
/* Decode hex string into a big number.
 *
 * @param [in, out] bn   Pointer to a big number. May point to NULL.
 * @param [in]      str  Hex string to decode.
 * @return  0 as implementation not available..
 */
int wolfSSL_BN_dec2bn(WOLFSSL_BIGNUM** bn, const char* str)
{
    (void)bn;
    (void)str;
    WOLFSSL_ENTER("wolfSSL_BN_bn2dec");
    return 0;
}
#endif

/*******************************************************************************
 * Get/Set APIs
 ******************************************************************************/

/* Calculate the number of bytes need to represent big number.
 *
 * Return compliant with OpenSSL.
 *
 * @param [in] bn  Big number to use.
 * @return  Size of BIGNUM in bytes on success.
 * @return  0 when bn or internal representation of bn is NULL.
 */
int wolfSSL_BN_num_bytes(const WOLFSSL_BIGNUM* bn)
{
    int ret;

    WOLFSSL_ENTER("wolfSSL_BN_num_bytes");

    /* Validate parameter. */
    if (BN_IS_NULL(bn)) {
        ret = 0;
    }
    else {
        /* Get size from wolfCrypt. */
        ret = mp_unsigned_bin_size((mp_int*)bn->internal);
    }

    return ret;
}

/* Calculate the number of bits need to represent big number.
 *
 * Return compliant with OpenSSL.
 *
 * @param [in] bn  Big number to use.
 * @return  Size of BIGNUM in bits on success.
 * @return  0 when bn or internal representation of bn is NULL.
 */
int wolfSSL_BN_num_bits(const WOLFSSL_BIGNUM* bn)
{
    int ret;

    WOLFSSL_ENTER("wolfSSL_BN_num_bits");

    /* Validate parameter. */
    if (BN_IS_NULL(bn)) {
        ret = 0;
    }
    else {
        /* Get size from wolfCrypt. */
        ret = mp_count_bits((mp_int*)bn->internal);
    }

    return ret;
}

/* Indicates whether a big number is negative.
 *
 * @param [in] bn  Big number to use.
 * @return  1 when number is negative.
 * @return  0 when number is positive.
 * @return  0 when bn is NULL.
 */
int wolfSSL_BN_is_negative(const WOLFSSL_BIGNUM* bn)
{
    int ret;

    /* Validate parameter. */
    if (BN_IS_NULL(bn)) {
        ret = 0;
    }
    else {
        /* Check sign with wolfCrypt. */
        ret = mp_isneg((mp_int*)bn->internal);
    }

    return ret;
}

/* Indicates whether a big number is odd.
 *
 * Return compliant with OpenSSL.
 *
 * @param [in] bn  Big number to use.
 * @return  1 when number is odd.
 * @return  0 when number is even.
 * @return  0 when bn or internal representation of bn is NULL.
 */
int wolfSSL_BN_is_odd(const WOLFSSL_BIGNUM* bn)
{
    int ret;

    WOLFSSL_ENTER("wolfSSL_BN_is_odd");

    /* Validate parameter. */
    if (BN_IS_NULL(bn)) {
        ret = 0;
    }
    else {
        /* wolfCrypt checks whether value is odd. */
        ret = (mp_isodd((mp_int*)bn->internal) == MP_YES);
    }

    return ret;
}

#ifndef NO_WOLFSSL_STUB
/* Mask the lowest n bits.
 *
 * TODO: mp_mod_2d()
 *
 * Return compliant with OpenSSL.
 *
 * @param [in, out] bn  Big number to operation on.
 * @param [in]      n   Number of bits.
 * @return  0 on failure.
 */
int wolfSSL_mask_bits(WOLFSSL_BIGNUM* bn, int n)
{
    (void)bn;
    (void)n;
    WOLFSSL_ENTER("wolfSSL_BN_mask_bits");
    WOLFSSL_STUB("BN_mask_bits");
    return 0;
}
#endif

/* Set a bit of the value in a big number.
 *
 * Return code compliant with OpenSSL.
 *
 * @param [in, out] bn  Big number to modify.
 * @return  1 on success.
 * @return  0 when bn or internal representation of bn is NULL.
 * @return  0 when failed to set bit.
 */
int wolfSSL_BN_set_bit(WOLFSSL_BIGNUM* bn, int n)
{
    int ret = 1;

    if (BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        ret = 0;
    }
    else if (mp_set_bit((mp_int*)bn->internal, n) != MP_OKAY) {
        WOLFSSL_MSG("mp_set_bit error");
        ret = 0;
    }

    return ret;
}

/* Clear a bit of the value in a big number.
 *
 * Return code compliant with OpenSSL.
 *
 * @param [in] bn  Big number to check.
 * @param [in] n   Inidex of bit to check.
 * @return  1 on success.
 * @return  0 when bn or internal representation of bn is NULL.
 * @return  0 when failed to clear bit.
 */
int wolfSSL_BN_clear_bit(WOLFSSL_BIGNUM* bn, int n)
{
    int ret = 1;
    WC_DECLARE_VAR(tmp, mp_int, 1, 0);

    /* Validate parameters. */
    if (BN_IS_NULL(bn) || (n < 0)) {
        WOLFSSL_MSG("bn NULL error");
        ret = 0;
    }
    /* Check if bit is set to clear. */
    if ((ret == 1) && (mp_is_bit_set((mp_int*)bn->internal, n))) {
        /* Allocate a new MP integer to hold bit to clear. */
        WC_ALLOC_VAR_EX(tmp, mp_int, 1, NULL, DYNAMIC_TYPE_BIGINT, ret=0);
        if (ret == 1) {
            /* Reset new MP integer. */
            XMEMSET(tmp, 0, sizeof(mp_int));
            if (mp_init(tmp) != MP_OKAY) {
                ret = 0;
            }
        }
        /* Set the bit to clear into temporary MP integer. */
        if ((ret == 1) && (mp_set_bit(tmp, n) != MP_OKAY)) {
            ret = 0;
        }
        /* Clear bit by sutraction. */
        if ((ret == 1) && (mp_sub((mp_int*)bn->internal, tmp,
                (mp_int*)bn->internal) != MP_OKAY)) {
            ret = 0;
        }

        /* Free any dynamic memory in MP integer. */
        mp_clear(tmp);
        WC_FREE_VAR_EX(tmp, NULL, DYNAMIC_TYPE_BIGINT);
    }

    return ret;
}

/* Returns whether the bit is set in the value of the big number.
 *
 * When bn is NULL, returns 0.
 *
 * Return code compliant with OpenSSL.
 *
 * @param [in] bn  Big number to check.
 * @param [in] n   Inidex of bit to check.
 * @return  1 if bit set.
 * @return  0 otherwise.
 */
int wolfSSL_BN_is_bit_set(const WOLFSSL_BIGNUM* bn, int n)
{
    int ret;

    /* Check for big number value. */
    if (BN_IS_NULL(bn) || (n < 0)) {
        WOLFSSL_MSG("bn NULL error");
        ret = 0;
    }
    else {
        /* Set bit with wolfCrypt. */
        ret = mp_is_bit_set((mp_int*)bn->internal, (mp_digit)n);
    }

    return ret;
}

/* Set the big number to the value 0.
 *
 * @param [in, out] bn  Big number to use.
 */
void wolfSSL_BN_zero(WOLFSSL_BIGNUM* bn)
{
    /* Validate parameter. */
    if (!BN_IS_NULL(bn)) {
        /* Set wolfCrypt representation to 0. */
        mp_zero((mp_int*)bn->internal);
    }
}

/* Set the big number to the value 0.
 *
 * @param [in, out] bn  Big number to use.
 * @return  1 on success.
 * @return  0 when bn or internal representation of bn is NULL.
 */
int wolfSSL_BN_one(WOLFSSL_BIGNUM* bn)
{
    int ret;

    /* Validate parameter. */
    if (BN_IS_NULL(bn)) {
        ret = 0;
    }
    else {
        /* Set to value one. */
        ret = wolfSSL_BN_set_word(bn, 1);
    }

    return ret;
}

/* Get the value of the MP integer as a word.
 *
 * Assumes the MP integer value will fit in a word.
 *
 * @param [in] mp  MP integer.
 * @return  Value of MP integer as an unsigned long.
 */
static WOLFSSL_BN_ULONG wolfssl_bn_get_word_1(mp_int *mp) {
#if DIGIT_BIT >= (SIZEOF_LONG * CHAR_BIT)
    return (WOLFSSL_BN_ULONG)mp->dp[0];
#else
    WOLFSSL_BN_ULONG ret = 0UL;
    unsigned int i;

    for (i = 0; i < (unsigned int)mp->used; ++i) {
        ret |= ((WOLFSSL_BN_ULONG)mp->dp[i]) << (DIGIT_BIT * i);
    }

    return ret;
#endif
}

/* Return the value of big number as an unsigned long if possible.
 *
 * @param [in] bn  Big number to get value from.
 * @return  Value or 0xFFFFFFFFL if bigger than unsigned long.
 */
WOLFSSL_BN_ULONG wolfSSL_BN_get_word(const WOLFSSL_BIGNUM* bn)
{
    WOLFSSL_BN_ULONG ret;

    WOLFSSL_ENTER("wolfSSL_BN_get_word");

    /* Validate parameter. */
    if (BN_IS_NULL(bn)) {
        WOLFSSL_MSG("Invalid argument");
        ret = 0;
    }
    /* Check whether big number is to fit in an unsigned long. */
    else if (wolfSSL_BN_num_bytes(bn) > (int)sizeof(unsigned long)) {
        WOLFSSL_MSG("bignum is larger than unsigned long");
        ret = WOLFSSL_BN_MAX_VAL;
    }
    else {
        /* Get the word from the internal representation. */
        ret = wolfssl_bn_get_word_1((mp_int*)bn->internal);
    }

    return ret;
}

/* Set the big number to the value in the word.
 *
 * Return code compliant with OpenSSL.
 *
 * @param [in, out] bn  Big number to set.
 * @param [in       w   Word to set.
 * @return  1 on success.
 * @return  0 when bn is NULL or setting value failed.
 */
int wolfSSL_BN_set_word(WOLFSSL_BIGNUM* bn, unsigned long w)
{
    int ret = 1;

    WOLFSSL_ENTER("wolfSSL_BN_set_word");

    /* Validate parameters. */
    if (BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        ret = 0;
    }

    /* Set the word into the internal representation. */
    if ((ret == 1) && (mp_set_int((mp_int*)bn->internal, w) != MP_OKAY)) {
        WOLFSSL_MSG("mp_init_set_int error");
        ret = 0;
    }

    return ret;
}

/*******************************************************************************
 * Comparison APIs
 ******************************************************************************/

/* Compares two big numbers. a <=> b
 *
 * NULL equals NULL
 * NULL less than not NULL
 * not NULL greater than NULL.
 *
 * Return compliant with OpenSSL.
 *
 * @param [in] bn  First big number to compare.
 * @param [in] bn  Second big number to compare.
 * @return  -1 when a is less than b (a < b).
 * @return  0 when a is equal to b (a == b).
 * @return  1 when a is greater than b (a > b).
 * @return  0 when bn or internal representation of bn is NULL.
 */
int wolfSSL_BN_cmp(const WOLFSSL_BIGNUM* a, const WOLFSSL_BIGNUM* b)
{
    int ret;
    int bIsNull;

    WOLFSSL_ENTER("wolfSSL_BN_cmp");

    /* Must know whether b is NULL. */
    bIsNull = BN_IS_NULL(b);
    /* Check whether a is NULL. */
    if (BN_IS_NULL(a)) {
        if (bIsNull) {
            /* NULL equals NULL. */
            ret = 0;
        }
        else {
            ret = -1; /* NULL less than not NULL. */
        }
    }
    else if (bIsNull) {
        /* not NULL greater than NULL. */
        ret = 1;
    }
    else {
        PRAGMA_GCC_DIAG_PUSH
        PRAGMA_GCC("GCC diagnostic ignored \"-Wduplicated-branches\"")
        /* Compare big numbers with wolfCrypt. */
        ret = mp_cmp((mp_int*)a->internal, (mp_int*)b->internal);
        /* Convert wolfCrypt return value. */
        if (ret == MP_EQ) {
            ret = 0;
        }
        else if (ret == MP_GT) {
            ret = 1;
        }
        else if (ret == MP_LT) {
            ret = -1;
        }
        else {
            /* ignored warning here because the same return value
               was intentional */
            ret = WOLFSSL_FATAL_ERROR; /* also -1 */
        }
        PRAGMA_GCC_DIAG_POP
    }

    return ret;
}

/* Same as above, but compare absolute value. */
int wolfSSL_BN_ucmp(const WOLFSSL_BIGNUM* a, const WOLFSSL_BIGNUM* b)
{
    int ret = 0;
    int bIsNull;

    WOLFSSL_ENTER("wolfSSL_BN_ucmp");

    /* Must know whether b is NULL. */
    bIsNull = BN_IS_NULL(b);
    /* Check whether a is NULL. */
    if (BN_IS_NULL(a)) {
        if (bIsNull) {
            /* NULL equals NULL. */
            ret = 0;
        }
        else {
            ret = -1; /* NULL less than not NULL. */
        }
    }
    else if (bIsNull) {
        /* not NULL greater than NULL. */
        ret = 1;
    }
    else {
        /* Neither are NULL; copy to new instances and switch to positive if
         * required, compare, and then free.  Must copy because there is
         * possibility of switch to positive but they are declared const.
         * wolfssl_bn_set_neg() only returns -1 if the bn is NULL, but we
         * already check that so we can ignore the return code. Note for
         * wolfSSL_BN_is_negative if n=1 then set to positive. */
        WOLFSSL_BIGNUM* abs_a = wolfSSL_BN_dup(a);
        WOLFSSL_BIGNUM* abs_b = wolfSSL_BN_dup(b);

        if (abs_a == NULL || abs_b == NULL) {
            WOLFSSL_MSG("wolfSSL_BN_dup failed");
            wolfSSL_BN_free(abs_a);
            wolfSSL_BN_free(abs_b);
            return WOLFSSL_FATAL_ERROR;
        }

        if (wolfSSL_BN_is_negative(abs_a)) {
            wolfssl_bn_set_neg(abs_a, 1);
        }

        if (wolfSSL_BN_is_negative(abs_b)) {
            wolfssl_bn_set_neg(abs_b, 1);
        }

        ret = wolfSSL_BN_cmp(abs_a, abs_b);
        wolfSSL_BN_free(abs_a);
        wolfSSL_BN_free(abs_b);
    }
    return ret;
}

/* Indicates whether a big number is the value 0.
 *
 * Return compliant with OpenSSL.
 *
 * @param [in] bn  Big number to use.
 * @return  1 when number is zero.
 * @return  0 when number is not zero.
 * @return  0 when bn or internal representation of bn is NULL.
 */
int wolfSSL_BN_is_zero(const WOLFSSL_BIGNUM* bn)
{
    int ret;

    WOLFSSL_ENTER("wolfSSL_BN_is_zero");

    /* Validate parameter. */
    if (BN_IS_NULL(bn)) {
        ret = 0;
    }
    else {
        /* wolfCrypt checks whether value is 0. */
        ret = (mp_iszero((mp_int*)bn->internal) == MP_YES);
    }

    return ret;
}

/* Indicates whether a big number is the value 1.
 *
 * Return compliant with OpenSSL.
 *
 * @param [in] bn  Big number to use.
 * @return  1 when number is one.
 * @return  0 when number is not one.
 * @return  0 when bn or internal representation of bn is NULL.
 */
int wolfSSL_BN_is_one(const WOLFSSL_BIGNUM* bn)
{
    int ret;

    WOLFSSL_ENTER("wolfSSL_BN_is_one");

    /* Validate parameter. */
    if (BN_IS_NULL(bn)) {
        ret = 0;
    }
    else {
        /* wolfCrypt checks whether value is 1. */
        ret = (mp_cmp_d((mp_int*)bn->internal, 1) == MP_EQ);
    }

    return ret;
}

/* Indicates whether a big number is the value passed in.
 *
 * Return compliant with OpenSSL.
 *
 * @param [in] bn  Big number to use.
 * @return  1 when big number is the value.
 * @return  0 when big number is not the value.
 * @return  0 when bn or internal representation of bn is NULL.
 */
int wolfSSL_BN_is_word(const WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w)
{
    int ret;

    WOLFSSL_ENTER("wolfSSL_BN_is_word");

    /* Validate parameter. */
    if (BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        ret = 0;
    }
    else
#if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT)
    /* When value is greater than what can be stored in one digit - special
     * case. */
    if (w > (WOLFSSL_BN_ULONG)MP_MASK) {
        /* TODO: small stack */
        mp_int w_mp;

        /* Create a MP to hold the number. */
        if (mp_init(&w_mp) != MP_OKAY) {
            ret = 0;
        }
        /* Set the value - held in more than one digit. */
        else if (mp_set_int(&w_mp, w) != MP_OKAY) {
            ret = 0;
        }
        else {
            /* Compare MP representations. */
            ret = (mp_cmp((mp_int *)bn->internal, &w_mp) == MP_EQ);
            mp_free(&w_mp);
        }
    }
    else
#endif
    {
        /* wolfCrypt checks whether it is the value. */
        ret = (mp_isword((mp_int*)bn->internal, (mp_digit)w) == MP_YES);
    }

    return ret;
}


/*******************************************************************************
 * Word operation APIs.
 ******************************************************************************/

enum BN_WORD_OP {
    BN_WORD_ADD = 0,
    BN_WORD_SUB = 1,
    BN_WORD_MUL = 2,
    BN_WORD_DIV = 3,
    BN_WORD_MOD = 4
};

/* Helper function for word operations.
 *
 * @param [in, out] bn  Big number to operate on.
 * @param [in]      w   Word to operate with.
 * @param [in]      op  Operation to perform. See BN_WORD_OP for valid values.
 * @param [out]     mod_res Result of the modulo operation.
 * @return  1 on success.
 * @return  0 on failure.
 */
static int bn_word_helper(const WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w,
        enum BN_WORD_OP op, WOLFSSL_BN_ULONG* mod_res)
{
    int ret = 1;

    WOLFSSL_ENTER("bn_word_helper");

    /* Validate parameters. */
    if (ret == 1 && BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        ret = 0;
    }

    if (ret == 1) {
        int rc = MP_OKAY;
#if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT)
        /* When input 'w' is greater than what can be stored in one digit */
        if (w > (WOLFSSL_BN_ULONG)MP_MASK) {
            DECL_MP_INT_SIZE_DYN(w_mp, sizeof(WOLFSSL_BN_ULONG) * CHAR_BIT,
                                       sizeof(WOLFSSL_BN_ULONG) * CHAR_BIT);
            NEW_MP_INT_SIZE(w_mp, sizeof(WOLFSSL_BN_ULONG) * CHAR_BIT, NULL,
                    DYNAMIC_TYPE_TMP_BUFFER);
#ifdef MP_INT_SIZE_CHECK_NULL
            if (w_mp == NULL) {
                WOLFSSL_MSG("NEW_MP_INT_SIZE error");
                ret = 0;
            }
#endif
            if (ret == 1 && mp_set_int(w_mp, w) != MP_OKAY) {
                WOLFSSL_MSG("mp_set_int error");
                ret = 0;
            }
            if (ret == 1) {
                switch (op) {
                    case BN_WORD_ADD:
                        rc = mp_add((mp_int*)bn->internal, w_mp,
                                (mp_int*)bn->internal);
                        break;
                    case BN_WORD_SUB:
                        rc = mp_sub((mp_int*)bn->internal, w_mp,
                                (mp_int*)bn->internal);
                        break;
                    case BN_WORD_MUL:
                        rc = mp_mul((mp_int*)bn->internal, w_mp,
                                (mp_int*)bn->internal);
                        break;
                    case BN_WORD_DIV:
                        rc = mp_div((mp_int*)bn->internal, w_mp,
                                (mp_int*)bn->internal, NULL);
                        break;
                    case BN_WORD_MOD:
                        rc = mp_mod((mp_int*) bn->internal, w_mp,
                                w_mp);
                        if (rc == MP_OKAY && mod_res != NULL)
                            *mod_res = wolfssl_bn_get_word_1(w_mp);
                        break;
                    default:
                        rc = WOLFSSL_NOT_IMPLEMENTED;
                        break;
                }
            }
            FREE_MP_INT_SIZE(w_mp, NULL, DYNAMIC_TYPE_RSA);
        }
        else
#endif
        {
            switch (op) {
                case BN_WORD_ADD:
                    rc = mp_add_d((mp_int*)bn->internal, (mp_digit)w,
                            (mp_int*)bn->internal);
                    break;
                case BN_WORD_SUB:
                    rc = mp_sub_d((mp_int*)bn->internal, (mp_digit)w,
                            (mp_int*)bn->internal);
                    break;
                case BN_WORD_MUL:
                    rc = mp_mul_d((mp_int*)bn->internal, (mp_digit)w,
                            (mp_int*)bn->internal);
                    break;
                case BN_WORD_DIV:
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
/* copied from sp_int.h */
#if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
    defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \
    defined(WC_MP_TO_RADIX)
                    rc = mp_div_d((mp_int*)bn->internal, (mp_digit)w,
                            (mp_int*)bn->internal, NULL);
#else
                    rc = WOLFSSL_NOT_IMPLEMENTED;
#endif
#else
                    rc = WOLFSSL_NOT_IMPLEMENTED;
#endif
                    break;
                case BN_WORD_MOD:
                    {
                        mp_digit _mod_res;
                        rc = mp_mod_d((mp_int*) bn->internal, (mp_digit) w,
                                &_mod_res);
                        if (rc == MP_OKAY && mod_res != NULL)
                            *mod_res = (WOLFSSL_BN_ULONG)_mod_res;
                    }
                    break;
                default:
                    rc = WOLFSSL_NOT_IMPLEMENTED;
                    break;
            }
        }
        if (ret == 1 && rc != MP_OKAY) {
            WOLFSSL_MSG("mp word operation error or not implemented");
            ret = 0;
        }
    }

    WOLFSSL_LEAVE("bn_word_helper", ret);

    return ret;
}

/* Add a word to a big number.
 *
 * Return code compliant with OpenSSL.
 *
 * @param [in, out] bn   Big number to operate on.
 * @param [in]      w    Word to operate with.
 * @return  1 on success.
 * @return  0 in failure.
 */
int wolfSSL_BN_add_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w)
{
    int ret;

    WOLFSSL_ENTER("wolfSSL_BN_add_word");

    ret = bn_word_helper(bn, w, BN_WORD_ADD, NULL);

    WOLFSSL_LEAVE("wolfSSL_BN_add_word", ret);

    return ret;
}

/* Subtract a word from a big number.
 *
 * Return code compliant with OpenSSL.
 *
 * @param [in, out] bn   Big number to operate on.
 * @param [in]      w    Word to operate with.
 * @return  1 on success.
 * @return  0 in failure.
 */
int wolfSSL_BN_sub_word(WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w)
{
    int ret;

    WOLFSSL_ENTER("wolfSSL_BN_sub_word");

    ret = bn_word_helper(bn, w, BN_WORD_SUB, NULL);

    WOLFSSL_LEAVE("wolfSSL_BN_sub_word", ret);

    return ret;
}

int wolfSSL_BN_mul_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w)
{
    int ret;

    WOLFSSL_ENTER("wolfSSL_BN_mul_word");

    ret = bn_word_helper(bn, w, BN_WORD_MUL, NULL);

    WOLFSSL_LEAVE("wolfSSL_BN_mul_word", ret);

    return ret;
}


int wolfSSL_BN_div_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w)
{
    int ret;

    WOLFSSL_ENTER("wolfSSL_BN_div_word");

    ret = bn_word_helper(bn, w, BN_WORD_DIV, NULL);

    WOLFSSL_LEAVE("wolfSSL_BN_div_word", ret);

    return ret;
}

#if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
    !defined(NO_DSA))
/* Calculate bn modulo word w. bn % w
 *
 * Return code compliant with OpenSSL.
 *
 * @return  Word result on success
 * @return  -1 on error
 */
WOLFSSL_BN_ULONG wolfSSL_BN_mod_word(const WOLFSSL_BIGNUM *bn,
    WOLFSSL_BN_ULONG w)
{
    int ret;
    WOLFSSL_BN_ULONG res = 0;

    WOLFSSL_ENTER("wolfSSL_BN_mod_word");

    ret = bn_word_helper(bn, w, BN_WORD_MOD, &res);

    WOLFSSL_LEAVE("wolfSSL_BN_mod_word", ret);

    return ret == 1 ? res : (WOLFSSL_BN_ULONG)-1;
}
#endif /* WOLFSSL_KEY_GEN && (!NO_RSA || !NO_DH || !NO_DSA) */

/*******************************************************************************
 * Shift APIs
 ******************************************************************************/

#ifndef WOLFSSL_SP_MATH
/* Shift the value in bn left by n bits into r. r = bn << n
 *
 * Return code compliant with OpenSSL.
 *
 * @return  1 on success.
 * @return  0 when r or bn or internal representation is NULL.
 * @return  0 on failure.
 */
int wolfSSL_BN_lshift(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *bn, int n)
{
    int ret = 1;

    WOLFSSL_ENTER("wolfSSL_BN_lshift");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        ret = 0;
    }
    else if (n < 0) {
        WOLFSSL_MSG("n value error");
        ret = 0;
    }

    /* Use wolfCrypt perform operation. */
    if ((ret == 1) && (mp_mul_2d((mp_int*)bn->internal, n,
            (mp_int*)r->internal) != MP_OKAY)) {
        WOLFSSL_MSG("mp_mul_2d error");
        ret = 0;
    }

    return ret;
}

/* Shift the value in bn right by n bits into r. r = bn >> n
 *
 * Return code compliant with OpenSSL.
 *
 * @return  1 on success.
 * @return  0 when r or bn or internal representation is NULL.
 * @return  0 on failure.
 */
int wolfSSL_BN_rshift(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *bn, int n)
{
    int ret = 1;

    WOLFSSL_ENTER("wolfSSL_BN_rshift");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        ret = 0;
    }
    else if (n < 0) {
        WOLFSSL_MSG("n value error");
        ret = 0;
    }

    /* Use wolfCrypt perform operation. */
    if ((ret == 1) && (mp_div_2d((mp_int*)bn->internal, n, (mp_int*)r->internal,
            NULL) != MP_OKAY)) {
        WOLFSSL_MSG("mp_mul_2d error");
        ret = 0;
    }

    return ret;
}
#endif

/*******************************************************************************
 * Simple Math APIs
 ******************************************************************************/

/* Add a to b into r. r = a + b
 *
 * Return code compliant with OpenSSL.
 *
 * @param [out] r  Big number to put result into.
 * @param [in]  a  Big number to be added to.
 * @param [in]  b  Big number to add with.
 *
 * @return  1 on success.
 * @return  0 when r, a or b or internal representation is NULL.
 * @return  0 on failure.
 */
int wolfSSL_BN_add(WOLFSSL_BIGNUM *r, WOLFSSL_BIGNUM *a, WOLFSSL_BIGNUM *b)
{
    int ret = 1;

    WOLFSSL_ENTER("wolfSSL_BN_add");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
        WOLFSSL_MSG("bn NULL error");
        ret = 0;
    }

    /* Add the internal representations into internal representation. */
    if ((ret == 1) && (mp_add((mp_int*)a->internal, (mp_int*)b->internal,
            (mp_int*)r->internal) != MP_OKAY)) {
        WOLFSSL_MSG("mp_add_d error");
        ret = 0;
    }

    return ret;
}

/* Subtract a from b into r. r = a - b
 *
 * @param [out] r  Big number to put result into.
 * @param [in]  a  Big number to be subtracted from.
 * @param [in]  b  Big number to subtract with.
 *
 * @return  1 on success.
 * @return  0 when r, a or b is NULL.
 * @return  0 on failure.
 */
int wolfSSL_BN_sub(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* a,
    const WOLFSSL_BIGNUM* b)
{
    int ret = 1;

    WOLFSSL_ENTER("wolfSSL_BN_sub");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
        ret = 0;
    }

    /* Have wolfCrypt perform operation with internal representations. */
    if ((ret == 1) && (mp_sub((mp_int*)a->internal,(mp_int*)b->internal,
            (mp_int*)r->internal) != MP_OKAY)) {
        ret = 0;
    }

    WOLFSSL_LEAVE("wolfSSL_BN_sub mp_sub", ret);
    return ret;
}

/* Multiply a with b into r. r = a * b
 *
 * @param [out] r    Big number to put result into.
 * @param [in]  a    Big number to be multiplied.
 * @param [in]  b    Big number to multiply with.
 * @param [in]  ctx  BN context object. Unused.
 *
 * @return  1 on success.
 * @return  0 when r, a or b is NULL.
 * @return  0 when internal representation of r, a or b is NULL.
 * @return  0 on failure.
 */
int wolfSSL_BN_mul(WOLFSSL_BIGNUM *r, WOLFSSL_BIGNUM *a, WOLFSSL_BIGNUM *b,
    WOLFSSL_BN_CTX *ctx)
{
    int ret = 1;

    (void)ctx;

    WOLFSSL_ENTER("wolfSSL_BN_mul");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
        ret = 0;
    }

    /* Have wolfCrypt perform operation with internal representations. */
    if ((ret == 1) && (mp_mul((mp_int*)a->internal, (mp_int*)b->internal,
            (mp_int*)r->internal) != MP_OKAY)) {
        ret = 0;
    }

    WOLFSSL_LEAVE("wolfSSL_BN_mul", ret);
    return ret;
}

#ifndef WOLFSSL_SP_MATH
/* Divide a by b into dv and put remainder into rem. dv = a / b, rem = a % b
 *
 * @param [out] dv   Big number to put division result into.
 * @param [out] rem  Big number to put remainder into.
 * @param [in]  a    Big number to be divided.
 * @param [in]  b    Big number to divide with.
 * @param [in]  ctx  BN context object. Unused.
 *
 * @return  1 on success.
 * @return  0 when dv, rem, a or b is NULL.
 * @return  0 when internal representation of dv, rem, a or b is NULL.
 * @return  0 on failure.
 */
int wolfSSL_BN_div(WOLFSSL_BIGNUM* dv, WOLFSSL_BIGNUM* rem,
    const WOLFSSL_BIGNUM* a, const WOLFSSL_BIGNUM* d, WOLFSSL_BN_CTX* ctx)
{
    int ret = 1;
    WOLFSSL_BIGNUM* res = dv;

    /* BN context not needed. */
    (void)ctx;

    WOLFSSL_ENTER("wolfSSL_BN_div");

    if (BN_IS_NULL(res)) {
        res = wolfSSL_BN_new();
    }

    /* Validate parameters. */
    if (BN_IS_NULL(res) || BN_IS_NULL(rem) || BN_IS_NULL(a) || BN_IS_NULL(d)) {
        ret = 0;
    }

    /* Have wolfCrypt perform operation with internal representations. */
    if ((ret == 1) && (mp_div((mp_int*)a->internal, (mp_int*)d->internal,
            (mp_int*)res->internal, (mp_int*)rem->internal) != MP_OKAY)) {
        ret = 0;
    }

    if (res != dv)
        wolfSSL_BN_free(res);

    WOLFSSL_LEAVE("wolfSSL_BN_div", ret);
    return ret;
}
#endif

/* Calculate a mod b into r. r = a % b
 *
 * @param [out] r    Big number to put result into.
 * @param [in]  a    Big number to reduced
 * @param [in]  b    Big number to reduce with.
 * @param [in]  ctx  BN context object. Unused.
 *
 * @return  1 on success.
 * @return  0 when r, a or b is NULL.
 * @return  0 on failure.
 */
int wolfSSL_BN_mod(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* a,
                  const WOLFSSL_BIGNUM* b, const WOLFSSL_BN_CTX* c)
{
    int ret = 1;

    (void)c;

    WOLFSSL_ENTER("wolfSSL_BN_mod");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
        ret = 0;
    }

    /* Have wolfCrypt perform operation with internal representations. */
    if ((ret == 1) && (mp_mod((mp_int*)a->internal,(mp_int*)b->internal,
            (mp_int*)r->internal) != MP_OKAY)) {
        ret = 0;
    }

    WOLFSSL_LEAVE("wolfSSL_BN_mod mp_mod", ret);
    return ret;
}

/*******************************************************************************
 * Math and Mod APIs
 ******************************************************************************/

#ifndef WOLFSSL_SP_MATH
/* Add a to b modulo m into r. r = a + b (mod m)
 *
 * @param [in, out] r    Big number to hold result.
 * @param [in]      a    Big number to add to.
 * @param [in]      b    Big number to add with.
 * @param [in]      m    Big number that is the modulus.
 * @param [in]      ctx  BN context. Not used.
 * @return  1 on success.
 * @return  0 when r, a or b or internal representation is NULL.
 * @return  0 on calculation failure.
 */
int wolfSSL_BN_mod_add(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
    const WOLFSSL_BIGNUM *b, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
{
    int ret = 1;

    /* BN context not needed. */
    (void)ctx;

    WOLFSSL_ENTER("wolfSSL_BN_add");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b) || BN_IS_NULL(m)) {
        WOLFSSL_MSG("bn NULL error");
        ret = 0;
    }

    /* Perform operation with wolfCrypt. */
    if ((ret == 1) && (mp_addmod((mp_int*)a->internal, (mp_int*)b->internal,
            (mp_int*)m->internal, (mp_int*)r->internal) != MP_OKAY)) {
        WOLFSSL_MSG("mp_add_d error");
        ret = 0;
    }

    return ret;
}
#endif

/* Calculate a multiplied by b, mod m into r. r = (a * b) % m
 *
 * @param [out] r    Big number to put result into.
 * @param [in]  a    Base as a big number.
 * @param [in]  b    Multiplier as a big number.
 * @param [in]  m    Modulus as a big number.
 * @param [in]  ctx  BN context object. Unused.
 *
 * @return  1 on success.
 * @return  0 when r, a, b or m is NULL.
 * @return  0 on failure.
 */
int wolfSSL_BN_mod_mul(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
    const WOLFSSL_BIGNUM *b, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
{
    int ret = 1;

    /* BN context not needed. */
    (void)ctx;

    WOLFSSL_ENTER("wolfSSL_BN_mod_mul");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b) || BN_IS_NULL(m)) {
        WOLFSSL_MSG("Bad Argument");
        ret = 0;
    }

    /* Have wolfCrypt perform operation with internal representations. */
    if ((ret == 1) && (mp_mulmod((mp_int*)a->internal, (mp_int*)b->internal,
            (mp_int*)m->internal, (mp_int*)r->internal)) != MP_OKAY) {
        ret = 0;
    }

    WOLFSSL_LEAVE("wolfSSL_BN_mod_mul", ret);
    return ret;
}


/* Calculate a to the power of e, mod m into r. r = (a ^ e) % m
 *
 * @param [out] r    Big number to put result into.
 * @param [in]  a    Base as a big number.
 * @param [in]  e    Exponent as a big number.
 * @param [in]  m    Modulus as a big number.
 * @param [in]  ctx  BN context object. Unused.
 *
 * @return  1 on success.
 * @return  0 when r, a, p or m is NULL.
 * @return  0 on failure.
 */
int wolfSSL_BN_mod_exp(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
    const WOLFSSL_BIGNUM *e, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
{
    int ret = 1;

    /* BN context not needed. */
    (void)ctx;

    WOLFSSL_ENTER("wolfSSL_BN_mod_exp");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(e) || BN_IS_NULL(m)) {
        WOLFSSL_MSG("Bad Argument");
        ret = 0;
    }

    /* Have wolfCrypt perform operation with internal representations. */
    if ((ret == 1) && (mp_exptmod((mp_int*)a->internal, (mp_int*)e->internal,
            (mp_int*)m->internal, (mp_int*)r->internal)) != MP_OKAY) {
        ret = 0;
    }

    WOLFSSL_LEAVE("wolfSSL_BN_mod_exp", ret);
    return ret;
}

/* Calculate the modular inverse of a mod m into r. r = a^-1 mod m
 *
 * A new big number is allocated when r is NULL.
 *
 * @param [in, out] r    Big number to hold result. May be NULL.
 * @param [in]      a    Big number to invert.
 * @param [in]      m    Big number that is the modulus.
 * @param [in]      ctx  BN context. Not used.
 * @return  Big number holding result on success.
 * @return  NULL on failure.
 */
WOLFSSL_BIGNUM *wolfSSL_BN_mod_inverse(WOLFSSL_BIGNUM *r, WOLFSSL_BIGNUM *a,
    const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
{
    int err = 0;
    WOLFSSL_BIGNUM* t = NULL;

    /* BN context not needed. */
    (void)ctx;

    WOLFSSL_ENTER("wolfSSL_BN_mod_inverse");

    /* Validate parameters. */
    if (BN_IS_NULL(a) || BN_IS_NULL(m) || ((r != NULL) &&
            (r->internal == NULL))) {
        WOLFSSL_MSG("a or n NULL error");
        err = 1;
    }
    /* Check whether we have a result big number. */
    if ((!err) && (r == NULL)) {
        /* Allocate a new big number to hold result and be returned. */
        t = wolfSSL_BN_new();
        if (t == NULL){
            WOLFSSL_MSG("WolfSSL_BN_new() failed");
            err = 1;
        }
        r = t;
    }

    /* Compute inverse of a modulo n and return in r */
    if ((!err) && (mp_invmod((mp_int *)a->internal, (mp_int *)m->internal,
            (mp_int*)r->internal) != MP_OKAY)) {
        WOLFSSL_MSG("mp_invmod() error");
        err = 1;
    }

    if (err) {
        wolfSSL_BN_free(t);
        r = NULL;
    }
    return r;
}

/*******************************************************************************
 * Other Math APIs
 ******************************************************************************/

#if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
/* Needed to get mp_gcd. */
/* Greatest Common Divisor (GCM) of a and b into r. r = GCD(a, b)
 *
 * @param [out] r    Big number to put result into.
 * @param [in]  a    First big number.
 * @param [in]  b    Second big number.
 * @param [in]  ctx  BN context object. Unused.
 *
 * @return  1 on success.
 * @return  0 when r, a or b is NULL.
 * @return  0 when internal representation of r, a or b is NULL.
 * @return  0 on failure.
 */
int wolfSSL_BN_gcd(WOLFSSL_BIGNUM* r, WOLFSSL_BIGNUM* a, WOLFSSL_BIGNUM* b,
    WOLFSSL_BN_CTX* ctx)
{
    int ret = 1;

    /* BN context not needed. */
    (void)ctx;

    WOLFSSL_ENTER("wolfSSL_BN_gcd");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
        ret = 0;
    }

    /* Have wolfCrypt perform operation with internal representations. */
    if ((ret == 1) && (mp_gcd((mp_int*)a->internal, (mp_int*)b->internal,
            (mp_int*)r->internal) != MP_OKAY)) {
        ret = 0;
    }

    WOLFSSL_LEAVE("wolfSSL_BN_gcd", ret);
    return ret;
}
#endif /* !NO_RSA && WOLFSSL_KEY_GEN */

/*******************************************************************************
 * Random APIs
 ******************************************************************************/

/* Generates a random number up to bits long.
 *
 * @param [in, out] bn       Big number to generate into.
 * @param [in]      bits     Number of bits in word.
 * @param [in]      top      Whether top bits must be set.
 *                           Valid values: WOLFSSL_BN_RAND_TOP_ANY,
 *                           WOLFSSL_BN_RAND_TOP_ONE, WOLFSSL_BN_RAND_TOP_TWO.
 * @param [in]      bottom   Whether bottom bit must be set.
 *                           Valid values: WOLFSSL_BN_RAND_BOTTOM_ANY,
                             WOLFSSL_BN_RAND_BOTTOM_ODD.
 * @return  1 on success.
 * @return  0 when bn is NULL.
 * @return  0 when bits is invalid.
 * @return  0 when bits and top/bottom are invalid.
 * @return  0 when generation fails.
 */
int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
{
    int ret = 1;
    word32 len = (word32)((bits + 7) / 8);
    WC_RNG* rng;

    WOLFSSL_ENTER("wolfSSL_BN_rand");

    /* Validate parameters. */
    if (BN_IS_NULL(bn)) {
        WOLFSSL_MSG("Bad argument - bn");
        ret = 0;
    }
    else if (bits < 0) {
        WOLFSSL_MSG("Bad argument - bits < 0");
        ret = 0;
    }
    else if ((bits == 0) && ((bottom != WOLFSSL_BN_RAND_BOTTOM_ANY) ||
            (top != WOLFSSL_BN_RAND_TOP_ANY))) {
        WOLFSSL_MSG("Bad top/bottom - bits == 0");
        ret = 0;
    }
    else if ((bits == 1) && (top > WOLFSSL_BN_RAND_TOP_ONE)) {
        WOLFSSL_MSG("Bad top - bits == 1");
        ret = 0;
    }

    /* Handle simple case of zero bits. */
    if ((ret == 1) && (bits == 0)) {
        mp_zero((mp_int*)bn->internal);
    }
    else if (ret == 1) {
        byte* buff = NULL;

        /* Get random to global random to generate bits. */
        if ((rng = wolfssl_make_global_rng()) == NULL) {
            WOLFSSL_MSG("Failed to use global RNG.");
            ret = 0;
        }

        /* Allocate buffer to hold generated bits. */
        if ((ret == 1) && ((buff = (byte*)XMALLOC(len, NULL,
                DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
            WOLFSSL_MSG("Failed to allocate buffer.");
            ret = 0;
        }
        /* Generate bytes to cover bits. */
        if ((ret == 1) && wc_RNG_GenerateBlock(rng, buff, len) != 0) {
            WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
            ret = 0;
        }
        /* Read bytes in to big number. */
        if ((ret == 1) && mp_read_unsigned_bin((mp_int*)bn->internal, buff, len)
                != MP_OKAY) {
            WOLFSSL_MSG("mp_read_unsigned_bin failed");
            ret = 0;
        }
        /* Dispose of buffer - no longer needed. */
        XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);

        if (ret == 1) {
            /* Truncate to requested bit length. */
            mp_rshb((mp_int*)bn->internal, 8 - (bits % 8));
        }

        /* Set top bit when required. */
        if ((ret == 1) && (top >= WOLFSSL_BN_RAND_TOP_ONE) &&
                (mp_set_bit((mp_int*)bn->internal, bits - 1) != MP_OKAY)) {
            WOLFSSL_MSG("Failed to set top bit");
            ret = 0;
        }
        /* Set second top bit when required. */
        if ((ret == 1) && (top > WOLFSSL_BN_RAND_TOP_ONE) &&
                (mp_set_bit((mp_int*)bn->internal, bits - 2) != MP_OKAY)) {
            WOLFSSL_MSG("Failed to set second top bit");
            ret = 0;
        }
        /* Set bottom bit when required. */
        if ((ret == 1) && (bottom == WOLFSSL_BN_RAND_BOTTOM_ODD) &&
                (mp_set_bit((mp_int*)bn->internal, 0) != MP_OKAY)) {
            WOLFSSL_MSG("Failed to set 0th bit");
            ret = 0;
        }
    }

    WOLFSSL_LEAVE("wolfSSL_BN_rand", ret);

    return ret;
}

/* Generates a pseudo-random number up to bits long.
 *
 * Implemented using wolfSSL_BN_rand().
 *
 * @param [in, out] bn       Big number to generate into.
 * @param [in]      bits     Number of bits in word.
 * @param [in]      top      Whether top bits must be set.
 *                           Valid values: WOLFSSL_BN_RAND_TOP_ANY,
 *                           WOLFSSL_BN_RAND_TOP_ONE, WOLFSSL_BN_RAND_TOP_TWO.
 * @param [in]      bottom   Whether bottom bit must be set.
 *                           Valid values: WOLFSSL_BN_RAND_BOTTOM_ANY,
                             WOLFSSL_BN_RAND_BOTTOM_ODD.
 * @return  1 on success.
 * @return  0 when bn is NULL.
 * @return  0 when bits is invalid.
 * @return  0 when bits and top/bottom are invalid.
 * @return  0 when generation fails.
 */
int wolfSSL_BN_pseudo_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
{
    return wolfSSL_BN_rand(bn, bits, top, bottom);
}

/* Maximum number of trials to attempt at generating a number in range. */
#define RANGE_MAX_TRIALS    100

/* Generate big number to be a value between 0 and range-1.
 *
 * N = length of range input var
 *
 * Generate N-bit length numbers until generated number is less than range
 * @param [in] r      Big number to generate into.
 * @param [in] range  The upper limit of generated number.
 * @return  1 on success.
 * @return  0 on failure.
 */
int wolfSSL_BN_rand_range(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *range)
{
    int ret = 1;

    WOLFSSL_ENTER("wolfSSL_BN_rand_range");

    /* Validate parameters. */
    if (BN_IS_NULL(r) || BN_IS_NULL(range)) {
        WOLFSSL_MSG("Bad parameter");
        ret = 0;
    }

    if (ret == 1) {
        /* Calculate number of bits in modulus. */
        int n = wolfSSL_BN_num_bits(range);

        if (n <= 1) {
            /* Modulus is 0 or 1. */
            wolfSSL_BN_zero(r);
        }
        else {
            int i;
            /* Try generating a number in range for a limited number of trials.
             */
            for (i = 0; (ret == 1) && (i < RANGE_MAX_TRIALS); i++) {
                /* Generate a random number in range. */
                if (wolfSSL_BN_pseudo_rand(r, n, WOLFSSL_BN_RAND_TOP_ANY,
                        WOLFSSL_BN_RAND_BOTTOM_ANY) == 0) {
                    WOLFSSL_MSG("wolfSSL_BN_rand error");
                    ret = 0;
                }
                /* Check if in range. */
                else if (wolfSSL_BN_cmp(r, range) < 0) {
                    break;
                }
            }
            /* Fail if max trial attempts made. */
            if (i >= RANGE_MAX_TRIALS) {
                WOLFSSL_MSG("wolfSSL_BN_rand_range too many iterations");
                ret = 0;
            }
        }
    }

    return ret;
}

/*******************************************************************************
 * Prime APIs
 ******************************************************************************/

#if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
    !defined(NO_DSA))

/* Generate a prime number.
 *
 * @param [in, out] prime  Big number to generate into.
 * @param [in]      bits   Number of bits in generated number.
 * @param [in]      safe   Whether number must be a safe prime.
 * @param [in]      add    Value to add when generating. Not used.
 * @param [in]      rem    Remainder of number modulo add. Not used.
 * @param [in]      cb     Generation callback. Not used.
 * @return  1 on success.
 * @return  0 when prime is NULL.
 * @return  0 when safe required or add or rem is not NULL.
 * @return  0 on generation failure.
 */
int wolfSSL_BN_generate_prime_ex(WOLFSSL_BIGNUM* prime, int bits,
    int safe, const WOLFSSL_BIGNUM* add, const WOLFSSL_BIGNUM* rem,
    WOLFSSL_BN_GENCB* cb)
{
    int ret = 1;
    WC_DECLARE_VAR(tmpRng, WC_RNG, 1, 0);
    WC_RNG* rng = NULL;
    int localRng = 0;

    /* Callback not used. */
    (void)cb;

    WOLFSSL_ENTER("wolfSSL_BN_generate_prime_ex");

    /* Check unsupported parameters. */
    if ((safe == 1) || (add != NULL) || (rem != NULL)) {
        ret = 0;
    }
    /* Validate parameters. */
    else if (BN_IS_NULL(prime)) {
        ret = 0;
    }

    /* Create a new RNG or use global. */
    if ((ret == 1) && ((rng = wolfssl_make_rng(tmpRng, &localRng)) == NULL)) {
        ret = 0;
    }

    /* Use wolfCrypt to generate a prime. */
    if ((ret == 1) && (mp_rand_prime((mp_int*)prime->internal, (bits + 7) / 8,
            rng, NULL) != MP_OKAY)) {
        ret = 0;
    }

    if (localRng) {
        /* Dispose of local RNG that was created. */
        wc_FreeRng(rng);
        WC_FREE_VAR_EX(rng, NULL, DYNAMIC_TYPE_RNG);
    }

    WOLFSSL_LEAVE("wolfSSL_BN_generate_prime_ex", ret);

    return ret;
}

/* Check whether a big number is prime.
 *
 * Return code compliant with OpenSSL.
 *
 * @param [in] bn      Big number to check.
 * @param [in] checks  Number of Miller-Rabin tests to perform.
 * @param [in] ctx     BN context. Not used.
 * @param [in] cb      Generation callback. Not used.
 * @return  1 when number is prime.
 * @return  0 when number is not prime.
 * @return  -1 when bn is NULL or failure when checking.
 */
int wolfSSL_BN_is_prime_ex(const WOLFSSL_BIGNUM *bn, int checks,
    WOLFSSL_BN_CTX *ctx, WOLFSSL_BN_GENCB *cb)
{
    int ret = 1;
    WC_RNG* rng    = NULL;
    WC_DECLARE_VAR(tmpRng, WC_RNG, 1, 0);
    int localRng = 0;
    int res = MP_NO;

    /* BN context not needed. */
    (void)ctx;
    (void)cb;

    WOLFSSL_ENTER("wolfSSL_BN_is_prime_ex");

    if (BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        ret = WOLFSSL_FATAL_ERROR;
    }

    /* Create a new RNG or use global. */
    if ((ret == 1) && ((rng = wolfssl_make_rng(tmpRng, &localRng)) == NULL)) {
        ret = WOLFSSL_FATAL_ERROR;
    }

    if ((ret == 1) && (mp_prime_is_prime_ex((mp_int*)bn->internal, checks, &res,
            rng) != MP_OKAY)) {
        WOLFSSL_MSG("mp_prime_is_prime_ex error");
        ret = WOLFSSL_FATAL_ERROR;
    }

    if (localRng) {
        wc_FreeRng(rng);
        WC_FREE_VAR_EX(rng, NULL, DYNAMIC_TYPE_RNG);
    }

    if ((ret != -1) && (res != MP_YES)) {
        WOLFSSL_MSG("mp_prime_is_prime_ex not prime");
        ret = 0;
    }

    return ret;
}

#endif /* WOLFSSL_KEY_GEN && (!NO_RSA || !NO_DH || !NO_DSA) */

/*******************************************************************************
 * Print APIs
 ******************************************************************************/

#if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
    defined(XFPRINTF)
/* Print big number to file pointer.
 *
 * Return code compliant with OpenSSL.
 *
 * @param [in] fp  File pointer.
 * @param [in] bn  Big number to print.
 * @return  1 on success.
 * @return  0 when fp is a bad file pointer.
 * @return  0 when bn is NULL.
 * @return  0 when creating hex string fails.
 * @return  0 when printing fails.
 */
int wolfSSL_BN_print_fp(XFILE fp, const WOLFSSL_BIGNUM *bn)
{
    int ret = 1;
    char* buf = NULL;

    WOLFSSL_ENTER("wolfSSL_BN_print_fp");

    /* Validate parameters. */
    if ((fp == XBADFILE) || BN_IS_NULL(bn)) {
        WOLFSSL_MSG("bn NULL error");
        ret = 0;
    }

    /* Create a hex string of big number. */
    if ((ret == 1) && ((buf = wolfSSL_BN_bn2hex(bn)) == NULL)) {
        WOLFSSL_MSG("wolfSSL_BN_bn2hex failure");
        ret = 0;
    }

    /* Print hex string to file pointer. */
    if ((ret == 1) && (XFPRINTF(fp, "%s", buf) < 0)) {
        ret = 0;
    }

    /* Dispose of any allocated data. */
    XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);

    return ret;
}
#endif /* !NO_FILESYSTEM && XFPRINTF */

#ifndef NO_WOLFSSL_BN_CTX
/*******************************************************************************
 * BN_CTX APIs
 ******************************************************************************/

/* Create a new BN context object.
 *
 * @return  BN context object on success.
 * @return  NULL on failure.
 */
WOLFSSL_BN_CTX* wolfSSL_BN_CTX_new(void)
{
    WOLFSSL_BN_CTX* ctx = NULL;

    WOLFSSL_ENTER("wolfSSL_BN_CTX_new");
    ctx = (WOLFSSL_BN_CTX*)XMALLOC(sizeof(WOLFSSL_BN_CTX), NULL,
            DYNAMIC_TYPE_OPENSSL);
    if (ctx != NULL) {
        XMEMSET(ctx, 0, sizeof(WOLFSSL_BN_CTX));
    }

    return ctx;
}


#ifndef NO_WOLFSSL_STUB
/* deprecated
 *
 * Initialize a BN context object.
 * This function was removed in OpenSSL 1.1.0 and later.
 * Keeping a stub function here for older applications that have BN_CTX_init()
 * calls.
 *
 * @param [in] ctx  Dummy BN context.
 */
void wolfSSL_BN_CTX_init(WOLFSSL_BN_CTX* ctx)
{
    (void)ctx;
    WOLFSSL_ENTER("wolfSSL_BN_CTX_init");
    WOLFSSL_STUB("wolfSSL_BN_CTX_init");
    WOLFSSL_MSG("wolfSSL_BN_CTX_init is deprecated");
}
#endif


/* Free a BN context object.
 *
 * @param [in] ctx  BN context object.
 */
void wolfSSL_BN_CTX_free(WOLFSSL_BN_CTX* ctx)
{
    WOLFSSL_ENTER("wolfSSL_BN_CTX_free");
    if (ctx != NULL) {
        while (ctx->list != NULL) {
            struct WOLFSSL_BN_CTX_LIST* tmp = ctx->list;
            ctx->list = ctx->list->next;
            wolfSSL_BN_free(tmp->bn);
            XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
        }
        XFREE(ctx, NULL, DYNAMIC_TYPE_OPENSSL);
    }
}

/* Get a big number from the BN context.
 *
 * @param [in] ctx  BN context object.
 * @return  Big number on success.
 * @return  NULL on failure.
 */
WOLFSSL_BIGNUM *wolfSSL_BN_CTX_get(WOLFSSL_BN_CTX *ctx)
{
    WOLFSSL_BIGNUM* bn = NULL;

    WOLFSSL_ENTER("wolfSSL_BN_CTX_get");
    if (ctx != NULL) {
        struct WOLFSSL_BN_CTX_LIST* node = (struct WOLFSSL_BN_CTX_LIST*)XMALLOC(
                sizeof(struct WOLFSSL_BN_CTX_LIST), NULL, DYNAMIC_TYPE_OPENSSL);
        if (node != NULL) {
            XMEMSET(node, 0, sizeof(struct WOLFSSL_BN_CTX_LIST));
            bn = node->bn = wolfSSL_BN_new();
            if (node->bn != NULL) {
                node->next = ctx->list;
                ctx->list = node;
            }
            else {
                XFREE(node, NULL, DYNAMIC_TYPE_OPENSSL);
                node = NULL;
            }
        }
    }

    return bn;
}

#ifndef NO_WOLFSSL_STUB
/* Start stack of temporary big numbers.
 *
 * Newly allocated big numbers are returned instead of having a stack.
 *
 * @param [in] ctx  BN context. Not used.
 */
void wolfSSL_BN_CTX_start(WOLFSSL_BN_CTX *ctx)
{
    (void)ctx;

    WOLFSSL_ENTER("wolfSSL_BN_CTX_start");
    WOLFSSL_STUB("BN_CTX_start");
    WOLFSSL_MSG("wolfSSL_BN_CTX_start TBD");
}
#endif

#endif /* NO_WOLFSSL_BN_CTX */

/*******************************************************************************
 * BN_MONT_CTX APIs
 ******************************************************************************/

WOLFSSL_BN_MONT_CTX* wolfSSL_BN_MONT_CTX_new(void)
{
    /* wolfcrypt doesn't need BN MONT context. */
    static int mont;
    WOLFSSL_ENTER("wolfSSL_BN_MONT_CTX_new");
    return (WOLFSSL_BN_MONT_CTX*)&mont;
}

void wolfSSL_BN_MONT_CTX_free(WOLFSSL_BN_MONT_CTX *mont)
{
    (void)mont;
    WOLFSSL_ENTER("wolfSSL_BN_MONT_CTX_free");
    /* Don't do anything since using dummy, static BN context. */
}

int wolfSSL_BN_MONT_CTX_set(WOLFSSL_BN_MONT_CTX *mont,
        const WOLFSSL_BIGNUM *mod, WOLFSSL_BN_CTX *ctx)
{
    (void) mont;
    (void) mod;
    (void) ctx;
    WOLFSSL_ENTER("wolfSSL_BN_MONT_CTX_set");
    return WOLFSSL_SUCCESS;
}

/* Calculate r = a ^ p % m.
 *
 * @param [out] r    Big number to store the result.
 * @param [in]  a    Base as an unsigned long.
 * @param [in]  p    Exponent as a big number.
 * @param [in]  m    Modulus as a big number.
 * @param [in]  ctx  BN context object. Unused.
 * @param [in]  mont Montgomery context object. Unused.
 *
 * @return  1 on success.
 * @return  0 on failure.
 */
int wolfSSL_BN_mod_exp_mont_word(WOLFSSL_BIGNUM *r, WOLFSSL_BN_ULONG a,
        const WOLFSSL_BIGNUM *p, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx,
        WOLFSSL_BN_MONT_CTX *mont)
{
    WOLFSSL_BIGNUM* tmp = NULL;
    int ret = WOLFSSL_SUCCESS;

    (void)mont;
    WOLFSSL_ENTER("wolfSSL_BN_mod_exp_mont_word");

    if (ret == WOLFSSL_SUCCESS && (tmp = wolfSSL_BN_new()) == NULL) {
        WOLFSSL_MSG("wolfSSL_BN_new failed");
        ret = WOLFSSL_FAILURE;
    }
    if (ret == WOLFSSL_SUCCESS && (wolfSSL_BN_set_word(tmp, (unsigned long)a))
                                   == WC_NO_ERR_TRACE(WOLFSSL_FAILURE)) {
        WOLFSSL_MSG("wolfSSL_BN_set_word failed");
        ret = WOLFSSL_FAILURE;
    }
    if (ret == WOLFSSL_SUCCESS)
        ret = wolfSSL_BN_mod_exp(r, tmp, p, m, ctx);

    wolfSSL_BN_free(tmp);
    return ret;
}

#endif /* OPENSSL_EXTRA */

#endif /* !WOLFSSL_SSL_BN_INCLUDED */