2016-04-18 19:30:19 +01:00
|
|
|
/* Copyright (c) 2016, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/crypto.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
|
2016-05-19 18:30:52 +01:00
|
|
|
#define NTESTS 1
|
2016-04-18 19:30:19 +01:00
|
|
|
|
2016-04-27 00:25:31 +01:00
|
|
|
static int test_keys(void) {
|
2016-04-18 19:30:19 +01:00
|
|
|
NEWHOPE_POLY *sk = NEWHOPE_POLY_new();
|
2016-05-19 18:30:52 +01:00
|
|
|
uint8_t offer_key[SHA256_DIGEST_LENGTH], accept_key[SHA256_DIGEST_LENGTH];
|
|
|
|
uint8_t offermsg[NEWHOPE_OFFERMSG_LENGTH];
|
|
|
|
uint8_t acceptmsg[NEWHOPE_ACCEPTMSG_LENGTH];
|
2016-04-18 19:30:19 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NTESTS; i++) {
|
|
|
|
/* Alice generates a public key */
|
2016-05-19 18:30:52 +01:00
|
|
|
NEWHOPE_offer(offermsg, sk);
|
2016-04-18 19:30:19 +01:00
|
|
|
|
|
|
|
/* Bob derives a secret key and creates a response */
|
2016-05-19 18:30:52 +01:00
|
|
|
if (!NEWHOPE_accept(accept_key, acceptmsg, offermsg, sizeof(offermsg))) {
|
|
|
|
fprintf(stderr, "ERROR accept key exchange failed\n");
|
2016-04-18 19:30:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Alice uses Bob's response to get her secret key */
|
2016-05-19 18:30:52 +01:00
|
|
|
if (!NEWHOPE_finish(offer_key, sk, acceptmsg, sizeof(acceptmsg))) {
|
|
|
|
fprintf(stderr, "ERROR finish key exchange failed\n");
|
2016-04-18 19:30:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-19 18:30:52 +01:00
|
|
|
if (memcmp(offer_key, accept_key, SHA256_DIGEST_LENGTH) != 0) {
|
2016-04-18 19:30:19 +01:00
|
|
|
fprintf(stderr, "ERROR keys did not agree\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NEWHOPE_POLY_free(sk);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:25:31 +01:00
|
|
|
static int test_invalid_sk_a(void) {
|
2016-04-18 19:30:19 +01:00
|
|
|
NEWHOPE_POLY *sk = NEWHOPE_POLY_new();
|
2016-05-19 18:30:52 +01:00
|
|
|
uint8_t offer_key[SHA256_DIGEST_LENGTH], accept_key[SHA256_DIGEST_LENGTH];
|
|
|
|
uint8_t offermsg[NEWHOPE_OFFERMSG_LENGTH];
|
|
|
|
uint8_t acceptmsg[NEWHOPE_ACCEPTMSG_LENGTH];
|
2016-04-18 19:30:19 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NTESTS; i++) {
|
|
|
|
/* Alice generates a public key */
|
2016-05-19 18:30:52 +01:00
|
|
|
NEWHOPE_offer(offermsg, sk);
|
2016-04-18 19:30:19 +01:00
|
|
|
|
|
|
|
/* Bob derives a secret key and creates a response */
|
2016-05-19 18:30:52 +01:00
|
|
|
if (!NEWHOPE_accept(accept_key, acceptmsg, offermsg, sizeof(offermsg))) {
|
|
|
|
fprintf(stderr, "ERROR accept key exchange failed\n");
|
2016-04-18 19:30:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:10:53 +01:00
|
|
|
/* Corrupt the secret key */
|
2016-05-19 18:30:52 +01:00
|
|
|
NEWHOPE_offer(offermsg /* not used below */, sk);
|
2016-04-18 19:30:19 +01:00
|
|
|
|
|
|
|
/* Alice uses Bob's response to get her secret key */
|
2016-05-19 18:30:52 +01:00
|
|
|
if (!NEWHOPE_finish(offer_key, sk, acceptmsg, sizeof(acceptmsg))) {
|
|
|
|
fprintf(stderr, "ERROR finish key exchange failed\n");
|
2016-04-18 19:30:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-19 18:30:52 +01:00
|
|
|
if (memcmp(offer_key, accept_key, SHA256_DIGEST_LENGTH) == 0) {
|
2016-04-18 19:30:19 +01:00
|
|
|
fprintf(stderr, "ERROR invalid sk_a\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NEWHOPE_POLY_free(sk);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:25:31 +01:00
|
|
|
static int test_invalid_ciphertext(void) {
|
2016-04-18 19:30:19 +01:00
|
|
|
NEWHOPE_POLY *sk = NEWHOPE_POLY_new();
|
2016-05-19 18:30:52 +01:00
|
|
|
uint8_t offer_key[SHA256_DIGEST_LENGTH], accept_key[SHA256_DIGEST_LENGTH];
|
|
|
|
uint8_t offermsg[NEWHOPE_OFFERMSG_LENGTH];
|
|
|
|
uint8_t acceptmsg[NEWHOPE_ACCEPTMSG_LENGTH];
|
2016-04-18 19:30:19 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
/* Alice generates a public key */
|
2016-05-19 18:30:52 +01:00
|
|
|
NEWHOPE_offer(offermsg, sk);
|
2016-04-18 19:30:19 +01:00
|
|
|
|
|
|
|
/* Bob derives a secret key and creates a response */
|
2016-05-19 18:30:52 +01:00
|
|
|
if (!NEWHOPE_accept(accept_key, acceptmsg, offermsg, sizeof(offermsg))) {
|
|
|
|
fprintf(stderr, "ERROR accept key exchange failed\n");
|
2016-04-18 19:30:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change some byte in the "ciphertext" */
|
2016-05-19 18:30:52 +01:00
|
|
|
acceptmsg[42] ^= 1;
|
2016-04-18 19:30:19 +01:00
|
|
|
|
|
|
|
/* Alice uses Bob's response to get her secret key */
|
2016-05-19 18:30:52 +01:00
|
|
|
if (!NEWHOPE_finish(offer_key, sk, acceptmsg, sizeof(acceptmsg))) {
|
|
|
|
fprintf(stderr, "ERROR finish key exchange failed\n");
|
2016-04-18 19:30:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-19 18:30:52 +01:00
|
|
|
if (!memcmp(offer_key, accept_key, SHA256_DIGEST_LENGTH)) {
|
|
|
|
fprintf(stderr, "ERROR invalid acceptmsg\n");
|
2016-04-18 19:30:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NEWHOPE_POLY_free(sk);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-27 00:25:31 +01:00
|
|
|
int main(void) {
|
2016-04-18 19:30:19 +01:00
|
|
|
if (!test_keys() || !test_invalid_sk_a() || !test_invalid_ciphertext()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("PASS\n");
|
|
|
|
return 0;
|
|
|
|
}
|