2019-03-05 13:14:47 +00:00
|
|
|
#include <stdint.h>
|
2019-01-15 15:03:38 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-05 13:14:47 +00:00
|
|
|
#include "api.h"
|
|
|
|
#include "randombytes.h"
|
|
|
|
|
2019-01-15 16:13:19 +00:00
|
|
|
#define NTESTS 10
|
|
|
|
|
2019-03-05 13:14:47 +00:00
|
|
|
const uint8_t canary[8] = {
|
2019-02-13 16:45:09 +00:00
|
|
|
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
|
|
|
|
};
|
2019-02-11 08:41:32 +00:00
|
|
|
|
2019-01-15 16:13:19 +00:00
|
|
|
/* allocate a bit more for all keys and messages and
|
|
|
|
* make sure it is not touched by the implementations.
|
|
|
|
*/
|
2019-03-05 13:14:47 +00:00
|
|
|
static void write_canary(uint8_t *d) {
|
2019-04-02 19:06:06 +01:00
|
|
|
for (size_t i = 0; i < 8; i++) {
|
2019-02-11 08:41:32 +00:00
|
|
|
d[i] = canary[i];
|
|
|
|
}
|
2019-01-15 16:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:14:47 +00:00
|
|
|
static int check_canary(const uint8_t *d) {
|
2019-04-02 19:06:06 +01:00
|
|
|
for (size_t i = 0; i < 8; i++) {
|
2019-03-05 13:14:47 +00:00
|
|
|
if (d[i] != canary[i]) {
|
2019-02-11 08:41:32 +00:00
|
|
|
return -1;
|
2019-03-05 13:14:47 +00:00
|
|
|
}
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-16 12:52:53 +00:00
|
|
|
return 0;
|
2019-01-15 16:13:19 +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)
|
|
|
|
|
2019-03-19 13:28:15 +00:00
|
|
|
#define CRYPTO_BYTES NAMESPACE(CRYPTO_BYTES)
|
|
|
|
#define CRYPTO_PUBLICKEYBYTES NAMESPACE(CRYPTO_PUBLICKEYBYTES)
|
|
|
|
#define CRYPTO_SECRETKEYBYTES NAMESPACE(CRYPTO_SECRETKEYBYTES)
|
|
|
|
#define CRYPTO_CIPHERTEXTBYTES NAMESPACE(CRYPTO_CIPHERTEXTBYTES)
|
|
|
|
#define CRYPTO_ALGNAME NAMESPACE(CRYPTO_ALGNAME)
|
|
|
|
|
2019-01-22 16:37:58 +00:00
|
|
|
#define crypto_kem_keypair NAMESPACE(crypto_kem_keypair)
|
|
|
|
#define crypto_kem_enc NAMESPACE(crypto_kem_enc)
|
|
|
|
#define crypto_kem_dec NAMESPACE(crypto_kem_dec)
|
|
|
|
|
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-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-03-19 14:03:24 +00:00
|
|
|
|
2019-01-15 16:13:19 +00:00
|
|
|
static int test_keys(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
|
|
|
|
*/
|
2019-03-19 14:03:24 +00:00
|
|
|
|
2019-03-05 13:14:47 +00:00
|
|
|
uint8_t key_a_aligned[CRYPTO_BYTES + 16 + 1];
|
|
|
|
uint8_t key_b_aligned[CRYPTO_BYTES + 16 + 1];
|
|
|
|
uint8_t pk_aligned[CRYPTO_PUBLICKEYBYTES + 16 + 1];
|
|
|
|
uint8_t sendb_aligned[CRYPTO_CIPHERTEXTBYTES + 16 + 1];
|
|
|
|
uint8_t sk_a_aligned[CRYPTO_SECRETKEYBYTES + 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.
|
|
|
|
*/
|
2019-03-05 13:14:47 +00:00
|
|
|
uint8_t *key_a = (uint8_t *) ((uintptr_t) key_a_aligned|(uintptr_t) 1);
|
|
|
|
uint8_t *key_b = (uint8_t *) ((uintptr_t) key_b_aligned|(uintptr_t) 1);
|
|
|
|
uint8_t *pk = (uint8_t *) ((uintptr_t) pk_aligned|(uintptr_t) 1);
|
|
|
|
uint8_t *sendb = (uint8_t *) ((uintptr_t) sendb_aligned|(uintptr_t) 1);
|
|
|
|
uint8_t *sk_a = (uint8_t *) ((uintptr_t) sk_a_aligned|(uintptr_t) 1);
|
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(key_a);
|
2019-02-27 15:28:20 +00:00
|
|
|
write_canary(key_a + CRYPTO_BYTES + 8);
|
2019-01-16 10:02:32 +00:00
|
|
|
write_canary(key_b);
|
2019-02-27 15:28:20 +00:00
|
|
|
write_canary(key_b + CRYPTO_BYTES + 8);
|
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(sendb);
|
2019-02-27 15:28:20 +00:00
|
|
|
write_canary(sendb + CRYPTO_CIPHERTEXTBYTES + 8);
|
2019-01-16 10:02:32 +00:00
|
|
|
write_canary(sk_a);
|
2019-02-27 15:28:20 +00:00
|
|
|
write_canary(sk_a + CRYPTO_SECRETKEYBYTES + 8);
|
2019-01-16 10:02:32 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NTESTS; i++) {
|
|
|
|
// Alice generates a public key
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_kem_keypair(pk + 8, sk_a + 8));
|
2019-01-16 10:02:32 +00:00
|
|
|
|
|
|
|
// Bob derives a secret key and creates a response
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_kem_enc(sendb + 8, key_b + 8, pk + 8));
|
2019-01-16 10:02:32 +00:00
|
|
|
|
|
|
|
// Alice uses Bobs response to get her secret key
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_kem_dec(key_a + 8, sendb + 8, sk_a + 8));
|
2019-01-16 10:02:32 +00:00
|
|
|
|
|
|
|
if (memcmp(key_a + 8, key_b + 8, CRYPTO_BYTES) != 0) {
|
|
|
|
printf("ERROR KEYS\n");
|
2019-01-23 12:09:18 +00:00
|
|
|
return -1;
|
2019-01-16 12:02:35 +00:00
|
|
|
}
|
2019-01-23 12:09:18 +00:00
|
|
|
|
2019-02-27 15:28:20 +00:00
|
|
|
// Validate that the implementation did not touch the canary
|
|
|
|
if (check_canary(key_a) || check_canary(key_a + CRYPTO_BYTES + 8) ||
|
|
|
|
check_canary(key_b) || check_canary(key_b + CRYPTO_BYTES + 8 ) ||
|
|
|
|
check_canary(pk) || check_canary(pk + CRYPTO_PUBLICKEYBYTES + 8 ) ||
|
|
|
|
check_canary(sendb) || check_canary(sendb + CRYPTO_CIPHERTEXTBYTES + 8 ) ||
|
|
|
|
check_canary(sk_a) || check_canary(sk_a + CRYPTO_SECRETKEYBYTES + 8 )) {
|
2019-01-16 10:02:32 +00:00
|
|
|
printf("ERROR canary overwritten\n");
|
2019-01-23 12:09:18 +00:00
|
|
|
return -1;
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 16:13:19 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
return 0;
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 16:13:19 +00:00
|
|
|
static int test_invalid_sk_a(void) {
|
2019-03-05 13:14:47 +00:00
|
|
|
uint8_t sk_a[CRYPTO_SECRETKEYBYTES];
|
|
|
|
uint8_t key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES];
|
|
|
|
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
|
|
|
|
uint8_t sendb[CRYPTO_CIPHERTEXTBYTES];
|
2019-01-16 10:02:32 +00:00
|
|
|
int i;
|
2019-01-23 12:09:18 +00:00
|
|
|
int returncode;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
for (i = 0; i < NTESTS; i++) {
|
|
|
|
// Alice generates a public key
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_kem_keypair(pk, sk_a));
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
// Bob derives a secret key and creates a response
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_kem_enc(sendb, key_b, pk));
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
// Replace secret key with random values
|
|
|
|
randombytes(sk_a, CRYPTO_SECRETKEYBYTES);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
// Alice uses Bobs response to get her secret key
|
2019-01-29 16:32:30 +00:00
|
|
|
if ((returncode = crypto_kem_dec(key_a, sendb, sk_a)) > 0) {
|
2019-02-11 09:05:54 +00:00
|
|
|
printf("ERROR failing crypto_kem_dec returned %d instead of "
|
|
|
|
"negative or zero code\n",
|
|
|
|
returncode);
|
2019-01-23 12:09:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
if (!memcmp(key_a, key_b, CRYPTO_BYTES)) {
|
|
|
|
printf("ERROR invalid sk_a\n");
|
2019-04-04 17:39:47 +01:00
|
|
|
return -1;
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 16:13:19 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
return 0;
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 16:13:19 +00:00
|
|
|
static int test_invalid_ciphertext(void) {
|
2019-03-05 13:14:47 +00:00
|
|
|
uint8_t sk_a[CRYPTO_SECRETKEYBYTES];
|
|
|
|
uint8_t key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES];
|
|
|
|
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
|
|
|
|
uint8_t sendb[CRYPTO_CIPHERTEXTBYTES];
|
2019-01-16 10:02:32 +00:00
|
|
|
int i;
|
|
|
|
size_t pos;
|
2019-01-23 12:09:18 +00:00
|
|
|
int returncode;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
for (i = 0; i < NTESTS; i++) {
|
2019-03-05 13:14:47 +00:00
|
|
|
randombytes((uint8_t *)&pos, sizeof(size_t));
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
// Alice generates a public key
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_kem_keypair(pk, sk_a));
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
// Bob derives a secret key and creates a response
|
2019-01-23 12:09:18 +00:00
|
|
|
RETURNS_ZERO(crypto_kem_enc(sendb, key_b, pk));
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
// Change some byte in the ciphertext (i.e., encapsulated key)
|
|
|
|
sendb[pos % CRYPTO_CIPHERTEXTBYTES] ^= 23;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
// Alice uses Bobs response to get her secret key
|
2019-01-29 15:05:44 +00:00
|
|
|
if ((returncode = crypto_kem_dec(key_a, sendb, sk_a)) > 0) {
|
2019-02-11 09:05:54 +00:00
|
|
|
printf("ERROR crypto_kem_dec should either fail (negative "
|
|
|
|
"returncode) or succeed (return 0) but returned %d\n",
|
|
|
|
returncode);
|
2019-01-23 12:09:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
if (!memcmp(key_a, key_b, CRYPTO_BYTES)) {
|
|
|
|
printf("ERROR invalid ciphertext\n");
|
2019-04-04 17:39:47 +01:00
|
|
|
return -1;
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 16:13:19 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
return 0;
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 15:34:01 +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-03-19 14:03:24 +00:00
|
|
|
|
2019-01-16 12:02:35 +00:00
|
|
|
int result = 0;
|
|
|
|
result += test_keys();
|
|
|
|
result += test_invalid_sk_a();
|
|
|
|
result += test_invalid_ciphertext();
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 12:02:35 +00:00
|
|
|
if (result != 0) {
|
|
|
|
puts("Errors occurred");
|
|
|
|
}
|
|
|
|
return result;
|
2019-01-15 16:13:19 +00:00
|
|
|
}
|