Nagram/TMessagesProj/jni/boringssl/third_party/sike/sike_test.cc
2019-12-31 16:08:08 +03:00

252 lines
10 KiB
C++

/* 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 <gtest/gtest.h>
#include <stdint.h>
#include "sike.h"
#include "fpx.h"
#include "../../crypto/test/abi_test.h"
TEST(SIKE, RoundTrip) {
uint8_t sk[SIKE_PRV_BYTESZ] = {0};
uint8_t pk[SIKE_PUB_BYTESZ] = {0};
uint8_t ct[SIKE_CT_BYTESZ] = {0};
uint8_t ss_enc[SIKE_SS_BYTESZ] = {0};
uint8_t ss_dec[SIKE_SS_BYTESZ] = {0};
for (size_t i = 0; i < 30; i++) {
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, SIKE_SS_BYTESZ), 0);
}
}
TEST(SIKE, Decapsulation) {
const uint8_t sk[SIKE_PRV_BYTESZ] = {
0xB1, 0xFD, 0x34, 0x42, 0xDB, 0x02, 0xBC, 0x9D, 0x4C, 0xD0,
0x72, 0x34, 0x4D, 0xBD, 0x06, 0xDF, 0x1C, 0x7D, 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_dec, ss_exp, sizeof(ss_exp)), 0);
}
// SIKE_encaps and SIKE_keypair doesn't return zeros.
TEST(SIKE, NonZero) {
uint8_t sk[SIKE_PRV_BYTESZ] = {0};
uint8_t pk[SIKE_PUB_BYTESZ] = {0};
uint8_t ct[SIKE_CT_BYTESZ] = {0};
uint8_t ss[SIKE_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[SIKE_PRV_BYTESZ] = {0};
uint8_t pk[SIKE_PUB_BYTESZ] = {0};
uint8_t ct[SIKE_CT_BYTESZ] = {0};
uint8_t ss_enc[SIKE_SS_BYTESZ] = {0};
uint8_t ss_dec[SIKE_SS_BYTESZ] = {0};
EXPECT_EQ(SIKE_keypair(sk, pk), 1);
SIKE_encaps(ss_enc, ct, pk);
// Change cipertext
uint8_t ct_tmp[SIKE_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, SIKE_SS_BYTESZ), 0);
// Change secret key
uint8_t sk_tmp[SIKE_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, SIKE_SS_BYTESZ), 0);
// Change public key
uint8_t pk_tmp[SIKE_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, SIKE_SS_BYTESZ), 0);
}
TEST(SIKE, Unaligned) {
alignas(4) uint8_t priv[SIKE_PRV_BYTESZ + 1];
alignas(4) uint8_t pub[SIKE_PUB_BYTESZ + 1];
alignas(4) uint8_t shared_key1[SIKE_SS_BYTESZ + 1];
alignas(4) uint8_t ciphertext[SIKE_CT_BYTESZ + 1];
alignas(4) uint8_t shared_key2[SIKE_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, SIKE_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);
}
// Additional tests for checking if assembly implementation
// of MUL and REDC handles carry chains correctly.
TEST(SIKE, CarryChains) {
// Expected results
const dfelm_t exp_mul = {
0x0000000000000001, 0x0000000000000000, 0x0000000000000000,
0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
0x0000000000000000, 0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
};
const felm_t exp_redc = {
0x93AA0C8C2D3235BE, 0xA8CD35DDDE399B46, 0xB9BBA5469509CA65,
0x6B2FB3A5A2FB86E4, 0x585591BA6DBE862C, 0xD92D3FF5FE0938F2,
0x0001E1F0EE75A1E1
};
// Input
dfelm_t in14 = {
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
};
felm_t in7 = {
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF
};
dfelm_t res;
sike_mpmul(in7, in7, res);
EXPECT_EQ(memcmp(exp_mul, res, sizeof(exp_mul)), 0);
// modifies in14 and in7
sike_fprdc(in14, in7);
EXPECT_EQ(memcmp(exp_redc, in7, sizeof(exp_redc)), 0);
}
#endif // SUPPORTS_ABI_TEST && (X86_64 || AARCH64)