Added "ctmulq" implementation of Poly1305 (using 64->128 multiplications when available).

This commit is contained in:
Thomas Pornin 2017-03-18 16:46:00 +01:00
parent 2f88a67304
commit 5db2d48b12
10 changed files with 588 additions and 17 deletions

View File

@ -1768,4 +1768,38 @@ void br_poly1305_i15_run(const void *key, const void *iv,
void *data, size_t len, const void *aad, size_t aad_len,
void *tag, br_chacha20_run ichacha, int encrypt);
/**
* \brief ChaCha20+Poly1305 AEAD implementation (ctmulq).
*
* This implementation uses 64-bit multiplications (result over 128 bits).
* It is available only on platforms that offer such a primitive (in
* practice, 64-bit architectures). Use `br_poly1305_ctmulq_get()` to
* dynamically obtain a pointer to that function, or 0 if not supported.
*
* \see br_poly1305_run
*
* \param key secret key (32 bytes).
* \param iv nonce (12 bytes).
* \param data data to encrypt or decrypt.
* \param len data length (in bytes).
* \param aad additional authenticated data.
* \param aad_len length of additional authenticated data (in bytes).
* \param tag output buffer for the authentication tag.
* \param ichacha implementation of ChaCha20.
* \param encrypt non-zero for encryption, zero for decryption.
*/
void br_poly1305_ctmulq_run(const void *key, const void *iv,
void *data, size_t len, const void *aad, size_t aad_len,
void *tag, br_chacha20_run ichacha, int encrypt);
/**
* \brief Get the ChaCha20+Poly1305 "ctmulq" implementation, if available.
*
* This function returns a pointer to the `br_poly1305_ctmulq_run()`
* function if supported on the current platform; otherwise, it returns 0.
*
* \return the ctmulq ChaCha20+Poly1305 implementation, or 0.
*/
br_poly1305_run br_poly1305_ctmulq_get(void);
#endif

File diff suppressed because one or more lines are too long

View File

@ -259,6 +259,7 @@ coresrc=" \
src/symcipher/des_tab_cbcenc.c \
src/symcipher/poly1305_ctmul.c \
src/symcipher/poly1305_ctmul32.c \
src/symcipher/poly1305_ctmulq.c \
src/symcipher/poly1305_i15.c \
src/x509/skey_decoder.c \
src/x509/x509_decoder.c \

View File

@ -172,4 +172,21 @@
#define BR_POWER8 1
*/
/*
* When BR_INT128 is enabled, then code using the 'unsigned __int64'
* and 'unsigned __int128' types will be used to leverage 64x64->128
* unsigned multiplications. This should work with GCC and compatible
* compilers on 64-bit architectures.
*
#define BR_INT128 1
*/
/*
* When BR_UMUL128 is enabled, then code using the '_umul128()' and
* '_addcarry_u64()' intrinsics will be used to implement 64x64->128
* unsigned multiplications. This should work on Visual C on x64 systems.
*
#define BR_UMUL128 1
*/
#endif

View File

