X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/catacomb-python/blobdiff_plain/5c17375ab0e48de370a4d02951d24feba8b5065a..ce0340b65241b46976bf52e14e80879debee5dba:/catacomb/__init__.py diff --git a/catacomb/__init__.py b/catacomb/__init__.py index 6745bce..9963572 100644 --- a/catacomb/__init__.py +++ b/catacomb/__init__.py @@ -107,6 +107,26 @@ class _tmp: _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.