3 from sys import argv, exit
4 from struct import unpack, pack
5 from itertools import izip
10 ###--------------------------------------------------------------------------
17 yield [things[i] for i in ii]
19 if j == k - 1: lim = n
32 try: return POLYMAP[nbits]
34 base = C.GF(0).setbit(nbits).setbit(0)
35 for k in xrange(1, nbits, 2):
36 for cc in combs(range(1, nbits), k):
37 p = base + sum(C.GF(0).setbit(c) for c in cc)
38 if p.irreduciblep(): POLYMAP[nbits] = p; return p
39 raise ValueError, nbits
42 ## No fancy way to do this: I'd need a much cleverer factoring algorithm
43 ## than I have in my pockets.
44 if nbits == 64: cc = [64, 4, 3, 1, 0]
45 elif nbits == 96: cc = [96, 10, 9, 6, 0]
46 elif nbits == 128: cc = [128, 7, 2, 1, 0]
47 elif nbits == 192: cc = [192, 15, 11, 5, 0]
48 elif nbits == 256: cc = [256, 10, 5, 2, 0]
49 else: raise ValueError, 'no field for %d bits' % nbits
51 for c in cc: p = p.setbit(c)
55 return C.ByteString.zero(n)
57 def mul_blk_gf(m, x, p): return ((C.GF.loadb(m)*x)%p).storeb((p.nbits + 6)/8)
62 except StopIteration: raise ValueError, 'empty iter'
67 except StopIteration: lastp = True
71 if len(x): return hex(x)
76 if isinstance(ksz, C.KeySZSet): kk = ksz.set
77 elif isinstance(ksz, C.KeySZRange): kk = range(ksz.min, ksz.max, ksz.mod)
78 elif isinstance(ksz, C.KeySZAny): kk = range(64); sel = [0]
79 kk = list(kk); kk = kk[:]
81 while n and len(sel) < 4:
84 kk[i], kk[n] = kk[n], kk[i]
93 return C.ByteString(m)
97 if r: m += '\x80' + Z(r - 1)
98 return C.ByteString(m)
102 while (i&1) == 0: i >>= 1; j += 1
106 v, i, n = [], 0, len(x)
108 v.append(C.ByteString(x[i:i + w]))
110 return v, C.ByteString(x[i:])
116 if len(tl) == w: v.append(tl); tl = EMPTY
119 def dummygen(bc): return []
123 ###--------------------------------------------------------------------------
126 class RC6Cipher (type):
127 def __new__(cls, w, r):
128 name = 'rc6-%d/%d' % (w, r)
129 me = type(name, (RC6Base,), {})
134 me.keysz = C.KeySZRange(me.blksz, 1, 255, 1)
138 return w.bit_length() - 1
141 m0, m1 = C.MP(0).setbit(w - n) - 1, C.MP(0).setbit(n) - 1
142 return ((x&m0) << n) | (x >> (w - n))&m1
145 m0, m1 = C.MP(0).setbit(n) - 1, C.MP(0).setbit(w - n) - 1
146 return ((x&m0) << (w - n)) | (x >> n)&m1
148 class RC6Base (object):
151 P400 = C.MP(0xb7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfef324e7738926cfbe5f4bf8d8d8c31d763da06)
152 Q400 = C.MP(0x9e3779b97f4a7c15f39cc0605cedc8341082276bf3a27251f86c6a11d0c18e952767f0b153d27b7f0347045b5bf1827f0188)
156 ## Build the magic numbers.
157 P = me.P400 >> (400 - me.w)
159 Q = me.Q400 >> (400 - me.w)
161 M = C.MP(0).setbit(me.w) - 1
163 ## Convert the key into words.
165 c = (len(k) + wb - 1)/wb
166 kb, ktl = blocks(k, me.w/8)
167 L = map(C.MP.loadl, kb + [ktl])
170 ## Build the subkey table.
173 S = [(P + i*Q)&M for i in xrange(n)]
175 ##for j in xrange(c):
176 ## print 'L[%3d] = %s' % (j, hex(L[j]).upper()[2:].rjust(2*wb, '0'))
177 ##for i in xrange(n):
178 ## print 'S[%3d] = %s' % (i, hex(S[i]).upper()[2:].rjust(2*wb, '0'))
183 for s in xrange(3*max(c, n)):
184 A = S[i] = rol(me.w, S[i] + A + B, 3)
185 B = L[j] = rol(me.w, L[j] + A + B, (A + B)%(1 << me.d))
186 ##print 'S[%3d] = %s' % (i, hex(S[i]).upper()[2:].rjust(2*wb, '0'))
187 ##print 'L[%3d] = %s' % (j, hex(L[j]).upper()[2:].rjust(2*wb, '0'))
195 M = C.MP(0).setbit(me.w) - 1
196 a, b, c, d = map(C.MP.loadl, blocks0(x, me.blksz/4)[0])
199 ##print 'B = %s' % (hex(b).upper()[2:].rjust(me.w/4, '0'))
200 ##print 'D = %s' % (hex(d).upper()[2:].rjust(me.w/4, '0'))
201 for i in xrange(2, 2*me.r + 2, 2):
202 t = rol(me.w, 2*b*b + b, me.d)
203 u = rol(me.w, 2*d*d + d, me.d)
204 a = (rol(me.w, a ^ t, u%(1 << me.d)) + me.s[i + 0])&M
205 c = (rol(me.w, c ^ u, t%(1 << me.d)) + me.s[i + 1])&M
206 ##print 'A = %s' % (hex(a).upper()[2:].rjust(me.w/4, '0'))
207 ##print 'C = %s' % (hex(c).upper()[2:].rjust(me.w/4, '0'))
208 a, b, c, d = b, c, d, a
209 a = (a + me.s[2*me.r + 2])&M
210 c = (c + me.s[2*me.r + 3])&M
211 ##print 'A = %s' % (hex(a).upper()[2:].rjust(me.w/4, '0'))
212 ##print 'C = %s' % (hex(c).upper()[2:].rjust(me.w/4, '0'))
213 return C.ByteString(a.storel(me.blksz/4) + b.storel(me.blksz/4) +
214 c.storel(me.blksz/4) + d.storel(me.blksz/4))
217 M = C.MP(0).setbit(me.w) - 1
218 a, b, c, d = map(C.MP.loadl, blocks0(x, me.blksz/4))
219 c = (c - me.s[2*me.r + 3])&M
220 a = (a - me.s[2*me.r + 2])&M
221 for i in xrange(2*me.r + 1, 1, -2):
222 a, b, c, d = d, a, b, c
223 u = rol(me.w, 2*d*d + d, me.d)
224 t = rol(me.w, 2*b*b + b, me.d)
225 c = ror(me.w, (c - me.s[i + 1])&M, t%(1 << me.d)) ^ u
226 a = ror(me.w, (a - me.s[i + 0])&M, u%(1 << me.d)) ^ t
227 a = (a + s[2*me.r + 2])&M
228 c = (c + s[2*me.r + 3])&M
229 return C.ByteString(a.storel(me.blksz/4) + b.storel(me.blksz/4) +
230 c.storel(me.blksz/4) + d.storel(me.blksz/4))
232 for (w, r) in [(8, 16), (16, 16), (24, 16), (32, 16),
233 (32, 20), (48, 16), (64, 16), (96, 16), (128, 16),
234 (192, 16), (256, 16), (400, 16)]:
235 CUSTOM['rc6-%d/%d' % (w, r)] = RC6Cipher(w, r)
237 ###--------------------------------------------------------------------------
241 blksz = E.__class__.blksz
245 m0 = mul_blk_gf(L, 2, p)
246 m1 = mul_blk_gf(m0, 2, p)
250 blksz = E.__class__.blksz
251 m0, m1 = omac_masks(E)
252 print 'L = %s' % hex(E.encrypt(Z(blksz)))
253 print 'm0 = %s' % hex(m0)
254 print 'm1 = %s' % hex(m1)
256 print 'v%d = %s' % (t, hex(E.encrypt(C.MP(t).storeb(blksz))))
257 print 'z%d = %s' % (t, hex(omac(E, t, '')))
260 blksz = E.__class__.blksz
261 m0, m1 = omac_masks(E)
263 if t is not None: m = C.MP(t).storeb(blksz) + m
264 v, tl = blocks(m, blksz)
265 for x in v: a = E.encrypt(a ^ x)
268 a = E.encrypt(a ^ tl ^ m0)
270 pad = pad10star(tl, blksz)
271 a = E.encrypt(a ^ pad ^ m1)
275 if VERBOSE: dump_omac(E)
276 return omac(E, None, m),
283 ###--------------------------------------------------------------------------
287 blksz = E.__class__.blksz
290 while y.size < len(m):
291 y.put(E.encrypt(c.storeb(blksz)))
293 return C.ByteString(m) ^ C.ByteString(y)[:len(m)]
295 ###--------------------------------------------------------------------------
308 return C.ByteString(y)
313 u, v = C.GF.loadl(gcm_mangle(x)), C.GF.loadl(gcm_mangle(y))
315 return gcm_mangle(z.storel(w))
320 u = C.GF.loadl(gcm_mangle(x))
322 return gcm_mangle(z.storel(w))
324 def gcm_ctr(E, m, c0):
327 c, = unpack('>L', c0[-4:])
328 while y.size < len(m):
330 y.put(E.encrypt(pre + pack('>L', c)))
331 return C.ByteString(m) ^ C.ByteString(y)[:len(m)]
333 def g(what, x, m, a0 = None):
335 if a0 is None: a = Z(n)
338 for b in blocks0(m, n)[0]:
339 a = gcm_mul(a ^ b, x)
340 if VERBOSE: print '%s[%d] = %s -> %s' % (what, i, hex(b), hex(a))
345 return C.ByteString(x + Z(-len(x)%w))
347 def gcm_lens(w, a, b):
350 return C.ByteString(C.MP(a).storeb(n) + C.MP(b).storeb(n))
352 def ghash(whata, whatb, x, a, b):
354 ha = g(whata, x, gcm_pad(w, a))
355 hb = g(whatb, x, gcm_pad(w, b))
357 hc = gcm_mul(ha, gcm_pow(x, (len(b) + w - 1)/w)) ^ hb
358 if VERBOSE: print '%s || %s -> %s' % (whata, whatb, hex(hc))
361 return g(whatb, x, gcm_lens(w, 8*len(a), 8*len(b)), hc)
363 def gcmenc(E, n, h, m, tsz = None):
364 w = E.__class__.blksz
366 if VERBOSE: print 'x = %s' % hex(x)
367 if len(n) + 4 == w: c0 = C.ByteString(n + pack('>L', 1))
368 else: c0 = ghash('?', 'n', x, EMPTY, n)
369 if VERBOSE: print 'c0 = %s' % hex(c0)
370 y = gcm_ctr(E, m, c0)
371 t = ghash('h', 'y', x, h, y) ^ E.encrypt(c0)
374 def gcmdec(E, n, h, y, t):
375 w = E.__class__.blksz
377 if VERBOSE: print 'x = %s' % hex(x)
378 if len(n) + 4 == w: c0 = C.ByteString(n + pack('>L', 1))
379 else: c0 = ghash('?', 'n', x, EMPTY, n)
380 if VERBOSE: print 'c0 = %s' % hex(c0)
381 m = gcm_ctr(E, y, c0)
382 tt = ghash('h', 'y', x, h, y) ^ E.encrypt(c0)
383 if t == tt: return m,
387 return [(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1),
388 (bc.blksz, 3*bc.blksz, 3*bc.blksz),
389 (bc.blksz - 4, bc.blksz + 3, 3*bc.blksz + 9),
390 (bc.blksz - 1, 3*bc.blksz - 5, 3*bc.blksz + 5)]
392 ###--------------------------------------------------------------------------
395 def stbe(n, w): return C.MP(n).storeb(w)
397 def ccm_fmthdr(blksz, n, hsz, msz, tsz):
400 q = blksz - len(n) - 1
405 b.putu8(f).put(n).put(stbe(msz, q))
407 q = blksz - len(n) - 1
410 f |= (tsz - 2)/2 << 3
412 b.putu8(f).put(n).put(stbe(msz, q))
414 q = blksz - len(n) - 2
419 b.putu8(f0).putu8(f1).put(n).put(stbe(msz, q))
421 if VERBOSE: print 'hdr = %s' % hex(b)
424 def ccm_fmtctr(blksz, n, i = 0):
426 if blksz == 8 or blksz == 16:
427 q = blksz - len(n) - 1
428 b.putu8(q - 1).put(n).put(stbe(i, q))
430 q = blksz - len(n) - 2
431 b.putu8(0).putu8(q).put(n).put(stbe(i, q))
433 if VERBOSE: print 'ctr = %s' % hex(b)
436 def ccmaad(b, h, blksz):
439 elif hsz < 0xfffe: b.putu16(hsz)
440 elif hsz <= 0xffffffff: b.putu16(0xfffe).putu32(hsz)
441 else: b.putu16(0xffff).putu64(hsz)
442 b.put(h); b.zero((-b.size)%blksz)
444 def ccmenc(E, n, h, m, tsz = None):
445 blksz = E.__class__.blksz
446 if tsz is None: tsz = blksz
448 b.put(ccm_fmthdr(blksz, n, len(h), len(m), tsz))
450 b.put(m); b.zero((-b.size)%blksz)
453 v, _ = blocks0(b, blksz)
458 print 'b[%d] = %s' % (i, hex(x))
459 print 'a[%d] = %s' % (i + 1, hex(a))
461 y = ctr(E, a + m, ccm_fmtctr(blksz, n))
462 return C.ByteString(y[blksz:]), C.ByteString(y[0:tsz])
464 def ccmdec(E, n, h, y, t):
465 blksz = E.__class__.blksz
468 b.put(ccm_fmthdr(blksz, n, len(h), len(y), tsz))
470 mm = ctr(E, t + Z(blksz - tsz) + y, ccm_fmtctr(blksz, n))
471 u, m = C.ByteString(mm[0:tsz]), C.ByteString(mm[blksz:])
472 b.put(m); b.zero((-b.size)%blksz)
475 v, _ = blocks0(b, blksz)
480 print 'b[%d] = %s' % (i, hex(x))
481 print 'a[%d] = %s' % (i + 1, hex(a))
483 if u == a[:tsz]: return m,
488 return [(bsz - 5, 0, 0, 4), (bsz - 5, 1, 0, 4), (bsz - 5, 0, 1, 4),
489 (bsz/2 + 1, 3*bc.blksz, 3*bc.blksz),
490 (bsz/2 + 1, 3*bc.blksz - 5, 3*bc.blksz + 5)]
492 ###--------------------------------------------------------------------------
495 def eaxenc(E, n, h, m, tsz = None):
497 print 'k = %s' % hex(k)
498 print 'n = %s' % hex(n)
499 print 'h = %s' % hex(h)
500 print 'm = %s' % hex(m)
502 if tsz is None: tsz = E.__class__.blksz
508 print 'c0 = %s' % hex(c0)
509 print 'ht = %s' % hex(ht)
510 print 'yt = %s' % hex(yt)
511 return y, C.ByteString((c0 ^ ht ^ yt)[:tsz])
513 def eaxdec(E, n, h, y, t):
515 print 'k = %s' % hex(k)
516 print 'n = %s' % hex(n)
517 print 'h = %s' % hex(h)
518 print 'y = %s' % hex(y)
519 print 't = %s' % hex(t)
526 print 'c0 = %s' % hex(c0)
527 print 'ht = %s' % hex(ht)
528 print 'yt = %s' % hex(yt)
529 if t == (c0 ^ ht ^ yt)[:len(t)]: return m,
533 return [(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1),
534 (bc.blksz, 3*bc.blksz, 3*bc.blksz),
535 (bc.blksz - 1, 3*bc.blksz - 5, 3*bc.blksz + 5)]
537 ###--------------------------------------------------------------------------
541 blksz = E.__class__.blksz
543 x = C.GF(2); xinv = p.modinv(x)
546 Lxinv = mul_blk_gf(L, xinv, p)
548 for i in xrange(1, len(Lgamma)):
549 Lgamma[i] = mul_blk_gf(Lgamma[i - 1], x, p)
553 Lgamma, Lxinv = ocb_masks(E)
554 print 'L x^-1 = %s' % hex(Lxinv)
555 for i, lg in enumerate(Lgamma[:16]):
556 print 'L x^%d = %s' % (i, hex(lg))
559 blksz = E.__class__.blksz
560 Lgamma, Lxinv = ocb_masks(E)
563 v, tl = blocks(m, blksz)
568 a ^= E.encrypt(x ^ o)
570 print 'Z[%d]: %d -> %s' % (i, b, hex(o))
571 print 'A[%d]: %s' % (i, hex(a))
572 if len(tl) == blksz: a ^= tl ^ Lxinv
573 else: a ^= pad10star(tl, blksz)
577 blksz = E.__class__.blksz
579 L = E.encrypt(Z(blksz))
580 o = mul_blk_gf(L, 10, p)
582 v, tl = blocks(m, blksz)
584 a ^= E.encrypt(x ^ o)
585 o = mul_blk_gf(o, 2, p)
586 if len(tl) == blksz: a ^= tl ^ mul_blk_gf(o, 3, p)
587 else: a ^= pad10star(tl, blksz) ^ mul_blk_gf(o, 5, p)
591 Lgamma, _ = ocb_masks(E)
594 return Lstar, Ldollar, Lgamma[2:]
597 Lstar, Ldollar, Lgamma = ocb3_masks(E)
598 print 'L_* = %s' % hex(Lstar)
599 print 'L_$ = %s' % hex(Ldollar)
600 for i, lg in enumerate(Lgamma[:16]):
601 print 'L x^%d = %s' % (i, hex(lg))
604 ## Note that `PMAC3' is /not/ a secure MAC. It depends on other parts of
605 ## OCB3 to prevent a rather easy linear-algebra attack.
606 blksz = E.__class__.blksz
607 Lstar, Ldollar, Lgamma = ocb3_masks(E)
610 v, tl = blocks0(m, blksz)
615 a ^= E.encrypt(x ^ o)
617 print 'Z[%d]: %d -> %s' % (i, b, hex(o))
618 print 'A[%d]: %s' % (i, hex(a))
621 a ^= E.encrypt(pad10star(tl, blksz) ^ o)
623 print 'Z[%d]: * -> %s' % (i, hex(o))
624 print 'A[%d]: %s' % (i, hex(a))
628 if VERBOSE: dump_ocb(E)
636 ###--------------------------------------------------------------------------
639 def ocb1enc(E, n, h, m, tsz = None):
640 ## This is OCB1.PMAC1 from Rogaway's `Authenticated-Encryption with
642 blksz = E.__class__.blksz
643 if VERBOSE: dump_ocb(E)
644 Lgamma, Lxinv = ocb_masks(E)
645 if tsz is None: tsz = blksz
647 o = E.encrypt(n ^ Lgamma[0])
648 if VERBOSE: print 'R = %s' % hex(o)
651 v, tl = blocks(m, blksz)
658 print 'Z[%d]: %d -> %s' % (i, b, hex(o))
659 print 'A[%d]: %s' % (i, hex(a))
660 y.put(E.encrypt(x ^ o) ^ o)
666 print 'Z[%d]: %d -> %s' % (i, b, hex(o))
667 print 'LEN = %s' % hex(C.MP(8*n).storeb(blksz))
668 yfinal = E.encrypt(C.MP(8*n).storeb(blksz) ^ Lxinv ^ o)
669 cfinal = tl ^ yfinal[:n]
670 a ^= o ^ (tl + yfinal[n:])
673 if h: t ^= pmac1(E, h)
674 return C.ByteString(y), C.ByteString(t[:tsz])
676 def ocb1dec(E, n, h, y, t):
677 ## This is OCB1.PMAC1 from Rogaway's `Authenticated-Encryption with
679 blksz = E.__class__.blksz
680 if VERBOSE: dump_ocb(E)
681 Lgamma, Lxinv = ocb_masks(E)
683 o = E.encrypt(n ^ Lgamma[0])
684 if VERBOSE: print 'R = %s' % hex(o)
687 v, tl = blocks(y, blksz)
693 print 'Z[%d]: %d -> %s' % (i, b, hex(o))
694 print 'A[%d]: %s' % (i, hex(a))
695 u = E.decrypt(x ^ o) ^ o
703 print 'Z[%d]: %d -> %s' % (i, b, hex(o))
704 print 'LEN = %s' % hex(C.MP(8*n).storeb(blksz))
705 yfinal = E.encrypt(C.MP(8*n).storeb(blksz) ^ Lxinv ^ o)
706 mfinal = tl ^ yfinal[:n]
707 a ^= o ^ (mfinal + yfinal[n:])
710 if h: u ^= pmac1(E, h)
711 if t == u[:len(t)]: return C.ByteString(m),
714 def ocb2enc(E, n, h, m, tsz = None):
715 ## For OCB2, it's important for security that n = log_x (x + 1) is large in
716 ## the field representations of GF(2^w) used -- in fact, we need more, that
717 ## i n (mod 2^w - 1) is large for i in {4, -3, -2, -1, 1, 2, 3, 4}. The
718 ## original paper lists the values for 64 and 128, but we support other
719 ## block sizes, so here's the result of the (rather large, in some cases)
722 ## Block size log_x (x + 1)
724 ## 64 9686038906114705801
725 ## 96 63214690573408919568138788065
726 ## 128 338793687469689340204974836150077311399
727 ## 192 161110085006042185925119981866940491651092686475226538785
728 ## 256 22928580326165511958494515843249267194111962539778797914076675796261938307298
730 blksz = E.__class__.blksz
731 if tsz is None: tsz = blksz
734 o = mul_blk_gf(L, 2, p)
736 v, tl = blocks(m, blksz)
740 y.put(E.encrypt(x ^ o) ^ o)
741 o = mul_blk_gf(o, 2, p)
743 yfinal = E.encrypt(C.MP(8*n).storeb(blksz) ^ o)
744 cfinal = tl ^ yfinal[:n]
745 a ^= (tl + yfinal[n:]) ^ mul_blk_gf(o, 3, p)
748 if h: t ^= pmac2(E, h)
749 return C.ByteString(y), C.ByteString(t[:tsz])
751 def ocb2dec(E, n, h, y, t):
752 blksz = E.__class__.blksz
755 o = mul_blk_gf(L, 2, p)
757 v, tl = blocks(y, blksz)
760 u = E.encrypt(x ^ o) ^ o
763 o = mul_blk_gf(o, 2, p)
765 yfinal = E.encrypt(C.MP(8*n).storeb(blksz) ^ o)
766 mfinal = tl ^ yfinal[:n]
767 a ^= (mfinal + yfinal[n:]) ^ mul_blk_gf(o, 3, p)
770 if h: u ^= pmac2(E, h)
771 if t == u[:len(t)]: return C.ByteString(m),
774 OCB3_STRETCH = { 4: ( 4, 17),
786 def ocb3nonce(E, n, tsz):
788 ## Figure out how much we need to glue onto the nonce. This ends up being
789 ## [t mod w]_v || 0^p || 1 || N, where w is the block size in bits, t is
790 ## the tag length in bits, v = floor(log_2(w - 1)) + 1, and p = w - l(N) -
791 ## v - 1. But this is an annoying way to think about it because of the
792 ## byte misalignment. Instead, think of it as a byte-aligned prefix
793 ## encoding the tag and an `is the nonce full-length' flag, followed by
794 ## optional padding, and then the nonce:
796 ## F || N if l(N) = w - f
797 ## F || 0^p || 1 || N otherwise
799 ## where F is [t mod w]_v || 0^{f-v-1} || b; f = floor(log_2(w - 1)) + 2;
800 ## b is 1 if l(N) = w - f, or 0 otherwise; and p = w - f - l(N) - 1.
801 blksz = E.__class__.blksz
802 tszbits = min(C.MP(8*blksz - 1).nbits, 8)
804 f = 8*(tsz%blksz) << + 8*fwd - tszbits
806 ## Form the augmented nonce.
808 nsz, nwd = len(n), blksz - fwd
809 if nsz == nwd: f |= 1
810 nb.put(C.MP(f).storeb(fwd))
811 if nsz < nwd: nb.zero(nwd - nsz - 1).putu8(1)
813 nn = C.ByteString(nb)
814 if VERBOSE: print 'aug-nonce = %s' % hex(nn)
816 ## Calculate the initial offset.
817 split, shift = OCB3_STRETCH[blksz]
818 t2pw = C.MP(0).setbit(8*blksz) - 1
819 lomask = (C.MP(0).setbit(split) - 1)
821 top, bottom = nn&himask.storeb2c(blksz), C.MP.loadb(nn)&lomask
822 ktop = C.MP.loadb(E.encrypt(top))
823 stretch = (ktop << 8*blksz) | (ktop ^ (ktop << shift)&t2pw)
824 o = (stretch >> 8*blksz - bottom).storeb(blksz)
826 print 'stretch = %s' % hex(stretch.storeb(2*blksz))
827 print 'Z[0] = %s' % hex(o)
831 def ocb3enc(E, n, h, m, tsz = None):
832 blksz = E.__class__.blksz
833 if tsz is None: tsz = blksz
834 Lstar, Ldollar, Lgamma = ocb3_masks(E)
835 if VERBOSE: dump_ocb3(E)
838 o = ocb3nonce(E, n, tsz)
839 a = C.ByteString.zero(blksz)
841 ## Split the message into blocks.
844 v, tl = blocks0(m, blksz)
851 print 'Z[%d]: %d -> %s' % (i, b, hex(o))
852 print 'A[%d]: %s' % (i, hex(a))
853 y.put(E.encrypt(x ^ o) ^ o)
858 a ^= pad10star(tl, blksz)
860 print 'Z[%d]: * -> %s' % (i, hex(o))
861 print 'A[%d]: %s' % (i, hex(a))
864 t = E.encrypt(a ^ o) ^ pmac3(E, h)
865 return C.ByteString(y), C.ByteString(t[:tsz])
867 def ocb3dec(E, n, h, y, t):
868 blksz = E.__class__.blksz
870 Lstar, Ldollar, Lgamma = ocb3_masks(E)
871 if VERBOSE: dump_ocb3(E)
874 o = ocb3nonce(E, n, tsz)
875 a = C.ByteString.zero(blksz)
877 ## Split the message into blocks.
880 v, tl = blocks0(y, blksz)
886 print 'Z[%d]: %d -> %s' % (i, b, hex(o))
887 print 'A[%d]: %s' % (i, hex(a))
888 u = E.encrypt(x ^ o) ^ o
896 print 'Z[%d]: * -> %s' % (i, hex(o))
897 print 'A[%d]: %s' % (i, hex(a))
900 a ^= pad10star(u, blksz)
902 u = E.encrypt(a ^ o) ^ pmac3(E, h)
903 if t == u[:tsz]: return C.ByteString(m),
908 return [(w, 0, 0), (w, 1, 0), (w, 0, 1),
912 (w, 3*w - 5, 3*w + 5)]
916 return [(w - 2, 0, 0), (w - 2, 1, 0), (w - 2, 0, 1),
920 (w - 2, 3*w - 5, 3*w + 5)]
922 def ocb3_mct(bc, ksz, tsz):
923 k = C.ByteString(C.WriteBuffer().zero(ksz - 4).putu32(8*tsz))
927 cbuf = C.WriteBuffer()
928 for i in xrange(128):
929 s = C.ByteString.zero(i)
930 y, t = ocb3enc(E, n.storeb(nw), s, s, tsz); n += 1; cbuf.put(y).put(t)
931 y, t = ocb3enc(E, n.storeb(nw), EMPTY, s, tsz); n += 1; cbuf.put(y).put(t)
932 y, t = ocb3enc(E, n.storeb(nw), s, EMPTY, tsz); n += 1; cbuf.put(y).put(t)
933 _, t = ocb3enc(E, n.storeb(nw), C.ByteString(cbuf), EMPTY, tsz)
937 k = C.bytes('000102030405060708090a0b0c0d0e0f')
939 tsz = min(E.blksz, 32)
941 cbuf = C.WriteBuffer()
942 for i in xrange(128):
943 sbuf = C.WriteBuffer()
944 for j in xrange(i): sbuf.putu8(j)
945 s = C.ByteString(sbuf)
946 y, t = ocb3enc(E, n.storeb(2), s, s, tsz); n += 1; cbuf.put(y).put(t)
947 y, t = ocb3enc(E, n.storeb(2), EMPTY, s, tsz); n += 1; cbuf.put(y).put(t)
948 y, t = ocb3enc(E, n.storeb(2), s, EMPTY, tsz); n += 1; cbuf.put(y).put(t)
949 _, t = ocb3enc(E, n.storeb(2), C.ByteString(cbuf), EMPTY, tsz)
952 ###--------------------------------------------------------------------------
955 class struct (object):
956 def __init__(me, **kw):
957 me.__dict__.update(kw)
959 binarg = struct(mk = R.block, parse = C.bytes, show = safehex)
960 intarg = struct(mk = lambda x: x, parse = int, show = None)
962 MODEMAP = { 'eax-enc': (eaxgen, 3*[binarg] + [intarg], eaxenc),
963 'eax-dec': (dummygen, 4*[binarg], eaxdec),
964 'ccm-enc': (ccmgen, 3*[binarg] + [intarg], ccmenc),
965 'ccm-dec': (dummygen, 4*[binarg], ccmdec),
966 'cmac': (cmacgen, [binarg], cmac),
967 'gcm-enc': (gcmgen, 3*[binarg] + [intarg], gcmenc),
968 'gcm-dec': (dummygen, 4*[binarg], gcmdec),
969 'ocb1-enc': (ocbgen, 3*[binarg] + [intarg], ocb1enc),
970 'ocb1-dec': (dummygen, 4*[binarg], ocb1dec),
971 'ocb2-enc': (ocbgen, 3*[binarg] + [intarg], ocb2enc),
972 'ocb2-dec': (dummygen, 4*[binarg], ocb2dec),
973 'ocb3-enc': (ocb3gen, 3*[binarg] + [intarg], ocb3enc),
974 'ocb3-dec': (dummygen, 4*[binarg], ocb3dec),
975 'pmac1': (pmacgen, [binarg], pmac1_pub) }
979 for d in CUSTOM, C.gcprps:
981 except KeyError: pass
983 if bc is None: raise KeyError, argv[2]
984 if len(argv) == 5 and mode == 'ocb3-mct':
986 ksz, tsz = int(argv[3]), int(argv[4])
987 ocb3_mct(bc, ksz, tsz)
989 if len(argv) == 3 and mode == 'ocb3-mct2':
995 gen, argty, func = MODEMAP[mode]
996 if mode.endswith('-enc'): mode = mode[:-4]
997 print '%s-%s {' % (bc.name, mode)
998 for ksz in keylens(bc.keysz):
999 for argvals in gen(bc):
1001 args = [t.mk(a) for t, a in izip(argty, argvals)]
1002 rets = func(bc(k), *args)
1003 print ' %s' % safehex(k)
1004 for t, a in izip(argty, args):
1005 if t.show: print ' %s' % t.show(a)
1006 for r, lastp in with_lastp(rets):
1007 print ' %s%s' % (safehex(r), lastp and ';' or '')
1011 k = C.bytes(argv[3])
1012 gen, argty, func = MODEMAP[mode]
1013 args = [t.parse(a) for t, a in izip(argty, argv[4:])]
1014 rets = func(bc(k), *args)
1016 if r is None: print "X"
1019 ###----- That's all, folks --------------------------------------------------