forked from wolfSSL/wolfcrypt-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_ffi.py
More file actions
1375 lines (1174 loc) · 52 KB
/
build_ffi.py
File metadata and controls
1375 lines (1174 loc) · 52 KB
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
# build_ffi.py
#
# Copyright (C) 2006-2022 wolfSSL Inc.
#
# This file is part of wolfSSL. (formerly known as CyaSSL)
#
# 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 2 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-1301, USA
import os
import sys
import re
import subprocess
from contextlib import contextmanager
from distutils.util import get_platform
from cffi import FFI
import shutil
from wolfcrypt._version import __wolfssl_version__ as version
def local_path(path):
""" Return path relative to the root of this project
"""
current = os.path.abspath(os.getcwd())
return os.path.abspath(os.path.join(current, path))
WOLFSSL_SRC_PATH = local_path("lib/wolfssl")
def wolfssl_inc_dirs(local_wolfssl=None, fips=False):
"""Returns the wolfSSL include directories needed to build the CFFI.
"""
include_paths = []
if local_wolfssl:
include_dir = os.path.join(local_wolfssl, "include")
# If an include subdirectory exists under local_wolfssl, use that.
# Otherwise, use local_wolfssl (e.g. local_wolfssl may point to a
# wolfssl source code directory).
if os.path.exists(include_dir):
include_paths.append(include_dir)
else:
include_paths.append(local_wolfssl)
if sys.platform == "win32":
# Add the user_settings.h directory.
if fips:
include_paths.append(os.path.join(local_wolfssl, "IDE",
"WIN10"))
else:
include_paths.append(os.path.join(local_wolfssl, "IDE",
"WIN"))
else:
include_paths.append(os.path.join(WOLFSSL_SRC_PATH, get_platform(),
version, "include"))
if sys.platform == "win32":
# Add the user_settings.h directory.
include_paths.append(os.path.join(WOLFSSL_SRC_PATH, "build"))
return include_paths
def wolfssl_lib_dir(local_wolfssl=None, fips=False):
"""Returns the directory containg the wolfSSL library.
"""
lib_dir = None
if local_wolfssl:
lib_names = []
if sys.platform == "win32":
lib_names.append("wolfssl-fips.dll")
lib_names.append("wolfssl.lib")
else:
lib_names.append("libwolfssl.a")
lib_names.append("libwolfssl.so")
found = False
for root, dirs, files in os.walk(local_wolfssl):
for name in lib_names:
if name in files:
lib_dir = root
found = True
break
if found:
break
else:
lib_dir = os.path.join(WOLFSSL_SRC_PATH, get_platform(), version, "lib")
if not lib_dir:
e = ("Unable to find wolfSSL library. If using USE_LOCAL_WOLFSSL, "
"ensure wolfSSL has been built.")
raise FileNotFoundError(e)
return lib_dir
def call(cmd):
print("Calling: '{}' from working directory {}".format(cmd, os.getcwd()))
old_env = os.environ["PATH"]
os.environ["PATH"] = "{}:{}".format(WOLFSSL_SRC_PATH, old_env)
subprocess.check_call(cmd, shell=True, env=os.environ)
os.environ["PATH"] = old_env
@contextmanager
def chdir(new_path, mkdir=False):
old_path = os.getcwd()
if mkdir:
try:
os.mkdir(new_path)
except OSError:
pass
try:
yield os.chdir(new_path)
finally:
os.chdir(old_path)
def checkout_version(version):
""" Ensure that we have the right version.
"""
with chdir(WOLFSSL_SRC_PATH):
current = ""
try:
current = subprocess.check_output(
["git", "describe", "--all", "--exact-match"]
).strip().decode().split('/')[-1]
except:
pass
if current != version:
tags = subprocess.check_output(
["git", "tag"]
).strip().decode().split("\n")
if version != "master" and version not in tags:
call("git fetch --depth=1 origin tag {}".format(version))
call("git checkout --force {}".format(version))
return True # rebuild needed
return False
def ensure_wolfssl_src(ref):
""" Ensure that wolfssl sources are presents and up-to-date.
"""
if not os.path.isdir("lib"):
os.mkdir("lib")
with chdir("lib"):
subprocess.run(["git", "clone", "--depth=1", "https://github.com/wolfssl/wolfssl"])
if not os.path.isdir(os.path.join(WOLFSSL_SRC_PATH, "wolfssl")):
subprocess.run(["git", "submodule", "update", "--init", "--depth=1"])
return checkout_version(version)
def make_flags(prefix, fips):
""" Returns compilation flags.
"""
if sys.platform == "win32":
flags = []
flags.append("-DCMAKE_INSTALL_PREFIX={}".format(prefix))
flags.append("-DWOLFSSL_CRYPT_TESTS=no")
flags.append("-DWOLFSSL_EXAMPLES=no")
flags.append("-DBUILD_SHARED_LIBS=no")
flags.append("-DWOLFSSL_USER_SETTINGS=yes")
if fips:
flags.append("-DCMAKE_CXX_FLAGS=-I" + local_path("../IDE/WIN10"))
else:
flags.append("-DCMAKE_CXX_FLAGS=-I" + local_path("../IDE/WIN"))
return " ".join(flags)
else:
flags = []
if get_platform() in ["linux-x86_64", "linux-i686"]:
flags.append("CFLAGS=-fPIC")
# install location
flags.append("--prefix={}".format(prefix))
# crypt only, lib only
flags.append("--enable-cryptonly")
flags.append("--disable-crypttests")
flags.append("--disable-shared")
# symmetric ciphers
flags.append("--enable-aes")
flags.append("--enable-aesctr")
flags.append("--enable-des3")
flags.append("--enable-chacha")
flags.append("--enable-aesgcm-stream")
flags.append("--enable-aesgcm")
flags.append("--enable-aessiv")
# hashes and MACs
flags.append("--enable-sha")
flags.append("--enable-sha384")
flags.append("--enable-sha512")
flags.append("--enable-sha3")
flags.append("--enable-hkdf")
flags.append("--disable-md5")
flags.append("--disable-sha224")
flags.append("--enable-poly1305")
# asymmetric ciphers
flags.append("--enable-rsa")
flags.append("--enable-rsapss")
flags.append("--enable-ecc")
flags.append("--enable-ed25519")
flags.append("--enable-ed448")
flags.append("--enable-curve25519")
flags.append("--enable-keygen")
flags.append("--disable-dh")
# pwdbased
flags.append("--enable-pwdbased")
flags.append("--enable-pkcs7")
# ML-KEM
flags.append("--enable-kyber")
# ML-DSA
flags.append("--enable-dilithium")
# disabling other configs enabled by default
flags.append("--disable-oldtls")
flags.append("--disable-oldnames")
flags.append("--disable-extended-master")
return " ".join(flags)
def make(configure_flags, fips=False):
""" Create a release of wolfSSL C library
"""
if sys.platform == 'win32':
build_path = os.path.join(WOLFSSL_SRC_PATH, "build")
if not os.path.isdir(build_path):
os.mkdir(build_path)
if not fips:
shutil.copy(local_path("windows/non_fips/user_settings.h"),
build_path)
else:
raise Exception("Cannot build wolfSSL FIPS from git repo.")
with chdir(build_path):
call("cmake {} ..".format(configure_flags))
call("cmake --build . --config Release")
call("cmake --install . --config Release")
else:
with chdir(WOLFSSL_SRC_PATH):
call("git clean -fdX")
try:
call("./autogen.sh")
except subprocess.CalledProcessError:
call("libtoolize")
call("./autogen.sh")
call("./configure {}".format(configure_flags))
call("make")
call("make install")
def get_libwolfssl():
if sys.platform == "win32":
libwolfssl_path = os.path.join(wolfssl_lib_dir(), "wolfssl.lib")
if not os.path.exists(libwolfssl_path):
return False
else:
return True
else:
libwolfssl_path = os.path.join(wolfssl_lib_dir(), "libwolfssl.a")
if not os.path.exists(libwolfssl_path):
libwolfssl_path = os.path.join(wolfssl_lib_dir(), "libwolfssl.so")
if not os.path.exists(libwolfssl_path):
return False
else:
return True
else:
return True
def generate_libwolfssl(fips):
ensure_wolfssl_src(version)
prefix = os.path.join(WOLFSSL_SRC_PATH, get_platform(), version)
make(make_flags(prefix, fips))
def get_features(local_wolfssl, features):
fips = False
if sys.platform == "win32":
# On Windows, we assume the local_wolfssl path is to a wolfSSL source
# directory where the library has been built.
fips_file = os.path.join(local_wolfssl, "wolfssl", "wolfcrypt",
"fips.h")
else:
# On non-Windows platforms, first assume local_wolfssl is an
# installation directory with an include subdirectory.
fips_file = os.path.join(local_wolfssl, "include", "wolfssl",
"wolfcrypt", "fips.h")
if not os.path.exists(fips_file):
# Try assuming local_wolfssl is a wolfSSL source directory.
fips_file = os.path.join(local_wolfssl, "wolfssl", "wolfcrypt",
"fips.h")
if os.path.exists(fips_file):
with open(fips_file, "r") as f:
contents = f.read()
if not contents.isspace():
fips = True
include_dirs = wolfssl_inc_dirs(local_wolfssl, fips)
defines_files = []
for d in include_dirs:
if not os.path.exists(d):
e = f"Invalid wolfSSL include dir: {d}"
raise FileNotFoundError(e)
options = os.path.join(d, "wolfssl", "options.h")
if os.path.exists(options):
defines_files.append(options)
user_settings = os.path.join(d, "user_settings.h")
if os.path.exists(user_settings):
defines_files.append(user_settings)
if len(defines_files) == 0:
e = "No options.h or user_settings.h found for feature detection."
raise RuntimeError(e)
defines = []
for file in defines_files:
with open(file, 'r') as f:
defines += f.read().splitlines()
features["MPAPI"] = 1 if '#define WOLFSSL_PUBLIC_MP' in defines else 0
features["SHA"] = 0 if '#define NO_SHA' in defines else 1
features["SHA256"] = 0 if '#define NO_SHA256' in defines else 1
features["SHA384"] = 1 if '#define WOLFSSL_SHA384' in defines else 0
features["SHA512"] = 1 if '#define WOLFSSL_SHA512' in defines else 0
features["SHA3"] = 1 if '#define WOLFSSL_SHA3' in defines else 0
features["DES3"] = 0 if '#define NO_DES3' in defines else 1
features["AES"] = 0 if '#define NO_AES' in defines else 1
features["AES_SIV"] = 1 if '#define WOLFSSL_AES_SIV' in defines else 0
features["CHACHA"] = 1 if '#define HAVE_CHACHA' in defines else 0
features["HMAC"] = 0 if '#define NO_HMAC' in defines else 1
features["RSA"] = 0 if '#define NO_RSA' in defines else 1
features["ECC_TIMING_RESISTANCE"] = 1 if '#define ECC_TIMING_RESISTANT' in defines else 0
features["RSA_BLINDING"] = 1 if '#define WC_RSA_BLINDING' in defines else 0
features["ECC"] = 1 if '#define HAVE_ECC' in defines else 0
features["ED25519"] = 1 if '#define HAVE_ED25519' in defines else 0
features["ED448"] = 1 if '#define HAVE_ED448' in defines else 0
features["KEYGEN"] = 1 if '#define WOLFSSL_KEY_GEN' in defines else 0
features["PWDBASED"] = 0 if '#define NO_PWDBASED' in defines else 1
features["ERROR_STRINGS"] = 0 if '#define NO_ERROR_STRINGS' in defines else 1
features["ASN"] = 0 if '#define NO_ASN' in defines else 1
features["WC_RNG_SEED_CB"] = 1 if '#define WC_RNG_SEED_CB' in defines else 0
features["AESGCM_STREAM"] = 1 if '#define WOLFSSL_AESGCM_STREAM' in defines else 0
features["RSA_PSS"] = 1 if '#define WC_RSA_PSS' in defines else 0
features["CHACHA20_POLY1305"] = 1 if ('#define HAVE_CHACHA' in defines and '#define HAVE_POLY1305' in defines) else 0
features["ML_DSA"] = 1 if '#define HAVE_DILITHIUM' in defines else 0
features["ML_KEM"] = 1 if '#define WOLFSSL_HAVE_MLKEM' in defines else 0
features["HKDF"] = 1 if "#define HAVE_HKDF" in defines else 0
if '#define HAVE_FIPS' in defines:
if not fips:
e = "fips.c empty but HAVE_FIPS defined."
raise RuntimeError(e)
features["FIPS"] = 1
version_match = re.search(r'#define HAVE_FIPS_VERSION\s+(\d+)', '\n'.join(defines))
if version_match is not None:
features["FIPS_VERSION"] = int(version_match.group(1))
else:
e = "Saw #define HAVE_FIPS but no FIPS version found."
raise RuntimeError(e)
return features
def build_ffi(local_wolfssl, features):
cffi_include_dirs = wolfssl_inc_dirs(local_wolfssl, features["FIPS"])
cffi_libraries = []
if sys.platform == 'win32':
if features["FIPS"]:
# To use the CFFI library, we need wolfssl-fips.dll. It should exist
# alongside the .pyd created by CFFI, so we copy it over here.
shutil.copy(os.path.join(wolfssl_lib_dir(local_wolfssl,
features["FIPS"]), "wolfssl-fips.dll"),
local_path("wolfcrypt/"))
cffi_libraries.append("wolfssl-fips")
else:
cffi_libraries.append("wolfssl")
# Needed for WIN32 functions in random.c.
cffi_libraries.append("Advapi32")
else:
cffi_libraries.append("wolfssl")
includes_string = ""
if sys.platform == 'win32':
includes_string += """
#ifndef WOLFSSL_USER_SETTINGS
#define WOLFSSL_USER_SETTINGS
#endif
#include \"user_settings.h\"\n
"""
else:
includes_string += "#include <wolfssl/options.h>\n"
includes_string += """
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/sha.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/sha512.h>
#include <wolfssl/wolfcrypt/sha3.h>
#include <wolfssl/wolfcrypt/hmac.h>
#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/chacha.h>
#include <wolfssl/wolfcrypt/des3.h>
#include <wolfssl/wolfcrypt/asn.h>
#include <wolfssl/wolfcrypt/pwdbased.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/rsa.h>
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/ed25519.h>
#include <wolfssl/wolfcrypt/ed448.h>
#include <wolfssl/wolfcrypt/curve25519.h>
#include <wolfssl/wolfcrypt/poly1305.h>
#include <wolfssl/wolfcrypt/chacha20_poly1305.h>
#include <wolfssl/wolfcrypt/mlkem.h>
#include <wolfssl/wolfcrypt/wc_mlkem.h>
#include <wolfssl/wolfcrypt/dilithium.h>
"""
init_source_string = """
#ifdef __cplusplus
extern "C" {
#endif
""" + includes_string + """
#ifdef __cplusplus
}
#endif
int ERROR_STRINGS_ENABLED = """ + str(features["ERROR_STRINGS"]) + """;
int MPAPI_ENABLED = """ + str(features["MPAPI"]) + """;
int SHA_ENABLED = """ + str(features["SHA"]) + """;
int SHA256_ENABLED = """ + str(features["SHA256"]) + """;
int SHA384_ENABLED = """ + str(features["SHA384"]) + """;
int SHA512_ENABLED = """ + str(features["SHA512"]) + """;
int SHA3_ENABLED = """ + str(features["SHA3"]) + """;
int DES3_ENABLED = """ + str(features["DES3"]) + """;
int AES_ENABLED = """ + str(features["AES"]) + """;
int AES_SIV_ENABLED = """ + str(features["AES_SIV"]) + """;
int CHACHA_ENABLED = """ + str(features["CHACHA"]) + """;
int HMAC_ENABLED = """ + str(features["HMAC"]) + """;
int RSA_ENABLED = """ + str(features["RSA"]) + """;
int RSA_BLINDING_ENABLED = """ + str(features["RSA_BLINDING"]) + """;
int ECC_TIMING_RESISTANCE_ENABLED = """ + str(features["ECC_TIMING_RESISTANCE"]) + """;
int ECC_ENABLED = """ + str(features["ECC"]) + """;
int ED25519_ENABLED = """ + str(features["ED25519"]) + """;
int ED448_ENABLED = """ + str(features["ED448"]) + """;
int KEYGEN_ENABLED = """ + str(features["KEYGEN"]) + """;
int PWDBASED_ENABLED = """ + str(features["PWDBASED"]) + """;
int FIPS_ENABLED = """ + str(features["FIPS"]) + """;
int FIPS_VERSION = """ + str(features["FIPS_VERSION"]) + """;
int ASN_ENABLED = """ + str(features["ASN"]) + """;
int WC_RNG_SEED_CB_ENABLED = """ + str(features["WC_RNG_SEED_CB"]) + """;
int AESGCM_STREAM_ENABLED = """ + str(features["AESGCM_STREAM"]) + """;
int RSA_PSS_ENABLED = """ + str(features["RSA_PSS"]) + """;
int CHACHA20_POLY1305_ENABLED = """ + str(features["CHACHA20_POLY1305"]) + """;
int ML_KEM_ENABLED = """ + str(features["ML_KEM"]) + """;
int ML_DSA_ENABLED = """ + str(features["ML_DSA"]) + """;
int HKDF_ENABLED = """ + str(features["HKDF"]) + """;
"""
ffibuilder.set_source( "wolfcrypt._ffi", init_source_string,
include_dirs=cffi_include_dirs,
library_dirs=[wolfssl_lib_dir(local_wolfssl, features["FIPS"])],
libraries=cffi_libraries)
# TODO: change cdef to cdef.
# cdef = ""
cdef = """
extern int ERROR_STRINGS_ENABLED;
extern int MPAPI_ENABLED;
extern int SHA_ENABLED;
extern int SHA256_ENABLED;
extern int SHA384_ENABLED;
extern int SHA512_ENABLED;
extern int SHA3_ENABLED;
extern int DES3_ENABLED;
extern int AES_ENABLED;
extern int AES_SIV_ENABLED;
extern int CHACHA_ENABLED;
extern int HMAC_ENABLED;
extern int RSA_ENABLED;
extern int RSA_BLINDING_ENABLED;
extern int ECC_TIMING_RESISTANCE_ENABLED;
extern int ECC_ENABLED;
extern int ED25519_ENABLED;
extern int ED448_ENABLED;
extern int KEYGEN_ENABLED;
extern int PWDBASED_ENABLED;
extern int FIPS_ENABLED;
extern int FIPS_VERSION;
extern int ASN_ENABLED;
extern int WC_RNG_SEED_CB_ENABLED;
extern int AESGCM_STREAM_ENABLED;
extern int RSA_PSS_ENABLED;
extern int CHACHA20_POLY1305_ENABLED;
extern int ML_KEM_ENABLED;
extern int ML_DSA_ENABLED;
extern int HKDF_ENABLED;
typedef unsigned char byte;
typedef unsigned int word32;
typedef struct { ...; } WC_RNG;
typedef struct { ...; } OS_Seed;
int wc_InitRng(WC_RNG*);
int wc_InitRngNonce(WC_RNG*, byte*, word32);
int wc_InitRngNonce_ex(WC_RNG*, byte*, word32, void*, int);
int wc_RNG_GenerateBlock(WC_RNG*, byte*, word32);
int wc_RNG_GenerateByte(WC_RNG*, byte*);
int wc_FreeRng(WC_RNG*);
"""
if features["ERROR_STRINGS"]:
cdef += """
static const int WC_FAILURE;
static const int MAX_CODE_E;
static const int WC_FIRST_E;
static const int WC_SPAN1_FIRST_E;
static const int MP_MEM;
static const int MP_VAL;
static const int MP_WOULDBLOCK;
static const int MP_NOT_INF;
static const int OPEN_RAN_E;
static const int READ_RAN_E;
static const int WINCRYPT_E;
static const int CRYPTGEN_E;
static const int RAN_BLOCK_E;
static const int BAD_MUTEX_E;
static const int WC_TIMEOUT_E;
static const int WC_PENDING_E;
static const int WC_NO_PENDING_E;
static const int MP_INIT_E;
static const int MP_READ_E;
static const int MP_EXPTMOD_E;
static const int MP_TO_E;
static const int MP_SUB_E;
static const int MP_ADD_E;
static const int MP_MUL_E;
static const int MP_MULMOD_E;
static const int MP_MOD_E;
static const int MP_INVMOD_E;
static const int MP_CMP_E;
static const int MP_ZERO_E;
static const int AES_EAX_AUTH_E;
static const int KEY_EXHAUSTED_E;
static const int MEMORY_E;
static const int VAR_STATE_CHANGE_E;
static const int FIPS_DEGRADED_E;
static const int FIPS_CODE_SZ_E;
static const int FIPS_DATA_SZ_E;
static const int RSA_WRONG_TYPE_E;
static const int RSA_BUFFER_E;
static const int BUFFER_E;
static const int ALGO_ID_E;
static const int PUBLIC_KEY_E;
static const int DATE_E;
static const int SUBJECT_E;
static const int ISSUER_E;
static const int CA_TRUE_E;
static const int EXTENSIONS_E;
static const int ASN_PARSE_E;
static const int ASN_VERSION_E;
static const int ASN_GETINT_E;
static const int ASN_RSA_KEY_E;
static const int ASN_OBJECT_ID_E;
static const int ASN_TAG_NULL_E;
static const int ASN_EXPECT_0_E;
static const int ASN_BITSTR_E;
static const int ASN_UNKNOWN_OID_E;
static const int ASN_DATE_SZ_E;
static const int ASN_BEFORE_DATE_E;
static const int ASN_AFTER_DATE_E;
static const int ASN_SIG_OID_E;
static const int ASN_TIME_E;
static const int ASN_INPUT_E;
static const int ASN_SIG_CONFIRM_E;
static const int ASN_SIG_HASH_E;
static const int ASN_SIG_KEY_E;
static const int ASN_DH_KEY_E;
static const int KDF_SRTP_KAT_FIPS_E;
static const int ASN_CRIT_EXT_E;
static const int ASN_ALT_NAME_E;
static const int ASN_NO_PEM_HEADER;
static const int ED25519_KAT_FIPS_E;
static const int ED448_KAT_FIPS_E;
static const int PBKDF2_KAT_FIPS_E;
static const int WC_KEY_MISMATCH_E;
static const int ECC_BAD_ARG_E;
static const int ASN_ECC_KEY_E;
static const int ECC_CURVE_OID_E;
static const int BAD_FUNC_ARG;
static const int NOT_COMPILED_IN;
static const int UNICODE_SIZE_E;
static const int NO_PASSWORD;
static const int ALT_NAME_E;
static const int BAD_OCSP_RESPONDER;
static const int CRL_CERT_DATE_ERR;
static const int AES_GCM_AUTH_E;
static const int AES_CCM_AUTH_E;
static const int ASYNC_INIT_E;
static const int COMPRESS_INIT_E;
static const int COMPRESS_E;
static const int DECOMPRESS_INIT_E;
static const int DECOMPRESS_E;
static const int BAD_ALIGN_E;
static const int ASN_NO_SIGNER_E;
static const int ASN_CRL_CONFIRM_E;
static const int ASN_CRL_NO_SIGNER_E;
static const int ASN_OCSP_CONFIRM_E;
static const int BAD_STATE_E;
static const int BAD_PADDING_E;
static const int REQ_ATTRIBUTE_E;
static const int PKCS7_OID_E;
static const int PKCS7_RECIP_E;
static const int FIPS_NOT_ALLOWED_E;
static const int ASN_NAME_INVALID_E;
static const int RNG_FAILURE_E;
static const int HMAC_MIN_KEYLEN_E;
static const int RSA_PAD_E;
static const int LENGTH_ONLY_E;
static const int IN_CORE_FIPS_E;
static const int AES_KAT_FIPS_E;
static const int DES3_KAT_FIPS_E;
static const int HMAC_KAT_FIPS_E;
static const int RSA_KAT_FIPS_E;
static const int DRBG_KAT_FIPS_E;
static const int DRBG_CONT_FIPS_E;
static const int AESGCM_KAT_FIPS_E;
static const int THREAD_STORE_KEY_E;
static const int THREAD_STORE_SET_E;
static const int MAC_CMP_FAILED_E;
static const int IS_POINT_E;
static const int ECC_INF_E;
static const int ECC_PRIV_KEY_E;
static const int ECC_OUT_OF_RANGE_E;
static const int SRP_CALL_ORDER_E;
static const int SRP_VERIFY_E;
static const int SRP_BAD_KEY_E;
static const int ASN_NO_SKID;
static const int ASN_NO_AKID;
static const int ASN_NO_KEYUSAGE;
static const int SKID_E;
static const int AKID_E;
static const int KEYUSAGE_E;
static const int CERTPOLICIES_E;
static const int WC_INIT_E;
static const int SIG_VERIFY_E;
static const int BAD_COND_E;
static const int SIG_TYPE_E;
static const int HASH_TYPE_E;
static const int FIPS_INVALID_VER_E;
static const int WC_KEY_SIZE_E;
static const int ASN_COUNTRY_SIZE_E;
static const int MISSING_RNG_E;
static const int ASN_PATHLEN_SIZE_E;
static const int ASN_PATHLEN_INV_E;
static const int BAD_KEYWRAP_ALG_E;
static const int BAD_KEYWRAP_IV_E;
static const int WC_CLEANUP_E;
static const int ECC_CDH_KAT_FIPS_E;
static const int DH_CHECK_PUB_E;
static const int BAD_PATH_ERROR;
static const int ASYNC_OP_E;
static const int ECC_PRIVATEONLY_E;
static const int EXTKEYUSAGE_E;
static const int WC_HW_E;
static const int WC_HW_WAIT_E;
static const int PSS_SALTLEN_E;
static const int PRIME_GEN_E;
static const int BER_INDEF_E;
static const int RSA_OUT_OF_RANGE_E;
static const int RSAPSS_PAT_FIPS_E;
static const int ECDSA_PAT_FIPS_E;
static const int DH_KAT_FIPS_E;
static const int AESCCM_KAT_FIPS_E;
static const int SHA3_KAT_FIPS_E;
static const int ECDHE_KAT_FIPS_E;
static const int AES_GCM_OVERFLOW_E;
static const int AES_CCM_OVERFLOW_E;
static const int RSA_KEY_PAIR_E;
static const int DH_CHECK_PRIV_E;
static const int WC_AFALG_SOCK_E;
static const int WC_DEVCRYPTO_E;
static const int ZLIB_INIT_ERROR;
static const int ZLIB_COMPRESS_ERROR;
static const int ZLIB_DECOMPRESS_ERROR;
static const int PKCS7_NO_SIGNER_E;
static const int WC_PKCS7_WANT_READ_E;
static const int CRYPTOCB_UNAVAILABLE;
static const int PKCS7_SIGNEEDS_CHECK;
static const int PSS_SALTLEN_RECOVER_E;
static const int CHACHA_POLY_OVERFLOW;
static const int ASN_SELF_SIGNED_E;
static const int SAKKE_VERIFY_FAIL_E;
static const int MISSING_IV;
static const int MISSING_KEY;
static const int BAD_LENGTH_E;
static const int ECDSA_KAT_FIPS_E;
static const int RSA_PAT_FIPS_E;
static const int KDF_TLS12_KAT_FIPS_E;
static const int KDF_TLS13_KAT_FIPS_E;
static const int KDF_SSH_KAT_FIPS_E;
static const int DHE_PCT_E;
static const int ECC_PCT_E;
static const int FIPS_PRIVATE_KEY_LOCKED_E;
static const int PROTOCOLCB_UNAVAILABLE;
static const int AES_SIV_AUTH_E;
static const int NO_VALID_DEVID;
static const int IO_FAILED_E;
static const int SYSLIB_FAILED_E;
static const int USE_HW_PSK;
static const int ENTROPY_RT_E;
static const int ENTROPY_APT_E;
static const int ASN_DEPTH_E;
static const int ASN_LEN_E;
static const int SM4_GCM_AUTH_E;
static const int SM4_CCM_AUTH_E;
static const int WC_SPAN1_LAST_E;
static const int WC_SPAN1_MIN_CODE_E;
static const int WC_SPAN2_FIRST_E;
static const int DEADLOCK_AVERTED_E;
static const int ASCON_AUTH_E;
static const int WC_ACCEL_INHIBIT_E;
static const int BAD_INDEX_E;
static const int INTERRUPTED_E;
static const int WC_SPAN2_LAST_E;
static const int WC_LAST_E;
static const int WC_SPAN2_MIN_CODE_E;
static const int MIN_CODE_E;
const char* wc_GetErrorString(int error);
"""
if not features["FIPS"] or features["FIPS_VERSION"] > 2:
cdef += """
int wc_GenerateSeed(OS_Seed* os, byte* seed, word32 sz);
"""
if features["MPAPI"]:
cdef += """
typedef struct { ...; } mp_int;
int mp_init (mp_int * a);
int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
int mp_to_unsigned_bin_len (mp_int * a, unsigned char *b, int c);
int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
"""
if features["SHA"]:
cdef += """
typedef struct { ...; } wc_Sha;
int wc_InitSha(wc_Sha*);
int wc_ShaUpdate(wc_Sha*, const byte*, word32);
int wc_ShaFinal(wc_Sha*, byte*);
"""
if features["SHA256"]:
cdef += """
typedef struct { ...; } wc_Sha256;
int wc_InitSha256(wc_Sha256*);
int wc_Sha256Update(wc_Sha256*, const byte*, word32);
int wc_Sha256Final(wc_Sha256*, byte*);
"""
if features["SHA384"]:
cdef += """
typedef struct { ...; } wc_Sha384;
int wc_InitSha384(wc_Sha384*);
int wc_Sha384Update(wc_Sha384*, const byte*, word32);
int wc_Sha384Final(wc_Sha384*, byte*);
"""
if features["SHA512"]:
cdef += """
typedef struct { ...; } wc_Sha512;
int wc_InitSha512(wc_Sha512*);
int wc_Sha512Update(wc_Sha512*, const byte*, word32);
int wc_Sha512Final(wc_Sha512*, byte*);
"""
if features["SHA3"]:
cdef += """
typedef struct { ...; } wc_Sha3;
int wc_InitSha3_224(wc_Sha3*, void *, int);
int wc_InitSha3_256(wc_Sha3*, void *, int);
int wc_InitSha3_384(wc_Sha3*, void *, int);
int wc_InitSha3_512(wc_Sha3*, void *, int);
int wc_Sha3_224_Update(wc_Sha3*, const byte*, word32);
int wc_Sha3_256_Update(wc_Sha3*, const byte*, word32);
int wc_Sha3_384_Update(wc_Sha3*, const byte*, word32);
int wc_Sha3_512_Update(wc_Sha3*, const byte*, word32);
int wc_Sha3_224_Final(wc_Sha3*, byte*);
int wc_Sha3_256_Final(wc_Sha3*, byte*);
int wc_Sha3_384_Final(wc_Sha3*, byte*);
int wc_Sha3_512_Final(wc_Sha3*, byte*);
"""
if features["DES3"]:
cdef += """
typedef struct { ...; } Des3;
int wc_Des3_SetKey(Des3*, const byte*, const byte*, int);
int wc_Des3_CbcEncrypt(Des3*, byte*, const byte*, word32);
int wc_Des3_CbcDecrypt(Des3*, byte*, const byte*, word32);
"""
if features["AES"]:
cdef += """
typedef struct { ...; } Aes;
int wc_AesSetKey(Aes*, const byte*, word32, const byte*, int);
int wc_AesCbcEncrypt(Aes*, byte*, const byte*, word32);
int wc_AesCbcDecrypt(Aes*, byte*, const byte*, word32);
int wc_AesCtrEncrypt(Aes*, byte*, const byte*, word32);
"""
if features["AES"] and features["AESGCM_STREAM"]:
cdef += """
int wc_AesInit(Aes* aes, void* heap, int devId);
int wc_AesGcmInit(Aes* aes, const byte* key, word32 len,
const byte* iv, word32 ivSz);
int wc_AesGcmEncryptInit(Aes* aes, const byte* key, word32 len,
const byte* iv, word32 ivSz);
int wc_AesGcmEncryptInit_ex(Aes* aes, const byte* key, word32 len,
byte* ivOut, word32 ivOutSz);
int wc_AesGcmEncryptUpdate(Aes* aes, byte* out, const byte* in,
word32 sz, const byte* authIn, word32 authInSz);
int wc_AesGcmEncryptFinal(Aes* aes, byte* authTag,
word32 authTagSz);
int wc_AesGcmDecryptInit(Aes* aes, const byte* key, word32 len,
const byte* iv, word32 ivSz);
int wc_AesGcmDecryptUpdate(Aes* aes, byte* out, const byte* in,
word32 sz, const byte* authIn, word32 authInSz);
int wc_AesGcmDecryptFinal(Aes* aes, const byte* authTag,
word32 authTagSz);
"""
if features["AES"] and features["AES_SIV"]:
cdef += """
typedef struct AesSivAssoc_s {
const byte* assoc;
word32 assocSz;
} AesSivAssoc;
int wc_AesSivEncrypt(const byte* key, word32 keySz, const byte* assoc,
word32 assocSz, const byte* nonce, word32 nonceSz,
const byte* in, word32 inSz, byte* siv, byte* out);
int wc_AesSivDecrypt(const byte* key, word32 keySz, const byte* assoc,
word32 assocSz, const byte* nonce, word32 nonceSz,
const byte* in, word32 inSz, byte* siv, byte* out);
int wc_AesSivEncrypt_ex(const byte* key, word32 keySz, const AesSivAssoc* assoc,
word32 numAssoc, const byte* nonce, word32 nonceSz,
const byte* in, word32 inSz, byte* siv, byte* out);
int wc_AesSivDecrypt_ex(const byte* key, word32 keySz, const AesSivAssoc* assoc,
word32 numAssoc, const byte* nonce, word32 nonceSz,
const byte* in, word32 inSz, byte* siv, byte* out);
"""
if features["CHACHA"]:
cdef += """
typedef struct { ...; } ChaCha;
int wc_Chacha_SetKey(ChaCha*, const byte*, word32);
int wc_Chacha_SetIV(ChaCha*, const byte*, word32);
int wc_Chacha_Process(ChaCha*, byte*, const byte*,word32);
"""
if features["CHACHA20_POLY1305"]:
cdef += """
typedef struct { ...; } ChaChaPoly_Aead;
int wc_ChaCha20Poly1305_Encrypt(const byte* inKey, const byte* inIV, const byte* inAAD,
word32 inAADLen, const byte* inPlaintext, word32 inPlaintextLen, byte* outCiphertext,
byte* outAuthTag);
int wc_ChaCha20Poly1305_Decrypt(const byte* inKey, const byte* inIV, const byte* inAAD,
word32 inAADLen, const byte* inCiphertext, word32 inCiphertextLen,
const byte* inAuthTag, byte* outPlaintext);
int wc_ChaCha20Poly1305_UpdateAad(ChaChaPoly_Aead* aead,
const byte* inAAD, word32 inAADLen);
int wc_ChaCha20Poly1305_Init(ChaChaPoly_Aead* aead, const byte* inKey, const byte* inIV,
int isEncrypt);
int wc_ChaCha20Poly1305_UpdateData(ChaChaPoly_Aead* aead,
const byte* inData, byte* outData, word32 dataLen);
int wc_ChaCha20Poly1305_Final(ChaChaPoly_Aead* aead, byte* outTag);
int wc_ChaCha20Poly1305_CheckTag(const byte* authtag, const byte* authTagChk);
"""
if features["HMAC"]:
cdef += """
typedef struct { ...; } Hmac;
int wc_HmacInit(Hmac* hmac, void* heap, int devId);
int wc_HmacSetKey(Hmac*, int, const byte*, word32);
int wc_HmacUpdate(Hmac*, const byte*, word32);
int wc_HmacFinal(Hmac*, byte*);
"""
if features["RSA"]:
cdef += """
static const int WC_RSA_PKCSV15_PAD;
static const int WC_RSA_OAEP_PAD;
static const int WC_RSA_PSS_PAD;
static const int WC_RSA_NO_PAD;
static const int WC_MGF1NONE;
static const int WC_MGF1SHA1;
static const int WC_MGF1SHA224;
static const int WC_MGF1SHA256;
static const int WC_MGF1SHA384;
static const int WC_MGF1SHA512;
static const int WC_HASH_TYPE_NONE;
static const int WC_HASH_TYPE_MD2;
static const int WC_HASH_TYPE_MD4;
static const int WC_HASH_TYPE_MD5;
static const int WC_HASH_TYPE_SHA;
static const int WC_HASH_TYPE_SHA224;
static const int WC_HASH_TYPE_SHA256;
static const int WC_HASH_TYPE_SHA384;
static const int WC_HASH_TYPE_SHA512;
static const int WC_HASH_TYPE_MD5_SHA;