Merge pull request #1671 from SparkiDev/x25519_asm_fix
Fix for Curve25519 ASM
This commit is contained in:
commit
df6c496c4e
@ -494,6 +494,16 @@ static WC_INLINE void fe_mul_avx2(fe r, const fe a, const fe b)
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"adcq $0, %%r11\n\t"
|
||||
"# Reduce if top bit set\n\t"
|
||||
"movq %%r11, %%rdx\n\t"
|
||||
"shrq $63, %%rdx\n\t"
|
||||
"imulq $19, %%rdx, %%rax\n\t"
|
||||
"andq %%rcx, %%r11\n\t"
|
||||
"addq %%rax, %%r8\n\t"
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"adcq $0, %%r11\n\t"
|
||||
"# Store\n\t"
|
||||
"movq %%r8, 0(%[r])\n\t"
|
||||
"movq %%r9, 8(%[r])\n\t"
|
||||
"movq %%r10, 16(%[r])\n\t"
|
||||
@ -647,6 +657,16 @@ static WC_INLINE void fe_mul_x64(fe r, const fe a, const fe b)
|
||||
"adcq $0, %%r8\n\t"
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"# Reduce if top bit set\n\t"
|
||||
"movq %%r10, %%rdx\n\t"
|
||||
"shrq $63, %%rdx\n\t"
|
||||
"imulq $19, %%rdx, %%rax\n\t"
|
||||
"andq %%rbx, %%r10\n\t"
|
||||
"addq %%rax, %%rcx\n\t"
|
||||
"adcq $0, %%r8\n\t"
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"# Store\n\t"
|
||||
"movq %%rcx, 0(%[r])\n\t"
|
||||
"movq %%r8, 8(%[r])\n\t"
|
||||
"movq %%r9, 16(%[r])\n\t"
|
||||
@ -762,6 +782,16 @@ static WC_INLINE void fe_sq_avx2(fe r, const fe a)
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"adcq $0, %%r11\n\t"
|
||||
"# Reduce if top bit set\n\t"
|
||||
"movq %%r11, %%rdx\n\t"
|
||||
"shrq $63, %%rdx\n\t"
|
||||
"imulq $19, %%rdx, %%rax\n\t"
|
||||
"andq %%rcx, %%r11\n\t"
|
||||
"addq %%rax, %%r8\n\t"
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"adcq $0, %%r11\n\t"
|
||||
"# Store\n\t"
|
||||
"movq %%r8, 0(%[r])\n\t"
|
||||
"movq %%r9, 8(%[r])\n\t"
|
||||
"movq %%r10, 16(%[r])\n\t"
|
||||
@ -888,6 +918,16 @@ static WC_INLINE void fe_sq_x64(fe r, const fe a)
|
||||
"adcq $0, %%r8\n\t"
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"# Reduce if top bit set\n\t"
|
||||
"movq %%r10, %%rdx\n\t"
|
||||
"shrq $63, %%rdx\n\t"
|
||||
"imulq $19, %%rdx, %%rax\n\t"
|
||||
"andq %%rbx, %%r10\n\t"
|
||||
"addq %%rax, %%rcx\n\t"
|
||||
"adcq $0, %%r8\n\t"
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"# Store\n\t"
|
||||
"movq %%rcx, 0(%[r])\n\t"
|
||||
"movq %%r8, 8(%[r])\n\t"
|
||||
"movq %%r9, 16(%[r])\n\t"
|
||||
@ -1352,6 +1392,16 @@ static WC_INLINE void fe_sq2_avx2(fe r, const fe a)
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"adcq $0, %%r11\n\t"
|
||||
"# Reduce if top bit set\n\t"
|
||||
"movq %%r11, %%rdx\n\t"
|
||||
"shrq $63, %%rdx\n\t"
|
||||
"imulq $19, %%rdx, %%rax\n\t"
|
||||
"andq %%rbx, %%r11\n\t"
|
||||
"addq %%rax, %%r8\n\t"
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"adcq $0, %%r11\n\t"
|
||||
"# Store\n\t"
|
||||
"movq %%r8, 0(%[r])\n\t"
|
||||
"movq %%r9, 8(%[r])\n\t"
|
||||
"movq %%r10, 16(%[r])\n\t"
|
||||
@ -1489,6 +1539,16 @@ static WC_INLINE void fe_sq2_x64(fe r, const fe a)
|
||||
"adcq $0, %%r8\n\t"
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"# Reduce if top bit set\n\t"
|
||||
"movq %%r10, %%rdx\n\t"
|
||||
"shrq $63, %%rdx\n\t"
|
||||
"imulq $19, %%rdx, %%rax\n\t"
|
||||
"andq %%rbx, %%r10\n\t"
|
||||
"addq %%rax, %%rcx\n\t"
|
||||
"adcq $0, %%r8\n\t"
|
||||
"adcq $0, %%r9\n\t"
|
||||
"adcq $0, %%r10\n\t"
|
||||
"# Store\n\t"
|
||||
"movq %%rcx, 0(%[r])\n\t"
|
||||
"movq %%r8, 8(%[r])\n\t"
|
||||
"movq %%r9, 16(%[r])\n\t"
|
||||
|
@ -16667,6 +16667,149 @@ int ecc_test_buffers(void) {
|
||||
|
||||
|
||||
#ifdef HAVE_CURVE25519
|
||||
#if defined(HAVE_CURVE25519_SHARED_SECRET) && \
|
||||
defined(HAVE_CURVE25519_KEY_IMPORT)
|
||||
#ifdef CURVE25519_OVERFLOW_ALL_TESTS
|
||||
#define X25519_TEST_CNT 5
|
||||
#else
|
||||
#define X25519_TEST_CNT 1
|
||||
#endif
|
||||
static int curve25519_overflow_test(void)
|
||||
{
|
||||
/* secret key for party a */
|
||||
byte sa[X25519_TEST_CNT][32] = {
|
||||
{
|
||||
0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
|
||||
0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
|
||||
0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
|
||||
0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
|
||||
},
|
||||
#ifdef CURVE25519_OVERFLOW_ALL_TESTS
|
||||
{
|
||||
0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
|
||||
0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
|
||||
0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
|
||||
0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
|
||||
},
|
||||
{
|
||||
0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
|
||||
0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
|
||||
0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
|
||||
0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
|
||||
},
|
||||
{
|
||||
0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
|
||||
0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
|
||||
0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
|
||||
0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
|
||||
},
|
||||
{
|
||||
0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
|
||||
0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
|
||||
0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
|
||||
0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
/* public key for party b */
|
||||
byte pb[X25519_TEST_CNT][32] = {
|
||||
{
|
||||
0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
||||
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
||||
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
||||
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
|
||||
},
|
||||
#ifdef CURVE25519_OVERFLOW_ALL_TESTS
|
||||
{
|
||||
/* 0xff first byte in original - invalid! */
|
||||
0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
||||
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
||||
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
|
||||
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
|
||||
},
|
||||
{
|
||||
0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
|
||||
0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
|
||||
0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
|
||||
0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
|
||||
},
|
||||
{
|
||||
0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
|
||||
0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
|
||||
0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
|
||||
0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
|
||||
},
|
||||
{
|
||||
0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
|
||||
0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
|
||||
0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
|
||||
0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
/* expected shared key */
|
||||
byte ss[X25519_TEST_CNT][32] = {
|
||||
{
|
||||
0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
|
||||
0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
|
||||
0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
|
||||
0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
|
||||
},
|
||||
#ifdef CURVE25519_OVERFLOW_ALL_TESTS
|
||||
{
|
||||
0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
|
||||
0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
|
||||
0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
|
||||
0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
|
||||
},
|
||||
{
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
|
||||
},
|
||||
{
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
|
||||
},
|
||||
{
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
int i;
|
||||
word32 y;
|
||||
byte shared[32];
|
||||
curve25519_key userA;
|
||||
|
||||
wc_curve25519_init(&userA);
|
||||
|
||||
for (i = 0; i < X25519_TEST_CNT; i++) {
|
||||
if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
|
||||
sizeof(pb[i]), &userA) != 0)
|
||||
return -8750 - i;
|
||||
|
||||
/* test against known test vector */
|
||||
XMEMSET(shared, 0, sizeof(shared));
|
||||
y = sizeof(shared);
|
||||
if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0)
|
||||
return -8755 - i;
|
||||
|
||||
if (XMEMCMP(ss[i], shared, y))
|
||||
return -8760 - i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
|
||||
|
||||
int curve25519_test(void)
|
||||
{
|
||||
@ -16683,7 +16826,8 @@ int curve25519_test(void)
|
||||
word32 x;
|
||||
curve25519_key userA, userB, pubKey;
|
||||
|
||||
#if defined(HAVE_CURVE25519_SHARED_SECRET) && defined(HAVE_CURVE25519_KEY_IMPORT)
|
||||
#if defined(HAVE_CURVE25519_SHARED_SECRET) && \
|
||||
defined(HAVE_CURVE25519_KEY_IMPORT)
|
||||
/* test vectors from
|
||||
https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
|
||||
*/
|
||||
@ -16778,7 +16922,8 @@ int curve25519_test(void)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_CURVE25519_SHARED_SECRET) && defined(HAVE_CURVE25519_KEY_IMPORT)
|
||||
#if defined(HAVE_CURVE25519_SHARED_SECRET) && \
|
||||
defined(HAVE_CURVE25519_KEY_IMPORT)
|
||||
/* test shared key after importing a public key */
|
||||
XMEMSET(sharedB, 0, sizeof(sharedB));
|
||||
y = sizeof(sharedB);
|
||||
@ -16837,7 +16982,11 @@ int curve25519_test(void)
|
||||
|
||||
if (XMEMCMP(sharedA, sharedB, x))
|
||||
return -8822;
|
||||
#endif /* HAVE_CURVE25519_SHARED_SECRET */
|
||||
|
||||
ret = curve25519_overflow_test();
|
||||
if (ret != 0)
|
||||
return ret;
|
||||
#endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
|
||||
|
||||
/* clean up keys when done */
|
||||
wc_curve25519_free(&pubKey);
|
||||
|
Loading…
x
Reference in New Issue
Block a user