Crypto.php 4.0 KB

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