Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

95 linhas
2.9 KiB

  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. #include <openssl/base.h>
  15. #if !defined(OPENSSL_SMALL)
  16. #include <assert.h>
  17. #include <string.h>
  18. #include "internal.h"
  19. #include "../internal.h"
  20. /* byte_reverse reverses the order of the bytes in |b->c|. */
  21. static void byte_reverse(polyval_block *b) {
  22. const uint64_t t = CRYPTO_bswap8(b->u[0]);
  23. b->u[0] = CRYPTO_bswap8(b->u[1]);
  24. b->u[1] = t;
  25. }
  26. /* reverse_and_mulX_ghash interprets the bytes |b->c| as a reversed element of
  27. * the GHASH field, multiplies that by 'x' and serialises the result back into
  28. * |b|, but with GHASH's backwards bit ordering. */
  29. static void reverse_and_mulX_ghash(polyval_block *b) {
  30. uint64_t hi = b->u[0];
  31. uint64_t lo = b->u[1];
  32. const unsigned carry = constant_time_eq(hi & 1, 1);
  33. hi >>= 1;
  34. hi |= lo << 63;
  35. lo >>= 1;
  36. lo ^= ((uint64_t) constant_time_select(carry, 0xe1, 0)) << 56;
  37. b->u[0] = CRYPTO_bswap8(lo);
  38. b->u[1] = CRYPTO_bswap8(hi);
  39. }
  40. /* POLYVAL(H, X_1, ..., X_n) =
  41. * ByteReverse(GHASH(mulX_GHASH(ByteReverse(H)), ByteReverse(X_1), ...,
  42. * ByteReverse(X_n))).
  43. *
  44. * See https://tools.ietf.org/html/draft-irtf-cfrg-gcmsiv-02#appendix-A. */
  45. void CRYPTO_POLYVAL_init(struct polyval_ctx *ctx, const uint8_t key[16]) {
  46. polyval_block H;
  47. OPENSSL_memcpy(H.c, key, 16);
  48. reverse_and_mulX_ghash(&H);
  49. CRYPTO_ghash_init(&ctx->gmult, &ctx->ghash, &ctx->H, ctx->Htable, H.c);
  50. OPENSSL_memset(&ctx->S, 0, sizeof(ctx->S));
  51. }
  52. void CRYPTO_POLYVAL_update_blocks(struct polyval_ctx *ctx, const uint8_t *in,
  53. size_t in_len) {
  54. assert((in_len & 15) == 0);
  55. polyval_block reversed[32];
  56. while (in_len > 0) {
  57. size_t todo = in_len;
  58. if (todo > sizeof(reversed)) {
  59. todo = sizeof(reversed);
  60. }
  61. OPENSSL_memcpy(reversed, in, todo);
  62. in += todo;
  63. in_len -= todo;
  64. size_t blocks = todo / sizeof(polyval_block);
  65. for (size_t i = 0; i < blocks; i++) {
  66. byte_reverse(&reversed[i]);
  67. }
  68. ctx->ghash(ctx->S.u, ctx->Htable, (const uint8_t *) reversed, todo);
  69. }
  70. }
  71. void CRYPTO_POLYVAL_finish(const struct polyval_ctx *ctx, uint8_t out[16]) {
  72. polyval_block S = ctx->S;
  73. byte_reverse(&S);
  74. OPENSSL_memcpy(out, &S.c, sizeof(polyval_block));
  75. }
  76. #endif /* !OPENSSL_SMALL */