27 if (MacEnum::MAC_POLY1305() !== $macAlgorithm) {
28 throw new \InvalidArgumentException(
'Unsupported algorithm');
31 if (32 !== strlen($key)) {
32 throw new \InvalidArgumentException(
'Invalid key');
35 $r = substr($key, 0, 16);
36 $s = substr($key, 16);
39 $r[ 3] =
$r[ 3] & $t1;
40 $r[ 4] =
$r[ 4] & $t2;
41 $r[ 7] =
$r[ 7] & $t1;
42 $r[ 8] =
$r[ 8] & $t2;
43 $r[11] =
$r[11] & $t1;
44 $r[12] =
$r[12] & $t2;
45 $r[15] =
$r[15] & $t1;
46 $this->s = gmp_init(bin2hex(strrev(
$s)), 16);
47 $this->r = gmp_init(bin2hex(strrev(
$r)), 16);
51 protected function internalUpdate($data)
56 protected function internalFinalize()
59 if (
'' !== $this->data) {
60 $chunks = str_split($this->data, 16);
62 foreach ($chunks as $i => $chunk) {
63 $t = gmp_init(bin2hex(strrev($chunk .
"\x01")), 16);
64 $res = gmp_add($res, gmp_mul($t, gmp_pow($this->r, $q - $i)));
66 $res = gmp_mod($res, gmp_sub(gmp_pow(2, 130), 5));
69 $res = gmp_mod(gmp_add($res, $this->s), gmp_pow(2, 128));
70 return strrev(pack(
'H*', str_pad(gmp_strval($res, 16), 32,
'0', STR_PAD_LEFT)));
__construct(MacEnum $macAlgorithm, SubAlgorithmAbstractEnum $innerAlgorithm, $key, $nonce= '')
$s
Second half of secret key.
$r
First half of secret key (second half in the specs due to endianness).