blob: 1277e09af6c76a430b68838d0d5ab0ed108f38f6 [file] [log] [blame]
/* Copyright (c) 2018, 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 <stdint.h>
#include <gtest/gtest.h>
#include "../../crypto/test/abi_test.h"
#include "sike.h"
#include "fpx.h"
TEST(SIKE, RoundTrip) {
uint8_t sk[SIKEp503_PRV_BYTESZ] = {0};
uint8_t pk[SIKEp503_PUB_BYTESZ] = {0};
uint8_t ct[SIKEp503_CT_BYTESZ] = {0};
uint8_t ss_enc[SIKEp503_SS_BYTESZ] = {0};
uint8_t ss_dec[SIKEp503_SS_BYTESZ] = {0};
EXPECT_EQ(SIKE_keypair(sk, pk), 1);
SIKE_encaps(ss_enc, ct, pk);
SIKE_decaps(ss_dec, ct, pk, sk);
EXPECT_EQ(memcmp(ss_enc, ss_dec, SIKEp503_SS_BYTESZ), 0);
}
TEST(SIKE, Decapsulation) {
const uint8_t sk[SIKEp503_PRV_BYTESZ] = {
0xDB, 0xAF, 0x2C, 0x89, 0xCA, 0x5A, 0xD4, 0x9D, 0x4F, 0x13,
0x40, 0xDF, 0x2D, 0xB1, 0x5F, 0x4C, 0x91, 0xA7, 0x1F, 0x0B,
0x29, 0x15, 0x01, 0x59,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30, 0xFD, 0x6B, 0xD9, 0xB8, 0x1D, 0x72, 0x8A,
0x56, 0xCC, 0x2F, 0x82, 0xE4, 0x46, 0x4D, 0x75, 0xD8, 0x92,
0xE6, 0x9C, 0xCC, 0xD2, 0xCD, 0x35, 0xE4, 0xFC, 0x2A, 0x85,
0x6B, 0xA9, 0xB2, 0x27, 0xC9, 0xA1, 0xFF, 0xB3, 0x96, 0x3E,
0x59, 0xF6, 0x4C, 0x66, 0x56, 0x2E, 0xF5, 0x1B, 0x97, 0x32,
0xB0, 0x71, 0x5A, 0x9C, 0x50, 0x4B, 0x6F, 0xC4, 0xCA, 0x94,
0x75, 0x37, 0x46, 0x10, 0x12, 0x2F, 0x4F, 0xA3, 0x82, 0xCD,
0xBD, 0x7C};
const uint8_t ss_exp[SIKEp503_SS_BYTESZ] = {
0x74, 0x3D, 0x25, 0x36, 0x00, 0x24, 0x63, 0x1A, 0x39, 0x1A,
0xB4, 0xAD, 0x01, 0x17, 0x78, 0xE9};
uint8_t ss_dec[SIKEp503_SS_BYTESZ] = {0};
SIKE_decaps(ss_dec, ct_exp, pk, sk);
EXPECT_EQ(memcmp(ss_dec, ss_exp, sizeof(ss_exp)), 0);
}
// SIKE_encaps and SIKE_keypair doesn't return zeros.
TEST(SIKE, NonZero) {
uint8_t sk[SIKEp503_PRV_BYTESZ] = {0};
uint8_t pk[SIKEp503_PUB_BYTESZ] = {0};
uint8_t ct[SIKEp503_CT_BYTESZ] = {0};
uint8_t ss[SIKEp503_SS_BYTESZ] = {0};
// Check secret and public key returned by SIKE_keypair
EXPECT_EQ(SIKE_keypair(sk, pk), 1);
uint8_t tmp = 0;
for (size_t i=0; i<sizeof(sk); i++) tmp|=sk[i];
EXPECT_NE(tmp, 0);
tmp = 0;
for (size_t i=0; i<sizeof(pk); i++) tmp|=pk[i];
EXPECT_NE(tmp, 0);
// Check shared secret and ciphertext returned by SIKE_encaps
SIKE_encaps(ss, ct, pk);
tmp = 0;
for (size_t i=0; i<sizeof(ct); i++) tmp|=ct[i];
EXPECT_NE(tmp, 0);
tmp = 0;
for (size_t i=0; i<sizeof(ss); i++) tmp|=ss[i];
EXPECT_NE(tmp, 0);
}
TEST(SIKE, Negative) {
uint8_t sk[SIKEp503_PRV_BYTESZ] = {0};
uint8_t pk[SIKEp503_PUB_BYTESZ] = {0};
uint8_t ct[SIKEp503_CT_BYTESZ] = {0};
uint8_t ss_enc[SIKEp503_SS_BYTESZ] = {0};
uint8_t ss_dec[SIKEp503_SS_BYTESZ] = {0};
EXPECT_EQ(SIKE_keypair(sk, pk), 1);
SIKE_encaps(ss_enc, ct, pk);
// Change cipertext
uint8_t ct_tmp[SIKEp503_CT_BYTESZ] = {0};
memcpy(ct_tmp, ct, sizeof(ct));
ct_tmp[0] = ~ct_tmp[0];
SIKE_decaps(ss_dec, ct_tmp, pk, sk);
EXPECT_NE(memcmp(ss_enc, ss_dec, SIKEp503_SS_BYTESZ), 0);
// Change secret key
uint8_t sk_tmp[SIKEp503_PRV_BYTESZ] = {0};
memcpy(sk_tmp, sk, sizeof(sk));
sk_tmp[0] = ~sk_tmp[0];
SIKE_decaps(ss_dec, ct, pk, sk_tmp);
EXPECT_NE(memcmp(ss_enc, ss_dec, SIKEp503_SS_BYTESZ), 0);
// Change public key
uint8_t pk_tmp[SIKEp503_PUB_BYTESZ] = {0};
memcpy(pk_tmp, pk, sizeof(pk));
pk_tmp[0] = ~pk_tmp[0];
SIKE_decaps(ss_dec, ct, pk_tmp, sk);
EXPECT_NE(memcmp(ss_enc, ss_dec, SIKEp503_SS_BYTESZ), 0);
}
TEST(SIKE, Unaligned) {
alignas(4) uint8_t priv[SIKEp503_PRV_BYTESZ + 1];
alignas(4) uint8_t pub[SIKEp503_PUB_BYTESZ + 1];
alignas(4) uint8_t shared_key1[SIKEp503_SS_BYTESZ + 1];
alignas(4) uint8_t ciphertext[SIKEp503_CT_BYTESZ + 1];
alignas(4) uint8_t shared_key2[SIKEp503_SS_BYTESZ + 1];
ASSERT_TRUE(SIKE_keypair(priv + 1, pub + 1));
SIKE_encaps(shared_key1 + 1, ciphertext + 1, pub + 1);
SIKE_decaps(shared_key2 + 1, ciphertext + 1, pub + 1, priv + 1);
EXPECT_EQ(memcmp(shared_key1 + 1, shared_key2 + 1, SIKEp503_SS_BYTESZ), 0);
}
#if defined(SUPPORTS_ABI_TEST) && (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64))
TEST(SIKE, ABI) {
felm_t a, b, c;
dfelm_t d, e, f;
CHECK_ABI(sike_fpadd, a, b, c);
CHECK_ABI(sike_fpsub, a, b, c);
CHECK_ABI(sike_mpmul, a, b, d);
CHECK_ABI(sike_fprdc, d, a);
CHECK_ABI(sike_mpadd_asm, a, b, c);
CHECK_ABI(sike_mpsubx2_asm, d, e, f);
CHECK_ABI(sike_mpdblsubx2_asm, d, e, f);
}
#endif // SUPPORTS_ABI_TEST && (X86_64 || AARCH64)