All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vladimir Serbinenko <phcoder@gmail.com>
To: grub-devel@gnu.org
Cc: Vladimir Serbinenko <phcoder@gmail.com>
Subject: [PATCH v14 12/15] import_gcry: Fix pylint warnings
Date: Mon,  7 Jul 2025 14:52:18 +0000	[thread overview]
Message-ID: <20250707145318.97596-13-phcoder@gmail.com> (raw)
In-Reply-To: <20250707145318.97596-1-phcoder@gmail.com>

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
---
 util/import_gcry.py | 1276 +++++++++++++++++++++----------------------
 1 file changed, 622 insertions(+), 654 deletions(-)

diff --git a/util/import_gcry.py b/util/import_gcry.py
index 9b0e8d936..086bde77c 100644
--- a/util/import_gcry.py
+++ b/util/import_gcry.py
@@ -1,3 +1,4 @@
+# pylint: disable=invalid-name,line-too-long
 #*
 #*  GRUB  --  GRand Unified Bootloader
 #*  Copyright (C) 2009  Free Software Foundation, Inc.
@@ -27,8 +28,7 @@ def removesuffix(base: str, suffix: str) -> str:
 
     if base.endswith(suffix):
         return base[:-len(suffix)]
-    else:
-        return base
+    return base
 
 
 def removeprefix(base: str, prefix: str) -> str:
@@ -36,8 +36,7 @@ def removeprefix(base: str, prefix: str) -> str:
 
     if base.startswith(prefix):
         return base[len(prefix):]
-    else:
-        return base
+    return base
 
 if len (sys.argv) < 3:
     print ("Usage: %s SOURCE DESTINATION" % sys.argv[0])
@@ -46,44 +45,16 @@ indir = sys.argv[1]
 outdir = sys.argv[2]
 
 basedir = os.path.join (outdir, "lib/libgcrypt-grub")
-try:
-    os.makedirs (basedir)
-except:
-    print ("WARNING: %s already exists" % basedir)
+os.makedirs (basedir, exist_ok=True)
 cipher_dir_in = os.path.join (indir, "cipher")
 cipher_dir_out = os.path.join (basedir, "cipher")
-try:
-    os.makedirs (cipher_dir_out)
-except:
-    print ("WARNING: %s already exists" % cipher_dir_out)
+os.makedirs (cipher_dir_out, exist_ok=True)
 mpidir =  os.path.join (basedir, "mpi")
-try:
-    os.makedirs (mpidir)
-except:
-    print ("WARNING: %s already exists" % mpidir)
+os.makedirs (mpidir, exist_ok=True)
 
 srcdir =  os.path.join (basedir, "src")
-try:
-    os.makedirs (srcdir)
-except:
-    print ("WARNING: %s already exists" % srcdir)
+os.makedirs (srcdir, exist_ok=True)
 
-cipher_files = sorted (os.listdir (cipher_dir_in))
-conf = codecs.open (os.path.join ("grub-core", "Makefile.gcry.def"), "w", "utf-8")
-conf.write ("AutoGen definitions Makefile.tpl;\n\n")
-confutil = codecs.open ("Makefile.utilgcry.def", "w", "utf-8")
-confutil.write ("AutoGen definitions Makefile.tpl;\n\n")
-confutil.write ("library = {\n");
-confutil.write ("  name = libgrubgcry.a;\n");
-confutil.write ("  cflags = '$(CFLAGS_GCRY)';\n");
-confutil.write ("  cppflags = '$(CPPFLAGS_GCRY)';\n");
-confutil.write ("  extra_dist = grub-core/lib/libgcrypt-grub/cipher/ChangeLog;\n");
-confutil.write ("\n");
-
-for src in ['src/const-time.c']:
-    confutil.write ("  common = grub-core/lib/libgcrypt-grub/%s;\n" % src)
-
-confutil.write ("\n");
 
 chlog = ""
 modules_sym_md = []
@@ -123,636 +94,633 @@ mdblocksizes = {"_gcry_digest_spec_crc32" : 64,
                 "_gcry_digest_spec_cshake256": 64,
                 "_gcry_digest_spec_blake2": "GRUB_BLAKE2 ## BS ## _BLOCK_SIZE"}
 
