This source file includes following definitions.
- Get64
- Put64
- Reverse
- Reset
- UpdateAdditional
- UpdateCiphertext
- Finish
- Add
- Double
- MulAfterPrecomputation
- Mul16
- UpdateBlocks
- Update
#include "crypto/ghash.h"
#include <algorithm>
#include "base/logging.h"
#include "base/sys_byteorder.h"
namespace crypto {
namespace {
uint64 Get64(const uint8 bytes[8]) {
uint64 t;
memcpy(&t, bytes, sizeof(t));
return base::NetToHost64(t);
}
void Put64(uint8 bytes[8], uint64 x) {
x = base::HostToNet64(x);
memcpy(bytes, &x, sizeof(x));
}
int Reverse(int i) {
i = ((i << 2) & 0xc) | ((i >> 2) & 0x3);
i = ((i << 1) & 0xa) | ((i >> 1) & 0x5);
return i;
}
}
GaloisHash::GaloisHash(const uint8 key[16]) {
Reset();
FieldElement x = {Get64(key), Get64(key+8)};
product_table_[0].low = 0;
product_table_[0].hi = 0;
product_table_[Reverse(1)] = x;
for (int i = 0; i < 16; i += 2) {
product_table_[Reverse(i)] = Double(product_table_[Reverse(i/2)]);
product_table_[Reverse(i+1)] = Add(product_table_[Reverse(i)], x);
}
}
void GaloisHash::Reset() {
state_ = kHashingAdditionalData;
additional_bytes_ = 0;
ciphertext_bytes_ = 0;
buf_used_ = 0;
y_.low = 0;
y_.hi = 0;
}
void GaloisHash::UpdateAdditional(const uint8* data, size_t length) {
DCHECK_EQ(state_, kHashingAdditionalData);
additional_bytes_ += length;
Update(data, length);
}
void GaloisHash::UpdateCiphertext(const uint8* data, size_t length) {
if (state_ == kHashingAdditionalData) {
if (buf_used_ > 0) {
memset(&buf_[buf_used_], 0, sizeof(buf_)-buf_used_);
UpdateBlocks(buf_, 1);
buf_used_ = 0;
}
state_ = kHashingCiphertext;
}
DCHECK_EQ(state_, kHashingCiphertext);
ciphertext_bytes_ += length;
Update(data, length);
}
void GaloisHash::Finish(void* output, size_t len) {
DCHECK(state_ != kComplete);
if (buf_used_ > 0) {
memset(&buf_[buf_used_], 0, sizeof(buf_)-buf_used_);
UpdateBlocks(buf_, 1);
buf_used_ = 0;
}
state_ = kComplete;
y_.low ^= additional_bytes_*8;
y_.hi ^= ciphertext_bytes_*8;
MulAfterPrecomputation(product_table_, &y_);
uint8 *result, result_tmp[16];
if (len >= 16) {
result = reinterpret_cast<uint8*>(output);
} else {
result = result_tmp;
}
Put64(result, y_.low);
Put64(result + 8, y_.hi);
if (len < 16)
memcpy(output, result_tmp, len);
}
GaloisHash::FieldElement GaloisHash::Add(
const FieldElement& x,
const FieldElement& y) {
FieldElement z = {x.low^y.low, x.hi^y.hi};
return z;
}
GaloisHash::FieldElement GaloisHash::Double(const FieldElement& x) {
const bool msb_set = x.hi & 1;
FieldElement xx;
xx.hi = x.hi >> 1;
xx.hi |= x.low << 63;
xx.low = x.low >> 1;
if (msb_set)
xx.low ^= 0xe100000000000000ULL;
return xx;
}
void GaloisHash::MulAfterPrecomputation(const FieldElement* table,
FieldElement* x) {
FieldElement z = {0, 0};
for (unsigned i = 0; i < 2; i++) {
uint64 word;
if (i == 0) {
word = x->hi;
} else {
word = x->low;
}
for (unsigned j = 0; j < 64; j += 4) {
Mul16(&z);
const FieldElement& t = table[word & 0xf];
z.low ^= t.low;
z.hi ^= t.hi;
word >>= 4;
}
}
*x = z;
}
static const uint16 kReductionTable[16] = {
0x0000, 0x1c20, 0x3840, 0x2460, 0x7080, 0x6ca0, 0x48c0, 0x54e0,
0xe100, 0xfd20, 0xd940, 0xc560, 0x9180, 0x8da0, 0xa9c0, 0xb5e0,
};
void GaloisHash::Mul16(FieldElement* x) {
const unsigned msw = x->hi & 0xf;
x->hi >>= 4;
x->hi |= x->low << 60;
x->low >>= 4;
x->low ^= static_cast<uint64>(kReductionTable[msw]) << 48;
}
void GaloisHash::UpdateBlocks(const uint8* bytes, size_t num_blocks) {
for (size_t i = 0; i < num_blocks; i++) {
y_.low ^= Get64(bytes);
bytes += 8;
y_.hi ^= Get64(bytes);
bytes += 8;
MulAfterPrecomputation(product_table_, &y_);
}
}
void GaloisHash::Update(const uint8* data, size_t length) {
if (buf_used_ > 0) {
const size_t n = std::min(length, sizeof(buf_) - buf_used_);
memcpy(&buf_[buf_used_], data, n);
buf_used_ += n;
length -= n;
data += n;
if (buf_used_ == sizeof(buf_)) {
UpdateBlocks(buf_, 1);
buf_used_ = 0;
}
}
if (length >= 16) {
const size_t n = length / 16;
UpdateBlocks(data, n);
length -= n*16;
data += n*16;
}
if (length > 0) {
memcpy(buf_, data, length);
buf_used_ = length;
}
}
}