andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* This implementation of poly1305 is by Andrew Moon andre@0: * (https://github.com/floodyberry/poly1305-donna) and released as public andre@0: * domain. It implements SIMD vectorization based on the algorithm described in andre@0: * http://cr.yp.to/papers.html#neoncrypto. Unrolled to 2 powers, i.e. 64 byte andre@0: * block size. */ andre@0: andre@0: #include andre@0: #include andre@0: andre@0: #include "poly1305.h" andre@0: andre@0: #define ALIGN(x) __attribute__((aligned(x))) andre@0: #define INLINE inline andre@0: #define U8TO64_LE(m) (*(uint64_t*)(m)) andre@0: #define U8TO32_LE(m) (*(uint32_t*)(m)) andre@0: #define U64TO8_LE(m,v) (*(uint64_t*)(m)) = v andre@0: andre@0: typedef __m128i xmmi; andre@0: typedef unsigned __int128 uint128_t; andre@0: andre@0: static const uint32_t ALIGN(16) poly1305_x64_sse2_message_mask[4] = {(1 << 26) - 1, 0, (1 << 26) - 1, 0}; andre@0: static const uint32_t ALIGN(16) poly1305_x64_sse2_5[4] = {5, 0, 5, 0}; andre@0: static const uint32_t ALIGN(16) poly1305_x64_sse2_1shl128[4] = {(1 << 24), 0, (1 << 24), 0}; andre@0: andre@0: static uint128_t INLINE andre@0: add128(uint128_t a, uint128_t b) { andre@0: return a + b; andre@0: } andre@0: andre@0: static uint128_t INLINE andre@0: add128_64(uint128_t a, uint64_t b) { andre@0: return a + b; andre@0: } andre@0: andre@0: static uint128_t INLINE andre@0: mul64x64_128(uint64_t a, uint64_t b) { andre@0: return (uint128_t)a * b; andre@0: } andre@0: andre@0: static uint64_t INLINE andre@0: lo128(uint128_t a) { andre@0: return (uint64_t)a; andre@0: } andre@0: andre@0: static uint64_t INLINE andre@0: shr128(uint128_t v, const int shift) { andre@0: return (uint64_t)(v >> shift); andre@0: } andre@0: andre@0: static uint64_t INLINE andre@0: shr128_pair(uint64_t hi, uint64_t lo, const int shift) { andre@0: return (uint64_t)((((uint128_t)hi << 64) | lo) >> shift); andre@0: } andre@0: andre@0: typedef struct poly1305_power_t { andre@0: union { andre@0: xmmi v; andre@0: uint64_t u[2]; andre@0: uint32_t d[4]; andre@0: } R20,R21,R22,R23,R24,S21,S22,S23,S24; andre@0: } poly1305_power; andre@0: andre@0: typedef struct poly1305_state_internal_t { andre@0: poly1305_power P[2]; /* 288 bytes, top 32 bit halves unused = 144 bytes of free storage */ andre@0: union { andre@0: xmmi H[5]; /* 80 bytes */ andre@0: uint64_t HH[10]; andre@0: }; andre@0: /* uint64_t r0,r1,r2; [24 bytes] */ andre@0: /* uint64_t pad0,pad1; [16 bytes] */ andre@0: uint64_t started; /* 8 bytes */ andre@0: uint64_t leftover; /* 8 bytes */ andre@0: uint8_t buffer[64]; /* 64 bytes */ andre@0: } poly1305_state_internal; /* 448 bytes total + 63 bytes for alignment = 511 bytes raw */ andre@0: andre@0: static poly1305_state_internal INLINE andre@0: *poly1305_aligned_state(poly1305_state *state) { andre@0: return (poly1305_state_internal *)(((uint64_t)state + 63) & ~63); andre@0: } andre@0: andre@0: /* copy 0-63 bytes */ andre@0: static void INLINE andre@0: poly1305_block_copy(uint8_t *dst, const uint8_t *src, size_t bytes) { andre@0: size_t offset = src - dst; andre@0: if (bytes & 32) { andre@0: _mm_storeu_si128((xmmi *)(dst + 0), _mm_loadu_si128((xmmi *)(dst + offset + 0))); andre@0: _mm_storeu_si128((xmmi *)(dst + 16), _mm_loadu_si128((xmmi *)(dst + offset + 16))); andre@0: dst += 32; andre@0: } andre@0: if (bytes & 16) { _mm_storeu_si128((xmmi *)dst, _mm_loadu_si128((xmmi *)(dst + offset))); dst += 16; } andre@0: if (bytes & 8) { *(uint64_t *)dst = *(uint64_t *)(dst + offset); dst += 8; } andre@0: if (bytes & 4) { *(uint32_t *)dst = *(uint32_t *)(dst + offset); dst += 4; } andre@0: if (bytes & 2) { *(uint16_t *)dst = *(uint16_t *)(dst + offset); dst += 2; } andre@0: if (bytes & 1) { *( uint8_t *)dst = *( uint8_t *)(dst + offset); } andre@0: } andre@0: andre@0: /* zero 0-15 bytes */ andre@0: static void INLINE andre@0: poly1305_block_zero(uint8_t *dst, size_t bytes) { andre@0: if (bytes & 8) { *(uint64_t *)dst = 0; dst += 8; } andre@0: if (bytes & 4) { *(uint32_t *)dst = 0; dst += 4; } andre@0: if (bytes & 2) { *(uint16_t *)dst = 0; dst += 2; } andre@0: if (bytes & 1) { *( uint8_t *)dst = 0; } andre@0: } andre@0: andre@0: static size_t INLINE andre@0: poly1305_min(size_t a, size_t b) { andre@0: return (a < b) ? a : b; andre@0: } andre@0: andre@0: void andre@0: Poly1305Init(poly1305_state *state, const unsigned char key[32]) { andre@0: poly1305_state_internal *st = poly1305_aligned_state(state); andre@0: poly1305_power *p; andre@0: uint64_t r0,r1,r2; andre@0: uint64_t t0,t1; andre@0: andre@0: /* clamp key */ andre@0: t0 = U8TO64_LE(key + 0); andre@0: t1 = U8TO64_LE(key + 8); andre@0: r0 = t0 & 0xffc0fffffff; t0 >>= 44; t0 |= t1 << 20; andre@0: r1 = t0 & 0xfffffc0ffff; t1 >>= 24; andre@0: r2 = t1 & 0x00ffffffc0f; andre@0: andre@0: /* store r in un-used space of st->P[1] */ andre@0: p = &st->P[1]; andre@0: p->R20.d[1] = (uint32_t)(r0 ); andre@0: p->R20.d[3] = (uint32_t)(r0 >> 32); andre@0: p->R21.d[1] = (uint32_t)(r1 ); andre@0: p->R21.d[3] = (uint32_t)(r1 >> 32); andre@0: p->R22.d[1] = (uint32_t)(r2 ); andre@0: p->R22.d[3] = (uint32_t)(r2 >> 32); andre@0: andre@0: /* store pad */ andre@0: p->R23.d[1] = U8TO32_LE(key + 16); andre@0: p->R23.d[3] = U8TO32_LE(key + 20); andre@0: p->R24.d[1] = U8TO32_LE(key + 24); andre@0: p->R24.d[3] = U8TO32_LE(key + 28); andre@0: andre@0: /* H = 0 */ andre@0: st->H[0] = _mm_setzero_si128(); andre@0: st->H[1] = _mm_setzero_si128(); andre@0: st->H[2] = _mm_setzero_si128(); andre@0: st->H[3] = _mm_setzero_si128(); andre@0: st->H[4] = _mm_setzero_si128(); andre@0: andre@0: st->started = 0; andre@0: st->leftover = 0; andre@0: } andre@0: andre@0: static void andre@0: poly1305_first_block(poly1305_state_internal *st, const uint8_t *m) { andre@0: const xmmi MMASK = _mm_load_si128((xmmi *)poly1305_x64_sse2_message_mask); andre@0: const xmmi FIVE = _mm_load_si128((xmmi*)poly1305_x64_sse2_5); andre@0: const xmmi HIBIT = _mm_load_si128((xmmi*)poly1305_x64_sse2_1shl128); andre@0: xmmi T5,T6; andre@0: poly1305_power *p; andre@0: uint128_t d[3]; andre@0: uint64_t r0,r1,r2; andre@0: uint64_t r20,r21,r22,s22; andre@0: uint64_t pad0,pad1; andre@0: uint64_t c; andre@0: uint64_t i; andre@0: andre@0: /* pull out stored info */ andre@0: p = &st->P[1]; andre@0: andre@0: r0 = ((uint64_t)p->R20.d[3] << 32) | (uint64_t)p->R20.d[1]; andre@0: r1 = ((uint64_t)p->R21.d[3] << 32) | (uint64_t)p->R21.d[1]; andre@0: r2 = ((uint64_t)p->R22.d[3] << 32) | (uint64_t)p->R22.d[1]; andre@0: pad0 = ((uint64_t)p->R23.d[3] << 32) | (uint64_t)p->R23.d[1]; andre@0: pad1 = ((uint64_t)p->R24.d[3] << 32) | (uint64_t)p->R24.d[1]; andre@0: andre@0: /* compute powers r^2,r^4 */ andre@0: r20 = r0; andre@0: r21 = r1; andre@0: r22 = r2; andre@0: for (i = 0; i < 2; i++) { andre@0: s22 = r22 * (5 << 2); andre@0: andre@0: d[0] = add128(mul64x64_128(r20, r20), mul64x64_128(r21 * 2, s22)); andre@0: d[1] = add128(mul64x64_128(r22, s22), mul64x64_128(r20 * 2, r21)); andre@0: d[2] = add128(mul64x64_128(r21, r21), mul64x64_128(r22 * 2, r20)); andre@0: andre@0: r20 = lo128(d[0]) & 0xfffffffffff; c = shr128(d[0], 44); andre@0: d[1] = add128_64(d[1], c); r21 = lo128(d[1]) & 0xfffffffffff; c = shr128(d[1], 44); andre@0: d[2] = add128_64(d[2], c); r22 = lo128(d[2]) & 0x3ffffffffff; c = shr128(d[2], 42); andre@0: r20 += c * 5; c = (r20 >> 44); r20 = r20 & 0xfffffffffff; andre@0: r21 += c; andre@0: andre@0: p->R20.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)( r20 ) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); andre@0: p->R21.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r20 >> 26) | (r21 << 18)) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); andre@0: p->R22.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r21 >> 8) ) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); andre@0: p->R23.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r21 >> 34) | (r22 << 10)) & 0x3ffffff), _MM_SHUFFLE(1,0,1,0)); andre@0: p->R24.v = _mm_shuffle_epi32(_mm_cvtsi32_si128((uint32_t)((r22 >> 16) ) ), _MM_SHUFFLE(1,0,1,0)); andre@0: p->S21.v = _mm_mul_epu32(p->R21.v, FIVE); andre@0: p->S22.v = _mm_mul_epu32(p->R22.v, FIVE); andre@0: p->S23.v = _mm_mul_epu32(p->R23.v, FIVE); andre@0: p->S24.v = _mm_mul_epu32(p->R24.v, FIVE); andre@0: p--; andre@0: } andre@0: andre@0: /* put saved info back */ andre@0: p = &st->P[1]; andre@0: p->R20.d[1] = (uint32_t)(r0 ); andre@0: p->R20.d[3] = (uint32_t)(r0 >> 32); andre@0: p->R21.d[1] = (uint32_t)(r1 ); andre@0: p->R21.d[3] = (uint32_t)(r1 >> 32); andre@0: p->R22.d[1] = (uint32_t)(r2 ); andre@0: p->R22.d[3] = (uint32_t)(r2 >> 32); andre@0: p->R23.d[1] = (uint32_t)(pad0 ); andre@0: p->R23.d[3] = (uint32_t)(pad0 >> 32); andre@0: p->R24.d[1] = (uint32_t)(pad1 ); andre@0: p->R24.d[3] = (uint32_t)(pad1 >> 32); andre@0: andre@0: /* H = [Mx,My] */ andre@0: T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 0)), _mm_loadl_epi64((xmmi *)(m + 16))); andre@0: T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 8)), _mm_loadl_epi64((xmmi *)(m + 24))); andre@0: st->H[0] = _mm_and_si128(MMASK, T5); andre@0: st->H[1] = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); andre@0: st->H[2] = _mm_and_si128(MMASK, T5); andre@0: st->H[3] = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: st->H[4] = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); andre@0: } andre@0: andre@0: static void andre@0: poly1305_blocks(poly1305_state_internal *st, const uint8_t *m, size_t bytes) { andre@0: const xmmi MMASK = _mm_load_si128((xmmi *)poly1305_x64_sse2_message_mask); andre@0: const xmmi FIVE = _mm_load_si128((xmmi*)poly1305_x64_sse2_5); andre@0: const xmmi HIBIT = _mm_load_si128((xmmi*)poly1305_x64_sse2_1shl128); andre@0: andre@0: poly1305_power *p; andre@0: xmmi H0,H1,H2,H3,H4; andre@0: xmmi T0,T1,T2,T3,T4,T5,T6; andre@0: xmmi M0,M1,M2,M3,M4; andre@0: xmmi C1,C2; andre@0: andre@0: H0 = st->H[0]; andre@0: H1 = st->H[1]; andre@0: H2 = st->H[2]; andre@0: H3 = st->H[3]; andre@0: H4 = st->H[4]; andre@0: andre@0: while (bytes >= 64) { andre@0: /* H *= [r^4,r^4] */ andre@0: p = &st->P[0]; andre@0: T0 = _mm_mul_epu32(H0, p->R20.v); andre@0: T1 = _mm_mul_epu32(H0, p->R21.v); andre@0: T2 = _mm_mul_epu32(H0, p->R22.v); andre@0: T3 = _mm_mul_epu32(H0, p->R23.v); andre@0: T4 = _mm_mul_epu32(H0, p->R24.v); andre@0: T5 = _mm_mul_epu32(H1, p->S24.v); T6 = _mm_mul_epu32(H1, p->R20.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H2, p->S23.v); T6 = _mm_mul_epu32(H2, p->S24.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H3, p->S22.v); T6 = _mm_mul_epu32(H3, p->S23.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H4, p->S21.v); T6 = _mm_mul_epu32(H4, p->S22.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H1, p->R21.v); T6 = _mm_mul_epu32(H1, p->R22.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H2, p->R20.v); T6 = _mm_mul_epu32(H2, p->R21.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H3, p->S24.v); T6 = _mm_mul_epu32(H3, p->R20.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H4, p->S23.v); T6 = _mm_mul_epu32(H4, p->S24.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H1, p->R23.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(H2, p->R22.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(H3, p->R21.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(H4, p->R20.v); T4 = _mm_add_epi64(T4, T5); andre@0: andre@0: /* H += [Mx,My]*[r^2,r^2] */ andre@0: T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 0)), _mm_loadl_epi64((xmmi *)(m + 16))); andre@0: T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 8)), _mm_loadl_epi64((xmmi *)(m + 24))); andre@0: M0 = _mm_and_si128(MMASK, T5); andre@0: M1 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); andre@0: M2 = _mm_and_si128(MMASK, T5); andre@0: M3 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: M4 = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); andre@0: andre@0: p = &st->P[1]; andre@0: T5 = _mm_mul_epu32(M0, p->R20.v); T6 = _mm_mul_epu32(M0, p->R21.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(M1, p->S24.v); T6 = _mm_mul_epu32(M1, p->R20.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(M2, p->S23.v); T6 = _mm_mul_epu32(M2, p->S24.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(M3, p->S22.v); T6 = _mm_mul_epu32(M3, p->S23.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(M4, p->S21.v); T6 = _mm_mul_epu32(M4, p->S22.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(M0, p->R22.v); T6 = _mm_mul_epu32(M0, p->R23.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(M1, p->R21.v); T6 = _mm_mul_epu32(M1, p->R22.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(M2, p->R20.v); T6 = _mm_mul_epu32(M2, p->R21.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(M3, p->S24.v); T6 = _mm_mul_epu32(M3, p->R20.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(M4, p->S23.v); T6 = _mm_mul_epu32(M4, p->S24.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(M0, p->R24.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(M1, p->R23.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(M2, p->R22.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(M3, p->R21.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(M4, p->R20.v); T4 = _mm_add_epi64(T4, T5); andre@0: andre@0: /* H += [Mx,My] */ andre@0: T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 32)), _mm_loadl_epi64((xmmi *)(m + 48))); andre@0: T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 40)), _mm_loadl_epi64((xmmi *)(m + 56))); andre@0: M0 = _mm_and_si128(MMASK, T5); andre@0: M1 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); andre@0: M2 = _mm_and_si128(MMASK, T5); andre@0: M3 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: M4 = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); andre@0: andre@0: T0 = _mm_add_epi64(T0, M0); andre@0: T1 = _mm_add_epi64(T1, M1); andre@0: T2 = _mm_add_epi64(T2, M2); andre@0: T3 = _mm_add_epi64(T3, M3); andre@0: T4 = _mm_add_epi64(T4, M4); andre@0: andre@0: /* reduce */ andre@0: C1 = _mm_srli_epi64(T0, 26); C2 = _mm_srli_epi64(T3, 26); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_and_si128(T3, MMASK); T1 = _mm_add_epi64(T1, C1); T4 = _mm_add_epi64(T4, C2); andre@0: C1 = _mm_srli_epi64(T1, 26); C2 = _mm_srli_epi64(T4, 26); T1 = _mm_and_si128(T1, MMASK); T4 = _mm_and_si128(T4, MMASK); T2 = _mm_add_epi64(T2, C1); T0 = _mm_add_epi64(T0, _mm_mul_epu32(C2, FIVE)); andre@0: C1 = _mm_srli_epi64(T2, 26); C2 = _mm_srli_epi64(T0, 26); T2 = _mm_and_si128(T2, MMASK); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_add_epi64(T3, C1); T1 = _mm_add_epi64(T1, C2); andre@0: C1 = _mm_srli_epi64(T3, 26); T3 = _mm_and_si128(T3, MMASK); T4 = _mm_add_epi64(T4, C1); andre@0: andre@0: /* H = (H*[r^4,r^4] + [Mx,My]*[r^2,r^2] + [Mx,My]) */ andre@0: H0 = T0; andre@0: H1 = T1; andre@0: H2 = T2; andre@0: H3 = T3; andre@0: H4 = T4; andre@0: andre@0: m += 64; andre@0: bytes -= 64; andre@0: } andre@0: andre@0: st->H[0] = H0; andre@0: st->H[1] = H1; andre@0: st->H[2] = H2; andre@0: st->H[3] = H3; andre@0: st->H[4] = H4; andre@0: } andre@0: andre@0: static size_t andre@0: poly1305_combine(poly1305_state_internal *st, const uint8_t *m, size_t bytes) { andre@0: const xmmi MMASK = _mm_load_si128((xmmi *)poly1305_x64_sse2_message_mask); andre@0: const xmmi HIBIT = _mm_load_si128((xmmi*)poly1305_x64_sse2_1shl128); andre@0: const xmmi FIVE = _mm_load_si128((xmmi*)poly1305_x64_sse2_5); andre@0: andre@0: poly1305_power *p; andre@0: xmmi H0,H1,H2,H3,H4; andre@0: xmmi M0,M1,M2,M3,M4; andre@0: xmmi T0,T1,T2,T3,T4,T5,T6; andre@0: xmmi C1,C2; andre@0: andre@0: uint64_t r0,r1,r2; andre@0: uint64_t t0,t1,t2,t3,t4; andre@0: uint64_t c; andre@0: size_t consumed = 0; andre@0: andre@0: H0 = st->H[0]; andre@0: H1 = st->H[1]; andre@0: H2 = st->H[2]; andre@0: H3 = st->H[3]; andre@0: H4 = st->H[4]; andre@0: andre@0: /* p = [r^2,r^2] */ andre@0: p = &st->P[1]; andre@0: andre@0: if (bytes >= 32) { andre@0: /* H *= [r^2,r^2] */ andre@0: T0 = _mm_mul_epu32(H0, p->R20.v); andre@0: T1 = _mm_mul_epu32(H0, p->R21.v); andre@0: T2 = _mm_mul_epu32(H0, p->R22.v); andre@0: T3 = _mm_mul_epu32(H0, p->R23.v); andre@0: T4 = _mm_mul_epu32(H0, p->R24.v); andre@0: T5 = _mm_mul_epu32(H1, p->S24.v); T6 = _mm_mul_epu32(H1, p->R20.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H2, p->S23.v); T6 = _mm_mul_epu32(H2, p->S24.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H3, p->S22.v); T6 = _mm_mul_epu32(H3, p->S23.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H4, p->S21.v); T6 = _mm_mul_epu32(H4, p->S22.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H1, p->R21.v); T6 = _mm_mul_epu32(H1, p->R22.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H2, p->R20.v); T6 = _mm_mul_epu32(H2, p->R21.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H3, p->S24.v); T6 = _mm_mul_epu32(H3, p->R20.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H4, p->S23.v); T6 = _mm_mul_epu32(H4, p->S24.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H1, p->R23.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(H2, p->R22.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(H3, p->R21.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(H4, p->R20.v); T4 = _mm_add_epi64(T4, T5); andre@0: andre@0: /* H += [Mx,My] */ andre@0: T5 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 0)), _mm_loadl_epi64((xmmi *)(m + 16))); andre@0: T6 = _mm_unpacklo_epi64(_mm_loadl_epi64((xmmi *)(m + 8)), _mm_loadl_epi64((xmmi *)(m + 24))); andre@0: M0 = _mm_and_si128(MMASK, T5); andre@0: M1 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: T5 = _mm_or_si128(_mm_srli_epi64(T5, 52), _mm_slli_epi64(T6, 12)); andre@0: M2 = _mm_and_si128(MMASK, T5); andre@0: M3 = _mm_and_si128(MMASK, _mm_srli_epi64(T5, 26)); andre@0: M4 = _mm_or_si128(_mm_srli_epi64(T6, 40), HIBIT); andre@0: andre@0: T0 = _mm_add_epi64(T0, M0); andre@0: T1 = _mm_add_epi64(T1, M1); andre@0: T2 = _mm_add_epi64(T2, M2); andre@0: T3 = _mm_add_epi64(T3, M3); andre@0: T4 = _mm_add_epi64(T4, M4); andre@0: andre@0: /* reduce */ andre@0: C1 = _mm_srli_epi64(T0, 26); C2 = _mm_srli_epi64(T3, 26); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_and_si128(T3, MMASK); T1 = _mm_add_epi64(T1, C1); T4 = _mm_add_epi64(T4, C2); andre@0: C1 = _mm_srli_epi64(T1, 26); C2 = _mm_srli_epi64(T4, 26); T1 = _mm_and_si128(T1, MMASK); T4 = _mm_and_si128(T4, MMASK); T2 = _mm_add_epi64(T2, C1); T0 = _mm_add_epi64(T0, _mm_mul_epu32(C2, FIVE)); andre@0: C1 = _mm_srli_epi64(T2, 26); C2 = _mm_srli_epi64(T0, 26); T2 = _mm_and_si128(T2, MMASK); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_add_epi64(T3, C1); T1 = _mm_add_epi64(T1, C2); andre@0: C1 = _mm_srli_epi64(T3, 26); T3 = _mm_and_si128(T3, MMASK); T4 = _mm_add_epi64(T4, C1); andre@0: andre@0: /* H = (H*[r^2,r^2] + [Mx,My]) */ andre@0: H0 = T0; andre@0: H1 = T1; andre@0: H2 = T2; andre@0: H3 = T3; andre@0: H4 = T4; andre@0: andre@0: consumed = 32; andre@0: } andre@0: andre@0: /* finalize, H *= [r^2,r] */ andre@0: r0 = ((uint64_t)p->R20.d[3] << 32) | (uint64_t)p->R20.d[1]; andre@0: r1 = ((uint64_t)p->R21.d[3] << 32) | (uint64_t)p->R21.d[1]; andre@0: r2 = ((uint64_t)p->R22.d[3] << 32) | (uint64_t)p->R22.d[1]; andre@0: andre@0: p->R20.d[2] = (uint32_t)( r0 ) & 0x3ffffff; andre@0: p->R21.d[2] = (uint32_t)((r0 >> 26) | (r1 << 18)) & 0x3ffffff; andre@0: p->R22.d[2] = (uint32_t)((r1 >> 8) ) & 0x3ffffff; andre@0: p->R23.d[2] = (uint32_t)((r1 >> 34) | (r2 << 10)) & 0x3ffffff; andre@0: p->R24.d[2] = (uint32_t)((r2 >> 16) ) ; andre@0: p->S21.d[2] = p->R21.d[2] * 5; andre@0: p->S22.d[2] = p->R22.d[2] * 5; andre@0: p->S23.d[2] = p->R23.d[2] * 5; andre@0: p->S24.d[2] = p->R24.d[2] * 5; andre@0: andre@0: /* H *= [r^2,r] */ andre@0: T0 = _mm_mul_epu32(H0, p->R20.v); andre@0: T1 = _mm_mul_epu32(H0, p->R21.v); andre@0: T2 = _mm_mul_epu32(H0, p->R22.v); andre@0: T3 = _mm_mul_epu32(H0, p->R23.v); andre@0: T4 = _mm_mul_epu32(H0, p->R24.v); andre@0: T5 = _mm_mul_epu32(H1, p->S24.v); T6 = _mm_mul_epu32(H1, p->R20.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H2, p->S23.v); T6 = _mm_mul_epu32(H2, p->S24.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H3, p->S22.v); T6 = _mm_mul_epu32(H3, p->S23.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H4, p->S21.v); T6 = _mm_mul_epu32(H4, p->S22.v); T0 = _mm_add_epi64(T0, T5); T1 = _mm_add_epi64(T1, T6); andre@0: T5 = _mm_mul_epu32(H1, p->R21.v); T6 = _mm_mul_epu32(H1, p->R22.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H2, p->R20.v); T6 = _mm_mul_epu32(H2, p->R21.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H3, p->S24.v); T6 = _mm_mul_epu32(H3, p->R20.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H4, p->S23.v); T6 = _mm_mul_epu32(H4, p->S24.v); T2 = _mm_add_epi64(T2, T5); T3 = _mm_add_epi64(T3, T6); andre@0: T5 = _mm_mul_epu32(H1, p->R23.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(H2, p->R22.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(H3, p->R21.v); T4 = _mm_add_epi64(T4, T5); andre@0: T5 = _mm_mul_epu32(H4, p->R20.v); T4 = _mm_add_epi64(T4, T5); andre@0: andre@0: C1 = _mm_srli_epi64(T0, 26); C2 = _mm_srli_epi64(T3, 26); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_and_si128(T3, MMASK); T1 = _mm_add_epi64(T1, C1); T4 = _mm_add_epi64(T4, C2); andre@0: C1 = _mm_srli_epi64(T1, 26); C2 = _mm_srli_epi64(T4, 26); T1 = _mm_and_si128(T1, MMASK); T4 = _mm_and_si128(T4, MMASK); T2 = _mm_add_epi64(T2, C1); T0 = _mm_add_epi64(T0, _mm_mul_epu32(C2, FIVE)); andre@0: C1 = _mm_srli_epi64(T2, 26); C2 = _mm_srli_epi64(T0, 26); T2 = _mm_and_si128(T2, MMASK); T0 = _mm_and_si128(T0, MMASK); T3 = _mm_add_epi64(T3, C1); T1 = _mm_add_epi64(T1, C2); andre@0: C1 = _mm_srli_epi64(T3, 26); T3 = _mm_and_si128(T3, MMASK); T4 = _mm_add_epi64(T4, C1); andre@0: andre@0: /* H = H[0]+H[1] */ andre@0: H0 = _mm_add_epi64(T0, _mm_srli_si128(T0, 8)); andre@0: H1 = _mm_add_epi64(T1, _mm_srli_si128(T1, 8)); andre@0: H2 = _mm_add_epi64(T2, _mm_srli_si128(T2, 8)); andre@0: H3 = _mm_add_epi64(T3, _mm_srli_si128(T3, 8)); andre@0: H4 = _mm_add_epi64(T4, _mm_srli_si128(T4, 8)); andre@0: andre@0: t0 = _mm_cvtsi128_si32(H0) ; c = (t0 >> 26); t0 &= 0x3ffffff; andre@0: t1 = _mm_cvtsi128_si32(H1) + c; c = (t1 >> 26); t1 &= 0x3ffffff; andre@0: t2 = _mm_cvtsi128_si32(H2) + c; c = (t2 >> 26); t2 &= 0x3ffffff; andre@0: t3 = _mm_cvtsi128_si32(H3) + c; c = (t3 >> 26); t3 &= 0x3ffffff; andre@0: t4 = _mm_cvtsi128_si32(H4) + c; c = (t4 >> 26); t4 &= 0x3ffffff; andre@0: t0 = t0 + (c * 5); c = (t0 >> 26); t0 &= 0x3ffffff; andre@0: t1 = t1 + c; andre@0: andre@0: st->HH[0] = ((t0 ) | (t1 << 26) ) & 0xfffffffffffull; andre@0: st->HH[1] = ((t1 >> 18) | (t2 << 8) | (t3 << 34)) & 0xfffffffffffull; andre@0: st->HH[2] = ((t3 >> 10) | (t4 << 16) ) & 0x3ffffffffffull; andre@0: andre@0: return consumed; andre@0: } andre@0: andre@0: void andre@0: Poly1305Update(poly1305_state *state, const unsigned char *m, size_t bytes) { andre@0: poly1305_state_internal *st = poly1305_aligned_state(state); andre@0: size_t want; andre@0: andre@0: /* need at least 32 initial bytes to start the accelerated branch */ andre@0: if (!st->started) { andre@0: if ((st->leftover == 0) && (bytes > 32)) { andre@0: poly1305_first_block(st, m); andre@0: m += 32; andre@0: bytes -= 32; andre@0: } else { andre@0: want = poly1305_min(32 - st->leftover, bytes); andre@0: poly1305_block_copy(st->buffer + st->leftover, m, want); andre@0: bytes -= want; andre@0: m += want; andre@0: st->leftover += want; andre@0: if ((st->leftover < 32) || (bytes == 0)) andre@0: return; andre@0: poly1305_first_block(st, st->buffer); andre@0: st->leftover = 0; andre@0: } andre@0: st->started = 1; andre@0: } andre@0: andre@0: /* handle leftover */ andre@0: if (st->leftover) { andre@0: want = poly1305_min(64 - st->leftover, bytes); andre@0: poly1305_block_copy(st->buffer + st->leftover, m, want); andre@0: bytes -= want; andre@0: m += want; andre@0: st->leftover += want; andre@0: if (st->leftover < 64) andre@0: return; andre@0: poly1305_blocks(st, st->buffer, 64); andre@0: st->leftover = 0; andre@0: } andre@0: andre@0: /* process 64 byte blocks */ andre@0: if (bytes >= 64) { andre@0: want = (bytes & ~63); andre@0: poly1305_blocks(st, m, want); andre@0: m += want; andre@0: bytes -= want; andre@0: } andre@0: andre@0: if (bytes) { andre@0: poly1305_block_copy(st->buffer + st->leftover, m, bytes); andre@0: st->leftover += bytes; andre@0: } andre@0: } andre@0: andre@0: void andre@0: Poly1305Finish(poly1305_state *state, unsigned char mac[16]) { andre@0: poly1305_state_internal *st = poly1305_aligned_state(state); andre@0: size_t leftover = st->leftover; andre@0: uint8_t *m = st->buffer; andre@0: uint128_t d[3]; andre@0: uint64_t h0,h1,h2; andre@0: uint64_t t0,t1; andre@0: uint64_t g0,g1,g2,c,nc; andre@0: uint64_t r0,r1,r2,s1,s2; andre@0: poly1305_power *p; andre@0: andre@0: if (st->started) { andre@0: size_t consumed = poly1305_combine(st, m, leftover); andre@0: leftover -= consumed; andre@0: m += consumed; andre@0: } andre@0: andre@0: /* st->HH will either be 0 or have the combined result */ andre@0: h0 = st->HH[0]; andre@0: h1 = st->HH[1]; andre@0: h2 = st->HH[2]; andre@0: andre@0: p = &st->P[1]; andre@0: r0 = ((uint64_t)p->R20.d[3] << 32) | (uint64_t)p->R20.d[1]; andre@0: r1 = ((uint64_t)p->R21.d[3] << 32) | (uint64_t)p->R21.d[1]; andre@0: r2 = ((uint64_t)p->R22.d[3] << 32) | (uint64_t)p->R22.d[1]; andre@0: s1 = r1 * (5 << 2); andre@0: s2 = r2 * (5 << 2); andre@0: andre@0: if (leftover < 16) andre@0: goto poly1305_donna_atmost15bytes; andre@0: andre@0: poly1305_donna_atleast16bytes: andre@0: t0 = U8TO64_LE(m + 0); andre@0: t1 = U8TO64_LE(m + 8); andre@0: h0 += t0 & 0xfffffffffff; andre@0: t0 = shr128_pair(t1, t0, 44); andre@0: h1 += t0 & 0xfffffffffff; andre@0: h2 += (t1 >> 24) | ((uint64_t)1 << 40); andre@0: andre@0: poly1305_donna_mul: andre@0: d[0] = add128(add128(mul64x64_128(h0, r0), mul64x64_128(h1, s2)), mul64x64_128(h2, s1)); andre@0: d[1] = add128(add128(mul64x64_128(h0, r1), mul64x64_128(h1, r0)), mul64x64_128(h2, s2)); andre@0: d[2] = add128(add128(mul64x64_128(h0, r2), mul64x64_128(h1, r1)), mul64x64_128(h2, r0)); andre@0: h0 = lo128(d[0]) & 0xfffffffffff; c = shr128(d[0], 44); andre@0: d[1] = add128_64(d[1], c); h1 = lo128(d[1]) & 0xfffffffffff; c = shr128(d[1], 44); andre@0: d[2] = add128_64(d[2], c); h2 = lo128(d[2]) & 0x3ffffffffff; c = shr128(d[2], 42); andre@0: h0 += c * 5; andre@0: andre@0: m += 16; andre@0: leftover -= 16; andre@0: if (leftover >= 16) goto poly1305_donna_atleast16bytes; andre@0: andre@0: /* final bytes */ andre@0: poly1305_donna_atmost15bytes: andre@0: if (!leftover) goto poly1305_donna_finish; andre@0: andre@0: m[leftover++] = 1; andre@0: poly1305_block_zero(m + leftover, 16 - leftover); andre@0: leftover = 16; andre@0: andre@0: t0 = U8TO64_LE(m+0); andre@0: t1 = U8TO64_LE(m+8); andre@0: h0 += t0 & 0xfffffffffff; t0 = shr128_pair(t1, t0, 44); andre@0: h1 += t0 & 0xfffffffffff; andre@0: h2 += (t1 >> 24); andre@0: andre@0: goto poly1305_donna_mul; andre@0: andre@0: poly1305_donna_finish: andre@0: c = (h0 >> 44); h0 &= 0xfffffffffff; andre@0: h1 += c; c = (h1 >> 44); h1 &= 0xfffffffffff; andre@0: h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff; andre@0: h0 += c * 5; andre@0: andre@0: g0 = h0 + 5; c = (g0 >> 44); g0 &= 0xfffffffffff; andre@0: g1 = h1 + c; c = (g1 >> 44); g1 &= 0xfffffffffff; andre@0: g2 = h2 + c - ((uint64_t)1 << 42); andre@0: andre@0: c = (g2 >> 63) - 1; andre@0: nc = ~c; andre@0: h0 = (h0 & nc) | (g0 & c); andre@0: h1 = (h1 & nc) | (g1 & c); andre@0: h2 = (h2 & nc) | (g2 & c); andre@0: andre@0: /* pad */ andre@0: t0 = ((uint64_t)p->R23.d[3] << 32) | (uint64_t)p->R23.d[1]; andre@0: t1 = ((uint64_t)p->R24.d[3] << 32) | (uint64_t)p->R24.d[1]; andre@0: h0 += (t0 & 0xfffffffffff) ; c = (h0 >> 44); h0 &= 0xfffffffffff; t0 = shr128_pair(t1, t0, 44); andre@0: h1 += (t0 & 0xfffffffffff) + c; c = (h1 >> 44); h1 &= 0xfffffffffff; t1 = (t1 >> 24); andre@0: h2 += (t1 ) + c; andre@0: andre@0: U64TO8_LE(mac + 0, ((h0 ) | (h1 << 44))); andre@0: U64TO8_LE(mac + 8, ((h1 >> 20) | (h2 << 24))); andre@0: }