Exemple #1
0
 def iterblocks(self,M,bitlen=None):
     # pad M into M' (see spec, p.12-13)
     if bitlen is None:
         bitlen=len(M)*8
     else:
         M = hex(Bits(M,bitlen)//Bits(1,1))
     # get BitPad flag:
     B = 1 if bitlen%8 else 0
     # pad M' into M'':
     l = len(M)
     lb = len(self.G)
     nb,rb = divmod(l,lb)
     lp=0
     if l==0 or rb>0:
         lp = lb-rb
         M += '\0'*lp
         nb += 1
     # init generator:
     P = StringIO.StringIO(M)
     Ts = self.Ts
     Ts.First = 1
     for b in range(nb-1):
         m = P.read(lb)
         Ts.Position += lb
         yield (pack(Ts),m)
         Ts.First=0
     # last M'' block:
     Ts.Final = 1
     Ts.BitPad = B
     m = P.read(lb)
     Ts.Position += lb-lp
     yield (pack(Ts),m)
Exemple #2
0
 def output(self,G):
     lq,lr = divmod(self.No,8)
     if lr!=0: lq += 1
     O = []
     n = l = 0
     T = Tweak(Type='out')
     ubi = UBI(Threefish,G,T)
     while l<lq:
         o = ubi(pack(Bits(n,64)))
         l += len(o)
         O.append(o)
         n += 1
     if l>lq: O[-1]=O[-1][:(lq-l)]
     return ''.join(O)
Exemple #3
0
 def __init__(self,Nb,No,
              schema="SHA3",version=1,Yl=0,Yf=0,Ym=0,
              key=None,
              prs=None,
              PK=None,
              kdf=None,
              nonce=None):
     # block length and output length:
     assert Nb in (256,512,1024)
     self.Nb = Nb/8
     self.No = No
     # Config string (256 bits)
     self.C = schema+pack(Bits(version,16)//Bits(0,16)//Bits(No,64))
     self.Yl = Yl
     self.Yf = Yf
     self.Ym = Ym
     self.C += chr(Yl)+chr(Yf)+chr(Ym)+'\0'*13
     self.key = key
     self.prs = prs
     self.PK  = PK
     self.kdf = kdf
     self.non = nonce