ctmul64.c (3672B)
1 /* 2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org> 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining 5 * a copy of this software and associated documentation files (the 6 * "Software"), to deal in the Software without restriction, including 7 * without limitation the rights to use, copy, modify, merge, publish, 8 * distribute, sublicense, and/or sell copies of the Software, and to 9 * permit persons to whom the Software is furnished to do so, subject to 10 * the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 25 /* 26 * This is the 64-bit variant of br_ghash_ctmul32(), with 64-bit operands 27 * and bit reversal of 64-bit words. 28 */ 29 30 static inline uint64_t 31 bmul64(uint64_t x, uint64_t y) 32 { 33 uint64_t x0, x1, x2, x3; 34 uint64_t y0, y1, y2, y3; 35 uint64_t z0, z1, z2, z3; 36 37 x0 = x & (uint64_t)0x1111111111111111; 38 x1 = x & (uint64_t)0x2222222222222222; 39 x2 = x & (uint64_t)0x4444444444444444; 40 x3 = x & (uint64_t)0x8888888888888888; 41 y0 = y & (uint64_t)0x1111111111111111; 42 y1 = y & (uint64_t)0x2222222222222222; 43 y2 = y & (uint64_t)0x4444444444444444; 44 y3 = y & (uint64_t)0x8888888888888888; 45 z0 = (x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1); 46 z1 = (x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2); 47 z2 = (x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3); 48 z3 = (x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0); 49 z0 &= (uint64_t)0x1111111111111111; 50 z1 &= (uint64_t)0x2222222222222222; 51 z2 &= (uint64_t)0x4444444444444444; 52 z3 &= (uint64_t)0x8888888888888888; 53 return z0 | z1 | z2 | z3; 54 } 55 56 static uint64_t 57 rev64(uint64_t x) 58 { 59 #define RMS(m, s) do { \ 60 x = ((x & (uint64_t)(m)) << (s)) \ 61 | ((x >> (s)) & (uint64_t)(m)); \ 62 } while (0) 63 64 RMS(0x5555555555555555, 1); 65 RMS(0x3333333333333333, 2); 66 RMS(0x0F0F0F0F0F0F0F0F, 4); 67 RMS(0x00FF00FF00FF00FF, 8); 68 RMS(0x0000FFFF0000FFFF, 16); 69 return (x << 32) | (x >> 32); 70 71 #undef RMS 72 } 73 74 75 static void 76 pv_mul_y_h_ctmul64(polyval_t *pv) 77 { 78 uint64_t y0, y1; 79 uint64_t h0, h1, h2, h0r, h1r, h2r; 80 81 y0 = CTMUL64_MEMBER(pv->y).lo; 82 y1 = CTMUL64_MEMBER(pv->y).hi; 83 h0 = CTMUL64_MEMBER(pv->key.h).lo; 84 h1 = CTMUL64_MEMBER(pv->key.h).hi; 85 h0r = rev64(h0); 86 h1r = rev64(h1); 87 88 h2 = h0 ^ h1; 89 h2r = h0r ^ h1r; 90 { 91 uint64_t y0r, y1r, y2, y2r; 92 uint64_t z0, z1, z2, z0h, z1h, z2h; 93 uint64_t v0, v1, v2, v3; 94 95 y0r = rev64(y0); 96 y1r = rev64(y1); 97 y2 = y0 ^ y1; 98 y2r = y0r ^ y1r; 99 100 z0 = bmul64(y0, h0); 101 z1 = bmul64(y1, h1); 102 z2 = bmul64(y2, h2); 103 z0h = bmul64(y0r, h0r); 104 z1h = bmul64(y1r, h1r); 105 z2h = bmul64(y2r, h2r); 106 z2 ^= z0 ^ z1; 107 z2h ^= z0h ^ z1h; 108 z0h = rev64(z0h) >> 1; 109 z1h = rev64(z1h) >> 1; 110 z2h = rev64(z2h) >> 1; 111 112 v0 = z0; 113 v1 = z0h ^ z2; 114 v2 = z1 ^ z2h; 115 v3 = z1h; 116 117 #if 0 118 // This step is GHASH only. 119 v3 = (v3 << 1) | (v2 >> 63); 120 v2 = (v2 << 1) | (v1 >> 63); 121 v1 = (v1 << 1) | (v0 >> 63); 122 v0 = (v0 << 1); 123 #endif 124 125 v2 ^= v0 ^ (v0 >> 1) ^ (v0 >> 2) ^ (v0 >> 7); 126 v1 ^= (v0 << 63) ^ (v0 << 62) ^ (v0 << 57); 127 v3 ^= v1 ^ (v1 >> 1) ^ (v1 >> 2) ^ (v1 >> 7); 128 v2 ^= (v1 << 63) ^ (v1 << 62) ^ (v1 << 57); 129 130 CTMUL64_MEMBER(pv->y).lo = v2; 131 CTMUL64_MEMBER(pv->y).hi = v3; 132 } 133 }