aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/crypto/ghash.h
blob: bae8badb5fcc5e7434a25dea083965ba1d90c138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
 *------------------------------------------------------------------
 * Copyright (c) 2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

/*
 *------------------------------------------------------------------
 *  Copyright(c) 2018, Intel Corporation All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *    * Neither the name of Intel Corporation nor the names of its
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES * LOSS OF USE,
 *  DATA, OR PROFITS * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *------------------------------------------------------------------
 */

/*
 * Based on work by: Shay Gueron, Michael E. Kounavis, Erdinc Ozturk,
 *                   Vinodh Gopal, James Guilford, Tomasz Kantecki
 *
 * References:
 * [1] Vinodh Gopal et. al. Optimized Galois-Counter-Mode Implementation on
 *     Intel Architecture Processors. August, 2010
 * [2] Erdinc Ozturk et. al. Enabling High-Performance Galois-Counter-Mode on
 *     Intel Architecture Processors. October, 2012.
 * [3] intel-ipsec-mb library, https://github.com/01org/intel-ipsec-mb.git
 *
 * Definitions:
 *  GF    Galois Extension Field GF(2^128) - finite field where elements are
 *        represented as polynomials with coefficients in GF(2) with the
 *        highest degree of 127. Polynomials are represented as 128-bit binary
 *        numbers where each bit represents one coefficient.
 *        e.g. polynomial x^5 + x^3 + x + 1 is represented in binary 101011.
 *  H     hash key (128 bit)
 *  POLY  irreducible polynomial x^127 + x^7 + x^2 + x + 1
 *  RPOLY irreducible polynomial x^128 + x^127 + x^126 + x^121 + 1
 *  +     addition in GF, which equals to XOR operation
 *  *     multiplication in GF
 *
 * GF multiplication consists of 2 steps:
 *  - carry-less multiplication of two 128-bit operands into 256-bit result
 *  - reduction of 256-bit result into 128-bit with modulo POLY
 *
 * GHash is calculated on 128-bit blocks of data according to the following
 * formula:
 *    GH = (GH + data) * hash_key
 *
 * To avoid bit-reflection of data, this code uses GF multipication
 * with reversed polynomial:
 *   a * b * x^-127 mod RPOLY
 *
 * To improve computation speed table Hi is precomputed with powers of H',
 * where H' is calculated as H<<1 mod RPOLY.
 * This allows us to improve performance by deferring reduction. For example
 * to caclulate ghash of 4 128-bit blocks of data (b0, b1, b2, b3), we can do:
 *
 * u8x16 Hi[4];
 * ghash_precompute (H, Hi, 4);
 *
 * ghash_data_t _gd, *gd = &_gd;
 * ghash_mul_first (gd, GH ^ b0, Hi[3]);
 * ghash_mul_next (gd, b1, Hi[2]);
 * ghash_mul_next (gd, b2, Hi[1]);
 * ghash_mul_next (gd, b3, Hi[0]);
 * ghash_reduce (gd);
 * ghash_reduce2 (gd);
 * GH = ghash_final (gd);
 *
 * Reduction step is split into 3 functions so it can be better interleaved
 * with other code, (i.e. with AES computation).
 */

#ifndef __ghash_h__
#define __ghash_h__

static_always_inline u8x16
gmul_lo_lo (u8x16 a, u8x16 b)
{
#if defined (__PCLMUL__)
  return (u8x16) _mm_clmulepi64_si128 ((__m128i) a, (__m128i) b, 0x00);
#elif defined (__ARM_FEATURE_CRYPTO)
  return (u8x16) vmull_p64 ((poly64_t) vget_low_p64 ((poly64x2_t) a),
			    (poly64_t) vget_low_p64 ((poly64x2_t) b));
#endif
}

static_always_inline u8x16
gmul_hi_lo (u8x16 a, u8x16 b)
{
#if defined (__PCLMUL__)
  return (u8x16) _mm_clmulepi64_si128 ((__m128i) a, (__m128i) b, 0x01);
#elif defined (__ARM_FEATURE_CRYPTO)
  return (u8x16) vmull_p64 ((poly64_t) vget_high_p64 ((poly64x2_t) a),
			    (poly64_t) vget_low_p64 ((poly64x2_t) b));
#endif
}

static_always_inline u8x16
gmul_lo_hi (u8x16 a, u8x16 b)
{
#if defined (__PCLMUL__)
  return (u8x16) _mm_clmulepi64_si128 ((__m128i) a, (__m128i) b, 0x10);
#elif defined (__ARM_FEATURE_CRYPTO)
  return (u8x16) vmull_p64 ((poly64_t) vget_low_p64 ((poly64x2_t) a),
			    (poly64_t) vget_high_p64 ((poly64x2_t) b));
#endif
}

