ó
BwLc           @   s@  d  Z  d Z d d l Z d d l Z d d l m Z m Z d d d „  ƒ  YZ e d k r<d d l	 Z	 d d l
 Z
 d d l Z d Z d	 Z d
 Z d d „ Z y, e
 j
 e	 j d d d d g ƒ \ Z Z Wn# e
 j k
 rì Z e d e ƒ n Xe re d d ƒ n  xW e D]O \ Z Z e d  k r/e d
 ƒ q
e d! k rDe Z q
e d" k r
d Z q
q
We d e d d d g ƒ Z e e ƒ Z d GHe  GHd GHe j e  ƒ Z d GHxZ e d e e e ƒ ƒ e ƒ D]: \ Z  Z! d e  Ge rïe e! ƒ GHqÇe j" e! ƒ d  GHqÇWe e ƒ Z# e# j$ e ƒ Z% e% e  k r4d GHq<d GHn  d S(#   sã  This file implements all-or-nothing package transformations.

An all-or-nothing package transformation is one in which some text is
transformed into message blocks, such that all blocks must be obtained before
the reverse transformation can be applied.  Thus, if any blocks are corrupted
or lost, the original message cannot be reproduced.

An all-or-nothing package transformation is not encryption, although a block
cipher algorithm is used.  The encryption key is randomly generated and is
extractable from the message blocks.

This class implements the All-Or-Nothing package transformation algorithm
described in:

Ronald L. Rivest.  "All-Or-Nothing Encryption and The Package Transform"
http://theory.lcs.mit.edu/~rivest/fusion.pdf

