Created
June 16, 2017 15:36
-
-
Save romen/8afbbba3d847128c737555529a70ffaa to your computer and use it in GitHub Desktop.
NaCl sign test clients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef HACL_CRYPTO_SIGN_H | |
#define HACL_CRYPTO_SIGN_H | |
#define crypto_sign_BYTES crypto_sign_ed25519_BYTES | |
#define crypto_sign_PUBLICKEYBYTES crypto_sign_ed25519_PUBLICKEYBYTES | |
#define crypto_sign_SECRETKEYBYTES crypto_sign_ed25519_SECRETKEYBYTES | |
#define crypto_sign_keypair(pk, sk) \ | |
crypto_sign_ed25519_keypair(pk,sk) | |
#define crypto_sign(sm, smlen_p, m, mlen, sk) \ | |
crypto_sign_ed25519(sm, smlen_p, m, mlen, sk) | |
#define crypto_sign_open(m, mlen_p, sm, smlen, pk) \ | |
crypto_sign_ed25519_open(m, mlen_p, sm, smlen, pk) | |
#define crypto_sign_detached(sig, siglen_p, m, mlen, sk) \ | |
crypto_sign_ed25519_detached(sig, siglen_p, m, mlen, sk) | |
#define crypto_sign_verify_detached(sig, m, mlen, pk) \ | |
crypto_sign_ed25519_verify_detached(sig, m, mlen, pk) | |
#define crypto_sign_ed25519_BYTES 64U | |
#define crypto_sign_ed25519_PUBLICKEYBYTES 32U | |
#define crypto_sign_ed25519_SECRETKEYBYTES (32U + 32U) | |
int crypto_sign_ed25519_keypair(unsigned char *pk, unsigned char *sk); | |
int crypto_sign_ed25519(unsigned char *sm, unsigned long long *smlen_p, | |
const unsigned char *m, unsigned long long mlen, | |
const unsigned char *sk); | |
int crypto_sign_ed25519_open(unsigned char *m, unsigned long long *mlen_p, | |
const unsigned char *sm, unsigned long long smlen, | |
const unsigned char *pk); | |
int crypto_sign_ed25519_detached(unsigned char *sig, | |
unsigned long long *siglen_p, | |
const unsigned char *m, | |
unsigned long long mlen, | |
const unsigned char *sk); | |
int crypto_sign_ed25519_verify_detached(const unsigned char *sig, | |
const unsigned char *m, | |
unsigned long long mlen, | |
const unsigned char *pk); | |
#endif // HACL_CRYPTO_SIGN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LIBSODIUM_PREFIX=/opt/libsodium-debug | |
LIBSODIUM_INC=$(LIBSODIUM_PREFIX)/include | |
LIBSODIUM_LIB=$(LIBSODIUM_PREFIX)/lib | |
LIBSODIUM_LFLAGS=-L$(LIBSODIUM_LIB) -lsodium | |
LIBSODIUM_RPATH=-Wl,-rpath=$(LIBSODIUM_LIB) | |
LIBHACL_ROOT=$(HOME)/repos/hacl-star | |
LIBHACL_PREFIX=$(LIBHACL_ROOT)/snapshots/hacl-c | |
LIBHACL_INC=$(LIBHACL_PREFIX) | |
LIBHACL_LIB=$(LIBHACL_PREFIX) | |
LIBHACL_LFLAGS=-L$(LIBHACL_LIB) -lhacl | |
LIBHACL_RPATH=-Wl,-rpath=$(LIBHACL_LIB) | |
COMPILER=clang | |
CFLAGS=-Weverything | |
NACL_TESTCLIENTS=nacl_testclient_sign nacl_testclient_sign_detached | |
HACL_TESTCLIENTS=hacl_testclient_sign hacl_testclient_sign_detached | |
test: nacl_testclients | |
./nacl_testclient_sign | |
@echo | |
./nacl_testclient_sign_detached | |
.PHONY: test | |
all: nacl_testclients | |
.PHONY: all | |
nacl_testclients: $(NACL_TESTCLIENTS) | |
.PHONY: nacl_testclients | |
nacl_testclient_sign: nacl_testclient_sign.c | |
$(COMPILER) $(CFLAGS) -o $@ -I$(LIBSODIUM_INC)/sodium $^ $(LIBSODIUM_LFLAGS) $(LIBSODIUM_RPATH) | |
nacl_testclient_sign_detached: nacl_testclient_sign_detached.c | |
$(COMPILER) $(CFLAGS) -o $@ -I$(LIBSODIUM_INC)/sodium $^ $(LIBSODIUM_LFLAGS) $(LIBSODIUM_RPATH) | |
hacl_testclients: $(HACL_TESTCLIENTS) | |
.PHONY: hacl_testclients | |
hacl_testclient_sign: nacl_testclient_sign.c $(LIBHACL_LIB)/libhacl.so | |
$(COMPILER) $(CFLAGS) -o $@ -I. -I$(LIBHACL_INC) $^ $(LIBHACL_LFLAGS) $(LIBHACL_RPATH) | |
hacl_testclient_sign_detached: nacl_testclient_sign_detached.c $(LIBHACL_LIB)/libhacl.so | |
$(COMPILER) $(CFLAGS) -o $@ -I. -I$(LIBHACL_INC) $^ $(LIBHACL_LFLAGS) $(LIBHACL_RPATH) | |
$(LIBHACL_LIB)/libhacl.so: | |
cd $(LIBHACL_ROOT) | |
make -C snapshots libhacl | |
clean: | |
rm -f $(NACL_TESTCLIENTS) $(HACL_TESTCLIENTS) | |
.PHONY: clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Test client for signatures under libsodium or compatible libraries, using | |
* the "Combined" mode | |
* | |
* Compile with: | |
gcc -o nacl_testclient_sign -I$LIBSODIUM_PREFIX/include/sodium nacl_testclient_sign.c -L$LIBSODIUM_PREFIX/lib -lsodium -Wl,-rpath=$LIBSODIUM_PREFIX/lib | |
* | |
* | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <crypto_sign.h> | |
#define MESSAGE (const unsigned char *) "This is a very important message" | |
#define TAMPERED_MESSAGE (const unsigned char *) "This is not the original message" | |
#define MESSAGE_LEN (strlen((const char*)(MESSAGE))) | |
int main(void) { | |
int err = 0; | |
unsigned char pk[crypto_sign_PUBLICKEYBYTES]; | |
unsigned char sk[crypto_sign_SECRETKEYBYTES]; | |
crypto_sign_keypair(pk, sk); // random keypair generation | |
unsigned char signed_message[MESSAGE_LEN+crypto_sign_BYTES]; | |
unsigned long long signed_message_len; | |
crypto_sign(signed_message, &signed_message_len, MESSAGE, MESSAGE_LEN, sk); | |
unsigned char unsigned_message[MESSAGE_LEN]; | |
unsigned long long unsigned_message_len; | |
fprintf(stdout, "signed_message: \t"); | |
if (crypto_sign_open(unsigned_message, &unsigned_message_len, signed_message, signed_message_len, pk) != 0) { | |
/* Incorrect signature! */ | |
fprintf(stdout, "Incorrect signature!\n"); | |
err = 0x01; | |
} else { | |
fprintf(stdout, "Valid signature!\n"); | |
} | |
strncpy((char*)signed_message, (const char*)TAMPERED_MESSAGE, MESSAGE_LEN); | |
fprintf(stdout, "tampered_message: \t"); | |
if (crypto_sign_open(unsigned_message, &unsigned_message_len, signed_message, signed_message_len, pk) != 0) { | |
/* Incorrect signature! */ | |
fprintf(stdout, "Incorrect signature!\n"); | |
} else { | |
fprintf(stdout, "Valid signature!\n"); | |
err = 0x1 << 1; | |
} | |
if (err != 0 ) { | |
fprintf(stderr, "FAIL\n"); | |
return err; | |
} | |
fprintf(stderr, "PASS\n"); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Test client for signatures under libsodium or compatible libraries, using | |
* the "Detached" mode | |
* | |
* Compile with: | |
gcc -o nacl_testclient_sign_detached -I$LIBSODIUM_PREFIX/include/sodium nacl_testclient_sign_detached.c -L$LIBSODIUM_PREFIX/lib -lsodium -Wl,-rpath=$LIBSODIUM_PREFIX/lib | |
* | |
* | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <crypto_sign.h> | |
#define MESSAGE (const unsigned char *) "This is a very important message" | |
#define TAMPERED_MESSAGE (const unsigned char *) "This is not the original message" | |
#define MESSAGE_LEN (strlen((const char *)(MESSAGE))) | |
int main(void) { | |
int err = 0; | |
unsigned char pk[crypto_sign_PUBLICKEYBYTES]; | |
unsigned char sk[crypto_sign_SECRETKEYBYTES]; | |
crypto_sign_keypair(pk, sk); // random keypair generation | |
unsigned char sig[crypto_sign_BYTES]; | |
unsigned long long sig_len; | |
crypto_sign_detached(sig, &sig_len, MESSAGE, MESSAGE_LEN, sk); | |
fprintf(stdout, "signed_message: \t"); | |
if (crypto_sign_verify_detached(sig, MESSAGE, MESSAGE_LEN, pk) != 0) { | |
/* Incorrect signature! */ | |
fprintf(stdout, "Incorrect signature!\n"); | |
err = 0x01; | |
} else { | |
fprintf(stdout, "Valid signature!\n"); | |
} | |
fprintf(stdout, "tampered_message: \t"); | |
if (crypto_sign_verify_detached(sig, TAMPERED_MESSAGE, MESSAGE_LEN, pk) != 0) { | |
/* Incorrect signature! */ | |
fprintf(stdout, "Incorrect signature!\n"); | |
} else { | |
fprintf(stdout, "Valid signature!\n"); | |
err = 0x1 << 1; | |
} | |
if (err != 0 ) { | |
fprintf(stderr, "FAIL\n"); | |
return err; | |
} | |
fprintf(stderr, "PASS\n"); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment