test.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. # *****************************************************************************
  2. # \file test.py
  3. # \project bee2evp [EVP-interfaces over bee2 / engine of OpenSSL]
  4. # \brief Python tests for openssl[bee2evp]
  5. # \created 2019.07.10
  6. # \version 2020.02.27
  7. # \license This program is released under the GNU General Public License
  8. # version 3 with the additional exemption that compiling, linking,
  9. # and/or using OpenSSL is allowed. See Copyright Notices in bee2evp/info.h.
  10. # *****************************************************************************
  11. from openssl import openssl
  12. from belt import *
  13. from bign import *
  14. from settings import *
  15. import sys, os, shutil
  16. import tempfile
  17. import re
  18. fail = False
  19. def test_result(test_name, retcode):
  20. global fail
  21. if(retcode == 1):
  22. sys.stdout.write(test_name + ': ')
  23. print_colored('success', bcolors.OKGREEN)
  24. else:
  25. sys.stdout.write(test_name + ': ')
  26. print_colored('fail', bcolors.FAIL)
  27. fail = True
  28. def test_version():
  29. retcode, out, __ = openssl('version')
  30. test_result('version', retcode)
  31. print(out.decode())
  32. def test_engine():
  33. retcode, out, er__ = openssl('engine -c -t bee2evp')
  34. test_result('engine', retcode)
  35. print(out.decode())
  36. def test_belt():
  37. #Block (|X| = 128)
  38. #A.1 Encrypt
  39. block = hex_decoder('b194bac80a08f53b366d008e584a5de4')[0]
  40. key = hex_decoder('e9dee72c8f0c0fa62ddb49f46f739647'
  41. '06075316ed247a3739cba38303a98bf6')[0]
  42. block = beltBlockEncr(bytes(block), bytes(key))
  43. res = hex_encoder(block)[0].decode() == '69cca1c93557c9e3d66bc3e0fa88fa6e'
  44. test_result('Block Encrypt', res)
  45. #A.4 Decrypt
  46. block = hex_decoder('e12bdc1ae28257ec703fccf095ee8df1')[0]
  47. key = hex_decoder('92bd9b1ce5d141015445fbc95e4d0ef2'
  48. '682080aa227d642f2687f93490405511')[0]
  49. block = beltBlockDecr(bytes(block), bytes(key))
  50. res = hex_encoder(block)[0].decode() == '0dc5300600cab840b38448e5e993f421'
  51. test_result('Block Decrypt', res)
  52. #ECB (|X| = 384)
  53. #A.6 Encrypt
  54. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  55. '8504fa9d1bb6c7ac252e72c202fdce0d'
  56. '5be3d61217b96181fe6786ad716b890b')[0]
  57. key = hex_decoder('e9dee72c8f0c0fa62ddb49f46f739647'
  58. '06075316ed247a3739cba38303a98bf6')[0]
  59. dest = beltECBEncr(bytes(src), bytes(key))
  60. res = hex_encoder(dest)[0].decode() == (
  61. '69cca1c93557c9e3d66bc3e0fa88fa6e'
  62. '5f23102ef109710775017f73806da9dc'
  63. '46fb2ed2ce771f26dcb5e5d1569f9ab0')
  64. test_result('ECB Encrypt', res)
  65. #A.8 Decrypt
  66. src = hex_decoder('e12bdc1ae28257ec703fccf095ee8df1'
  67. 'c1ab76389fe678caf7c6f860d5bb9c4f'
  68. 'f33c657b637c306add4ea7799eb23d31')[0]
  69. key = hex_decoder('92bd9b1ce5d141015445fbc95e4d0ef2'
  70. '682080aa227d642f2687f93490405511')[0]
  71. dest = beltECBDecr(bytes(src), bytes(key))
  72. res = hex_encoder(dest)[0].decode() == (
  73. '0dc5300600cab840b38448e5e993f421'
  74. 'e55a239f2ab5c5d5fdb6e81b40938e2a'
  75. '54120ca3e6e19c7ad750fc3531daeab7')
  76. test_result('ECB Decrypt', res)
  77. #CBC (|X| = 384)
  78. #A.10 Encrypt
  79. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  80. '8504fa9d1bb6c7ac252e72c202fdce0d'
  81. '5be3d61217b96181fe6786ad716b890b')[0]
  82. iv = hex_decoder('be32971343fc9a48a02a885f194b09a1')[0]
  83. key = hex_decoder('e9dee72c8f0c0fa62ddb49f46f739647'
  84. '06075316ed247a3739cba38303a98bf6')[0]
  85. dest = beltCBCEncr(bytes(src), bytes(key), bytes(iv))
  86. res = hex_encoder(dest)[0].decode() == (
  87. '10116efae6ad58ee14852e11da1b8a74'
  88. '5cf2480e8d03f1c19492e53ed3a70f60'
  89. '657c1ee8c0e0ae5b58388bf8a68e3309')
  90. test_result('CBC Encrypt', res)
  91. #A.12 Decrypt
  92. src = hex_decoder('e12bdc1ae28257ec703fccf095ee8df1'
  93. 'c1ab76389fe678caf7c6f860d5bb9c4f'
  94. 'f33c657b637c306add4ea7799eb23d31')[0]
  95. iv = hex_decoder('7ecda4d01544af8ca58450bf66d2e88a')[0]
  96. key = hex_decoder('92bd9b1ce5d141015445fbc95e4d0ef2'
  97. '682080aa227d642f2687f93490405511')[0]
  98. dest = beltCBCDecr(bytes(src), bytes(key), bytes(iv))
  99. res = hex_encoder(dest)[0].decode() == (
  100. '730894d6158e17cc1600185a8f411cab'
  101. '0471ff85c83792398d8924ebd57d03db'
  102. '95b97a9b7907e4b020960455e46176f8')
  103. test_result('CBC Decrypt', res)
  104. #CFB (|X| = 384)
  105. #A.14 Encrypt
  106. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  107. '8504fa9d1bb6c7ac252e72c202fdce0d'
  108. '5be3d61217b96181fe6786ad716b890b')[0]
  109. iv = hex_decoder('be32971343fc9a48a02a885f194b09a1')[0]
  110. key = hex_decoder('e9dee72c8f0c0fa62ddb49f46f739647'
  111. '06075316ed247a3739cba38303a98bf6')[0]
  112. dest = beltCFBEncr(bytes(src), bytes(key), bytes(iv))
  113. res = hex_encoder(dest)[0].decode() == (
  114. 'c31e490a90efa374626cc99e4b7b8540'
  115. 'a6e48685464a5a06849c9ca769a1b0ae'
  116. '55c2cc5939303ec832dd2fe16c8e5a1b')
  117. test_result('CFB Encrypt', res)
  118. #A.15 Decrypt
  119. src = hex_decoder('e12bdc1ae28257ec703fccf095ee8df1'
  120. 'c1ab76389fe678caf7c6f860d5bb9c4f'
  121. 'f33c657b637c306add4ea7799eb23d31')[0]
  122. iv = hex_decoder('7ecda4d01544af8ca58450bf66d2e88a')[0]
  123. key = hex_decoder('92bd9b1ce5d141015445fbc95e4d0ef2'
  124. '682080aa227d642f2687f93490405511')[0]
  125. dest = beltCFBDecr(bytes(src), bytes(key), bytes(iv))
  126. res = hex_encoder(dest)[0].decode() == (
  127. 'fa9d107a86f375ee65cd1db881224bd0'
  128. '16aff814938ed39b3361abb0bf0851b6'
  129. '52244eb06842dd4c94aa4500774e40bb')
  130. test_result('CFB Decrypt', res)
  131. #CTR (|X| = 384)
  132. #A.16
  133. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  134. '8504fa9d1bb6c7ac252e72c202fdce0d'
  135. '5be3d61217b96181fe6786ad716b890b')[0]
  136. iv = hex_decoder('be32971343fc9a48a02a885f194b09a1')[0]
  137. key = hex_decoder('e9dee72c8f0c0fa62ddb49f46f739647'
  138. '06075316ed247a3739cba38303a98bf6')[0]
  139. dest = beltCTREncr(bytes(src), bytes(key), bytes(iv))
  140. res = hex_encoder(dest)[0].decode() == (
  141. '52c9af96ff50f64435fc43def56bd797'
  142. 'd5b5b1ff79fb41257ab9cdf6e63e81f8'
  143. 'f00341473eae409833622de05213773a')
  144. test_result('CTR Encrypt', res)
  145. #MAC-256
  146. #A.18
  147. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  148. '8504fa9d1bb6c7ac252e72c202fdce0d'
  149. '5be3d61217b96181fe6786ad716b890b')[0]
  150. key = hex_decoder('e9dee72c8f0c0fa62ddb49f46f739647'
  151. '06075316ed247a3739cba38303a98bf6')[0]
  152. mac = beltMAC(bytes(src), bytes(key))
  153. res = hex_encoder(mac)[0].decode() == '2dab59771b4b16d0'
  154. test_result('MAC-256', res)
  155. #MAC-128
  156. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  157. '8504fa9d1bb6c7ac252e72c202fdce0d'
  158. '5be3d61217b96181fe6786ad716b890b')[0]
  159. key = hex_decoder('e9dee72c8f0c0fa62ddb49f46f739647')[0]
  160. mac = beltMAC(bytes(src), bytes(key))
  161. res = hex_encoder(mac)[0].decode() != ''
  162. test_result('MAC-128', res)
  163. #MAC-192
  164. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  165. '8504fa9d1bb6c7ac252e72c202fdce0d'
  166. '5be3d61217b96181fe6786ad716b890b')[0]
  167. key = hex_decoder('e9dee72c8f0c0fa62ddb49f46f739647'
  168. '06075316ed247a37')[0]
  169. mac = beltMAC(bytes(src), bytes(key))
  170. res = hex_encoder(mac)[0].decode() != ''
  171. test_result('MAC-192', res)
  172. #HMAC
  173. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  174. '8504fa9d1bb6c7ac252e72c202fdce0d'
  175. '5be3d61217b96181fe6786ad716b890b')[0]
  176. key = hex_decoder('e9dee72c8f0c0fa62ddb49f46f739647'
  177. '06075316ed247a37')[0]
  178. mac = beltMAC(bytes(src), bytes(key))
  179. res = hex_encoder(mac)[0].decode() != ''
  180. test_result('HMAC', res)
  181. #HASH
  182. #A.25
  183. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  184. '8504fa9d1bb6c7ac252e72c202fdce0d')[0]
  185. hash_ = beltHash(bytes(src))
  186. res = hex_encoder(hash_)[0].decode() == (
  187. '749e4c3653aece5e48db4761227742eb'
  188. '6dbe13f4a80f7beff1a9cf8d10ee7786')
  189. test_result('belt-hash', res)
  190. #Bash256
  191. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  192. '8504fa9d1bb6c7ac252e72c202fdce0d')[0]
  193. hash_ = bash256Hash(bytes(src))
  194. res = hex_encoder(hash_)[0].decode() != ''
  195. test_result('bash256', res)
  196. #Bash384
  197. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  198. '8504fa9d1bb6c7ac252e72c202fdce0d')[0]
  199. hash_ = bash384Hash(bytes(src))
  200. res = hex_encoder(hash_)[0].decode() != ''
  201. test_result('bash384', res)
  202. #Bash512
  203. src = hex_decoder('b194bac80a08f53b366d008e584a5de4'
  204. '8504fa9d1bb6c7ac252e72c202fdce0d')[0]
  205. hash_ = bash512Hash(bytes(src))
  206. res = hex_encoder(hash_)[0].decode() != ''
  207. test_result('bash384', res)
  208. def test_bign():
  209. # Create temporary directory for testing
  210. tmpdirname = tempfile.mkdtemp()
  211. # Gen params bign-curve256v1
  212. params256 = os.path.join(tmpdirname, 'params256v1.pem')
  213. bignStdParams('bign-curve256v1', params256)
  214. out = openssl('asn1parse -in {}'.format(params256))
  215. res = out[1].decode().find('bign-curve256v1') != -1
  216. test_result('Gen params bign-curve256v1', res)
  217. # Gen params bign-curve384v1
  218. params384 = os.path.join(tmpdirname, 'params384v1.pem')
  219. bignStdParams('bign-curve384v1', params384)
  220. out = openssl('asn1parse -in {}'.format(params384))
  221. res = out[1].decode().find('bign-curve384v1') != -1
  222. test_result('Gen params bign-curve384v1', res)
  223. # Gen params bign-curve512v1
  224. params512 = os.path.join(tmpdirname, 'params512v1.pem')
  225. bignStdParams('bign-curve512v1', params512)
  226. out = openssl('asn1parse -in {}'.format(params512))
  227. res = out[1].decode().find('bign-curve512v1') != -1
  228. test_result('Gen params bign-curve512v1', res)
  229. # Gen private key bign-curve256v1
  230. prkey256 = os.path.join(tmpdirname,'prkey256v1.pem')
  231. bignGenKeypair(params256, prkey256)
  232. out = openssl('asn1parse -in {}'.format(prkey256))
  233. res = (out[1].decode().find('bign-curve256v1') != -1 & out[1].decode()
  234. .find('bign-pubkey') != -1)
  235. test_result('Gen private key bign-curve256v1', res)
  236. # Gen private key G.1
  237. key = '1F66B5B84B7339674533F0329C74F21834281FED0732429E0C79235FC273E269'
  238. asn1cnf = '''
  239. asn1 = SEQUENCE:SubjectPublicKeyInfo
  240. [SubjectPublicKeyInfo]
  241. version = INTEGER:0
  242. algorithm = SEQUENCE:AlgorithmIdentifier
  243. subjectPublicKey = FORMAT:HEX,OCTETSTRING:{}
  244. [AlgorithmIdentifier]
  245. algorithm = OBJECT:bign-pubkey
  246. parameters = OBJECT:bign-curve256v1
  247. '''.format(key)
  248. asn1_conf_file = os.path.join(tmpdirname, 'asn1_conf')
  249. with open(asn1_conf_file,'w') as f:
  250. f.write(asn1cnf)
  251. G1prkey256der = os.path.join(tmpdirname, 'G1prkey256.der')
  252. G1prkey256pem = os.path.join(tmpdirname, 'G1prkey256.pem')
  253. retcode, out, er__ = openssl(
  254. 'asn1parse -genconf {} -out {}'.format(asn1_conf_file, G1prkey256der))
  255. openssl('pkey -inform DER -in {} -outform PEM -out {}'
  256. .format(G1prkey256der,G1prkey256pem))
  257. retcode, out, er__ = openssl('asn1parse -in {}'.format(G1prkey256pem))
  258. out = out.decode()[out.decode().rfind('[HEX DUMP]:'):].split(':')[1][:-1]
  259. res = (out == key)
  260. test_result('Generate private key G.1', res)
  261. # Gen private key bign-curve384v1
  262. prkey384 = os.path.join(tmpdirname, 'prkey384v1.pem')
  263. bignGenKeypair(params384, prkey384)
  264. out = openssl('asn1parse -in {}'.format(prkey384))
  265. res = (out[1].decode().find('bign-curve384v1') != -1 &
  266. out[1].decode().find('bign-pubkey') != -1)
  267. test_result('Gen private key bign-curve384v1', res)
  268. # Gen private key bign-curve512v1
  269. prkey512 = os.path.join(tmpdirname, 'prkey512v1.pem')
  270. bignGenKeypair(params512, prkey512)
  271. out = openssl('asn1parse -in {}'.format(prkey512))
  272. res = (out[1].decode().find('bign-curve512v1') != -1 &
  273. out[1].decode().find('bign-pubkey') != -1)
  274. test_result('Gen private key bign-curve512v1', res)
  275. # Calc public key bign-curve256v1
  276. pubkey256 = os.path.join(tmpdirname, 'pubkey256v1.pem')
  277. bignCalcPubkey(prkey256, pubkey256)
  278. out = openssl('asn1parse -in {}'.format(pubkey256))
  279. res = (out[1].decode().find('bign-curve512v1') != -1 &
  280. out[1].decode().find('bign-pubkey') != -1)
  281. test_result('Calc public key bign-curve256v1', res)
  282. # Calc public key G.1
  283. G1pubkey256 = os.path.join(tmpdirname, 'G1pubkey256v1.pem')
  284. bignCalcPubkey(G1prkey256pem, G1pubkey256)
  285. out = openssl('asn1parse -in {} -offset 28 -dump'.format(G1pubkey256))
  286. out = re.sub('[\s\n]', '', out[1].decode())
  287. matches = re.findall('[0-9A-Fa-f]{4}-[0-9A-Fa-f]+-*[0-9A-Fa-f]+', out)
  288. ans = ''
  289. for match in matches:
  290. items = match.split('-')
  291. for item in items[1:]:
  292. ans += item
  293. res = (ans[2:] == ('bd1a5650179d79e03fcee49d4c2bd5dd'
  294. 'f54ce46d0cf11e4ff87bf7a890857fd0'
  295. '7ac6a60361e8c8173491686d461b2826'
  296. '190c2eda5909054a9ab84d2ab9d99a90'))
  297. test_result('Calc public key G.1', res)
  298. # Calc public key bign-curve384v1
  299. pubkey384 = os.path.join(tmpdirname, 'pubkey384v1.pem')
  300. bignCalcPubkey(prkey384, pubkey384)
  301. out = openssl('asn1parse -in {}'.format(pubkey384))
  302. res = (out[1].decode().find('bign-curve512v1') != -1 &
  303. out[1].decode().find('bign-pubkey') != -1)
  304. test_result('Calc public key bign-curve384v1', res)
  305. # Calc public key bign-curve512v1
  306. pubkey512 = os.path.join(tmpdirname, 'pubkey512v1.pem')
  307. bignCalcPubkey(prkey512, pubkey512)
  308. out = openssl('asn1parse -in {}'.format(pubkey512))
  309. res = (out[1].decode().find('bign-curve512v1') != -1 &
  310. out[1].decode().find('bign-pubkey') != -1)
  311. test_result('Calc public key bign-curve512v1', res)
  312. # Calc dgst belt-hash
  313. src = hex_decoder('b194bac80a08f53b366d008e58')[0]
  314. signbelth = os.path.join(tmpdirname, 'signbelth.sign')
  315. retcode = bignSign(prkey256, 'belt-hash', bytes(src), signbelth)
  316. res = (retcode == 1)
  317. test_result('Calc dgst belt-hash', res)
  318. # Verify dgst belt-hash
  319. out = bignVerify(prkey256, 'belt-hash', bytes(src), signbelth)
  320. res = (out == 'Verified OK')
  321. test_result('Verify dgst belt-hash', res)
  322. # Calc deterministic dgst belt-hash
  323. src = hex_decoder('b194bac80a08f53b366d008e58')[0]
  324. dsignbelth = os.path.join(tmpdirname, 'dsignbelth.sign')
  325. retcode = bignSign(G1prkey256pem, 'belt-hash', bytes(src), dsignbelth)
  326. res = (retcode == 1)
  327. test_result('Calc deterministic dgst belt-hash', res)
  328. # Verify deterministic dgst belt-hash
  329. out = bignVerify(G1prkey256pem, 'belt-hash', bytes(src), dsignbelth)
  330. res = (out == 'Verified OK')
  331. test_result('Verify deterministic dgst belt-hash', res)
  332. shutil.rmtree(tmpdirname)
  333. def test_belt_kwp_dwp():
  334. tmpdirname = tempfile.mkdtemp()
  335. params256 = os.path.join(tmpdirname, 'params256.pem')
  336. cmd = ('genpkey -genparam -algorithm bign -pkeyopt params:bign-curve256v1'
  337. ' -pkeyopt enc_params:specified -pkeyopt enc_params:cofactor -out')
  338. openssl('{} {}'.format(cmd, params256))
  339. retcode, out, er__ = openssl(
  340. 'genpkey -paramfile {} -belt-kwp128 -pass pass:root'.format(params256))
  341. test_result('belt-kwp128', retcode)
  342. retcode, out, er__ = openssl(
  343. 'genpkey -paramfile {} -belt-kwp192 -pass pass:root'.format(params256))
  344. test_result('belt-kwp192', retcode)
  345. retcode, out, er__ = openssl(
  346. 'genpkey -paramfile {} -belt-kwp256 -pass pass:root'.format(params256))
  347. test_result('belt-kwp256', retcode)
  348. retcode, out, er__ = openssl(
  349. 'genpkey -paramfile {} -belt-dwp128 -pass pass:root'.format(params256))
  350. test_result('belt-dwp128', retcode)
  351. retcode, out, er__ = openssl(
  352. 'genpkey -paramfile {} -belt-dwp192 -pass pass:root'.format(params256))
  353. test_result('belt-dwp192', retcode)
  354. retcode, out, er__ = openssl(
  355. 'genpkey -paramfile {} -belt-dwp256 -pass pass:root'.format(params256))
  356. test_result('belt-dwp256', retcode)
  357. shutil.rmtree(tmpdirname)
  358. if __name__ == '__main__':
  359. global fail
  360. test_version()
  361. test_engine()
  362. test_belt()
  363. test_bign()
  364. test_belt_kwp_dwp()
  365. if (fail == True):
  366. sys.exit(1)