Get MbedTLS and its unit tests passing AARCH64

This commit is contained in:
Justine Tunney 2023-05-11 21:53:15 -07:00
parent 5e2f7f7ced
commit 4edbc98811
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
37 changed files with 632 additions and 725 deletions

View file

@ -15,9 +15,9 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "third_party/mbedtls/aesni.h"
#include "libc/intrin/bits.h"
#include "libc/str/str.h"
#include "third_party/mbedtls/aesni.h"
#include "third_party/mbedtls/common.h"
asm(".ident\t\"\\n\\n\

View file

@ -273,6 +273,7 @@ int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
return 0;
}
#ifdef __x86_64__
if (!IsTiny() && i == j) {
if (X->n < i * 2) {
if ((ret = mbedtls_mpi_grow(X, i * 2))) return ret;
@ -293,6 +294,7 @@ int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
return 0;
}
}
#endif /* __x86_64__ */
mbedtls_mpi_init( &TA );
mbedtls_mpi_init( &TB );
@ -310,9 +312,8 @@ int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
B = &TB;
}
if (!IsTiny() &&
i >= 16 && i == j && !(i & (i - 1)) &&
X86_HAVE(BMI2) && X86_HAVE(ADX) &&
(K = malloc(i * 4 * sizeof(*K)))) {
i >= 16 && i == j && !(i & (i - 1)) &&
(K = malloc(i * 4 * sizeof(*K)))) {
Karatsuba(X->p, A->p, B->p, i, K);
free(K);
} else {

View file

@ -103,7 +103,7 @@
* Hardens against against sbox side channels
*/
#define MBEDTLS_AESNI_C
#ifndef TINY
#if defined(__x86_64__) && !defined(TINY)
#define MBEDTLS_HAVE_X86_64
#define MBEDTLS_HAVE_SSE2
#endif

View file

@ -15,12 +15,11 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "third_party/mbedtls/des.h"
#include "libc/mem/mem.h"
#include "libc/mem/gc.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "third_party/mbedtls/common.h"
#include "third_party/mbedtls/des.h"
#include "third_party/mbedtls/endian.h"
#include "third_party/mbedtls/platform.h"
@ -831,14 +830,11 @@ static const unsigned char des3_test_cbc_enc[3][8] =
};
#endif /* MBEDTLS_CIPHER_MODE_CBC */
/*
* Checkup routine
*/
int mbedtls_des_self_test( int verbose )
static int mbedtls_des_self_test_impl( int verbose,
mbedtls_des_context *ctx,
mbedtls_des3_context *ctx3 )
{
int i, j, u, v, ret = 0;
mbedtls_des_context *ctx = gc(malloc(sizeof(mbedtls_des_context)));
mbedtls_des3_context *ctx3 = gc(malloc(sizeof(mbedtls_des3_context)));
unsigned char buf[8];
#if defined(MBEDTLS_CIPHER_MODE_CBC)
unsigned char prv[8];
@ -1023,4 +1019,17 @@ exit:
return( ret );
}
int mbedtls_des_self_test( int verbose )
{
int rc;
mbedtls_des_context *ctx;
mbedtls_des3_context *ctx3;
ctx = malloc( sizeof( mbedtls_des_context ) );
ctx3 = malloc( sizeof( mbedtls_des3_context ) );
rc = mbedtls_des_self_test_impl( verbose, ctx, ctx3 );
free( ctx3 );
free( ctx );
return( rc );
}
#endif /* MBEDTLS_SELF_TEST */

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/asan.internal.h"
#include "libc/mem/gc.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
@ -187,10 +186,20 @@ mbedtls_p384_mul( uint64_t X[12],
}
else
{
if( A == X ) A = _gc( memcpy( malloc( 6 * 8 ), A, 6 * 8 ) );
if( B == X ) B = _gc( memcpy( malloc( 6 * 8 ), B, 6 * 8 ) );
void *f = 0;
if( A == X )
{
A = memcpy( malloc( 6 * 8 ), A, 6 * 8 );
f = A;
}
else if( B == X )
{
B = memcpy( malloc( 6 * 8 ), B, 6 * 8 );
f = B;
}
Mul( X, A, n, B, m );
mbedtls_platform_zeroize( X + n + m, (12 - n - m) * 8 );
free( f );
}
mbedtls_p384_mod( X );
}

View file

