40
41
print ("WARNING: %s already exists" % cipher_dir_out)
42
43
cipher_files = os.listdir (cipher_dir_in)
43
conf = open (os.path.join ("grub-core", "Makefile.gcry.def"), "w")
44
conf = codecs.open (os.path.join ("grub-core", "Makefile.gcry.def"), "w", "utf-8")
44
45
conf.write ("AutoGen definitions Makefile.tpl;\n\n")
46
confutil = codecs.open ("Makefile.utilgcry.def", "w", "utf-8")
47
confutil.write ("AutoGen definitions Makefile.tpl;\n\n")
48
confutil.write ("library = {\n");
49
confutil.write (" name = libgrubgcry.a;\n");
50
confutil.write (" cflags = '$(CFLAGS_GCRY)';\n");
51
confutil.write (" cppflags = '$(CPPFLAGS_GCRY)';\n");
52
confutil.write (" extra_dist = grub-core/lib/libgcrypt-grub/cipher/ChangeLog;\n");
53
confutil.write ("\n");
47
57
# Strictly speaking CRC32/CRC24 work on bytes so this value should be 1
48
58
# But libgcrypt uses 64. Let's keep the value for compatibility. Since
61
71
"_gcry_digest_spec_tiger" : 64,
62
72
"_gcry_digest_spec_whirlpool" : 64}
64
cryptolist = open (os.path.join (cipher_dir_out, "crypto.lst"), "w")
74
cryptolist = codecs.open (os.path.join (cipher_dir_out, "crypto.lst"), "w", "utf-8")
66
76
# rijndael is the only cipher using aliases. So no need for mangling, just
73
83
cryptolist.write ("AES-192: gcry_rijndael\n");
74
84
cryptolist.write ("AES-256: gcry_rijndael\n");
86
cryptolist.write ("ADLER32: adler32\n");
87
cryptolist.write ("CRC64: crc64\n");
76
89
for cipher_file in cipher_files:
77
90
infile = os.path.join (cipher_dir_in, cipher_file)
78
91
outfile = os.path.join (cipher_dir_out, cipher_file)
89
102
if re.match (".*\.[ch]$", cipher_file):
90
103
isc = re.match (".*\.c$", cipher_file)
91
f = open (infile, "r")
92
fw = open (outfile, "w")
104
f = codecs.open (infile, "r", "utf-8")
105
fw = codecs.open (outfile, "w", "utf-8")
93
106
fw.write ("/* This file was automatically imported with \n")
94
107
fw.write (" import_gcry.py. Please don't modify it */\n")
95
108
fw.write ("#include <grub/dl.h>\n")
109
if cipher_file == "camellia.h":
110
fw.write ("#include <grub/misc.h>\n")
111
fw.write ("void camellia_setup128(const unsigned char *key, grub_uint32_t *subkey);\n")
112
fw.write ("void camellia_setup192(const unsigned char *key, grub_uint32_t *subkey);\n")
113
fw.write ("void camellia_setup256(const unsigned char *key, grub_uint32_t *subkey);\n")
114
fw.write ("void camellia_encrypt128(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
115
fw.write ("void camellia_encrypt192(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
116
fw.write ("void camellia_encrypt256(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
117
fw.write ("void camellia_decrypt128(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
118
fw.write ("void camellia_decrypt192(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
119
fw.write ("void camellia_decrypt256(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
120
fw.write ("#define memcpy grub_memcpy\n")
96
121
# Whole libgcrypt is distributed under GPLv3+ or compatible
98
123
fw.write ("GRUB_MOD_LICENSE (\"GPLv3+\");\n")
132
159
sg = s.groups()[0]
133
160
cryptolist.write (("%s: %s\n") % (sg, modname))
134
161
iscryptostart = False
136
163
if not re.search (" *};", line) is None:
137
if not mdblocksizes.has_key (mdname):
138
print ("ERROR: Unknown digest blocksize: %s\n" % mdname)
141
165
fw.write (" ,\n")
142
fw.write (" .blocksize = %s\n" % mdblocksizes [mdname])
166
fw.write ("#ifdef GRUB_UTIL\n");
167
fw.write (" .modname = \"%s\",\n" % modname);
168
fw.write ("#endif\n");
170
if not (mdname in mdblocksizes):
171
print ("ERROR: Unknown digest blocksize: %s\n"
174
fw.write (" .blocksize = %s\n"
175
% mdblocksizes [mdname])
144
178
iscomma = not re.search (",$", line) is None
145
179
# Used only for selftests.
146
180
m = re.match ("(static byte|static unsigned char) (weak_keys_chksum)\[[0-9]*\] =", line)
159
193
# We're optimising for size.
160
if not re.match ("(run_selftests|selftest|_gcry_aes_c.._..c|_gcry_[a-z0-9]*_hash_buffer|tripledes_set2keys|do_tripledes_set_extra_info)", line) is None:
194
if not re.match ("(run_selftests|selftest|_gcry_aes_c.._..c|_gcry_[a-z0-9]*_hash_buffer|tripledes_set2keys|do_tripledes_set_extra_info|_gcry_rmd160_mixblock|serpent_test)", line) is None:
196
if not re.match ("serpent_test", line) is None:
197
fw.write ("static const char *serpent_test (void) { return 0; }\n");
162
198
fname = re.match ("[a-zA-Z0-9_]*", line).group ()
163
199
chmsg = "(%s): Removed." % fname
171
207
fw.write (holdline)
172
m = re.match ("#include <.*>", line)
208
m = re.match ("# *include <(.*)>", line)
173
209
if not m is None:
174
chmsg = "Removed including of %s" % \
175
m.group () [len ("#include <"):len (m.group ()) - 1]
210
chmsg = "Removed including of %s" % m.groups ()[0]
177
212
chlognew = "%s\n %s" % (chlognew, chmsg)
182
217
m = re.match ("gcry_cipher_spec_t", line)
183
218
if isc and not m is None:
184
219
assert (not iscryptostart)
220
assert (not iscipher)
221
assert (not iscryptostart)
185
222
ciphername = line [len ("gcry_cipher_spec_t"):].strip ()
186
223
ciphername = re.match("[a-zA-Z0-9_]*",ciphername).group ()
187
224
ciphernames.append (ciphername)
188
226
iscryptostart = True
189
227
m = re.match ("gcry_md_spec_t", line)
190
228
if isc and not m is None:
191
229
assert (not ismd)
230
assert (not iscipher)
192
231
assert (not iscryptostart)
193
232
mdname = line [len ("gcry_md_spec_t"):].strip ()
194
233
mdname = re.match("[a-zA-Z0-9_]*",mdname).group ()
249
288
% (cipher_file, cipher_file.replace ("-glue.c", ".c"))
251
290
modfiles = "lib/libgcrypt-grub/cipher/%s" % cipher_file
291
modules.append (modname)
252
292
chmsg = "(GRUB_MOD_INIT(%s)): New function\n" % modname
254
294
chlognew = "%s\n %s" % (chlognew, chmsg)
283
323
conf.write (" name = %s;\n" % modname)
284
324
for src in modfiles.split():
285
325
conf.write (" common = %s;\n" % src)
286
conf.write (" cflags = '$(CFLAGS_GCRY)';\n");
326
confutil.write (" common = grub-core/%s;\n" % src)
327
if 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":
328
# Alignment checked by hand
329
conf.write (" cflags = '$(CFLAGS_GCRY) -Wno-cast-align -Wno-strict-aliasing';\n");
331
conf.write (" cflags = '$(CFLAGS_GCRY)';\n");
287
332
conf.write (" cppflags = '$(CPPFLAGS_GCRY)';\n");
288
333
conf.write ("};\n\n")
337
chlog = "%s%s\n" % (chlog, chlognew)
289
338
elif isc and cipher_file != "camellia.c":
290
339
print ("WARNING: C file isn't a module: %s" % cipher_file)
294
chlog = "%s%s\n" % (chlog, chlognew)
343
chlog = "%s\n * %s: Removed" % (chlog, cipher_file)
296
345
chlog = "%s%sSkipped unknown file\n" % (chlog, chlognew)
297
346
print ("WARNING: unknown file %s" % cipher_file)
300
349
chlog = "%s * crypto.lst: New file.\n" % chlog
302
351
outfile = os.path.join (cipher_dir_out, "types.h")
303
fw=open (outfile, "w")
352
fw=codecs.open (outfile, "w", "utf-8")
304
353
fw.write ("#include <grub/types.h>\n")
305
354
fw.write ("#include <cipher_wrap.h>\n")
306
355
chlog = "%s * types.h: New file.\n" % chlog
309
358
outfile = os.path.join (cipher_dir_out, "memory.h")
310
fw=open (outfile, "w")
359
fw=codecs.open (outfile, "w", "utf-8")
311
360
fw.write ("#include <cipher_wrap.h>\n")
312
361
chlog = "%s * memory.h: New file.\n" % chlog
316
365
outfile = os.path.join (cipher_dir_out, "cipher.h")
317
fw=open (outfile, "w")
366
fw=codecs.open (outfile, "w", "utf-8")
318
367
fw.write ("#include <grub/crypto.h>\n")
319
368
fw.write ("#include <cipher_wrap.h>\n")
320
369
chlog = "%s * cipher.h: Likewise.\n" % chlog
323
372
outfile = os.path.join (cipher_dir_out, "g10lib.h")
324
fw=open (outfile, "w")
373
fw=codecs.open (outfile, "w", "utf-8")
325
374
fw.write ("#include <cipher_wrap.h>\n")
326
375
chlog = "%s * g10lib.h: Likewise.\n" % chlog
329
378
infile = os.path.join (cipher_dir_in, "ChangeLog")
330
379
outfile = os.path.join (cipher_dir_out, "ChangeLog")
334
fw=open (outfile, "w")
383
initfile = codecs.open (os.path.join (cipher_dir_out, "init.c"), "w", "utf-8")
384
initfile.write ("#include <grub/crypto.h>\n")
385
for module in modules:
386
initfile.write ("extern void grub_%s_init (void);\n" % module)
387
initfile.write ("extern void grub_%s_fini (void);\n" % module)
388
initfile.write ("\n")
389
initfile.write ("void\n")
390
initfile.write ("grub_gcry_init_all (void)\n")
391
initfile.write ("{\n")
392
for module in modules:
393
initfile.write (" grub_%s_init ();\n" % module)
394
initfile.write ("}\n")
395
initfile.write ("\n")
396
initfile.write ("void\n")
397
initfile.write ("grub_gcry_fini_all (void)\n")
398
initfile.write ("{\n")
399
for module in modules:
400
initfile.write (" grub_%s_fini ();\n" % module)
401
initfile.write ("}\n")
404
confutil.write (" common = grub-core/lib/libgcrypt-grub/cipher/init.c;\n")
405
confutil.write ("};\n");
409
f=codecs.open (infile, "r", "utf-8")
410
fw=codecs.open (outfile, "w", "utf-8")
335
411
dt = datetime.date.today ()
336
412
fw.write ("%04d-%02d-%02d Automatic import tool\n" % \
337
413
(dt.year,dt.month, dt.day))