+### Counter mode.
+
+def ctr(E, m, c0):
+ blksz = E.__class__.blksz
+ y = C.WriteBuffer()
+ c = C.MP.loadb(c0)
+ while y.size < len(m):
+ y.put(E.encrypt(c.storeb(blksz)))
+ c += 1
+ return C.ByteString(m) ^ C.ByteString(y)[:len(m)]
+
+###--------------------------------------------------------------------------
+### EAX.
+
+def eaxenc(E, n, h, m, tsz = None):
+ if VERBOSE:
+ print 'k = %s' % hex(k)
+ print 'n = %s' % hex(n)
+ print 'h = %s' % hex(h)
+ print 'm = %s' % hex(m)
+ dump_omac(E)
+ if tsz is None: tsz = E.__class__.blksz
+ c0 = omac(E, 0, n)
+ y = ctr(E, m, c0)
+ ht = omac(E, 1, h)
+ yt = omac(E, 2, y)
+ if VERBOSE:
+ print 'c0 = %s' % hex(c0)
+ print 'ht = %s' % hex(ht)
+ print 'yt = %s' % hex(yt)
+ return y, C.ByteString((c0 ^ ht ^ yt)[:tsz])
+
+def eaxdec(E, n, h, y, t):
+ if VERBOSE:
+ print 'k = %s' % hex(k)
+ print 'n = %s' % hex(n)
+ print 'h = %s' % hex(h)
+ print 'y = %s' % hex(y)
+ print 't = %s' % hex(t)
+ dump_omac(E)
+ c0 = omac(E, 0, n)
+ m = ctr(E, y, c0)
+ ht = omac(E, 1, h)
+ yt = omac(E, 2, y)
+ if VERBOSE:
+ print 'c0 = %s' % hex(c0)
+ print 'ht = %s' % hex(ht)
+ print 'yt = %s' % hex(yt)
+ if t == (c0 ^ ht ^ yt)[:len(t)]: return m,
+ else: return None,
+
+def eaxgen(bc):
+ return [(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1),
+ (bc.blksz, 3*bc.blksz, 3*bc.blksz),
+ (bc.blksz - 1, 3*bc.blksz - 5, 3*bc.blksz + 5)]
+
+###--------------------------------------------------------------------------