crypto.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Security;
  23. use Crypt_AES;
  24. use Crypt_Hash;
  25. use OCP\Security\ICrypto;
  26. use OCP\Security\ISecureRandom;
  27. use OCP\Security\StringUtils;
  28. use OCP\IConfig;
  29. /**
  30. * Class Crypto provides a high-level encryption layer using AES-CBC. If no key has been provided
  31. * it will use the secret defined in config.php as key. Additionally the message will be HMAC'd.
  32. *
  33. * Usage:
  34. * $encryptWithDefaultPassword = \OC::$server->getCrypto()->encrypt('EncryptedText');
  35. * $encryptWithCustompassword = \OC::$server->getCrypto()->encrypt('EncryptedText', 'password');
  36. *
  37. * @package OC\Security
  38. */
  39. class Crypto implements ICrypto {
  40. /** @var Crypt_AES $cipher */
  41. private $cipher;
  42. /** @var int */
  43. private $ivLength = 16;
  44. /** @var IConfig */
  45. private $config;
  46. /** @var ISecureRandom */
  47. private $random;
  48. function __construct(IConfig $config, ISecureRandom $random) {
  49. $this->cipher = new Crypt_AES();
  50. $this->config = $config;
  51. $this->random = $random;
  52. }
  53. /**
  54. * @param string $message The message to authenticate
  55. * @param string $password Password to use (defaults to `secret` in config.php)
  56. * @return string Calculated HMAC
  57. */
  58. public function calculateHMAC($message, $password = '') {
  59. if($password === '') {
  60. $password = $this->config->getSystemValue('secret');
  61. }
  62. // Append an "a" behind the password and hash it to prevent reusing the same password as for encryption
  63. $password = hash('sha512', $password . 'a');
  64. $hash = new Crypt_Hash('sha512');
  65. $hash->setKey($password);
  66. return $hash->hash($message);
  67. }
  68. /**
  69. * Encrypts a value and adds an HMAC (Encrypt-Then-MAC)
  70. * @param string $plaintext
  71. * @param string $password Password to encrypt, if not specified the secret from config.php will be taken
  72. * @return string Authenticated ciphertext
  73. */
  74. public function encrypt($plaintext, $password = '') {
  75. if($password === '') {
  76. $password = $this->config->getSystemValue('secret');
  77. }
  78. $this->cipher->setPassword($password);
  79. $iv = $this->random->getLowStrengthGenerator()->generate($this->ivLength);
  80. $this->cipher->setIV($iv);
  81. $ciphertext = bin2hex($this->cipher->encrypt($plaintext));
  82. $hmac = bin2hex($this->calculateHMAC($ciphertext.$iv, $password));
  83. return $ciphertext.'|'.$iv.'|'.$hmac;
  84. }
  85. /**
  86. * Decrypts a value and verifies the HMAC (Encrypt-Then-Mac)
  87. * @param string $authenticatedCiphertext
  88. * @param string $password Password to encrypt, if not specified the secret from config.php will be taken
  89. * @return string plaintext
  90. * @throws \Exception If the HMAC does not match
  91. */
  92. public function decrypt($authenticatedCiphertext, $password = '') {
  93. if($password === '') {
  94. $password = $this->config->getSystemValue('secret');
  95. }
  96. $this->cipher->setPassword($password);
  97. $parts = explode('|', $authenticatedCiphertext);
  98. if(sizeof($parts) !== 3) {
  99. throw new \Exception('Authenticated ciphertext could not be decoded.');
  100. }
  101. $ciphertext = hex2bin($parts[0]);
  102. $iv = $parts[1];
  103. $hmac = hex2bin($parts[2]);
  104. $this->cipher->setIV($iv);
  105. if(!StringUtils::equals($this->calculateHMAC($parts[0].$parts[1], $password), $hmac)) {
  106. throw new \Exception('HMAC does not match.');
  107. }
  108. return $this->cipher->decrypt($ciphertext);
  109. }
  110. }