pynacl

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

commit 6c95cbbbe0f4af92522b4ef4d0d9ddcd154fd1d6
parent b0de5e10dc26c7324aa20be076054af636b7a2dd
Author: Sean Lynch <seanl@literati.org>
Date:   Tue, 14 Jun 2011 14:54:26 -0700

Add crypto_scalarmult_curve25519 and clean up the docs a bit.

Diffstat:
MREADME.md | 34++++++++++++++++++++++++++++++----
Mnacl.i | 22++++++++++++++++++++++
Mtest.py | 19+++++++++++++++++++
3 files changed, 71 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md @@ -5,10 +5,28 @@ Overview -------- This is a simple wrapper for the [NaCl](http://nacl.cace-project.eu/) -cryptographic library (not Google's NativeClient). It currently wraps -crypto\_hash\_sha256, crypto\_hash\_sha512, crypto\_randombytes, and -the crypto\_box, crypto\_sign, crypto\_secretbox, crypto\_stream, -crypto\_auth, crypto\_scalarmult and crypto\_onetimeauth default primitives. +cryptographic library (not Google's NativeClient). + + +Features +-------- + +PyNaCl currently wraps the following NaCl functions: + +* crypto\_hash\_sha256 +* crypto\_hash\_sha512 +* crypto\_randombytes + +And the following groups of functions (if not otherwise specified by +the suffix, it just wraps the default primitive): + +* crypto\_scalarmult\_curve25519 +* crypto\_box +* crypto\_sign +* crypto\_secretbox +* crypto\_stream +* crypto\_auth +* crypto\_onetimeauth API @@ -58,3 +76,11 @@ License ------ PyNaCl is released under version 2.0 of the Apache license. + + +To do +----- + +* Convert to a package so I can add Python code +* Implement a higher-level API +* Implement fromseed versions of other key generation functions diff --git a/nacl.i b/nacl.i @@ -18,6 +18,7 @@ %{ #include "crypto_box.h" + #include "crypto_scalarmult_curve25519.h" #include "crypto_sign.h" #include "crypto_scalarmult_curve25519.h" #include "crypto_secretbox.h" @@ -318,6 +319,27 @@ int crypto_box_open_afternm(unsigned char out[crypto_box_ZEROBYTES], const unsigned char n[crypto_box_NONCEBYTES], const unsigned char k[crypto_box_BEFORENMBYTES]); + +/** + * Scalar multiplication + */ +%constant int crypto_scalarmult_curve25519_BYTES; +%constant int crypto_scalarmult_curve25519_SCALARBYTES; +%constant char *crypto_scalarmult_curve25519_IMPLEMENTATION; +%constant char *crypto_scalarmult_curve25519_VERSION; + +int crypto_scalarmult_curve25519(unsigned char + q[crypto_scalarmult_curve25519_BYTES], + const unsigned char + n[crypto_scalarmult_curve25519_SCALARBYTES], + const unsigned char + p[crypto_scalarmult_curve25519_BYTES]); +int crypto_scalarmult_curve25519_base(unsigned char + q[crypto_scalarmult_curve25519_BYTES], + const unsigned char + n[crypto_scalarmult_curve25519_SCALARBYTES]); + + /** * Scalar multiplication */ diff --git a/test.py b/test.py @@ -97,6 +97,25 @@ class BoxTestCase(unittest.TestCase): self.sk1) +class ScalarMultTestCase(unittest.TestCase): + def setUp(self): + self.sk1 = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES) + self.pk1 = nacl.crypto_scalarmult_curve25519_base(self.sk1) + self.sk2 = nacl.randombytes(nacl.crypto_scalarmult_curve25519_BYTES) + self.pk2 = nacl.crypto_scalarmult_curve25519_base(self.sk2) + + def test_dh(self): + """Check that both shared secrets are the same.""" + s1 = nacl.crypto_scalarmult_curve25519(self.sk1, self.pk2) + s2 = nacl.crypto_scalarmult_curve25519(self.sk2, self.pk1) + self.assertEqual(s1, s2) + + def test_dh_badkey(self): + s1 = nacl.crypto_scalarmult_curve25519(self.sk1, self.pk2) + s2 = nacl.crypto_scalarmult_curve25519(perturb(self.sk2), self.pk1) + self.assertNotEqual(s1, s2) + + class SignTestCase(unittest.TestCase): msg = b"The quick brown fox jumps over the lazy dog."