0001-crda-support-python-3-in-utils-key2pub.py.patch 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. From 8228c484a1533ff904b276c342adcb6310abe272 Mon Sep 17 00:00:00 2001
  2. From: Taahir Ahmed <ahmed.taahir@gmail.com>
  3. Date: Wed, 30 Mar 2016 11:23:54 -0300
  4. Subject: [PATCH] crda: support python 3 in utils/key2pub.py
  5. utils/key2pub.py can now be run under either python 2.7 or python 3.x.
  6. This required some minor syntactical changes as well as switching from
  7. M2Crypto to pycryptodomex, since M2Crypto doesn't support python 3.x.
  8. In addition, some errors in the generated source file keys-ssl.h are
  9. fixed:
  10. * The correct OpenSSL header for BN_ULONG is included.
  11. * The generated constants are given the 'ull' suffix to prevent
  12. warnings about constants that are too large.
  13. [Gustavo: don't call /utils/key2pub.py since that doesn't compute]
  14. Use pycryptodomex insdead of pycrypto
  15. From [1]:
  16. "PyCryptodome is a fork of PyCrypto, which is not maintained any more
  17. (the last release dates back to 2013 [2]). It exposes almost the same
  18. API, but there are a few incompatibilities [3]."
  19. [1] https://github.com/OP-TEE/optee_os/commit/90ad2450436fdd9fc0d28a3f92f3fbcfd89a38f0
  20. [2] https://pypi.org/project/pycrypto/#history
  21. [3] https://pycryptodome.readthedocs.io/en/latest/src/vs_pycrypto.html
  22. Signed-off-by: Gustavo Zacarias <gustavo@zacarias.com.ar>
  23. [Rebased against crda-4.14]
  24. Signed-off-by: Peter Seiderer <ps.report@gmx.net>
  25. [Romain: Use pycryptodomex]
  26. Signed-off-by: Romain Naour <romain.naour@gmail.com>
  27. ---
  28. Makefile | 2 +-
  29. utils/key2pub.py | 146 ++++++++++++++++++++++++-----------------------
  30. 2 files changed, 75 insertions(+), 73 deletions(-)
  31. diff --git a/Makefile b/Makefile
  32. index a3ead30..8da38d0 100644
  33. --- a/Makefile
  34. +++ b/Makefile
  35. @@ -112,7 +112,7 @@ $(REG_BIN):
  36. keys-%.c: utils/key2pub.py $(wildcard $(PUBKEY_DIR)/*.pem)
  37. $(NQ) ' GEN ' $@
  38. $(NQ) ' Trusted pubkeys:' $(wildcard $(PUBKEY_DIR)/*.pem)
  39. - $(Q)./utils/key2pub.py --$* $(wildcard $(PUBKEY_DIR)/*.pem) $@
  40. + $(Q) python utils/key2pub.py --$* $(wildcard $(PUBKEY_DIR)/*.pem) $@
  41. $(LIBREG): regdb.h reglib.h reglib.c
  42. $(NQ) ' CC ' $@
  43. diff --git a/utils/key2pub.py b/utils/key2pub.py
  44. index 9bb04cd..8a0ba2a 100755
  45. --- a/utils/key2pub.py
  46. +++ b/utils/key2pub.py
  47. @@ -1,126 +1,128 @@
  48. #!/usr/bin/env python
  49. +import io
  50. import sys
  51. try:
  52. - from M2Crypto import RSA
  53. -except ImportError, e:
  54. - sys.stderr.write('ERROR: Failed to import the "M2Crypto" module: %s\n' % e.message)
  55. - sys.stderr.write('Please install the "M2Crypto" Python module.\n')
  56. - sys.stderr.write('On Debian GNU/Linux the package is called "python-m2crypto".\n')
  57. - sys.exit(1)
  58. + from Cryptodome.PublicKey import RSA
  59. +except ImportError as e:
  60. + sys.stderr.write('ERROR: Failed to import the "Cryptodome.PublicKey" module: %s\n' % e.message)
  61. + sys.stderr.write('Please install the "Cryptodome.PublicKey" Python module.\n')
  62. + sys.stderr.write('On Debian GNU/Linux the package is called "python-cryptodomex".\n')
  63. + sys.exit(1)
  64. +
  65. +def bitwise_collect(value, radix_bits):
  66. + words = []
  67. + radix_mask = (1 << radix_bits) - 1
  68. + while value != 0:
  69. + words.append(value & radix_mask)
  70. + value >>= radix_bits
  71. + return words
  72. def print_ssl_64(output, name, val):
  73. - while val[0] == '\0':
  74. - val = val[1:]
  75. - while len(val) % 8:
  76. - val = '\0' + val
  77. - vnew = []
  78. - while len(val):
  79. - vnew.append((val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7]))
  80. - val = val[8:]
  81. - vnew.reverse()
  82. - output.write('static BN_ULONG %s[%d] = {\n' % (name, len(vnew)))
  83. + # OpenSSL expects 64-bit words given least-significant-word first.
  84. + vwords = bitwise_collect(val, 64)
  85. +
  86. + output.write(u'static BN_ULONG {}[] = {{\n'.format(name))
  87. idx = 0
  88. - for v1, v2, v3, v4, v5, v6, v7, v8 in vnew:
  89. + for vword in vwords:
  90. if not idx:
  91. - output.write('\t')
  92. - output.write('0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x, ' % (ord(v1), ord(v2), ord(v3), ord(v4), ord(v5), ord(v6), ord(v7), ord(v8)))
  93. + output.write(u'\t')
  94. + output.write(u'0x{:016x}ULL, '.format(vword))
  95. idx += 1
  96. if idx == 2:
  97. idx = 0
  98. - output.write('\n')
  99. + output.write(u'\n')
  100. if idx:
  101. - output.write('\n')
  102. - output.write('};\n\n')
  103. + output.write(u'\n')
  104. + output.write(u'};\n\n')
  105. def print_ssl_32(output, name, val):
  106. - while val[0] == '\0':
  107. - val = val[1:]
  108. - while len(val) % 4:
  109. - val = '\0' + val
  110. - vnew = []
  111. - while len(val):
  112. - vnew.append((val[0], val[1], val[2], val[3], ))
  113. - val = val[4:]
  114. - vnew.reverse()
  115. - output.write('static BN_ULONG %s[%d] = {\n' % (name, len(vnew)))
  116. + # OpenSSL expects 32-bit words given least-significant-word first.
  117. + vwords = bitwise_collect(val, 32)
  118. +
  119. + output.write(u'static BN_ULONG {}[] = {{\n'.format(name))
  120. idx = 0
  121. - for v1, v2, v3, v4 in vnew:
  122. + for vword in vwords:
  123. if not idx:
  124. - output.write('\t')
  125. - output.write('0x%.2x%.2x%.2x%.2x, ' % (ord(v1), ord(v2), ord(v3), ord(v4)))
  126. + output.write(u'\t')
  127. + output.write(u'0x{:08x}, '.format(vword))
  128. idx += 1
  129. if idx == 4:
  130. idx = 0
  131. - output.write('\n')
  132. + output.write(u'\n')
  133. if idx:
  134. - output.write('\n')
  135. - output.write('};\n\n')
  136. + output.write(u'\n')
  137. + output.write(u'};\n\n')
  138. def print_ssl(output, name, val):
  139. +
  140. + output.write(u'#include <stdint.h>\n')
  141. + output.write(u'#include <openssl/bn.h>\n')
  142. +
  143. import struct
  144. - output.write('#include <stdint.h>\n')
  145. if len(struct.pack('@L', 0)) == 8:
  146. return print_ssl_64(output, name, val)
  147. else:
  148. return print_ssl_32(output, name, val)
  149. def print_ssl_keys(output, n):
  150. - output.write(r'''
  151. + output.write(u'''
  152. struct pubkey {
  153. struct bignum_st e, n;
  154. };
  155. -#define KEY(data) { \
  156. - .d = data, \
  157. - .top = sizeof(data)/sizeof(data[0]), \
  158. +#define KEY(data) { \\
  159. + .d = data, \\
  160. + .top = sizeof(data)/sizeof(data[0]), \\
  161. }
  162. -#define KEYS(e,n) { KEY(e), KEY(n), }
  163. +#define KEYS(e,n) { KEY(e), KEY(n), }
  164. static struct pubkey keys[] = {
  165. ''')
  166. for n in xrange(n + 1):
  167. - output.write(' KEYS(e_%d, n_%d),\n' % (n, n))
  168. - output.write('};\n')
  169. + output.write(u' KEYS(e_{0}, n_{0}),\n'.format(n))
  170. + output.write(u'};\n')
  171. pass
  172. def print_gcrypt(output, name, val):
  173. - output.write('#include <stdint.h>\n')
  174. - while val[0] == '\0':
  175. - val = val[1:]
  176. - output.write('static const uint8_t %s[%d] = {\n' % (name, len(val)))
  177. + # gcrypt expects 8-bit words most-significant-word first
  178. + vwords = bitwise_collect(val, 8)
  179. + vwords.reverse()
  180. +
  181. + output.write(u'#include <stdint.h>\n')
  182. + output.write(u'static const uint8_t %s[%d] = {\n' % (name, len(vwords)))
  183. idx = 0
  184. - for v in val:
  185. + for vword in vwords:
  186. if not idx:
  187. - output.write('\t')
  188. - output.write('0x%.2x, ' % ord(v))
  189. + output.write(u'\t')
  190. + output.write(u'0x{:02x}, '.format(vword))
  191. idx += 1
  192. if idx == 8:
  193. idx = 0
  194. - output.write('\n')
  195. + output.write(u'\n')
  196. if idx:
  197. - output.write('\n')
  198. - output.write('};\n\n')
  199. + output.write(u'\n')
  200. + output.write(u'};\n\n')
  201. def print_gcrypt_keys(output, n):
  202. - output.write(r'''
  203. + output.write(u'''
  204. struct key_params {
  205. const uint8_t *e, *n;
  206. uint32_t len_e, len_n;
  207. };
  208. -#define KEYS(_e, _n) { \
  209. - .e = _e, .len_e = sizeof(_e), \
  210. - .n = _n, .len_n = sizeof(_n), \
  211. +#define KEYS(_e, _n) { \\
  212. + .e = _e, .len_e = sizeof(_e), \\
  213. + .n = _n, .len_n = sizeof(_n), \\
  214. }
  215. static const struct key_params __attribute__ ((unused)) keys[] = {
  216. ''')
  217. - for n in xrange(n + 1):
  218. - output.write(' KEYS(e_%d, n_%d),\n' % (n, n))
  219. - output.write('};\n')
  220. -
  221. + for n in range(n + 1):
  222. + output.write(u' KEYS(e_{0}, n_{0}),\n'.format(n))
  223. + output.write(u'};\n')
  224. +
  225. modes = {
  226. '--ssl': (print_ssl, print_ssl_keys),
  227. @@ -135,21 +137,21 @@ except IndexError:
  228. mode = None
  229. if not mode in modes:
  230. - print 'Usage: %s [%s] input-file... output-file' % (sys.argv[0], '|'.join(modes.keys()))
  231. + print('Usage: {} [{}] input-file... output-file'.format(sys.argv[0], '|'.join(modes.keys())))
  232. sys.exit(2)
  233. -output = open(outfile, 'w')
  234. +output = io.open(outfile, 'w')
  235. # load key
  236. idx = 0
  237. for f in files:
  238. - try:
  239. - key = RSA.load_pub_key(f)
  240. - except RSA.RSAError:
  241. - key = RSA.load_key(f)
  242. - modes[mode][0](output, 'e_%d' % idx, key.e[4:])
  243. - modes[mode][0](output, 'n_%d' % idx, key.n[4:])
  244. + key_contents = io.open(f, 'rb').read()
  245. + key = RSA.importKey(key_contents)
  246. +
  247. + modes[mode][0](output, 'e_{}'.format(idx), key.e)
  248. + modes[mode][0](output, 'n_{}'.format(idx), key.n)
  249. +
  250. idx += 1
  251. modes[mode][1](output, idx - 1)
  252. --
  253. 2.25.3