You are here

public function ParagonIE_Sodium_Core32_Poly1305_State::finish in Automatic Updates 8

Same name and namespace in other branches
  1. 7 vendor/paragonie/sodium_compat/src/Core32/Poly1305/State.php \ParagonIE_Sodium_Core32_Poly1305_State::finish()

@internal You should not use this directly from another application

Return value

string

Throws

SodiumException

TypeError

File

vendor/paragonie/sodium_compat/src/Core32/Poly1305/State.php, line 337

Class

ParagonIE_Sodium_Core32_Poly1305_State
Class ParagonIE_Sodium_Core32_Poly1305_State

Code

public function finish() {

  /* process the remaining block */
  if ($this->leftover) {
    $i = $this->leftover;
    $this->buffer[$i++] = 1;
    for (; $i < ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE; ++$i) {
      $this->buffer[$i] = 0;
    }
    $this->final = true;
    $this
      ->blocks(self::substr(self::intArrayToString($this->buffer), 0, ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE), $b = ParagonIE_Sodium_Core32_Poly1305::BLOCK_SIZE);
  }

  /**
   * @var ParagonIE_Sodium_Core32_Int32 $f
   * @var ParagonIE_Sodium_Core32_Int32 $g0
   * @var ParagonIE_Sodium_Core32_Int32 $g1
   * @var ParagonIE_Sodium_Core32_Int32 $g2
   * @var ParagonIE_Sodium_Core32_Int32 $g3
   * @var ParagonIE_Sodium_Core32_Int32 $g4
   * @var ParagonIE_Sodium_Core32_Int32 $h0
   * @var ParagonIE_Sodium_Core32_Int32 $h1
   * @var ParagonIE_Sodium_Core32_Int32 $h2
   * @var ParagonIE_Sodium_Core32_Int32 $h3
   * @var ParagonIE_Sodium_Core32_Int32 $h4
   */
  $h0 = $this->h[0];
  $h1 = $this->h[1];
  $h2 = $this->h[2];
  $h3 = $this->h[3];
  $h4 = $this->h[4];
  $c = $h1
    ->shiftRight(26);

  # $c = $h1 >> 26;
  $h1 = $h1
    ->mask(0x3ffffff);

  # $h1 &= 0x3ffffff;
  $h2 = $h2
    ->addInt32($c);

  # $h2 += $c;
  $c = $h2
    ->shiftRight(26);

  # $c = $h2 >> 26;
  $h2 = $h2
    ->mask(0x3ffffff);

  # $h2 &= 0x3ffffff;
  $h3 = $h3
    ->addInt32($c);

  # $h3 += $c;
  $c = $h3
    ->shiftRight(26);

  # $c = $h3 >> 26;
  $h3 = $h3
    ->mask(0x3ffffff);

  # $h3 &= 0x3ffffff;
  $h4 = $h4
    ->addInt32($c);

  # $h4 += $c;
  $c = $h4
    ->shiftRight(26);

  # $c = $h4 >> 26;
  $h4 = $h4
    ->mask(0x3ffffff);

  # $h4 &= 0x3ffffff;
  $h0 = $h0
    ->addInt32($c
    ->mulInt(5, 3));

  # $h0 += self::mul($c, 5);
  $c = $h0
    ->shiftRight(26);

  # $c = $h0 >> 26;
  $h0 = $h0
    ->mask(0x3ffffff);

  # $h0 &= 0x3ffffff;
  $h1 = $h1
    ->addInt32($c);

  # $h1 += $c;

  /* compute h + -p */
  $g0 = $h0
    ->addInt(5);
  $c = $g0
    ->shiftRight(26);
  $g0 = $g0
    ->mask(0x3ffffff);
  $g1 = $h1
    ->addInt32($c);
  $c = $g1
    ->shiftRight(26);
  $g1 = $g1
    ->mask(0x3ffffff);
  $g2 = $h2
    ->addInt32($c);
  $c = $g2
    ->shiftRight(26);
  $g2 = $g2
    ->mask(0x3ffffff);
  $g3 = $h3
    ->addInt32($c);
  $c = $g3
    ->shiftRight(26);
  $g3 = $g3
    ->mask(0x3ffffff);
  $g4 = $h4
    ->addInt32($c)
    ->subInt(1 << 26);

  # $mask = ($g4 >> 31) - 1;

  /* select h if h < p, or h + -p if h >= p */
  $mask = (int) (($g4
    ->toInt() >> 31) + 1);
  $g0 = $g0
    ->mask($mask);
  $g1 = $g1
    ->mask($mask);
  $g2 = $g2
    ->mask($mask);
  $g3 = $g3
    ->mask($mask);
  $g4 = $g4
    ->mask($mask);

  /** @var int $mask */
  $mask = ~$mask & 0xffffffff;
  $h0 = $h0
    ->mask($mask)
    ->orInt32($g0);
  $h1 = $h1
    ->mask($mask)
    ->orInt32($g1);
  $h2 = $h2
    ->mask($mask)
    ->orInt32($g2);
  $h3 = $h3
    ->mask($mask)
    ->orInt32($g3);
  $h4 = $h4
    ->mask($mask)
    ->orInt32($g4);

  /* h = h % (2^128) */
  $h0 = $h0
    ->orInt32($h1
    ->shiftLeft(26));
  $h1 = $h1
    ->shiftRight(6)
    ->orInt32($h2
    ->shiftLeft(20));
  $h2 = $h2
    ->shiftRight(12)
    ->orInt32($h3
    ->shiftLeft(14));
  $h3 = $h3
    ->shiftRight(18)
    ->orInt32($h4
    ->shiftLeft(8));

  /* mac = (h + pad) % (2^128) */
  $f = $h0
    ->toInt64()
    ->addInt64($this->pad[0]);
  $h0 = $f
    ->toInt32();
  $f = $h1
    ->toInt64()
    ->addInt64($this->pad[1])
    ->addInt($h0->overflow);
  $h1 = $f
    ->toInt32();
  $f = $h2
    ->toInt64()
    ->addInt64($this->pad[2])
    ->addInt($h1->overflow);
  $h2 = $f
    ->toInt32();
  $f = $h3
    ->toInt64()
    ->addInt64($this->pad[3])
    ->addInt($h2->overflow);
  $h3 = $f
    ->toInt32();
  return $h0
    ->toReverseString() . $h1
    ->toReverseString() . $h2
    ->toReverseString() . $h3
    ->toReverseString();
}