2019-01-15 15:03:38 +00:00
|
|
|
#include "api.h"
|
|
|
|
#include "randombytes.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-01-15 15:50:07 +00:00
|
|
|
#define NTESTS 100
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
int test_keys() {
|
2019-01-15 15:03:38 +00:00
|
|
|
unsigned char key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES];
|
|
|
|
unsigned char pk[CRYPTO_PUBLICKEYBYTES];
|
|
|
|
unsigned char sendb[CRYPTO_CIPHERTEXTBYTES];
|
|
|
|
unsigned char sk_a[CRYPTO_SECRETKEYBYTES];
|
|
|
|
int i;
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
for (i = 0; i < NTESTS; i++) {
|
|
|
|
// Alice generates a public key
|
2019-01-15 15:03:38 +00:00
|
|
|
crypto_kem_keypair(pk, sk_a);
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
// Bob derives a secret key and creates a response
|
2019-01-15 15:03:38 +00:00
|
|
|
crypto_kem_enc(sendb, key_b, pk);
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
// Alice uses Bobs response to get her secret key
|
2019-01-15 15:03:38 +00:00
|
|
|
crypto_kem_dec(key_a, sendb, sk_a);
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
if (memcmp(key_a, key_b, CRYPTO_BYTES))
|
2019-01-15 15:03:38 +00:00
|
|
|
printf("ERROR keys\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
int test_invalid_sk_a() {
|
2019-01-15 15:03:38 +00:00
|
|
|
unsigned char sk_a[CRYPTO_SECRETKEYBYTES];
|
|
|
|
unsigned char key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES];
|
|
|
|
unsigned char pk[CRYPTO_PUBLICKEYBYTES];
|
|
|
|
unsigned char sendb[CRYPTO_CIPHERTEXTBYTES];
|
|
|
|
int i;
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
for (i = 0; i < NTESTS; i++) {
|
|
|
|
// Alice generates a public key
|
2019-01-15 15:03:38 +00:00
|
|
|
crypto_kem_keypair(pk, sk_a);
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
// Bob derives a secret key and creates a response
|
2019-01-15 15:03:38 +00:00
|
|
|
crypto_kem_enc(sendb, key_b, pk);
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
// Replace secret key with random values
|
2019-01-15 15:03:38 +00:00
|
|
|
randombytes(sk_a, CRYPTO_SECRETKEYBYTES);
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
// Alice uses Bobs response to get her secre key
|
2019-01-15 15:03:38 +00:00
|
|
|
crypto_kem_dec(key_a, sendb, sk_a);
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
if (!memcmp(key_a, key_b, CRYPTO_BYTES))
|
2019-01-15 15:03:38 +00:00
|
|
|
printf("ERROR invalid sk_a\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
int test_invalid_ciphertext() {
|
2019-01-15 15:03:38 +00:00
|
|
|
unsigned char sk_a[CRYPTO_SECRETKEYBYTES];
|
|
|
|
unsigned char key_a[CRYPTO_BYTES], key_b[CRYPTO_BYTES];
|
|
|
|
unsigned char pk[CRYPTO_PUBLICKEYBYTES];
|
|
|
|
unsigned char sendb[CRYPTO_CIPHERTEXTBYTES];
|
|
|
|
int i;
|
|
|
|
size_t pos;
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
for (i = 0; i < NTESTS; i++) {
|
2019-01-15 15:03:38 +00:00
|
|
|
randombytes((unsigned char *)&pos, sizeof(size_t));
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
// Alice generates a public key
|
2019-01-15 15:03:38 +00:00
|
|
|
crypto_kem_keypair(pk, sk_a);
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
// Bob derives a secret key and creates a response
|
2019-01-15 15:03:38 +00:00
|
|
|
crypto_kem_enc(sendb, key_b, pk);
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
// Change some byte in the ciphertext (i.e., encapsulated key)
|
2019-01-15 15:03:38 +00:00
|
|
|
sendb[pos % CRYPTO_CIPHERTEXTBYTES] ^= 23;
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
// Alice uses Bobs response to get her secre key
|
2019-01-15 15:03:38 +00:00
|
|
|
crypto_kem_dec(key_a, sendb, sk_a);
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
if (!memcmp(key_a, key_b, CRYPTO_BYTES))
|
2019-01-15 15:03:38 +00:00
|
|
|
printf("ERROR invalid ciphertext\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
int main(void) {
|
2019-01-15 15:03:38 +00:00
|
|
|
test_keys();
|
|
|
|
test_invalid_sk_a();
|
|
|
|
test_invalid_ciphertext();
|
|
|
|
|
2019-01-15 15:34:01 +00:00
|
|
|
printf("CRYPTO_SECRETKEYBYTES: %d\n", CRYPTO_SECRETKEYBYTES);
|
|
|
|
printf("CRYPTO_PUBLICKEYBYTES: %d\n", CRYPTO_PUBLICKEYBYTES);
|
|
|
|
printf("CRYPTO_CIPHERTEXTBYTES: %d\n", CRYPTO_CIPHERTEXTBYTES);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|