polyval.c
Go to the documentation of this file.
1 /* Copyright (c) 2016, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/base.h>
16 
17 #include <assert.h>
18 #include <string.h>
19 
20 #include "internal.h"
21 #include "../../internal.h"
22 
23 
24 // byte_reverse reverses the order of the bytes in |b->c|.
25 static void byte_reverse(polyval_block *b) {
26  const uint64_t t = CRYPTO_bswap8(b->u[0]);
27  b->u[0] = CRYPTO_bswap8(b->u[1]);
28  b->u[1] = t;
29 }
30 
31 // reverse_and_mulX_ghash interprets the bytes |b->c| as a reversed element of
32 // the GHASH field, multiplies that by 'x' and serialises the result back into
33 // |b|, but with GHASH's backwards bit ordering.
35  uint64_t hi = b->u[0];
36  uint64_t lo = b->u[1];
37  const crypto_word_t carry = constant_time_eq_w(hi & 1, 1);
38  hi >>= 1;
39  hi |= lo << 63;
40  lo >>= 1;
41  lo ^= ((uint64_t) constant_time_select_w(carry, 0xe1, 0)) << 56;
42 
43  b->u[0] = CRYPTO_bswap8(lo);
44  b->u[1] = CRYPTO_bswap8(hi);
45 }
46 
47 // POLYVAL(H, X_1, ..., X_n) =
48 // ByteReverse(GHASH(mulX_GHASH(ByteReverse(H)), ByteReverse(X_1), ...,
49 // ByteReverse(X_n))).
50 //
51 // See https://tools.ietf.org/html/draft-irtf-cfrg-gcmsiv-02#appendix-A.
52 
53 void CRYPTO_POLYVAL_init(struct polyval_ctx *ctx, const uint8_t key[16]) {
55  OPENSSL_memcpy(H.c, key, 16);
57 
58  int is_avx;
59  CRYPTO_ghash_init(&ctx->gmult, &ctx->ghash, &ctx->H, ctx->Htable, &is_avx,
60  H.c);
61  OPENSSL_memset(&ctx->S, 0, sizeof(ctx->S));
62 }
63 
65  size_t in_len) {
66  assert((in_len & 15) == 0);
68 
69  while (in_len > 0) {
70  size_t todo = in_len;
71  if (todo > sizeof(reversed)) {
72  todo = sizeof(reversed);
73  }
75  in += todo;
76  in_len -= todo;
77 
78  size_t blocks = todo / sizeof(polyval_block);
79  for (size_t i = 0; i < blocks; i++) {
81  }
82 
83  ctx->ghash(ctx->S.u, ctx->Htable, (const uint8_t *) reversed, todo);
84  }
85 }
86 
87 void CRYPTO_POLYVAL_finish(const struct polyval_ctx *ctx, uint8_t out[16]) {
88  polyval_block S = ctx->S;
89  byte_reverse(&S);
90  OPENSSL_memcpy(out, &S.c, sizeof(polyval_block));
91 }
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
ctx
Definition: benchmark-async.c:30
S
#define S(s)
string.h
CRYPTO_bswap8
static uint64_t CRYPTO_bswap8(uint64_t x)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:759
cpp.ast.reversed
def reversed(seq)
Definition: bloaty/third_party/googletest/googlemock/scripts/generator/cpp/ast.py:52
CRYPTO_POLYVAL_finish
void CRYPTO_POLYVAL_finish(const struct polyval_ctx *ctx, uint8_t out[16])
Definition: polyval.c:87
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
base.h
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
reverse_and_mulX_ghash
static void reverse_and_mulX_ghash(polyval_block *b)
Definition: polyval.c:34
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
H
#define H(b, c, d)
Definition: md4.c:114
CRYPTO_ghash_init
#define CRYPTO_ghash_init
Definition: boringssl_prefix_symbols.h:1170
internal.h
key
const char * key
Definition: hpack_parser_table.cc:164
byte_reverse
static void byte_reverse(polyval_block *b)
Definition: polyval.c:25
CRYPTO_POLYVAL_init
void CRYPTO_POLYVAL_init(struct polyval_ctx *ctx, const uint8_t key[16])
Definition: polyval.c:53
polyval_block
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/internal.h:387
constant_time_select_w
static crypto_word_t constant_time_select_w(crypto_word_t mask, crypto_word_t a, crypto_word_t b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:420
polyval_ctx
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/modes/internal.h:392
CRYPTO_POLYVAL_update_blocks
void CRYPTO_POLYVAL_update_blocks(struct polyval_ctx *ctx, const uint8_t *in, size_t in_len)
Definition: polyval.c:64
constant_time_eq_w
static crypto_word_t constant_time_eq_w(crypto_word_t a, crypto_word_t b)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:394
mkowners.todo
todo
Definition: mkowners.py:209
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:53