static_always_inline u8x16
gmul_hi_hi (u8x16 a, u8x16 b)
{
#if defined (__PCLMUL__)
  return (u8x16) _mm_clmulepi64_si128 ((__m128i) a, (__m128i) b, 0x11);
#elif defined (__ARM_FEATURE_CRYPTO)
  return (u8x16) vmull_high_p64 ((poly64x2_t) a, (poly64x2_t) b);
#endif
}

typedef struct
{
  u8x16 mid, hi, lo, tmp_lo, tmp_hi;
  u8x32 hi2, lo2, mid2, tmp_lo2, tmp_hi2;
  u8x64 hi4, lo4, mid4, tmp_lo4, tmp_hi4;
  int pending;
} ghash_data_t;

static const u8x16 ghash_poly = {
  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2
};

static const u8x16 ghash_poly2 = {
  0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2
};

static_always_inline void
ghash_mul_first (ghash_data_t * gd, u8x16 a, u8x16 b)
{
  /* a1 * b1 */
  gd->hi = gmul_hi_hi (a, b);
  /* a0 * b0 */
  gd->lo = gmul_lo_lo (a, b);
  /* a0 * b1 ^ a1 * b0 */
  gd->mid = gmul_hi_lo (a, b) ^ gmul_lo_hi (a, b);

  /* set gd->pending to 0 so next invocation of ghash_mul_next(...) knows that
     there is no pending data in tmp_lo and tmp_hi */
  gd->pending = 0;
}

static_always_inline void
ghash_mul_next (ghash_data_t * gd, u8x16 a, u8x16 b)
{
  /* a1 * b1 */
  u8x16 hi = gmul_hi_hi (a, b);
  /* a0 * b0 */
  u8x16 lo = gmul_lo_lo (a, b);

  /* this branch will be optimized out by the compiler, and it allows us to
     reduce number of XOR operations by using ternary logic */
  if (gd->pending)
    {
      /* there is peding data from previous invocation so we can XOR */
      gd->hi = u8x16_xor3 (gd->hi, gd->tmp_hi, hi);
      gd->lo = u8x16_xor3 (gd->lo, gd->tmp_lo, lo);
      gd->pending = 0;
    }
  else
    {
      /* there is no peding data from previous invocation so we postpone XOR */
      gd->tmp_hi = hi;
      gd->tmp_lo = lo;
      gd->pending = 1;
    }

  /* gd->mid ^= a0 * b1 ^ a1 * b0  */
  gd->mid = u8x16_xor3 (gd->mid, gmul_hi_lo (a, b), gmul_lo_hi (a, b));
}

static_always_inline void
ghash_reduce (ghash_data_t * gd)
{
  u8x16 r;

  /* Final combination:
     gd->lo ^= gd->mid << 64
     gd->hi ^= gd->mid >> 64 */
  u8x16 midl = u8x16_word_shift_left (gd->mid, 8);
  u8x16 midr = u8x16_word_shift_right (gd->mid, 8);

  if (gd->pending)
    {
      gd->lo = u8x16_xor3 (gd->lo, gd->tmp_lo, midl);
      gd->hi = u8x16_xor3 (gd->hi, gd->tmp_hi, midr);
    }
  else
    {
      gd->lo ^= midl;
      gd->hi ^= midr;
    }
  r = gmul_hi_lo (ghash_poly2, gd->lo);
  gd->lo ^= u8x16_word_shift_left (r, 8);
}

static_always_inline void
ghash_reduce2 (ghash_data_t * gd)
{
  gd->tmp_lo = gmul_lo_lo (ghash_poly2, gd->lo);
  gd->tmp_hi = gmul_lo_hi (ghash_poly2, gd->lo);
}

static_always_inline u8x16
ghash_final (ghash_data_t * gd)
{
  return u8x16_xor3 (gd->hi, u8x16_word_shift_right (gd->tmp_lo, 4),
		     u8x16_word_shift_left (gd->tmp_hi, 4));
}

static_always_inline u8x16
ghash_mul (u8x16 a, u8x16 b)
{
  ghash_data_t _gd, *gd = &_gd;
  ghash_mul_first (gd, a, b);
  ghash_reduce (gd);
  ghash_reduce2 (gd);
  return ghash_final (gd);
}

#if defined(__VPCLMULQDQ__) && defined(__AVX512F__)

