home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2258 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  11.6 KB  |  563 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __revision__ = '$Id: test.py,v 1.16 2004/08/13 22:24:18 akuchling Exp $'
  5. import binascii
  6. import string
  7. import testdata
  8. from Crypto.Cipher import *
  9.  
  10. def die(string):
  11.     import sys
  12.     print '***ERROR: ', string
  13.  
  14.  
  15. def print_timing(size, delta, verbose):
  16.     if verbose:
  17.         if delta == 0:
  18.             print 'Unable to measure time -- elapsed time too small'
  19.         else:
  20.             print '%.2f K/sec' % size / delta
  21.     
  22.  
  23.  
  24. def exerciseBlockCipher(cipher, verbose):
  25.     import string
  26.     import time
  27.     
  28.     try:
  29.         ciph = eval(cipher)
  30.     except NameError:
  31.         print cipher, 'module not available'
  32.         return None
  33.  
  34.     print cipher + ':'
  35.     str = '1'
  36.     for i in xrange(0, 17):
  37.         str = str + str
  38.     
  39.     if ciph.key_size == 0:
  40.         ciph.key_size = 16
  41.     
  42.     password = 'password12345678Extra text for password'[0:ciph.key_size]
  43.     IV = 'Test IV Test IV Test IV Test'[0:ciph.block_size]
  44.     if verbose:
  45.         print '  ECB mode:',
  46.     
  47.     obj = ciph.new(password, ciph.MODE_ECB)
  48.     if obj.block_size != ciph.block_size:
  49.         die("Module and cipher object block_size don't match")
  50.     
  51.     text = '1234567812345678'[0:ciph.block_size]
  52.     c = obj.encrypt(text)
  53.     if obj.decrypt(c) != text:
  54.         die('Error encrypting "' + text + '"')
  55.     
  56.     text = 'KuchlingKuchling'[0:ciph.block_size]
  57.     c = obj.encrypt(text)
  58.     if obj.decrypt(c) != text:
  59.         die('Error encrypting "' + text + '"')
  60.     
  61.     text = 'NotTodayNotEver!'[0:ciph.block_size]
  62.     c = obj.encrypt(text)
  63.     if obj.decrypt(c) != text:
  64.         die('Error encrypting "' + text + '"')
  65.     
  66.     start = time.time()
  67.     s = obj.encrypt(str)
  68.     s2 = obj.decrypt(s)
  69.     end = time.time()
  70.     if str != s2:
  71.         die('Error in resulting plaintext from ECB mode')
  72.     
  73.     print_timing(256, end - start, verbose)
  74.     del obj
  75.     if verbose:
  76.         print '  CFB mode:',
  77.     
  78.     obj1 = ciph.new(password, ciph.MODE_CFB, IV)
  79.     obj2 = ciph.new(password, ciph.MODE_CFB, IV)
  80.     start = time.time()
  81.     ciphertext = obj1.encrypt(str[0:65536])
  82.     plaintext = obj2.decrypt(ciphertext)
  83.     end = time.time()
  84.     if plaintext != str[0:65536]:
  85.         die('Error in resulting plaintext from CFB mode')
  86.     
  87.     print_timing(64, end - start, verbose)
  88.     del obj1
  89.     del obj2
  90.     if verbose:
  91.         print '  CBC mode:',
  92.     
  93.     obj1 = ciph.new(password, ciph.MODE_CBC, IV)
  94.     obj2 = ciph.new(password, ciph.MODE_CBC, IV)
  95.     start = time.time()
  96.     ciphertext = obj1.encrypt(str)
  97.     plaintext = obj2.decrypt(ciphertext)
  98.     end = time.time()
  99.     if plaintext != str:
  100.         die('Error in resulting plaintext from CBC mode')
  101.     
  102.     print_timing(256, end - start, verbose)
  103.     del obj1
  104.     del obj2
  105.     if verbose:
  106.         print '  PGP mode:',
  107.     
  108.     obj1 = ciph.new(password, ciph.MODE_PGP, IV)
  109.     obj2 = ciph.new(password, ciph.MODE_PGP, IV)
  110.     start = time.time()
  111.     ciphertext = obj1.encrypt(str)
  112.     plaintext = obj2.decrypt(ciphertext)
  113.     end = time.time()
  114.     if plaintext != str:
  115.         die('Error in resulting plaintext from PGP mode')
  116.     
  117.     print_timing(256, end - start, verbose)
  118.     del obj1
  119.     del obj2
  120.     if verbose:
  121.         print '  OFB mode:',
  122.     
  123.     obj1 = ciph.new(password, ciph.MODE_OFB, IV)
  124.     obj2 = ciph.new(password, ciph.MODE_OFB, IV)
  125.     start = time.time()
  126.     ciphertext = obj1.encrypt(str)
  127.     plaintext = obj2.decrypt(ciphertext)
  128.     end = time.time()
  129.     if plaintext != str:
  130.         die('Error in resulting plaintext from OFB mode')
  131.     
  132.     print_timing(256, end - start, verbose)
  133.     del obj1
  134.     del obj2
  135.     
  136.     def counter(length = ciph.block_size):
  137.         return length * 'a'
  138.  
  139.     if verbose:
  140.         print '  CTR mode:',
  141.     
  142.     obj1 = ciph.new(password, ciph.MODE_CTR, counter = counter)
  143.     obj2 = ciph.new(password, ciph.MODE_CTR, counter = counter)
  144.     start = time.time()
  145.     ciphertext = obj1.encrypt(str)
  146.     plaintext = obj2.decrypt(ciphertext)
  147.     end = time.time()
  148.     if plaintext != str:
  149.         die('Error in resulting plaintext from CTR mode')
  150.     
  151.     print_timing(256, end - start, verbose)
  152.     del obj1
  153.     del obj2
  154.     if verbose:
  155.         print '  Testing IV handling'
  156.     
  157.     obj1 = ciph.new(password, ciph.MODE_CBC, IV)
  158.     plaintext = 'Test' * (ciph.block_size / 4) * 3
  159.     ciphertext1 = obj1.encrypt(plaintext)
  160.     obj1.IV = IV
  161.     ciphertext2 = obj1.encrypt(plaintext)
  162.     if ciphertext1 != ciphertext2:
  163.         die('Error in setting IV')
  164.     
  165.     obj1 = ciph.new(key = password)
  166.     obj1 = ciph.new(password, mode = ciph.MODE_CBC)
  167.     obj1 = ciph.new(mode = ciph.MODE_CBC, key = password)
  168.     obj1 = ciph.new(IV = IV, mode = ciph.MODE_CBC, key = password)
  169.     return ciph
  170.  
  171.  
  172. def exerciseStreamCipher(cipher, verbose):
  173.     import string
  174.     import time
  175.     
  176.     try:
  177.         ciph = eval(cipher)
  178.     except NameError:
  179.         print cipher, 'module not available'
  180.         return None
  181.  
  182.     print cipher + ':',
  183.     str = '1'
  184.     for i in xrange(0, 17):
  185.         str = str + str
  186.     
  187.     if not ciph.key_size:
  188.         pass
  189.     key_size = 16
  190.     password = 'password12345678Extra text for password'[0:key_size]
  191.     obj1 = ciph.new(password)
  192.     obj2 = ciph.new(password)
  193.     if obj1.block_size != ciph.block_size:
  194.         die("Module and cipher object block_size don't match")
  195.     
  196.     if obj1.key_size != ciph.key_size:
  197.         die("Module and cipher object key_size don't match")
  198.     
  199.     text = '1234567812345678Python'
  200.     c = obj1.encrypt(text)
  201.     if obj2.decrypt(c) != text:
  202.         die('Error encrypting "' + text + '"')
  203.     
  204.     text = 'B1FF I2 A R3A11Y |<00L D00D!!!!!'
  205.     c = obj1.encrypt(text)
  206.     if obj2.decrypt(c) != text:
  207.         die('Error encrypting "' + text + '"')
  208.     
  209.     text = 'SpamSpamSpamSpamSpamSpamSpamSpamSpam'
  210.     c = obj1.encrypt(text)
  211.     if obj2.decrypt(c) != text:
  212.         die('Error encrypting "' + text + '"')
  213.     
  214.     start = time.time()
  215.     s = obj1.encrypt(str)
  216.     str = obj2.decrypt(s)
  217.     end = time.time()
  218.     print_timing(256, end - start, verbose)
  219.     del obj1
  220.     del obj2
  221.     return ciph
  222.  
  223.  
  224. def TestStreamModules(args = [
  225.     'arc4',
  226.     'XOR'], verbose = 1):
  227.     import sys
  228.     import string
  229.     args = map(string.lower, args)
  230.     if 'arc4' in args:
  231.         arc4 = exerciseStreamCipher('ARC4', verbose)
  232.         if arc4 != None:
  233.             for entry in testdata.arc4:
  234.                 (key, plain, cipher) = entry
  235.                 key = binascii.a2b_hex(key)
  236.                 plain = binascii.a2b_hex(plain)
  237.                 cipher = binascii.a2b_hex(cipher)
  238.                 obj = arc4.new(key)
  239.                 ciphertext = obj.encrypt(plain)
  240.                 if ciphertext != cipher:
  241.                     die('ARC4 failed on entry ' + `entry`)
  242.                     continue
  243.             
  244.         
  245.     
  246.     if 'xor' in args:
  247.         XOR = exerciseStreamCipher('XOR', verbose)
  248.         if XOR != None:
  249.             for entry in testdata.xor:
  250.                 (key, plain, cipher) = entry
  251.                 key = binascii.a2b_hex(key)
  252.                 plain = binascii.a2b_hex(plain)
  253.                 cipher = binascii.a2b_hex(cipher)
  254.                 obj = XOR.new(key)
  255.                 ciphertext = obj.encrypt(plain)
  256.                 if ciphertext != cipher:
  257.                     die('XOR failed on entry ' + `entry`)
  258.                     continue
  259.             
  260.         
  261.     
  262.  
  263.  
  264. def TestBlockModules(args = [
  265.     'aes',
  266.     'arc2',
  267.     'des',
  268.     'blowfish',
  269.     'cast',
  270.     'des3',
  271.     'idea',
  272.     'rc5'], verbose = 1):
  273.     import string
  274.     args = map(string.lower, args)
  275.     if 'aes' in args:
  276.         ciph = exerciseBlockCipher('AES', verbose)
  277.         if ciph != None:
  278.             if verbose:
  279.                 print '  Verifying against test suite...'
  280.             
  281.             for entry in testdata.aes:
  282.                 (key, plain, cipher) = entry
  283.                 key = binascii.a2b_hex(key)
  284.                 plain = binascii.a2b_hex(plain)
  285.                 cipher = binascii.a2b_hex(cipher)
  286.                 obj = ciph.new(key, ciph.MODE_ECB)
  287.                 ciphertext = obj.encrypt(plain)
  288.                 if ciphertext != cipher:
  289.                     die('AES failed on entry ' + `entry`)
  290.                     for i in ciphertext:
  291.                         if verbose:
  292.                             print hex(ord(i)),
  293.                             continue
  294.                     
  295.                     if verbose:
  296.                         print 
  297.                     
  298.                 verbose
  299.             
  300.             for entry in testdata.aes_modes:
  301.                 (mode, key, plain, cipher, kw) = entry
  302.                 key = binascii.a2b_hex(key)
  303.                 plain = binascii.a2b_hex(plain)
  304.                 cipher = binascii.a2b_hex(cipher)
  305.                 obj = ciph.new(key, mode, **kw)
  306.                 obj2 = ciph.new(key, mode, **kw)
  307.                 ciphertext = obj.encrypt(plain)
  308.                 if ciphertext != cipher:
  309.                     die('AES encrypt failed on entry ' + `entry`)
  310.                     for i in ciphertext:
  311.                         if verbose:
  312.                             print hex(ord(i)),
  313.                             continue
  314.                     
  315.                     if verbose:
  316.                         print 
  317.                     
  318.                 
  319.                 plain2 = obj2.decrypt(ciphertext)
  320.                 if plain2 != plain:
  321.                     die('AES decrypt failed on entry ' + `entry`)
  322.                     for i in plain2:
  323.                         if verbose:
  324.                             print hex(ord(i)),
  325.                             continue
  326.                     
  327.                     if verbose:
  328.                         print 
  329.                     
  330.                 verbose
  331.             
  332.         
  333.     
  334.     if 'arc2' in args:
  335.         ciph = exerciseBlockCipher('ARC2', verbose)
  336.         if ciph != None:
  337.             if verbose:
  338.                 print '  Verifying against test suite...'
  339.             
  340.             for entry in testdata.arc2:
  341.                 (key, plain, cipher) = entry
  342.                 key = binascii.a2b_hex(key)
  343.                 plain = binascii.a2b_hex(plain)
  344.                 cipher = binascii.a2b_hex(cipher)
  345.                 obj = ciph.new(key, ciph.MODE_ECB)
  346.                 ciphertext = obj.encrypt(plain)
  347.                 if ciphertext != cipher:
  348.                     die('ARC2 failed on entry ' + `entry`)
  349.                     for i in ciphertext:
  350.                         if verbose:
  351.                             print hex(ord(i)),
  352.                             continue
  353.                     
  354.                     print 
  355.                     continue
  356.             
  357.         
  358.     
  359.     if 'blowfish' in args:
  360.         ciph = exerciseBlockCipher('Blowfish', verbose)
  361.         if ciph != None:
  362.             if verbose:
  363.                 print '  Verifying against test suite...'
  364.             
  365.             for entry in testdata.blowfish:
  366.                 (key, plain, cipher) = entry
  367.                 key = binascii.a2b_hex(key)
  368.                 plain = binascii.a2b_hex(plain)
  369.                 cipher = binascii.a2b_hex(cipher)
  370.                 obj = ciph.new(key, ciph.MODE_ECB)
  371.                 ciphertext = obj.encrypt(plain)
  372.                 if ciphertext != cipher:
  373.                     die('Blowfish failed on entry ' + `entry`)
  374.                     for i in ciphertext:
  375.                         if verbose:
  376.                             print hex(ord(i)),
  377.                             continue
  378.                     
  379.                     if verbose:
  380.                         print 
  381.                     
  382.                 verbose
  383.             
  384.         
  385.     
  386.     if 'cast' in args:
  387.         ciph = exerciseBlockCipher('CAST', verbose)
  388.         if ciph != None:
  389.             if verbose:
  390.                 print '  Verifying against test suite...'
  391.             
  392.             for entry in testdata.cast:
  393.                 (key, plain, cipher) = entry
  394.                 key = binascii.a2b_hex(key)
  395.                 plain = binascii.a2b_hex(plain)
  396.                 cipher = binascii.a2b_hex(cipher)
  397.                 obj = ciph.new(key, ciph.MODE_ECB)
  398.                 ciphertext = obj.encrypt(plain)
  399.                 if ciphertext != cipher:
  400.                     die('CAST failed on entry ' + `entry`)
  401.                     for i in ciphertext:
  402.                         if verbose:
  403.                             print hex(ord(i)),
  404.                             continue
  405.                     
  406.                     if verbose:
  407.                         print 
  408.                     
  409.                 verbose
  410.             
  411.         
  412.     
  413.     if 'des' in args:
  414.         des = exerciseBlockCipher('DES', verbose)
  415.         if des != None:
  416.             obj = des.new(binascii.a2b_hex('0123456789abcdef'), des.MODE_ECB)
  417.             s = obj.encrypt('Now is t')
  418.             if s != binascii.a2b_hex('3fa40e8a984d4815'):
  419.                 die('DES fails test 1')
  420.             
  421.             obj = des.new(binascii.a2b_hex('08192a3b4c5d6e7f'), des.MODE_ECB)
  422.             s = obj.encrypt('\x00\x00\x00\x00\x00\x00\x00\x00')
  423.             if s != binascii.a2b_hex('25ddac3e96176467'):
  424.                 die('DES fails test 2')
  425.             
  426.             obj = des.new(binascii.a2b_hex('0123456789abcdef'), des.MODE_CBC, binascii.a2b_hex('1234567890abcdef'))
  427.             s = obj.encrypt('Now is the time for all ')
  428.             if s != binascii.a2b_hex('e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6'):
  429.                 die('DES fails test 3')
  430.             
  431.             obj = des.new(binascii.a2b_hex('0123456789abcdef'), des.MODE_CBC, binascii.a2b_hex('fedcba9876543210'))
  432.             s = obj.encrypt('7654321 Now is the time for \x00\x00\x00\x00')
  433.             if s != binascii.a2b_hex('ccd173ffab2039f4acd8aefddfd8a1eb468e91157888ba681d269397f7fe62b4'):
  434.                 die('DES fails test 4')
  435.             
  436.             del obj
  437.             del s
  438.             x = binascii.a2b_hex('9474B8E8C73BCA7D')
  439.             for i in range(0, 16):
  440.                 obj = des.new(x, des.MODE_ECB)
  441.                 if i & 1:
  442.                     x = obj.decrypt(x)
  443.                     continue
  444.                 x = obj.encrypt(x)
  445.             
  446.             if x != binascii.a2b_hex('1B1A2DDB4C642438'):
  447.                 die("DES fails Rivest's test")
  448.             
  449.             if verbose:
  450.                 print '  Verifying against test suite...'
  451.             
  452.             for entry in testdata.des:
  453.                 (key, plain, cipher) = entry
  454.                 key = binascii.a2b_hex(key)
  455.                 plain = binascii.a2b_hex(plain)
  456.                 cipher = binascii.a2b_hex(cipher)
  457.                 obj = des.new(key, des.MODE_ECB)
  458.                 ciphertext = obj.encrypt(plain)
  459.                 if ciphertext != cipher:
  460.                     die('DES failed on entry ' + `entry`)
  461.                     continue
  462.             
  463.             for entry in testdata.des_cbc:
  464.                 (key, iv, plain, cipher) = entry
  465.                 key = binascii.a2b_hex(key)
  466.                 iv = binascii.a2b_hex(iv)
  467.                 cipher = binascii.a2b_hex(cipher)
  468.                 obj1 = des.new(key, des.MODE_CBC, iv)
  469.                 obj2 = des.new(key, des.MODE_CBC, iv)
  470.                 ciphertext = obj1.encrypt(plain)
  471.                 if ciphertext != cipher:
  472.                     die('DES CBC mode failed on entry ' + `entry`)
  473.                     continue
  474.             
  475.         
  476.     
  477.     if 'des3' in args:
  478.         ciph = exerciseBlockCipher('DES3', verbose)
  479.         if ciph != None:
  480.             if verbose:
  481.                 print '  Verifying against test suite...'
  482.             
  483.             for entry in testdata.des3:
  484.                 (key, plain, cipher) = entry
  485.                 key = binascii.a2b_hex(key)
  486.                 plain = binascii.a2b_hex(plain)
  487.                 cipher = binascii.a2b_hex(cipher)
  488.                 obj = ciph.new(key, ciph.MODE_ECB)
  489.                 ciphertext = obj.encrypt(plain)
  490.                 if ciphertext != cipher:
  491.                     die('DES3 failed on entry ' + `entry`)
  492.                     for i in ciphertext:
  493.                         if verbose:
  494.                             print hex(ord(i)),
  495.                             continue
  496.                     
  497.                     if verbose:
  498.                         print 
  499.                     
  500.                 verbose
  501.             
  502.             for entry in testdata.des3_cbc:
  503.                 (key, iv, plain, cipher) = entry
  504.                 key = binascii.a2b_hex(key)
  505.                 iv = binascii.a2b_hex(iv)
  506.                 cipher = binascii.a2b_hex(cipher)
  507.                 obj1 = ciph.new(key, ciph.MODE_CBC, iv)
  508.                 obj2 = ciph.new(key, ciph.MODE_CBC, iv)
  509.                 ciphertext = obj1.encrypt(plain)
  510.                 if ciphertext != cipher:
  511.                     die('DES3 CBC mode failed on entry ' + `entry`)
  512.                     continue
  513.             
  514.         
  515.     
  516.     if 'idea' in args:
  517.         ciph = exerciseBlockCipher('IDEA', verbose)
  518.         if ciph != None:
  519.             if verbose:
  520.                 print '  Verifying against test suite...'
  521.             
  522.             for entry in testdata.idea:
  523.                 (key, plain, cipher) = entry
  524.                 key = binascii.a2b_hex(key)
  525.                 plain = binascii.a2b_hex(plain)
  526.                 cipher = binascii.a2b_hex(cipher)
  527.                 obj = ciph.new(key, ciph.MODE_ECB)
  528.                 ciphertext = obj.encrypt(plain)
  529.                 if ciphertext != cipher:
  530.                     die('IDEA failed on entry ' + `entry`)
  531.                     continue
  532.             
  533.         
  534.     
  535.     if 'rc5' in args:
  536.         ciph = exerciseBlockCipher('RC5', verbose)
  537.         if ciph != None:
  538.             if verbose:
  539.                 print '  Verifying against test suite...'
  540.             
  541.             for entry in testdata.rc5:
  542.                 (key, plain, cipher) = entry
  543.                 key = binascii.a2b_hex(key)
  544.                 plain = binascii.a2b_hex(plain)
  545.                 cipher = binascii.a2b_hex(cipher)
  546.                 obj = ciph.new(key[4:], ciph.MODE_ECB, version = ord(key[0]), word_size = ord(key[1]), rounds = ord(key[2]))
  547.                 ciphertext = obj.encrypt(plain)
  548.                 if ciphertext != cipher:
  549.                     die('RC5 failed on entry ' + `entry`)
  550.                     for i in ciphertext:
  551.                         if verbose:
  552.                             print hex(ord(i)),
  553.                             continue
  554.                     
  555.                     if verbose:
  556.                         print 
  557.                     
  558.                 verbose
  559.             
  560.         
  561.     
  562.  
  563.