B c. @sdZdZddlZddlZddlmZmZddlTddlm Z ddZ Gd d d Z e d kr ddlZddl Z ddlZd Zd ZdZd#ddZy$e ejdddddg\ZZWn.e jk rZzedeWddZ[XYnXereddx@eD]8\ZZedkredqedkreZqedkrdZqWededddgZe eZedeeedeeeZ edx^e!e"e#e$e e D]D\Z%Z&ede%ddereee&nee'e&dd qWe eZ(e()e Z*e*eekred!ned"dS)$aThis 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 z$Id$N) bytes_to_long long_to_bytes)*)reducecCs*d}y ||7}Wntk r$dSXdS)Nr) TypeError)xZtestr O/opt/alt/python37/lib64/python3.7/site-packages/Crypto/Protocol/AllOrNothing.pyisInt5s  r c@sBeZdZdZdddZedZddZdd Zd d Z d d Z dS) AllOrNothingaVClass 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. NcCs8||_||_||_|j|_t|jr.|jdkr4d|_dS)aDAllOrNothing(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. rN)_AllOrNothing__ciphermodule_AllOrNothing__mode_AllOrNothing__IVkey_size_AllOrNothing__key_sizer )self ciphermodulemodeZIVr r r __init__Is zAllOrNothing.__init__icsj}jj}|}|}jj}|t||}|td|}tt||d}g} g} xt d|dD]} | d|} | |} || | }t||kst | t | |}t |t |A}| || t || A|}| t |q~W| d} | t | |}|t |A}| || t || A|}| t |t |ttj| A}| |fdd| DS)a"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.  rrcsg|]}t|jjqSr )rr block_size).0i)rr r sz'AllOrNothing.digest..) _inventkeyr_AllOrNothing__K0digit_AllOrNothing__newcipherrrlenbdivmodrangeAssertionErrorencryptrrappendroperatorxor)rtextkeyK0mcipherhcipherrpadbytessblockshashesrstartendmi cipherblockmtickihiZ mtick_stickr )rr digest`s:          zAllOrNothing.digestcCs0t|dkrtdttt|}|j|j}||}|jj }g}xDt dt|D]2}||d|A}| t ||}| t|qTW|dttj|A} |t | |j} g} xDt dt|D]2}| t ||} ||dt| A} | | qWt| d}tdtt | dd}|d| S)a5undigest(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. zList must be at least length 2.rN)r ValueErrorlistmaprrrrrrr#r%rr&rr'r(intr!join)rr0r+r-rr1rr6r7r*r,partsr5r4r.r)r r r undigests*    zAllOrNothing.undigestcCsddlm}||S)Nr)Random)ZCryptorCnewread)rrrCr r r rs zAllOrNothing._inventkeycCsR|jdkr |jdkr |j|S|jdkr:|j||jS|j||j|jSdS)N)rrrrD)rr*r r r Z __newciphers   zAllOrNothing.__newcipher)NN) __name__ __module__ __qualname____doc__rZbchrrr8rBrrr r r r r =s  R6r __main__aDTest 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 ZAEScCs2|r t|tttjdtdt|dS)Nr)Zprogramr)printusagemsgsysargvrexit)codemsgr r r usages   rRrzc:lzcipher=aslongzToo many arguments)z-hz--help)z-cz--cipher)z-lz--aslongzCrypto.Cipher.rDzOriginal text: ==========z ==========zmessage blocks:z %3dr)r3r:z They match!z They differ!)N)+rIZ __revision__r'rMZCrypto.Util.numberrrZCrypto.Util.py3compat functoolsrr r rFZgetoptbase64rLrrSrRrNZoptsargserrorrQoptarg __import__modulerrKr8r!Z msgblockszipr=r#r rZblkZ encodestringyrBr)r r r r +s` ;