589 |
595 |
590 $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
596 $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
591 $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
597 $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
592 |
598 |
593 $hs = hash_init('sha512'); |
599 $hs = hash_init('sha512'); |
594 hash_update($hs, self::substr($az, 32, 32)); |
600 self::hash_update($hs, self::substr($az, 32, 32)); |
595 /** @var resource $hs */ |
601 /** @var resource $hs */ |
596 $hs = self::updateHashWithFile($hs, $fp, $size); |
602 $hs = self::updateHashWithFile($hs, $fp, $size); |
597 |
603 |
598 /** @var string $nonceHash */ |
604 /** @var string $nonceHash */ |
599 $nonceHash = hash_final($hs, true); |
605 $nonceHash = hash_final($hs, true); |
608 $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes( |
614 $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes( |
609 ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce) |
615 ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce) |
610 ); |
616 ); |
611 |
617 |
612 $hs = hash_init('sha512'); |
618 $hs = hash_init('sha512'); |
613 hash_update($hs, self::substr($sig, 0, 32)); |
619 self::hash_update($hs, self::substr($sig, 0, 32)); |
614 hash_update($hs, self::substr($pk, 0, 32)); |
620 self::hash_update($hs, self::substr($pk, 0, 32)); |
615 /** @var resource $hs */ |
621 /** @var resource $hs */ |
616 $hs = self::updateHashWithFile($hs, $fp, $size); |
622 $hs = self::updateHashWithFile($hs, $fp, $size); |
617 |
623 |
618 /** @var string $hramHash */ |
624 /** @var string $hramHash */ |
619 $hramHash = hash_final($hs, true); |
625 $hramHash = hash_final($hs, true); |
720 |
726 |
721 /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ |
727 /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ |
722 $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey); |
728 $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey); |
723 |
729 |
724 $hs = hash_init('sha512'); |
730 $hs = hash_init('sha512'); |
725 hash_update($hs, self::substr($sig, 0, 32)); |
731 self::hash_update($hs, self::substr($sig, 0, 32)); |
726 hash_update($hs, self::substr($publicKey, 0, 32)); |
732 self::hash_update($hs, self::substr($publicKey, 0, 32)); |
727 /** @var resource $hs */ |
733 /** @var resource $hs */ |
728 $hs = self::updateHashWithFile($hs, $fp, $size); |
734 $hs = self::updateHashWithFile($hs, $fp, $size); |
729 /** @var string $hDigest */ |
735 /** @var string $hDigest */ |
730 $hDigest = hash_final($hs, true); |
736 $hDigest = hash_final($hs, true); |
731 |
737 |
1075 |
1081 |
1076 /** |
1082 /** |
1077 * Update a hash context with the contents of a file, without |
1083 * Update a hash context with the contents of a file, without |
1078 * loading the entire file into memory. |
1084 * loading the entire file into memory. |
1079 * |
1085 * |
1080 * @param resource|object $hash |
1086 * @param resource|HashContext $hash |
1081 * @param resource $fp |
1087 * @param resource $fp |
1082 * @param int $size |
1088 * @param int $size |
1083 * @return resource|object Resource on PHP < 7.2, HashContext object on PHP >= 7.2 |
1089 * @return resource|object Resource on PHP < 7.2, HashContext object on PHP >= 7.2 |
1084 * @throws SodiumException |
1090 * @throws SodiumException |
1085 * @throws TypeError |
1091 * @throws TypeError |
1125 if (!is_string($message)) { |
1131 if (!is_string($message)) { |
1126 throw new SodiumException('Unexpected error reading from file.'); |
1132 throw new SodiumException('Unexpected error reading from file.'); |
1127 } |
1133 } |
1128 /** @var string $message */ |
1134 /** @var string $message */ |
1129 /** @psalm-suppress InvalidArgument */ |
1135 /** @psalm-suppress InvalidArgument */ |
1130 hash_update($hash, $message); |
1136 self::hash_update($hash, $message); |
1131 } |
1137 } |
1132 // Reset file pointer's position |
1138 // Reset file pointer's position |
1133 fseek($fp, $originalPosition, SEEK_SET); |
1139 fseek($fp, $originalPosition, SEEK_SET); |
1134 return $hash; |
1140 return $hash; |
1135 } |
1141 } |
1167 |
1173 |
1168 $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
1174 $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
1169 $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
1175 $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
1170 |
1176 |
1171 $hs = hash_init('sha512'); |
1177 $hs = hash_init('sha512'); |
1172 hash_update($hs, self::substr($az, 32, 32)); |
1178 self::hash_update($hs, self::substr($az, 32, 32)); |
1173 /** @var resource $hs */ |
1179 /** @var resource $hs */ |
1174 $hs = self::updateHashWithFile($hs, $fp, $size); |
1180 $hs = self::updateHashWithFile($hs, $fp, $size); |
1175 |
1181 |
1176 /** @var string $nonceHash */ |
1182 /** @var string $nonceHash */ |
1177 $nonceHash = hash_final($hs, true); |
1183 $nonceHash = hash_final($hs, true); |
1186 $sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes( |
1192 $sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes( |
1187 ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce) |
1193 ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce) |
1188 ); |
1194 ); |
1189 |
1195 |
1190 $hs = hash_init('sha512'); |
1196 $hs = hash_init('sha512'); |
1191 hash_update($hs, self::substr($sig, 0, 32)); |
1197 self::hash_update($hs, self::substr($sig, 0, 32)); |
1192 hash_update($hs, self::substr($pk, 0, 32)); |
1198 self::hash_update($hs, self::substr($pk, 0, 32)); |
1193 /** @var resource $hs */ |
1199 /** @var resource $hs */ |
1194 $hs = self::updateHashWithFile($hs, $fp, $size); |
1200 $hs = self::updateHashWithFile($hs, $fp, $size); |
1195 |
1201 |
1196 /** @var string $hramHash */ |
1202 /** @var string $hramHash */ |
1197 $hramHash = hash_final($hs, true); |
1203 $hramHash = hash_final($hs, true); |
1270 |
1276 |
1271 /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ |
1277 /** @var ParagonIE_Sodium_Core32_Curve25519_Ge_P3 $A */ |
1272 $A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime($publicKey); |
1278 $A = ParagonIE_Sodium_Core32_Ed25519::ge_frombytes_negate_vartime($publicKey); |
1273 |
1279 |
1274 $hs = hash_init('sha512'); |
1280 $hs = hash_init('sha512'); |
1275 hash_update($hs, self::substr($sig, 0, 32)); |
1281 self::hash_update($hs, self::substr($sig, 0, 32)); |
1276 hash_update($hs, self::substr($publicKey, 0, 32)); |
1282 self::hash_update($hs, self::substr($publicKey, 0, 32)); |
1277 /** @var resource $hs */ |
1283 /** @var resource $hs */ |
1278 $hs = self::updateHashWithFile($hs, $fp, $size); |
1284 $hs = self::updateHashWithFile($hs, $fp, $size); |
1279 /** @var string $hDigest */ |
1285 /** @var string $hDigest */ |
1280 $hDigest = hash_final($hs, true); |
1286 $hDigest = hash_final($hs, true); |
1281 |
1287 |
1519 $mlen = 0 |
1525 $mlen = 0 |
1520 ) { |
1526 ) { |
1521 /** @var int $pos */ |
1527 /** @var int $pos */ |
1522 $pos = self::ftell($ifp); |
1528 $pos = self::ftell($ifp); |
1523 |
1529 |
1524 /** @var int $iter */ |
|
1525 $iter = 1; |
|
1526 |
|
1527 /** @var int $incr */ |
|
1528 $incr = self::BUFFER_SIZE >> 6; |
|
1529 |
|
1530 while ($mlen > 0) { |
1530 while ($mlen > 0) { |
1531 $blockSize = $mlen > self::BUFFER_SIZE |
1531 $blockSize = $mlen > self::BUFFER_SIZE |
1532 ? self::BUFFER_SIZE |
1532 ? self::BUFFER_SIZE |
1533 : $mlen; |
1533 : $mlen; |
1534 $ciphertext = fread($ifp, $blockSize); |
1534 $ciphertext = fread($ifp, $blockSize); |
1535 if (!is_string($ciphertext)) { |
1535 if (!is_string($ciphertext)) { |
1536 throw new SodiumException('Could not read input file'); |
1536 throw new SodiumException('Could not read input file'); |
1537 } |
1537 } |
1538 $state->update($ciphertext); |
1538 $state->update($ciphertext); |
1539 $mlen -= $blockSize; |
1539 $mlen -= $blockSize; |
1540 $iter += $incr; |
|
1541 } |
1540 } |
1542 $res = ParagonIE_Sodium_Core32_Util::verify_16($tag, $state->finish()); |
1541 $res = ParagonIE_Sodium_Core32_Util::verify_16($tag, $state->finish()); |
1543 |
1542 |
1544 fseek($ifp, $pos, SEEK_SET); |
1543 fseek($ifp, $pos, SEEK_SET); |
1545 return $res; |
1544 return $res; |