3 namespace CoreFramework\Stream\Crypto;
5 // Import framework stuff
6 use CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
9 * An OpenSSL-based encryption stream
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class OpenSslStream extends BaseStream implements EncryptableStream {
32 * Protected constructor
36 protected function __construct () {
37 // Call parent constructor
38 parent::__construct(__CLASS__);
42 * Creates an instance of this node class
44 * @param $rngInstance An RNG instance
45 * @return $streamInstance An instance of this node class
47 public static final function createOpenSslStream (RandomNumberGenerator $rngInstance) {
49 $streamInstance = new OpenSslStream();
51 // Set the RNG instance
52 $streamInstance->setRngInstance($rngInstance);
54 // Return the instance
55 return $streamInstance;
59 * Encrypt the string with fixed salt
61 * @param $str The unencrypted string
62 * @param $key Optional key, if none provided, a random key will be generated
63 * @return $encrypted Encrypted string
65 public function encryptStream ($str, $key = NULL) {
70 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: key[' . gettype($key) . ']=' . $key);
73 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
74 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
76 // Generate key, if none provided
79 $key = $this->getRngInstance()->generateKey();
82 // Add some "payload" to the string
83 switch ($this->getRngInstance()->randomNumber(0, 8)) {
85 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
89 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
93 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
97 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
101 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
105 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
109 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
113 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
117 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
121 // Encrypt the string
122 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
129 * Decrypt the string with fixed salt
131 * @param $encrypted Encrypted string
132 * @param $key Optional key, if none provided, a random key will be generated
133 * @return $str The unencrypted string
135 public function decryptStream ($encrypted, $key = NULL) {
139 // Init crypto module
140 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
141 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
143 // Shall we use a default key or custom?
145 // Generate (default) key
146 $key = $this->getRngInstance()->generateKey();
149 // Decrypt the string
150 $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
152 // Get the real string out
153 $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString);
155 // Does the element count match?
156 assert(count($strArray) == 3);
159 $str = base64_decode($strArray[1]);
161 // Trim trailing nulls away
162 $str = rtrim($str, "\0");
169 * Streams the data and maybe does something to it
171 * @param $data The data (string mostly) to "stream"
172 * @return $data The data (string mostly) to "stream"
173 * @throws UnsupportedOperationException If this method is called (which is a mistake)
175 public function streamData ($data) {
176 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
177 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);