]> git.mxchange.org Git - friendica-addons.git/blobdiff - securemail/php-gpg/libs/GPG.php
New experimental plugin 'secure mail'
[friendica-addons.git] / securemail / php-gpg / libs / GPG.php
diff --git a/securemail/php-gpg/libs/GPG.php b/securemail/php-gpg/libs/GPG.php
new file mode 100644 (file)
index 0000000..053a54b
--- /dev/null
@@ -0,0 +1,186 @@
+<?php\r
+/** @package    php-gpg */\r
+\r
+/** require supporting files */\r
+require_once("GPG/Expanded_Key.php");\r
+require_once("GPG/Public_Key.php");\r
+require_once("GPG/AES.php");\r
+require_once("GPG/globals.php");\r
+\r
+/**\r
+ * Pure PHP implementation of PHP/GPG encryption.  \r
+ * Supports RSA, DSA public key length of 2,4,8,16,512,1024,2048 or 4096\r
+ * Currently supports only encrypt\r
+ *\r
+ * @package php-gpg::Encryption\r
+ * @link http://www.verysimple.com/\r
+ * @copyright 1997-2012 VerySimple, Inc.\r
+ * @license http://www.gnu.org/licenses/gpl.html  GPL\r
+ * @todo implement decryption\r
+ * @version 1.1\r
+ * \r
+ * @example \r
+ *             require_once 'libs/GPG.php';\r
+ *             $gpg = new GPG();\r
+ *             $pub_key = new GPG_Public_Key($public_key_ascii);\r
+ *             $encrypted = $gpg->encrypt($pub_key,$plain_text_string);\r
+ */\r
+class GPG \r
+{\r
+\r
+       private $width = 16;\r
+       private $el = array(3, 5, 9, 17, 513, 1025, 2049, 4097);\r
+       private $version = "1.4.7";\r
+\r
+       private function gpg_encrypt($key, $text) {\r
+\r
+               $i = 0;\r
+               $i = 0;\r
+               $len = strlen($text);\r
+               $len = strlen($text);\r
+               $iblock = array_fill(0, $this->width, 0);\r
+               $rblock = array_fill(0, $this->width, 0);\r
+               $ct = array_fill(0, $this->width + 2, 0);\r
+        \r
+               $cipher = "";\r
+\r
+               if($len % $this->width) {\r
+                       for($i = ($len % $this->width); $i < $this->width; $i++) $text .= "\0";\r
+               }\r
+        \r
+               $ekey = new Expanded_Key($key);\r
+\r
+               for($i = 0; $i < $this->width; $i++) {\r
+                       $iblock[$i] = 0;\r
+                       $rblock[$i] = GPG_Utility::c_random();\r
+               }\r
+\r
+\r
+               $iblock = GPG_AES::encrypt($iblock, $ekey);\r
+               for($i = 0; $i < $this->width; $i++) {\r
+                       $ct[$i] = ($iblock[$i] ^= $rblock[$i]);\r
+               }\r
+\r
+               $iblock = GPG_AES::encrypt($iblock, $ekey);\r
+               $ct[$this->width]   = ($iblock[0] ^ $rblock[$this->width - 2]);\r
+               $ct[$this->width + 1] = ($iblock[1] ^ $rblock[$this->width - 1]);\r
+        \r
+               for($i = 0; $i < $this->width + 2; $i++) $cipher .= chr($ct[$i]);\r
+\r
+               $iblock = array_slice($ct, 2, $this->width + 2);\r
+\r
+               for($n = 0; $n < strlen($text); $n += $this->width) {\r
+                       $iblock = GPG_AES::encrypt($iblock, $ekey);\r
+                       for($i = 0; $i < $this->width; $i++) {\r
+                               $iblock[$i] ^= ord($text[$n + $i]);\r
+                               $cipher .= chr($iblock[$i]);\r
+                       }\r
+               }\r
+        \r
+               return substr($cipher, 0, $len + $this->width + 2);\r
+       }\r
+\r
+       private function gpg_header($tag, $len)\r
+       {\r
+               if ($len > 0xff) $tag += 1;\r
+               $h = chr($tag);\r
+               if ($len > 0xff) $h .= chr($len / 0x100);\r
+               $h .= chr($len % 0x100);\r
+\r
+               return $h;\r
+       }\r
+\r
+       private function gpg_session($key_id, $key_type, $session_key, $public_key)\r
+       { \r
+\r
+               $mod = array();\r
+               $exp = array();\r
+               $enc = "";\r
+        \r
+               $s = base64_decode($public_key);\r
+               $l = floor((ord($s[0]) * 256 + ord($s[1]) + 7) / 8);\r
+               $mod = mpi2b(substr($s, 0, $l + 2));\r
+               if($key_type) {\r
+                       $grp = array();\r
+                       $y = array();\r
+                       $B = array();\r
+                       $C = array();\r
+\r
+                       $l2 = floor((ord($s[$l + 2]) * 256 + ord($s[$l + 3]) + 7) / 8) + 2;\r
+                       $grp = mpi2b(substr($s, $l + 2, $l2));\r
+                       $y = mpi2b(substr($s, $l + 2 + $l2));\r
+                       $exp[0] = $this->el[GPG_Utility::c_random() & 7];\r
+                       $B = bmodexp($grp, $exp, $mod);\r
+                       $C = bmodexp($y, $exp, $mod);\r
+               } else {\r
+                       $exp = mpi2b(substr($s, $l + 2));\r
+               }\r
+\r
+               $c = 0;\r
+               $lsk = strlen($session_key);\r
+               for($i = 0; $i < $lsk; $i++) $c += ord($session_key[$i]);\r
+               $c &= 0xffff;\r
+\r
+               $lm = ($l - 2) * 8 + 2;\r
+               $m = chr($lm / 256) . chr($lm % 256) .\r
+                       chr(2) . GPG_Utility::s_random($l - $lsk - 6, 1) . "\0" .\r
+                       chr(7) . $session_key .\r
+                       chr($c / 256) . chr($c & 0xff);\r
+\r
+               if($key_type) {\r
+                       $enc = b2mpi($B) . b2mpi(bmod(bmul(mpi2b($m), $C), $mod));\r
+                       return $this->gpg_header(0x84,strlen($enc) + 10) .\r
+                               chr(3) . $key_id . chr(16) . $enc;\r
+               } else {\r
+                       $enc = b2mpi(bmodexp(mpi2b($m), $exp, $mod));\r
+                       return $this->gpg_header(0x84, strlen($enc) + 10) .\r
+                               chr(3) . $key_id . chr(1) . $enc;\r
+               }\r
+       }\r
+\r
+       private function gpg_literal($text)\r
+       {\r
+               if (strpos($text, "\r\n") === false)\r
+                       $text = str_replace("\n", "\r\n", $text);\r
+\r
+               return\r
+               $this->gpg_header(0xac, strlen($text) + 10) . "t" .\r
+                       chr(4) . "file\0\0\0\0" . $text;\r
+       }\r
+\r
+       private function gpg_data($key, $text)\r
+       {\r
+               $enc = $this->gpg_encrypt($key, $this->gpg_literal($text));\r
+               return $this->gpg_header(0xa4, strlen($enc)) . $enc;\r
+       }\r
+\r
+       /**\r
+        * GPG Encypts a message to the provided public key\r
+        *\r
+        * @param GPG_Public_Key $pk\r
+        * @param string $plaintext\r
+        * @return string encrypted text\r
+        */\r
+       function encrypt($pk, $plaintext)\r
+       {\r
+               // normalize the public key\r
+               $key_id = $pk->GetKeyId();\r
+               $key_type = $pk->GetKeyType();\r
+               $public_key = $pk->GetPublicKey();\r
+\r
+               $session_key = GPG_Utility::s_random($this->width, 0);\r
+               $key_id = GPG_Utility::hex2bin($key_id);\r
+               $cp = $this->gpg_session($key_id, $key_type, $session_key, $public_key) .\r
+                       $this->gpg_data($session_key, $plaintext);\r
+\r
+               $code = base64_encode($cp);\r
+               $code = wordwrap($code, 60, "\n", 1);\r
+\r
+               return\r
+                       "-----BEGIN PGP MESSAGE-----\nVersion: VerySimple PHP-GPG v".$this->version."\n\n" .\r
+                       $code . "\n=" . base64_encode(GPG_Utility::crc24($cp)) .\r
+                       "\n-----END PGP MESSAGE-----\n";\r
+       }\r
+}\r
+\r
+?>
\ No newline at end of file