Cryptor.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace RNCryptor\RNCryptor;
  3. use stdClass;
  4. class Cryptor
  5. {
  6. const DEFAULT_SCHEMA_VERSION = 3;
  7. protected $config;
  8. protected $iterations = 10000;
  9. /**
  10. * Set the number of PBKDF2 iterations to use
  11. *
  12. * NOTE: The RNCryptor spec explicitly specifies 10,000 iterations and does not allow this to be customized.
  13. * However, some integrators desire to customize it anyway in order to reduce CPU consumption. This is
  14. * supported by some implementations in other languages (e.g. Java, Swift). So we're allowing it here too.
  15. *
  16. * WARNING: Reducing the number of iterations weakens security. Only do this if you are sure you need it, and
  17. * are prepared to deal with the potential consequences.
  18. *
  19. * IMPORTANT: The same number of iterations MUST be used for encrypting a given payload as are used for decrypting
  20. * it, and the encrypted payload doesn't inform the decryptor about how many iterations were used. Therefore,
  21. * any custom number of iterations is out-of-band and will have to be known ahead of time by whatever implementation
  22. * is decrypting the same payload.
  23. *
  24. * @param int $iterations Number of iterations
  25. */
  26. public function setIterations($iterations) : self
  27. {
  28. $this->iterations = $iterations;
  29. return $this;
  30. }
  31. /**
  32. * @return int Number of iterations which this instance is configured to use
  33. */
  34. public function getIterations()
  35. {
  36. return $this->iterations;
  37. }
  38. public function generateKey($salt, $password, $version = self::DEFAULT_SCHEMA_VERSION)
  39. {
  40. $this->configure($version);
  41. return $this->makeKey($salt, $password);
  42. }
  43. protected function aesCtrLittleEndianCrypt($payload, $key, $iv)
  44. {
  45. $numOfBlocks = ceil(strlen($payload) / strlen($iv));
  46. $counter = '';
  47. for ($i = 0; $i < $numOfBlocks; ++$i) {
  48. $counter .= $iv;
  49. // Yes, the next line only ever increments the first character
  50. // of the counter string, ignoring overflow conditions. This
  51. // matches CommonCrypto's behavior!
  52. $iv[0] = chr(ord($iv[0]) + 1);
  53. }
  54. return $payload ^ $this->encryptInternal($key, $counter, 'ecb');
  55. }
  56. protected function encryptInternal($key, $payload, $mode, $iv = null)
  57. {
  58. return openssl_encrypt($payload, $this->config->algorithm . $mode, $key, OPENSSL_RAW_DATA, (string)$iv);
  59. }
  60. protected function makeHmac(stdClass $components, $hmacKey)
  61. {
  62. $hmacMessage = '';
  63. if ($this->config->hmac->includesHeader) {
  64. $hmacMessage .= ''
  65. . $components->headers->version
  66. . $components->headers->options
  67. . (isset($components->headers->encSalt) ? $components->headers->encSalt : '')
  68. . (isset($components->headers->hmacSalt) ? $components->headers->hmacSalt : '')
  69. . $components->headers->iv;
  70. }
  71. $hmacMessage .= $components->ciphertext;
  72. $hmac = hash_hmac($this->config->hmac->algorithm, $hmacMessage, $hmacKey, true);
  73. if ($this->config->hmac->includesPadding) {
  74. $hmac = str_pad($hmac, $this->config->hmac->length, chr(0));
  75. }
  76. return $hmac;
  77. }
  78. protected function makeKey($salt, $password)
  79. {
  80. if ($this->config->truncatesMultibytePasswords) {
  81. $utf8Length = mb_strlen($password, 'utf-8');
  82. $password = substr($password, 0, $utf8Length);
  83. }
  84. $algo = $this->config->pbkdf2->prf;
  85. $iterations = $this->config->pbkdf2->iterations;
  86. $length = $this->config->pbkdf2->keyLength;
  87. return hash_pbkdf2($algo, $password, $salt, $iterations, $length, true);
  88. }
  89. protected function configure($version)
  90. {
  91. $config = new stdClass;
  92. $config->algorithm = 'aes-256-';
  93. $config->saltLength = 8;
  94. $config->ivLength = 16;
  95. $config->pbkdf2 = new stdClass;
  96. $config->pbkdf2->prf = 'sha1';
  97. $config->pbkdf2->iterations = $this->iterations;
  98. $config->pbkdf2->keyLength = 32;
  99. $config->hmac = new stdClass();
  100. $config->hmac->length = 32;
  101. if (!$version) {
  102. $this->configureVersionZero($config);
  103. } elseif ($version <= 3) {
  104. $config->mode = 'cbc';
  105. $config->options = 1;
  106. $config->hmac->algorithm = 'sha256';
  107. $config->hmac->includesPadding = false;
  108. switch ($version) {
  109. case 1:
  110. $config->hmac->includesHeader = false;
  111. $config->truncatesMultibytePasswords = true;
  112. break;
  113. case 2:
  114. $config->hmac->includesHeader = true;
  115. $config->truncatesMultibytePasswords = true;
  116. break;
  117. case 3:
  118. $config->hmac->includesHeader = true;
  119. $config->truncatesMultibytePasswords = false;
  120. break;
  121. }
  122. } else {
  123. throw new \RuntimeException('Unsupported schema version ' . $version);
  124. }
  125. $this->config = $config;
  126. }
  127. private function configureVersionZero(stdClass $config)
  128. {
  129. $config->mode = 'ctr';
  130. $config->options = 0;
  131. $config->hmac->includesHeader = false;
  132. $config->hmac->algorithm = 'sha1';
  133. $config->hmac->includesPadding = true;
  134. $config->truncatesMultibytePasswords = true;
  135. }
  136. }