Make stronger crypto nearly as fast

One of the disadvantages of x25519 and ℘256 is it only provides 126 bits
of security, so that seems like a weak link in the chain, if we're using
ECDHE-ECDSA-AES256-GCM-SHA384. The U.S. government wants classified data
to be encrypted using a curve at least as strong as ℘384, which provides
192 bits of security, but if you read the consensus of stack exchange it
would give you the impression that ℘384 is three times slower.

This change (as well as the previous one) makes ℘384 three times as fast
by tuning its modulus and multiplication subroutines with new tests that
should convincingly show: the optimized code behaves the same way as the
old code. Some of the diff noise from the previous change is now removed
too, so that our vendored fork can be more easily compared with upstream
sources. So you can now have stronger cryptography without compromises.

℘384 modulus Justine                        l:         28𝑐          9𝑛𝑠
℘384 modulus MbedTLS NIST                   l:        127𝑐         41𝑛𝑠
℘384 modulus MbedTLS MPI                    l:      1,850𝑐        597𝑛𝑠

The benchmarks above show the improvements made by secp384r1() which is
an important function since it needs to be called 13,000 times whenever
someone establishes a connection to your web server. The same's true of
Mul6x6Adx() which is able to multiply 384-bit numbers in 73 cycles, but
only if your CPU was purchased after 2014 when Broadwell was introduced
This commit is contained in:
Justine Tunney 2021-07-26 15:16:43 -07:00
parent 398f0c16fb
commit ea83cc0ad0
27 changed files with 4291 additions and 3361 deletions

View file

@ -39,6 +39,7 @@
#include "third_party/mbedtls/des.h"
#include "third_party/mbedtls/dhm.h"
#include "third_party/mbedtls/ecp.h"
#include "third_party/mbedtls/ecp_internal.h"
#include "third_party/mbedtls/entropy.h"
#include "third_party/mbedtls/error.h"
#include "third_party/mbedtls/gcm.h"
@ -148,17 +149,17 @@ static void P256_MPI(mbedtls_mpi *N) {
static void P256_JUSTINE(mbedtls_mpi *N) {
memcpy(N->p, rng, 8 * 8);
ecp_mod_p256(N);
secp256r1(N->p);
}
static void P384_MPI(mbedtls_mpi *N) {
memcpy(N->p, rng, 8 * 8);
memcpy(N->p, rng, 12 * 8);
ASSERT_EQ(0, mbedtls_mpi_mod_mpi(N, N, &grp.P));
}
static void P384_JUSTINE(mbedtls_mpi *N) {
memcpy(N->p, rng, 8 * 8);
ecp_mod_p384(N);
memcpy(N->p, rng, 12 * 8);
secp384r1(N->p);
}
BENCH(p256, bench) {
@ -166,6 +167,7 @@ BENCH(p256, bench) {
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1);
mbedtls_mpi x = {1, 8, gc(calloc(8, 8))};
rngset(x.p, 8 * 8, rand64, -1);
EZBENCH2("P-256 modulus MbedTLS MPI lib", donothing, P256_MPI(&x));
EZBENCH2("P-256 modulus Justine rewrite", donothing, P256_JUSTINE(&x));
mbedtls_ecp_group_free(&grp);
@ -176,10 +178,10 @@ BENCH(p384, bench) {
#ifdef MBEDTLS_ECP_C
mbedtls_ecp_group_init(&grp);
mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP384R1);
uint64_t y[12];
mbedtls_mpi x = {1, 12, gc(calloc(12, 8))};
EZBENCH2("P-384 modulus MbedTLS MPI lib", donothing, P384_MPI(&x));
EZBENCH2("P-384 modulus Justine rewrite", donothing, P384_JUSTINE(&x));
rngset(x.p, 12 * 8, rand64, -1);
mbedtls_ecp_group_free(&grp);
#endif
}
@ -1112,3 +1114,49 @@ BENCH(cmpint, bench) {
EZBENCH2("cmpint 3.1", donothing, mbedtls_mpi_cmp_int(&z, 0));
EZBENCH2("cmpint 3.2", donothing, mbedtls_mpi_cmp_int(&z, 1));
}
mbedtls_mpi_uint F1(mbedtls_mpi_uint *d, const mbedtls_mpi_uint *a,
const mbedtls_mpi_uint *b, size_t n) {
size_t i;
unsigned char cf;
mbedtls_mpi_uint c, x;
cf = c = i = 0;
for (; i < n; ++i) SBB(d[i], a[i], b[i], c, c);
return c;
}
mbedtls_mpi_uint F2(mbedtls_mpi_uint *d, const mbedtls_mpi_uint *a,
const mbedtls_mpi_uint *b, size_t n) {
size_t i;
unsigned char cf;
mbedtls_mpi_uint c, x;
cf = c = i = 0;
asm volatile("xor\t%1,%1\n\t"
".align\t16\n1:\t"
"mov\t(%5,%3,8),%1\n\t"
"sbb\t(%6,%3,8),%1\n\t"
"mov\t%1,(%4,%3,8)\n\t"
"lea\t1(%3),%3\n\t"
"dec\t%2\n\t"
"jnz\t1b"
: "=@ccb"(cf), "=&r"(x), "+c"(n), "=r"(i)
: "r"(d), "r"(a), "r"(b), "3"(0)
: "cc", "memory");
return cf;
}
TEST(wut, wut) {
uint64_t A[8];
uint64_t B[8];
uint64_t C[8];
uint64_t D[8];
int i;
for (i = 0; i < 1000; ++i) {
rngset(A, sizeof(A), rand64, -1);
rngset(B, sizeof(B), rand64, -1);
int x = F1(C, A, B, 8);
int y = F2(D, A, B, 8);
ASSERT_EQ(x, y);
ASSERT_EQ(0, memcmp(C, D, sizeof(C)));
}
}