@ -16,9 +16,11 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/nexgen32e/x86feature.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "third_party/mbedtls/bignum_internal.h"
#include "third_party/mbedtls/math.h"
#include "third_party/mbedtls/platform.h"
forceinline int Cmp(uint64_t *a, uint64_t *b, size_t n) {
@ -37,6 +39,7 @@ forceinline int Cmp(uint64_t *a, uint64_t *b, size_t n) {
forceinline bool Sub(uint64_t *C, uint64_t *A, uint64_t *B, size_t n) {
bool cf;
uint64_t c, i;
#ifdef __x86_64__
asm volatile("xor\t%1,%1\n\t"
".align\t16\n1:\t"
"mov\t(%5,%3,8),%1\n\t"
@ -48,12 +51,18 @@ forceinline bool Sub(uint64_t *C, uint64_t *A, uint64_t *B, size_t n) {
: "=@ccb"(cf), "=&r"(c), "+c"(n), "=r"(i)
: "r"(C), "r"(A), "r"(B), "3"(0)
: "cc", "memory");
#else
for (cf = false, c = i = 0; i < n; ++i) {
SBB(C[i], A[i], B[i], cf, cf);
}
#endif
return cf;
}
forceinline bool Add(uint64_t *C, uint64_t *A, uint64_t *B, size_t n) {
bool cf;
uint64_t c, i;
#ifdef __x86_64__
asm volatile("xor\t%1,%1\n\t"
".align\t16\n1:\t"
"mov\t(%5,%3,8),%1\n\t"
@ -65,6 +74,11 @@ forceinline bool Add(uint64_t *C, uint64_t *A, uint64_t *B, size_t n) {
: "=@ccc"(cf), "=&r"(c), "+c"(n), "=r"(i)
: "r"(C), "r"(A), "r"(B), "3"(0)
: "cc", "memory");
#else
for (cf = false, c = i = 0; i < n; ++i) {
ADC(C[i], A[i], B[i], cf, cf);
}
#endif
return cf;
}
@ -80,7 +94,13 @@ void Karatsuba(uint64_t *C, uint64_t *A, uint64_t *B, size_t n, uint64_t *K) {
uint64_t c, t;
uint64_t *x, *y;
if (n == 8) {
Mul8x8Adx(C, A, B);
#ifdef __x86_64__
if (X86_HAVE(BMI2) && X86_HAVE(ADX)) {
Mul8x8Adx(C, A, B);
return;
}
#endif
Mul(C, A, 8, B, 8);
return;
}
switch (Cmp(A, A + n / 2, n / 2) * 3 + Cmp(B + n / 2, B, n / 2)) {

View file

@ -61,13 +61,11 @@ o/$(MODE)/third_party/mbedtls/bigmul6.o: private \
OVERRIDE_CFLAGS += \
-O2
ifeq ($(ARCH), x86_64)
o/$(MODE)/third_party/mbedtls/shiftright-avx.o: private \
OVERRIDE_CFLAGS += \
-O3 -mavx
o/$(MODE)/third_party/mbedtls/shiftright2-avx.o: private \
OVERRIDE_CFLAGS += \
-O3 -mavx
endif
o/$(MODE)/third_party/mbedtls/zeroize.o: private \
OVERRIDE_CFLAGS += \

View file

@ -19,6 +19,8 @@
#include "third_party/mbedtls/bignum_internal.h"
#include "third_party/mbedtls/platform.h"
#ifdef __x86_64__
typedef uint64_t xmm_t __attribute__((__vector_size__(16), __aligned__(1)));
void ShiftRightAvx(uint64_t *p, size_t n, unsigned char k) {
@ -49,3 +51,5 @@ void ShiftRightAvx(uint64_t *p, size_t n, unsigned char k) {
p[0] = p[0] >> k | p1 << (64 - k);
}
}
#endif /* __x86_64__ */

View file

@ -1,6 +1,5 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_MBEDTLS_TEST_LIB_H_
#define COSMOPOLITAN_THIRD_PARTY_MBEDTLS_TEST_LIB_H_
#include "libc/mem/gc.internal.h"
#include "libc/str/str.h"
#include "libc/x/x.h"
#include "libc/x/xasprintf.h"

View file

@ -131,8 +131,8 @@ o/$(MODE)/third_party/mbedtls/test/%.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/third_party/mbedtls/test/%.com.runs: o/$(MODE)/third_party/mbedtls/test/%.com
@$(COMPILE) -ACHECK -wtT$@ $< $(TESTARGS)
o/$(MODE)/third_party/mbedtls/test/%.com.runs: o/$(MODE)/third_party/mbedtls/test/%.com $(VM)
@$(COMPILE) -ACHECK -wtT$@ $(VM) $< $(TESTARGS)
o/$(MODE)/third_party/mbedtls/test/lib.o: private \
OVERRIDE_CFLAGS += \