3 namespace CoreFramework\Stream\Crypto;
5 // Import framework stuff
6 use CoreFramework\Crypto\RandomNumber\RandomNumberGenerator;
7 use CoreFramework\Stream\BaseStream;
10 * An OpenSSL-based encryption stream
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class OpenSslStream extends BaseStream implements EncryptableStream {
33 * Protected constructor
37 protected function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Creates an instance of this node class
45 * @param $rngInstance An RNG instance
46 * @return $streamInstance An instance of this node class
48 public static final function createOpenSslStream (RandomNumberGenerator $rngInstance) {
50 $streamInstance = new OpenSslStream();
52 // Set the RNG instance
53 $streamInstance->setRngInstance($rngInstance);
55 // Return the instance
56 return $streamInstance;
60 * Encrypt the string with fixed salt
62 * @param $str The unencrypted string
63 * @param $key Optional key, if none provided, a random key will be generated
64 * @return $encrypted Encrypted string
66 public function encryptStream ($str, $key = NULL) {
71 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: key[' . gettype($key) . ']=' . $key);
74 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
75 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
77 // Generate key, if none provided
80 $key = $this->getRngInstance()->generateKey();
83 // Add some "payload" to the string
84 switch ($this->getRngInstance()->randomNumber(0, 8)) {
86 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
90 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
94 $payloadString = crc32($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
98 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
102 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
106 $payloadString = md5($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
110 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
114 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
118 $payloadString = sha1($this->getRngInstance()->randomString(10)) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . EncryptableStream::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
122 // Encrypt the string
123 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
130 * Decrypt the string with fixed salt
132 * @param $encrypted Encrypted string
133 * @param $key Optional key, if none provided, a random key will be generated
134 * @return $str The unencrypted string
136 public function decryptStream ($encrypted, $key = NULL) {
140 // Init crypto module
141 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
142 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
144 // Shall we use a default key or custom?
146 // Generate (default) key
147 $key = $this->getRngInstance()->generateKey();
150 // Decrypt the string
151 $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
153 // Get the real string out
154 $strArray = explode(EncryptableStream::DATA_PAYLOAD_SEPARATOR, $payloadString);
156 // Does the element count match?
157 assert(count($strArray) == 3);
160 $str = base64_decode($strArray[1]);
162 // Trim trailing nulls away
163 $str = rtrim($str, "\0");
170 * Streams the data and maybe does something to it
172 * @param $data The data (string mostly) to "stream"
173 * @return $data The data (string mostly) to "stream"
174 * @throws UnsupportedOperationException If this method is called (which is a mistake)
176 public function streamData ($data) {
177 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
178 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);