wp/wp-includes/sodium_compat/src/File.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 22 8c2e4d02f4ef
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
  1152      * @throws SodiumException
  1152      * @throws SodiumException
  1153      * @throws TypeError
  1153      * @throws TypeError
  1154      */
  1154      */
  1155     private static function sign_core32($filePath, $secretKey)
  1155     private static function sign_core32($filePath, $secretKey)
  1156     {
  1156     {
  1157         /** @var int|bool $size */
       
  1158         $size = filesize($filePath);
  1157         $size = filesize($filePath);
  1159         if (!is_int($size)) {
  1158         if (!is_int($size)) {
  1160             throw new SodiumException('Could not obtain the file size');
  1159             throw new SodiumException('Could not obtain the file size');
  1161         }
  1160         }
  1162         /** @var int $size */
  1161 
  1163 
       
  1164         /** @var resource|bool $fp */
       
  1165         $fp = fopen($filePath, 'rb');
  1162         $fp = fopen($filePath, 'rb');
  1166         if (!is_resource($fp)) {
  1163         if (!is_resource($fp)) {
  1167             throw new SodiumException('Could not open input file for reading');
  1164             throw new SodiumException('Could not open input file for reading');
  1168         }
  1165         }
  1169         /** @var resource $fp */
       
  1170 
  1166 
  1171         /** @var string $az */
  1167         /** @var string $az */
  1172         $az = hash('sha512', self::substr($secretKey, 0, 32), true);
  1168         $az = hash('sha512', self::substr($secretKey, 0, 32), true);
  1173 
  1169 
  1174         $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
  1170         $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);
  1177         $hs = hash_init('sha512');
  1173         $hs = hash_init('sha512');
  1178         self::hash_update($hs, self::substr($az, 32, 32));
  1174         self::hash_update($hs, self::substr($az, 32, 32));
  1179         /** @var resource $hs */
  1175         /** @var resource $hs */
  1180         $hs = self::updateHashWithFile($hs, $fp, $size);
  1176         $hs = self::updateHashWithFile($hs, $fp, $size);
  1181 
  1177 
  1182         /** @var string $nonceHash */
       
  1183         $nonceHash = hash_final($hs, true);
  1178         $nonceHash = hash_final($hs, true);
  1184 
       
  1185         /** @var string $pk */
       
  1186         $pk = self::substr($secretKey, 32, 32);
  1179         $pk = self::substr($secretKey, 32, 32);
  1187 
       
  1188         /** @var string $nonce */
       
  1189         $nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
  1180         $nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32);
  1190 
       
  1191         /** @var string $sig */
       
  1192         $sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes(
  1181         $sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes(
  1193             ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce)
  1182             ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce)
  1194         );
  1183         );
  1195 
  1184 
  1196         $hs = hash_init('sha512');
  1185         $hs = hash_init('sha512');
  1197         self::hash_update($hs, self::substr($sig, 0, 32));
  1186         self::hash_update($hs, self::substr($sig, 0, 32));
  1198         self::hash_update($hs, self::substr($pk, 0, 32));
  1187         self::hash_update($hs, self::substr($pk, 0, 32));
  1199         /** @var resource $hs */
  1188         /** @var resource $hs */
  1200         $hs = self::updateHashWithFile($hs, $fp, $size);
  1189         $hs = self::updateHashWithFile($hs, $fp, $size);
  1201 
  1190 
  1202         /** @var string $hramHash */
       
  1203         $hramHash = hash_final($hs, true);
  1191         $hramHash = hash_final($hs, true);
  1204 
  1192 
  1205         /** @var string $hram */
       
  1206         $hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hramHash);
  1193         $hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hramHash);
  1207 
  1194 
  1208         /** @var string $sigAfter */
       
  1209         $sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd($hram, $az, $nonce);
  1195         $sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd($hram, $az, $nonce);
  1210 
  1196 
  1211         /** @var string $sig */
  1197         /** @var string $sig */
  1212         $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
  1198         $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);
  1213 
  1199 
  1241             throw new SodiumException('S < L - Invalid signature');
  1227             throw new SodiumException('S < L - Invalid signature');
  1242         }
  1228         }
  1243         if (ParagonIE_Sodium_Core32_Ed25519::small_order($sig)) {
  1229         if (ParagonIE_Sodium_Core32_Ed25519::small_order($sig)) {
  1244             throw new SodiumException('Signature is on too small of an order');
  1230             throw new SodiumException('Signature is on too small of an order');
  1245         }
  1231         }
       
  1232 
  1246         if ((self::chrToInt($sig[63]) & 224) !== 0) {
  1233         if ((self::chrToInt($sig[63]) & 224) !== 0) {
  1247             throw new SodiumException('Invalid signature');
  1234             throw new SodiumException('Invalid signature');
  1248         }
  1235         }
  1249         $d = 0;
  1236         $d = 0;
  1250         for ($i = 0; $i < 32; ++$i) {
  1237         for ($i = 0; $i < 32; ++$i) {