{"action":"created","comment":{"url":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/comments/136767412","pull_request_review_id":60350619,"id":136767412,"diff_hunk":"@@ -1,21 +1,91 @@\n-import pyaes\n+from ctypes.util import find_library\n+lib = find_library('ssl')\n \n+if not lib:\n+    # libssl.so not found; use pyaes\n+    import pyaes\n \n-class AESModeCTR:\n-    \"\"\"Wrapper around pyaes.AESModeOfOperationCTR mode with custom IV\"\"\"\n-    # TODO Maybe make a pull request to pyaes to support iv on CTR\n+    class AESModeCTR:\n+        \"\"\"Wrapper around pyaes.AESModeOfOperationCTR mode with custom IV\"\"\"\n+        # TODO Maybe make a pull request to pyaes to support iv on CTR\n \n-    def __init__(self, key, iv):\n-        # TODO Use libssl if available\n-        assert isinstance(key, bytes)\n-        self._aes = pyaes.AESModeOfOperationCTR(key)\n+        def __init__(self, key, iv):\n+            # TODO Use libssl if available\n+            assert isinstance(key, bytes)\n+            self._aes = pyaes.AESModeOfOperationCTR(key)\n \n-        assert isinstance(iv, bytes)\n-        assert len(iv) == 16\n-        self._aes._counter._counter = list(iv)\n+            assert isinstance(iv, bytes)\n+            assert len(iv) == 16\n+            self._aes._counter._counter = list(iv)\n \n-    def encrypt(self, data):\n-        return self._aes.encrypt(data)\n+        def encrypt(self, data):\n+            return self._aes.encrypt(data)\n \n-    def decrypt(self, data):\n-        return self._aes.decrypt(data)\n+        def decrypt(self, data):\n+            return self._aes.decrypt(data)\n+\n+else:\n+\n+    _libssl = ctypes.cdll.LoadLibrary(lib)\n+\n+    AES_MAXNR = 14\n+    class AES_KEY(ctypes.Structure):\n+        _fields_ = [\n+            #uint32_t rd_key[4 * (AES_MAXNR + 1)];\n+            ('rd_key', ctypes.c_uint32 * (4*(AES_MAXNR + 1))),\n+            ('rounds', ctypes.c_uint),\n+        ]\n+\n+\n+    class AESModeCTR:\n+        \"\"\"Wrapper around pyaes.AESModeOfOperationCTR mode with custom IV\"\"\"\n+\n+        def __init__(self, key, iv):\n+            assert isinstance(key, bytes)\n+            self.aeskey = AES_KEY()\n+            self.liv = len(iv)\n+            self.civ = (ctypes.c_ubyte * self.liv)(*iv)\n+            ckey = (ctypes.c_ubyte * len(key))(*key)\n+            cklen = ctypes.c_int(len(key)*8)\n+            _libssl.AES_set_encrypt_key(ckey, cklen,  ctypes.byref(self.aeskey))\n+\n+        def encrypt(self, data):\n+\n+            cin = (ctypes.c_ubyte * len(plain))(*data)\n+            ctlen = ctypes.c_size_t(len(data))\n+            cout = (ctypes.c_ubyte * len(data))()\n+            ccount = (ctypes.c_ubyte * self.liv)(*(b'\\x00'*self.liv))\n+            cnum = ctypes.c_uint(0)\n+\n+            _libssl.AES_ctr128_encrypt(\n+                ctypes.byref(cin), ctypes.byref(cout),\n+                ctlen,  ctypes.byref(aeskey),","path":"telethon/crypto/aes_ctr.py","position":77,"original_position":77,"commit_id":"9ad42a1e41fb6e497baa7f13a27823b491b33444","original_commit_id":"9ad42a1e41fb6e497baa7f13a27823b491b33444","user":{"login":"Lonami","id":6297805,"avatar_url":"https://avatars0.githubusercontent.com/u/6297805?v=4","gravatar_id":"","url":"https://api.github.com/users/Lonami","html_url":"https://github.com/Lonami","followers_url":"https://api.github.com/users/Lonami/followers","following_url":"https://api.github.com/users/Lonami/following{/other_user}","gists_url":"https://api.github.com/users/Lonami/gists{/gist_id}","starred_url":"https://api.github.com/users/Lonami/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Lonami/subscriptions","organizations_url":"https://api.github.com/users/Lonami/orgs","repos_url":"https://api.github.com/users/Lonami/repos","events_url":"https://api.github.com/users/Lonami/events{/privacy}","received_events_url":"https://api.github.com/users/Lonami/received_events","type":"User","site_admin":false},"body":"`self.aeskey`","created_at":"2017-09-04T08:24:55Z","updated_at":"2017-09-04T08:24:56Z","html_url":"https://github.com/LonamiWebs/Telethon/pull/223#discussion_r136767412","pull_request_url":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/223","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/comments/136767412"},"html":{"href":"https://github.com/LonamiWebs/Telethon/pull/223#discussion_r136767412"},"pull_request":{"href":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/223"}}},"pull_request":{"url":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/223","id":139058099,"html_url":"https://github.com/LonamiWebs/Telethon/pull/223","diff_url":"https://github.com/LonamiWebs/Telethon/pull/223.diff","patch_url":"https://github.com/LonamiWebs/Telethon/pull/223.patch","issue_url":"https://api.github.com/repos/LonamiWebs/Telethon/issues/223","number":223,"state":"open","locked":false,"title":"Add libssl interface for AES CTR","user":{"login":"feodoran","id":29159589,"avatar_url":"https://avatars3.githubusercontent.com/u/29159589?v=4","gravatar_id":"","url":"https://api.github.com/users/feodoran","html_url":"https://github.com/feodoran","followers_url":"https://api.github.com/users/feodoran/followers","following_url":"https://api.github.com/users/feodoran/following{/other_user}","gists_url":"https://api.github.com/users/feodoran/gists{/gist_id}","starred_url":"https://api.github.com/users/feodoran/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/feodoran/subscriptions","organizations_url":"https://api.github.com/users/feodoran/orgs","repos_url":"https://api.github.com/users/feodoran/repos","events_url":"https://api.github.com/users/feodoran/events{/privacy}","received_events_url":"https://api.github.com/users/feodoran/received_events","type":"User","site_admin":false},"body":"As discussed in https://github.com/LonamiWebs/Telethon/pull/204 this will interface AES CTR with `libssl`.\r\n\r\nI could not test this with Telethon, as I have no actual example.","created_at":"2017-09-03T19:07:40Z","updated_at":"2017-09-04T08:24:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e438f5aa6536f2672a205dedd64b841b0bbbc976","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/223/commits","review_comments_url":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/223/comments","review_comment_url":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/comments{/number}","comments_url":"https://api.github.com/repos/LonamiWebs/Telethon/issues/223/comments","statuses_url":"https://api.github.com/repos/LonamiWebs/Telethon/statuses/9ad42a1e41fb6e497baa7f13a27823b491b33444","head":{"label":"feodoran:aes_ctr","ref":"aes_ctr","sha":"9ad42a1e41fb6e497baa7f13a27823b491b33444","user":{"login":"feodoran","id":29159589,"avatar_url":"https://avatars3.githubusercontent.com/u/29159589?v=4","gravatar_id":"","url":"https://api.github.com/users/feodoran","html_url":"https://github.com/feodoran","followers_url":"https://api.github.com/users/feodoran/followers","following_url":"https://api.github.com/users/feodoran/following{/other_user}","gists_url":"https://api.github.com/users/feodoran/gists{/gist_id}","starred_url":"https://api.github.com/users/feodoran/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/feodoran/subscriptions","organizations_url":"https://api.github.com/users/feodoran/orgs","repos_url":"https://api.github.com/users/feodoran/repos","events_url":"https://api.github.com/users/feodoran/events{/privacy}","received_events_url":"https://api.github.com/users/feodoran/received_events","type":"User","site_admin":false},"repo":{"id":94374892,"name":"Telethon","full_name":"feodoran/Telethon","owner":{"login":"feodoran","id":29159589,"avatar_url":"https://avatars3.githubusercontent.com/u/29159589?v=4","gravatar_id":"","url":"https://api.github.com/users/feodoran","html_url":"https://github.com/feodoran","followers_url":"https://api.github.com/users/feodoran/followers","following_url":"https://api.github.com/users/feodoran/following{/other_user}","gists_url":"https://api.github.com/users/feodoran/gists{/gist_id}","starred_url":"https://api.github.com/users/feodoran/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/feodoran/subscriptions","organizations_url":"https://api.github.com/users/feodoran/orgs","repos_url":"https://api.github.com/users/feodoran/repos","events_url":"https://api.github.com/users/feodoran/events{/privacy}","received_events_url":"https://api.github.com/users/feodoran/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/feodoran/Telethon","description":"Pure Python 3 Telegram client library","fork":true,"url":"https://api.github.com/repos/feodoran/Telethon","forks_url":"https://api.github.com/repos/feodoran/Telethon/forks","keys_url":"https://api.github.com/repos/feodoran/Telethon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/feodoran/Telethon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/feodoran/Telethon/teams","hooks_url":"https://api.github.com/repos/feodoran/Telethon/hooks","issue_events_url":"https://api.github.com/repos/feodoran/Telethon/issues/events{/number}","events_url":"https://api.github.com/repos/feodoran/Telethon/events","assignees_url":"https://api.github.com/repos/feodoran/Telethon/assignees{/user}","branches_url":"https://api.github.com/repos/feodoran/Telethon/branches{/branch}","tags_url":"https://api.github.com/repos/feodoran/Telethon/tags","blobs_url":"https://api.github.com/repos/feodoran/Telethon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/feodoran/Telethon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/feodoran/Telethon/git/refs{/sha}","trees_url":"https://api.github.com/repos/feodoran/Telethon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/feodoran/Telethon/statuses/{sha}","languages_url":"https://api.github.com/repos/feodoran/Telethon/languages","stargazers_url":"https://api.github.com/repos/feodoran/Telethon/stargazers","contributors_url":"https://api.github.com/repos/feodoran/Telethon/contributors","subscribers_url":"https://api.github.com/repos/feodoran/Telethon/subscribers","subscription_url":"https://api.github.com/repos/feodoran/Telethon/subscription","commits_url":"https://api.github.com/repos/feodoran/Telethon/commits{/sha}","git_commits_url":"https://api.github.com/repos/feodoran/Telethon/git/commits{/sha}","comments_url":"https://api.github.com/repos/feodoran/Telethon/comments{/number}","issue_comment_url":"https://api.github.com/repos/feodoran/Telethon/issues/comments{/number}","contents_url":"https://api.github.com/repos/feodoran/Telethon/contents/{+path}","compare_url":"https://api.github.com/repos/feodoran/Telethon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/feodoran/Telethon/merges","archive_url":"https://api.github.com/repos/feodoran/Telethon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/feodoran/Telethon/downloads","issues_url":"https://api.github.com/repos/feodoran/Telethon/issues{/number}","pulls_url":"https://api.github.com/repos/feodoran/Telethon/pulls{/number}","milestones_url":"https://api.github.com/repos/feodoran/Telethon/milestones{/number}","notifications_url":"https://api.github.com/repos/feodoran/Telethon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/feodoran/Telethon/labels{/name}","releases_url":"https://api.github.com/repos/feodoran/Telethon/releases{/id}","deployments_url":"https://api.github.com/repos/feodoran/Telethon/deployments","created_at":"2017-06-14T21:33:07Z","updated_at":"2017-06-14T21:33:08Z","pushed_at":"2017-09-03T19:04:18Z","git_url":"git://github.com/feodoran/Telethon.git","ssh_url":"git@github.com:feodoran/Telethon.git","clone_url":"https://github.com/feodoran/Telethon.git","svn_url":"https://github.com/feodoran/Telethon","homepage":"https://pypi.python.org/pypi/Telethon","size":1627,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"LonamiWebs:master","ref":"master","sha":"62a52679f4e3aa86113ae6565adaa48985f61cb0","user":{"login":"LonamiWebs","id":9887454,"avatar_url":"https://avatars3.githubusercontent.com/u/9887454?v=4","gravatar_id":"","url":"https://api.github.com/users/LonamiWebs","html_url":"https://github.com/LonamiWebs","followers_url":"https://api.github.com/users/LonamiWebs/followers","following_url":"https://api.github.com/users/LonamiWebs/following{/other_user}","gists_url":"https://api.github.com/users/LonamiWebs/gists{/gist_id}","starred_url":"https://api.github.com/users/LonamiWebs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LonamiWebs/subscriptions","organizations_url":"https://api.github.com/users/LonamiWebs/orgs","repos_url":"https://api.github.com/users/LonamiWebs/repos","events_url":"https://api.github.com/users/LonamiWebs/events{/privacy}","received_events_url":"https://api.github.com/users/LonamiWebs/received_events","type":"Organization","site_admin":false},"repo":{"id":66641037,"name":"Telethon","full_name":"LonamiWebs/Telethon","owner":{"login":"LonamiWebs","id":9887454,"avatar_url":"https://avatars3.githubusercontent.com/u/9887454?v=4","gravatar_id":"","url":"https://api.github.com/users/LonamiWebs","html_url":"https://github.com/LonamiWebs","followers_url":"https://api.github.com/users/LonamiWebs/followers","following_url":"https://api.github.com/users/LonamiWebs/following{/other_user}","gists_url":"https://api.github.com/users/LonamiWebs/gists{/gist_id}","starred_url":"https://api.github.com/users/LonamiWebs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LonamiWebs/subscriptions","organizations_url":"https://api.github.com/users/LonamiWebs/orgs","repos_url":"https://api.github.com/users/LonamiWebs/repos","events_url":"https://api.github.com/users/LonamiWebs/events{/privacy}","received_events_url":"https://api.github.com/users/LonamiWebs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/LonamiWebs/Telethon","description":"Pure Python 3 Telegram client library","fork":false,"url":"https://api.github.com/repos/LonamiWebs/Telethon","forks_url":"https://api.github.com/repos/LonamiWebs/Telethon/forks","keys_url":"https://api.github.com/repos/LonamiWebs/Telethon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/LonamiWebs/Telethon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/LonamiWebs/Telethon/teams","hooks_url":"https://api.github.com/repos/LonamiWebs/Telethon/hooks","issue_events_url":"https://api.github.com/repos/LonamiWebs/Telethon/issues/events{/number}","events_url":"https://api.github.com/repos/LonamiWebs/Telethon/events","assignees_url":"https://api.github.com/repos/LonamiWebs/Telethon/assignees{/user}","branches_url":"https://api.github.com/repos/LonamiWebs/Telethon/branches{/branch}","tags_url":"https://api.github.com/repos/LonamiWebs/Telethon/tags","blobs_url":"https://api.github.com/repos/LonamiWebs/Telethon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/LonamiWebs/Telethon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/LonamiWebs/Telethon/git/refs{/sha}","trees_url":"https://api.github.com/repos/LonamiWebs/Telethon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/LonamiWebs/Telethon/statuses/{sha}","languages_url":"https://api.github.com/repos/LonamiWebs/Telethon/languages","stargazers_url":"https://api.github.com/repos/LonamiWebs/Telethon/stargazers","contributors_url":"https://api.github.com/repos/LonamiWebs/Telethon/contributors","subscribers_url":"https://api.github.com/repos/LonamiWebs/Telethon/subscribers","subscription_url":"https://api.github.com/repos/LonamiWebs/Telethon/subscription","commits_url":"https://api.github.com/repos/LonamiWebs/Telethon/commits{/sha}","git_commits_url":"https://api.github.com/repos/LonamiWebs/Telethon/git/commits{/sha}","comments_url":"https://api.github.com/repos/LonamiWebs/Telethon/comments{/number}","issue_comment_url":"https://api.github.com/repos/LonamiWebs/Telethon/issues/comments{/number}","contents_url":"https://api.github.com/repos/LonamiWebs/Telethon/contents/{+path}","compare_url":"https://api.github.com/repos/LonamiWebs/Telethon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/LonamiWebs/Telethon/merges","archive_url":"https://api.github.com/repos/LonamiWebs/Telethon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/LonamiWebs/Telethon/downloads","issues_url":"https://api.github.com/repos/LonamiWebs/Telethon/issues{/number}","pulls_url":"https://api.github.com/repos/LonamiWebs/Telethon/pulls{/number}","milestones_url":"https://api.github.com/repos/LonamiWebs/Telethon/milestones{/number}","notifications_url":"https://api.github.com/repos/LonamiWebs/Telethon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/LonamiWebs/Telethon/labels{/name}","releases_url":"https://api.github.com/repos/LonamiWebs/Telethon/releases{/id}","deployments_url":"https://api.github.com/repos/LonamiWebs/Telethon/deployments","created_at":"2016-08-26T10:59:24Z","updated_at":"2017-09-04T03:09:25Z","pushed_at":"2017-09-03T19:07:41Z","git_url":"git://github.com/LonamiWebs/Telethon.git","ssh_url":"git@github.com:LonamiWebs/Telethon.git","clone_url":"https://github.com/LonamiWebs/Telethon.git","svn_url":"https://github.com/LonamiWebs/Telethon","homepage":"https://lonamiwebs.github.io/Telethon","size":1750,"stargazers_count":268,"watchers_count":268,"language":"Python","has_issues":true,"has_projects":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":59,"mirror_url":null,"open_issues_count":19,"forks":59,"open_issues":19,"watchers":268,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/223"},"html":{"href":"https://github.com/LonamiWebs/Telethon/pull/223"},"issue":{"href":"https://api.github.com/repos/LonamiWebs/Telethon/issues/223"},"comments":{"href":"https://api.github.com/repos/LonamiWebs/Telethon/issues/223/comments"},"review_comments":{"href":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/223/comments"},"review_comment":{"href":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/LonamiWebs/Telethon/pulls/223/commits"},"statuses":{"href":"https://api.github.com/repos/LonamiWebs/Telethon/statuses/9ad42a1e41fb6e497baa7f13a27823b491b33444"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"Lonami"}}