~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to util/import_gcry.py

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-09-13 18:02:04 UTC
  • mfrom: (1.17.15 upstream)
  • mto: (17.6.27 experimental)
  • mto: This revision was merged to the branch mainline in revision 145.
  • Revision ID: package-import@ubuntu.com-20120913180204-mojnmocbimlom4im
Tags: upstream-2.00
ImportĀ upstreamĀ versionĀ 2.00

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import sys
21
21
import os
22
22
import datetime
 
23
import codecs
23
24
 
24
25
if len (sys.argv) < 3:
25
26
    print ("Usage: %s SOURCE DESTINATION" % sys.argv[0])
40
41
    print ("WARNING: %s already exists" % cipher_dir_out)
41
42
 
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");
45
54
chlog = ""
 
55
modules = []
46
56
 
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}
63
73
 
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")
65
75
 
66
76
# rijndael is the only cipher using aliases. So no need for mangling, just
67
77
# hardcode it
73
83
cryptolist.write ("AES-192: gcry_rijndael\n");
74
84
cryptolist.write ("AES-256: gcry_rijndael\n");
75
85
 
 
86
cryptolist.write ("ADLER32: adler32\n");
 
87
cryptolist.write ("CRC64: crc64\n");
 
88
 
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)
88
101
    nch = False
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
97
122
        if isc:
98
123
            fw.write ("GRUB_MOD_LICENSE (\"GPLv3+\");\n")
103
128
        skip = False
104
129
        skip2 = False
105
130
        ismd = False
 
131
        iscipher = False
106
132
        iscryptostart = False
107
133
        iscomma = False
108
134
        isglue = False
114
140
                isglue = True
115
141
            modname = "gcry_%s" % modname
116
142
        for line in f:
 
143
            line = line
117
144
            if skip_statement:
118
145
                if not re.search (";", line) is None:
119
146
                    skip_statement = False
132
159
                    sg = s.groups()[0]
133
160
                    cryptolist.write (("%s: %s\n") % (sg, modname))
134
161
                    iscryptostart = False
135
 
            if ismd:
 
162
            if ismd or iscipher:
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)
139
 
                        exit (1)
140
164
                    if not iscomma:
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");
 
169
                    if ismd:
 
170
                        if not (mdname in mdblocksizes):
 
171
                            print ("ERROR: Unknown digest blocksize: %s\n"
 
172
                                   % mdname)
 
173
                            exit (1)
 
174
                        fw.write ("    .blocksize = %s\n"
 
175
                                  % mdblocksizes [mdname])
143
176
                    ismd = False
 
177
                    iscipher = False
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)
157
191
            if hold:
158
192
                hold = False
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:
161
195
                    skip = True
 
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
164
200
                    if nch:
169
205
                    continue
170
206
                else:
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]
176
211
                if nch:
177
212
                    chlognew = "%s\n    %s" % (chlognew, chmsg)
178
213
                else:
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)
 
225
                iscipher = True
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"))
250
289
            else:
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
253
293
            if nch:
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");
 
330
            else:
 
331
                conf.write ("  cflags = '$(CFLAGS_GCRY)';\n");
287
332
            conf.write ("  cppflags = '$(CPPFLAGS_GCRY)';\n");
288
333
            conf.write ("};\n\n")
 
334
            f.close ()
 
335
            fw.close ()
 
336
            if nch:
 
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)
291
 
        f.close ()
292
 
        fw.close ()
293
 
        if nch:
294
 
            chlog = "%s%s\n" % (chlog, chlognew)
 
340
            f.close ()
 
341
            fw.close ()
 
342
            os.remove (outfile)
 
343
            chlog = "%s\n       * %s: Removed" % (chlog, cipher_file)
295
344
        continue
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
301
350
 
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
307
356
fw.close ()
308
357
 
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
313
362
fw.close ()
314
363
 
315
364
 
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
321
370
fw.close ()
322
371
 
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
327
376
fw.close ()
329
378
infile = os.path.join (cipher_dir_in, "ChangeLog")
330
379
outfile = os.path.join (cipher_dir_out, "ChangeLog")
331
380
 
332
 
 
333
 
f=open (infile, "r")
334
 
fw=open (outfile, "w")
 
381
conf.close ();
 
382
 
 
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")
 
402
initfile.close ()
 
403
 
 
404
confutil.write ("  common = grub-core/lib/libgcrypt-grub/cipher/init.c;\n")
 
405
confutil.write ("};\n");
 
406
confutil.close ();
 
407
 
 
408
 
 
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))