@ -209,6 +209,17 @@
#endif
#endif
/*
* Detect support for 128-bit integers.
*/
#if !defined BR_INT128 && !defined BR_UMUL128
#ifdef __SIZEOF_INT128__
#define BR_INT128 1
#elif _M_X64
#define BR_UMUL128 1
#endif
#endif
/* ==================================================================== */
/*
* Encoding/decoding functions.

View File

@ -28,10 +28,21 @@
void
br_ssl_engine_set_default_chapol(br_ssl_engine_context *cc)
{
#if BR_INT128 || BR_UMUL128
br_poly1305_run bp;
#endif
br_ssl_engine_set_chapol(cc,
&br_sslrec_in_chapol_vtable,
&br_sslrec_out_chapol_vtable);
br_ssl_engine_set_chacha20(cc, &br_chacha20_ct_run);
#if BR_INT128 || BR_UMUL128
bp = br_poly1305_ctmulq_get();
if (bp) {
br_ssl_engine_set_poly1305(cc, bp);
return;
}
#endif
#if BR_LOMUL
br_ssl_engine_set_poly1305(cc, &br_poly1305_ctmul32_run);
#else

View File

@ -0,0 +1,475 @@
/*
* Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "inner.h"
#if BR_INT128 || BR_UMUL128
#if BR_INT128
#define MUL128(hi, lo, x, y) do { \
unsigned __int128 mul128tmp; \
mul128tmp = (unsigned __int128)(x) * (unsigned __int128)(y); \
(hi) = (uint64_t)(mul128tmp >> 64); \
(lo) = (uint64_t)mul128tmp; \
} while (0)
#elif BR_UMUL128
#include <intrin.h>
#define MUL128(hi, lo, x, y) do { \
(lo) = _umul128((x), (y), &(hi)); \
} while (0)
#endif
#define MASK42 ((uint64_t)0x000003FFFFFFFFFF)
#define MASK44 ((uint64_t)0x00000FFFFFFFFFFF)
/*
* The "accumulator" word is nominally a 130-bit value. We split it into
* words of 44 bits, each held in a 64-bit variable.
*
* If the current accumulator is a = a0 + a1*W + a2*W^2 (where W = 2^44)
* and r = r0 + r1*W + r2*W^2, then:
*
* a*r = (a0*r0)
* + (a0*r1 + a1*r0) * W
* + (a0*r2 + a1*r1 + a2*r0) * W^2
* + (a1*r2 + a2*r1) * W^3
* + (a2*r2) * W^4
*
* We want to reduce that value modulo p = 2^130-5, so W^3 = 20 mod p,
* and W^4 = 20*W mod p. Thus, if we define u1 = 20*r1 and u2 = 20*r2,
* then the equations above become:
*
* b0 = a0*r0 + a1*u2 + a2*u1
* b1 = a0*r1 + a1*r0 + a2*u2
* b2 = a0*r2 + a1*r1 + a2*r0
*
* In order to make u1 fit in 44 bits, we can change these equations
* into:
*
* b0 = a0*r0 + a1*u2 + a2*t1
* b1 = a0*r1 + a1*r0 + a2*t2
* b2 = a0*r2 + a1*r1 + a2*r0
*
* Where t1 is u1 truncated to 44 bits, and t2 is u2 added to the extra
* bits of u1. Note that since r is clamped down to a 124-bit value, the
* values u2 and t2 fit on 44 bits too.
*
* The bx values are larger than 44 bits, so we may split them into a
* lower half (cx, 44 bits) and an upper half (dx). The new values for
* the accumulator are then:
*
* e0 = c0 + 20*d2
* e1 = c1 + d0
* e2 = c2 + d1
*
* The equations allow for some room, i.e. the ax values may be larger
* than 44 bits. Similarly, the ex values will usually be larger than
* the ax. Thus, some sort of carry propagation must be done regularly,
* though not necessarily at each iteration. In particular, we do not
* need to compute the additions (for the bx values) over 128-bit
* quantities; we can stick to 64-bit computations.
*
*
* Since the 128-bit result of a 64x64 multiplication is actually
* represented over two 64-bit registers, it is cheaper to arrange for
* any split that happens between the "high" and "low" halves to be on
* that 64-bit boundary. This is done by left shifting the rx, ux and tx
* by 20 bits (since they all fit on 44 bits each, this shift is
* always possible).
*/
static void
poly1305_inner_big(uint64_t *acc, uint64_t *r, const void *data, size_t len)
{
#define MX(hi, lo, m0, m1, m2) do { \
uint64_t mxhi, mxlo; \
MUL128(mxhi, mxlo, a0, m0); \
(hi) = mxhi; \
(lo) = mxlo >> 20; \
MUL128(mxhi, mxlo, a1, m1); \
(hi) += mxhi; \
(lo) += mxlo >> 20; \
MUL128(mxhi, mxlo, a2, m2); \
(hi) += mxhi; \
(lo) += mxlo >> 20; \
} while (0)
const unsigned char *buf;
uint64_t a0, a1, a2;
uint64_t r0, r1, r2, t1, t2, u2;
r0 = r[0];
r1 = r[1];
r2 = r[2];
t1 = r[3];
t2 = r[4];
u2 = r[5];
a0 = acc[0];
a1 = acc[1];
a2 = acc[2];
buf = data;
while (len > 0) {
uint64_t v0, v1, v2;
uint64_t c0, c1, c2, d0, d1, d2;
v0 = br_dec64le(buf + 0);
v1 = br_dec64le(buf + 8);
v2 = v1 >> 24;
v1 = ((v0 >> 44) | (v1 << 20)) & MASK44;
v0 &= MASK44;
a0 += v0;
a1 += v1;
a2 += v2 + ((uint64_t)1 << 40);
MX(d0, c0, r0, u2, t1);
MX(d1, c1, r1, r0, t2);
MX(d2, c2, r2, r1, r0);
a0 = c0 + 20 * d2;
a1 = c1 + d0;
a2 = c2 + d1;
v0 = br_dec64le(buf + 16);
v1 = br_dec64le(buf + 24);
v2 = v1 >> 24;
v1 = ((v0 >> 44) | (v1 << 20)) & MASK44;
v0 &= MASK44;
a0 += v0;
a1 += v1;
a2 += v2 + ((uint64_t)1 << 40);
MX(d0, c0, r0, u2, t1);
MX(d1, c1, r1, r0, t2);
MX(d2, c2, r2, r1, r0);
a0 = c0 + 20 * d2;
a1 = c1 + d0;
a2 = c2 + d1;
v0 = br_dec64le(buf + 32);
v1 = br_dec64le(buf + 40);
v2 = v1 >> 24;
v1 = ((v0 >> 44) | (v1 << 20)) & MASK44;
v0 &= MASK44;
a0 += v0;
a1 += v1;
a2 += v2 + ((uint64_t)1 << 40);
MX(d0, c0, r0, u2, t1);
MX(d1, c1, r1, r0, t2);
MX(d2, c2, r2, r1, r0);
a0 = c0 + 20 * d2;
a1 = c1 + d0;
a2 = c2 + d1;
v0 = br_dec64le(buf + 48);
v1 = br_dec64le(buf + 56);
v2 = v1 >> 24;
v1 = ((v0 >> 44) | (v1 << 20)) & MASK44;
v0 &= MASK44;
a0 += v0;
a1 += v1;
a2 += v2 + ((uint64_t)1 << 40);
MX(d0, c0, r0, u2, t1);
MX(d1, c1, r1, r0, t2);
MX(d2, c2, r2, r1, r0);
a0 = c0 + 20 * d2;
a1 = c1 + d0;
a2 = c2 + d1;
a1 += a0 >> 44;
a0 &= MASK44;
a2 += a1 >> 44;
a1 &= MASK44;
a0 += 20 * (a2 >> 44);
a2 &= MASK44;
buf += 64;
len -= 64;
}
acc[0] = a0;
acc[1] = a1;
acc[2] = a2;
#undef MX
}
static void
poly1305_inner_small(uint64_t *acc, uint64_t *r, const void *data, size_t len)
{
const unsigned char *buf;
uint64_t a0, a1, a2;
uint64_t r0, r1, r2, t1, t2, u2;
r0 = r[0];
r1 = r[1];
r2 = r[2];
t1 = r[3];
t2 = r[4];
u2 = r[5];
a0 = acc[0];
a1 = acc[1];
a2 = acc[2];
buf = data;
while (len > 0) {
uint64_t v0, v1, v2;
uint64_t c0, c1, c2, d0, d1, d2;
unsigned char tmp[16];
if (len < 16) {
memcpy(tmp, buf, len);
memset(tmp + len, 0, (sizeof tmp) - len);
buf = tmp;
len = 16;
}
v0 = br_dec64le(buf + 0);
v1 = br_dec64le(buf + 8);
v2 = v1 >> 24;
v1 = ((v0 >> 44) | (v1 << 20)) & MASK44;
v0 &= MASK44;
a0 += v0;
a1 += v1;
a2 += v2 + ((uint64_t)1 << 40);
#define MX(hi, lo, m0, m1, m2) do { \
uint64_t mxhi, mxlo; \
MUL128(mxhi, mxlo, a0, m0); \
(hi) = mxhi; \
(lo) = mxlo >> 20; \
MUL128(mxhi, mxlo, a1, m1); \
(hi) += mxhi; \
(lo) += mxlo >> 20; \
MUL128(mxhi, mxlo, a2, m2); \
(hi) += mxhi; \
(lo) += mxlo >> 20; \
} while (0)
MX(d0, c0, r0, u2, t1);
MX(d1, c1, r1, r0, t2);
MX(d2, c2, r2, r1, r0);
#undef MX
a0 = c0 + 20 * d2;
a1 = c1 + d0;
a2 = c2 + d1;
a1 += a0 >> 44;
a0 &= MASK44;
a2 += a1 >> 44;
a1 &= MASK44;
a0 += 20 * (a2 >> 44);
a2 &= MASK44;
buf += 16;
len -= 16;
}
acc[0] = a0;
acc[1] = a1;
acc[2] = a2;
}
static inline void
poly1305_inner(uint64_t *acc, uint64_t *r, const void *data, size_t len)
{
if (len >= 64) {
size_t len2;
len2 = len & ~(size_t)63;
poly1305_inner_big(acc, r, data, len2);
data = (const unsigned char *)data + len2;
len -= len2;
}
if (len > 0) {
poly1305_inner_small(acc, r, data, len);
}
}
/* see bearssl_block.h */
void
br_poly1305_ctmulq_run(const void *key, const void *iv,
void *data, size_t len, const void *aad, size_t aad_len,
void *tag, br_chacha20_run ichacha, int encrypt)
{
unsigned char pkey[32], foot[16];
uint64_t r[6], acc[3], r0, r1;
uint32_t v0, v1, v2, v3, v4;
uint64_t w0, w1, w2, w3;
uint32_t ctl;
/*
* Compute the MAC key. The 'r' value is the first 16 bytes of
* pkey[].
*/
memset(pkey, 0, sizeof pkey);
ichacha(key, iv, 0, pkey, sizeof pkey);
/*
* If encrypting, ChaCha20 must run first, followed by Poly1305.
* When decrypting, the operations are reversed.
*/
if (encrypt) {
ichacha(key, iv, 1, data, len);
}
/*
* Run Poly1305. We must process the AAD, then ciphertext, then
* the footer (with the lengths). Note that the AAD and ciphertext
* are meant to be padded with zeros up to the next multiple of 16,
* and the length of the footer is 16 bytes as well.
*/
/*
* Apply the "clamping" on r.
*/
pkey[ 3] &= 0x0F;
pkey[ 4] &= 0xFC;
pkey[ 7] &= 0x0F;
pkey[ 8] &= 0xFC;
pkey[11] &= 0x0F;
pkey[12] &= 0xFC;
pkey[15] &= 0x0F;
/*
* Decode the 'r' value into 44-bit words, left-shifted by 20 bits.
* Also compute the u1 and u2 values.
*/
r0 = br_dec64le(pkey + 0);
r1 = br_dec64le(pkey + 8);
r[0] = r0 << 20;
r[1] = ((r0 >> 24) | (r1 << 40)) & ~(uint64_t)0xFFFFF;
r[2] = (r1 >> 4) & ~(uint64_t)0xFFFFF;
r1 = 20 * (r[1] >> 20);
r[3] = r1 << 20;
r[5] = 20 * r[2];
r[4] = (r[5] + (r1 >> 24)) & ~(uint64_t)0xFFFFF;
/*
* Accumulator is 0.
*/
acc[0] = 0;
acc[1] = 0;
acc[2] = 0;
/*
* Process the additional authenticated data, ciphertext, and
* footer in due order.
*/
br_enc64le(foot, (uint64_t)aad_len);
br_enc64le(foot + 8, (uint64_t)len);
poly1305_inner(acc, r, aad, aad_len);
poly1305_inner(acc, r, data, len);
poly1305_inner_small(acc, r, foot, sizeof foot);
/*
* Finalise modular reduction. At that point, the value consists
* in three 44-bit values (the lowest one might be slightly above
* 2^44). Two loops shall be sufficient.
*/
acc[1] += (acc[0] >> 44);
acc[0] &= MASK44;
acc[2] += (acc[1] >> 44);
acc[1] &= MASK44;
acc[0] += 5 * (acc[2] >> 42);
acc[2] &= MASK42;
acc[1] += (acc[0] >> 44);
acc[0] &= MASK44;
acc[2] += (acc[1] >> 44);
acc[1] &= MASK44;
acc[0] += 5 * (acc[2] >> 42);
acc[2] &= MASK42;
/*
* The value may still fall in the 2^130-5..2^130-1 range, in
* which case we must reduce it again. The code below selects,
* in constant-time, between 'acc' and 'acc-p'. We encode the
* value over four 32-bit integers to finish the operation.
*/
v0 = (uint32_t)acc[0];
v1 = (uint32_t)(acc[0] >> 32) | ((uint32_t)acc[1] << 12);
v2 = (uint32_t)(acc[1] >> 20) | ((uint32_t)acc[2] << 24);
v3 = (uint32_t)(acc[2] >> 8);
v4 = (uint32_t)(acc[2] >> 40);
ctl = GT(v0, 0xFFFFFFFA);
ctl &= EQ(v1, 0xFFFFFFFF);
ctl &= EQ(v2, 0xFFFFFFFF);
ctl &= EQ(v3, 0xFFFFFFFF);
ctl &= EQ(v4, 0x00000003);
v0 = MUX(ctl, v0 + 5, v0);
v1 = MUX(ctl, 0, v1);
v2 = MUX(ctl, 0, v2);
v3 = MUX(ctl, 0, v3);
/*
* Add the "s" value. This is done modulo 2^128. Don't forget
* carry propagation...
*/
w0 = (uint64_t)v0 + (uint64_t)br_dec32le(pkey + 16);
w1 = (uint64_t)v1 + (uint64_t)br_dec32le(pkey + 20) + (w0 >> 32);
w2 = (uint64_t)v2 + (uint64_t)br_dec32le(pkey + 24) + (w1 >> 32);
w3 = (uint64_t)v3 + (uint64_t)br_dec32le(pkey + 28) + (w2 >> 32);
v0 = (uint32_t)w0;
v1 = (uint32_t)w1;
v2 = (uint32_t)w2;
v3 = (uint32_t)w3;
/*
* Encode the tag.
*/
br_enc32le((unsigned char *)tag + 0, v0);
br_enc32le((unsigned char *)tag + 4, v1);
br_enc32le((unsigned char *)tag + 8, v2);
br_enc32le((unsigned char *)tag + 12, v3);
/*
* If decrypting, then ChaCha20 runs _after_ Poly1305.
*/
if (!encrypt) {
ichacha(key, iv, 1, data, len);
}
}
/* see bearssl_block.h */
br_poly1305_run
br_poly1305_ctmulq_get(void)
{
return &br_poly1305_ctmulq_run;
}
#else
/* see bearssl_block.h */
br_poly1305_run
br_poly1305_ctmulq_get(void)
{
return 0;
}
#endif

