ó
‚¾^Yc           @   sf   d  Z  d d l Z d d l m Z m Z m Z m Z d d l m Z d „  Z	 d „  Z
 d d g Z d S(	   s_  Large file support

.. deprecated:: 3.4

    The VARBLOCK format is NOT recommended for general use, has been deprecated since
    Python-RSA 3.4, and will be removed in a future release. It's vulnerable to a
    number of attacks:

    1. decrypt/encrypt_bigfile() does not implement `Authenticated encryption`_ nor
       uses MACs to verify messages before decrypting public key encrypted messages.

    2. decrypt/encrypt_bigfile() does not use hybrid encryption (it uses plain RSA)
       and has no method for chaining, so block reordering is possible.

    See `issue #19 on Github`_ for more information.

.. _Authenticated encryption: https://en.wikipedia.org/wiki/Authenticated_encryption
.. _issue #19 on Github: https://github.com/sybrenstuvel/python-rsa/issues/13


This module contains functions to:

    - break a file into smaller blocks, and encrypt them, and store the
      encrypted blocks in another file.

    - take such an encrypted files, decrypt its blocks, and reconstruct the
      original file.

The encrypted file format is as follows, where || denotes byte concatenation:

    FILE := VERSION || BLOCK || BLOCK ...

    BLOCK := LENGTH || DATA

    LENGTH := varint-encoded length of the subsequent data. Varint comes from
    Google Protobuf, and encodes an integer into a variable number of bytes.
    Each byte uses the 7 lowest bits to encode the value. The highest bit set
    to 1 indicates the next byte is also part of the varint. The last byte will
    have this bit set to 0.

This file format is called the VARBLOCK format, in line with the varint format
used to denote the block sizes.

iÿÿÿÿN(   t   keyt   commont   pkcs1t   varblock(   t   bytec         C   sÇ   t  j d t d d ƒt | t j ƒ s; t d | ƒ ‚ n  t j | j	 ƒ d } | d } | j
 t t j ƒ ƒ xO t j |  | ƒ D]; } t j | | ƒ } t j | t | ƒ ƒ | j
 | ƒ q„ Wd S(   s&  Encrypts a file, writing it to 'outfile' in VARBLOCK format.

    .. deprecated:: 3.4
        This function was deprecated in Python-RSA version 3.4 due to security issues
        in the VARBLOCK format. See the documentation_ for more information.

    .. _documentation: https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files

    :param infile: file-like object to read the cleartext from
    :param outfile: file-like object to write the crypto in VARBLOCK format to
    :param pub_key: :py:class:`rsa.PublicKey` to encrypt with

    sß   The 'rsa.bigfile.encrypt_bigfile' function was deprecated in Python-RSA version 3.4 due to security issues in the VARBLOCK format. See https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files for more information.t
   stackleveli   s   Public key required, but got %ri   i   N(   t   warningst   warnt   DeprecationWarningt
   isinstanceR    t	   PublicKeyt	   TypeErrorR   t   bit_sizet   nt   writeR   R   t   VARBLOCK_VERSIONt   yield_fixedblocksR   t   encryptt   write_varintt   len(   t   infilet   outfilet   pub_keyt	   key_bytest	   blocksizet   blockt   crypto(    (    s(   /tmp/pip-build-kpPAdC/rsa/rsa/bigfile.pyt   encrypt_bigfileD   s    	
c         C   sx   t  j d t d d ƒt | t j ƒ s; t d | ƒ ‚ n  x6 t j |  ƒ D]% } t	 j
 | | ƒ } | j | ƒ qK Wd S(   s(  Decrypts an encrypted VARBLOCK file, writing it to 'outfile'

    .. deprecated:: 3.4
        This function was deprecated in Python-RSA version 3.4 due to security issues
        in the VARBLOCK format. See the documentation_ for more information.

    .. _documentation: https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files

    :param infile: file-like object to read the crypto in VARBLOCK format from
    :param outfile: file-like object to write the cleartext to
    :param priv_key: :py:class:`rsa.PrivateKey` to decrypt with

    sß   The 'rsa.bigfile.decrypt_bigfile' function was deprecated in Python-RSA version 3.4 due to security issues in the VARBLOCK format. See https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files for more information.R   i   s    Private key required, but got %rN(   R   R   R   R	   R    t
   PrivateKeyR   R   t   yield_varblocksR   t   decryptR   (   R   R   t   priv_keyR   t	   cleartext(    (    s(   /tmp/pip-build-kpPAdC/rsa/rsa/bigfile.pyt   decrypt_bigfilej   s    	R   R!   (   t   __doc__R   t   rsaR    R   R   R   t   rsa._compatR   R   R!   t   __all__(    (    (    s(   /tmp/pip-build-kpPAdC/rsa/rsa/bigfile.pyt   <module><   s   "	&	