From 2a8202e74846d191a321cca1202175af9db6107d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 5 Nov 2020 01:45:06 +0100 Subject: Convert to sibs project --- lib/curve25519-donna/.gitignore | 7 ++ lib/curve25519-donna/contrib/Curve25519Donna.c | 118 --------------------- lib/curve25519-donna/contrib/Curve25519Donna.h | 53 --------- lib/curve25519-donna/contrib/Curve25519Donna.java | 77 -------------- lib/curve25519-donna/contrib/make-snippets | 68 ------------ lib/curve25519-donna/project.conf | 5 + .../python-src/curve25519/__init__.py | 4 - .../python-src/curve25519/curve25519module.c | 105 ------------------ lib/curve25519-donna/python-src/curve25519/keys.py | 46 -------- .../python-src/curve25519/test/__init__.py | 0 .../python-src/curve25519/test/test_curve25519.py | 99 ----------------- .../python-src/curve25519/test/test_speed.py | 46 -------- lib/curve25519-donna/test-curve25519.c | 54 ---------- lib/curve25519-donna/test-noncanon.c | 39 ------- lib/curve25519-donna/test-sc-curve25519.c | 72 ------------- lib/curve25519-donna/test-sc-curve25519.s | 8 -- 16 files changed, 12 insertions(+), 789 deletions(-) delete mode 100644 lib/curve25519-donna/contrib/Curve25519Donna.c delete mode 100644 lib/curve25519-donna/contrib/Curve25519Donna.h delete mode 100644 lib/curve25519-donna/contrib/Curve25519Donna.java delete mode 100644 lib/curve25519-donna/contrib/make-snippets create mode 100644 lib/curve25519-donna/project.conf delete mode 100644 lib/curve25519-donna/python-src/curve25519/__init__.py delete mode 100644 lib/curve25519-donna/python-src/curve25519/curve25519module.c delete mode 100644 lib/curve25519-donna/python-src/curve25519/keys.py delete mode 100644 lib/curve25519-donna/python-src/curve25519/test/__init__.py delete mode 100755 lib/curve25519-donna/python-src/curve25519/test/test_curve25519.py delete mode 100755 lib/curve25519-donna/python-src/curve25519/test/test_speed.py delete mode 100644 lib/curve25519-donna/test-curve25519.c delete mode 100644 lib/curve25519-donna/test-noncanon.c delete mode 100644 lib/curve25519-donna/test-sc-curve25519.c delete mode 100644 lib/curve25519-donna/test-sc-curve25519.s (limited to 'lib/curve25519-donna') diff --git a/lib/curve25519-donna/.gitignore b/lib/curve25519-donna/.gitignore index ccabede..1d764d7 100644 --- a/lib/curve25519-donna/.gitignore +++ b/lib/curve25519-donna/.gitignore @@ -10,3 +10,10 @@ *.pyc /dist /MANIFEST + + +# Compiled sibs files +sibs-build/ +compile_commands.json +tests/sibs-build/ +tests/compile_commands.json diff --git a/lib/curve25519-donna/contrib/Curve25519Donna.c b/lib/curve25519-donna/contrib/Curve25519Donna.c deleted file mode 100644 index 71b816c..0000000 --- a/lib/curve25519-donna/contrib/Curve25519Donna.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - James Robson - Public domain. -*/ - -#include "Curve25519Donna.h" -#include -#include - -extern void curve25519_donna(unsigned char *output, const unsigned char *a, - const unsigned char *b); - -unsigned char* -as_unsigned_char_array(JNIEnv* env, jbyteArray array, int* len); - -jbyteArray as_byte_array(JNIEnv* env, unsigned char* buf, int len); - - -jbyteArray as_byte_array(JNIEnv* env, unsigned char* buf, int len) { - jbyteArray array = (*env)->NewByteArray(env, len); - (*env)->SetByteArrayRegion(env, array, 0, len, (jbyte*)buf); - - //int i; - //for (i = 0;i < len;++i) printf("%02x",(unsigned int) buf[i]); printf(" "); - //printf("\n"); - - return array; -} - -unsigned char* -as_unsigned_char_array(JNIEnv* env, jbyteArray array, int* len) { - - *len = (*env)->GetArrayLength(env, array); - unsigned char* buf = (unsigned char*)calloc(*len+1, sizeof(char)); - (*env)->GetByteArrayRegion (env, array, 0, *len, (jbyte*)buf); - return buf; - -} - -JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_curve25519Donna - (JNIEnv *env, jobject obj, jbyteArray a, jbyteArray b) { - - unsigned char o[32] = {0}; - int l1, l2; - unsigned char* a1 = as_unsigned_char_array(env, a, &l1); - unsigned char* b1 = as_unsigned_char_array(env, b, &l2); - - if ( !(l1 == 32 && l2 == 32) ) { - fprintf(stderr, "Error, must be length 32"); - return NULL; - } - - - curve25519_donna(o, (const unsigned char*)a1, (const unsigned char*)b1); - - free(a1); - free(b1); - - return as_byte_array(env, (unsigned char*)o, 32); -} - -JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_makePrivate - (JNIEnv *env, jobject obj, jbyteArray secret) { - - int len; - unsigned char* k = as_unsigned_char_array(env, secret, &len); - - if (len != 32) { - fprintf(stderr, "Error, must be length 32"); - return NULL; - } - - k[0] &= 248; - k[31] &= 127; - k[31] |= 64; - return as_byte_array(env, k, 32); -} - -JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_getPublic - (JNIEnv *env, jobject obj, jbyteArray privkey) { - - int len; - unsigned char* private = as_unsigned_char_array(env, privkey, &len); - - if (len != 32) { - fprintf(stderr, "Error, must be length 32"); - return NULL; - } - - unsigned char pubkey[32]; - unsigned char basepoint[32] = {9}; - - curve25519_donna(pubkey, private, basepoint); - return as_byte_array(env, (unsigned char*)pubkey, 32); -} - -JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_makeSharedSecret - (JNIEnv *env, jobject obj, jbyteArray privkey, jbyteArray their_pubkey) { - - unsigned char shared_secret[32]; - - int l1, l2; - unsigned char* private = as_unsigned_char_array(env, privkey, &l1); - unsigned char* pubkey = as_unsigned_char_array(env, their_pubkey, &l2); - - if ( !(l1 == 32 && l2 == 32) ) { - fprintf(stderr, "Error, must be length 32"); - return NULL; - } - - curve25519_donna(shared_secret, private, pubkey); - return as_byte_array(env, (unsigned char*)shared_secret, 32); -} - -JNIEXPORT void JNICALL Java_Curve25519Donna_helowrld - (JNIEnv *env, jobject obj) { - printf("helowrld\n"); -} diff --git a/lib/curve25519-donna/contrib/Curve25519Donna.h b/lib/curve25519-donna/contrib/Curve25519Donna.h deleted file mode 100644 index 3cd4ca0..0000000 --- a/lib/curve25519-donna/contrib/Curve25519Donna.h +++ /dev/null @@ -1,53 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class Curve25519Donna */ - -#ifndef _Included_Curve25519Donna -#define _Included_Curve25519Donna -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: Curve25519Donna - * Method: curve25519Donna - * Signature: ([B[B)[B - */ -JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_curve25519Donna - (JNIEnv *, jobject, jbyteArray, jbyteArray); - -/* - * Class: Curve25519Donna - * Method: makePrivate - * Signature: ([B)[B - */ -JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_makePrivate - (JNIEnv *, jobject, jbyteArray); - -/* - * Class: Curve25519Donna - * Method: getPublic - * Signature: ([B)[B - */ -JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_getPublic - (JNIEnv *, jobject, jbyteArray); - -/* - * Class: Curve25519Donna - * Method: makeSharedSecret - * Signature: ([B[B)[B - */ -JNIEXPORT jbyteArray JNICALL Java_Curve25519Donna_makeSharedSecret - (JNIEnv *, jobject, jbyteArray, jbyteArray); - -/* - * Class: Curve25519Donna - * Method: helowrld - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_Curve25519Donna_helowrld - (JNIEnv *, jobject); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/lib/curve25519-donna/contrib/Curve25519Donna.java b/lib/curve25519-donna/contrib/Curve25519Donna.java deleted file mode 100644 index e28cb53..0000000 --- a/lib/curve25519-donna/contrib/Curve25519Donna.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - James Robson - Public domain. -*/ - -public class Curve25519Donna { - - final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); - - public static String bytesToHex(byte[] bytes) { - char[] hexChars = new char[bytes.length * 2]; - int v; - for ( int j = 0; j < bytes.length; j++ ) { - v = bytes[j] & 0xFF; - hexChars[j * 2] = hexArray[v >>> 4]; - hexChars[j * 2 + 1] = hexArray[v & 0x0F]; - } - return new String(hexChars); - } - - public native byte[] curve25519Donna(byte[] a, byte[] b); - public native byte[] makePrivate(byte[] secret); - public native byte[] getPublic(byte[] privkey); - public native byte[] makeSharedSecret(byte[] privkey, byte[] theirPubKey); - public native void helowrld(); - - // Uncomment if your Java is 32-bit: - //static { System.loadLibrary("Curve25519Donna"); } - - // Otherwise, load this 64-bit .jnilib: - static { System.loadLibrary("Curve25519Donna_64"); } - - /* - To give the old tires a kick (OSX): - java -cp `pwd` Curve25519Donna - */ - public static void main (String[] args) { - - Curve25519Donna c = new Curve25519Donna(); - - // These should be 32 bytes long - byte[] user1Secret = "abcdefghijklmnopqrstuvwxyz123456".getBytes(); - byte[] user2Secret = "654321zyxwvutsrqponmlkjihgfedcba".getBytes(); - - - // You can use the curve function directly... - - //byte[] o = c.curve25519Donna(a, b); - //System.out.println("o = " + bytesToHex(o)); - - - // ... but it's not really necessary. Just use the following - // convenience methods: - - byte[] privKey = c.makePrivate(user1Secret); - byte[] pubKey = c.getPublic(privKey); - - byte[] privKey2 = c.makePrivate(user2Secret); - byte[] pubKey2 = c.getPublic(privKey2); - - System.out.println("'user1' privKey = " + bytesToHex(privKey)); - System.out.println("'user1' pubKey = " + bytesToHex(pubKey)); - System.out.println("==================================================="); - - System.out.println("'user2' privKey = " + bytesToHex(privKey2)); - System.out.println("'user2' pubKey = " + bytesToHex(pubKey2)); - System.out.println("==================================================="); - - - byte[] ss1 = c.makeSharedSecret(privKey, pubKey2); - System.out.println("'user1' computes shared secret: " + bytesToHex(ss1)); - - byte[] ss2 = c.makeSharedSecret(privKey2, pubKey); - System.out.println("'user2' computes shared secret: " + bytesToHex(ss2)); - - } -} diff --git a/lib/curve25519-donna/contrib/make-snippets b/lib/curve25519-donna/contrib/make-snippets deleted file mode 100644 index 4568721..0000000 --- a/lib/curve25519-donna/contrib/make-snippets +++ /dev/null @@ -1,68 +0,0 @@ -CFLAGS=-Wmissing-prototypes -Wdeclaration-after-statement -O2 -Wall -CC=clang - - -targets: curve25519-donna.a curve25519-donna-c64.a - -test: test-donna test-donna-c64 - - -clean: - rm -f java-src/*.class java-src/*.jnilib *.dylib *.o *.a *.pp test-curve25519-donna test-curve25519-donna-c64 speed-curve25519-donna speed-curve25519-donna-c64 - -curve25519-donna.a: curve25519-donna.o - ar -rc curve25519-donna.a curve25519-donna.o - ranlib curve25519-donna.a - - -##### OSX dynamic library (32- & 64-bit) - -curve25519donna.dylib: curve25519-donna.a curve25519-donna-c64.a - $(CC) -m32 -fpic -shared -Wl,-all_load curve25519-donna.a -Wl,-all_load -o libcurve25519donna.dylib - $(CC) -fpic -shared -Wl,-all_load curve25519-donna-c64.a -Wl,-all_load -o libcurve25519donna_64.dylib - -##### OSX/Java section hence - -# Java JNI - compiled for OSX (32- & 64-bit) -Curve25519Donna.class: - cd java-src; javah -jni Curve25519Donna; cd .. - cd java-src; javac Curve25519Donna.java; cd .. - -Curve25519Donna.jnilib: curve25519-donna.a curve25519-donna-c64.a Curve25519Donna.class - @echo "Building 32-bit..." - clang -o java-src/libCurve25519Donna.jnilib $(CFLAGS) -lc -shared -m32 -I /System/Library/Frameworks/JavaVM.framework/Headers curve25519-donna.o java-src/Curve25519Donna.c - @echo "Building 64-bit..." - clang -o java-src/libCurve25519Donna_64.jnilib $(CFLAGS) -lc -shared -I /System/Library/Frameworks/JavaVM.framework/Headers curve25519-donna-c64.o java-src/Curve25519Donna.c - -##### OSX/Java section end - -curve25519-donna.o: curve25519-donna.c - $(CC) -c curve25519-donna.c $(CFLAGS) -m32 - -curve25519-donna-c64.a: curve25519-donna-c64.o - ar -rc curve25519-donna-c64.a curve25519-donna-c64.o - ranlib curve25519-donna-c64.a - -curve25519-donna-c64.o: curve25519-donna-c64.c - $(CC) -c curve25519-donna-c64.c $(CFLAGS) - -test-donna: test-curve25519-donna - ./test-curve25519-donna | head -123456 | tail -1 - -test-donna-c64: test-curve25519-donna-c64 - ./test-curve25519-donna-c64 | head -123456 | tail -1 - -test-curve25519-donna: test-curve25519.c curve25519-donna.a - $(CC) -o test-curve25519-donna test-curve25519.c curve25519-donna.a $(CFLAGS) -m32 - -test-curve25519-donna-c64: test-curve25519.c curve25519-donna-c64.a - $(CC) -o test-curve25519-donna-c64 test-curve25519.c curve25519-donna-c64.a $(CFLAGS) - -speed-curve25519-donna: speed-curve25519.c curve25519-donna.a - $(CC) -o speed-curve25519-donna speed-curve25519.c curve25519-donna.a $(CFLAGS) -m32 - -speed-curve25519-donna-c64: speed-curve25519.c curve25519-donna-c64.a - $(CC) -o speed-curve25519-donna-c64 speed-curve25519.c curve25519-donna-c64.a $(CFLAGS) - -test-sc-curve25519-donna-c64: test-sc-curve25519.c curve25519-donna-c64.a - $(CC) -o test-sc-curve25519-donna-c64 -O test-sc-curve25519.c curve25519-donna-c64.a test-sc-curve25519.s $(CFLAGS) diff --git a/lib/curve25519-donna/project.conf b/lib/curve25519-donna/project.conf new file mode 100644 index 0000000..5ed7b82 --- /dev/null +++ b/lib/curve25519-donna/project.conf @@ -0,0 +1,5 @@ +[package] +name = "curve25519-donna" +type = "static" +version = "0.1.0" +platforms = ["any"] diff --git a/lib/curve25519-donna/python-src/curve25519/__init__.py b/lib/curve25519-donna/python-src/curve25519/__init__.py deleted file mode 100644 index 873ff57..0000000 --- a/lib/curve25519-donna/python-src/curve25519/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ - -from .keys import Private, Public - -hush_pyflakes = [Private, Public]; del hush_pyflakes diff --git a/lib/curve25519-donna/python-src/curve25519/curve25519module.c b/lib/curve25519-donna/python-src/curve25519/curve25519module.c deleted file mode 100644 index e309ec0..0000000 --- a/lib/curve25519-donna/python-src/curve25519/curve25519module.c +++ /dev/null @@ -1,105 +0,0 @@ -/* tell python that PyArg_ParseTuple(t#) means Py_ssize_t, not int */ -#define PY_SSIZE_T_CLEAN -#include -#if (PY_VERSION_HEX < 0x02050000) - typedef int Py_ssize_t; -#endif - -/* This is required for compatibility with Python 2. */ -#if PY_MAJOR_VERSION >= 3 - #include - #define y "y" -#else - #define PyBytes_FromStringAndSize PyString_FromStringAndSize - #define y "t" -#endif - -int curve25519_donna(char *mypublic, - const char *secret, const char *basepoint); - -static PyObject * -pycurve25519_makeprivate(PyObject *self, PyObject *args) -{ - char *in1; - Py_ssize_t in1len; - if (!PyArg_ParseTuple(args, y"#:clamp", &in1, &in1len)) - return NULL; - if (in1len != 32) { - PyErr_SetString(PyExc_ValueError, "input must be 32-byte string"); - return NULL; - } - in1[0] &= 248; - in1[31] &= 127; - in1[31] |= 64; - return PyBytes_FromStringAndSize((char *)in1, 32); -} - -static PyObject * -pycurve25519_makepublic(PyObject *self, PyObject *args) -{ - const char *private; - char mypublic[32]; - char basepoint[32] = {9}; - Py_ssize_t privatelen; - if (!PyArg_ParseTuple(args, y"#:makepublic", &private, &privatelen)) - return NULL; - if (privatelen != 32) { - PyErr_SetString(PyExc_ValueError, "input must be 32-byte string"); - return NULL; - } - curve25519_donna(mypublic, private, basepoint); - return PyBytes_FromStringAndSize((char *)mypublic, 32); -} - -static PyObject * -pycurve25519_makeshared(PyObject *self, PyObject *args) -{ - const char *myprivate, *theirpublic; - char shared_key[32]; - Py_ssize_t myprivatelen, theirpubliclen; - if (!PyArg_ParseTuple(args, y"#"y"#:generate", - &myprivate, &myprivatelen, &theirpublic, &theirpubliclen)) - return NULL; - if (myprivatelen != 32) { - PyErr_SetString(PyExc_ValueError, "input must be 32-byte string"); - return NULL; - } - if (theirpubliclen != 32) { - PyErr_SetString(PyExc_ValueError, "input must be 32-byte string"); - return NULL; - } - curve25519_donna(shared_key, myprivate, theirpublic); - return PyBytes_FromStringAndSize((char *)shared_key, 32); -} - - -static PyMethodDef -curve25519_functions[] = { - {"make_private", pycurve25519_makeprivate, METH_VARARGS, "data->private"}, - {"make_public", pycurve25519_makepublic, METH_VARARGS, "private->public"}, - {"make_shared", pycurve25519_makeshared, METH_VARARGS, "private+public->shared"}, - {NULL, NULL, 0, NULL}, -}; - -#if PY_MAJOR_VERSION >= 3 - static struct PyModuleDef - curve25519_module = { - PyModuleDef_HEAD_INIT, - "_curve25519", - NULL, - NULL, - curve25519_functions, - }; - - PyObject * - PyInit__curve25519(void) - { - return PyModule_Create(&curve25519_module); - } -#else - PyMODINIT_FUNC - init_curve25519(void) - { - (void)Py_InitModule("_curve25519", curve25519_functions); - } -#endif \ No newline at end of file diff --git a/lib/curve25519-donna/python-src/curve25519/keys.py b/lib/curve25519-donna/python-src/curve25519/keys.py deleted file mode 100644 index e131dac..0000000 --- a/lib/curve25519-donna/python-src/curve25519/keys.py +++ /dev/null @@ -1,46 +0,0 @@ -from . import _curve25519 -from hashlib import sha256 -import os - -# the curve25519 functions are really simple, and could be used without an -# OOP layer, but it's a bit too easy to accidentally swap the private and -# public keys that way. - -def _hash_shared(shared): - return sha256(b"curve25519-shared:"+shared).digest() - -class Private: - def __init__(self, secret=None, seed=None): - if secret is None: - if seed is None: - secret = os.urandom(32) - else: - secret = sha256(b"curve25519-private:"+seed).digest() - else: - assert seed is None, "provide secret, seed, or neither, not both" - if not isinstance(secret, bytes) or len(secret) != 32: - raise TypeError("secret= must be 32-byte string") - self.private = _curve25519.make_private(secret) - - def serialize(self): - return self.private - - def get_public(self): - return Public(_curve25519.make_public(self.private)) - - def get_shared_key(self, public, hashfunc=None): - if not isinstance(public, Public): - raise ValueError("'public' must be an instance of Public") - if hashfunc is None: - hashfunc = _hash_shared - shared = _curve25519.make_shared(self.private, public.public) - return hashfunc(shared) - -class Public: - def __init__(self, public): - assert isinstance(public, bytes) - assert len(public) == 32 - self.public = public - - def serialize(self): - return self.public diff --git a/lib/curve25519-donna/python-src/curve25519/test/__init__.py b/lib/curve25519-donna/python-src/curve25519/test/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/lib/curve25519-donna/python-src/curve25519/test/test_curve25519.py b/lib/curve25519-donna/python-src/curve25519/test/test_curve25519.py deleted file mode 100755 index b3a5447..0000000 --- a/lib/curve25519-donna/python-src/curve25519/test/test_curve25519.py +++ /dev/null @@ -1,99 +0,0 @@ -#! /usr/bin/env python - -import unittest - -from curve25519 import Private, Public -from hashlib import sha1, sha256 -from binascii import hexlify - -class Basic(unittest.TestCase): - def test_basic(self): - secret1 = b"abcdefghijklmnopqrstuvwxyz123456" - self.assertEqual(len(secret1), 32) - - secret2 = b"654321zyxwvutsrqponmlkjihgfedcba" - self.assertEqual(len(secret2), 32) - priv1 = Private(secret=secret1) - pub1 = priv1.get_public() - priv2 = Private(secret=secret2) - pub2 = priv2.get_public() - shared12 = priv1.get_shared_key(pub2) - e = b"b0818125eab42a8ac1af5e8b9b9c15ed2605c2bbe9675de89e5e6e7f442b9598" - self.assertEqual(hexlify(shared12), e) - shared21 = priv2.get_shared_key(pub1) - self.assertEqual(shared12, shared21) - - pub2a = Public(pub2.serialize()) - shared12a = priv1.get_shared_key(pub2a) - self.assertEqual(hexlify(shared12a), e) - - def test_errors(self): - priv1 = Private() - self.assertRaises(ValueError, priv1.get_shared_key, priv1) - - def test_seed(self): - # use 32-byte secret - self.assertRaises(TypeError, Private, secret=123) - self.assertRaises(TypeError, Private, secret=b"too short") - secret1 = b"abcdefghijklmnopqrstuvwxyz123456" - assert len(secret1) == 32 - priv1 = Private(secret=secret1) - priv1a = Private(secret=secret1) - priv1b = Private(priv1.serialize()) - self.assertEqual(priv1.serialize(), priv1a.serialize()) - self.assertEqual(priv1.serialize(), priv1b.serialize()) - e = b"6062636465666768696a6b6c6d6e6f707172737475767778797a313233343576" - self.assertEqual(hexlify(priv1.serialize()), e) - - # the private key is a clamped form of the secret, so they won't - # quite be the same - p = Private(secret=b"\x00"*32) - self.assertEqual(hexlify(p.serialize()), b"00"*31+b"40") - p = Private(secret=b"\xff"*32) - self.assertEqual(hexlify(p.serialize()), b"f8"+b"ff"*30+b"7f") - - # use arbitrary-length seed - self.assertRaises(TypeError, Private, seed=123) - priv1 = Private(seed=b"abc") - priv1a = Private(seed=b"abc") - priv1b = Private(priv1.serialize()) - self.assertEqual(priv1.serialize(), priv1a.serialize()) - self.assertEqual(priv1.serialize(), priv1b.serialize()) - self.assertRaises(AssertionError, Private, seed=b"abc", secret=b"no") - - priv1 = Private(seed=b"abc") - priv1a = Private(priv1.serialize()) - self.assertEqual(priv1.serialize(), priv1a.serialize()) - self.assertRaises(AssertionError, Private, seed=b"abc", secret=b"no") - - # use built-in os.urandom - priv2 = Private() - priv2a = Private(priv2.private) - self.assertEqual(priv2.serialize(), priv2a.serialize()) - - # attempt to use both secret= and seed=, not allowed - self.assertRaises(AssertionError, Private, seed=b"abc", secret=b"no") - - def test_hashfunc(self): - priv1 = Private(seed=b"abc") - priv2 = Private(seed=b"def") - shared_sha256 = priv1.get_shared_key(priv2.get_public()) - e = b"da959ffe77ebeb4757fe5ba310e28ede425ae0d0ff5ec9c884e2d08f311cf5e5" - self.assertEqual(hexlify(shared_sha256), e) - - # confirm the hash function remains what we think it is - def myhash(shared_key): - return sha256(b"curve25519-shared:"+shared_key).digest() - shared_myhash = priv1.get_shared_key(priv2.get_public(), myhash) - self.assertEqual(hexlify(shared_myhash), e) - - def hexhash(shared_key): - return sha1(shared_key).hexdigest().encode() - shared_hexhash = priv1.get_shared_key(priv2.get_public(), hexhash) - self.assertEqual(shared_hexhash, - b"80eec98222c8edc4324fb9477a3c775ce7c6c93a") - - -if __name__ == "__main__": - unittest.main() - diff --git a/lib/curve25519-donna/python-src/curve25519/test/test_speed.py b/lib/curve25519-donna/python-src/curve25519/test/test_speed.py deleted file mode 100755 index 4d7e0c8..0000000 --- a/lib/curve25519-donna/python-src/curve25519/test/test_speed.py +++ /dev/null @@ -1,46 +0,0 @@ -#! /usr/bin/env python - -from time import time -from curve25519 import Private - -count = 10000 -elapsed_get_public = 0.0 -elapsed_get_shared = 0.0 - -def abbreviate_time(data): - # 1.23s, 790ms, 132us - if data is None: - return "" - s = float(data) - if s >= 10: - #return abbreviate.abbreviate_time(data) - return "%d" % s - if s >= 1.0: - return "%.2fs" % s - if s >= 0.01: - return "%dms" % (1000*s) - if s >= 0.001: - return "%.1fms" % (1000*s) - if s >= 0.000001: - return "%.1fus" % (1000000*s) - return "%dns" % (1000000000*s) - -def nohash(key): return key - -for i in range(count): - p = Private() - start = time() - pub = p.get_public() - elapsed_get_public += time() - start - pub2 = Private().get_public() - start = time() - shared = p.get_shared_key(pub2) #, hashfunc=nohash) - elapsed_get_shared += time() - start - -print("get_public: %s" % abbreviate_time(elapsed_get_public / count)) -print("get_shared: %s" % abbreviate_time(elapsed_get_shared / count)) - -# these take about 560us-570us each (with the default compiler settings, -Os) -# on my laptop, same with -O2 -# of which the python overhead is about 5us -# and the get_shared_key() hash step adds about 5us diff --git a/lib/curve25519-donna/test-curve25519.c b/lib/curve25519-donna/test-curve25519.c deleted file mode 100644 index 591d871..0000000 --- a/lib/curve25519-donna/test-curve25519.c +++ /dev/null @@ -1,54 +0,0 @@ -/* -test-curve25519 version 20050915 -D. J. Bernstein -Public domain. - -Tiny modifications by agl -*/ - -#include - -extern void curve25519_donna(unsigned char *output, const unsigned char *a, - const unsigned char *b); -void doit(unsigned char *ek,unsigned char *e,unsigned char *k); - -void doit(unsigned char *ek,unsigned char *e,unsigned char *k) -{ - int i; - - for (i = 0;i < 32;++i) printf("%02x",(unsigned int) e[i]); printf(" "); - for (i = 0;i < 32;++i) printf("%02x",(unsigned int) k[i]); printf(" "); - curve25519_donna(ek,e,k); - for (i = 0;i < 32;++i) printf("%02x",(unsigned int) ek[i]); printf("\n"); -} - -unsigned char e1k[32]; -unsigned char e2k[32]; -unsigned char e1e2k[32]; -unsigned char e2e1k[32]; -unsigned char e1[32] = {3}; -unsigned char e2[32] = {5}; -unsigned char k[32] = {9}; - -int -main() -{ - int loop; - int i; - - for (loop = 0;loop < 10000;++loop) { - doit(e1k,e1,k); - doit(e2e1k,e2,e1k); - doit(e2k,e2,k); - doit(e1e2k,e1,e2k); - for (i = 0;i < 32;++i) if (e1e2k[i] != e2e1k[i]) { - printf("fail\n"); - return 1; - } - for (i = 0;i < 32;++i) e1[i] ^= e2k[i]; - for (i = 0;i < 32;++i) e2[i] ^= e1k[i]; - for (i = 0;i < 32;++i) k[i] ^= e1e2k[i]; - } - - return 0; -} diff --git a/lib/curve25519-donna/test-noncanon.c b/lib/curve25519-donna/test-noncanon.c deleted file mode 100644 index 6de4e8d..0000000 --- a/lib/curve25519-donna/test-noncanon.c +++ /dev/null @@ -1,39 +0,0 @@ -/* This file can be used to test whether the code handles non-canonical curve - * points (i.e. points with the 256th bit set) in the same way as the reference - * implementation. */ - -#include -#include -#include - -extern void curve25519_donna(unsigned char *output, const unsigned char *a, - const unsigned char *b); -int -main() -{ - static const uint8_t point1[32] = { - 0x25,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - }; - static const uint8_t point2[32] = { - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - }; - static const uint8_t scalar[32] = { 1 }; - uint8_t out1[32], out2[32]; - - curve25519_donna(out1, scalar, point1); - curve25519_donna(out2, scalar, point2); - - if (0 == memcmp(out1, out2, sizeof(out1))) { - fprintf(stderr, "Top bit not ignored.\n"); - return 1; - } - - fprintf(stderr, "Top bit correctly ignored.\n"); - return 0; -} diff --git a/lib/curve25519-donna/test-sc-curve25519.c b/lib/curve25519-donna/test-sc-curve25519.c deleted file mode 100644 index 14a7e3c..0000000 --- a/lib/curve25519-donna/test-sc-curve25519.c +++ /dev/null @@ -1,72 +0,0 @@ -#define _GNU_SOURCE - -#include -#include -#include -#include - -extern void curve25519_donna(uint8_t *, const uint8_t *, const uint8_t *); -extern uint64_t tsc_read(); - -int -main(int argc, char **argv) { - uint8_t private_key[32], public[32], peer1[32], peer2[32], output[32]; - static const uint8_t basepoint[32] = {9}; - unsigned i; - uint64_t sum = 0, sum_squares = 0, skipped = 0, mean; - static const unsigned count = 200000; - - memset(private_key, 42, sizeof(private_key)); - - private_key[0] &= 248; - private_key[31] &= 127; - private_key[31] |= 64; - - curve25519_donna(public, private_key, basepoint); - memset(peer1, 0, sizeof(peer1)); - memset(peer2, 255, sizeof(peer2)); - - for (i = 0; i < count; ++i) { - const uint64_t start = tsc_read(); - curve25519_donna(output, peer1, public); - const uint64_t end = tsc_read(); - const uint64_t delta = end - start; - if (delta > 650000) { - // something terrible happened (task switch etc) - skipped++; - continue; - } - sum += delta; - sum_squares += (delta * delta); - } - - mean = sum / ((uint64_t) count); - printf("all 0: mean:%lu sd:%f skipped:%lu\n", - mean, - sqrt((double)(sum_squares/((uint64_t) count) - mean*mean)), - skipped); - - sum = sum_squares = skipped = 0; - - for (i = 0; i < count; ++i) { - const uint64_t start = tsc_read(); - curve25519_donna(output, peer2, public); - const uint64_t end = tsc_read(); - const uint64_t delta = end - start; - if (delta > 650000) { - // something terrible happened (task switch etc) - skipped++; - continue; - } - sum += delta; - sum_squares += (delta * delta); - } - - mean = sum / ((uint64_t) count); - printf("all 1: mean:%lu sd:%f skipped:%lu\n", - mean, - sqrt((double)(sum_squares/((uint64_t) count) - mean*mean)), - skipped); - - return 0; -} diff --git a/lib/curve25519-donna/test-sc-curve25519.s b/lib/curve25519-donna/test-sc-curve25519.s deleted file mode 100644 index 1da4f68..0000000 --- a/lib/curve25519-donna/test-sc-curve25519.s +++ /dev/null @@ -1,8 +0,0 @@ -.text -.globl tsc_read - -tsc_read: -rdtsc -shl $32,%rdx -or %rdx,%rax -ret -- cgit v1.2.3