StonePhpSafeCrypt_blockscramble.php 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /////////
  3. //
  4. // Stone PHP SafeCrypt Block Scrambler
  5. // -----------------------------------
  6. //
  7. // Block scramble and block descramble are utility functions for Pack crypt and Pack decrypt.
  8. // These are usable as lightweight encryption, and are very fast, but are also very weak and
  9. // should not be used independantly. These are only used because of the specific, well-known
  10. // characteristics of the IV stream as used by Pack crypt, where the OTP nature of the IV
  11. // leader makes these scrambles sufficient to prevent CBC MITM leading-block attacks.
  12. function BlockScramble(&$data, &$weakkey) {
  13. // Performs a simple modulo arithmetic cipher on the IV and datastream. The PHP manual
  14. // incorrectly states that the initialization vector may safely be transmitted plaintext;
  15. // in http://www.ciphersbyritter.com/GLOSSARY.HTM#IV it's made clear that in CBC mode, a
  16. // man in the middle attack is possible on the very first block returned by manipulating
  17. // the IV. However, since the IV is just a randomness salt, it carries all of the
  18. // important characteristics of a truncated one time pad; therefore, rotated with the MD5
  19. // hash of the key, which is well-distributed, we have a non-attackable binary result.
  20. // This protects CBC mode encryptions from a MITM leading block attack; also, it's nice
  21. // to have an extra source of white noise in the signal to slow down identifications.
  22. $strongkey = md5($weakkey);
  23. $keysize = strlen($strongkey); // because calling sizeof() every ten cycles is retarded
  24. $datasize = strlen($data); // and again
  25. $output = str_repeat(' ', $datasize); // pre-allocate output buffer to prevent reallocation thrash
  26. $di = 0; // data index cursor
  27. $bi = 0; // block index cursor
  28. // net2ftp - added the next line to avoid a PHP Notice about an "undefined variable"
  29. $ki = 0;
  30. for (; $di < $datasize; ++$di, ++$ki) {
  31. if ($ki >= $keysize) { $ki = 0; } // key's usually smaller than data, so bound it
  32. $output[$di] = chr((ord($data[$di]) + ord($strongkey[$ki])) % 256); // and record the scrambled byte
  33. }
  34. return $output;
  35. }
  36. function BlockDescramble(&$data, &$weakkey) {
  37. // Performs a simple modulo arithmetic cipher on the IV and datastream. The PHP manual
  38. // incorrectly states that the initialization vector may safely be transmitted plaintext;
  39. // in http://www.ciphersbyritter.com/GLOSSARY.HTM#IV it's made clear that in CBC mode, a
  40. // man in the middle attack is possible on the very first block returned by manipulating
  41. // the IV. However, since the IV is just a randomness salt, it carries all of the
  42. // important characteristics of a truncated one time pad; therefore, rotated with the MD5
  43. // hash of the key, which is well-distributed, we have a non-attackable binary result.
  44. // This protects CBC mode encryptions from a MITM leading block attack.
  45. $strongkey = md5($weakkey);
  46. $output = str_repeat(' ', strlen($data)); // pre-allocate output buffer to prevent reallocation thrash
  47. $keysize = strlen($strongkey); // because calling sizeof() every ten cycles is retarded
  48. $datasize = strlen($data); // and again
  49. $di = 0; // data index cursor
  50. $bi = 0; // block index cursor
  51. // net2ftp - added the next line to avoid a PHP Notice about an "undefined variable"
  52. $ki = 0;
  53. for (; $di < $datasize; ++$di, ++$ki) {
  54. if ($ki >= $keysize) { $ki = 0; } // key's usually smaller than data, so bound it
  55. $work = (ord($data[$di]) - ord($strongkey[$ki])); // descramble the scrambled byte
  56. if ($work < 0) { $work += 256; } // reorigin low-range bytes
  57. $output[$di] = chr($work); // record the origin-normalized byte
  58. }
  59. return $output;
  60. }
  61. ?>