static const u8x64 ghash4_poly2 = {
  0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2,
  0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2,
  0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2,
  0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2,
};

static_always_inline u8x64
gmul4_lo_lo (u8x64 a, u8x64 b)
{
  return (u8x64) _mm512_clmulepi64_epi128 ((__m512i) a, (__m512i) b, 0x00);
}

static_always_inline u8x64
gmul4_hi_lo (u8x64 a, u8x64 b)
{
  return (u8x64) _mm512_clmulepi64_epi128 ((__m512i) a, (__m512i) b, 0x01);
}

static_always_inline u8x64
gmul4_lo_hi (u8x64 a, u8x64 b)
{
  return (u8x64) _mm512_clmulepi64_epi128 ((__m512i) a, (__m512i) b, 0x10);
}

static_always_inline u8x64
gmul4_hi_hi (u8x64 a, u8x64 b)
{
  return (u8x64) _mm512_clmulepi64_epi128 ((__m512i) a, (__m512i) b, 0x11);
}

static_always_inline void
ghash4_mul_first (ghash_data_t *gd, u8x64 a, u8x64 b)
{
  gd->hi4 = gmul4_hi_hi (a, b);
  gd->lo4 = gmul4_lo_lo (a, b);
  gd->mid4 = gmul4_hi_lo (a, b) ^ gmul4_lo_hi (a, b);
  gd->pending = 0;
}

static_always_inline void
ghash4_mul_next (ghash_data_t *gd, u8x64 a, u8x64 b)
{
  u8x64 hi = gmul4_hi_hi (a, b);
  u8x64 lo = gmul4_lo_lo (a, b);

  if (gd->pending)
    {
      /* there is peding data from previous invocation so we can XOR */
      gd->hi4 = u8x64_xor3 (gd->hi4, gd->tmp_hi4, hi);
      gd->lo4 = u8x64_xor3 (gd->lo4, gd->tmp_lo4, lo);
      gd->pending = 0;
    }
  else
    {
      /* there is no peding data from previous invocation so we postpone XOR */
      gd->tmp_hi4 = hi;
      gd->tmp_lo4 = lo;
      gd->pending = 1;
    }
  gd->mid4 = u8x64_xor3 (gd->mid4, gmul4_hi_lo (a, b), gmul4_lo_hi (a, b));
}

static_always_inline void
ghash4_reduce (ghash_data_t *gd)
{
  u8x64 r;

  /* Final combination:
     gd->lo4 ^= gd->mid4 << 64
     gd->hi4 ^= gd->mid4 >> 64 */

  u8x64 midl = u8x64_word_shift_left (gd->mid4, 8);
  u8x64 midr = u8x64_word_shift_right (gd->mid4, 8);

  if (gd->pending)
    {
      gd->lo4 = u8x64_xor3 (gd->lo4, gd->tmp_lo4, midl);
      gd->hi4 = u8x64_xor3 (gd->hi4, gd->tmp_hi4, midr);
    }
  else
    {
      gd->lo4 ^= midl;
      gd->hi4 ^= midr;
    }

  r = gmul4_hi_lo (ghash4_poly2, gd->lo4);
  gd->lo4 ^= u8x64_word_shift_left (r, 8);
}

static_always_inline void
ghash4_reduce2 (ghash_data_t *gd)
{
  gd->tmp_lo4 = gmul4_lo_lo (ghash4_poly2, gd->lo4);
  gd->tmp_hi4 = gmul4_lo_hi (ghash4_poly2, gd->lo4);
}

static_always_inline u8x16
ghash4_final (ghash_data_t *gd)
{
  u8x64 r;
  u8x32 t;

  r = u8x64_xor3 (gd->hi4, u8x64_word_shift_right (gd->tmp_lo4, 4),
		  u8x64_word_shift_left (gd->tmp_hi4, 4));

  /* horizontal XOR of 4 128-bit lanes */
  t = u8x64_extract_lo (r) ^ u8x64_extract_hi (r);
  return u8x32_extract_hi (t) ^ u8x32_extract_lo (t);
}
#endif

#if defined(__VPCLMULQDQ__)

static const u8x32 ghash2_poly2 = {
  0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0xc2, 0x00, 0x00, 0x00, 0xc2, 0x01, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2,
};

static_always_inline u8x32
gmul2_lo_lo (u8x32 a, u8x32 b)
{
  return (u8x32) _mm256_clmulepi64_epi128 ((__m256i) a, (__m256i) b, 0x00);
}

