Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: crypto/ghash.cc

Issue 11175015: crypto: add GHASH implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 "base/logging.h"
8 #include "base/sys_byteorder.h"
9
10 namespace crypto {
11
12 // GaliosHash is a polynomial authenticator that works in GF(2^128).
13 //
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.)
18 //
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.
22 //
23 // [1] http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gc m-revised-spec.pdf
24
25 namespace {
26
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);
32 }
33
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, 8);
wtc 2012/10/19 21:35:22 Nit: 8 => sizeof(x) ? (You use sizeof(t) on line
agl 2012/10/22 21:50:56 Done.
38 }
39
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);
wtc 2012/10/19 21:35:22 Nit: add spaces around the & operators.
agl 2012/10/22 21:50:56 Done.
44 return i;
45 }
46
47 } // anonymous namespace
48
49 GaliosHash::GaliosHash(const uint8 key[16]) {
50 Reset();
51
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 0010b = 2.
wtc 2012/10/19 21:35:22 Nit: your use of the 'b' suffix confused me for a
agl 2012/10/22 21:50:56 Done.
57 FieldElement x = {Get64(key), Get64(key+8)};
58 productTable[0].low = 0;
59 productTable[0].hi = 0;
60 productTable[Reverse(1)] = x;
61
62 for (int i = 0; i < 16; i += 2) {
63 productTable[Reverse(i)] = Double(productTable[Reverse(i/2)]);
64 productTable[Reverse(i+1)] = Add(productTable[Reverse(i)], x);
wtc 2012/10/19 21:35:22 In Reverse(i+1), you are relying on the fact that
agl 2012/10/22 21:50:56 Reverse(16) should never be called, I believe. The
65 }
66 }
67
68 void GaliosHash::Reset() {
69 state_ = kHashingAdditionalData;
70 additional_bytes_ = 0;
71 ciphertext_bytes_ = 0;
72 buf_used_ = 0;
73 y_.low = 0;
74 y_.hi = 0;
75 }
76
77 void GaliosHash::UpdateAdditional(const uint8* data, size_t length) {
78 DCHECK_EQ(state_, kHashingAdditionalData);
79 additional_bytes_ += length;
80 Update(data, length);
81 }
82
83 void GaliosHash::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;
91 }
92 state_ = kHashingCiphertext;
93 }
94
95 DCHECK_EQ(state_, kHashingCiphertext);
96 ciphertext_bytes_ += length;
97 Update(data, length);
98 }
99
100 void GaliosHash::Digest(uint8 result[16]) {
101 DCHECK(state_ != kComplete);
102
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;
109 }
110
111 state_ = kComplete;
112
113 // The lengths of the additional data and ciphertext are included as the last
114 // block. The lengths are the number of bits.
wtc 2012/10/19 21:35:22 This shows you implemented the original GHASH.
115 y_.low ^= additional_bytes_*8;
116 y_.hi ^= ciphertext_bytes_*8;
117 MulAfterPrecomputation(productTable, &y_);
118
119 Put64(result, y_.low);
120 Put64(result+8, y_.hi);
wtc 2012/10/19 21:35:22 I suggest adding spaces around the operators in th
agl 2012/10/22 21:50:56 Done.
121 }
122
123 // static
124 GaliosHash::FieldElement GaliosHash::Add(
125 const FieldElement& x,
126 const FieldElement& y) {
127 // Addition in a characteristic 2 field is just XOR.
128 FieldElement z = {x.low^y.low, x.hi^y.hi};
129 return z;
130 }
131
132 // static
133 GaliosHash::FieldElement GaliosHash::Double(const FieldElement& x) {
134 const bool msbSet = x.hi & 1;
wtc 2012/10/19 21:35:22 msbSet => msb_set
agl 2012/10/22 21:50:56 Done.
135
136 FieldElement xx;
137 // Because of the bit-ordering, doubling is actually a right shift.
138 xx.hi = x.hi >> 1;
139 xx.hi |= x.low << 63;
140 xx.low = x.low >> 1;
141
142 // If the most-significant bit was set before shifting then it, conceptually,
143 // becomes a term of x^128. This is greater than the irreducible polynomial
144 // so the result has to be reduced. The irreducible polynomial is
145 // 1+x+x^2+x^7+x^128. We can subtract that to eliminate the term at x^128
146 // which also means subtracting the other four terms. In characteristic 2
147 // fields, subtraction == addition == XOR.
148 if (msbSet) {
149 xx.low ^= 0xe100000000000000;
wtc 2012/10/19 21:35:22 You may need to add a ULL suffix to the constant t
agl 2012/10/22 21:50:56 Done.
150 }
151
152 return xx;
153 }
154
155 void GaliosHash::MulAfterPrecomputation(const FieldElement* table,
156 FieldElement* x) {
157 FieldElement z = {0, 0};
158
159 // In order to efficiently multiply, we use the precomputed table of i*key,
160 // for i in 0..15, to handle four bits at a time. We could obviously use
161 // larger tables for greater speedups but the next convenient table size is
162 // 4K, which is a little large.
163 //
164 // In other fields one would use bit positions spread out across the field in
165 // order to reduce the number of doublings required. However, in
166 // characteristic 2 fields, repeated doublings are exceptionally cheap and
167 // it's not worth spending more precomputation time to eliminate them.
168 for (unsigned i = 0; i < 2; i++) {
169 uint64 word;
170 if (i == 0) {
171 word = x->hi;
172 } else {
173 word = x->low;
174 }
175
176 for (unsigned j = 0; j < 64; j += 4) {
177 Mul16(&z);
178 // the values in |table| are ordered for little-endian bit positions. See
179 // the comment in the constructor.
180 const FieldElement& t = table[word & 0xf];
181 z.low ^= t.low;
182 z.hi ^= t.hi;
183 word >>= 4;
184 }
185 }
186
187 *x = z;
188 }
189
190 // kReductionTable allows for rapid multiplications by 16. A multiplication by
191 // 16 is a right shift by four bits, which results in four bits at 2**128.
192 // These terms have to be eliminated by dividing by the irreducible polynomial.
193 // In GHASH, the polynomial is such that all the terms occur in the
194 // least-significant 8 bits, save for the term at x^128. Therefore we can
195 // precompute the value to be added to the field element for each of the 16 bit
196 // patterns at 2**128 and the values fit within 12 bits.
197 static const uint16 kReductionTable[16] = {
198 0x0000, 0x1c20, 0x3840, 0x2460, 0x7080, 0x6ca0, 0x48c0, 0x54e0,
199 0xe100, 0xfd20, 0xd940, 0xc560, 0x9180, 0x8da0, 0xa9c0, 0xb5e0,
200 };
201
202 // static
203 void GaliosHash::Mul16(FieldElement* x) {
204 const unsigned msw = x->hi & 0xf;
205 x->hi >>= 4;
206 x->hi |= x->low << 60;
207 x->low >>= 4;
208 x->low ^= static_cast<uint64>(kReductionTable[msw]) << 48;
209 }
210
211 void GaliosHash::UpdateBlocks(const uint8* bytes, size_t num_blocks) {
212 for (size_t i = 0; i < num_blocks; i++) {
213 y_.low ^= Get64(bytes);
214 bytes += 8;
215 y_.hi ^= Get64(bytes);
216 bytes += 8;
217 MulAfterPrecomputation(productTable, &y_);
218 }
219 }
220
221 void GaliosHash::Update(const uint8* data, size_t length) {
222 if (buf_used_ > 0) {
223 const size_t n = std::min(length, buf_used_);
224 memcpy(&buf_[buf_used_], data, n);
225 buf_used_ += n;
226 length -= n;
227 data += n;
228
229 if (buf_used_ == sizeof(buf_)) {
230 UpdateBlocks(buf_, 1);
231 buf_used_ = 0;
232 }
233 }
234
235 if (length >= 16) {
236 const size_t n = length / 16;
237 UpdateBlocks(data, n);
238 length -= n*16;
239 data += n*16;
240 }
241
242 if (length > 0) {
243 memcpy(buf_, data, length);
244 buf_used_ = length;
245 }
246 }
247
248 } // namespace crypto
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698