ó
BwLc           @   s  d  Z  d Z d d l Z d d l Z d d l m Z m Z e j d k  rU d „  Z n
 e	 d 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 „  Z d „  Z d S(   s&   Self-testing for PyCrypto hash moduless   $Id$iÿÿÿÿN(   t   a2b_hext   b2a_hexi  c          K   s
   |  j  ƒ  S(   N(   t   copy(   t   kwargs(    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   dict%   s    R   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   sÿ   t  j j |  ƒ | |  _ | j ƒ  } t | d ƒ |  _ t | d ƒ |  _ t | d ƒ |  _ t | d ƒ |  _	 t | d d  ƒ |  _ t | d d  ƒ } t | ƒ |  _ | d  k	 rà t |  j d | ƒ |  _ t | d d  ƒ |  _ n d  |  _ d  |  _ | |  _ d  S(	   Nt   descriptiont   keyt	   plaintextt
   ciphertextt   module_namet   modet   MODE_t   iv(   t   unittestt   TestCaset   __init__t   moduleR   R   R   R   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   9   s     			c         C   s   |  j  S(   N(   R   (   R    (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   shortDescriptionR   s    c         C   s²  |  j  j ƒ  } t |  j d ƒ r$|  j |  j j k r$d d l m } t | d | j	 ƒ } t | d i  ƒ j ƒ  } | j
 d ƒ r™ t | d ƒ | d <n  | j
 d ƒ r¿ 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 rO|  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    t
   block_sizet   lent   getR   R   R   (   R    R!   R$   R%   R&   (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   _newU   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   R3   t   encryptt   decryptt   assertEqual(   R    R   R   t   ct1t   pt1t   ct2t   pt2(    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   runTestm   s    (   R   R   R   R"   R3   R;   (    (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyR   7   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 d j	 | ƒ ƒ } |  j
 |  j | ƒ g  } |  j ƒ  } xA t d t | ƒ d ƒ D]' } | j | j | | | d !ƒ ƒ qÇ Wt d j	 | ƒ ƒ } |  j
 |  j | ƒ d  S(   Ni    i   R*   (   R    R   R   R3   t   rangeR1   t   appendR4   R   t   joinR6   (   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   s?   t  j j |  ƒ | |  _ | d |  _ | j d d  ƒ |  _ d  S(   NR   R   (   R   R   R   R   R   R2   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   R.   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.pyRE   ™   s   		t   CTRWraparoundTestc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s?   t  j j |  ƒ | |  _ | d |  _ | j d d  ƒ |  _ d  S(   NR   R   (   R   R   R   R   R   R2   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
 | ƒ} 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$   R.   R   R0   R    R   R#   R4   RF   t   OverflowError(   R    R$   RK   RJ   t   ctrRB   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.pyRH   §   s   		t   CFBSegmentSizeTestc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s7   t  j j |  ƒ | |  _ | d |  _ | d |  _ d  S(   NR   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>   RF   t
   ValueErrorR   R.   R    R   t   MODE_CFB(   R    RC   (    (    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.pyRP   ½   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 <| s5| t |  | ƒ t	 |  | ƒ t
 |  | ƒ g 7} d } n  | j t |  | ƒ ƒ | d k rp| 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&   RK   (   R>   R1   t   updatet   AssertionErrorR   R   R   R2   RE   RH   RP   R?   R   R<   R/   (   R   R   t	   test_datat   testst   extra_tests_addedRC   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>   R1   RV   RW   R   R   R   R?   R   R<   (   R   R   RX   RY   RC   R[   R!   R   R\   R]   R^   R_   R`   R   Rb   (    (    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
   hexversionR   t   __builtins__R   R   R   R   R<   RE   RH   RP   Re   Rf   (    (    (    sA   /usr/lib/python2.7/dist-packages/Crypto/SelfTest/Cipher/common.pyt   <module>   s    
D	A