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