import_gcry.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #*
  2. #* GRUB -- GRand Unified Bootloader
  3. #* Copyright (C) 2009 Free Software Foundation, Inc.
  4. #*
  5. #* GRUB is free software: you can redistribute it and/or modify
  6. #* it under the terms of the GNU General Public License as published by
  7. #* the Free Software Foundation, either version 3 of the License, or
  8. #* (at your option) any later version.
  9. #*
  10. #* GRUB is distributed in the hope that it will be useful,
  11. #* but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. #* GNU General Public License for more details.
  14. #*
  15. #* You should have received a copy of the GNU General Public License
  16. #* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. #*
  18. import re
  19. import sys
  20. import os
  21. import datetime
  22. import codecs
  23. if len (sys.argv) < 3:
  24. print ("Usage: %s SOURCE DESTINATION" % sys.argv[0])
  25. exit (0)
  26. indir = sys.argv[1]
  27. outdir = sys.argv[2]
  28. basedir = os.path.join (outdir, "lib/libgcrypt-grub")
  29. try:
  30. os.makedirs (basedir)
  31. except:
  32. print ("WARNING: %s already exists" % basedir)
  33. cipher_dir_in = os.path.join (indir, "cipher")
  34. cipher_dir_out = os.path.join (basedir, "cipher")
  35. try:
  36. os.makedirs (cipher_dir_out)
  37. except:
  38. print ("WARNING: %s already exists" % cipher_dir_out)
  39. mpidir = os.path.join (basedir, "mpi")
  40. try:
  41. os.makedirs (mpidir)
  42. except:
  43. print ("WARNING: %s already exists" % mpidir)
  44. srcdir = os.path.join (basedir, "src")
  45. try:
  46. os.makedirs (srcdir)
  47. except:
  48. print ("WARNING: %s already exists" % srcdir)
  49. cipher_files = sorted (os.listdir (cipher_dir_in))
  50. conf = codecs.open (os.path.join ("grub-core", "Makefile.gcry.def"), "w", "utf-8")
  51. conf.write ("AutoGen definitions Makefile.tpl;\n\n")
  52. confutil = codecs.open ("Makefile.utilgcry.def", "w", "utf-8")
  53. confutil.write ("AutoGen definitions Makefile.tpl;\n\n")
  54. confutil.write ("library = {\n");
  55. confutil.write (" name = libgrubgcry.a;\n");
  56. confutil.write (" cflags = '$(CFLAGS_GCRY)';\n");
  57. confutil.write (" cppflags = '$(CPPFLAGS_GCRY)';\n");
  58. confutil.write (" extra_dist = grub-core/lib/libgcrypt-grub/cipher/ChangeLog;\n");
  59. confutil.write ("\n");
  60. chlog = ""
  61. modules_sym_md = []
  62. # Strictly speaking CRC32/CRC24 work on bytes so this value should be 1
  63. # But libgcrypt uses 64. Let's keep the value for compatibility. Since
  64. # noone uses CRC24/CRC32 for HMAC this is no problem
  65. mdblocksizes = {"_gcry_digest_spec_crc32" : 64,
  66. "_gcry_digest_spec_crc32_rfc1510" : 64,
  67. "_gcry_digest_spec_crc24_rfc2440" : 64,
  68. "_gcry_digest_spec_md4" : 64,
  69. "_gcry_digest_spec_md5" : 64,
  70. "_gcry_digest_spec_rmd160" : 64,
  71. "_gcry_digest_spec_sha1" : 64,
  72. "_gcry_digest_spec_sha224" : 64,
  73. "_gcry_digest_spec_sha256" : 64,
  74. "_gcry_digest_spec_sha384" : 128,
  75. "_gcry_digest_spec_sha512" : 128,
  76. "_gcry_digest_spec_tiger" : 64,
  77. "_gcry_digest_spec_tiger1" : 64,
  78. "_gcry_digest_spec_tiger2" : 64,
  79. "_gcry_digest_spec_whirlpool" : 64}
  80. cryptolist = codecs.open (os.path.join (cipher_dir_out, "crypto.lst"), "w", "utf-8")
  81. # rijndael is the only cipher using aliases. So no need for mangling, just
  82. # hardcode it
  83. cryptolist.write ("RIJNDAEL: gcry_rijndael\n");
  84. cryptolist.write ("RIJNDAEL192: gcry_rijndael\n");
  85. cryptolist.write ("RIJNDAEL256: gcry_rijndael\n");
  86. cryptolist.write ("AES128: gcry_rijndael\n");
  87. cryptolist.write ("AES-128: gcry_rijndael\n");
  88. cryptolist.write ("AES-192: gcry_rijndael\n");
  89. cryptolist.write ("AES-256: gcry_rijndael\n");
  90. cryptolist.write ("ADLER32: adler32\n");
  91. cryptolist.write ("CRC64: crc64\n");
  92. for cipher_file in cipher_files:
  93. infile = os.path.join (cipher_dir_in, cipher_file)
  94. outfile = os.path.join (cipher_dir_out, cipher_file)
  95. if cipher_file == "ChangeLog" or cipher_file == "ChangeLog-2011":
  96. continue
  97. chlognew = " * %s" % cipher_file
  98. if re.match ("(Manifest|Makefile\.am|ac\.c|cipher\.c|hash-common\.c|hmac-tests\.c|md\.c|pubkey\.c)$", cipher_file) or cipher_file == "kdf.c" or cipher_file == "elgamal.c" or cipher_file == "primegen.c" or cipher_file == "ecc.c" or cipher_file == "test-getrusage.c":
  99. chlog = "%s%s: Removed\n" % (chlog, chlognew)
  100. continue
  101. # Autogenerated files. Not even worth mentionning in ChangeLog
  102. if re.match ("Makefile\.in$", cipher_file):
  103. continue
  104. nch = False
  105. if re.match (".*\.[ch]$", cipher_file):
  106. isc = re.match (".*\.c$", cipher_file)
  107. f = codecs.open (infile, "r", "utf-8")
  108. fw = codecs.open (outfile, "w", "utf-8")
  109. fw.write ("/* This file was automatically imported with \n")
  110. fw.write (" import_gcry.py. Please don't modify it */\n")
  111. fw.write ("#include <grub/dl.h>\n")
  112. if cipher_file == "camellia.h":
  113. fw.write ("#include <grub/misc.h>\n")
  114. fw.write ("void camellia_setup128(const unsigned char *key, grub_uint32_t *subkey);\n")
  115. fw.write ("void camellia_setup192(const unsigned char *key, grub_uint32_t *subkey);\n")
  116. fw.write ("void camellia_setup256(const unsigned char *key, grub_uint32_t *subkey);\n")
  117. fw.write ("void camellia_encrypt128(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
  118. fw.write ("void camellia_encrypt192(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
  119. fw.write ("void camellia_encrypt256(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
  120. fw.write ("void camellia_decrypt128(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
  121. fw.write ("void camellia_decrypt192(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
  122. fw.write ("void camellia_decrypt256(const grub_uint32_t *subkey, grub_uint32_t *io);\n")
  123. fw.write ("#define memcpy grub_memcpy\n")
  124. # Whole libgcrypt is distributed under GPLv3+ or compatible
  125. if isc:
  126. fw.write ("GRUB_MOD_LICENSE (\"GPLv3+\");\n")
  127. ciphernames = []
  128. mdnames = []
  129. mdctxsizes = []
  130. pknames = []
  131. hold = False
  132. skip = 0
  133. skip2 = False
  134. ismd = False
  135. mdarg = 0
  136. ispk = False
  137. iscipher = False
  138. iscryptostart = False
  139. iscomma = False
  140. isglue = False
  141. skip_statement = False
  142. if isc:
  143. modname = cipher_file [0:len(cipher_file) - 2]
  144. if re.match (".*-glue$", modname):
  145. modname = modname.replace ("-glue", "")
  146. isglue = True
  147. modname = "gcry_%s" % modname
  148. for line in f:
  149. line = line
  150. if skip_statement:
  151. if not re.search (";", line) is None:
  152. skip_statement = False
  153. continue
  154. if skip > 0:
  155. if line[0] == "}":
  156. skip = skip - 1
  157. continue
  158. if skip2:
  159. if not re.search (" *};", line) is None:
  160. skip2 = False
  161. continue
  162. if iscryptostart:
  163. s = re.search (" *\"([A-Z0-9_a-z]*)\"", line)
  164. if not s is None:
  165. sg = s.groups()[0]
  166. cryptolist.write (("%s: %s\n") % (sg, modname))
  167. iscryptostart = False
  168. if ismd:
  169. spl = line.split (",")
  170. if mdarg + len (spl) > 9 and mdarg <= 9 and ("sizeof" in spl[9-mdarg]):
  171. mdctxsizes.append (spl[9-mdarg].lstrip ().rstrip())
  172. mdarg = mdarg + len (spl) - 1
  173. if ismd or iscipher or ispk:
  174. if not re.search (" *};", line) is None:
  175. if not iscomma:
  176. fw.write (" ,\n")
  177. fw.write ("#ifdef GRUB_UTIL\n");
  178. fw.write (" .modname = \"%s\",\n" % modname);
  179. fw.write ("#endif\n");
  180. if ismd:
  181. if not (mdname in mdblocksizes):
  182. print ("ERROR: Unknown digest blocksize: %s\n"
  183. % mdname)
  184. exit (1)
  185. fw.write (" .blocksize = %s\n"
  186. % mdblocksizes [mdname])
  187. ismd = False
  188. mdarg = 0
  189. iscipher = False
  190. ispk = False
  191. iscomma = not re.search (",$", line) is None
  192. # Used only for selftests.
  193. m = re.match ("(static byte|static unsigned char) (weak_keys_chksum)\[[0-9]*\] =", line)
  194. if not m is None:
  195. skip = 1
  196. fname = m.groups ()[1]
  197. chmsg = "(%s): Removed." % fname
  198. if nch:
  199. chlognew = "%s\n %s" % (chlognew, chmsg)
  200. else:
  201. chlognew = "%s %s" % (chlognew, chmsg)
  202. nch = True
  203. continue
  204. if hold:
  205. hold = False
  206. # We're optimising for size and exclude anything needing good
  207. # randomness.
  208. 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|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|compute_keygrip|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)", line) is None:
  209. skip = 1
  210. if not re.match ("selftest", line) is None and cipher_file == "idea.c":
  211. skip = 3
  212. if not re.match ("serpent_test", line) is None:
  213. fw.write ("static const char *serpent_test (void) { return 0; }\n");
  214. if not re.match ("dsa_generate", line) is None:
  215. fw.write ("#define dsa_generate 0");
  216. if not re.match ("ecc_generate", line) is None:
  217. fw.write ("#define ecc_generate 0");
  218. if not re.match ("rsa_generate ", line) is None:
  219. fw.write ("#define rsa_generate 0");
  220. if not re.match ("rsa_sign", line) is None:
  221. fw.write ("#define rsa_sign 0");
  222. if not re.match ("rsa_decrypt", line) is None:
  223. fw.write ("#define rsa_decrypt 0");
  224. if not re.match ("dsa_sign", line) is None:
  225. fw.write ("#define dsa_sign 0");
  226. if not re.match ("ecc_sign", line) is None:
  227. fw.write ("#define ecc_sign 0");
  228. fname = re.match ("[a-zA-Z0-9_]*", line).group ()
  229. chmsg = "(%s): Removed." % fname
  230. if nch:
  231. chlognew = "%s\n %s" % (chlognew, chmsg)
  232. else:
  233. chlognew = "%s %s" % (chlognew, chmsg)
  234. nch = True
  235. continue
  236. else:
  237. fw.write (holdline)
  238. m = re.match ("# *include <(.*)>", line)
  239. if not m is None:
  240. chmsg = "Removed including of %s" % m.groups ()[0]
  241. if nch:
  242. chlognew = "%s\n %s" % (chlognew, chmsg)
  243. else:
  244. chlognew = "%s: %s" % (chlognew, chmsg)
  245. nch = True
  246. continue
  247. m = re.match ("gcry_cipher_spec_t", line)
  248. if isc and not m is None:
  249. assert (not ismd)
  250. assert (not ispk)
  251. assert (not iscipher)
  252. assert (not iscryptostart)
  253. ciphername = line [len ("gcry_cipher_spec_t"):].strip ()
  254. ciphername = re.match("[a-zA-Z0-9_]*",ciphername).group ()
  255. ciphernames.append (ciphername)
  256. iscipher = True
  257. iscryptostart = True
  258. m = re.match ("gcry_pk_spec_t", line)
  259. if isc and not m is None:
  260. assert (not ismd)
  261. assert (not ispk)
  262. assert (not iscipher)
  263. assert (not iscryptostart)
  264. pkname = line [len ("gcry_pk_spec_t"):].strip ()
  265. pkname = re.match("[a-zA-Z0-9_]*",pkname).group ()
  266. pknames.append (pkname)
  267. ispk = True
  268. iscryptostart = True
  269. m = re.match ("gcry_md_spec_t", line)
  270. if isc and not m is None:
  271. assert (not ismd)
  272. assert (not ispk)
  273. assert (not iscipher)
  274. assert (not iscryptostart)
  275. mdname = line [len ("gcry_md_spec_t"):].strip ()
  276. mdname = re.match("[a-zA-Z0-9_]*",mdname).group ()
  277. mdnames.append (mdname)
  278. ismd = True
  279. mdarg = 0
  280. iscryptostart = True
  281. m = re.match ("static const char \*selftest.*;$", line)
  282. if not m is None:
  283. fname = line[len ("static const char \*"):]
  284. fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
  285. chmsg = "(%s): Removed declaration." % fname
  286. if nch:
  287. chlognew = "%s\n %s" % (chlognew, chmsg)
  288. else:
  289. chlognew = "%s %s" % (chlognew, chmsg)
  290. nch = True
  291. continue
  292. m = re.match ("static gcry_mpi_t gen_k .*;$", line)
  293. if not m is None:
  294. chmsg = "(gen_k): Removed declaration."
  295. if nch:
  296. chlognew = "%s\n %s" % (chlognew, chmsg)
  297. else:
  298. chlognew = "%s %s" % (chlognew, chmsg)
  299. nch = True
  300. continue
  301. m = re.match ("static (int|void) test_keys .*;$", line)
  302. if not m is None:
  303. chmsg = "(test_keys): Removed declaration."
  304. if nch:
  305. chlognew = "%s\n %s" % (chlognew, chmsg)
  306. else:
  307. chlognew = "%s %s" % (chlognew, chmsg)
  308. nch = True
  309. continue
  310. m = re.match ("static void secret .*;$", line)
  311. if not m is None:
  312. chmsg = "(secret): Removed declaration."
  313. if nch:
  314. chlognew = "%s\n %s" % (chlognew, chmsg)
  315. else:
  316. chlognew = "%s %s" % (chlognew, chmsg)
  317. nch = True
  318. continue
  319. m = re.match ("static void \(\*progress_cb\).*;$", line)
  320. if not m is None:
  321. chmsg = "(progress_cb): Removed declaration."
  322. if nch:
  323. chlognew = "%s\n %s" % (chlognew, chmsg)
  324. else:
  325. chlognew = "%s %s" % (chlognew, chmsg)
  326. nch = True
  327. continue
  328. m = re.match ("static void \*progress_cb_data.*;$", line)
  329. if not m is None:
  330. chmsg = "(progress_cb): Removed declaration."
  331. if nch:
  332. chlognew = "%s\n %s" % (chlognew, chmsg)
  333. else:
  334. chlognew = "%s %s" % (chlognew, chmsg)
  335. nch = True
  336. continue
  337. m = re.match ("(static const char( |)\*|static gpg_err_code_t|void|static int|static gcry_err_code_t|static gcry_mpi_t|static void|void|static elliptic_curve_t) *$", line)
  338. if not m is None:
  339. hold = True
  340. holdline = line
  341. continue
  342. m = re.match ("static int tripledes_set2keys \(.*\);", line)
  343. if not m is None:
  344. continue
  345. m = re.match ("static int tripledes_set3keys \(.*\);", line)
  346. if not m is None:
  347. continue
  348. m = re.match ("static int tripledes_set2keys \(", line)
  349. if not m is None:
  350. skip_statement = True
  351. continue
  352. m = re.match ("static int tripledes_set3keys \(", line)
  353. if not m is None:
  354. skip_statement = True
  355. continue
  356. m = re.match ("static const char sample_secret_key", line)
  357. if not m is None:
  358. skip_statement = True
  359. continue
  360. m = re.match ("static const char sample_public_key", line)
  361. if not m is None:
  362. skip_statement = True
  363. continue
  364. m = re.match ("static void sign|static gpg_err_code_t sign|static gpg_err_code_t generate",
  365. line)
  366. if not m is None:
  367. skip_statement = True
  368. continue
  369. m = re.match ("cipher_extra_spec_t", line)
  370. if isc and not m is None:
  371. skip2 = True
  372. fname = line[len ("cipher_extra_spec_t "):]
  373. fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
  374. chmsg = "(%s): Removed." % fname
  375. if nch:
  376. chlognew = "%s\n %s" % (chlognew, chmsg)
  377. else:
  378. chlognew = "%s %s" % (chlognew, chmsg)
  379. nch = True
  380. continue
  381. m = re.match ("pk_extra_spec_t", line)
  382. if isc and not m is None:
  383. skip2 = True
  384. fname = line[len ("pk_extra_spec_t "):]
  385. fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
  386. chmsg = "(%s): Removed." % fname
  387. if nch:
  388. chlognew = "%s\n %s" % (chlognew, chmsg)
  389. else:
  390. chlognew = "%s %s" % (chlognew, chmsg)
  391. nch = True
  392. continue
  393. m = re.match ("md_extra_spec_t", line)
  394. if isc and not m is None:
  395. skip2 = True
  396. fname = line[len ("md_extra_spec_t "):]
  397. fname = re.match ("[a-zA-Z0-9_]*", fname).group ()
  398. chmsg = "(%s): Removed." % fname
  399. if nch:
  400. chlognew = "%s\n %s" % (chlognew, chmsg)
  401. else:
  402. chlognew = "%s %s" % (chlognew, chmsg)
  403. nch = True
  404. continue
  405. fw.write (line)
  406. if len (ciphernames) > 0 or len (mdnames) > 0 or len (pknames) > 0:
  407. if isglue:
  408. modfiles = "lib/libgcrypt-grub/cipher/%s lib/libgcrypt-grub/cipher/%s" \
  409. % (cipher_file, cipher_file.replace ("-glue.c", ".c"))
  410. else:
  411. modfiles = "lib/libgcrypt-grub/cipher/%s" % cipher_file
  412. if len (ciphernames) > 0 or len (mdnames) > 0:
  413. modules_sym_md.append (modname)
  414. chmsg = "(GRUB_MOD_INIT(%s)): New function\n" % modname
  415. if nch:
  416. chlognew = "%s\n %s" % (chlognew, chmsg)
  417. else:
  418. chlognew = "%s%s" % (chlognew, chmsg)
  419. nch = True
  420. fw.write ("\n\nGRUB_MOD_INIT(%s)\n" % modname)
  421. fw.write ("{\n")
  422. for ciphername in ciphernames:
  423. chmsg = "Register cipher %s" % ciphername
  424. chlognew = "%s\n %s" % (chlognew, chmsg)
  425. fw.write (" grub_cipher_register (&%s);\n" % ciphername)
  426. for ctxsize in mdctxsizes:
  427. fw.write (" COMPILE_TIME_ASSERT(%s <= GRUB_CRYPTO_MAX_MD_CONTEXT_SIZE);\n" % ctxsize)
  428. for mdname in mdnames:
  429. chmsg = "Register digest %s" % mdname
  430. chlognew = "%s\n %s" % (chlognew, chmsg)
  431. fw.write (" grub_md_register (&%s);\n" % mdname)
  432. for pkname in pknames:
  433. chmsg = "Register pk %s" % mdname
  434. chlognew = "%s\n %s" % (chlognew, chmsg)
  435. fw.write (" grub_crypto_pk_%s = &%s;\n"
  436. % (pkname.replace ("_gcry_pubkey_spec_", ""), pkname))
  437. fw.write ("}")
  438. chmsg = "(GRUB_MOD_FINI(%s)): New function\n" % modname
  439. chlognew = "%s\n %s" % (chlognew, chmsg)
  440. fw.write ("\n\nGRUB_MOD_FINI(%s)\n" % modname)
  441. fw.write ("{\n")
  442. for ciphername in ciphernames:
  443. chmsg = "Unregister cipher %s" % ciphername
  444. chlognew = "%s\n %s" % (chlognew, chmsg)
  445. fw.write (" grub_cipher_unregister (&%s);\n" % ciphername)
  446. for mdname in mdnames:
  447. chmsg = "Unregister MD %s" % mdname
  448. chlognew = "%s\n %s" % (chlognew, chmsg)
  449. fw.write (" grub_md_unregister (&%s);\n" % mdname)
  450. for pkname in pknames:
  451. chmsg = "Unregister pk %s" % mdname
  452. chlognew = "%s\n %s" % (chlognew, chmsg)
  453. fw.write (" grub_crypto_pk_%s = 0;\n"
  454. % (pkname.replace ("_gcry_pubkey_spec_", "")))
  455. fw.write ("}\n")
  456. conf.write ("module = {\n")
  457. conf.write (" name = %s;\n" % modname)
  458. for src in modfiles.split():
  459. conf.write (" common = %s;\n" % src)
  460. if len (ciphernames) > 0 or len (mdnames) > 0:
  461. confutil.write (" common = grub-core/%s;\n" % src)
  462. if modname == "gcry_ecc":
  463. conf.write (" common = lib/libgcrypt-grub/mpi/ec.c;\n")
  464. conf.write (" cflags = '$(CFLAGS_GCRY) -Wno-redundant-decls -Wno-sign-compare';\n")
  465. 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":
  466. # Alignment checked by hand
  467. conf.write (" cflags = '$(CFLAGS_GCRY) -Wno-cast-align';\n");
  468. else:
  469. conf.write (" cflags = '$(CFLAGS_GCRY)';\n");
  470. conf.write (" cppflags = '$(CPPFLAGS_GCRY)';\n");
  471. conf.write ("};\n\n")
  472. f.close ()
  473. fw.close ()
  474. if nch:
  475. chlog = "%s%s\n" % (chlog, chlognew)
  476. elif isc and cipher_file != "camellia.c":
  477. print ("WARNING: C file isn't a module: %s" % cipher_file)
  478. f.close ()
  479. fw.close ()
  480. os.remove (outfile)
  481. chlog = "%s\n * %s: Removed" % (chlog, cipher_file)
  482. continue
  483. chlog = "%s%sSkipped unknown file\n" % (chlog, chlognew)
  484. print ("WARNING: unknown file %s" % cipher_file)
  485. cryptolist.close ()
  486. for src in sorted (os.listdir (os.path.join (indir, "src"))):
  487. if src == "versioninfo.rc.in" or src == "ath.c" or src == "ChangeLog-2011" \
  488. or src == "dumpsexp.c" or src == "fips.c" or src == "gcrypt.h.in" \
  489. or src == "gcryptrnd.c"or src == "getrandom.c" \
  490. or src == "global.c" or src == "hmac256.c" \
  491. or src == "hwfeatures.c" or src == "libgcrypt-config.in" \
  492. or src == "libgcrypt.def" or src == "libgcrypt.m4" \
  493. or src == "libgcrypt.vers" or src == "Makefile.am" \
  494. or src == "Manifest" or src == "misc.c" \
  495. or src == "missing-string.c" or src == "module.c" \
  496. or src == "secmem.c" or src == "sexp.c" \
  497. or src == "stdmem.c" or src == "visibility.c":
  498. continue
  499. outfile = os.path.join (basedir, "src", src)
  500. infile = os.path.join (indir, "src", src)
  501. if os.path.isdir (infile):
  502. continue
  503. fw = codecs.open (outfile, "w", "utf-8")
  504. if src == "gcrypt-module.h":
  505. fw.close ()
  506. continue
  507. if src == "visibility.h":
  508. fw.write ("# include <grub/gcrypt/gcrypt.h>\n")
  509. fw.close ()
  510. continue
  511. f = codecs.open (infile, "r", "utf-8")
  512. if src == "types.h":
  513. fw.write (f.read ().replace ("float f;", "").replace ("double g;", ""))
  514. f.close ()
  515. fw.close ()
  516. continue
  517. if src == "g10lib.h":
  518. fw.write (f.read ().replace ("(printf,f,a)", "(__printf__,f,a)"))
  519. f.close ()
  520. fw.close ()
  521. continue
  522. fw.write (f.read ())
  523. f.close ()
  524. fw.close ()
  525. for src in sorted (os.listdir (os.path.join (indir, "mpi"))):
  526. if src == "config.links" or src == "ChangeLog-2011" \
  527. or src == "mpi-scan.c" or src == "Manifest" \
  528. or src == "Makefile.am":
  529. continue
  530. infile = os.path.join (indir, "mpi", src)
  531. outfile = os.path.join (basedir, "mpi", src)
  532. if os.path.isdir (infile):
  533. continue
  534. f = codecs.open (infile, "r", "utf-8")
  535. fw = codecs.open (outfile, "w", "utf-8")
  536. fw.write ("/* This file was automatically imported with \n")
  537. fw.write (" import_gcry.py. Please don't modify it */\n")
  538. hold = False
  539. skip = 0
  540. for line in f:
  541. if skip > 0:
  542. if line[0] == "}":
  543. skip = skip - 1
  544. continue
  545. if hold:
  546. hold = False
  547. # We're optimising for size and exclude anything needing good
  548. # randomness.
  549. if not re.match ("(_gcry_mpi_get_hw_config|gcry_mpi_randomize)", line) is None:
  550. skip = 1
  551. continue
  552. else:
  553. fw.write (holdline)
  554. m = re.match ("(const char( |)\*|void) *$", line)
  555. if not m is None:
  556. hold = True
  557. holdline = line
  558. continue
  559. m = re.match ("#include \"mod-source-info\.h\"", line)
  560. if not m is None:
  561. continue
  562. fw.write (line)
  563. chlog = "%s * crypto.lst: New file.\n" % chlog
  564. outfile = os.path.join (cipher_dir_out, "types.h")
  565. fw=codecs.open (outfile, "w", "utf-8")
  566. fw.write ("#include <grub/types.h>\n")
  567. fw.write ("#include <cipher_wrap.h>\n")
  568. chlog = "%s * types.h: New file.\n" % chlog
  569. fw.close ()
  570. outfile = os.path.join (cipher_dir_out, "memory.h")
  571. fw=codecs.open (outfile, "w", "utf-8")
  572. fw.write ("#include <cipher_wrap.h>\n")
  573. chlog = "%s * memory.h: New file.\n" % chlog
  574. fw.close ()
  575. outfile = os.path.join (cipher_dir_out, "cipher.h")
  576. fw=codecs.open (outfile, "w", "utf-8")
  577. fw.write ("#include <grub/crypto.h>\n")
  578. fw.write ("#include <cipher_wrap.h>\n")
  579. chlog = "%s * cipher.h: Likewise.\n" % chlog
  580. fw.close ()
  581. outfile = os.path.join (cipher_dir_out, "g10lib.h")
  582. fw=codecs.open (outfile, "w", "utf-8")
  583. fw.write ("#include <cipher_wrap.h>\n")
  584. chlog = "%s * g10lib.h: Likewise.\n" % chlog
  585. fw.close ()
  586. infile = os.path.join (cipher_dir_in, "ChangeLog")
  587. outfile = os.path.join (cipher_dir_out, "ChangeLog")
  588. conf.close ();
  589. initfile = codecs.open (os.path.join (cipher_dir_out, "init.c"), "w", "utf-8")
  590. initfile.write ("#include <grub/crypto.h>\n")
  591. for module in modules_sym_md:
  592. initfile.write ("extern void grub_%s_init (void);\n" % module)
  593. initfile.write ("extern void grub_%s_fini (void);\n" % module)
  594. initfile.write ("\n")
  595. initfile.write ("void\n")
  596. initfile.write ("grub_gcry_init_all (void)\n")
  597. initfile.write ("{\n")
  598. for module in modules_sym_md:
  599. initfile.write (" grub_%s_init ();\n" % module)
  600. initfile.write ("}\n")
  601. initfile.write ("\n")
  602. initfile.write ("void\n")
  603. initfile.write ("grub_gcry_fini_all (void)\n")
  604. initfile.write ("{\n")
  605. for module in modules_sym_md:
  606. initfile.write (" grub_%s_fini ();\n" % module)
  607. initfile.write ("}\n")
  608. initfile.close ()
  609. confutil.write (" common = grub-core/lib/libgcrypt-grub/cipher/init.c;\n")
  610. confutil.write ("};\n");
  611. confutil.close ();
  612. f=codecs.open (infile, "r", "utf-8")
  613. fw=codecs.open (outfile, "w", "utf-8")
  614. dt = datetime.date.today ()
  615. fw.write ("%04d-%02d-%02d Automatic import tool\n" % \
  616. (dt.year,dt.month, dt.day))
  617. fw.write ("\n")
  618. fw.write (" Imported ciphers to GRUB\n")
  619. fw.write ("\n")
  620. fw.write (chlog)
  621. fw.write ("\n")
  622. for line in f:
  623. fw.write (line)
  624. f.close ()
  625. fw.close ()