static_always_inline u8x32
gmul2_hi_lo (u8x32 a, u8x32 b)
{
  return (u8x32) _mm256_clmulepi64_epi128 ((__m256i) a, (__m256i) b, 0x01);
}

static_always_inline u8x32
gmul2_lo_hi (u8x32 a, u8x32 b)
{
  return (u8x32) _mm256_clmulepi64_epi128 ((__m256i) a, (__m256i) b, 0x10);
}

static_always_inline u8x32
gmul2_hi_hi (u8x32 a, u8x32 b)
{
  return (u8x32) _mm256_clmulepi64_epi128 ((__m256i) a, (__m256i) b, 0x11);
}

static_always_inline void
ghash2_mul_first (ghash_data_t *gd, u8x32 a, u8x32 b)
{
  gd->hi2 = gmul2_hi_hi (a, b);
  gd->lo2 = gmul2_lo_lo (a, b);
  gd->mid2 = gmul2_hi_lo (a, b) ^ gmul2_lo_hi (a, b);
  gd->pending = 0;
}

static_always_inline void
ghash2_mul_next (ghash_data_t *gd, u8x32 a, u8x32 b)
{
  u8x32 hi = gmul2_hi_hi (a, b);
  u8x32 lo = gmul2_lo_lo (a, b);

  if (gd->pending)
    {
      /* there is peding data from previous invocation so we can XOR */
      gd->hi2 = u8x32_xor3 (gd->hi2, gd->tmp_hi2, hi);
      gd->lo2 = u8x32_xor3 (gd->lo2, gd->tmp_lo2, lo);
      gd->pending = 0;
    }
  else
    {
      /* there is no peding data from previous invocation so we postpone XOR */
      gd->tmp_hi2 = hi;
      gd->tmp_lo2 = lo;
      gd->pending = 1;
    }
  gd->mid2 = u8x32_xor3 (gd->mid2, gmul2_hi_lo (a, b), gmul2_lo_hi (a, b));
}

static_always_inline void
ghash2_reduce (ghash_data_t *gd)
{
  u8x32 r;

  /* Final combination:
     gd->lo2 ^= gd->mid2 << 64
     gd->hi2 ^= gd->mid2 >> 64 */

  u8x32 midl = u8x32_word_shift_left (gd->mid2, 8);
  u8x32 midr = u8x32_word_shift_right (gd->mid2, 8);

  if (gd->pending)
    {
      gd->lo2 = u8x32_xor3 (gd->lo2, gd->tmp_lo2, midl);
      gd->hi2 = u8x32_xor3 (gd->hi2, gd->tmp_hi2, midr);
    }
  else
    {
      gd->lo2 ^= midl;
      gd->hi2 ^= midr;
    }

  r = gmul2_hi_lo (ghash2_poly2, gd->lo2);
  gd->lo2 ^= u8x32_word_shift_left (r, 8);
}

static_always_inline void
ghash2_reduce2 (ghash_data_t *gd)
{
  gd->tmp_lo2 = gmul2_lo_lo (ghash2_poly2, gd->lo2);
  gd->tmp_hi2 = gmul2_lo_hi (ghash2_poly2, gd->lo2);
}

static_always_inline u8x16
ghash2_final (ghash_data_t *gd)
{
  u8x32 r;

  r = u8x32_xor3 (gd->hi2, u8x32_word_shift_right (gd->tmp_lo2, 4),
		  u8x32_word_shift_left (gd->tmp_hi2, 4));

  /* horizontal XOR of 2 128-bit lanes */
  return u8x32_extract_hi (r) ^ u8x32_extract_lo (r);
}
#endif

static_always_inline void
ghash_precompute (u8x16 H, u8x16 * Hi, int n)
{
  u8x16 r8;
  u32x4 r32;
  /* calcullate H<<1 mod poly from the hash key */
  r8 = (u8x16) ((u64x2) H >> 63);
  H = (u8x16) ((u64x2) H << 1);
  H |= u8x16_word_shift_left (r8, 8);
  r32 = (u32x4) u8x16_word_shift_right (r8, 8);
#ifdef __SSE2__
  r32 = u32x4_shuffle (r32, 0, 1, 2, 0);
#else
  r32[3] = r32[0];
#endif
  r32 = r32 == (u32x4) {1, 0, 0, 1};
  Hi[n - 1] = H = H ^ ((u8x16) r32 & ghash_poly);

  /* calculate H^(i + 1) */
  for (int i = n - 2; i >= 0; i--)
    Hi[i] = ghash_mul (H, Hi[i + 1]);
}

#endif /* __ghash_h__ */