s   $Id$iÿÿÿÿN(   t   bytes_to_longt   long_to_bytest   AllOrNothingc           B   sM   e  Z d  Z d d d „ Z e d ƒ Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   sV  Class implementing the All-or-Nothing package transform.

    Methods for subclassing:

        _inventkey(key_size):
            Returns a randomly generated key.  Subclasses can use this to
            implement better random key generating algorithms.  The default
            algorithm is probably not very cryptographically secure.

    c         C   sF   | |  _  | |  _ | |  _ | j |  _ |  j d k rB d |  _ n  d S(   sD  AllOrNothing(ciphermodule, mode=None, IV=None)

        ciphermodule is a module implementing the cipher algorithm to
        use.  It must provide the PEP272 interface.

        Note that the encryption key is randomly generated
        automatically when needed.  Optional arguments mode and IV are
        passed directly through to the ciphermodule.new() method; they
        are the feedback mode and initialization vector to use.  All
        three arguments must be the same for the object used to create
        the digest, and to undigest'ify the message blocks.
        i    i   N(   t   _AllOrNothing__ciphermodulet   _AllOrNothing__modet   _AllOrNothing__IVt   key_sizet   _AllOrNothing__key_size(   t   selft   ciphermodulet   modet   IV(    (    s@   /usr/lib/python2.7/dist-packages/Crypto/Protocol/AllOrNothing.pyt   __init__A   s    			ii   c         C   s  |  j  |  j ƒ } |  j |  j } |  j | ƒ } |  j | ƒ } |  j j } | t | ƒ | } | d | } t | ƒ | } g  }	 g  }
 xÂ t d | d ƒ D]­ } | d | } | | } | | | !} t | ƒ | k sá t ‚ | j	 t
 | | ƒ ƒ } t | ƒ t | ƒ A} |	 j | ƒ | j	 t
 | | A| ƒ ƒ } |
 j t | ƒ ƒ qž W| d } | j	 t
 | | ƒ ƒ } | t | ƒ A} |	 j | ƒ | j	 t
 | | A| ƒ ƒ } |
 j t | ƒ ƒ t | ƒ t t j |
 ƒ A} |	 j | ƒ g  |	 D] } t
 | |  j j ƒ ^ qíS(   s"  digest(text:string) : [string]

        Perform the All-or-Nothing package transform on the given
        string.  Output is a list of message blocks describing the
        transformed text, where each block is a string of bit length equal
        to the ciphermodule's block_size.
        t    i   (   t
   _inventkeyR   t   _AllOrNothing__K0digitt   _AllOrNothing__newcipherR   t
   block_sizet   lent   ranget   AssertionErrort   encryptR   R    t   appendt   reducet   operatort   xor(   R   t   textt   keyt   K0t   mciphert   hcipherR   t   padbytest   st   blockst   hashest   it   startt   endt   mit   cipherblockt   mtickit   hit   mtick_stick(    (    s@   /usr/lib/python2.7/dist-packages/Crypto/Protocol/AllOrNothing.pyt   digestX   s:    

c         C   s‚  t  | ƒ d k  r t d ‚ n  t t | ƒ } |  j |  j } |  j | ƒ } g  } xW t d t  | ƒ ƒ D]@ } | | d | A} | j t	 | ƒ ƒ } | j
 t | ƒ ƒ qh W| d t t j | ƒ A} |  j t	 | ƒ ƒ }	 |  j j }
 g  } xZ t d t  | ƒ ƒ D]C } |	 j t	 | |
 ƒ ƒ } | | d t | ƒ A} | j
 | ƒ qWt | d ƒ } t j t t	 | d  ƒ d ƒ } | |  S(   s5  undigest(blocks : [string]) : string

        Perform the reverse package transformation on a list of message
        blocks.  Note that the ciphermodule used for both transformations
        must be the same.  blocks is a list of strings of bit length
        equal to the ciphermodule's block_size.
        i   s   List must be at least length 2.i   iÿÿÿÿt    (   R   t
   ValueErrort   mapR    R   R   R   R   R   R   R   R   R   R   R   R   t   intt   stringt   join(   R   R!   R   R   R"   R#   R(   R)   R   R   R   t   partsR'   R&   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/Crypto/Protocol/AllOrNothing.pyt   undigestª   s*    c         C   s#   d d l  m } | j ƒ  j | ƒ S(   Niÿÿÿÿ(   t   Random(   t   CryptoR4   t   newt   read(   R   R   R4   (    (    s@   /usr/lib/python2.7/dist-packages/Crypto/Protocol/AllOrNothing.pyR   à   s    c         C   ss   |  j  d  k r. |  j d  k r. |  j j | ƒ S|  j d  k rS |  j j | |  j  ƒ S|  j j | |  j  |  j ƒ Sd  S(   N(   R   t   NoneR   R   R6   (   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/Crypto/Protocol/AllOrNothing.pyt   __newcipherå   s
    N(   t   __name__t
   __module__t   __doc__R8   R   t   chrR   R+   R3   R   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/Crypto/Protocol/AllOrNothing.pyR   5   s   
	R	6	t   __main__sD  Test module usage: %(program)s [-c cipher] [-l] [-h]

Where:
    --cipher module
    -c module
        Cipher module to use.  Default: %(ciphermodule)s

    --aslong
    -l
        Print the encoded message blocks as long integers instead of base64
        encoded strings

    --help
    -h
        Print this help message
t   AESi    c         C   s=   | r | GHn  t  i t j d d 6t d 6GHt j |  ƒ d  S(   Ni    t   programR	   (   t   usagemsgt   syst   argvR	   t   exit(   t   codet   msg(    (    s@   /usr/lib/python2.7/dist-packages/Crypto/Protocol/AllOrNothing.pyt   usage	  s
    
i   s   c:ls   cipher=t   aslongs   Too many argumentss   -hs   --helps   -cs   --ciphers   -ls   --aslongs   Crypto.Cipher.R6   s   Original text:
==========s
   ==========s   message blocks:s       %3ds   They match!s   They differ!(    (   s   -hs   --help(   s   -cs   --cipher(   s   -ls   --aslong(&   R<   t   __revision__R   R0   t   Crypto.Util.numberR    R   R   R:   RB   t   getoptt   base64RA   R	   RH   R8   RG   RC   t   optst   argst   errorRF   t   optt   argt
   __import__t   modulet   aR+   t	   msgblocksR.   R   R   R#   t   blkt   encodestringt   bR3   R   (    (    (    s@   /usr/lib/python2.7/dist-packages/Crypto/Protocol/AllOrNothing.pyt   <module>+   sX   º	+