PageRenderTime 61ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/OldUniversityStuff/ZIKS/ezPyCrypto-0.1.1/easy/pycrypto-1.9a5/Util/test.py

https://bitbucket.org/Behemot/university-rep
Python | 446 lines | 377 code | 45 blank | 24 comment | 130 complexity | 3b7f3d17b032db9bc0b553e8fa7903e8 MD5 | raw file
Possible License(s): GPL-2.0
  1. #
  2. # test.py : Functions used for testing the modules
  3. #
  4. # Part of the Python Cryptography Toolkit
  5. #
  6. # Distribute and use freely; there are no restrictions on further
  7. # dissemination and usage except those imposed by the laws of your
  8. # country of residence. This software is provided "as is" without
  9. # warranty of fitness for use or suitability for any purpose, express
  10. # or implied. Use at your own risk or not at all.
  11. #
  12. __revision__ = "$Id: test.py,v 1.15 2003/02/28 15:26:01 akuchling Exp $"
  13. import binascii
  14. import string
  15. import testdata
  16. from Crypto.Cipher import *
  17. def die(string):
  18. import sys
  19. print '***ERROR: ', string
  20. # sys.exit(0) # Will default to continuing onward...
  21. def exerciseBlockCipher(cipher, verbose):
  22. import string, time
  23. try:
  24. ciph = eval(cipher)
  25. except NameError:
  26. print cipher, 'module not available'
  27. return None
  28. print cipher+ ':'
  29. str='1' # Build 128K of test data
  30. for i in xrange(0, 17):
  31. str=str+str
  32. if ciph.key_size==0: ciph.key_size=16
  33. password = 'password12345678Extra text for password'[0:ciph.key_size]
  34. IV = 'Test IV Test IV Test IV Test'[0:ciph.block_size]
  35. if verbose: print ' ECB mode:',
  36. obj=ciph.new(password, ciph.MODE_ECB)
  37. if obj.block_size != ciph.block_size:
  38. die("Module and cipher object block_size don't match")
  39. text='1234567812345678'[0:ciph.block_size]
  40. c=obj.encrypt(text)
  41. if (obj.decrypt(c)!=text): die('Error encrypting "'+text+'"')
  42. text='KuchlingKuchling'[0:ciph.block_size]
  43. c=obj.encrypt(text)
  44. if (obj.decrypt(c)!=text): die('Error encrypting "'+text+'"')
  45. text='NotTodayNotEver!'[0:ciph.block_size]
  46. c=obj.encrypt(text)
  47. if (obj.decrypt(c)!=text): die('Error encrypting "'+text+'"')
  48. start=time.time()
  49. s=obj.encrypt(str)
  50. s2=obj.decrypt(s)
  51. end=time.time()
  52. if (str!=s2):
  53. die('Error in resulting plaintext from ECB mode')
  54. if verbose: print 256/(end-start), 'K/sec'
  55. del obj
  56. if verbose: print ' CFB mode:',
  57. obj1=ciph.new(password, ciph.MODE_CFB, IV)
  58. obj2=ciph.new(password, ciph.MODE_CFB, IV)
  59. start=time.time()
  60. ciphertext=obj1.encrypt(str[0:65536])
  61. plaintext=obj2.decrypt(ciphertext)
  62. end=time.time()
  63. if (plaintext!=str[0:65536]):
  64. die('Error in resulting plaintext from CFB mode')
  65. if verbose: print 64/(end-start), 'K/sec'
  66. del obj1, obj2
  67. if verbose: print ' CBC mode:',
  68. obj1=ciph.new(password, ciph.MODE_CBC, IV)
  69. obj2=ciph.new(password, ciph.MODE_CBC, IV)
  70. start=time.time()
  71. ciphertext=obj1.encrypt(str)
  72. plaintext=obj2.decrypt(ciphertext)
  73. end=time.time()
  74. if (plaintext!=str):
  75. die('Error in resulting plaintext from CBC mode')
  76. if verbose: print 256/(end-start), 'K/sec'
  77. del obj1, obj2
  78. if verbose: print ' PGP mode:',
  79. obj1=ciph.new(password, ciph.MODE_PGP, IV)
  80. obj2=ciph.new(password, ciph.MODE_PGP, IV)
  81. start=time.time()
  82. ciphertext=obj1.encrypt(str)
  83. plaintext=obj2.decrypt(ciphertext)
  84. end=time.time()
  85. if (plaintext!=str):
  86. die('Error in resulting plaintext from PGP mode')
  87. if verbose: print 256/(end-start), 'K/sec'
  88. del obj1, obj2
  89. if verbose: print ' OFB mode:',
  90. obj1=ciph.new(password, ciph.MODE_OFB, IV)
  91. obj2=ciph.new(password, ciph.MODE_OFB, IV)
  92. start=time.time()
  93. ciphertext=obj1.encrypt(str)
  94. plaintext=obj2.decrypt(ciphertext)
  95. end=time.time()
  96. if (plaintext!=str):
  97. die('Error in resulting plaintext from OFB mode')
  98. if verbose: print 256/(end-start), 'K/sec'
  99. del obj1, obj2
  100. def counter(length=ciph.block_size):
  101. return length * 'a'
  102. if verbose: print ' CTR mode:',
  103. obj1=ciph.new(password, ciph.MODE_CTR, counter=counter)
  104. obj2=ciph.new(password, ciph.MODE_CTR, counter=counter)
  105. start=time.time()
  106. ciphertext=obj1.encrypt(str)
  107. plaintext=obj2.decrypt(ciphertext)
  108. end=time.time()
  109. if (plaintext!=str):
  110. die('Error in resulting plaintext from CTR mode')
  111. if verbose: print 256/(end-start), 'K/sec'
  112. del obj1, obj2
  113. # Test the IV handling
  114. if verbose: print ' Testing IV handling'
  115. obj1=ciph.new(password, ciph.MODE_CBC, IV)
  116. plaintext='Test'*(ciph.block_size/4)*3
  117. ciphertext1=obj1.encrypt(plaintext)
  118. obj1.IV=IV
  119. ciphertext2=obj1.encrypt(plaintext)
  120. if ciphertext1!=ciphertext2:
  121. die('Error in setting IV')
  122. # Test keyword arguments
  123. obj1=ciph.new(key=password)
  124. obj1=ciph.new(password, mode=ciph.MODE_CBC)
  125. obj1=ciph.new(mode=ciph.MODE_CBC, key=password)
  126. obj1=ciph.new(IV=IV, mode=ciph.MODE_CBC, key=password)
  127. return ciph
  128. def exerciseStreamCipher(cipher, verbose):
  129. import string, time
  130. try:
  131. ciph = eval(cipher)
  132. except (NameError):
  133. print cipher, 'module not available'
  134. return None
  135. print cipher + ':',
  136. str='1' # Build 128K of test data
  137. for i in xrange(0, 17):
  138. str=str+str
  139. key_size = ciph.key_size or 16
  140. password = 'password12345678Extra text for password'[0:key_size]
  141. obj1=ciph.new(password)
  142. obj2=ciph.new(password)
  143. if obj1.block_size != ciph.block_size:
  144. die("Module and cipher object block_size don't match")
  145. if obj1.key_size != ciph.key_size:
  146. die("Module and cipher object key_size don't match")
  147. text='1234567812345678Python'
  148. c=obj1.encrypt(text)
  149. if (obj2.decrypt(c)!=text): die('Error encrypting "'+text+'"')
  150. text='B1FF I2 A R3A11Y |<00L D00D!!!!!'
  151. c=obj1.encrypt(text)
  152. if (obj2.decrypt(c)!=text): die('Error encrypting "'+text+'"')
  153. text='SpamSpamSpamSpamSpamSpamSpamSpamSpam'
  154. c=obj1.encrypt(text)
  155. if (obj2.decrypt(c)!=text): die('Error encrypting "'+text+'"')
  156. start=time.time()
  157. s=obj1.encrypt(str)
  158. str=obj2.decrypt(s)
  159. end=time.time()
  160. if verbose: print 256/(end-start), 'K/sec'
  161. del obj1, obj2
  162. return ciph
  163. def TestStreamModules(args=['arc4', 'XOR'], verbose=1):
  164. import sys, string
  165. args=map(string.lower, args)
  166. if 'arc4' in args:
  167. # Test ARC4 stream cipher
  168. arc4=exerciseStreamCipher('ARC4', verbose)
  169. if (arc4!=None):
  170. for entry in testdata.arc4:
  171. key,plain,cipher=entry
  172. key=binascii.a2b_hex(key)
  173. plain=binascii.a2b_hex(plain)
  174. cipher=binascii.a2b_hex(cipher)
  175. obj=arc4.new(key)
  176. ciphertext=obj.encrypt(plain)
  177. if (ciphertext!=cipher):
  178. die('ARC4 failed on entry '+`entry`)
  179. if 'xor' in args:
  180. # Test XOR stream cipher
  181. XOR=exerciseStreamCipher('XOR', verbose)
  182. if (XOR!=None):
  183. for entry in testdata.xor:
  184. key,plain,cipher=entry
  185. key=binascii.a2b_hex(key)
  186. plain=binascii.a2b_hex(plain)
  187. cipher=binascii.a2b_hex(cipher)
  188. obj=XOR.new(key)
  189. ciphertext=obj.encrypt(plain)
  190. if (ciphertext!=cipher):
  191. die('XOR failed on entry '+`entry`)
  192. def TestBlockModules(args=['aes', 'arc2', 'des', 'blowfish', 'cast', 'des3',
  193. 'idea', 'rc5'],
  194. verbose=1):
  195. import string
  196. args=map(string.lower, args)
  197. if 'aes' in args:
  198. ciph=exerciseBlockCipher('AES', verbose) # AES
  199. if (ciph!=None):
  200. if verbose: print ' Verifying against test suite...'
  201. for entry in testdata.aes:
  202. key,plain,cipher=entry
  203. key=binascii.a2b_hex(key)
  204. plain=binascii.a2b_hex(plain)
  205. cipher=binascii.a2b_hex(cipher)
  206. obj=ciph.new(key, ciph.MODE_ECB)
  207. ciphertext=obj.encrypt(plain)
  208. if (ciphertext!=cipher):
  209. die('AES failed on entry '+`entry`)
  210. for i in ciphertext:
  211. if verbose: print hex(ord(i)),
  212. if verbose: print
  213. for entry in testdata.aes_modes:
  214. mode, key, plain, cipher, kw = entry
  215. key=binascii.a2b_hex(key)
  216. plain=binascii.a2b_hex(plain)
  217. cipher=binascii.a2b_hex(cipher)
  218. obj=ciph.new(key, mode, **kw)
  219. obj2=ciph.new(key, mode, **kw)
  220. ciphertext=obj.encrypt(plain)
  221. if (ciphertext!=cipher):
  222. die('AES encrypt failed on entry '+`entry`)
  223. for i in ciphertext:
  224. if verbose: print hex(ord(i)),
  225. if verbose: print
  226. plain2=obj2.decrypt(ciphertext)
  227. if plain2!=plain:
  228. die('AES decrypt failed on entry '+`entry`)
  229. for i in plain2:
  230. if verbose: print hex(ord(i)),
  231. if verbose: print
  232. if 'arc2' in args:
  233. ciph=exerciseBlockCipher('ARC2', verbose) # Alleged RC2
  234. if (ciph!=None):
  235. if verbose: print ' Verifying against test suite...'
  236. for entry in testdata.arc2:
  237. key,plain,cipher=entry
  238. key=binascii.a2b_hex(key)
  239. plain=binascii.a2b_hex(plain)
  240. cipher=binascii.a2b_hex(cipher)
  241. obj=ciph.new(key, ciph.MODE_ECB)
  242. ciphertext=obj.encrypt(plain)
  243. if (ciphertext!=cipher):
  244. die('ARC2 failed on entry '+`entry`)
  245. for i in ciphertext:
  246. if verbose: print hex(ord(i)),
  247. print
  248. if 'blowfish' in args:
  249. ciph=exerciseBlockCipher('Blowfish',verbose)# Bruce Schneier's Blowfish cipher
  250. if (ciph!=None):
  251. if verbose: print ' Verifying against test suite...'
  252. for entry in testdata.blowfish:
  253. key,plain,cipher=entry
  254. key=binascii.a2b_hex(key)
  255. plain=binascii.a2b_hex(plain)
  256. cipher=binascii.a2b_hex(cipher)
  257. obj=ciph.new(key, ciph.MODE_ECB)
  258. ciphertext=obj.encrypt(plain)
  259. if (ciphertext!=cipher):
  260. die('Blowfish failed on entry '+`entry`)
  261. for i in ciphertext:
  262. if verbose: print hex(ord(i)),
  263. if verbose: print
  264. if 'cast' in args:
  265. ciph=exerciseBlockCipher('CAST', verbose) # CAST-128
  266. if (ciph!=None):
  267. if verbose: print ' Verifying against test suite...'
  268. for entry in testdata.cast:
  269. key,plain,cipher=entry
  270. key=binascii.a2b_hex(key)
  271. plain=binascii.a2b_hex(plain)
  272. cipher=binascii.a2b_hex(cipher)
  273. obj=ciph.new(key, ciph.MODE_ECB)
  274. ciphertext=obj.encrypt(plain)
  275. if (ciphertext!=cipher):
  276. die('CAST failed on entry '+`entry`)
  277. for i in ciphertext:
  278. if verbose: print hex(ord(i)),
  279. if verbose: print
  280. if 0:
  281. # The full-maintenance test; it requires 4 million encryptions,
  282. # and correspondingly is quite time-consuming. I've disabled
  283. # it; it's faster to compile block/cast.c with -DTEST and run
  284. # the resulting program.
  285. a = b = '\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A'
  286. for i in range(0, 1000000):
  287. obj = cast.new(b, cast.MODE_ECB)
  288. a = obj.encrypt(a[:8]) + obj.encrypt(a[-8:])
  289. obj = cast.new(a, cast.MODE_ECB)
  290. b = obj.encrypt(b[:8]) + obj.encrypt(b[-8:])
  291. if a!="\xEE\xA9\xD0\xA2\x49\xFD\x3B\xA6\xB3\x43\x6F\xB8\x9D\x6D\xCA\x92":
  292. if verbose: print 'CAST test failed: value of "a" doesn\'t match'
  293. if b!="\xB2\xC9\x5E\xB0\x0C\x31\xAD\x71\x80\xAC\x05\xB8\xE8\x3D\x69\x6E":
  294. if verbose: print 'CAST test failed: value of "b" doesn\'t match'
  295. if 'des' in args:
  296. # Test/benchmark DES block cipher
  297. des=exerciseBlockCipher('DES', verbose)
  298. if (des!=None):
  299. # Various tests taken from the DES library packaged with Kerberos V4
  300. obj=des.new(binascii.a2b_hex('0123456789abcdef'), des.MODE_ECB)
  301. s=obj.encrypt('Now is t')
  302. if (s!=binascii.a2b_hex('3fa40e8a984d4815')):
  303. die('DES fails test 1')
  304. obj=des.new(binascii.a2b_hex('08192a3b4c5d6e7f'), des.MODE_ECB)
  305. s=obj.encrypt('\000\000\000\000\000\000\000\000')
  306. if (s!=binascii.a2b_hex('25ddac3e96176467')):
  307. die('DES fails test 2')
  308. obj=des.new(binascii.a2b_hex('0123456789abcdef'), des.MODE_CBC,
  309. binascii.a2b_hex('1234567890abcdef'))
  310. s=obj.encrypt("Now is the time for all ")
  311. if (s!=binascii.a2b_hex('e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6')):
  312. die('DES fails test 3')
  313. obj=des.new(binascii.a2b_hex('0123456789abcdef'), des.MODE_CBC,
  314. binascii.a2b_hex('fedcba9876543210'))
  315. s=obj.encrypt("7654321 Now is the time for \000\000\000\000")
  316. if (s!=binascii.a2b_hex("ccd173ffab2039f4acd8aefddfd8a1eb468e91157888ba681d269397f7fe62b4")):
  317. die('DES fails test 4')
  318. del obj,s
  319. # R. Rivest's test: see http://theory.lcs.mit.edu/~rivest/destest.txt
  320. x=binascii.a2b_hex('9474B8E8C73BCA7D')
  321. for i in range(0, 16):
  322. obj=des.new(x, des.MODE_ECB)
  323. if (i & 1): x=obj.decrypt(x)
  324. else: x=obj.encrypt(x)
  325. if x!=binascii.a2b_hex('1B1A2DDB4C642438'):
  326. die("DES fails Rivest's test")
  327. if verbose: print ' Verifying against test suite...'
  328. for entry in testdata.des:
  329. key,plain,cipher=entry
  330. key=binascii.a2b_hex(key)
  331. plain=binascii.a2b_hex(plain)
  332. cipher=binascii.a2b_hex(cipher)
  333. obj=des.new(key, des.MODE_ECB)
  334. ciphertext=obj.encrypt(plain)
  335. if (ciphertext!=cipher):
  336. die('DES failed on entry '+`entry`)
  337. for entry in testdata.des_cbc:
  338. key, iv, plain, cipher=entry
  339. key, iv, cipher=binascii.a2b_hex(key),binascii.a2b_hex(iv),binascii.a2b_hex(cipher)
  340. obj1=des.new(key, des.MODE_CBC, iv)
  341. obj2=des.new(key, des.MODE_CBC, iv)
  342. ciphertext=obj1.encrypt(plain)
  343. if (ciphertext!=cipher):
  344. die('DES CBC mode failed on entry '+`entry`)
  345. if 'des3' in args:
  346. ciph=exerciseBlockCipher('DES3', verbose) # Triple DES
  347. if (ciph!=None):
  348. if verbose: print ' Verifying against test suite...'
  349. for entry in testdata.des3:
  350. key,plain,cipher=entry
  351. key=binascii.a2b_hex(key)
  352. plain=binascii.a2b_hex(plain)
  353. cipher=binascii.a2b_hex(cipher)
  354. obj=ciph.new(key, ciph.MODE_ECB)
  355. ciphertext=obj.encrypt(plain)
  356. if (ciphertext!=cipher):
  357. die('DES3 failed on entry '+`entry`)
  358. for i in ciphertext:
  359. if verbose: print hex(ord(i)),
  360. if verbose: print
  361. for entry in testdata.des3_cbc:
  362. key, iv, plain, cipher=entry
  363. key, iv, cipher=binascii.a2b_hex(key),binascii.a2b_hex(iv),binascii.a2b_hex(cipher)
  364. obj1=ciph.new(key, ciph.MODE_CBC, iv)
  365. obj2=ciph.new(key, ciph.MODE_CBC, iv)
  366. ciphertext=obj1.encrypt(plain)
  367. if (ciphertext!=cipher):
  368. die('DES3 CBC mode failed on entry '+`entry`)
  369. if 'idea' in args:
  370. ciph=exerciseBlockCipher('IDEA', verbose) # IDEA block cipher
  371. if (ciph!=None):
  372. if verbose: print ' Verifying against test suite...'
  373. for entry in testdata.idea:
  374. key,plain,cipher=entry
  375. key=binascii.a2b_hex(key)
  376. plain=binascii.a2b_hex(plain)
  377. cipher=binascii.a2b_hex(cipher)
  378. obj=ciph.new(key, ciph.MODE_ECB)
  379. ciphertext=obj.encrypt(plain)
  380. if (ciphertext!=cipher):
  381. die('IDEA failed on entry '+`entry`)
  382. if 'rc5' in args:
  383. # Ronald Rivest's RC5 algorithm
  384. ciph=exerciseBlockCipher('RC5', verbose)
  385. if (ciph!=None):
  386. if verbose: print ' Verifying against test suite...'
  387. for entry in testdata.rc5:
  388. key,plain,cipher=entry
  389. key=binascii.a2b_hex(key)
  390. plain=binascii.a2b_hex(plain)
  391. cipher=binascii.a2b_hex(cipher)
  392. obj=ciph.new(key[4:], ciph.MODE_ECB,
  393. version =ord(key[0]),
  394. word_size=ord(key[1]),
  395. rounds =ord(key[2]) )
  396. ciphertext=obj.encrypt(plain)
  397. if (ciphertext!=cipher):
  398. die('RC5 failed on entry '+`entry`)
  399. for i in ciphertext:
  400. if verbose: print hex(ord(i)),
  401. if verbose: print