B c@sdZddlZejddkr0ejddkr0ddlTddlTddlmZddlZddlZddl m Z ddl m Z dd l m Z Gd d d eZd d ZGdddeZdS)z$Id$N)*)b2a_hex)ClockRewindWarning)SHAd256)FortunaGeneratorc@s>eZdZdZejZddZddZddZdd Z d d Z d S) FortunaPoolagFortuna pool type This object acts like a hash object, with the following differences: - It keeps a count (the .length attribute) of the number of bytes that have been added to the pool - It supports a .reset() method for in-place reinitialization - The method to add bytes to the pool is .append(), not .update(). cCs |dS)N)reset)selfr [/opt/alt/python37/lib64/python3.7/site-packages/Crypto/Random/Fortuna/FortunaAccumulator.py__init__6szFortunaPool.__init__cCs"|j||jt|7_dS)N)_hupdatelengthlen)r datar r r append9s zFortunaPool.appendcCs |jS)N)rdigest)r r r r r=szFortunaPool.digestcCs.tjddkrt|St|SdS)Nrr)sys version_inforrdecode)r r r r hexdigest@s zFortunaPool.hexdigestcCst|_d|_dS)Nr)rnewrr)r r r r r Fs zFortunaPool.resetN) __name__ __module__ __qualname____doc__rZ digest_sizerrrrr r r r r r )s r cCsP|dks tg}d}x6tdD]*}||@dkr:||nP|d>dB}qW|S)a?Return a list of pools indexes (in range(32)) that are to be included during reseed number r. According to _Practical Cryptography_, chapter 10.5.2 "Pools": "Pool P_i is included if 2**i is a divisor of r. Thus P_0 is used every reseed, P_1 every other reseed, P_2 every fourth reseed, etc." rr )AssertionErrorranger)rZretvalmaskir r r which_poolsJs   r%c@s>eZdZdZdZddZddZddZdd d Zd d Z d S)FortunaAccumulator@g?cCsFd|_t|_d|_ddtdD|_|jd|jdk sBtdS)NrcSsg|] }tqSr )r ).0r$r r r ~sz/FortunaAccumulator.__init__..rr) reseed_countrZ AESGenerator generator last_reseedr!poolsr )r r r r rus  zFortunaAccumulator.__init__cCs d|_dS)N)r,)r r r r _forget_last_reseedsz&FortunaAccumulator._forget_last_reseedcCspt}|jdk r.|j|kr.tdtd|_|jdj|jkrd|jdksZ||j|jkrd| ||j |S)Nz-Clock rewind detected. Resetting last_reseed.r) timer,warningswarnrr-r min_pool_sizereseed_interval_reseedr+Zpseudo_random_data)r bytes current_timer r r random_datas   zFortunaAccumulator.random_dataNcCs||dkrt}g}|jd7_||_x4t|jD]&}||j||j|q4Wtd |}|j |dS)Nr) r/r*r,r%rr-rr bjoinr+Zreseed)r r6Zseedr$r r r r4szFortunaAccumulator._reseedcCsdt|krdksntd|kr2dks8ntd|krLdksRnt|j|t||j|tt||j||dS)Nrrr)rr r-rZbchr)r Z source_numberZ pool_numberrr r r add_random_events z#FortunaAccumulator.add_random_event)N) rrrr2r3rr.r7r4r=r r r r r&_s    r&)Z __revision__rrZCrypto.Util.py21compatZCrypto.Util.py3compatZbinasciirr/r0ZCrypto.pct_warningsrr8rrobjectr r%r&r r r r s    !