-cryptolist = codecs.open (os.path.join (cipher_dir_out, "crypto.lst"), "w", "utf-8")
-
-# rijndael is the only cipher using aliases. So no need for mangling, just
-# hardcode it
-cryptolist.write ("RIJNDAEL: gcry_rijndael\n");
-cryptolist.write ("RIJNDAEL192: gcry_rijndael\n");
-cryptolist.write ("RIJNDAEL256: gcry_rijndael\n");
-cryptolist.write ("AES128: gcry_rijndael\n");
-cryptolist.write ("AES-128: gcry_rijndael\n");
-cryptolist.write ("AES-192: gcry_rijndael\n");
-cryptolist.write ("AES-256: gcry_rijndael\n");
-
-cryptolist.write ("ADLER32: adler32\n");
-cryptolist.write ("CRC64: crc64\n");
-
-extra_files = {
-    "gcry_camellia": ["camellia.c"], # Main file is camellia-glue.c
-    "gcry_sha512"  : ["hash-common.c"],
-}
-extra_files_list = [x for xs in extra_files.values() for x in xs] + ["pubkey-util.c", "rsa-common.c", "dsa-common.c", "md.c"]
-
-for cipher_file in cipher_files:
-    infile = os.path.join (cipher_dir_in, cipher_file)
-    outfile = os.path.join (cipher_dir_out, cipher_file)
-    if cipher_file == "ChangeLog" or cipher_file == "ChangeLog-2011":
-        continue
-    chlognew = "	* %s" % cipher_file
-    # Unused generic support files
-    if re.match (r"(Makefile\.am|primegen\.c|cipher\.c|cipher-.*\.c|mac-.*\.c|mac\.c|pubkey\.c)$", cipher_file):
-        chlog = "%s%s: Removed\n" % (chlog, chlognew)
-        continue
-    # TODO: Support KDF
-    if re.match (r"(kdf\.c|scrypt\.c)$", cipher_file):
-        chlog = "%s%s: Removed\n" % (chlog, chlognew)
-        continue
-    # TODO: Support chacha20 and poly1305
-    # TODO: Support ECC
-    # TODO: Support quantum-resistant
-    if cipher_file in ["poly1305.c", "chacha20.c", "ecc.c", "elgamal.c",
-                       "sntrup761.c", "mceliece6688128f.c", "kyber-common.c", "kyber.c", "kyber-kdep.c", "kem-ecc.c", "kem.c"] or re.match (r"^ecc-.*\.c$", cipher_file):
-        chlog = "%s%s: Removed\n" % (chlog, chlognew)
-        continue
-    # TODO: Use optimized versions
-    if re.match (r"(.*\.[sS]|.*-intel-shaext\.c|.*-ssse3-i386\.c|.*-ppc\.c|.*-ssse3-amd64\.c|.*-s390x\.c|rijndael-aesni\.c|crc-intel-pclmul\.c|.*-armv8-ce.c|.*-aarch64-ce\.c|.*-p10le\.c|rijndael-padlock.c|.*-ppc[89]le.c|rijndael-vaes.c|rijndael-vaes-i386.c|serpent-avx512-x86.c)$", cipher_file):
-        chlog = "%s%s: Removed\n" % (chlog, chlognew)
-        continue
-    # We use pregenerated version
-    if cipher_file == "gost-s-box.c":
-        chlog = "%s%s: Removed\n" % (chlog, chlognew)
-        continue
-    # Autogenerated files. Not even worth mentionning in ChangeLog
-    if re.match (r"Makefile\.in$", cipher_file):
-        continue
-    nch = False
-    if re.match (r".*\.[ch]$", cipher_file):
-        isc = re.match (r".*\.c$", cipher_file)
-        f = codecs.open (infile, "r", "utf-8")
-        fw = codecs.open (outfile, "w", "utf-8")
-        fw.write ("/* This file was automatically imported with \n")
-        fw.write ("   import_gcry.py. Please don't modify it */\n")
-        add_license = cipher_file == "pubkey-util.c" or (isc and not cipher_file in extra_files_list)
-        if add_license:
-            fw.write ("#include <grub/dl.h>\n")
-        if cipher_file == "camellia.h":
-            fw.write ("#include <grub/misc.h>\n")
-            fw.write ("void camellia_setup128(const unsigned char *key, grub_uint32_t *subkey);\n")
-            fw.write ("void camellia_setup192(const unsigned char *key, grub_uint32_t *subkey);\n")
-            fw.write ("void camellia_setup256(const unsigned char *key, grub_uint32_t *subkey);\n")
-            fw.write ("void camellia_encrypt128(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
-            fw.write ("void camellia_encrypt192(const grub_uint32_t *subkey, grub_uint32_t *io);\n")                      
-            fw.write ("void camellia_encrypt256(const grub_uint32_t *subkey, grub_uint32_t *io);\n")                      
-            fw.write ("void camellia_decrypt128(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
-            fw.write ("void camellia_decrypt192(const grub_uint32_t *subkey, grub_uint32_t *io);\n")                      
-            fw.write ("void camellia_decrypt256(const grub_uint32_t *subkey, grub_uint32_t *io);\n")                      
-            fw.write ("#define memcpy grub_memcpy\n")
-        # Whole libgcrypt is distributed under GPLv3+ or compatible
-        if add_license:
-            fw.write ("GRUB_MOD_LICENSE (\"GPLv3+\");\n")
-
-        ciphernames = []
-        mdnames = []
-        mdctxsizes = []
-        pknames = []
-        hold = False
-        skip = 0
-        skip2 = False
-        ismd = False
-        ismddefine = False
-        mdarg = 0
-        ispk = False
-        iscipher = False
-        iscryptostart = False
-        iscomma = False
-        skip_statement = False
-        skip_comma = False
-        if isc:
-            modname = "gcry_%s" % removesuffix(removesuffix(cipher_file, ".c"), "-glue").replace("-", "_")
-        for line in f:
-            line = line
-            if skip_statement:
-                if not re.search (";", line) is None:
-                    skip_statement = False
-                continue
-            if skip > 0:
-                if line[0] == "}":
-                    skip = skip - 1
-                continue
-            if skip2:
-                if not re.search (" *};", line) is None:
-                    skip2 = False
-                continue
-            if iscryptostart:
-                s = re.search (" *\"([A-Z0-9_a-z-]*)\"", line)
-                if not s is None:
-                    sg = s.groups()[0]
-                    cryptolist.write (("%s: %s\n") % (sg, modname))
-                    iscryptostart = False
-            if ismd:
-                spl = line.split (",")
-                if mdarg + len (spl) > 9 and mdarg <= 9 and ("sizeof" in spl[9-mdarg]):
-                    mdctxsizes.append (spl[9-mdarg].lstrip ().rstrip())
-                mdarg = mdarg + len (spl) - 1
-            if ismd or iscipher or ispk:
-                if not re.search (" *};", line) is None:
-                    escapenl = " \\" if ismddefine else ""
-                    if not iscomma:
-                        fw.write ("    ,%s\n" % escapenl)
-                    fw.write ("    GRUB_UTIL_MODNAME(\"%s\")%s\n" % (modname, escapenl))
-                    if ismd:
-                        if not (mdname in mdblocksizes):
-                            print ("ERROR: Unknown digest blocksize: %s\n"
-                                   % mdname)
-                            exit (1)
-                        fw.write ("    .blocksize = %s%s\n"
-                                  % (mdblocksizes [mdname], escapenl))
-                    ismd = False
-                    ismddefine = False
-                    mdarg = 0
-                    iscipher = False
-                    ispk = False
-                iscomma = not re.search (",$", line) is None
-            # Used only for selftests.
-            m = re.match (r"(static byte|static unsigned char|static const gcry_md_spec_t \* const) (weak_keys_chksum|digest_list)\[[0-9]*\] =", line)
-            if not m is None:
-                skip = 1
-                fname = m.groups ()[1]
-                chmsg = "(%s): Removed." % fname
-                if nch:
-                    chlognew = "%s\n	%s" % (chlognew, chmsg)
-                else:
-                    chlognew = "%s %s" % (chlognew, chmsg)
-                    nch = True
-                continue
-            if (not hold) and (re.match (r"[ \t]*(run_selftests|do_tripledes_set_extra_info),?", line) is not None):
-                iscomma = True
-                line = ""
-            if hold:
+cipher_files = sorted (os.listdir (cipher_dir_in))
+
+with codecs.open (os.path.join (cipher_dir_out, "crypto.lst"), "w", "utf-8") as cryptolist, \
+     codecs.open (os.path.join ("grub-core", "Makefile.gcry.def"), "w", "utf-8") as conf, \
+     codecs.open ("Makefile.utilgcry.def", "w", "utf-8") as confutil:
+
+    conf.write ("AutoGen definitions Makefile.tpl;\n\n")
+    confutil.write ("AutoGen definitions Makefile.tpl;\n\n")
+    confutil.write ("library = {\n")
+    confutil.write ("  name = libgrubgcry.a;\n")
+    confutil.write ("  cflags = '$(CFLAGS_GCRY)';\n")
+    confutil.write ("  cppflags = '$(CPPFLAGS_GCRY)';\n")
+    confutil.write ("  extra_dist = grub-core/lib/libgcrypt-grub/cipher/ChangeLog;\n")
+    confutil.write ("\n")
+
+    for src in ['src/const-time.c']:
+        confutil.write ("  common = grub-core/lib/libgcrypt-grub/%s;\n" % src)
+
+    confutil.write ("\n")
+
+
+    # rijndael is the only cipher using aliases. So no need for mangling, just
+    # hardcode it
+    cryptolist.write ("RIJNDAEL: gcry_rijndael\n")
+    cryptolist.write ("RIJNDAEL192: gcry_rijndael\n")
+    cryptolist.write ("RIJNDAEL256: gcry_rijndael\n")
+    cryptolist.write ("AES128: gcry_rijndael\n")
+    cryptolist.write ("AES-128: gcry_rijndael\n")
+    cryptolist.write ("AES-192: gcry_rijndael\n")
+    cryptolist.write ("AES-256: gcry_rijndael\n")
+
+    cryptolist.write ("ADLER32: adler32\n")
+    cryptolist.write ("CRC64: crc64\n")
+
+    extra_files = {
+        "gcry_camellia": ["camellia.c"], # Main file is camellia-glue.c
+        "gcry_sha512"  : ["hash-common.c"],
+    }
+    extra_files_list = [x for xs in extra_files.values() for x in xs] + [
+        "pubkey-util.c", "rsa-common.c", "dsa-common.c", "md.c"]
+
+    for cipher_file in cipher_files:
+        infile = os.path.join (cipher_dir_in, cipher_file)
+        outfile = os.path.join (cipher_dir_out, cipher_file)
+        if cipher_file in ["ChangeLog", "ChangeLog-2011"]:
+            continue
+        chlognew = "	* %s" % cipher_file
+        # Unused generic support files
+        if re.match (r"(Makefile\.am|primegen\.c|cipher\.c|cipher-.*\.c|mac-.*\.c|mac\.c|pubkey\.c)$", cipher_file):
+            chlog = "%s%s: Removed\n" % (chlog, chlognew)
+            continue
+        # TODO: Support KDF
+        if re.match (r"(kdf\.c|scrypt\.c)$", cipher_file):
+            chlog = "%s%s: Removed\n" % (chlog, chlognew)
+            continue
+        # TODO: Support chacha20 and poly1305
+        # TODO: Support ECC
+        # TODO: Support quantum-resistant
+        if cipher_file in ["poly1305.c", "chacha20.c", "ecc.c", "elgamal.c",
+                           "sntrup761.c", "mceliece6688128f.c", "kyber-common.c",
+                           "kyber.c", "kyber-kdep.c", "kem-ecc.c",
+                           "kem.c"] or re.match (r"^ecc-.*\.c$", cipher_file):
+            chlog = "%s%s: Removed\n" % (chlog, chlognew)
+            continue
+        # TODO: Use optimized versions
+        if re.match (r"(.*\.[sS]|.*-intel-shaext\.c|.*-ssse3-i386\.c|.*-ppc\.c|.*-ssse3-amd64\.c|.*-s390x\.c|rijndael-aesni\.c|crc-intel-pclmul\.c|.*-armv8-ce.c|.*-aarch64-ce\.c|.*-p10le\.c|rijndael-padlock.c|.*-ppc[89]le.c|rijndael-vaes.c|rijndael-vaes-i386.c|serpent-avx512-x86.c)$", cipher_file):
+            chlog = "%s%s: Removed\n" % (chlog, chlognew)
+            continue
+        # We use pregenerated version
+        if cipher_file == "gost-s-box.c":
+            chlog = "%s%s: Removed\n" % (chlog, chlognew)
+            continue
+        # Autogenerated files. Not even worth mentionning in ChangeLog
+        if re.match (r"Makefile\.in$", cipher_file):
+            continue
+        nch = False
+        if re.match (r".*\.[ch]$", cipher_file):
+            isc = re.match (r".*\.c$", cipher_file)
+            with codecs.open (infile, "r", "utf-8") as f, codecs.open (outfile, "w", "utf-8") as fw:
+                fw.write ("/* This file was automatically imported with \n")
+                fw.write ("   import_gcry.py. Please don't modify it */\n")
+                add_license = cipher_file == "pubkey-util.c" or (isc and not cipher_file in extra_files_list)
+                if add_license:
+                    fw.write ("#include <grub/dl.h>\n")
+                if cipher_file == "camellia.h":
+                    fw.write ("#include <grub/misc.h>\n")
+                    fw.write ("void camellia_setup128(const unsigned char *key, grub_uint32_t *subkey);\n")
+                    fw.write ("void camellia_setup192(const unsigned char *key, grub_uint32_t *subkey);\n")
+                    fw.write ("void camellia_setup256(const unsigned char *key, grub_uint32_t *subkey);\n")
+                    fw.write ("void camellia_encrypt128(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
+                    fw.write ("void camellia_encrypt192(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
+                    fw.write ("void camellia_encrypt256(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
+                    fw.write ("void camellia_decrypt128(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
+                    fw.write ("void camellia_decrypt192(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
+                    fw.write ("void camellia_decrypt256(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
+                    fw.write ("#define memcpy grub_memcpy\n")
+                # Whole libgcrypt is distributed under GPLv3+ or compatible
+                if add_license:
+                    fw.write ("GRUB_MOD_LICENSE (\"GPLv3+\");\n")
+
+                ciphernames = []
+                mdnames = []
+                mdctxsizes = []
+                pknames = []
                 hold = False
-                # We're optimising for size and exclude anything needing good
-                # randomness.
-                if re.match ("(_gcry_hash_selftest_check_one|bulk_selftest_setkey|run_selftests|do_tripledes_set_extra_info|selftest|sm4_selftest|_gcry_[a-z0-9_]*_hash_buffers|_gcry_sha1_hash_buffer|tripledes_set2keys|_gcry_rmd160_mixblock|serpent_test|dsa_generate_ext|test_keys|gen_k|sign|gen_x931_parm_xp|generate_x931|generate_key|dsa_generate|dsa_sign|ecc_sign|generate|generate_fips186|_gcry_register_pk_dsa_progress|_gcry_register_pk_ecc_progress|progress|scanval|ec2os|ecc_generate_ext|ecc_generate|ecc_get_param|_gcry_register_pk_dsa_progress|gen_x931_parm_xp|gen_x931_parm_xi|rsa_decrypt|rsa_sign|rsa_generate_ext|rsa_generate|secret|check_exponent|rsa_blind|rsa_unblind|extract_a_from_sexp|curve_free|curve_copy|point_set|_gcry_dsa_gen_rfc6979_k|bits2octets|int2octets|_gcry_md_debug|_gcry_md_selftest|_gcry_md_is_enabled|_gcry_md_is_secure|_gcry_md_init|_gcry_md_info|md_get_algo|md_extract|_gcry_md_get |_gcry_md_get_algo |_gcry_md_extract|_gcry_md_setkey|md_setkey|prepare_macpads|_gcry_md_algo_name|search_oid|spec_from_oid|spec_from_name|spec_from_algo|map_algo|cshake_hash_buffers|selftest_seq)", line) is not None:
-
-                    skip = 1
-                    if not re.match ("selftest", line) is None and cipher_file == "idea.c":
-                        skip = 3
-
-                    if not re.match ("serpent_test", line) is None:
-                        fw.write ("static const char *serpent_test (void) { return 0; }\n");
-                    if not re.match ("sm4_selftest", line) is None:
-                        fw.write ("static const char *sm4_selftest (void) { return 0; }\n");
-                    hash_buf = re.match ("(_gcry_[a-z0-9_]*_hash_buffers)", line)
-                    if hash_buf is not None:
-                        fw.write ("#define %s 0" % (hash_buf.group(0)))
-                    if not re.match ("dsa_generate", line) is None:
-                        fw.write ("#define dsa_generate 0");
-                    if not re.match ("ecc_generate", line) is None:
-                        fw.write ("#define ecc_generate 0");
-                    if not re.match ("rsa_generate ", line) is None:
-                        fw.write ("#define rsa_generate 0");
-                    if not re.match ("rsa_sign", line) is None:
-                        fw.write ("#define rsa_sign 0");
-                    if not re.match ("rsa_decrypt", line) is None:
-                        fw.write ("#define rsa_decrypt 0");
-                    if not re.match ("dsa_sign", line) is None:
-                        fw.write ("#define dsa_sign 0");
-                    if not re.match ("ecc_sign", line) is None:
-                        fw.write ("#define ecc_sign 0");
-                    if not re.match ("search_oid", line) is None:
-                        fw.write ("#define search_oid(a,b) grub_crypto_lookup_md_by_oid(a)")
-                    if not re.match ("spec_from_name", line) is None:
-                        fw.write ("#define spec_from_name grub_crypto_lookup_md_by_name")
-                    fname = re.match ("[a-zA-Z0-9_]*", line).group ()
-                    chmsg = "(%s): Removed." % fname
+                skip = 0
+                skip2 = False
+                ismd = False
+                ismddefine = False
+                mdarg = 0
+                ispk = False
+                iscipher = False
+                iscryptostart = False
+                iscomma = False
+                skip_statement = False
+                skip_comma = False
+                if isc:
+                    modname = "gcry_%s" % removesuffix(removesuffix(cipher_file, ".c"),
+                                                       "-glue").replace("-", "_")
+                for line in f:
+                    if skip_statement:
+                        if not re.search (";", line) is None:
+                            skip_statement = False
+                        continue
+                    if skip > 0:
+                        if line[0] == "}":
+                            skip = skip - 1
+                        continue
+                    if skip2:
+                        if not re.search (" *};", line) is None:
+                            skip2 = False
+                        continue
+                    if iscryptostart:
+                        s = re.search (" *\"([A-Z0-9_a-z-]*)\"", line)
+                        if not s is None:
+                            sg = s.groups()[0]
+                            cryptolist.write (("%s: %s\n") % (sg, modname))
+                            iscryptostart = False
+                    if ismd:
+                        spl = line.split (",")
+                        if mdarg + len (spl) > 9 and mdarg <= 9 and ("sizeof" in spl[9-mdarg]):
+                            mdctxsizes.append (spl[9-mdarg].lstrip ().rstrip())
+                        mdarg = mdarg + len (spl) - 1
+                    if ismd or iscipher or ispk:
+                        if not re.search (" *};", line) is None:
+                            escapenl = " \\" if ismddefine else ""
+                            if not iscomma:
+                                fw.write ("    ,%s\n" % escapenl)
+                            fw.write ("    GRUB_UTIL_MODNAME(\"%s\")%s\n" % (modname, escapenl))
+                            if ismd:
+                                if mdname not in mdblocksizes:
+                                    print ("ERROR: Unknown digest blocksize: %s\n"
+                                           % mdname)
+                                    exit (1)
+                                fw.write ("    .blocksize = %s%s\n"
+                                          % (mdblocksizes [mdname], escapenl))
+                            ismd = False
+                            ismddefine = False
+                            mdarg = 0
+                            iscipher = False
+                            ispk = False
+                        iscomma = not re.search (",$", line) is None
+                    # Used only for selftests.
+                    m = re.match (r"(static byte|static unsigned char|static const gcry_md_spec_t \* const) (weak_keys_chksum|digest_list)\[[0-9]*\] =", line)
+                    if not m is None:
+                        skip = 1
+                        fname = m.groups ()[1]
+                        chmsg = "(%s): Removed." % fname
+                        if nch:
+                            chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        else:
+                            chlognew = "%s %s" % (chlognew, chmsg)
+                            nch = True
+                        continue
+                    if (not hold) and (re.match (r"[ \t]*(run_selftests|do_tripledes_set_extra_info),?", line) is not None):
+                        iscomma = True
+                        line = ""
+                    if hold:
+                        hold = False
+                        # We're optimising for size and exclude anything needing good
+                        # randomness.
+                        if re.match ("(_gcry_hash_selftest_check_one|bulk_selftest_setkey|run_selftests|do_tripledes_set_extra_info|selftest|sm4_selftest|_gcry_[a-z0-9_]*_hash_buffers|_gcry_sha1_hash_buffer|tripledes_set2keys|_gcry_rmd160_mixblock|serpent_test|dsa_generate_ext|test_keys|gen_k|sign|gen_x931_parm_xp|generate_x931|generate_key|dsa_generate|dsa_sign|ecc_sign|generate|generate_fips186|_gcry_register_pk_dsa_progress|_gcry_register_pk_ecc_progress|progress|scanval|ec2os|ecc_generate_ext|ecc_generate|ecc_get_param|_gcry_register_pk_dsa_progress|gen_x931_parm_xp|gen_x931_parm_xi|rsa_decrypt|rsa_sign|rsa_generate_ext|rsa_generate|secret|check_exponent|rsa_blind|rsa_unblind|extract_a_from_sexp|curve_free|curve_copy|point_set|_gcry_dsa_gen_rfc6979_k|bits2octets|int2octets|_gcry_md_debug|_gcry_md_selftest|_gcry_md_is_enabled|_gcry_md_is_secure|_gcry_md_init|_gcry_md_info|md_get_algo|md_extract|_gcry_md_get |_gcry_md_get_algo |_gcry_md_extract|_gcry_md_setkey|md_setkey|prepare_macpads|_gcry_md_algo_name|search_oid|spec_from_oid|spec_from_name|spec_from_algo|map_algo|cshake_hash_buffers|selftest_seq)", line) is not None:
+                            skip = 1
+                            if not re.match ("selftest", line) is None and cipher_file == "idea.c":
+                                skip = 3
+
+                            if not re.match ("serpent_test", line) is None:
+                                fw.write ("static const char *serpent_test (void) { return 0; }\n")
+                            if not re.match ("sm4_selftest", line) is None:
+                                fw.write ("static const char *sm4_selftest (void) { return 0; }\n")
+                            hash_buf = re.match ("(_gcry_[a-z0-9_]*_hash_buffers)", line)
+                            if hash_buf is not None:
+                                fw.write ("#define %s 0" % (hash_buf.group(0)))
+                            if not re.match ("dsa_generate", line) is None:
+                                fw.write ("#define dsa_generate 0")
+                            if not re.match ("ecc_generate", line) is None:
+                                fw.write ("#define ecc_generate 0")
+                            if not re.match ("rsa_generate ", line) is None:
+                                fw.write ("#define rsa_generate 0")
+                            if not re.match ("rsa_sign", line) is None:
+                                fw.write ("#define rsa_sign 0")
+                            if not re.match ("rsa_decrypt", line) is None:
+                                fw.write ("#define rsa_decrypt 0")
+                            if not re.match ("dsa_sign", line) is None:
+                                fw.write ("#define dsa_sign 0")
+                            if not re.match ("ecc_sign", line) is None:
+                                fw.write ("#define ecc_sign 0")
+                            if not re.match ("search_oid", line) is None:
+                                fw.write ("#define search_oid(a,b) grub_crypto_lookup_md_by_oid(a)")
+                            if not re.match ("spec_from_name", line) is None:
+                                fw.write ("#define spec_from_name grub_crypto_lookup_md_by_name")
+                            fname = re.match ("[a-zA-Z0-9_]*", line).group ()
+                            chmsg = "(%s): Removed." % fname
+                            if nch:
+                                chlognew = "%s\n	%s" % (chlognew, chmsg)
+                            else:
+                                chlognew = "%s %s" % (chlognew, chmsg)
+                                nch = True
+                            continue
+                        else:
+                            fw.write (holdline)
+                    m = re.match ("# *include <(.*)>", line)
+                    if not m is None:
+                        chmsg = "Removed including of %s" % m.groups ()[0]
+                        if nch:
+                            chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        else:
+                            chlognew = "%s: %s" % (chlognew, chmsg)
+                            nch = True
+                        continue
+                    m = re.match ("(const )?gcry_cipher_spec_t", line)
+                    if isc and not m is None:
+                        assert not ismd
+                        assert not ispk
+                        assert not iscipher
+                        assert not iscryptostart
+                        ciphername = removeprefix(removeprefix(line, "const "),
+                                                  "gcry_cipher_spec_t").strip ()
+                        ciphername = re.match("[a-zA-Z0-9_]*",ciphername).group ()
+                        ciphernames.append (ciphername)
+                        iscipher = True
+                        iscryptostart = True
+
+                    m = re.match ("(const )?gcry_pk_spec_t", line)
+                    if isc and not m is None:
+                        assert not ismd
+                        assert not ispk
+                        assert not iscipher
+                        assert not iscryptostart
+                        pkname = removeprefix(removeprefix(line, "const "), "gcry_pk_spec_t").strip ()
+                        pkname = re.match("[a-zA-Z0-9_]*",pkname).group ()
+                        pknames.append (pkname)
+                        ispk = True
+                        iscryptostart = True
+
+                    m = re.match (r"DEFINE_BLAKE2_VARIANT\((.), (.), ([0-9]*)", line)
+                    if isc and not m is None:
+                        bs = m.groups()[0]
+                        bits = m.groups()[2]
+                        mdname = "_gcry_digest_spec_blake2%s_%s" % (bs, bits)
+                        mdnames.append (mdname)
+
+                    m = re.match ("(const )?gcry_md_spec_t", line)
+                    if isc and not m is None:
+                        assert not ismd
+                        assert not ispk
+                        assert not iscipher
+                        assert not iscryptostart
+                        line = removeprefix(line, "const ")
+                        mdname = removeprefix(removeprefix(line, "const "), "gcry_md_spec_t").strip ()
+                        mdname = re.match("[a-zA-Z0-9_]*",mdname).group ()
+                        mdnames.append (mdname)
+                        ismd = True
+                        ismddefine = False
+                        mdarg = 0
+                        iscryptostart = True
+                    m = re.match ("  (const )?gcry_md_spec_t _gcry_digest_spec_blake2.*\\\\", line)
+                    if isc and not m is None:
+                        assert not ismd
+                        assert not ispk
+                        assert not iscipher
+                        assert not iscryptostart
+                        line = removeprefix(line, "  const ")
+                        ismd = True
+                        ismddefine = True
+                        mdname = "_gcry_digest_spec_blake2"
+                        mdarg = 0
+                        iscryptostart = True
+                    m = re.match (r"static const char \*selftest.*;$", line)
+                    if not m is None:
+                        fname = line[len (r"static const char \*"):]
+                        fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
+                        chmsg = "(%s): Removed declaration." % fname
+                        if nch:
+                            chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        else:
+                            chlognew = "%s %s" % (chlognew, chmsg)
+                            nch = True
+                        continue
+                    m = re.match ("static gcry_mpi_t gen_k .*;$", line)
+                    if not m is None:
+                        chmsg = "(gen_k): Removed declaration."
+                        if nch:
+                            chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        else:
+                            chlognew = "%s %s" % (chlognew, chmsg)
+                            nch = True
+                        continue
+                    m = re.match ("static (int|void) test_keys .*;$", line)
+                    if not m is None:
+                        chmsg = "(test_keys): Removed declaration."
+                        if nch:
+                            chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        else:
+                            chlognew = "%s %s" % (chlognew, chmsg)
+                            nch = True
+                        continue
+                    m = re.match ("static void secret .*;$", line)
+                    if not m is None:
+                        chmsg = "(secret): Removed declaration."
+                        if nch:
+                            chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        else:
+                            chlognew = "%s %s" % (chlognew, chmsg)
+                            nch = True
+                        continue
+                    m = re.match (r"static void \(\*progress_cb\).*;$", line)
+                    if not m is None:
+                        chmsg = "(progress_cb): Removed declaration."
+                        if nch:
+                            chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        else:
+                            chlognew = "%s %s" % (chlognew, chmsg)
+                            nch = True
+                        continue
+                    m = re.match (r"static void \*progress_cb_data.*;$", line)
+                    if not m is None:
+                        chmsg = "(progress_cb): Removed declaration."
+                        if nch:
+                            chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        else:
+                            chlognew = "%s %s" % (chlognew, chmsg)
+                            nch = True
+                        continue
+
+                    m = re.match (r"((static )?const char( |)\*|static const gcry_md_spec_t \*|(static )?gpg_err_code_t|gpg_error_t|void|(static )?int|(static )?unsigned int|(static )?gcry_err_code_t|static gcry_mpi_t|static void|void|static elliptic_curve_t) *$", line)
+                    if not m is None:
+                        hold = True
+                        holdline = line
+                        continue
+                    m = re.match (r"static int tripledes_set2keys \(.*\);", line)
+                    if not m is None:
+                        continue
+                    m = re.match (r"static int tripledes_set3keys \(.*\);", line)
+                    if not m is None:
+                        continue
+                    m = re.match (r"static int tripledes_set2keys \(", line)
+                    if not m is None:
+                        skip_statement = True
+                        continue
+                    m = re.match (r"static int tripledes_set3keys \(", line)
+                    if not m is None:
+                        skip_statement = True
+                        continue
+                    m = re.match ("static const char sample_secret_key", line)
+                    if not m is None:
+                        skip_statement = True
+                        continue
+                    m = re.match ("static const char sample_public_key", line)
+                    if not m is None:
+                        skip_statement = True
+                        continue
+                    m = re.match ("static void sign|static gpg_err_code_t sign|static gpg_err_code_t generate",
+                                  line)
+                    if not m is None:
+                        skip_statement = True
+                        continue
+
+                    m = re.match ("cipher_extra_spec_t", line)
+                    if isc and not m is None:
+                        skip2 = True
+                        fname = line[len ("cipher_extra_spec_t "):]
+                        fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
+                        chmsg = "(%s): Removed." % fname
+                        if nch:
+                            chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        else:
+                            chlognew = "%s %s" % (chlognew, chmsg)
+                            nch = True
+                        continue
+                    m = re.match ("pk_extra_spec_t", line)
+                    if isc and not m is None:
+                        skip2 = True
+                        fname = line[len ("pk_extra_spec_t "):]
+                        fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
+                        chmsg = "(%s): Removed." % fname
+                        if nch:
+                            chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        else:
+                            chlognew = "%s %s" % (chlognew, chmsg)
+                            nch = True
+                        continue
+                    m = re.match ("md_extra_spec_t", line)
+                    if isc and not m is None:
+                        skip2 = True
+                        fname = line[len ("md_extra_spec_t "):]
+                        fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
+                        chmsg = "(%s): Removed." % fname
+                        if nch:
+                            chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        else:
+                            chlognew = "%s %s" % (chlognew, chmsg)
+                            nch = True
+                        continue
+                    fw.write (line)
+                if len (ciphernames) > 0 or len (mdnames) > 0 or len (pknames) > 0:
+                    modfiles = [cipher_file]
+                    if modname in extra_files:
+                        modfiles += extra_files[modname]
+                    if len (ciphernames) > 0 or len (mdnames) > 0:
+                        modules_sym_md.append (modname)
+                    chmsg = "(GRUB_MOD_INIT(%s)): New function\n" % modname
                     if nch:
                         chlognew = "%s\n	%s" % (chlognew, chmsg)
                     else:
-                        chlognew = "%s %s" % (chlognew, chmsg)
-                        nch = True                        
-                    continue
-                else:
-                    fw.write (holdline)
-            m = re.match ("# *include <(.*)>", line)
-            if not m is None:
-                chmsg = "Removed including of %s" % m.groups ()[0]
-                if nch:
-                    chlognew = "%s\n	%s" % (chlognew, chmsg)
-                else:
-                    chlognew = "%s: %s" % (chlognew, chmsg)
-                    nch = True
-                continue
-            m = re.match ("(const )?gcry_cipher_spec_t", line)
-            if isc and not m is None:
-                assert (not ismd)
-                assert (not ispk)
-                assert (not iscipher)
-                assert (not iscryptostart)
-                ciphername = removeprefix(removeprefix(line, "const "), "gcry_cipher_spec_t").strip ()
-                ciphername = re.match("[a-zA-Z0-9_]*",ciphername).group ()
-                ciphernames.append (ciphername)
-                iscipher = True
-                iscryptostart = True
-
-            m = re.match ("(const )?gcry_pk_spec_t", line)
-            if isc and not m is None:
-                assert (not ismd)
-                assert (not ispk)
-                assert (not iscipher)
-                assert (not iscryptostart)
-                pkname = removeprefix(removeprefix(line, "const "), "gcry_pk_spec_t").strip ()
-                pkname = re.match("[a-zA-Z0-9_]*",pkname).group ()
-                pknames.append (pkname)
-                ispk = True
-                iscryptostart = True
-
-            m = re.match (r"DEFINE_BLAKE2_VARIANT\((.), (.), ([0-9]*)", line)
-            if isc and not m is None:
-                bs = m.groups()[0]
-                bits = m.groups()[2]
-                mdname = "_gcry_digest_spec_blake2%s_%s" % (bs, bits)
-                mdnames.append (mdname)
-
-            m = re.match ("(const )?gcry_md_spec_t", line)
-            if isc and not m is None:
-                assert (not ismd)
-                assert (not ispk)
-                assert (not iscipher)
-                assert (not iscryptostart)
-                line = removeprefix(line, "const ")
-                mdname = removeprefix(removeprefix(line, "const "), "gcry_md_spec_t").strip ()
-                mdname = re.match("[a-zA-Z0-9_]*",mdname).group ()
-                mdnames.append (mdname)
-                ismd = True
-                ismddefine = False
-                mdarg = 0
-                iscryptostart = True
-            m = re.match ("  (const )?gcry_md_spec_t _gcry_digest_spec_blake2.*\\\\", line)
-            if isc and not m is None:
-                assert (not ismd)
-                assert (not ispk)
-                assert (not iscipher)
-                assert (not iscryptostart)
-                line = removeprefix(line, "  const ")
-                ismd = True
-                ismddefine = True
-                mdname = "_gcry_digest_spec_blake2"
-                mdarg = 0
-                iscryptostart = True
-            m = re.match (r"static const char \*selftest.*;$", line)
-            if not m is None:
-                fname = line[len (r"static const char \*"):]
-                fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
-                chmsg = "(%s): Removed declaration." % fname
-                if nch:
-                    chlognew = "%s\n	%s" % (chlognew, chmsg)
-                else:
-                    chlognew = "%s %s" % (chlognew, chmsg)
-                    nch = True
-                continue
-            m = re.match ("static gcry_mpi_t gen_k .*;$", line)
-            if not m is None:
-                chmsg = "(gen_k): Removed declaration."
-                if nch:
-                    chlognew = "%s\n	%s" % (chlognew, chmsg)
-                else:
-                    chlognew = "%s %s" % (chlognew, chmsg)
-                    nch = True
-                continue
-            m = re.match ("static (int|void) test_keys .*;$", line)
-            if not m is None:
-                chmsg = "(test_keys): Removed declaration."
-                if nch:
-                    chlognew = "%s\n	%s" % (chlognew, chmsg)
-                else:
-                    chlognew = "%s %s" % (chlognew, chmsg)
-                    nch = True
-                continue
-            m = re.match ("static void secret .*;$", line)
-            if not m is None:
-                chmsg = "(secret): Removed declaration."
-                if nch:
+                        chlognew = "%s%s" % (chlognew, chmsg)
+                        nch = True
+                    fw.write ("\n\nGRUB_MOD_INIT(%s)\n" % modname)
+                    fw.write ("{\n")
+                    for ciphername in ciphernames:
+                        chmsg = "Register cipher %s" % ciphername
+                        chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        fw.write ("  grub_cipher_register (&%s);\n" % ciphername)
+                    for ctxsize in mdctxsizes:
+                        fw.write ("  COMPILE_TIME_ASSERT(%s <= GRUB_CRYPTO_MAX_MD_CONTEXT_SIZE);\n" % ctxsize)
+                    for mdname in mdnames:
+                        chmsg = "Register digest %s" % mdname
+                        chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        fw.write ("  grub_md_register (&%s);\n" % mdname)
+                    for pkname in pknames:
+                        chmsg = "Register pk %s" % pkname
+                        chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        fw.write ("  grub_crypto_pk_%s = &%s;\n"
+                                  % (pkname.replace ("_gcry_pubkey_spec_", ""), pkname))
+                    fw.write ("}")
+                    chmsg = "(GRUB_MOD_FINI(%s)): New function\n" % modname
                     chlognew = "%s\n	%s" % (chlognew, chmsg)
-                else:
-                    chlognew = "%s %s" % (chlognew, chmsg)
-                    nch = True
+                    fw.write ("\n\nGRUB_MOD_FINI(%s)\n" % modname)
+                    fw.write ("{\n")
+                    for ciphername in ciphernames:
+                        chmsg = "Unregister cipher %s" % ciphername
+                        chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        fw.write ("  grub_cipher_unregister (&%s);\n" % ciphername)
+                    for mdname in mdnames:
+                        chmsg = "Unregister MD %s" % mdname
+                        chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        fw.write ("  grub_md_unregister (&%s);\n" % mdname)
+                    for pkname in pknames:
+                        chmsg = "Unregister pk %s" % pkname
+                        chlognew = "%s\n	%s" % (chlognew, chmsg)
+                        fw.write ("  grub_crypto_pk_%s = 0;\n"
+                                  % (pkname.replace ("_gcry_pubkey_spec_", "")))
+                    fw.write ("}\n")
+                    conf.write ("module = {\n")
+                    conf.write ("  name = %s;\n" % modname)
+                    for src in modfiles:
+                        conf.write ("  common = lib/libgcrypt-grub/cipher/%s;\n" % src)
+                        if len (ciphernames) > 0 or len (mdnames) > 0:
+                            confutil.write ("  common = grub-core/lib/libgcrypt-grub/cipher/%s;\n" % src)
+                    if modname == "gcry_ecc":
+                        conf.write ("  common = lib/libgcrypt-grub/mpi/ec.c;\n")
+                        conf.write ("  cflags = '$(CFLAGS_GCRY) -Wno-redundant-decls';\n")
+                    elif modname in ["gcry_rijndael", "gcry_md4", "gcry_md5", "gcry_rmd160", "gcry_sha1", "gcry_sha256", "gcry_sha512", "gcry_tiger"]:
+                        # Alignment checked by hand
+                        conf.write ("  cflags = '$(CFLAGS_GCRY) -Wno-cast-align';\n")
+                    else:
+                        conf.write ("  cflags = '$(CFLAGS_GCRY)';\n")
+                    conf.write ("  cppflags = '$(CPPFLAGS_GCRY)';\n")
+                    conf.write ("};\n\n")
+                    if nch:
+                        chlog = "%s%s\n" % (chlog, chlognew)
+                elif isc and cipher_file not in extra_files_list:
+                    print ("WARNING: C file isn't a module: %s" % cipher_file)
+                    os.remove (outfile)
+                    chlog = "%s\n	* %s: Removed" % (chlog, cipher_file)
                 continue
-            m = re.match (r"static void \(\*progress_cb\).*;$", line)
-            if not m is None:
-                chmsg = "(progress_cb): Removed declaration."
-                if nch:
-                    chlognew = "%s\n	%s" % (chlognew, chmsg)
-                else:
-                    chlognew = "%s %s" % (chlognew, chmsg)
-                    nch = True
+        chlog = "%s%sSkipped unknown file\n" % (chlog, chlognew)
+        print ("WARNING: unknown file %s" % cipher_file)
+
+    for src in sorted (os.listdir (os.path.join (indir, "src"))):
+        if src in ["versioninfo.rc.in", "ath.c", "ChangeLog-2011",
+                   "dumpsexp.c", "fips.c", "gcrypt.h.in",
+                   "gcryptrnd.c", "getrandom.c",
+                   "global.c", "hmac256.c",
+                   "hwfeatures.c", "libgcrypt-config.in",
+                   "libgcrypt.def", "libgcrypt.m4",
+                   "libgcrypt.vers", "Makefile.am",
+                   "Manifest", "misc.c",
+                   "missing-string.c", "module.c",
+                   "secmem.c",
+                   "stdmem.c", "visibility.c"]:
+            continue
+        outfile = os.path.join (basedir, "src", src)
+        infile = os.path.join (indir, "src", src)
+        if os.path.isdir (infile):
+            continue
+        with codecs.open (outfile, "w", "utf-8") as fw:
+            if src == "gcrypt-module.h":
                 continue
-            m = re.match (r"static void \*progress_cb_data.*;$", line)
-            if not m is None:
-                chmsg = "(progress_cb): Removed declaration."
-                if nch:
-                    chlognew = "%s\n	%s" % (chlognew, chmsg)
-                else:
-                    chlognew = "%s %s" % (chlognew, chmsg)
-                    nch = True
+            if src == "visibility.h":
+                fw.write ("# include <grub/gcrypt/gcrypt.h>\n")
                 continue
+            with codecs.open (infile, "r", "utf-8") as f:
+                if src == "types.h":
+                    fw.write (f.read ().replace ("float f;", "").replace ("double g;", ""))
+                    continue
 
-            m = re.match (r"((static )?const char( |)\*|static const gcry_md_spec_t \*|(static )?gpg_err_code_t|gpg_error_t|void|(static )?int|(static )?unsigned int|(static )?gcry_err_code_t|static gcry_mpi_t|static void|void|static elliptic_curve_t) *$", line)
-            if not m is None:
-                hold = True
-                holdline = line
-                continue
-            m = re.match (r"static int tripledes_set2keys \(.*\);", line)
-            if not m is None:
-                continue
-            m = re.match (r"static int tripledes_set3keys \(.*\);", line)
-            if not m is None:
-                continue
-            m = re.match (r"static int tripledes_set2keys \(", line)
-            if not m is None:
-                skip_statement = True
-                continue
-            m = re.match (r"static int tripledes_set3keys \(", line)
-            if not m is None:
-                skip_statement = True
-                continue
-            m = re.match ("static const char sample_secret_key", line)
-            if not m is None:
-                skip_statement = True
-                continue
-            m = re.match ("static const char sample_public_key", line)
-            if not m is None:
-                skip_statement = True
-                continue
-            m = re.match ("static void sign|static gpg_err_code_t sign|static gpg_err_code_t generate",
-                          line)
-            if not m is None:
-                skip_statement = True
-                continue
+                if src == "cipher-proto.h":
+                    fw.write("#include <grub/crypto.h>\n")
+                    fw.write("typedef gcry_selftest_func_t selftest_func_t;")
+                    continue
 
-            m = re.match ("cipher_extra_spec_t", line)
-            if isc and not m is None:
-                skip2 = True
-                fname = line[len ("cipher_extra_spec_t "):]
-                fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
-                chmsg = "(%s): Removed." % fname
-                if nch:
-                    chlognew = "%s\n	%s" % (chlognew, chmsg)
-                else:
-                    chlognew = "%s %s" % (chlognew, chmsg)
-                    nch = True
-                continue
-            m = re.match ("pk_extra_spec_t", line)
-            if isc and not m is None:
-                skip2 = True
-                fname = line[len ("pk_extra_spec_t "):]
-                fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
-                chmsg = "(%s): Removed." % fname
-                if nch:
-                    chlognew = "%s\n	%s" % (chlognew, chmsg)
-                else:
-                    chlognew = "%s %s" % (chlognew, chmsg)
-                    nch = True
-                continue
-            m = re.match ("md_extra_spec_t", line)
-            if isc and not m is None:
-                skip2 = True
-                fname = line[len ("md_extra_spec_t "):]
-                fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
-                chmsg = "(%s): Removed." % fname
-                if nch:
-                    chlognew = "%s\n	%s" % (chlognew, chmsg)
-                else:
-                    chlognew = "%s %s" % (chlognew, chmsg)
-                    nch = True
-                continue
-            fw.write (line)
-        if len (ciphernames) > 0 or len (mdnames) > 0 or len (pknames) > 0:
-            modfiles = [cipher_file]
-            if modname in extra_files.keys():
-                modfiles += extra_files[modname]
-            if len (ciphernames) > 0 or len (mdnames) > 0:
-                modules_sym_md.append (modname)
-            chmsg = "(GRUB_MOD_INIT(%s)): New function\n" % modname
-            if nch:
-                chlognew = "%s\n	%s" % (chlognew, chmsg)
-            else:
-                chlognew = "%s%s" % (chlognew, chmsg)
-                nch = True
-            fw.write ("\n\nGRUB_MOD_INIT(%s)\n" % modname)
-            fw.write ("{\n")
-            for ciphername in ciphernames:
-                chmsg = "Register cipher %s" % ciphername
-                chlognew = "%s\n	%s" % (chlognew, chmsg)
-                fw.write ("  grub_cipher_register (&%s);\n" % ciphername)
-            for ctxsize in mdctxsizes:
-                fw.write ("  COMPILE_TIME_ASSERT(%s <= GRUB_CRYPTO_MAX_MD_CONTEXT_SIZE);\n" % ctxsize)
-            for mdname in mdnames:
-                chmsg = "Register digest %s" % mdname
-                chlognew = "%s\n	%s" % (chlognew, chmsg)
-                fw.write ("  grub_md_register (&%s);\n" % mdname)
-            for pkname in pknames:
-                chmsg = "Register pk %s" % pkname
-                chlognew = "%s\n	%s" % (chlognew, chmsg)
-                fw.write ("  grub_crypto_pk_%s = &%s;\n"
-                          % (pkname.replace ("_gcry_pubkey_spec_", ""), pkname))
-            fw.write ("}")
-            chmsg = "(GRUB_MOD_FINI(%s)): New function\n" % modname
-            chlognew = "%s\n	%s" % (chlognew, chmsg)
-            fw.write ("\n\nGRUB_MOD_FINI(%s)\n" % modname)
-            fw.write ("{\n")
-            for ciphername in ciphernames:
-                chmsg = "Unregister cipher %s" % ciphername
-                chlognew = "%s\n	%s" % (chlognew, chmsg)
-                fw.write ("  grub_cipher_unregister (&%s);\n" % ciphername)
-            for mdname in mdnames:
-                chmsg = "Unregister MD %s" % mdname
-                chlognew = "%s\n	%s" % (chlognew, chmsg)
-                fw.write ("  grub_md_unregister (&%s);\n" % mdname)
-            for pkname in pknames:
-                chmsg = "Unregister pk %s" % pkname
-                chlognew = "%s\n	%s" % (chlognew, chmsg)
-                fw.write ("  grub_crypto_pk_%s = 0;\n"
-                          % (pkname.replace ("_gcry_pubkey_spec_", "")))
-            fw.write ("}\n")
-            conf.write ("module = {\n")
-            conf.write ("  name = %s;\n" % modname)
-            for src in modfiles:
-                conf.write ("  common = lib/libgcrypt-grub/cipher/%s;\n" % src)
-                if len (ciphernames) > 0 or len (mdnames) > 0:
-                    confutil.write ("  common = grub-core/lib/libgcrypt-grub/cipher/%s;\n" % src)
-            if modname == "gcry_ecc":
-                conf.write ("  common = lib/libgcrypt-grub/mpi/ec.c;\n")
-                conf.write ("  cflags = '$(CFLAGS_GCRY) -Wno-redundant-decls';\n")
-            elif modname == "gcry_rijndael" or modname == "gcry_md4" or modname == "gcry_md5" or modname == "gcry_rmd160" or modname == "gcry_sha1" or modname == "gcry_sha256" or modname == "gcry_sha512" or modname == "gcry_tiger":
-                # Alignment checked by hand
-                conf.write ("  cflags = '$(CFLAGS_GCRY) -Wno-cast-align';\n");
-            else:
-                conf.write ("  cflags = '$(CFLAGS_GCRY)';\n");
-            conf.write ("  cppflags = '$(CPPFLAGS_GCRY)';\n");
-            conf.write ("};\n\n")
-            f.close ()
-            fw.close ()
-            if nch:
-                chlog = "%s%s\n" % (chlog, chlognew)
-        elif isc and cipher_file not in extra_files_list:
-            print ("WARNING: C file isn't a module: %s" % cipher_file)
-            f.close ()
-            fw.close ()
-            os.remove (outfile)
-            chlog = "%s\n	* %s: Removed" % (chlog, cipher_file)
-        continue
-    chlog = "%s%sSkipped unknown file\n" % (chlog, chlognew)
-    print ("WARNING: unknown file %s" % cipher_file)
-
-cryptolist.close ()
-
-for src in sorted (os.listdir (os.path.join (indir, "src"))):
-    if src == "versioninfo.rc.in" or src == "ath.c" or src == "ChangeLog-2011" \
-            or src == "dumpsexp.c" or src == "fips.c" or src == "gcrypt.h.in" \
-            or src == "gcryptrnd.c"or src == "getrandom.c" \
-            or src == "global.c" or src == "hmac256.c" \
-            or src == "hwfeatures.c" or src == "libgcrypt-config.in" \
-            or src == "libgcrypt.def" or src == "libgcrypt.m4" \
-            or src == "libgcrypt.vers" or src == "Makefile.am" \
-            or src == "Manifest" or src == "misc.c" \
-            or src == "missing-string.c" or src == "module.c" \
-            or src == "secmem.c" \
-            or src == "stdmem.c" or src == "visibility.c":
-        continue
-    outfile = os.path.join (basedir, "src", src)
-    infile = os.path.join (indir, "src", src)
-    if os.path.isdir (infile):
-        continue
-    fw = codecs.open (outfile, "w", "utf-8")
-    if src == "gcrypt-module.h":
-        fw.close ()
-        continue
-    if src == "visibility.h":
-        fw.write ("# include <grub/gcrypt/gcrypt.h>\n")
-        fw.close ()
-        continue
-    f = codecs.open (infile, "r", "utf-8")
-    if src == "types.h":
-        fw.write (f.read ().replace ("float f;", "").replace ("double g;", ""))
-        f.close ()
-        fw.close ()
-        continue
-
-    if src == "cipher-proto.h":
-        fw.write("#include <grub/crypto.h>\n")
-        fw.write("typedef gcry_selftest_func_t selftest_func_t;")
-        f.close ()
-        fw.close ()
-        continue
-
-    if src == "g10lib.h":
-        fw.write("#include <cipher_wrap.h>\n")
-        fw.write("#include <grub/crypto.h>\n")
-        fw.write("#include <stdlib.h>\n")
-        fw.write (f.read ().replace ("(printf,f,a)", "(__printf__,f,a)").replace ("#include \"../compat/libcompat.h\"", "").replace("#define N_(a) (a)", ""))
-        f.close ()
-        fw.close ()
-        continue
-
-    fw.write (f.read ())
-    f.close ()
-    fw.close ()
-
-for src in sorted (os.listdir (os.path.join (indir, "mpi"))):
-    if src == "config.links" or src == "ChangeLog-2011" \
-            or src == "Manifest" \
-            or src == "Makefile.am":
-        continue
-    infile = os.path.join (indir, "mpi", src)
-    outfile = os.path.join (basedir, "mpi", src)
-    if os.path.isdir (infile):
-        continue
-    f = codecs.open (infile, "r", "utf-8")
-    fw = codecs.open (outfile, "w", "utf-8")
-    fw.write ("/* This file was automatically imported with \n")
-    fw.write ("   import_gcry.py. Please don't modify it */\n")
-    hold = False
-    skip = 0
-    for line in f:
-        if skip > 0:
-            if line[0] == "}":
-                skip = skip - 1
-            continue
-        if hold:
-            hold = False
-            # We're optimising for size and exclude anything needing good
-            # randomness.
-            if not re.match ("(_gcry_mpi_get_hw_config|gcry_mpi_randomize|_gcry_mpi_randomize)", line) is None:
-                skip = 1
-                continue
-            else:
-                fw.write (holdline)
-        m = re.match (r"(const char( |)\*|void) *$", line)
-        if not m is None:
-            hold = True
-            holdline = line
+                if src == "g10lib.h":
+                    fw.write("#include <cipher_wrap.h>\n")
+                    fw.write("#include <grub/crypto.h>\n")
+                    fw.write("#include <stdlib.h>\n")
+                    fw.write (f.read ().replace ("(printf,f,a)", "(__printf__,f,a)").replace ("#include \"../compat/libcompat.h\"", "").replace("#define N_(a) (a)", ""))
+                    continue
+
+                fw.write (f.read ())
+
+    for src in sorted (os.listdir (os.path.join (indir, "mpi"))):
+        if src in ["config.links", "ChangeLog-2011", "Manifest", "Makefile.am"]:
             continue
-        m = re.match (r"#include \"mod-source-info\.h\"", line)
-        if not m is None:
+        infile = os.path.join (indir, "mpi", src)
+        outfile = os.path.join (basedir, "mpi", src)
+        if os.path.isdir (infile):
             continue
-        fw.write (line)
-
-chlog = "%s	* crypto.lst: New file.\n" % chlog
-
-outfile = os.path.join (cipher_dir_out, "types.h")
-fw=codecs.open (outfile, "w", "utf-8")
-fw.write ("#include <grub/types.h>\n")
-fw.write ("#include <cipher_wrap.h>\n")
-chlog = "%s	* types.h: New file.\n" % chlog
-fw.close ()
-
-outfile = os.path.join (cipher_dir_out, "memory.h")
-fw=codecs.open (outfile, "w", "utf-8")
-fw.write ("#include <cipher_wrap.h>\n")
-chlog = "%s	* memory.h: New file.\n" % chlog
-fw.close ()
-
-
-outfile = os.path.join (cipher_dir_out, "cipher.h")
-fw=codecs.open (outfile, "w", "utf-8")
-fw.write ("#include <grub/crypto.h>\n")
-fw.write ("#include <cipher_wrap.h>\n")
-chlog = "%s	* cipher.h: Likewise.\n" % chlog
-fw.close ()
-
-outfile = os.path.join (cipher_dir_out, "g10lib.h")
-fw=codecs.open (outfile, "w", "utf-8")
-fw.write ("#include <cipher_wrap.h>\n")
-chlog = "%s	* g10lib.h: Likewise.\n" % chlog
-fw.close ()
-
-conf.close ();
-
-initfile = codecs.open (os.path.join (cipher_dir_out, "init.c"), "w", "utf-8")
-initfile.write ("#include <grub/crypto.h>\n")
-for module in modules_sym_md:
-    initfile.write ("extern void grub_%s_init (void);\n" % module)
-    initfile.write ("extern void grub_%s_fini (void);\n" % module)
-initfile.write ("\n")
-initfile.write ("void\n")
-initfile.write ("grub_gcry_init_all (void)\n")
-initfile.write ("{\n")
-for module in modules_sym_md:
-    initfile.write ("  grub_%s_init ();\n" % module)
-initfile.write ("}\n")
-initfile.write ("\n")
-initfile.write ("void\n")
-initfile.write ("grub_gcry_fini_all (void)\n")
-initfile.write ("{\n")
-for module in modules_sym_md:
-    initfile.write ("  grub_%s_fini ();\n" % module)
-initfile.write ("}\n")
-initfile.close ()
-
-confutil.write ("  common = grub-core/lib/libgcrypt-grub/cipher/init.c;\n")
-confutil.write ("};\n");
-confutil.close ();
+        with codecs.open (infile, "r", "utf-8") as f, codecs.open (outfile, "w", "utf-8") as fw:
+            fw.write ("/* This file was automatically imported with \n")
+            fw.write ("   import_gcry.py. Please don't modify it */\n")
+            hold = False
+            holdline = ""
+            skip = 0
+            for line in f:
+                if skip > 0:
+                    if line[0] == "}":
+                        skip = skip - 1
+                    continue
+                if hold:
+                    hold = False
+                    # We're optimising for size and exclude anything needing good
+                    # randomness.
+                    if not re.match ("(_gcry_mpi_get_hw_config|gcry_mpi_randomize|_gcry_mpi_randomize)", line) is None:
+                        skip = 1
+                        continue
+                    else:
+                        fw.write (holdline)
+                m = re.match (r"(const char( |)\*|void) *$", line)
+                if not m is None:
+                    hold = True
+                    holdline = line
+                    continue
+                m = re.match (r"#include \"mod-source-info\.h\"", line)
+                if not m is None:
+                    continue
+                fw.write (line)
+
+    chlog = "%s	* crypto.lst: New file.\n" % chlog
+
+    outfile = os.path.join (cipher_dir_out, "types.h")
+    with codecs.open (outfile, "w", "utf-8") as fw:
+        fw.write ("#include <grub/types.h>\n")
+        fw.write ("#include <cipher_wrap.h>\n")
+        chlog = "%s	* types.h: New file.\n" % chlog
+
+    outfile = os.path.join (cipher_dir_out, "memory.h")
+    with codecs.open (outfile, "w", "utf-8") as fw:
+        fw.write ("#include <cipher_wrap.h>\n")
+        chlog = "%s	* memory.h: New file.\n" % chlog
+
+
+    outfile = os.path.join (cipher_dir_out, "cipher.h")
+    with codecs.open (outfile, "w", "utf-8") as fw:
+        fw.write ("#include <grub/crypto.h>\n")
+        fw.write ("#include <cipher_wrap.h>\n")
+        chlog = "%s	* cipher.h: Likewise.\n" % chlog
+
+    outfile = os.path.join (cipher_dir_out, "g10lib.h")
+    with codecs.open (outfile, "w", "utf-8") as fw:
+        fw.write ("#include <cipher_wrap.h>\n")
+        chlog = "%s	* g10lib.h: Likewise.\n" % chlog
+
+    conf.close ()
+
+    with codecs.open (os.path.join (cipher_dir_out, "init.c"), "w", "utf-8") as initfile:
+        initfile.write ("#include <grub/crypto.h>\n")
+        for module in modules_sym_md:
+            initfile.write ("extern void grub_%s_init (void);\n" % module)
+            initfile.write ("extern void grub_%s_fini (void);\n" % module)
+        initfile.write ("\n")
+        initfile.write ("void\n")
+        initfile.write ("grub_gcry_init_all (void)\n")
+        initfile.write ("{\n")
+        for module in modules_sym_md:
+            initfile.write ("  grub_%s_init ();\n" % module)
+        initfile.write ("}\n")
+        initfile.write ("\n")
+        initfile.write ("void\n")
+        initfile.write ("grub_gcry_fini_all (void)\n")
+        initfile.write ("{\n")
+        for module in modules_sym_md:
+            initfile.write ("  grub_%s_fini ();\n" % module)
+        initfile.write ("}\n")
+
+    confutil.write ("  common = grub-core/lib/libgcrypt-grub/cipher/init.c;\n")
+    confutil.write ("};\n")
+    confutil.close ()
 
 
 outfile = os.path.join (cipher_dir_out, "ChangeLog")
-fw=codecs.open (outfile, "w", "utf-8")
-dt = datetime.date.today ()
-fw.write ("%04d-%02d-%02d  Automatic import tool\n" % \
-          (dt.year,dt.month, dt.day))
-fw.write ("\n")
-fw.write ("	Imported ciphers to GRUB\n")
-fw.write ("\n")
-fw.write (chlog)
-fw.write ("\n")
-fw.close ()
+with codecs.open (outfile, "w", "utf-8") as fw:
+    dt = datetime.date.today ()
+    fw.write ("%04d-%02d-%02d  Automatic import tool\n" % \
+              (dt.year,dt.month, dt.day))
+    fw.write ("\n")
+    fw.write ("	Imported ciphers to GRUB\n")
+    fw.write ("\n")
+    fw.write (chlog)
+    fw.write ("\n")
-- 
2.49.0


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

  parent reply	other threads:[~2025-07-07 15:23 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-07 14:52 [PATCH v14 00/15] Import libgcrypt 1.11 Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 01/15] " Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 02/15] Import b64dec from gpg-error Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 03/15] b64dec: Add harness for compilation in GRUB environment Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 04/15] Adjust import script, definitions and API users for libgcrypt 1.11 Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 05/15] Add DSA and RSA SEXP tests Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 06/15] keccak: Disable acceleration with SSE asm Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 07/15] libgcrypt: Fix coverity warnings Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 08/15] Remove now unneeded gcrypt compilation flag Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 09/15] gcry: Ignore sign-compare warnings Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 10/15] libgcrypt: Import blake family of hashes Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 11/15] import_gcry: Make compatible with python 3.4 Vladimir Serbinenko
2025-07-07 14:52 ` Vladimir Serbinenko [this message]
2025-07-07 14:52 ` [PATCH v14 13/15] libgcrypt: Don't use 64-bit division on platforms where it's slow Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 14/15] libgcrypt: Fix a memory leak Vladimir Serbinenko
2025-07-07 14:52 ` [PATCH v14 15/15] Write how to import new libgcrypt Vladimir Serbinenko
2025-07-10 15:34 ` [PATCH v14 00/15] Import libgcrypt 1.11 Daniel Kiper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250707145318.97596-13-phcoder@gmail.com \
    --to=phcoder@gmail.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.