| 1 | /* =================================================================== |
| 2 | * |
| 3 | * Copyright (c) 2018, Helder Eijs <[email protected]> |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 21 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 27 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | * =================================================================== |
| 30 | */ |
| 31 | |
| 32 | #include "common.h" |
| 33 | |
| 34 | FAKE_INIT(ghash_clmul) |
| 35 | |
| 36 | #if defined(HAVE_INTRIN_H) |
| 37 | #include <intrin.h> |
| 38 | #elif defined(HAVE_X86INTRIN_H) |
| 39 | #include <x86intrin.h> |
| 40 | #elif defined(HAVE_EMMINTRIN_H) |
| 41 | #include <xmmintrin.h> |
| 42 | #include <emmintrin.h> |
| 43 | #else |
| 44 | #error No SSE2 headers available |
| 45 | #endif |
| 46 | |
| 47 | #if defined(HAVE_WMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) |
| 48 | #include <wmmintrin.h> |
| 49 | #include <tmmintrin.h> |
| 50 | #else |
| 51 | #error No CLMUL headers available |
| 52 | #endif |
| 53 | |
| 54 | /** |
| 55 | * This module implement the basic GHASH multiplication, as described in |
| 56 | * NIST SP 800-38D. |
| 57 | * |
| 58 | * At the core, we perform a binary polynomial multiplication (carry-less) |
| 59 | * modulo P(x) = x^128 + x^7 + x + 1, that is a finite field multiplication in |
| 60 | * GF(2^128). |
| 61 | * |
| 62 | * The GCM standard requires that coefficients of the two polynomials are encoded |
| 63 | * little-endian byte-wise but also (oddly enough) bit-wise (i.e. within a byte). |
| 64 | * |
| 65 | * In other words, the unity element x is encoded in memory as: |
| 66 | * |
| 67 | * 0x80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 = (binary) 100000.....0 |
| 68 | * |
| 69 | * Note how the least significant bit (LSB) is the leftmost one in the first byte. |
| 70 | * |
| 71 | * In order to use the native CPU instructions though, the conventional representation of |
| 72 | * bits within a byte (with leftmost bit being the most significant) must be in place. |
| 73 | * |
| 74 | * To that end, instead of performing expensive bit-swapping, computing the |
| 75 | * multiplication, and then bit-swapping again, we can use an equivalent |
| 76 | * expression (see [2][3]) that operates directly on *bit-reflected* data. |
| 77 | * Such expression interprets the original encoding of the factors as if the |
| 78 | * rightmost bit was the most significant. |
| 79 | * |
| 80 | * The new expression A * (B * x) * x^{-128} modulo p(x) = x^128 + x^127 + x^126 + x^121 + 1 |
| 81 | * |
| 82 | * For instance, what used to be the unity element x for the original multiplication, |
| 83 | * is now the value x^127 for the equivalent expression. Within each byte, the |
| 84 | * leftmost bit is the most significant as desired. |
| 85 | * |
| 86 | * However, this also means that bytes end up encoded big-endian in memory. In |
| 87 | * order to use x86 arithmetic (and the XMM registers), data must be |
| 88 | * byte-swapped when loaded from or stored into memory. |
| 89 | * |
| 90 | * References: |
| 91 | * [1] http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.447.379&rep=rep1&type=pdf |
| 92 | * [2] https://crypto.stanford.edu/RealWorldCrypto/slides/gueron.pdf |
| 93 | * [3] https://blog.quarkslab.com/reversing-a-finite-field-multiplication-optimization.html |
| 94 | */ |
| 95 | |
| 96 | struct exp_key { |
| 97 | /** |
| 98 | * Powers of H (already swapped in byte endianness, and pre-multiplied by X) |
| 99 | * |
| 100 | * h[0] is x*H mod p(x) |
| 101 | * h[1] is x*H^2 mod p(x) |
| 102 | * h[2] is x*H^3 mod p(x) |
| 103 | * h[3] is x*H^4 mod p(x) |
| 104 | */ |
| 105 | __m128i h[4]; |
| 106 | }; |
| 107 | |
| 108 | /** |
| 109 | * Perform the Montgomery reduction on a polynomial of degree 255, |
| 110 | * using basis x^128 and modulus p(x) = x^128 + x^127 + x^126 + x^121 + 1. |
| 111 | * |
| 112 | * See at the bottom for an explanation. |
| 113 | */ |
| 114 | STATIC __m128i reduce(__m128i prod_high, __m128i prod_low) |
| 115 | { |
| 116 | const uint64_t c2 = (uint64_t)0xc2 << 56; |
| 117 | __m128i t1, t2, t3, t4, t7; |
| 118 | |
| 119 | t1 = prod_high; /* U3:U2 */ |
| 120 | t7 = prod_low; /* U1:U0 */ |
| 121 | t3 = _mm_loadl_epi64((__m128i*)&c2); |
| 122 | t2 = _mm_clmulepi64_si128(t3, t7, 0x00); /* A */ |
| 123 | t4 = _mm_shuffle_epi32(t7, _MM_SHUFFLE(1,0,3,2)); /* U0:U1 */ |
| 124 | t4 = _mm_xor_si128(t4, t2); /* B */ |
| 125 | t2 = _mm_clmulepi64_si128(t3, t4, 0x00); /* C */ |
| 126 | t4 = _mm_shuffle_epi32(t4, _MM_SHUFFLE(1,0,3,2)); /* B0:B1 */ |
| 127 | t4 = _mm_xor_si128(t4, t2); /* D */ |
| 128 | t1 = _mm_xor_si128(t1, t4); /* T */ |
| 129 | |
| 130 | return t1; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Perform the carry-less multiplication of two polynomials of degree 127. |
| 135 | */ |
| 136 | STATIC void clmult(__m128i *prod_high, __m128i *prod_low, __m128i a, __m128i b) |
| 137 | { |
| 138 | __m128i c, d, e, f, g, h, i; |
| 139 | |
| 140 | c = _mm_clmulepi64_si128(a, b, 0x00); /* A0*B0 */ |
| 141 | d = _mm_clmulepi64_si128(a, b, 0x11); /* A1*B1 */ |
| 142 | e = _mm_clmulepi64_si128(a, b, 0x10); /* A0*B1 */ |
| 143 | f = _mm_clmulepi64_si128(a, b, 0x01); /* A1*B0 */ |
| 144 | g = _mm_xor_si128(e, f); /* E1+F1:E0+F0 */ |
| 145 | h = _mm_slli_si128(g, 8); /* E0+F0:0 */ |
| 146 | i = _mm_srli_si128(g, 8); /* 0:E1+F1 */ |
| 147 | *prod_high = _mm_xor_si128(d, i); |
| 148 | *prod_low = _mm_xor_si128(c, h); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Multiply a polynomial of degree 127 by x, modulo p(x) = x^128 + x^127 + x^126 + x^121 + 1 |
| 153 | */ |
| 154 | STATIC __m128i multx(__m128i a) |
| 155 | { |
| 156 | int msb; |
| 157 | int64_t r; |
| 158 | uint64_t p0, p1; |
| 159 | __m128i t0, t1, t2, t3, t4, t5, t6, t7; |
| 160 | |
| 161 | msb = _mm_movemask_epi8(a) >> 15; /* Bit 0 is a[127] */ |
| 162 | r = (msb ^ 1) - 1; /* MSB is copied into all 64 positions */ |
| 163 | p0 = (uint64_t)r & 0x0000000000000001U; /* Zero or XOR mask (low) */ |
| 164 | p1 = (uint64_t)r & ((uint64_t)0xc2 << 56); /* Zero or XOR mask (high) */ |
| 165 | t0 = _mm_loadl_epi64((__m128i*)&p0); |
| 166 | t1 = _mm_loadl_epi64((__m128i*)&p1); |
| 167 | t2 = _mm_unpacklo_epi64(t0, t1); /* Zero or XOR mask */ |
| 168 | |
| 169 | /* Shift value a left by 1 bit */ |
| 170 | t3 = _mm_slli_si128(a, 8); /* Shift a left by 64 bits (lower 64 bits are zero) */ |
| 171 | t4 = _mm_srli_epi64(t3, 63); /* Bit 64 is now a[63], all other bits are 0 */ |
| 172 | t5 = _mm_slli_epi64(a, 1); /* Shift left by 1 bit, but bit 64 is zero, not a[63] */ |
| 173 | t6 = _mm_or_si128(t4, t5); /* Actual result of shift left by 1 bit */ |
| 174 | |
| 175 | /* XOR conditional mask */ |
| 176 | t7 = _mm_xor_si128(t2, t6); |
| 177 | |
| 178 | return t7; |
| 179 | } |
| 180 | |
| 181 | /** Swap bytes in an XMM register **/ |
| 182 | STATIC __m128i swap(__m128i a) |
| 183 | { |
| 184 | __m128i mask; |
| 185 | |
| 186 | mask = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); |
| 187 | return _mm_shuffle_epi8(a, mask); |
| 188 | } |
| 189 | |
| 190 | EXPORT_SYM int ghash_expand_clmul(const uint8_t h[16], struct exp_key **expanded) |
| 191 | { |
| 192 | __m128i h128; |
| 193 | unsigned i; |
| 194 | |
| 195 | if (NULL==h || NULL==expanded) |
| 196 | return ERR_NULL; |
| 197 | |
| 198 | *expanded = align_alloc(sizeof(struct exp_key), 16); |
| 199 | if (NULL == *expanded) |
| 200 | return ERR_MEMORY; |
| 201 | |
| 202 | h128 = swap(_mm_loadu_si128((__m128i*)h)); |
| 203 | (*expanded)->h[0] = multx(h128); /** x*H **/ |
| 204 | |
| 205 | for (i=1; i<4; i++) { |
| 206 | __m128i r0, r1; |
| 207 | |
| 208 | clmult(&r0, &r1, (*expanded)->h[i-1], (*expanded)->h[0]); |
| 209 | (*expanded)->h[i] = reduce(r0, r1); |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | EXPORT_SYM int ghash_destroy_clmul(struct exp_key *expanded) |
| 216 | { |
| 217 | align_free(expanded); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | EXPORT_SYM int ghash_clmul( |
| 222 | uint8_t y_out[16], |
| 223 | const uint8_t block_data[], |
| 224 | size_t len, |
| 225 | const uint8_t y_in[16], |
| 226 | struct exp_key *expanded |
| 227 | ) |
| 228 | { |
| 229 | size_t i; |
| 230 | __m128i y_temp; |
| 231 | size_t len16; |
| 232 | |
| 233 | if (NULL==y_out || NULL==block_data || NULL==y_in || NULL==expanded) |
| 234 | return ERR_NULL; |
| 235 | |
| 236 | if (len % 16) |
| 237 | return ERR_NOT_ENOUGH_DATA; |
| 238 | |
| 239 | y_temp = swap(_mm_loadu_si128((__m128i*)y_in)); |
| 240 | |
| 241 | /** Authenticate 64 bytes per cycle **/ |
| 242 | len16 = len ^ (len & 0x3F); |
| 243 | for (i=0; i<len16; i+=16*4) { |
| 244 | __m128i sum0, sum1; |
| 245 | __m128i xm3, xm2, xm1, x; |
| 246 | __m128i r0, r1, r2, r3, r4, r5, r6; |
| 247 | |
| 248 | xm3 = swap(_mm_loadu_si128((__m128i*)&block_data[i])); |
| 249 | xm2 = swap(_mm_loadu_si128((__m128i*)&block_data[i+16])); |
| 250 | xm1 = swap(_mm_loadu_si128((__m128i*)&block_data[i+16*2])); |
| 251 | x = swap(_mm_loadu_si128((__m128i*)&block_data[i+16*3])); |
| 252 | |
| 253 | /** (X_{i-3} + Y_{i-4}) * H^4 **/ |
| 254 | r0 = _mm_xor_si128(xm3, y_temp); |
| 255 | clmult(&sum0, &sum1, r0, expanded->h[3]); |
| 256 | |
| 257 | /** X_{i-2} * H^3 **/ |
| 258 | clmult(&r1, &r2, xm2, expanded->h[2]); |
| 259 | sum0 = _mm_xor_si128(sum0, r1); |
| 260 | sum1 = _mm_xor_si128(sum1, r2); |
| 261 | |
| 262 | /** X_{i-1} * H^2 **/ |
| 263 | clmult(&r3, &r4, xm1, expanded->h[1]); |
| 264 | sum0 = _mm_xor_si128(sum0, r3); |
| 265 | sum1 = _mm_xor_si128(sum1, r4); |
| 266 | |
| 267 | /** X_{i} * H **/ |
| 268 | clmult(&r5, &r6, x, expanded->h[0]); |
| 269 | sum0 = _mm_xor_si128(sum0, r5); |
| 270 | sum1 = _mm_xor_si128(sum1, r6); |
| 271 | |
| 272 | /** mod P **/ |
| 273 | y_temp = reduce(sum0, sum1); |
| 274 | } |
| 275 | |
| 276 | /** Y_i = (X_i + Y_{i-1}) * H mod P **/ |
| 277 | for (; i<len; i+=16) { |
| 278 | __m128i z, xi; |
| 279 | __m128i prod_hi, prod_lo; |
| 280 | |
| 281 | xi = swap(_mm_loadu_si128((__m128i*)&block_data[i])); |
| 282 | z = _mm_xor_si128(y_temp, xi); |
| 283 | clmult(&prod_hi, &prod_lo, z, expanded->h[0]); |
| 284 | y_temp = reduce(prod_hi, prod_lo); |
| 285 | } |
| 286 | |
| 287 | y_temp = swap(y_temp); |
| 288 | _mm_storeu_si128((__m128i*)y_out, y_temp); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * The function reduce() computes the Montgomery reduction |
| 294 | * of U (input, 256 bits) with FastREDC algorithm: |
| 295 | * |
| 296 | * Q = ((U mod X^128) * p' mod X^128 |
| 297 | * T = (U + Q*p) div X^128 |
| 298 | * |
| 299 | * where: |
| 300 | * p = 1:C200000000000000:1 = 1:c2:1 |
| 301 | * p' = p^{-1} mod X^128 = C200000000000000:1 = c2:1 |
| 302 | * |
| 303 | * U3:U2 : U1:U0 (256 bit) |
| 304 | * Q1:Q0 (128 bit) |
| 305 | * T1:T0 (128 bit) |
| 306 | * |
| 307 | * Q = (U mod X^128) * p' mod X^128 |
| 308 | * = (U1:U0) * p' mod X^128 = (U1:U0) * (c2:1) mod X^128 = Q1:Q0 |
| 309 | * Q0 = U0 |
| 310 | * Q1 = L(U0*c2) + U1 |
| 311 | * |
| 312 | * T = (U + Q*p) div X^128 = T1:T0 |
| 313 | * |
| 314 | * Q*p = S = Q1:Q0 * 1:c2:1 |
| 315 | * S0 = Q0 (dropped) |
| 316 | * S1 = L(c2*Q0) + Q1 (dropped) |
| 317 | * S2 = Q0 + L(c2*Q1) + H(c2*Q0) |
| 318 | * S3 = Q1 + H(c2*Q1) |
| 319 | * |
| 320 | * T0 = S2 + U2 |
| 321 | * T1 = S3 + U3 |
| 322 | * |
| 323 | * Q1 = L(U0*c2) + U1 |
| 324 | * T0 = U0 + L(c2*Q1) + H(c2*U0) + U2 |
| 325 | * T1 = Q1 + H(c2*Q1) + U3 |
| 326 | * |
| 327 | * A = c2*U0 |
| 328 | * Q1 = A0 + U1 |
| 329 | * T0 = U0 + L(c2*Q1) + A1 + U2 |
| 330 | * T1 = Q1 + H(c2*Q1) + U3 |
| 331 | * |
| 332 | * A = c2*U0 |
| 333 | * B = A + U0:U1 = B1:Q1 |
| 334 | * T0 = L(c2*B0) + B1 + U2 |
| 335 | * T1 = B0 + H(c2*B0) + U3 |
| 336 | * |
| 337 | * A = c2*U0 |
| 338 | * B = A + U0:U1 |
| 339 | * C = c2*B0 |
| 340 | * T0 = C0 + B1 + U2 |
| 341 | * T1 = B0 + C1 + U3 |
| 342 | * |
| 343 | * A = c2*U0 |
| 344 | * B = A + U0:U1 |
| 345 | * C = c2*B0 |
| 346 | * D = C + B0:B1 |
| 347 | * T0 = D0 + U2 |
| 348 | * T1 = D1 + U3 |
| 349 | */ |
| 350 | |
| 351 | |
| 352 |