ó õùPc@s\dZdZddlZddlZddlmZmZddlTd„Zdd!d„ƒYZ e d krXddlZddl Z ddl Z d Z d Zd Zdd „Zy,e j ejddddgƒ\ZZWn#e jk 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((s7..\python\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((s7..\python\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|} | |} || | !}t|ƒ|ksðt ‚|j t | |ƒƒ}t |ƒt |ƒA}| j|ƒ|j t || A|ƒƒ}| jt |ƒƒq­W| d} |j t | |ƒƒ}|t |ƒA}| j|ƒ|j t || A|ƒƒ}| jt |ƒƒ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_sizetlentbtdivmodtrangetAssertionErrortencryptRRtappendtreducetoperatortxor(R ttexttkeytK0tmcipherthcipherRtpadbyteststblocksthashestitstarttendtmit cipherblocktmtickithit mtick_stick((s7..\python\site-packages\Crypto\Protocol\AllOrNothing.pytdigest_s:        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 RRRRRRRRRR tintRtjoin(R R(R#R%RR)R*R/R0R"R$tpartsR.R-R&R!((s7..\python\site-packages\Crypto\Protocol\AllOrNothing.pytundigest±s*   "cCs#ddlm}|jƒj|ƒS(Niÿÿÿÿ(tRandom(tCryptoR:tnewtread(R R R:((s7..\python\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"((s7..\python\site-packages\Crypto\Protocol\AllOrNothing.pyt __newcipherìs N( t__name__t __module__t__doc__R>RtbchrRR2R9RR(((s7..\python\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((s7..\python\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 %3ds They match!s They differ!((s-hs--help(s-cs--cipher(s-ls--aslong((RBt __revision__RRHtCrypto.Util.numberRRtCrypto.Util.py3compatRRR@tgetopttbase64RGRRNR>RMRItoptstargsterrorRLtopttargt __import__tmoduleRR2Rt msgblockstzipRRR*tblkt encodestringtyR9R!(((s7..\python\site-packages\Crypto\Protocol\AllOrNothing.pyt+s\    º            (