pynacl

Python wrapper for http://nacl.cace-project.eu/
git clone https://code.literati.org/pynacl.git
Log | Files | Refs | README

commit bfd8c707407432f45a478b86eace269f11d1bb01
parent 6ce9a293b13e89ccd0107ed4ca7ff45de886243a
Author: Sean Lynch <seanl@literati.org>
Date:   Sun, 12 Jun 2011 19:17:39 -0700

Add authenticated secret-key encryption (crypto_secretbox) support.

Diffstat:
Mnacl.i | 37++++++++++++++++++++++++++++++++++++-
Mtest.py | 22+++++++++++++++++++++-
2 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/nacl.i b/nacl.i @@ -173,6 +173,12 @@ unsigned long long mlen), (unsigned char out[crypto_box_ZEROBYTES], const unsigned char in[crypto_box_BOXZEROBYTES], + unsigned long long mlen), + (unsigned char out[crypto_secretbox_BOXZEROBYTES], + const unsigned char in[crypto_secretbox_ZEROBYTES], + unsigned long long mlen), + (unsigned char out[crypto_secretbox_ZEROBYTES], + const unsigned char in[crypto_secretbox_BOXZEROBYTES], unsigned long long mlen) { if (!PyString_Check($input)) { PyErr_SetString(PyExc_ValueError, "Expecting a string"); @@ -190,6 +196,12 @@ unsigned long long mlen), (unsigned char out[crypto_box_ZEROBYTES], const unsigned char in[crypto_box_BOXZEROBYTES], + unsigned long long mlen), + (unsigned char out[crypto_secretbox_BOXZEROBYTES], + const unsigned char in[crypto_secretbox_ZEROBYTES], + unsigned long long mlen), + (unsigned char out[crypto_secretbox_ZEROBYTES], + const unsigned char in[crypto_secretbox_BOXZEROBYTES], unsigned long long mlen) { $result = PyString_FromStringAndSize((char *)&$1[$1_dim0], $3 - $1_dim0); free($1); @@ -249,7 +261,7 @@ int crypto_box_open_afternm(unsigned char out[crypto_box_ZEROBYTES], const unsigned char k[crypto_box_BEFORENMBYTES]); /** - * Signature stuff + * Signatures */ %constant int crypto_sign_PUBLICKEYBYTES; %constant int crypto_sign_SECRETKEYBYTES; @@ -266,3 +278,26 @@ int crypto_sign(unsigned char *sm, unsigned long long *smlen, int crypto_sign_open(unsigned char *m, unsigned long long *mlen, const unsigned char *sm, unsigned long long smlen, const unsigned char pk[crypto_sign_PUBLICKEYBYTES]); + + +/** + * Authenticated secret-key encryption + */ +%constant int crypto_secretbox_KEYBYTES; +%constant int crypto_secretbox_NONCEBYTES; +%constant int crypto_secretbox_ZEROBYTES; +%constant int crypto_secretbox_BOXZEROBYTES; +%constant char *crypto_secretbox_PRIMITIVE; +%constant char *crypto_secretbox_IMPLEMENTATION; +%constant char *crypto_secretbox_VERSION; + +int crypto_secretbox(unsigned char out[crypto_secretbox_BOXZEROBYTES], + const unsigned char in[crypto_secretbox_ZEROBYTES], + unsigned long long mlen, + const unsigned char n[crypto_secretbox_NONCEBYTES], + const unsigned char k[crypto_secretbox_KEYBYTES]); +int crypto_secretbox_open(unsigned char out[crypto_secretbox_ZEROBYTES], + const unsigned char in[crypto_secretbox_BOXZEROBYTES], + unsigned long long mlen, + const unsigned char n[crypto_secretbox_NONCEBYTES], + const unsigned char k[crypto_secretbox_KEYBYTES]); diff --git a/test.py b/test.py @@ -45,6 +45,7 @@ class HashTestCase(unittest.TestCase): "90cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3" "c463d481c7e586c39ac1ed") + class BoxTestCase(unittest.TestCase): msg = "The quick brown fox jumps over the lazy dog." def setUp(self): @@ -69,8 +70,27 @@ class BoxTestCase(unittest.TestCase): self.sk1) +class SecretBoxTestCase(unittest.TestCase): + msg = "The quick brown fox jumps over the lazy dog." + def setUp(self): + self.k = nacl.randombytes(nacl.crypto_secretbox_KEYBYTES) + + def test_secretbox(self): + nonce = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES) + c = nacl.crypto_secretbox(self.msg, nonce, self.k) + m = nacl.crypto_secretbox_open(c, nonce, self.k) + self.assertEqual(m, self.msg) + + def test_secretbox_badsig(self): + nonce = nacl.randombytes(nacl.crypto_secretbox_NONCEBYTES) + c = nacl.crypto_secretbox(self.msg, nonce, self.k) + c1 = c[:-1] + chr((ord(c[-1]) + 1) % 256) + self.assertRaises(ValueError, nacl.crypto_secretbox_open, c1, nonce, + self.k) + + class SignTestCase(unittest.TestCase): - msg = "hello world" + msg = "The quick brown fox jumps over the lazy dog." def setUp(self): self.pk, self.sk = nacl.crypto_sign_keypair()