Home | History | Annotate | Download | only in crypto
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "crypto/ghash.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "base/logging.h"
     10 #include "base/sys_byteorder.h"
     11 
     12 namespace crypto {
     13 
     14 // GaloisHash is a polynomial authenticator that works in GF(2^128).
     15 //
     16 // Elements of the field are represented in `little-endian' order (which
     17 // matches the description in the paper[1]), thus the most significant bit is
     18 // the right-most bit. (This is backwards from the way that everybody else does
     19 // it.)
     20 //
     21 // We store field elements in a pair of such `little-endian' uint64s. So the
     22 // value one is represented by {low = 2**63, high = 0} and doubling a value
     23 // involves a *right* shift.
     24 //
     25 // [1] http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
     26 
     27 namespace {
     28 
     29 // Get64 reads a 64-bit, big-endian number from |bytes|.
     30 uint64 Get64(const uint8 bytes[8]) {
     31   uint64 t;
     32   memcpy(&t, bytes, sizeof(t));
     33   return base::NetToHost64(t);
     34 }
     35 
     36 // Put64 writes |x| to |bytes| as a 64-bit, big-endian number.
     37 void Put64(uint8 bytes[8], uint64 x) {
     38   x = base::HostToNet64(x);
     39   memcpy(bytes, &x, sizeof(x));
     40 }
     41 
     42 // Reverse reverses the order of the bits of 4-bit number in |i|.
     43 int Reverse(int i) {
     44   i = ((i << 2) & 0xc) | ((i >> 2) & 0x3);
     45   i = ((i << 1) & 0xa) | ((i >> 1) & 0x5);
     46   return i;
     47 }
     48 
     49 }  // namespace
     50 
     51 GaloisHash::GaloisHash(const uint8 key[16]) {
     52   Reset();
     53 
     54   // We precompute 16 multiples of |key|. However, when we do lookups into this
     55   // table we'll be using bits from a field element and therefore the bits will
     56   // be in the reverse order. So normally one would expect, say, 4*key to be in
     57   // index 4 of the table but due to this bit ordering it will actually be in
     58   // index 0010 (base 2) = 2.
     59   FieldElement x = {Get64(key), Get64(key+8)};
     60   product_table_[0].low = 0;
     61   product_table_[0].hi = 0;
     62   product_table_[Reverse(1)] = x;
     63 
     64   for (int i = 0; i < 16; i += 2) {
     65     product_table_[Reverse(i)] = Double(product_table_[Reverse(i/2)]);
     66     product_table_[Reverse(i+1)] = Add(product_table_[Reverse(i)], x);
     67   }
     68 }
     69 
     70 void GaloisHash::Reset() {
     71   state_ = kHashingAdditionalData;
     72   additional_bytes_ = 0;
     73   ciphertext_bytes_ = 0;
     74   buf_used_ = 0;
     75   y_.low = 0;
     76   y_.hi = 0;
     77 }
     78 
     79 void GaloisHash::UpdateAdditional(const uint8* data, size_t length) {
     80   DCHECK_EQ(state_, kHashingAdditionalData);
     81   additional_bytes_ += length;
     82   Update(data, length);
     83 }
     84 
     85 void GaloisHash::UpdateCiphertext(const uint8* data, size_t length) {
     86   if (state_ == kHashingAdditionalData) {
     87     // If there's any remaining additional data it's zero padded to the next
     88     // full block.
     89     if (buf_used_ > 0) {
     90       memset(&buf_[buf_used_], 0, sizeof(buf_)-buf_used_);
     91       UpdateBlocks(buf_, 1);
     92       buf_used_ = 0;
     93     }
     94     state_ = kHashingCiphertext;
     95   }
     96 
     97   DCHECK_EQ(state_, kHashingCiphertext);
     98   ciphertext_bytes_ += length;
     99   Update(data, length);
    100 }
    101 
    102 void GaloisHash::Finish(void* output, size_t len) {
    103   DCHECK(state_ != kComplete);
    104 
    105   if (buf_used_ > 0) {
    106     // If there's any remaining data (additional data or ciphertext), it's zero
    107     // padded to the next full block.
    108     memset(&buf_[buf_used_], 0, sizeof(buf_)-buf_used_);
    109     UpdateBlocks(buf_, 1);
    110     buf_used_ = 0;
    111   }
    112 
    113   state_ = kComplete;
    114 
    115   // The lengths of the additional data and ciphertext are included as the last
    116   // block. The lengths are the number of bits.
    117   y_.low ^= additional_bytes_*8;
    118   y_.hi ^= ciphertext_bytes_*8;
    119   MulAfterPrecomputation(product_table_, &y_);
    120 
    121   uint8 *result, result_tmp[16];
    122   if (len >= 16) {
    123     result = reinterpret_cast<uint8*>(output);
    124   } else {
    125     result = result_tmp;
    126   }
    127 
    128   Put64(result, y_.low);
    129   Put64(result + 8, y_.hi);
    130 
    131   if (len < 16)
    132     memcpy(output, result_tmp, len);
    133 }
    134 
    135 // static
    136 GaloisHash::FieldElement GaloisHash::Add(
    137     const FieldElement& x,
    138     const FieldElement& y) {
    139   // Addition in a characteristic 2 field is just XOR.
    140   FieldElement z = {x.low^y.low, x.hi^y.hi};
    141   return z;
    142 }
    143 
    144 // static
    145 GaloisHash::FieldElement GaloisHash::Double(const FieldElement& x) {
    146   const bool msb_set = x.hi & 1;
    147 
    148   FieldElement xx;
    149   // Because of the bit-ordering, doubling is actually a right shift.
    150   xx.hi = x.hi >> 1;
    151   xx.hi |= x.low << 63;
    152   xx.low = x.low >> 1;
    153 
    154   // If the most-significant bit was set before shifting then it, conceptually,
    155   // becomes a term of x^128. This is greater than the irreducible polynomial
    156   // so the result has to be reduced. The irreducible polynomial is
    157   // 1+x+x^2+x^7+x^128. We can subtract that to eliminate the term at x^128
    158   // which also means subtracting the other four terms. In characteristic 2
    159   // fields, subtraction == addition == XOR.
    160   if (msb_set)
    161     xx.low ^= 0xe100000000000000ULL;
    162 
    163   return xx;
    164 }
    165 
    166 void GaloisHash::MulAfterPrecomputation(const FieldElement* table,
    167                                         FieldElement* x) {
    168   FieldElement z = {0, 0};
    169 
    170   // In order to efficiently multiply, we use the precomputed table of i*key,
    171   // for i in 0..15, to handle four bits at a time. We could obviously use
    172   // larger tables for greater speedups but the next convenient table size is
    173   // 4K, which is a little large.
    174   //
    175   // In other fields one would use bit positions spread out across the field in
    176   // order to reduce the number of doublings required. However, in
    177   // characteristic 2 fields, repeated doublings are exceptionally cheap and
    178   // it's not worth spending more precomputation time to eliminate them.
    179   for (unsigned i = 0; i < 2; i++) {
    180     uint64 word;
    181     if (i == 0) {
    182       word = x->hi;
    183     } else {
    184       word = x->low;
    185     }
    186 
    187     for (unsigned j = 0; j < 64; j += 4) {
    188       Mul16(&z);
    189       // the values in |table| are ordered for little-endian bit positions. See
    190       // the comment in the constructor.
    191       const FieldElement& t = table[word & 0xf];
    192       z.low ^= t.low;
    193       z.hi ^= t.hi;
    194       word >>= 4;
    195     }
    196   }
    197 
    198   *x = z;
    199 }
    200 
    201 // kReductionTable allows for rapid multiplications by 16. A multiplication by
    202 // 16 is a right shift by four bits, which results in four bits at 2**128.
    203 // These terms have to be eliminated by dividing by the irreducible polynomial.
    204 // In GHASH, the polynomial is such that all the terms occur in the
    205 // least-significant 8 bits, save for the term at x^128. Therefore we can
    206 // precompute the value to be added to the field element for each of the 16 bit
    207 // patterns at 2**128 and the values fit within 12 bits.
    208 static const uint16 kReductionTable[16] = {
    209   0x0000, 0x1c20, 0x3840, 0x2460, 0x7080, 0x6ca0, 0x48c0, 0x54e0,
    210   0xe100, 0xfd20, 0xd940, 0xc560, 0x9180, 0x8da0, 0xa9c0, 0xb5e0,
    211 };
    212 
    213 // static
    214 void GaloisHash::Mul16(FieldElement* x) {
    215   const unsigned msw = x->hi & 0xf;
    216   x->hi >>= 4;
    217   x->hi |= x->low << 60;
    218   x->low >>= 4;
    219   x->low ^= static_cast<uint64>(kReductionTable[msw]) << 48;
    220 }
    221 
    222 void GaloisHash::UpdateBlocks(const uint8* bytes, size_t num_blocks) {
    223   for (size_t i = 0; i < num_blocks; i++) {
    224     y_.low ^= Get64(bytes);
    225     bytes += 8;
    226     y_.hi ^= Get64(bytes);
    227     bytes += 8;
    228     MulAfterPrecomputation(product_table_, &y_);
    229   }
    230 }
    231 
    232 void GaloisHash::Update(const uint8* data, size_t length) {
    233   if (buf_used_ > 0) {
    234     const size_t n = std::min(length, sizeof(buf_) - buf_used_);
    235     memcpy(&buf_[buf_used_], data, n);
    236     buf_used_ += n;
    237     length -= n;
    238     data += n;
    239 
    240     if (buf_used_ == sizeof(buf_)) {
    241       UpdateBlocks(buf_, 1);
    242       buf_used_ = 0;
    243     }
    244   }
    245 
    246   if (length >= 16) {
    247     const size_t n = length / 16;
    248     UpdateBlocks(data, n);
    249     length -= n*16;
    250     data += n*16;
    251   }
    252 
    253   if (length > 0) {
    254     memcpy(buf_, data, length);
    255     buf_used_ = length;
    256   }
    257 }
    258 
    259 }  // namespace crypto
    260