chiark / gitweb /
catacomb/__init__.py: Use `%#x' rather than `hex' now.
[catacomb-python] / catacomb / __init__.py
index 120ef111d967e3bc368daf9584a1ff7b285b06fd..43d74d9a7bd7612d0ef1028a38b61b19d6e966ac 100644 (file)
@@ -94,8 +94,39 @@ class _tmp:
   def __repr__(me):
     return 'bytes(%r)' % hex(me)
 _augment(ByteString, _tmp)
+ByteString.__hash__ = str.__hash__
 bytes = ByteString.fromhex
 
+###--------------------------------------------------------------------------
+### Hashing.
+
+class _tmp:
+  def check(me, h):
+    hh = me.done()
+    return ctstreq(h, hh)
+_augment(GHash, _tmp)
+_augment(Poly1305Hash, _tmp)
+
+###--------------------------------------------------------------------------
+### NaCl `secretbox'.
+
+def secret_box(k, n, m):
+  E = xsalsa20(k).setiv(n)
+  r = E.enczero(poly1305.keysz.default)
+  s = E.enczero(poly1305.masksz)
+  y = E.encrypt(m)
+  t = poly1305(r)(s).hash(y).done()
+  return ByteString(t + y)
+
+def secret_unbox(k, n, c):
+  E = xsalsa20(k).setiv(n)
+  r = E.enczero(poly1305.keysz.default)
+  s = E.enczero(poly1305.masksz)
+  y = c[poly1305.tagsz:]
+  if not poly1305(r)(s).hash(y).check(c[0:poly1305.tagsz]):
+    raise ValueError, 'decryption failed'
+  return E.decrypt(c[poly1305.tagsz:])
+
 ###--------------------------------------------------------------------------
 ### Multiprecision integers and binary polynomials.
 
@@ -198,7 +229,7 @@ class _tmp:
 _augment(PrimeField, _tmp)
 
 class _tmp:
-  def __repr__(me): return '%s(%sL)' % (type(me).__name__, hex(me.p))
+  def __repr__(me): return '%s(%#xL)' % (type(me).__name__, me.p)
   def ec(me, a, b): return ECBinProjCurve(me, a, b)
 _augment(BinField, _tmp)
 
@@ -427,6 +458,72 @@ class _tmp:
   def sign(me, msg, enc): return me.privop(enc.encode(msg, me.n.nbits))
 _augment(RSAPriv, _tmp)
 
+###--------------------------------------------------------------------------
+### Bernstein's elliptic curve crypto and related schemes.
+
+X25519_BASE = \
+  bytes('0900000000000000000000000000000000000000000000000000000000000000')
+
+X448_BASE = \
+  bytes('05000000000000000000000000000000000000000000000000000000'
+        '00000000000000000000000000000000000000000000000000000000')
+
+Z128 = bytes('00000000000000000000000000000000')
+
+class _BoxyPub (object):
+  def __init__(me, pub, *kw, **kwargs):
+    if len(pub) != me._PUBSZ: raise ValueError, 'bad public key'
+    super(_BoxyPub, me).__init__(*kw, **kwargs)
+    me.pub = pub
+
+class _BoxyPriv (_BoxyPub):
+  def __init__(me, priv, pub = None, *kw, **kwargs):
+    if len(priv) != me._KEYSZ: raise ValueError, 'bad private key'
+    if pub is None: pub = me._op(priv, me._BASE)
+    super(_BoxyPriv, me).__init__(pub = pub, *kw, **kwargs)
+    me.priv = priv
+  def agree(me, you): return me._op(me.priv, you.pub)
+  def boxkey(me, recip):
+    return me._hashkey(me.agree(recip))
+  def box(me, recip, n, m):
+    return secret_box(me.boxkey(recip), n, m)
+  def unbox(me, recip, n, c):
+    return secret_unbox(me.boxkey(recip, n, c))
+
+class X25519Pub (_BoxyPub):
+  _PUBSZ = X25519_PUBSZ
+  _BASE = X25519_BASE
+
+class X25519Priv (_BoxyPriv, X25519Pub):
+  _KEYSZ = X25519_KEYSZ
+  def _op(me, k, X): return x25519(k, X)
+  def _hashkey(me, z): return hsalsa20_prf(z, Z128)
+
+class X448Pub (_BoxyPub):
+  _PUBSZ = X448_PUBSZ
+  _BASE = X448_BASE
+
+class X448Priv (_BoxyPriv, X448Pub):
+  _KEYSZ = X448_KEYSZ
+  def _op(me, k, X): return x448(k, X)
+  ##def _hashkey(me, z): return ???
+
+class Ed25519Pub (object):
+  def __init__(me, pub):
+    me.pub = pub
+  def verify(me, msg, sig):
+    return ed25519_verify(me.pub, msg, sig)
+
+class Ed25519Priv (Ed25519Pub):
+  def __init__(me, priv):
+    me.priv = priv
+    Ed25519Pub.__init__(me, ed25519_pubkey(priv))
+  def sign(me, msg):
+    return ed25519_sign(me.priv, msg, pub = me.pub)
+  @classmethod
+  def generate(cls, rng = rand):
+    return cls(rng.block(ED25519_KEYSZ))
+
 ###--------------------------------------------------------------------------
 ### Built-in named curves and prime groups.