Ñò è~_>c@sPdZdZddkZddkZddklZlZddd„ƒYZedjoòddk Z ddk Z ddk Z dZ d Z d Zdd „Zy,e i e id d ddgƒ\ZZWn%e ij oZed eƒnXeoed dƒnx]eD]U\ZZed joed ƒqed!jo eZ qed"jo d ZqqWede dddgƒZeeƒZdGHeGHdGHeieƒZdGHx\edeeeƒƒeƒD]<\Z Z!de Geoee!ƒGHqÒe i"e!ƒd GHqÒWeeƒZ#e#i$eƒZ%e%ejo dGHqLdGHndS(#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: AllOrNothing.py,v 1.8 2003/02/28 15:23:20 akuchling Exp $iÿÿÿÿN(t bytes_to_longt long_to_bytest 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. cCsH||_||_||_|i|_|idjo d|_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_size(tselft ciphermoduletmodetIV((sB/usr/lib64/python2.6/site-packages/Crypto/Protocol/AllOrNothing.pyt__init__(s     iicCsÛ|i|iƒ}|i|i}|i|ƒ}|i|ƒ}|ii}|t|ƒ|}|d|}t|ƒ|}g} g} xªtd|dƒD]•} | d|} | |} || | !}|it | |ƒƒ}t |ƒt |ƒA}| i |ƒ|it || A|ƒƒ}| i t |ƒƒqžW| d} |it | |ƒƒ}|t |ƒA}| i |ƒ|it || A|ƒƒ}| i t |ƒƒt |ƒt t i| ƒA}| i |ƒtt | ƒ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 _inventkeyRt_AllOrNothing__K0digitt_AllOrNothing__newcipherRt block_sizetlentrangetencryptRRtappendtreducetoperatortxortmap(RttexttkeytK0tmcipherthcipherRtpadbyteststblocksthashestitstarttendtmit cipherblocktmtickithit mtick_stick((sB/usr/lib64/python2.6/site-packages/Crypto/Protocol/AllOrNothing.pytdigest?s:        c Cs„t|ƒdjo td‚ntt|ƒ}|i|i}|i|ƒ}g}xWtdt|ƒƒD]@}||d|A}|it |ƒƒ}|i t|ƒƒqjW|dt t i |ƒA}|it |ƒƒ} |ii} g} xZtdt|ƒƒD]C}| it || ƒƒ} ||dt| ƒA} | i | ƒqWt| dƒ}titt | d ƒ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 ValueErrorRRRRRRRRRRRRRRtinttstringtjoin(RR!RRR"R#R(R)RRRtpartsR'R&RR((sB/usr/lib64/python2.6/site-packages/Crypto/Protocol/AllOrNothing.pytundigest‘s.   cCs^ddk}ddkl}|i|dƒ}x||ijo|iƒq2W|i|ƒS(Niÿÿÿÿ(trandpooli(ttimet Crypto.UtilR3t RandomPooltentropyt add_eventt get_bytes(RRR4R3tpool((sB/usr/lib64/python2.6/site-packages/Crypto/Protocol/AllOrNothing.pyRÇs cCsx|idjo!|idjo|ii|ƒS|idjo|ii||iƒS|ii||i|iƒSdS(N(RtNoneRRtnew(RR((sB/usr/lib64/python2.6/site-packages/Crypto/Protocol/AllOrNothing.pyt __newcipherÔs  N( t__name__t __module__t__doc__R;R tchrRR+R2RR(((sB/usr/lib64/python2.6/site-packages/Crypto/Protocol/AllOrNothing.pyRs   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?|o |GHnthtidd6td6GHti|ƒdS(NitprogramR (tusagemsgtsystargvR texit(tcodetmsg((sB/usr/lib64/python2.6/site-packages/Crypto/Protocol/AllOrNothing.pytusageøs   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(&R@t __revision__RR/tCrypto.Util.numberRRRR>RFtgetopttbase64RER RLR;RKRGtoptstargsterrorRJtopttargt __import__tmoduletaR+t msgblocksRRRR#tblkt encodestringtbR2R(((sB/usr/lib64/python2.6/site-packages/Crypto/Protocol/AllOrNothing.pyts\