Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/chacha20poly1305.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
#include "mystdint.h"
#include <string.h>

#if defined(__wasm_simd128__)
#include <wasm_simd128.h>
#elif defined(__SSE4_1__)
#include <immintrin.h>
#elif defined(__aarch64__)
#include <arm_neon.h>
#endif

#define ROL32(x, c) (((x) << (c)) | ((x) >> (32-(c))))
#define ROR32(x, c) (((x) >> (c)) | ((x) << (32-(c))))

Expand Down Expand Up @@ -215,6 +223,21 @@ void poly1305(const uint8_t* msg, size_t n, const uint8_t key[32],
SQLITE_PRIVATE
int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16])
{
#if defined(__wasm_simd128__)
v128_t tagDifference = wasm_v128_xor(
wasm_v128_load(tag1), wasm_v128_load(tag2));
return wasm_v128_any_true(tagDifference);
#elif defined(__SSE4_1__)
__m128i tag1_vec = _mm_loadu_si128((const __m128i*)tag1);
__m128i tag2_vec = _mm_loadu_si128((const __m128i*)tag2);
__m128i tagDifference = _mm_xor_si128(tag1_vec, tag2_vec);
return !_mm_testz_si128(tagDifference, tagDifference);
#elif defined(__aarch64__)
uint8x16_t tag1_vec = vld1q_u8(tag1);
uint8x16_t tag2_vec = vld1q_u8(tag2);
uint8x16_t tagDifference = veorq_u8(tag1_vec, tag2_vec);
return vmaxvq_u8(tagDifference) != 0;
#else
uint8_t d = 0;
d |= tag1[ 0] ^ tag2[ 0];
d |= tag1[ 1] ^ tag2[ 1];
Expand All @@ -233,6 +256,7 @@ int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16])
d |= tag1[14] ^ tag2[14];
d |= tag1[15] ^ tag2[15];
return (int) d;
#endif
}

/*
Expand Down
Loading