Skip to content

Commit

Permalink
Take BoringSSL '0378578': Dedup a few more load/store implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Sep 23, 2023
2 parents 6ccdf7b + 0378578 commit 5233928
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
10 changes: 10 additions & 0 deletions crypto/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,16 @@ static inline void CRYPTO_store_u32_be(void *out, uint32_t v) {
OPENSSL_memcpy(out, &v, sizeof(v));
}

static inline uint64_t CRYPTO_load_u64_le(const void *in) {
uint64_t v;
OPENSSL_memcpy(&v, in, sizeof(v));
return v;
}

static inline void CRYPTO_store_u64_le(void *out, uint64_t v) {
OPENSSL_memcpy(out, &v, sizeof(v));
}

static inline uint64_t CRYPTO_load_u64_be(const void *ptr) {
uint64_t ret;
OPENSSL_memcpy(&ret, ptr, sizeof(ret));
Expand Down
40 changes: 12 additions & 28 deletions crypto/poly1305/poly1305_vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,6 @@

#include <emmintrin.h>

static uint32_t load_u32_le(const uint8_t in[4]) {
uint32_t ret;
OPENSSL_memcpy(&ret, in, 4);
return ret;
}

static uint64_t load_u64_le(const uint8_t in[8]) {
uint64_t ret;
OPENSSL_memcpy(&ret, in, 8);
return ret;
}

static void store_u64_le(uint8_t out[8], uint64_t v) {
OPENSSL_memcpy(out, &v, 8);
}

typedef __m128i xmmi;

static const alignas(16) uint32_t poly1305_x64_sse2_message_mask[4] = {
Expand Down Expand Up @@ -117,8 +101,8 @@ void CRYPTO_poly1305_init(poly1305_state *state, const uint8_t key[32]) {
uint64_t t0, t1;

// clamp key
t0 = load_u64_le(key + 0);
t1 = load_u64_le(key + 8);
t0 = CRYPTO_load_u64_le(key + 0);
t1 = CRYPTO_load_u64_le(key + 8);
r0 = t0 & 0xffc0fffffff;
t0 >>= 44;
t0 |= t1 << 20;
Expand All @@ -136,10 +120,10 @@ void CRYPTO_poly1305_init(poly1305_state *state, const uint8_t key[32]) {
p->R22.d[3] = (uint32_t)(r2 >> 32);

// store pad
p->R23.d[1] = load_u32_le(key + 16);
p->R23.d[3] = load_u32_le(key + 20);
p->R24.d[1] = load_u32_le(key + 24);
p->R24.d[3] = load_u32_le(key + 28);
p->R23.d[1] = CRYPTO_load_u32_le(key + 16);
p->R23.d[3] = CRYPTO_load_u32_le(key + 20);
p->R24.d[1] = CRYPTO_load_u32_le(key + 24);
p->R24.d[3] = CRYPTO_load_u32_le(key + 28);

// H = 0
st->H[0] = _mm_setzero_si128();
Expand Down Expand Up @@ -771,8 +755,8 @@ void CRYPTO_poly1305_finish(poly1305_state *state, uint8_t mac[16]) {
}

poly1305_donna_atleast16bytes:
t0 = load_u64_le(m + 0);
t1 = load_u64_le(m + 8);
t0 = CRYPTO_load_u64_le(m + 0);
t1 = CRYPTO_load_u64_le(m + 8);
h0 += t0 & 0xfffffffffff;
t0 = shr128_pair(t1, t0, 44);
h1 += t0 & 0xfffffffffff;
Expand Down Expand Up @@ -811,8 +795,8 @@ void CRYPTO_poly1305_finish(poly1305_state *state, uint8_t mac[16]) {
OPENSSL_memset(m + leftover, 0, 16 - leftover);
leftover = 16;

t0 = load_u64_le(m + 0);
t1 = load_u64_le(m + 8);
t0 = CRYPTO_load_u64_le(m + 0);
t1 = CRYPTO_load_u64_le(m + 8);
h0 += t0 & 0xfffffffffff;
t0 = shr128_pair(t1, t0, 44);
h1 += t0 & 0xfffffffffff;
Expand Down Expand Up @@ -858,8 +842,8 @@ void CRYPTO_poly1305_finish(poly1305_state *state, uint8_t mac[16]) {
t1 = (t1 >> 24);
h2 += (t1)+c;

store_u64_le(mac + 0, ((h0) | (h1 << 44)));
store_u64_le(mac + 8, ((h1 >> 20) | (h2 << 24)));
CRYPTO_store_u64_le(mac + 0, ((h0) | (h1 << 44)));
CRYPTO_store_u64_le(mac + 8, ((h1 >> 20) | (h2 << 24)));
}

#endif // BORINGSSL_HAS_UINT128 && OPENSSL_X86_64

0 comments on commit 5233928

Please sign in to comment.