Path: blob/main/contrib/bearssl/src/hash/ghash_ctmul64.c
39536 views
/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/*27* This is the 64-bit variant of br_ghash_ctmul32(), with 64-bit operands28* and bit reversal of 64-bit words.29*/3031static inline uint64_t32bmul64(uint64_t x, uint64_t y)33{34uint64_t x0, x1, x2, x3;35uint64_t y0, y1, y2, y3;36uint64_t z0, z1, z2, z3;3738x0 = x & (uint64_t)0x1111111111111111;39x1 = x & (uint64_t)0x2222222222222222;40x2 = x & (uint64_t)0x4444444444444444;41x3 = x & (uint64_t)0x8888888888888888;42y0 = y & (uint64_t)0x1111111111111111;43y1 = y & (uint64_t)0x2222222222222222;44y2 = y & (uint64_t)0x4444444444444444;45y3 = y & (uint64_t)0x8888888888888888;46z0 = (x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1);47z1 = (x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2);48z2 = (x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3);49z3 = (x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0);50z0 &= (uint64_t)0x1111111111111111;51z1 &= (uint64_t)0x2222222222222222;52z2 &= (uint64_t)0x4444444444444444;53z3 &= (uint64_t)0x8888888888888888;54return z0 | z1 | z2 | z3;55}5657static uint64_t58rev64(uint64_t x)59{60#define RMS(m, s) do { \61x = ((x & (uint64_t)(m)) << (s)) \62| ((x >> (s)) & (uint64_t)(m)); \63} while (0)6465RMS(0x5555555555555555, 1);66RMS(0x3333333333333333, 2);67RMS(0x0F0F0F0F0F0F0F0F, 4);68RMS(0x00FF00FF00FF00FF, 8);69RMS(0x0000FFFF0000FFFF, 16);70return (x << 32) | (x >> 32);7172#undef RMS73}7475/* see bearssl_ghash.h */76void77br_ghash_ctmul64(void *y, const void *h, const void *data, size_t len)78{79const unsigned char *buf, *hb;80unsigned char *yb;81uint64_t y0, y1;82uint64_t h0, h1, h2, h0r, h1r, h2r;8384buf = data;85yb = y;86hb = h;87y1 = br_dec64be(yb);88y0 = br_dec64be(yb + 8);89h1 = br_dec64be(hb);90h0 = br_dec64be(hb + 8);91h0r = rev64(h0);92h1r = rev64(h1);93h2 = h0 ^ h1;94h2r = h0r ^ h1r;95while (len > 0) {96const unsigned char *src;97unsigned char tmp[16];98uint64_t y0r, y1r, y2, y2r;99uint64_t z0, z1, z2, z0h, z1h, z2h;100uint64_t v0, v1, v2, v3;101102if (len >= 16) {103src = buf;104buf += 16;105len -= 16;106} else {107memcpy(tmp, buf, len);108memset(tmp + len, 0, (sizeof tmp) - len);109src = tmp;110len = 0;111}112y1 ^= br_dec64be(src);113y0 ^= br_dec64be(src + 8);114115y0r = rev64(y0);116y1r = rev64(y1);117y2 = y0 ^ y1;118y2r = y0r ^ y1r;119120z0 = bmul64(y0, h0);121z1 = bmul64(y1, h1);122z2 = bmul64(y2, h2);123z0h = bmul64(y0r, h0r);124z1h = bmul64(y1r, h1r);125z2h = bmul64(y2r, h2r);126z2 ^= z0 ^ z1;127z2h ^= z0h ^ z1h;128z0h = rev64(z0h) >> 1;129z1h = rev64(z1h) >> 1;130z2h = rev64(z2h) >> 1;131132v0 = z0;133v1 = z0h ^ z2;134v2 = z1 ^ z2h;135v3 = z1h;136137v3 = (v3 << 1) | (v2 >> 63);138v2 = (v2 << 1) | (v1 >> 63);139v1 = (v1 << 1) | (v0 >> 63);140v0 = (v0 << 1);141142v2 ^= v0 ^ (v0 >> 1) ^ (v0 >> 2) ^ (v0 >> 7);143v1 ^= (v0 << 63) ^ (v0 << 62) ^ (v0 << 57);144v3 ^= v1 ^ (v1 >> 1) ^ (v1 >> 2) ^ (v1 >> 7);145v2 ^= (v1 << 63) ^ (v1 << 62) ^ (v1 << 57);146147y0 = v2;148y1 = v3;149}150151br_enc64be(yb, y1);152br_enc64be(yb + 8, y0);153}154155156