[paramiko] HPN-SSH

Dwayne C. Litzenberger dlitz at dlitz.net
Mon Feb 18 22:25:15 PST 2008


On Sun, Feb 17, 2008 at 09:43:19PM -0800, Robey Pointer wrote:
>Multithreading AES-CTR is pretty clever. If pycrypto supported CTR,  

If?

     from Crypto.Cipher import AES
     
     class Counter(str):
         def __init__(self, initial_ctr):
             if not isinstance(initial_ctr, str):
                 raise TypeError("nonce must be str")
             self.c = int(initial_ctr.encode('hex'), 16)
         def __call__(self):
             # This might be slow, but it works as a demonstration
             ctr = ("%032x" % (self.c,)).decode('hex')
             self.c += 1
             return ctr
     
     # NIST SP 800-38A, 2001 ed. - F.5.1 test vector (page 62)
     # http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
     key = "2b7e151628aed2a6abf7158809cf4f3c".decode('hex')
     initial_ctr = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff".decode('hex')
     blocks = [
         '6bc1bee22e409f96e93d7e117393172a'.decode('hex'),
         'ae2d8a571e03ac9c9eb76fac45af8e51'.decode('hex'),
         '30c81c46a35ce411e5fbc1191a0a52ef'.decode('hex'),
     ]
     
     cipher = AES.new(key, AES.MODE_CTR, "\0" * 16, Counter(initial_ctr))
     for b in blocks:
         print cipher.encrypt(b).encode('hex')

Outputs (as it should):

     874d6191b620e3261bef6864990db6ce
     9806f66b7970fdff8617187bb9fffdff
     5ae4df3edbd5d35e5b4f09020db03eab

-- 
Dwayne C. Litzenberger <dlitz at dlitz.net>



More information about the paramiko mailing list