From ed8cc62dda4ba6c81ce63cbc88289eb9a9acf3b5 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sat, 28 Jan 2006 19:12:38 +0000 Subject: [PATCH] __init__.py: Spruce up a lot. * Divide into sections and add commentary. * Add static methods for BBSPriv class (ooops). --- catacomb/__init__.py | 147 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 92 insertions(+), 55 deletions(-) diff --git a/catacomb/__init__.py b/catacomb/__init__.py index 2b391eb..8b1d0ef 100644 --- a/catacomb/__init__.py +++ b/catacomb/__init__.py @@ -25,13 +25,20 @@ # along with Catacomb/Python; if not, write to the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +#----- Imports -------------------------------------------------------------- + import _base import types as _types from binascii import hexlify as _hexify, unhexlify as _unhexify from sys import argv as _argv +#----- Basic stuff ---------------------------------------------------------- + +## For the benefit of the default keyreporter, we need the program na,e. _base._ego(_argv[0]) +## Initialize the module. Drag in the static methods of the various +## classes; create names for the various known crypto algorithms. def _init(): d = globals() b = _base.__dict__; @@ -40,7 +47,8 @@ def _init(): d[i] = b[i]; for i in ['MP', 'GF', 'Field', 'ECPt', 'ECPtCurve', 'ECCurve', 'ECInfo', - 'DHInfo', 'BinDHInfo', 'RSAPriv', 'PrimeFilter', 'RabinMiller', + 'DHInfo', 'BinDHInfo', 'RSAPriv', 'BBSPriv', + 'PrimeFilter', 'RabinMiller', 'Group', 'GE', 'KeyData']: c = d[i] @@ -49,12 +57,16 @@ def _init(): for j in b: if j[:plen] == pre: setattr(c, j[plen:], classmethod(b[j])) - for i in [gcciphers, gchashes, gcmacs]: - for j in i: - c = i[j] + for i in [gcciphers, gchashes, gcmacs, gcprps]: + for c in i.itervalues(): d[c.name.replace('-', '_')] = c + for c in gccrands.itervalues(): + d[c.name.replace('-', '_') + 'rand'] = c _init() +## A handy function for our work: add the methods of a named class to an +## existing class. This is how we write the Python-implemented parts of our +## mostly-C types. def _augment(c, cc): for i in cc.__dict__: a = cc.__dict__[i] @@ -64,6 +76,17 @@ def _augment(c, cc): continue setattr(c, i, a) +## Parsing functions tend to return the object parsed and the remainder of +## the input. This checks that the remainder is input and, if so, returns +## just the object. +def _checkend(r): + x, rest = r + if rest != '': + raise SyntaxError, 'junk at end of string' + return x + +#----- Bytestrings ---------------------------------------------------------- + class _tmp: def fromhex(x): return ByteString(_unhexify(x)) @@ -75,6 +98,8 @@ class _tmp: _augment(ByteString, _tmp) bytes = ByteString.fromhex +#----- Multiprecision integers and binary polynomials ----------------------- + class _tmp: def negp(x): return x < 0 def posp(x): return x > 0 @@ -87,21 +112,24 @@ class _tmp: def factorial(x): 'factorial(X) -> X!' if x < 0: raise ValueError, 'factorial argument must be > 0' - return MP.product(xrange(1, x + 1)) + return MPMul.product(xrange(1, x + 1)) factorial = staticmethod(factorial) _augment(MP, _tmp) -def _checkend(r): - x, rest = r - if rest != '': - raise SyntaxError, 'junk at end of string' - return x - class _tmp: - def reduce(x): return GReduce(x) + def reduce(x): return GFReduce(x) _augment(GF, _tmp) class _tmp: + def product(*arg): + 'product(ITERABLE) or product(I, ...) -> PRODUCT' + return MPMul(*arg).done() + product = staticmethod(product) +_augment(MPMul, _tmp) + +#----- Abstract fields ------------------------------------------------------ + +class _tmp: def fromstring(str): return _checkend(Field.parse(str)) fromstring = staticmethod(fromstring) _augment(Field, _tmp) @@ -121,31 +149,7 @@ class _tmp: def __repr__(me): return '%s(%s)' % (repr(me.field), repr(me.value)) _augment(FE, _tmp) -class _groupmap (object): - def __init__(me, map, nth): - me.map = map - me.nth = nth - me.i = [None] * (max(map.values()) + 1) - def __repr__(me): - return '{%s}' % ', '.join(['%r: %r' % (k, me[k]) for k in me]) - def __contains__(me, k): - return k in me.map - def __getitem__(me, k): - i = me.map[k] - if me.i[i] is None: - me.i[i] = me.nth(i) - return me.i[i] - def __setitem__(me, k, v): - raise TypeError, "immutable object" - def __iter__(me): - return iter(me.map) - def keys(me): - return [k for k in me] - def values(me): - return [me[k] for k in me] -eccurves = _groupmap(_base._eccurves, ECInfo._curven) -primegroups = _groupmap(_base._pgroups, DHInfo._groupn) -bingroups = _groupmap(_base._bingroups, BinDHInfo._groupn) +#----- Elliptic curves ------------------------------------------------------ class _tmp: def __repr__(me): @@ -184,6 +188,8 @@ class _tmp: return '(%s, %s)' % (me.x, me.y) _augment(ECPtCurve, _tmp) +#----- Key sizes ------------------------------------------------------------ + class _tmp: def __repr__(me): return 'KeySZAny(%d)' % me.default def check(me, sz): return True @@ -212,6 +218,8 @@ class _tmp: return found _augment(KeySZSet, _tmp) +#----- Abstract groups ------------------------------------------------------ + class _tmp: def __repr__(me): return '%s(p = %s, r = %s, g = %s)' % \ @@ -236,7 +244,9 @@ class _tmp: return '%r(%r)' % (me.group, str(me)) _augment(GE, _tmp) -class PKCS1Crypt(object): +#----- RSA encoding techniques ---------------------------------------------- + +class PKCS1Crypt (object): def __init__(me, ep = '', rng = rand): me.ep = ep me.rng = rng @@ -245,7 +255,7 @@ class PKCS1Crypt(object): def decode(me, ct, nbits): return _base._p1crypt_decode(ct, nbits, me.ep, me.rng) -class PKCS1Sig(object): +class PKCS1Sig (object): def __init__(me, ep = '', rng = rand): me.ep = ep me.rng = rng @@ -254,7 +264,7 @@ class PKCS1Sig(object): def decode(me, msg, sig, nbits): return _base._p1sig_decode(msg, sig, nbits, me.ep, me.rng) -class OAEP(object): +class OAEP (object): def __init__(me, mgf = sha_mgf, hash = sha, ep = '', rng = rand): me.mgf = mgf me.hash = hash @@ -265,7 +275,7 @@ class OAEP(object): def decode(me, ct, nbits): return _base._oaep_decode(ct, nbits, me.mgf, me.hash, me.ep, me.rng) -class PSS(object): +class PSS (object): def __init__(me, mgf = sha_mgf, hash = sha, saltsz = None, rng = rand): me.mgf = mgf me.hash = hash @@ -296,6 +306,47 @@ class _tmp: def sign(me, msg, enc): return me.privop(enc.encode(msg, me.n.nbits)) _augment(RSAPriv, _tmp) +#----- Built-in named curves and prime groups ------------------------------- + +class _groupmap (object): + def __init__(me, map, nth): + me.map = map + me.nth = nth + me.i = [None] * (max(map.values()) + 1) + def __repr__(me): + return '{%s}' % ', '.join(['%r: %r' % (k, me[k]) for k in me]) + def __contains__(me, k): + return k in me.map + def __getitem__(me, k): + i = me.map[k] + if me.i[i] is None: + me.i[i] = me.nth(i) + return me.i[i] + def __setitem__(me, k, v): + raise TypeError, "immutable object" + def __iter__(me): + return iter(me.map) + def keys(me): + return [k for k in me] + def values(me): + return [me[k] for k in me] +eccurves = _groupmap(_base._eccurves, ECInfo._curven) +primegroups = _groupmap(_base._pgroups, DHInfo._groupn) +bingroups = _groupmap(_base._bingroups, BinDHInfo._groupn) + +#----- Prime number generation ---------------------------------------------- + +class PrimeGenEventHandler (object): + def pg_begin(me, ev): + return me.pg_try(ev) + def pg_done(me, ev): + return PGEN_DONE + def pg_abort(me, ev): + return PGEN_TRY + def pg_fail(me, ev): + return PGEN_TRY + def pg_pass(me, ev): + return PGEN_TRY class SophieGermainStepJump (object): def pg_begin(me, ev): @@ -358,18 +409,6 @@ class SophieGermainTester (object): del me.lr del me.hr -class PrimeGenEventHandler (object): - def pg_begin(me, ev): - return me.pg_try(ev) - def pg_done(me, ev): - return PGEN_DONE - def pg_abort(me, ev): - return PGEN_TRY - def pg_fail(me, ev): - return PGEN_TRY - def pg_pass(me, ev): - return PGEN_TRY - class PrimitiveStepper (PrimeGenEventHandler): def __init__(me): pass @@ -477,6 +516,4 @@ def kcdsaprime(pbits, qbits, rng = rand, p = 2 * q * h + 1 return p, q, h -import pwsafe - #----- That's all, folks ---------------------------------------------------- -- 2.11.0