Copyright updated
[core.git] / inc / classes / main / streams / crypto / class_McryptStream.php
1 <?php
2 /**
3  * A mcrypt-based encryption stream
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
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 General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class McryptStream extends BaseStream implements EncryptableStream {
25         /**
26          * Separator on many places
27          */
28         const DATA_PAYLOAD_SEPARATOR = '|';
29
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Creates an instance of this node class
42          *
43          * @param       $rngInstance            An RNG instance
44          * @return      $streamInstance         An instance of this node class
45          */
46         public static final function createMcryptStream (RandomNumberGenerator $rngInstance) {
47                 // Get a new instance
48                 $streamInstance = new McryptStream();
49
50                 // Set the RNG instance
51                 $streamInstance->setRngInstance($rngInstance);
52
53                 // Return the instance
54                 return $streamInstance;
55         }
56
57         /**
58          * Encrypt the string with fixed salt
59          *
60          * @param       $str            The unencrypted string
61          * @param       $key            Optional key, if none provided, a random key will be generated
62          * @return      $encrypted      Encrypted string
63          */
64         public function encryptStream ($str, $key = NULL) {
65                 // Init crypto module
66                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
67                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
68
69                 // Generate key, if none provided
70                 if (is_null($key)) {
71                         // None provided
72                         $key = $this->getRngInstance()->generateKey();
73                 } // END - if
74
75                 // Add some "payload" to the string
76                 switch ($this->getRngInstance()->randomNumber(0, 8)) {
77                         case 0:
78                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
79                                 break;
80
81                         case 1:
82                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
83                                 break;
84
85                         case 2:
86                                 $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
87                                 break;
88
89                         case 3:
90                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
91                                 break;
92
93                         case 4:
94                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
95                                 break;
96
97                         case 5:
98                                 $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
99                                 break;
100
101                         case 6:
102                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . crc32($this->getRngInstance()->randomString(20));
103                                 break;
104
105                         case 7:
106                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . md5($this->getRngInstance()->randomString(20));
107                                 break;
108
109                         case 8:
110                                 $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPARATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPARATOR . sha1($this->getRngInstance()->randomString(20));
111                                 break;
112                 }
113
114                 // Encrypt the string
115                 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
116
117                 // Return the string
118                 return $encrypted;
119         }
120
121         /**
122          * Decrypt the string with fixed salt
123          *
124          * @param       $encrypted      Encrypted string
125          * @param       $key            Optional key, if none provided, a random key will be generated
126          * @return      $str            The unencrypted string
127          */
128         public function decryptStream ($encrypted, $key = NULL) {
129                 // Init crypto module
130                 $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
131                 $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
132
133                 // Shall we use a default key or custom?
134                 if (is_null($key)) {
135                         // Generate (default) key
136                         $key = $this->getRngInstance()->generateKey();
137                 } // END - if
138
139                 // Decrypt the string
140                 $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
141
142                 // Get the real string out
143                 $strArray = explode(self::DATA_PAYLOAD_SEPARATOR, $payloadString);
144
145                 // Does the element count match?
146                 assert(count($strArray) == 3);
147
148                 // Decode the string
149                 $str = base64_decode($strArray[1]);
150
151                 // Trim trailing nulls away
152                 $str = rtrim($str, "\0");
153
154                 // Return the string
155                 return $str;
156         }
157
158         /**
159          * Streams the data and maybe does something to it
160          *
161          * @param       $data   The data (string mostly) to "stream"
162          * @return      $data   The data (string mostly) to "stream"
163          * @throws      UnsupportedOperationException   If this method is called (which is a mistake)
164          */
165         public function streamData ($data) {
166                 $this->debugOutput('Unhandled ' . strlen($data) . ' bytes in this stream.');
167                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
168         }
169 }
170
171 // [EOF]
172 ?>