ó
T:´Nc           @   s-  d  Z  d Z d d l Z d d l Z d d l m Z m Z d d l Te j d k  r_ d „  Z	 n e	 Z	 d d d	 „  ƒ  YZ
 e
 d
 „ Z d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d „  Z d „  Z d S(   s&   Self-testing for PyCrypto hash moduless   $Id$iÿÿÿÿN(   t   a2b_hext   b2a_hex(   t   *i  c          K   s
   |  j  ƒ  S(   N(   t   copy(   t   kwargs(    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   dict&   s    t
   _NoDefaultc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR   +   s    c         C   s@   y |  | } Wn$ t  k
 r4 | t k r0 ‚  n  | SX|  | =| S(   sA   Get an item from a dictionary, and remove it from the dictionary.(   t   KeyErrorR   (   t   dt   kt   defaultt   retval(    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   _extract,   s    t   CipherSelfTestc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s5  t  j j |  ƒ | |  _ | j ƒ  } t | d ƒ |  _ t t | d ƒ ƒ |  _ t t | d ƒ ƒ |  _	 t t | d ƒ ƒ |  _
 t | d d  ƒ |  _ t | d d  ƒ } t | ƒ |  _ | d  k	 rt |  j d | ƒ |  _ t | d d  ƒ |  _ |  j d  k	 r(t |  j ƒ |  _ q(n d  |  _ d  |  _ | |  _ d  S(	   Nt   descriptiont   keyt	   plaintextt
   ciphertextt   module_namet   modet   MODE_t   iv(   t   unittestt   TestCaset   __init__t   moduleR   R   R   t   bR   R   R   t   NoneR   t   strt	   mode_namet   getattrR   R   t   extra_params(   t   selfR   t   paramsR   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR   :   s$    	 		c         C   s   |  j  S(   N(   R   (   R"   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   shortDescriptionT   s    c         C   s¾  |  j  j ƒ  } t |  j d ƒ r0|  j |  j j k r0d d l m } t | d | j	 ƒ } t | d i  ƒ j ƒ  } | j
 d ƒ rŸ t t | d ƒ ƒ | d <n  | j
 d ƒ rË t t | d ƒ ƒ | d <n  | j
 d ƒ sd	 |  j j t | j d d
 ƒ ƒ t | j d d
 ƒ ƒ | d <n  | |   | d <n  |  j d  k r[|  j j	 t |  j ƒ |  S|  j d  k rŒ|  j j	 t |  j ƒ |  j |  S|  j j	 t |  j ƒ |  j t |  j ƒ |  Sd  S(   Nt   MODE_CTRiÿÿÿÿ(   t   Countert	   ctr_classt
   ctr_paramst   prefixt   suffixt   nbitsi   t    t   counter(   R!   R   t   hasattrR   R   R%   t   Crypto.UtilR&   R   t   newt   has_keyR    R   t
   block_sizet   lent   getR   R   R   (   R"   R#   R&   R'   R(   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   _newW   s"    '  C"c         C   sÚ   t  |  j ƒ } t  |  j ƒ } t |  j ƒ  j | ƒ ƒ } t |  j ƒ  j | ƒ ƒ } t |  j ƒ  j | ƒ ƒ } t |  j ƒ  j | ƒ ƒ } |  j |  j | ƒ |  j |  j | ƒ |  j |  j | ƒ |  j |  j | ƒ d  S(   N(   R    R   R   R   R5   t   encryptt   decryptt   assertEqual(   R"   R   R   t   ct1t   pt1t   ct2t   pt2(    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   runTesto   s    (   R   R   R   R$   R5   R=   (    (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR   8   s   			t   CipherStreamingSelfTestc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s:   |  j  } |  j d  k	 r/ | d |  j f 7} n  d | f S(   Ns    in %s modes%   %s should behave like a stream cipher(   R   R   R   R   (   R"   t   desc(    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR$      s    	c         C   s*  t  |  j ƒ } t  |  j ƒ } g  } |  j ƒ  } xA t d t | ƒ d ƒ D]' } | j | j | | | d !ƒ ƒ qI Wt t	 d ƒ j
 | ƒ ƒ } |  j |  j | ƒ g  } |  j ƒ  } xA t d t | ƒ d ƒ D]' } | j | j | | | d !ƒ ƒ qÍ Wt t	 d ƒ j
 | ƒ ƒ } |  j |  j | ƒ d  S(   Ni    i   R,   (   R    R   R   R5   t   rangeR3   t   appendR6   R   R   t   joinR8   (   R"   R   R   t   ct3t   ciphert   it   pt3(    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR=   …   s    %%(   R   R   R$   R=   (    (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR>   }   s   	t   CTRSegfaultTestc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   sE   t  j j |  ƒ | |  _ t | d ƒ |  _ | j d d  ƒ |  _ d  S(   NR   R   (	   R   R   R   R   R   R   R4   R   R   (   R"   R   R#   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR   ž   s    	c         C   s   d |  j  |  j  f S(   NsN   Regression test: %s.new(key, %s.MODE_CTR) should raise TypeError, not segfault(   R   (   R"   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR$   ¤   s    c         C   s/   |  j  t |  j j t |  j ƒ |  j j ƒ d  S(   N(   t   assertRaisest	   TypeErrorR   R0   R    R   R%   (   R"   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR=   §   s    (   R   R   R   R$   R=   (    (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyRG   œ   s   		t   CTRWraparoundTestc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   sE   t  j j |  ƒ | |  _ t | d ƒ |  _ | j d d  ƒ |  _ d  S(   NR   R   (	   R   R   R   R   R   R   R4   R   R   (   R"   R   R#   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR   ¬   s    	c         C   s   d |  j  f S(   Ns]   Regression test: %s with MODE_CTR should raise OverflowError on wraparound when shortcut used(   R   (   R"   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR$   ²   s    c      
   C   sÖ   d d l  m } x¿ d D]· } x® d D]¦ } | j d |  j j d d d |  j j d d | d	 | ƒ} |  j j t |  j ƒ |  j j d
 | ƒ} t d ƒ |  j j } | j	 | ƒ |  j
 t | j	 | ƒ q$ Wq Wd  S(   Niÿÿÿÿ(   R&   i    i   i   t   initial_valuel    t   little_endiant   disable_shortcutR-   t    (   i    i   (   i    i   (   R/   R&   R0   R   R2   R    R   R%   R   R6   RH   t   OverflowError(   R"   R&   RM   RL   t   ctrRD   t   block(    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR=   µ   s    =*(   R   R   R   R$   R=   (    (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyRJ   ª   s   		t   CFBSegmentSizeTestc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s=   t  j j |  ƒ | |  _ t | d ƒ |  _ | d |  _ d  S(   NR   R   (   R   R   R   R   R   R   R   (   R"   R   R#   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR   Â   s    	c         C   s   |  j  S(   N(   R   (   R"   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR$   È   s    c         C   sw   xH t  d d ƒ D]7 } |  j t |  j j t |  j ƒ |  j j d | ƒq W|  j j t |  j ƒ |  j j d d ƒd S(   sn   Regression test: m.new(key, m.MODE_CFB, segment_size=N) should require segment_size to be a multiple of 8 bitsi   i   t   segment_sizeN(   R@   RH   t
   ValueErrorR   R0   R    R   t   MODE_CFB(   R"   RE   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR=   Ë   s    5(   R   R   R   R$   R=   (    (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyRR   À   s   		t   RoundtripTestc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   d d l  m } t j j |  ƒ | |  _ | j | j ƒ |  _ t	 | d ƒ |  _
 d t	 | d ƒ |  _ | j d d  ƒ |  _ d  S(   Niÿÿÿÿ(   t   RandomR   id   R   R   (   t   CryptoRW   R   R   R   R   t   get_random_bytesR2   R   R   R   R   R4   R   R   (   R"   R   R#   RW   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR   Ò   s    	c         C   s   d |  j  f S(   Ns8   %s .decrypt() output of .encrypt() should not be garbled(   R   (   R"   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR$   Û   s    c         C   s¾   x· |  j  j |  j  j |  j  j |  j  j |  j  j f D]‚ } |  j  j t |  j ƒ | |  j	 ƒ } |  j  j t |  j ƒ | |  j	 ƒ } | j
 |  j ƒ } | j | ƒ } |  j |  j | ƒ q4 Wd  S(   N(   R   t   MODE_ECBt   MODE_CBCRU   t   MODE_PGPt   MODE_OFBR0   R    R   R   R6   R   R7   R8   (   R"   R   t   encryption_ciphert   decryption_cipherR   t   decrypted_plaintext(    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR=   Þ   s    :$$(   R   R   R   R$   R=   (    (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyRV   Ñ   s   			c      	   C   s  g  } d } xt  t | ƒ ƒ D]ô} | | } i d d 6} t | ƒ d k rl | \ | d <| d <| d <n— t | ƒ d k r£ | \ | d <| d <| d <| d	 <n` t | ƒ d
 k rê | \ | d <| d <| d <| d	 <} | j | ƒ n t d t | ƒ f ƒ ‚ | j ƒ  }	 t |	 d ƒ }
 t |	 d ƒ } t |	 d ƒ } t |	 d	 d  ƒ } |	 j d d ƒ } | d k rt |	 d d ƒ n  | d  k	 r”| } n9 | d k rº|	 rºd | |
 f } n d | |
 |	 f } d | | d | f } | | d	 <| | d <| sA| t |  | ƒ t	 |  | ƒ t
 |  | ƒ t |  | ƒ g 7} d } n  | j t |  | ƒ ƒ | d k r|| j t |  | ƒ ƒ n  | d k r | j d ƒ r | j ƒ  } | d	 c d 7<| j d i  ƒ j ƒ  } | | d <| d j d ƒ súd | d d <n  | j t |  | ƒ ƒ q q W| S(   Ni    t   ECBR   i   R   R   R   i   R   i   s   Unsupported tuple size %ds
   p=%s, k=%ss   p=%s, k=%s, %rs
   %s #%d: %si   R   t   CTRR'   s    (shortcut disabled)R(   RM   (   R@   R3   t   updatet   AssertionErrorR   R   R   R4   RG   RJ   RR   RV   RA   R   R>   R1   (   R   R   t	   test_datat   testst   extra_tests_addedRE   t   rowR#   R!   t   p2t   p_keyt   p_plaintextt   p_ciphertextt   p_descriptiont   p_modeR   t   namet   params2t   ctr_params2(    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   make_block_testsæ   s`    
%%	

	
c         C   sá  g  } xÔt  t | ƒ ƒ D]À} | | } i  } t | ƒ d k r_ | \ | d <| d <| d <n— t | ƒ d k r– | \ | d <| d <| d <| d <n` t | ƒ d k rÝ | \ | d <| d <| d <| d <} | j | ƒ n t d t | ƒ f ƒ ‚ | j ƒ  } t | d ƒ }	 t | d ƒ }
 t | d ƒ } t | d d  ƒ } | d  k	 rV| } n, | sod	 |
 |	 f } n d
 |
 |	 | f } d | | d | f } | | d <| | d <| j t |  | ƒ ƒ | j t	 |  | ƒ ƒ q W| S(   Ni   R   R   R   i   R   i   s   Unsupported tuple size %ds
   p=%s, k=%ss   p=%s, k=%s, %rs
   %s #%d: %si   R   (
   R@   R3   Rc   Rd   R   R   R   RA   R   R>   (   R   R   Re   Rf   RE   Rh   R#   R!   Ri   Rj   Rk   Rl   Rm   R   Ro   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   make_stream_tests(  s8    
%%	

(    (   t   __doc__t   __revision__t   sysR   t   binasciiR    R   t   Crypto.Util.py3compatt
   hexversionR   R   R   R   R   R>   RG   RJ   RR   RV   Rr   Rs   (    (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   <module>   s$   
E	B