2019-01-16 09:15:18 +00:00
|
|
|
#include "api.h"
|
|
|
|
#include "randombytes.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define NTESTS 15
|
|
|
|
#define MLEN 32
|
|
|
|
|
2019-02-13 16:45:09 +00:00
|
|
|
const unsigned char canary[8] = {
|
|
|
|
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
|
|
|
|
};
|
2019-02-09 00:59:13 +00:00
|
|
|
|
2019-01-16 09:15:18 +00:00
|
|
|
/* allocate a bit more for all keys and messages and
|
|
|
|
* make sure it is not touched by the implementations.
|
|
|
|
*/
|
|
|
|
static void write_canary(unsigned char *d) {
|
2019-02-11 08:41:32 +00:00
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
d[i] = canary[i];
|
|
|
|
}
|
2019-01-16 09:15:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 12:02:35 +00:00
|
|
|
static int check_canary(const unsigned char *d) {
|
2019-02-11 08:41:32 +00:00
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
if (d[i] != canary[i])
|
|
|
|
return -1;
|
2019-01-16 12:02:35 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2019-01-16 09:15:18 +00:00
|
|
|
}
|
2019-01-22 16:37:58 +00:00
|
|
|
|
|
|
|
// https://stackoverflow.com/a/1489985/1711232
|
|
|
|
#define PASTER(x, y) x##_##y
|
|
|
|
#define EVALUATOR(x, y) PASTER(x, y)
|
|
|
|
#define NAMESPACE(fun) EVALUATOR(PQCLEAN_NAMESPACE, fun)
|
|
|
|
|
|
|
|
#define crypto_sign_keypair NAMESPACE(crypto_sign_keypair)
|
|
|
|
#define crypto_sign NAMESPACE(crypto_sign)
|
|
|
|
#define crypto_sign_open NAMESPACE(crypto_sign_open)
|
|
|
|
|
2019-02-13 16:45:09 +00:00
|
|
|
#define RETURNS_ZERO(f) \
|
|
|
|
if ((f) != 0) { \
|
|
|
|
puts("(f) returned non-zero returncode"); \
|
|
|
|
return -1; \
|
2019-01-23 12:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 09:15:18 +00:00
|
|
|
static int test_sign(void) {
|
2019-01-16 10:02:32 +00:00
|
|
|
unsigned char pk[CRYPTO_PUBLICKEYBYTES + 16];
|
|
|
|
unsigned char sk[CRYPTO_SECRETKEYBYTES + 16];
|
|
|
|
unsigned char sm[MLEN + CRYPTO_BYTES + 16];
|
|
|
|
unsigned char m[MLEN + 16];
|
|
|
|
|
|
|
|
unsigned long long mlen;
|
|
|
|
unsigned long long smlen;
|
2019-01-23 12:09:18 +00:00
|
|
|
int returncode;
|
2019-01-16 10:02:32 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
write_canary(pk);
|
|
|
|
write_canary(pk + sizeof(pk) - 8);
|
|
|
|
write_canary(sk);
|
|
|
|
write_canary(sk + sizeof(sk) - 8);
|
|
|
|
write_canary(sm);
|
|
|
|
write_canary(sm + sizeof(sm) - 8);
|
|
|
|
write_canary(m);
|
|
|
|
write_canary(m + sizeof(m) - 8);
|
|
|
|
|
|
|
|
for (i = 0; i < NTESTS; i++) {
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_sign_keypair(pk + 8, sk + 8));
|
2019-01-16 10:02:32 +00:00
|
|
|
|
|
|
|
randombytes(m + 8, MLEN);
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_sign(sm + 8, &smlen, m + 8, MLEN, sk + 8));
|
2019-01-16 10:02:32 +00:00
|
|
|
|
|
|
|
// By relying on m == sm we prevent having to allocate CRYPTO_BYTES
|
|
|
|
// twice
|
2019-02-11 09:05:54 +00:00
|
|
|
if ((returncode =
|
|
|
|
crypto_sign_open(sm + 8, &mlen, sm + 8, smlen, pk + 8)) != 0) {
|
2019-01-16 10:02:32 +00:00
|
|
|
printf("ERROR Signature did not verify correctly!\n");
|
2019-01-23 12:09:18 +00:00
|
|
|
if (returncode > 0) {
|
|
|
|
puts("ERROR return code should be < 0 on failure");
|
|
|
|
}
|
2019-01-16 12:02:35 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (check_canary(pk) || check_canary(pk + sizeof(pk) - 8) ||
|
|
|
|
check_canary(sk) || check_canary(sk + sizeof(sk) - 8) ||
|
|
|
|
check_canary(sm) || check_canary(sm + sizeof(sm) - 8) ||
|
|
|
|
check_canary(m) || check_canary(m + sizeof(m) - 8)) {
|
2019-01-16 10:02:32 +00:00
|
|
|
printf("ERROR canary overwritten\n");
|
2019-01-16 12:02:35 +00:00
|
|
|
return 1;
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-16 09:15:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
return 0;
|
2019-01-16 09:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int test_wrong_pk(void) {
|
2019-01-16 10:02:32 +00:00
|
|
|
unsigned char pk[CRYPTO_PUBLICKEYBYTES];
|
|
|
|
unsigned char pk2[CRYPTO_PUBLICKEYBYTES];
|
|
|
|
unsigned char sk[CRYPTO_SECRETKEYBYTES];
|
|
|
|
unsigned char sm[MLEN + CRYPTO_BYTES];
|
|
|
|
unsigned char m[MLEN];
|
2019-01-16 09:15:18 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
unsigned long long mlen;
|
|
|
|
unsigned long long smlen;
|
2019-01-16 09:15:18 +00:00
|
|
|
|
2019-01-23 12:09:18 +00:00
|
|
|
int returncode;
|
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
int i;
|
2019-01-16 09:15:18 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
for (i = 0; i < NTESTS; i++) {
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_sign_keypair(pk2, sk));
|
2019-01-16 09:15:18 +00:00
|
|
|
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_sign_keypair(pk, sk));
|
2019-01-16 09:15:18 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
randombytes(m, MLEN);
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_sign(sm, &smlen, m, MLEN, sk));
|
2019-01-16 09:15:18 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
// By relying on m == sm we prevent having to allocate CRYPTO_BYTES
|
|
|
|
// twice
|
2019-02-27 16:08:59 +00:00
|
|
|
returncode = crypto_sign_open(sm, &mlen, sm, smlen, pk2);
|
|
|
|
if (!returncode) {
|
|
|
|
printf("ERROR Signature did verify correctly under wrong public key!\n");
|
2019-01-23 12:09:18 +00:00
|
|
|
if (returncode > 0) {
|
|
|
|
puts("ERROR return code should be < 0");
|
|
|
|
}
|
2019-01-16 12:02:35 +00:00
|
|
|
return 1;
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-16 09:15:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
return 0;
|
2019-01-16 09:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2019-01-16 12:02:35 +00:00
|
|
|
int result = 0;
|
|
|
|
result += test_sign();
|
|
|
|
result += test_wrong_pk();
|
2019-01-16 09:15:18 +00:00
|
|
|
|
2019-01-16 12:02:35 +00:00
|
|
|
return result;
|
2019-01-16 09:15:18 +00:00
|
|
|
}
|