Exemplo n.º 1
0
 def test(mode_name, enc_mode, dec_mode, block_size):
     print 'Test mode %s block size %d' % (mode_name, block_size)
     print '     one chank coding'
     iv = ''.join(map(chr, range(48 + block_size, 48, -1)))
     c = fake_chifer()
     text = 'ABCDEFGHabcdefgh'
     e = enc_mode(c, iv)
     cryp = e(text)
     d = dec_mode(c, iv)
     textb = d(cryp)
     print pad('%s %s' % (text, textb), 70), ok(text == textb)
     print '     stream coding'
     text = ''.join(map(lambda x: chr(x), xrange(65, 65+32)))
     e = enc_mode(c, iv)
     cryp = ''
     cryp += e(text[:16])
     cryp += e(text[16:])
     d = dec_mode(c, iv)
     textb = ''
     textb += d(cryp[:16])
     textb += d(cryp[16:])
     print pad('%s %s' % (text, textb), 70), ok(text == textb)
Exemplo n.º 2
0
        ),
        ('\x8e\x73\xb0\xf7\xda\x0e\x64\x52\xc8\x10\xf3\x2b\x80\x90\x79\xe5\x62\xf8\xea\xd2\x52\x2c\x6b\x7b',
         (('\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a',
           '\xbd\x33\x4f\x1d\x6e\x45\xf2\x5f\xf7\x12\xa2\x14\x57\x1f\xa5\xcc'),
          ('\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51',
           '\x97\x41\x04\x84\x6d\x0a\xd3\xad\x77\x34\xec\xb3\xec\xee\x4e\xef'),
          ('\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef',
           '\xef\x7a\xfd\x22\x70\xe2\xe6\x0a\xdc\xe0\xba\x2f\xac\xe6\x44\x4e'),
          ('\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10',
           '\x9a\x4b\x41\xba\x73\x8d\x6c\x72\xfb\x16\x69\x16\x03\xc1\x8e\x0e'),
         )
        ),
        ('\x60\x3d\xeb\x10\x15\xca\x71\xbe\x2b\x73\xae\xf0\x85\x7d\x77\x81\x1f\x35\x2c\x07\x3b\x61\x08\xd7\x2d\x98\x10\xa3\x09\x14\xdf\xf4',
         (('\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a',
           '\xf3\xee\xd1\xbd\xb5\xd2\xa0\x3c\x06\x4b\x5a\x7e\x3d\xb1\x81\xf8'),
          ('\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51',
           '\x59\x1c\xcb\x10\xd4\x10\xed\x26\xdc\x5b\xa7\x4a\x31\x36\x28\x70'),
          ('\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef',
           '\xb6\xed\x21\xb9\x9c\xa6\xf4\xf9\xf1\x53\xe7\xb1\xbe\xaf\xed\x1d'),
          ('\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10',
           '\x23\x30\x4b\x7a\x39\xf9\xf3\xff\x06\x7d\x8d\x8f\x9e\x24\xec\xc7'),
         )
        )):
        print 'key_len = %d bit' % (len(key) * 8)
        c = aes(key)
        for text, cryp in text_cryp:
            cc = c.enc(text)
            print qrepr(cryp), ok(cc == cryp)
            tt = c.dec(cc)
            print qrepr(tt), ok(tt == text)
Exemplo n.º 3
0
    from hashlib import md5 as md5_new
except ImportError:
    # for Python < 2.5
    from md5 import new as md5_new

__all__ = 'gen_salt', 'passphrase_to_salted_key_and_iv'

__doc__ = '''Tools related to key derivation service

See RFC2898 PKCS #5: Password-Based Cryptography Specification Version 2.0'''

md5 = lambda x: md5_new(x).digest()

def gen_salt(saltlen=8):
    return ''.join(map(lambda x: chr(randrange(256)), xrange(saltlen)))

def passphrase_to_salted_key_and_iv(passphrase, salt='', klen=16, ivlen=8):
    dklen = klen + ivlen
    dk = ''
    d = ''
    while len(dk) < dklen:
        d = md5(d + passphrase + salt)
        dk += d
    return (dk[:klen], dk[klen:dklen])

if __name__ == '__main__':
    from testutil import ok, qrepr
    a = '\x09\x8f\x6b\xcd\x46\x21\xd3\x73\xca\xde\x4e\x83\x26\x27\xb4\xf6'
    b = md5('test')
    print qrepr(b), ok(a == b)
Exemplo n.º 4
0
     ('\x00\x00\x00\x00\x00\x00\x00\x00',
      '\xff\xff\xff\xff\xff\xff\xff\xff',
      '\x01\x49\x33\xe0\xcd\xaf\xf6\xe4'),
     ('\xff\xff\xff\xff\xff\xff\xff\xff',
      '\x00\x00\x00\x00\x00\x00\x00\x00',
      '\xf2\x1e\x9a\x77\xb7\x1c\x49\xbc'),
     ('\x01\x23\x45\x67\x89\xab\xcd\xef',
      '\x00\x00\x00\x00\x00\x00\x00\x00',
      '\x24\x59\x46\x88\x57\x54\x36\x9a'),
     ('\xfe\xdc\xba\x98\x76\x54\x32\x10',
      '\xff\xff\xff\xff\xff\xff\xff\xff',
      '\x6b\x5c\x5a\x9c\x5d\x9e\x0a\x5a'),):
     c = blowfish(k)
     ee = c.enc(t)
     tt = c.dec(ee)
     print qrepr(e), qrepr(ee), ok(e == ee)
     print qrepr(t), qrepr(tt), ok(t == tt)
 # Test keys and low-level interface
 kk = '\xf0\xe1\xd2\xc3\xb4\xa5\x96\x87\x78\x69\x5a\x4b\x3c\x2d\x1e\x0f\x00\x11\x22\x33\x44\x55\x66\x77'
 p, q = text_to_pair('\xfe\xdc\xba\x98\x76\x54\x32\x10')
 for l, e in zip(xrange(1, 25), (
     '\xf9\xad\x59\x7c\x49\xdb\x00\x5e',
     '\xe9\x1d\x21\xc1\xd9\x61\xa6\xd6',
     '\xe9\xc2\xb7\x0a\x1b\xc6\x5c\xf3',
     '\xbe\x1e\x63\x94\x08\x64\x0f\x05',
     '\xb3\x9e\x44\x48\x1b\xdb\x1e\x6e',
     '\x94\x57\xaa\x83\xb1\x92\x8c\x0d',
     '\x8b\xb7\x70\x32\xf9\x60\x62\x9d',
     '\xe8\x7a\x24\x4e\x2c\xc8\x5e\x82',
     '\x15\x75\x0e\x7a\x4f\x4e\xc5\x77',
     '\x12\x2b\xa7\x0b\x3a\xb6\x4a\xe0',
Exemplo n.º 5
0
('openssl -aes-256-ecb -nosalt', openssl_enc_aes_256_ecb, openssl_dec_aes_256_ecb, False, '\xce\xac6\xc4\xed\xcf\x98\xfa\x91o\x8b\x20\xd0e\xd4x\x8ek5\xc3\xe7\x16\x5c\xa2\xb5Y\xc7\x89\x0f_\x07\xff'),
('openssl -aes-128-cbc -salt', openssl_enc_aes_128_cbc, openssl_dec_aes_128_cbc, True, 'Salted__\x2dd\x22\xdd\x7d\x93\xdc\xa6\xcbj\x9cu\x2d\xa0\xb2\x26\x5d\xf9\x3b\x8d\xb4s7\x92a\xd1\x98\x9e\x04Pmo\x16\x0a\xd2\x14\x92\xb0l\x2e'),
('openssl -aes-128-cbc -nosalt', openssl_enc_aes_128_cbc, openssl_dec_aes_128_cbc, False, '\x04Us\x99\xbc\x0f\xaf\xdb\xeb\xd9\x91\xf1\x3c\x0dr3\xf9\xa8\xe7i\x3fF\xf7M\x236\xa6\xc3XJ\x87\xda'),
('openssl -aes-192-cbc -salt', openssl_enc_aes_192_cbc, openssl_dec_aes_192_cbc, True, 'Salted__\xb0t\x95\xc1\x8e\xe8\xcdr\x03\x1e\x9d\x1afJx\xa0\xdcU\x0ap\xf0\xd0\xe96\x8aoJ\x17\xe5\x8a\x81\xc7\x5e\xb3\xcd\xd8\xe0\xfe\x90S'),
('openssl -aes-192-cbc -nosalt', openssl_enc_aes_192_cbc, openssl_dec_aes_192_cbc, False, '\xc5\xbaF\xecm\xea\x00\xc4\x1bv\x01\xec\x99\x9c\x81\xe2\xd7\x1d\xe8\x3e\x17\xf3\x89\x90\xbf\x5b\x0b\x98Q\x00\x40G'),
('openssl -aes-256-cbc -salt', openssl_enc_aes_256_cbc, openssl_dec_aes_256_cbc, True, 'Salted__5\x5b\x0a\xe8M\xc9\xf8\xc7K\xbe\xfe\x84\x9cK\xc2\x08\xdd\x2c\x9e\xfe\xb5\xcd\x1b\x11\x201\xb95C\x0a\xaa\xd9N\x0b\x01n\xb2\xa5\x18W'),
('openssl -aes-256-cbc -nosalt', openssl_enc_aes_256_cbc, openssl_dec_aes_256_cbc, False, '\xe1\xd7cS\xe5\x60\x1a\x27\xb8\xbce\x2f\xfa\x06\xdc\x17\xef\x26\x91\x83\x3afG\x87\xb4\x9d\x0b5\xf7s\x9c\x02'),
    ):
        if salted:
            salt = ref_cipher[8:16]
        else:
            salt = None
        e = enc_op(passphrase, text, salted, salt)
#        print repr(e)
#        print repr(ref_cipher)
        test_enc = ok(e == ref_cipher)
        t = dec_op(passphrase, e)
#        print repr(t)
#        print repr(text)
        test_dec = ok(t == text)
        # random salt
        t = dec_op(passphrase, enc_op(passphrase, text))
        test_rand = ok(t == text)
        print pad(rem, 50), \
              'enc:', test_enc, \
              'dec:', test_dec, \
              'rand_salt:', test_enc
    __how_to_preapre_test_vectors__=r'''
#!/bin/sh

data='Red leather, Yellow leather.'
Exemplo n.º 6
0
('Self PCBC/Blowfish/nosalt klen=16', enc_bf_pcbc, dec_bf_pcbc, 16, '', 'g\xec\x11\xc2(\xd4\xb5W\x80\xc8\xc7\xdc8\xa3.^\xf7?\x87\x9dC\xcf\x85c\x8b\xce\x12\x8a?#E"'),
('Self PCBC/Blowfish/salt klen=32 (=perl)', enc_bf_pcbc, dec_bf_pcbc, 32, '2,Y\x1d\xd0`y<', '\x1e\xca\xe8B\xa6\x82n\x1e\x90\x06L\xc21\xdbA\x05\xa2\x8b\xcbH\x0e\xa4Y\xf0\xc1\x81-\xf4lJ\xb0\x88'),
('Self PCBC/Blowfish/nosalt klen=32', enc_bf_pcbc, dec_bf_pcbc, 32, '', 'kkm\x87"\xb9{_\x06\x99\x15\xa8\x90m@uyo\xa5\xea\xcb"\xb5j\xfd\x9d\xaf\x9d \xfb\xa4\xd1'),
('Self PCBC/Blowfish/salt klen=48 (=perl)', enc_bf_pcbc, dec_bf_pcbc, 48, '9m\xa3\x7f\xb5N\x9b\x03', 'B\x9dm\xfa\xe0cr\xef\xdb\xe4\x06\x03\n\xb9\x0f\xdfn^\xbd\xb7j\x96\xed\x932R\xa5\xebg[ \xa8'),
('Self PCBC/Blowfish/nosalt klen=48', enc_bf_pcbc, dec_bf_pcbc, 48, '', 'g\xaf{\x0c\x1f\xdf\xeb_P=\x11\xdb\xf0\x03#\xfd\xc6I\xea\xa9g\xa5\x0e\xa9\xa3\xb6\r\xdb\x1d\x0em\x9e'),
('Self PCBC/Blowfish/salt klen=56 (=perl)', enc_bf_pcbc, dec_bf_pcbc, 56, '\x88\x90\xcdJ\r\\Z]', '\x03\x8c%\x83\xdd\x0f\x18^\xb0\x89\x96d-\xac\xb6\xf1\x1b\x0b\x87\x04\xac\x02\x06BzV\xab\xfe%\x1ao.'),
('Self PCBC/Blowfish/nosalt klen=56', enc_bf_pcbc, dec_bf_pcbc, 56, '', "#\xe8'(\xd8\x01\x05\xd1\xa5g\xe1\x9eU\x07\x18\xa4\x97\x1d\xe1rg1\xfe\xbb\x96\xc2\xa0J\xe1\x01K\x17"),

('Self ECB/AES/salt klen=16 (=openssl)', enc_aes_ecb, dec_aes_ecb, 16, '\xc9\xef8\xb8B\x97\xa7*', '\xf8\xdav\xbal\xf4\xcdN\xe8\x02\x15u\xf17\x80(y,\xc1\xb5\xa6j\x82\x072;\xdd\x90e\x98|\xb5'),
('Self ECB/AES/nosalt klen=16 (=openssl)', enc_aes_ecb, dec_aes_ecb, 16, '', '\xb2^\x0fO\xc4\xfd\xae\x8f\x99\xa8\x18\x93\x83\xd2:\x9b\x9a3\x98\xcbk\xdc\x195\xc85-\xab\xb6U\x9f\xd5'),
('Self ECB/AES/salt klen=24 (=openssl)', enc_aes_ecb, dec_aes_ecb, 24, 'Te\xe6\xcd\xb9\x92:\xd2', '\xac\x01Kg\xa0u\x92\xc3\xb1\x9f\x06\x9f \xa7\xc2\x8c\x0f\xd5\xe88\x90\x95\x10\xdej6\x96D\x9aFU\xd2'),
('Self ECB/AES/nosalt klen=24 (=openssl)', enc_aes_ecb, dec_aes_ecb, 24, '', "\xd7\xac*!\x84\x94O\xa3\xb7\x90\xcbj\xfb\xa8\xa0=\x11\xb1\x10#\xd4,\xebu4>\xe4\xddO\x12b'"),
('Self ECB/AES/salt klen=32 (=openssl)', enc_aes_ecb, dec_aes_ecb, 32, '\xf8\xed\xf7R\xdc\x89\xff\xab', '\xe1\x07\x85\x05\x04\xd9\xb7L\xa5\xfaq\x19\xe0\xf0\xc5W\x95!\xb9\xdbd\xa8\xc66\x1e\x1c\xb6\xc4\xd6u/\xba'),
('Self ECB/AES/nosalt klen=32 (=openssl)', enc_aes_ecb, dec_aes_ecb, 32, '', '\xce\xac6\xc4\xed\xcf\x98\xfa\x91o\x8b \xd0e\xd4x\x8ek5\xc3\xe7\x16\\\xa2\xb5Y\xc7\x89\x0f_\x07\xff'),
('Self CBC/AES/salt klen=16 (=openssl)', enc_aes_cbc, dec_aes_cbc, 16, '\x8fV\xab3\xe4\xda[6', '\xdb\xdc\x0cD\x88l\x19t\x8a\xa7\x00\xf2\x04\x9d\xdc\x84\xf52\x88\xd3\xf4o,I\xa4\xe4q\xbc\xde\xb1K\x05'),
('Self CBC/AES/nosalt klen=16 (=openssl)', enc_aes_cbc, dec_aes_cbc, 16, '', '\x04Us\x99\xbc\x0f\xaf\xdb\xeb\xd9\x91\xf1<\rr3\xf9\xa8\xe7i?F\xf7M#6\xa6\xc3XJ\x87\xda'),
('Self CBC/AES/salt klen=24 (=openssl)', enc_aes_cbc, dec_aes_cbc, 24, '\xa8\xf3:_e{k\xf8', '\x8e\xcdb-\xb8\xcaf\xe9\xdf\xd0"\xf3\x1f\x98\x01a\xc0\xdd+`\xe2\'\xcb\xf1#\'E\x86\xbeZc\x8d'),
('Self CBC/AES/nosalt klen=24 (=openssl)', enc_aes_cbc, dec_aes_cbc, 24, '', '\xc5\xbaF\xecm\xea\x00\xc4\x1bv\x01\xec\x99\x9c\x81\xe2\xd7\x1d\xe8>\x17\xf3\x89\x90\xbf[\x0b\x98Q\x00@G'),
('Self CBC/AES/salt klen=32 (=openssl)', enc_aes_cbc, dec_aes_cbc, 32, ':\x01W\xf4,\xbb\xd2G', '\xaf_\x9dD\x88\xc5)\xb1\x91\xfc\x91o\x1a\xa8\xd4\xb7B\xad_0\xd1\xe0\x0b,\xfa\x8cr\x11\xc7J\t\xc4'),
('Self CBC/AES/nosalt klen=32 (=openssl)', enc_aes_cbc, dec_aes_cbc, 32, '', "\xe1\xd7cS\xe5`\x1a'\xb8\xbce/\xfa\x06\xdc\x17\xef&\x91\x83:fG\x87\xb4\x9d\x0b5\xf7s\x9c\x02"),
('Self PCBC/AES/salt klen=16', enc_aes_pcbc, dec_aes_pcbc, 16, '\rp]\x1b\x90G\xd5_', '\xf8\x8c\x98\xd1\xe8_\xfbc\xa4\xc4p\x8b\x89&\xcc\x1a\xde&\x9aC\xfc&)W\x8aqDv\x99\xc3\x96\x8a'),
('Self PCBC/AES/nosalt klen=16', enc_aes_pcbc, dec_aes_pcbc, 16, '', '\x04Us\x99\xbc\x0f\xaf\xdb\xeb\xd9\x91\xf1<\rr3\xa1^U6+\xbd \xf8\xd2\x06\xac1f&K2'),
('Self PCBC/AES/salt klen=24', enc_aes_pcbc, dec_aes_pcbc, 24, '\xb6{\xd6\x87\xd2\x19e\xfa', '7\xf9\xe9\xddBI\xf2\xfb\x11\xd9\r\x8a\xb0w\xf1|\xcb\xed\xa15@\xe3\x8f\xde\xf4.\xf8\x94\x9d\xeeo\x9c'),
('Self PCBC/AES/nosalt klen=24', enc_aes_pcbc, dec_aes_pcbc, 24, '', '\xc5\xbaF\xecm\xea\x00\xc4\x1bv\x01\xec\x99\x9c\x81\xe2\x00\xaf\xe6r\xfcW\xd8\xde\xe3"\xb6\xdc\xe9\x93L\x8f'),
('Self PCBC/AES/salt klen=32', enc_aes_pcbc, dec_aes_pcbc, 32, 'i+\x94\xc2l=\xcb\xdc', 'X\x81\xa6\xd1|\xd5\x90\xb3\xeaX\xe9\xb6\xb4\x9d37DTe\x19\xad\xc0\xa0\xaf\xe2\x01\x17\x07\xdd9\xd5$'),
('Self PCBC/AES/nosalt klen=32', enc_aes_pcbc, dec_aes_pcbc, 32, '', "\xe1\xd7cS\xe5`\x1a'\xb8\xbce/\xfa\x06\xdc\x17\xcc\x0f\xcc\xf1\xba\xef\x87#\x85h}\xae\xb3\xbei "),
        ):
        c = enc_op(text, salt, passphrase, klen)
        t = dec_op(c, salt, passphrase, klen)
        print pad(rem, 50), 'enc:', ok(c == ref_cryp), 'dec:', ok(t == text)