Skip to content

Commit

Permalink
Add test to decapsulation. Fix bug in field element comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklonga committed Dec 7, 2020
1 parent 23cb226 commit 840400c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/fpx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ int8_t cmp_f2elm(const f2elm_t x, const f2elm_t y)
{ // Comparison of two GF(p^2) elements in constant time.
// Is x != y? return -1 if condition is true, 0 otherwise.
f2elm_t a, b;
uint8_t r = 0;
digit_t r = 0;

fp2copy(x, a);
fp2copy(y, b);
Expand All @@ -1046,7 +1046,7 @@ int8_t cmp_f2elm(const f2elm_t x, const f2elm_t y)
for (int i = NWORDS_FIELD-1; i >= 0; i--)
r |= (a[0][i] ^ b[0][i]) | (a[1][i] ^ b[1][i]);

return (int8_t)((-(int32_t)r) >> (8*sizeof(uint32_t)-1));
return (int8_t)((0-(digit_t)r) >> (8*sizeof(digit_t)-1));
}


Expand Down
15 changes: 15 additions & 0 deletions tests/test_sike.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Abstract: benchmarking/testing isogeny-based key encapsulation mechanism
*********************************************************************************************/

#include "../src/random/random.h"


// Benchmark and test parameters
#if defined(OPTIMIZED_GENERIC_IMPLEMENTATION) || (TARGET == TARGET_ARM)
Expand All @@ -23,6 +25,8 @@ int cryptotest_kem()
unsigned char ct[CRYPTO_CIPHERTEXTBYTES] = {0};
unsigned char ss[CRYPTO_BYTES] = {0};
unsigned char ss_[CRYPTO_BYTES] = {0};
unsigned char bytes[4];
uint32_t* pos = (uint32_t*)bytes;
bool passed = true;

printf("\n\nTESTING ISOGENY-BASED KEY ENCAPSULATION MECHANISM %s\n", SCHEME_NAME);
Expand All @@ -38,6 +42,17 @@ int cryptotest_kem()
passed = false;
break;
}

// Testing decapsulation after changing one bit of ct
randombytes(bytes, 4);
*pos %= CRYPTO_CIPHERTEXTBYTES;
ct[*pos] ^= 1;
crypto_kem_dec(ss_, ct, sk);

if (memcmp(ss, ss_, CRYPTO_BYTES) == 0) {
passed = false;
break;
}
}

if (passed == true) printf(" KEM tests .................................................... PASSED");
Expand Down

0 comments on commit 840400c

Please sign in to comment.