1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-12-04 21:34:01 +00:00
pqcrypto/test/crypto_sign/functest.c

290 lines
8.9 KiB
C
Raw Normal View History

#include "api.h"
#include "randombytes.h"
#include <stddef.h>
#include <stdint.h>
2019-01-16 09:15:18 +00:00
#include <stdio.h>
#include <stdlib.h>
2019-01-16 09:15:18 +00:00
#include <string.h>
2019-04-18 10:21:30 +01:00
#ifndef NTESTS
#define NTESTS 5
2019-04-18 10:21:30 +01:00
#endif
#define MLEN 1024
2019-01-16 09:15:18 +00:00
const uint8_t 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(uint8_t *d) {
for (size_t i = 0; i < 8; i++) {
d[i] = canary[i];
}
2019-01-16 09:15:18 +00:00
}
static int check_canary(const uint8_t *d) {
for (size_t 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
/** Safe malloc */
inline static void* malloc_s(size_t size) {
void *ptr = malloc(size);
if (ptr == NULL) {
perror("Malloc failed!");
exit(1);
}
return ptr;
}
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)
2019-03-19 13:28:15 +00:00
#define CRYPTO_PUBLICKEYBYTES NAMESPACE(CRYPTO_PUBLICKEYBYTES)
#define CRYPTO_SECRETKEYBYTES NAMESPACE(CRYPTO_SECRETKEYBYTES)
#define CRYPTO_BYTES NAMESPACE(CRYPTO_BYTES)
#define CRYPTO_ALGNAME NAMESPACE(CRYPTO_ALGNAME)
2019-01-22 16:37:58 +00:00
#define crypto_sign_keypair NAMESPACE(crypto_sign_keypair)
#define crypto_sign NAMESPACE(crypto_sign)
#define crypto_sign_open NAMESPACE(crypto_sign_open)
#define crypto_sign_signature NAMESPACE(crypto_sign_signature)
#define crypto_sign_verify NAMESPACE(crypto_sign_verify)
2019-01-22 16:37:58 +00:00
#define RETURNS_ZERO(f) \
if ((f) != 0) { \
puts("(f) returned non-zero returncode"); \
res = 1; \
goto end; \
2019-01-23 12:09:18 +00:00
}
2019-03-19 15:20:59 +00:00
// https://stackoverflow.com/a/55243651/248065
#define MY_TRUTHY_VALUE_X 1
#define CAT(x,y) CAT_(x,y)
#define CAT_(x,y) x##y
#define HAS_NAMESPACE(x) CAT(CAT(MY_TRUTHY_VALUE_,CAT(PQCLEAN_NAMESPACE,CAT(_,x))),X)
#if !HAS_NAMESPACE(API_H)
#error "namespace not properly defined for header guard"
#endif
2019-01-16 09:15:18 +00:00
static int test_sign(void) {
2019-02-27 15:28:20 +00:00
/*
* This is most likely going to be aligned by the compiler.
* 16 extra bytes for canary
* 1 extra byte for unalignment
*/
uint8_t *pk_aligned = malloc_s(CRYPTO_PUBLICKEYBYTES + 16 + 1);
uint8_t *sk_aligned = malloc_s(CRYPTO_SECRETKEYBYTES + 16 + 1);
uint8_t *sm_aligned = malloc_s(MLEN + CRYPTO_BYTES + 16 + 1);
uint8_t *m_aligned = malloc_s(MLEN + 16 + 1);
2019-02-27 15:28:20 +00:00
/*
* Make sure all pointers are odd.
* This ensures that the implementation does not assume anything about the
* data alignment. For example this would catch if an implementation
* directly uses these pointers to load into vector registers using movdqa.
*/
uint8_t *pk = (uint8_t *) ((uintptr_t) pk_aligned|(uintptr_t) 1);
uint8_t *sk = (uint8_t *) ((uintptr_t) sk_aligned|(uintptr_t) 1);
uint8_t *sm = (uint8_t *) ((uintptr_t) sm_aligned|(uintptr_t) 1);
uint8_t *m = (uint8_t *) ((uintptr_t) m_aligned|(uintptr_t) 1);
2019-01-16 10:02:32 +00:00
size_t mlen;
size_t smlen;
2019-01-23 12:09:18 +00:00
int returncode;
int res = 0;
2019-01-16 10:02:32 +00:00
int i;
2019-02-27 15:28:20 +00:00
/*
* Write 8 byte canary before and after the actual memory regions.
* This is used to validate that the implementation does not assume
* anything about the placement of data in memory
* (e.g., assuming that the pk is always behind the sk)
*/
2019-01-16 10:02:32 +00:00
write_canary(pk);
2019-02-27 15:28:20 +00:00
write_canary(pk + CRYPTO_PUBLICKEYBYTES + 8);
2019-01-16 10:02:32 +00:00
write_canary(sk);
2019-02-27 15:28:20 +00:00
write_canary(sk + CRYPTO_SECRETKEYBYTES + 8);
2019-01-16 10:02:32 +00:00
write_canary(sm);
2019-02-27 15:28:20 +00:00
write_canary(sm + MLEN + CRYPTO_BYTES + 8);
2019-01-16 10:02:32 +00:00
write_canary(m);
2019-02-27 15:28:20 +00:00
write_canary(m + MLEN + 8);
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(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
if ((returncode =
crypto_sign_open(sm + 8, &mlen, sm + 8, smlen, pk + 8)) != 0) {
2019-03-04 14:12:38 +00:00
fprintf(stderr, "ERROR Signature did not verify correctly!\n");
2019-01-23 12:09:18 +00:00
if (returncode > 0) {
2019-03-04 14:12:38 +00:00
fprintf(stderr, "ERROR return code should be < 0 on failure");
2019-01-23 12:09:18 +00:00
}
res = 1;
goto end;
2019-01-16 12:02:35 +00:00
}
2019-02-27 15:28:20 +00:00
// Validate that the implementation did not touch the canary
if (check_canary(pk) || check_canary(pk + CRYPTO_PUBLICKEYBYTES + 8) ||
check_canary(sk) || check_canary(sk + CRYPTO_SECRETKEYBYTES + 8) ||
check_canary(sm) || check_canary(sm + MLEN + CRYPTO_BYTES + 8) ||
check_canary(m) || check_canary(m + MLEN + 8)) {
2019-03-04 14:12:38 +00:00
fprintf(stderr, "ERROR canary overwritten\n");
res = 1;
goto end;
2019-01-16 10:02:32 +00:00
}
2019-01-16 09:15:18 +00:00
}
end:
free(pk_aligned);
free(sk_aligned);
free(sm_aligned);
free(m_aligned);
2019-01-16 09:15:18 +00:00
return res;
2019-01-16 09:15:18 +00:00
}
static int test_sign_detached(void) {
/*
* This is most likely going to be aligned by the compiler.
* 16 extra bytes for canary
* 1 extra byte for unalignment
*/
uint8_t *pk_aligned = malloc_s(CRYPTO_PUBLICKEYBYTES + 16 + 1);
uint8_t *sk_aligned = malloc_s(CRYPTO_SECRETKEYBYTES + 16 + 1);
uint8_t *sig_aligned = malloc_s(CRYPTO_BYTES + 16 + 1);
uint8_t *m_aligned = malloc_s(MLEN + 16 + 1);
/*
* Make sure all pointers are odd.
* This ensures that the implementation does not assume anything about the
* data alignment. For example this would catch if an implementation
* directly uses these pointers to load into vector registers using movdqa.
*/
uint8_t *pk = (uint8_t *) ((uintptr_t) pk_aligned|(uintptr_t) 1);
uint8_t *sk = (uint8_t *) ((uintptr_t) sk_aligned|(uintptr_t) 1);
uint8_t *sig = (uint8_t *) ((uintptr_t) sig_aligned|(uintptr_t) 1);
uint8_t *m = (uint8_t *) ((uintptr_t) m_aligned|(uintptr_t) 1);
size_t siglen;
int returncode;
int res = 0;
int i;
/*
* Write 8 byte canary before and after the actual memory regions.
* This is used to validate that the implementation does not assume
* anything about the placement of data in memory
* (e.g., assuming that the pk is always behind the sk)
*/
write_canary(pk);
write_canary(pk + CRYPTO_PUBLICKEYBYTES + 8);
write_canary(sk);
write_canary(sk + CRYPTO_SECRETKEYBYTES + 8);
write_canary(sig);
write_canary(sig + CRYPTO_BYTES + 8);
write_canary(m);
write_canary(m + MLEN + 8);
for (i = 0; i < NTESTS; i++) {
RETURNS_ZERO(crypto_sign_keypair(pk + 8, sk + 8));
randombytes(m + 8, MLEN);
RETURNS_ZERO(crypto_sign_signature(sig + 8, &siglen, m + 8, MLEN, sk + 8));
if ((returncode =
crypto_sign_verify(sig + 8, siglen, m + 8, MLEN, pk + 8)) != 0) {
fprintf(stderr, "ERROR Signature did not verify correctly!\n");
if (returncode > 0) {
fprintf(stderr, "ERROR return code should be < 0 on failure");
}
res = 1;
goto end;
}
// Validate that the implementation did not touch the canary
if (check_canary(pk) || check_canary(pk + CRYPTO_PUBLICKEYBYTES + 8) ||
check_canary(sk) || check_canary(sk + CRYPTO_SECRETKEYBYTES + 8) ||
check_canary(sig) || check_canary(sig + CRYPTO_BYTES + 8) ||
check_canary(m) || check_canary(m + MLEN + 8)) {
fprintf(stderr, "ERROR canary overwritten\n");
res = 1;
goto end;
}
}
end:
free(pk_aligned);
free(sk_aligned);
free(sig_aligned);
free(m_aligned);
return res;
}
2019-01-16 09:15:18 +00:00
static int test_wrong_pk(void) {
uint8_t *pk = malloc_s(CRYPTO_PUBLICKEYBYTES);
uint8_t *pk2 = malloc_s(CRYPTO_PUBLICKEYBYTES);
uint8_t *sk = malloc_s(CRYPTO_SECRETKEYBYTES);
uint8_t *sm = malloc_s(MLEN + CRYPTO_BYTES);
uint8_t *m = malloc_s(MLEN);
size_t mlen;
size_t smlen;
2019-01-16 09:15:18 +00:00
int returncode, res = 0;
2019-01-23 12:09:18 +00:00
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
returncode = crypto_sign_open(sm, &mlen, sm, smlen, pk2);
if (!returncode) {
2019-03-04 14:12:38 +00:00
fprintf(stderr, "ERROR Signature did verify correctly under wrong public key!\n");
2019-01-23 12:09:18 +00:00
if (returncode > 0) {
2019-03-04 14:12:38 +00:00
fprintf(stderr, "ERROR return code should be < 0");
2019-01-23 12:09:18 +00:00
}
res = 1;
goto end;
2019-01-16 10:02:32 +00:00
}
2019-01-16 09:15:18 +00:00
}
end:
free(pk);
free(pk2);
free(sk);
free(sm);
free(m);
return res;
2019-01-16 09:15:18 +00:00
}
int main(void) {
2019-03-19 15:20:59 +00:00
// check if CRYPTO_ALGNAME is printable
2019-03-19 13:28:15 +00:00
puts(CRYPTO_ALGNAME);
2019-01-16 12:02:35 +00:00
int result = 0;
result += test_sign();
result += test_sign_detached();
2019-01-16 12:02:35 +00:00
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
}