diff options
author | zaycevm <[email protected]> | 2025-08-12 18:23:32 +0300 |
---|---|---|
committer | zaycevm <[email protected]> | 2025-08-12 18:58:10 +0300 |
commit | 7ce1229be33024092eb56910264510447d21116f (patch) | |
tree | 6697d934399db06c36ebd516ebe6f0a76245a3ba /contrib/restricted/google/boringssl/crypto/fipsmodule/modes/polyval.c | |
parent | 27af3f7dfd34fe3cdd1c1329f3d205ac5ac136a3 (diff) |
BoringSSL as optional cryptobackend for ngtcp2
PR добавляет возможность использовать BoringSSL в ngtcp2 в качестве криптобиблиотеки. Для проектов в Аркадии, уже зависящих от ngtcp2, добавлена явная зависимость от слоя абстракции quictls (сейчас в транке ngtcp2 собирается с quictls).
commit_hash:3d6607abecfcff2157859acbdd18f9d0345ac485
Diffstat (limited to 'contrib/restricted/google/boringssl/crypto/fipsmodule/modes/polyval.c')
-rw-r--r-- | contrib/restricted/google/boringssl/crypto/fipsmodule/modes/polyval.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/contrib/restricted/google/boringssl/crypto/fipsmodule/modes/polyval.c b/contrib/restricted/google/boringssl/crypto/fipsmodule/modes/polyval.c new file mode 100644 index 00000000000..cee7d3ca79e --- /dev/null +++ b/contrib/restricted/google/boringssl/crypto/fipsmodule/modes/polyval.c @@ -0,0 +1,90 @@ +/* Copyright (c) 2016, Google Inc. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ + +#include <contrib/restricted/google/boringssl/include/openssl/base.h> + +#include <assert.h> +#include <string.h> + +#include "internal.h" +#include "../../internal.h" + + +// byte_reverse reverses the order of the bytes in |b->c|. +static void byte_reverse(uint8_t b[16]) { + uint64_t hi = CRYPTO_load_u64_le(b); + uint64_t lo = CRYPTO_load_u64_le(b + 8); + CRYPTO_store_u64_le(b, CRYPTO_bswap8(lo)); + CRYPTO_store_u64_le(b + 8, CRYPTO_bswap8(hi)); +} + +// reverse_and_mulX_ghash interprets |b| as a reversed element of the GHASH +// field, multiplies that by 'x' and serialises the result back into |b|, but +// with GHASH's backwards bit ordering. +static void reverse_and_mulX_ghash(uint8_t b[16]) { + uint64_t hi = CRYPTO_load_u64_le(b); + uint64_t lo = CRYPTO_load_u64_le(b + 8); + const crypto_word_t carry = constant_time_eq_w(hi & 1, 1); + hi >>= 1; + hi |= lo << 63; + lo >>= 1; + lo ^= ((uint64_t) constant_time_select_w(carry, 0xe1, 0)) << 56; + + CRYPTO_store_u64_le(b, CRYPTO_bswap8(lo)); + CRYPTO_store_u64_le(b + 8, CRYPTO_bswap8(hi)); +} + +// POLYVAL(H, X_1, ..., X_n) = +// ByteReverse(GHASH(mulX_GHASH(ByteReverse(H)), ByteReverse(X_1), ..., +// ByteReverse(X_n))). +// +// See https://www.rfc-editor.org/rfc/rfc8452.html#appendix-A. + +void CRYPTO_POLYVAL_init(struct polyval_ctx *ctx, const uint8_t key[16]) { + alignas(8) uint8_t H[16]; + OPENSSL_memcpy(H, key, 16); + reverse_and_mulX_ghash(H); + + int is_avx; + CRYPTO_ghash_init(&ctx->gmult, &ctx->ghash, ctx->Htable, &is_avx, H); + OPENSSL_memset(&ctx->S, 0, sizeof(ctx->S)); +} + +void CRYPTO_POLYVAL_update_blocks(struct polyval_ctx *ctx, const uint8_t *in, + size_t in_len) { + assert((in_len & 15) == 0); + alignas(8) uint8_t buf[32 * 16]; + + while (in_len > 0) { + size_t todo = in_len; + if (todo > sizeof(buf)) { + todo = sizeof(buf); + } + OPENSSL_memcpy(buf, in, todo); + in += todo; + in_len -= todo; + + size_t blocks = todo / 16; + for (size_t i = 0; i < blocks; i++) { + byte_reverse(buf + 16 * i); + } + + ctx->ghash(ctx->S, ctx->Htable, buf, todo); + } +} + +void CRYPTO_POLYVAL_finish(const struct polyval_ctx *ctx, uint8_t out[16]) { + OPENSSL_memcpy(out, &ctx->S, 16); + byte_reverse(out); +} |