View File

@ -4190,24 +4190,12 @@ test_Poly1305_inner(const char *name, br_poly1305_run ipoly,
memcpy(data, plain, len);
ipoly(key, nonce, data, len,
aad, aad_len, tmp, br_chacha20_ct_run, 1);
if (memcmp(data, cipher, len) != 0) {
fprintf(stderr, "ChaCha20+Poly1305 KAT failed (1)\n");
exit(EXIT_FAILURE);
}
if (memcmp(tmp, tag, 16) != 0) {
fprintf(stderr, "ChaCha20+Poly1305 KAT failed (2)\n");
exit(EXIT_FAILURE);
}
check_equals("ChaCha20+Poly1305 KAT (1)", data, cipher, len);
check_equals("ChaCha20+Poly1305 KAT (2)", tmp, tag, 16);
ipoly(key, nonce, data, len,
aad, aad_len, tmp, br_chacha20_ct_run, 0);
if (memcmp(data, plain, len) != 0) {
fprintf(stderr, "ChaCha20+Poly1305 KAT failed (3)\n");
exit(EXIT_FAILURE);
}
if (memcmp(tmp, tag, 16) != 0) {
fprintf(stderr, "ChaCha20+Poly1305 KAT failed (4)\n");
exit(EXIT_FAILURE);
}
check_equals("ChaCha20+Poly1305 KAT (3)", data, plain, len);
check_equals("ChaCha20+Poly1305 KAT (4)", tmp, tag, 16);
printf(".");
fflush(stdout);
@ -4273,6 +4261,20 @@ test_Poly1305_i15(void)
&br_poly1305_ctmul_run);
}
static void
test_Poly1305_ctmulq(void)
{
br_poly1305_run bp;
bp = br_poly1305_ctmulq_get();
if (bp == 0) {
printf("Test Poly1305_ctmulq: UNAVAILABLE\n");
} else {
test_Poly1305_inner("Poly1305_ctmulq", bp,
&br_poly1305_ctmul_run);
}
}
/*
* A 1024-bit RSA key, generated with OpenSSL.
*/
@ -5670,6 +5672,7 @@ static const struct {
STU(ChaCha20_ct),
STU(Poly1305_ctmul),
STU(Poly1305_ctmul32),
STU(Poly1305_ctmulq),
STU(Poly1305_i15),
STU(RSA_i15),
STU(RSA_i31),

View File

@ -416,6 +416,19 @@ test_speed_poly1305_ctmul32(void)
&br_poly1305_ctmul32_run);
}
static void
test_speed_poly1305_ctmulq(void)
{
br_poly1305_run bp;
bp = br_poly1305_ctmulq_get();
if (bp == 0) {
printf("%-30s UNAVAILABLE\n", "Poly1305 (ctmulq)");
} else {
test_speed_poly1305_inner("Poly1305 (ctmulq)", bp);
}
}
static void
test_speed_poly1305_i15(void)
{
@ -1260,6 +1273,7 @@ static const struct {
STU(poly1305_ctmul),
STU(poly1305_ctmul32),
STU(poly1305_ctmulq),
STU(poly1305_i15),
STU(rsa_i15),

View File

@ -422,6 +422,8 @@ static const struct {
(const void *(*)(void))&br_ghash_pclmul_get },
{ "ghash_pwr8", "pwr8",
(const void *(*)(void))&br_ghash_pwr8_get },
{ "poly1305_ctmulq", "ctmulq",
(const void *(*)(void))&br_poly1305_ctmulq_get },
{ 0, 0, 0, }
};