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 4 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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,24 @@ 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`
- [libra-crypto](https://github.com/libra/libra/tree/master/crypto/crypto) : in unit tests
- 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,6 +97,7 @@ 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 |
Expand All @@ -102,15 +107,20 @@ For a total of 12 test vectors.
|Go | V | V | V | V | X | X | X | X | X | X | X | V |
|libra-crypto | X | X | X | V | X | X | X | X | X | X | X | X |
|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 | E | E | E | E | E | E | E | V |
|Zebra | V | V | V | V | V | V | X | X | X | V | V | V |
---------------------------------------------------------------
```
Note that wolfSSL can return three options, verified (V), not verfied
(X) and error in verification procedure (E).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's highly possible that this is true in other implementations as well, but iirc we used X for both errors and unverified sigs. We can make a comment to track those and update holistically (for the other implementations too) in another PR, but in this PR use X.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Expect to add a few more libraries. Will update this on the next addition.


Contributors
------------
Expand Down
4 changes: 4 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 @@ -9,10 +10,13 @@
|Hacl* | V | V | V | V | X | X | X | X | X | X | X | X |
|libra-crypto | X | X | X | V | X | X | X | X | X | X | X | X |
|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 | E | E | E | E | E | E | E | 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;
}
2 changes: 2 additions & 0 deletions scripts/monocypher/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main: main.c
gcc -o main monocypher-ed25519.c monocypher.c main.c -I$(PWD)
13 changes: 13 additions & 0 deletions scripts/monocypher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Monocypher is can be in the Public Domain or under a BSD 2 Clause
license. It is available at
https://monocypher.org


```
> wget https://monocypher.org/download/monocypher-3.1.3.tar.gz
> tar -xf monocypher-3.1.3.tar.gz
> cp monocypher-3.1.3/src/monocypher.* .
> cp monocypher-3.1.3/src/optional/monocypher-ed25519.* .
> make
> ./main
```
71 changes: 71 additions & 0 deletions scripts/monocypher/main.c
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 "monocypher.h"
#include "monocypher-ed25519.h"
#include <stdio.h>

#define MESSAGE_LEN 32
#define crypto_sign_PUBLICKEYBYTES 32
#define crypto_sign_BYTES 64

int main(void) {

unsigned char public_key[crypto_sign_PUBLICKEYBYTES];
unsigned char message[MESSAGE_LEN];
unsigned long long message_len = MESSAGE_LEN;
unsigned char signature[crypto_sign_BYTES];
unsigned long long signature_len = 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|Monocypher |");
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", &public_key[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_ed25519_check(signature, public_key, &message, message_len);

if (result != 0) {
printf(" X |");
// Incorrect signature!
} else {
printf(" V |");
}

}

printf("\n");
fclose(fp);
return 0;
}
2 changes: 2 additions & 0 deletions scripts/tweetnacl/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main: main.c
gcc -o main randombytes.c tweetnacl.c main.c -I$(PWD)
14 changes: 14 additions & 0 deletions scripts/tweetnacl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
TweetNaCl is in the Public Domain and is available at
https://tweetnacl.cr.yp.to/software.html

Randombytes is available under MIT License and is available at
https://github.com/dsprenkels/randombytes

```
> wget https://tweetnacl.cr.yp.to/20140427/tweetnacl.c
> wget https://tweetnacl.cr.yp.to/20140427/tweetnacl.h
> wget https://raw.githubusercontent.com/dsprenkels/randombytes/4ca4200730d3d5e8f18a1728663287792ad5e227/randombytes.c
> wget https://raw.githubusercontent.com/dsprenkels/randombytes/4ca4200730d3d5e8f18a1728663287792ad5e227/randombytes.h
> make
> ./main
```
71 changes: 71 additions & 0 deletions scripts/tweetnacl/main.c
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 "tweetnacl.h"
#include <stdio.h>

#define MESSAGE_LEN 32

int main(void) {

unsigned char pk[crypto_sign_PUBLICKEYBYTES];
// Array message is larger than MESSAGE_LEN because
// verification in TweetNACL uses the additional space
unsigned char message[crypto_sign_BYTES + MESSAGE_LEN];
unsigned long long message_len = MESSAGE_LEN;
unsigned char signed_message[crypto_sign_BYTES + MESSAGE_LEN];
unsigned long long signed_message_len = crypto_sign_BYTES + MESSAGE_LEN;

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|TweetNaCl |");
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]);
sscanf(buff + 4 + pos, "%2hhx", &signed_message[count+64]);
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", &signed_message[count]);
pos += 2;
}

int result = crypto_sign_open(message, &message_len,
signed_message, signed_message_len, &pk[0]);
if (result == -1) {
printf(" X |");
// Incorrect signature!
} else {
printf(" V |");
}

}

printf("\n");
fclose(fp);
return 0;
}
5 changes: 5 additions & 0 deletions scripts/wolfssl/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


LIBWOLFSSL_INSTALL_DIR=./wolfssl-install
main: main.c
gcc -o main main.c -I$(LIBWOLFSSL_INSTALL_DIR)/include -L$(LIBWOLFSSL_INSTALL_DIR)/lib -lwolfssl
17 changes: 17 additions & 0 deletions scripts/wolfssl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
wolfSSL is available under GPLv2 license as well as under
commercial/custom licenses.

```
> wget https://github.com/wolfSSL/wolfssl/archive/refs/tags/v5.5.3-stable.tar.gz
> tar -xf v5.5.3-stable.tar.gz
> export LIBWOLFSSL_INSTALL_PATH=$(pwd)/wolfssl-install
> cd wolfssl-5.5.3-stable/
> ./autogen.sh
> ./configure --prefix=$(LIBWOLFSSL_INSTALL_PATH) --enable-opensslextra --enable-opensslall --enable-curve25519 --enable-ed25519
> make
> make install
> cd ..
> export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBWOLFSSL_INSTALL_PATH/lib
> make
> ./main
```
Loading