Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Check tweetnacl C version #41

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,26 @@ For a total of 12 test vectors.

- [Apple CryptoKit](https://developer.apple.com/documentation/cryptokit) : in `scripts/ed25519-ios`
- BoringSSL, through [Ring](https://github.com/briansmith/ring) : in unit tests
- [Botan](https://github.com/randombit/botan) : `scripts/botan`
- [Bouncy Castle (Java)](https://www.bouncycastle.org/java.html) version 1.66 : in `scripts/ed25519-java`
- [Dalek](https://github.com/dalek-cryptography/ed25519-dalek) : in unit tests
- [ed25519-donna from Signal](https://github.com/signalapp/libsignal-protocol-c.git): in `scripts/ed25519-signal-donna`
- [ed25519-java](https://github.com/str4d/ed25519-java) version 0.3.0 : in `scripts/ed25519-java`
- [Go-ed25519](https://golang.org/pkg/crypto/ed25519/) : in `scripts/ed25519_test.go`
- [Libgcrypt](https://www.gnupg.org/software/libgcrypt/index.html) version 1.10.1 : in `scripts/libgcrypt`
- [libra-crypto](https://github.com/libra/libra/tree/master/crypto/crypto) : in unit tests
- [Libressl](https://www.libressl.org/) patched version 3.6.1 : in `scripts/libressl`
- LibSodium, through [pynacl](https://github.com/pyca/pynacl) : in `scripts/pynacl_test.py`
- [Monocypher](https://monocypher.org) : in `scripts/monocypher`
- nCipher's ed25519, by Rob Starkey
- [npm's ed25519](https://www.npmjs.com/package/ed25519) : in `scripts/eddsa_test`
- [OpenSSL](https://github.com/openssl/openssl) : in `scripts openssl_3/test_script.sh`
- [Pyca](https://cryptography.io/en/latest/) using OpenSSL 1.1.1g as default backend : in `scripts/pyca-openssl`
- [python-ed25519](https://github.com/warner/python-ed25519)) : in `scripts/python-ed25519`
- [ref10 from SUPERCOP through Python bindings](https://github.com/warner/python-ed25519) : in `scripts/python-ed25519.py`
- [tweetnacl](https://www.npmjs.com/package/tweetnacl) version 1.0.3 : in `scripts/tweetnacl`
- [tweetnacl](https://tweetnacl.cr.yp.to/software.html) version 20140427 : in `scripts/tweetnacl`
- [tweetnacl.js](https://www.npmjs.com/package/tweetnacl) version 1.0.3 : in `scripts/tweetnacl-js`
- [wolfSSL](https://github.com/wolfSSL/wolfssl) version 5.5.3 : in `scripts/wolfssl`
- [Zebra](https://github.com/ZcashFoundation/ed25519-zebra) : in unit tests

## Results
Expand All @@ -93,21 +99,27 @@ For a total of 12 test vectors.
|Library | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11|
|---------------+---+---+---+---+---+---+---+---+---+---+---+---|
|BoringSSL | V | V | V | V | X | X | X | X | X | X | X | V |
|Botan | V | V | V | V | X | X | V | X | X | X | X | V |
|BouncyCastle | V | V | V | V | X | X | X | X | X | X | X | X |
|CryptoKit | V | V | V | V | X | X | X | X | X | X | X | V |
|Dalek | V | V | V | V | X | X | X | X | X | X | X | V |
|Dalek strict | X | X | X | V | X | X | X | X | X | X | X | X |
|ed25519-donna | V | V | V | V | X | X | V | X | X | X | X | V |
|ed25519-java | V | V | V | V | X | X | V | V | X | X | V | X |
|Go | V | V | V | V | X | X | X | X | X | X | X | V |
|Libgcyrpt | X | X | V | X | X | X | V | V | X | X | X | X |
|libra-crypto | X | X | X | V | X | X | X | X | X | X | X | X |
|LibreSSL-3.6.1 | V | V | V | V | X | X | V | X | X | X | X | V |
|LibSodium | X | X | X | V | X | X | X | X | X | X | X | X |
|Monocypher | V | V | V | V | X | X | X | X | X | X | X | V |
|npm | V | V | V | V | X | X | X | X | X | X | X | V |
|OpenSSL-3.0 | V | V | V | V | X | X | X | X | X | X | X | V |
|PyCA | V | V | V | V | X | X | X | X | X | X | X | V |
|python-ed25519 | V | V | V | V | X | X | V | V | X | X | X | V |
|ref10 | V | V | V | V | X | X | V | X | X | X | X | V |
|TweetNaCl | V | V | V | V | X | X | V | V | X | X | X | V |
|TweetNaCl-js | V | V | V | V | X | X | V | V | X | X | X | V |
|WOLFSSL 5.5.3 | V | V | V | V | X | X | X | X | X | X | X | V |
|Zebra | V | V | V | V | V | V | X | X | X | V | V | V |
---------------------------------------------------------------
```
Expand Down
6 changes: 6 additions & 0 deletions results.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
|BoringSSL | V | V | V | V | X | X | X | X | X | X | X | V |
|Botan | V | V | V | V | X | X | V | X | X | X | X | V |
|BouncyCastle | V | V | V | V | X | X | X | X | X | X | X | X |
|CryptoKit | V | V | V | V | X | X | X | X | X | X | X | V |
|Dalek | V | V | V | V | X | X | X | X | X | X | X | V |
Expand All @@ -7,12 +8,17 @@
|ed25519-java | V | V | V | V | X | X | V | V | X | X | V | X |
|Go | V | V | V | V | X | X | X | X | X | X | X | V |
|Hacl* | V | V | V | V | X | X | X | X | X | X | X | X |
|Libgcyrpt | X | X | V | X | X | X | V | V | X | X | X | X |
|libra-crypto | X | X | X | V | X | X | X | X | X | X | X | X |
|LibreSSL-3.6.1 | V | V | V | V | X | X | V | X | X | X | X | V |
|LibSodium | X | X | X | V | X | X | X | X | X | X | X | X |
|Monocypher | V | V | V | V | X | X | X | X | X | X | X | V |
|npm | V | V | V | V | X | X | X | X | X | X | X | V |
|OpenSSL-3.0 | V | V | V | V | X | X | X | X | X | X | X | V |
|PyCA | V | V | V | V | X | X | X | X | X | X | X | V |
|python-ed25519 | V | V | V | V | X | X | V | V | X | X | X | V |
|ref10 | V | V | V | V | X | X | V | X | X | X | X | V |
|TweetNaCl | V | V | V | V | X | X | V | V | X | X | X | V |
|TweetNaCl-js | V | V | V | V | X | X | V | V | X | X | X | V |
|WOLFSSL 5.5.3 | V | V | V | V | X | X | X | X | X | X | X | V |
|Zebra | V | V | V | V | V | V | X | X | X | V | V | V |
6 changes: 6 additions & 0 deletions scripts/botan/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBBOTAN_INSTALL_DIR=$(pwd)/libbotan-build
main: main.o
g++ -o main main.o -L$(LIBBOTAN_INSTALL_DIR)/lib64 -lbotan-2

main.o: main.cpp
g++ -c -I$(LIBBOTAN_INSTALL_DIR)/include/botan-2 main.cpp -o main.o
17 changes: 17 additions & 0 deletions scripts/botan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Botan](https://botan.randombit.net) is available under a BSD-2-Clause license

```
> wget https://botan.randombit.net/releases/Botan-2.19.2.tar.xz
> tar -xf Botan-2.19.2.tar.xz
> mkdir libbotan-build
> export LIBBOTAN_INSTALL_DIR=$(pwd)/libbotan-build
> cd Botan-2.19.2
> ./configure --prefix=$(LIBBOTAN_INSTALL_DIR)
> make
> make check
> make install
> cd ..
> export LD_LIBRARY_PATH=$(pwd)/libbotan-build/lib64:$LD_LIBRARY_PATH
> make
> ./main
```
71 changes: 71 additions & 0 deletions scripts/botan/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Benson Muite
//
// This source code is licensed under the APACHE 2.0 license found in
// the LICENSE file in the root directory of this source tree.

#include <botan/sodium.h>
#include <stdio.h>

#define MESSAGE_LEN 32

using namespace Botan::Sodium;
int main(void) {
if (sodium_init() < 0) {
/* panic! the library couldn't be initialized, it is not safe to use */
printf("PANIC \n");
return 0;
}

unsigned char pk[crypto_sign_PUBLICKEYBYTES];
unsigned char message[MESSAGE_LEN];
uint8_t message_len = MESSAGE_LEN;
unsigned char signature[crypto_sign_BYTES];

FILE *fp;
int number_of_test_vectors = 0;
char buff[255];
int pos;

fp = fopen("../../cases.txt", "r+");
fscanf(fp, "%i", &number_of_test_vectors);
// printf("Number of test vectors: %i\n", number_of_test_vectors);
printf("\n|Botan |");
for (int i = 0; i < number_of_test_vectors; i++) {
// reading the message
fscanf(fp, "%s", buff);
pos = 0;
for (size_t count = 0; count < 32; count++) {
sscanf(buff + 4 + pos, "%2hhx", &message[count]);
pos += 2;
}

// reading the public key
fscanf(fp, "%s", buff); // message 32 bytes
pos = 0;
for (size_t count = 0; count < 32; count++) {
sscanf(buff + 4 + pos, "%2hhx", &pk[count]);
pos += 2;
}

// reading the signature
fscanf(fp, "%s", buff);
pos = 0;
for (size_t count = 0; count < 64; count++) {
sscanf(buff + 4 + pos, "%2hhx", &signature[count]);
pos += 2;
}

int result = crypto_sign_ed25519_verify_detached(signature, message, message_len, pk);

if (result == -1) {
printf(" X |");
/* Incorrect signature! */
} else {
printf(" V |");
}
}
printf("\n");
fclose(fp);
return 0;
}
5 changes: 5 additions & 0 deletions scripts/libgcrypt/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
LIBGCRYPT_INSTALL_DIR=./libgcrypt-install
LIBPTH_INSTALL_DIR=./libpth-install
LIBGPG_ERROR_INSTALL_DIR=./libgpg-error-install
main: main.c
gcc -o main main.c -I$(LIBGCRYPT_INSTALL_DIR)/include -L$(LIBGCRYPT_INSTALL_DIR)/lib -lgcrypt -L$(LIBGPG_ERROR_INSTALL_DIR)/lib -lgpg-error -L$(LIBPTH_INSTALL_DIR)/lib -lpth
45 changes: 45 additions & 0 deletions scripts/libgcrypt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[Libgcrypt](https://www.gnupg.org/software/libgcrypt/index.html) is
available under LGPLv2.1+ with the documentation and some helper
programs are available under GPLv2+.

[Libgpg-error](https://www.gnupg.org/software/libgpg-error/index.html)
is available under LGPLv2.1+

[Pth](https://www.gnupg.org/software/npth/index.html) is available under
LGPLv2.1+

Note that if some of these libraries are already installed on your system,
it may be easier to use them rather than update paths to a new installation.

```
> wget ftp://ftp.gnu.org/gnu/pth/pth-2.0.7.tar.gz
> tar -xf pth-2.0.7.tar.gz
> export PTH_INSTALL_DIR=$(pwd)/pth-install
> cd pth-2.0.7
> ./configure --prefix=$(PTH_INSTALL_DIR)
> make
> make check
> make install
> cd ..
> wget https://www.gnupg.org/ftp/gcrypt/libgpg-error/libgpg-error-1.46.tar.bz2
> tar -xf libgpg-error-1.46.tar.bz2
> export LIBGPG_ERROR_INSTALL_DIR=$(pwd)/libgpg-error-install
> cd libgpg-error-1.46/
> ./configure --prefix=$(LIBGPG_ERROR_INSTALL_DIR)
> make
> make check
> make install
> cd ..
> wget https://www.gnupg.org/ftp/gcrypt/libgcrypt/libgcrypt-1.10.1.tar.bz2
> tar -xf libgcrypt-1.10.1.tar.bz2
> export LIBGCRYPT_INSTALL_DIR=$(pwd)/libgcrypt-install
> cd libgcrypt-1.10.1/
> ./configure --prefix=$(LIBGCRYPT_INSTALL_DIR) --with-libgpg-error-prefix=$(LIBGPG_ERROR_INSTALL_DIR) --with-pth-prefix=$(PTH_INSTALL_DIR)
> make
> make check
> make install
> cd ..
> export LD_LIBRARY_PATH=$(LIBGPG_ERROR_INSTALL_DIR)/lib:$(PTH_INSTALL_DIR)/lib:$(LIBGCRYPT_INSTALL_DIR)/lib:$LD_LIBRARY_PATH
> make
> ./main
```
126 changes: 126 additions & 0 deletions scripts/libgcrypt/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Benson Muite
//
// This source code is licensed under the APACHE 2.0 license found in
// the LICENSE file in the root directory of this source tree.

#include <gcrypt.h>
#include <stdio.h>
#include <errno.h>

#define MESSAGE_LEN 32
#define crypto_sign_PUBLICKEYBYTES 32
#define crypto_sign_BYTES 64
int main(void) {

unsigned char pk[crypto_sign_PUBLICKEYBYTES];
unsigned char message[MESSAGE_LEN];
unsigned long long message_len = MESSAGE_LEN;
unsigned char sig_r[crypto_sign_BYTES/2];
unsigned char sig_s[crypto_sign_BYTES/2];
unsigned long long signature_len = crypto_sign_BYTES;
gpg_error_t err;

FILE *fp;
int number_of_test_vectors = 0;
char buff[255];
int pos;
int res;

if (!gcry_check_version(GCRYPT_VERSION))
{
fputs("libgcrypt version mismatch\n", stderr);
exit(2);
}

gcry_control(GCRYCTL_DISABLE_SECMEM, 0);

gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);

if(!gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
{
fputs("libgcrypt has not been initialized\n", stderr);
abort();
}

fp = fopen("../../cases.txt", "r+");
fscanf(fp, "%i", &number_of_test_vectors);
// printf("Number of test vectors: %i\n", number_of_test_vectors);
printf("\n|Libgcyrpt |");
for (int i = 0; i < number_of_test_vectors; i++) {
gcry_sexp_t s_pk = NULL;
gcry_sexp_t s_msg= NULL;
gcry_sexp_t s_sig= NULL;

// reading the message
fscanf(fp, "%s", buff);
pos = 0;
for (size_t count = 0; count < 32; count++) {
sscanf(buff + 4 + pos, "%2hhx", &message[count]);
pos += 2;
}

// reading the public key
fscanf(fp, "%s", buff); // message 32 bytes
pos = 0;
for (size_t count = 0; count < 32; count++) {
sscanf(buff + 4 + pos, "%2hhx", &pk[count]);
pos += 2;
}

// reading the signature
fscanf(fp, "%s", buff);
pos = 0;
for (size_t count = 0; count < 32; count++) {
sscanf(buff + 4 + pos, "%2hhx", &sig_r[count]);
pos += 2;
}
for (size_t count = 0; count < 32; count++) {
sscanf(buff + 4 + pos, "%2hhx", &sig_s[count]);
pos += 2;
}

err = gcry_sexp_build (&s_sig, NULL,
"(sig-val"
" (eddsa"
" (r %b)"
" (s %b)))",
(int)sizeof(sig_r), sig_r,
(int)sizeof(sig_s), sig_s);

err = gcry_sexp_build (&s_pk, NULL,
"(public-key"
" (ecc"
" (curve \"Ed25519\")"
" (flags eddsa)"
" (q %b)))",
(int)sizeof(pk), pk);
if (err)
printf ("gcry_sexp_build failed: %s\n", gpg_strerror (err));

err = gcry_sexp_build (&s_msg, NULL,
"(data"
" (flags eddsa)"
" (raw)"
" (value %b))",
(int)sizeof(message), message);
if (err)
printf ("gcry_sexp_build failed: %s\n", gpg_strerror (err));

gpg_error_t result = gcry_pk_verify (s_sig, s_msg, s_pk);
// printf(" %s \n",gpg_strerror (result));
if (result != 0) {
printf(" X |");
/* Incorrect signature! */
} else {
printf(" V |");
}
gcry_sexp_release (s_sig);
gcry_sexp_release (s_pk);
gcry_sexp_release (s_msg);
}
printf("\n");
fclose(fp);

return 0;
}
4 changes: 4 additions & 0 deletions scripts/libressl/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
LIBRESSL_INSTALL_DIR=./libressl-install
main: main.c
g++ -o main main.c -I$(LIBRESSL_INSTALL_DIR)/include -L$(LIBRESSL_INSTALL_DIR)/lib -lcrypto -lssl -ltls

22 changes: 22 additions & 0 deletions scripts/libressl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[Libressl](https://www.libressl.org/) is available under a combination
of several licenses, including ISC, OpenSSL, SSLeay and public domain.
Examine the source files to determine the ones that are appropriate.

At present ED25519 support is not enabled by default. A patch is applied
to enable it.

```
> wget https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-3.6.1.tar.gz
> tar -xf libressl-3.6.1.tar.gz
> export LIBRESSL_INSTALL_DIR=$(pwd)/libressl-install
> patch -p0 < try_ed25519.patch
> cd libressl-3.6.1
> ./configure --prefix=$(LIBRESSL_INSTALL_DIR)
> make
> make check
> make install
> cd ..
> make
> export LD_LIBRARY_PATH=$(LIBRESSL_INSTALL_DIR)/lib:$LD_LIBRARY_PATH
> ./main
```
Loading