Current File : //usr/lib64/python2.7/site-packages/Crypto/Protocol/AllOrNothing.pyo
�
�/�Oc@s\dZdZddlZddlZddlmZmZddlTd�Zdd!d��YZ	e
d	krXddlZddlZddlZd
Z
dZdZdd
�Zy,ejejddddg�\ZZWn#ejk
r�Zede�nXeredd�nxWeD]O\ZZed"krBed�qed#krWeZqed$krdZqqWededddg�Ze	e�ZdGHeGHdGHejee��ZdGHxWee e!e��e�D]:\Z"Z#de"Geree#�GHq�ej$e#�d GHq�We	e�Z%e%j&e�Z'e'ee�krPdGHqXd GHndS(%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_bytes(t*cCs-d}y||7}Wntk
r(dSXdS(Nii(t	TypeError(txttest((sB/usr/lib64/python2.7/site-packages/Crypto/Protocol/AllOrNothing.pytisInt4s
tAllOrNothingcBsMeZdZddd�Zed�Zd�Zd�Zd�Z	d�Z
RS(sVClass 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.

    cCsV||_||_||_|j|_t|j�sF|jdkrRd|_ndS(sDAllOrNothing(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.
        iiN(t_AllOrNothing__ciphermodulet_AllOrNothing__modet_AllOrNothing__IVtkey_sizet_AllOrNothing__key_sizeR(tselftciphermoduletmodetIV((sB/usr/lib64/python2.7/site-packages/Crypto/Protocol/AllOrNothing.pyt__init__Hs			iicCs|j|j�}|j|j}|j|�}|j|�}|jj}|t|�|}|td�|}tt|�|�d}g}	g}
x�t	d|d�D]�}|d|}||}
|||
!}|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|�ttj|
�A}|	j
|�g|	D]}t||jj�^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 ii(t
_inventkeyRt_AllOrNothing__K0digitt_AllOrNothing__newcipherRt
block_sizetlentbtdivmodtrangetencryptRRtappendtreducetoperatortxor(R
ttexttkeytK0tmcipherthcipherRtpadbyteststblocksthashestitstarttendtmitcipherblocktmtickithitmtick_stick((sB/usr/lib64/python2.7/site-packages/Crypto/Protocol/AllOrNothing.pytdigest_s8






cCs�t|�dkrtd�ntt|�}|j|j}|j|�}|jj}g}xZt	dt|��D]C}||d|A}|j
t||��}|jt|��qtW|dt
tj|�A}	|jt|	|j��}
g}xZt	dt|��D]C}|
j
t||��}||dt|�A}
|j|
�qWt|d�}td�jtt|d ��}|| S(s5undigest(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.
        isList must be at least length 2.ii����t(Rt
ValueErrortmapRRRRRRRRRRRRRtintRtjoin(R
R'R"R$RR(R)R.R/R!R#tpartsR-R,R%R ((sB/usr/lib64/python2.7/site-packages/Crypto/Protocol/AllOrNothing.pytundigest�s*"cCs#ddlm}|j�j|�S(Ni����(tRandom(tCryptoR9tnewtread(R
RR9((sB/usr/lib64/python2.7/site-packages/Crypto/Protocol/AllOrNothing.pyR�scCss|jdkr.|jdkr.|jj|�S|jdkrS|jj||j�S|jj||j|j�SdS(N(R	tNoneR
RR;(R
R!((sB/usr/lib64/python2.7/site-packages/Crypto/Protocol/AllOrNothing.pyt__newcipher�s
N(t__name__t
__module__t__doc__R=RtbchrRR1R8RR(((sB/usr/lib64/python2.7/site-packages/Crypto/Protocol/AllOrNothing.pyR<s
	R	6	t__main__sDTest 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
tAESicCs=|r|GHntitjdd6td6GHtj|�dS(NitprogramR(tusagemsgtsystargvRtexit(tcodetmsg((sB/usr/lib64/python2.7/site-packages/Crypto/Protocol/AllOrNothing.pytusages

isc:lscipher=taslongsToo many argumentss-hs--helps-cs--ciphers-ls--aslongsCrypto.Cipher.R;sOriginal text:
==========s
==========smessage blocks:s    %3dsThey match!sThey differ!((s-hs--help(s-cs--cipher(s-ls--aslong((RAt__revision__RRGtCrypto.Util.numberRRtCrypto.Util.py3compatRRR?tgetopttbase64RFRRMR=RLRHtoptstargsterrorRKtopttargt
__import__tmoduleRR1Rt	msgblockstzipRRR)tblktencodestringtyR8R (((sB/usr/lib64/python2.7/site-packages/Crypto/Protocol/AllOrNothing.pyt<module>+s\
	�
	
(