@@ -44,99 +44,99 @@ |
||
| 44 | 44 | * @package OC\Security |
| 45 | 45 | */ |
| 46 | 46 | class Crypto implements ICrypto { |
| 47 | - /** @var AES $cipher */ |
|
| 48 | - private $cipher; |
|
| 49 | - /** @var int */ |
|
| 50 | - private $ivLength = 16; |
|
| 51 | - /** @var IConfig */ |
|
| 52 | - private $config; |
|
| 53 | - /** @var ISecureRandom */ |
|
| 54 | - private $random; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @param IConfig $config |
|
| 58 | - * @param ISecureRandom $random |
|
| 59 | - */ |
|
| 60 | - public function __construct(IConfig $config, ISecureRandom $random) { |
|
| 61 | - $this->cipher = new AES(); |
|
| 62 | - $this->config = $config; |
|
| 63 | - $this->random = $random; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param string $message The message to authenticate |
|
| 68 | - * @param string $password Password to use (defaults to `secret` in config.php) |
|
| 69 | - * @return string Calculated HMAC |
|
| 70 | - */ |
|
| 71 | - public function calculateHMAC(string $message, string $password = ''): string { |
|
| 72 | - if($password === '') { |
|
| 73 | - $password = $this->config->getSystemValue('secret'); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - // Append an "a" behind the password and hash it to prevent reusing the same password as for encryption |
|
| 77 | - $password = hash('sha512', $password . 'a'); |
|
| 78 | - |
|
| 79 | - $hash = new Hash('sha512'); |
|
| 80 | - $hash->setKey($password); |
|
| 81 | - return $hash->hash($message); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Encrypts a value and adds an HMAC (Encrypt-Then-MAC) |
|
| 86 | - * @param string $plaintext |
|
| 87 | - * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 88 | - * @return string Authenticated ciphertext |
|
| 89 | - */ |
|
| 90 | - public function encrypt(string $plaintext, string $password = ''): string { |
|
| 91 | - if($password === '') { |
|
| 92 | - $password = $this->config->getSystemValue('secret'); |
|
| 93 | - } |
|
| 94 | - $this->cipher->setPassword($password); |
|
| 95 | - |
|
| 96 | - $iv = $this->random->generate($this->ivLength); |
|
| 97 | - $this->cipher->setIV($iv); |
|
| 98 | - |
|
| 99 | - $ciphertext = bin2hex($this->cipher->encrypt($plaintext)); |
|
| 100 | - $hmac = bin2hex($this->calculateHMAC($ciphertext.$iv, $password)); |
|
| 101 | - |
|
| 102 | - return $ciphertext.'|'.$iv.'|'.$hmac; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Decrypts a value and verifies the HMAC (Encrypt-Then-Mac) |
|
| 107 | - * @param string $authenticatedCiphertext |
|
| 108 | - * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 109 | - * @return string plaintext |
|
| 110 | - * @throws \Exception If the HMAC does not match |
|
| 111 | - * @throws \Exception If the decryption failed |
|
| 112 | - */ |
|
| 113 | - public function decrypt(string $authenticatedCiphertext, string $password = ''): string { |
|
| 114 | - if ($password === '') { |
|
| 115 | - $password = $this->config->getSystemValue('secret'); |
|
| 116 | - } |
|
| 117 | - $this->cipher->setPassword($password); |
|
| 118 | - |
|
| 119 | - $parts = explode('|', $authenticatedCiphertext); |
|
| 120 | - if (\count($parts) !== 3) { |
|
| 121 | - throw new \Exception('Authenticated ciphertext could not be decoded.'); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - $ciphertext = hex2bin($parts[0]); |
|
| 125 | - $iv = $parts[1]; |
|
| 126 | - $hmac = hex2bin($parts[2]); |
|
| 127 | - |
|
| 128 | - $this->cipher->setIV($iv); |
|
| 129 | - |
|
| 130 | - if (!hash_equals($this->calculateHMAC($parts[0] . $parts[1], $password), $hmac)) { |
|
| 131 | - throw new \Exception('HMAC does not match.'); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - $result = $this->cipher->decrypt($ciphertext); |
|
| 135 | - if ($result === false) { |
|
| 136 | - throw new \Exception('Decryption failed'); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $result; |
|
| 140 | - } |
|
| 47 | + /** @var AES $cipher */ |
|
| 48 | + private $cipher; |
|
| 49 | + /** @var int */ |
|
| 50 | + private $ivLength = 16; |
|
| 51 | + /** @var IConfig */ |
|
| 52 | + private $config; |
|
| 53 | + /** @var ISecureRandom */ |
|
| 54 | + private $random; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @param IConfig $config |
|
| 58 | + * @param ISecureRandom $random |
|
| 59 | + */ |
|
| 60 | + public function __construct(IConfig $config, ISecureRandom $random) { |
|
| 61 | + $this->cipher = new AES(); |
|
| 62 | + $this->config = $config; |
|
| 63 | + $this->random = $random; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param string $message The message to authenticate |
|
| 68 | + * @param string $password Password to use (defaults to `secret` in config.php) |
|
| 69 | + * @return string Calculated HMAC |
|
| 70 | + */ |
|
| 71 | + public function calculateHMAC(string $message, string $password = ''): string { |
|
| 72 | + if($password === '') { |
|
| 73 | + $password = $this->config->getSystemValue('secret'); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + // Append an "a" behind the password and hash it to prevent reusing the same password as for encryption |
|
| 77 | + $password = hash('sha512', $password . 'a'); |
|
| 78 | + |
|
| 79 | + $hash = new Hash('sha512'); |
|
| 80 | + $hash->setKey($password); |
|
| 81 | + return $hash->hash($message); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Encrypts a value and adds an HMAC (Encrypt-Then-MAC) |
|
| 86 | + * @param string $plaintext |
|
| 87 | + * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 88 | + * @return string Authenticated ciphertext |
|
| 89 | + */ |
|
| 90 | + public function encrypt(string $plaintext, string $password = ''): string { |
|
| 91 | + if($password === '') { |
|
| 92 | + $password = $this->config->getSystemValue('secret'); |
|
| 93 | + } |
|
| 94 | + $this->cipher->setPassword($password); |
|
| 95 | + |
|
| 96 | + $iv = $this->random->generate($this->ivLength); |
|
| 97 | + $this->cipher->setIV($iv); |
|
| 98 | + |
|
| 99 | + $ciphertext = bin2hex($this->cipher->encrypt($plaintext)); |
|
| 100 | + $hmac = bin2hex($this->calculateHMAC($ciphertext.$iv, $password)); |
|
| 101 | + |
|
| 102 | + return $ciphertext.'|'.$iv.'|'.$hmac; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Decrypts a value and verifies the HMAC (Encrypt-Then-Mac) |
|
| 107 | + * @param string $authenticatedCiphertext |
|
| 108 | + * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 109 | + * @return string plaintext |
|
| 110 | + * @throws \Exception If the HMAC does not match |
|
| 111 | + * @throws \Exception If the decryption failed |
|
| 112 | + */ |
|
| 113 | + public function decrypt(string $authenticatedCiphertext, string $password = ''): string { |
|
| 114 | + if ($password === '') { |
|
| 115 | + $password = $this->config->getSystemValue('secret'); |
|
| 116 | + } |
|
| 117 | + $this->cipher->setPassword($password); |
|
| 118 | + |
|
| 119 | + $parts = explode('|', $authenticatedCiphertext); |
|
| 120 | + if (\count($parts) !== 3) { |
|
| 121 | + throw new \Exception('Authenticated ciphertext could not be decoded.'); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + $ciphertext = hex2bin($parts[0]); |
|
| 125 | + $iv = $parts[1]; |
|
| 126 | + $hmac = hex2bin($parts[2]); |
|
| 127 | + |
|
| 128 | + $this->cipher->setIV($iv); |
|
| 129 | + |
|
| 130 | + if (!hash_equals($this->calculateHMAC($parts[0] . $parts[1], $password), $hmac)) { |
|
| 131 | + throw new \Exception('HMAC does not match.'); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + $result = $this->cipher->decrypt($ciphertext); |
|
| 135 | + if ($result === false) { |
|
| 136 | + throw new \Exception('Decryption failed'); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $result; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | 142 | } |
@@ -69,12 +69,12 @@ discard block |
||
| 69 | 69 | * @return string Calculated HMAC |
| 70 | 70 | */ |
| 71 | 71 | public function calculateHMAC(string $message, string $password = ''): string { |
| 72 | - if($password === '') { |
|
| 72 | + if ($password === '') { |
|
| 73 | 73 | $password = $this->config->getSystemValue('secret'); |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // Append an "a" behind the password and hash it to prevent reusing the same password as for encryption |
| 77 | - $password = hash('sha512', $password . 'a'); |
|
| 77 | + $password = hash('sha512', $password.'a'); |
|
| 78 | 78 | |
| 79 | 79 | $hash = new Hash('sha512'); |
| 80 | 80 | $hash->setKey($password); |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | * @return string Authenticated ciphertext |
| 89 | 89 | */ |
| 90 | 90 | public function encrypt(string $plaintext, string $password = ''): string { |
| 91 | - if($password === '') { |
|
| 91 | + if ($password === '') { |
|
| 92 | 92 | $password = $this->config->getSystemValue('secret'); |
| 93 | 93 | } |
| 94 | 94 | $this->cipher->setPassword($password); |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | |
| 128 | 128 | $this->cipher->setIV($iv); |
| 129 | 129 | |
| 130 | - if (!hash_equals($this->calculateHMAC($parts[0] . $parts[1], $password), $hmac)) { |
|
| 130 | + if (!hash_equals($this->calculateHMAC($parts[0].$parts[1], $password), $hmac)) { |
|
| 131 | 131 | throw new \Exception('HMAC does not match.'); |
| 132 | 132 | } |
| 133 | 133 | |
@@ -37,31 +37,31 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | interface ICrypto { |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param string $message The message to authenticate |
|
| 42 | - * @param string $password Password to use (defaults to `secret` in config.php) |
|
| 43 | - * @return string Calculated HMAC |
|
| 44 | - * @since 8.0.0 |
|
| 45 | - */ |
|
| 46 | - public function calculateHMAC(string $message, string $password = ''): string; |
|
| 40 | + /** |
|
| 41 | + * @param string $message The message to authenticate |
|
| 42 | + * @param string $password Password to use (defaults to `secret` in config.php) |
|
| 43 | + * @return string Calculated HMAC |
|
| 44 | + * @since 8.0.0 |
|
| 45 | + */ |
|
| 46 | + public function calculateHMAC(string $message, string $password = ''): string; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Encrypts a value and adds an HMAC (Encrypt-Then-MAC) |
|
| 50 | - * @param string $plaintext |
|
| 51 | - * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 52 | - * @return string Authenticated ciphertext |
|
| 53 | - * @since 8.0.0 |
|
| 54 | - */ |
|
| 55 | - public function encrypt(string $plaintext, string $password = ''): string; |
|
| 48 | + /** |
|
| 49 | + * Encrypts a value and adds an HMAC (Encrypt-Then-MAC) |
|
| 50 | + * @param string $plaintext |
|
| 51 | + * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 52 | + * @return string Authenticated ciphertext |
|
| 53 | + * @since 8.0.0 |
|
| 54 | + */ |
|
| 55 | + public function encrypt(string $plaintext, string $password = ''): string; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Decrypts a value and verifies the HMAC (Encrypt-Then-Mac) |
|
| 59 | - * @param string $authenticatedCiphertext |
|
| 60 | - * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 61 | - * @return string plaintext |
|
| 62 | - * @throws \Exception If the HMAC does not match |
|
| 63 | - * @throws \Exception If the decryption failed |
|
| 64 | - * @since 8.0.0 |
|
| 65 | - */ |
|
| 66 | - public function decrypt(string $authenticatedCiphertext, string $password = ''): string; |
|
| 57 | + /** |
|
| 58 | + * Decrypts a value and verifies the HMAC (Encrypt-Then-Mac) |
|
| 59 | + * @param string $authenticatedCiphertext |
|
| 60 | + * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
| 61 | + * @return string plaintext |
|
| 62 | + * @throws \Exception If the HMAC does not match |
|
| 63 | + * @throws \Exception If the decryption failed |
|
| 64 | + * @since 8.0.0 |
|
| 65 | + */ |
|
| 66 | + public function decrypt(string $authenticatedCiphertext, string $password = ''): string; |
|
| 67 | 67 | } |